call_hl
ret
-unit_handle_cpu_inputs:
- ; pick where to go
- call roll_d16
- and a, 3 ; 0-3
- ; 0 == left
- cp a, 0
- call z, unit_try_move_left
-
- ; 1 == right
- cp a, 1
- call z, unit_try_move_right
-
- ; 2 == up
- cp a, 2
- call z, unit_try_move_up
-
- ; 3 == down
- cp a, 3
- call z, unit_try_move_down
-
- ld bc, st_unit_delay_to_active_fast
- ret
-
; inputs
; de: actor
; actor initiative based on tile flags
; actor position
unit_try_move_up:
- call unit_use_move
- ret z
-
; y - 1
unit_test_collision dec b, CF_COLLISION
ret
unit_try_move_down:
- call unit_use_move
- ret z
-
; y + 1
unit_test_collision inc b, CF_COLLISION
ret
unit_try_move_left:
- call unit_use_move
- ret z
-
; x - 1
unit_test_collision dec c, CF_COLLISION
ret
unit_try_move_right:
- call unit_use_move
- ret z
; x + 1
unit_test_collision inc c, CF_COLLISION
ld [hl], a
ret
- ; consumes a move
- ; sets UI redraw flag
- ; fails (z flag set) if no moves left
- ; switches to next unit if moves reach 0
- ; inputs:
- ; de: unit
-unit_use_move:
- push de
- call ui_unit_need_draw
-
- ld hl, act_moves
- add hl, de
- pop de ; hl = act_moves
-
- ld a, [hl] ; current
- cp a, 0 ; if 0 exit
- ret z
-
- dec a
- ld [hl], a
-
- ; we need the z flag to not be set ehre
- ; so just do something that will always unset it
- or a, 1
- ret
-
; centers the current scroll on the selected unit
; snaps to corners of the map
; inputs:
call memcpy
ret
- ; requests unit next
- ; to be executed
- ; inputs:
- ; de: current unit
-unit_next_request:
- ld a, [gameplay_flags]
- and a, GPF_UNIT_NEXT
- ret nz ; do not call again if flag is set
-
- ld a, [gameplay_flags]
- or a, GPF_UNIT_NEXT
- ld [gameplay_flags], a
-
- ld a, e
- ld [gameplay_unit_current], a
- ld a, d
- ld [gameplay_unit_current+1], a
-
- ; set up delay timer
- ld a, 16
- ld [unit_next_timer], a
-
- ret
-
- ; finds a unit with a higher initiative value
- ; than the current one that has moves > 0 set
- ; if all unts have have 0 moves, set moves to
- ; initial value and try again
- ; when a unit has higher initiative it can still fail
- ; to take the turn if it rolls a d16 and the roll is < initiative
- ; skips any ACT_T_NULL types
- ; inputs:
- ; gameplay_unit_current: unit that requested a change
- ; uses:
- ; unit_next_best_init: as a temporary buffer
-unit_next:
- ; if the unit_next timer is not 8
- ; wait and timer--
- ld a, [unit_next_timer]
- cp a, 0
- jr z, @go REL
- dec a
- ld [unit_next_timer], a
- ret
-@go:
- ; unset flag
- ld a, [gameplay_flags]
- xor a, GPF_UNIT_NEXT
- ld [gameplay_flags], a
-
- ; load requestor unit
- ld a, [gameplay_unit_current]
- ld e, a
- ld a, [gameplay_unit_current+1]
- ld d, a
-
- push de
- call unit_sleep
- pop de
- ; set moves of current unit to 0
- ld hl, act_moves
- add hl, de
- ld [hl], 0 ; moves = 0
-unit_next_no_current:
-@again:
- xor a, a
- ld [unit_next_best_init], a
-
- ; clear current ptr
- ld [unit_next_best_act_ptr], a
- ld [unit_next_best_act_ptr+1], a
-
- ld hl, p0_units
- ld b, UNITS_MAX
-
- ; same as unit_next:
- ; inputs:
- ; 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
- push hl
- ld de, act_moves
- add hl, de
- 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 a, [unit_next_best_init]
- cp a, 0 ; if prev best is 0 proceed anyway
- ld d, a ; d = previous best init
-
- pop de ; de = actor
- push de ; store again for later
- call stat_calc_init ; a = init of new actor
- jr z, @first_hit REL ; on first match
-
- ; check if this init value is better
- ; if not proceed
- cp a, d
- jr c, @skip REL ; d > a?
-@first_hit:
- ; otherwise store new init value and hl
- pop hl
- push hl
-
- ld [unit_next_best_init], a
-
- ld a, l
- ld [unit_next_best_act_ptr], a
- ld a, h
- ld [unit_next_best_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
-
- ; if here unit_next_best_act_ptr is still 0000
- ; reste all moves and try again
- ld a, [unit_next_best_act_ptr]
- ld b, a
- ld a, [unit_next_best_act_ptr+1]
- xor a, b
- jr z, @retry REL
-
- ; load new bext into hl
- ; and set active
- ld a, [unit_next_best_act_ptr]
- ld [ui_draw_actor], a
- ld e, a
- ld a, [unit_next_best_act_ptr+1]
- ld [ui_draw_actor+1], a
- ld d, a
-
- push de
- call unit_wake_up
- pop de
- call ui_unit_need_draw
- ret
-
-@retry:
- call unit_reset_all_moves
- jp @again
-
- ; resets all moves
- ; of all actors in p0 and p1_unts
-unit_reset_all_moves:
- ld b, UNITS_MAX
- ld hl, p0_units
-@loop:
- push hl
-
- pop de
- push de
- call stat_calc_moves_max
-
- ld de, act_moves
- add hl, de ; hl = current moves
- ld [hl], a ; moves are now reset
-
- pop hl
- ld de, act_size
- add hl, de ; move to next act
- dec b
- jr nz, @loop REL
-
- ret
-
; called before switching to active
; this call also checks if the unit may need to
; hand over control to a new unit
; de: unit
unit_delay_to_active:
call unit_resume_objs
- ld hl, act_moves
- add hl, de ; hl = moves
- ld a, [hl]
- cp a, 0
- call z, unit_next_request
ldnull bc
ret
push de
call unit_handle_cpu_inputs
pop de
-
+
ret
+unit_handle_cpu_inputs:
+ ; pick where to go
+ call roll_d16
+ and a, 3 ; 0-3
+ ; 0 == left
+ cp a, 0
+ call z, unit_try_move_left
+
+ ; 1 == right
+ cp a, 1
+ call z, unit_try_move_right
+
+ ; 2 == up
+ cp a, 2
+ call z, unit_try_move_up
+
+ ; 3 == down
+ cp a, 3
+ call z, unit_try_move_down
+
+ ld bc, st_unit_delay_to_active
+ ret
+
+
unit_demo_1_cpu_update_idle:
- call unit_use_move
- call unit_next_request
ldnull bc
ret
unit_demo_2:
st_def 0x00, unit_demo_1_init, st_unit_idle
act_def ACT_T_DEMO_1, 0, 3, 3, 0
- act_stat_def1 1, 1, 1, 1, 1, 1
+ act_stat_def1 1, 1, 1, 1, 1
act_stat_def2 1, 1, 1, 1, 1, 1, 1, 1
act_attr_def_empty
act_inventory_empty
act_rt_def
unit_demo_3:
- st_def 0x00, unit_demo_1_init, st_unit_idle
+ st_def 0x00, unit_demo_1_init, st_unit_demo_1_cpu_update
act_def ACT_T_DEMO_1, 0, 4, 4, 0
- act_stat_def1 1, 1, 1, 1, 1, 1
+ act_stat_def1 1, 1, 1, 1, 1
act_stat_def2 1, 1, 1, 1, 1, 1, 1, 1
act_attr_def_empty
act_inventory_empty