map: Added basic structure and docs for mapgen
authorLukas Krickl <lukas@krickl.dev>
Mon, 30 Dec 2024 14:16:29 +0000 (15:16 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 30 Dec 2024 14:16:29 +0000 (15:16 +0100)
This implementation is not yet functional, but the basic idea is taking
shape :^)

src/map.s
src/mem.s
src/wram.s

index 2196059d4ee44cbe8367f907da8fed5d9587f2fb..327f75f7b81c0c81a8b41d4560d6b5db4311c70b 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -470,19 +470,96 @@ room_get_flag_masked:
   ;   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 
@@ -491,6 +568,16 @@ map_gen_copy_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 
index a825b54f4b8964071d2894705a431ebd404fa63e..09c90c18ecb6d101b424f9a86905d44be7f9ef82 100644 (file)
--- a/src/mem.s
+++ b/src/mem.s
@@ -30,6 +30,10 @@ mem_init:
 ;   de: source
 ;   hl: destination
 ;   bc: length
+; returns:
+;   hl: hl+length
+;   de: de+length
+;   bc: 0
 memcpy:
 @loop:
   ld a, [de]
index c8b7b1b025b55c9c7ebb0790dcecacd1ba4c9ba7..cb6e064af9613df69dd362e3e5a49722a0d79615 100644 (file)
@@ -165,7 +165,9 @@ floor: .adv 1
   ; version of save game
 .de save_game_version, 1
   ; saving rng seed
-.de save_game_seed, 1
+.de save_game_srand, 1
+  ; saving floor number 
+.de save_game_floor, 1
   ; saving all room headers
   ; it is important to ensure 
   ; that the rooms are loaded back in their intended memory space
@@ -264,3 +266,9 @@ anim_target_x: .adv 1
 ct_poy: .adv 1
 ct_pox: .adv 1
 ct_mask: .adv 1
+
+  ; mapgen tmp values
+  
+  ; current depth
+  ; used for recursion in mapgen
+mapgen_depth: .adv 1