mapgen: Room patterns are now randomly chosen.
authorLukas Krickl <lukas@krickl.dev>
Tue, 17 Feb 2026 06:59:57 +0000 (07:59 +0100)
committerLukas Krickl <lukas@krickl.dev>
Tue, 17 Feb 2026 06:59:57 +0000 (07:59 +0100)
Added init code for mapgen seed.

assets
makefile
maps/f1r2.inc [new file with mode: 0644]
src/mapgen.s
src/mem.s
src/rand.s
src/sram.s

diff --git a/assets b/assets
index dfacc03a9d29342f17f2a688733c6124edc841a7..49200b3a091eca46b333f2d636e56f371d07fe52 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit dfacc03a9d29342f17f2a688733c6124edc841a7
+Subproject commit 49200b3a091eca46b333f2d636e56f371d07fe52
index 8c778e495f71e417f5e7ec321a2403316030910d..ba4d6d2a834848728ad7469fc0b0bdf61fcfbf4b 100644 (file)
--- a/makefile
+++ b/makefile
@@ -23,4 +23,5 @@ tiles:
 maps: 
        ./tools/tmx2map.py assets/maps/l1.tmx > maps/l1.inc
        ./tools/tmx2map.py assets/maps/f1r1.tmx > maps/f1r1.inc
+       ./tools/tmx2map.py assets/maps/f1r2.tmx > maps/f1r2.inc
 
diff --git a/maps/f1r2.inc b/maps/f1r2.inc
new file mode 100644 (file)
index 0000000..9cdf1a0
--- /dev/null
@@ -0,0 +1,7 @@
+; this map was generated by tmx2map.py
+
+.db 0x1, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, 0x1
+.db 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x2, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x2
+.db 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x2
+.db 0x0, 0x0, 0x0, 0x0
+
index fb7cfc1e12a4d07330283cd5be44e635e9a4cfbc..b771602646229e379cea64a19b1f4f5d84209ed3 100644 (file)
@@ -10,7 +10,7 @@ map_generate:
        ld bc, MAP_TILES
        call memset
        
-       ; 1) setup seed
+       ; 1) setup seed,,,
        ; store exiting seed in tmp game seed
        ld a, [srand]
        ld [mapgen_game_seed], a
@@ -164,6 +164,22 @@ mapgen_make_rooms:
        ;               hl: start position in tile map
 mapgen_make_room:
        ; TODO: pick a room pattern
+       push hl
+
+       ld b, a
+       call rand
+       dec b
+       and a, b ; ensure we are in range
+       add a, a ; *2 for offset
+
+       ld h, 0
+       ld l, a
+       add hl, de
+       push hl
+       pop de ; de = correct room to draw
+
+       pop hl
+
        
        ; de = room pattern to draw
        ld a, [de]
@@ -189,8 +205,11 @@ mapgen_make_room:
 
 f1r1:
 #include "f1r1.inc"
+f1r2:
+#include "f1r2.inc"
+
 
 floor1_patterns:
 dw f1r1
-dw f1r1
+dw f1r2
 floor1_patterns_end:
index 21b9d2ec0b7014728e2af232cb1d165396c8ebc0..32496802a75a4bd855f70bd3f1fa0bd033e87d48 100644 (file)
--- a/src/mem.s
+++ b/src/mem.s
@@ -12,14 +12,15 @@ mem_init:
   ld bc, WRAMLEN/2
   call memset
 
+  ; set up rand
+  call rand_init
+
        call map_globals_init
   
   ; set up shadow IE
   ld a, IVBLANK | ILCD
   ld [shadow_ie], a
 
-  ; set up rand
-  call rand_init
 
   ; copy shadow oam dma function 
   ld de, shadow_oam_to_oam 
index 8414b118838275f7e62e015f419e4dce749e0c50..e9b057c4772e6d7eaa767719b3124ea0d514bde0 100644 (file)
@@ -12,6 +12,13 @@ rand_init:
   or a, 1
   ld [srand+1], a
 
+       ld a, [sram_mapgen_srand]
+       or a, 1
+       ld [mapgen_seed], a
+       ld a, [sram_mapgen_srand+1]
+       or a, 1
+       ld [mapgen_seed+1], a
+
   call mbc1_ram_disable
   ret
 
@@ -26,6 +33,12 @@ rand_save_srand:
   ld a, [srand+1]
   ld [sram_srand+1], a
 
+       ; save mapgen seed in sram
+       ld a, [mapgen_seed]
+       ld [sram_mapgen_srand], a
+       ld a, [mapgen_seed+1]
+       ld [sram_mapgen_srand], a
+
   call mbc1_ram_disable
   ret
 
@@ -36,6 +49,8 @@ rand_save_srand:
   ;   a, hl, de
   ; returns:
   ;   a: random value
+       ; uses:
+       ;               hl, a
 rand:
   ; load seed
   ld a, [srand]
index 27ee968edc49b04c217e297f0f69db3995032784..e6ffe5535fcb532c3afc53b65c212faedac19608 100644 (file)
@@ -10,3 +10,4 @@
   ; flag for init of sram
 sram_magic: .adv 1
 sram_srand: .adv 2
+sram_mapgen_srand: .adv 2