Ensure the flowers get placed within map bounds
authorWuzzy <Wuzzy2@mail.ru>
Mon, 03 Jun 2019 16:49:42 +0200
changeset 15128 b4a0dc4e85e8
parent 15127 f9d57bade2a5
child 15129 7a54df47b852
Ensure the flowers get placed within map bounds
hedgewars/uGears.pas
--- a/hedgewars/uGears.pas	Mon Jun 03 15:37:32 2019 +0200
+++ b/hedgewars/uGears.pas	Mon Jun 03 16:49:42 2019 +0200
@@ -958,7 +958,7 @@
 end;
 
 procedure AssignHHCoords;
-var i, t, p, j: LongInt;
+var i, t, p, j, x, y: LongInt;
     ar: array[0..Pred(cMaxHHs)] of PHedgehog;
     Count: Longword;
     divide, sectionDivide: boolean;
@@ -1059,13 +1059,15 @@
             with Hedgehogs[i] do
                 if (Gear <> nil) and (Gear^.State and gsttmpFlag <> 0) then
                     begin
-                    ForcePlaceOnLand(hwRound(Gear^.X) - SpritesData[sprTargetBee].Width div 2, 
-                                     hwRound(Gear^.Y) - SpritesData[sprTargetBee].Height div 2, 
-                                     sprTargetBee, 0, lfBasic, $FFFFFFFF, false, false, false);
-                    Gear^.Y:= int2hwFloat(hwRound(Gear^.Y) - 16 - Gear^.Radius);
+                    x:= hwRound(Gear^.X) - SpritesData[sprTargetBee].Width div 2;
+                    y:= hwRound(Gear^.Y) - SpritesData[sprTargetBee].Height div 2;
+                    x:= max(min(x, RightX - SpritesData[sprTargetBee].Width), LeftX);
+                    y:= max(y, TopY);
+                    ForcePlaceOnLand(x, y, sprTargetBee, 0, lfBasic, $FFFFFFFF, false, false, false);
+                    Gear^.Y:= int2hwFloat(hwRound(Gear^.Y) - (SpritesData[sprTargetBee].Height div 2) - Gear^.Radius);
                     AddCI(Gear);
                     Gear^.State:= Gear^.State and (not gsttmpFlag);
-                    AddFileLog('Placed flower for hog at coordinates (' + inttostr(hwRound(Gear^.X)) + ',' + inttostr(hwRound(Gear^.Y)) + ')')
+                    AddFileLog('Placed flower for hog at coordinates (' + inttostr(x) + ',' + inttostr(y) + ')')
                     end;