#define NULL 0
#define UNITS_MAX 20
+#define UNITS_SPAWN_MIN 4
+#define UNITS_SPAWN_MASK 7
#define STACK_BEGIN 0xDFFF
; 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
; 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
+