Don't waste CPU cycles trying to find a piece of land when there's no free land left
authorunc0rr
Mon, 10 Jan 2011 15:53:49 +0300
changeset 4830 c5ddc3e6c8a4
parent 4829 1d555e738a3a
child 4831 57b46b5dbfff
Don't waste CPU cycles trying to find a piece of land when there's no free land left
hedgewars/uGears.pas
--- a/hedgewars/uGears.pas	Mon Jan 10 15:35:22 2011 +0300
+++ b/hedgewars/uGears.pas	Mon Jan 10 15:53:49 2011 +0300
@@ -1094,15 +1094,22 @@
 begin
 AddGear(0, 0, gtATStartGame, 0, _0, _0, 2000);
 
-for i:= 0 to Pred(cLandMines) do
+i:= 0;
+Gear:= PGear(1);
+while (i < cLandMines) and (Gear <> nil) do
     begin
     Gear:= AddGear(0, 0, gtMine, 0, _0, _0, 0);
     FindPlace(Gear, false, 0, LAND_WIDTH);
+    inc(i)
     end;
-for i:= 0 to Pred(cExplosives) do
+
+i:= 0;
+Gear:= PGear(1);
+while (i < cExplosives) and (Gear <> nil) do
     begin
     Gear:= AddGear(0, 0, gtExplosives, 0, _0, _0, 0);
     FindPlace(Gear, false, 0, LAND_WIDTH);
+    inc(i)
     end;
 
 if (GameFlags and gfLowGravity) <> 0 then
@@ -1426,12 +1433,13 @@
 var
     t: PGear;
 begin
+    r:= r*r;
     GearsNear := nil;
     t := GearsList;
     while t <> nil do begin
         if (t^.Kind = Kind) then begin
             if (X - t^.X)*(X - t^.X) + (Y - t^.Y)*(Y-t^.Y) <
-                int2hwFloat(r)*int2hwFloat(r) then
+                int2hwFloat(r) then
             begin
                 SetLength(GearsNear, Length(GearsNear)+1);
                 GearsNear[High(GearsNear)] := t;