diff --git a/Moose Development/Moose/Ops/FlightGroup.lua b/Moose Development/Moose/Ops/FlightGroup.lua index d5fae6836..40c740a02 100644 --- a/Moose Development/Moose/Ops/FlightGroup.lua +++ b/Moose Development/Moose/Ops/FlightGroup.lua @@ -25,7 +25,7 @@ -- @field #number ceiling Max altitude the aircraft can fly at in meters. -- @field #number tankertype The refueling system type (0=boom, 1=probe), if the group is a tanker. -- @field #number refueltype The refueling system type (0=boom, 1=probe), if the group can refuel from a tanker. --- @field #FLIGHTGROUP.Ammo ammo Ammunition data. Number of Guns, Rockets, Bombs, Missiles. +-- @field #FLIGHTGROUP.Ammo ammo Ammunition data. Number of Guns, Rockets, Bombs, Missiles. -- @field #boolean ai If true, flight is purely AI. If false, flight contains at least one human player. -- @field #boolean fuellow Fuel low switch. -- @field #number fuellowthresh Low fuel threshold in percent. @@ -42,7 +42,7 @@ -- @field #table menu F10 radio menu. -- @field #string controlstatus Flight control status. -- @field #boolean ishelo If true, the is a helicopter group. --- +-- -- @extends Ops.OpsGroup#OPSGROUP --- *To invent an airplane is nothing. To build one is something. To fly is everything.* -- Otto Lilienthal @@ -54,78 +54,78 @@ -- # The FLIGHTGROUP Concept -- -- # Events --- +-- -- This class introduces a lot of additional events that will be handy in many situations. -- Certain events like landing, takeoff etc. are triggered for each element and also have a corresponding event when the whole group reaches this state. --- +-- -- ## Spawning --- +-- -- ## Parking --- +-- -- ## Taxiing --- +-- -- ## Takeoff --- +-- -- ## Airborne --- +-- -- ## Landed --- +-- -- ## Arrived --- +-- -- ## Dead --- +-- -- ## Fuel --- +-- -- ## Ammo --- +-- -- ## Detected Units --- +-- -- ## Check In Zone --- +-- -- ## Passing Waypoint --- --- +-- +-- -- # Tasking --- +-- -- The FLIGHTGROUP class significantly simplifies the monitoring of DCS tasks. Two types of tasks can be set --- +-- -- * **Scheduled Tasks** -- * **Waypoint Tasks** --- +-- -- ## Scheduled Tasks --- +-- -- ## Waypoint Tasks --- +-- -- # Missions --- +-- -- ## Anti-ship --- +-- -- ## AWACS --- +-- -- ## INTERCEPT --- --- +-- +-- -- # Examples --- +-- -- Here are some examples to show how things are done. --- +-- -- ## 1. Spawn --- +-- -- ## 2. Attack Group --- +-- -- ## 3. Whatever --- +-- -- ## 4. Simple Tanker --- +-- -- ## 5. Simple AWACS --- +-- -- ## 6. Scheduled Tasks --- +-- -- ## 7. Waypoint Tasks --- +-- -- ## 8. Enroute Tasks --- --- +-- +-- -- -- -- @field #FLIGHTGROUP @@ -142,7 +142,7 @@ FLIGHTGROUP = { fuellow = false, fuellowthresh = nil, fuellowrtb = nil, - fuelcritical = nil, + fuelcritical = nil, fuelcriticalthresh = nil, fuelcriticalrtb = false, squadron = nil, @@ -242,7 +242,7 @@ function FLIGHTGROUP:New(group) -- Inherit everything from FSM class. local self=BASE:Inherit(self, OPSGROUP:New(group)) -- #FLIGHTGROUP - + -- Set some string id for output to DCS.log file. self.lid=string.format("FLIGHTGROUP %s | ", self.groupname) @@ -253,34 +253,34 @@ function FLIGHTGROUP:New(group) self:SetDefaultROT() self:SetDetection() - -- Holding flag. + -- Holding flag. self.flaghold=USERFLAG:New(string.format("%s_FlagHold", self.groupname)) self.flaghold:Set(0) -- Add FSM transitions. - -- From State --> Event --> To State + -- From State --> Event --> To State self:AddTransition("*", "RTB", "Inbound") -- Group is returning to destination base. self:AddTransition("*", "RTZ", "Inbound") -- Group is returning to destination zone. Not implemented yet! self:AddTransition("Inbound", "Holding", "Holding") -- Group is in holding pattern. - + self:AddTransition("*", "Refuel", "Going4Fuel") -- Group is send to refuel at a tanker. Not implemented yet! self:AddTransition("Going4Fuel", "Refueled", "Airborne") -- Group is send to refuel at a tanker. Not implemented yet! - + self:AddTransition("*", "LandAt", "LandingAt") -- Helo group is ordered to land at a specific point. self:AddTransition("LandingAt", "LandedAt", "LandedAt") -- Helo group landed landed at a specific point. self:AddTransition("*", "Wait", "Waiting") -- Group is orbiting. - + self:AddTransition("*", "FuelLow", "*") -- Fuel state of group is low. Default ~25%. self:AddTransition("*", "FuelCritical", "*") -- Fuel state of group is critical. Default ~10%. - + self:AddTransition("*", "OutOfMissilesAA", "*") -- Group is out of A2A missiles. Not implemented yet! self:AddTransition("*", "OutOfMissilesAG", "*") -- Group is out of A2G missiles. Not implemented yet! self:AddTransition("*", "OutOfMissilesAS", "*") -- Group is out of A2G missiles. Not implemented yet! self:AddTransition("Airborne", "EngageTargets", "Engaging") -- Engage targets. self:AddTransition("Engaging", "Disengage", "Airborne") -- Engagement over. - + self:AddTransition("*", "ElementParking", "*") -- An element is parking. self:AddTransition("*", "ElementEngineOn", "*") -- An element spooled up the engines. @@ -292,8 +292,8 @@ function FLIGHTGROUP:New(group) self:AddTransition("*", "ElementOutOfAmmo", "*") -- An element is completely out of ammo. - - + + self:AddTransition("*", "FlightParking", "Parking") -- The whole flight group is parking. self:AddTransition("*", "FlightTaxiing", "Taxiing") -- The whole flight group is taxiing. self:AddTransition("*", "FlightTakeoff", "Airborne") -- The whole flight group is airborne. @@ -323,10 +323,10 @@ function FLIGHTGROUP:New(group) BASE:TraceClass(self.ClassName) BASE:TraceLevel(1) end - + -- Add to data base. _DATABASE:AddFlightGroup(self) - + -- Handle events: self:HandleEvent(EVENTS.Birth, self.OnEventBirth) self:HandleEvent(EVENTS.EngineStartup, self.OnEventEngineStartup) @@ -341,7 +341,7 @@ function FLIGHTGROUP:New(group) -- Init waypoints. self:InitWaypoints() - + -- Initialize group. self:_InitGroup() @@ -389,7 +389,7 @@ end -- @param Ops.FlightControl#FLIGHTCONTROL flightcontrol The FLIGHTCONTROL object. -- @return #FLIGHTGROUP self function FLIGHTGROUP:SetFlightControl(flightcontrol) - + -- Check if there is already a FC. if self.flightcontrol then if self.flightcontrol.airbasename==flightcontrol.airbasename then @@ -400,19 +400,19 @@ function FLIGHTGROUP:SetFlightControl(flightcontrol) self.flightcontrol:_RemoveFlight(self) end end - + -- Set FC. self:I(self.lid..string.format("Setting FLIGHTCONTROL to airbase %s", flightcontrol.airbasename)) self.flightcontrol=flightcontrol - + -- Add flight to all flights. table.insert(flightcontrol.flights, self) - + -- Update flight's F10 menu. if self.ai==false then self:_UpdateMenu(0.5) end - + return self end @@ -625,7 +625,7 @@ function FLIGHTGROUP:StartUncontrolled(delay) else self:E(self.lid.."ERROR: Could not start uncontrolled group as it is NOT alive!") end - + end return self @@ -640,12 +640,12 @@ function FLIGHTGROUP:ClearToLand(Delay) if Delay and Delay>0 then self:ScheduleOnce(Delay, FLIGHTGROUP.ClearToLand, self) else - + if self:IsHolding() then - self:I(self.lid..string.format("Clear to land ==> setting holding flag to 1 (true)")) + self:I(self.lid..string.format("Clear to land ==> setting holding flag to 1 (true)")) self.flaghold:Set(1) end - + end return self end @@ -658,20 +658,20 @@ function FLIGHTGROUP:GetFuelMin() local fuelmin=math.huge for i,_element in pairs(self.elements) do local element=_element --#FLIGHTGROUP.Element - + local unit=element.unit - + local life=unit:GetLife() - + if unit and unit:IsAlive() and life>1 then local fuel=unit:GetFuel() if fuel10 meters, we consider the unit as taxiing. -- TODO: Check distance threshold! If element is taxiing, the parking spot is free again. -- When the next plane is spawned on this spot, collisions should be avoided! @@ -719,49 +719,49 @@ function FLIGHTGROUP:onafterStatus(From, Event, To) self:ElementTaxiing(element) end end - + else --self:E(self.lid..string.format("Element %s is in PARKING queue but has no parking spot assigned!", element.name)) end - end + end end - + --- -- Elements --- - + local nTaskTot, nTaskSched, nTaskWP=self:CountRemainingTasks() local nMissions=self:CountRemainingMissison() -- Short info. - if self.verbose>0 then + if self.verbose>0 then local text=string.format("Status %s [%d/%d]: Tasks=%d (%d,%d) Current=%d. Missions=%s. Waypoint=%d/%d. Detected=%d. Destination=%s, FC=%s", - fsmstate, #self.elements, #self.elements, nTaskTot, nTaskSched, nTaskWP, self.taskcurrent, nMissions, self.currentwp or 0, self.waypoints and #self.waypoints or 0, + fsmstate, #self.elements, #self.elements, nTaskTot, nTaskSched, nTaskWP, self.taskcurrent, nMissions, self.currentwp or 0, self.waypoints and #self.waypoints or 0, self.detectedunits:Count(), self.destbase and self.destbase:GetName() or "unknown", self.flightcontrol and self.flightcontrol.airbasename or "none") self:I(self.lid..text) end -- Element status. - if self.verbose>1 then + if self.verbose>1 then local text="Elements:" for i,_element in pairs(self.elements) do local element=_element --#FLIGHTGROUP.Element - + local name=element.name local status=element.status local unit=element.unit local fuel=unit:GetFuel() or 0 local life=unit:GetLifeRelative() or 0 local parking=element.parking and tostring(element.parking.TerminalID) or "X" - + -- Check if element is not dead and we missed an event. if life<0 and element.status~=OPSGROUP.ElementStatus.DEAD and element.status~=OPSGROUP.ElementStatus.INUTERO then self:ElementDead(element) end - + -- Get ammo. local ammo=self:GetAmmoElement(element) - + -- Output text for element. text=text..string.format("\n[%d] %s: status=%s, fuel=%.1f, life=%.1f, guns=%d, rockets=%d, bombs=%d, missiles=%d (AA=%d, AG=%d, AS=%s), parking=%s", i, name, status, fuel*100, life*100, ammo.Guns, ammo.Rockets, ammo.Bombs, ammo.Missiles, ammo.MissilesAA, ammo.MissilesAG, ammo.MissilesAS, parking) @@ -779,67 +779,67 @@ function FLIGHTGROUP:onafterStatus(From, Event, To) if self.verbose>1 and self:IsAlive() and self.position then local time=timer.getAbsTime() - + -- Current position. local position=self:GetCoordinate() - + -- Travelled distance since last check. local ds=self.position:Get3DDistance(position) - + -- Time interval. local dt=time-self.traveltime - + -- Speed. local v=ds/dt - + -- Add up travelled distance. self.traveldist=self.traveldist+ds - - + + -- Max fuel time remaining. local TmaxFuel=math.huge - + for _,_element in pairs(self.elements) do local element=_element --#FLIGHTGROUP.Element - + -- Get relative fuel of element. local fuel=element.unit:GetFuel() or 0 - + -- Relative fuel used since last check. local dFrel=element.fuelrel-fuel - + -- Relative fuel used per second. local dFreldt=dFrel/dt - + -- Fuel remaining in seconds. local Tfuel=fuel/dFreldt - + if Tfuel Tfuel=%.1f min", element.name, fuel*100, dFrel*100, dFreldt*100*60, Tfuel/60)) - + -- Store rel fuel. element.fuelrel=fuel end - + -- Log outut. self:I(self.lid..string.format("Travelled ds=%.1f km dt=%.1f s ==> v=%.1f knots. Fuel left for %.1f min", self.traveldist/1000, dt, UTILS.MpsToKnots(v), TmaxFuel/60)) - + -- Update parameters. self.traveltime=time - self.position=position - end - + self.position=position + end + --- -- Tasks --- - + -- Task queue. - if #self.taskqueue>0 and self.verbose>1 then + if #self.taskqueue>0 and self.verbose>1 then local text=string.format("Tasks #%d", #self.taskqueue) for i,_task in pairs(self.taskqueue) do local task=_task --#FLIGHTGROUP.Task @@ -869,22 +869,22 @@ function FLIGHTGROUP:onafterStatus(From, Event, To) end self:I(self.lid..text) end - + --- -- Missions --- - + -- Current mission name. - if self.verbose>0 then + if self.verbose>0 then local Mission=self:GetMissionByID(self.currentmission) - + -- Current status. local text=string.format("Missions %d, Current: %s", self:CountRemainingMissison(), Mission and Mission.name or "none") for i,_mission in pairs(self.missionqueue) do local mission=_mission --Ops.Auftrag#AUFTRAG local Cstart= UTILS.SecondsToClock(mission.Tstart, true) local Cstop = mission.Tstop and UTILS.SecondsToClock(mission.Tstop, true) or "INF" - text=text..string.format("\n[%d] %s (%s) status=%s (%s), Time=%s-%s, prio=%d wp=%s targets=%d", + text=text..string.format("\n[%d] %s (%s) status=%s (%s), Time=%s-%s, prio=%d wp=%s targets=%d", i, tostring(mission.name), mission.type, mission:GetGroupStatus(self), tostring(mission.status), Cstart, Cstop, mission.prio, tostring(mission:GetGroupWaypointIndex(self)), mission:CountMissionTargets()) end self:I(self.lid..text) @@ -893,31 +893,31 @@ function FLIGHTGROUP:onafterStatus(From, Event, To) --- -- Fuel State --- - + -- Only if group is in air. if self:IsAlive() and self.group:IsAirborne(true) then local fuelmin=self:GetFuelMin() - + if fuelmin>=self.fuellowthresh then self.fuellow=false end - + if fuelmin>=self.fuelcriticalthresh then self.fuelcritical=false end - - + + -- Low fuel? if fuelmin1 groups to have passed. -- TODO: Can I do this more rigorously? self:ScheduleOnce(1, reset) - + else - + -- Set homebase if not already set. if EventData.Place then self.homebase=self.homebase or EventData.Place end - + -- Get element. local element=self:GetElementByName(unitname) - + -- Create element spawned event if not already present. if not self:_IsElement(unitname) then element=self:AddElementByName(unitname) end - + -- Set element to spawned state. - self:T3(self.lid..string.format("EVENT: Element %s born ==> spawned", element.name)) + self:T3(self.lid..string.format("EVENT: Element %s born ==> spawned", element.name)) self:ElementSpawned(element) - - end - + + end + end end @@ -1011,7 +1011,7 @@ function FLIGHTGROUP:OnEventEngineStartup(EventData) local element=self:GetElementByName(unitname) if element then - + if self:IsAirborne() or self:IsInbound() or self:IsHolding() then -- TODO: what? else @@ -1028,7 +1028,7 @@ function FLIGHTGROUP:OnEventEngineStartup(EventData) end end end - + end end @@ -1103,12 +1103,12 @@ function FLIGHTGROUP:OnEventEngineShutdown(EventData) local element=self:GetElementByName(unitname) if element then - + if element.unit and element.unit:IsAlive() then - + local airbase=element.unit:GetCoordinate():GetClosestAirbase() local parking=self:GetParkingSpot(element, 10, airbase) - + if airbase and parking then self:ElementArrived(element, airbase, parking) self:T3(self.lid..string.format("EVENT: Element %s shut down engines ==> arrived", element.name)) @@ -1116,13 +1116,13 @@ function FLIGHTGROUP:OnEventEngineShutdown(EventData) self:T3(self.lid..string.format("EVENT: Element %s shut down engines (in air) ==> dead", element.name)) self:ElementDead(element) end - + else - + self:I(self.lid..string.format("EVENT: Element %s shut down engines but is NOT alive ==> waiting for crash event (==> dead)", element.name)) end - + else -- element is nil end @@ -1164,7 +1164,7 @@ function FLIGHTGROUP:OnEventUnitLost(EventData) if EventData and EventData.IniGroup and EventData.IniUnit and EventData.IniGroupName and EventData.IniGroupName==self.groupname then self:I(self.lid..string.format("EVENT: Unit %s lost!", EventData.IniUnitName)) end - + end --- Flightgroup event function handling the crash of a unit. @@ -1183,7 +1183,7 @@ function FLIGHTGROUP:OnEventRemoveUnit(EventData) if element then self:I(self.lid..string.format("EVENT: Element %s removed ==> dead", element.name)) - self:ElementDead(element) + self:ElementDead(element) end end @@ -1210,15 +1210,15 @@ function FLIGHTGROUP:onafterElementSpawned(From, Event, To, Element) -- Trigger ElementAirborne event. Add a little delay because spawn is also delayed! self:__ElementAirborne(0.11, Element) else - + -- Get parking spot. local spot=self:GetParkingSpot(Element, 10) - + if spot then - + -- Trigger ElementParking event. Add a little delay because spawn is also delayed! self:__ElementParking(0.11, Element, spot) - + else -- TODO: This can happen if spawned on deck of a carrier! self:E(self.lid..string.format("Element spawned not in air but not on any parking spot.")) @@ -1236,14 +1236,14 @@ end -- @param #FLIGHTGROUP.ParkingSpot Spot Parking Spot. function FLIGHTGROUP:onafterElementParking(From, Event, To, Element, Spot) self:T(self.lid..string.format("Element parking %s at spot %s", Element.name, Element.parking and tostring(Element.parking.TerminalID) or "N/A")) - + -- Set element status. self:_UpdateStatus(Element, OPSGROUP.ElementStatus.PARKING) - + if Spot then self:_SetElementParkingAt(Element, Spot) end - + if self:IsTakeoffCold() then -- Wait for engine startup event. elseif self:IsTakeoffHot() then @@ -1284,7 +1284,7 @@ function FLIGHTGROUP:onafterElementTaxiing(From, Event, To, Element) -- Set parking spot to free. Also for FC. self:_SetElementParkingFree(Element) - + -- Set element status. self:_UpdateStatus(Element, OPSGROUP.ElementStatus.TAXIING) end @@ -1306,7 +1306,7 @@ function FLIGHTGROUP:onafterElementTakeoff(From, Event, To, Element, airbase) -- Set element status. self:_UpdateStatus(Element, OPSGROUP.ElementStatus.TAKEOFF, airbase) - + -- Trigger element airborne event. self:__ElementAirborne(2, Element) end @@ -1333,14 +1333,14 @@ end -- @param Wrapper.Airbase#AIRBASE airbase The airbase if applicable or nil. function FLIGHTGROUP:onafterElementLanded(From, Event, To, Element, airbase) self:T2(self.lid..string.format("Element landed %s at %s airbase", Element.name, airbase and airbase:GetName() or "unknown")) - + -- Helos with skids land directly on parking spots. if self.ishelo then - + local Spot=self:GetParkingSpot(Element, 10, airbase) - + self:_SetElementParkingAt(Element, Spot) - + end -- Set element status. @@ -1356,10 +1356,10 @@ end -- @param Wrapper.Airbase#AIRBASE airbase The airbase, where the element arrived. -- @param Wrapper.Airbase#AIRBASE.ParkingSpot Parking The Parking spot the element has. function FLIGHTGROUP:onafterElementArrived(From, Event, To, Element, airbase, Parking) - self:T(self.lid..string.format("Element arrived %s at %s airbase using parking spot %d", Element.name, airbase and airbase:GetName() or "unknown", Parking and Parking.TerminalID or -99)) - + self:T(self.lid..string.format("Element arrived %s at %s airbase using parking spot %d", Element.name, airbase and airbase:GetName() or "unknown", Parking and Parking.TerminalID or -99)) + self:_SetElementParkingAt(Element, Parking) - + -- Set element status. self:_UpdateStatus(Element, OPSGROUP.ElementStatus.ARRIVED) end @@ -1375,8 +1375,8 @@ function FLIGHTGROUP:onafterElementDead(From, Event, To, Element) if self.flightcontrol and Element.parking then self.flightcontrol:SetParkingFree(Element.parking) - end - + end + -- Not parking any more. Element.parking=nil @@ -1392,39 +1392,39 @@ end -- @param #string To To state. function FLIGHTGROUP:onafterSpawned(From, Event, To) self:I(self.lid..string.format("Flight spawned!")) - + if self.ai then - + -- Set default ROE and ROT options. self:SetOptionROE(self.roe) self:SetOptionROT(self.rot) - + -- TODO: make this input. self.group:SetOption(AI.Option.Air.id.PROHIBIT_JETT, true) self.group:SetOption(AI.Option.Air.id.PROHIBIT_AB, true) -- Does not seem to work. AI still used the after burner. self.group:SetOption(AI.Option.Air.id.RTB_ON_BINGO, false) --self.group:SetOption(AI.Option.Air.id.RADAR_USING, AI.Option.Air.val.RADAR_USING.FOR_CONTINUOUS_SEARCH) - + -- Turn TACAN beacon on. if self.tacanChannelDefault then self:SwitchTACANOn(self.tacanChannelDefault, self.tacanMorseDefault) end - + -- Turn on the radio. if self.radioFreqDefault then self:SwitchRadioOn(self.radioFreqDefault, self.radioModuDefault) end - + -- Update route. self:__UpdateRoute(-0.5) - + else - + -- F10 other menu. self:_UpdateMenu() - - end - + + end + end --- On after "FlightParking" event. Add flight to flightcontrol of airbase. @@ -1436,32 +1436,32 @@ function FLIGHTGROUP:onafterFlightParking(From, Event, To) self:I(self.lid..string.format("Flight is parking")) local airbase=self.group:GetCoordinate():GetClosestAirbase() - + local airbasename=airbase:GetName() or "unknown" - + -- Parking time stamp. self.Tparking=timer.getAbsTime() -- Get FC of this airbase. local flightcontrol=_DATABASE:GetFlightControl(airbasename) - + if flightcontrol then - + -- Set FC for this flight self:SetFlightControl(flightcontrol) - + if self.flightcontrol then - + -- Set flight status. self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.PARKING) - + -- Update player menu. if not self.ai then self:_UpdateMenu(0.5) end - + end - end + end end --- On after "FlightTaxiing" event. @@ -1471,18 +1471,18 @@ end -- @param #string To To state. function FLIGHTGROUP:onafterFlightTaxiing(From, Event, To) self:T(self.lid..string.format("Flight is taxiing")) - + -- Parking over. self.Tparking=nil -- TODO: need a better check for the airbase. local airbase=self.group:GetCoordinate():GetClosestAirbase(nil, self.group:GetCoalition()) - if self.flightcontrol and airbase and self.flightcontrol.airbasename==airbase:GetName() then + if self.flightcontrol and airbase and self.flightcontrol.airbasename==airbase:GetName() then -- Add AI flight to takeoff queue. if self.ai then - -- AI flights go directly to TAKEOFF as we don't know when they finished taxiing. + -- AI flights go directly to TAKEOFF as we don't know when they finished taxiing. self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.TAKEOFF) else -- Human flights go to TAXI OUT queue. They will go to the ready for takeoff queue when they request it. @@ -1490,7 +1490,7 @@ function FLIGHTGROUP:onafterFlightTaxiing(From, Event, To) -- Update menu. self:_UpdateMenu() end - + end end @@ -1509,7 +1509,7 @@ function FLIGHTGROUP:onafterFlightTakeoff(From, Event, To, airbase) self.flightcontrol:_RemoveFlight(self) self.flightcontrol=nil end - + end --- On after "FlightAirborne" event. @@ -1519,7 +1519,7 @@ end -- @param #string To To state. function FLIGHTGROUP:onafterFlightAirborne(From, Event, To) self:I(self.lid..string.format("Flight airborne")) - + if not self.ai then self:_UpdateMenu() end @@ -1534,7 +1534,7 @@ function FLIGHTGROUP:onafterFlightLanding(From, Event, To) self:T(self.lid..string.format("Flight is landing")) self:_SetElementStatusAll(OPSGROUP.ElementStatus.LANDING) - + end --- On after "FlightLanded" event. @@ -1567,11 +1567,11 @@ function FLIGHTGROUP:onafterFlightArrived(From, Event, To) -- Flight Control if self.flightcontrol then -- Add flight to arrived queue. - self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.ARRIVED) + self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.ARRIVED) end - + -- Stop and despawn in 5 min. - self:__Stop(5*60) + self:__Stop(5*60) end --- On after "FlightDead" event. @@ -1585,22 +1585,22 @@ function FLIGHTGROUP:onafterFlightDead(From, Event, To) -- Delete waypoints so they are re-initialized at the next spawn. self.waypoints=nil self.groupinitialized=false - + -- Remove flight from all FC queues. - if self.flightcontrol then + if self.flightcontrol then self.flightcontrol:_RemoveFlight(self) self.flightcontrol=nil end - - -- Cancel all mission. + + -- Cancel all mission. for _,_mission in pairs(self.missionqueue) do local mission=_mission --Ops.Auftrag#AUFTRAG - + self:MissionCancel(mission) mission:FlightDead(self) - + end - + -- Stop self:Stop() end @@ -1629,48 +1629,48 @@ function FLIGHTGROUP:onbeforeUpdateRoute(From, Event, To, n) else -- Not airborne yet. Try again in 1 sec. self:T3(self.lid.."FF update route denied ==> checking back in 5 sec") - trepeat=-5 + trepeat=-5 allowed=false end - + if n and n<1 then self:E(self.lid.."Update route denied because waypoint n<1!") - allowed=false + allowed=false end - + if not self.currentwp then self:E(self.lid.."Update route denied because self.currentwp=nil!") - allowed=false + allowed=false end - + local N=n or self.currentwp+1 if not N or N<1 then self:E(self.lid.."FF update route denied because N=nil or N<1") trepeat=-5 - allowed=false + allowed=false end - + if self.taskcurrent>0 then self:E(self.lid.."Update route denied because taskcurrent>0") allowed=false end - + -- Not good, because mission will never start. Better only check if there is a current task! --if self.currentmission then --end - -- Only AI flights. + -- Only AI flights. if not self.ai then allowed=false end - + -- Debug info. self:T2(self.lid..string.format("Onbefore Updateroute allowed=%s state=%s repeat in %s", tostring(allowed), self:GetState(), tostring(trepeat))) - + if trepeat then self:__UpdateRoute(trepeat, n) end - + return allowed end @@ -1684,44 +1684,44 @@ function FLIGHTGROUP:onafterUpdateRoute(From, Event, To, n) -- Update route from this waypoint number onwards. n=n or self.currentwp+1 - + -- Update waypoint tasks, i.e. inject WP tasks into waypoint table. self:_UpdateWaypointTasks() -- Waypoints. local wp={} - + -- Current velocity. local speed=self.group and self.group:GetVelocityKMH() or 100 - + -- Set current waypoint or we get problem that the _PassingWaypoint function is triggered too early, i.e. right now and not when passing the next WP. local current=self.group:GetCoordinate():WaypointAir("BARO", COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, speed, true, nil, {}, "Current") table.insert(wp, current) - + -- Add remaining waypoints to route. for i=n, #self.waypoints do table.insert(wp, self.waypoints[i]) end - + -- Debug info. local hb=self.homebase and self.homebase:GetName() or "unknown" local db=self.destbase and self.destbase:GetName() or "unknown" self:T(self.lid..string.format("Updating route for WP #%d-%d homebase=%s destination=%s", n, #wp, hb, db)) - + if #wp>1 then -- Route group to all defined waypoints remaining. self:Route(wp, 1) - + else - + --- -- No waypoints left --- - + self:_CheckGroupDone() - + end end @@ -1737,7 +1737,7 @@ function FLIGHTGROUP:onafterRespawn(From, Event, To, Template) self:T(self.lid.."Respawning group!") local template=UTILS.DeepCopy(Template or self.template) - + if self.group and self.group:InAir() then template.lateActivation=false self.respawning=true @@ -1747,13 +1747,13 @@ function FLIGHTGROUP:onafterRespawn(From, Event, To, Template) end --- Check if flight is done, i.e. --- --- * passed the final waypoint, +-- +-- * passed the final waypoint, -- * no current task -- * no current mission -- * number of remaining tasks is zero -- * number of remaining missions is zero --- +-- -- @param #FLIGHTGROUP self -- @param #number delay Delay in seconds. function FLIGHTGROUP:_CheckGroupDone(delay) @@ -1764,29 +1764,29 @@ function FLIGHTGROUP:_CheckGroupDone(delay) -- Delayed call. self:ScheduleOnce(delay, FLIGHTGROUP._CheckGroupDone, self) else - - -- First check if there is a paused mission that + + -- First check if there is a paused mission that if self.missionpaused then self:UnpauseMission() return end - - + + -- Number of tasks remaining. local nTasks=self:CountRemainingTasks() - + -- Number of mission remaining. local nMissions=self:CountRemainingMissison() - + -- Final waypoint passed? if self.passedfinalwp then - + -- Got current mission or task? if self.currentmission==nil and self.taskcurrent==0 then - + -- Number of remaining tasks/missions? if nTasks==0 and nMissions==0 then - + -- Send flight to destination. if self.destbase then self:I(self.lid.."Passed Final WP and No current and/or future missions/task ==> RTB!") @@ -1796,24 +1796,24 @@ function FLIGHTGROUP:_CheckGroupDone(delay) self:__RTZ(-3, self.destzone) else self:I(self.lid.."Passed Final WP and NO Tasks/Missions left. No DestBase or DestZone ==> Wait!") - self:__Wait(-1) + self:__Wait(-1) end - + else self:I(self.lid..string.format("Passed Final WP but Tasks=%d or Missions=%d left in the queue. Wait!", nTasks, nMissions)) - self:__Wait(-1) + self:__Wait(-1) end else self:I(self.lid..string.format("Passed Final WP but still have current Task (#%s) or Mission (#%s) left to do", tostring(self.taskcurrent), tostring(self.currentmission))) - end + end else self:I(self.lid..string.format("Flight (status=%s) did NOT pass the final waypoint yet ==> update route", self:GetState())) self:__UpdateRoute(-1) - end + end end - + end - + end --- On before "RTB" event. @@ -1825,9 +1825,9 @@ end -- @param #number SpeedTo Speed used for travelling from current position to holding point in knots. -- @param #number SpeedHold Holding speed in knots. function FLIGHTGROUP:onbeforeRTB(From, Event, To, airbase, SpeedTo, SpeedHold) - + if self:IsAlive() then - + local allowed=true local Tsuspend=nil @@ -1835,56 +1835,56 @@ function FLIGHTGROUP:onbeforeRTB(From, Event, To, airbase, SpeedTo, SpeedHold) self:E(self.lid.."ERROR: Airbase is nil in RTB() call!") allowed=false end - + -- Check that coaliton is okay. We allow same (blue=blue, red=red) or landing on neutral bases. if airbase and airbase:GetCoalition()~=self.group:GetCoalition() and airbase:GetCoalition()>0 then self:E(self.lid..string.format("ERROR: Wrong airbase coalition %d in RTB() call! We allow only same as group %d or neutral airbases 0.", airbase:GetCoalition(), self.group:GetCoalition())) allowed=false - end - + end + if not self.group:IsAirborne(true) then self:I(self.lid..string.format("WARNING: Group is not AIRBORNE ==> RTB event is suspended for 10 sec.")) allowed=false - Tsuspend=-10 + Tsuspend=-10 end - + -- Only if fuel is not low or critical. if not (self:IsFuelLow() or self:IsFuelCritical()) then - + -- Check if there are remaining tasks. local Ntot,Nsched, Nwp=self:CountRemainingTasks() - + if self.taskcurrent>0 then self:I(self.lid..string.format("WARNING: Got current task ==> RTB event is suspended for 10 sec.")) Tsuspend=-10 allowed=false end - + if Nsched>0 then self:I(self.lid..string.format("WARNING: Still got %d SCHEDULED tasks in the queue ==> RTB event is suspended for 10 sec.", Nsched)) Tsuspend=-10 allowed=false end - + if Nwp>0 then self:I(self.lid..string.format("WARNING: Still got %d WAYPOINT tasks in the queue ==> RTB event is suspended for 10 sec.", Nwp)) Tsuspend=-10 allowed=false end - + end - + if Tsuspend and not allowed then - self:__RTB(Tsuspend, airbase, SpeedTo, SpeedHold) + self:__RTB(Tsuspend, airbase, SpeedTo, SpeedHold) end - + return allowed - + else self:E(self.lid.."WARNING: Group is not alive! RTB call not allowed.") return false end - + end --- On after "RTB" event. Order flight to hold at an airbase and wait for signal to land. @@ -1899,13 +1899,13 @@ end function FLIGHTGROUP:onafterRTB(From, Event, To, airbase, SpeedTo, SpeedHold, SpeedLand) self:I(self.lid..string.format("RTB: event=%s: %s --> %s to %s", Event, From, To, airbase:GetName())) - + -- Set the destination base. self.destbase=airbase - + -- Clear holding time in any case. self.Tholding=nil - + -- Defaults: SpeedTo=SpeedTo or UTILS.KmphToKnots(self.speedCruise) SpeedHold=SpeedHold or (self.ishelo and 80 or 250) @@ -1917,13 +1917,13 @@ function FLIGHTGROUP:onafterRTB(From, Event, To, airbase, SpeedTo, SpeedHold, Sp self:T(self.lid..text) local althold=self.ishelo and 1000+math.random(10)*100 or math.random(4,10)*1000 - + -- Holding points. local c0=self.group:GetCoordinate() local p0=airbase:GetZone():GetRandomCoordinate():SetAltitude(UTILS.FeetToMeters(althold)) local p1=nil local wpap=nil - + -- Do we have a flight control? local fc=_DATABASE:GetFlightControl(airbase:GetName()) if fc then @@ -1931,37 +1931,37 @@ function FLIGHTGROUP:onafterRTB(From, Event, To, airbase, SpeedTo, SpeedHold, Sp local HoldingPoint=fc:_GetHoldingpoint(self) p0=HoldingPoint.pos0 p1=HoldingPoint.pos1 - + -- Debug marks. if self.Debug then p0:MarkToAll("Holding point P0") p1:MarkToAll("Holding point P1") end - + -- Set flightcontrol for this flight. self:SetFlightControl(fc) - + -- Add flight to inbound queue. self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.INBOUND) end - + -- Altitude above ground for a glide slope of 3 degrees. local x1=self.ishelo and UTILS.NMToMeters(5.0) or UTILS.NMToMeters(10) local x2=self.ishelo and UTILS.NMToMeters(2.5) or UTILS.NMToMeters(5) - local alpha=math.rad(3) + local alpha=math.rad(3) local h1=x1*math.tan(alpha) local h2=x2*math.tan(alpha) - + local runway=airbase:GetActiveRunway() - + -- Set holding flag to 0=false. self.flaghold:Set(0) - + local holdtime=5*60 if fc or self.airboss then holdtime=nil end - + -- Task fuction when reached holding point. local TaskArrived=self.group:TaskFunction("FLIGHTGROUP._ReachedHolding", self) @@ -1970,72 +1970,72 @@ function FLIGHTGROUP:onafterRTB(From, Event, To, airbase, SpeedTo, SpeedHold, Sp local TaskLand = self.group:TaskCondition(nil, self.flaghold.UserFlagName, 1, nil, holdtime) local TaskHold = self.group:TaskControlled(TaskOrbit, TaskLand) local TaskKlar = self.group:TaskFunction("FLIGHTGROUP._ClearedToLand", self) -- Once the holding flag becomes true, set trigger FLIGHTLANDING, i.e. set flight STATUS to LANDING. - + -- Waypoints from current position to holding point. local wp={} wp[#wp+1]=c0:WaypointAir(nil, COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, UTILS.KnotsToKmph(SpeedTo), true , nil, {}, "Current Pos") wp[#wp+1]=p0:WaypointAir(nil, COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, UTILS.KnotsToKmph(SpeedTo), true , nil, {TaskArrived, TaskHold, TaskKlar}, "Holding Point") - + -- Approach point: 10 NN in direction of runway. if airbase:GetAirbaseCategory()==Airbase.Category.AIRDROME then - + --- -- Airdrome --- local papp=airbase:GetCoordinate():Translate(x1, runway.heading-180):SetAltitude(h1) - wp[#wp+1]=papp:WaypointAirTurningPoint(nil, UTILS.KnotsToKmph(SpeedLand), {}, "Final Approach") - + wp[#wp+1]=papp:WaypointAirTurningPoint(nil, UTILS.KnotsToKmph(SpeedLand), {}, "Final Approach") + -- Okay, it looks like it's best to specify the coordinates not at the airbase but a bit away. This causes a more direct landing approach. - local pland=airbase:GetCoordinate():Translate(x2, runway.heading-180):SetAltitude(h2) + local pland=airbase:GetCoordinate():Translate(x2, runway.heading-180):SetAltitude(h2) wp[#wp+1]=pland:WaypointAirLanding(UTILS.KnotsToKmph(SpeedLand), airbase, {}, "Landing") - + elseif airbase:GetAirbaseCategory()==Airbase.Category.SHIP then - + --- -- Ship --- - local pland=airbase:GetCoordinate() + local pland=airbase:GetCoordinate() wp[#wp+1]=pland:WaypointAirLanding(UTILS.KnotsToKmph(SpeedLand), airbase, {}, "Landing") - - end - + + end + if self.ai then - + local routeto=false if fc or world.event.S_EVENT_KILL then routeto=true end - + -- Clear all tasks. self:ClearTasks() - + -- Respawn? if routeto then - + --self:I(self.lid.."FF route (not repawn)") - + -- Just route the group. Respawn might happen when going from holding to final. self:Route(wp, 1) - - else - + + else + --self:I(self.lid.."FF respawn (not route)") - + -- Get group template. local Template=self.group:GetTemplate() - + -- Set route points. Template.route.points=wp - + --Respawn the group with new waypoints. self:Respawn(Template) - - end - + + end + end - + end --- On before "Wait" event. @@ -2059,7 +2059,7 @@ function FLIGHTGROUP:onbeforeWait(From, Event, To, Coord, Altitude, Speed) Tsuspend=-10 allowed=false end - + if Nsched>0 then self:I(self.lid..string.format("WARNING: Still got %d SCHEDULED tasks in the queue ==> WAIT event is suspended for 10 sec.", Nsched)) Tsuspend=-10 @@ -2071,11 +2071,11 @@ function FLIGHTGROUP:onbeforeWait(From, Event, To, Coord, Altitude, Speed) Tsuspend=-10 allowed=false end - + if Tsuspend and not allowed then self:__Wait(Tsuspend, Coord, Altitude, Speed) end - + return allowed end @@ -2089,7 +2089,7 @@ end -- @param #number Altitude Altitude in feet. Default 10000 ft. -- @param #number Speed Speed in knots. Default 250 kts. function FLIGHTGROUP:onafterWait(From, Event, To, Coord, Altitude, Speed) - + Coord=Coord or self.group:GetCoordinate() Altitude=Altitude or (self.ishelo and 1000 or 10000) Speed=Speed or (self.ishelo and 80 or 250) @@ -2101,12 +2101,12 @@ function FLIGHTGROUP:onafterWait(From, Event, To, Coord, Altitude, Speed) --TODO: set ROE passive. introduce roe event/state/variable. - -- Orbit task. + -- Orbit task. local TaskOrbit=self.group:TaskOrbit(Coord, UTILS.FeetToMeters(Altitude), UTILS.KnotsToMps(Speed)) -- Set task. self:SetTask(TaskOrbit) - + end @@ -2117,7 +2117,7 @@ end -- @param #string To To state. -- @param Core.Point#COORDINATE Coordinate The coordinate. function FLIGHTGROUP:onafterRefuel(From, Event, To, Coordinate) - + -- Debug message. local text=string.format("Flight group set to refuel at the nearest tanker") MESSAGE:New(text, 30, "DEBUG"):ToAllIf(self.Debug) @@ -2133,18 +2133,18 @@ function FLIGHTGROUP:onafterRefuel(From, Event, To, Coordinate) local TaskRefuel=self.group:TaskRefueling() local TaskFunction=self.group:TaskFunction("FLIGHTGROUP._FinishedRefuelling", self) local DCSTasks={TaskRefuel, TaskFunction} - + local Speed=self.speedCruise local coordinate=self.group:GetCoordinate() - + Coordinate=Coordinate or coordinate:Translate(UTILS.NMToMeters(5), self.group:GetHeading(), true) local wp0=coordinate:WaypointAir("BARO", COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, Speed, true) local wp9=Coordinate:WaypointAir("BARO", COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, Speed, true, nil, DCSTasks, "Refuel") - + self:Route({wp0, wp9}) - + end --- On after "Refueled" event. @@ -2157,10 +2157,10 @@ function FLIGHTGROUP:onafterRefueled(From, Event, To) local text=string.format("Flight group finished refuelling") MESSAGE:New(text, 30, "DEBUG"):ToAllIf(self.Debug) self:I(self.lid..text) - + -- Check if flight is done. self:_CheckGroupDone(1) - + end @@ -2173,47 +2173,47 @@ function FLIGHTGROUP:onafterHolding(From, Event, To) -- Set holding flag to 0 (just in case). self.flaghold:Set(0) - + -- Holding time stamp. - self.Tholding=timer.getAbsTime() + self.Tholding=timer.getAbsTime() local text=string.format("Flight group %s is HOLDING now", self.groupname) MESSAGE:New(text, 10, "DEBUG"):ToAllIf(self.Debug) self:I(self.lid..text) - + -- Add flight to waiting/holding queue. if self.flightcontrol then - + -- Set flight status to holding self.flightcontrol:SetFlightStatus(self, FLIGHTCONTROL.FlightStatus.HOLDING) - + if not self.ai then self:_UpdateMenu() end - + elseif self.airboss then - + if self.ishelo then - + local carrierpos=self.airboss:GetCoordinate() local carrierheading=self.airboss:GetHeading() - + local Distance=UTILS.NMToMeters(5) local Angle=carrierheading+90 local altitude=math.random(12, 25)*100 local oc=carrierpos:Translate(Distance,Angle):SetAltitude(altitude, true) - + -- Orbit until flaghold=1 (true) but max 5 min if no FC is giving the landing clearance. local TaskOrbit=self.group:TaskOrbit(oc, nil, UTILS.KnotsToMps(50)) local TaskLand=self.group:TaskCondition(nil, self.flaghold.UserFlagName, 1) local TaskHold=self.group:TaskControlled(TaskOrbit, TaskLand) local TaskKlar=self.group:TaskFunction("FLIGHTGROUP._ClearedToLand", self) -- Once the holding flag becomes true, set trigger FLIGHTLANDING, i.e. set flight STATUS to LANDING. - + local DCSTask=self.group:TaskCombo({TaskOrbit, TaskHold, TaskKlar}) - + self:SetTask(DCSTask) end - + end end @@ -2227,21 +2227,21 @@ end function FLIGHTGROUP:onafterEngageTargets(From, Event, To, TargetUnitSet) local DCSTasks={} - + for _,_unit in paris(TargetUnitSet:GetSet()) do local unit=_unit --Wrapper.Unit#UNIT local task=self.group:TaskAttackUnit(unit, true) table.insert(DCSTasks) end - + -- Task combo. local DCSTask=self.group:TaskCombo(DCSTasks) - + --TODO needs a task function that calls EngageDone or so event and updates the route again. - + -- Lets try if pushtask actually leaves the remaining tasks untouched. self:SetTask(DCSTask) - + end --- On before "LandAt" event. Check we have a helo group. @@ -2266,18 +2266,18 @@ function FLIGHTGROUP:onafterLandAt(From, Event, To, Coordinate, Duration) -- Duration. Duration=Duration or 600 - + Coordinate=Coordinate or self:GetCoordinate() local DCStask=self.group:TaskLandAtVec2(Coordinate:GetVec2(), Duration) - + local Task=self:NewTaskScheduled(DCStask, 1, "Task_Land_At", 0) -- Add task with high priority. --self:AddTask(task, 1, "Task_Land_At", 0) - + self:TaskExecute(Task) - + end --- On after "FuelLow" event. @@ -2291,55 +2291,55 @@ function FLIGHTGROUP:onafterFuelLow(From, Event, To) local text=string.format("Low fuel for flight group %s", self.groupname) MESSAGE:New(text, 30, "DEBUG"):ToAllIf(self.Debug) self:I(self.lid..text) - + -- Set switch to true. self.fuellow=true -- Back to destination or home. local airbase=self.destbase or self.homebase - + if self.airwing then - + -- Get closest tanker from airwing that can refuel this flight. local tanker=self.airwing:GetTankerForFlight(self) - + if tanker then - + -- Send flight to tanker with refueling task. self:Refuel(tanker.flightgroup:GetCoordinate()) - + else - + if airbase and self.fuellowrtb then self:RTB(airbase) --TODO: RTZ end - + end - + else - + if self.fuellowrefuel and self.refueltype then - + local tanker=self:FindNearestTanker(50) - + if tanker then - + self:I(self.lid..string.format("Send to refuel at tanker %s", tanker:GetName())) - + self:Refuel() - - return + + return end end - + if airbase and self.fuellowrtb then self:RTB(airbase) --TODO: RTZ end - + end - + end --- On after "FuelCritical" event. @@ -2353,13 +2353,13 @@ function FLIGHTGROUP:onafterFuelCritical(From, Event, To) local text=string.format("Critical fuel for flight group %s", self.groupname) MESSAGE:New(text, 30, "DEBUG"):ToAllIf(self.Debug) self:I(self.lid..text) - + -- Set switch to true. self.fuelcritical=true -- Airbase. local airbase=self.destbase or self.homebase - + if airbase and self.fuelcriticalrtb and not self:IsGoing4Fuel() then self:RTB(airbase) --TODO: RTZ @@ -2375,7 +2375,7 @@ function FLIGHTGROUP:onafterStop(From, Event, To) -- Check if group is still alive. if self:IsAlive() then - + -- Set element parking spot to FREE (after arrived for example). if self.flightcontrol then for _,_element in pairs(self.elements) do @@ -2383,7 +2383,7 @@ function FLIGHTGROUP:onafterStop(From, Event, To) self:_SetElementParkingFree(element) end end - + -- Destroy group. No event is generated. self.group:Destroy(false) end @@ -2398,9 +2398,9 @@ function FLIGHTGROUP:onafterStop(From, Event, To) self:UnHandleEvent(EVENTS.Ejection) self:UnHandleEvent(EVENTS.Crash) self:UnHandleEvent(EVENTS.RemoveUnit) - + self.CallScheduler:Clear() - + _DATABASE.FLIGHTGROUPS[self.groupname]=nil self:I(self.lid.."STOPPED! Unhandled events, cleared scheduler and removed from database.") @@ -2477,44 +2477,44 @@ function FLIGHTGROUP:_InitGroup() -- Helo group. self.ishelo=self.group:IsHelicopter() - + -- Is (template) group uncontrolled. self.isUncontrolled=self.template.uncontrolled - + -- Is (template) group late activated. self.isLateActivated=self.template.lateActivation - + -- Max speed in km/h. self.speedmax=self.group:GetSpeedMax() - + -- Cruise speed limit 350 kts for fixed and 80 knots for rotary wings. local speedCruiseLimit=self.ishelo and UTILS.KnotsToKmph(80) or UTILS.KnotsToKmph(350) - + -- Cruise speed: 70% of max speed but within limit. self.speedCruise=math.min(self.speedmax*0.7, speedCruiseLimit) - + -- Group ammo. self.ammo=self:GetAmmoTot() - + -- Initial fuel mass. -- TODO: this is a unit property! self.fuelmass=0 - + self.traveldist=0 self.traveltime=timer.getAbsTime() self.position=self:GetCoordinate() - + -- Radio parameters from template. self.radioOn=self.template.communication self.radioFreq=self.template.frequency self.radioModu=self.template.modulation - + -- If not set by the use explicitly yet, we take the template values as defaults. if not self.radioFreqDefault then self.radioFreqDefault=self.radioFreq self.radioModuDefault=self.radioModu end - + -- Set default formation. if not self.formationDefault then if self.ishelo then @@ -2523,38 +2523,38 @@ function FLIGHTGROUP:_InitGroup() self.formationDefault=ENUMS.Formation.FixedWing.EchelonLeft.Group end end - + self.ai=not self:_IsHuman(self.group) - + if not self.ai then self.menu=self.menu or {} self.menu.atc=self.menu.atc or {} - self.menu.atc.root=self.menu.atc.root or MENU_GROUP:New(self.group, "ATC") + self.menu.atc.root=self.menu.atc.root or MENU_GROUP:New(self.group, "ATC") end - + self:SwitchFormation(self.formationDefault) - + -- Add elemets. for _,unit in pairs(self.group:GetUnits()) do local element=self:AddElementByName(unit:GetName()) - end - + end + -- Get first unit. This is used to extract other parameters. local unit=self.group:GetUnit(1) - + if unit then - + self.rangemax=unit:GetRange() - + self.descriptors=unit:GetDesc() - + self.actype=unit:GetTypeName() - + self.ceiling=self.descriptors.Hmax - + self.tankertype=select(2, unit:IsTanker()) self.refueltype=select(2, unit:IsRefuelable()) - + -- Debug info. local text=string.format("Initialized Flight Group %s:\n", self.groupname) text=text..string.format("AC type = %s\n", self.actype) @@ -2576,14 +2576,14 @@ function FLIGHTGROUP:_InitGroup() text=text..string.format("Start Air = %s\n", tostring(self:IsTakeoffAir())) text=text..string.format("Start Cold = %s\n", tostring(self:IsTakeoffCold())) text=text..string.format("Start Hot = %s\n", tostring(self:IsTakeoffHot())) - text=text..string.format("Start Rwy = %s\n", tostring(self:IsTakeoffRunway())) + text=text..string.format("Start Rwy = %s\n", tostring(self:IsTakeoffRunway())) self:I(self.lid..text) - + -- Init done. self.groupinitialized=true - + end - + return self end @@ -2603,7 +2603,7 @@ function FLIGHTGROUP:AddElementByName(unitname) element.unit=unit element.status=OPSGROUP.ElementStatus.INUTERO element.group=unit:GetGroup() - + element.modex=element.unit:GetTemplate().onboard_num element.skill=element.unit:GetTemplate().skill element.pylons=element.unit:GetTemplatePylons() @@ -2614,21 +2614,21 @@ function FLIGHTGROUP:AddElementByName(unitname) element.categoryname=element.unit:GetCategoryName() element.callsign=element.unit:GetCallsign() element.size=element.unit:GetObjectSize() - + if element.skill=="Client" or element.skill=="Player" then element.ai=false element.client=CLIENT:FindByName(unitname) else element.ai=true end - + local text=string.format("Adding element %s: status=%s, skill=%s, modex=%s, fuelmass=%.1f (%d %%), category=%d, categoryname=%s, callsign=%s, ai=%s", element.name, element.status, element.skill, element.modex, element.fuelmass, element.fuelrel, element.category, element.categoryname, element.callsign, tostring(element.ai)) self:I(self.lid..text) -- Add element to table. table.insert(self.elements, element) - + if unit:IsAlive() then self:ElementSpawned(element) end @@ -2646,24 +2646,24 @@ end function FLIGHTGROUP:GetHomebaseFromWaypoints() local wp=self:GetWaypoint(1) - + if wp then - - if wp and wp.action and wp.action==COORDINATE.WaypointAction.FromParkingArea - or wp.action==COORDINATE.WaypointAction.FromParkingAreaHot + + if wp and wp.action and wp.action==COORDINATE.WaypointAction.FromParkingArea + or wp.action==COORDINATE.WaypointAction.FromParkingAreaHot or wp.action==COORDINATE.WaypointAction.FromRunway then - + -- Get airbase ID depending on airbase category. local airbaseID=wp.airdromeId or wp.helipadId - + local airbase=AIRBASE:FindByID(airbaseID) - - return airbase + + return airbase end - + --TODO: Handle case where e.g. only one WP but that is not landing. --TODO: Probably other cases need to be taken care of. - + end return nil @@ -2681,24 +2681,24 @@ function FLIGHTGROUP:FindNearestAirbase(Radius) local airbase=nil --Wrapper.Airbase#AIRBASE for _,_airbase in pairs(AIRBASE.GetAllAirbases()) do local ab=_airbase --Wrapper.Airbase#AIRBASE - + local coalitionAB=ab:GetCoalition() - + if coalitionAB==self:GetCoalition() or coalitionAB==coalition.side.NEUTRAL then - + if airbase then local d=ab:GetCoordinate():Get2DDistance(coord) - + if d %s Destination", #self.waypoints, self.homebase and self.homebase:GetName() or "unknown", self.destbase and self.destbase:GetName() or "uknown")) - + -- Update route. if #self.waypoints>0 then - + -- Check if only 1 wp? if #self.waypoints==1 then self.passedfinalwp=true end - + -- Update route (when airborne). --self:_CheckGroupDone(1) self:__UpdateRoute(-1) @@ -2998,11 +2998,11 @@ function FLIGHTGROUP:AddWaypoint(coordinate, wpnumber, speed, updateroute) -- Waypoint number. Default is at the end. wpnumber=wpnumber or #self.waypoints+1 - + if wpnumber>self.currentwp then self.passedfinalwp=false end - + -- Speed in knots. speed=speed or 350 @@ -3011,20 +3011,20 @@ function FLIGHTGROUP:AddWaypoint(coordinate, wpnumber, speed, updateroute) -- Create air waypoint. local wp=coordinate:WaypointAir(COORDINATE.WaypointAltType.BARO, COORDINATE.WaypointType.TurningPoint, COORDINATE.WaypointAction.TurningPoint, speedkmh, true, nil, {}, string.format("Added Waypoint #%d", wpnumber)) - + -- Add to table. table.insert(self.waypoints, wpnumber, wp) - + -- Debug info. self:T(self.lid..string.format("Adding AIR waypoint #%d, speed=%.1f knots. Last waypoint passed was #%s. Total waypoints #%d", wpnumber, speed, self.currentwp, #self.waypoints)) - + -- Shift all waypoint tasks after the inserted waypoint. for _,_task in pairs(self.taskqueue) do local task=_task --#FLIGHTGROUP.Task if task.type==OPSGROUP.TaskType.WAYPOINT and task.waypoint and task.waypoint>=wpnumber then task.waypoint=task.waypoint+1 end - end + end -- Shift all mission waypoints after the inserted waypoint. for _,_mission in pairs(self.missionqueue) do @@ -3032,20 +3032,20 @@ function FLIGHTGROUP:AddWaypoint(coordinate, wpnumber, speed, updateroute) -- Get mission waypoint index. local wpidx=mission:GetGroupWaypointIndex(self) - + -- Increase number if this waypoint lies in the future. if wpidx and wpidx>=wpnumber then mission:SetGroupWaypointIndex(self, wpidx+1) - end - + end + end - + -- Update route. if updateroute==nil or updateroute==true then self:_CheckGroupDone(1) --self:__UpdateRoute(-1) end - + return wpnumber end @@ -3079,17 +3079,17 @@ function FLIGHTGROUP:_SetElementParkingAt(Element, Spot) -- Element is parking here. Element.parking=Spot - + if Spot then - - self:I(self.lid..string.format("Element %s is parking on spot %d", Element.name, Spot.TerminalID)) - + + self:I(self.lid..string.format("Element %s is parking on spot %d", Element.name, Spot.TerminalID)) + if self.flightcontrol then - + -- Set parking spot to OCCUPIED. self.flightcontrol:SetParkingOccupied(Element.parking, Element.name) end - + end end @@ -3105,12 +3105,12 @@ function FLIGHTGROUP:_SetElementParkingFree(Element) if self.flightcontrol then self.flightcontrol:SetParkingFree(Element.parking) end - + -- Not parking any more. Element.parking=nil end - + end --- Get onboard number. @@ -3142,10 +3142,10 @@ end -- @param Wrapper.Unit#UNIT unit Aircraft unit. -- @return #boolean If true, human player inside the unit. function FLIGHTGROUP:_IsHumanUnit(unit) - + -- Get player unit or nil if no player unit. local playerunit=self:_GetPlayerUnitAndName(unit:GetName()) - + if playerunit then return true else @@ -3161,7 +3161,7 @@ function FLIGHTGROUP:_IsHuman(group) -- Get all units of the group. local units=group:GetUnits() - + -- Loop over all units. for _,_unit in pairs(units) do -- Check if unit is human. @@ -3174,7 +3174,7 @@ function FLIGHTGROUP:_IsHuman(group) return false end ---- Returns the unit of a player and the player name. If the unit does not belong to a player, nil is returned. +--- Returns the unit of a player and the player name. If the unit does not belong to a player, nil is returned. -- @param #FLIGHTGROUP self -- @param #string _unitName Name of the player unit. -- @return Wrapper.Unit#UNIT Unit of player or nil. @@ -3183,23 +3183,23 @@ function FLIGHTGROUP:_GetPlayerUnitAndName(_unitName) self:F2(_unitName) if _unitName ~= nil then - + -- Get DCS unit from its name. local DCSunit=Unit.getByName(_unitName) - + if DCSunit then - + local playername=DCSunit:getPlayerName() local unit=UNIT:Find(DCSunit) - + if DCSunit and unit and playername then return unit, playername end - + end - + end - + -- Return nil if we could not find a player. return nil,nil end @@ -3215,19 +3215,19 @@ function FLIGHTGROUP:GetParkingSpot(element, maxdist, airbase) local coord=element.unit:GetCoordinate() airbase=airbase or coord:GetClosestAirbase(nil, self:GetCoalition()) - + -- TODO: replace by airbase.parking if AIRBASE is updated. local parking=airbase:GetParkingSpotsTable() - + local spot=nil --Wrapper.Airbase#AIRBASE.ParkingSpot local dist=nil - local distmin=math.huge + local distmin=math.huge for _,_parking in pairs(parking) do local parking=_parking --Wrapper.Airbase#AIRBASE.ParkingSpot dist=coord:Get2DDistance(parking.Coordinate) if dist