; if these flags are set goto_y and
; goto_x are treated as relative offsets
; instead of absolute tile positions
-.de MAP_EXITF_GOTOY_REL, 1
-.de MAP_EXITF_GOTOX_REL, 2
+.de EXITF_GOTOY_REL, 1
+.de EXITF_GOTOX_REL, 2
+
+.def int EXITF_GOTOXY_REL = EXITF_GOTOY_REL | EXITF_GOTOX_REL
; map exit table entry struct
.se 0
default_map_exit_table:
-exit_def 0, BTNRIGHT, 0, 0, default_map_header
+exit_def EXITF_GOTOXY_REL, BTNRIGHT, 0, -15, default_map_header
exit_def 0, 0, 0, 0, default_map_header
ld l, a ; hl = map header ptr
call map_load
- pop hl
- ; TODO
+ pop bc ; bc = exit table now
; finally adjust player position
+
+ ; hl = player
+ ld hl, player_unit
+ ld de, act_pos_y
+ add hl, de ; hl = y pos
+
+ ; adjust y position
+ ld a, [bc]
+ and a, EXITF_GOTOY_REL
+
+ inc bc
+ inc bc ; bc = y adjust
+ ld a, [bc]
+ dec bc
+ dec bc
+ call z, map_exit_absolute_adjust
+ call nz, map_exit_relative_adjust
+
+ inc hl ; hl = x pos
+
+ ; adjust x position
+ ld a, [bc]
+ and a, EXITF_GOTOX_REL
+
+ inc bc
+ inc bc
+ inc bc ; bc = x adjust
+ ld a, [bc]
+ call z, map_exit_absolute_adjust
+ call nz, map_exit_relative_adjust
+
+ ret
+
+ ; adjusts player position to aboslute
+ ; position
+ ; inputs:
+ ; hl: player_y or player_x
+ ; a: new y or new x
+ ; preserves all registers and flags
+map_exit_absolute_adjust:
+ ld [hl], a
+ ret
+
+ ; adjusts player position by a relative
+ ; position offset
+ ; inputs:
+ ; hl: player_y or player_x
+ ; a: new y or new x
+ ; preserves all registers and flags
+map_exit_relative_adjust:
+ push_all
+
+ ld b, a
+ ld a, [hl]
+ add a, b
+ ld [hl], a
+ pop_all
ret