map: Added ability to re-roll exit if direction is already taken
authorLukas Krickl <lukas@krickl.dev>
Thu, 2 Jan 2025 20:01:36 +0000 (21:01 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 2 Jan 2025 20:01:36 +0000 (21:01 +0100)
This is still lacking a fallback in case all 4 directions are taken.
This should not be possible right now anyway though.

Added .sav to girignore :^)

.gitignore
src/map.s

index f596bf3e5ff98266cf381d28add820bac882011a..9ee8adca6e1bb64e9379f0f1cf145ce3e74fc4b5 100644 (file)
@@ -72,3 +72,5 @@ rg.lst
 testrg.gb
 testrg.mlb
 testrg.lst
+
+*.sav
index 0b45d15eee051db512a305dff2713b00ee6f88c6..5f1876912f35980b9e28dd997a5518a1a2c91280 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -563,12 +563,7 @@ map_gen_next:
   push de ; save it again
 
   ; for now just pick a direction
-  push hl
-  push de
-  call rand 
-  pop de
-  pop hl
-  and a, 3 ; discard remaining bits 
+  call map_gen_pick_direction 
   push hl
   call map_gen_link_rooms
   
@@ -606,6 +601,59 @@ map_gen_next:
   pop hl
   ret
 
+  ; picks a direction in mapgen
+  ; inputs:
+  ;   hl: next room ptr
+  ;   de: previous room ptr 
+  ; returns:
+  ;   a: direction 
+  ; de/hl are preserved 
+map_gen_pick_direction:
+  push hl
+  push de
+  
+  ; TODO: add failure state if all 4 directions are taken 
+  ; returns special value to indicate failure
+@again:
+  call rand 
+  and a, 3 ; discard remaining bits
+  
+  ; store result for now
+  ld [tmp], a 
+  ld hl, map_gen_reverse_direction
+  ld d, 0
+  ld e, a
+  add hl, de
+  ld a, [hl] ; get reverse direction of the chosen one 
+
+  ; now check if direction is already used
+  ; in previous room
+  pop de
+  push de ; push again 
+
+  ld hl, roomb_exits
+  add hl, de ; hl = room exit struct 
+
+  add a, a ; a * 2 = ptr offset 
+  ld d, 0
+  ld e, a 
+  add hl, de ; hl = exit ptr
+
+  ; add both bytes of ptr 
+  ; to check if its NULL 
+  ld a, [hl+]
+  ld e, a
+  ld a, [hl]
+  add a, e ; this must be 0 
+  cp a, 0
+  jr nz, @again REL
+
+  ; get original result
+  ld a, [tmp]
+
+  pop de
+  pop hl
+  ret
 
   ; lut for reverse direction 
 map_gen_reverse_direction: