hedgewars/uGearsUtils.pas
changeset 13622 a6abc2c1fc1a
parent 13614 13f68f3e7153
child 13632 605767bbd022
--- 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;