; 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