mapgen: Added door drawing routine
authorLukas Krickl <lukas@krickl.dev>
Tue, 19 Aug 2025 16:24:03 +0000 (18:24 +0200)
committerLukas Krickl <lukas@krickl.dev>
Tue, 19 Aug 2025 16:24:03 +0000 (18:24 +0200)
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
src/mapgen.s
src/player.s

index 88bf025629c721675f8feacda946d6e99b9aca87..97062746c293bf6e8dc173b27bb518a66579f1f5 100644 (file)
--- 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
index 127dc81722d84b47ca03fb06c1b72ec9f2ce5854..1c430db0f8e2c770dfce80b0d17117dd60527cf0 100644 (file)
@@ -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
 
 
index a4616f5383d12da335f828ca506e6a9c62a2c4b4..330e8953179fb3817f722d7401dbfc80f23e25ce 100644 (file)
@@ -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