# HG changeset patch # User alfadur # Date 1539463763 -10800 # Node ID 9954f9b800f5f0c96eb45ce2c3c9bfbb45c495da # Parent 7163f5ec3ce2a951699252337119e41db5c57aa3 improve stability for checks diff -r 7163f5ec3ce2 -r 9954f9b800f5 hedgewars/uGearsUtils.pas --- a/hedgewars/uGearsUtils.pas Sat Oct 13 23:08:15 2018 +0300 +++ b/hedgewars/uGearsUtils.pas Sat Oct 13 23:49:23 2018 +0300 @@ -1057,14 +1057,15 @@ function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt, exclude: PGear): PGear; begin var t: PGear; - width, halfWidth, dX, dY: hwFloat; + width, dX, dY: hwFloat; + bound: LongInt; isHit: Boolean; begin t:= GearsList; + bound:= _1_5 * int2hwFloat(max(rX, rY)); rX:= sqr(rX); rY:= sqr(rY); width:= int2hwFloat(RightX - LeftX); - halfWidth:= width / _2; while t <> nil do begin @@ -1072,13 +1073,16 @@ begin dX := X - t^.X; dY := Y - t^.Y; - isHit := not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1); + isHit := (hwAbs(dX) + hwAbs(dY) < bound) + and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1)); if (not isHit) and (WorldEdge = weWrap) then begin - if (dX > halfWidth) and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then + if (hwAbs(dX - width) + hwAbs(dY) < bound) + and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then isHit := true - else if (dX < -halfWidth) and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then + else if (hwAbs(dX + width) + hwAbs(dY) < bound) + and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then isHit := true end;