unit_cpu: Added left and right attacks
authorLukas Krickl <lukas@krickl.dev>
Sat, 30 Aug 2025 04:26:15 +0000 (06:26 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 30 Aug 2025 04:26:15 +0000 (06:26 +0200)
src/unit_cpu.s

index fa2c33b9cb7dfc2b1ac6f558cdb8c62bd734f024..7a3ff94933452dd62e504c03efdb2b64a9785d79 100644 (file)
@@ -38,7 +38,6 @@ unit_handle_cpu_inputs:
        ldnull bc
        ret z
        
-       
        ; check if player has taken turn
        ; if not exit
        ldnull bc
@@ -46,12 +45,19 @@ unit_handle_cpu_inputs:
        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
@@ -136,7 +142,26 @@ unit_handle_cpu_inputs:
        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
@@ -159,7 +184,8 @@ unit_cpu_attack_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
        
@@ -168,18 +194,9 @@ unit_cpu_attack_player:
 
        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
@@ -194,6 +211,39 @@ unit_cpu_attack_player:
 @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