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
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: