act_save: actsave now stores mp and hp
authorLukas Krickl <lukas@krickl.dev>
Fri, 22 Aug 2025 15:53:09 +0000 (17:53 +0200)
committerLukas Krickl <lukas@krickl.dev>
Fri, 22 Aug 2025 15:53:09 +0000 (17:53 +0200)
Added damage calculation to attack.

Fixed a bug in act_sg that caused the previous slot to be re-read by the wrong map.
This was fixed by storign the previous frame's player map cursor. This allows us to use the previous frame
as a save location and the current value as a restore location.

src/action.s
src/actortables.s
src/actsave.s
src/defs.s
src/stats.s
src/unit_cpu.s
src/update.s
src/wram.s

index d8a9c8c9cccee193be22e96844fd6b5a1f7c22a2..21adf4afd7f9d17996b85b030b03d594e36c7a5b 100644 (file)
@@ -223,15 +223,19 @@ unit_action_attack:
 unit_action_attack_damage_calc:
        ; TODO: do real damage calculation here
        ; for now just delete the actor
-
+       
+       push de
        call unit_attack_get_attack_tile
        call unit_find_at
 
-       ; set type to NULL
-       ld de, act_type
-       add hl, de
-       ld a, ACT_T_NULL
-       ld [hl], a
+       pop de
+       push hl
+       pop bc
+       ; de = attacker
+       ; bc = attacked unit
+       push hl 
+       call stat_calc_physical_damage_vs
+       pop hl
        
        ldnull bc
        ret
index c528f78b9ed32c80acd7fe2f6707b89ad75a57f7..a4b668d28618e122249dfc1cf400a0d85a0472a5 100644 (file)
@@ -11,7 +11,7 @@
 floor_1_actor_table:
 map_c_actor_table:
 .db 7 ; size
-dw unit_demo_2
+dw unit_demo_warrior
 dw unit_demo_warrior
 dw unit_demo_warrior
 dw unit_demo_mage
index f632eef8fb26260549394d8b3663deadb1d599b3..edd6598ae0006a9f668305c5881eea88ca4ebf65 100644 (file)
@@ -9,11 +9,11 @@ act_save_init:
        
        ; loads current start of save game slot
        ; based on the current map seed index
+       ; inputs:
+       ;               a: map cursor
        ; returns:
        ;               bc: act save game slot
 act_sg_load_current_slot:
-       ld a, [player_map_cursor] ; a = seed index
-
        ld hl, act_sg
        ; no need to loop if a is 0
        cp a, 0
@@ -28,6 +28,7 @@ act_sg_load_current_slot:
 @done:
        push hl
        pop bc ; we want return value in bc
+       BREAK
 
        ret
 
@@ -41,6 +42,8 @@ act_sg_store:
        ld a, [map_header+1]
        or a, b
        ret z
+
+       ld a, [player_map_cursor_prev]
        call act_sg_load_current_slot
 
        ld hl, p0_units
@@ -91,6 +94,26 @@ act_sg_store_single:
        inc bc
        pop hl
 
+       ; store hp
+       push hl
+       ld de, act_hp
+       add hl, de
+
+       ld a, [hl]
+       ld [bc], a
+       inc bc
+       pop hl
+
+       ; sotre mp 
+       push hl
+       ld de, act_mp
+       add hl, de
+
+       ld a, [hl]
+       ld [bc], a
+       inc bc
+       pop hl
+
        ; advance to next actor
        ld de, act_size
        add hl, de
@@ -100,6 +123,7 @@ act_sg_store_single:
        ; restores actor save game based on
        ; current map's seed offset
 act_sg_restore:
+       ld a, [player_map_cursor]
        call act_sg_load_current_slot 
        ; skip over player
 .rep i, act_sg_size, 1, inc bc
@@ -156,6 +180,27 @@ act_sg_restore_single:
 
        pop hl
 
+       ; load hp
+       push hl
+       ld de, act_hp
+       add hl, de
+
+       ld a, [bc]
+       ld [hl], a
+       inc bc
+
+       pop hl
+
+       ; load mp
+       push hl
+       ld de, act_mp
+       add hl, de
+
+       ld a, [bc]
+       ld [hl], a
+       inc bc
+       pop hl
+
        ld de, act_size
        add hl, de ; next actor
        ret
