; sets the current room
; sets player position
map_generate:
+ ; clear map0
+ ld d, 0
+ ld hl, map0
+ ld bc, map0_end - map0
+ call memset
+
+ ; basic mapgen setup
+ xor a, a
+ ld [mapgen_depth], a
+
ld d, ROOMS_TOTAL ; d = loop counter
ld hl, map0 ; hl = current room
- ; TODO: this is placeholder mapgen
+
+ ; move to map_gen_next from here
+ ; generate the next room
+ ; inputs:
+ ; hl: current room
+map_gen_next:
+ ; depth ++
+ ld a, [mapgen_depth]
+ inc a
+ ld [mapgen_depth], a
+
+ ; select a room
+ ; and copy it to the current position
+ ; linking back NULL on all exits initially
+ push hl
+
+ ; load base room ptr
+ ; TODO we are simply loading base_room_header every time
+ ld hl, base_room_table
+ ld a, [hl+]
+ ld e, a
+ ld a, [hl]
+ ld d, a
+ pop hl ; hl = room dst
+ ; set default exit ptr to 0000
+ ; to indicate it is empty
+ xor a, a
+ ld b, a
+ ld c, a
+
+ ; now we copy
+ push hl ; save original ptr for now
+ call map_gen_copy_base_room
+ pop hl
+
+ ; select a direction to walk into and call again
+ ; and set up pointers
+ ; if direction is already taken try again
+ ; if direction is special return to previous room unless depth is 1
+ ; if all directions are taken, return
+ ; otherwise set up next ptr and move there
+ ; update door tiles in picked direction
+ ; push map ptr to stack before calling again
+
+ ; depth --
+ ld a, [mapgen_depth]
+ dec a
+ ld [mapgen_depth], a
ret
; copies a base room
; inputs:
- ; hl: target
; de: base room
+ ; hl: target
; bc: default exit room ptr
map_gen_copy_base_room:
+ push bc
+
+ ; this routine copies a base room
+ ; and fixes the header accordingly
+
+ ; copy header
+ ld bc, room_size
+ call memcpy
+
+ ; copy tiles
+ ld bc, ROOM_TILES_SIZE
+ call memcpy
+
+ ; copy flags
+
+ ; copy actors
+
+ ; set exits
+ pop bc ; restore default exit value
+
ret
; base room
#include "./maps/base_room.s"
#include "./maps/base_room2.s"
+ ; a table of all base room ptrs
+ ; length must be divisible by 2
+base_room_table:
+ dw base_room_header
+ 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:
dw base_room2_header