ret
#undefine scratch_loop_i
+#define TMP_MOVEMENT_SPEED scratch
+ ; calculates movement speed based un the current unit
+ ; inputs:
+ ; de: unit
+ ; returns:
+ ; movement speed in sub-tiles in TMP_UNIT_MOVEMENT_SPEED
+unit_calc_movement_speed:
+ call stat_calc_speed
+ ; movement speed is simply speed / 16
+ div16 a
+ cp a, 0
+ jr nz, @not_zero_speed REL
+ inc a
+@not_zero_speed:
+ ld [TMP_MOVEMENT_SPEED], a
+ ret
+
+ ; performs a sub tile move
+ ; inputs:
+ ; de: actor
+ ; $1: swap a/nop
+ ; $2: add/sub
+ ; $3: mask to apply to move speed (0xF0 or 0x0F)
+ ; returns if sub-tile movement does not
+ ; cause an over/underflow
+ ; preserves:
+ ; hl
+#macro unit_sub_tile_move
+ push hl
+
+ push de
+ call unit_calc_movement_speed
+ pop de
+
+ ld hl, act_rt_subtile_pos
+ add hl, de ; hl = subtile pos
+
+ ld a, [TMP_MOVEMENT_SPEED]
+ ld b, a ; b = movement speed
+
+ ld a, [hl]
+ $1 ; swap
+ and a, 0x0F ; mask bits we don't need
+ $2 a, b ; sub/add
+ ; test if overflow
+ ld c, a ; need value again in a bit
+ and a, 0xF0
+ ; z flag is set if no overflow
+ push af ; push flag on to stack for now
+
+ ld a, c ; now cut off value
+ and a, 0x0F ; mask it out in case of overflow
+
+ $1 ; swap back
+ ld b, a ; b = new position
+ ld a, [hl] ; load value again
+ and a, $3 ; mask off
+ or a, b
+ ld [hl], a ; store new value
+
+ ; if we did not over/underflow
+ ; exit now
+ pop af ; get back flag result
+ pop hl
+ ret nz
+#endmacro
+
; moves a unit up
; moves are aborted
; if no more initiative is left
; y - 1
unit_test_collision dec b, CF_COLLISION
+ push de
ld hl, act_pos_y
add hl, de
; hl = actor y
ld a, [hl]
cp a, 0 ; upper bound
+
+ pop de
ret z
+ unit_sub_tile_move swap a, sub, 0xF0
+
+ ld a, [hl]
dec a
ld [hl], a
+
ret
unit_try_move_down:
ld [hl], a
ret
+#undefine TMP_MOVEMENT_SPEED
+
; centers the current scroll on the selected unit
; snaps to corners of the map
; inputs: