Show correct health increase when hog health cap is reached
authorWuzzy <Wuzzy2@mail.ru>
Mon, 06 Aug 2018 22:39:39 +0200
changeset 13622 a6abc2c1fc1a
parent 13621 5fdb18e892c5
child 13623 edffc9b7ad58
Show correct health increase when hog health cap is reached
hedgewars/uGearsHedgehog.pas
hedgewars/uGearsUtils.pas
hedgewars/uScript.pas
--- a/hedgewars/uGearsHedgehog.pas	Mon Aug 06 21:41:06 2018 +0200
+++ b/hedgewars/uGearsHedgehog.pas	Mon Aug 06 22:39:39 2018 +0200
@@ -783,6 +783,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 procedure PickUp(HH, Gear: PGear);
 var ag, gi: PGear;
+    healthBoost: LongInt;
 begin
 if Gear^.State and gstFrozen <> 0 then exit;
 
@@ -829,14 +830,11 @@
                     end;
      posCaseHealth: begin
                     PlaySound(sndShotgunReload);
-                    inc(HH^.Health, Gear^.Health);
-                    // Prevent overflow
-                    if (HH^.Health < 0) or (HH^.Health > cMaxHogHealth) then
-                        HH^.Health:= cMaxHogHealth;
+                    healthBoost:= IncHogHealth(HH^.Hedgehog, Gear^.Health);
                     HH^.Hedgehog^.Effects[hePoisoned] := 0;
                     RenderHealth(HH^.Hedgehog^);
                     RecountTeamHealth(HH^.Hedgehog^.Team);
-                    HHHeal(HH^.Hedgehog, Gear^.Health, true);
+                    HHHeal(HH^.Hedgehog, healthBoost, true);
                     end;
      end
 end;
--- a/hedgewars/uGearsUtils.pas	Mon Aug 06 21:41:06 2018 +0200
+++ b/hedgewars/uGearsUtils.pas	Mon Aug 06 22:39:39 2018 +0200
@@ -32,8 +32,9 @@
 procedure ApplyDamage(Gear: PGear; AttackerHog: PHedgehog; Damage: Longword; Source: TDamageSource);
 procedure spawnHealthTagForHH(HHGear: PGear; dmg: Longword);
 procedure HHHurt(Hedgehog: PHedgehog; Source: TDamageSource);
-procedure HHHeal(Hedgehog: PHedgehog; healthBoost: Longword; showMessage: boolean; vgTint: Longword);
-procedure HHHeal(Hedgehog: PHedgehog; healthBoost: Longword; showMessage: boolean);
+procedure HHHeal(Hedgehog: PHedgehog; healthBoost: LongInt; showMessage: boolean; vgTint: Longword);
+procedure HHHeal(Hedgehog: PHedgehog; healthBoost: LongInt; showMessage: boolean);
+function IncHogHealth(Hedgehog: PHedgehog; healthBoost: LongInt): LongInt;
 procedure CheckHHDamage(Gear: PGear);
 procedure CalcRotationDirAngle(Gear: PGear);
 procedure ResurrectHedgehog(var gear: PGear);
@@ -290,10 +291,7 @@
                     begin
                     // was considering pulsing on attack, Tiy thinks it should be permanent while in play
                     //CurrentHedgehog^.Gear^.State:= CurrentHedgehog^.Gear^.State or gstVampiric;
-                    inc(CurrentHedgehog^.Gear^.Health,vampDmg);
-                    // Prevent overflow
-                    if (CurrentHedgehog^.Gear^.Health < 0) or (CurrentHedgehog^.Gear^.Health > cMaxHogHealth) then
-                        CurrentHedgehog^.Gear^.Health:= cMaxHogHealth;
+                    vampDmg:= IncHogHealth(CurrentHedgehog, vampDmg);
                     RenderHealth(CurrentHedgehog^);
                     RecountTeamHealth(CurrentHedgehog^.Team);
                     HHHeal(CurrentHedgehog, vampDmg, true, $FF0000FF);
