From a4a39e6ac8b19513288b6db06ecb449a6802adee Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Wed, 1 Jan 2025 16:47:12 +0100 Subject: [PATCH] map: rooms are now generate in a basic recursive manner Rooms can generate until a pre-set max is reached They alwys link north-south for now. --- src/map.s | 58 +++++++++++++++++++++++++++++++++--------------------- src/wram.s | 2 ++ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/map.s b/src/map.s index 20f004c..81218f9 100644 --- a/src/map.s +++ b/src/map.s @@ -26,6 +26,9 @@ #define TWALLD1 0x4A #define TWALLL1 0x4C + ; count of all pointers in base room table +#define BASE_ROOM_TABLE_LEN ((base_room_table_end - base_room_table) / 2) + ; maps: ; a map is a collection of room structs @@ -486,6 +489,7 @@ map_generate: ; basic mapgen setup xor a, a ld [mapgen_depth], a + ld [mapgen_total], a ld d, ROOMS_TOTAL ; d = loop counter ld hl, map0 ; hl = current room @@ -497,8 +501,6 @@ map_generate: ; inputs: ; hl: current room ; de: previous room - ; a: 0 == stop recursion - ; a: 1 == continue recurions map_gen_next: push de ; save previous room push hl ; save current room @@ -508,16 +510,24 @@ map_gen_next: inc a ld [mapgen_depth], a + ld a, [mapgen_total] + inc a + ld [mapgen_total], a + ; select a room ; and copy it to the current position ; linking back NULL on all exits initially ; load base room ptr - ; TODO begin test - ; TODO we are simply loading base_room_header every time - ; TODO for now we are just copying room1 and room2 and linking them + ; select a room randomly from the table + call rand ld hl, base_room_table + and a, BASE_ROOM_TABLE_LEN + ld b, 0 + ld c, a + add hl, bc ; base + random offset + ld a, [hl+] ld e, a ld a, [hl] @@ -527,28 +537,26 @@ map_gen_next: ; now we copy call map_gen_copy_base_room - ; hl = next room + ; hl = next room l + + ; check if we're done generating + ld a, [mapgen_total] + cp a, ROOMS_TOTAL - 1 + jp z, @gen_done + + ; if not done link to next room + pop de ; de = previous room ptr push de ; save it again ld a, SOUTH ; link south for now push hl call map_gen_link_rooms - - ld hl, base_room_table - ld b, 0 - ld c, 2 ; add 2 to load next base room - add hl, bc - - ld a, [hl+] - ld e, a - ld a, [hl] - ld d, a + ; hl = current room pop hl - - call map_gen_copy_base_room - - ; TODO end test + pop de ; de = previous room + push de ; push again for later + call map_gen_next ; select a direction to walk into and call again ; and set up pointers @@ -569,6 +577,14 @@ map_gen_next: pop de ret +@gen_done: + ; clean up the stack + pop hl + pop hl + pop hl + pop hl + pop hl + ret ; lut for reverse direction @@ -797,8 +813,6 @@ base_room_table: dw base_room2_header base_room_table_end: - ; count of all pointers in base room table -#define BASE_ROOM_TABLE_LEN ((base_room_table_end - base_room_table) / 2) ; exit table for empty exits room_empty_exits: diff --git a/src/wram.s b/src/wram.s index d355026..e577eaf 100644 --- a/src/wram.s +++ b/src/wram.s @@ -275,3 +275,5 @@ ct_mask: .adv 1 ; current depth ; used for recursion in mapgen mapgen_depth: .adv 1 + ; counts how many rooms we have generated already +mapgen_total: .adv 1 -- 2.30.2