hedgewars/uGearsUtils.pas
changeset 15897 7f3d7f015aa5
parent 15847 2839b68a3732
child 15900 128ace913837
child 15956 7125918637e9
equal deleted inserted replaced
15896:d2dafe1b25f5 15897:7f3d7f015aa5
  1078     end
  1078     end
  1079 end;
  1079 end;
  1080 
  1080 
  1081 function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt; exclude: PGear): PGear;
  1081 function CheckGearNearImpl(Kind: TGearType; X, Y: hwFloat; rX, rY: LongInt; exclude: PGear): PGear;
  1082 var t: PGear;
  1082 var t: PGear;
  1083     width, bound, dX, dY: hwFloat;
  1083     width, dX, dY: hwFloat;
  1084     isHit: Boolean;
  1084     isHit: Boolean;
  1085     i, j: LongWord;
  1085     i, j, bound: LongWord;
  1086 begin
  1086 begin
  1087     bound:= _1_5 * int2hwFloat(max(rX, rY));
  1087     bound:= max(rX, rY) * 3 div 2;
  1088     rX:= sqr(rX);
  1088     rX:= sqr(rX);
  1089     rY:= sqr(rY);
  1089     rY:= sqr(rY);
  1090     width:= int2hwFloat(RightX - LeftX);
  1090     width:= int2hwFloat(RightX - LeftX);
  1091     if (Kind = gtHedgehog) then
  1091     if (Kind = gtHedgehog) then
  1092         begin
  1092         begin
  1098                             if (not Unplaced) and (Gear <> nil) and (Gear <> exclude) then
  1098                             if (not Unplaced) and (Gear <> nil) and (Gear <> exclude) then
  1099                                 begin
  1099                                 begin
  1100                                 // code duplication - could throw into an inline function I guess
  1100                                 // code duplication - could throw into an inline function I guess
  1101                                 dX := X - Gear^.X;
  1101                                 dX := X - Gear^.X;
  1102                                 dY := Y - Gear^.Y;
  1102                                 dY := Y - Gear^.Y;
  1103                                 isHit := (hwAbs(dX) + hwAbs(dY) < bound)
  1103                                 isHit := (dX.Round + dY.Round < bound)
  1104                                     and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
  1104                                     and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
  1105 
  1105 
  1106                                 if (not isHit) and (WorldEdge = weWrap) then
  1106                                 if (not isHit) and (WorldEdge = weWrap) then
  1107                                     begin
  1107                                     begin
  1108                                     if (hwAbs(dX - width) + hwAbs(dY) < bound)
  1108                                     if ((dX - width).Round + dY.Round < bound)
  1109                                         and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1109                                         and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1110                                         isHit := true
  1110                                         isHit := true
  1111                                     else if (hwAbs(dX + width) + hwAbs(dY) < bound)
  1111                                     else if ((dX + width).Round + dY.Round < bound)
  1112                                         and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1112                                         and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1113                                         isHit := true
  1113                                         isHit := true
  1114                                     end;
  1114                                     end;
  1115 
  1115 
  1116                                 if isHit then
  1116                                 if isHit then
  1128             begin
  1128             begin
  1129             if (t <> exclude) and (t^.Kind = Kind) then
  1129             if (t <> exclude) and (t^.Kind = Kind) then
  1130                 begin
  1130                 begin
  1131                 dX := X - t^.X;
  1131                 dX := X - t^.X;
  1132                 dY := Y - t^.Y;
  1132                 dY := Y - t^.Y;
  1133                 isHit := (hwAbs(dX) + hwAbs(dY) < bound)
  1133                 isHit := (dX.Round + dY.Round < bound)
  1134                     and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
  1134                     and (not ((hwSqr(dX) / rX + hwSqr(dY) / rY) > _1));
  1135 
  1135 
  1136                 if (not isHit) and (WorldEdge = weWrap) then
  1136                 if (not isHit) and (WorldEdge = weWrap) then
  1137                     begin
  1137                     begin
  1138                     if (hwAbs(dX - width) + hwAbs(dY) < bound)
  1138                     if ((dX - width).Round + dY.Round < bound)
  1139                         and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1139                         and (not ((hwSqr(dX - width) / rX + hwSqr(dY) / rY) > _1)) then
  1140                         isHit := true
  1140                         isHit := true
  1141                     else if (hwAbs(dX + width) + hwAbs(dY) < bound)
  1141                     else if ((dX + width).Round + dY.Round < bound)
  1142                         and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1142                         and (not ((hwSqr(dX + width) / rX + hwSqr(dY) / rY) > _1)) then
  1143                         isHit := true
  1143                         isHit := true
  1144                     end;
  1144                     end;
  1145 
  1145 
  1146                 if isHit then
  1146                 if isHit then