roomgen: wip room generation
authorLukas Krickl <lukas@krickl.dev>
Sat, 26 Jul 2025 12:37:12 +0000 (14:37 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 26 Jul 2025 12:37:12 +0000 (14:37 +0200)
This is currently rather broken and corrupts the map :^)

src/defs.s
src/map.s
src/mapgen.s
src/roompatterns.s

index bff715d2aa6587cbbb846fce0a06584913f34a75..8d58554373d4de5b5659e9bdae8cecdd68a8e6d5 100644 (file)
 
 #define MAP_NAME_LEN 8
 
+  ; map flags1
+.se 1
+  ; disables random generation
+.de MAPF1_NO_RAND, 1
+
   ; map header struct
 .se 0
 .de map_flags_1, 1
index ca1b64820a171b0ce7b5bc42553223f99c4b878d..cf9de486f31f540a9f9687ffd5d838cd950ed1fa 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -41,6 +41,10 @@ map_load:
   push hl
   call map_load_exit_table
   pop hl
+  
+  push hl
+  call map_call_gen
+  pop hl
 
   call map_draw_all
 
@@ -51,6 +55,50 @@ map_load:
 
   ret
 
+  ; calls mapgen unless disabled
+  ; for current map
+  ; loads the seed based on map offset
+  ; inputs:
+  ;   hl: map_ptr
+map_call_gen:
+  push hl
+  
+  ; check if rand is disabled
+  ld de, map_flags_1
+  add hl, de
+  ld a, [hl]
+  and a, MAPF1_NO_RAND
+
+  pop hl
+  ; bail if flag is set
+  ret nz
+
+  ; load seed offset
+  push hl
+  
+  ld de, map_seed_index
+  add hl, de ; hl = seed index
+  ld a, [hl]
+
+  ; * 2 because it is a 2-byte table
+  add a, a
+  ld d, 0
+  ld e, a  
+  ld hl, map_seeds
+  add hl, de
+  ; hl = seeds+offset
+  ld a, [hl+] ; a = seed LO
+  ld d, a
+  ld a, [hl]
+  ld e, a ; de = seed
+
+  pop hl
+  
+  ld hl, map
+  call mapgen
+
+  ret
+
   ; draws map title to UI
   ; inputs:
   ;   hl: map_ptr
index 9420e419042d1e5e161ffa067276b1440b959cfc..c101245224f32a1394d3c2f788cb42bb37f0e3bc 100644 (file)
@@ -70,6 +70,8 @@ mapgen:
   ; inputs:
   ;   hl: [map]
 mapgen_up_left_room:
+  ld bc, room_pattern_6by6 
+  call mapgen_draw_room_pattern
   ret
 
   ; draws a room pattern to the map
@@ -90,12 +92,37 @@ mapgen_draw_room_pattern:
   inc bc ; move past header
   ; bc = first room pattern entry
 @y_loop:
+    push hl
     call mapgen_draw_room_pattern_row
+    pop hl
+    
+    push bc
+    ld bc, MAP_W
+    add hl, bc ; next row
+    pop bc
+
     dec d
   jr nz, @y_loop REL
 
   ret
 
+  ; reads a value from a table
+  ; inputs:
+  ;   a: pattern value
+  ;   $1: table name
+  ; preserves: hl and de
+#macro mapgen_read_value_from_table
+    push hl
+    push de
+    ld hl, $1 
+    ld d, 0
+    ld e, a
+    add hl, de ; hl = offset into table
+    ld a, [hl]
+    pop de
+    pop hl
+#endmacro
+
   ; draws a single map pattern row
   ; writes tile flags
   ; inputs:
@@ -111,15 +138,26 @@ mapgen_draw_room_pattern_row:
   push de  
 @x_loop:
     ld a, [bc]
-    inc bc ; next pattern
 
     ; read from tile and flag tables and write to map
-    push hl
-    push de
-    ld hl, room_pattern_tile_translation
+
+    ; tile table first
+    mapgen_read_value_from_table room_pattern_tile_translation
+
+    ; write to map
+    ld [hl+], a ; write tile
     
-    pop de
-    pop hl
+    ; read pattern again
+    ld a, [bc] 
+    
+    ; now flags table
+    mapgen_read_value_from_table room_pattern_flags_translation
+
+    ; write to flags
+    ld [hl+], a
+
+    ; hl = next tile
+    inc bc ; next pattern
 
     ; tiles--
     dec e
index dcfb3699e51e1e659fccf3a47017cb0a761cc870..35e9b1b2ac9ebc958adcfe4ca49908ff97775907 100644 (file)
@@ -72,7 +72,7 @@ room_pattern_tile_translation:
  .db 0x00, 0x00, 0x00, 0x00 
 
   ; translation table for flags
-room_pattern_floor1_flags_translation:
+room_pattern_flags_translation:
  ; walls
  .db CF_COLLISION, CF_COLLISION, CF_COLLISION 
  .db CF_COLLISION, CF_COLLISION, CF_COLLISION