Added enumerators (untested)

This commit is contained in:
funkyfranky 2017-09-05 16:56:06 +02:00
parent 9fc00dd9c3
commit c84df9bf5a

View File

@ -101,6 +101,20 @@ RAT.cat={
heli="heli" heli="heli"
} }
--- RAT takeoff style.
-- @field #RAT waypoint
RAT.waypoint={
air=1,
runway=2,
hot=3,
cold=4,
climb=5,
cruise=6,
descent=7,
holding=8,
landing=9,
}
--- RAT unit conversions. --- RAT unit conversions.
-- @field #RAT unit -- @field #RAT unit
-- @field #number ft2meter -- @field #number ft2meter
@ -116,6 +130,8 @@ RAT.unit={
-- @field #RAT markerid -- @field #RAT markerid
RAT.markerid=0 RAT.markerid=0
--- Main F10 menu.
-- @field #RAT MenuF10
RAT.MenuF10=nil RAT.MenuF10=nil
--- Some ID to identify where we are --- Some ID to identify where we are
@ -138,7 +154,7 @@ myid="RAT | "
--DONE: Improve status reports. --DONE: Improve status reports.
--TODO: Check compatibility with other #SPAWN functions. --TODO: Check compatibility with other #SPAWN functions.
--DONE: Add possibility to continue journey at destination. Need "place" in event data for that. --DONE: Add possibility to continue journey at destination. Need "place" in event data for that.
--TODO: Add enumerators and get rid off error prone string comparisons. --DONE: Add enumerators and get rid off error prone string comparisons.
--DONE: Check that FARPS are not used as airbases for planes. --DONE: Check that FARPS are not used as airbases for planes.
--DONE: Add special cases for ships (similar to FARPs). --DONE: Add special cases for ships (similar to FARPs).
--DONE: Add cases for helicopters. --DONE: Add cases for helicopters.
@ -226,7 +242,7 @@ function RAT:Spawn(naircraft)
local Tstart=self.spawndelay local Tstart=self.spawndelay
local dt=self.spawninterval local dt=self.spawninterval
-- Ensure that interval is >= 180 seconds if spawn at runway is chosen. Aircraft need time to takeoff or the runway gets jammed. -- Ensure that interval is >= 180 seconds if spawn at runway is chosen. Aircraft need time to takeoff or the runway gets jammed.
if self.takeoff:lower()=="takeoff-runway" or self.takeoff:lower()=="runway" then if self.takeoff==RAT.waypoint.runway then
dt=math.max(dt, 180) dt=math.max(dt, 180)
end end
local Tstop=Tstart+dt*(naircraft-1) local Tstop=Tstart+dt*(naircraft-1)
@ -268,24 +284,18 @@ end
-- @usage RAT:Takeoff("cold") will spawn RAT objects at airports with engines off. -- @usage RAT:Takeoff("cold") will spawn RAT objects at airports with engines off.
-- @usage RAT:Takeoff("air") will spawn RAT objects in air over random airports or within pre-defined zones. -- @usage RAT:Takeoff("air") will spawn RAT objects in air over random airports or within pre-defined zones.
function RAT:SetTakeoff(type) function RAT:SetTakeoff(type)
-- All possible types for random selection.
local types={"takeoff-cold", "takeoff-hot", "air"}
--TODO: Need to get rid of the string comparisons and introduce enumerators.
local _Type local _Type
if type:lower()=="takeoff-cold" or type:lower()=="cold" then if type:lower()=="takeoff-cold" or type:lower()=="cold" then
_Type="takeoff-cold" _Type=RAT.waypoint.cold
elseif type:lower()=="takeoff-hot" or type:lower()=="hot" then elseif type:lower()=="takeoff-hot" or type:lower()=="hot" then
_Type="takeoff-hot" _Type=RAT.waypoint.hot
elseif type:lower()=="takeoff-runway" or type:lower()=="runway" then elseif type:lower()=="takeoff-runway" or type:lower()=="runway" then
_Type="takeoff-runway" _Type=RAT.waypoint.runway
elseif type:lower()=="air" then elseif type:lower()=="air" then
_Type="air" _Type=RAT.waypoint.air
elseif type:lower()=="random" then
_Type=types[math.random(#types)]
else else
_Type="takeoff-hot" _Type=RAT.waypoint.hot
end end
self.takeoff=_Type self.takeoff=_Type
@ -517,9 +527,9 @@ function RAT:_InitAircraft(DCSgroup)
-- set category -- set category
if DCScategory==Group.Category.AIRPLANE then if DCScategory==Group.Category.AIRPLANE then
self.category="plane" self.category=RAT.cat.plane
elseif DCScategory==Group.Category.HELICOPTER then elseif DCScategory==Group.Category.HELICOPTER then
self.category="heli" self.category=RAT.cat.heli
else else
self.category="other" self.category="other"
env.error(myid.."Group of RAT is neither airplane nor helicopter!") env.error(myid.."Group of RAT is neither airplane nor helicopter!")
@ -547,7 +557,7 @@ function RAT:_InitAircraft(DCSgroup)
self.aircraft.ceiling=DCSdesc.Hmax self.aircraft.ceiling=DCSdesc.Hmax
-- Default flight level (ASL). -- Default flight level (ASL).
if self.category=="plane" then if self.category==RAT.cat.plane then
-- For planes: FL200 = 20000 ft = 6096 m. -- For planes: FL200 = 20000 ft = 6096 m.
self.aircraft.FLcruise=200*RAT.unit.FL2m self.aircraft.FLcruise=200*RAT.unit.FL2m
else else
@ -623,6 +633,9 @@ function RAT:_SpawnWithRoute(_departure, _destination)
local name=self.aircraft.type.." ID "..tostring(self.SpawnIndex) local name=self.aircraft.type.." ID "..tostring(self.SpawnIndex)
self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex]=MENU_MISSION:New(name, self.Menu[self.SpawnTemplatePrefix].groups) self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex]=MENU_MISSION:New(name, self.Menu[self.SpawnTemplatePrefix].groups)
MENU_MISSION_COMMAND:New("Status report", self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex], self.Status, self, true, self.SpawnIndex) MENU_MISSION_COMMAND:New("Status report", self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex], self.Status, self, true, self.SpawnIndex)
--TODO: no sure if it works with group as argument.
--MENU_MISSION_COMMAND:New("Despawn group", self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex], self._Despawn, self, self.SpawnIndex)
MENU_MISSION_COMMAND:New("Despawn group", self.Menu[self.SpawnTemplatePrefix].groups[self.SpawnIndex], self._Despawn, self, group)
end end
end end
@ -715,7 +728,7 @@ function RAT:_SetRoute(_departure, _destination)
-- Coordinates of departure point. -- Coordinates of departure point.
local Pdeparture local Pdeparture
if self.takeoff=="air" then if self.takeoff==RAT.waypoint.air then
-- For an air start, we take a random point within the spawn zone. -- For an air start, we take a random point within the spawn zone.
local vec2=departure:GetRandomVec2() local vec2=departure:GetRandomVec2()
--Pdeparture=COORDINATE:New(vec2.x, self.aircraft.FLcruise, vec2.y) --Pdeparture=COORDINATE:New(vec2.x, self.aircraft.FLcruise, vec2.y)
@ -726,10 +739,10 @@ function RAT:_SetRoute(_departure, _destination)
-- Height ASL of departure point. -- Height ASL of departure point.
local H_departure local H_departure
if self.takeoff=="air" then if self.takeoff==RAT.waypoint.air then
-- Departure altitude is 70% of default cruise with 30% variation and limited to 1000 m AGL (50 m for helos). -- Departure altitude is 70% of default cruise with 30% variation and limited to 1000 m AGL (50 m for helos).
local Hmin local Hmin
if self.category=="plane" then if self.category==RAT.cat.plane then
Hmin=1000 Hmin=1000
else else
Hmin=50 Hmin=50
@ -771,7 +784,7 @@ function RAT:_SetRoute(_departure, _destination)
-- DESCENT/HOLDING POINT -- DESCENT/HOLDING POINT
-- Get a random point between 5 and 10 km away from the destination. -- Get a random point between 5 and 10 km away from the destination.
local Vholding local Vholding
if self.category=="plane" then if self.category==RAT.cat.plane then
Vholding=destination:GetCoordinate():GetRandomVec2InRadius(10000, 5000) Vholding=destination:GetCoordinate():GetRandomVec2InRadius(10000, 5000)
else else
-- For helos we set a distance between 500 to 1000 m. -- For helos we set a distance between 500 to 1000 m.
@ -785,7 +798,7 @@ function RAT:_SetRoute(_departure, _destination)
-- Holding point altitude. For planes between 1600 and 2400 m AGL. For helos 160 to 240 m AGL. -- Holding point altitude. For planes between 1600 and 2400 m AGL. For helos 160 to 240 m AGL.
local h_holding local h_holding
if self.category=="plane" then if self.category==RAT.cat.plane then
h_holding=1200 h_holding=1200
else else
h_holding=150 h_holding=150
@ -828,7 +841,7 @@ function RAT:_SetRoute(_departure, _destination)
end end
-- For helicopters we take cruise alt between 50 to 1000 meters above ground. Default cruise alt is ~150 m. -- For helicopters we take cruise alt between 50 to 1000 meters above ground. Default cruise alt is ~150 m.
if self.category=="heli" then if self.category==RAT.cat.heli then
FLmin=math.max(H_departure, H_destination)+50 FLmin=math.max(H_departure, H_destination)+50
FLmax=math.max(H_departure, H_destination)+1000 FLmax=math.max(H_departure, H_destination)+1000
end end
@ -911,13 +924,13 @@ function RAT:_SetRoute(_departure, _destination)
local c6=Pdestination local c6=Pdestination
--Convert coordinates into route waypoints. --Convert coordinates into route waypoints.
local wp0=self:_Waypoint(self.takeoff, c0, VxClimb, H_departure, departure) local wp0=self:_Waypoint(self.takeoff, c0, VxClimb, H_departure, departure)
local wp1=self:_Waypoint("climb", c1, VxClimb, H_departure+(FLcruise-H_departure)/2) local wp1=self:_Waypoint(RAT.waypoint.climb, c1, VxClimb, H_departure+(FLcruise-H_departure)/2)
local wp2=self:_Waypoint("cruise", c2, VxCruise, FLcruise) local wp2=self:_Waypoint(RAT.waypoint.cruise, c2, VxCruise, FLcruise)
local wp3=self:_Waypoint("cruise", c3, VxCruise, FLcruise) local wp3=self:_Waypoint(RAT.waypoint.cruise, c3, VxCruise, FLcruise)
local wp4=self:_Waypoint("descent", c4, VxDescent, FLcruise-(FLcruise-(h_holding+H_holding))/2) local wp4=self:_Waypoint(RAT.waypoint.descent, c4, VxDescent, FLcruise-(FLcruise-(h_holding+H_holding))/2)
local wp5=self:_Waypoint("holding", c5, VxHolding, H_holding+h_holding) local wp5=self:_Waypoint(RAT.waypoint.holding, c5, VxHolding, H_holding+h_holding)
local wp6=self:_Waypoint("landing", c6, VxFinal, H_destination, destination) local wp6=self:_Waypoint(RAT.waypoint.landing, c6, VxFinal, H_destination, destination)
-- set waypoints -- set waypoints
local waypoints = {wp0, wp1, wp2, wp3, wp4, wp5, wp6} local waypoints = {wp0, wp1, wp2, wp3, wp4, wp5, wp6}
@ -952,7 +965,7 @@ function RAT:_PickDeparture()
-- Array of possible departure airports or zones. -- Array of possible departure airports or zones.
local departures={} local departures={}
if self.takeoff=="air" then if self.takeoff==RAT.waypoint.air then
if self.random_departure then if self.random_departure then
@ -997,7 +1010,7 @@ function RAT:_PickDeparture()
local departure=departures[math.random(#departures)] local departure=departures[math.random(#departures)]
local text local text
if self.takeoff=="air" then if self.takeoff==RAT.waypoint.air then
text="Chosen departure zone: "..departure:GetName() text="Chosen departure zone: "..departure:GetName()
else else
text="Chosen departure airport: "..departure:GetName().." (ID "..departure:GetID()..")" text="Chosen departure airport: "..departure:GetName().." (ID "..departure:GetID()..")"
@ -1019,17 +1032,8 @@ end
function RAT:_PickDestination(destinations, _random) function RAT:_PickDestination(destinations, _random)
env.info(myid.."pickdestinations _random = "..tostring(_random)) env.info(myid.."pickdestinations _random = "..tostring(_random))
self:E(destinations) self:E(destinations)
--[[
if self.random_destination or _random then --
-- All airports of friendly coalitons.
for _,airport in pairs(destinations) do
table.insert(destinations, airport)
end
else
]]
if not (self.random_destination or _random) then if not (self.random_destination or _random) then
destinations=nil destinations=nil
destinations={} destinations={}
@ -1134,6 +1138,7 @@ function RAT:_GetAirportsOfMap()
local _name=airbase:getName() local _name=airbase:getName()
local _myab=AIRBASE:FindByName(_name) local _myab=AIRBASE:FindByName(_name)
table.insert(self.airports_map, _myab) table.insert(self.airports_map, _myab)
--TODO: check here if MOOSE gives the same ID as the native DCS API
local text="Airport ID = ".._myab:GetID().." and Name = ".._myab:GetName()..", Category = ".._myab:GetCategory()..", TypeName = ".._myab:GetTypeName() local text="Airport ID = ".._myab:GetID().." and Name = ".._myab:GetName()..", Category = ".._myab:GetCategory()..", TypeName = ".._myab:GetTypeName()
if self.debug then if self.debug then
env.info(myid..text) env.info(myid..text)
@ -1151,9 +1156,9 @@ function RAT:_GetAirportsOfCoalition()
for _,airport in pairs(self.airports_map) do for _,airport in pairs(self.airports_map) do
if airport:GetCoalition()==coalition then if airport:GetCoalition()==coalition then
-- Planes cannot land on FARPs. -- Planes cannot land on FARPs.
local condition1=self.category=="plane" and airport:GetTypeName()=="FARP" local condition1=self.category==RAT.cat.plane and airport:GetTypeName()=="FARP"
-- Planes cannot land on ships. -- Planes cannot land on ships.
local condition2=self.category=="plane" and airport:GetCategory()==1 local condition2=self.category==RAT.cat.plane and airport:GetCategory()==1
if not (condition1 or condition2) then if not (condition1 or condition2) then
table.insert(self.airports, airport) table.insert(self.airports, airport)
end end
@ -1603,8 +1608,6 @@ function RAT:_Waypoint(Type, Coord, Speed, Altitude, Airport)
-- Altitude of input parameter or y-component of 3D-coordinate. -- Altitude of input parameter or y-component of 3D-coordinate.
local _Altitude=Altitude or Coord.y local _Altitude=Altitude or Coord.y
--TODO: _Type should be generalized to Grouptemplate.Type
-- Land height at given coordinate. -- Land height at given coordinate.
local Hland=Coord:GetLandHeight() local Hland=Coord:GetLandHeight()
@ -1614,53 +1617,53 @@ function RAT:_Waypoint(Type, Coord, Speed, Altitude, Airport)
local _alttype="RADIO" local _alttype="RADIO"
local _AID=nil local _AID=nil
if Type:lower()=="takeoff-cold" or Type:lower()=="cold" then if Type==RAT.waypoint.cold then
-- take-off with engine off -- take-off with engine off
_Type="TakeOffParking" _Type="TakeOffParking"
_Action="From Parking Area" _Action="From Parking Area"
_Altitude = 2 _Altitude = 2
_alttype="RADIO" _alttype="RADIO"
_AID = Airport:GetID() _AID = Airport:GetID()
elseif Type:lower()=="takeoff-hot" or Type:lower()=="hot" then elseif Type==RAT.waypoint.hot then
-- take-off with engine on -- take-off with engine on
_Type="TakeOffParkingHot" _Type="TakeOffParkingHot"
_Action="From Parking Area" _Action="From Parking Area"
_Altitude = 2 _Altitude = 2
_alttype="RADIO" _alttype="RADIO"
_AID = Airport:GetID() _AID = Airport:GetID()
elseif Type:lower()=="takeoff-runway" or Type:lower()=="runway" then elseif Type==RAT.waypoint.runway then
-- take-off from runway -- take-off from runway
_Type="TakeOff" _Type="TakeOff"
_Action="From Parking Area" _Action="From Parking Area"
_Altitude = 2 _Altitude = 2
_alttype="RADIO" _alttype="RADIO"
_AID = Airport:GetID() _AID = Airport:GetID()
elseif Type:lower()=="air" then elseif Type==RAT.waypoint.air then
-- air start -- air start
_Type="Turning Point" _Type="Turning Point"
_Action="Turning Point" _Action="Turning Point"
_alttype="BARO" _alttype="BARO"
elseif Type:lower()=="climb" then elseif Type==RAT.waypoint.climb then
_Type="Turning Point" _Type="Turning Point"
_Action="Turning Point" _Action="Turning Point"
--_Action="Fly Over Point" --_Action="Fly Over Point"
_alttype="BARO" _alttype="BARO"
elseif Type:lower()=="cruise" then elseif Type==RAT.waypoint.cruise then
_Type="Turning Point" _Type="Turning Point"
_Action="Turning Point" _Action="Turning Point"
--_Action="Fly Over Point" --_Action="Fly Over Point"
_alttype="BARO" _alttype="BARO"
elseif Type:lower()=="descent" then elseif Type==RAT.waypoint.descent then
_Type="Turning Point" _Type="Turning Point"
_Action="Turning Point" _Action="Turning Point"
--_Action="Fly Over Point" --_Action="Fly Over Point"
_alttype="BARO" _alttype="BARO"
elseif Type:lower()=="holding" then elseif Type==RAT.waypoint.holding then
_Type="Turning Point" _Type="Turning Point"
_Action="Turning Point" _Action="Turning Point"
--_Action="Fly Over Point" --_Action="Fly Over Point"
_alttype="BARO" _alttype="BARO"
elseif Type:lower()=="landing" or Type:lower()=="land" then elseif Type==RAT.waypoint.landing then
_Type="Land" _Type="Land"
_Action="Landing" _Action="Landing"
_Altitude = 2 _Altitude = 2
@ -1676,14 +1679,14 @@ function RAT:_Waypoint(Type, Coord, Speed, Altitude, Airport)
-- some debug info about input parameters -- some debug info about input parameters
local text=string.format("\n******************************************************\n") local text=string.format("\n******************************************************\n")
text=text..string.format("Template = %s\n", self.SpawnTemplatePrefix) text=text..string.format("Template = %s\n", self.SpawnTemplatePrefix)
text=text..string.format("Type: %s - %s\n", Type, _Type) text=text..string.format("Type: %i - %s\n", Type, _Type)
text=text..string.format("Action: %s\n", _Action) text=text..string.format("Action: %s\n", _Action)
text=text..string.format("Coord: x = %6.1f km, y = %6.1f km, alt = %6.1f m\n", Coord.x/1000, Coord.z/1000, Coord.y) text=text..string.format("Coord: x = %6.1f km, y = %6.1f km, alt = %6.1f m\n", Coord.x/1000, Coord.z/1000, Coord.y)
text=text..string.format("Speed = %6.1f m/s = %6.1f km/h = %6.1f knots\n", Speed, Speed*3.6, Speed*1.94384) text=text..string.format("Speed = %6.1f m/s = %6.1f km/h = %6.1f knots\n", Speed, Speed*3.6, Speed*1.94384)
text=text..string.format("Land = %6.1f m ASL\n", Hland) text=text..string.format("Land = %6.1f m ASL\n", Hland)
text=text..string.format("Altitude = %6.1f m (%s)\n", _Altitude, _alttype) text=text..string.format("Altitude = %6.1f m (%s)\n", _Altitude, _alttype)
if Airport then if Airport then
if Type:lower() == "air" then if Type==RAT.waypoint.air then
text=text..string.format("Zone = %s\n", Airport:GetName()) text=text..string.format("Zone = %s\n", Airport:GetName())
else else
text=text..string.format("Airport = %s with ID %i\n", Airport:GetName(), Airport:GetID()) text=text..string.format("Airport = %s with ID %i\n", Airport:GetName(), Airport:GetID())
@ -1727,8 +1730,10 @@ function RAT:_Waypoint(Type, Coord, Speed, Altitude, Airport)
["steer"] = 2, ["steer"] = 2,
} }
-- task -- task
if Type:lower()=="holding" then if Type==RAT.waypoint.holding then
RoutePoint.task=self:_TaskHolding({x=Coord.x, y=Coord.z}, Altitude, Speed) -- Duration of holing. Between 10 and 170 seconds.
local Duration=self:_Randomize(90,0.9)
RoutePoint.task=self:_TaskHolding({x=Coord.x, y=Coord.z}, Altitude, Speed, Duration)
else else
RoutePoint.task = {} RoutePoint.task = {}
RoutePoint.task.id = "ComboTask" RoutePoint.task.id = "ComboTask"
@ -1848,17 +1853,19 @@ end
--- Orbit at a specified position at a specified alititude with a specified speed. --- Orbit at a specified position at a specified alititude with a specified speed.
-- @param #RAT self -- @param #RAT self
-- @param Dcs.DCSTypes#Vec2 P1 The point to hold the position. -- @param Dcs.DCSTypes#Vec2 P1 The point to hold the position.
-- @param #number Altitude The altitude AGL to hold the position. -- @param #number Altitude The altitude ASL at which to hold the position.
-- @param #number Speed The speed flying when holding the position in m/s. -- @param #number Speed The speed flying when holding the position in m/s.
-- @param #number Duration Duration of holding pattern in seconds.
-- @return Dcs.DCSTasking.Task#Task DCSTask -- @return Dcs.DCSTasking.Task#Task DCSTask
function RAT:_TaskHolding(P1, Altitude, Speed) function RAT:_TaskHolding(P1, Altitude, Speed, Duration)
local LandHeight = land.getHeight(P1)
--local LandHeight = land.getHeight(P1)
--TODO: randomize P1 --TODO: randomize P1
-- Second point is 3 km north of P1 and 200 m for helos. -- Second point is 3 km north of P1 and 200 m for helos.
local dx=3000 local dx=3000
local dy=0 local dy=0
if self.category=="heli" then if self.category==RAT.cat.heli then
dx=200 dx=200
dy=0 dy=0
end end
@ -1873,18 +1880,15 @@ function RAT:_TaskHolding(P1, Altitude, Speed)
point = P1, point = P1,
point2 = P2, point2 = P2,
speed = Speed, speed = Speed,
altitude = Altitude + LandHeight altitude = Altitude
} }
} }
-- Duration of holing. Between 10 and 170 seconds.
local d=self:_Randomize(90,0.9)
local DCSTask={} local DCSTask={}
DCSTask.id="ControlledTask" DCSTask.id="ControlledTask"
DCSTask.params={} DCSTask.params={}
DCSTask.params.task=Task DCSTask.params.task=Task
DCSTask.params.stopCondition={duration=d} DCSTask.params.stopCondition={duration=Duration}
return DCSTask return DCSTask
end end
@ -1935,6 +1939,7 @@ function RAT:_AirportExists(name)
return false return false
end end
--- Set ROE for a group. --- Set ROE for a group.
-- @param #RAT self -- @param #RAT self
-- @param Wrapper.Group#GROUP group Group for which the ROE is set. -- @param Wrapper.Group#GROUP group Group for which the ROE is set.
@ -1991,6 +1996,7 @@ function RAT:_SetCoalitionTable()
self:T({"Coalition table: ", self.ctable}) self:T({"Coalition table: ", self.ctable})
end end
---Determine the heading from point a to point b. ---Determine the heading from point a to point b.
--@param #RAT self --@param #RAT self
--@param Core.Point#COORDINATE a Point from. --@param Core.Point#COORDINATE a Point from.