push de
@x_loop:
ld a, [bc]
+ call mapgen_fix_door_adjacent_to_collider
; read from tile and flag tables and write to map
pop de
ret
+
+ ; fixed door tile if it is adjacent to
+ ; a collider tile
+ ; inputs:
+ ; hl: map_ptr
+ ; a: pattern entry
+ ; returns:
+ ; a: unchanged if not next to collider or not a door pattern
+ ; a: changed to wall tile if it is a door and is next to a collider
+ ; preserves:
+ ; hl, bc, de
+mapgen_fix_door_adjacent_to_collider:
+ cp a, RPDU
+ jp z, @up_door
+
+ cp a, RPDB
+ jp z, @down_door
+
+ cp a, RPDL
+ jp z, @left_door
+
+ cp a, RPDR
+ jp z, @right_door
+
+ ret
+@up_door:
+
+ ; move up one row
+ ; and see if the tile is a collider
+
+ ret
+
+@down_door:
+ ret
+@left_door:
+ push hl
+ push de
+
+ ; move left one tile
+ ; and check for colliders
+ ld de, (-c_size) & 0xFFFF
+ add hl, de
+ ; hl = tile to the left
+
+ inc hl ; go to flags
+ ld d, a ; store a for later
+ ld a, [hl]
+ and a, CF_COLLISION
+ ld a, d ; restore a's value
+
+ ; if not collider do not fix
+ jr z, @no_left_fix REL
+ ; otherwise load left wall into a
+ ld a, RPLW
+
+@no_left_fix:
+
+ pop de
+ pop hl
+
+ ret
+@right_door:
+ ret