map: rooms are now generate in a basic recursive manner
authorLukas Krickl <lukas@krickl.dev>
Wed, 1 Jan 2025 15:47:12 +0000 (16:47 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 1 Jan 2025 15:47:12 +0000 (16:47 +0100)
Rooms can generate until a pre-set max is reached
They alwys link north-south for now.

src/map.s
src/wram.s

index 20f004c5d7cead0c73c7930917ed71cc48ae0428..81218f9688917a706b5f975759d5b92c74dd37d4 100644 (file)
--- 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:
index d355026deec39fd9ae4eef50822225ace53badbb..e577eaf7d35aeedb1a8add20952175e17d96e203 100644 (file)
@@ -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