From 14f502af4debe3bdad114cb79c1a28edf3ef0546 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Tue, 19 Aug 2025 18:24:03 +0200 Subject: [PATCH] mapgen: Added door drawing routine This is the first step to making a better map generator. Doors are now placed based on a door exit list for each possible room in a floor. --- src/map.s | 4 +- src/mapgen.s | 101 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/player.s | 6 +++ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/src/map.s b/src/map.s index 88bf025..9706274 100644 --- a/src/map.s +++ b/src/map.s @@ -100,9 +100,7 @@ map_call_gen: ; load seed offset push hl - ld de, map_seed_index - add hl, de ; hl = seed index - ld a, [hl] + ld a, [player_map_cursor] ; * 2 because it is a 2-byte table add a, a diff --git a/src/mapgen.s b/src/mapgen.s index 127dc81..1c430db 100644 --- a/src/mapgen.s +++ b/src/mapgen.s @@ -1,6 +1,7 @@ ; generates a new set of seeds ; and places them into map_seeds + ; also places initial door locations mapgen_seed: ld hl, map_seeds ; 2 bytes per seed @@ -14,6 +15,8 @@ mapgen_seed: dec b jr nz, @loop REL + call mapgen_make_doors + ret ; places a rectangular room @@ -30,10 +33,6 @@ mapgen_seed: ; of size MAP_H * MAP_W ; de: the seed used ; map_header: the current map header - ; returns: - ; a == 0: room was unable to be placed - ; a == 1: room was placed - ; de: seed after generation has finished ; preserves: ; srand mapgen: @@ -60,6 +59,8 @@ mapgen: call mapgen_bottom_right_room call mapgen_place_special_room + + call mapgen_draw_doors call mapgen_place_actors @@ -72,6 +73,90 @@ mapgen: ld [srand], a ret + + ; generates door locations for each map +mapgen_make_doors: + ld a, FLOOR_MAP_COUNT + ld hl, map_doors_location + ; TODO: fnor now just place all 4 doors +@loop: + push af + ld a, DIRUP | DIRDOWN | DIRLEFT | DIRRIGHT + ld [hl+], a + pop af + dec a + jr nz, @loop REL + ret + + ; loads the current player map cursor + ; and draws the required doors + ; inputs: + ; hl: [map] + ; preserves: hl +mapgen_draw_doors: + push hl + ld hl, map_doors_location + ld a, [player_map_cursor] + ld e, a + ld d, 0 + add hl, de ; hl = door entry + + ld a, [hl] ; a = door pattern + pop hl ; hl = map + ld b, a ; b = door pattern backup + + and a, DIRUP + jr z, @no_door_up REL + push hl + ld de, 7*c_size ; move over 7 tiles + add hl, de + ld a, DOOR_TILE_TOP + ld [hl+], a + ld a, CF_DOOR | CF_COLLISION + ld [hl], a + pop hl +@no_door_up: + + ld a, b + and a, DIRDOWN + jr z, @no_door_down REL + push hl + ld de, 7*c_size + MAP_W * (MAP_H - 1) * c_size + add hl, de + ld a, DOOR_TILE_BOTTOM + ld [hl+], a + ld a, CF_DOOR | CF_COLLISION + ld [hl], a + pop hl +@no_door_down: + + ld a, b + and a, DIRLEFT + jr z, @no_door_left REL + push hl + ld de, 7 * MAP_W * c_size + add hl, de + ld a, DOOR_TILE_LEFT + ld [hl+], a + ld a, CF_DOOR | CF_COLLISION + ld [hl], a + pop hl +@no_door_left: + + ld a, b + and a, DIRRIGHT + jr z, @no_door_right REL + push hl + ld de, 7 * MAP_W * c_size + (MAP_W - 1) * c_size + add hl, de + ld a, DOOR_TILE_RIGHT + ld [hl+], a + ld a, CF_DOOR | CF_COLLISION + ld [hl], a + pop hl +@no_door_right: + + ret ; selects a random room pattern ; inputs: @@ -192,6 +277,7 @@ mapgen_bottom_right_room: ; special rooms are placed starting in the top left corner ; inputs: ; hl: [map] + ; preserves: hl mapgen_place_special_room: push hl call rand @@ -199,6 +285,8 @@ mapgen_place_special_room: pop hl ret z ; bail if no speical room is requested + push hl + ; move to correct location on map ld bc, MAP_W*c_size add hl, bc @@ -211,9 +299,10 @@ mapgen_place_special_room: ld hl, room_pattern_special call mapgen_select_pattern pop hl - - call mapgen_draw_room_pattern + call mapgen_draw_room_pattern + + pop hl ret diff --git a/src/player.s b/src/player.s index a4616f5..330e895 100644 --- a/src/player.s +++ b/src/player.s @@ -116,6 +116,12 @@ unit_player_remove_door: ld hl, act_rt_collision_pos_y add hl, de ld a, [hl] ; load y offset + + ; if y is at bottom of map go back up a tile... + cp a, MAP_H-1 ; + jr nz, @not_end_of_map REL + dec a +@not_end_of_map: call map_request_redraw_at ret -- 2.30.2