ldnull bc
ret z
-
; check if player has taken turn
; if not exit
ldnull bc
and a, GPF_PLAYER_TURN_TAKEN
ret z
+ ; if rand is > 127 we do not
+ ; attack no matter what
+ call rand
+ cp a, 127
+ jr c, @skip_attack REL
+
; attempt an attack
; and jump to move made if attack was performed
call unit_cpu_attack_player
cp a, 0
jp nz, @attack_started
-
+
+@skip_attack:
; clear move made buffer
xor a, a
ld [MOVE_MADE], a
ld bc, st_action_attack_damage_actor
ret
#undefine MOVE_MADE
-
+
+ ; loads attack runtime ptr and clears values
+ ; inputs
+ ; de: actor
+ ; returns:
+ ; hl: act_rt_action_dat1
+ ; preserves: af
+unit_cpu_load_and_clear_attack_rt_dat:
+ ; hl = rt action dat 1
+ ld hl, act_rt_action_dat1
+ add hl, de
+
+ push af
+ inc hl ; rt action dat 2
+ ; clear attack frame timer
+ xor a, a
+ ld [hl], a
+ dec hl ; back to dat 1
+ pop af
+ ret
; performs an attack
; in the direction of the player
; they need to be equal to be in range
cp a, b
jr nz, @not_in_y_range REL
-
+
+ ; now check y position distance
ld hl, act_pos_y
add hl, de
ld hl, player_unit+act_pos_y
ld a, [hl] ; a = player y
+
+ call unit_cpu_load_and_clear_attack_rt_dat
- ; hl = rt action dat 1
- ld hl, act_rt_action_dat1
- add hl, de
-
- push af
- inc hl ; rt action dat 2
- ; clear attack frame timer
- xor a, a
- ld [hl], a
- dec hl ; back to dat 1
- pop af
; check y distance
call distance
@not_in_y_range:
; check x distance
+ ; first check if y == y player
+ ld hl, act_pos_y
+ add hl, de
+ ld a, [hl]
+ ld b, a ; b = act y
+
+ ld hl, player_unit+act_pos_y
+ ld a, [hl]
+
+ cp a, b
+ jr nz, @not_in_x_range REL
+
+ ; now check x position distance
+ ld hl, act_pos_x
+ add hl, de
+ ld a, [hl] ; a = current unit x
+ ld b, a ; move to b
+
+ ld hl, player_unit+act_pos_x
+ ld a, [hl] ; a = player_x
+
+ call unit_cpu_load_and_clear_attack_rt_dat
+
+ ; check x distance
+ call distance
+ cp a, 1
+ jr nz, @not_in_x_range REL
+ ld a, c ; find out which direction
+ cp a, DISTANCE_AGTB
+ ; jump left or right
+ jp nz, @attack_left
+ jp @attack_right
+@not_in_x_range:
ld a, 0 ; no attack
ret