Merge pull request #699 from FlightControl-Master/funkyfranky

Funkyfranky
This commit is contained in:
Sven Van de Velde 2017-09-15 14:38:08 +02:00 committed by GitHub
commit a9679f831d
2 changed files with 176 additions and 3269 deletions

View File

@ -86,7 +86,7 @@
-- @field #table departure_ports Array containing the names of the destination airports.
-- @field #table destination_ports Array containing the names of the destination airports.
-- @field #table ratcraft Array with the spawned RAT aircraft.
-- @field #number Tinactive Time in seconds after which inactive units will be destroyed. Default is 180 seconds.
-- @field #number Tinactive Time in seconds after which inactive units will be destroyed. Default is 300 seconds.
-- @field #boolean reportstatus Aircraft report status.
-- @field #number statusinterval Intervall between status checks (and reports if enabled).
-- @field #boolean placemarkers Place markers of waypoints on F10 map.
@ -103,6 +103,8 @@
-- @field #boolean respawn_at_landing Respawn aircraft the moment they land rather than at engine shutdown.
-- @field #number respawn_delay Delay in seconds until repawn happens after landing.
-- @field #table markerids Array with marker IDs.
-- @field #string livery Livery of the aircraft set by user.
-- @field #string skill Skill of AI.
-- @extends Functional.Spawn#SPAWN
---# RAT class, extends @{Spawn#SPAWN}
@ -122,7 +124,7 @@
-- ### The default behavior can be changed:
--
-- * A specific departure and/or destination airport can be chosen.
-- * Valid coalitions can be set, e.g. only red, blue or neutral, all three <EFBFBD>colours<EFBFBD>.
-- * Valid coalitions can be set, e.g. only red, blue or neutral, all three "colours".
-- * It is possible to start in air within a zone defined in the mission editor or within a zone above an airport of the map.
--
-- ## Flight Plan
@ -151,7 +153,7 @@
-- * Give the group a good name. In the example above the group is named "RAT_YAK".
-- * Activate the "LATE ACTIVATION" tick box. Note that this aircraft will not be spawned itself but serves a template for each RAT aircraft spawned when the mission starts.
--
-- Voil<EFBFBD>, your already done!
-- Voilà, your already done!
--
-- Optionally, you can set a specific livery for the aircraft or give it some weapons.
-- However, the aircraft will by default not engage any enemies. Think of them as beeing on a peaceful or ferry mission.
@ -263,7 +265,7 @@ RAT={
departure_ports={}, -- Array containing the names of the departure airports.
destination_ports={}, -- Array containing the names of the destination airports.
ratcraft={}, -- Array with the spawned RAT aircraft.
Tinactive=180, -- Time in seconds after which inactive units will be destroyed. Default is 180 seconds.
Tinactive=300, -- Time in seconds after which inactive units will be destroyed. Default is 300 seconds.
reportstatus=false, -- Aircraft report status.
statusinterval=30, -- Intervall between status checks (and reports if enabled).
placemarkers=false, -- Place markers of waypoints on F10 map.
@ -280,6 +282,8 @@ RAT={
respawn_at_landing=false, -- Respawn aircraft the moment they land rather than at engine shutdown.
respawn_delay=nil, -- Delay in seconds until repawn happens after landing.
markerids={}, -- Array with marker IDs.
livery=nil, -- Livery of the aircraft.
skill="High", -- Skill of AI.
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -390,7 +394,7 @@ RAT.id="RAT | "
--DONE: Improve behaviour when no destination or departure airports were found. Leads to crash, e.g. 1184: attempt to get length of local 'destinations' (a nil value)
--DONE: Check cases where aircraft get shot down.
--DONE: Handle the case where more than 10 RAT objects are spawned. Likewise, more than 10 groups of one object. Causes problems with the number of menu items! ==> not now!
--TODO: Add custom livery choice if possible.
--DONE: Add custom livery choice if possible.
--TODO: When only a destination is set, it should be checked that the departure is within range. Also, that departure and destination are not the same.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -553,9 +557,7 @@ function RAT:SetCoalition(friendly)
elseif friendly:lower()=="red" then
self.friendly=RAT.coal.red
elseif friendly:lower()=="redonly" then
self.friendly=RAT.coal.red
elseif friendly:lower()=="blue" then
self.friendly=RAT.coal.blue
self.friendly=RAT.coal.redonly
elseif friendly:lower()=="neutral" then
self.friendly=RAT.coal.neutral
else
@ -671,6 +673,18 @@ function RAT:SetDestination(names)
end
--- Set livery of aircraft. If more than one livery is specified in a table, the actually used one is chosen randomly from the selection.
-- @param #RAT self
-- @param #string skins Name of livery or table of names of liveries.
function RAT:Livery(skins)
if type(skins)=="string" then
self.livery={skins}
else
self.livery=skins
end
end
--- Aircraft will continue their journey from their destination. This means they are respawned at their destination and get a new random destination.
-- @param #RAT self
-- @param #boolean switch Turn journey on=true or off=false. If no value is given switch=true.
@ -711,7 +725,7 @@ function RAT:RespawnAfterLanding(delay)
self.respawn_delay=delay
end
--- Set the time after which inactive groups will be destroyed. Default is 180 seconds.
--- Set the time after which inactive groups will be destroyed. Default is 300 seconds.
-- @param #RAT self
-- @param #number time Time in seconds.
function RAT:TimeDestroyInactive(time)
@ -828,6 +842,13 @@ function RAT:SetFLmax(height)
self.FLmaxuser=height*RAT.unit.FL2m
end
--- Set max cruising altitude above sea level.
-- @param #RAT self
-- @param #number alt Altitude ASL in meters.
function RAT:SetMaCruiseAltitude(alt)
self.FLmaxuser=alt
end
--- Set min flight level. Setting this value will overrule all other logic. Aircraft will try to fly at higher than this FL regardless.
-- @param #RAT self
-- @param #number height Maximum FL in hundrets of feet.
@ -835,6 +856,13 @@ function RAT:SetFLmin(height)
self.FLminuser=height*RAT.unit.FL2m
end
--- Set min cruising altitude above sea level.
-- @param #RAT self
-- @param #number alt Altitude ASL in meters.
function RAT:SetMinCruiseAltitude(alt)
self.FLminuser=alt
end
--- Set flight level of cruising part. This is still be checked for consitancy with selected route and prone to radomization.
-- Default is FL200 for planes and FL005 for helicopters.
-- @param #RAT self
@ -843,6 +871,13 @@ function RAT:SetFLcruise(height)
self.aircraft.FLcruise=height*RAT.unit.FL2m
end
--- Set cruising altitude. This is still be checked for consitancy with selected route and prone to radomization.
-- @param #RAT self
-- @param #number alt Cruising altitude ASL in meters.
function RAT:SetCruiseAltitude(alt)
self.aircraft.FLcruise=alt
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Initialize basic parameters of the aircraft based on its (template) group in the mission editor.
@ -1075,9 +1110,6 @@ function RAT:_SetRoute(takeoff, _departure, _destination)
-- Min cruise speed 70% of max cruise or 600 km/h whichever is lower.
local VxCruiseMin = math.min(VxCruiseMax*0.70, 166)
-- Cruise speed (randomized).
--local VxCruise = self:_Randomize((VxCruiseMax-VxCruiseMin)/2+VxCruiseMin, 0.3 , VxCruiseMin, VxCruiseMax)
-- Cruise speed (randomized). Expectation value at midpoint between min and max.
local VxCruise = self:_Random_Gaussian((VxCruiseMax-VxCruiseMin)/2+VxCruiseMin, (VxCruiseMax-VxCruiseMax)/4, VxCruiseMin, VxCruiseMax)
@ -1151,6 +1183,13 @@ function RAT:_SetRoute(takeoff, _departure, _destination)
H_departure=Pdeparture.y
end
-- Adjust min distance between departure and destination for user set min flight level.
if self.FLminuser then
self.mindist=self:_MinDistance(AlphaClimb, AlphaDescent, self.FLminuser-H_departure)
local text=string.format("Adjusting min distance to %d km (for given min FL%03d)", self.mindist/1000, self.FLminuser/RAT.unit.FL2m)
env.info(RAT.id..text)
end
-- DESTINATION AIRPORT
local destination=nil
if _destination then
@ -1269,9 +1308,14 @@ function RAT:_SetRoute(takeoff, _departure, _destination)
FLmax=self.FLmaxuser
end
-- Set cruise altitude: default with 100% randomization but limited to FLmin and FLmax.
--local FLcruise=self:_Randomize(self.aircraft.FLcruise, 1.0, FLmin, FLmax)
-- Adjust FLcruise to be at leat FLmin and at most FLmax
if self.aircraft.FLcruise<FLmin then
self.aircraft.FLcruise=FLmin
end
if self.aircraft.FLcruise>FLmax then
self.aircraft.FLcruise=FLmax
end
-- Set cruise altitude. Selected from Gaussian distribution but limited to FLmin and FLmax.
local FLcruise=self:_Random_Gaussian(self.aircraft.FLcruise, (FLmax-FLmin)/4, FLmin, FLmax)
@ -2269,61 +2313,6 @@ end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Modifies the template of the group to be spawned.
-- In particular, the waypoints of the group's flight plan are copied into the spawn template.
-- This allows to spawn at airports and also land at other airports, i.e. circumventing the DCS "landing bug".
-- @param #RAT self
-- @param #table waypoints The waypoints of the AI flight plan.
function RAT:_ModifySpawnTemplate(waypoints)
-- The 3D vector of the first waypoint, i.e. where we actually spawn the template group.
local PointVec3 = {x=waypoints[1].x, y=waypoints[1].alt, z=waypoints[1].y}
-- Heading from first to seconds waypoints to align units in case of air start.
local heading = self:_Course(waypoints[1], waypoints[2])
if self:_GetSpawnIndex(self.SpawnIndex+1) then
-- Get copy of spawn template.
local SpawnTemplate = self.SpawnGroups[self.SpawnIndex].SpawnTemplate
if SpawnTemplate then
self:T(SpawnTemplate)
-- Translate the position of the Group Template to the Vec3.
for UnitID = 1, #SpawnTemplate.units do
self:T('Before Translation SpawnTemplate.units['..UnitID..'].x = '..SpawnTemplate.units[UnitID].x..', SpawnTemplate.units['..UnitID..'].y = '..SpawnTemplate.units[UnitID].y)
local UnitTemplate = SpawnTemplate.units[UnitID]
local SX = UnitTemplate.x
local SY = UnitTemplate.y
local BX = SpawnTemplate.route.points[1].x
local BY = SpawnTemplate.route.points[1].y
local TX = PointVec3.x + (SX-BX)
local TY = PointVec3.z + (SY-BY)
SpawnTemplate.units[UnitID].x = TX
SpawnTemplate.units[UnitID].y = TY
SpawnTemplate.units[UnitID].alt = PointVec3.y
SpawnTemplate.units[UnitID].heading = math.rad(heading)
self:T('After Translation SpawnTemplate.units['..UnitID..'].x = '..SpawnTemplate.units[UnitID].x..', SpawnTemplate.units['..UnitID..'].y = '..SpawnTemplate.units[UnitID].y)
end
-- Copy waypoints into spawntemplate. By this we avoid the nasty DCS "landing bug" :)
for i,wp in ipairs(waypoints) do
SpawnTemplate.route.points[i]=wp
end
-- Also modify x,y of the template. Not sure why.
SpawnTemplate.x = PointVec3.x
SpawnTemplate.y = PointVec3.z
--SpawnTemplate.uncontrolled=true
-- Update modified template for spawn group.
self.SpawnGroups[self.SpawnIndex].SpawnTemplate=SpawnTemplate
self:T(SpawnTemplate)
end
end
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -2381,7 +2370,7 @@ end
-- @param #number d Distance between the two airports [m].
-- @param #number phi Angle between departure and destination [rad].
-- @param #number h0 Height [m] of departure airport. Note we implicitly assume that the height difference between departure and destination is negligible.
-- @return #number Maximal flight level in meters.
-- @return #number Maximal flight level in meters.
function RAT:_FLmax(alpha, beta, d, phi, h0)
-- Solve ASA triangle for one side (d) and two adjacent angles (alpha, beta) given.
local gamma=math.rad(180)-alpha-beta
@ -2402,6 +2391,18 @@ function RAT:_FLmax(alpha, beta, d, phi, h0)
return h3+h0
end
--- Calculate min distance between departure and destination for given minimum flight level and climb/decent rates
-- @param #RAT self
-- @param #number alpha Angle of climb [rad].
-- @param #number beta Angle of descent [rad].
-- @param #number h min height AGL.
-- @return #number Minimum distance between departure and destiantion.
function RAT:_MinDistance(alpha, beta, h)
local d1=h/math.tan(alpha)
local d2=h/math.tan(beta)
return d1+d2
end
--- Test if an airport exists on the current map.
-- @param #RAT self
@ -2560,11 +2561,23 @@ function RAT:_Random_Gaussian(x0, sigma, xmin, xmax)
-- Cut-off distribution at xmin.
if xmin then
r=math.max(r, xmin)
if r<xmin then
if xmax then
r=math.min(math.max(x0,xmin), xmax)
else
r=math.max(x0,xmin)
end
end
end
-- Cut-off distribution at xmax.
if xmax then
r=math.min(r, xmax)
if r>xmax then
if xmin then
r=math.max(math.min(x0,xmax),xmin)
else
r=math.min(x0,xmax)
end
end
end
return r
@ -2615,4 +2628,94 @@ function RAT:_DeleteMarkers()
--self:E({"self ids after: ", self.markerids})
end
--- Utility function which checks if table contains a specific value.
-- @param #RAT self
-- @param #table tab Table with elements to check.
-- @param #string val The value we are looking for.
-- @return #boolean True if element in the list, false otherwise.
function RAT:has_value (tab, val)
for _,value in pairs(tab) do
if value == val then
return true
end
end
return false
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Modifies the template of the group to be spawned.
-- In particular, the waypoints of the group's flight plan are copied into the spawn template.
-- This allows to spawn at airports and also land at other airports, i.e. circumventing the DCS "landing bug".
-- @param #RAT self
-- @param #table waypoints The waypoints of the AI flight plan.
function RAT:_ModifySpawnTemplate(waypoints)
-- The 3D vector of the first waypoint, i.e. where we actually spawn the template group.
local PointVec3 = {x=waypoints[1].x, y=waypoints[1].alt, z=waypoints[1].y}
-- Heading from first to seconds waypoints to align units in case of air start.
local heading = self:_Course(waypoints[1], waypoints[2])
if self:_GetSpawnIndex(self.SpawnIndex+1) then
local SpawnTemplate = self.SpawnGroups[self.SpawnIndex].SpawnTemplate
if SpawnTemplate then
self:T(SpawnTemplate)
-- Translate the position of the Group Template to the Vec3.
for UnitID = 1, #SpawnTemplate.units do
self:T('Before Translation SpawnTemplate.units['..UnitID..'].x = '..SpawnTemplate.units[UnitID].x..', SpawnTemplate.units['..UnitID..'].y = '..SpawnTemplate.units[UnitID].y)
-- Tranlate position.
local UnitTemplate = SpawnTemplate.units[UnitID]
local SX = UnitTemplate.x
local SY = UnitTemplate.y
local BX = SpawnTemplate.route.points[1].x
local BY = SpawnTemplate.route.points[1].y
local TX = PointVec3.x + (SX-BX)
local TY = PointVec3.z + (SY-BY)
SpawnTemplate.units[UnitID].x = TX
SpawnTemplate.units[UnitID].y = TY
SpawnTemplate.units[UnitID].alt = PointVec3.y
SpawnTemplate.units[UnitID].heading = math.rad(heading)
-- Set (another) livery.
if self.livery then
SpawnTemplate.units[UnitID].livery_id = self.livery[math.random(#self.livery)]
end
--SpawnTemplate.units[UnitID]["type"] = "Tu-142"
-- Set AI skill.
SpawnTemplate.units[UnitID]["skill"] = self.skill
-- Onboard number.
SpawnTemplate.units[UnitID]["onboard_num"] = self.SpawnIndex
-- Parking spot.
--SpawnTemplate.units[UnitID]["parking"]=19
self:T('After Translation SpawnTemplate.units['..UnitID..'].x = '..SpawnTemplate.units[UnitID].x..', SpawnTemplate.units['..UnitID..'].y = '..SpawnTemplate.units[UnitID].y)
end
-- Copy waypoints into spawntemplate. By this we avoid the nasty DCS "landing bug" :)
for i,wp in ipairs(waypoints) do
SpawnTemplate.route.points[i]=wp
end
-- Also modify x,y of the template. Not sure why.
SpawnTemplate.x = PointVec3.x
SpawnTemplate.y = PointVec3.z
--SpawnTemplate.uncontrolled=true
-- Update modified template for spawn group.
self.SpawnGroups[self.SpawnIndex].SpawnTemplate=SpawnTemplate
self:T(SpawnTemplate)
end
end
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

File diff suppressed because it is too large Load Diff