Refactor collision mask checks, remove hardcoded numbers
authorWuzzy <Wuzzy2@mail.ru>
Tue, 10 Jul 2018 18:35:31 +0200
changeset 13470 7b4643ff60ea
parent 13469 f1d349a52bc7
child 13471 58dcb3803d30
Refactor collision mask checks, remove hardcoded numbers
hedgewars/uCollisions.pas
hedgewars/uConsts.pas
hedgewars/uGearsHandlersMess.pas
hedgewars/uGearsList.pas
--- a/hedgewars/uCollisions.pas	Tue Jul 10 18:00:04 2018 +0200
+++ b/hedgewars/uCollisions.pas	Tue Jul 10 18:35:31 2018 +0200
@@ -344,7 +344,7 @@
 if (Gear^.CollisionMask = lfNotCurHogCrate) and (Gear^.Kind <> gtHedgehog) and (Gear^.Hedgehog <> nil) and (Gear^.Hedgehog^.Gear <> nil) and
     ((hwRound(Gear^.Hedgehog^.Gear^.X) + Gear^.Hedgehog^.Gear^.Radius + 16 < hwRound(Gear^.X) - Gear^.Radius) or
      (hwRound(Gear^.Hedgehog^.Gear^.X) - Gear^.Hedgehog^.Gear^.Radius - 16 > hwRound(Gear^.X) + Gear^.Radius)) then
-    Gear^.CollisionMask:= $FFFF;
+    Gear^.CollisionMask:= lfAll;
 
 x:= hwRound(Gear^.X);
 if Dir < 0 then
@@ -373,7 +373,7 @@
 if (Gear^.CollisionMask = lfNotCurHogCrate) and (Gear^.Kind <> gtHedgehog) and (Gear^.Hedgehog <> nil) and (Gear^.Hedgehog^.Gear <> nil) and
     ((hwRound(Gear^.Hedgehog^.Gear^.Y) + Gear^.Hedgehog^.Gear^.Radius + 16 < hwRound(Gear^.Y) - Gear^.Radius) or
      (hwRound(Gear^.Hedgehog^.Gear^.Y) - Gear^.Hedgehog^.Gear^.Radius - 16 > hwRound(Gear^.Y) + Gear^.Radius)) then
-    Gear^.CollisionMask:= $FFFF;
+    Gear^.CollisionMask:= lfAll;
 
 y:= hwRound(Gear^.Y);
 if Dir < 0 then
@@ -417,7 +417,7 @@
             begin
             if Land[y, x] and Gear^.CollisionMask <> 0 then
                 begin
-                if Land[y, x] and Gear^.CollisionMask > 255 then
+                if ((Land[y, x] and Gear^.CollisionMask) and lfNotObjMask) <> 0 then
                     exit(Land[y, x] and Gear^.CollisionMask)
                 else
                     pixel:= Land[y, x] and Gear^.CollisionMask;
@@ -483,7 +483,7 @@
     if (x and LAND_WIDTH_MASK) = 0 then
         if Land[y, x] > 0 then
             begin
-            if Land[y, x] and Gear^.CollisionMask > 255 then
+            if ((Land[y, x] and Gear^.CollisionMask) and lfNotObjMask) <> 0 then
                 exit(Land[y, x] and Gear^.CollisionMask)
             else // if Land[y, x] <> 0 then
                 pixel:= Land[y, x] and Gear^.CollisionMask;
@@ -558,7 +558,7 @@
     i:= y + Gear^.Radius * 2 - 2;
     repeat
         if (y and LAND_HEIGHT_MASK) = 0 then
-            if Land[y, x] and Gear^.CollisionMask > 255 then
+            if ((Land[y, x] and Gear^.CollisionMask) and lfNotObjMask) <> 0 then
                 exit(Land[y, x] and Gear^.CollisionMask);
     inc(y)
     until (y > i);
@@ -581,7 +581,7 @@
     i:= x + Gear^.Radius * 2 - 2;
     repeat
         if (x and LAND_WIDTH_MASK) = 0 then
-            if Land[y, x] and Gear^.CollisionMask > 255 then
+            if ((Land[y, x] and Gear^.CollisionMask) and lfNotObjMask) <> 0 then
                 exit(Land[y, x] and Gear^.CollisionMask);
     inc(x)
     until (x > i);
@@ -897,7 +897,7 @@
     i:= x + Gear^.Radius * 2 - 2;
     repeat
     if (x and LAND_WIDTH_MASK) = 0 then
-        if Land[y, x] > 255 then
+        if (Land[y, x] and lfNotObjMask) <> 0 then
             if (not isColl) or (abs(x-gx) < abs(collX-gx)) then
                 begin
                 isColl:= true;
--- a/hedgewars/uConsts.pas	Tue Jul 10 18:00:04 2018 +0200
+++ b/hedgewars/uConsts.pas	Tue Jul 10 18:35:31 2018 +0200
@@ -138,6 +138,8 @@
     // consists of 0-127 counted for object checkins and $80 as a bit flag for current hog.
     lfAllObjMask     = $00FF;  // lfCurHogCrate or lfObjMask
 
+    lfAll            = $FFFF;  // everything
+
 
 
     cMaxPower     = 1500;
--- a/hedgewars/uGearsHandlersMess.pas	Tue Jul 10 18:00:04 2018 +0200
+++ b/hedgewars/uGearsHandlersMess.pas	Tue Jul 10 18:35:31 2018 +0200
@@ -3832,7 +3832,7 @@
     end;
 
     tempColl:= Gear^.CollisionMask;
-    Gear^.CollisionMask:= $007F;
+    Gear^.CollisionMask:= lfObjMask;
     if (TestCollisionYWithGear(Gear, hwSign(Gear^.dY)) <> 0) or (TestCollisionXWithGear(Gear, hwSign(Gear^.dX)) <> 0) or (GameTicks > Gear^.FlightTime) then
         t := CheckGearsCollision(Gear)
     else t := nil;
@@ -6588,7 +6588,7 @@
 var   a: real;
 begin
     // Gear is shrunk so it can actually escape the hog without carving into the terrain
-    if (Gear^.Radius = 4) and (Gear^.CollisionMask = $FFFF) then Gear^.Radius:= 7;
+    if (Gear^.Radius = 4) and (Gear^.CollisionMask = lfAll) then Gear^.Radius:= 7;
     if Gear^.Damage > 100 then Gear^.CollisionMask:= 0
     else if Gear^.Damage > 30 then
         if GetRandom(max(4,18-Gear^.Damage div 10)) < 3 then Gear^.CollisionMask:= 0;
--- a/hedgewars/uGearsList.pas	Tue Jul 10 18:00:04 2018 +0200
+++ b/hedgewars/uGearsList.pas	Tue Jul 10 18:35:31 2018 +0200
@@ -204,7 +204,7 @@
 gear^.Density:= _1;
 // Define ammo association, if any.
 gear^.AmmoType:= GearKindAmmoTypeMap[Kind];
-gear^.CollisionMask:= $FFFF;
+gear^.CollisionMask:= lfAll;
 gear^.Tint:= $FFFFFFFF;
 gear^.Data:= nil;