CheckGearDrowning could delete hedgehogs gear! (CheckGearDrowning -> ResurrectHedgehog -> FindPlace). Fixes bug 620.
authorunc0rr
Thu, 02 May 2013 21:56:24 +0400
changeset 8947 e906ebd59612
parent 8946 3e07b8ff2ae1
child 8948 c8a6f789398d
CheckGearDrowning could delete hedgehogs gear! (CheckGearDrowning -> ResurrectHedgehog -> FindPlace). Fixes bug #620.
hedgewars/uGearsHedgehog.pas
hedgewars/uGearsUtils.pas
--- a/hedgewars/uGearsHedgehog.pas	Thu May 02 21:55:03 2013 +0400
+++ b/hedgewars/uGearsHedgehog.pas	Thu May 02 21:56:24 2013 +0400
@@ -961,8 +961,11 @@
     begin
     Gear^.State:= Gear^.State and (not gstWinner);
     Gear^.State:= Gear^.State and (not gstMoving);
-    while (TestCollisionYWithGear(Gear,1) = 0) and (not CheckGearDrowning(Gear)) do
-        Gear^.Y:= Gear^.Y+_1;
+    while (TestCollisionYWithGear(Gear,1) = 0) and (not CheckGearDrowning(Gear)) and (Gear <> nil) do
+        Gear^.Y:= Gear^.Y + _1;
+
+    // could become nil in CheckGearDrowning if ai's hog fails to respawn in ai survival
+    if Gear = nil then exit;
     SetLittle(Gear^.dX);
     Gear^.dY:= _0
     end
@@ -982,7 +985,10 @@
         Gear^.dY:= _0;
         Gear^.Y:= Gear^.Y + _1
         end;
+
     CheckGearDrowning(Gear);
+    // could become nil if ai's hog fails to respawn in ai survival
+    if Gear = nil then exit;
     // hide target cursor if current hog is drowning
     if (Gear^.State and gstDrowning) <> 0 then
         if (CurrentHedgehog^.Gear = Gear) then
--- a/hedgewars/uGearsUtils.pas	Thu May 02 21:55:03 2013 +0400
+++ b/hedgewars/uGearsUtils.pas	Thu May 02 21:56:24 2013 +0400
@@ -31,13 +31,13 @@
 procedure HHHurt(Hedgehog: PHedgehog; Source: TDamageSource);
 procedure CheckHHDamage(Gear: PGear);
 procedure CalcRotationDirAngle(Gear: PGear);
-procedure ResurrectHedgehog(gear: PGear);
+procedure ResurrectHedgehog(var gear: PGear);
 
 procedure FindPlace(var Gear: PGear; withFall: boolean; Left, Right: LongInt); inline;
 procedure FindPlace(var Gear: PGear; withFall: boolean; Left, Right: LongInt; skipProximity: boolean);
 
 function  CheckGearNear(Gear: PGear; Kind: TGearType; rX, rY: LongInt): PGear;
-function  CheckGearDrowning(Gear: PGear): boolean;
+function  CheckGearDrowning(var Gear: PGear): boolean;
 procedure CheckCollision(Gear: PGear); inline;
 procedure CheckCollisionWithLand(Gear: PGear); inline;
 
@@ -337,7 +337,7 @@
         Gear^.DirAngle := Gear^.DirAngle - 360
 end;
 
-function CheckGearDrowning(Gear: PGear): boolean;
+function CheckGearDrowning(var Gear: PGear): boolean;
 var 
     skipSpeed, skipAngle, skipDecay: hwFloat;
     i, maxDrops, X, Y: LongInt;
@@ -390,7 +390,11 @@
                     if Gear^.Kind = gtHedgehog then
                         begin
                         if Gear^.Hedgehog^.Effects[heResurrectable] <> 0 then
-                            ResurrectHedgehog(Gear)
+                            begin
+                            // Gear could become nil after this, just exit to skip splashes
+                            ResurrectHedgehog(Gear);
+                            exit
+                            end
                         else
                             begin
                             Gear^.doStep := @doStepDrowningGear;
@@ -465,7 +469,7 @@
 end;
 
 
-procedure ResurrectHedgehog(gear: PGear);
+procedure ResurrectHedgehog(var gear: PGear);
 var tempTeam : PTeam;
     sparkles: PVisualGear;
     gX, gY: LongInt;
@@ -507,7 +511,7 @@
         RenderHealth(gear^.Hedgehog^);
         ScriptCall('onGearResurrect', gear^.uid);
         gear^.State := gstWait;
-    end;
+        end;
     RecountTeamHealth(tempTeam);
 end;