Each actor gets 3 bytes. the sm load ptr macro now loads the correct
offset.
When a turn is ended the sm offset is advanced by sm_size. If the actor
table is reset to 0, sm offset is also reset.
@no_player:
ld [who], a
+ call sm_advance
+
ret
; TODO: take into account offset for each
; actor by adding who*sm_size to hl
; => also need to reserve wram for this purpose
+ push de
ld $1, state_machine
+ ld a, [state_machine_offset]
+ ld e, a
+ ld d, 0
+ add hl, de
+ pop de
#endmacro
; load initial state if required
call sm_load_initial_state
- ld de, state_machine
push hl
push hl
pop bc
dec bc ; bc = actor_ptr for player
+
+ call sm_call_state
- ld hl, state_table
- ld a, [de]
- call call_tbl
; restore hl
pop hl
sm_nop:
ret
+ ; advances state machine to
+ ; next offset
+ ; resets if who is 0
+ ; preserves: all registers
+sm_advance:
+ push_all
+ ld a, [who]
+ cp a, 0
+ jr nz, @not_reset REL
+ ; offset = 0
+ ld [state_machine_offset], a
+ pop_all
+ ret
+@not_reset:
+ ; offset + sm_size
+ ld a, [state_machine_offset]
+ ld b, sm_size
+ add a, b
+ ld [state_machine_offset], a
+ pop_all
+ ret
+
+ ; end turn state
sm_end_turn:
call sm_clear
turn_finish
; clears the state machine
sm_clear:
- xor a, a
sm_load_ptr hl
+ xor a, a
ld [hl+], a
ld [hl+], a
ld [hl], a
; uses:
; tmp
sm_load_initial_state:
- push de
+ push hl
ld [tmp], a
- ld de, state_machine
- ld a, [de]
+ sm_load_ptr hl
+ ld a, [hl]
cp a, 0 ; if state is not 0 ret
jr nz, @done REL
; set initial state
ld a, [tmp]
- ld [de], a
+ ld [hl], a
@done:
- pop de
+ pop hl
ret
+ ; calls the current state machine state
+sm_call_state:
+ sm_load_ptr hl
+ ld a, [hl]
+ ld hl, state_table
+ jp call_tbl
anim_target_y: .adv 1
anim_target_x: .adv 1
-state_machine: .adv sm_size
+ ; ptr offset for state machine
+state_machine_offset: .adv 1
+state_machine: .adv sm_size * (ACTOR_TABLE_SIZE + 1)
; collision tile tmp values
ct_poy: .adv 1