Optimize doStepSnowflake a bit, losing backwards compatibility
authorunC0Rr
Fri, 09 Dec 2022 08:18:50 +0100
changeset 15927 046182f1080b
parent 15926 7f3d7f015aa5
child 15928 73cdc306888f
Optimize doStepSnowflake a bit, losing backwards compatibility
hedgewars/uGearsHandlersMess.pas
--- a/hedgewars/uGearsHandlersMess.pas	Thu Dec 01 14:23:48 2022 +0100
+++ b/hedgewars/uGearsHandlersMess.pas	Fri Dec 09 08:18:50 2022 +0100
@@ -834,13 +834,14 @@
     s: PSDL_Surface;
     p: PLongwordArray;
     lf: LongWord;
+    oldY: hwFloat;
 begin
-inc(Gear^.Pos);
 gun:= (Gear^.State and gstTmpFlag) <> 0;
 move:= false;
 draw:= false;
 if gun then
     begin
+    inc(Gear^.Pos);
     Gear^.State:= Gear^.State and (not gstInvisible);
     doStepFallingGear(Gear);
     CheckCollision(Gear);
@@ -857,12 +858,13 @@
         end
     end
 else if GameTicks and $7 = 0 then
-    begin
     with Gear^ do
         begin
+        inc(Pos, 8);
         State:= State and (not gstInvisible);
+        oldY:= Y;
         X:= X + cWindSpeed * 3200 + dX;
-        Y:= Y + dY + cGravity * vobFallSpeed * 8;  // using same value as flakes to try and get similar results
+        Y:= Y + dY + cGravity * (vobFallSpeed * 8);  // using same value as flakes to try and get similar results
         xx:= hwRound(X);
         yy:= hwRound(Y);
         if vobVelocity <> 0 then
@@ -878,7 +880,7 @@
             move:= true
         else if (xx > snowRight) or (xx < snowLeft) then
             move:=true
-        else if (cGravity < _0) and (yy < LAND_HEIGHT-1200) then
+        else if (cGravity.isNegative) and (yy < LAND_HEIGHT-1200) then
             move:=true
         // Solid pixel encountered
         else if ((yy and LAND_HEIGHT_MASK) = 0) and ((xx and LAND_WIDTH_MASK) = 0) and (Land[yy, xx] <> 0) then
@@ -891,33 +893,40 @@
                 X:= X - cWindSpeed * 1600 - dX;
                 end
             // If there's room below, on the sides, fill the gaps
-            else if (((yy-1) and LAND_HEIGHT_MASK) = 0) and (((xx-(1*hwSign(cWindSpeed))) and LAND_WIDTH_MASK) = 0) and (Land[yy-1, (xx-(1*hwSign(cWindSpeed)))] = 0) then
-                begin
-                X:= X - _0_8 * hwSign(cWindSpeed);
-                Y:= Y - dY - cGravity * vobFallSpeed * 8;
-                end
-            else if (((yy-1) and LAND_HEIGHT_MASK) = 0) and (((xx-(2*hwSign(cWindSpeed))) and LAND_WIDTH_MASK) = 0) and (Land[yy-1, (xx-(2*hwSign(cWindSpeed)))] = 0) then
-                begin
-                X:= X - _0_8 * 2 * hwSign(cWindSpeed);
-                Y:= Y - dY - cGravity * vobFallSpeed * 8;
-                end
-            else if (((yy-1) and LAND_HEIGHT_MASK) = 0) and (((xx+(1*hwSign(cWindSpeed))) and LAND_WIDTH_MASK) = 0) and (Land[yy-1, (xx+(1*hwSign(cWindSpeed)))] = 0) then
-                begin
-                X:= X + _0_8 * hwSign(cWindSpeed);
-                Y:= Y - dY - cGravity * vobFallSpeed * 8;
-                end
-            else if (((yy-1) and LAND_HEIGHT_MASK) = 0) and (((xx+(2*hwSign(cWindSpeed))) and LAND_WIDTH_MASK) = 0) and (Land[yy-1, (xx+(2*hwSign(cWindSpeed)))] = 0) then
-                begin
-                X:= X + _0_8 * 2 * hwSign(cWindSpeed);
-                Y:= Y - dY - cGravity * vobFallSpeed * 8;
-                end
+            else if (((yy-1) and LAND_HEIGHT_MASK) = 0) then 
+		    begin
+		    if (((xx - 1) and LAND_WIDTH_MASK) = 0) and (Land[yy - 1, (xx - 1)] = 0) then
+		        begin
+		        X:= X - _0_8;
+		        Y:= oldY;
+		        end
+		    else if (((xx - 2) and LAND_WIDTH_MASK) = 0) and (Land[yy - 1, (xx - 2)] = 0) then
+		        begin
+		        X:= X - _1_6;
+		        Y:= oldY;
+		        end
+		    else if (((xx + 1) and LAND_WIDTH_MASK) = 0) and (Land[yy - 1, (xx + 1)] = 0) then
+		        begin
+		        X:= X + _0_8;
+		        Y:= oldY;
+		        end
+		    else if (((xx + 2) and LAND_WIDTH_MASK) = 0) and (Land[yy - 1, (xx + 2)] = 0) then
+		        begin
+		        X:= X + _1_6;
+		        Y:= oldY;
+		        end else
+		    if ((((yy+1) and LAND_HEIGHT_MASK) = 0) and ((Land[yy + 1, xx] and $FF) <> 0)) then 
+		       move:=true 
+		    else 
+		       draw:= true
+		    end
             // if there's an hog/object below do nothing
             else if ((((yy+1) and LAND_HEIGHT_MASK) = 0) and ((Land[yy+1, xx] and $FF) <> 0))
                 then move:=true
             else draw:= true
             end
-        end
-    end;
+        end;
+
 if draw then
     with Gear^ do
         begin
@@ -926,7 +935,6 @@
         if (Pos > 20) and ((CurAmmoGear = nil)
         or (CurAmmoGear^.Kind <> gtRope)) then
             begin
-////////////////////////////////// TODO - ASK UNC0RR FOR A GOOD HOME FOR THIS ////////////////////////////////////
             if not gun then
                 begin
                 dec(yy,3);
@@ -969,9 +977,6 @@
                 p:= PLongWordArray(@(p^[s^.pitch shr 2]))
                 end;
 
-            // Why is this here.  For one thing, there's no test on +1 being safe.
-            //Land[py, px+1]:= lfBasic;
-
             if allpx then
                 UpdateLandTexture(xx, Pred(s^.h), yy, Pred(s^.w), true)
             else
@@ -983,7 +988,6 @@
                     min(LAND_HEIGHT - yy, Pred(s^.h)), false // could this be true without unnecessarily creating blanks?
                 );
                 end;
-////////////////////////////////// TODO - ASK UNC0RR FOR A GOOD HOME FOR THIS ////////////////////////////////////
             end
         end;
 
@@ -996,7 +1000,7 @@
         end;
     Gear^.Pos:= 0;
     Gear^.X:= int2hwFloat(LongInt(GetRandom(snowRight - snowLeft)) + snowLeft);
-    if (cGravity < _0) and (yy < LAND_HEIGHT-1200) then
+    if (cGravity.isNegative) and (yy < LAND_HEIGHT-1200) then
          Gear^.Y:= int2hwFloat(LAND_HEIGHT - 50 - LongInt(GetRandom(50)))
     else Gear^.Y:= int2hwFloat(LAND_HEIGHT + LongInt(GetRandom(50)) - 1250);
     Gear^.State:= Gear^.State or gstInvisible;