diff --git a/Moose Development/Moose/Core/Point.lua b/Moose Development/Moose/Core/Point.lua index 6ffe65e16..5139d50a2 100644 --- a/Moose Development/Moose/Core/Point.lua +++ b/Moose Development/Moose/Core/Point.lua @@ -462,7 +462,31 @@ do -- COORDINATE end return set + end + + --- Find the closest unit to the COORDINATE within a certain radius. + -- @param #COORDINATE self + -- @param #number radius Scan radius in meters. Default 100 m. + -- @return Wrapper.Unit#UNIT The closest unit or #nil if no unit is inside the given radius. + function COORDINATE:FindClosestUnit(radius) + + local units=self:ScanUnits(radius) + + local umin=nil --Wrapper.Unit#UNIT + local dmin=math.huge + for _,_unit in pairs(units.Set) do + local unit=_unit --Wrapper.Unit#UNIT + local coordinate=unit:GetCoordinate() + local d=self:Get2DDistance(coordinate) + if d0 then + self:ScheduleOnce(delay, FLIGHTGROUP._UpdateMenu, self) + else - -- Get all FLIGHTCONTROLS - local fc={} - for airbasename,_flightcontrol in pairs(_DATABASE.FLIGHTCONTROLS) do - - local airbase=AIRBASE:FindByName(airbasename) + self:I(self.lid.."FF updating menu") - local coord=airbase:GetCoordinate() - - local dist=coord:Get2DDistance(position) - - local fcitem={airbasename=airbasename, dist=dist} - - table.insert(fc, fcitem) - end + -- Get current position of group. + local position=self.group:GetCoordinate() - -- Sort table wrt distance to airbases. - local function _sort(a,b) - return a.dist