From 8c92857d74af1bc1fff78540551e75ba166f0a70 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sun, 23 Feb 2025 12:16:14 +0100 Subject: [PATCH] state machine: every actor now has its own state machine memory area 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. --- src/actor.s | 2 ++ src/macros.inc | 6 ++++++ src/player.s | 6 ++---- src/state.s | 41 +++++++++++++++++++++++++++++++++++------ src/wram.s | 4 +++- 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/actor.s b/src/actor.s index 300733c..ccfda64 100644 --- a/src/actor.s +++ b/src/actor.s @@ -484,4 +484,6 @@ who_next: @no_player: ld [who], a + call sm_advance + ret diff --git a/src/macros.inc b/src/macros.inc index fcc79db..1c01b7d 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -107,5 +107,11 @@ ; 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 diff --git a/src/player.s b/src/player.s index 44b18da..fdf3c42 100644 --- a/src/player.s +++ b/src/player.s @@ -68,15 +68,13 @@ player_update: ; 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 diff --git a/src/state.s b/src/state.s index 059e54a..15f85c8 100644 --- a/src/state.s +++ b/src/state.s @@ -18,6 +18,29 @@ state_table: 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 @@ -26,8 +49,8 @@ sm_end_turn: ; 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 @@ -160,19 +183,25 @@ sm_bat_pick_direction: ; 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 diff --git a/src/wram.s b/src/wram.s index 4cad0a2..386c8a1 100644 --- a/src/wram.s +++ b/src/wram.s @@ -105,7 +105,9 @@ anim_step_x: .adv 1 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 -- 2.30.2