state machine: every actor now has its own state machine memory area
authorLukas Krickl <lukas@krickl.dev>
Sun, 23 Feb 2025 11:16:14 +0000 (12:16 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 23 Feb 2025 11:16:14 +0000 (12:16 +0100)
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
src/macros.inc
src/player.s
src/state.s
src/wram.s

index 300733cde4447a914b4b8e01edf8d406b0a0b5a5..ccfda643c7483209e6e9ff0e2f56086f4b63f615 100644 (file)
@@ -484,4 +484,6 @@ who_next:
 @no_player:
   ld [who], a
 
+  call sm_advance
+
   ret
index fcc79db72879b7944dc73b88f7c25ce6c62b5399..1c01b7def200cd133302b77c71cfe5da2d561e98 100644 (file)
   ; 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 
index 44b18da518984fe0a0e9f7fc61780c69e4ed63e1..fdf3c421896ad5c4913229e12bafb53f39204d63 100644 (file)
@@ -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
 
index 059e54a7a3ccce07843e7f06a19743607f69932d..15f85c8607a98d30b5806a6bd91bc9652ca86ad4 100644 (file)
@@ -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
index 4cad0a29f9a673c076f50c0bfb5d3980cf590403..386c8a1ada44b0752fede967bcde1141b9000fea 100644 (file)
@@ -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