mapgen: Added check for units spawning in walls and randomized units that are spawned
authorLukas Krickl <lukas@krickl.dev>
Fri, 1 Aug 2025 15:54:40 +0000 (17:54 +0200)
committerLukas Krickl <lukas@krickl.dev>
Fri, 1 Aug 2025 15:54:40 +0000 (17:54 +0200)
src/defs.s
src/mapgen.s

index 1bea292a219d8560afbac8ada80b34f406454567..21c8fc20da8da0e66ed9f0170f07bbf99e542648 100644 (file)
@@ -21,6 +21,8 @@
 #define NULL 0
 
 #define UNITS_MAX 20
+#define UNITS_SPAWN_MIN 4
+#define UNITS_SPAWN_MASK 7
 
 #define STACK_BEGIN 0xDFFF
 
index 8c1a99db81d03e55c12db71ed09510364e36d712..b64cc77a344bba0d4de0a8460398e3318da900e9 100644 (file)
@@ -439,6 +439,13 @@ mapgen_place_actors:
        
        ; b = loop counter
        ld b, UNITS_MAX - 1
+       push hl
+       call rand
+       and a, UNITS_SPAWN_MASK 
+       add a, UNITS_SPAWN_MIN 
+       ld b, a 
+       ; b = units to spawn, at least 4 but might be more
+       pop hl
 @spawn_another:
                push bc
                push de
@@ -500,24 +507,37 @@ mapgen_place_actors:
        ;               de: actor table entry
        ; preserves all registers
 mapgent_unit_randomize_or_realod_position:
-       ; TODO: check if new position is wall and if so 
-       ; try again
        push_all
+@retry:
        ld hl, act_pos_y
        add hl, de ; hl = y pos
        
        ; select position
        push hl
        call rand
-       pop hl
        and a, MAP_H - 1
-       ld [hl+], a
-       
-       push hl
+       ld b, a
        call rand
+       and a, MAP_H - 1
+       ld c, a
+       ; bc = y/x
+
+       ; check if tile is OK to spawn on
+       push bc
+       call map_get_tile
+       and a, CF_COLLISION
+       pop bc
        pop hl
-       and a, MAP_W - 1
+       ; if collision flag is set try again
+       jr nz, @retry REL
+       
+       ; write y and x positions
+       ld a, b
+       ld [hl+], a
+       
+       ld a, c
        ld [hl], a
 
        pop_all
        ret
+