From: Lukas Krickl Date: Fri, 1 Aug 2025 15:54:40 +0000 (+0200) Subject: mapgen: Added check for units spawning in walls and randomized units that are spawned X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=acf31652152a6aeed92881331c908bc36f104008;p=gbrg%2F.git mapgen: Added check for units spawning in walls and randomized units that are spawned --- diff --git a/src/defs.s b/src/defs.s index 1bea292..21c8fc2 100644 --- a/src/defs.s +++ b/src/defs.s @@ -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 diff --git a/src/mapgen.s b/src/mapgen.s index 8c1a99d..b64cc77 100644 --- a/src/mapgen.s +++ b/src/mapgen.s @@ -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 +