index 849af37b860c2c0b357f627a1acc02de17e688b9..f2182dc0c00b89737c919cd38534bccc92e99c8d 100644 (file)
 .de act_sg_pos_y, 1
 .de act_sg_pos_x, 1
 .de act_sg_p0, 1
+.de act_sg_hp, 1
+.de act_sg_mana, 1
 .de act_sg_size, 0
 
 #define ACT_SG_DEFAULT_VALUE 0xFF
index fbeccb00b78c71851a573bf2db7d1d9adefd4fa7..ffac5a3b805e9638d5dc7f56a0c0e0b6c822a633 100644 (file)
@@ -74,6 +74,7 @@ stat_calc_hp_max:
        ;               a: damage
 stat_calc_physical_damage_vs:
        ; actor 1
+       push bc
 
        call stat_calc_weapon_damage
        ; a = damage
@@ -81,19 +82,18 @@ stat_calc_physical_damage_vs:
 
        ; weapon damage + strenth / 8
        call stat_calc_str
-       sla a ; / 2
-       sla a ; / 4
-       sla a ; / 8
+       sra a ; / 2
+       sra a ; / 4
+       sra a ; / 8
        add a, b
        ld b, a ; b = real damage
 
        ; actor 2
        
-       push bc
        pop de ; de = actor2 now
        ; damage - ac/2
        call stat_calc_ac
-       sla a
+       sra a
        sub_ba
        jp c, @no_damage
 
@@ -110,6 +110,9 @@ stat_calc_physical_damage_vs:
 
        ; write new hp
        ld [hl], a
+       
+       ; a = damage dealt
+       ld a, b 
 
        ret
 @no_damage:
@@ -118,6 +121,7 @@ stat_calc_physical_damage_vs:
 @dead:
        xor a, a
        ld [hl], a ; write 0 hp
+       ld a, b ; a = damage dealt
        ret
 
   ; calculates the real speed state
index 2325cad277580fed04dc466c73a1fa1ca3a9b2c9..e6c2af40308ae55b74483463da7ad5f2dc9e3f43 100644 (file)
@@ -1,5 +1,27 @@
 #define UNIT_SCAN_RANGE_Y MAP_H/4
 #define UNIT_SCAN_RANGE_X MAP_W/4
+       
+       ; checks if the unit is dead
+       ; if so sets type to 0
+       ;       inputs:
+       ;               de: actor
+       ;       returns:
+       ;               z-flag if dead
+unit_cpu_check_dead:
+       ld hl, act_hp
+       add hl, de
+       ld a, [hl]
+       cp a, 0
+       ret nz ; not dead
+       push af
+
+       ld a, ACT_T_NULL
+       ld hl, act_type
+       add hl, de
+       ld [hl], a ; set type to NULL
+       
+       pop af
+       ret
 
        ; handles cpu inputs
        ; chaser CPU script 
@@ -9,6 +31,11 @@
        ;               bc: next state
 unit_handle_cpu_inputs:
 #define MOVE_MADE scratch 
+
+       ; check if dead
+       ; if so set type to 0
+       call unit_cpu_check_dead
+       ret z
        
        ; check if player has taken turn
        ; if not exit
index 5cad867d2edbc78b438c3d7e8aba43254b5b5230..37e862f961fa377501fcbb90f8f9c2847a98b1a5 100644 (file)
@@ -23,6 +23,9 @@ update_game:
        ld de, objanim
        call st_update
 
+       ld a, [player_map_cursor]
+       ld [player_map_cursor_prev], a
+
   ldnull bc
   ret
 
index d157d9cd30e0f3ed0ae5309b53d55cfc3a6bf71a..3537764c9fcc8757bb429016cba89be1cce738f7 100644 (file)
@@ -161,6 +161,8 @@ p0_units: .adv act_size * UNITS_MAX
        ; go left: dec
        ;       go right: inc
 player_map_cursor: .adv 1
+       ; the previous frame's map cursor
+player_map_cursor_prev: .adv 1 
        
        ; one byte per map
        ; indicating where doors should be placed