diff --git a/Moose Development/Moose/Functional/RAT.lua b/Moose Development/Moose/Functional/RAT.lua index 4c8fca65e..408ec1058 100644 --- a/Moose Development/Moose/Functional/RAT.lua +++ b/Moose Development/Moose/Functional/RAT.lua @@ -866,7 +866,10 @@ function RAT:Spawn(naircraft) local Tstop=Tstart+dt*(self.ngroups-1) -- Status check and report scheduler. - SCHEDULER:New(nil, self.Status, {self}, Tstart+1, self.statusinterval) + --SCHEDULER:New(nil, self.Status, {self}, Tstart+1, self.statusinterval) + --self.sid_Status=self:ScheduleRepeat(Start,Repeat,RandomizeFactor,Stop,SchedulerFunction,...) + self.sid_Status=self:ScheduleRepeat(Tstart+1, self.statusinterval, nil, nil, RAT.Status, self) + -- Handle events. self:HandleEvent(EVENTS.Birth, self._OnBirth) @@ -884,13 +887,13 @@ function RAT:Spawn(naircraft) end -- Start scheduled spawning. - SCHEDULER:New(nil, self._SpawnWithRoute, {self}, Tstart, dt, 0.0, Tstop) - - --self.sid_spawn=self.Scheduler:Schedule(MasterObject,SchedulerFunction,SchedulerArguments,Start,Repeat,RandomizeFactor,Stop,TraceLevel,Fsm) + --SCHEDULER:New(nil, self._SpawnWithRoute, {self}, Tstart, dt, 0.0, Tstop) + self.sid_Spawn=self:ScheduleRepeat(Tstart, dt, 0.0, Tstop, RAT._SpawnWithRoute, self) -- Start scheduled activation of uncontrolled groups. if self.uncontrolled and self.activate_uncontrolled then - SCHEDULER:New(nil, self._ActivateUncontrolled, {self}, self.activate_delay, self.activate_delta, self.activate_frand) + --SCHEDULER:New(nil, self._ActivateUncontrolled, {self}, self.activate_delay, self.activate_delta, self.activate_frand) + self.sid_Activate=self:ScheduleRepeat(self.activate_delay, self.activate_delta, self.activate_frand, nil, RAT._ActivateUncontrolled, self) end return true @@ -1710,7 +1713,7 @@ function RAT:Uncontrolled() return self end ---- Activate uncontrolled aircraft. +--- Define how aircraft that are spawned in uncontrolled state are activate. -- @param #RAT self -- @param #number maxactivated Maximal numnber of activated aircraft. Absolute maximum will be the number of spawned groups. Default is 1. -- @param #number delay Time delay in seconds before (first) aircraft is activated. Default is 1 second. @@ -2240,14 +2243,19 @@ function RAT:_SpawnWithRoute(_departure, _destination, _takeoff, _landing, _live end --- Function called when flight is holding. - function flightgroup.OnAfterHolding(flightgroup, From,Event,To) - self:T(self.lid..string.format("RAT group is HOLDING ==> ATCRegisterFlight")) - self:_ATCRegisterFlight(groupname, timer.getTime()) + function flightgroup.OnAfterHolding(Flightgroup, From, Event, To) + local flightgroup=Flightgroup --Ops.FlightGroup#FLIGHTGROUP + + if self.ATCswitch then + self:T(self.lid..string.format("RAT group is HOLDING ==> ATCRegisterFlight")) + self:_ATCRegisterFlight(groupname, timer.getTime()) + end -- Register aircraft at ATC. if self.ATCswitch then if self.f10menu then - MENU_MISSION_COMMAND:New("Clear for landing", self.Menu[self.SubMenuName].groups[self.SpawnIndex], self.ClearForLanding, self, groupname) + --MENU_MISSION_COMMAND:New("Clear for landing", self.Menu[self.SubMenuName].groups[self.SpawnIndex], self.ClearForLanding, self, groupname) + MENU_MISSION_COMMAND:New("Clear for landing", self.Menu[self.SubMenuName].groups[self.SpawnIndex], flightgroup.ClearToLand, flightgroup) end self:_ATCRegisterFlight(groupname, timer.getTime()) end @@ -2452,6 +2460,10 @@ function RAT:_Respawn(group, lastpos, delay) local _lastpos=nil if self.continuejourney then + + --- + -- Continue Journey + --- -- We continue our journey from the old departure airport. _departure=destination:GetName() @@ -2462,7 +2474,7 @@ function RAT:_Respawn(group, lastpos, delay) -- Last known position of the aircraft, which should be the sparking spot location. -- Note: we have to check that it was supposed to land and not respawned directly after landing or after takeoff. -- TODO: Need to think if continuejourney with respawn_after_takeoff actually makes sense. - if landing==RAT.wp.landing and lastpos and not (self.respawn_at_landing or self.respawn_after_takeoff) then + if landing==RAT.wp.landing and not (self.respawn_at_landing or self.respawn_after_takeoff) then -- Check that we have an airport or FARP but not a ship (which would be categroy 1). if destination:GetCategory()==4 then _lastpos=lastpos @@ -2500,7 +2512,11 @@ function RAT:_Respawn(group, lastpos, delay) end elseif self.commute then - + + --- + -- Commute + --- + -- We commute between departure and destination. if self.starshape==true then @@ -2623,39 +2639,14 @@ function RAT:_Despawn(group, delay) -- Get ratcraft. local ratcraft=self.ratcraft[index] --#RAT.RatCraft - - self.ratcraft[index].group=nil - self.ratcraft[index]["status"]="Dead" - - --TODO: Maybe here could be some more arrays deleted? Somehow this causes issues! - --[[ - --self.ratcraft[index]["group"]=group - self.ratcraft[index]["destination"]=nil - self.ratcraft[index]["departure"]=nil - self.ratcraft[index]["waypoints"]=nil - self.ratcraft[index]["airborne"]=nil - self.ratcraft[index]["Tground"]=nil - self.ratcraft[index]["Pground"]=nil - self.ratcraft[index]["Tlastcheck"]=nil - self.ratcraft[index]["P0"]=nil - self.ratcraft[index]["Pnow"]=nil - self.ratcraft[index]["Distance"]=nil - self.ratcraft[index].takeoff=nil - self.ratcraft[index].landing=nil - self.ratcraft[index].wpholding=nil - self.ratcraft[index].wpfinal=nil - self.ratcraft[index].active=false - self.ratcraft[index]["status"]=nil - self.ratcraft[index].livery=nil - self.ratcraft[index].despawnme=nil - self.ratcraft[index].nrespawn=nil - ]] - --ratcraft.flightgroup:Destroy(0) + -- Despawn flightgroup and stop. ratcraft.flightgroup:Despawn() ratcraft.flightgroup:__Stop(0.1) - + -- Nil ratcraft in table. + self.ratcraft[index].group=nil + self.ratcraft[index]["status"]="Dead" self.ratcraft[index]=nil -- Remove submenu for this group. @@ -3916,109 +3907,106 @@ function RAT:_OnBirth(EventData) -- Get the template name of the group. This can be nil if this was not a spawned group. local EventPrefix = self:_GetPrefixFromGroup(SpawnGroup) - if EventPrefix then + -- Check that the template name actually belongs to this object. + if EventPrefix and EventPrefix == self.alias then - -- Check that the template name actually belongs to this object. - if EventPrefix == self.alias then + local text="Event: Group "..SpawnGroup:GetName().." was born." + self:T(self.lid..text) - local text="Event: Group "..SpawnGroup:GetName().." was born." - self:T(self.lid..text) + -- Set status. + local status="unknown in birth" + if SpawnGroup:InAir() then + status=RAT.status.EventBirthAir + elseif self.uncontrolled then + status=RAT.status.Uncontrolled + else + status=RAT.status.EventBirth + end + self:_SetStatus(SpawnGroup, status) + - -- Set status. - local status="unknown in birth" - if SpawnGroup:InAir() then - status=RAT.status.EventBirthAir - elseif self.uncontrolled then - status=RAT.status.Uncontrolled + -- Get some info ablout this flight. + local i=self:GetSpawnIndexFromGroup(SpawnGroup) + + local ratcraft=self.ratcraft[i] --#RAT.RatCraft + + local _departure=ratcraft.departure:GetName() + local _destination=ratcraft.destination:GetName() + local _nrespawn=ratcraft.nrespawn + local _takeoff=ratcraft.takeoff + local _landing=ratcraft.landing + local _livery=ratcraft.livery + + -- Some is only useful for an actual airbase (not a zone). + local _airbase=AIRBASE:FindByName(_departure) + + -- Check if aircraft group was accidentally spawned on the runway. + -- This can happen due to no parking slots available and other DCS bugs. + local onrunway=false + if _airbase then + -- Check that we did not want to spawn at a runway or in air. + if self.checkonrunway and _takeoff ~= RAT.wp.runway and _takeoff ~= RAT.wp.air then + onrunway=_airbase:CheckOnRunWay(SpawnGroup, self.onrunwayradius, false) + end + end + + -- Workaround if group was spawned on runway. + if onrunway then + + -- Error message. + local text=string.format("ERROR: RAT group of %s was spawned on runway. Group #%d will be despawned immediately!", self.alias, i) + MESSAGE:New(text,30):ToAllIf(self.Debug) + self:E(self.lid..text) + if self.Debug then + SpawnGroup:FlareRed() + end + + -- Despawn the group. + self:_Despawn(SpawnGroup) + + -- Try to respawn the group if there is at least another airport or random airport selection is used. + if (self.Ndeparture_Airports>=2 or self.random_departure) and _nrespawn=2 or self.random_departure) and _nrespawn