unit: wip unit switching
authorLukas Krickl <lukas@krickl.dev>
Mon, 19 May 2025 16:09:26 +0000 (18:09 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 19 May 2025 16:09:26 +0000 (18:09 +0200)
This still has very broken side effects.

Unit switching breaks the camera and the active actor.
The second actor should be switched to but it is not moving at all.

src/map.s
src/unit.s
src/wram.s

index 6b961ba77dd26048785b5e01e12ee3a5ea6944ce..1e777e6f994a27054d8e9fbc758904c85e843419 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -108,6 +108,11 @@ map_load_demo_actors:
   ld hl, p0_units
   ld bc, act_size
   call memcpy
+
+  ld de, unit_demo_1
+  ld hl, p0_units + act_size
+  ld bc, act_size
+  call memcpy
   ret
 
 #include "default_map.s"
index 90d949d5c582436f44541a4a8eceb16018db7efb..6e13caf4e539892e9298087f2c9581387d9fafc8 100644 (file)
@@ -237,10 +237,12 @@ unit_try_move_right:
   ; 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
@@ -248,10 +250,14 @@ unit_use_move:
 
   ld a, [hl] ; current
   cp a, 0
+  
+  push_all
+  call z, unit_next
+  pop_all
   ret z
+
   dec a
   ld [hl], a
-  call ui_unit_need_draw
   ret
 
   ; centers the current scroll on the selected unit
@@ -343,6 +349,7 @@ unit_switch_to_idle:
   ; 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
@@ -352,8 +359,8 @@ unit_next:
   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 [unit_next_best_act_ptr], a
+  ld [unit_next_best_act_ptr+1], a
 
   ld hl, p0_units 
   ld b, UNITS_MAX * PLAYERS 
@@ -375,7 +382,9 @@ unit_next:
 
     ; 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
@@ -388,7 +397,6 @@ unit_next:
       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
@@ -401,9 +409,9 @@ unit_next:
         ld [unit_next_best_init], a
 
         ld a, l
-        ld [unit_next_bext_act_ptr], a
+        ld [unit_next_best_act_ptr], a
         ld a, h
-        ld [unit_next_bext_act_ptr+1], a
+        ld [unit_next_best_act_ptr+1], a
         
 @skip:
     pop hl ; restore table
@@ -415,11 +423,22 @@ unit_next:
 
   ; if here unit_next_best_act_ptr is still 0000
   ; reste all moves and try again
-  ld a, [unit_next_bext_act_ptr]
+  ld a, [unit_next_best_act_ptr]
   ld b, a
-  ld a, [unit_next_bext_act_ptr+1]
+  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
+  call unit_switch_to_active
+
   ret
 
 @retry:
index d3fd16776c94e6d10076adb7611de9d6ed1f3ca0..51e1fe8fac6cf394ea81462e9a70bfbc8e59f1c9 100644 (file)
@@ -18,7 +18,7 @@ game_mode: .adv st_size
 
 ; current best init found in units_next
 unit_next_best_init: .adv 1
-unit_next_bext_act_ptr: .adv 2
+unit_next_best_act_ptr: .adv 2
 
   ; actors 
   ; actors are state machines