@@ -378,9 +376,9 @@
 Hedgehog: Hedgehog which gets the health boost
 healthBoost: Amount of added health added
 showMessage: Whether to show announcer message
-vgTint: Tint of heal particle
+vgTint: Tint of heal particle (if 0, don't render particles)
 -}
-procedure HHHeal(Hedgehog: PHedgehog; healthBoost: Longword; showMessage: boolean; vgTint: Longword);
+procedure HHHeal(Hedgehog: PHedgehog; healthBoost: LongInt; showMessage: boolean; vgTint: Longword);
 var i: LongInt;
     vg: PVisualGear;
     s: ansistring;
@@ -396,25 +394,45 @@
 
     i:= 0;
     // One particle for every 5 HP. Max. 200 particles
-    while (i < healthBoost) and (i < 1000) do
-        begin
-        vg:= AddVisualGear(hwRound(Hedgehog^.Gear^.X), hwRound(Hedgehog^.Gear^.Y), vgtStraightShot);
-        if vg <> nil then
-            with vg^ do
-                begin
-                Tint:= vgTint;
-                State:= ord(sprHealth)
-                end;
-        inc(i, 5)
-        end;
+    if (vgTint <> 0) then
+        while (i < healthBoost) and (i < 1000) do
+            begin
+            vg:= AddVisualGear(hwRound(Hedgehog^.Gear^.X), hwRound(Hedgehog^.Gear^.Y), vgtStraightShot);
+            if vg <> nil then
+                with vg^ do
+                    begin
+                    Tint:= vgTint;
+                    State:= ord(sprHealth)
+                    end;
+            inc(i, 5)
+            end;
 end;
 
 // Shorthand for the same above, but with tint implied
-procedure HHHeal(Hedgehog: PHedgehog; healthBoost: Longword; showMessage: boolean);
+procedure HHHeal(Hedgehog: PHedgehog; healthBoost: LongInt; showMessage: boolean);
 begin
     HHHeal(Hedgehog, healthBoost, showMessage, $00FF00FF);
 end;
 
+// Increase hog health by healthBoost (at least 1).
+// Resulting health is capped at cMaxHogHealth.
+// Returns actual amount healed.
+function IncHogHealth(Hedgehog: PHedgehog; healthBoost: LongInt): LongInt;
+var oldHealth: LongInt;
+begin
+   if healthBoost < 1 then
+       begin
+       IncHogHealth:= 0;
+       exit;
+       end;
+   oldHealth:= Hedgehog^.Gear^.Health;
+   inc(Hedgehog^.Gear^.Health, healthBoost);
+   // Prevent overflow
+   if (Hedgehog^.Gear^.Health < 1) or (Hedgehog^.Gear^.Health > cMaxHogHealth) then
+       Hedgehog^.Gear^.Health:= cMaxHogHealth;
+   IncHogHealth:= Hedgehog^.Gear^.Health - oldHealth;
+end;
+
 procedure CheckHHDamage(Gear: PGear);
 var
     dmg: LongInt;
--- a/hedgewars/uScript.pas	Mon Aug 06 21:41:06 2018 +0200
+++ b/hedgewars/uScript.pas	Mon Aug 06 22:39:39 2018 +0200
@@ -1817,11 +1817,7 @@
         healthBoost:= Trunc(lua_tonumber(L, 2));
         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) and (healthBoost >= 1) then
             begin
-            inc(gear^.Health, healthBoost);
-            // Prevent overflow
-            if (gear^.Health < 0) or (gear^.Health > cMaxHogHealth) then
-                gear^.Health:= cMaxHogHealth;
-
+            healthBoost:= IncHogHealth(gear^.Hedgehog, healthBoost);
             RenderHealth(gear^.Hedgehog^);
             RecountTeamHealth(gear^.Hedgehog^.Team);
             if n = 4 then