From e9d390024dbedceac8f8c10c75179d985ef0d78f Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 2 Jan 2025 13:19:14 +0100 Subject: [PATCH] map: Rooms now dynamically add doors 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 | 4 +- maps/base_room2.s | 4 +- src/map.s | 92 +++++++++++++++++++++++++++++++++++++++++++++- src/mem.s | 22 +++++++++++ tiles/tileset1.inc | 80 ++++++++++++++++++++-------------------- 5 files changed, 156 insertions(+), 46 deletions(-) diff --git a/maps/base_room.s b/maps/base_room.s index 028afbf..dab76f7 100644 --- a/maps/base_room.s +++ b/maps/base_room.s @@ -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, 0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 0x1, 0x1 diff --git a/maps/base_room2.s b/maps/base_room2.s index 4a14eec..810a460 100644 --- a/maps/base_room2.s +++ b/maps/base_room2.s @@ -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 diff --git a/src/map.s b/src/map.s index 81218f9..b096b68 100644 --- 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: diff --git a/src/mem.s b/src/mem.s index 09c90c1..7e24aab 100644 --- 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 diff --git a/tiles/tileset1.inc b/tiles/tileset1.inc index a9f349b..ef4191e 100644 --- a/tiles/tileset1.inc +++ b/tiles/tileset1.inc @@ -1592,14 +1592,14 @@ .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 @@ -1853,23 +1853,23 @@ .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 @@ -1997,23 +1997,23 @@ .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 -- 2.30.2