unit: Added basic chaser cpu script
authorLukas Krickl <lukas@krickl.dev>
Tue, 5 Aug 2025 18:21:45 +0000 (20:21 +0200)
committerLukas Krickl <lukas@krickl.dev>
Tue, 5 Aug 2025 18:21:45 +0000 (20:21 +0200)
src/defs.s
src/math.s
src/unit_cpu.s

index b5f8ae050245a9b1d6c0da6cf3667d608e2578cd..7d5faddb13d260cc63aeeb429cf56192dc8022f1 100644 (file)
 #define DD_TIME 90 
 #define DD_D0_TILE 0x90 
 #define DD_CROSS_TILE 0xA0
+
+#define DISTANCE_BGTA 1
+#define DISTANCE_AGTB 0
index 2d1d4b06cddba49bc0f1f6d89909c99fb8dc8921..4d90eec38f859478e30f3a6caf9aa5f78a874980 100644 (file)
@@ -15,7 +15,7 @@ distance:
        
        ; otherwise a simple sub will do
        sub a, b
-       ld c, 0
+       ld c, DISTANCE_AGTB 
        ret
 @b_gt_a:
        ; exchange a and b and sub
@@ -23,5 +23,5 @@ distance:
        ld a, b
        ld b, c
        sub a, b
-       ld c, 1 
+       ld c, DISTANCE_BGTA 
        ret
index 10a5df869d9e5a1332cd6e7b5782b79da6993230..f74361736c6177c7af88ce05c7c8326d119d414b 100644 (file)
@@ -1,15 +1,95 @@
+#define UNIT_SCAN_RANGE_Y MAP_H/4
+#define UNIT_SCAN_RANGE_X MAP_W/4
 
        ; handles cpu inputs
+       ; chaser CPU script 
        ; inputs:
        ;               de: actor
        ;       returns:
        ;               bc: next state
 unit_handle_cpu_inputs:
+#define MOVE_MADE scratch 
+       ; clear move made buffer
+       xor a, a
+       ld [MOVE_MADE], a
+       
+       ; there's a change the
+       ; unit will just move randomly anyway
+       call rand
+       and a, 0x7F
+       cp a, 0
+       jp z, @random_move
+
+       ; otherwise follow player
+
+       ; check y position
+       ; and follow player in y direction if player is in range
+       ld hl, player_unit+act_pos_y
+       ld a, [hl]
+       ld b, a ; b = player y pos
+       push de
+       ld hl, act_pos_y
+       add hl, de
+       ld a, [hl] ; a = act y pos
+       call distance
+       pop de
+
+       ; are we in scan range?
+       cp a, UNIT_SCAN_RANGE_Y
+       jr nc, @not_in_y_range REL
+       cp a, 1 
+       jr z, @not_in_y_range REL
+
+       ; which direction?
+       ld a, c
+       cp a, DISTANCE_BGTA
+       push af
+       call nz, unit_try_move_up
+       pop af
+       call z, unit_try_move_down
+       ld a, 1
+       ld [MOVE_MADE], a
+@not_in_y_range:
+
+       ; check x position
+       ; and follow player in x direction if player is in range
+       ld hl, player_unit+act_pos_x
+       ld a, [hl]
+       ld b, a ; b = player x pos
+       push de
+       ld hl, act_pos_x
+       add hl, de
+       ld a, [hl] ; a = act x pos
        call distance
+       pop de
+
+       ; are we in scan range?
+       cp a, UNIT_SCAN_RANGE_X
+       jr nc, @not_in_x_range REL
+       cp a, 1 
+       jr z, @not_in_x_range REL
+
+       ; which direction?
+       ld a, c
+       cp a, DISTANCE_BGTA
+       push af
+       call nz, unit_try_move_left
+       pop af
+       call z, unit_try_move_right
+       ld a, 1
+       ld [MOVE_MADE], a
+@not_in_x_range:
+@random_move:
+       
+       ; call random move if move was not made
+       ld a, [MOVE_MADE]
+       cp a, 0
+       call z, unit_cpu_random_move
 
-       call unit_cpu_random_move
+@move_made:
   ld bc, st_unit_delay_to_active
        ret
+#undefine MOVE_MADE
 
        
        ; moves actor into a random direction