unit: wip actor switching
authorLukas Krickl <lukas@krickl.dev>
Sun, 18 May 2025 05:33:37 +0000 (07:33 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sun, 18 May 2025 05:33:37 +0000 (07:33 +0200)
src/unit.s
src/wram.s

index 35cabac29861b52ae7989e2be1d566613c860cb4..9f462d0e366cd750d8b43ce330f3aa34cbfd3c0c 100644 (file)
@@ -339,26 +339,89 @@ unit_switch_to_idle:
   ; initial value and try again
   ; skips any ACT_T_NULL types
   ; inputs:
-  ;   de: unit
+  ;   de: current unit
   ; uses:
   ;   unit_next_best_init: as a temporary buffer
 unit_next:
+  ; set moves of current unit to 0
+  ld hl, act_moves
+  add hl, de
+
   xor a, a
+  ld [hl], a ; moves = 0
   ld [unit_next_best_init], a
 
+  ; clear current ptr
+  ld [unit_next_bext_act_ptr], a
+  ld [unit_next_bext_act_ptr+1], a
+
   ld hl, p0_units 
   ld b, UNITS_MAX * PLAYERS 
 
   ; same as unit_next:
   ; inputs:
-  ;   de: unit
+  ;   de: free to use
   ;   hl: unit table
   ;    b: unit count
 @unit_next_loop:
-  
+    push hl ; save table 
+    ld de, act_type
+    add hl, de ; hl = type
+
+    ; check type skip it NULL
+    ld a, [hl]
+    cp a, ACT_T_NULL
+    jr z, @skip REL
+
+    ; check if moves are not 0
+    pop hl
+    ld de, act_moves
+    ld a, [hl]
+    cp a, 0
+    jr z, @skip REL
+      
+      pop hl
+      push hl
+      ; check if this actor is better 
+      ; than the previous actor
+      ld de, act_init
+      add hl, de
+      ld a, [hl] 
+      ld d, a ; b = init of new actor 
+
+      ld a, [unit_next_best_init]
+      ; check if this init value is better 
+      ; if not proceed
+      cp a, d 
+      jr nc, @skip REL ; d > a?
+
+        ; otherwise store new init value and hl
+        pop hl
+        push hl
+        ld [unit_next_best_init], a
+
+        ld a, l
+        ld [unit_next_bext_act_ptr], a
+        ld a, h
+        ld [unit_next_bext_act_ptr+1], a
+        
+@skip:
+    pop hl ; restore table
+    ld de, act_size
+    add hl, de ; next act
+
     dec b
   jr nz, @unit_next_loop REL
 
+  ; TODO:
+  ; if here unit_next_best_act_ptr is still 0000
+  ; reste all moves and try again
+  
+  ret
+
+  ; resets all moves
+  ; of all actors un p0 and p1_unts
+unit_reset_all_moves:
   ret
 
 
index 021154b4194ec94e5c2dda580ad13e50aa59b384..d3fd16776c94e6d10076adb7611de9d6ed1f3ca0 100644 (file)
@@ -18,6 +18,7 @@ game_mode: .adv st_size
 
 ; current best init found in units_next
 unit_next_best_init: .adv 1
+unit_next_bext_act_ptr: .adv 2
 
   ; actors 
   ; actors are state machines