hedgewars/uLandObjects.pas
changeset 15161 84e98f0f7f7b
parent 15147 d97f0b96e45f
child 15345 4e91a7050add
--- a/hedgewars/uLandObjects.pas	Sat Jun 15 17:00:43 2019 +0200
+++ b/hedgewars/uLandObjects.pas	Sat Jun 15 18:15:32 2019 +0200
@@ -619,7 +619,8 @@
 
 function TryPut2(var Obj: TSprayObject; Surface: PSDL_Surface): boolean;
 const MaxPointsIndex = 8095;
-var x, y: Longword;
+var x, y, xStart, yStart: Longword;
+    xWraps, yWraps: Byte;
     ar: array[0..MaxPointsIndex] of TPoint;
     cnt, i: Longword;
     r: TSDL_Rect;
@@ -631,13 +632,20 @@
     begin
     if Maxcnt = 0 then
         exit;
-    x:= 0;
+    xWraps:= 0;
+    yWraps:= 0;
+    // Start at random coordinates
+    xStart:= getrandom(LAND_WIDTH - Width);
+    yStart:= 8 + getrandom(LAND_HEIGHT - Height - 16);
+    x:= xStart;
+    y:= yStart;
     r.x:= 0;
     r.y:= 0;
     r.w:= Width;
     r.h:= Height + 16;
+    // Then iterate through the whole map; this requires we wrap one time per axis
     repeat
-        y:= 8;
+        yWraps:= 0;
         repeat
             if CheckLand(r, x, y - 8, lfBasic)
             and (not CheckIntersect(x, y, Width, Height)) then
@@ -652,9 +660,19 @@
                     else inc(cnt);
                 end;
             inc(y, 12);
-        until y >= LAND_HEIGHT - Height - 8;
-        inc(x, getrandom(12) + 12)
-    until x >= LAND_WIDTH - Width;
+            if (y >= LAND_HEIGHT - Height - 8) or ((yWraps > 0) and (y >= yStart)) then
+                begin
+                inc(yWraps);
+                y:= 8;
+                end;
+        until yWraps > 1;
+        inc(x, getrandom(12) + 12);
+        if (x >= LAND_WIDTH - Width) or ((xWraps > 0) and (x >= xStart)) then
+            begin
+            inc(xWraps);
+            x:= 0;
+            end;
+    until xWraps > 1;
     bRes:= cnt <> 0;
     if bRes then
         begin