mapgen: Added room pattern placement for all 4 basic corners of the map
authorLukas Krickl <lukas@krickl.dev>
Sat, 26 Jul 2025 15:28:09 +0000 (17:28 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 26 Jul 2025 15:28:09 +0000 (17:28 +0200)
Currently there are only 2 room patterns but the basic idea works
correctly ;)

src/mapgen.s
src/roompatterns.s

index c101245224f32a1394d3c2f788cb42bb37f0e3bc..8ffda5adf4b5ce86e33b2c63ef63afda033a0f8a 100644 (file)
@@ -53,9 +53,10 @@ mapgen:
   ld [srand], a
   
   ; generate first room
-  push hl 
   call mapgen_up_left_room
-  pop hl
+  call mapgen_bottom_left_room
+  call mapgen_up_right_room
+  call mapgen_bottom_right_room
 
 
   ; restore seed
@@ -67,13 +68,119 @@ mapgen:
 
   ret
 
+  ; selects a random room pattern
+  ; inputs:
+  ;   hl: room pattern table
+  ;    a: table size
+  ; returns: 
+  ;   bc: room pattern ptr
+mapgen_select_pattern:
+  push hl
+
+  ld b, a
+  call rand
+  and a, b ; cap rand to table size
+
+  pop hl
+
+  ld b, 0
+  ld c, a
+  add hl, bc
+
+
+  ; load table ptr
+  ld a, [hl+]
+  ld c, a
+  ld a, [hl]
+  ld b, a
+
+  ret
+
+  ; select room pattern 6x6
+  ; preserves: hl
+mapgen_select_pattern_6x6:
+  push hl
+  ld a, ROOM_PATTERN_6X6_SIZE
+  ld hl, room_pattern_6x6
+  call mapgen_select_pattern
+  pop hl
+  ret
+
   ; inputs:
   ;   hl: [map]
+  ; preserves: hl
 mapgen_up_left_room:
-  ld bc, room_pattern_6by6 
+  push hl
+  
+  ; move to correct location on map
+  ld bc, MAP_W*c_size
+  add hl, bc
+  inc hl
+  inc hl 
+
+  call mapgen_select_pattern_6x6
+
   call mapgen_draw_room_pattern
+  pop hl
   ret
 
+
+  ; inputs:
+  ;   hl: [map]
+  ; preserves: hl
+mapgen_bottom_left_room:
+  push hl
+  
+  ; move to correct location on map
+  ld bc, MAP_W*9*c_size
+  add hl, bc
+  inc hl
+  inc hl 
+
+  call mapgen_select_pattern_6x6
+
+  call mapgen_draw_room_pattern
+  pop hl
+  ret
+
+  ; inputs:
+  ;   hl: [map]
+  ; preserves: hl
+mapgen_up_right_room:
+  push hl
+  
+  ; move to correct location on map
+  ld bc, (MAP_W/2)*c_size + MAP_W*c_size
+  add hl, bc
+  inc hl
+  inc hl 
+
+  call mapgen_select_pattern_6x6
+
+  call mapgen_draw_room_pattern
+  pop hl
+  ret
+
+
+  ; inputs:
+  ;   hl: [map]
+  ; preserves: hl
+mapgen_bottom_right_room:
+  push hl
+  
+  ; move to correct location on map
+  ld bc, (MAP_W/2)*c_size + MAP_W*9*c_size
+  add hl, bc
+  inc hl
+  inc hl 
+
+  call mapgen_select_pattern_6x6
+
+  call mapgen_draw_room_pattern
+  pop hl
+  ret
+
+
   ; draws a room pattern to the map
   ; inputs:
   ;   hl: origin inside of map struct
@@ -97,7 +204,7 @@ mapgen_draw_room_pattern:
     pop hl
     
     push bc
-    ld bc, MAP_W
+    ld bc, MAP_W*c_size
     add hl, bc ; next row
     pop bc
 
@@ -143,7 +250,6 @@ mapgen_draw_room_pattern_row:
 
     ; tile table first
     mapgen_read_value_from_table room_pattern_tile_translation
-
     ; write to map
     ld [hl+], a ; write tile
     
index 35e9b1b2ac9ebc958adcfe4ca49908ff97775907..92d06bd7f2f0ca5c268e1d0f40822b2498c70b82 100644 (file)
   ; door right
 .de RPDR, 1
 
+room_pattern_empty:
+  rpheaderdef 0x66
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+  rprow RPFL, RPFL, RPFL, RPFL, RPFL, RPFL
+
 room_pattern1:
   rpheaderdef 0x66 
   rprow RPUL, RPUW, RPUW, RPDU, RPUW, RPUR
@@ -64,9 +73,9 @@ room_pattern1:
   ; translation tables for tiles
 room_pattern_tile_translation:
   ; walls
- .db 0x6b, 0x6b, 0x6b 
- .db 0x6b, 0x6b, 0x6b
- .db 0x6b, 0x6b
+ .db 0x44, 0x44, 0x44 
+ .db 0x44, 0x44, 0x44
+ .db 0x44, 0x44
  .db 0x00 ; floor
  ; doors
  .db 0x00, 0x00, 0x00, 0x00 
@@ -82,6 +91,9 @@ room_pattern_flags_translation:
  .db CF_DOOR, CF_DOOR, CF_DOOR, CF_DOOR 
 
   ; table of 6 by 6 room patterns
-room_pattern_6by6:
+room_pattern_6x6:
+  dw room_pattern_empty
   dw room_pattern1
-room_pattern_6by6_end:
+room_pattern_6x6_end:
+
+#define ROOM_PATTERN_6X6_SIZE ((room_pattern_6x6_end - room_pattern_6x6) / 2)