map: Rooms now dynamically add doors
authorLukas Krickl <lukas@krickl.dev>
Thu, 2 Jan 2025 12:19:14 +0000 (13:19 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 2 Jan 2025 12:19:14 +0000 (13:19 +0100)
This is done by writing the flags and tiles when linking.
Also added a memcpy that will skip copying if the value is not zero.

maps/base_room.s
maps/base_room2.s
src/map.s
src/mem.s
tiles/tileset1.inc

index 028afbf8e7415ae1dc6a2493316d4639efe18117..dab76f7afdb8fd08ca20aacfb79cb65d0863576e 100644 (file)
@@ -9,6 +9,6 @@ base_room_actors:
 .db  1 , 64 , 72 , 0
 .db  1 , 48 , 56 , 0
 base_room_bg:
-.db 0x60, 0x62, 0x62, 0x64, 0x40, 0x40, 0x66, 0x62, 0x62, 0x68, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x46, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x48, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe2, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x6c, 0x4a, 0x4a, 0x42, 0x40, 0x40, 0x44, 0x4a, 0x4a, 0x6e
+.db 0x60, 0x62, 0x62, 0x64, 0x62, 0x62, 0x66, 0x62, 0x62, 0x68, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x46, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe0, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x48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100644 (file)
@@ -9,6 +9,6 @@ base_room2_actors:
 .db  1 , 64 , 72 , 0
 .db  1 , 48 , 56 , 0
 base_room2_bg:
-.db 0x60, 0x62, 0x62, 0x64, 0x40, 0x40, 0x66, 0x62, 0x62, 0x68, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x46, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe0, 0x40, 0x40, 0x40, 0x40, 0x60, 0x68, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6c, 0x6e, 0x40, 0x40, 0x40, 0x40, 0x48, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe2, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x6c, 0x4a, 0x4a, 0x42, 0x40, 0x40, 0x44, 0x4a, 0x4a, 0x6e
+.db 0x60, 0x62, 0x62, 0x64, 0x62, 0x62, 0x66, 0x62, 0x62, 0x68, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x46, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe0, 0x4c, 0x40, 0x40, 0x40, 0x4e, 0x4e, 0x40, 0x40, 0x40, 0x6a, 0x4c, 0x40, 0x40, 0x40, 0x4e, 0x4e, 0x40, 0x40, 0x40, 0x6a, 0x48, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xe2, 0x4c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x6a, 0x6c, 0x4a, 0x4a, 0x42, 0x4a, 0x4a, 0x44, 0x4a, 0x4a, 0x6e
 base_room2_flags:
-.db 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2, 0x2, 0x1, 0x1, 0x1, 0x1
+.db 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1
index 81218f9688917a706b5f975759d5b92c74dd37d4..b096b68d32320e94ff3e0d5b58ded0a4309ea068 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -26,6 +26,8 @@
 #define TWALLD1 0x4A
 #define TWALLL1 0x4C
 
+#define TEXIT1 0x4E
+
   ;  count of all pointers in base room table
 #define BASE_ROOM_TABLE_LEN ((base_room_table_end - base_room_table) / 2)
 
@@ -624,6 +626,8 @@ map_gen_link_rooms:
   ;  hl: source roomb header
   ;  de: target roomb header 
 map_gen_link_room_to:
+  call map_gen_fix_room_door
+
   ; skip hl and de to the exit table location 
   ld bc, roomb_size - ROOM_EXIT_TABLE_SIZE 
   add hl, bc ; hl = source exit table 
@@ -642,6 +646,90 @@ map_gen_link_room_to:
 
   ret
 
+  ; fixes a room's wall by placing a door in the desired position
+  ; and flagging the wall as a door instead 
+  ; inputs:
+  ;   a: direction
+  ;  hl: source roomb header
+  ; registers are preserved 
+map_gen_fix_room_door:
+  push hl
+  push af
+  push de
+
+  ld bc, room_size 
+  add hl, bc ; hl = tile map
+
+  ; need different logic based on direction
+  cp a, NORTH
+  jr nz, @not_north REL
+    ld d, 0
+    ld e, 0x04
+    add hl, de ; go to door tile
+    
+    ld a, TEXIT1
+    ld [hl+], a
+    ld [hl], a
+
+    dec hl ; hl is back to tile 
+    ; now set flags
+    
+    ld de, ROOM_TILES_SIZE
+    add hl, de ; hl = flags area 
+    
+    ld a, RF_DOOR 
+    ld [hl+], a
+    ld [hl], a
+
+    jp @done 
+@not_north:
+
+  cp a, SOUTH
+  jr nz, @not_south REL
+
+    ld de, ROOM_TILES_SIZE - 6 
+    add hl, de ; go to door tile
+    
+    ld a, TEXIT1
+    ld [hl+], a
+    ld [hl], a
+
+    dec hl ; hl is back to tile 
+    ; now set flags
+    
+    ld de, ROOM_TILES_SIZE
+    add hl, de ; hl = flags area 
+    
+    ld a, RF_DOOR 
+    ld [hl+], a
+    ld [hl], a
+
+    jp @done 
+@not_south:
+
+  cp a, EAST
+  jr nz, @not_east REL
+    
+  ; TODO
+
+    jp @done
+@not_east:
+
+  cp a, WEST
+  jr nz, @not_west REL
+  
+  ; TODO
+
+    ; no need to jp @done here
+@not_west:
+
+@done:
+  pop de
+  pop af
+  pop hl
+  ret
+
 
   ; copies a base room
   ; sets all exits to 0000 (null ptr)
@@ -698,7 +786,7 @@ map_gen_copy_base_room:
 
   pop hl ; get target back
   ld bc, ROOM_TILES_SIZE
-  call memcpy
+  call memcpyz
   push hl ; save hl again just like before 
 
 @copy_flags:
@@ -733,7 +821,7 @@ map_gen_copy_base_room:
 
   ; copy flags
   ld bc, ROOM_FLAGS_SIZE
-  call memcpy
+  call memcpyz
   push hl ; save hl again 
 
 @copy_actors:
index 09c90c18ecb6d101b424f9a86905d44be7f9ef82..7e24aabe73d999868377c5096e4a72faa790652a 100644 (file)
--- a/src/mem.s
+++ b/src/mem.s
@@ -47,6 +47,28 @@ memcpy:
 
   ret
 
+  ; same as memcpy
+  ; but only performs a write
+  ; if the value is currently 0
+memcpyz:
+@loop:
+  ; skip check
+  ld a, [hl]
+  cp a, 0
+  jr nz, @skip REL
+
+  ld a, [de]
+  ld [hl], a
+@skip:
+  inc hl
+  inc de
+  dec bc
+  ld a, b
+  or a, c
+  jp nz, @loop
+
+  ret
+
   ; sets a memory location to a specific value 
   ; inputs:
   ;   hl: destination
index a9f349bbd2434db9ae264c874a54778a17f9dc8e..ef4191e81576091af2cc84f11f42c350eb4c6212 100644 (file)
 .chr 00000000
 .chr 00000000
 ; tile 177
-.chr 00000000
-.chr 00100000
-.chr 00000100
-.chr 00010000
-.chr 00000000
-.chr 00100000
-.chr 00000100
-.chr 10000000
+.chr 11111111
+.chr 11111111
+.chr 11111111
+.chr 11111111
+.chr 11111111
+.chr 11111111
+.chr 11111111
+.chr 11111111
 ; tile 178
 .chr 33333333
 .chr 33333333
 .chr 31122123
 .chr 32122113
 ; tile 206
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
 ; tile 207
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
 ; tile 208
 .chr 30222222
 .chr 30222222
 .chr 32122123
 .chr 32122113
 ; tile 222
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
 ; tile 223
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
-.chr 00000000
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
+.chr 22222222
 ; tile 224
 .chr 33333333
 .chr 33322222