diff --git a/Moose Development/LDT External Tools/Moose DOCUMENTATION Generate.launch b/Moose Development/LDT External Tools/Moose DOCUMENTATION Generate.launch
index 4d645a201..85f0a3b46 100644
--- a/Moose Development/LDT External Tools/Moose DOCUMENTATION Generate.launch
+++ b/Moose Development/LDT External Tools/Moose DOCUMENTATION Generate.launch
@@ -3,7 +3,6 @@
-
-
-
+
+
diff --git a/Moose Development/LDT External Tools/Moose LOADER Generate Dynamic Moose.lua.launch b/Moose Development/LDT External Tools/Moose LOADER Generate Dynamic Moose.lua.launch
index c55f6dd26..ffc89c77d 100644
--- a/Moose Development/LDT External Tools/Moose LOADER Generate Dynamic Moose.lua.launch
+++ b/Moose Development/LDT External Tools/Moose LOADER Generate Dynamic Moose.lua.launch
@@ -3,7 +3,7 @@
-
+
diff --git a/Moose Development/LDT External Tools/Moose LOADER Generate Static Moose.lua.launch b/Moose Development/LDT External Tools/Moose LOADER Generate Static Moose.lua.launch
index 121638ba4..b8a402dc5 100644
--- a/Moose Development/LDT External Tools/Moose LOADER Generate Static Moose.lua.launch
+++ b/Moose Development/LDT External Tools/Moose LOADER Generate Static Moose.lua.launch
@@ -3,7 +3,7 @@
-
+
diff --git a/Moose Development/LDT External Tools/Moose UPDATE ALL MOOSE Demonstration Missions.launch b/Moose Development/LDT External Tools/Moose UPDATE ALL MOOSE Demonstration Missions.launch
index d959c8bf5..5bf8b4ae1 100644
--- a/Moose Development/LDT External Tools/Moose UPDATE ALL MOOSE Demonstration Missions.launch
+++ b/Moose Development/LDT External Tools/Moose UPDATE ALL MOOSE Demonstration Missions.launch
@@ -4,6 +4,5 @@
-
-
+
diff --git a/Moose Development/Moose/Core/Radio.lua b/Moose Development/Moose/Core/Radio.lua
index 38735de28..890474ed3 100644
--- a/Moose Development/Moose/Core/Radio.lua
+++ b/Moose Development/Moose/Core/Radio.lua
@@ -1,6 +1,6 @@
--- **Core** - The RADIO class is responsible for **transmitting radio communications**.
--
--- --- bitmap
+-- 
--
-- ===
--
@@ -27,19 +27,20 @@
--
-- ===
--
--- ### Authors: Hugues "Grey_Echo" Bousquet
+-- ### Author: Hugues "Grey_Echo" Bousquet
--
-- @module Radio
+
--- # 1) RADIO class, extends @{Base#BASE}
--
-- ## 1.1) RADIO usage
--
-- There are 3 steps to a successful radio transmission.
--
--- * First, you need to **"add" a @{#RADIO} object** to your @{Positionable#POSITIONABLE}. This is done using the @{Positionable#POSITIONABLE.GetRadio}() function,
+-- * First, you need to **"add a @{#RADIO} object** to your @{Positionable#POSITIONABLE}. This is done using the @{Positionable#POSITIONABLE.GetRadio}() function,
-- * Then, you will **set the relevant parameters** to the transmission (see below),
--- * When done, you can actually **broadcast the transmission** (i.e. play the sound) with the @{Positionable#POSITIONABLE.Broadcast}() function.
+-- * When done, you can actually **broadcast the transmission** (i.e. play the sound) with the @{RADIO.Broadcast}() function.
--
-- Methods to set relevant parameters for both a @{Unit#UNIT} or a @{Group#GROUP} or any other @{Positionable#POSITIONABLE}
--
@@ -53,7 +54,7 @@
-- * @{#RADIO.SetSubtitle}() : Set both the subtitle and its duration,
-- * @{#RADIO.NewUnitTransmission}() : Shortcut to set all the relevant parameters in one method call
--
--- Additional Methods to set relevant parameters if the transmiter is any other @{Wrapper.Positionable#POSITIONABLE}
+-- Additional Methods to set relevant parameters if the transmiter is any other @{Positionable#POSITIONABLE}
--
-- * @{#RADIO.SetPower}() : Sets the power of the antenna in Watts
-- * @{#RADIO.NewGenericTransmission}() : Shortcut to set all the relevant parameters in one method call
@@ -68,7 +69,7 @@
-- * Note that if the transmission has a subtitle, it will be readable, regardless of the quality of the transmission.
--
-- @type RADIO
--- @field Wrapper.Positionable#POSITIONABLE Positionable The transmiter
+-- @field Positionable#POSITIONABLE Positionable The transmiter
-- @field #string FileName Name of the sound file
-- @field #number Frequency Frequency of the transmission in Hz
-- @field #number Modulation Modulation of the transmission (either radio.modulation.AM or radio.modulation.FM)
diff --git a/Moose Development/Moose/Core/Zone.lua b/Moose Development/Moose/Core/Zone.lua
index 432a360a7..f174464bf 100644
--- a/Moose Development/Moose/Core/Zone.lua
+++ b/Moose Development/Moose/Core/Zone.lua
@@ -66,13 +66,6 @@
-- @module Zone
---- The ZONE_BASE class
--- @type ZONE_BASE
--- @field #string ZoneName Name of the zone.
--- @field #number ZoneProbability A value between 0 and 1. 0 = 0% and 1 = 100% probability.
--- @extends Core.Base#BASE
-
-
--- # 1) ZONE_BASE class, extends @{Base#BASE}
--
-- This class is an abstract BASE class for derived classes, and is not meant to be instantiated.
@@ -107,7 +100,11 @@
-- * @{#ZONE_BASE.FlareZone}(): Flares the zone boundaries in a color.
--
-- ===
--- @field #ZONE_BASE ZONE_BASE
+--
+-- @type ZONE_BASE
+-- @field #string ZoneName Name of the zone.
+-- @field #number ZoneProbability A value between 0 and 1. 0 = 0% and 1 = 100% probability.
+-- @extends Core.Base#BASE
ZONE_BASE = {
ClassName = "ZONE_BASE",
ZoneName = "",
@@ -306,12 +303,6 @@ function ZONE_BASE:GetZoneMaybe()
end
---- The ZONE_RADIUS class, defined by a zone name, a location and a radius.
--- @type ZONE_RADIUS
--- @field Dcs.DCSTypes#Vec2 Vec2 The current location of the zone.
--- @field Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @extends Core.Zone#ZONE_BASE
-
--- # 2) @{Zone#ZONE_RADIUS} class, extends @{Zone#ZONE_BASE}
--
-- The ZONE_RADIUS class defined by a zone name, a location and a radius.
@@ -342,8 +333,11 @@ end
--
-- ===
--
--- @field #ZONE_RADIUS ZONE_RADIUS
---
+-- The ZONE_RADIUS class, defined by a zone name, a location and a radius.
+-- @type ZONE_RADIUS
+-- @field Dcs.DCSTypes#Vec2 Vec2 The current location of the zone.
+-- @field Dcs.DCSTypes#Distance Radius The radius of the zone.
+-- @extends Core.Zone#ZONE_BASE
ZONE_RADIUS = {
ClassName="ZONE_RADIUS",
}
@@ -379,7 +373,6 @@ function ZONE_RADIUS:BoundZone( Points, CountryID, UnBound )
local Angle
local RadialBase = math.pi*2
- --
for Angle = 0, 360, (360 / Points ) do
local Radial = Angle * RadialBase / 360
Point.x = Vec2.x + math.cos( Radial ) * self:GetRadius()
diff --git a/Moose Mission Setup/Moose.lua b/Moose Mission Setup/Moose.lua
index d3784431a..d8b4f16ce 100644
--- a/Moose Mission Setup/Moose.lua
+++ b/Moose Mission Setup/Moose.lua
@@ -1,35878 +1,74 @@
-env.info( '*** MOOSE STATIC INCLUDE START *** ' )
-env.info( 'Moose Generation Timestamp: 20170402_1214' )
+env.info( '*** MOOSE DYNAMIC INCLUDE START *** ' )
+env.info( 'Moose Generation Timestamp: 20170405_0123' )
---- Various routines
--- @module routines
--- @author Flightcontrol
+local base = _G
-env.setErrorMessageBoxEnabled(false)
+__Moose = {}
---- Extract of MIST functions.
--- @author Grimes
-
-routines = {}
-
-
--- don't change these
-routines.majorVersion = 3
-routines.minorVersion = 3
-routines.build = 22
-
------------------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------
--- Utils- conversion, Lua utils, etc.
-routines.utils = {}
-
---from http://lua-users.org/wiki/CopyTable
-routines.utils.deepCopy = function(object)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for index, value in pairs(object) do
- new_table[_copy(index)] = _copy(value)
- end
- return setmetatable(new_table, getmetatable(object))
- end
- local objectreturn = _copy(object)
- return objectreturn
-end
-
-
--- porting in Slmod's serialize_slmod2
-routines.utils.oneLineSerialize = function(tbl) -- serialization of a table all on a single line, no comments, made to replace old get_table_string function
-
- lookup_table = {}
-
- local function _Serialize( tbl )
-
- if type(tbl) == 'table' then --function only works for tables!
-
- if lookup_table[tbl] then
- return lookup_table[object]
- end
-
- local tbl_str = {}
-
- lookup_table[tbl] = tbl_str
-
- tbl_str[#tbl_str + 1] = '{'
-
- for ind,val in pairs(tbl) do -- serialize its fields
- local ind_str = {}
- if type(ind) == "number" then
- ind_str[#ind_str + 1] = '['
- ind_str[#ind_str + 1] = tostring(ind)
- ind_str[#ind_str + 1] = ']='
- else --must be a string
- ind_str[#ind_str + 1] = '['
- ind_str[#ind_str + 1] = routines.utils.basicSerialize(ind)
- ind_str[#ind_str + 1] = ']='
- end
-
- local val_str = {}
- if ((type(val) == 'number') or (type(val) == 'boolean')) then
- val_str[#val_str + 1] = tostring(val)
- val_str[#val_str + 1] = ','
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'string' then
- val_str[#val_str + 1] = routines.utils.basicSerialize(val)
- val_str[#val_str + 1] = ','
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'nil' then -- won't ever happen, right?
- val_str[#val_str + 1] = 'nil,'
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'table' then
- if ind == "__index" then
- -- tbl_str[#tbl_str + 1] = "__index"
- -- tbl_str[#tbl_str + 1] = ',' --I think this is right, I just added it
- else
-
- val_str[#val_str + 1] = _Serialize(val)
- val_str[#val_str + 1] = ',' --I think this is right, I just added it
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- end
- elseif type(val) == 'function' then
- -- tbl_str[#tbl_str + 1] = "function " .. tostring(ind)
- -- tbl_str[#tbl_str + 1] = ',' --I think this is right, I just added it
- else
--- env.info('unable to serialize value type ' .. routines.utils.basicSerialize(type(val)) .. ' at index ' .. tostring(ind))
--- env.info( debug.traceback() )
- end
-
- end
- tbl_str[#tbl_str + 1] = '}'
- return table.concat(tbl_str)
+__Moose.Include = function( IncludeFile )
+ if not __Moose.Includes[ IncludeFile ] then
+ __Moose.Includes[IncludeFile] = IncludeFile
+ local f = assert( base.loadfile( __Moose.ProgramPath .. IncludeFile ) )
+ if f == nil then
+ error ("Moose: Could not load Moose file " .. IncludeFile )
else
- return tostring(tbl)
- end
- end
-
- local objectreturn = _Serialize(tbl)
- return objectreturn
-end
-
---porting in Slmod's "safestring" basic serialize
-routines.utils.basicSerialize = function(s)
- if s == nil then
- return "\"\""
- else
- if ((type(s) == 'number') or (type(s) == 'boolean') or (type(s) == 'function') or (type(s) == 'table') or (type(s) == 'userdata') ) then
- return tostring(s)
- elseif type(s) == 'string' then
- s = string.format('%q', s)
- return s
- end
- end
-end
-
-
-routines.utils.toDegree = function(angle)
- return angle*180/math.pi
-end
-
-routines.utils.toRadian = function(angle)
- return angle*math.pi/180
-end
-
-routines.utils.metersToNM = function(meters)
- return meters/1852
-end
-
-routines.utils.metersToFeet = function(meters)
- return meters/0.3048
-end
-
-routines.utils.NMToMeters = function(NM)
- return NM*1852
-end
-
-routines.utils.feetToMeters = function(feet)
- return feet*0.3048
-end
-
-routines.utils.mpsToKnots = function(mps)
- return mps*3600/1852
-end
-
-routines.utils.mpsToKmph = function(mps)
- return mps*3.6
-end
-
-routines.utils.knotsToMps = function(knots)
- return knots*1852/3600
-end
-
-routines.utils.kmphToMps = function(kmph)
- return kmph/3.6
-end
-
-function routines.utils.makeVec2(Vec3)
- if Vec3.z then
- return {x = Vec3.x, y = Vec3.z}
- else
- return {x = Vec3.x, y = Vec3.y} -- it was actually already vec2.
- end
-end
-
-function routines.utils.makeVec3(Vec2, y)
- if not Vec2.z then
- if not y then
- y = 0
- end
- return {x = Vec2.x, y = y, z = Vec2.y}
- else
- return {x = Vec2.x, y = Vec2.y, z = Vec2.z} -- it was already Vec3, actually.
- end
-end
-
-function routines.utils.makeVec3GL(Vec2, offset)
- local adj = offset or 0
-
- if not Vec2.z then
- return {x = Vec2.x, y = (land.getHeight(Vec2) + adj), z = Vec2.y}
- else
- return {x = Vec2.x, y = (land.getHeight({x = Vec2.x, y = Vec2.z}) + adj), z = Vec2.z}
- end
-end
-
-routines.utils.zoneToVec3 = function(zone)
- local new = {}
- if type(zone) == 'table' and zone.point then
- new.x = zone.point.x
- new.y = zone.point.y
- new.z = zone.point.z
- return new
- elseif type(zone) == 'string' then
- zone = trigger.misc.getZone(zone)
- if zone then
- new.x = zone.point.x
- new.y = zone.point.y
- new.z = zone.point.z
- return new
- end
- end
-end
-
--- gets heading-error corrected direction from point along vector vec.
-function routines.utils.getDir(vec, point)
- local dir = math.atan2(vec.z, vec.x)
- dir = dir + routines.getNorthCorrection(point)
- if dir < 0 then
- dir = dir + 2*math.pi -- put dir in range of 0 to 2*pi
- end
- return dir
-end
-
--- gets distance in meters between two points (2 dimensional)
-function routines.utils.get2DDist(point1, point2)
- point1 = routines.utils.makeVec3(point1)
- point2 = routines.utils.makeVec3(point2)
- return routines.vec.mag({x = point1.x - point2.x, y = 0, z = point1.z - point2.z})
-end
-
--- gets distance in meters between two points (3 dimensional)
-function routines.utils.get3DDist(point1, point2)
- return routines.vec.mag({x = point1.x - point2.x, y = point1.y - point2.y, z = point1.z - point2.z})
-end
-
-
-
-
-
---3D Vector manipulation
-routines.vec = {}
-
-routines.vec.add = function(vec1, vec2)
- return {x = vec1.x + vec2.x, y = vec1.y + vec2.y, z = vec1.z + vec2.z}
-end
-
-routines.vec.sub = function(vec1, vec2)
- return {x = vec1.x - vec2.x, y = vec1.y - vec2.y, z = vec1.z - vec2.z}
-end
-
-routines.vec.scalarMult = function(vec, mult)
- return {x = vec.x*mult, y = vec.y*mult, z = vec.z*mult}
-end
-
-routines.vec.scalar_mult = routines.vec.scalarMult
-
-routines.vec.dp = function(vec1, vec2)
- return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z
-end
-
-routines.vec.cp = function(vec1, vec2)
- return { x = vec1.y*vec2.z - vec1.z*vec2.y, y = vec1.z*vec2.x - vec1.x*vec2.z, z = vec1.x*vec2.y - vec1.y*vec2.x}
-end
-
-routines.vec.mag = function(vec)
- return (vec.x^2 + vec.y^2 + vec.z^2)^0.5
-end
-
-routines.vec.getUnitVec = function(vec)
- local mag = routines.vec.mag(vec)
- return { x = vec.x/mag, y = vec.y/mag, z = vec.z/mag }
-end
-
-routines.vec.rotateVec2 = function(vec2, theta)
- return { x = vec2.x*math.cos(theta) - vec2.y*math.sin(theta), y = vec2.x*math.sin(theta) + vec2.y*math.cos(theta)}
-end
----------------------------------------------------------------------------------------------------------------------------
-
-
-
-
--- acc- the accuracy of each easting/northing. 0, 1, 2, 3, 4, or 5.
-routines.tostringMGRS = function(MGRS, acc)
- if acc == 0 then
- return MGRS.UTMZone .. ' ' .. MGRS.MGRSDigraph
- else
- return MGRS.UTMZone .. ' ' .. MGRS.MGRSDigraph .. ' ' .. string.format('%0' .. acc .. 'd', routines.utils.round(MGRS.Easting/(10^(5-acc)), 0))
- .. ' ' .. string.format('%0' .. acc .. 'd', routines.utils.round(MGRS.Northing/(10^(5-acc)), 0))
- end
-end
-
---[[acc:
-in DM: decimal point of minutes.
-In DMS: decimal point of seconds.
-position after the decimal of the least significant digit:
-So:
-42.32 - acc of 2.
-]]
-routines.tostringLL = function(lat, lon, acc, DMS)
-
- local latHemi, lonHemi
- if lat > 0 then
- latHemi = 'N'
- else
- latHemi = 'S'
- end
-
- if lon > 0 then
- lonHemi = 'E'
- else
- lonHemi = 'W'
- end
-
- lat = math.abs(lat)
- lon = math.abs(lon)
-
- local latDeg = math.floor(lat)
- local latMin = (lat - latDeg)*60
-
- local lonDeg = math.floor(lon)
- local lonMin = (lon - lonDeg)*60
-
- if DMS then -- degrees, minutes, and seconds.
- local oldLatMin = latMin
- latMin = math.floor(latMin)
- local latSec = routines.utils.round((oldLatMin - latMin)*60, acc)
-
- local oldLonMin = lonMin
- lonMin = math.floor(lonMin)
- local lonSec = routines.utils.round((oldLonMin - lonMin)*60, acc)
-
- if latSec == 60 then
- latSec = 0
- latMin = latMin + 1
- end
-
- if lonSec == 60 then
- lonSec = 0
- lonMin = lonMin + 1
- end
-
- local secFrmtStr -- create the formatting string for the seconds place
- if acc <= 0 then -- no decimal place.
- secFrmtStr = '%02d'
- else
- local width = 3 + acc -- 01.310 - that's a width of 6, for example.
- secFrmtStr = '%0' .. width .. '.' .. acc .. 'f'
- end
-
- return string.format('%02d', latDeg) .. ' ' .. string.format('%02d', latMin) .. '\' ' .. string.format(secFrmtStr, latSec) .. '"' .. latHemi .. ' '
- .. string.format('%02d', lonDeg) .. ' ' .. string.format('%02d', lonMin) .. '\' ' .. string.format(secFrmtStr, lonSec) .. '"' .. lonHemi
-
- else -- degrees, decimal minutes.
- latMin = routines.utils.round(latMin, acc)
- lonMin = routines.utils.round(lonMin, acc)
-
- if latMin == 60 then
- latMin = 0
- latDeg = latDeg + 1
- end
-
- if lonMin == 60 then
- lonMin = 0
- lonDeg = lonDeg + 1
- end
-
- local minFrmtStr -- create the formatting string for the minutes place
- if acc <= 0 then -- no decimal place.
- minFrmtStr = '%02d'
- else
- local width = 3 + acc -- 01.310 - that's a width of 6, for example.
- minFrmtStr = '%0' .. width .. '.' .. acc .. 'f'
- end
-
- return string.format('%02d', latDeg) .. ' ' .. string.format(minFrmtStr, latMin) .. '\'' .. latHemi .. ' '
- .. string.format('%02d', lonDeg) .. ' ' .. string.format(minFrmtStr, lonMin) .. '\'' .. lonHemi
-
- end
-end
-
---[[ required: az - radian
- required: dist - meters
- optional: alt - meters (set to false or nil if you don't want to use it).
- optional: metric - set true to get dist and alt in km and m.
- precision will always be nearest degree and NM or km.]]
-routines.tostringBR = function(az, dist, alt, metric)
- az = routines.utils.round(routines.utils.toDegree(az), 0)
-
- if metric then
- dist = routines.utils.round(dist/1000, 2)
- else
- dist = routines.utils.round(routines.utils.metersToNM(dist), 2)
- end
-
- local s = string.format('%03d', az) .. ' for ' .. dist
-
- if alt then
- if metric then
- s = s .. ' at ' .. routines.utils.round(alt, 0)
- else
- s = s .. ' at ' .. routines.utils.round(routines.utils.metersToFeet(alt), 0)
- end
- end
- return s
-end
-
-routines.getNorthCorrection = function(point) --gets the correction needed for true north
- if not point.z then --Vec2; convert to Vec3
- point.z = point.y
- point.y = 0
- end
- local lat, lon = coord.LOtoLL(point)
- local north_posit = coord.LLtoLO(lat + 1, lon)
- return math.atan2(north_posit.z - point.z, north_posit.x - point.x)
-end
-
-
-do
- local idNum = 0
-
- --Simplified event handler
- routines.addEventHandler = function(f) --id is optional!
- local handler = {}
- idNum = idNum + 1
- handler.id = idNum
- handler.f = f
- handler.onEvent = function(self, event)
- self.f(event)
- end
- world.addEventHandler(handler)
- end
-
- routines.removeEventHandler = function(id)
- for key, handler in pairs(world.eventHandlers) do
- if handler.id and handler.id == id then
- world.eventHandlers[key] = nil
- return true
- end
- end
- return false
- end
-end
-
--- need to return a Vec3 or Vec2?
-function routines.getRandPointInCircle(point, radius, innerRadius)
- local theta = 2*math.pi*math.random()
- local rad = math.random() + math.random()
- if rad > 1 then
- rad = 2 - rad
- end
-
- local radMult
- if innerRadius and innerRadius <= radius then
- radMult = (radius - innerRadius)*rad + innerRadius
- else
- radMult = radius*rad
- end
-
- if not point.z then --might as well work with vec2/3
- point.z = point.y
- end
-
- local rndCoord
- if radius > 0 then
- rndCoord = {x = math.cos(theta)*radMult + point.x, y = math.sin(theta)*radMult + point.z}
- else
- rndCoord = {x = point.x, y = point.z}
- end
- return rndCoord
-end
-
-routines.goRoute = function(group, path)
- local misTask = {
- id = 'Mission',
- params = {
- route = {
- points = routines.utils.deepCopy(path),
- },
- },
- }
- if type(group) == 'string' then
- group = Group.getByName(group)
- end
- local groupCon = group:getController()
- if groupCon then
- groupCon:setTask(misTask)
- return true
- end
-
- Controller.setTask(groupCon, misTask)
- return false
-end
-
-
--- Useful atomic functions from mist, ported.
-
-routines.ground = {}
-routines.fixedWing = {}
-routines.heli = {}
-
-routines.ground.buildWP = function(point, overRideForm, overRideSpeed)
-
- local wp = {}
- wp.x = point.x
-
- if point.z then
- wp.y = point.z
- else
- wp.y = point.y
- end
- local form, speed
-
- if point.speed and not overRideSpeed then
- wp.speed = point.speed
- elseif type(overRideSpeed) == 'number' then
- wp.speed = overRideSpeed
- else
- wp.speed = routines.utils.kmphToMps(20)
- end
-
- if point.form and not overRideForm then
- form = point.form
- else
- form = overRideForm
- end
-
- if not form then
- wp.action = 'Cone'
- else
- form = string.lower(form)
- if form == 'off_road' or form == 'off road' then
- wp.action = 'Off Road'
- elseif form == 'on_road' or form == 'on road' then
- wp.action = 'On Road'
- elseif form == 'rank' or form == 'line_abrest' or form == 'line abrest' or form == 'lineabrest'then
- wp.action = 'Rank'
- elseif form == 'cone' then
- wp.action = 'Cone'
- elseif form == 'diamond' then
- wp.action = 'Diamond'
- elseif form == 'vee' then
- wp.action = 'Vee'
- elseif form == 'echelon_left' or form == 'echelon left' or form == 'echelonl' then
- wp.action = 'EchelonL'
- elseif form == 'echelon_right' or form == 'echelon right' or form == 'echelonr' then
- wp.action = 'EchelonR'
- else
- wp.action = 'Cone' -- if nothing matched
- end
- end
-
- wp.type = 'Turning Point'
-
- return wp
-
-end
-
-routines.fixedWing.buildWP = function(point, WPtype, speed, alt, altType)
-
- local wp = {}
- wp.x = point.x
-
- if point.z then
- wp.y = point.z
- else
- wp.y = point.y
- end
-
- if alt and type(alt) == 'number' then
- wp.alt = alt
- else
- wp.alt = 2000
- end
-
- if altType then
- altType = string.lower(altType)
- if altType == 'radio' or 'agl' then
- wp.alt_type = 'RADIO'
- elseif altType == 'baro' or 'asl' then
- wp.alt_type = 'BARO'
- end
- else
- wp.alt_type = 'RADIO'
- end
-
- if point.speed then
- speed = point.speed
- end
-
- if point.type then
- WPtype = point.type
- end
-
- if not speed then
- wp.speed = routines.utils.kmphToMps(500)
- else
- wp.speed = speed
- end
-
- if not WPtype then
- wp.action = 'Turning Point'
- else
- WPtype = string.lower(WPtype)
- if WPtype == 'flyover' or WPtype == 'fly over' or WPtype == 'fly_over' then
- wp.action = 'Fly Over Point'
- elseif WPtype == 'turningpoint' or WPtype == 'turning point' or WPtype == 'turning_point' then
- wp.action = 'Turning Point'
- else
- wp.action = 'Turning Point'
- end
- end
-
- wp.type = 'Turning Point'
- return wp
-end
-
-routines.heli.buildWP = function(point, WPtype, speed, alt, altType)
-
- local wp = {}
- wp.x = point.x
-
- if point.z then
- wp.y = point.z
- else
- wp.y = point.y
- end
-
- if alt and type(alt) == 'number' then
- wp.alt = alt
- else
- wp.alt = 500
- end
-
- if altType then
- altType = string.lower(altType)
- if altType == 'radio' or 'agl' then
- wp.alt_type = 'RADIO'
- elseif altType == 'baro' or 'asl' then
- wp.alt_type = 'BARO'
- end
- else
- wp.alt_type = 'RADIO'
- end
-
- if point.speed then
- speed = point.speed
- end
-
- if point.type then
- WPtype = point.type
- end
-
- if not speed then
- wp.speed = routines.utils.kmphToMps(200)
- else
- wp.speed = speed
- end
-
- if not WPtype then
- wp.action = 'Turning Point'
- else
- WPtype = string.lower(WPtype)
- if WPtype == 'flyover' or WPtype == 'fly over' or WPtype == 'fly_over' then
- wp.action = 'Fly Over Point'
- elseif WPtype == 'turningpoint' or WPtype == 'turning point' or WPtype == 'turning_point' then
- wp.action = 'Turning Point'
- else
- wp.action = 'Turning Point'
- end
- end
-
- wp.type = 'Turning Point'
- return wp
-end
-
-routines.groupToRandomPoint = function(vars)
- local group = vars.group --Required
- local point = vars.point --required
- local radius = vars.radius or 0
- local innerRadius = vars.innerRadius
- local form = vars.form or 'Cone'
- local heading = vars.heading or math.random()*2*math.pi
- local headingDegrees = vars.headingDegrees
- local speed = vars.speed or routines.utils.kmphToMps(20)
-
-
- local useRoads
- if not vars.disableRoads then
- useRoads = true
- else
- useRoads = false
- end
-
- local path = {}
-
- if headingDegrees then
- heading = headingDegrees*math.pi/180
- end
-
- if heading >= 2*math.pi then
- heading = heading - 2*math.pi
- end
-
- local rndCoord = routines.getRandPointInCircle(point, radius, innerRadius)
-
- local offset = {}
- local posStart = routines.getLeadPos(group)
-
- offset.x = routines.utils.round(math.sin(heading - (math.pi/2)) * 50 + rndCoord.x, 3)
- offset.z = routines.utils.round(math.cos(heading + (math.pi/2)) * 50 + rndCoord.y, 3)
- path[#path + 1] = routines.ground.buildWP(posStart, form, speed)
-
-
- if useRoads == true and ((point.x - posStart.x)^2 + (point.z - posStart.z)^2)^0.5 > radius * 1.3 then
- path[#path + 1] = routines.ground.buildWP({['x'] = posStart.x + 11, ['z'] = posStart.z + 11}, 'off_road', speed)
- path[#path + 1] = routines.ground.buildWP(posStart, 'on_road', speed)
- path[#path + 1] = routines.ground.buildWP(offset, 'on_road', speed)
- else
- path[#path + 1] = routines.ground.buildWP({['x'] = posStart.x + 25, ['z'] = posStart.z + 25}, form, speed)
- end
-
- path[#path + 1] = routines.ground.buildWP(offset, form, speed)
- path[#path + 1] = routines.ground.buildWP(rndCoord, form, speed)
-
- routines.goRoute(group, path)
-
- return
-end
-
-routines.groupRandomDistSelf = function(gpData, dist, form, heading, speed)
- local pos = routines.getLeadPos(gpData)
- local fakeZone = {}
- fakeZone.radius = dist or math.random(300, 1000)
- fakeZone.point = {x = pos.x, y, pos.y, z = pos.z}
- routines.groupToRandomZone(gpData, fakeZone, form, heading, speed)
-
- return
-end
-
-routines.groupToRandomZone = function(gpData, zone, form, heading, speed)
- if type(gpData) == 'string' then
- gpData = Group.getByName(gpData)
- end
-
- if type(zone) == 'string' then
- zone = trigger.misc.getZone(zone)
- elseif type(zone) == 'table' and not zone.radius then
- zone = trigger.misc.getZone(zone[math.random(1, #zone)])
- end
-
- if speed then
- speed = routines.utils.kmphToMps(speed)
- end
-
- local vars = {}
- vars.group = gpData
- vars.radius = zone.radius
- vars.form = form
- vars.headingDegrees = heading
- vars.speed = speed
- vars.point = routines.utils.zoneToVec3(zone)
-
- routines.groupToRandomPoint(vars)
-
- return
-end
-
-routines.isTerrainValid = function(coord, terrainTypes) -- vec2/3 and enum or table of acceptable terrain types
- if coord.z then
- coord.y = coord.z
- end
- local typeConverted = {}
-
- if type(terrainTypes) == 'string' then -- if its a string it does this check
- for constId, constData in pairs(land.SurfaceType) do
- if string.lower(constId) == string.lower(terrainTypes) or string.lower(constData) == string.lower(terrainTypes) then
- table.insert(typeConverted, constId)
- end
- end
- elseif type(terrainTypes) == 'table' then -- if its a table it does this check
- for typeId, typeData in pairs(terrainTypes) do
- for constId, constData in pairs(land.SurfaceType) do
- if string.lower(constId) == string.lower(typeData) or string.lower(constData) == string.lower(typeId) then
- table.insert(typeConverted, constId)
- end
- end
- end
- end
- for validIndex, validData in pairs(typeConverted) do
- if land.getSurfaceType(coord) == land.SurfaceType[validData] then
- return true
- end
- end
- return false
-end
-
-routines.groupToPoint = function(gpData, point, form, heading, speed, useRoads)
- if type(point) == 'string' then
- point = trigger.misc.getZone(point)
- end
- if speed then
- speed = routines.utils.kmphToMps(speed)
- end
-
- local vars = {}
- vars.group = gpData
- vars.form = form
- vars.headingDegrees = heading
- vars.speed = speed
- vars.disableRoads = useRoads
- vars.point = routines.utils.zoneToVec3(point)
- routines.groupToRandomPoint(vars)
-
- return
-end
-
-
-routines.getLeadPos = function(group)
- if type(group) == 'string' then -- group name
- group = Group.getByName(group)
- end
-
- local units = group:getUnits()
-
- local leader = units[1]
- if not leader then -- SHOULD be good, but if there is a bug, this code future-proofs it then.
- local lowestInd = math.huge
- for ind, unit in pairs(units) do
- if ind < lowestInd then
- lowestInd = ind
- leader = unit
- end
- end
- end
- if leader and Unit.isExist(leader) then -- maybe a little too paranoid now...
- return leader:getPosition().p
- end
-end
-
---[[ vars for routines.getMGRSString:
-vars.units - table of unit names (NOT unitNameTable- maybe this should change).
-vars.acc - integer between 0 and 5, inclusive
-]]
-routines.getMGRSString = function(vars)
- local units = vars.units
- local acc = vars.acc or 5
- local avgPos = routines.getAvgPos(units)
- if avgPos then
- return routines.tostringMGRS(coord.LLtoMGRS(coord.LOtoLL(avgPos)), acc)
- end
-end
-
---[[ vars for routines.getLLString
-vars.units - table of unit names (NOT unitNameTable- maybe this should change).
-vars.acc - integer, number of numbers after decimal place
-vars.DMS - if true, output in degrees, minutes, seconds. Otherwise, output in degrees, minutes.
-
-
-]]
-routines.getLLString = function(vars)
- local units = vars.units
- local acc = vars.acc or 3
- local DMS = vars.DMS
- local avgPos = routines.getAvgPos(units)
- if avgPos then
- local lat, lon = coord.LOtoLL(avgPos)
- return routines.tostringLL(lat, lon, acc, DMS)
- end
-end
-
---[[
-vars.zone - table of a zone name.
-vars.ref - vec3 ref point, maybe overload for vec2 as well?
-vars.alt - boolean, if used, includes altitude in string
-vars.metric - boolean, gives distance in km instead of NM.
-]]
-routines.getBRStringZone = function(vars)
- local zone = trigger.misc.getZone( vars.zone )
- local ref = routines.utils.makeVec3(vars.ref, 0) -- turn it into Vec3 if it is not already.
- local alt = vars.alt
- local metric = vars.metric
- if zone then
- local vec = {x = zone.point.x - ref.x, y = zone.point.y - ref.y, z = zone.point.z - ref.z}
- local dir = routines.utils.getDir(vec, ref)
- local dist = routines.utils.get2DDist(zone.point, ref)
- if alt then
- alt = zone.y
- end
- return routines.tostringBR(dir, dist, alt, metric)
- else
- env.info( 'routines.getBRStringZone: error: zone is nil' )
- end
-end
-
---[[
-vars.units- table of unit names (NOT unitNameTable- maybe this should change).
-vars.ref - vec3 ref point, maybe overload for vec2 as well?
-vars.alt - boolean, if used, includes altitude in string
-vars.metric - boolean, gives distance in km instead of NM.
-]]
-routines.getBRString = function(vars)
- local units = vars.units
- local ref = routines.utils.makeVec3(vars.ref, 0) -- turn it into Vec3 if it is not already.
- local alt = vars.alt
- local metric = vars.metric
- local avgPos = routines.getAvgPos(units)
- if avgPos then
- local vec = {x = avgPos.x - ref.x, y = avgPos.y - ref.y, z = avgPos.z - ref.z}
- local dir = routines.utils.getDir(vec, ref)
- local dist = routines.utils.get2DDist(avgPos, ref)
- if alt then
- alt = avgPos.y
- end
- return routines.tostringBR(dir, dist, alt, metric)
- end
-end
-
-
--- Returns the Vec3 coordinates of the average position of the concentration of units most in the heading direction.
---[[ vars for routines.getLeadingPos:
-vars.units - table of unit names
-vars.heading - direction
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees
-]]
-routines.getLeadingPos = function(vars)
- local units = vars.units
- local heading = vars.heading
- local radius = vars.radius
- if vars.headingDegrees then
- heading = routines.utils.toRadian(vars.headingDegrees)
- end
-
- local unitPosTbl = {}
- for i = 1, #units do
- local unit = Unit.getByName(units[i])
- if unit and unit:isExist() then
- unitPosTbl[#unitPosTbl + 1] = unit:getPosition().p
- end
- end
- if #unitPosTbl > 0 then -- one more more units found.
- -- first, find the unit most in the heading direction
- local maxPos = -math.huge
-
- local maxPosInd -- maxPos - the furthest in direction defined by heading; maxPosInd =
- for i = 1, #unitPosTbl do
- local rotatedVec2 = routines.vec.rotateVec2(routines.utils.makeVec2(unitPosTbl[i]), heading)
- if (not maxPos) or maxPos < rotatedVec2.x then
- maxPos = rotatedVec2.x
- maxPosInd = i
- end
- end
-
- --now, get all the units around this unit...
- local avgPos
- if radius then
- local maxUnitPos = unitPosTbl[maxPosInd]
- local avgx, avgy, avgz, totNum = 0, 0, 0, 0
- for i = 1, #unitPosTbl do
- if routines.utils.get2DDist(maxUnitPos, unitPosTbl[i]) <= radius then
- avgx = avgx + unitPosTbl[i].x
- avgy = avgy + unitPosTbl[i].y
- avgz = avgz + unitPosTbl[i].z
- totNum = totNum + 1
- end
- end
- avgPos = { x = avgx/totNum, y = avgy/totNum, z = avgz/totNum}
- else
- avgPos = unitPosTbl[maxPosInd]
- end
-
- return avgPos
- end
-end
-
-
---[[ vars for routines.getLeadingMGRSString:
-vars.units - table of unit names
-vars.heading - direction
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees
-vars.acc - number, 0 to 5.
-]]
-routines.getLeadingMGRSString = function(vars)
- local pos = routines.getLeadingPos(vars)
- if pos then
- local acc = vars.acc or 5
- return routines.tostringMGRS(coord.LLtoMGRS(coord.LOtoLL(pos)), acc)
- end
-end
-
---[[ vars for routines.getLeadingLLString:
-vars.units - table of unit names
-vars.heading - direction, number
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees
-vars.acc - number of digits after decimal point (can be negative)
-vars.DMS - boolean, true if you want DMS.
-]]
-routines.getLeadingLLString = function(vars)
- local pos = routines.getLeadingPos(vars)
- if pos then
- local acc = vars.acc or 3
- local DMS = vars.DMS
- local lat, lon = coord.LOtoLL(pos)
- return routines.tostringLL(lat, lon, acc, DMS)
- end
-end
-
-
-
---[[ vars for routines.getLeadingBRString:
-vars.units - table of unit names
-vars.heading - direction, number
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees
-vars.metric - boolean, if true, use km instead of NM.
-vars.alt - boolean, if true, include altitude.
-vars.ref - vec3/vec2 reference point.
-]]
-routines.getLeadingBRString = function(vars)
- local pos = routines.getLeadingPos(vars)
- if pos then
- local ref = vars.ref
- local alt = vars.alt
- local metric = vars.metric
-
- local vec = {x = pos.x - ref.x, y = pos.y - ref.y, z = pos.z - ref.z}
- local dir = routines.utils.getDir(vec, ref)
- local dist = routines.utils.get2DDist(pos, ref)
- if alt then
- alt = pos.y
- end
- return routines.tostringBR(dir, dist, alt, metric)
- end
-end
-
---[[ vars for routines.message.add
- vars.text = 'Hello World'
- vars.displayTime = 20
- vars.msgFor = {coa = {'red'}, countries = {'Ukraine', 'Georgia'}, unitTypes = {'A-10C'}}
-
-]]
-
---[[ vars for routines.msgMGRS
-vars.units - table of unit names (NOT unitNameTable- maybe this should change).
-vars.acc - integer between 0 and 5, inclusive
-vars.text - text in the message
-vars.displayTime - self explanatory
-vars.msgFor - scope
-]]
-routines.msgMGRS = function(vars)
- local units = vars.units
- local acc = vars.acc
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getMGRSString{units = units, acc = acc}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-end
-
---[[ vars for routines.msgLL
-vars.units - table of unit names (NOT unitNameTable- maybe this should change) (Yes).
-vars.acc - integer, number of numbers after decimal place
-vars.DMS - if true, output in degrees, minutes, seconds. Otherwise, output in degrees, minutes.
-vars.text - text in the message
-vars.displayTime - self explanatory
-vars.msgFor - scope
-]]
-routines.msgLL = function(vars)
- local units = vars.units -- technically, I don't really need to do this, but it helps readability.
- local acc = vars.acc
- local DMS = vars.DMS
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getLLString{units = units, acc = acc, DMS = DMS}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-
-end
-
-
---[[
-vars.units- table of unit names (NOT unitNameTable- maybe this should change).
-vars.ref - vec3 ref point, maybe overload for vec2 as well?
-vars.alt - boolean, if used, includes altitude in string
-vars.metric - boolean, gives distance in km instead of NM.
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-routines.msgBR = function(vars)
- local units = vars.units -- technically, I don't really need to do this, but it helps readability.
- local ref = vars.ref -- vec2/vec3 will be handled in routines.getBRString
- local alt = vars.alt
- local metric = vars.metric
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getBRString{units = units, ref = ref, alt = alt, metric = metric}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-
-end
-
-
---------------------------------------------------------------------------------------------
--- basically, just sub-types of routines.msgBR... saves folks the work of getting the ref point.
---[[
-vars.units- table of unit names (NOT unitNameTable- maybe this should change).
-vars.ref - string red, blue
-vars.alt - boolean, if used, includes altitude in string
-vars.metric - boolean, gives distance in km instead of NM.
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-routines.msgBullseye = function(vars)
- if string.lower(vars.ref) == 'red' then
- vars.ref = routines.DBs.missionData.bullseye.red
- routines.msgBR(vars)
- elseif string.lower(vars.ref) == 'blue' then
- vars.ref = routines.DBs.missionData.bullseye.blue
- routines.msgBR(vars)
- end
-end
-
---[[
-vars.units- table of unit names (NOT unitNameTable- maybe this should change).
-vars.ref - unit name of reference point
-vars.alt - boolean, if used, includes altitude in string
-vars.metric - boolean, gives distance in km instead of NM.
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-
-routines.msgBRA = function(vars)
- if Unit.getByName(vars.ref) then
- vars.ref = Unit.getByName(vars.ref):getPosition().p
- if not vars.alt then
- vars.alt = true
- end
- routines.msgBR(vars)
- end
-end
---------------------------------------------------------------------------------------------
-
---[[ vars for routines.msgLeadingMGRS:
-vars.units - table of unit names
-vars.heading - direction
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees (optional)
-vars.acc - number, 0 to 5.
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-routines.msgLeadingMGRS = function(vars)
- local units = vars.units -- technically, I don't really need to do this, but it helps readability.
- local heading = vars.heading
- local radius = vars.radius
- local headingDegrees = vars.headingDegrees
- local acc = vars.acc
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getLeadingMGRSString{units = units, heading = heading, radius = radius, headingDegrees = headingDegrees, acc = acc}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-
-
-end
---[[ vars for routines.msgLeadingLL:
-vars.units - table of unit names
-vars.heading - direction, number
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees (optional)
-vars.acc - number of digits after decimal point (can be negative)
-vars.DMS - boolean, true if you want DMS. (optional)
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-routines.msgLeadingLL = function(vars)
- local units = vars.units -- technically, I don't really need to do this, but it helps readability.
- local heading = vars.heading
- local radius = vars.radius
- local headingDegrees = vars.headingDegrees
- local acc = vars.acc
- local DMS = vars.DMS
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getLeadingLLString{units = units, heading = heading, radius = radius, headingDegrees = headingDegrees, acc = acc, DMS = DMS}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-
-end
-
---[[
-vars.units - table of unit names
-vars.heading - direction, number
-vars.radius - number
-vars.headingDegrees - boolean, switches heading to degrees (optional)
-vars.metric - boolean, if true, use km instead of NM. (optional)
-vars.alt - boolean, if true, include altitude. (optional)
-vars.ref - vec3/vec2 reference point.
-vars.text - text of the message
-vars.displayTime
-vars.msgFor - scope
-]]
-routines.msgLeadingBR = function(vars)
- local units = vars.units -- technically, I don't really need to do this, but it helps readability.
- local heading = vars.heading
- local radius = vars.radius
- local headingDegrees = vars.headingDegrees
- local metric = vars.metric
- local alt = vars.alt
- local ref = vars.ref -- vec2/vec3 will be handled in routines.getBRString
- local text = vars.text
- local displayTime = vars.displayTime
- local msgFor = vars.msgFor
-
- local s = routines.getLeadingBRString{units = units, heading = heading, radius = radius, headingDegrees = headingDegrees, metric = metric, alt = alt, ref = ref}
- local newText
- if string.find(text, '%%s') then -- look for %s
- newText = string.format(text, s) -- insert the coordinates into the message
- else -- else, just append to the end.
- newText = text .. s
- end
-
- routines.message.add{
- text = newText,
- displayTime = displayTime,
- msgFor = msgFor
- }
-end
-
-
-function spairs(t, order)
- -- collect the keys
- local keys = {}
- for k in pairs(t) do keys[#keys+1] = k end
-
- -- if order function given, sort by it by passing the table and keys a, b,
- -- otherwise just sort the keys
- if order then
- table.sort(keys, function(a,b) return order(t, a, b) end)
- else
- table.sort(keys)
- end
-
- -- return the iterator function
- local i = 0
- return function()
- i = i + 1
- if keys[i] then
- return keys[i], t[keys[i]]
- end
- end
-end
-
-
-function routines.IsPartOfGroupInZones( CargoGroup, LandingZones )
---trace.f()
-
- local CurrentZoneID = nil
-
- if CargoGroup then
- local CargoUnits = CargoGroup:getUnits()
- for CargoUnitID, CargoUnit in pairs( CargoUnits ) do
- if CargoUnit and CargoUnit:getLife() >= 1.0 then
- CurrentZoneID = routines.IsUnitInZones( CargoUnit, LandingZones )
- if CurrentZoneID then
- break
- end
- end
- end
- end
-
---trace.r( "", "", { CurrentZoneID } )
- return CurrentZoneID
-end
-
-
-
-function routines.IsUnitInZones( TransportUnit, LandingZones )
---trace.f("", "routines.IsUnitInZones" )
-
- local TransportZoneResult = nil
- local TransportZonePos = nil
- local TransportZone = nil
-
- -- fill-up some local variables to support further calculations to determine location of units within the zone.
- if TransportUnit then
- local TransportUnitPos = TransportUnit:getPosition().p
- if type( LandingZones ) == "table" then
- for LandingZoneID, LandingZoneName in pairs( LandingZones ) do
- TransportZone = trigger.misc.getZone( LandingZoneName )
- if TransportZone then
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportUnitPos.x - TransportZonePos.x)^2 + (TransportUnitPos.z - TransportZonePos.z)^2)^0.5 <= TransportZonePos.radius) then
- TransportZoneResult = LandingZoneID
- break
- end
- end
- end
- else
- TransportZone = trigger.misc.getZone( LandingZones )
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportUnitPos.x - TransportZonePos.x)^2 + (TransportUnitPos.z - TransportZonePos.z)^2)^0.5 <= TransportZonePos.radius) then
- TransportZoneResult = 1
- end
- end
- if TransportZoneResult then
- --trace.i( "routines", "TransportZone:" .. TransportZoneResult )
- else
- --trace.i( "routines", "TransportZone:nil logic" )
- end
- return TransportZoneResult
- else
- --trace.i( "routines", "TransportZone:nil hard" )
- return nil
- end
-end
-
-function routines.IsUnitNearZonesRadius( TransportUnit, LandingZones, ZoneRadius )
---trace.f("", "routines.IsUnitInZones" )
-
- local TransportZoneResult = nil
- local TransportZonePos = nil
- local TransportZone = nil
-
- -- fill-up some local variables to support further calculations to determine location of units within the zone.
- if TransportUnit then
- local TransportUnitPos = TransportUnit:getPosition().p
- if type( LandingZones ) == "table" then
- for LandingZoneID, LandingZoneName in pairs( LandingZones ) do
- TransportZone = trigger.misc.getZone( LandingZoneName )
- if TransportZone then
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportUnitPos.x - TransportZonePos.x)^2 + (TransportUnitPos.z - TransportZonePos.z)^2)^0.5 <= ZoneRadius ) then
- TransportZoneResult = LandingZoneID
- break
- end
- end
- end
- else
- TransportZone = trigger.misc.getZone( LandingZones )
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportUnitPos.x - TransportZonePos.x)^2 + (TransportUnitPos.z - TransportZonePos.z)^2)^0.5 <= ZoneRadius ) then
- TransportZoneResult = 1
- end
- end
- if TransportZoneResult then
- --trace.i( "routines", "TransportZone:" .. TransportZoneResult )
- else
- --trace.i( "routines", "TransportZone:nil logic" )
- end
- return TransportZoneResult
- else
- --trace.i( "routines", "TransportZone:nil hard" )
- return nil
- end
-end
-
-
-function routines.IsStaticInZones( TransportStatic, LandingZones )
---trace.f()
-
- local TransportZoneResult = nil
- local TransportZonePos = nil
- local TransportZone = nil
-
- -- fill-up some local variables to support further calculations to determine location of units within the zone.
- local TransportStaticPos = TransportStatic:getPosition().p
- if type( LandingZones ) == "table" then
- for LandingZoneID, LandingZoneName in pairs( LandingZones ) do
- TransportZone = trigger.misc.getZone( LandingZoneName )
- if TransportZone then
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportStaticPos.x - TransportZonePos.x)^2 + (TransportStaticPos.z - TransportZonePos.z)^2)^0.5 <= TransportZonePos.radius) then
- TransportZoneResult = LandingZoneID
- break
- end
- end
- end
- else
- TransportZone = trigger.misc.getZone( LandingZones )
- TransportZonePos = {radius = TransportZone.radius, x = TransportZone.point.x, y = TransportZone.point.y, z = TransportZone.point.z}
- if ((( TransportStaticPos.x - TransportZonePos.x)^2 + (TransportStaticPos.z - TransportZonePos.z)^2)^0.5 <= TransportZonePos.radius) then
- TransportZoneResult = 1
- end
- end
-
---trace.r( "", "", { TransportZoneResult } )
- return TransportZoneResult
-end
-
-
-function routines.IsUnitInRadius( CargoUnit, ReferencePosition, Radius )
---trace.f()
-
- local Valid = true
-
- -- fill-up some local variables to support further calculations to determine location of units within the zone.
- local CargoPos = CargoUnit:getPosition().p
- local ReferenceP = ReferencePosition.p
-
- if (((CargoPos.x - ReferenceP.x)^2 + (CargoPos.z - ReferenceP.z)^2)^0.5 <= Radius) then
- else
- Valid = false
- end
-
- return Valid
-end
-
-function routines.IsPartOfGroupInRadius( CargoGroup, ReferencePosition, Radius )
---trace.f()
-
- local Valid = true
-
- Valid = routines.ValidateGroup( CargoGroup, "CargoGroup", Valid )
-
- -- fill-up some local variables to support further calculations to determine location of units within the zone
- local CargoUnits = CargoGroup:getUnits()
- for CargoUnitId, CargoUnit in pairs( CargoUnits ) do
- local CargoUnitPos = CargoUnit:getPosition().p
--- env.info( 'routines.IsPartOfGroupInRadius: CargoUnitPos.x = ' .. CargoUnitPos.x .. ' CargoUnitPos.z = ' .. CargoUnitPos.z )
- local ReferenceP = ReferencePosition.p
--- env.info( 'routines.IsPartOfGroupInRadius: ReferenceGroupPos.x = ' .. ReferenceGroupPos.x .. ' ReferenceGroupPos.z = ' .. ReferenceGroupPos.z )
-
- if ((( CargoUnitPos.x - ReferenceP.x)^2 + (CargoUnitPos.z - ReferenceP.z)^2)^0.5 <= Radius) then
- else
- Valid = false
- break
- end
- end
-
- return Valid
-end
-
-
-function routines.ValidateString( Variable, VariableName, Valid )
---trace.f()
-
- if type( Variable ) == "string" then
- if Variable == "" then
- error( "routines.ValidateString: error: " .. VariableName .. " must be filled out!" )
- Valid = false
- end
- else
- error( "routines.ValidateString: error: " .. VariableName .. " is not a string." )
- Valid = false
- end
-
---trace.r( "", "", { Valid } )
- return Valid
-end
-
-function routines.ValidateNumber( Variable, VariableName, Valid )
---trace.f()
-
- if type( Variable ) == "number" then
- else
- error( "routines.ValidateNumber: error: " .. VariableName .. " is not a number." )
- Valid = false
- end
-
---trace.r( "", "", { Valid } )
- return Valid
-
-end
-
-function routines.ValidateGroup( Variable, VariableName, Valid )
---trace.f()
-
- if Variable == nil then
- error( "routines.ValidateGroup: error: " .. VariableName .. " is a nil value!" )
- Valid = false
- end
-
---trace.r( "", "", { Valid } )
- return Valid
-end
-
-function routines.ValidateZone( LandingZones, VariableName, Valid )
---trace.f()
-
- if LandingZones == nil then
- error( "routines.ValidateGroup: error: " .. VariableName .. " is a nil value!" )
- Valid = false
- end
-
- if type( LandingZones ) == "table" then
- for LandingZoneID, LandingZoneName in pairs( LandingZones ) do
- if trigger.misc.getZone( LandingZoneName ) == nil then
- error( "routines.ValidateGroup: error: Zone " .. LandingZoneName .. " does not exist!" )
- Valid = false
- break
- end
- end
- else
- if trigger.misc.getZone( LandingZones ) == nil then
- error( "routines.ValidateGroup: error: Zone " .. LandingZones .. " does not exist!" )
- Valid = false
- end
- end
-
---trace.r( "", "", { Valid } )
- return Valid
-end
-
-function routines.ValidateEnumeration( Variable, VariableName, Enum, Valid )
---trace.f()
-
- local ValidVariable = false
-
- for EnumId, EnumData in pairs( Enum ) do
- if Variable == EnumData then
- ValidVariable = true
- break
- end
- end
-
- if ValidVariable then
- else
- error( 'TransportValidateEnum: " .. VariableName .. " is not a valid type.' .. Variable )
- Valid = false
- end
-
---trace.r( "", "", { Valid } )
- return Valid
-end
-
-function routines.getGroupRoute(groupIdent, task) -- same as getGroupPoints but returns speed and formation type along with vec2 of point}
- -- refactor to search by groupId and allow groupId and groupName as inputs
- local gpId = groupIdent
- if type(groupIdent) == 'string' and not tonumber(groupIdent) then
- gpId = _DATABASE.Templates.Groups[groupIdent].groupId
- end
-
- for coa_name, coa_data in pairs(env.mission.coalition) do
- if (coa_name == 'red' or coa_name == 'blue') and type(coa_data) == 'table' then
- if coa_data.country then --there is a country table
- for cntry_id, cntry_data in pairs(coa_data.country) do
- for obj_type_name, obj_type_data in pairs(cntry_data) do
- if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" then -- only these types have points
- if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then --there's a group!
- for group_num, group_data in pairs(obj_type_data.group) do
- if group_data and group_data.groupId == gpId then -- this is the group we are looking for
- if group_data.route and group_data.route.points and #group_data.route.points > 0 then
- local points = {}
-
- for point_num, point in pairs(group_data.route.points) do
- local routeData = {}
- if not point.point then
- routeData.x = point.x
- routeData.y = point.y
- else
- routeData.point = point.point --it's possible that the ME could move to the point = Vec2 notation.
- end
- routeData.form = point.action
- routeData.speed = point.speed
- routeData.alt = point.alt
- routeData.alt_type = point.alt_type
- routeData.airdromeId = point.airdromeId
- routeData.helipadId = point.helipadId
- routeData.type = point.type
- routeData.action = point.action
- if task then
- routeData.task = point.task
- end
- points[point_num] = routeData
- end
-
- return points
- end
- return
- end --if group_data and group_data.name and group_data.name == 'groupname'
- end --for group_num, group_data in pairs(obj_type_data.group) do
- end --if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then
- end --if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" or obj_type_name == "static" then
- end --for obj_type_name, obj_type_data in pairs(cntry_data) do
- end --for cntry_id, cntry_data in pairs(coa_data.country) do
- end --if coa_data.country then --there is a country table
- end --if coa_name == 'red' or coa_name == 'blue' and type(coa_data) == 'table' then
- end --for coa_name, coa_data in pairs(mission.coalition) do
-end
-
-routines.ground.patrolRoute = function(vars)
-
-
- local tempRoute = {}
- local useRoute = {}
- local gpData = vars.gpData
- if type(gpData) == 'string' then
- gpData = Group.getByName(gpData)
- end
-
- local useGroupRoute
- if not vars.useGroupRoute then
- useGroupRoute = vars.gpData
- else
- useGroupRoute = vars.useGroupRoute
- end
- local routeProvided = false
- if not vars.route then
- if useGroupRoute then
- tempRoute = routines.getGroupRoute(useGroupRoute)
- end
- else
- useRoute = vars.route
- local posStart = routines.getLeadPos(gpData)
- useRoute[1] = routines.ground.buildWP(posStart, useRoute[1].action, useRoute[1].speed)
- routeProvided = true
- end
-
-
- local overRideSpeed = vars.speed or 'default'
- local pType = vars.pType
- local offRoadForm = vars.offRoadForm or 'default'
- local onRoadForm = vars.onRoadForm or 'default'
-
- if routeProvided == false and #tempRoute > 0 then
- local posStart = routines.getLeadPos(gpData)
-
-
- useRoute[#useRoute + 1] = routines.ground.buildWP(posStart, offRoadForm, overRideSpeed)
- for i = 1, #tempRoute do
- local tempForm = tempRoute[i].action
- local tempSpeed = tempRoute[i].speed
-
- if offRoadForm == 'default' then
- tempForm = tempRoute[i].action
- end
- if onRoadForm == 'default' then
- onRoadForm = 'On Road'
- end
- if (string.lower(tempRoute[i].action) == 'on road' or string.lower(tempRoute[i].action) == 'onroad' or string.lower(tempRoute[i].action) == 'on_road') then
- tempForm = onRoadForm
- else
- tempForm = offRoadForm
- end
-
- if type(overRideSpeed) == 'number' then
- tempSpeed = overRideSpeed
- end
-
-
- useRoute[#useRoute + 1] = routines.ground.buildWP(tempRoute[i], tempForm, tempSpeed)
- end
-
- if pType and string.lower(pType) == 'doubleback' then
- local curRoute = routines.utils.deepCopy(useRoute)
- for i = #curRoute, 2, -1 do
- useRoute[#useRoute + 1] = routines.ground.buildWP(curRoute[i], curRoute[i].action, curRoute[i].speed)
- end
- end
-
- useRoute[1].action = useRoute[#useRoute].action -- make it so the first WP matches the last WP
- end
-
- local cTask3 = {}
- local newPatrol = {}
- newPatrol.route = useRoute
- newPatrol.gpData = gpData:getName()
- cTask3[#cTask3 + 1] = 'routines.ground.patrolRoute('
- cTask3[#cTask3 + 1] = routines.utils.oneLineSerialize(newPatrol)
- cTask3[#cTask3 + 1] = ')'
- cTask3 = table.concat(cTask3)
- local tempTask = {
- id = 'WrappedAction',
- params = {
- action = {
- id = 'Script',
- params = {
- command = cTask3,
-
- },
- },
- },
- }
-
-
- useRoute[#useRoute].task = tempTask
- routines.goRoute(gpData, useRoute)
-
- return
-end
-
-routines.ground.patrol = function(gpData, pType, form, speed)
- local vars = {}
-
- if type(gpData) == 'table' and gpData:getName() then
- gpData = gpData:getName()
- end
-
- vars.useGroupRoute = gpData
- vars.gpData = gpData
- vars.pType = pType
- vars.offRoadForm = form
- vars.speed = speed
-
- routines.ground.patrolRoute(vars)
-
- return
-end
-
-function routines.GetUnitHeight( CheckUnit )
---trace.f( "routines" )
-
- local UnitPoint = CheckUnit:getPoint()
- local UnitPosition = { x = UnitPoint.x, y = UnitPoint.z }
- local UnitHeight = UnitPoint.y
-
- local LandHeight = land.getHeight( UnitPosition )
-
- --env.info(( 'CarrierHeight: LandHeight = ' .. LandHeight .. ' CarrierHeight = ' .. CarrierHeight ))
-
- --trace.f( "routines", "Unit Height = " .. UnitHeight - LandHeight )
-
- return UnitHeight - LandHeight
-
-end
-
-
-
-Su34Status = { status = {} }
-boardMsgRed = { statusMsg = "" }
-boardMsgAll = { timeMsg = "" }
-SpawnSettings = {}
-Su34MenuPath = {}
-Su34Menus = 0
-
-
-function Su34AttackCarlVinson(groupName)
---trace.menu("", "Su34AttackCarlVinson")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34.getController(groupSu34)
- local groupCarlVinson = Group.getByName("US Carl Vinson #001")
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.OPEN_FIRE )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.EVADE_FIRE )
- if groupCarlVinson ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupCarlVinson:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = true}})
- end
- Su34Status.status[groupName] = 1
- MessageToRed( string.format('%s: ',groupName) .. 'Attacking carrier Carl Vinson. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34AttackWest(groupName)
---trace.f("","Su34AttackWest")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34.getController(groupSu34)
- local groupShipWest1 = Group.getByName("US Ship West #001")
- local groupShipWest2 = Group.getByName("US Ship West #002")
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.OPEN_FIRE )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.EVADE_FIRE )
- if groupShipWest1 ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupShipWest1:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = true}})
- end
- if groupShipWest2 ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupShipWest2:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = true}})
- end
- Su34Status.status[groupName] = 2
- MessageToRed( string.format('%s: ',groupName) .. 'Attacking invading ships in the west. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34AttackNorth(groupName)
---trace.menu("","Su34AttackNorth")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34.getController(groupSu34)
- local groupShipNorth1 = Group.getByName("US Ship North #001")
- local groupShipNorth2 = Group.getByName("US Ship North #002")
- local groupShipNorth3 = Group.getByName("US Ship North #003")
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.OPEN_FIRE )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.EVADE_FIRE )
- if groupShipNorth1 ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupShipNorth1:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = false}})
- end
- if groupShipNorth2 ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupShipNorth2:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = false}})
- end
- if groupShipNorth3 ~= nil then
- controllerSu34.pushTask(controllerSu34,{id = 'AttackGroup', params = { groupId = groupShipNorth3:getID(), expend = AI.Task.WeaponExpend.ALL, attackQtyLimit = false}})
- end
- Su34Status.status[groupName] = 3
- MessageToRed( string.format('%s: ',groupName) .. 'Attacking invading ships in the north. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34Orbit(groupName)
---trace.menu("","Su34Orbit")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34:getController()
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.WEAPON_HOLD )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.EVADE_FIRE )
- controllerSu34:pushTask( {id = 'ControlledTask', params = { task = { id = 'Orbit', params = { pattern = AI.Task.OrbitPattern.RACE_TRACK } }, stopCondition = { duration = 600 } } } )
- Su34Status.status[groupName] = 4
- MessageToRed( string.format('%s: ',groupName) .. 'In orbit and awaiting further instructions. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34TakeOff(groupName)
---trace.menu("","Su34TakeOff")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34:getController()
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.WEAPON_HOLD )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.BYPASS_AND_ESCAPE )
- Su34Status.status[groupName] = 8
- MessageToRed( string.format('%s: ',groupName) .. 'Take-Off. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34Hold(groupName)
---trace.menu("","Su34Hold")
- local groupSu34 = Group.getByName( groupName )
- local controllerSu34 = groupSu34:getController()
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.WEAPON_HOLD )
- controllerSu34.setOption( controllerSu34, AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.BYPASS_AND_ESCAPE )
- Su34Status.status[groupName] = 5
- MessageToRed( string.format('%s: ',groupName) .. 'Holding Weapons. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34RTB(groupName)
---trace.menu("","Su34RTB")
- Su34Status.status[groupName] = 6
- MessageToRed( string.format('%s: ',groupName) .. 'Return to Krasnodar. ', 10, 'RedStatus' .. groupName )
-end
-
-function Su34Destroyed(groupName)
---trace.menu("","Su34Destroyed")
- Su34Status.status[groupName] = 7
- MessageToRed( string.format('%s: ',groupName) .. 'Destroyed. ', 30, 'RedStatus' .. groupName )
-end
-
-function GroupAlive( groupName )
---trace.menu("","GroupAlive")
- local groupTest = Group.getByName( groupName )
-
- local groupExists = false
-
- if groupTest then
- groupExists = groupTest:isExist()
- end
-
- --trace.r( "", "", { groupExists } )
- return groupExists
-end
-
-function Su34IsDead()
---trace.f()
-
-end
-
-function Su34OverviewStatus()
---trace.menu("","Su34OverviewStatus")
- local msg = ""
- local currentStatus = 0
- local Exists = false
-
- for groupName, currentStatus in pairs(Su34Status.status) do
-
- env.info(('Su34 Overview Status: GroupName = ' .. groupName ))
- Alive = GroupAlive( groupName )
-
- if Alive then
- if currentStatus == 1 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Attacking carrier Carl Vinson. "
- elseif currentStatus == 2 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Attacking supporting ships in the west. "
- elseif currentStatus == 3 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Attacking invading ships in the north. "
- elseif currentStatus == 4 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "In orbit and awaiting further instructions. "
- elseif currentStatus == 5 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Holding Weapons. "
- elseif currentStatus == 6 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Return to Krasnodar. "
- elseif currentStatus == 7 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Destroyed. "
- elseif currentStatus == 8 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Take-Off. "
- end
- else
- if currentStatus == 7 then
- msg = msg .. string.format("%s: ",groupName)
- msg = msg .. "Destroyed. "
- else
- Su34Destroyed(groupName)
- end
- end
- end
-
- boardMsgRed.statusMsg = msg
-end
-
-
-function UpdateBoardMsg()
---trace.f()
- Su34OverviewStatus()
- MessageToRed( boardMsgRed.statusMsg, 15, 'RedStatus' )
-end
-
-function MusicReset( flg )
---trace.f()
- trigger.action.setUserFlag(95,flg)
-end
-
-function PlaneActivate(groupNameFormat, flg)
---trace.f()
- local groupName = groupNameFormat .. string.format("#%03d", trigger.misc.getUserFlag(flg))
- --trigger.action.outText(groupName,10)
- trigger.action.activateGroup(Group.getByName(groupName))
-end
-
-function Su34Menu(groupName)
---trace.f()
-
- --env.info(( 'Su34Menu(' .. groupName .. ')' ))
- local groupSu34 = Group.getByName( groupName )
-
- if Su34Status.status[groupName] == 1 or
- Su34Status.status[groupName] == 2 or
- Su34Status.status[groupName] == 3 or
- Su34Status.status[groupName] == 4 or
- Su34Status.status[groupName] == 5 then
- if Su34MenuPath[groupName] == nil then
- if planeMenuPath == nil then
- planeMenuPath = missionCommands.addSubMenuForCoalition(
- coalition.side.RED,
- "SU-34 anti-ship flights",
- nil
- )
- end
- Su34MenuPath[groupName] = missionCommands.addSubMenuForCoalition(
- coalition.side.RED,
- "Flight " .. groupName,
- planeMenuPath
- )
-
- missionCommands.addCommandForCoalition(
- coalition.side.RED,
- "Attack carrier Carl Vinson",
- Su34MenuPath[groupName],
- Su34AttackCarlVinson,
- groupName
- )
-
- missionCommands.addCommandForCoalition(
- coalition.side.RED,
- "Attack ships in the west",
- Su34MenuPath[groupName],
- Su34AttackWest,
- groupName
- )
-
- missionCommands.addCommandForCoalition(
- coalition.side.RED,
- "Attack ships in the north",
- Su34MenuPath[groupName],
- Su34AttackNorth,
- groupName
- )
-
- missionCommands.addCommandForCoalition(
- coalition.side.RED,
- "Hold position and await instructions",
- Su34MenuPath[groupName],
- Su34Orbit,
- groupName
- )
-
- missionCommands.addCommandForCoalition(
- coalition.side.RED,
- "Report status",
- Su34MenuPath[groupName],
- Su34OverviewStatus
- )
- end
- else
- if Su34MenuPath[groupName] then
- missionCommands.removeItemForCoalition(coalition.side.RED, Su34MenuPath[groupName])
- end
- end
-end
-
---- Obsolete function, but kept to rework in framework.
-
-function ChooseInfantry ( TeleportPrefixTable, TeleportMax )
---trace.f("Spawn")
- --env.info(( 'ChooseInfantry: ' ))
-
- TeleportPrefixTableCount = #TeleportPrefixTable
- TeleportPrefixTableIndex = math.random( 1, TeleportPrefixTableCount )
-
- --env.info(( 'ChooseInfantry: TeleportPrefixTableIndex = ' .. TeleportPrefixTableIndex .. ' TeleportPrefixTableCount = ' .. TeleportPrefixTableCount .. ' TeleportMax = ' .. TeleportMax ))
-
- local TeleportFound = false
- local TeleportLoop = true
- local Index = TeleportPrefixTableIndex
- local TeleportPrefix = ''
-
- while TeleportLoop do
- TeleportPrefix = TeleportPrefixTable[Index]
- if SpawnSettings[TeleportPrefix] then
- if SpawnSettings[TeleportPrefix]['SpawnCount'] - 1 < TeleportMax then
- SpawnSettings[TeleportPrefix]['SpawnCount'] = SpawnSettings[TeleportPrefix]['SpawnCount'] + 1
- TeleportFound = true
- else
- TeleportFound = false
- end
- else
- SpawnSettings[TeleportPrefix] = {}
- SpawnSettings[TeleportPrefix]['SpawnCount'] = 0
- TeleportFound = true
- end
- if TeleportFound then
- TeleportLoop = false
- else
- if Index < TeleportPrefixTableCount then
- Index = Index + 1
- else
- TeleportLoop = false
- end
- end
- --env.info(( 'ChooseInfantry: Loop 1 - TeleportPrefix = ' .. TeleportPrefix .. ' Index = ' .. Index ))
- end
-
- if TeleportFound == false then
- TeleportLoop = true
- Index = 1
- while TeleportLoop do
- TeleportPrefix = TeleportPrefixTable[Index]
- if SpawnSettings[TeleportPrefix] then
- if SpawnSettings[TeleportPrefix]['SpawnCount'] - 1 < TeleportMax then
- SpawnSettings[TeleportPrefix]['SpawnCount'] = SpawnSettings[TeleportPrefix]['SpawnCount'] + 1
- TeleportFound = true
- else
- TeleportFound = false
- end
- else
- SpawnSettings[TeleportPrefix] = {}
- SpawnSettings[TeleportPrefix]['SpawnCount'] = 0
- TeleportFound = true
- end
- if TeleportFound then
- TeleportLoop = false
- else
- if Index < TeleportPrefixTableIndex then
- Index = Index + 1
- else
- TeleportLoop = false
- end
- end
- --env.info(( 'ChooseInfantry: Loop 2 - TeleportPrefix = ' .. TeleportPrefix .. ' Index = ' .. Index ))
- end
- end
-
- local TeleportGroupName = ''
- if TeleportFound == true then
- TeleportGroupName = TeleportPrefix .. string.format("#%03d", SpawnSettings[TeleportPrefix]['SpawnCount'] )
- else
- TeleportGroupName = ''
- end
-
- --env.info(('ChooseInfantry: TeleportGroupName = ' .. TeleportGroupName ))
- --env.info(('ChooseInfantry: return'))
-
- return TeleportGroupName
-end
-
-SpawnedInfantry = 0
-
-function LandCarrier ( CarrierGroup, LandingZonePrefix )
---trace.f()
- --env.info(( 'LandCarrier: ' ))
- --env.info(( 'LandCarrier: CarrierGroup = ' .. CarrierGroup:getName() ))
- --env.info(( 'LandCarrier: LandingZone = ' .. LandingZonePrefix ))
-
- local controllerGroup = CarrierGroup:getController()
-
- local LandingZone = trigger.misc.getZone(LandingZonePrefix)
- local LandingZonePos = {}
- LandingZonePos.x = LandingZone.point.x + math.random(LandingZone.radius * -1, LandingZone.radius)
- LandingZonePos.y = LandingZone.point.z + math.random(LandingZone.radius * -1, LandingZone.radius)
-
- controllerGroup:pushTask( { id = 'Land', params = { point = LandingZonePos, durationFlag = true, duration = 10 } } )
-
- --env.info(( 'LandCarrier: end' ))
-end
-
-EscortCount = 0
-function EscortCarrier ( CarrierGroup, EscortPrefix, EscortLastWayPoint, EscortEngagementDistanceMax, EscortTargetTypes )
---trace.f()
- --env.info(( 'EscortCarrier: ' ))
- --env.info(( 'EscortCarrier: CarrierGroup = ' .. CarrierGroup:getName() ))
- --env.info(( 'EscortCarrier: EscortPrefix = ' .. EscortPrefix ))
-
- local CarrierName = CarrierGroup:getName()
-
- local EscortMission = {}
- local CarrierMission = {}
-
- local EscortMission = SpawnMissionGroup( EscortPrefix )
- local CarrierMission = SpawnMissionGroup( CarrierGroup:getName() )
-
- if EscortMission ~= nil and CarrierMission ~= nil then
-
- EscortCount = EscortCount + 1
- EscortMissionName = string.format( EscortPrefix .. '#Escort %s', CarrierName )
- EscortMission.name = EscortMissionName
- EscortMission.groupId = nil
- EscortMission.lateActivation = false
- EscortMission.taskSelected = false
-
- local EscortUnits = #EscortMission.units
- for u = 1, EscortUnits do
- EscortMission.units[u].name = string.format( EscortPrefix .. '#Escort %s %02d', CarrierName, u )
- EscortMission.units[u].unitId = nil
- end
-
-
- EscortMission.route.points[1].task = { id = "ComboTask",
- params =
- {
- tasks =
- {
- [1] =
- {
- enabled = true,
- auto = false,
- id = "Escort",
- number = 1,
- params =
- {
- lastWptIndexFlagChangedManually = false,
- groupId = CarrierGroup:getID(),
- lastWptIndex = nil,
- lastWptIndexFlag = false,
- engagementDistMax = EscortEngagementDistanceMax,
- targetTypes = EscortTargetTypes,
- pos =
- {
- y = 20,
- x = 20,
- z = 0,
- } -- end of ["pos"]
- } -- end of ["params"]
- } -- end of [1]
- } -- end of ["tasks"]
- } -- end of ["params"]
- } -- end of ["task"]
-
- SpawnGroupAdd( EscortPrefix, EscortMission )
-
- end
-end
-
-function SendMessageToCarrier( CarrierGroup, CarrierMessage )
---trace.f()
-
- if CarrierGroup ~= nil then
- MessageToGroup( CarrierGroup, CarrierMessage, 30, 'Carrier/' .. CarrierGroup:getName() )
- end
-
-end
-
-function MessageToGroup( MsgGroup, MsgText, MsgTime, MsgName )
---trace.f()
-
- if type(MsgGroup) == 'string' then
- --env.info( 'MessageToGroup: Converted MsgGroup string "' .. MsgGroup .. '" into a Group structure.' )
- MsgGroup = Group.getByName( MsgGroup )
- end
-
- if MsgGroup ~= nil then
- local MsgTable = {}
- MsgTable.text = MsgText
- MsgTable.displayTime = MsgTime
- MsgTable.msgFor = { units = { MsgGroup:getUnits()[1]:getName() } }
- MsgTable.name = MsgName
- --routines.message.add( MsgTable )
- --env.info(('MessageToGroup: Message sent to ' .. MsgGroup:getUnits()[1]:getName() .. ' -> ' .. MsgText ))
- end
-end
-
-function MessageToUnit( UnitName, MsgText, MsgTime, MsgName )
---trace.f()
-
- if UnitName ~= nil then
- local MsgTable = {}
- MsgTable.text = MsgText
- MsgTable.displayTime = MsgTime
- MsgTable.msgFor = { units = { UnitName } }
- MsgTable.name = MsgName
- --routines.message.add( MsgTable )
- end
-end
-
-function MessageToAll( MsgText, MsgTime, MsgName )
---trace.f()
-
- MESSAGE:New( MsgText, MsgTime, "Message" ):ToCoalition( coalition.side.RED ):ToCoalition( coalition.side.BLUE )
-end
-
-function MessageToRed( MsgText, MsgTime, MsgName )
---trace.f()
-
- MESSAGE:New( MsgText, MsgTime, "To Red Coalition" ):ToCoalition( coalition.side.RED )
-end
-
-function MessageToBlue( MsgText, MsgTime, MsgName )
---trace.f()
-
- MESSAGE:New( MsgText, MsgTime, "To Blue Coalition" ):ToCoalition( coalition.side.RED )
-end
-
-function getCarrierHeight( CarrierGroup )
---trace.f()
-
- if CarrierGroup ~= nil then
- if table.getn(CarrierGroup:getUnits()) == 1 then
- local CarrierUnit = CarrierGroup:getUnits()[1]
- local CurrentPoint = CarrierUnit:getPoint()
-
- local CurrentPosition = { x = CurrentPoint.x, y = CurrentPoint.z }
- local CarrierHeight = CurrentPoint.y
-
- local LandHeight = land.getHeight( CurrentPosition )
-
- --env.info(( 'CarrierHeight: LandHeight = ' .. LandHeight .. ' CarrierHeight = ' .. CarrierHeight ))
-
- return CarrierHeight - LandHeight
- else
- return 999999
- end
- else
- return 999999
- end
-
-end
-
-function GetUnitHeight( CheckUnit )
---trace.f()
-
- local UnitPoint = CheckUnit:getPoint()
- local UnitPosition = { x = CurrentPoint.x, y = CurrentPoint.z }
- local UnitHeight = CurrentPoint.y
-
- local LandHeight = land.getHeight( CurrentPosition )
-
- --env.info(( 'CarrierHeight: LandHeight = ' .. LandHeight .. ' CarrierHeight = ' .. CarrierHeight ))
-
- return UnitHeight - LandHeight
-
-end
-
-
-_MusicTable = {}
-_MusicTable.Files = {}
-_MusicTable.Queue = {}
-_MusicTable.FileCnt = 0
-
-
-function MusicRegister( SndRef, SndFile, SndTime )
---trace.f()
-
- env.info(( 'MusicRegister: SndRef = ' .. SndRef ))
- env.info(( 'MusicRegister: SndFile = ' .. SndFile ))
- env.info(( 'MusicRegister: SndTime = ' .. SndTime ))
-
-
- _MusicTable.FileCnt = _MusicTable.FileCnt + 1
-
- _MusicTable.Files[_MusicTable.FileCnt] = {}
- _MusicTable.Files[_MusicTable.FileCnt].Ref = SndRef
- _MusicTable.Files[_MusicTable.FileCnt].File = SndFile
- _MusicTable.Files[_MusicTable.FileCnt].Time = SndTime
-
- if not _MusicTable.Function then
- _MusicTable.Function = routines.scheduleFunction( MusicScheduler, { }, timer.getTime() + 10, 10)
- end
-
-end
-
-function MusicToPlayer( SndRef, PlayerName, SndContinue )
---trace.f()
-
- --env.info(( 'MusicToPlayer: SndRef = ' .. SndRef ))
-
- local PlayerUnits = AlivePlayerUnits()
- for PlayerUnitIdx, PlayerUnit in pairs(PlayerUnits) do
- local PlayerUnitName = PlayerUnit:getPlayerName()
- --env.info(( 'MusicToPlayer: PlayerUnitName = ' .. PlayerUnitName ))
- if PlayerName == PlayerUnitName then
- PlayerGroup = PlayerUnit:getGroup()
- if PlayerGroup then
- --env.info(( 'MusicToPlayer: PlayerGroup = ' .. PlayerGroup:getName() ))
- MusicToGroup( SndRef, PlayerGroup, SndContinue )
- end
- break
- end
- end
-
- --env.info(( 'MusicToPlayer: end' ))
-
-end
-
-function MusicToGroup( SndRef, SndGroup, SndContinue )
---trace.f()
-
- --env.info(( 'MusicToGroup: SndRef = ' .. SndRef ))
-
- if SndGroup ~= nil then
- if _MusicTable and _MusicTable.FileCnt > 0 then
- if SndGroup:isExist() then
- if MusicCanStart(SndGroup:getUnit(1):getPlayerName()) then
- --env.info(( 'MusicToGroup: OK for Sound.' ))
- local SndIdx = 0
- if SndRef == '' then
- --env.info(( 'MusicToGroup: SndRef as empty. Queueing at random.' ))
- SndIdx = math.random( 1, _MusicTable.FileCnt )
- else
- for SndIdx = 1, _MusicTable.FileCnt do
- if _MusicTable.Files[SndIdx].Ref == SndRef then
- break
- end
- end
- end
- --env.info(( 'MusicToGroup: SndIdx = ' .. SndIdx ))
- --env.info(( 'MusicToGroup: Queueing Music ' .. _MusicTable.Files[SndIdx].File .. ' for Group ' .. SndGroup:getID() ))
- trigger.action.outSoundForGroup( SndGroup:getID(), _MusicTable.Files[SndIdx].File )
- MessageToGroup( SndGroup, 'Playing ' .. _MusicTable.Files[SndIdx].File, 15, 'Music-' .. SndGroup:getUnit(1):getPlayerName() )
-
- local SndQueueRef = SndGroup:getUnit(1):getPlayerName()
- if _MusicTable.Queue[SndQueueRef] == nil then
- _MusicTable.Queue[SndQueueRef] = {}
- end
- _MusicTable.Queue[SndQueueRef].Start = timer.getTime()
- _MusicTable.Queue[SndQueueRef].PlayerName = SndGroup:getUnit(1):getPlayerName()
- _MusicTable.Queue[SndQueueRef].Group = SndGroup
- _MusicTable.Queue[SndQueueRef].ID = SndGroup:getID()
- _MusicTable.Queue[SndQueueRef].Ref = SndIdx
- _MusicTable.Queue[SndQueueRef].Continue = SndContinue
- _MusicTable.Queue[SndQueueRef].Type = Group
- end
- end
- end
- end
-end
-
-function MusicCanStart(PlayerName)
---trace.f()
-
- --env.info(( 'MusicCanStart:' ))
-
- local MusicOut = false
-
- if _MusicTable['Queue'] ~= nil and _MusicTable.FileCnt > 0 then
- --env.info(( 'MusicCanStart: PlayerName = ' .. PlayerName ))
- local PlayerFound = false
- local MusicStart = 0
- local MusicTime = 0
- for SndQueueIdx, SndQueue in pairs( _MusicTable.Queue ) do
- if SndQueue.PlayerName == PlayerName then
- PlayerFound = true
- MusicStart = SndQueue.Start
- MusicTime = _MusicTable.Files[SndQueue.Ref].Time
- break
- end
- end
- if PlayerFound then
- --env.info(( 'MusicCanStart: MusicStart = ' .. MusicStart ))
- --env.info(( 'MusicCanStart: MusicTime = ' .. MusicTime ))
- --env.info(( 'MusicCanStart: timer.getTime() = ' .. timer.getTime() ))
-
- if MusicStart + MusicTime <= timer.getTime() then
- MusicOut = true
- end
- else
- MusicOut = true
- end
- end
-
- if MusicOut then
- --env.info(( 'MusicCanStart: true' ))
- else
- --env.info(( 'MusicCanStart: false' ))
- end
-
- return MusicOut
-end
-
-function MusicScheduler()
---trace.scheduled("", "MusicScheduler")
-
- --env.info(( 'MusicScheduler:' ))
- if _MusicTable['Queue'] ~= nil and _MusicTable.FileCnt > 0 then
- --env.info(( 'MusicScheduler: Walking Sound Queue.'))
- for SndQueueIdx, SndQueue in pairs( _MusicTable.Queue ) do
- if SndQueue.Continue then
- if MusicCanStart(SndQueue.PlayerName) then
- --env.info(('MusicScheduler: MusicToGroup'))
- MusicToPlayer( '', SndQueue.PlayerName, true )
- end
- end
- end
- end
-
-end
-
-
-env.info(( 'Init: Scripts Loaded v1.1' ))
-
---- This module contains derived utilities taken from the MIST framework,
--- which are excellent tools to be reused in an OO environment!.
---
--- ### Authors:
---
--- * Grimes : Design & Programming of the MIST framework.
---
--- ### Contributions:
---
--- * FlightControl : Rework to OO framework
---
--- @module Utils
-
-
---- @type SMOKECOLOR
--- @field Green
--- @field Red
--- @field White
--- @field Orange
--- @field Blue
-
-SMOKECOLOR = trigger.smokeColor -- #SMOKECOLOR
-
---- @type FLARECOLOR
--- @field Green
--- @field Red
--- @field White
--- @field Yellow
-
-FLARECOLOR = trigger.flareColor -- #FLARECOLOR
-
---- Utilities static class.
--- @type UTILS
-UTILS = {}
-
-
---from http://lua-users.org/wiki/CopyTable
-UTILS.DeepCopy = function(object)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for index, value in pairs(object) do
- new_table[_copy(index)] = _copy(value)
- end
- return setmetatable(new_table, getmetatable(object))
- end
- local objectreturn = _copy(object)
- return objectreturn
-end
-
-
--- porting in Slmod's serialize_slmod2
-UTILS.OneLineSerialize = function( tbl ) -- serialization of a table all on a single line, no comments, made to replace old get_table_string function
-
- lookup_table = {}
-
- local function _Serialize( tbl )
-
- if type(tbl) == 'table' then --function only works for tables!
-
- if lookup_table[tbl] then
- return lookup_table[object]
- end
-
- local tbl_str = {}
-
- lookup_table[tbl] = tbl_str
-
- tbl_str[#tbl_str + 1] = '{'
-
- for ind,val in pairs(tbl) do -- serialize its fields
- local ind_str = {}
- if type(ind) == "number" then
- ind_str[#ind_str + 1] = '['
- ind_str[#ind_str + 1] = tostring(ind)
- ind_str[#ind_str + 1] = ']='
- else --must be a string
- ind_str[#ind_str + 1] = '['
- ind_str[#ind_str + 1] = routines.utils.basicSerialize(ind)
- ind_str[#ind_str + 1] = ']='
- end
-
- local val_str = {}
- if ((type(val) == 'number') or (type(val) == 'boolean')) then
- val_str[#val_str + 1] = tostring(val)
- val_str[#val_str + 1] = ','
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'string' then
- val_str[#val_str + 1] = routines.utils.basicSerialize(val)
- val_str[#val_str + 1] = ','
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'nil' then -- won't ever happen, right?
- val_str[#val_str + 1] = 'nil,'
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- elseif type(val) == 'table' then
- if ind == "__index" then
- -- tbl_str[#tbl_str + 1] = "__index"
- -- tbl_str[#tbl_str + 1] = ',' --I think this is right, I just added it
- else
-
- val_str[#val_str + 1] = _Serialize(val)
- val_str[#val_str + 1] = ',' --I think this is right, I just added it
- tbl_str[#tbl_str + 1] = table.concat(ind_str)
- tbl_str[#tbl_str + 1] = table.concat(val_str)
- end
- elseif type(val) == 'function' then
- tbl_str[#tbl_str + 1] = "f() " .. tostring(ind)
- tbl_str[#tbl_str + 1] = ',' --I think this is right, I just added it
- else
- env.info('unable to serialize value type ' .. routines.utils.basicSerialize(type(val)) .. ' at index ' .. tostring(ind))
- env.info( debug.traceback() )
- end
-
- end
- tbl_str[#tbl_str + 1] = '}'
- return table.concat(tbl_str)
- else
- return tostring(tbl)
- end
- end
-
- local objectreturn = _Serialize(tbl)
- return objectreturn
-end
-
---porting in Slmod's "safestring" basic serialize
-UTILS.BasicSerialize = function(s)
- if s == nil then
- return "\"\""
- else
- if ((type(s) == 'number') or (type(s) == 'boolean') or (type(s) == 'function') or (type(s) == 'table') or (type(s) == 'userdata') ) then
- return tostring(s)
- elseif type(s) == 'string' then
- s = string.format('%q', s)
- return s
- end
- end
-end
-
-
-UTILS.ToDegree = function(angle)
- return angle*180/math.pi
-end
-
-UTILS.ToRadian = function(angle)
- return angle*math.pi/180
-end
-
-UTILS.MetersToNM = function(meters)
- return meters/1852
-end
-
-UTILS.MetersToFeet = function(meters)
- return meters/0.3048
-end
-
-UTILS.NMToMeters = function(NM)
- return NM*1852
-end
-
-UTILS.FeetToMeters = function(feet)
- return feet*0.3048
-end
-
-UTILS.MpsToKnots = function(mps)
- return mps*3600/1852
-end
-
-UTILS.MpsToKmph = function(mps)
- return mps*3.6
-end
-
-UTILS.KnotsToMps = function(knots)
- return knots*1852/3600
-end
-
-UTILS.KmphToMps = function(kmph)
- return kmph/3.6
-end
-
---[[acc:
-in DM: decimal point of minutes.
-In DMS: decimal point of seconds.
-position after the decimal of the least significant digit:
-So:
-42.32 - acc of 2.
-]]
-UTILS.tostringLL = function( lat, lon, acc, DMS)
-
- local latHemi, lonHemi
- if lat > 0 then
- latHemi = 'N'
- else
- latHemi = 'S'
- end
-
- if lon > 0 then
- lonHemi = 'E'
- else
- lonHemi = 'W'
- end
-
- lat = math.abs(lat)
- lon = math.abs(lon)
-
- local latDeg = math.floor(lat)
- local latMin = (lat - latDeg)*60
-
- local lonDeg = math.floor(lon)
- local lonMin = (lon - lonDeg)*60
-
- if DMS then -- degrees, minutes, and seconds.
- local oldLatMin = latMin
- latMin = math.floor(latMin)
- local latSec = UTILS.Round((oldLatMin - latMin)*60, acc)
-
- local oldLonMin = lonMin
- lonMin = math.floor(lonMin)
- local lonSec = UTILS.Round((oldLonMin - lonMin)*60, acc)
-
- if latSec == 60 then
- latSec = 0
- latMin = latMin + 1
- end
-
- if lonSec == 60 then
- lonSec = 0
- lonMin = lonMin + 1
- end
-
- local secFrmtStr -- create the formatting string for the seconds place
- if acc <= 0 then -- no decimal place.
- secFrmtStr = '%02d'
- else
- local width = 3 + acc -- 01.310 - that's a width of 6, for example.
- secFrmtStr = '%0' .. width .. '.' .. acc .. 'f'
- end
-
- return string.format('%02d', latDeg) .. ' ' .. string.format('%02d', latMin) .. '\' ' .. string.format(secFrmtStr, latSec) .. '"' .. latHemi .. ' '
- .. string.format('%02d', lonDeg) .. ' ' .. string.format('%02d', lonMin) .. '\' ' .. string.format(secFrmtStr, lonSec) .. '"' .. lonHemi
-
- else -- degrees, decimal minutes.
- latMin = UTILS.Round(latMin, acc)
- lonMin = UTILS.Round(lonMin, acc)
-
- if latMin == 60 then
- latMin = 0
- latDeg = latDeg + 1
- end
-
- if lonMin == 60 then
- lonMin = 0
- lonDeg = lonDeg + 1
- end
-
- local minFrmtStr -- create the formatting string for the minutes place
- if acc <= 0 then -- no decimal place.
- minFrmtStr = '%02d'
- else
- local width = 3 + acc -- 01.310 - that's a width of 6, for example.
- minFrmtStr = '%0' .. width .. '.' .. acc .. 'f'
- end
-
- return string.format('%02d', latDeg) .. ' ' .. string.format(minFrmtStr, latMin) .. '\'' .. latHemi .. ' '
- .. string.format('%02d', lonDeg) .. ' ' .. string.format(minFrmtStr, lonMin) .. '\'' .. lonHemi
-
- end
-end
-
-
---- From http://lua-users.org/wiki/SimpleRound
--- use negative idp for rounding ahead of decimal place, positive for rounding after decimal place
-function UTILS.Round( num, idp )
- local mult = 10 ^ ( idp or 0 )
- return math.floor( num * mult + 0.5 ) / mult
-end
-
--- porting in Slmod's dostring
-function UTILS.DoString( s )
- local f, err = loadstring( s )
- if f then
- return true, f()
- else
- return false, err
- end
-end
---- **Core** - BASE forms **the basis of the MOOSE framework**. Each class within the MOOSE framework derives from BASE.
---
--- 
---
--- ===
---
--- The @{#BASE} class is the core root class from where every other class in moose is derived.
---
--- ===
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- YYYY-MM-DD: CLASS:**NewFunction**( Params ) replaces CLASS:_OldFunction_( Params )
--- YYYY-MM-DD: CLASS:**NewFunction( Params )** added
---
--- Hereby the change log:
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * None.
---
--- ### Authors:
---
--- * **FlightControl**: Design & Programming
---
--- @module Base
-
-
-
-local _TraceOnOff = true
-local _TraceLevel = 1
-local _TraceAll = false
-local _TraceClass = {}
-local _TraceClassMethod = {}
-
-local _ClassID = 0
-
---- @type BASE
--- @field ClassName The name of the class.
--- @field ClassID The ID number of the class.
--- @field ClassNameAndID The name of the class concatenated with the ID number of the class.
-
---- # 1) #BASE class
---
--- All classes within the MOOSE framework are derived from the BASE class.
---
--- BASE provides facilities for :
---
--- * The construction and inheritance of MOOSE classes.
--- * The class naming and numbering system.
--- * The class hierarchy search system.
--- * The tracing of information or objects during mission execution for debuggin purposes.
--- * The subscription to DCS events for event handling in MOOSE objects.
---
--- Note: The BASE class is an abstract class and is not meant to be used directly.
---
--- ## 1.1) BASE constructor
---
--- Any class derived from BASE, will use the @{Base#BASE.New} constructor embedded in the @{Base#BASE.Inherit} method.
--- See an example at the @{Base#BASE.New} method how this is done.
---
--- ## 1.2) Trace information for debugging
---
--- The BASE class contains trace methods to trace progress within a mission execution of a certain object.
--- These trace methods are inherited by each MOOSE class interiting BASE, soeach object created from derived class from BASE can use the tracing methods to trace its execution.
---
--- Any type of information can be passed to these tracing methods. See the following examples:
---
--- self:E( "Hello" )
---
--- Result in the word "Hello" in the dcs.log.
---
--- local Array = { 1, nil, "h", { "a","b" }, "x" }
--- self:E( Array )
---
--- Results with the text [1]=1,[3]="h",[4]={[1]="a",[2]="b"},[5]="x"} in the dcs.log.
---
--- local Object1 = "Object1"
--- local Object2 = 3
--- local Object3 = { Object 1, Object 2 }
--- self:E( { Object1, Object2, Object3 } )
---
--- Results with the text [1]={[1]="Object",[2]=3,[3]={[1]="Object",[2]=3}} in the dcs.log.
---
--- local SpawnObject = SPAWN:New( "Plane" )
--- local GroupObject = GROUP:FindByName( "Group" )
--- self:E( { Spawn = SpawnObject, Group = GroupObject } )
---
--- Results with the text [1]={Spawn={....),Group={...}} in the dcs.log.
---
--- Below a more detailed explanation of the different method types for tracing.
---
--- ### 1.2.1) Tracing methods categories
---
--- There are basically 3 types of tracing methods available:
---
--- * @{#BASE.F}: Used to trace the entrance of a function and its given parameters. An F is indicated at column 44 in the DCS.log file.
--- * @{#BASE.T}: Used to trace further logic within a function giving optional variables or parameters. A T is indicated at column 44 in the DCS.log file.
--- * @{#BASE.E}: Used to always trace information giving optional variables or parameters. An E is indicated at column 44 in the DCS.log file.
---
--- ### 1.2.2) Tracing levels
---
--- There are 3 tracing levels within MOOSE.
--- These tracing levels were defined to avoid bulks of tracing to be generated by lots of objects.
---
--- As such, the F and T methods have additional variants to trace level 2 and 3 respectively:
---
--- * @{#BASE.F2}: Trace the beginning of a function and its given parameters with tracing level 2.
--- * @{#BASE.F3}: Trace the beginning of a function and its given parameters with tracing level 3.
--- * @{#BASE.T2}: Trace further logic within a function giving optional variables or parameters with tracing level 2.
--- * @{#BASE.T3}: Trace further logic within a function giving optional variables or parameters with tracing level 3.
---
--- ### 1.2.3) Trace activation.
---
--- Tracing can be activated in several ways:
---
--- * Switch tracing on or off through the @{#BASE.TraceOnOff}() method.
--- * Activate all tracing through the @{#BASE.TraceAll}() method.
--- * Activate only the tracing of a certain class (name) through the @{#BASE.TraceClass}() method.
--- * Activate only the tracing of a certain method of a certain class through the @{#BASE.TraceClassMethod}() method.
--- * Activate only the tracing of a certain level through the @{#BASE.TraceLevel}() method.
---
--- ### 1.2.4) Check if tracing is on.
---
--- The method @{#BASE.IsTrace}() will validate if tracing is activated or not.
---
--- ## 1.3 DCS simulator Event Handling
---
--- The BASE class provides methods to catch DCS Events. These are events that are triggered from within the DCS simulator,
--- and handled through lua scripting. MOOSE provides an encapsulation to handle these events more efficiently.
---
--- ### 1.3.1 Subscribe / Unsubscribe to DCS Events
---
--- At first, the mission designer will need to **Subscribe** to a specific DCS event for the class.
--- So, when the DCS event occurs, the class will be notified of that event.
--- There are two methods which you use to subscribe to or unsubscribe from an event.
---
--- * @{#BASE.HandleEvent}(): Subscribe to a DCS Event.
--- * @{#BASE.UnHandleEvent}(): Unsubscribe from a DCS Event.
---
--- ### 1.3.2 Event Handling of DCS Events
---
--- Once the class is subscribed to the event, an **Event Handling** method on the object or class needs to be written that will be called
--- when the DCS event occurs. The Event Handling method receives an @{Event#EVENTDATA} structure, which contains a lot of information
--- about the event that occurred.
---
--- Find below an example of the prototype how to write an event handling function for two units:
---
--- local Tank1 = UNIT:FindByName( "Tank A" )
--- local Tank2 = UNIT:FindByName( "Tank B" )
---
--- -- Here we subscribe to the Dead events. So, if one of these tanks dies, the Tank1 or Tank2 objects will be notified.
--- Tank1:HandleEvent( EVENTS.Dead )
--- Tank2:HandleEvent( EVENTS.Dead )
---
--- --- This function is an Event Handling function that will be called when Tank1 is Dead.
--- -- @param Wrapper.Unit#UNIT self
--- -- @param Core.Event#EVENTDATA EventData
--- function Tank1:OnEventDead( EventData )
---
--- self:SmokeGreen()
--- end
---
--- --- This function is an Event Handling function that will be called when Tank2 is Dead.
--- -- @param Wrapper.Unit#UNIT self
--- -- @param Core.Event#EVENTDATA EventData
--- function Tank2:OnEventDead( EventData )
---
--- self:SmokeBlue()
--- end
---
---
---
--- See the @{Event} module for more information about event handling.
---
--- ## 1.4) Class identification methods
---
--- BASE provides methods to get more information of each object:
---
--- * @{#BASE.GetClassID}(): Gets the ID (number) of the object. Each object created is assigned a number, that is incremented by one.
--- * @{#BASE.GetClassName}(): Gets the name of the object, which is the name of the class the object was instantiated from.
--- * @{#BASE.GetClassNameAndID}(): Gets the name and ID of the object.
---
--- ## 1.5) All objects derived from BASE can have "States"
---
--- A mechanism is in place in MOOSE, that allows to let the objects administer **states**.
--- States are essentially properties of objects, which are identified by a **Key** and a **Value**.
---
--- The method @{#BASE.SetState}() can be used to set a Value with a reference Key to the object.
--- To **read or retrieve** a state Value based on a Key, use the @{#BASE.GetState} method.
---
--- These two methods provide a very handy way to keep state at long lasting processes.
--- Values can be stored within the objects, and later retrieved or changed when needed.
--- There is one other important thing to note, the @{#BASE.SetState}() and @{#BASE.GetState} methods
--- receive as the **first parameter the object for which the state needs to be set**.
--- Thus, if the state is to be set for the same object as the object for which the method is used, then provide the same
--- object name to the method.
---
--- ## 1.10) Inheritance
---
--- The following methods are available to implement inheritance
---
--- * @{#BASE.Inherit}: Inherits from a class.
--- * @{#BASE.GetParent}: Returns the parent object from the object it is handling, or nil if there is no parent object.
---
--- ===
---
--- @field #BASE BASE
---
-BASE = {
- ClassName = "BASE",
- ClassID = 0,
- _Private = {},
- Events = {},
- States = {}
-}
-
---- The Formation Class
--- @type FORMATION
--- @field Cone A cone formation.
-FORMATION = {
- Cone = "Cone"
-}
-
-
-
---- BASE constructor.
---
--- This is an example how to use the BASE:New() constructor in a new class definition when inheriting from BASE.
---
--- function EVENT:New()
--- local self = BASE:Inherit( self, BASE:New() ) -- #EVENT
--- return self
--- end
---
--- @param #BASE self
--- @return #BASE
-function BASE:New()
- local self = routines.utils.deepCopy( self ) -- Create a new self instance
- local MetaTable = {}
- setmetatable( self, MetaTable )
- self.__index = self
- _ClassID = _ClassID + 1
- self.ClassID = _ClassID
-
-
- return self
-end
-
-function BASE:_Destructor()
- --self:E("_Destructor")
-
- --self:EventRemoveAll()
-end
-
-
--- THIS IS WHY WE NEED LUA 5.2 ...
-function BASE:_SetDestructor()
-
- -- TODO: Okay, this is really technical...
- -- When you set a proxy to a table to catch __gc, weak tables don't behave like weak...
- -- Therefore, I am parking this logic until I've properly discussed all this with the community.
-
- local proxy = newproxy(true)
- local proxyMeta = getmetatable(proxy)
-
- proxyMeta.__gc = function ()
- env.info("In __gc for " .. self:GetClassNameAndID() )
- if self._Destructor then
- self:_Destructor()
- end
- end
-
- -- keep the userdata from newproxy reachable until the object
- -- table is about to be garbage-collected - then the __gc hook
- -- will be invoked and the destructor called
- rawset( self, '__proxy', proxy )
-
-end
-
---- This is the worker method to inherit from a parent class.
--- @param #BASE self
--- @param Child is the Child class that inherits.
--- @param #BASE Parent is the Parent class that the Child inherits from.
--- @return #BASE Child
-function BASE:Inherit( Child, Parent )
- local Child = routines.utils.deepCopy( Child )
- --local Parent = routines.utils.deepCopy( Parent )
- --local Parent = Parent
- if Child ~= nil then
- setmetatable( Child, Parent )
- Child.__index = Child
-
- --Child:_SetDestructor()
- end
- --self:T( 'Inherited from ' .. Parent.ClassName )
- return Child
-end
-
---- This is the worker method to retrieve the Parent class.
--- Note that the Parent class must be passed to call the parent class method.
---
--- self:GetParent(self):ParentMethod()
---
---
--- @param #BASE self
--- @param #BASE Child is the Child class from which the Parent class needs to be retrieved.
--- @return #BASE
-function BASE:GetParent( Child )
- local Parent = getmetatable( Child )
--- env.info('Inherited class of ' .. Child.ClassName .. ' is ' .. Parent.ClassName )
- return Parent
-end
-
---- Get the ClassName + ClassID of the class instance.
--- The ClassName + ClassID is formatted as '%s#%09d'.
--- @param #BASE self
--- @return #string The ClassName + ClassID of the class instance.
-function BASE:GetClassNameAndID()
- return string.format( '%s#%09d', self.ClassName, self.ClassID )
-end
-
---- Get the ClassName of the class instance.
--- @param #BASE self
--- @return #string The ClassName of the class instance.
-function BASE:GetClassName()
- return self.ClassName
-end
-
---- Get the ClassID of the class instance.
--- @param #BASE self
--- @return #string The ClassID of the class instance.
-function BASE:GetClassID()
- return self.ClassID
-end
-
-do -- Event Handling
-
- --- Returns the event dispatcher
- -- @param #BASE self
- -- @return Core.Event#EVENT
- function BASE:EventDispatcher()
-
- return _EVENTDISPATCHER
- end
-
-
- --- Get the Class @{Event} processing Priority.
- -- The Event processing Priority is a number from 1 to 10,
- -- reflecting the order of the classes subscribed to the Event to be processed.
- -- @param #BASE self
- -- @return #number The @{Event} processing Priority.
- function BASE:GetEventPriority()
- return self._Private.EventPriority or 5
- end
-
- --- Set the Class @{Event} processing Priority.
- -- The Event processing Priority is a number from 1 to 10,
- -- reflecting the order of the classes subscribed to the Event to be processed.
- -- @param #BASE self
- -- @param #number EventPriority The @{Event} processing Priority.
- -- @return self
- function BASE:SetEventPriority( EventPriority )
- self._Private.EventPriority = EventPriority
- end
-
- --- Remove all subscribed events
- -- @param #BASE self
- -- @return #BASE
- function BASE:EventRemoveAll()
-
- self:EventDispatcher():RemoveAll( self )
-
- return self
- end
-
- --- Subscribe to a DCS Event.
- -- @param #BASE self
- -- @param Core.Event#EVENTS Event
- -- @param #function EventFunction (optional) The function to be called when the event occurs for the unit.
- -- @return #BASE
- function BASE:HandleEvent( Event, EventFunction )
-
- self:EventDispatcher():OnEventGeneric( EventFunction, self, Event )
-
- return self
- end
-
- --- UnSubscribe to a DCS event.
- -- @param #BASE self
- -- @param Core.Event#EVENTS Event
- -- @return #BASE
- function BASE:UnHandleEvent( Event )
-
- self:EventDispatcher():Remove( self, Event )
-
- return self
- end
-
- -- Event handling function prototypes
-
- --- Occurs whenever any unit in a mission fires a weapon. But not any machine gun or autocannon based weapon, those are handled by EVENT.ShootingStart.
- -- @function [parent=#BASE] OnEventShot
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs whenever an object is hit by a weapon.
- -- initiator : The unit object the fired the weapon
- -- weapon: Weapon object that hit the target
- -- target: The Object that was hit.
- -- @function [parent=#BASE] OnEventHit
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when an aircraft takes off from an airbase, farp, or ship.
- -- initiator : The unit that tookoff
- -- place: Object from where the AI took-off from. Can be an Airbase Object, FARP, or Ships
- -- @function [parent=#BASE] OnEventTakeoff
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when an aircraft lands at an airbase, farp or ship
- -- initiator : The unit that has landed
- -- place: Object that the unit landed on. Can be an Airbase Object, FARP, or Ships
- -- @function [parent=#BASE] OnEventLand
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any aircraft crashes into the ground and is completely destroyed.
- -- initiator : The unit that has crashed
- -- @function [parent=#BASE] OnEventCrash
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when a pilot ejects from an aircraft
- -- initiator : The unit that has ejected
- -- @function [parent=#BASE] OnEventEjection
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when an aircraft connects with a tanker and begins taking on fuel.
- -- initiator : The unit that is receiving fuel.
- -- @function [parent=#BASE] OnEventRefueling
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when an object is completely destroyed.
- -- initiator : The unit that is was destroyed.
- -- @function [parent=#BASE] OnEvent
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when the pilot of an aircraft is killed. Can occur either if the player is alive and crashes or if a weapon kills the pilot without completely destroying the plane.
- -- initiator : The unit that the pilot has died in.
- -- @function [parent=#BASE] OnEventPilotDead
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when a ground unit captures either an airbase or a farp.
- -- initiator : The unit that captured the base
- -- place: The airbase that was captured, can be a FARP or Airbase. When calling place:getCoalition() the faction will already be the new owning faction.
- -- @function [parent=#BASE] OnEventBaseCaptured
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when a mission starts
- -- @function [parent=#BASE] OnEventMissionStart
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when a mission ends
- -- @function [parent=#BASE] OnEventMissionEnd
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when an aircraft is finished taking fuel.
- -- initiator : The unit that was receiving fuel.
- -- @function [parent=#BASE] OnEventRefuelingStop
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any object is spawned into the mission.
- -- initiator : The unit that was spawned
- -- @function [parent=#BASE] OnEventBirth
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any system fails on a human controlled aircraft.
- -- initiator : The unit that had the failure
- -- @function [parent=#BASE] OnEventHumanFailure
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any aircraft starts its engines.
- -- initiator : The unit that is starting its engines.
- -- @function [parent=#BASE] OnEventEngineStartup
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any aircraft shuts down its engines.
- -- initiator : The unit that is stopping its engines.
- -- @function [parent=#BASE] OnEventEngineShutdown
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any player assumes direct control of a unit.
- -- initiator : The unit that is being taken control of.
- -- @function [parent=#BASE] OnEventPlayerEnterUnit
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any player relieves control of a unit to the AI.
- -- initiator : The unit that the player left.
- -- @function [parent=#BASE] OnEventPlayerLeaveUnit
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any unit begins firing a weapon that has a high rate of fire. Most common with aircraft cannons (GAU-8), autocannons, and machine guns.
- -- initiator : The unit that is doing the shooing.
- -- target: The unit that is being targeted.
- -- @function [parent=#BASE] OnEventShootingStart
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
- --- Occurs when any unit stops firing its weapon. Event will always correspond with a shooting start event.
- -- initiator : The unit that was doing the shooing.
- -- @function [parent=#BASE] OnEventShootingEnd
- -- @param #BASE self
- -- @param Core.Event#EVENTDATA EventData The EventData structure.
-
-end
-
-
---- Creation of a Birth Event.
--- @param #BASE self
--- @param Dcs.DCSTypes#Time EventTime The time stamp of the event.
--- @param Dcs.DCSWrapper.Object#Object Initiator The initiating object of the event.
--- @param #string IniUnitName The initiating unit name.
--- @param place
--- @param subplace
-function BASE:CreateEventBirth( EventTime, Initiator, IniUnitName, place, subplace )
- self:F( { EventTime, Initiator, IniUnitName, place, subplace } )
-
- local Event = {
- id = world.event.S_EVENT_BIRTH,
- time = EventTime,
- initiator = Initiator,
- IniUnitName = IniUnitName,
- place = place,
- subplace = subplace
- }
-
- world.onEvent( Event )
-end
-
---- Creation of a Crash Event.
--- @param #BASE self
--- @param Dcs.DCSTypes#Time EventTime The time stamp of the event.
--- @param Dcs.DCSWrapper.Object#Object Initiator The initiating object of the event.
-function BASE:CreateEventCrash( EventTime, Initiator )
- self:F( { EventTime, Initiator } )
-
- local Event = {
- id = world.event.S_EVENT_CRASH,
- time = EventTime,
- initiator = Initiator,
- }
-
- world.onEvent( Event )
-end
-
--- TODO: Complete Dcs.DCSTypes#Event structure.
---- The main event handling function... This function captures all events generated for the class.
--- @param #BASE self
--- @param Dcs.DCSTypes#Event event
-function BASE:onEvent(event)
- --self:F( { BaseEventCodes[event.id], event } )
-
- if self then
- for EventID, EventObject in pairs( self.Events ) do
- if EventObject.EventEnabled then
- --env.info( 'onEvent Table EventObject.Self = ' .. tostring(EventObject.Self) )
- --env.info( 'onEvent event.id = ' .. tostring(event.id) )
- --env.info( 'onEvent EventObject.Event = ' .. tostring(EventObject.Event) )
- if event.id == EventObject.Event then
- if self == EventObject.Self then
- if event.initiator and event.initiator:isExist() then
- event.IniUnitName = event.initiator:getName()
- end
- if event.target and event.target:isExist() then
- event.TgtUnitName = event.target:getName()
- end
- --self:T( { BaseEventCodes[event.id], event } )
- --EventObject.EventFunction( self, event )
- end
- end
- end
- end
- end
-end
-
---- Set a state or property of the Object given a Key and a Value.
--- Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone.
--- @param #BASE self
--- @param Object The object that will hold the Value set by the Key.
--- @param Key The key that is used as a reference of the value. Note that the key can be a #string, but it can also be any other type!
--- @param Value The value to is stored in the object.
--- @return The Value set.
--- @return #nil The Key was not found and thus the Value could not be retrieved.
-function BASE:SetState( Object, Key, Value )
-
- local ClassNameAndID = Object:GetClassNameAndID()
-
- self.States[ClassNameAndID] = self.States[ClassNameAndID] or {}
- self.States[ClassNameAndID][Key] = Value
- self:T2( { ClassNameAndID, Key, Value } )
-
- return self.States[ClassNameAndID][Key]
-end
-
-
---- Get a Value given a Key from the Object.
--- Note that if the Object is destroyed, nillified or garbage collected, then the Values and Keys will also be gone.
--- @param #BASE self
--- @param Object The object that holds the Value set by the Key.
--- @param Key The key that is used to retrieve the value. Note that the key can be a #string, but it can also be any other type!
--- @param Value The value to is stored in the Object.
--- @return The Value retrieved.
-function BASE:GetState( Object, Key )
-
- local ClassNameAndID = Object:GetClassNameAndID()
-
- if self.States[ClassNameAndID] then
- local Value = self.States[ClassNameAndID][Key] or false
- self:T2( { ClassNameAndID, Key, Value } )
- return Value
- end
-
- return nil
-end
-
-function BASE:ClearState( Object, StateName )
-
- local ClassNameAndID = Object:GetClassNameAndID()
- if self.States[ClassNameAndID] then
- self.States[ClassNameAndID][StateName] = nil
- end
-end
-
--- Trace section
-
--- Log a trace (only shown when trace is on)
--- TODO: Make trace function using variable parameters.
-
---- Set trace on or off
--- Note that when trace is off, no debug statement is performed, increasing performance!
--- When Moose is loaded statically, (as one file), tracing is switched off by default.
--- So tracing must be switched on manually in your mission if you are using Moose statically.
--- When moose is loading dynamically (for moose class development), tracing is switched on by default.
--- @param #BASE self
--- @param #boolean TraceOnOff Switch the tracing on or off.
--- @usage
--- -- Switch the tracing On
--- BASE:TraceOnOff( true )
---
--- -- Switch the tracing Off
--- BASE:TraceOnOff( false )
-function BASE:TraceOnOff( TraceOnOff )
- _TraceOnOff = TraceOnOff
-end
-
-
---- Enquires if tracing is on (for the class).
--- @param #BASE self
--- @return #boolean
-function BASE:IsTrace()
-
- if debug and ( _TraceAll == true ) or ( _TraceClass[self.ClassName] or _TraceClassMethod[self.ClassName] ) then
- return true
- else
- return false
- end
-end
-
---- Set trace level
--- @param #BASE self
--- @param #number Level
-function BASE:TraceLevel( Level )
- _TraceLevel = Level
- self:E( "Tracing level " .. Level )
-end
-
---- Trace all methods in MOOSE
--- @param #BASE self
--- @param #boolean TraceAll true = trace all methods in MOOSE.
-function BASE:TraceAll( TraceAll )
-
- _TraceAll = TraceAll
-
- if _TraceAll then
- self:E( "Tracing all methods in MOOSE " )
- else
- self:E( "Switched off tracing all methods in MOOSE" )
- end
-end
-
---- Set tracing for a class
--- @param #BASE self
--- @param #string Class
-function BASE:TraceClass( Class )
- _TraceClass[Class] = true
- _TraceClassMethod[Class] = {}
- self:E( "Tracing class " .. Class )
-end
-
---- Set tracing for a specific method of class
--- @param #BASE self
--- @param #string Class
--- @param #string Method
-function BASE:TraceClassMethod( Class, Method )
- if not _TraceClassMethod[Class] then
- _TraceClassMethod[Class] = {}
- _TraceClassMethod[Class].Method = {}
- end
- _TraceClassMethod[Class].Method[Method] = true
- self:E( "Tracing method " .. Method .. " of class " .. Class )
-end
-
---- Trace a function call. This function is private.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:_F( Arguments, DebugInfoCurrentParam, DebugInfoFromParam )
-
- if debug and ( _TraceAll == true ) or ( _TraceClass[self.ClassName] or _TraceClassMethod[self.ClassName] ) then
-
- local DebugInfoCurrent = DebugInfoCurrentParam and DebugInfoCurrentParam or debug.getinfo( 2, "nl" )
- local DebugInfoFrom = DebugInfoFromParam and DebugInfoFromParam or debug.getinfo( 3, "l" )
-
- local Function = "function"
- if DebugInfoCurrent.name then
- Function = DebugInfoCurrent.name
- end
-
- if _TraceAll == true or _TraceClass[self.ClassName] or _TraceClassMethod[self.ClassName].Method[Function] then
- local LineCurrent = 0
- if DebugInfoCurrent.currentline then
- LineCurrent = DebugInfoCurrent.currentline
- end
- local LineFrom = 0
- if DebugInfoFrom then
- LineFrom = DebugInfoFrom.currentline
- end
- env.info( string.format( "%6d(%6d)/%1s:%20s%05d.%s(%s)" , LineCurrent, LineFrom, "F", self.ClassName, self.ClassID, Function, routines.utils.oneLineSerialize( Arguments ) ) )
- end
- end
-end
-
---- Trace a function call. Must be at the beginning of the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:F( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 1 then
- self:_F( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
-
---- Trace a function call level 2. Must be at the beginning of the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:F2( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 2 then
- self:_F( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
---- Trace a function call level 3. Must be at the beginning of the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:F3( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 3 then
- self:_F( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
---- Trace a function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:_T( Arguments, DebugInfoCurrentParam, DebugInfoFromParam )
-
- if debug and ( _TraceAll == true ) or ( _TraceClass[self.ClassName] or _TraceClassMethod[self.ClassName] ) then
-
- local DebugInfoCurrent = DebugInfoCurrentParam and DebugInfoCurrentParam or debug.getinfo( 2, "nl" )
- local DebugInfoFrom = DebugInfoFromParam and DebugInfoFromParam or debug.getinfo( 3, "l" )
-
- local Function = "function"
- if DebugInfoCurrent.name then
- Function = DebugInfoCurrent.name
- end
-
- if _TraceAll == true or _TraceClass[self.ClassName] or _TraceClassMethod[self.ClassName].Method[Function] then
- local LineCurrent = 0
- if DebugInfoCurrent.currentline then
- LineCurrent = DebugInfoCurrent.currentline
- end
- local LineFrom = 0
- if DebugInfoFrom then
- LineFrom = DebugInfoFrom.currentline
- end
- env.info( string.format( "%6d(%6d)/%1s:%20s%05d.%s" , LineCurrent, LineFrom, "T", self.ClassName, self.ClassID, routines.utils.oneLineSerialize( Arguments ) ) )
- end
- end
-end
-
---- Trace a function logic level 1. Can be anywhere within the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:T( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 1 then
- self:_T( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
-
---- Trace a function logic level 2. Can be anywhere within the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:T2( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 2 then
- self:_T( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
---- Trace a function logic level 3. Can be anywhere within the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:T3( Arguments )
-
- if debug and _TraceOnOff then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- if _TraceLevel >= 3 then
- self:_T( Arguments, DebugInfoCurrent, DebugInfoFrom )
- end
- end
-end
-
---- Log an exception which will be traced always. Can be anywhere within the function logic.
--- @param #BASE self
--- @param Arguments A #table or any field.
-function BASE:E( Arguments )
-
- if debug then
- local DebugInfoCurrent = debug.getinfo( 2, "nl" )
- local DebugInfoFrom = debug.getinfo( 3, "l" )
-
- local Function = "function"
- if DebugInfoCurrent.name then
- Function = DebugInfoCurrent.name
- end
-
- local LineCurrent = DebugInfoCurrent.currentline
- local LineFrom = -1
- if DebugInfoFrom then
- LineFrom = DebugInfoFrom.currentline
- end
-
- env.info( string.format( "%6d(%6d)/%1s:%20s%05d.%s(%s)" , LineCurrent, LineFrom, "E", self.ClassName, self.ClassID, Function, routines.utils.oneLineSerialize( Arguments ) ) )
- end
-
-end
-
-
-
---- **Core** - SCHEDULER prepares and handles the **execution of functions over scheduled time (intervals)**.
---
--- 
---
--- ===
---
--- # 1) @{Scheduler#SCHEDULER} class, extends @{Base#BASE}
---
--- The @{Scheduler#SCHEDULER} class creates schedule.
---
--- ## 1.1) SCHEDULER constructor
---
--- The SCHEDULER class is quite easy to use, but note that the New constructor has variable parameters:
---
--- * @{Scheduler#SCHEDULER.New}( nil ): Setup a new SCHEDULER object, which is persistently executed after garbage collection.
--- * @{Scheduler#SCHEDULER.New}( Object ): Setup a new SCHEDULER object, which is linked to the Object. When the Object is nillified or destroyed, the SCHEDULER object will also be destroyed and stopped after garbage collection.
--- * @{Scheduler#SCHEDULER.New}( nil, Function, FunctionArguments, Start, ... ): Setup a new persistent SCHEDULER object, and start a new schedule for the Function with the defined FunctionArguments according the Start and sequent parameters.
--- * @{Scheduler#SCHEDULER.New}( Object, Function, FunctionArguments, Start, ... ): Setup a new SCHEDULER object, linked to Object, and start a new schedule for the Function with the defined FunctionArguments according the Start and sequent parameters.
---
--- ## 1.2) SCHEDULER timer stopping and (re-)starting.
---
--- The SCHEDULER can be stopped and restarted with the following methods:
---
--- * @{Scheduler#SCHEDULER.Start}(): (Re-)Start the schedules within the SCHEDULER object. If a CallID is provided to :Start(), only the schedule referenced by CallID will be (re-)started.
--- * @{Scheduler#SCHEDULER.Stop}(): Stop the schedules within the SCHEDULER object. If a CallID is provided to :Stop(), then only the schedule referenced by CallID will be stopped.
---
--- ## 1.3) Create a new schedule
---
--- With @{Scheduler#SCHEDULER.Schedule}() a new time event can be scheduled. This function is used by the :New() constructor when a new schedule is planned.
---
--- ===
---
--- ### Contributions:
---
--- * FlightControl : Concept & Testing
---
--- ### Authors:
---
--- * FlightControl : Design & Programming
---
--- ### Test Missions:
---
--- * SCH - Scheduler
---
--- ===
---
--- @module Scheduler
-
-
---- The SCHEDULER class
--- @type SCHEDULER
--- @field #number ScheduleID the ID of the scheduler.
--- @extends Core.Base#BASE
-SCHEDULER = {
- ClassName = "SCHEDULER",
- Schedules = {},
-}
-
---- SCHEDULER constructor.
--- @param #SCHEDULER self
--- @param #table SchedulerObject Specified for which Moose object the timer is setup. If a value of nil is provided, a scheduler will be setup without an object reference.
--- @param #function SchedulerFunction The event function to be called when a timer event occurs. The event function needs to accept the parameters specified in SchedulerArguments.
--- @param #table SchedulerArguments Optional arguments that can be given as part of scheduler. The arguments need to be given as a table { param1, param 2, ... }.
--- @param #number Start Specifies the amount of seconds that will be waited before the scheduling is started, and the event function is called.
--- @param #number Repeat Specifies the interval in seconds when the scheduler will call the event function.
--- @param #number RandomizeFactor Specifies a randomization factor between 0 and 1 to randomize the Repeat.
--- @param #number Stop Specifies the amount of seconds when the scheduler will be stopped.
--- @return #SCHEDULER self.
--- @return #number The ScheduleID of the planned schedule.
-function SCHEDULER:New( SchedulerObject, SchedulerFunction, SchedulerArguments, Start, Repeat, RandomizeFactor, Stop )
- local self = BASE:Inherit( self, BASE:New() )
- self:F2( { Start, Repeat, RandomizeFactor, Stop } )
-
- local ScheduleID = nil
-
- self.MasterObject = SchedulerObject
-
- if SchedulerFunction then
- ScheduleID = self:Schedule( SchedulerObject, SchedulerFunction, SchedulerArguments, Start, Repeat, RandomizeFactor, Stop )
- end
-
- return self, ScheduleID
-end
-
---function SCHEDULER:_Destructor()
--- --self:E("_Destructor")
---
--- _SCHEDULEDISPATCHER:RemoveSchedule( self.CallID )
---end
-
---- Schedule a new time event. Note that the schedule will only take place if the scheduler is *started*. Even for a single schedule event, the scheduler needs to be started also.
--- @param #SCHEDULER self
--- @param #table SchedulerObject Specified for which Moose object the timer is setup. If a value of nil is provided, a scheduler will be setup without an object reference.
--- @param #function SchedulerFunction The event function to be called when a timer event occurs. The event function needs to accept the parameters specified in SchedulerArguments.
--- @param #table SchedulerArguments Optional arguments that can be given as part of scheduler. The arguments need to be given as a table { param1, param 2, ... }.
--- @param #number Start Specifies the amount of seconds that will be waited before the scheduling is started, and the event function is called.
--- @param #number Repeat Specifies the interval in seconds when the scheduler will call the event function.
--- @param #number RandomizeFactor Specifies a randomization factor between 0 and 1 to randomize the Repeat.
--- @param #number Stop Specifies the amount of seconds when the scheduler will be stopped.
--- @return #number The ScheduleID of the planned schedule.
-function SCHEDULER:Schedule( SchedulerObject, SchedulerFunction, SchedulerArguments, Start, Repeat, RandomizeFactor, Stop )
- self:F2( { Start, Repeat, RandomizeFactor, Stop } )
- self:T3( { SchedulerArguments } )
-
- local ObjectName = "-"
- if SchedulerObject and SchedulerObject.ClassName and SchedulerObject.ClassID then
- ObjectName = SchedulerObject.ClassName .. SchedulerObject.ClassID
- end
- self:F3( { "Schedule :", ObjectName, tostring( SchedulerObject ), Start, Repeat, RandomizeFactor, Stop } )
- self.SchedulerObject = SchedulerObject
-
- local ScheduleID = _SCHEDULEDISPATCHER:AddSchedule(
- self,
- SchedulerFunction,
- SchedulerArguments,
- Start,
- Repeat,
- RandomizeFactor,
- Stop
- )
-
- self.Schedules[#self.Schedules+1] = ScheduleID
-
- return ScheduleID
-end
-
---- (Re-)Starts the schedules or a specific schedule if a valid ScheduleID is provided.
--- @param #SCHEDULER self
--- @param #number ScheduleID (optional) The ScheduleID of the planned (repeating) schedule.
-function SCHEDULER:Start( ScheduleID )
- self:F3( { ScheduleID } )
-
- _SCHEDULEDISPATCHER:Start( self, ScheduleID )
-end
-
---- Stops the schedules or a specific schedule if a valid ScheduleID is provided.
--- @param #SCHEDULER self
--- @param #number ScheduleID (optional) The ScheduleID of the planned (repeating) schedule.
-function SCHEDULER:Stop( ScheduleID )
- self:F3( { ScheduleID } )
-
- _SCHEDULEDISPATCHER:Stop( self, ScheduleID )
-end
-
---- Removes a specific schedule if a valid ScheduleID is provided.
--- @param #SCHEDULER self
--- @param #number ScheduleID (optional) The ScheduleID of the planned (repeating) schedule.
-function SCHEDULER:Remove( ScheduleID )
- self:F3( { ScheduleID } )
-
- _SCHEDULEDISPATCHER:Remove( self, ScheduleID )
-end
-
---- Clears all pending schedules.
--- @param #SCHEDULER self
-function SCHEDULER:Clear()
- self:F3( )
-
- _SCHEDULEDISPATCHER:Clear( self )
-end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
---- This module defines the SCHEDULEDISPATCHER class, which is used by a central object called _SCHEDULEDISPATCHER.
---
--- ===
---
--- Takes care of the creation and dispatching of scheduled functions for SCHEDULER objects.
---
--- This class is tricky and needs some thorought explanation.
--- SCHEDULE classes are used to schedule functions for objects, or as persistent objects.
--- The SCHEDULEDISPATCHER class ensures that:
---
--- - Scheduled functions are planned according the SCHEDULER object parameters.
--- - Scheduled functions are repeated when requested, according the SCHEDULER object parameters.
--- - Scheduled functions are automatically removed when the schedule is finished, according the SCHEDULER object parameters.
---
--- The SCHEDULEDISPATCHER class will manage SCHEDULER object in memory during garbage collection:
--- - When a SCHEDULER object is not attached to another object (that is, it's first :Schedule() parameter is nil), then the SCHEDULER
--- object is _persistent_ within memory.
--- - When a SCHEDULER object *is* attached to another object, then the SCHEDULER object is _not persistent_ within memory after a garbage collection!
--- The none persistency of SCHEDULERS attached to objects is required to allow SCHEDULER objects to be garbage collectged, when the parent object is also desroyed or nillified and garbage collected.
--- Even when there are pending timer scheduled functions to be executed for the SCHEDULER object,
--- these will not be executed anymore when the SCHEDULER object has been destroyed.
---
--- The SCHEDULEDISPATCHER allows multiple scheduled functions to be planned and executed for one SCHEDULER object.
--- The SCHEDULER object therefore keeps a table of "CallID's", which are returned after each planning of a new scheduled function by the SCHEDULEDISPATCHER.
--- The SCHEDULER object plans new scheduled functions through the @{Scheduler#SCHEDULER.Schedule}() method.
--- The Schedule() method returns the CallID that is the reference ID for each planned schedule.
---
--- ===
---
--- ===
---
--- ### Contributions: -
--- ### Authors: FlightControl : Design & Programming
---
--- @module ScheduleDispatcher
-
---- The SCHEDULEDISPATCHER structure
--- @type SCHEDULEDISPATCHER
-SCHEDULEDISPATCHER = {
- ClassName = "SCHEDULEDISPATCHER",
- CallID = 0,
-}
-
-function SCHEDULEDISPATCHER:New()
- local self = BASE:Inherit( self, BASE:New() )
- self:F3()
- return self
-end
-
---- Add a Schedule to the ScheduleDispatcher.
--- The development of this method was really tidy.
--- It is constructed as such that a garbage collection is executed on the weak tables, when the Scheduler is nillified.
--- Nothing of this code should be modified without testing it thoroughly.
--- @param #SCHEDULEDISPATCHER self
--- @param Core.Scheduler#SCHEDULER Scheduler
-function SCHEDULEDISPATCHER:AddSchedule( Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop )
- self:F2( { Scheduler, ScheduleFunction, ScheduleArguments, Start, Repeat, Randomize, Stop } )
-
- self.CallID = self.CallID + 1
-
- -- Initialize the ObjectSchedulers array, which is a weakly coupled table.
- -- If the object used as the key is nil, then the garbage collector will remove the item from the Functions array.
- self.PersistentSchedulers = self.PersistentSchedulers or {}
-
- -- Initialize the ObjectSchedulers array, which is a weakly coupled table.
- -- If the object used as the key is nil, then the garbage collector will remove the item from the Functions array.
- self.ObjectSchedulers = self.ObjectSchedulers or setmetatable( {}, { __mode = "v" } ) -- or {}
-
- if Scheduler.MasterObject then
- self.ObjectSchedulers[self.CallID] = Scheduler
- self:F3( { CallID = self.CallID, ObjectScheduler = tostring(self.ObjectSchedulers[self.CallID]), MasterObject = tostring(Scheduler.MasterObject) } )
- else
- self.PersistentSchedulers[self.CallID] = Scheduler
- self:F3( { CallID = self.CallID, PersistentScheduler = self.PersistentSchedulers[self.CallID] } )
- end
-
- self.Schedule = self.Schedule or setmetatable( {}, { __mode = "k" } )
- self.Schedule[Scheduler] = self.Schedule[Scheduler] or {}
- self.Schedule[Scheduler][self.CallID] = {}
- self.Schedule[Scheduler][self.CallID].Function = ScheduleFunction
- self.Schedule[Scheduler][self.CallID].Arguments = ScheduleArguments
- self.Schedule[Scheduler][self.CallID].StartTime = timer.getTime() + ( Start or 0 )
- self.Schedule[Scheduler][self.CallID].Start = Start + .1
- self.Schedule[Scheduler][self.CallID].Repeat = Repeat
- self.Schedule[Scheduler][self.CallID].Randomize = Randomize
- self.Schedule[Scheduler][self.CallID].Stop = Stop
-
- self:T3( self.Schedule[Scheduler][self.CallID] )
-
- self.Schedule[Scheduler][self.CallID].CallHandler = function( CallID )
- self:F2( CallID )
-
- local ErrorHandler = function( errmsg )
- env.info( "Error in timer function: " .. errmsg )
- if debug ~= nil then
- env.info( debug.traceback() )
- end
- return errmsg
- end
-
- local Scheduler = self.ObjectSchedulers[CallID]
- if not Scheduler then
- Scheduler = self.PersistentSchedulers[CallID]
- end
-
- self:T3( { Scheduler = Scheduler } )
-
- if Scheduler then
-
- local Schedule = self.Schedule[Scheduler][CallID]
-
- self:T3( { Schedule = Schedule } )
-
- local ScheduleObject = Scheduler.SchedulerObject
- --local ScheduleObjectName = Scheduler.SchedulerObject:GetNameAndClassID()
- local ScheduleFunction = Schedule.Function
- local ScheduleArguments = Schedule.Arguments
- local Start = Schedule.Start
- local Repeat = Schedule.Repeat or 0
- local Randomize = Schedule.Randomize or 0
- local Stop = Schedule.Stop or 0
- local ScheduleID = Schedule.ScheduleID
-
- local Status, Result
- if ScheduleObject then
- local function Timer()
- return ScheduleFunction( ScheduleObject, unpack( ScheduleArguments ) )
- end
- Status, Result = xpcall( Timer, ErrorHandler )
- else
- local function Timer()
- return ScheduleFunction( unpack( ScheduleArguments ) )
- end
- Status, Result = xpcall( Timer, ErrorHandler )
- end
-
- local CurrentTime = timer.getTime()
- local StartTime = CurrentTime + Start
-
- if Status and (( Result == nil ) or ( Result and Result ~= false ) ) then
- if Repeat ~= 0 and ( Stop == 0 ) or ( Stop ~= 0 and CurrentTime <= StartTime + Stop ) then
- local ScheduleTime =
- CurrentTime +
- Repeat +
- math.random(
- - ( Randomize * Repeat / 2 ),
- ( Randomize * Repeat / 2 )
- ) +
- 0.01
- self:T3( { Repeat = CallID, CurrentTime, ScheduleTime, ScheduleArguments } )
- return ScheduleTime -- returns the next time the function needs to be called.
- else
- self:Stop( Scheduler, CallID )
- end
- else
- self:Stop( Scheduler, CallID )
- end
- else
- self:E( "Scheduled obscolete call for CallID: " .. CallID )
- end
-
- return nil
- end
-
- self:Start( Scheduler, self.CallID )
-
- return self.CallID
-end
-
-function SCHEDULEDISPATCHER:RemoveSchedule( Scheduler, CallID )
- self:F2( { Remove = CallID, Scheduler = Scheduler } )
-
- if CallID then
- self:Stop( Scheduler, CallID )
- self.Schedule[Scheduler][CallID] = nil
- end
-end
-
-function SCHEDULEDISPATCHER:Start( Scheduler, CallID )
- self:F2( { Start = CallID, Scheduler = Scheduler } )
-
- if CallID then
- local Schedule = self.Schedule[Scheduler]
- -- Only start when there is no ScheduleID defined!
- -- This prevents to "Start" the scheduler twice with the same CallID...
- if not Schedule[CallID].ScheduleID then
- Schedule[CallID].ScheduleID = timer.scheduleFunction(
- Schedule[CallID].CallHandler,
- CallID,
- timer.getTime() + Schedule[CallID].Start
- )
- end
- else
- for CallID, Schedule in pairs( self.Schedule[Scheduler] ) do
- self:Start( Scheduler, CallID ) -- Recursive
- end
- end
-end
-
-function SCHEDULEDISPATCHER:Stop( Scheduler, CallID )
- self:F2( { Stop = CallID, Scheduler = Scheduler } )
-
- if CallID then
- local Schedule = self.Schedule[Scheduler]
- -- Only stop when there is a ScheduleID defined for the CallID.
- -- So, when the scheduler was stopped before, do nothing.
- if Schedule[CallID].ScheduleID then
- timer.removeFunction( Schedule[CallID].ScheduleID )
- Schedule[CallID].ScheduleID = nil
- end
- else
- for CallID, Schedule in pairs( self.Schedule[Scheduler] ) do
- self:Stop( Scheduler, CallID ) -- Recursive
- end
- end
-end
-
-function SCHEDULEDISPATCHER:Clear( Scheduler )
- self:F2( { Scheduler = Scheduler } )
-
- for CallID, Schedule in pairs( self.Schedule[Scheduler] ) do
- self:Stop( Scheduler, CallID ) -- Recursive
- end
-end
-
-
-
---- **Core** - EVENT models DCS **event dispatching** using a **publish-subscribe** model.
---
--- 
---
--- ===
---
--- # 1) Event Handling Overview
---
--- 
---
--- Within a running mission, various DCS events occur. Units are dynamically created, crash, die, shoot stuff, get hit etc.
--- This module provides a mechanism to dispatch those events occuring within your running mission, to the different objects orchestrating your mission.
---
--- 
---
--- Objects can subscribe to different events. The Event dispatcher will publish the received DCS events to the subscribed MOOSE objects, in a specified order.
--- In this way, the subscribed MOOSE objects are kept in sync with your evolving running mission.
---
--- ## 1.1) Event Dispatching
---
--- 
---
--- The _EVENTDISPATCHER object is automatically created within MOOSE,
--- and handles the dispatching of DCS Events occurring
--- in the simulator to the subscribed objects
--- in the correct processing order.
---
--- 
---
--- There are 5 levels of kind of objects that the _EVENTDISPATCHER services:
---
--- * _DATABASE object: The core of the MOOSE objects. Any object that is created, deleted or updated, is done in this database.
--- * SET_ derived classes: Subsets of the _DATABASE object. These subsets are updated by the _EVENTDISPATCHER as the second priority.
--- * UNIT objects: UNIT objects can subscribe to DCS events. Each DCS event will be directly published to teh subscribed UNIT object.
--- * GROUP objects: GROUP objects can subscribe to DCS events. Each DCS event will be directly published to the subscribed GROUP object.
--- * Any other object: Various other objects can subscribe to DCS events. Each DCS event triggered will be published to each subscribed object.
---
--- 
---
--- For most DCS events, the above order of updating will be followed.
---
--- 
---
--- But for some DCS events, the publishing order is reversed. This is due to the fact that objects need to be **erased** instead of added.
---
--- ## 1.2) Event Handling
---
--- 
---
--- The actual event subscribing and handling is not facilitated through the _EVENTDISPATCHER, but it is done through the @{BASE} class, @{UNIT} class and @{GROUP} class.
--- The _EVENTDISPATCHER is a component that is quietly working in the background of MOOSE.
---
--- 
---
--- The BASE class provides methods to catch DCS Events. These are events that are triggered from within the DCS simulator,
--- and handled through lua scripting. MOOSE provides an encapsulation to handle these events more efficiently.
---
--- ### 1.2.1 Subscribe / Unsubscribe to DCS Events
---
--- At first, the mission designer will need to **Subscribe** to a specific DCS event for the class.
--- So, when the DCS event occurs, the class will be notified of that event.
--- There are two functions which you use to subscribe to or unsubscribe from an event.
---
--- * @{Base#BASE.HandleEvent}(): Subscribe to a DCS Event.
--- * @{Base#BASE.UnHandleEvent}(): Unsubscribe from a DCS Event.
---
--- Note that for a UNIT, the event will be handled **for that UNIT only**!
--- Note that for a GROUP, the event will be handled **for all the UNITs in that GROUP only**!
---
--- For all objects of other classes, the subscribed events will be handled for **all UNITs within the Mission**!
--- So if a UNIT within the mission has the subscribed event for that object,
--- then the object event handler will receive the event for that UNIT!
---
--- ### 1.3.2 Event Handling of DCS Events
---
--- Once the class is subscribed to the event, an **Event Handling** method on the object or class needs to be written that will be called
--- when the DCS event occurs. The Event Handling method receives an @{Event#EVENTDATA} structure, which contains a lot of information
--- about the event that occurred.
---
--- Find below an example of the prototype how to write an event handling function for two units:
---
--- local Tank1 = UNIT:FindByName( "Tank A" )
--- local Tank2 = UNIT:FindByName( "Tank B" )
---
--- -- Here we subscribe to the Dead events. So, if one of these tanks dies, the Tank1 or Tank2 objects will be notified.
--- Tank1:HandleEvent( EVENTS.Dead )
--- Tank2:HandleEvent( EVENTS.Dead )
---
--- --- This function is an Event Handling function that will be called when Tank1 is Dead.
--- -- @param Wrapper.Unit#UNIT self
--- -- @param Core.Event#EVENTDATA EventData
--- function Tank1:OnEventDead( EventData )
---
--- self:SmokeGreen()
--- end
---
--- --- This function is an Event Handling function that will be called when Tank2 is Dead.
--- -- @param Wrapper.Unit#UNIT self
--- -- @param Core.Event#EVENTDATA EventData
--- function Tank2:OnEventDead( EventData )
---
--- self:SmokeBlue()
--- end
---
--- ### 1.3.3 Event Handling methods that are automatically called upon subscribed DCS events
---
--- 
---
--- The following list outlines which EVENTS item in the structure corresponds to which Event Handling method.
--- Always ensure that your event handling methods align with the events being subscribed to, or nothing will be executed.
---
--- # 2) EVENTS type
---
--- The EVENTS structure contains names for all the different DCS events that objects can subscribe to using the
--- @{Base#BASE.HandleEvent}() method.
---
--- # 3) EVENTDATA type
---
--- The @{Event#EVENTDATA} structure contains all the fields that are populated with event information before
--- an Event Handler method is being called by the event dispatcher.
--- The Event Handler received the EVENTDATA object as a parameter, and can be used to investigate further the different events.
--- There are basically 4 main categories of information stored in the EVENTDATA structure:
---
--- * Initiator Unit data: Several fields documenting the initiator unit related to the event.
--- * Target Unit data: Several fields documenting the target unit related to the event.
--- * Weapon data: Certain events populate weapon information.
--- * Place data: Certain events populate place information.
---
--- --- This function is an Event Handling function that will be called when Tank1 is Dead.
--- -- EventData is an EVENTDATA structure.
--- -- We use the EventData.IniUnit to smoke the tank Green.
--- -- @param Wrapper.Unit#UNIT self
--- -- @param Core.Event#EVENTDATA EventData
--- function Tank1:OnEventDead( EventData )
---
--- EventData.IniUnit:SmokeGreen()
--- end
---
---
--- Find below an overview which events populate which information categories:
---
--- 
---
--- **IMPORTANT NOTE:** Some events can involve not just UNIT objects, but also STATIC objects!!!
--- In that case the initiator or target unit fields will refer to a STATIC object!
--- In case a STATIC object is involved, the documentation indicates which fields will and won't not be populated.
--- The fields **IniObjectCategory** and **TgtObjectCategory** contain the indicator which **kind of object is involved** in the event.
--- You can use the enumerator **Object.Category.UNIT** and **Object.Category.STATIC** to check on IniObjectCategory and TgtObjectCategory.
--- Example code snippet:
---
--- if Event.IniObjectCategory == Object.Category.UNIT then
--- ...
--- end
--- if Event.IniObjectCategory == Object.Category.STATIC then
--- ...
--- end
---
--- When a static object is involved in the event, the Group and Player fields won't be populated.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- YYYY-MM-DD: CLASS:**NewFunction**( Params ) replaces CLASS:_OldFunction_( Params )
--- YYYY-MM-DD: CLASS:**NewFunction( Params )** added
---
--- Hereby the change log:
---
--- * 2017-03-07: Added the correct event dispatching in case the event is subscribed by a GROUP.
---
--- * 2017-02-07: Did a complete revision of the Event Handing API and underlying mechanisms.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- ### Authors:
---
--- * [**FlightControl**](https://forums.eagle.ru/member.php?u=89536): Design & Programming & documentation.
---
--- @module Event
-
-
---- The EVENT structure
--- @type EVENT
--- @field #EVENT.Events Events
--- @extends Core.Base#BASE
-EVENT = {
- ClassName = "EVENT",
- ClassID = 0,
-}
-
---- The different types of events supported by MOOSE.
--- Use this structure to subscribe to events using the @{Base#BASE.HandleEvent}() method.
--- @type EVENTS
-EVENTS = {
- Shot = world.event.S_EVENT_SHOT,
- Hit = world.event.S_EVENT_HIT,
- Takeoff = world.event.S_EVENT_TAKEOFF,
- Land = world.event.S_EVENT_LAND,
- Crash = world.event.S_EVENT_CRASH,
- Ejection = world.event.S_EVENT_EJECTION,
- Refueling = world.event.S_EVENT_REFUELING,
- Dead = world.event.S_EVENT_DEAD,
- PilotDead = world.event.S_EVENT_PILOT_DEAD,
- BaseCaptured = world.event.S_EVENT_BASE_CAPTURED,
- MissionStart = world.event.S_EVENT_MISSION_START,
- MissionEnd = world.event.S_EVENT_MISSION_END,
- TookControl = world.event.S_EVENT_TOOK_CONTROL,
- RefuelingStop = world.event.S_EVENT_REFUELING_STOP,
- Birth = world.event.S_EVENT_BIRTH,
- HumanFailure = world.event.S_EVENT_HUMAN_FAILURE,
- EngineStartup = world.event.S_EVENT_ENGINE_STARTUP,
- EngineShutdown = world.event.S_EVENT_ENGINE_SHUTDOWN,
- PlayerEnterUnit = world.event.S_EVENT_PLAYER_ENTER_UNIT,
- PlayerLeaveUnit = world.event.S_EVENT_PLAYER_LEAVE_UNIT,
- PlayerComment = world.event.S_EVENT_PLAYER_COMMENT,
- ShootingStart = world.event.S_EVENT_SHOOTING_START,
- ShootingEnd = world.event.S_EVENT_SHOOTING_END,
-}
-
---- The Event structure
--- Note that at the beginning of each field description, there is an indication which field will be populated depending on the object type involved in the Event:
---
--- * A (Object.Category.)UNIT : A UNIT object type is involved in the Event.
--- * A (Object.Category.)STATIC : A STATIC object type is involved in the Event.µ
---
--- @type EVENTDATA
--- @field #number id The identifier of the event.
---
--- @field Dcs.DCSUnit#Unit initiator (UNIT/STATIC/SCENERY) The initiating @{Dcs.DCSUnit#Unit} or @{Dcs.DCSStaticObject#StaticObject}.
--- @field Dcs.DCSObject#Object.Category IniObjectCategory (UNIT/STATIC/SCENERY) The initiator object category ( Object.Category.UNIT or Object.Category.STATIC ).
--- @field Dcs.DCSUnit#Unit IniDCSUnit (UNIT/STATIC) The initiating @{DCSUnit#Unit} or @{DCSStaticObject#StaticObject}.
--- @field #string IniDCSUnitName (UNIT/STATIC) The initiating Unit name.
--- @field Wrapper.Unit#UNIT IniUnit (UNIT/STATIC) The initiating MOOSE wrapper @{Unit#UNIT} of the initiator Unit object.
--- @field #string IniUnitName (UNIT/STATIC) The initiating UNIT name (same as IniDCSUnitName).
--- @field Dcs.DCSGroup#Group IniDCSGroup (UNIT) The initiating {DCSGroup#Group}.
--- @field #string IniDCSGroupName (UNIT) The initiating Group name.
--- @field Wrapper.Group#GROUP IniGroup (UNIT) The initiating MOOSE wrapper @{Group#GROUP} of the initiator Group object.
--- @field #string IniGroupName UNIT) The initiating GROUP name (same as IniDCSGroupName).
--- @field #string IniPlayerName (UNIT) The name of the initiating player in case the Unit is a client or player slot.
--- @field Dcs.DCScoalition#coalition.side IniCoalition (UNIT) The coalition of the initiator.
--- @field Dcs.DCSUnit#Unit.Category IniCategory (UNIT) The category of the initiator.
--- @field #string IniTypeName (UNIT) The type name of the initiator.
---
--- @field Dcs.DCSUnit#Unit target (UNIT/STATIC) The target @{Dcs.DCSUnit#Unit} or @{DCSStaticObject#StaticObject}.
--- @field Dcs.DCSObject#Object.Category TgtObjectCategory (UNIT/STATIC) The target object category ( Object.Category.UNIT or Object.Category.STATIC ).
--- @field Dcs.DCSUnit#Unit TgtDCSUnit (UNIT/STATIC) The target @{DCSUnit#Unit} or @{DCSStaticObject#StaticObject}.
--- @field #string TgtDCSUnitName (UNIT/STATIC) The target Unit name.
--- @field Wrapper.Unit#UNIT TgtUnit (UNIT/STATIC) The target MOOSE wrapper @{Unit#UNIT} of the target Unit object.
--- @field #string TgtUnitName (UNIT/STATIC) The target UNIT name (same as TgtDCSUnitName).
--- @field Dcs.DCSGroup#Group TgtDCSGroup (UNIT) The target {DCSGroup#Group}.
--- @field #string TgtDCSGroupName (UNIT) The target Group name.
--- @field Wrapper.Group#GROUP TgtGroup (UNIT) The target MOOSE wrapper @{Group#GROUP} of the target Group object.
--- @field #string TgtGroupName (UNIT) The target GROUP name (same as TgtDCSGroupName).
--- @field #string TgtPlayerName (UNIT) The name of the target player in case the Unit is a client or player slot.
--- @field Dcs.DCScoalition#coalition.side TgtCoalition (UNIT) The coalition of the target.
--- @field Dcs.DCSUnit#Unit.Category TgtCategory (UNIT) The category of the target.
--- @field #string TgtTypeName (UNIT) The type name of the target.
---
--- @field weapon The weapon used during the event.
--- @field Weapon
--- @field WeaponName
--- @field WeaponTgtDCSUnit
-
-
-local _EVENTMETA = {
- [world.event.S_EVENT_SHOT] = {
- Order = 1,
- Event = "OnEventShot",
- Text = "S_EVENT_SHOT"
- },
- [world.event.S_EVENT_HIT] = {
- Order = 1,
- Event = "OnEventHit",
- Text = "S_EVENT_HIT"
- },
- [world.event.S_EVENT_TAKEOFF] = {
- Order = 1,
- Event = "OnEventTakeoff",
- Text = "S_EVENT_TAKEOFF"
- },
- [world.event.S_EVENT_LAND] = {
- Order = 1,
- Event = "OnEventLand",
- Text = "S_EVENT_LAND"
- },
- [world.event.S_EVENT_CRASH] = {
- Order = -1,
- Event = "OnEventCrash",
- Text = "S_EVENT_CRASH"
- },
- [world.event.S_EVENT_EJECTION] = {
- Order = 1,
- Event = "OnEventEjection",
- Text = "S_EVENT_EJECTION"
- },
- [world.event.S_EVENT_REFUELING] = {
- Order = 1,
- Event = "OnEventRefueling",
- Text = "S_EVENT_REFUELING"
- },
- [world.event.S_EVENT_DEAD] = {
- Order = -1,
- Event = "OnEventDead",
- Text = "S_EVENT_DEAD"
- },
- [world.event.S_EVENT_PILOT_DEAD] = {
- Order = 1,
- Event = "OnEventPilotDead",
- Text = "S_EVENT_PILOT_DEAD"
- },
- [world.event.S_EVENT_BASE_CAPTURED] = {
- Order = 1,
- Event = "OnEventBaseCaptured",
- Text = "S_EVENT_BASE_CAPTURED"
- },
- [world.event.S_EVENT_MISSION_START] = {
- Order = 1,
- Event = "OnEventMissionStart",
- Text = "S_EVENT_MISSION_START"
- },
- [world.event.S_EVENT_MISSION_END] = {
- Order = 1,
- Event = "OnEventMissionEnd",
- Text = "S_EVENT_MISSION_END"
- },
- [world.event.S_EVENT_TOOK_CONTROL] = {
- Order = 1,
- Event = "OnEventTookControl",
- Text = "S_EVENT_TOOK_CONTROL"
- },
- [world.event.S_EVENT_REFUELING_STOP] = {
- Order = 1,
- Event = "OnEventRefuelingStop",
- Text = "S_EVENT_REFUELING_STOP"
- },
- [world.event.S_EVENT_BIRTH] = {
- Order = 1,
- Event = "OnEventBirth",
- Text = "S_EVENT_BIRTH"
- },
- [world.event.S_EVENT_HUMAN_FAILURE] = {
- Order = 1,
- Event = "OnEventHumanFailure",
- Text = "S_EVENT_HUMAN_FAILURE"
- },
- [world.event.S_EVENT_ENGINE_STARTUP] = {
- Order = 1,
- Event = "OnEventEngineStartup",
- Text = "S_EVENT_ENGINE_STARTUP"
- },
- [world.event.S_EVENT_ENGINE_SHUTDOWN] = {
- Order = 1,
- Event = "OnEventEngineShutdown",
- Text = "S_EVENT_ENGINE_SHUTDOWN"
- },
- [world.event.S_EVENT_PLAYER_ENTER_UNIT] = {
- Order = 1,
- Event = "OnEventPlayerEnterUnit",
- Text = "S_EVENT_PLAYER_ENTER_UNIT"
- },
- [world.event.S_EVENT_PLAYER_LEAVE_UNIT] = {
- Order = -1,
- Event = "OnEventPlayerLeaveUnit",
- Text = "S_EVENT_PLAYER_LEAVE_UNIT"
- },
- [world.event.S_EVENT_PLAYER_COMMENT] = {
- Order = 1,
- Event = "OnEventPlayerComment",
- Text = "S_EVENT_PLAYER_COMMENT"
- },
- [world.event.S_EVENT_SHOOTING_START] = {
- Order = 1,
- Event = "OnEventShootingStart",
- Text = "S_EVENT_SHOOTING_START"
- },
- [world.event.S_EVENT_SHOOTING_END] = {
- Order = 1,
- Event = "OnEventShootingEnd",
- Text = "S_EVENT_SHOOTING_END"
- },
-}
-
-
---- The Events structure
--- @type EVENT.Events
--- @field #number IniUnit
-
-function EVENT:New()
- local self = BASE:Inherit( self, BASE:New() )
- self:F2()
- self.EventHandler = world.addEventHandler( self )
- return self
-end
-
-function EVENT:EventText( EventID )
-
- local EventText = _EVENTMETA[EventID].Text
-
- return EventText
-end
-
-
---- Initializes the Events structure for the event
--- @param #EVENT self
--- @param Dcs.DCSWorld#world.event EventID
--- @param Core.Base#BASE EventClass
--- @return #EVENT.Events
-function EVENT:Init( EventID, EventClass )
- self:F3( { _EVENTMETA[EventID].Text, EventClass } )
-
- if not self.Events[EventID] then
- -- Create a WEAK table to ensure that the garbage collector is cleaning the event links when the object usage is cleaned.
- self.Events[EventID] = setmetatable( {}, { __mode = "k" } )
- end
-
- -- Each event has a subtable of EventClasses, ordered by EventPriority.
- local EventPriority = EventClass:GetEventPriority()
- if not self.Events[EventID][EventPriority] then
- self.Events[EventID][EventPriority] = setmetatable( {}, { __mode = "k" } )
- end
-
- if not self.Events[EventID][EventPriority][EventClass] then
- self.Events[EventID][EventPriority][EventClass] = setmetatable( {}, { __mode = "v" } )
- end
- return self.Events[EventID][EventPriority][EventClass]
-end
-
---- Removes an Events entry
--- @param #EVENT self
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is.
--- @param Dcs.DCSWorld#world.event EventID
--- @return #EVENT.Events
-function EVENT:Remove( EventClass, EventID )
- self:F3( { EventClass, _EVENTMETA[EventID].Text } )
-
- local EventClass = EventClass
- local EventPriority = EventClass:GetEventPriority()
- self.Events[EventID][EventPriority][EventClass] = nil
-end
-
---- Removes an Events entry for a UNIT.
--- @param #EVENT self
--- @param #string UnitName The name of the UNIT.
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is.
--- @param Dcs.DCSWorld#world.event EventID
--- @return #EVENT.Events
-function EVENT:RemoveForUnit( UnitName, EventClass, EventID )
- self:F3( { EventClass, _EVENTMETA[EventID].Text } )
-
- local EventClass = EventClass
- local EventPriority = EventClass:GetEventPriority()
- local Event = self.Events[EventID][EventPriority][EventClass]
- Event.EventUnit[UnitName] = nil
-end
-
---- Removes an Events entry for a GROUP.
--- @param #EVENT self
--- @param #string GroupName The name of the GROUP.
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is.
--- @param Dcs.DCSWorld#world.event EventID
--- @return #EVENT.Events
-function EVENT:RemoveForGroup( GroupName, EventClass, EventID )
- self:F3( { EventClass, _EVENTMETA[EventID].Text } )
-
- local EventClass = EventClass
- local EventPriority = EventClass:GetEventPriority()
- local Event = self.Events[EventID][EventPriority][EventClass]
- Event.EventGroup[GroupName] = nil
-end
-
---- Clears all event subscriptions for a @{Base#BASE} derived object.
--- @param #EVENT self
--- @param Core.Base#BASE EventObject
-function EVENT:RemoveAll( EventObject )
- self:F3( { EventObject:GetClassNameAndID() } )
-
- local EventClass = EventObject:GetClassNameAndID()
- local EventPriority = EventClass:GetEventPriority()
- for EventID, EventData in pairs( self.Events ) do
- self.Events[EventID][EventPriority][EventClass] = nil
- end
-end
-
-
-
---- Create an OnDead event handler for a group
--- @param #EVENT self
--- @param #table EventTemplate
--- @param #function EventFunction The function to be called when the event occurs for the unit.
--- @param EventClass The instance of the class for which the event is.
--- @param #function OnEventFunction
--- @return #EVENT
-function EVENT:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EventID )
- self:F2( EventTemplate.name )
-
- for EventUnitID, EventUnit in pairs( EventTemplate.units ) do
- self:OnEventForUnit( EventUnit.name, EventFunction, EventClass, EventID )
- end
- return self
-end
-
---- Set a new listener for an S_EVENT_X event independent from a unit or a weapon.
--- @param #EVENT self
--- @param #function EventFunction The function to be called when the event occurs for the unit.
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is captured. When the event happens, the event process will be called in this class provided.
--- @param EventID
--- @return #EVENT
-function EVENT:OnEventGeneric( EventFunction, EventClass, EventID )
- self:F2( { EventID } )
-
- local EventData = self:Init( EventID, EventClass )
- EventData.EventFunction = EventFunction
- EventData.EventClass = EventClass
-
- return self
-end
-
-
---- Set a new listener for an S_EVENT_X event for a UNIT.
--- @param #EVENT self
--- @param #string UnitName The name of the UNIT.
--- @param #function EventFunction The function to be called when the event occurs for the GROUP.
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is.
--- @param EventID
--- @return #EVENT
-function EVENT:OnEventForUnit( UnitName, EventFunction, EventClass, EventID )
- self:F2( UnitName )
-
- local EventData = self:Init( EventID, EventClass )
- if not EventData.EventUnit then
- EventData.EventUnit = {}
- end
- EventData.EventUnit[UnitName] = {}
- EventData.EventUnit[UnitName].EventFunction = EventFunction
- EventData.EventUnit[UnitName].EventClass = EventClass
- return self
-end
-
---- Set a new listener for an S_EVENT_X event for a GROUP.
--- @param #EVENT self
--- @param #string GroupName The name of the GROUP.
--- @param #function EventFunction The function to be called when the event occurs for the GROUP.
--- @param Core.Base#BASE EventClass The self instance of the class for which the event is.
--- @param EventID
--- @return #EVENT
-function EVENT:OnEventForGroup( GroupName, EventFunction, EventClass, EventID )
- self:F2( GroupName )
-
- local Event = self:Init( EventID, EventClass )
- if not Event.EventGroup then
- Event.EventGroup = {}
- end
- Event.EventGroup[GroupName] = {}
- Event.EventGroup[GroupName].EventFunction = EventFunction
- Event.EventGroup[GroupName].EventClass = EventClass
- return self
-end
-
-do -- OnBirth
-
- --- Create an OnBirth event handler for a group
- -- @param #EVENT self
- -- @param Wrapper.Group#GROUP EventGroup
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnBirthForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.Birth )
-
- return self
- end
-
-end
-
-do -- OnCrash
-
- --- Create an OnCrash event handler for a group
- -- @param #EVENT self
- -- @param Wrapper.Group#GROUP EventGroup
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnCrashForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.Crash )
-
- return self
- end
-
-end
-
-do -- OnDead
-
- --- Create an OnDead event handler for a group
- -- @param #EVENT self
- -- @param Wrapper.Group#GROUP EventGroup
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnDeadForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.Dead )
-
- return self
- end
-
-end
-
-
-do -- OnLand
- --- Create an OnLand event handler for a group
- -- @param #EVENT self
- -- @param #table EventTemplate
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnLandForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.Land )
-
- return self
- end
-
-end
-
-do -- OnTakeOff
- --- Create an OnTakeOff event handler for a group
- -- @param #EVENT self
- -- @param #table EventTemplate
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnTakeOffForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.Takeoff )
-
- return self
- end
-
-end
-
-do -- OnEngineShutDown
-
- --- Create an OnDead event handler for a group
- -- @param #EVENT self
- -- @param #table EventTemplate
- -- @param #function EventFunction The function to be called when the event occurs for the unit.
- -- @param EventClass The self instance of the class for which the event is.
- -- @return #EVENT
- function EVENT:OnEngineShutDownForTemplate( EventTemplate, EventFunction, EventClass )
- self:F2( EventTemplate.name )
-
- self:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EVENTS.EngineShutdown )
-
- return self
- end
-
-end
-
-
---- @param #EVENT self
--- @param #EVENTDATA Event
-function EVENT:onEvent( Event )
-
- local ErrorHandler = function( errmsg )
-
- env.info( "Error in SCHEDULER function:" .. errmsg )
- if debug ~= nil then
- env.info( debug.traceback() )
- end
-
- return errmsg
- end
-
- self:E( _EVENTMETA[Event.id].Text, Event )
-
- if self and self.Events and self.Events[Event.id] then
-
-
- if Event.initiator then
-
- Event.IniObjectCategory = Event.initiator:getCategory()
-
- if Event.IniObjectCategory == Object.Category.UNIT then
- Event.IniDCSUnit = Event.initiator
- Event.IniDCSUnitName = Event.IniDCSUnit:getName()
- Event.IniUnitName = Event.IniDCSUnitName
- Event.IniDCSGroup = Event.IniDCSUnit:getGroup()
- Event.IniUnit = UNIT:FindByName( Event.IniDCSUnitName )
- if not Event.IniUnit then
- -- Unit can be a CLIENT. Most likely this will be the case ...
- Event.IniUnit = CLIENT:FindByName( Event.IniDCSUnitName, '', true )
- end
- Event.IniDCSGroupName = ""
- if Event.IniDCSGroup and Event.IniDCSGroup:isExist() then
- Event.IniDCSGroupName = Event.IniDCSGroup:getName()
- Event.IniGroup = GROUP:FindByName( Event.IniDCSGroupName )
- if Event.IniGroup then
- Event.IniGroupName = Event.IniDCSGroupName
- end
- end
- Event.IniPlayerName = Event.IniDCSUnit:getPlayerName()
- Event.IniCoalition = Event.IniDCSUnit:getCoalition()
- Event.IniTypeName = Event.IniDCSUnit:getTypeName()
- Event.IniCategory = Event.IniDCSUnit:getDesc().category
- end
-
- if Event.IniObjectCategory == Object.Category.STATIC then
- Event.IniDCSUnit = Event.initiator
- Event.IniDCSUnitName = Event.IniDCSUnit:getName()
- Event.IniUnitName = Event.IniDCSUnitName
- Event.IniUnit = STATIC:FindByName( Event.IniDCSUnitName, false )
- Event.IniCoalition = Event.IniDCSUnit:getCoalition()
- Event.IniCategory = Event.IniDCSUnit:getDesc().category
- Event.IniTypeName = Event.IniDCSUnit:getTypeName()
- end
-
- if Event.IniObjectCategory == Object.Category.SCENERY then
- Event.IniDCSUnit = Event.initiator
- Event.IniDCSUnitName = Event.IniDCSUnit:getName()
- Event.IniUnitName = Event.IniDCSUnitName
- Event.IniUnit = SCENERY:Register( Event.IniDCSUnitName, Event.initiator )
- Event.IniCategory = Event.IniDCSUnit:getDesc().category
- Event.IniTypeName = Event.IniDCSUnit:getTypeName()
- end
- end
-
- if Event.target then
-
- Event.TgtObjectCategory = Event.target:getCategory()
-
- if Event.TgtObjectCategory == Object.Category.UNIT then
- Event.TgtDCSUnit = Event.target
- Event.TgtDCSGroup = Event.TgtDCSUnit:getGroup()
- Event.TgtDCSUnitName = Event.TgtDCSUnit:getName()
- Event.TgtUnitName = Event.TgtDCSUnitName
- Event.TgtUnit = UNIT:FindByName( Event.TgtDCSUnitName )
- Event.TgtDCSGroupName = ""
- if Event.TgtDCSGroup and Event.TgtDCSGroup:isExist() then
- Event.TgtDCSGroupName = Event.TgtDCSGroup:getName()
- Event.TgtGroup = GROUP:FindByName( Event.TgtDCSGroupName )
- if Event.TgtGroup then
- Event.TgtGroupName = Event.TgtDCSGroupName
- end
- end
- Event.TgtPlayerName = Event.TgtDCSUnit:getPlayerName()
- Event.TgtCoalition = Event.TgtDCSUnit:getCoalition()
- Event.TgtCategory = Event.TgtDCSUnit:getDesc().category
- Event.TgtTypeName = Event.TgtDCSUnit:getTypeName()
- end
-
- if Event.TgtObjectCategory == Object.Category.STATIC then
- Event.TgtDCSUnit = Event.target
- Event.TgtDCSUnitName = Event.TgtDCSUnit:getName()
- Event.TgtUnitName = Event.TgtDCSUnitName
- Event.TgtUnit = STATIC:FindByName( Event.TgtDCSUnitName )
- Event.TgtCoalition = Event.TgtDCSUnit:getCoalition()
- Event.TgtCategory = Event.TgtDCSUnit:getDesc().category
- Event.TgtTypeName = Event.TgtDCSUnit:getTypeName()
- end
-
- if Event.TgtObjectCategory == Object.Category.SCENERY then
- Event.TgtDCSUnit = Event.target
- Event.TgtDCSUnitName = Event.TgtDCSUnit:getName()
- Event.TgtUnitName = Event.TgtDCSUnitName
- Event.TgtUnit = SCENERY:Register( Event.TgtDCSUnitName, Event.target )
- Event.TgtCategory = Event.TgtDCSUnit:getDesc().category
- Event.TgtTypeName = Event.TgtDCSUnit:getTypeName()
- end
- end
-
- if Event.weapon then
- Event.Weapon = Event.weapon
- Event.WeaponName = Event.Weapon:getTypeName()
- Event.WeaponUNIT = CLIENT:Find( Event.Weapon, '', true ) -- Sometimes, the weapon is a player unit!
- Event.WeaponPlayerName = Event.WeaponUNIT and Event.Weapon:getPlayerName()
- Event.WeaponCoalition = Event.WeaponUNIT and Event.Weapon:getCoalition()
- Event.WeaponCategory = Event.WeaponUNIT and Event.Weapon:getDesc().category
- Event.WeaponTypeName = Event.WeaponUNIT and Event.Weapon:getTypeName()
- --Event.WeaponTgtDCSUnit = Event.Weapon:getTarget()
- end
-
- local PriorityOrder = _EVENTMETA[Event.id].Order
- local PriorityBegin = PriorityOrder == -1 and 5 or 1
- local PriorityEnd = PriorityOrder == -1 and 1 or 5
-
- if Event.IniObjectCategory ~= 3 then
- self:E( { _EVENTMETA[Event.id].Text, Event, Event.IniDCSUnitName, Event.TgtDCSUnitName, PriorityOrder } )
- end
-
- for EventPriority = PriorityBegin, PriorityEnd, PriorityOrder do
-
- if self.Events[Event.id][EventPriority] then
-
- -- Okay, we got the event from DCS. Now loop the SORTED self.EventSorted[] table for the received Event.id, and for each EventData registered, check if a function needs to be called.
- for EventClass, EventData in pairs( self.Events[Event.id][EventPriority] ) do
-
- Event.IniGroup = GROUP:FindByName( Event.IniDCSGroupName )
- Event.TgtGroup = GROUP:FindByName( Event.TgtDCSGroupName )
-
- -- If the EventData is for a UNIT, the call directly the EventClass EventFunction for that UNIT.
- if ( Event.IniDCSUnitName and EventData.EventUnit and EventData.EventUnit[Event.IniDCSUnitName] ) or
- ( Event.TgtDCSUnitName and EventData.EventUnit and EventData.EventUnit[Event.TgtDCSUnitName] ) then
-
- if EventData.EventUnit[Event.IniDCSUnitName] then
-
- -- First test if a EventFunction is Set, otherwise search for the default function
- if EventData.EventUnit[Event.IniDCSUnitName].EventFunction then
-
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling EventFunction for UNIT ", EventClass:GetClassNameAndID(), ", Unit ", Event.IniUnitName, EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventData.EventUnit[Event.IniDCSUnitName].EventFunction( EventClass, Event )
- end, ErrorHandler )
-
- else
-
- -- There is no EventFunction defined, so try to find if a default OnEvent function is defined on the object.
- local EventFunction = EventClass[ _EVENTMETA[Event.id].Event ]
- if EventFunction and type( EventFunction ) == "function" then
-
- -- Now call the default event function.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling " .. _EVENTMETA[Event.id].Event .. " for Class ", EventClass:GetClassNameAndID(), EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventFunction( EventClass, Event )
- end, ErrorHandler )
- end
- end
- end
-
- if EventData.EventUnit[Event.TgtDCSUnitName] then
-
- -- First test if a EventFunction is Set, otherwise search for the default function
- if EventData.EventUnit[Event.TgtDCSUnitName].EventFunction then
-
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling EventFunction for UNIT ", EventClass:GetClassNameAndID(), ", Unit ", Event.TgtUnitName, EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventData.EventUnit[Event.TgtDCSUnitName].EventFunction( EventClass, Event )
- end, ErrorHandler )
-
- else
-
- -- There is no EventFunction defined, so try to find if a default OnEvent function is defined on the object.
- local EventFunction = EventClass[ _EVENTMETA[Event.id].Event ]
- if EventFunction and type( EventFunction ) == "function" then
-
- -- Now call the default event function.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling " .. _EVENTMETA[Event.id].Event .. " for Class ", EventClass:GetClassNameAndID(), EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventFunction( EventClass, Event )
- end, ErrorHandler )
- end
- end
- end
-
- else
-
- -- If the EventData is for a GROUP, the call directly the EventClass EventFunction for the UNIT in that GROUP.
- if ( Event.IniDCSUnitName and Event.IniDCSGroupName and Event.IniGroupName and EventData.EventGroup and EventData.EventGroup[Event.IniGroupName] ) or
- ( Event.TgtDCSUnitName and Event.TgtDCSGroupName and Event.TgtGroupName and EventData.EventGroup and EventData.EventGroup[Event.TgtGroupName] ) then
-
- if EventData.EventGroup[Event.IniGroupName] then
- -- First test if a EventFunction is Set, otherwise search for the default function
- if EventData.EventGroup[Event.IniGroupName].EventFunction then
-
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling EventFunction for GROUP ", EventClass:GetClassNameAndID(), ", Unit ", Event.IniUnitName, EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventData.EventGroup[Event.IniGroupName].EventFunction( EventClass, Event )
- end, ErrorHandler )
-
- else
-
- -- There is no EventFunction defined, so try to find if a default OnEvent function is defined on the object.
- local EventFunction = EventClass[ _EVENTMETA[Event.id].Event ]
- if EventFunction and type( EventFunction ) == "function" then
-
- -- Now call the default event function.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling " .. _EVENTMETA[Event.id].Event .. " for GROUP ", EventClass:GetClassNameAndID(), EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventFunction( EventClass, Event )
- end, ErrorHandler )
- end
- end
- end
-
- if EventData.EventGroup[Event.TgtGroupName] then
- if EventData.EventGroup[Event.TgtGroupName].EventFunction then
-
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling EventFunction for GROUP ", EventClass:GetClassNameAndID(), ", Unit ", Event.TgtUnitName, EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventData.EventGroup[Event.TgtGroupName].EventFunction( EventClass, Event )
- end, ErrorHandler )
-
- else
-
- -- There is no EventFunction defined, so try to find if a default OnEvent function is defined on the object.
- local EventFunction = EventClass[ _EVENTMETA[Event.id].Event ]
- if EventFunction and type( EventFunction ) == "function" then
-
- -- Now call the default event function.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling " .. _EVENTMETA[Event.id].Event .. " for GROUP ", EventClass:GetClassNameAndID(), EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- return EventFunction( EventClass, Event )
- end, ErrorHandler )
- end
- end
- end
-
- else
-
- -- If the EventData is not bound to a specific unit, then call the EventClass EventFunction.
- -- Note that here the EventFunction will need to implement and determine the logic for the relevant source- or target unit, or weapon.
- if (Event.IniDCSUnit or Event.WeaponUNIT) and not EventData.EventUnit then
-
- if EventClass == EventData.EventClass then
-
- -- First test if a EventFunction is Set, otherwise search for the default function
- if EventData.EventFunction then
-
- -- There is an EventFunction defined, so call the EventFunction.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling EventFunction for Class ", EventClass:GetClassNameAndID(), EventPriority } )
- end
- local Result, Value = xpcall(
- function()
- return EventData.EventFunction( EventClass, Event )
- end, ErrorHandler )
- else
-
- -- There is no EventFunction defined, so try to find if a default OnEvent function is defined on the object.
- local EventFunction = EventClass[ _EVENTMETA[Event.id].Event ]
- if EventFunction and type( EventFunction ) == "function" then
-
- -- Now call the default event function.
- if Event.IniObjectCategory ~= 3 then
- self:E( { "Calling " .. _EVENTMETA[Event.id].Event .. " for Class ", EventClass:GetClassNameAndID(), EventPriority } )
- end
-
- local Result, Value = xpcall(
- function()
- local Result, Value = EventFunction( EventClass, Event )
- return Result, Value
- end, ErrorHandler )
- end
- end
- end
- end
- end
- end
- end
- end
- end
- else
- self:E( { _EVENTMETA[Event.id].Text, Event } )
- end
-
- Event = nil
-end
-
---- The EVENTHANDLER structure
--- @type EVENTHANDLER
--- @extends Core.Base#BASE
-EVENTHANDLER = {
- ClassName = "EVENTHANDLER",
- ClassID = 0,
-}
-
---- The EVENTHANDLER constructor
--- @param #EVENTHANDLER self
--- @return #EVENTHANDLER
-function EVENTHANDLER:New()
- self = BASE:Inherit( self, BASE:New() ) -- #EVENTHANDLER
- return self
-end
---- **Core** -- MENU_ classes model the definition of **hierarchical menu structures** and **commands for players** within a mission.
---
--- ===
---
--- DCS Menus can be managed using the MENU classes.
--- The advantage of using MENU classes is that it hides the complexity of dealing with menu management in more advanced scanerios where you need to
--- set menus and later remove them, and later set them again. You'll find while using use normal DCS scripting functions, that setting and removing
--- menus is not a easy feat if you have complex menu hierarchies defined.
--- Using the MOOSE menu classes, the removal and refreshing of menus are nicely being handled within these classes, and becomes much more easy.
--- On top, MOOSE implements **variable parameter** passing for command menus.
---
--- There are basically two different MENU class types that you need to use:
---
--- ### To manage **main menus**, the classes begin with **MENU_**:
---
--- * @{Menu#MENU_MISSION}: Manages main menus for whole mission file.
--- * @{Menu#MENU_COALITION}: Manages main menus for whole coalition.
--- * @{Menu#MENU_GROUP}: Manages main menus for GROUPs.
--- * @{Menu#MENU_CLIENT}: Manages main menus for CLIENTs. This manages menus for units with the skill level "Client".
---
--- ### To manage **command menus**, which are menus that allow the player to issue **functions**, the classes begin with **MENU_COMMAND_**:
---
--- * @{Menu#MENU_MISSION_COMMAND}: Manages command menus for whole mission file.
--- * @{Menu#MENU_COALITION_COMMAND}: Manages command menus for whole coalition.
--- * @{Menu#MENU_GROUP_COMMAND}: Manages command menus for GROUPs.
--- * @{Menu#MENU_CLIENT_COMMAND}: Manages command menus for CLIENTs. This manages menus for units with the skill level "Client".
---
--- ===
---
--- The above menus classes **are derived** from 2 main **abstract** classes defined within the MOOSE framework (so don't use these):
---
--- 1) MENU_ BASE abstract base classes (don't use them)
--- ====================================================
--- The underlying base menu classes are **NOT** to be used within your missions.
--- These are simply abstract base classes defining a couple of fields that are used by the
--- derived MENU_ classes to manage menus.
---
--- 1.1) @{#MENU_BASE} class, extends @{Base#BASE}
--- --------------------------------------------------
--- The @{#MENU_BASE} class defines the main MENU class where other MENU classes are derived from.
---
--- 1.2) @{#MENU_COMMAND_BASE} class, extends @{Base#BASE}
--- ----------------------------------------------------------
--- The @{#MENU_COMMAND_BASE} class defines the main MENU class where other MENU COMMAND_ classes are derived from, in order to set commands.
---
--- ===
---
--- **The next menus define the MENU classes that you can use within your missions.**
---
--- 2) MENU MISSION classes
--- ======================
--- The underlying classes manage the menus for a complete mission file.
---
--- 2.1) @{#MENU_MISSION} class, extends @{Menu#MENU_BASE}
--- ---------------------------------------------------------
--- The @{Menu#MENU_MISSION} class manages the main menus for a complete mission.
--- You can add menus with the @{#MENU_MISSION.New} method, which constructs a MENU_MISSION object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_MISSION.Remove}.
---
--- 2.2) @{#MENU_MISSION_COMMAND} class, extends @{Menu#MENU_COMMAND_BASE}
--- -------------------------------------------------------------------------
--- The @{Menu#MENU_MISSION_COMMAND} class manages the command menus for a complete mission, which allow players to execute functions during mission execution.
--- You can add menus with the @{#MENU_MISSION_COMMAND.New} method, which constructs a MENU_MISSION_COMMAND object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_MISSION_COMMAND.Remove}.
---
--- ===
---
--- 3) MENU COALITION classes
--- =========================
--- The underlying classes manage the menus for whole coalitions.
---
--- 3.1) @{#MENU_COALITION} class, extends @{Menu#MENU_BASE}
--- ------------------------------------------------------------
--- The @{Menu#MENU_COALITION} class manages the main menus for coalitions.
--- You can add menus with the @{#MENU_COALITION.New} method, which constructs a MENU_COALITION object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_COALITION.Remove}.
---
--- 3.2) @{Menu#MENU_COALITION_COMMAND} class, extends @{Menu#MENU_COMMAND_BASE}
--- ----------------------------------------------------------------------------
--- The @{Menu#MENU_COALITION_COMMAND} class manages the command menus for coalitions, which allow players to execute functions during mission execution.
--- You can add menus with the @{#MENU_COALITION_COMMAND.New} method, which constructs a MENU_COALITION_COMMAND object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_COALITION_COMMAND.Remove}.
---
--- ===
---
--- 4) MENU GROUP classes
--- =====================
--- The underlying classes manage the menus for groups. Note that groups can be inactive, alive or can be destroyed.
---
--- 4.1) @{Menu#MENU_GROUP} class, extends @{Menu#MENU_BASE}
--- --------------------------------------------------------
--- The @{Menu#MENU_GROUP} class manages the main menus for coalitions.
--- You can add menus with the @{#MENU_GROUP.New} method, which constructs a MENU_GROUP object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_GROUP.Remove}.
---
--- 4.2) @{Menu#MENU_GROUP_COMMAND} class, extends @{Menu#MENU_COMMAND_BASE}
--- ------------------------------------------------------------------------
--- The @{Menu#MENU_GROUP_COMMAND} class manages the command menus for coalitions, which allow players to execute functions during mission execution.
--- You can add menus with the @{#MENU_GROUP_COMMAND.New} method, which constructs a MENU_GROUP_COMMAND object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_GROUP_COMMAND.Remove}.
---
--- ===
---
--- 5) MENU CLIENT classes
--- ======================
--- The underlying classes manage the menus for units with skill level client or player.
---
--- 5.1) @{Menu#MENU_CLIENT} class, extends @{Menu#MENU_BASE}
--- ---------------------------------------------------------
--- The @{Menu#MENU_CLIENT} class manages the main menus for coalitions.
--- You can add menus with the @{#MENU_CLIENT.New} method, which constructs a MENU_CLIENT object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_CLIENT.Remove}.
---
--- 5.2) @{Menu#MENU_CLIENT_COMMAND} class, extends @{Menu#MENU_COMMAND_BASE}
--- -------------------------------------------------------------------------
--- The @{Menu#MENU_CLIENT_COMMAND} class manages the command menus for coalitions, which allow players to execute functions during mission execution.
--- You can add menus with the @{#MENU_CLIENT_COMMAND.New} method, which constructs a MENU_CLIENT_COMMAND object and returns you the object reference.
--- Using this object reference, you can then remove ALL the menus and submenus underlying automatically with @{#MENU_CLIENT_COMMAND.Remove}.
---
--- ===
---
--- ### Contributions: -
--- ### Authors: FlightControl : Design & Programming
---
--- @module Menu
-
-
-do -- MENU_BASE
-
- --- The MENU_BASE class
- -- @type MENU_BASE
- -- @extends Base#BASE
- MENU_BASE = {
- ClassName = "MENU_BASE",
- MenuPath = nil,
- MenuText = "",
- MenuParentPath = nil
- }
-
- --- Consructor
- -- @param #MENU_BASE
- -- @return #MENU_BASE
- function MENU_BASE:New( MenuText, ParentMenu )
-
- local MenuParentPath = {}
- if ParentMenu ~= nil then
- MenuParentPath = ParentMenu.MenuPath
- end
-
- local self = BASE:Inherit( self, BASE:New() )
-
- self.MenuPath = nil
- self.MenuText = MenuText
- self.MenuParentPath = MenuParentPath
- self.Menus = {}
- self.MenuCount = 0
- self.MenuRemoveParent = false
- self.MenuTime = timer.getTime()
-
- return self
- end
-
- --- Gets a @{Menu} from a parent @{Menu}
- -- @param #MENU_BASE self
- -- @param #string MenuText The text of the child menu.
- -- @return #MENU_BASE
- function MENU_BASE:GetMenu( MenuText )
- self:F( { self.Menus, MenuText } )
- return self.Menus[MenuText]
- end
-
- --- Sets a @{Menu} to remove automatically the parent menu when the menu removed is the last child menu of that parent @{Menu}.
- -- @param #MENU_BASE self
- -- @param #boolean RemoveParent If true, the parent menu is automatically removed when this menu is the last child menu of that parent @{Menu}.
- -- @return #MENU_BASE
- function MENU_BASE:SetRemoveParent( RemoveParent )
- self:F( { RemoveParent } )
- self.MenuRemoveParent = RemoveParent
- return self
- end
-
-
- --- Sets a time stamp for later prevention of menu removal.
- -- @param #MENU_BASE self
- -- @param MenuTime
- -- @return #MENU_BASE
- function MENU_BASE:SetTime( MenuTime )
- self.MenuTime = MenuTime
- return self
- end
-
-end
-
-do -- MENU_COMMAND_BASE
-
- --- The MENU_COMMAND_BASE class
- -- @type MENU_COMMAND_BASE
- -- @field #function MenuCallHandler
- -- @extends Core.Menu#MENU_BASE
- MENU_COMMAND_BASE = {
- ClassName = "MENU_COMMAND_BASE",
- CommandMenuFunction = nil,
- CommandMenuArgument = nil,
- MenuCallHandler = nil,
- }
-
- --- Constructor
- -- @param #MENU_COMMAND_BASE
- -- @return #MENU_COMMAND_BASE
- function MENU_COMMAND_BASE:New( MenuText, ParentMenu, CommandMenuFunction, CommandMenuArguments )
-
- local self = BASE:Inherit( self, MENU_BASE:New( MenuText, ParentMenu ) )
-
- self.CommandMenuFunction = CommandMenuFunction
- self.MenuCallHandler = function( CommandMenuArguments )
- self.CommandMenuFunction( unpack( CommandMenuArguments ) )
- end
-
- return self
- end
-
-end
-
-
-do -- MENU_MISSION
-
- --- The MENU_MISSION class
- -- @type MENU_MISSION
- -- @extends Core.Menu#MENU_BASE
- MENU_MISSION = {
- ClassName = "MENU_MISSION"
- }
-
- --- MENU_MISSION constructor. Creates a new MENU_MISSION object and creates the menu for a complete mission file.
- -- @param #MENU_MISSION self
- -- @param #string MenuText The text for the menu.
- -- @param #table ParentMenu The parent menu. This parameter can be ignored if you want the menu to be located at the perent menu of DCS world (under F10 other).
- -- @return #MENU_MISSION
- function MENU_MISSION:New( MenuText, ParentMenu )
-
- local self = BASE:Inherit( self, MENU_BASE:New( MenuText, ParentMenu ) )
-
- self:F( { MenuText, ParentMenu } )
-
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self.Menus = {}
-
- self:T( { MenuText } )
-
- self.MenuPath = missionCommands.addSubMenu( MenuText, self.MenuParentPath )
-
- self:T( { self.MenuPath } )
-
- if ParentMenu and ParentMenu.Menus then
- ParentMenu.Menus[self.MenuPath] = self
- end
-
- return self
- end
-
- --- Removes the sub menus recursively of this MENU_MISSION. Note that the main menu is kept!
- -- @param #MENU_MISSION self
- -- @return #MENU_MISSION
- function MENU_MISSION:RemoveSubMenus()
- self:F( self.MenuPath )
-
- for MenuID, Menu in pairs( self.Menus ) do
- Menu:Remove()
- end
-
- end
-
- --- Removes the main menu and the sub menus recursively of this MENU_MISSION.
- -- @param #MENU_MISSION self
- -- @return #nil
- function MENU_MISSION:Remove()
- self:F( self.MenuPath )
-
- self:RemoveSubMenus()
- missionCommands.removeItem( self.MenuPath )
- if self.ParentMenu then
- self.ParentMenu.Menus[self.MenuPath] = nil
- end
-
- return nil
- end
-
-end
-
-do -- MENU_MISSION_COMMAND
-
- --- The MENU_MISSION_COMMAND class
- -- @type MENU_MISSION_COMMAND
- -- @extends Core.Menu#MENU_COMMAND_BASE
- MENU_MISSION_COMMAND = {
- ClassName = "MENU_MISSION_COMMAND"
- }
-
- --- MENU_MISSION constructor. Creates a new radio command item for a complete mission file, which can invoke a function with parameters.
- -- @param #MENU_MISSION_COMMAND self
- -- @param #string MenuText The text for the menu.
- -- @param Menu#MENU_MISSION ParentMenu The parent menu.
- -- @param CommandMenuFunction A function that is called when the menu key is pressed.
- -- @param CommandMenuArgument An argument for the function. There can only be ONE argument given. So multiple arguments must be wrapped into a table. See the below example how to do this.
- -- @return #MENU_MISSION_COMMAND self
- function MENU_MISSION_COMMAND:New( MenuText, ParentMenu, CommandMenuFunction, ... )
-
- local self = BASE:Inherit( self, MENU_COMMAND_BASE:New( MenuText, ParentMenu, CommandMenuFunction, arg ) )
-
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self:T( { MenuText, CommandMenuFunction, arg } )
-
-
- self.MenuPath = missionCommands.addCommand( MenuText, self.MenuParentPath, self.MenuCallHandler, arg )
-
- ParentMenu.Menus[self.MenuPath] = self
-
- return self
- end
-
- --- Removes a radio command item for a coalition
- -- @param #MENU_MISSION_COMMAND self
- -- @return #nil
- function MENU_MISSION_COMMAND:Remove()
- self:F( self.MenuPath )
-
- missionCommands.removeItem( self.MenuPath )
- if self.ParentMenu then
- self.ParentMenu.Menus[self.MenuPath] = nil
- end
- return nil
- end
-
-end
-
-
-
-do -- MENU_COALITION
-
- --- The MENU_COALITION class
- -- @type MENU_COALITION
- -- @extends Core.Menu#MENU_BASE
- -- @usage
- -- -- This demo creates a menu structure for the planes within the red coalition.
- -- -- To test, join the planes, then look at the other radio menus (Option F10).
- -- -- Then switch planes and check if the menu is still there.
- --
- -- local Plane1 = CLIENT:FindByName( "Plane 1" )
- -- local Plane2 = CLIENT:FindByName( "Plane 2" )
- --
- --
- -- -- This would create a menu for the red coalition under the main DCS "Others" menu.
- -- local MenuCoalitionRed = MENU_COALITION:New( coalition.side.RED, "Manage Menus" )
- --
- --
- -- local function ShowStatus( StatusText, Coalition )
- --
- -- MESSAGE:New( Coalition, 15 ):ToRed()
- -- Plane1:Message( StatusText, 15 )
- -- Plane2:Message( StatusText, 15 )
- -- end
- --
- -- local MenuStatus -- Menu#MENU_COALITION
- -- local MenuStatusShow -- Menu#MENU_COALITION_COMMAND
- --
- -- local function RemoveStatusMenu()
- -- MenuStatus:Remove()
- -- end
- --
- -- local function AddStatusMenu()
- --
- -- -- This would create a menu for the red coalition under the MenuCoalitionRed menu object.
- -- MenuStatus = MENU_COALITION:New( coalition.side.RED, "Status for Planes" )
- -- MenuStatusShow = MENU_COALITION_COMMAND:New( coalition.side.RED, "Show Status", MenuStatus, ShowStatus, "Status of planes is ok!", "Message to Red Coalition" )
- -- end
- --
- -- local MenuAdd = MENU_COALITION_COMMAND:New( coalition.side.RED, "Add Status Menu", MenuCoalitionRed, AddStatusMenu )
- -- local MenuRemove = MENU_COALITION_COMMAND:New( coalition.side.RED, "Remove Status Menu", MenuCoalitionRed, RemoveStatusMenu )
- MENU_COALITION = {
- ClassName = "MENU_COALITION"
- }
-
- --- MENU_COALITION constructor. Creates a new MENU_COALITION object and creates the menu for a complete coalition.
- -- @param #MENU_COALITION self
- -- @param Dcs.DCSCoalition#coalition.side Coalition The coalition owning the menu.
- -- @param #string MenuText The text for the menu.
- -- @param #table ParentMenu The parent menu. This parameter can be ignored if you want the menu to be located at the perent menu of DCS world (under F10 other).
- -- @return #MENU_COALITION self
- function MENU_COALITION:New( Coalition, MenuText, ParentMenu )
-
- local self = BASE:Inherit( self, MENU_BASE:New( MenuText, ParentMenu ) )
-
- self:F( { Coalition, MenuText, ParentMenu } )
-
- self.Coalition = Coalition
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self.Menus = {}
-
- self:T( { MenuText } )
-
- self.MenuPath = missionCommands.addSubMenuForCoalition( Coalition, MenuText, self.MenuParentPath )
-
- self:T( { self.MenuPath } )
-
- if ParentMenu and ParentMenu.Menus then
- ParentMenu.Menus[self.MenuPath] = self
- end
-
- return self
- end
-
- --- Removes the sub menus recursively of this MENU_COALITION. Note that the main menu is kept!
- -- @param #MENU_COALITION self
- -- @return #MENU_COALITION
- function MENU_COALITION:RemoveSubMenus()
- self:F( self.MenuPath )
-
- for MenuID, Menu in pairs( self.Menus ) do
- Menu:Remove()
- end
-
- end
-
- --- Removes the main menu and the sub menus recursively of this MENU_COALITION.
- -- @param #MENU_COALITION self
- -- @return #nil
- function MENU_COALITION:Remove()
- self:F( self.MenuPath )
-
- self:RemoveSubMenus()
- missionCommands.removeItemForCoalition( self.Coalition, self.MenuPath )
- if self.ParentMenu then
- self.ParentMenu.Menus[self.MenuPath] = nil
- end
-
- return nil
- end
-
-end
-
-do -- MENU_COALITION_COMMAND
-
- --- The MENU_COALITION_COMMAND class
- -- @type MENU_COALITION_COMMAND
- -- @extends Core.Menu#MENU_COMMAND_BASE
- MENU_COALITION_COMMAND = {
- ClassName = "MENU_COALITION_COMMAND"
- }
-
- --- MENU_COALITION constructor. Creates a new radio command item for a coalition, which can invoke a function with parameters.
- -- @param #MENU_COALITION_COMMAND self
- -- @param Dcs.DCSCoalition#coalition.side Coalition The coalition owning the menu.
- -- @param #string MenuText The text for the menu.
- -- @param Menu#MENU_COALITION ParentMenu The parent menu.
- -- @param CommandMenuFunction A function that is called when the menu key is pressed.
- -- @param CommandMenuArgument An argument for the function. There can only be ONE argument given. So multiple arguments must be wrapped into a table. See the below example how to do this.
- -- @return #MENU_COALITION_COMMAND
- function MENU_COALITION_COMMAND:New( Coalition, MenuText, ParentMenu, CommandMenuFunction, ... )
-
- local self = BASE:Inherit( self, MENU_COMMAND_BASE:New( MenuText, ParentMenu, CommandMenuFunction, arg ) )
-
- self.MenuCoalition = Coalition
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self:T( { MenuText, CommandMenuFunction, arg } )
-
-
- self.MenuPath = missionCommands.addCommandForCoalition( self.MenuCoalition, MenuText, self.MenuParentPath, self.MenuCallHandler, arg )
-
- ParentMenu.Menus[self.MenuPath] = self
-
- return self
- end
-
- --- Removes a radio command item for a coalition
- -- @param #MENU_COALITION_COMMAND self
- -- @return #nil
- function MENU_COALITION_COMMAND:Remove()
- self:F( self.MenuPath )
-
- missionCommands.removeItemForCoalition( self.MenuCoalition, self.MenuPath )
- if self.ParentMenu then
- self.ParentMenu.Menus[self.MenuPath] = nil
- end
- return nil
- end
-
-end
-
-do -- MENU_CLIENT
-
- -- This local variable is used to cache the menus registered under clients.
- -- Menus don't dissapear when clients are destroyed and restarted.
- -- So every menu for a client created must be tracked so that program logic accidentally does not create
- -- the same menus twice during initialization logic.
- -- These menu classes are handling this logic with this variable.
- local _MENUCLIENTS = {}
-
- --- MENU_COALITION constructor. Creates a new radio command item for a coalition, which can invoke a function with parameters.
- -- @type MENU_CLIENT
- -- @extends Core.Menu#MENU_BASE
- -- @usage
- -- -- This demo creates a menu structure for the two clients of planes.
- -- -- Each client will receive a different menu structure.
- -- -- To test, join the planes, then look at the other radio menus (Option F10).
- -- -- Then switch planes and check if the menu is still there.
- -- -- And play with the Add and Remove menu options.
- --
- -- -- Note that in multi player, this will only work after the DCS clients bug is solved.
- --
- -- local function ShowStatus( PlaneClient, StatusText, Coalition )
- --
- -- MESSAGE:New( Coalition, 15 ):ToRed()
- -- PlaneClient:Message( StatusText, 15 )
- -- end
- --
- -- local MenuStatus = {}
- --
- -- local function RemoveStatusMenu( MenuClient )
- -- local MenuClientName = MenuClient:GetName()
- -- MenuStatus[MenuClientName]:Remove()
- -- end
- --
- -- --- @param Wrapper.Client#CLIENT MenuClient
- -- local function AddStatusMenu( MenuClient )
- -- local MenuClientName = MenuClient:GetName()
- -- -- This would create a menu for the red coalition under the MenuCoalitionRed menu object.
- -- MenuStatus[MenuClientName] = MENU_CLIENT:New( MenuClient, "Status for Planes" )
- -- MENU_CLIENT_COMMAND:New( MenuClient, "Show Status", MenuStatus[MenuClientName], ShowStatus, MenuClient, "Status of planes is ok!", "Message to Red Coalition" )
- -- end
- --
- -- SCHEDULER:New( nil,
- -- function()
- -- local PlaneClient = CLIENT:FindByName( "Plane 1" )
- -- if PlaneClient and PlaneClient:IsAlive() then
- -- local MenuManage = MENU_CLIENT:New( PlaneClient, "Manage Menus" )
- -- MENU_CLIENT_COMMAND:New( PlaneClient, "Add Status Menu Plane 1", MenuManage, AddStatusMenu, PlaneClient )
- -- MENU_CLIENT_COMMAND:New( PlaneClient, "Remove Status Menu Plane 1", MenuManage, RemoveStatusMenu, PlaneClient )
- -- end
- -- end, {}, 10, 10 )
- --
- -- SCHEDULER:New( nil,
- -- function()
- -- local PlaneClient = CLIENT:FindByName( "Plane 2" )
- -- if PlaneClient and PlaneClient:IsAlive() then
- -- local MenuManage = MENU_CLIENT:New( PlaneClient, "Manage Menus" )
- -- MENU_CLIENT_COMMAND:New( PlaneClient, "Add Status Menu Plane 2", MenuManage, AddStatusMenu, PlaneClient )
- -- MENU_CLIENT_COMMAND:New( PlaneClient, "Remove Status Menu Plane 2", MenuManage, RemoveStatusMenu, PlaneClient )
- -- end
- -- end, {}, 10, 10 )
- MENU_CLIENT = {
- ClassName = "MENU_CLIENT"
- }
-
- --- MENU_CLIENT constructor. Creates a new radio menu item for a client.
- -- @param #MENU_CLIENT self
- -- @param Wrapper.Client#CLIENT Client The Client owning the menu.
- -- @param #string MenuText The text for the menu.
- -- @param #table ParentMenu The parent menu.
- -- @return #MENU_CLIENT self
- function MENU_CLIENT:New( Client, MenuText, ParentMenu )
-
- -- Arrange meta tables
- local MenuParentPath = {}
- if ParentMenu ~= nil then
- MenuParentPath = ParentMenu.MenuPath
- end
-
- local self = BASE:Inherit( self, MENU_BASE:New( MenuText, MenuParentPath ) )
- self:F( { Client, MenuText, ParentMenu } )
-
- self.MenuClient = Client
- self.MenuClientGroupID = Client:GetClientGroupID()
- self.MenuParentPath = MenuParentPath
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self.Menus = {}
-
- if not _MENUCLIENTS[self.MenuClientGroupID] then
- _MENUCLIENTS[self.MenuClientGroupID] = {}
- end
-
- local MenuPath = _MENUCLIENTS[self.MenuClientGroupID]
-
- self:T( { Client:GetClientGroupName(), MenuPath[table.concat(MenuParentPath)], MenuParentPath, MenuText } )
-
- local MenuPathID = table.concat(MenuParentPath) .. "/" .. MenuText
- if MenuPath[MenuPathID] then
- missionCommands.removeItemForGroup( self.MenuClient:GetClientGroupID(), MenuPath[MenuPathID] )
- end
-
- self.MenuPath = missionCommands.addSubMenuForGroup( self.MenuClient:GetClientGroupID(), MenuText, MenuParentPath )
- MenuPath[MenuPathID] = self.MenuPath
-
- self:T( { Client:GetClientGroupName(), self.MenuPath } )
-
- if ParentMenu and ParentMenu.Menus then
- ParentMenu.Menus[self.MenuPath] = self
- end
- return self
- end
-
- --- Removes the sub menus recursively of this @{#MENU_CLIENT}.
- -- @param #MENU_CLIENT self
- -- @return #MENU_CLIENT self
- function MENU_CLIENT:RemoveSubMenus()
- self:F( self.MenuPath )
-
- for MenuID, Menu in pairs( self.Menus ) do
- Menu:Remove()
- end
-
- end
-
- --- Removes the sub menus recursively of this MENU_CLIENT.
- -- @param #MENU_CLIENT self
- -- @return #nil
- function MENU_CLIENT:Remove()
- self:F( self.MenuPath )
-
- self:RemoveSubMenus()
-
- if not _MENUCLIENTS[self.MenuClientGroupID] then
- _MENUCLIENTS[self.MenuClientGroupID] = {}
- end
-
- local MenuPath = _MENUCLIENTS[self.MenuClientGroupID]
-
- if MenuPath[table.concat(self.MenuParentPath) .. "/" .. self.MenuText] then
- MenuPath[table.concat(self.MenuParentPath) .. "/" .. self.MenuText] = nil
- end
-
- missionCommands.removeItemForGroup( self.MenuClient:GetClientGroupID(), self.MenuPath )
- self.ParentMenu.Menus[self.MenuPath] = nil
- return nil
- end
-
-
- --- The MENU_CLIENT_COMMAND class
- -- @type MENU_CLIENT_COMMAND
- -- @extends Core.Menu#MENU_COMMAND
- MENU_CLIENT_COMMAND = {
- ClassName = "MENU_CLIENT_COMMAND"
- }
-
- --- MENU_CLIENT_COMMAND constructor. Creates a new radio command item for a client, which can invoke a function with parameters.
- -- @param #MENU_CLIENT_COMMAND self
- -- @param Wrapper.Client#CLIENT Client The Client owning the menu.
- -- @param #string MenuText The text for the menu.
- -- @param #MENU_BASE ParentMenu The parent menu.
- -- @param CommandMenuFunction A function that is called when the menu key is pressed.
- -- @return Menu#MENU_CLIENT_COMMAND self
- function MENU_CLIENT_COMMAND:New( Client, MenuText, ParentMenu, CommandMenuFunction, ... )
-
- -- Arrange meta tables
-
- local MenuParentPath = {}
- if ParentMenu ~= nil then
- MenuParentPath = ParentMenu.MenuPath
- end
-
- local self = BASE:Inherit( self, MENU_COMMAND_BASE:New( MenuText, MenuParentPath, CommandMenuFunction, arg ) ) -- Menu#MENU_CLIENT_COMMAND
-
- self.MenuClient = Client
- self.MenuClientGroupID = Client:GetClientGroupID()
- self.MenuParentPath = MenuParentPath
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- if not _MENUCLIENTS[self.MenuClientGroupID] then
- _MENUCLIENTS[self.MenuClientGroupID] = {}
- end
-
- local MenuPath = _MENUCLIENTS[self.MenuClientGroupID]
-
- self:T( { Client:GetClientGroupName(), MenuPath[table.concat(MenuParentPath)], MenuParentPath, MenuText, CommandMenuFunction, arg } )
-
- local MenuPathID = table.concat(MenuParentPath) .. "/" .. MenuText
- if MenuPath[MenuPathID] then
- missionCommands.removeItemForGroup( self.MenuClient:GetClientGroupID(), MenuPath[MenuPathID] )
- end
-
- self.MenuPath = missionCommands.addCommandForGroup( self.MenuClient:GetClientGroupID(), MenuText, MenuParentPath, self.MenuCallHandler, arg )
- MenuPath[MenuPathID] = self.MenuPath
-
- if ParentMenu and ParentMenu.Menus then
- ParentMenu.Menus[self.MenuPath] = self
- end
-
- return self
- end
-
- --- Removes a menu structure for a client.
- -- @param #MENU_CLIENT_COMMAND self
- -- @return #nil
- function MENU_CLIENT_COMMAND:Remove()
- self:F( self.MenuPath )
-
- if not _MENUCLIENTS[self.MenuClientGroupID] then
- _MENUCLIENTS[self.MenuClientGroupID] = {}
- end
-
- local MenuPath = _MENUCLIENTS[self.MenuClientGroupID]
-
- if MenuPath[table.concat(self.MenuParentPath) .. "/" .. self.MenuText] then
- MenuPath[table.concat(self.MenuParentPath) .. "/" .. self.MenuText] = nil
- end
-
- missionCommands.removeItemForGroup( self.MenuClient:GetClientGroupID(), self.MenuPath )
- self.ParentMenu.Menus[self.MenuPath] = nil
- return nil
- end
-end
-
---- MENU_GROUP
-
-do
- -- This local variable is used to cache the menus registered under groups.
- -- Menus don't dissapear when groups for players are destroyed and restarted.
- -- So every menu for a client created must be tracked so that program logic accidentally does not create.
- -- the same menus twice during initialization logic.
- -- These menu classes are handling this logic with this variable.
- local _MENUGROUPS = {}
-
- --- The MENU_GROUP class
- -- @type MENU_GROUP
- -- @extends Core.Menu#MENU_BASE
- -- @usage
- -- -- This demo creates a menu structure for the two groups of planes.
- -- -- Each group will receive a different menu structure.
- -- -- To test, join the planes, then look at the other radio menus (Option F10).
- -- -- Then switch planes and check if the menu is still there.
- -- -- And play with the Add and Remove menu options.
- --
- -- -- Note that in multi player, this will only work after the DCS groups bug is solved.
- --
- -- local function ShowStatus( PlaneGroup, StatusText, Coalition )
- --
- -- MESSAGE:New( Coalition, 15 ):ToRed()
- -- PlaneGroup:Message( StatusText, 15 )
- -- end
- --
- -- local MenuStatus = {}
- --
- -- local function RemoveStatusMenu( MenuGroup )
- -- local MenuGroupName = MenuGroup:GetName()
- -- MenuStatus[MenuGroupName]:Remove()
- -- end
- --
- -- --- @param Wrapper.Group#GROUP MenuGroup
- -- local function AddStatusMenu( MenuGroup )
- -- local MenuGroupName = MenuGroup:GetName()
- -- -- This would create a menu for the red coalition under the MenuCoalitionRed menu object.
- -- MenuStatus[MenuGroupName] = MENU_GROUP:New( MenuGroup, "Status for Planes" )
- -- MENU_GROUP_COMMAND:New( MenuGroup, "Show Status", MenuStatus[MenuGroupName], ShowStatus, MenuGroup, "Status of planes is ok!", "Message to Red Coalition" )
- -- end
- --
- -- SCHEDULER:New( nil,
- -- function()
- -- local PlaneGroup = GROUP:FindByName( "Plane 1" )
- -- if PlaneGroup and PlaneGroup:IsAlive() then
- -- local MenuManage = MENU_GROUP:New( PlaneGroup, "Manage Menus" )
- -- MENU_GROUP_COMMAND:New( PlaneGroup, "Add Status Menu Plane 1", MenuManage, AddStatusMenu, PlaneGroup )
- -- MENU_GROUP_COMMAND:New( PlaneGroup, "Remove Status Menu Plane 1", MenuManage, RemoveStatusMenu, PlaneGroup )
- -- end
- -- end, {}, 10, 10 )
- --
- -- SCHEDULER:New( nil,
- -- function()
- -- local PlaneGroup = GROUP:FindByName( "Plane 2" )
- -- if PlaneGroup and PlaneGroup:IsAlive() then
- -- local MenuManage = MENU_GROUP:New( PlaneGroup, "Manage Menus" )
- -- MENU_GROUP_COMMAND:New( PlaneGroup, "Add Status Menu Plane 2", MenuManage, AddStatusMenu, PlaneGroup )
- -- MENU_GROUP_COMMAND:New( PlaneGroup, "Remove Status Menu Plane 2", MenuManage, RemoveStatusMenu, PlaneGroup )
- -- end
- -- end, {}, 10, 10 )
- --
- MENU_GROUP = {
- ClassName = "MENU_GROUP"
- }
-
- --- MENU_GROUP constructor. Creates a new radio menu item for a group.
- -- @param #MENU_GROUP self
- -- @param Wrapper.Group#GROUP MenuGroup The Group owning the menu.
- -- @param #string MenuText The text for the menu.
- -- @param #table ParentMenu The parent menu.
- -- @return #MENU_GROUP self
- function MENU_GROUP:New( MenuGroup, MenuText, ParentMenu )
-
- -- Determine if the menu was not already created and already visible at the group.
- -- If it is visible, then return the cached self, otherwise, create self and cache it.
-
- MenuGroup._Menus = MenuGroup._Menus or {}
- local Path = ( ParentMenu and ( table.concat( ParentMenu.MenuPath or {}, "@" ) .. "@" .. MenuText ) ) or MenuText
- if MenuGroup._Menus[Path] then
- self = MenuGroup._Menus[Path]
- else
- self = BASE:Inherit( self, MENU_BASE:New( MenuText, ParentMenu ) )
- MenuGroup._Menus[Path] = self
-
- self.MenuGroup = MenuGroup
- self.Path = Path
- self.MenuGroupID = MenuGroup:GetID()
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self:T( { "Adding Menu ", MenuText, self.MenuParentPath } )
- self.MenuPath = missionCommands.addSubMenuForGroup( self.MenuGroupID, MenuText, self.MenuParentPath )
-
- if self.ParentMenu and self.ParentMenu.Menus then
- self.ParentMenu.Menus[MenuText] = self
- self:F( { self.ParentMenu.Menus, MenuText } )
- self.ParentMenu.MenuCount = self.ParentMenu.MenuCount + 1
- end
- end
-
- --self:F( { MenuGroup:GetName(), MenuText, ParentMenu.MenuPath } )
-
- return self
- end
-
- --- Removes the sub menus recursively of this MENU_GROUP.
- -- @param #MENU_GROUP self
- -- @param MenuTime
- -- @return #MENU_GROUP self
- function MENU_GROUP:RemoveSubMenus( MenuTime )
- self:F2( { self.MenuPath, MenuTime, self.MenuTime } )
-
- self:T( { "Removing Group SubMenus:", self.MenuGroup:GetName(), self.MenuPath } )
- for MenuText, Menu in pairs( self.Menus ) do
- Menu:Remove( MenuTime )
- end
-
- end
-
-
- --- Removes the main menu and sub menus recursively of this MENU_GROUP.
- -- @param #MENU_GROUP self
- -- @param MenuTime
- -- @return #nil
- function MENU_GROUP:Remove( MenuTime )
- self:F( { self.MenuGroupID, self.MenuPath, MenuTime, self.MenuTime } )
-
- self:RemoveSubMenus( MenuTime )
-
- if not MenuTime or self.MenuTime ~= MenuTime then
- if self.MenuGroup._Menus[self.Path] then
- self = self.MenuGroup._Menus[self.Path]
-
- missionCommands.removeItemForGroup( self.MenuGroupID, self.MenuPath )
- if self.ParentMenu then
- self.ParentMenu.Menus[self.MenuText] = nil
- self.ParentMenu.MenuCount = self.ParentMenu.MenuCount - 1
- if self.ParentMenu.MenuCount == 0 then
- if self.MenuRemoveParent == true then
- self:T( "Removing Parent Menu " )
- self.ParentMenu:Remove()
- end
- end
- end
- self:T( { "Removing Group Menu:", self.MenuGroup:GetName(), self.MenuGroup._Menus[self.Path].Path } )
- self.MenuGroup._Menus[self.Path] = nil
- self = nil
- end
- end
-
- return nil
- end
-
-
- --- The MENU_GROUP_COMMAND class
- -- @type MENU_GROUP_COMMAND
- -- @extends Core.Menu#MENU_BASE
- MENU_GROUP_COMMAND = {
- ClassName = "MENU_GROUP_COMMAND"
- }
-
- --- Creates a new radio command item for a group
- -- @param #MENU_GROUP_COMMAND self
- -- @param Wrapper.Group#GROUP MenuGroup The Group owning the menu.
- -- @param MenuText The text for the menu.
- -- @param ParentMenu The parent menu.
- -- @param CommandMenuFunction A function that is called when the menu key is pressed.
- -- @param CommandMenuArgument An argument for the function.
- -- @return #MENU_GROUP_COMMAND
- function MENU_GROUP_COMMAND:New( MenuGroup, MenuText, ParentMenu, CommandMenuFunction, ... )
-
- MenuGroup._Menus = MenuGroup._Menus or {}
- local Path = ( ParentMenu and ( table.concat( ParentMenu.MenuPath or {}, "@" ) .. "@" .. MenuText ) ) or MenuText
- if MenuGroup._Menus[Path] then
- self = MenuGroup._Menus[Path]
- self:T( { "Re-using Group Command Menu:", MenuGroup:GetName(), MenuText } )
- else
- self = BASE:Inherit( self, MENU_COMMAND_BASE:New( MenuText, ParentMenu, CommandMenuFunction, arg ) )
- MenuGroup._Menus[Path] = self
-
- self.Path = Path
- self.MenuGroup = MenuGroup
- self.MenuGroupID = MenuGroup:GetID()
- self.MenuText = MenuText
- self.ParentMenu = ParentMenu
-
- self:T( { "Adding Group Command Menu:", MenuGroup:GetName(), MenuText, self.MenuParentPath } )
- self.MenuPath = missionCommands.addCommandForGroup( self.MenuGroupID, MenuText, self.MenuParentPath, self.MenuCallHandler, arg )
-
- if self.ParentMenu and self.ParentMenu.Menus then
- self.ParentMenu.Menus[MenuText] = self
- self.ParentMenu.MenuCount = self.ParentMenu.MenuCount + 1
- self:F( { ParentMenu.Menus, MenuText } )
- end
- end
-
- return self
- end
-
- --- Removes a menu structure for a group.
- -- @param #MENU_GROUP_COMMAND self
- -- @param MenuTime
- -- @return #nil
- function MENU_GROUP_COMMAND:Remove( MenuTime )
- self:F( { self.MenuGroupID, self.MenuPath, MenuTime, self.MenuTime } )
-
- if not MenuTime or self.MenuTime ~= MenuTime then
- if self.MenuGroup._Menus[self.Path] then
- self = self.MenuGroup._Menus[self.Path]
-
- missionCommands.removeItemForGroup( self.MenuGroupID, self.MenuPath )
- self:T( { "Removing Group Command Menu:", self.MenuGroup:GetName(), self.MenuText, self.Path, self.MenuGroup._Menus[self.Path].Path } )
-
- self.ParentMenu.Menus[self.MenuText] = nil
- self.ParentMenu.MenuCount = self.ParentMenu.MenuCount - 1
- if self.ParentMenu.MenuCount == 0 then
- if self.MenuRemoveParent == true then
- self:T( "Removing Parent Menu " )
- self.ParentMenu:Remove()
- end
- end
-
- self.MenuGroup._Menus[self.Path] = nil
- self = nil
- end
- end
-
- return nil
- end
-
-end
-
---- **Core** - ZONE classes define **zones** within your mission of **various forms**, with **various capabilities**.
---
--- 
---
--- ===
---
--- There are essentially two core functions that zones accomodate:
---
--- * Test if an object is within the zone boundaries.
--- * Provide the zone behaviour. Some zones are static, while others are moveable.
---
--- The object classes are using the zone classes to test the zone boundaries, which can take various forms:
---
--- * Test if completely within the zone.
--- * Test if partly within the zone (for @{Group#GROUP} objects).
--- * Test if not in the zone.
--- * Distance to the nearest intersecting point of the zone.
--- * Distance to the center of the zone.
--- * ...
---
--- Each of these ZONE classes have a zone name, and specific parameters defining the zone type:
---
--- * @{#ZONE_BASE}: The ZONE_BASE class defining the base for all other zone classes.
--- * @{#ZONE_RADIUS}: The ZONE_RADIUS class defined by a zone name, a location and a radius.
--- * @{#ZONE}: The ZONE class, defined by the zone name as defined within the Mission Editor.
--- * @{#ZONE_UNIT}: The ZONE_UNIT class defines by a zone around a @{Unit#UNIT} with a radius.
--- * @{#ZONE_GROUP}: The ZONE_GROUP class defines by a zone around a @{Group#GROUP} with a radius.
--- * @{#ZONE_POLYGON}: The ZONE_POLYGON class defines by a sequence of @{Group#GROUP} waypoints within the Mission Editor, forming a polygon.
---
--- ===
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-02-28: ZONE\_BASE:**IsVec2InZone()** replaces ZONE\_BASE:_IsPointVec2InZone()_.
--- 2017-02-28: ZONE\_BASE:**IsVec3InZone()** replaces ZONE\_BASE:_IsPointVec3InZone()_.
--- 2017-02-28: ZONE\_RADIUS:**IsVec2InZone()** replaces ZONE\_RADIUS:_IsPointVec2InZone()_.
--- 2017-02-28: ZONE\_RADIUS:**IsVec3InZone()** replaces ZONE\_RADIUS:_IsPointVec3InZone()_.
--- 2017-02-28: ZONE\_POLYGON:**IsVec2InZone()** replaces ZONE\_POLYGON:_IsPointVec2InZone()_.
--- 2017-02-28: ZONE\_POLYGON:**IsVec3InZone()** replaces ZONE\_POLYGON:_IsPointVec3InZone()_.
---
--- 2017-02-18: ZONE\_POLYGON_BASE:**GetRandomPointVec2()** added.
---
--- 2017-02-18: ZONE\_POLYGON_BASE:**GetRandomPointVec3()** added.
---
--- 2017-02-18: ZONE\_RADIUS:**GetRandomPointVec3( inner, outer )** added.
---
--- 2017-02-18: ZONE\_RADIUS:**GetRandomPointVec2( inner, outer )** added.
---
--- 2016-08-15: ZONE\_BASE:**GetName()** added.
---
--- 2016-08-15: ZONE\_BASE:**SetZoneProbability( ZoneProbability )** added.
---
--- 2016-08-15: ZONE\_BASE:**GetZoneProbability()** added.
---
--- 2016-08-15: ZONE\_BASE:**GetZoneMaybe()** added.
---
--- ===
---
--- @module Zone
-
-
---- The ZONE_BASE class
--- @type ZONE_BASE
--- @field #string ZoneName Name of the zone.
--- @field #number ZoneProbability A value between 0 and 1. 0 = 0% and 1 = 100% probability.
--- @extends Core.Base#BASE
-
-
---- # 1) ZONE_BASE class, extends @{Base#BASE}
---
--- This class is an abstract BASE class for derived classes, and is not meant to be instantiated.
---
--- ## 1.1) Each zone has a name:
---
--- * @{#ZONE_BASE.GetName}(): Returns the name of the zone.
---
--- ## 1.2) Each zone implements two polymorphic functions defined in @{Zone#ZONE_BASE}:
---
--- * @{#ZONE_BASE.IsVec2InZone}(): Returns if a Vec2 is within the zone.
--- * @{#ZONE_BASE.IsVec3InZone}(): Returns if a Vec3 is within the zone.
---
--- ## 1.3) A zone has a probability factor that can be set to randomize a selection between zones:
---
--- * @{#ZONE_BASE.SetRandomizeProbability}(): Set the randomization probability of a zone to be selected, taking a value between 0 and 1 ( 0 = 0%, 1 = 100% )
--- * @{#ZONE_BASE.GetRandomizeProbability}(): Get the randomization probability of a zone to be selected, passing a value between 0 and 1 ( 0 = 0%, 1 = 100% )
--- * @{#ZONE_BASE.GetZoneMaybe}(): Get the zone taking into account the randomization probability. nil is returned if this zone is not a candidate.
---
--- ## 1.4) A zone manages Vectors:
---
--- * @{#ZONE_BASE.GetVec2}(): Returns the @{DCSTypes#Vec2} coordinate of the zone.
--- * @{#ZONE_BASE.GetRandomVec2}(): Define a random @{DCSTypes#Vec2} within the zone.
---
--- ## 1.5) A zone has a bounding square:
---
--- * @{#ZONE_BASE.GetBoundingSquare}(): Get the outer most bounding square of the zone.
---
--- ## 1.6) A zone can be marked:
---
--- * @{#ZONE_BASE.SmokeZone}(): Smokes the zone boundaries in a color.
--- * @{#ZONE_BASE.FlareZone}(): Flares the zone boundaries in a color.
---
--- ===
--- @field #ZONE_BASE ZONE_BASE
-ZONE_BASE = {
- ClassName = "ZONE_BASE",
- ZoneName = "",
- ZoneProbability = 1,
- }
-
-
---- The ZONE_BASE.BoundingSquare
--- @type ZONE_BASE.BoundingSquare
--- @field Dcs.DCSTypes#Distance x1 The lower x coordinate (left down)
--- @field Dcs.DCSTypes#Distance y1 The lower y coordinate (left down)
--- @field Dcs.DCSTypes#Distance x2 The higher x coordinate (right up)
--- @field Dcs.DCSTypes#Distance y2 The higher y coordinate (right up)
-
-
---- ZONE_BASE constructor
--- @param #ZONE_BASE self
--- @param #string ZoneName Name of the zone.
--- @return #ZONE_BASE self
-function ZONE_BASE:New( ZoneName )
- local self = BASE:Inherit( self, BASE:New() )
- self:F( ZoneName )
-
- self.ZoneName = ZoneName
-
- return self
-end
-
---- Returns the name of the zone.
--- @param #ZONE_BASE self
--- @return #string The name of the zone.
-function ZONE_BASE:GetName()
- self:F2()
-
- return self.ZoneName
-end
---- Returns if a location is within the zone.
--- @param #ZONE_BASE self
--- @param Dcs.DCSTypes#Vec2 Vec2 The location to test.
--- @return #boolean true if the location is within the zone.
-function ZONE_BASE:IsVec2InZone( Vec2 )
- self:F2( Vec2 )
-
- return false
-end
-
---- Returns if a point is within the zone.
--- @param #ZONE_BASE self
--- @param Dcs.DCSTypes#Vec3 Vec3 The point to test.
--- @return #boolean true if the point is within the zone.
-function ZONE_BASE:IsVec3InZone( Vec3 )
- self:F2( Vec3 )
-
- local InZone = self:IsVec2InZone( { x = Vec3.x, y = Vec3.z } )
-
- return InZone
-end
-
---- Returns the @{DCSTypes#Vec2} coordinate of the zone.
--- @param #ZONE_BASE self
--- @return #nil.
-function ZONE_BASE:GetVec2()
- self:F2( self.ZoneName )
-
- return nil
-end
-
---- Returns a @{Point#POINT_VEC2} of the zone.
--- @param #ZONE_BASE self
--- @param Dcs.DCSTypes#Distance Height The height to add to the land height where the center of the zone is located.
--- @return Core.Point#POINT_VEC2 The PointVec2 of the zone.
-function ZONE_BASE:GetPointVec2()
- self:F2( self.ZoneName )
-
- local Vec2 = self:GetVec2()
-
- local PointVec2 = POINT_VEC2:NewFromVec2( Vec2 )
-
- self:T2( { PointVec2 } )
-
- return PointVec2
-end
-
-
---- Returns the @{DCSTypes#Vec3} of the zone.
--- @param #ZONE_BASE self
--- @param Dcs.DCSTypes#Distance Height The height to add to the land height where the center of the zone is located.
--- @return Dcs.DCSTypes#Vec3 The Vec3 of the zone.
-function ZONE_BASE:GetVec3( Height )
- self:F2( self.ZoneName )
-
- Height = Height or 0
-
- local Vec2 = self:GetVec2()
-
- local Vec3 = { x = Vec2.x, y = land.getHeight( self:GetVec2() ) + Height, z = Vec2.y }
-
- self:T2( { Vec3 } )
-
- return Vec3
-end
-
---- Returns a @{Point#POINT_VEC3} of the zone.
--- @param #ZONE_BASE self
--- @param Dcs.DCSTypes#Distance Height The height to add to the land height where the center of the zone is located.
--- @return Core.Point#POINT_VEC3 The PointVec3 of the zone.
-function ZONE_BASE:GetPointVec3( Height )
- self:F2( self.ZoneName )
-
- local Vec3 = self:GetVec3( Height )
-
- local PointVec3 = POINT_VEC3:NewFromVec3( Vec3 )
-
- self:T2( { PointVec3 } )
-
- return PointVec3
-end
-
-
---- Define a random @{DCSTypes#Vec2} within the zone.
--- @param #ZONE_BASE self
--- @return Dcs.DCSTypes#Vec2 The Vec2 coordinates.
-function ZONE_BASE:GetRandomVec2()
- return nil
-end
-
---- Define a random @{Point#POINT_VEC2} within the zone.
--- @param #ZONE_BASE self
--- @return Core.Point#POINT_VEC2 The PointVec2 coordinates.
-function ZONE_BASE:GetRandomPointVec2()
- return nil
-end
-
---- Define a random @{Point#POINT_VEC3} within the zone.
--- @param #ZONE_BASE self
--- @return Core.Point#POINT_VEC3 The PointVec3 coordinates.
-function ZONE_BASE:GetRandomPointVec3()
- return nil
-end
-
---- Get the bounding square the zone.
--- @param #ZONE_BASE self
--- @return #nil The bounding square.
-function ZONE_BASE:GetBoundingSquare()
- --return { x1 = 0, y1 = 0, x2 = 0, y2 = 0 }
- return nil
-end
-
---- Bound the zone boundaries with a tires.
--- @param #ZONE_BASE self
-function ZONE_BASE:BoundZone()
- self:F2()
-
-end
-
---- Smokes the zone boundaries in a color.
--- @param #ZONE_BASE self
--- @param Utilities.Utils#SMOKECOLOR SmokeColor The smoke color.
-function ZONE_BASE:SmokeZone( SmokeColor )
- self:F2( SmokeColor )
-
-end
-
---- Set the randomization probability of a zone to be selected.
--- @param #ZONE_BASE self
--- @param ZoneProbability A value between 0 and 1. 0 = 0% and 1 = 100% probability.
-function ZONE_BASE:SetZoneProbability( ZoneProbability )
- self:F2( ZoneProbability )
-
- self.ZoneProbability = ZoneProbability or 1
- return self
-end
-
---- Get the randomization probability of a zone to be selected.
--- @param #ZONE_BASE self
--- @return #number A value between 0 and 1. 0 = 0% and 1 = 100% probability.
-function ZONE_BASE:GetZoneProbability()
- self:F2()
-
- return self.ZoneProbability
-end
-
---- Get the zone taking into account the randomization probability of a zone to be selected.
--- @param #ZONE_BASE self
--- @return #ZONE_BASE The zone is selected taking into account the randomization probability factor.
--- @return #nil The zone is not selected taking into account the randomization probability factor.
-function ZONE_BASE:GetZoneMaybe()
- self:F2()
-
- local Randomization = math.random()
- if Randomization <= self.ZoneProbability then
- return self
- else
- return nil
- end
-end
-
-
---- The ZONE_RADIUS class, defined by a zone name, a location and a radius.
--- @type ZONE_RADIUS
--- @field Dcs.DCSTypes#Vec2 Vec2 The current location of the zone.
--- @field Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @extends Core.Zone#ZONE_BASE
-
---- # 2) @{Zone#ZONE_RADIUS} class, extends @{Zone#ZONE_BASE}
---
--- The ZONE_RADIUS class defined by a zone name, a location and a radius.
--- This class implements the inherited functions from Core.Zone#ZONE_BASE taking into account the own zone format and properties.
---
--- ## 2.1) @{Zone#ZONE_RADIUS} constructor
---
--- * @{#ZONE_RADIUS.New}(): Constructor.
---
--- ## 2.2) Manage the radius of the zone
---
--- * @{#ZONE_RADIUS.SetRadius}(): Sets the radius of the zone.
--- * @{#ZONE_RADIUS.GetRadius}(): Returns the radius of the zone.
---
--- ## 2.3) Manage the location of the zone
---
--- * @{#ZONE_RADIUS.SetVec2}(): Sets the @{DCSTypes#Vec2} of the zone.
--- * @{#ZONE_RADIUS.GetVec2}(): Returns the @{DCSTypes#Vec2} of the zone.
--- * @{#ZONE_RADIUS.GetVec3}(): Returns the @{DCSTypes#Vec3} of the zone, taking an additional height parameter.
---
--- ## 2.4) Zone point randomization
---
--- Various functions exist to find random points within the zone.
---
--- * @{#ZONE_RADIUS.GetRandomVec2}(): Gets a random 2D point in the zone.
--- * @{#ZONE_RADIUS.GetRandomPointVec2}(): Gets a @{Point#POINT_VEC2} object representing a random 2D point in the zone.
--- * @{#ZONE_RADIUS.GetRandomPointVec3}(): Gets a @{Point#POINT_VEC3} object representing a random 3D point in the zone. Note that the height of the point is at landheight.
---
--- ===
---
--- @field #ZONE_RADIUS ZONE_RADIUS
---
-ZONE_RADIUS = {
- ClassName="ZONE_RADIUS",
- }
-
---- Constructor of @{#ZONE_RADIUS}, taking the zone name, the zone location and a radius.
--- @param #ZONE_RADIUS self
--- @param #string ZoneName Name of the zone.
--- @param Dcs.DCSTypes#Vec2 Vec2 The location of the zone.
--- @param Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @return #ZONE_RADIUS self
-function ZONE_RADIUS:New( ZoneName, Vec2, Radius )
- local self = BASE:Inherit( self, ZONE_BASE:New( ZoneName ) ) -- #ZONE_RADIUS
- self:F( { ZoneName, Vec2, Radius } )
-
- self.Radius = Radius
- self.Vec2 = Vec2
-
- return self
-end
-
---- Bounds the zone with tires.
--- @param #ZONE_RADIUS self
--- @param #number Points (optional) The amount of points in the circle.
--- @param #boolean UnBound If true the tyres will be destroyed.
--- @return #ZONE_RADIUS self
-function ZONE_RADIUS:BoundZone( Points, CountryID, UnBound )
-
- local Point = {}
- local Vec2 = self:GetVec2()
-
- Points = Points and Points or 360
-
- local Angle
- local RadialBase = math.pi*2
-
- --
- for Angle = 0, 360, (360 / Points ) do
- local Radial = Angle * RadialBase / 360
- Point.x = Vec2.x + math.cos( Radial ) * self:GetRadius()
- Point.y = Vec2.y + math.sin( Radial ) * self:GetRadius()
-
- local CountryName = _DATABASE.COUNTRY_NAME[CountryID]
-
- local Tire = {
- ["country"] = CountryName,
- ["category"] = "Fortifications",
- ["canCargo"] = false,
- ["shape_name"] = "H-tyre_B_WF",
- ["type"] = "Black_Tyre_WF",
- --["unitId"] = Angle + 10000,
- ["y"] = Point.y,
- ["x"] = Point.x,
- ["name"] = string.format( "%s-Tire #%0d", self:GetName(), Angle ),
- ["heading"] = 0,
- } -- end of ["group"]
-
- local Group = coalition.addStaticObject( CountryID, Tire )
- if UnBound and UnBound == true then
- Group:destroy()
- end
- end
-
- return self
-end
-
-
---- Smokes the zone boundaries in a color.
--- @param #ZONE_RADIUS self
--- @param Utilities.Utils#SMOKECOLOR SmokeColor The smoke color.
--- @param #number Points (optional) The amount of points in the circle.
--- @return #ZONE_RADIUS self
-function ZONE_RADIUS:SmokeZone( SmokeColor, Points )
- self:F2( SmokeColor )
-
- local Point = {}
- local Vec2 = self:GetVec2()
-
- Points = Points and Points or 360
-
- local Angle
- local RadialBase = math.pi*2
-
- for Angle = 0, 360, 360 / Points do
- local Radial = Angle * RadialBase / 360
- Point.x = Vec2.x + math.cos( Radial ) * self:GetRadius()
- Point.y = Vec2.y + math.sin( Radial ) * self:GetRadius()
- POINT_VEC2:New( Point.x, Point.y ):Smoke( SmokeColor )
- end
-
- return self
-end
-
-
---- Flares the zone boundaries in a color.
--- @param #ZONE_RADIUS self
--- @param Utilities.Utils#FLARECOLOR FlareColor The flare color.
--- @param #number Points (optional) The amount of points in the circle.
--- @param Dcs.DCSTypes#Azimuth Azimuth (optional) Azimuth The azimuth of the flare.
--- @return #ZONE_RADIUS self
-function ZONE_RADIUS:FlareZone( FlareColor, Points, Azimuth )
- self:F2( { FlareColor, Azimuth } )
-
- local Point = {}
- local Vec2 = self:GetVec2()
-
- Points = Points and Points or 360
-
- local Angle
- local RadialBase = math.pi*2
-
- for Angle = 0, 360, 360 / Points do
- local Radial = Angle * RadialBase / 360
- Point.x = Vec2.x + math.cos( Radial ) * self:GetRadius()
- Point.y = Vec2.y + math.sin( Radial ) * self:GetRadius()
- POINT_VEC2:New( Point.x, Point.y ):Flare( FlareColor, Azimuth )
- end
-
- return self
-end
-
---- Returns the radius of the zone.
--- @param #ZONE_RADIUS self
--- @return Dcs.DCSTypes#Distance The radius of the zone.
-function ZONE_RADIUS:GetRadius()
- self:F2( self.ZoneName )
-
- self:T2( { self.Radius } )
-
- return self.Radius
-end
-
---- Sets the radius of the zone.
--- @param #ZONE_RADIUS self
--- @param Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @return Dcs.DCSTypes#Distance The radius of the zone.
-function ZONE_RADIUS:SetRadius( Radius )
- self:F2( self.ZoneName )
-
- self.Radius = Radius
- self:T2( { self.Radius } )
-
- return self.Radius
-end
-
---- Returns the @{DCSTypes#Vec2} of the zone.
--- @param #ZONE_RADIUS self
--- @return Dcs.DCSTypes#Vec2 The location of the zone.
-function ZONE_RADIUS:GetVec2()
- self:F2( self.ZoneName )
-
- self:T2( { self.Vec2 } )
-
- return self.Vec2
-end
-
---- Sets the @{DCSTypes#Vec2} of the zone.
--- @param #ZONE_RADIUS self
--- @param Dcs.DCSTypes#Vec2 Vec2 The new location of the zone.
--- @return Dcs.DCSTypes#Vec2 The new location of the zone.
-function ZONE_RADIUS:SetVec2( Vec2 )
- self:F2( self.ZoneName )
-
- self.Vec2 = Vec2
-
- self:T2( { self.Vec2 } )
-
- return self.Vec2
-end
-
---- Returns the @{DCSTypes#Vec3} of the ZONE_RADIUS.
--- @param #ZONE_RADIUS self
--- @param Dcs.DCSTypes#Distance Height The height to add to the land height where the center of the zone is located.
--- @return Dcs.DCSTypes#Vec3 The point of the zone.
-function ZONE_RADIUS:GetVec3( Height )
- self:F2( { self.ZoneName, Height } )
-
- Height = Height or 0
- local Vec2 = self:GetVec2()
-
- local Vec3 = { x = Vec2.x, y = land.getHeight( self:GetVec2() ) + Height, z = Vec2.y }
-
- self:T2( { Vec3 } )
-
- return Vec3
-end
-
-
---- Returns if a location is within the zone.
--- @param #ZONE_RADIUS self
--- @param Dcs.DCSTypes#Vec2 Vec2 The location to test.
--- @return #boolean true if the location is within the zone.
-function ZONE_RADIUS:IsVec2InZone( Vec2 )
- self:F2( Vec2 )
-
- local ZoneVec2 = self:GetVec2()
-
- if ZoneVec2 then
- if (( Vec2.x - ZoneVec2.x )^2 + ( Vec2.y - ZoneVec2.y ) ^2 ) ^ 0.5 <= self:GetRadius() then
- return true
- end
- end
-
- return false
-end
-
---- Returns if a point is within the zone.
--- @param #ZONE_RADIUS self
--- @param Dcs.DCSTypes#Vec3 Vec3 The point to test.
--- @return #boolean true if the point is within the zone.
-function ZONE_RADIUS:IsVec3InZone( Vec3 )
- self:F2( Vec3 )
-
- local InZone = self:IsVec2InZone( { x = Vec3.x, y = Vec3.z } )
-
- return InZone
-end
-
---- Returns a random Vec2 location within the zone.
--- @param #ZONE_RADIUS self
--- @param #number inner (optional) Minimal distance from the center of the zone. Default is 0.
--- @param #number outer (optional) Maximal distance from the outer edge of the zone. Default is the radius of the zone.
--- @return Dcs.DCSTypes#Vec2 The random location within the zone.
-function ZONE_RADIUS:GetRandomVec2( inner, outer )
- self:F( self.ZoneName, inner, outer )
-
- local Point = {}
- local Vec2 = self:GetVec2()
- local _inner = inner or 0
- local _outer = outer or self:GetRadius()
-
- local angle = math.random() * math.pi * 2;
- Point.x = Vec2.x + math.cos( angle ) * math.random(_inner, _outer);
- Point.y = Vec2.y + math.sin( angle ) * math.random(_inner, _outer);
-
- self:T( { Point } )
-
- return Point
-end
-
---- Returns a @{Point#POINT_VEC2} object reflecting a random 2D location within the zone.
--- @param #ZONE_RADIUS self
--- @param #number inner (optional) Minimal distance from the center of the zone. Default is 0.
--- @param #number outer (optional) Maximal distance from the outer edge of the zone. Default is the radius of the zone.
--- @return Core.Point#POINT_VEC2 The @{Point#POINT_VEC2} object reflecting the random 3D location within the zone.
-function ZONE_RADIUS:GetRandomPointVec2( inner, outer )
- self:F( self.ZoneName, inner, outer )
-
- local PointVec2 = POINT_VEC2:NewFromVec2( self:GetRandomVec2() )
-
- self:T3( { PointVec2 } )
-
- return PointVec2
-end
-
---- Returns a @{Point#POINT_VEC3} object reflecting a random 3D location within the zone.
--- @param #ZONE_RADIUS self
--- @param #number inner (optional) Minimal distance from the center of the zone. Default is 0.
--- @param #number outer (optional) Maximal distance from the outer edge of the zone. Default is the radius of the zone.
--- @return Core.Point#POINT_VEC3 The @{Point#POINT_VEC3} object reflecting the random 3D location within the zone.
-function ZONE_RADIUS:GetRandomPointVec3( inner, outer )
- self:F( self.ZoneName, inner, outer )
-
- local PointVec3 = POINT_VEC3:NewFromVec2( self:GetRandomVec2() )
-
- self:T3( { PointVec3 } )
-
- return PointVec3
-end
-
-
-
--- @type ZONE
--- @extends Core.Zone#ZONE_RADIUS
-
-
---- # 3) ZONE class, extends @{Zone#ZONE_RADIUS}
---
--- The ZONE class, defined by the zone name as defined within the Mission Editor.
--- This class implements the inherited functions from @{#ZONE_RADIUS} taking into account the own zone format and properties.
---
--- ===
---
--- @field #ZONE ZONE
---
-ZONE = {
- ClassName="ZONE",
- }
-
-
---- Constructor of ZONE, taking the zone name.
--- @param #ZONE self
--- @param #string ZoneName The name of the zone as defined within the mission editor.
--- @return #ZONE
-function ZONE:New( ZoneName )
-
- local Zone = trigger.misc.getZone( ZoneName )
-
- if not Zone then
- error( "Zone " .. ZoneName .. " does not exist." )
- return nil
- end
-
- local self = BASE:Inherit( self, ZONE_RADIUS:New( ZoneName, { x = Zone.point.x, y = Zone.point.z }, Zone.radius ) )
- self:F( ZoneName )
-
- self.Zone = Zone
-
- return self
-end
-
-
---- The ZONE_UNIT class defined by a zone around a @{Unit#UNIT} with a radius.
--- @type ZONE_UNIT
--- @field Wrapper.Unit#UNIT ZoneUNIT
--- @extends Core.Zone#ZONE_RADIUS
-
---- # 4) #ZONE_UNIT class, extends @{Zone#ZONE_RADIUS}
---
--- The ZONE_UNIT class defined by a zone around a @{Unit#UNIT} with a radius.
--- This class implements the inherited functions from @{#ZONE_RADIUS} taking into account the own zone format and properties.
---
--- ===
---
--- @field #ZONE_UNIT ZONE_UNIT
---
-ZONE_UNIT = {
- ClassName="ZONE_UNIT",
- }
-
---- Constructor to create a ZONE_UNIT instance, taking the zone name, a zone unit and a radius.
--- @param #ZONE_UNIT self
--- @param #string ZoneName Name of the zone.
--- @param Wrapper.Unit#UNIT ZoneUNIT The unit as the center of the zone.
--- @param Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @return #ZONE_UNIT self
-function ZONE_UNIT:New( ZoneName, ZoneUNIT, Radius )
- local self = BASE:Inherit( self, ZONE_RADIUS:New( ZoneName, ZoneUNIT:GetVec2(), Radius ) )
- self:F( { ZoneName, ZoneUNIT:GetVec2(), Radius } )
-
- self.ZoneUNIT = ZoneUNIT
- self.LastVec2 = ZoneUNIT:GetVec2()
-
- return self
-end
-
-
---- Returns the current location of the @{Unit#UNIT}.
--- @param #ZONE_UNIT self
--- @return Dcs.DCSTypes#Vec2 The location of the zone based on the @{Unit#UNIT}location.
-function ZONE_UNIT:GetVec2()
- self:F( self.ZoneName )
-
- local ZoneVec2 = self.ZoneUNIT:GetVec2()
- if ZoneVec2 then
- self.LastVec2 = ZoneVec2
- return ZoneVec2
- else
- return self.LastVec2
- end
-
- self:T( { ZoneVec2 } )
-
- return nil
-end
-
---- Returns a random location within the zone.
--- @param #ZONE_UNIT self
--- @return Dcs.DCSTypes#Vec2 The random location within the zone.
-function ZONE_UNIT:GetRandomVec2()
- self:F( self.ZoneName )
-
- local RandomVec2 = {}
- local Vec2 = self.ZoneUNIT:GetVec2()
-
- if not Vec2 then
- Vec2 = self.LastVec2
- end
-
- local angle = math.random() * math.pi*2;
- RandomVec2.x = Vec2.x + math.cos( angle ) * math.random() * self:GetRadius();
- RandomVec2.y = Vec2.y + math.sin( angle ) * math.random() * self:GetRadius();
-
- self:T( { RandomVec2 } )
-
- return RandomVec2
-end
-
---- Returns the @{DCSTypes#Vec3} of the ZONE_UNIT.
--- @param #ZONE_UNIT self
--- @param Dcs.DCSTypes#Distance Height The height to add to the land height where the center of the zone is located.
--- @return Dcs.DCSTypes#Vec3 The point of the zone.
-function ZONE_UNIT:GetVec3( Height )
- self:F2( self.ZoneName )
-
- Height = Height or 0
-
- local Vec2 = self:GetVec2()
-
- local Vec3 = { x = Vec2.x, y = land.getHeight( self:GetVec2() ) + Height, z = Vec2.y }
-
- self:T2( { Vec3 } )
-
- return Vec3
-end
-
---- @type ZONE_GROUP
--- @field Wrapper.Group#GROUP ZoneGROUP
--- @extends Core.Zone#ZONE_RADIUS
-
-
---- # 5) #ZONE_GROUP class, extends @{Zone#ZONE_RADIUS}
---
--- The ZONE_GROUP class defines by a zone around a @{Group#GROUP} with a radius. The current leader of the group defines the center of the zone.
--- This class implements the inherited functions from @{Zone#ZONE_RADIUS} taking into account the own zone format and properties.
---
--- ===
---
--- @field #ZONE_GROUP ZONE_GROUP
---
-ZONE_GROUP = {
- ClassName="ZONE_GROUP",
- }
-
---- Constructor to create a ZONE_GROUP instance, taking the zone name, a zone @{Group#GROUP} and a radius.
--- @param #ZONE_GROUP self
--- @param #string ZoneName Name of the zone.
--- @param Wrapper.Group#GROUP ZoneGROUP The @{Group} as the center of the zone.
--- @param Dcs.DCSTypes#Distance Radius The radius of the zone.
--- @return #ZONE_GROUP self
-function ZONE_GROUP:New( ZoneName, ZoneGROUP, Radius )
- local self = BASE:Inherit( self, ZONE_RADIUS:New( ZoneName, ZoneGROUP:GetVec2(), Radius ) )
- self:F( { ZoneName, ZoneGROUP:GetVec2(), Radius } )
-
- self.ZoneGROUP = ZoneGROUP
-
- return self
-end
-
-
---- Returns the current location of the @{Group}.
--- @param #ZONE_GROUP self
--- @return Dcs.DCSTypes#Vec2 The location of the zone based on the @{Group} location.
-function ZONE_GROUP:GetVec2()
- self:F( self.ZoneName )
-
- local ZoneVec2 = self.ZoneGROUP:GetVec2()
-
- self:T( { ZoneVec2 } )
-
- return ZoneVec2
-end
-
---- Returns a random location within the zone of the @{Group}.
--- @param #ZONE_GROUP self
--- @return Dcs.DCSTypes#Vec2 The random location of the zone based on the @{Group} location.
-function ZONE_GROUP:GetRandomVec2()
- self:F( self.ZoneName )
-
- local Point = {}
- local Vec2 = self.ZoneGROUP:GetVec2()
-
- local angle = math.random() * math.pi*2;
- Point.x = Vec2.x + math.cos( angle ) * math.random() * self:GetRadius();
- Point.y = Vec2.y + math.sin( angle ) * math.random() * self:GetRadius();
-
- self:T( { Point } )
-
- return Point
-end
-
-
-
---- @type ZONE_POLYGON_BASE
--- @field #ZONE_POLYGON_BASE.ListVec2 Polygon The polygon defined by an array of @{DCSTypes#Vec2}.
--- @extends Core.Zone#ZONE_BASE
-
-
---- # 6) ZONE_POLYGON_BASE class, extends @{Zone#ZONE_BASE}
---
--- The ZONE_POLYGON_BASE class defined by a sequence of @{Group#GROUP} waypoints within the Mission Editor, forming a polygon.
--- This class implements the inherited functions from @{Zone#ZONE_RADIUS} taking into account the own zone format and properties.
--- This class is an abstract BASE class for derived classes, and is not meant to be instantiated.
---
--- ## 6.1) Zone point randomization
---
--- Various functions exist to find random points within the zone.
---
--- * @{#ZONE_POLYGON_BASE.GetRandomVec2}(): Gets a random 2D point in the zone.
--- * @{#ZONE_POLYGON_BASE.GetRandomPointVec2}(): Return a @{Point#POINT_VEC2} object representing a random 2D point within the zone.
--- * @{#ZONE_POLYGON_BASE.GetRandomPointVec3}(): Return a @{Point#POINT_VEC3} object representing a random 3D point at landheight within the zone.
---
--- ===
---
--- @field #ZONE_POLYGON_BASE ZONE_POLYGON_BASE
---
-ZONE_POLYGON_BASE = {
- ClassName="ZONE_POLYGON_BASE",
- }
-
---- A points array.
--- @type ZONE_POLYGON_BASE.ListVec2
--- @list
-
---- Constructor to create a ZONE_POLYGON_BASE instance, taking the zone name and an array of @{DCSTypes#Vec2}, forming a polygon.
--- The @{Group#GROUP} waypoints define the polygon corners. The first and the last point are automatically connected.
--- @param #ZONE_POLYGON_BASE self
--- @param #string ZoneName Name of the zone.
--- @param #ZONE_POLYGON_BASE.ListVec2 PointsArray An array of @{DCSTypes#Vec2}, forming a polygon..
--- @return #ZONE_POLYGON_BASE self
-function ZONE_POLYGON_BASE:New( ZoneName, PointsArray )
- local self = BASE:Inherit( self, ZONE_BASE:New( ZoneName ) )
- self:F( { ZoneName, PointsArray } )
-
- local i = 0
-
- self.Polygon = {}
-
- for i = 1, #PointsArray do
- self.Polygon[i] = {}
- self.Polygon[i].x = PointsArray[i].x
- self.Polygon[i].y = PointsArray[i].y
- end
-
- return self
-end
-
---- Flush polygon coordinates as a table in DCS.log.
--- @param #ZONE_POLYGON_BASE self
--- @return #ZONE_POLYGON_BASE self
-function ZONE_POLYGON_BASE:Flush()
- self:F2()
-
- self:E( { Polygon = self.ZoneName, Coordinates = self.Polygon } )
-
- return self
-end
-
---- Smokes the zone boundaries in a color.
--- @param #ZONE_POLYGON_BASE self
--- @param #boolean UnBound If true, the tyres will be destroyed.
--- @return #ZONE_POLYGON_BASE self
-function ZONE_POLYGON_BASE:BoundZone( UnBound )
-
- local i
- local j
- local Segments = 10
-
- i = 1
- j = #self.Polygon
-
- while i <= #self.Polygon do
- self:T( { i, j, self.Polygon[i], self.Polygon[j] } )
-
- local DeltaX = self.Polygon[j].x - self.Polygon[i].x
- local DeltaY = self.Polygon[j].y - self.Polygon[i].y
-
- for Segment = 0, Segments do -- We divide each line in 5 segments and smoke a point on the line.
- local PointX = self.Polygon[i].x + ( Segment * DeltaX / Segments )
- local PointY = self.Polygon[i].y + ( Segment * DeltaY / Segments )
- local Tire = {
- ["country"] = "USA",
- ["category"] = "Fortifications",
- ["canCargo"] = false,
- ["shape_name"] = "H-tyre_B_WF",
- ["type"] = "Black_Tyre_WF",
- ["y"] = PointY,
- ["x"] = PointX,
- ["name"] = string.format( "%s-Tire #%0d", self:GetName(), ((i - 1) * Segments) + Segment ),
- ["heading"] = 0,
- } -- end of ["group"]
-
- local Group = coalition.addStaticObject( country.id.USA, Tire )
- if UnBound and UnBound == true then
- Group:destroy()
- end
-
- end
- j = i
- i = i + 1
- end
-
- return self
-end
-
-
-
---- Smokes the zone boundaries in a color.
--- @param #ZONE_POLYGON_BASE self
--- @param Utilities.Utils#SMOKECOLOR SmokeColor The smoke color.
--- @return #ZONE_POLYGON_BASE self
-function ZONE_POLYGON_BASE:SmokeZone( SmokeColor )
- self:F2( SmokeColor )
-
- local i
- local j
- local Segments = 10
-
- i = 1
- j = #self.Polygon
-
- while i <= #self.Polygon do
- self:T( { i, j, self.Polygon[i], self.Polygon[j] } )
-
- local DeltaX = self.Polygon[j].x - self.Polygon[i].x
- local DeltaY = self.Polygon[j].y - self.Polygon[i].y
-
- for Segment = 0, Segments do -- We divide each line in 5 segments and smoke a point on the line.
- local PointX = self.Polygon[i].x + ( Segment * DeltaX / Segments )
- local PointY = self.Polygon[i].y + ( Segment * DeltaY / Segments )
- POINT_VEC2:New( PointX, PointY ):Smoke( SmokeColor )
- end
- j = i
- i = i + 1
- end
-
- return self
-end
-
-
-
-
---- Returns if a location is within the zone.
--- Source learned and taken from: https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
--- @param #ZONE_POLYGON_BASE self
--- @param Dcs.DCSTypes#Vec2 Vec2 The location to test.
--- @return #boolean true if the location is within the zone.
-function ZONE_POLYGON_BASE:IsVec2InZone( Vec2 )
- self:F2( Vec2 )
-
- local Next
- local Prev
- local InPolygon = false
-
- Next = 1
- Prev = #self.Polygon
-
- while Next <= #self.Polygon do
- self:T( { Next, Prev, self.Polygon[Next], self.Polygon[Prev] } )
- if ( ( ( self.Polygon[Next].y > Vec2.y ) ~= ( self.Polygon[Prev].y > Vec2.y ) ) and
- ( Vec2.x < ( self.Polygon[Prev].x - self.Polygon[Next].x ) * ( Vec2.y - self.Polygon[Next].y ) / ( self.Polygon[Prev].y - self.Polygon[Next].y ) + self.Polygon[Next].x )
- ) then
- InPolygon = not InPolygon
- end
- self:T2( { InPolygon = InPolygon } )
- Prev = Next
- Next = Next + 1
- end
-
- self:T( { InPolygon = InPolygon } )
- return InPolygon
-end
-
---- Define a random @{DCSTypes#Vec2} within the zone.
--- @param #ZONE_POLYGON_BASE self
--- @return Dcs.DCSTypes#Vec2 The Vec2 coordinate.
-function ZONE_POLYGON_BASE:GetRandomVec2()
- self:F2()
-
- --- It is a bit tricky to find a random point within a polygon. Right now i am doing it the dirty and inefficient way...
- local Vec2Found = false
- local Vec2
- local BS = self:GetBoundingSquare()
-
- self:T2( BS )
-
- while Vec2Found == false do
- Vec2 = { x = math.random( BS.x1, BS.x2 ), y = math.random( BS.y1, BS.y2 ) }
- self:T2( Vec2 )
- if self:IsVec2InZone( Vec2 ) then
- Vec2Found = true
- end
- end
-
- self:T2( Vec2 )
-
- return Vec2
-end
-
---- Return a @{Point#POINT_VEC2} object representing a random 2D point at landheight within the zone.
--- @param #ZONE_POLYGON_BASE self
--- @return @{Point#POINT_VEC2}
-function ZONE_POLYGON_BASE:GetRandomPointVec2()
- self:F2()
-
- local PointVec2 = POINT_VEC2:NewFromVec2( self:GetRandomVec2() )
-
- self:T2( PointVec2 )
-
- return PointVec2
-end
-
---- Return a @{Point#POINT_VEC3} object representing a random 3D point at landheight within the zone.
--- @param #ZONE_POLYGON_BASE self
--- @return @{Point#POINT_VEC3}
-function ZONE_POLYGON_BASE:GetRandomPointVec3()
- self:F2()
-
- local PointVec3 = POINT_VEC3:NewFromVec2( self:GetRandomVec2() )
-
- self:T2( PointVec3 )
-
- return PointVec3
-end
-
-
---- Get the bounding square the zone.
--- @param #ZONE_POLYGON_BASE self
--- @return #ZONE_POLYGON_BASE.BoundingSquare The bounding square.
-function ZONE_POLYGON_BASE:GetBoundingSquare()
-
- local x1 = self.Polygon[1].x
- local y1 = self.Polygon[1].y
- local x2 = self.Polygon[1].x
- local y2 = self.Polygon[1].y
-
- for i = 2, #self.Polygon do
- self:T2( { self.Polygon[i], x1, y1, x2, y2 } )
- x1 = ( x1 > self.Polygon[i].x ) and self.Polygon[i].x or x1
- x2 = ( x2 < self.Polygon[i].x ) and self.Polygon[i].x or x2
- y1 = ( y1 > self.Polygon[i].y ) and self.Polygon[i].y or y1
- y2 = ( y2 < self.Polygon[i].y ) and self.Polygon[i].y or y2
-
- end
-
- return { x1 = x1, y1 = y1, x2 = x2, y2 = y2 }
-end
-
-
---- @type ZONE_POLYGON
--- @extends Core.Zone#ZONE_POLYGON_BASE
-
-
---- # 7) ZONE_POLYGON class, extends @{Zone#ZONE_POLYGON_BASE}
---
--- The ZONE_POLYGON class defined by a sequence of @{Group#GROUP} waypoints within the Mission Editor, forming a polygon.
--- This class implements the inherited functions from @{Zone#ZONE_RADIUS} taking into account the own zone format and properties.
---
--- ===
---
--- @field #ZONE_POLYGON ZONE_POLYGON
---
-ZONE_POLYGON = {
- ClassName="ZONE_POLYGON",
- }
-
---- Constructor to create a ZONE_POLYGON instance, taking the zone name and the name of the @{Group#GROUP} defined within the Mission Editor.
--- The @{Group#GROUP} waypoints define the polygon corners. The first and the last point are automatically connected by ZONE_POLYGON.
--- @param #ZONE_POLYGON self
--- @param #string ZoneName Name of the zone.
--- @param Wrapper.Group#GROUP ZoneGroup The GROUP waypoints as defined within the Mission Editor define the polygon shape.
--- @return #ZONE_POLYGON self
-function ZONE_POLYGON:New( ZoneName, ZoneGroup )
-
- local GroupPoints = ZoneGroup:GetTaskRoute()
-
- local self = BASE:Inherit( self, ZONE_POLYGON_BASE:New( ZoneName, GroupPoints ) )
- self:F( { ZoneName, ZoneGroup, self.Polygon } )
-
- return self
-end
-
---- This module contains the DATABASE class, managing the database of mission objects.
---
--- ====
---
--- 1) @{#DATABASE} class, extends @{Base#BASE}
--- ===================================================
--- Mission designers can use the DATABASE class to refer to:
---
--- * UNITS
--- * GROUPS
--- * CLIENTS
--- * AIRPORTS
--- * PLAYERSJOINED
--- * PLAYERS
---
--- On top, for internal MOOSE administration purposes, the DATBASE administers the Unit and Group TEMPLATES as defined within the Mission Editor.
---
--- Moose will automatically create one instance of the DATABASE class into the **global** object _DATABASE.
--- Moose refers to _DATABASE within the framework extensively, but you can also refer to the _DATABASE object within your missions if required.
---
--- 1.1) DATABASE iterators
--- -----------------------
--- You can iterate the database with the available iterator methods.
--- The iterator methods will walk the DATABASE set, and call for each element within the set a function that you provide.
--- The following iterator methods are currently available within the DATABASE:
---
--- * @{#DATABASE.ForEachUnit}: Calls a function for each @{UNIT} it finds within the DATABASE.
--- * @{#DATABASE.ForEachGroup}: Calls a function for each @{GROUP} it finds within the DATABASE.
--- * @{#DATABASE.ForEachPlayer}: Calls a function for each alive player it finds within the DATABASE.
--- * @{#DATABASE.ForEachPlayerJoined}: Calls a function for each joined player it finds within the DATABASE.
--- * @{#DATABASE.ForEachClient}: Calls a function for each @{CLIENT} it finds within the DATABASE.
--- * @{#DATABASE.ForEachClientAlive}: Calls a function for each alive @{CLIENT} it finds within the DATABASE.
---
--- ===
---
--- @module Database
--- @author FlightControl
-
---- DATABASE class
--- @type DATABASE
--- @extends Core.Base#BASE
-DATABASE = {
- ClassName = "DATABASE",
- Templates = {
- Units = {},
- Groups = {},
- ClientsByName = {},
- ClientsByID = {},
- },
- UNITS = {},
- STATICS = {},
- GROUPS = {},
- PLAYERS = {},
- PLAYERSJOINED = {},
- CLIENTS = {},
- AIRBASES = {},
- COUNTRY_ID = {},
- COUNTRY_NAME = {},
- NavPoints = {},
-}
-
-local _DATABASECoalition =
- {
- [1] = "Red",
- [2] = "Blue",
- }
-
-local _DATABASECategory =
- {
- ["plane"] = Unit.Category.AIRPLANE,
- ["helicopter"] = Unit.Category.HELICOPTER,
- ["vehicle"] = Unit.Category.GROUND_UNIT,
- ["ship"] = Unit.Category.SHIP,
- ["static"] = Unit.Category.STRUCTURE,
- }
-
-
---- Creates a new DATABASE object, building a set of units belonging to a coalitions, categories, countries, types or with defined prefix names.
--- @param #DATABASE self
--- @return #DATABASE
--- @usage
--- -- Define a new DATABASE Object. This DBObject will contain a reference to all Group and Unit Templates defined within the ME and the DCSRTE.
--- DBObject = DATABASE:New()
-function DATABASE:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, BASE:New() )
-
- self:SetEventPriority( 1 )
-
- self:HandleEvent( EVENTS.Birth, self._EventOnBirth )
- self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash )
- self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash )
-
- -- Follow alive players and clients
- self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit )
- self:HandleEvent( EVENTS.PlayerLeaveUnit, self._EventOnPlayerLeaveUnit )
-
- self:_RegisterTemplates()
- self:_RegisterGroupsAndUnits()
- self:_RegisterClients()
- self:_RegisterStatics()
- self:_RegisterPlayers()
- self:_RegisterAirbases()
-
- return self
-end
-
---- Finds a Unit based on the Unit Name.
--- @param #DATABASE self
--- @param #string UnitName
--- @return Wrapper.Unit#UNIT The found Unit.
-function DATABASE:FindUnit( UnitName )
-
- local UnitFound = self.UNITS[UnitName]
- return UnitFound
-end
-
-
---- Adds a Unit based on the Unit Name in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddUnit( DCSUnitName )
-
- if not self.UNITS[DCSUnitName] then
- local UnitRegister = UNIT:Register( DCSUnitName )
- self.UNITS[DCSUnitName] = UNIT:Register( DCSUnitName )
- end
-
- return self.UNITS[DCSUnitName]
-end
-
-
---- Deletes a Unit from the DATABASE based on the Unit Name.
--- @param #DATABASE self
-function DATABASE:DeleteUnit( DCSUnitName )
-
- --self.UNITS[DCSUnitName] = nil
-end
-
---- Adds a Static based on the Static Name in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddStatic( DCSStaticName )
-
- if not self.STATICS[DCSStaticName] then
- self.STATICS[DCSStaticName] = STATIC:Register( DCSStaticName )
- end
-end
-
-
---- Deletes a Static from the DATABASE based on the Static Name.
--- @param #DATABASE self
-function DATABASE:DeleteStatic( DCSStaticName )
-
- --self.STATICS[DCSStaticName] = nil
-end
-
---- Finds a STATIC based on the StaticName.
--- @param #DATABASE self
--- @param #string StaticName
--- @return Wrapper.Static#STATIC The found STATIC.
-function DATABASE:FindStatic( StaticName )
-
- local StaticFound = self.STATICS[StaticName]
- return StaticFound
-end
-
---- Adds a Airbase based on the Airbase Name in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddAirbase( DCSAirbaseName )
-
- if not self.AIRBASES[DCSAirbaseName] then
- self.AIRBASES[DCSAirbaseName] = AIRBASE:Register( DCSAirbaseName )
- end
-end
-
-
---- Deletes a Airbase from the DATABASE based on the Airbase Name.
--- @param #DATABASE self
-function DATABASE:DeleteAirbase( DCSAirbaseName )
-
- --self.AIRBASES[DCSAirbaseName] = nil
-end
-
---- Finds a AIRBASE based on the AirbaseName.
--- @param #DATABASE self
--- @param #string AirbaseName
--- @return Wrapper.Airbase#AIRBASE The found AIRBASE.
-function DATABASE:FindAirbase( AirbaseName )
-
- local AirbaseFound = self.AIRBASES[AirbaseName]
- return AirbaseFound
-end
-
-
---- Finds a CLIENT based on the ClientName.
--- @param #DATABASE self
--- @param #string ClientName
--- @return Wrapper.Client#CLIENT The found CLIENT.
-function DATABASE:FindClient( ClientName )
-
- local ClientFound = self.CLIENTS[ClientName]
- return ClientFound
-end
-
-
---- Adds a CLIENT based on the ClientName in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddClient( ClientName )
-
- if not self.CLIENTS[ClientName] then
- self.CLIENTS[ClientName] = CLIENT:Register( ClientName )
- end
-
- return self.CLIENTS[ClientName]
-end
-
-
---- Finds a GROUP based on the GroupName.
--- @param #DATABASE self
--- @param #string GroupName
--- @return Wrapper.Group#GROUP The found GROUP.
-function DATABASE:FindGroup( GroupName )
-
- local GroupFound = self.GROUPS[GroupName]
- return GroupFound
-end
-
-
---- Adds a GROUP based on the GroupName in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddGroup( GroupName )
-
- if not self.GROUPS[GroupName] then
- self:E( { "Add GROUP:", GroupName } )
- self.GROUPS[GroupName] = GROUP:Register( GroupName )
- end
-
- return self.GROUPS[GroupName]
-end
-
---- Adds a player based on the Player Name in the DATABASE.
--- @param #DATABASE self
-function DATABASE:AddPlayer( UnitName, PlayerName )
-
- if PlayerName then
- self:E( { "Add player for unit:", UnitName, PlayerName } )
- self.PLAYERS[PlayerName] = self:FindUnit( UnitName )
- self.PLAYERSJOINED[PlayerName] = PlayerName
- end
-end
-
---- Deletes a player from the DATABASE based on the Player Name.
--- @param #DATABASE self
-function DATABASE:DeletePlayer( PlayerName )
-
- if PlayerName then
- self:E( { "Clean player:", PlayerName } )
- self.PLAYERS[PlayerName] = nil
- end
-end
-
-
---- Instantiate new Groups within the DCSRTE.
--- This method expects EXACTLY the same structure as a structure within the ME, and needs 2 additional fields defined:
--- SpawnCountryID, SpawnCategoryID
--- This method is used by the SPAWN class.
--- @param #DATABASE self
--- @param #table SpawnTemplate
--- @return #DATABASE self
-function DATABASE:Spawn( SpawnTemplate )
- self:F( SpawnTemplate.name )
-
- self:T( { SpawnTemplate.SpawnCountryID, SpawnTemplate.SpawnCategoryID } )
-
- -- Copy the spawn variables of the template in temporary storage, nullify, and restore the spawn variables.
- local SpawnCoalitionID = SpawnTemplate.CoalitionID
- local SpawnCountryID = SpawnTemplate.CountryID
- local SpawnCategoryID = SpawnTemplate.CategoryID
-
- -- Nullify
- SpawnTemplate.CoalitionID = nil
- SpawnTemplate.CountryID = nil
- SpawnTemplate.CategoryID = nil
-
- self:_RegisterTemplate( SpawnTemplate, SpawnCoalitionID, SpawnCategoryID, SpawnCountryID )
-
- self:T3( SpawnTemplate )
- coalition.addGroup( SpawnCountryID, SpawnCategoryID, SpawnTemplate )
-
- -- Restore
- SpawnTemplate.CoalitionID = SpawnCoalitionID
- SpawnTemplate.CountryID = SpawnCountryID
- SpawnTemplate.CategoryID = SpawnCategoryID
-
- local SpawnGroup = self:AddGroup( SpawnTemplate.name )
- return SpawnGroup
-end
-
---- Set a status to a Group within the Database, this to check crossing events for example.
-function DATABASE:SetStatusGroup( GroupName, Status )
- self:F2( Status )
-
- self.Templates.Groups[GroupName].Status = Status
-end
-
---- Get a status to a Group within the Database, this to check crossing events for example.
-function DATABASE:GetStatusGroup( GroupName )
- self:F2( Status )
-
- if self.Templates.Groups[GroupName] then
- return self.Templates.Groups[GroupName].Status
- else
- return ""
- end
-end
-
---- Private method that registers new Group Templates within the DATABASE Object.
--- @param #DATABASE self
--- @param #table GroupTemplate
--- @return #DATABASE self
-function DATABASE:_RegisterTemplate( GroupTemplate, CoalitionID, CategoryID, CountryID )
-
- local GroupTemplateName = env.getValueDictByKey(GroupTemplate.name)
-
- local TraceTable = {}
-
- if not self.Templates.Groups[GroupTemplateName] then
- self.Templates.Groups[GroupTemplateName] = {}
- self.Templates.Groups[GroupTemplateName].Status = nil
- end
-
- -- Delete the spans from the route, it is not needed and takes memory.
- if GroupTemplate.route and GroupTemplate.route.spans then
- GroupTemplate.route.spans = nil
- end
-
- GroupTemplate.CategoryID = CategoryID
- GroupTemplate.CoalitionID = CoalitionID
- GroupTemplate.CountryID = CountryID
-
- self.Templates.Groups[GroupTemplateName].GroupName = GroupTemplateName
- self.Templates.Groups[GroupTemplateName].Template = GroupTemplate
- self.Templates.Groups[GroupTemplateName].groupId = GroupTemplate.groupId
- self.Templates.Groups[GroupTemplateName].UnitCount = #GroupTemplate.units
- self.Templates.Groups[GroupTemplateName].Units = GroupTemplate.units
- self.Templates.Groups[GroupTemplateName].CategoryID = CategoryID
- self.Templates.Groups[GroupTemplateName].CoalitionID = CoalitionID
- self.Templates.Groups[GroupTemplateName].CountryID = CountryID
-
-
- TraceTable[#TraceTable+1] = "Group"
- TraceTable[#TraceTable+1] = self.Templates.Groups[GroupTemplateName].GroupName
-
- TraceTable[#TraceTable+1] = "Coalition"
- TraceTable[#TraceTable+1] = self.Templates.Groups[GroupTemplateName].CoalitionID
- TraceTable[#TraceTable+1] = "Category"
- TraceTable[#TraceTable+1] = self.Templates.Groups[GroupTemplateName].CategoryID
- TraceTable[#TraceTable+1] = "Country"
- TraceTable[#TraceTable+1] = self.Templates.Groups[GroupTemplateName].CountryID
-
- TraceTable[#TraceTable+1] = "Units"
-
- for unit_num, UnitTemplate in pairs( GroupTemplate.units ) do
-
- UnitTemplate.name = env.getValueDictByKey(UnitTemplate.name)
-
- self.Templates.Units[UnitTemplate.name] = {}
- self.Templates.Units[UnitTemplate.name].UnitName = UnitTemplate.name
- self.Templates.Units[UnitTemplate.name].Template = UnitTemplate
- self.Templates.Units[UnitTemplate.name].GroupName = GroupTemplateName
- self.Templates.Units[UnitTemplate.name].GroupTemplate = GroupTemplate
- self.Templates.Units[UnitTemplate.name].GroupId = GroupTemplate.groupId
- self.Templates.Units[UnitTemplate.name].CategoryID = CategoryID
- self.Templates.Units[UnitTemplate.name].CoalitionID = CoalitionID
- self.Templates.Units[UnitTemplate.name].CountryID = CountryID
-
- if UnitTemplate.skill and (UnitTemplate.skill == "Client" or UnitTemplate.skill == "Player") then
- self.Templates.ClientsByName[UnitTemplate.name] = UnitTemplate
- self.Templates.ClientsByName[UnitTemplate.name].CategoryID = CategoryID
- self.Templates.ClientsByName[UnitTemplate.name].CoalitionID = CoalitionID
- self.Templates.ClientsByName[UnitTemplate.name].CountryID = CountryID
- self.Templates.ClientsByID[UnitTemplate.unitId] = UnitTemplate
- end
-
- TraceTable[#TraceTable+1] = self.Templates.Units[UnitTemplate.name].UnitName
- end
-
- self:E( TraceTable )
-end
-
-function DATABASE:GetGroupTemplate( GroupName )
- local GroupTemplate = self.Templates.Groups[GroupName].Template
- GroupTemplate.SpawnCoalitionID = self.Templates.Groups[GroupName].CoalitionID
- GroupTemplate.SpawnCategoryID = self.Templates.Groups[GroupName].CategoryID
- GroupTemplate.SpawnCountryID = self.Templates.Groups[GroupName].CountryID
- return GroupTemplate
-end
-
-function DATABASE:GetGroupNameFromUnitName( UnitName )
- return self.Templates.Units[UnitName].GroupName
-end
-
-function DATABASE:GetGroupTemplateFromUnitName( UnitName )
- return self.Templates.Units[UnitName].GroupTemplate
-end
-
-function DATABASE:GetCoalitionFromClientTemplate( ClientName )
- return self.Templates.ClientsByName[ClientName].CoalitionID
-end
-
-function DATABASE:GetCategoryFromClientTemplate( ClientName )
- return self.Templates.ClientsByName[ClientName].CategoryID
-end
-
-function DATABASE:GetCountryFromClientTemplate( ClientName )
- return self.Templates.ClientsByName[ClientName].CountryID
-end
-
---- Airbase
-
-function DATABASE:GetCoalitionFromAirbase( AirbaseName )
- return self.AIRBASES[AirbaseName]:GetCoalition()
-end
-
-function DATABASE:GetCategoryFromAirbase( AirbaseName )
- return self.AIRBASES[AirbaseName]:GetCategory()
-end
-
-
-
---- Private method that registers all alive players in the mission.
--- @param #DATABASE self
--- @return #DATABASE self
-function DATABASE:_RegisterPlayers()
-
- local CoalitionsData = { AlivePlayersRed = coalition.getPlayers( coalition.side.RED ), AlivePlayersBlue = coalition.getPlayers( coalition.side.BLUE ) }
- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
- for UnitId, UnitData in pairs( CoalitionData ) do
- self:T3( { "UnitData:", UnitData } )
- if UnitData and UnitData:isExist() then
- local UnitName = UnitData:getName()
- local PlayerName = UnitData:getPlayerName()
- if not self.PLAYERS[PlayerName] then
- self:E( { "Add player for unit:", UnitName, PlayerName } )
- self:AddPlayer( UnitName, PlayerName )
- end
- end
- end
- end
-
- return self
-end
-
-
---- Private method that registers all Groups and Units within in the mission.
--- @param #DATABASE self
--- @return #DATABASE self
-function DATABASE:_RegisterGroupsAndUnits()
-
- local CoalitionsData = { GroupsRed = coalition.getGroups( coalition.side.RED ), GroupsBlue = coalition.getGroups( coalition.side.BLUE ) }
- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
- for DCSGroupId, DCSGroup in pairs( CoalitionData ) do
-
- if DCSGroup:isExist() then
- local DCSGroupName = DCSGroup:getName()
-
- self:E( { "Register Group:", DCSGroupName } )
- self:AddGroup( DCSGroupName )
-
- for DCSUnitId, DCSUnit in pairs( DCSGroup:getUnits() ) do
-
- local DCSUnitName = DCSUnit:getName()
- self:E( { "Register Unit:", DCSUnitName } )
- self:AddUnit( DCSUnitName )
- end
- else
- self:E( { "Group does not exist: ", DCSGroup } )
- end
-
- end
- end
-
- return self
-end
-
---- Private method that registers all Units of skill Client or Player within in the mission.
--- @param #DATABASE self
--- @return #DATABASE self
-function DATABASE:_RegisterClients()
-
- for ClientName, ClientTemplate in pairs( self.Templates.ClientsByName ) do
- self:E( { "Register Client:", ClientName } )
- self:AddClient( ClientName )
- end
-
- return self
-end
-
---- @param #DATABASE self
-function DATABASE:_RegisterStatics()
-
- local CoalitionsData = { GroupsRed = coalition.getStaticObjects( coalition.side.RED ), GroupsBlue = coalition.getStaticObjects( coalition.side.BLUE ) }
- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
- for DCSStaticId, DCSStatic in pairs( CoalitionData ) do
-
- if DCSStatic:isExist() then
- local DCSStaticName = DCSStatic:getName()
-
- self:E( { "Register Static:", DCSStaticName } )
- self:AddStatic( DCSStaticName )
- else
- self:E( { "Static does not exist: ", DCSStatic } )
- end
- end
- end
-
- return self
-end
-
---- @param #DATABASE self
-function DATABASE:_RegisterAirbases()
-
- local CoalitionsData = { AirbasesRed = coalition.getAirbases( coalition.side.RED ), AirbasesBlue = coalition.getAirbases( coalition.side.BLUE ), AirbasesNeutral = coalition.getAirbases( coalition.side.NEUTRAL ) }
- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
- for DCSAirbaseId, DCSAirbase in pairs( CoalitionData ) do
-
- local DCSAirbaseName = DCSAirbase:getName()
-
- self:E( { "Register Airbase:", DCSAirbaseName } )
- self:AddAirbase( DCSAirbaseName )
- end
- end
-
- return self
-end
-
-
---- Events
-
---- Handles the OnBirth event for the alive units set.
--- @param #DATABASE self
--- @param Core.Event#EVENTDATA Event
-function DATABASE:_EventOnBirth( Event )
- self:F2( { Event } )
-
- if Event.IniDCSUnit then
- if Event.IniObjectCategory == 3 then
- self:AddStatic( Event.IniDCSUnitName )
- else
- if Event.IniObjectCategory == 1 then
- self:AddUnit( Event.IniDCSUnitName )
- self:AddGroup( Event.IniDCSGroupName )
- end
- end
- self:_EventOnPlayerEnterUnit( Event )
- end
-end
-
-
---- Handles the OnDead or OnCrash event for alive units set.
--- @param #DATABASE self
--- @param Core.Event#EVENTDATA Event
-function DATABASE:_EventOnDeadOrCrash( Event )
- self:F2( { Event } )
-
- if Event.IniDCSUnit then
- if Event.IniObjectCategory == 3 then
- if self.STATICS[Event.IniDCSUnitName] then
- self:DeleteStatic( Event.IniDCSUnitName )
- end
- else
- if Event.IniObjectCategory == 1 then
- if self.UNITS[Event.IniDCSUnitName] then
- self:DeleteUnit( Event.IniDCSUnitName )
- end
- end
- end
- end
-end
-
-
---- Handles the OnPlayerEnterUnit event to fill the active players table (with the unit filter applied).
--- @param #DATABASE self
--- @param Core.Event#EVENTDATA Event
-function DATABASE:_EventOnPlayerEnterUnit( Event )
- self:F2( { Event } )
-
- if Event.IniUnit then
- if Event.IniObjectCategory == 1 then
- self:AddUnit( Event.IniDCSUnitName )
- self:AddGroup( Event.IniDCSGroupName )
- local PlayerName = Event.IniUnit:GetPlayerName()
- if not self.PLAYERS[PlayerName] then
- self:AddPlayer( Event.IniUnitName, PlayerName )
- end
- end
- end
-end
-
-
---- Handles the OnPlayerLeaveUnit event to clean the active players table.
--- @param #DATABASE self
--- @param Core.Event#EVENTDATA Event
-function DATABASE:_EventOnPlayerLeaveUnit( Event )
- self:F2( { Event } )
-
- if Event.IniUnit then
- if Event.IniObjectCategory == 1 then
- local PlayerName = Event.IniUnit:GetPlayerName()
- if self.PLAYERS[PlayerName] then
- self:DeletePlayer( PlayerName )
- end
- end
- end
-end
-
---- Iterators
-
---- Iterate the DATABASE and call an iterator function for the given set, providing the Object for each element within the set and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is an alive player in the database.
--- @return #DATABASE self
-function DATABASE:ForEach( IteratorFunction, FinalizeFunction, arg, Set )
- self:F2( arg )
-
- local function CoRoutine()
- local Count = 0
- for ObjectID, Object in pairs( Set ) do
- self:T2( Object )
- IteratorFunction( Object, unpack( arg ) )
- Count = Count + 1
--- if Count % 100 == 0 then
--- coroutine.yield( false )
--- end
- end
- return true
- end
-
--- local co = coroutine.create( CoRoutine )
- local co = CoRoutine
-
- local function Schedule()
-
--- local status, res = coroutine.resume( co )
- local status, res = co()
- self:T3( { status, res } )
-
- if status == false then
- error( res )
- end
- if res == false then
- return true -- resume next time the loop
- end
- if FinalizeFunction then
- FinalizeFunction( unpack( arg ) )
- end
- return false
- end
-
- local Scheduler = SCHEDULER:New( self, Schedule, {}, 0.001, 0.001, 0 )
-
- return self
-end
-
-
---- Iterate the DATABASE and call an iterator function for each **alive** UNIT, providing the UNIT and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is an alive UNIT in the database. The function needs to accept a UNIT parameter.
--- @return #DATABASE self
-function DATABASE:ForEachUnit( IteratorFunction, FinalizeFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, FinalizeFunction, arg, self.UNITS )
-
- return self
-end
-
---- Iterate the DATABASE and call an iterator function for each **alive** GROUP, providing the GROUP and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is an alive GROUP in the database. The function needs to accept a GROUP parameter.
--- @return #DATABASE self
-function DATABASE:ForEachGroup( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.GROUPS )
-
- return self
-end
-
-
---- Iterate the DATABASE and call an iterator function for each **ALIVE** player, providing the player name and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is an player in the database. The function needs to accept the player name.
--- @return #DATABASE self
-function DATABASE:ForEachPlayer( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.PLAYERS )
-
- return self
-end
-
-
---- Iterate the DATABASE and call an iterator function for each player who has joined the mission, providing the Unit of the player and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is was a player in the database. The function needs to accept a UNIT parameter.
--- @return #DATABASE self
-function DATABASE:ForEachPlayerJoined( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.PLAYERSJOINED )
-
- return self
-end
-
---- Iterate the DATABASE and call an iterator function for each CLIENT, providing the CLIENT to the function and optional parameters.
--- @param #DATABASE self
--- @param #function IteratorFunction The function that will be called when there is an alive player in the database. The function needs to accept a CLIENT parameter.
--- @return #DATABASE self
-function DATABASE:ForEachClient( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.CLIENTS )
-
- return self
-end
-
-
-function DATABASE:_RegisterTemplates()
- self:F2()
-
- self.Navpoints = {}
- self.UNITS = {}
- --Build routines.db.units and self.Navpoints
- for CoalitionName, coa_data in pairs(env.mission.coalition) do
-
- if (CoalitionName == 'red' or CoalitionName == 'blue') and type(coa_data) == 'table' then
- --self.Units[coa_name] = {}
-
- local CoalitionSide = coalition.side[string.upper(CoalitionName)]
-
- ----------------------------------------------
- -- build nav points DB
- self.Navpoints[CoalitionName] = {}
- if coa_data.nav_points then --navpoints
- for nav_ind, nav_data in pairs(coa_data.nav_points) do
-
- if type(nav_data) == 'table' then
- self.Navpoints[CoalitionName][nav_ind] = routines.utils.deepCopy(nav_data)
-
- self.Navpoints[CoalitionName][nav_ind]['name'] = nav_data.callsignStr -- name is a little bit more self-explanatory.
- self.Navpoints[CoalitionName][nav_ind]['point'] = {} -- point is used by SSE, support it.
- self.Navpoints[CoalitionName][nav_ind]['point']['x'] = nav_data.x
- self.Navpoints[CoalitionName][nav_ind]['point']['y'] = 0
- self.Navpoints[CoalitionName][nav_ind]['point']['z'] = nav_data.y
- end
- end
- end
- -------------------------------------------------
- if coa_data.country then --there is a country table
- for cntry_id, cntry_data in pairs(coa_data.country) do
-
- local CountryName = string.upper(cntry_data.name)
- local CountryID = cntry_data.id
-
- self.COUNTRY_ID[CountryName] = CountryID
- self.COUNTRY_NAME[CountryID] = CountryName
-
- --self.Units[coa_name][countryName] = {}
- --self.Units[coa_name][countryName]["countryId"] = cntry_data.id
-
- if type(cntry_data) == 'table' then --just making sure
-
- for obj_type_name, obj_type_data in pairs(cntry_data) do
-
- if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" or obj_type_name == "static" then --should be an unncessary check
-
- local CategoryName = obj_type_name
-
- if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then --there's a group!
-
- --self.Units[coa_name][countryName][category] = {}
-
- for group_num, GroupTemplate in pairs(obj_type_data.group) do
-
- if GroupTemplate and GroupTemplate.units and type(GroupTemplate.units) == 'table' then --making sure again- this is a valid group
- self:_RegisterTemplate(
- GroupTemplate,
- CoalitionSide,
- _DATABASECategory[string.lower(CategoryName)],
- CountryID
- )
- end --if GroupTemplate and GroupTemplate.units then
- end --for group_num, GroupTemplate in pairs(obj_type_data.group) do
- end --if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then
- end --if obj_type_name == "helicopter" or obj_type_name == "ship" or obj_type_name == "plane" or obj_type_name == "vehicle" or obj_type_name == "static" then
- end --for obj_type_name, obj_type_data in pairs(cntry_data) do
- end --if type(cntry_data) == 'table' then
- end --for cntry_id, cntry_data in pairs(coa_data.country) do
- end --if coa_data.country then --there is a country table
- end --if coa_name == 'red' or coa_name == 'blue' and type(coa_data) == 'table' then
- end --for coa_name, coa_data in pairs(mission.coalition) do
-
- return self
-end
-
-
-
-
---- **Core** - SET_ classes define **collections** of objects to perform **bulk actions** and logically **group** objects.
---
--- 
---
--- ===
---
--- SET_ classes group objects of the same type into a collection, which is either:
---
--- * Manually managed using the **:Add...()** or **:Remove...()** methods. The initial SET can be filtered with the **@{#SET_BASE.FilterOnce}()** method
--- * Dynamically updated when new objects are created or objects are destroyed using the **@{#SET_BASE.FilterStart}()** method.
---
--- Various types of SET_ classes are available:
---
--- * @{#SET_UNIT}: Defines a colleciton of @{Unit}s filtered by filter criteria.
--- * @{#SET_GROUP}: Defines a collection of @{Group}s filtered by filter criteria.
--- * @{#SET_CLIENT}: Defines a collection of @{Client}s filterd by filter criteria.
--- * @{#SET_AIRBASE}: Defines a collection of @{Airbase}s filtered by filter criteria.
---
--- These classes are derived from @{#SET_BASE}, which contains the main methods to manage SETs.
---
--- A multitude of other methods are available in SET_ classes that allow to:
---
--- * Validate the presence of objects in the SET.
--- * Trigger events when objects in the SET change a zone presence.
---
--- ### Authors:
---
--- * FlightControl : Design & Programming
---
--- ### Contributions:
---
---
--- @module Set
-
-
---- @type SET_BASE
--- @field #table Filter
--- @field #table Set
--- @field #table List
--- @field Core.Scheduler#SCHEDULER CallScheduler
--- @extends Core.Base#BASE
-
-
---- # 1) SET_BASE class, extends @{Base#BASE}
--- The @{Set#SET_BASE} class defines the core functions that define a collection of objects.
--- A SET provides iterators to iterate the SET, but will **temporarily** yield the ForEach interator loop at defined **"intervals"** to the mail simulator loop.
--- In this way, large loops can be done while not blocking the simulator main processing loop.
--- The default **"yield interval"** is after 10 objects processed.
--- The default **"time interval"** is after 0.001 seconds.
---
--- ## 1.1) Add or remove objects from the SET
---
--- Some key core functions are @{Set#SET_BASE.Add} and @{Set#SET_BASE.Remove} to add or remove objects from the SET in your logic.
---
--- ## 1.2) Define the SET iterator **"yield interval"** and the **"time interval"**
---
--- Modify the iterator intervals with the @{Set#SET_BASE.SetInteratorIntervals} method.
--- You can set the **"yield interval"**, and the **"time interval"**. (See above).
---
--- @field #SET_BASE SET_BASE
-SET_BASE = {
- ClassName = "SET_BASE",
- Filter = {},
- Set = {},
- List = {},
- Index = {},
-}
-
-
---- Creates a new SET_BASE object, building a set of units belonging to a coalitions, categories, countries, types or with defined prefix names.
--- @param #SET_BASE self
--- @return #SET_BASE
--- @usage
--- -- Define a new SET_BASE Object. This DBObject will contain a reference to all Group and Unit Templates defined within the ME and the DCSRTE.
--- DBObject = SET_BASE:New()
-function SET_BASE:New( Database )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, BASE:New() ) -- Core.Set#SET_BASE
-
- self.Database = Database
-
- self.YieldInterval = 10
- self.TimeInterval = 0.001
-
- self.Set = {}
-
- self.List = {}
- self.List.__index = self.List
- self.List = setmetatable( { Count = 0 }, self.List )
-
- self.Index = {}
-
- self.CallScheduler = SCHEDULER:New( self )
-
- self:SetEventPriority( 2 )
-
- return self
-end
-
---- Finds an @{Base#BASE} object based on the object Name.
--- @param #SET_BASE self
--- @param #string ObjectName
--- @return Core.Base#BASE The Object found.
-function SET_BASE:_Find( ObjectName )
-
- local ObjectFound = self.Set[ObjectName]
- return ObjectFound
-end
-
-
---- Gets the Set.
--- @param #SET_BASE self
--- @return #SET_BASE self
-function SET_BASE:GetSet()
- self:F2()
-
- return self.Set
-end
-
---- Adds a @{Base#BASE} object in the @{Set#SET_BASE}, using a given ObjectName as the index.
--- @param #SET_BASE self
--- @param #string ObjectName
--- @param Core.Base#BASE Object
--- @return Core.Base#BASE The added BASE Object.
-function SET_BASE:Add( ObjectName, Object )
- self:F2( ObjectName )
-
- local t = { _ = Object }
-
- if self.List.last then
- self.List.last._next = t
- t._prev = self.List.last
- self.List.last = t
- else
- -- this is the first node
- self.List.first = t
- self.List.last = t
- end
-
- self.List.Count = self.List.Count + 1
-
- self.Set[ObjectName] = t._
-
- table.insert( self.Index, ObjectName )
-
-end
-
---- Adds a @{Base#BASE} object in the @{Set#SET_BASE}, using the Object Name as the index.
--- @param #SET_BASE self
--- @param Wrapper.Object#OBJECT Object
--- @return Core.Base#BASE The added BASE Object.
-function SET_BASE:AddObject( Object )
- self:F2( Object.ObjectName )
-
- self:T( Object.UnitName )
- self:T( Object.ObjectName )
- self:Add( Object.ObjectName, Object )
-
-end
-
-
-
---- Removes a @{Base#BASE} object from the @{Set#SET_BASE} and derived classes, based on the Object Name.
--- @param #SET_BASE self
--- @param #string ObjectName
-function SET_BASE:Remove( ObjectName )
- self:F( ObjectName )
-
- local t = self.Set[ObjectName]
-
- self:E( { ObjectName, t } )
-
- if t then
- if t._next then
- if t._prev then
- t._next._prev = t._prev
- t._prev._next = t._next
- else
- -- this was the first node
- t._next._prev = nil
- self.List._first = t._next
- end
- elseif t._prev then
- -- this was the last node
- t._prev._next = nil
- self.List._last = t._prev
- else
- -- this was the only node
- self.List._first = nil
- self.List._last = nil
- end
-
- t._next = nil
- t._prev = nil
- self.List.Count = self.List.Count - 1
-
- for Index, Key in ipairs( self.Index ) do
- if Key == ObjectName then
- table.remove( self.Index, Index )
- break
- end
- end
-
- self.Set[ObjectName] = nil
-
- end
-
-end
-
---- Gets a @{Base#BASE} object from the @{Set#SET_BASE} and derived classes, based on the Object Name.
--- @param #SET_BASE self
--- @param #string ObjectName
--- @return Core.Base#BASE
-function SET_BASE:Get( ObjectName )
- self:F( ObjectName )
-
- local t = self.Set[ObjectName]
-
- self:T3( { ObjectName, t } )
-
- return t
-
-end
-
---- Gets the first object from the @{Set#SET_BASE} and derived classes.
--- @param #SET_BASE self
--- @return Core.Base#BASE
-function SET_BASE:GetFirst()
- self:F()
-
- local ObjectName = self.Index[1]
- local FirstObject = self.Set[ObjectName]
- self:T3( { FirstObject } )
- return FirstObject
-end
-
---- Gets the last object from the @{Set#SET_BASE} and derived classes.
--- @param #SET_BASE self
--- @return Core.Base#BASE
-function SET_BASE:GetLast()
- self:F()
-
- local ObjectName = self.Index[#self.Index]
- local LastObject = self.Set[ObjectName]
- self:T3( { LastObject } )
- return LastObject
-end
-
---- Gets a random object from the @{Set#SET_BASE} and derived classes.
--- @param #SET_BASE self
--- @return Core.Base#BASE
-function SET_BASE:GetRandom()
- self:F()
-
- local RandomItem = self.Set[self.Index[math.random(#self.Index)]]
-
- self:T3( { RandomItem } )
-
- return RandomItem
-end
-
-
---- Retrieves the amount of objects in the @{Set#SET_BASE} and derived classes.
--- @param #SET_BASE self
--- @return #number Count
-function SET_BASE:Count()
-
- return #self.Index or 0
-end
-
-
-
---- Copies the Filter criteria from a given Set (for rebuilding a new Set based on an existing Set).
--- @param #SET_BASE self
--- @param #SET_BASE BaseSet
--- @return #SET_BASE
-function SET_BASE:SetDatabase( BaseSet )
-
- -- Copy the filter criteria of the BaseSet
- local OtherFilter = routines.utils.deepCopy( BaseSet.Filter )
- self.Filter = OtherFilter
-
- -- Now base the new Set on the BaseSet
- self.Database = BaseSet:GetSet()
- return self
-end
-
-
-
---- Define the SET iterator **"yield interval"** and the **"time interval"**.
--- @param #SET_BASE self
--- @param #number YieldInterval Sets the frequency when the iterator loop will yield after the number of objects processed. The default frequency is 10 objects processed.
--- @param #number TimeInterval Sets the time in seconds when the main logic will resume the iterator loop. The default time is 0.001 seconds.
--- @return #SET_BASE self
-function SET_BASE:SetIteratorIntervals( YieldInterval, TimeInterval )
-
- self.YieldInterval = YieldInterval
- self.TimeInterval = TimeInterval
-
- return self
-end
-
-
---- Filters for the defined collection.
--- @param #SET_BASE self
--- @return #SET_BASE self
-function SET_BASE:FilterOnce()
-
- for ObjectName, Object in pairs( self.Database ) do
-
- if self:IsIncludeObject( Object ) then
- self:Add( ObjectName, Object )
- end
- end
-
- return self
-end
-
---- Starts the filtering for the defined collection.
--- @param #SET_BASE self
--- @return #SET_BASE self
-function SET_BASE:_FilterStart()
-
- for ObjectName, Object in pairs( self.Database ) do
-
- if self:IsIncludeObject( Object ) then
- self:E( { "Adding Object:", ObjectName } )
- self:Add( ObjectName, Object )
- end
- end
-
- self:HandleEvent( EVENTS.Birth, self._EventOnBirth )
- self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash )
- self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash )
-
- -- Follow alive players and clients
- self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit )
- self:HandleEvent( EVENTS.PlayerLeaveUnit, self._EventOnPlayerLeaveUnit )
-
-
- return self
-end
-
---- Stops the filtering for the defined collection.
--- @param #SET_BASE self
--- @return #SET_BASE self
-function SET_BASE:FilterStop()
-
- self:UnHandleEvent( EVENTS.Birth )
- self:UnHandleEvent( EVENTS.Dead )
- self:UnHandleEvent( EVENTS.Crash )
-
- return self
-end
-
---- Iterate the SET_BASE while identifying the nearest object from a @{Point#POINT_VEC2}.
--- @param #SET_BASE self
--- @param Core.Point#POINT_VEC2 PointVec2 A @{Point#POINT_VEC2} object from where to evaluate the closest object in the set.
--- @return Core.Base#BASE The closest object.
-function SET_BASE:FindNearestObjectFromPointVec2( PointVec2 )
- self:F2( PointVec2 )
-
- local NearestObject = nil
- local ClosestDistance = nil
-
- for ObjectID, ObjectData in pairs( self.Set ) do
- if NearestObject == nil then
- NearestObject = ObjectData
- ClosestDistance = PointVec2:DistanceFromVec2( ObjectData:GetVec2() )
- else
- local Distance = PointVec2:DistanceFromVec2( ObjectData:GetVec2() )
- if Distance < ClosestDistance then
- NearestObject = ObjectData
- ClosestDistance = Distance
- end
- end
- end
-
- return NearestObject
-end
-
-
-
------ Private method that registers all alive players in the mission.
----- @param #SET_BASE self
----- @return #SET_BASE self
---function SET_BASE:_RegisterPlayers()
---
--- local CoalitionsData = { AlivePlayersRed = coalition.getPlayers( coalition.side.RED ), AlivePlayersBlue = coalition.getPlayers( coalition.side.BLUE ) }
--- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
--- for UnitId, UnitData in pairs( CoalitionData ) do
--- self:T3( { "UnitData:", UnitData } )
--- if UnitData and UnitData:isExist() then
--- local UnitName = UnitData:getName()
--- if not self.PlayersAlive[UnitName] then
--- self:E( { "Add player for unit:", UnitName, UnitData:getPlayerName() } )
--- self.PlayersAlive[UnitName] = UnitData:getPlayerName()
--- end
--- end
--- end
--- end
---
--- return self
---end
-
---- Events
-
---- Handles the OnBirth event for the Set.
--- @param #SET_BASE self
--- @param Core.Event#EVENTDATA Event
-function SET_BASE:_EventOnBirth( Event )
- self:F3( { Event } )
-
- if Event.IniDCSUnit then
- local ObjectName, Object = self:AddInDatabase( Event )
- self:T3( ObjectName, Object )
- if Object and self:IsIncludeObject( Object ) then
- self:Add( ObjectName, Object )
- --self:_EventOnPlayerEnterUnit( Event )
- end
- end
-end
-
---- Handles the OnDead or OnCrash event for alive units set.
--- @param #SET_BASE self
--- @param Core.Event#EVENTDATA Event
-function SET_BASE:_EventOnDeadOrCrash( Event )
- self:F3( { Event } )
-
- if Event.IniDCSUnit then
- local ObjectName, Object = self:FindInDatabase( Event )
- if ObjectName and Object ~= nil then
- self:Remove( ObjectName )
- end
- end
-end
-
---- Handles the OnPlayerEnterUnit event to fill the active players table (with the unit filter applied).
--- @param #SET_BASE self
--- @param Core.Event#EVENTDATA Event
-function SET_BASE:_EventOnPlayerEnterUnit( Event )
- self:F3( { Event } )
-
- if Event.IniDCSUnit then
- local ObjectName, Object = self:AddInDatabase( Event )
- self:T3( ObjectName, Object )
- if self:IsIncludeObject( Object ) then
- self:Add( ObjectName, Object )
- --self:_EventOnPlayerEnterUnit( Event )
- end
- end
-end
-
---- Handles the OnPlayerLeaveUnit event to clean the active players table.
--- @param #SET_BASE self
--- @param Core.Event#EVENTDATA Event
-function SET_BASE:_EventOnPlayerLeaveUnit( Event )
- self:F3( { Event } )
-
- local ObjectName = Event.IniDCSUnit
- if Event.IniDCSUnit then
- if Event.IniDCSGroup then
- local GroupUnits = Event.IniDCSGroup:getUnits()
- local PlayerCount = 0
- for _, DCSUnit in pairs( GroupUnits ) do
- if DCSUnit ~= Event.IniDCSUnit then
- if DCSUnit:getPlayerName() ~= nil then
- PlayerCount = PlayerCount + 1
- end
- end
- end
- self:E(PlayerCount)
- if PlayerCount == 0 then
- self:Remove( Event.IniDCSGroupName )
- end
- end
- end
-end
-
--- Iterators
-
---- Iterate the SET_BASE and derived classes and call an iterator function for the given SET_BASE, providing the Object for each element within the set and optional parameters.
--- @param #SET_BASE self
--- @param #function IteratorFunction The function that will be called.
--- @return #SET_BASE self
-function SET_BASE:ForEach( IteratorFunction, arg, Set, Function, FunctionArguments )
- self:F3( arg )
-
- Set = Set or self:GetSet()
- arg = arg or {}
-
- local function CoRoutine()
- local Count = 0
- for ObjectID, ObjectData in pairs( Set ) do
- local Object = ObjectData
- self:T3( Object )
- if Function then
- if Function( unpack( FunctionArguments ), Object ) == true then
- IteratorFunction( Object, unpack( arg ) )
- end
- else
- IteratorFunction( Object, unpack( arg ) )
- end
- Count = Count + 1
--- if Count % self.YieldInterval == 0 then
--- coroutine.yield( false )
--- end
- end
- return true
- end
-
--- local co = coroutine.create( CoRoutine )
- local co = CoRoutine
-
- local function Schedule()
-
--- local status, res = coroutine.resume( co )
- local status, res = co()
- self:T3( { status, res } )
-
- if status == false then
- error( res )
- end
- if res == false then
- return true -- resume next time the loop
- end
-
- return false
- end
-
- --self.CallScheduler:Schedule( self, Schedule, {}, self.TimeInterval, self.TimeInterval, 0 )
- Schedule()
-
- return self
-end
-
-
------ Iterate the SET_BASE and call an interator function for each **alive** unit, providing the Unit and optional parameters.
----- @param #SET_BASE self
----- @param #function IteratorFunction The function that will be called when there is an alive unit in the SET_BASE. The function needs to accept a UNIT parameter.
----- @return #SET_BASE self
---function SET_BASE:ForEachDCSUnitAlive( IteratorFunction, ... )
--- self:F3( arg )
---
--- self:ForEach( IteratorFunction, arg, self.DCSUnitsAlive )
---
--- return self
---end
---
------ Iterate the SET_BASE and call an interator function for each **alive** player, providing the Unit of the player and optional parameters.
----- @param #SET_BASE self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_BASE. The function needs to accept a UNIT parameter.
----- @return #SET_BASE self
---function SET_BASE:ForEachPlayer( IteratorFunction, ... )
--- self:F3( arg )
---
--- self:ForEach( IteratorFunction, arg, self.PlayersAlive )
---
--- return self
---end
---
---
------ Iterate the SET_BASE and call an interator function for each client, providing the Client to the function and optional parameters.
----- @param #SET_BASE self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_BASE. The function needs to accept a CLIENT parameter.
----- @return #SET_BASE self
---function SET_BASE:ForEachClient( IteratorFunction, ... )
--- self:F3( arg )
---
--- self:ForEach( IteratorFunction, arg, self.Clients )
---
--- return self
---end
-
-
---- Decides whether to include the Object
--- @param #SET_BASE self
--- @param #table Object
--- @return #SET_BASE self
-function SET_BASE:IsIncludeObject( Object )
- self:F3( Object )
-
- return true
-end
-
---- Flushes the current SET_BASE contents in the log ... (for debugging reasons).
--- @param #SET_BASE self
--- @return #string A string with the names of the objects.
-function SET_BASE:Flush()
- self:F3()
-
- local ObjectNames = ""
- for ObjectName, Object in pairs( self.Set ) do
- ObjectNames = ObjectNames .. ObjectName .. ", "
- end
- self:E( { "Objects in Set:", ObjectNames } )
-
- return ObjectNames
-end
-
-
---- @type SET_GROUP
--- @extends Core.Set#SET_BASE
-
---- # 2) SET_GROUP class, extends @{Set#SET_BASE}
---
--- Mission designers can use the @{Set#SET_GROUP} class to build sets of groups belonging to certain:
---
--- * Coalitions
--- * Categories
--- * Countries
--- * Starting with certain prefix strings.
---
--- ## 2.1) SET_GROUP constructor
---
--- Create a new SET_GROUP object with the @{#SET_GROUP.New} method:
---
--- * @{#SET_GROUP.New}: Creates a new SET_GROUP object.
---
--- ## 2.2) Add or Remove GROUP(s) from SET_GROUP
---
--- GROUPS can be added and removed using the @{Set#SET_GROUP.AddGroupsByName} and @{Set#SET_GROUP.RemoveGroupsByName} respectively.
--- These methods take a single GROUP name or an array of GROUP names to be added or removed from SET_GROUP.
---
--- ## 2.3) SET_GROUP filter criteria
---
--- You can set filter criteria to define the set of groups within the SET_GROUP.
--- Filter criteria are defined by:
---
--- * @{#SET_GROUP.FilterCoalitions}: Builds the SET_GROUP with the groups belonging to the coalition(s).
--- * @{#SET_GROUP.FilterCategories}: Builds the SET_GROUP with the groups belonging to the category(ies).
--- * @{#SET_GROUP.FilterCountries}: Builds the SET_GROUP with the gruops belonging to the country(ies).
--- * @{#SET_GROUP.FilterPrefixes}: Builds the SET_GROUP with the groups starting with the same prefix string(s).
---
--- Once the filter criteria have been set for the SET_GROUP, you can start filtering using:
---
--- * @{#SET_GROUP.FilterStart}: Starts the filtering of the groups within the SET_GROUP and add or remove GROUP objects **dynamically**.
---
--- Planned filter criteria within development are (so these are not yet available):
---
--- * @{#SET_GROUP.FilterZones}: Builds the SET_GROUP with the groups within a @{Zone#ZONE}.
---
--- ## 2.4) SET_GROUP iterators
---
--- Once the filters have been defined and the SET_GROUP has been built, you can iterate the SET_GROUP with the available iterator methods.
--- The iterator methods will walk the SET_GROUP set, and call for each element within the set a function that you provide.
--- The following iterator methods are currently available within the SET_GROUP:
---
--- * @{#SET_GROUP.ForEachGroup}: Calls a function for each alive group it finds within the SET_GROUP.
--- * @{#SET_GROUP.ForEachGroupCompletelyInZone}: Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence completely in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- * @{#SET_GROUP.ForEachGroupPartlyInZone}: Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence partly in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- * @{#SET_GROUP.ForEachGroupNotInZone}: Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence not in a @{Zone}, providing the GROUP and optional parameters to the called function.
---
--- ===
--- @field #SET_GROUP SET_GROUP
-SET_GROUP = {
- ClassName = "SET_GROUP",
- Filter = {
- Coalitions = nil,
- Categories = nil,
- Countries = nil,
- GroupPrefixes = nil,
- },
- FilterMeta = {
- Coalitions = {
- red = coalition.side.RED,
- blue = coalition.side.BLUE,
- neutral = coalition.side.NEUTRAL,
- },
- Categories = {
- plane = Group.Category.AIRPLANE,
- helicopter = Group.Category.HELICOPTER,
- ground = Group.Category.GROUND_UNIT,
- ship = Group.Category.SHIP,
- structure = Group.Category.STRUCTURE,
- },
- },
-}
-
-
---- Creates a new SET_GROUP object, building a set of groups belonging to a coalitions, categories, countries, types or with defined prefix names.
--- @param #SET_GROUP self
--- @return #SET_GROUP
--- @usage
--- -- Define a new SET_GROUP Object. This DBObject will contain a reference to all alive GROUPS.
--- DBObject = SET_GROUP:New()
-function SET_GROUP:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, SET_BASE:New( _DATABASE.GROUPS ) )
-
- return self
-end
-
---- Add GROUP(s) to SET_GROUP.
--- @param Core.Set#SET_GROUP self
--- @param #string AddGroupNames A single name or an array of GROUP names.
--- @return self
-function SET_GROUP:AddGroupsByName( AddGroupNames )
-
- local AddGroupNamesArray = ( type( AddGroupNames ) == "table" ) and AddGroupNames or { AddGroupNames }
-
- for AddGroupID, AddGroupName in pairs( AddGroupNamesArray ) do
- self:Add( AddGroupName, GROUP:FindByName( AddGroupName ) )
- end
-
- return self
-end
-
---- Remove GROUP(s) from SET_GROUP.
--- @param Core.Set#SET_GROUP self
--- @param Wrapper.Group#GROUP RemoveGroupNames A single name or an array of GROUP names.
--- @return self
-function SET_GROUP:RemoveGroupsByName( RemoveGroupNames )
-
- local RemoveGroupNamesArray = ( type( RemoveGroupNames ) == "table" ) and RemoveGroupNames or { RemoveGroupNames }
-
- for RemoveGroupID, RemoveGroupName in pairs( RemoveGroupNamesArray ) do
- self:Remove( RemoveGroupName.GroupName )
- end
-
- return self
-end
-
-
-
-
---- Finds a Group based on the Group Name.
--- @param #SET_GROUP self
--- @param #string GroupName
--- @return Wrapper.Group#GROUP The found Group.
-function SET_GROUP:FindGroup( GroupName )
-
- local GroupFound = self.Set[GroupName]
- return GroupFound
-end
-
-
-
---- Builds a set of groups of coalitions.
--- Possible current coalitions are red, blue and neutral.
--- @param #SET_GROUP self
--- @param #string Coalitions Can take the following values: "red", "blue", "neutral".
--- @return #SET_GROUP self
-function SET_GROUP:FilterCoalitions( Coalitions )
- if not self.Filter.Coalitions then
- self.Filter.Coalitions = {}
- end
- if type( Coalitions ) ~= "table" then
- Coalitions = { Coalitions }
- end
- for CoalitionID, Coalition in pairs( Coalitions ) do
- self.Filter.Coalitions[Coalition] = Coalition
- end
- return self
-end
-
-
---- Builds a set of groups out of categories.
--- Possible current categories are plane, helicopter, ground, ship.
--- @param #SET_GROUP self
--- @param #string Categories Can take the following values: "plane", "helicopter", "ground", "ship".
--- @return #SET_GROUP self
-function SET_GROUP:FilterCategories( Categories )
- if not self.Filter.Categories then
- self.Filter.Categories = {}
- end
- if type( Categories ) ~= "table" then
- Categories = { Categories }
- end
- for CategoryID, Category in pairs( Categories ) do
- self.Filter.Categories[Category] = Category
- end
- return self
-end
-
---- Builds a set of groups of defined countries.
--- Possible current countries are those known within DCS world.
--- @param #SET_GROUP self
--- @param #string Countries Can take those country strings known within DCS world.
--- @return #SET_GROUP self
-function SET_GROUP:FilterCountries( Countries )
- if not self.Filter.Countries then
- self.Filter.Countries = {}
- end
- if type( Countries ) ~= "table" then
- Countries = { Countries }
- end
- for CountryID, Country in pairs( Countries ) do
- self.Filter.Countries[Country] = Country
- end
- return self
-end
-
-
---- Builds a set of groups of defined GROUP prefixes.
--- All the groups starting with the given prefixes will be included within the set.
--- @param #SET_GROUP self
--- @param #string Prefixes The prefix of which the group name starts with.
--- @return #SET_GROUP self
-function SET_GROUP:FilterPrefixes( Prefixes )
- if not self.Filter.GroupPrefixes then
- self.Filter.GroupPrefixes = {}
- end
- if type( Prefixes ) ~= "table" then
- Prefixes = { Prefixes }
- end
- for PrefixID, Prefix in pairs( Prefixes ) do
- self.Filter.GroupPrefixes[Prefix] = Prefix
- end
- return self
-end
-
-
---- Starts the filtering.
--- @param #SET_GROUP self
--- @return #SET_GROUP self
-function SET_GROUP:FilterStart()
-
- if _DATABASE then
- self:_FilterStart()
- end
-
-
-
- return self
-end
-
---- Handles the Database to check on an event (birth) that the Object was added in the Database.
--- This is required, because sometimes the _DATABASE birth event gets called later than the SET_BASE birth event!
--- @param #SET_GROUP self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the GROUP
--- @return #table The GROUP
-function SET_GROUP:AddInDatabase( Event )
- self:F3( { Event } )
-
- if Event.IniObjectCategory == 1 then
- if not self.Database[Event.IniDCSGroupName] then
- self.Database[Event.IniDCSGroupName] = GROUP:Register( Event.IniDCSGroupName )
- self:T3( self.Database[Event.IniDCSGroupName] )
- end
- end
-
- return Event.IniDCSGroupName, self.Database[Event.IniDCSGroupName]
-end
-
---- Handles the Database to check on any event that Object exists in the Database.
--- This is required, because sometimes the _DATABASE event gets called later than the SET_BASE event or vise versa!
--- @param #SET_GROUP self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the GROUP
--- @return #table The GROUP
-function SET_GROUP:FindInDatabase( Event )
- self:F3( { Event } )
-
- return Event.IniDCSGroupName, self.Database[Event.IniDCSGroupName]
-end
-
---- Iterate the SET_GROUP and call an iterator function for each **alive** GROUP, providing the GROUP and optional parameters.
--- @param #SET_GROUP self
--- @param #function IteratorFunction The function that will be called when there is an alive GROUP in the SET_GROUP. The function needs to accept a GROUP parameter.
--- @return #SET_GROUP self
-function SET_GROUP:ForEachGroup( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set )
-
- return self
-end
-
---- Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence completely in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- @param #SET_GROUP self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive GROUP in the SET_GROUP. The function needs to accept a GROUP parameter.
--- @return #SET_GROUP self
-function SET_GROUP:ForEachGroupCompletelyInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Group#GROUP GroupObject
- function( ZoneObject, GroupObject )
- if GroupObject:IsCompletelyInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
---- Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence partly in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- @param #SET_GROUP self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive GROUP in the SET_GROUP. The function needs to accept a GROUP parameter.
--- @return #SET_GROUP self
-function SET_GROUP:ForEachGroupPartlyInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Group#GROUP GroupObject
- function( ZoneObject, GroupObject )
- if GroupObject:IsPartlyInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
---- Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence not in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- @param #SET_GROUP self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive GROUP in the SET_GROUP. The function needs to accept a GROUP parameter.
--- @return #SET_GROUP self
-function SET_GROUP:ForEachGroupNotInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Group#GROUP GroupObject
- function( ZoneObject, GroupObject )
- if GroupObject:IsNotInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
-
------ Iterate the SET_GROUP and call an interator function for each **alive** player, providing the Group of the player and optional parameters.
----- @param #SET_GROUP self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_GROUP. The function needs to accept a GROUP parameter.
----- @return #SET_GROUP self
---function SET_GROUP:ForEachPlayer( IteratorFunction, ... )
--- self:F2( arg )
---
--- self:ForEach( IteratorFunction, arg, self.PlayersAlive )
---
--- return self
---end
---
---
------ Iterate the SET_GROUP and call an interator function for each client, providing the Client to the function and optional parameters.
----- @param #SET_GROUP self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_GROUP. The function needs to accept a CLIENT parameter.
----- @return #SET_GROUP self
---function SET_GROUP:ForEachClient( IteratorFunction, ... )
--- self:F2( arg )
---
--- self:ForEach( IteratorFunction, arg, self.Clients )
---
--- return self
---end
-
-
----
--- @param #SET_GROUP self
--- @param Wrapper.Group#GROUP MooseGroup
--- @return #SET_GROUP self
-function SET_GROUP:IsIncludeObject( MooseGroup )
- self:F2( MooseGroup )
- local MooseGroupInclude = true
-
- if self.Filter.Coalitions then
- local MooseGroupCoalition = false
- for CoalitionID, CoalitionName in pairs( self.Filter.Coalitions ) do
- self:T3( { "Coalition:", MooseGroup:GetCoalition(), self.FilterMeta.Coalitions[CoalitionName], CoalitionName } )
- if self.FilterMeta.Coalitions[CoalitionName] and self.FilterMeta.Coalitions[CoalitionName] == MooseGroup:GetCoalition() then
- MooseGroupCoalition = true
- end
- end
- MooseGroupInclude = MooseGroupInclude and MooseGroupCoalition
- end
-
- if self.Filter.Categories then
- local MooseGroupCategory = false
- for CategoryID, CategoryName in pairs( self.Filter.Categories ) do
- self:T3( { "Category:", MooseGroup:GetCategory(), self.FilterMeta.Categories[CategoryName], CategoryName } )
- if self.FilterMeta.Categories[CategoryName] and self.FilterMeta.Categories[CategoryName] == MooseGroup:GetCategory() then
- MooseGroupCategory = true
- end
- end
- MooseGroupInclude = MooseGroupInclude and MooseGroupCategory
- end
-
- if self.Filter.Countries then
- local MooseGroupCountry = false
- for CountryID, CountryName in pairs( self.Filter.Countries ) do
- self:T3( { "Country:", MooseGroup:GetCountry(), CountryName } )
- if country.id[CountryName] == MooseGroup:GetCountry() then
- MooseGroupCountry = true
- end
- end
- MooseGroupInclude = MooseGroupInclude and MooseGroupCountry
- end
-
- if self.Filter.GroupPrefixes then
- local MooseGroupPrefix = false
- for GroupPrefixId, GroupPrefix in pairs( self.Filter.GroupPrefixes ) do
- self:T3( { "Prefix:", string.find( MooseGroup:GetName(), GroupPrefix, 1 ), GroupPrefix } )
- if string.find( MooseGroup:GetName(), GroupPrefix, 1 ) then
- MooseGroupPrefix = true
- end
- end
- MooseGroupInclude = MooseGroupInclude and MooseGroupPrefix
- end
-
- self:T2( MooseGroupInclude )
- return MooseGroupInclude
-end
-
---- @type SET_UNIT
--- @extends Core.Set#SET_BASE
-
---- # 3) SET_UNIT class, extends @{Set#SET_BASE}
---
--- Mission designers can use the SET_UNIT class to build sets of units belonging to certain:
---
--- * Coalitions
--- * Categories
--- * Countries
--- * Unit types
--- * Starting with certain prefix strings.
---
--- ## 3.1) SET_UNIT constructor
---
--- Create a new SET_UNIT object with the @{#SET_UNIT.New} method:
---
--- * @{#SET_UNIT.New}: Creates a new SET_UNIT object.
---
--- ## 3.2) Add or Remove UNIT(s) from SET_UNIT
---
--- UNITs can be added and removed using the @{Set#SET_UNIT.AddUnitsByName} and @{Set#SET_UNIT.RemoveUnitsByName} respectively.
--- These methods take a single UNIT name or an array of UNIT names to be added or removed from SET_UNIT.
---
--- ## 3.3) SET_UNIT filter criteria
---
--- You can set filter criteria to define the set of units within the SET_UNIT.
--- Filter criteria are defined by:
---
--- * @{#SET_UNIT.FilterCoalitions}: Builds the SET_UNIT with the units belonging to the coalition(s).
--- * @{#SET_UNIT.FilterCategories}: Builds the SET_UNIT with the units belonging to the category(ies).
--- * @{#SET_UNIT.FilterTypes}: Builds the SET_UNIT with the units belonging to the unit type(s).
--- * @{#SET_UNIT.FilterCountries}: Builds the SET_UNIT with the units belonging to the country(ies).
--- * @{#SET_UNIT.FilterPrefixes}: Builds the SET_UNIT with the units starting with the same prefix string(s).
---
--- Once the filter criteria have been set for the SET_UNIT, you can start filtering using:
---
--- * @{#SET_UNIT.FilterStart}: Starts the filtering of the units within the SET_UNIT.
---
--- Planned filter criteria within development are (so these are not yet available):
---
--- * @{#SET_UNIT.FilterZones}: Builds the SET_UNIT with the units within a @{Zone#ZONE}.
---
--- ## 3.4) SET_UNIT iterators
---
--- Once the filters have been defined and the SET_UNIT has been built, you can iterate the SET_UNIT with the available iterator methods.
--- The iterator methods will walk the SET_UNIT set, and call for each element within the set a function that you provide.
--- The following iterator methods are currently available within the SET_UNIT:
---
--- * @{#SET_UNIT.ForEachUnit}: Calls a function for each alive unit it finds within the SET_UNIT.
--- * @{#SET_GROUP.ForEachGroupCompletelyInZone}: Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence completely in a @{Zone}, providing the GROUP and optional parameters to the called function.
--- * @{#SET_GROUP.ForEachGroupNotInZone}: Iterate the SET_GROUP and call an iterator function for each **alive** GROUP presence not in a @{Zone}, providing the GROUP and optional parameters to the called function.
---
--- Planned iterators methods in development are (so these are not yet available):
---
--- * @{#SET_UNIT.ForEachUnitInUnit}: Calls a function for each unit contained within the SET_UNIT.
--- * @{#SET_UNIT.ForEachUnitCompletelyInZone}: Iterate and call an iterator function for each **alive** UNIT presence completely in a @{Zone}, providing the UNIT and optional parameters to the called function.
--- * @{#SET_UNIT.ForEachUnitNotInZone}: Iterate and call an iterator function for each **alive** UNIT presence not in a @{Zone}, providing the UNIT and optional parameters to the called function.
---
--- ## 3.5 ) SET_UNIT atomic methods
---
--- Various methods exist for a SET_UNIT to perform actions or calculations and retrieve results from the SET_UNIT:
---
--- * @{#SET_UNIT.GetTypeNames}(): Retrieve the type names of the @{Unit}s in the SET, delimited by a comma.
---
--- ===
--- @field #SET_UNIT SET_UNIT
-SET_UNIT = {
- ClassName = "SET_UNIT",
- Units = {},
- Filter = {
- Coalitions = nil,
- Categories = nil,
- Types = nil,
- Countries = nil,
- UnitPrefixes = nil,
- },
- FilterMeta = {
- Coalitions = {
- red = coalition.side.RED,
- blue = coalition.side.BLUE,
- neutral = coalition.side.NEUTRAL,
- },
- Categories = {
- plane = Unit.Category.AIRPLANE,
- helicopter = Unit.Category.HELICOPTER,
- ground = Unit.Category.GROUND_UNIT,
- ship = Unit.Category.SHIP,
- structure = Unit.Category.STRUCTURE,
- },
- },
-}
-
-
---- Creates a new SET_UNIT object, building a set of units belonging to a coalitions, categories, countries, types or with defined prefix names.
--- @param #SET_UNIT self
--- @return #SET_UNIT
--- @usage
--- -- Define a new SET_UNIT Object. This DBObject will contain a reference to all alive Units.
--- DBObject = SET_UNIT:New()
-function SET_UNIT:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, SET_BASE:New( _DATABASE.UNITS ) )
-
- return self
-end
-
---- Add UNIT(s) to SET_UNIT.
--- @param #SET_UNIT self
--- @param #string AddUnit A single UNIT.
--- @return #SET_UNIT self
-function SET_UNIT:AddUnit( AddUnit )
- self:F2( AddUnit:GetName() )
-
- self:Add( AddUnit:GetName(), AddUnit )
-
- return self
-end
-
-
---- Add UNIT(s) to SET_UNIT.
--- @param #SET_UNIT self
--- @param #string AddUnitNames A single name or an array of UNIT names.
--- @return #SET_UNIT self
-function SET_UNIT:AddUnitsByName( AddUnitNames )
-
- local AddUnitNamesArray = ( type( AddUnitNames ) == "table" ) and AddUnitNames or { AddUnitNames }
-
- self:T( AddUnitNamesArray )
- for AddUnitID, AddUnitName in pairs( AddUnitNamesArray ) do
- self:Add( AddUnitName, UNIT:FindByName( AddUnitName ) )
- end
-
- return self
-end
-
---- Remove UNIT(s) from SET_UNIT.
--- @param Core.Set#SET_UNIT self
--- @param Wrapper.Unit#UNIT RemoveUnitNames A single name or an array of UNIT names.
--- @return self
-function SET_UNIT:RemoveUnitsByName( RemoveUnitNames )
-
- local RemoveUnitNamesArray = ( type( RemoveUnitNames ) == "table" ) and RemoveUnitNames or { RemoveUnitNames }
-
- for RemoveUnitID, RemoveUnitName in pairs( RemoveUnitNamesArray ) do
- self:Remove( RemoveUnitName )
- end
-
- return self
-end
-
-
---- Finds a Unit based on the Unit Name.
--- @param #SET_UNIT self
--- @param #string UnitName
--- @return Wrapper.Unit#UNIT The found Unit.
-function SET_UNIT:FindUnit( UnitName )
-
- local UnitFound = self.Set[UnitName]
- return UnitFound
-end
-
-
-
---- Builds a set of units of coalitions.
--- Possible current coalitions are red, blue and neutral.
--- @param #SET_UNIT self
--- @param #string Coalitions Can take the following values: "red", "blue", "neutral".
--- @return #SET_UNIT self
-function SET_UNIT:FilterCoalitions( Coalitions )
- if not self.Filter.Coalitions then
- self.Filter.Coalitions = {}
- end
- if type( Coalitions ) ~= "table" then
- Coalitions = { Coalitions }
- end
- for CoalitionID, Coalition in pairs( Coalitions ) do
- self.Filter.Coalitions[Coalition] = Coalition
- end
- return self
-end
-
-
---- Builds a set of units out of categories.
--- Possible current categories are plane, helicopter, ground, ship.
--- @param #SET_UNIT self
--- @param #string Categories Can take the following values: "plane", "helicopter", "ground", "ship".
--- @return #SET_UNIT self
-function SET_UNIT:FilterCategories( Categories )
- if not self.Filter.Categories then
- self.Filter.Categories = {}
- end
- if type( Categories ) ~= "table" then
- Categories = { Categories }
- end
- for CategoryID, Category in pairs( Categories ) do
- self.Filter.Categories[Category] = Category
- end
- return self
-end
-
-
---- Builds a set of units of defined unit types.
--- Possible current types are those types known within DCS world.
--- @param #SET_UNIT self
--- @param #string Types Can take those type strings known within DCS world.
--- @return #SET_UNIT self
-function SET_UNIT:FilterTypes( Types )
- if not self.Filter.Types then
- self.Filter.Types = {}
- end
- if type( Types ) ~= "table" then
- Types = { Types }
- end
- for TypeID, Type in pairs( Types ) do
- self.Filter.Types[Type] = Type
- end
- return self
-end
-
-
---- Builds a set of units of defined countries.
--- Possible current countries are those known within DCS world.
--- @param #SET_UNIT self
--- @param #string Countries Can take those country strings known within DCS world.
--- @return #SET_UNIT self
-function SET_UNIT:FilterCountries( Countries )
- if not self.Filter.Countries then
- self.Filter.Countries = {}
- end
- if type( Countries ) ~= "table" then
- Countries = { Countries }
- end
- for CountryID, Country in pairs( Countries ) do
- self.Filter.Countries[Country] = Country
- end
- return self
-end
-
-
---- Builds a set of units of defined unit prefixes.
--- All the units starting with the given prefixes will be included within the set.
--- @param #SET_UNIT self
--- @param #string Prefixes The prefix of which the unit name starts with.
--- @return #SET_UNIT self
-function SET_UNIT:FilterPrefixes( Prefixes )
- if not self.Filter.UnitPrefixes then
- self.Filter.UnitPrefixes = {}
- end
- if type( Prefixes ) ~= "table" then
- Prefixes = { Prefixes }
- end
- for PrefixID, Prefix in pairs( Prefixes ) do
- self.Filter.UnitPrefixes[Prefix] = Prefix
- end
- return self
-end
-
---- Builds a set of units having a radar of give types.
--- All the units having a radar of a given type will be included within the set.
--- @param #SET_UNIT self
--- @param #table RadarTypes The radar types.
--- @return #SET_UNIT self
-function SET_UNIT:FilterHasRadar( RadarTypes )
-
- self.Filter.RadarTypes = self.Filter.RadarTypes or {}
- if type( RadarTypes ) ~= "table" then
- RadarTypes = { RadarTypes }
- end
- for RadarTypeID, RadarType in pairs( RadarTypes ) do
- self.Filter.RadarTypes[RadarType] = RadarType
- end
- return self
-end
-
---- Builds a set of SEADable units.
--- @param #SET_UNIT self
--- @return #SET_UNIT self
-function SET_UNIT:FilterHasSEAD()
-
- self.Filter.SEAD = true
- return self
-end
-
-
-
---- Starts the filtering.
--- @param #SET_UNIT self
--- @return #SET_UNIT self
-function SET_UNIT:FilterStart()
-
- if _DATABASE then
- self:_FilterStart()
- end
-
- return self
-end
-
---- Handles the Database to check on an event (birth) that the Object was added in the Database.
--- This is required, because sometimes the _DATABASE birth event gets called later than the SET_BASE birth event!
--- @param #SET_UNIT self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the UNIT
--- @return #table The UNIT
-function SET_UNIT:AddInDatabase( Event )
- self:F3( { Event } )
-
- if Event.IniObjectCategory == 1 then
- if not self.Database[Event.IniDCSUnitName] then
- self.Database[Event.IniDCSUnitName] = UNIT:Register( Event.IniDCSUnitName )
- self:T3( self.Database[Event.IniDCSUnitName] )
- end
- end
-
- return Event.IniDCSUnitName, self.Database[Event.IniDCSUnitName]
-end
-
---- Handles the Database to check on any event that Object exists in the Database.
--- This is required, because sometimes the _DATABASE event gets called later than the SET_BASE event or vise versa!
--- @param #SET_UNIT self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the UNIT
--- @return #table The UNIT
-function SET_UNIT:FindInDatabase( Event )
- self:E( { Event.IniDCSUnitName, self.Set[Event.IniDCSUnitName], Event } )
-
-
- return Event.IniDCSUnitName, self.Set[Event.IniDCSUnitName]
-end
-
---- Iterate the SET_UNIT and call an interator function for each **alive** UNIT, providing the UNIT and optional parameters.
--- @param #SET_UNIT self
--- @param #function IteratorFunction The function that will be called when there is an alive UNIT in the SET_UNIT. The function needs to accept a UNIT parameter.
--- @return #SET_UNIT self
-function SET_UNIT:ForEachUnit( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set )
-
- return self
-end
-
---- Iterate the SET_UNIT and call an iterator function for each **alive** UNIT presence completely in a @{Zone}, providing the UNIT and optional parameters to the called function.
--- @param #SET_UNIT self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive UNIT in the SET_UNIT. The function needs to accept a UNIT parameter.
--- @return #SET_UNIT self
-function SET_UNIT:ForEachUnitCompletelyInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Unit#UNIT UnitObject
- function( ZoneObject, UnitObject )
- if UnitObject:IsCompletelyInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
---- Iterate the SET_UNIT and call an iterator function for each **alive** UNIT presence not in a @{Zone}, providing the UNIT and optional parameters to the called function.
--- @param #SET_UNIT self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive UNIT in the SET_UNIT. The function needs to accept a UNIT parameter.
--- @return #SET_UNIT self
-function SET_UNIT:ForEachUnitNotInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Unit#UNIT UnitObject
- function( ZoneObject, UnitObject )
- if UnitObject:IsNotInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
---- Returns map of unit types.
--- @param #SET_UNIT self
--- @return #map<#string,#number> A map of the unit types found. The key is the UnitTypeName and the value is the amount of unit types found.
-function SET_UNIT:GetUnitTypes()
- self:F2()
-
- local MT = {} -- Message Text
- local UnitTypes = {}
-
- for UnitID, UnitData in pairs( self:GetSet() ) do
- local TextUnit = UnitData -- Wrapper.Unit#UNIT
- if TextUnit:IsAlive() then
- local UnitType = TextUnit:GetTypeName()
-
- if not UnitTypes[UnitType] then
- UnitTypes[UnitType] = 1
- else
- UnitTypes[UnitType] = UnitTypes[UnitType] + 1
- end
- end
- end
-
- for UnitTypeID, UnitType in pairs( UnitTypes ) do
- MT[#MT+1] = UnitType .. " of " .. UnitTypeID
- end
-
- return UnitTypes
-end
-
-
---- Returns a comma separated string of the unit types with a count in the @{Set}.
--- @param #SET_UNIT self
--- @return #string The unit types string
-function SET_UNIT:GetUnitTypesText()
- self:F2()
-
- local MT = {} -- Message Text
- local UnitTypes = self:GetUnitTypes()
-
- for UnitTypeID, UnitType in pairs( UnitTypes ) do
- MT[#MT+1] = UnitType .. " of " .. UnitTypeID
- end
-
- return table.concat( MT, ", " )
-end
-
---- Returns map of unit threat levels.
--- @param #SET_UNIT self
--- @return #table.
-function SET_UNIT:GetUnitThreatLevels()
- self:F2()
-
- local UnitThreatLevels = {}
-
- for UnitID, UnitData in pairs( self:GetSet() ) do
- local ThreatUnit = UnitData -- Wrapper.Unit#UNIT
- if ThreatUnit:IsAlive() then
- local UnitThreatLevel, UnitThreatLevelText = ThreatUnit:GetThreatLevel()
- local ThreatUnitName = ThreatUnit:GetName()
-
- UnitThreatLevels[UnitThreatLevel] = UnitThreatLevels[UnitThreatLevel] or {}
- UnitThreatLevels[UnitThreatLevel].UnitThreatLevelText = UnitThreatLevelText
- UnitThreatLevels[UnitThreatLevel].Units = UnitThreatLevels[UnitThreatLevel].Units or {}
- UnitThreatLevels[UnitThreatLevel].Units[ThreatUnitName] = ThreatUnit
- end
- end
-
- return UnitThreatLevels
-end
-
---- Calculate the maxium A2G threat level of the SET_UNIT.
--- @param #SET_UNIT self
-function SET_UNIT:CalculateThreatLevelA2G()
-
- local MaxThreatLevelA2G = 0
- for UnitName, UnitData in pairs( self:GetSet() ) do
- local ThreatUnit = UnitData -- Wrapper.Unit#UNIT
- local ThreatLevelA2G = ThreatUnit:GetThreatLevel()
- if ThreatLevelA2G > MaxThreatLevelA2G then
- MaxThreatLevelA2G = ThreatLevelA2G
- end
- end
-
- self:T3( MaxThreatLevelA2G )
- return MaxThreatLevelA2G
-
-end
-
-
---- Returns if the @{Set} has targets having a radar (of a given type).
--- @param #SET_UNIT self
--- @param Dcs.DCSWrapper.Unit#Unit.RadarType RadarType
--- @return #number The amount of radars in the Set with the given type
-function SET_UNIT:HasRadar( RadarType )
- self:F2( RadarType )
-
- local RadarCount = 0
- for UnitID, UnitData in pairs( self:GetSet()) do
- local UnitSensorTest = UnitData -- Wrapper.Unit#UNIT
- local HasSensors
- if RadarType then
- HasSensors = UnitSensorTest:HasSensors( Unit.SensorType.RADAR, RadarType )
- else
- HasSensors = UnitSensorTest:HasSensors( Unit.SensorType.RADAR )
- end
- self:T3(HasSensors)
- if HasSensors then
- RadarCount = RadarCount + 1
- end
- end
-
- return RadarCount
-end
-
---- Returns if the @{Set} has targets that can be SEADed.
--- @param #SET_UNIT self
--- @return #number The amount of SEADable units in the Set
-function SET_UNIT:HasSEAD()
- self:F2()
-
- local SEADCount = 0
- for UnitID, UnitData in pairs( self:GetSet()) do
- local UnitSEAD = UnitData -- Wrapper.Unit#UNIT
- if UnitSEAD:IsAlive() then
- local UnitSEADAttributes = UnitSEAD:GetDesc().attributes
-
- local HasSEAD = UnitSEAD:HasSEAD()
-
- self:T3(HasSEAD)
- if HasSEAD then
- SEADCount = SEADCount + 1
- end
- end
- end
-
- return SEADCount
-end
-
---- Returns if the @{Set} has ground targets.
--- @param #SET_UNIT self
--- @return #number The amount of ground targets in the Set.
-function SET_UNIT:HasGroundUnits()
- self:F2()
-
- local GroundUnitCount = 0
- for UnitID, UnitData in pairs( self:GetSet()) do
- local UnitTest = UnitData -- Wrapper.Unit#UNIT
- if UnitTest:IsGround() then
- GroundUnitCount = GroundUnitCount + 1
- end
- end
-
- return GroundUnitCount
-end
-
---- Returns if the @{Set} has friendly ground units.
--- @param #SET_UNIT self
--- @return #number The amount of ground targets in the Set.
-function SET_UNIT:HasFriendlyUnits( FriendlyCoalition )
- self:F2()
-
- local FriendlyUnitCount = 0
- for UnitID, UnitData in pairs( self:GetSet()) do
- local UnitTest = UnitData -- Wrapper.Unit#UNIT
- if UnitTest:IsFriendly( FriendlyCoalition ) then
- FriendlyUnitCount = FriendlyUnitCount + 1
- end
- end
-
- return FriendlyUnitCount
-end
-
-
-
------ Iterate the SET_UNIT and call an interator function for each **alive** player, providing the Unit of the player and optional parameters.
----- @param #SET_UNIT self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_UNIT. The function needs to accept a UNIT parameter.
----- @return #SET_UNIT self
---function SET_UNIT:ForEachPlayer( IteratorFunction, ... )
--- self:F2( arg )
---
--- self:ForEach( IteratorFunction, arg, self.PlayersAlive )
---
--- return self
---end
---
---
------ Iterate the SET_UNIT and call an interator function for each client, providing the Client to the function and optional parameters.
----- @param #SET_UNIT self
----- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_UNIT. The function needs to accept a CLIENT parameter.
----- @return #SET_UNIT self
---function SET_UNIT:ForEachClient( IteratorFunction, ... )
--- self:F2( arg )
---
--- self:ForEach( IteratorFunction, arg, self.Clients )
---
--- return self
---end
-
-
----
--- @param #SET_UNIT self
--- @param Wrapper.Unit#UNIT MUnit
--- @return #SET_UNIT self
-function SET_UNIT:IsIncludeObject( MUnit )
- self:F2( MUnit )
- local MUnitInclude = true
-
- if self.Filter.Coalitions then
- local MUnitCoalition = false
- for CoalitionID, CoalitionName in pairs( self.Filter.Coalitions ) do
- self:T3( { "Coalition:", MUnit:GetCoalition(), self.FilterMeta.Coalitions[CoalitionName], CoalitionName } )
- if self.FilterMeta.Coalitions[CoalitionName] and self.FilterMeta.Coalitions[CoalitionName] == MUnit:GetCoalition() then
- MUnitCoalition = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitCoalition
- end
-
- if self.Filter.Categories then
- local MUnitCategory = false
- for CategoryID, CategoryName in pairs( self.Filter.Categories ) do
- self:T3( { "Category:", MUnit:GetDesc().category, self.FilterMeta.Categories[CategoryName], CategoryName } )
- if self.FilterMeta.Categories[CategoryName] and self.FilterMeta.Categories[CategoryName] == MUnit:GetDesc().category then
- MUnitCategory = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitCategory
- end
-
- if self.Filter.Types then
- local MUnitType = false
- for TypeID, TypeName in pairs( self.Filter.Types ) do
- self:T3( { "Type:", MUnit:GetTypeName(), TypeName } )
- if TypeName == MUnit:GetTypeName() then
- MUnitType = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitType
- end
-
- if self.Filter.Countries then
- local MUnitCountry = false
- for CountryID, CountryName in pairs( self.Filter.Countries ) do
- self:T3( { "Country:", MUnit:GetCountry(), CountryName } )
- if country.id[CountryName] == MUnit:GetCountry() then
- MUnitCountry = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitCountry
- end
-
- if self.Filter.UnitPrefixes then
- local MUnitPrefix = false
- for UnitPrefixId, UnitPrefix in pairs( self.Filter.UnitPrefixes ) do
- self:T3( { "Prefix:", string.find( MUnit:GetName(), UnitPrefix, 1 ), UnitPrefix } )
- if string.find( MUnit:GetName(), UnitPrefix, 1 ) then
- MUnitPrefix = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitPrefix
- end
-
- if self.Filter.RadarTypes then
- local MUnitRadar = false
- for RadarTypeID, RadarType in pairs( self.Filter.RadarTypes ) do
- self:T3( { "Radar:", RadarType } )
- if MUnit:HasSensors( Unit.SensorType.RADAR, RadarType ) == true then
- if MUnit:GetRadar() == true then -- This call is necessary to evaluate the SEAD capability.
- self:T3( "RADAR Found" )
- end
- MUnitRadar = true
- end
- end
- MUnitInclude = MUnitInclude and MUnitRadar
- end
-
- if self.Filter.SEAD then
- local MUnitSEAD = false
- if MUnit:HasSEAD() == true then
- self:T3( "SEAD Found" )
- MUnitSEAD = true
- end
- MUnitInclude = MUnitInclude and MUnitSEAD
- end
-
- self:T2( MUnitInclude )
- return MUnitInclude
-end
-
-
---- Retrieve the type names of the @{Unit}s in the SET, delimited by an optional delimiter.
--- @param #SET_UNIT self
--- @param #string Delimiter (optional) The delimiter, which is default a comma.
--- @return #string The types of the @{Unit}s delimited.
-function SET_UNIT:GetTypeNames( Delimiter )
-
- Delimiter = Delimiter or ", "
- local TypeReport = REPORT:New()
- local Types = {}
-
- for UnitName, UnitData in pairs( self:GetSet() ) do
-
- local Unit = UnitData -- Wrapper.Unit#UNIT
- local UnitTypeName = Unit:GetTypeName()
-
- if not Types[UnitTypeName] then
- Types[UnitTypeName] = UnitTypeName
- TypeReport:Add( UnitTypeName )
- end
- end
-
- return TypeReport:Text( Delimiter )
-end
-
-
---- SET_CLIENT
-
-
---- @type SET_CLIENT
--- @extends Core.Set#SET_BASE
-
-
-
---- # 4) SET_CLIENT class, extends @{Set#SET_BASE}
---
--- Mission designers can use the @{Set#SET_CLIENT} class to build sets of units belonging to certain:
---
--- * Coalitions
--- * Categories
--- * Countries
--- * Client types
--- * Starting with certain prefix strings.
---
--- ## 4.1) SET_CLIENT constructor
---
--- Create a new SET_CLIENT object with the @{#SET_CLIENT.New} method:
---
--- * @{#SET_CLIENT.New}: Creates a new SET_CLIENT object.
---
--- ## 4.2) Add or Remove CLIENT(s) from SET_CLIENT
---
--- CLIENTs can be added and removed using the @{Set#SET_CLIENT.AddClientsByName} and @{Set#SET_CLIENT.RemoveClientsByName} respectively.
--- These methods take a single CLIENT name or an array of CLIENT names to be added or removed from SET_CLIENT.
---
--- ## 4.3) SET_CLIENT filter criteria
---
--- You can set filter criteria to define the set of clients within the SET_CLIENT.
--- Filter criteria are defined by:
---
--- * @{#SET_CLIENT.FilterCoalitions}: Builds the SET_CLIENT with the clients belonging to the coalition(s).
--- * @{#SET_CLIENT.FilterCategories}: Builds the SET_CLIENT with the clients belonging to the category(ies).
--- * @{#SET_CLIENT.FilterTypes}: Builds the SET_CLIENT with the clients belonging to the client type(s).
--- * @{#SET_CLIENT.FilterCountries}: Builds the SET_CLIENT with the clients belonging to the country(ies).
--- * @{#SET_CLIENT.FilterPrefixes}: Builds the SET_CLIENT with the clients starting with the same prefix string(s).
---
--- Once the filter criteria have been set for the SET_CLIENT, you can start filtering using:
---
--- * @{#SET_CLIENT.FilterStart}: Starts the filtering of the clients within the SET_CLIENT.
---
--- Planned filter criteria within development are (so these are not yet available):
---
--- * @{#SET_CLIENT.FilterZones}: Builds the SET_CLIENT with the clients within a @{Zone#ZONE}.
---
--- ## 4.4) SET_CLIENT iterators
---
--- Once the filters have been defined and the SET_CLIENT has been built, you can iterate the SET_CLIENT with the available iterator methods.
--- The iterator methods will walk the SET_CLIENT set, and call for each element within the set a function that you provide.
--- The following iterator methods are currently available within the SET_CLIENT:
---
--- * @{#SET_CLIENT.ForEachClient}: Calls a function for each alive client it finds within the SET_CLIENT.
---
--- ===
--- @field #SET_CLIENT SET_CLIENT
-SET_CLIENT = {
- ClassName = "SET_CLIENT",
- Clients = {},
- Filter = {
- Coalitions = nil,
- Categories = nil,
- Types = nil,
- Countries = nil,
- ClientPrefixes = nil,
- },
- FilterMeta = {
- Coalitions = {
- red = coalition.side.RED,
- blue = coalition.side.BLUE,
- neutral = coalition.side.NEUTRAL,
- },
- Categories = {
- plane = Unit.Category.AIRPLANE,
- helicopter = Unit.Category.HELICOPTER,
- ground = Unit.Category.GROUND_UNIT,
- ship = Unit.Category.SHIP,
- structure = Unit.Category.STRUCTURE,
- },
- },
-}
-
-
---- Creates a new SET_CLIENT object, building a set of clients belonging to a coalitions, categories, countries, types or with defined prefix names.
--- @param #SET_CLIENT self
--- @return #SET_CLIENT
--- @usage
--- -- Define a new SET_CLIENT Object. This DBObject will contain a reference to all Clients.
--- DBObject = SET_CLIENT:New()
-function SET_CLIENT:New()
- -- Inherits from BASE
- local self = BASE:Inherit( self, SET_BASE:New( _DATABASE.CLIENTS ) )
-
- return self
-end
-
---- Add CLIENT(s) to SET_CLIENT.
--- @param Core.Set#SET_CLIENT self
--- @param #string AddClientNames A single name or an array of CLIENT names.
--- @return self
-function SET_CLIENT:AddClientsByName( AddClientNames )
-
- local AddClientNamesArray = ( type( AddClientNames ) == "table" ) and AddClientNames or { AddClientNames }
-
- for AddClientID, AddClientName in pairs( AddClientNamesArray ) do
- self:Add( AddClientName, CLIENT:FindByName( AddClientName ) )
- end
-
- return self
-end
-
---- Remove CLIENT(s) from SET_CLIENT.
--- @param Core.Set#SET_CLIENT self
--- @param Wrapper.Client#CLIENT RemoveClientNames A single name or an array of CLIENT names.
--- @return self
-function SET_CLIENT:RemoveClientsByName( RemoveClientNames )
-
- local RemoveClientNamesArray = ( type( RemoveClientNames ) == "table" ) and RemoveClientNames or { RemoveClientNames }
-
- for RemoveClientID, RemoveClientName in pairs( RemoveClientNamesArray ) do
- self:Remove( RemoveClientName.ClientName )
- end
-
- return self
-end
-
-
---- Finds a Client based on the Client Name.
--- @param #SET_CLIENT self
--- @param #string ClientName
--- @return Wrapper.Client#CLIENT The found Client.
-function SET_CLIENT:FindClient( ClientName )
-
- local ClientFound = self.Set[ClientName]
- return ClientFound
-end
-
-
-
---- Builds a set of clients of coalitions.
--- Possible current coalitions are red, blue and neutral.
--- @param #SET_CLIENT self
--- @param #string Coalitions Can take the following values: "red", "blue", "neutral".
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterCoalitions( Coalitions )
- if not self.Filter.Coalitions then
- self.Filter.Coalitions = {}
- end
- if type( Coalitions ) ~= "table" then
- Coalitions = { Coalitions }
- end
- for CoalitionID, Coalition in pairs( Coalitions ) do
- self.Filter.Coalitions[Coalition] = Coalition
- end
- return self
-end
-
-
---- Builds a set of clients out of categories.
--- Possible current categories are plane, helicopter, ground, ship.
--- @param #SET_CLIENT self
--- @param #string Categories Can take the following values: "plane", "helicopter", "ground", "ship".
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterCategories( Categories )
- if not self.Filter.Categories then
- self.Filter.Categories = {}
- end
- if type( Categories ) ~= "table" then
- Categories = { Categories }
- end
- for CategoryID, Category in pairs( Categories ) do
- self.Filter.Categories[Category] = Category
- end
- return self
-end
-
-
---- Builds a set of clients of defined client types.
--- Possible current types are those types known within DCS world.
--- @param #SET_CLIENT self
--- @param #string Types Can take those type strings known within DCS world.
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterTypes( Types )
- if not self.Filter.Types then
- self.Filter.Types = {}
- end
- if type( Types ) ~= "table" then
- Types = { Types }
- end
- for TypeID, Type in pairs( Types ) do
- self.Filter.Types[Type] = Type
- end
- return self
-end
-
-
---- Builds a set of clients of defined countries.
--- Possible current countries are those known within DCS world.
--- @param #SET_CLIENT self
--- @param #string Countries Can take those country strings known within DCS world.
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterCountries( Countries )
- if not self.Filter.Countries then
- self.Filter.Countries = {}
- end
- if type( Countries ) ~= "table" then
- Countries = { Countries }
- end
- for CountryID, Country in pairs( Countries ) do
- self.Filter.Countries[Country] = Country
- end
- return self
-end
-
-
---- Builds a set of clients of defined client prefixes.
--- All the clients starting with the given prefixes will be included within the set.
--- @param #SET_CLIENT self
--- @param #string Prefixes The prefix of which the client name starts with.
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterPrefixes( Prefixes )
- if not self.Filter.ClientPrefixes then
- self.Filter.ClientPrefixes = {}
- end
- if type( Prefixes ) ~= "table" then
- Prefixes = { Prefixes }
- end
- for PrefixID, Prefix in pairs( Prefixes ) do
- self.Filter.ClientPrefixes[Prefix] = Prefix
- end
- return self
-end
-
-
-
-
---- Starts the filtering.
--- @param #SET_CLIENT self
--- @return #SET_CLIENT self
-function SET_CLIENT:FilterStart()
-
- if _DATABASE then
- self:_FilterStart()
- end
-
- return self
-end
-
---- Handles the Database to check on an event (birth) that the Object was added in the Database.
--- This is required, because sometimes the _DATABASE birth event gets called later than the SET_BASE birth event!
--- @param #SET_CLIENT self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the CLIENT
--- @return #table The CLIENT
-function SET_CLIENT:AddInDatabase( Event )
- self:F3( { Event } )
-
- return Event.IniDCSUnitName, self.Database[Event.IniDCSUnitName]
-end
-
---- Handles the Database to check on any event that Object exists in the Database.
--- This is required, because sometimes the _DATABASE event gets called later than the SET_BASE event or vise versa!
--- @param #SET_CLIENT self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the CLIENT
--- @return #table The CLIENT
-function SET_CLIENT:FindInDatabase( Event )
- self:F3( { Event } )
-
- return Event.IniDCSUnitName, self.Database[Event.IniDCSUnitName]
-end
-
---- Iterate the SET_CLIENT and call an interator function for each **alive** CLIENT, providing the CLIENT and optional parameters.
--- @param #SET_CLIENT self
--- @param #function IteratorFunction The function that will be called when there is an alive CLIENT in the SET_CLIENT. The function needs to accept a CLIENT parameter.
--- @return #SET_CLIENT self
-function SET_CLIENT:ForEachClient( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set )
-
- return self
-end
-
---- Iterate the SET_CLIENT and call an iterator function for each **alive** CLIENT presence completely in a @{Zone}, providing the CLIENT and optional parameters to the called function.
--- @param #SET_CLIENT self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive CLIENT in the SET_CLIENT. The function needs to accept a CLIENT parameter.
--- @return #SET_CLIENT self
-function SET_CLIENT:ForEachClientInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Client#CLIENT ClientObject
- function( ZoneObject, ClientObject )
- if ClientObject:IsInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
---- Iterate the SET_CLIENT and call an iterator function for each **alive** CLIENT presence not in a @{Zone}, providing the CLIENT and optional parameters to the called function.
--- @param #SET_CLIENT self
--- @param Core.Zone#ZONE ZoneObject The Zone to be tested for.
--- @param #function IteratorFunction The function that will be called when there is an alive CLIENT in the SET_CLIENT. The function needs to accept a CLIENT parameter.
--- @return #SET_CLIENT self
-function SET_CLIENT:ForEachClientNotInZone( ZoneObject, IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set,
- --- @param Core.Zone#ZONE_BASE ZoneObject
- -- @param Wrapper.Client#CLIENT ClientObject
- function( ZoneObject, ClientObject )
- if ClientObject:IsNotInZone( ZoneObject ) then
- return true
- else
- return false
- end
- end, { ZoneObject } )
-
- return self
-end
-
----
--- @param #SET_CLIENT self
--- @param Wrapper.Client#CLIENT MClient
--- @return #SET_CLIENT self
-function SET_CLIENT:IsIncludeObject( MClient )
- self:F2( MClient )
-
- local MClientInclude = true
-
- if MClient then
- local MClientName = MClient.UnitName
-
- if self.Filter.Coalitions then
- local MClientCoalition = false
- for CoalitionID, CoalitionName in pairs( self.Filter.Coalitions ) do
- local ClientCoalitionID = _DATABASE:GetCoalitionFromClientTemplate( MClientName )
- self:T3( { "Coalition:", ClientCoalitionID, self.FilterMeta.Coalitions[CoalitionName], CoalitionName } )
- if self.FilterMeta.Coalitions[CoalitionName] and self.FilterMeta.Coalitions[CoalitionName] == ClientCoalitionID then
- MClientCoalition = true
- end
- end
- self:T( { "Evaluated Coalition", MClientCoalition } )
- MClientInclude = MClientInclude and MClientCoalition
- end
-
- if self.Filter.Categories then
- local MClientCategory = false
- for CategoryID, CategoryName in pairs( self.Filter.Categories ) do
- local ClientCategoryID = _DATABASE:GetCategoryFromClientTemplate( MClientName )
- self:T3( { "Category:", ClientCategoryID, self.FilterMeta.Categories[CategoryName], CategoryName } )
- if self.FilterMeta.Categories[CategoryName] and self.FilterMeta.Categories[CategoryName] == ClientCategoryID then
- MClientCategory = true
- end
- end
- self:T( { "Evaluated Category", MClientCategory } )
- MClientInclude = MClientInclude and MClientCategory
- end
-
- if self.Filter.Types then
- local MClientType = false
- for TypeID, TypeName in pairs( self.Filter.Types ) do
- self:T3( { "Type:", MClient:GetTypeName(), TypeName } )
- if TypeName == MClient:GetTypeName() then
- MClientType = true
- end
- end
- self:T( { "Evaluated Type", MClientType } )
- MClientInclude = MClientInclude and MClientType
- end
-
- if self.Filter.Countries then
- local MClientCountry = false
- for CountryID, CountryName in pairs( self.Filter.Countries ) do
- local ClientCountryID = _DATABASE:GetCountryFromClientTemplate(MClientName)
- self:T3( { "Country:", ClientCountryID, country.id[CountryName], CountryName } )
- if country.id[CountryName] and country.id[CountryName] == ClientCountryID then
- MClientCountry = true
- end
- end
- self:T( { "Evaluated Country", MClientCountry } )
- MClientInclude = MClientInclude and MClientCountry
- end
-
- if self.Filter.ClientPrefixes then
- local MClientPrefix = false
- for ClientPrefixId, ClientPrefix in pairs( self.Filter.ClientPrefixes ) do
- self:T3( { "Prefix:", string.find( MClient.UnitName, ClientPrefix, 1 ), ClientPrefix } )
- if string.find( MClient.UnitName, ClientPrefix, 1 ) then
- MClientPrefix = true
- end
- end
- self:T( { "Evaluated Prefix", MClientPrefix } )
- MClientInclude = MClientInclude and MClientPrefix
- end
- end
-
- self:T2( MClientInclude )
- return MClientInclude
-end
-
---- @type SET_AIRBASE
--- @extends Core.Set#SET_BASE
-
---- # 5) SET_AIRBASE class, extends @{Set#SET_BASE}
---
--- Mission designers can use the @{Set#SET_AIRBASE} class to build sets of airbases optionally belonging to certain:
---
--- * Coalitions
---
--- ## 5.1) SET_AIRBASE constructor
---
--- Create a new SET_AIRBASE object with the @{#SET_AIRBASE.New} method:
---
--- * @{#SET_AIRBASE.New}: Creates a new SET_AIRBASE object.
---
--- ## 5.2) Add or Remove AIRBASEs from SET_AIRBASE
---
--- AIRBASEs can be added and removed using the @{Set#SET_AIRBASE.AddAirbasesByName} and @{Set#SET_AIRBASE.RemoveAirbasesByName} respectively.
--- These methods take a single AIRBASE name or an array of AIRBASE names to be added or removed from SET_AIRBASE.
---
--- ## 5.3) SET_AIRBASE filter criteria
---
--- You can set filter criteria to define the set of clients within the SET_AIRBASE.
--- Filter criteria are defined by:
---
--- * @{#SET_AIRBASE.FilterCoalitions}: Builds the SET_AIRBASE with the airbases belonging to the coalition(s).
---
--- Once the filter criteria have been set for the SET_AIRBASE, you can start filtering using:
---
--- * @{#SET_AIRBASE.FilterStart}: Starts the filtering of the airbases within the SET_AIRBASE.
---
--- ## 5.4) SET_AIRBASE iterators
---
--- Once the filters have been defined and the SET_AIRBASE has been built, you can iterate the SET_AIRBASE with the available iterator methods.
--- The iterator methods will walk the SET_AIRBASE set, and call for each airbase within the set a function that you provide.
--- The following iterator methods are currently available within the SET_AIRBASE:
---
--- * @{#SET_AIRBASE.ForEachAirbase}: Calls a function for each airbase it finds within the SET_AIRBASE.
---
--- ===
--- @field #SET_AIRBASE SET_AIRBASE
-SET_AIRBASE = {
- ClassName = "SET_AIRBASE",
- Airbases = {},
- Filter = {
- Coalitions = nil,
- },
- FilterMeta = {
- Coalitions = {
- red = coalition.side.RED,
- blue = coalition.side.BLUE,
- neutral = coalition.side.NEUTRAL,
- },
- Categories = {
- airdrome = Airbase.Category.AIRDROME,
- helipad = Airbase.Category.HELIPAD,
- ship = Airbase.Category.SHIP,
- },
- },
-}
-
-
---- Creates a new SET_AIRBASE object, building a set of airbases belonging to a coalitions and categories.
--- @param #SET_AIRBASE self
--- @return #SET_AIRBASE self
--- @usage
--- -- Define a new SET_AIRBASE Object. The DatabaseSet will contain a reference to all Airbases.
--- DatabaseSet = SET_AIRBASE:New()
-function SET_AIRBASE:New()
- -- Inherits from BASE
- local self = BASE:Inherit( self, SET_BASE:New( _DATABASE.AIRBASES ) )
-
- return self
-end
-
---- Add AIRBASEs to SET_AIRBASE.
--- @param Core.Set#SET_AIRBASE self
--- @param #string AddAirbaseNames A single name or an array of AIRBASE names.
--- @return self
-function SET_AIRBASE:AddAirbasesByName( AddAirbaseNames )
-
- local AddAirbaseNamesArray = ( type( AddAirbaseNames ) == "table" ) and AddAirbaseNames or { AddAirbaseNames }
-
- for AddAirbaseID, AddAirbaseName in pairs( AddAirbaseNamesArray ) do
- self:Add( AddAirbaseName, AIRBASE:FindByName( AddAirbaseName ) )
- end
-
- return self
-end
-
---- Remove AIRBASEs from SET_AIRBASE.
--- @param Core.Set#SET_AIRBASE self
--- @param Wrapper.Airbase#AIRBASE RemoveAirbaseNames A single name or an array of AIRBASE names.
--- @return self
-function SET_AIRBASE:RemoveAirbasesByName( RemoveAirbaseNames )
-
- local RemoveAirbaseNamesArray = ( type( RemoveAirbaseNames ) == "table" ) and RemoveAirbaseNames or { RemoveAirbaseNames }
-
- for RemoveAirbaseID, RemoveAirbaseName in pairs( RemoveAirbaseNamesArray ) do
- self:Remove( RemoveAirbaseName.AirbaseName )
- end
-
- return self
-end
-
-
---- Finds a Airbase based on the Airbase Name.
--- @param #SET_AIRBASE self
--- @param #string AirbaseName
--- @return Wrapper.Airbase#AIRBASE The found Airbase.
-function SET_AIRBASE:FindAirbase( AirbaseName )
-
- local AirbaseFound = self.Set[AirbaseName]
- return AirbaseFound
-end
-
-
-
---- Builds a set of airbases of coalitions.
--- Possible current coalitions are red, blue and neutral.
--- @param #SET_AIRBASE self
--- @param #string Coalitions Can take the following values: "red", "blue", "neutral".
--- @return #SET_AIRBASE self
-function SET_AIRBASE:FilterCoalitions( Coalitions )
- if not self.Filter.Coalitions then
- self.Filter.Coalitions = {}
- end
- if type( Coalitions ) ~= "table" then
- Coalitions = { Coalitions }
- end
- for CoalitionID, Coalition in pairs( Coalitions ) do
- self.Filter.Coalitions[Coalition] = Coalition
- end
- return self
-end
-
-
---- Builds a set of airbases out of categories.
--- Possible current categories are plane, helicopter, ground, ship.
--- @param #SET_AIRBASE self
--- @param #string Categories Can take the following values: "airdrome", "helipad", "ship".
--- @return #SET_AIRBASE self
-function SET_AIRBASE:FilterCategories( Categories )
- if not self.Filter.Categories then
- self.Filter.Categories = {}
- end
- if type( Categories ) ~= "table" then
- Categories = { Categories }
- end
- for CategoryID, Category in pairs( Categories ) do
- self.Filter.Categories[Category] = Category
- end
- return self
-end
-
---- Starts the filtering.
--- @param #SET_AIRBASE self
--- @return #SET_AIRBASE self
-function SET_AIRBASE:FilterStart()
-
- if _DATABASE then
- self:_FilterStart()
- end
-
- return self
-end
-
-
---- Handles the Database to check on an event (birth) that the Object was added in the Database.
--- This is required, because sometimes the _DATABASE birth event gets called later than the SET_BASE birth event!
--- @param #SET_AIRBASE self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the AIRBASE
--- @return #table The AIRBASE
-function SET_AIRBASE:AddInDatabase( Event )
- self:F3( { Event } )
-
- return Event.IniDCSUnitName, self.Database[Event.IniDCSUnitName]
-end
-
---- Handles the Database to check on any event that Object exists in the Database.
--- This is required, because sometimes the _DATABASE event gets called later than the SET_BASE event or vise versa!
--- @param #SET_AIRBASE self
--- @param Core.Event#EVENTDATA Event
--- @return #string The name of the AIRBASE
--- @return #table The AIRBASE
-function SET_AIRBASE:FindInDatabase( Event )
- self:F3( { Event } )
-
- return Event.IniDCSUnitName, self.Database[Event.IniDCSUnitName]
-end
-
---- Iterate the SET_AIRBASE and call an interator function for each AIRBASE, providing the AIRBASE and optional parameters.
--- @param #SET_AIRBASE self
--- @param #function IteratorFunction The function that will be called when there is an alive AIRBASE in the SET_AIRBASE. The function needs to accept a AIRBASE parameter.
--- @return #SET_AIRBASE self
-function SET_AIRBASE:ForEachAirbase( IteratorFunction, ... )
- self:F2( arg )
-
- self:ForEach( IteratorFunction, arg, self.Set )
-
- return self
-end
-
---- Iterate the SET_AIRBASE while identifying the nearest @{Airbase#AIRBASE} from a @{Point#POINT_VEC2}.
--- @param #SET_AIRBASE self
--- @param Core.Point#POINT_VEC2 PointVec2 A @{Point#POINT_VEC2} object from where to evaluate the closest @{Airbase#AIRBASE}.
--- @return Wrapper.Airbase#AIRBASE The closest @{Airbase#AIRBASE}.
-function SET_AIRBASE:FindNearestAirbaseFromPointVec2( PointVec2 )
- self:F2( PointVec2 )
-
- local NearestAirbase = self:FindNearestObjectFromPointVec2( PointVec2 )
- return NearestAirbase
-end
-
-
-
----
--- @param #SET_AIRBASE self
--- @param Wrapper.Airbase#AIRBASE MAirbase
--- @return #SET_AIRBASE self
-function SET_AIRBASE:IsIncludeObject( MAirbase )
- self:F2( MAirbase )
-
- local MAirbaseInclude = true
-
- if MAirbase then
- local MAirbaseName = MAirbase:GetName()
-
- if self.Filter.Coalitions then
- local MAirbaseCoalition = false
- for CoalitionID, CoalitionName in pairs( self.Filter.Coalitions ) do
- local AirbaseCoalitionID = _DATABASE:GetCoalitionFromAirbase( MAirbaseName )
- self:T3( { "Coalition:", AirbaseCoalitionID, self.FilterMeta.Coalitions[CoalitionName], CoalitionName } )
- if self.FilterMeta.Coalitions[CoalitionName] and self.FilterMeta.Coalitions[CoalitionName] == AirbaseCoalitionID then
- MAirbaseCoalition = true
- end
- end
- self:T( { "Evaluated Coalition", MAirbaseCoalition } )
- MAirbaseInclude = MAirbaseInclude and MAirbaseCoalition
- end
-
- if self.Filter.Categories then
- local MAirbaseCategory = false
- for CategoryID, CategoryName in pairs( self.Filter.Categories ) do
- local AirbaseCategoryID = _DATABASE:GetCategoryFromAirbase( MAirbaseName )
- self:T3( { "Category:", AirbaseCategoryID, self.FilterMeta.Categories[CategoryName], CategoryName } )
- if self.FilterMeta.Categories[CategoryName] and self.FilterMeta.Categories[CategoryName] == AirbaseCategoryID then
- MAirbaseCategory = true
- end
- end
- self:T( { "Evaluated Category", MAirbaseCategory } )
- MAirbaseInclude = MAirbaseInclude and MAirbaseCategory
- end
- end
-
- self:T2( MAirbaseInclude )
- return MAirbaseInclude
-end
---- **Core** - **POINT\_VEC** classes define an **extensive API** to **manage 3D points** in the simulation space.
---
--- 1) @{Point#POINT_VEC3} class, extends @{Base#BASE}
--- ==================================================
--- The @{Point#POINT_VEC3} class defines a 3D point in the simulator.
---
--- **Important Note:** Most of the functions in this section were taken from MIST, and reworked to OO concepts.
--- In order to keep the credibility of the the author, I want to emphasize that the of the MIST framework was created by Grimes, who you can find on the Eagle Dynamics Forums.
---
--- ## 1.1) POINT_VEC3 constructor
---
--- A new POINT_VEC3 instance can be created with:
---
--- * @{Point#POINT_VEC3.New}(): a 3D point.
--- * @{Point#POINT_VEC3.NewFromVec3}(): a 3D point created from a @{DCSTypes#Vec3}.
---
--- ## 1.2) Manupulate the X, Y, Z coordinates of the point
---
--- A POINT_VEC3 class works in 3D space. It contains internally an X, Y, Z coordinate.
--- Methods exist to manupulate these coordinates.
---
--- The current X, Y, Z axis can be retrieved with the methods @{#POINT_VEC3.GetX}(), @{#POINT_VEC3.GetY}(), @{#POINT_VEC3.GetZ}() respectively.
--- The methods @{#POINT_VEC3.SetX}(), @{#POINT_VEC3.SetY}(), @{#POINT_VEC3.SetZ}() change the respective axis with a new value.
--- The current axis values can be changed by using the methods @{#POINT_VEC3.AddX}(), @{#POINT_VEC3.AddY}(), @{#POINT_VEC3.AddZ}()
--- to add or substract a value from the current respective axis value.
--- Note that the Set and Add methods return the current POINT_VEC3 object, so these manipulation methods can be chained... For example:
---
--- local Vec3 = PointVec3:AddX( 100 ):AddZ( 150 ):GetVec3()
---
--- ## 1.3) Create waypoints for routes
---
--- A POINT_VEC3 can prepare waypoints for Ground, Air and Naval groups to be embedded into a Route.
---
---
--- ## 1.5) Smoke, flare, explode, illuminate
---
--- At the point a smoke, flare, explosion and illumination bomb can be triggered. Use the following methods:
---
--- ### 1.5.1) Smoke
---
--- * @{#POINT_VEC3.Smoke}(): To smoke the point in a certain color.
--- * @{#POINT_VEC3.SmokeBlue}(): To smoke the point in blue.
--- * @{#POINT_VEC3.SmokeRed}(): To smoke the point in red.
--- * @{#POINT_VEC3.SmokeOrange}(): To smoke the point in orange.
--- * @{#POINT_VEC3.SmokeWhite}(): To smoke the point in white.
--- * @{#POINT_VEC3.SmokeGreen}(): To smoke the point in green.
---
--- ### 1.5.2) Flare
---
--- * @{#POINT_VEC3.Flare}(): To flare the point in a certain color.
--- * @{#POINT_VEC3.FlareRed}(): To flare the point in red.
--- * @{#POINT_VEC3.FlareYellow}(): To flare the point in yellow.
--- * @{#POINT_VEC3.FlareWhite}(): To flare the point in white.
--- * @{#POINT_VEC3.FlareGreen}(): To flare the point in green.
---
--- ### 1.5.3) Explode
---
--- * @{#POINT_VEC3.Explosion}(): To explode the point with a certain intensity.
---
--- ### 1.5.4) Illuminate
---
--- * @{#POINT_VEC3.IlluminationBomb}(): To illuminate the point.
---
---
--- 2) @{Point#POINT_VEC2} class, extends @{Point#POINT_VEC3}
--- =========================================================
--- The @{Point#POINT_VEC2} class defines a 2D point in the simulator. The height coordinate (if needed) will be the land height + an optional added height specified.
---
--- 2.1) POINT_VEC2 constructor
--- ---------------------------
--- A new POINT_VEC2 instance can be created with:
---
--- * @{Point#POINT_VEC2.New}(): a 2D point, taking an additional height parameter.
--- * @{Point#POINT_VEC2.NewFromVec2}(): a 2D point created from a @{DCSTypes#Vec2}.
---
--- ## 1.2) Manupulate the X, Altitude, Y coordinates of the 2D point
---
--- A POINT_VEC2 class works in 2D space, with an altitude setting. It contains internally an X, Altitude, Y coordinate.
--- Methods exist to manupulate these coordinates.
---
--- The current X, Altitude, Y axis can be retrieved with the methods @{#POINT_VEC2.GetX}(), @{#POINT_VEC2.GetAlt}(), @{#POINT_VEC2.GetY}() respectively.
--- The methods @{#POINT_VEC2.SetX}(), @{#POINT_VEC2.SetAlt}(), @{#POINT_VEC2.SetY}() change the respective axis with a new value.
--- The current Lat(itude), Alt(itude), Lon(gitude) values can also be retrieved with the methods @{#POINT_VEC2.GetLat}(), @{#POINT_VEC2.GetAlt}(), @{#POINT_VEC2.GetLon}() respectively.
--- The current axis values can be changed by using the methods @{#POINT_VEC2.AddX}(), @{#POINT_VEC2.AddAlt}(), @{#POINT_VEC2.AddY}()
--- to add or substract a value from the current respective axis value.
--- Note that the Set and Add methods return the current POINT_VEC2 object, so these manipulation methods can be chained... For example:
---
--- local Vec2 = PointVec2:AddX( 100 ):AddY( 2000 ):GetVec2()
---
--- ===
---
--- **API CHANGE HISTORY**
--- ======================
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-03-03: POINT\_VEC3:**Explosion( ExplosionIntensity )** added.
--- 2017-03-03: POINT\_VEC3:**IlluminationBomb()** added.
---
--- 2017-02-18: POINT\_VEC3:**NewFromVec2( Vec2, LandHeightAdd )** added.
---
--- 2016-08-12: POINT\_VEC3:**Translate( Distance, Angle )** added.
---
--- 2016-08-06: Made PointVec3 and Vec3, PointVec2 and Vec2 terminology used in the code consistent.
---
--- * Replaced method _Point_Vec3() to **Vec3**() where the code manages a Vec3. Replaced all references to the method.
--- * Replaced method _Point_Vec2() to **Vec2**() where the code manages a Vec2. Replaced all references to the method.
--- * Replaced method Random_Point_Vec3() to **RandomVec3**() where the code manages a Vec3. Replaced all references to the method.
--- .
--- ===
---
--- ### Authors:
---
--- * FlightControl : Design & Programming
---
--- ### Contributions:
---
--- @module Point
-
---- The POINT_VEC3 class
--- @type POINT_VEC3
--- @field #number x The x coordinate in 3D space.
--- @field #number y The y coordinate in 3D space.
--- @field #number z The z coordiante in 3D space.
--- @field Utilities.Utils#SMOKECOLOR SmokeColor
--- @field Utilities.Utils#FLARECOLOR FlareColor
--- @field #POINT_VEC3.RoutePointAltType RoutePointAltType
--- @field #POINT_VEC3.RoutePointType RoutePointType
--- @field #POINT_VEC3.RoutePointAction RoutePointAction
--- @extends Core.Base#BASE
-POINT_VEC3 = {
- ClassName = "POINT_VEC3",
- Metric = true,
- RoutePointAltType = {
- BARO = "BARO",
- },
- RoutePointType = {
- TakeOffParking = "TakeOffParking",
- TurningPoint = "Turning Point",
- },
- RoutePointAction = {
- FromParkingArea = "From Parking Area",
- TurningPoint = "Turning Point",
- },
-}
-
---- The POINT_VEC2 class
--- @type POINT_VEC2
--- @field Dcs.DCSTypes#Distance x The x coordinate in meters.
--- @field Dcs.DCSTypes#Distance y the y coordinate in meters.
--- @extends Core.Point#POINT_VEC3
-POINT_VEC2 = {
- ClassName = "POINT_VEC2",
-}
-
-
-do -- POINT_VEC3
-
---- RoutePoint AltTypes
--- @type POINT_VEC3.RoutePointAltType
--- @field BARO "BARO"
-
---- RoutePoint Types
--- @type POINT_VEC3.RoutePointType
--- @field TakeOffParking "TakeOffParking"
--- @field TurningPoint "Turning Point"
-
---- RoutePoint Actions
--- @type POINT_VEC3.RoutePointAction
--- @field FromParkingArea "From Parking Area"
--- @field TurningPoint "Turning Point"
-
--- Constructor.
-
---- Create a new POINT_VEC3 object.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance x The x coordinate of the Vec3 point, pointing to the North.
--- @param Dcs.DCSTypes#Distance y The y coordinate of the Vec3 point, pointing Upwards.
--- @param Dcs.DCSTypes#Distance z The z coordinate of the Vec3 point, pointing to the Right.
--- @return Core.Point#POINT_VEC3 self
-function POINT_VEC3:New( x, y, z )
-
- local self = BASE:Inherit( self, BASE:New() )
- self.x = x
- self.y = y
- self.z = z
-
- return self
-end
-
---- Create a new POINT_VEC3 object from Vec2 coordinates.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 point.
--- @return Core.Point#POINT_VEC3 self
-function POINT_VEC3:NewFromVec2( Vec2, LandHeightAdd )
-
- local LandHeight = land.getHeight( Vec2 )
-
- LandHeightAdd = LandHeightAdd or 0
- LandHeight = LandHeight + LandHeightAdd
-
- self = self:New( Vec2.x, LandHeight, Vec2.y )
-
- self:F2( self )
-
- return self
-end
-
---- Create a new POINT_VEC3 object from Vec3 coordinates.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Vec3 Vec3 The Vec3 point.
--- @return Core.Point#POINT_VEC3 self
-function POINT_VEC3:NewFromVec3( Vec3 )
-
- self = self:New( Vec3.x, Vec3.y, Vec3.z )
- self:F2( self )
- return self
-end
-
-
---- Return the coordinates of the POINT_VEC3 in Vec3 format.
--- @param #POINT_VEC3 self
--- @return Dcs.DCSTypes#Vec3 The Vec3 coodinate.
-function POINT_VEC3:GetVec3()
- return { x = self.x, y = self.y, z = self.z }
-end
-
---- Return the coordinates of the POINT_VEC3 in Vec2 format.
--- @param #POINT_VEC3 self
--- @return Dcs.DCSTypes#Vec2 The Vec2 coodinate.
-function POINT_VEC3:GetVec2()
- return { x = self.x, y = self.z }
-end
-
-
---- Return the x coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @return #number The x coodinate.
-function POINT_VEC3:GetX()
- return self.x
-end
-
---- Return the y coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @return #number The y coodinate.
-function POINT_VEC3:GetY()
- return self.y
-end
-
---- Return the z coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @return #number The z coodinate.
-function POINT_VEC3:GetZ()
- return self.z
-end
-
---- Set the x coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number x The x coordinate.
--- @return #POINT_VEC3
-function POINT_VEC3:SetX( x )
- self.x = x
- return self
-end
-
---- Set the y coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number y The y coordinate.
--- @return #POINT_VEC3
-function POINT_VEC3:SetY( y )
- self.y = y
- return self
-end
-
---- Set the z coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number z The z coordinate.
--- @return #POINT_VEC3
-function POINT_VEC3:SetZ( z )
- self.z = z
- return self
-end
-
---- Add to the x coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number x The x coordinate value to add to the current x coodinate.
--- @return #POINT_VEC3
-function POINT_VEC3:AddX( x )
- self.x = self.x + x
- return self
-end
-
---- Add to the y coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number y The y coordinate value to add to the current y coodinate.
--- @return #POINT_VEC3
-function POINT_VEC3:AddY( y )
- self.y = self.y + y
- return self
-end
-
---- Add to the z coordinate of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #number z The z coordinate value to add to the current z coodinate.
--- @return #POINT_VEC3
-function POINT_VEC3:AddZ( z )
- self.z = self.z +z
- return self
-end
-
---- Return a random Vec2 within an Outer Radius and optionally NOT within an Inner Radius of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance OuterRadius
--- @param Dcs.DCSTypes#Distance InnerRadius
--- @return Dcs.DCSTypes#Vec2 Vec2
-function POINT_VEC3:GetRandomVec2InRadius( OuterRadius, InnerRadius )
- self:F2( { OuterRadius, InnerRadius } )
-
- local Theta = 2 * math.pi * math.random()
- local Radials = math.random() + math.random()
- if Radials > 1 then
- Radials = 2 - Radials
- end
-
- local RadialMultiplier
- if InnerRadius and InnerRadius <= OuterRadius then
- RadialMultiplier = ( OuterRadius - InnerRadius ) * Radials + InnerRadius
- else
- RadialMultiplier = OuterRadius * Radials
- end
-
- local RandomVec2
- if OuterRadius > 0 then
- RandomVec2 = { x = math.cos( Theta ) * RadialMultiplier + self:GetX(), y = math.sin( Theta ) * RadialMultiplier + self:GetZ() }
- else
- RandomVec2 = { x = self:GetX(), y = self:GetZ() }
- end
-
- return RandomVec2
-end
-
---- Return a random POINT_VEC2 within an Outer Radius and optionally NOT within an Inner Radius of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance OuterRadius
--- @param Dcs.DCSTypes#Distance InnerRadius
--- @return #POINT_VEC2
-function POINT_VEC3:GetRandomPointVec2InRadius( OuterRadius, InnerRadius )
- self:F2( { OuterRadius, InnerRadius } )
-
- return POINT_VEC2:NewFromVec2( self:GetRandomVec2InRadius( OuterRadius, InnerRadius ) )
-end
-
---- Return a random Vec3 within an Outer Radius and optionally NOT within an Inner Radius of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance OuterRadius
--- @param Dcs.DCSTypes#Distance InnerRadius
--- @return Dcs.DCSTypes#Vec3 Vec3
-function POINT_VEC3:GetRandomVec3InRadius( OuterRadius, InnerRadius )
-
- local RandomVec2 = self:GetRandomVec2InRadius( OuterRadius, InnerRadius )
- local y = self:GetY() + math.random( InnerRadius, OuterRadius )
- local RandomVec3 = { x = RandomVec2.x, y = y, z = RandomVec2.y }
-
- return RandomVec3
-end
-
---- Return a random POINT_VEC3 within an Outer Radius and optionally NOT within an Inner Radius of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance OuterRadius
--- @param Dcs.DCSTypes#Distance InnerRadius
--- @return #POINT_VEC3
-function POINT_VEC3:GetRandomPointVec3InRadius( OuterRadius, InnerRadius )
-
- return POINT_VEC3:NewFromVec3( self:GetRandomVec3InRadius( OuterRadius, InnerRadius ) )
-end
-
-
---- Return a direction vector Vec3 from POINT_VEC3 to the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #POINT_VEC3 TargetPointVec3 The target POINT_VEC3.
--- @return Dcs.DCSTypes#Vec3 DirectionVec3 The direction vector in Vec3 format.
-function POINT_VEC3:GetDirectionVec3( TargetPointVec3 )
- return { x = TargetPointVec3:GetX() - self:GetX(), y = TargetPointVec3:GetY() - self:GetY(), z = TargetPointVec3:GetZ() - self:GetZ() }
-end
-
---- Get a correction in radians of the real magnetic north of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @return #number CorrectionRadians The correction in radians.
-function POINT_VEC3:GetNorthCorrectionRadians()
- local TargetVec3 = self:GetVec3()
- local lat, lon = coord.LOtoLL(TargetVec3)
- local north_posit = coord.LLtoLO(lat + 1, lon)
- return math.atan2( north_posit.z - TargetVec3.z, north_posit.x - TargetVec3.x )
-end
-
-
---- Return a direction in radians from the POINT_VEC3 using a direction vector in Vec3 format.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Vec3 DirectionVec3 The direction vector in Vec3 format.
--- @return #number DirectionRadians The direction in radians.
-function POINT_VEC3:GetDirectionRadians( DirectionVec3 )
- local DirectionRadians = math.atan2( DirectionVec3.z, DirectionVec3.x )
- --DirectionRadians = DirectionRadians + self:GetNorthCorrectionRadians()
- if DirectionRadians < 0 then
- DirectionRadians = DirectionRadians + 2 * math.pi -- put dir in range of 0 to 2*pi ( the full circle )
- end
- return DirectionRadians
-end
-
---- Return the 2D distance in meters between the target POINT_VEC3 and the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #POINT_VEC3 TargetPointVec3 The target POINT_VEC3.
--- @return Dcs.DCSTypes#Distance Distance The distance in meters.
-function POINT_VEC3:Get2DDistance( TargetPointVec3 )
- local TargetVec3 = TargetPointVec3:GetVec3()
- local SourceVec3 = self:GetVec3()
- return ( ( TargetVec3.x - SourceVec3.x ) ^ 2 + ( TargetVec3.z - SourceVec3.z ) ^ 2 ) ^ 0.5
-end
-
---- Return the 3D distance in meters between the target POINT_VEC3 and the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #POINT_VEC3 TargetPointVec3 The target POINT_VEC3.
--- @return Dcs.DCSTypes#Distance Distance The distance in meters.
-function POINT_VEC3:Get3DDistance( TargetPointVec3 )
- local TargetVec3 = TargetPointVec3:GetVec3()
- local SourceVec3 = self:GetVec3()
- return ( ( TargetVec3.x - SourceVec3.x ) ^ 2 + ( TargetVec3.y - SourceVec3.y ) ^ 2 + ( TargetVec3.z - SourceVec3.z ) ^ 2 ) ^ 0.5
-end
-
---- Provides a Bearing / Range string
--- @param #POINT_VEC3 self
--- @param #number AngleRadians The angle in randians
--- @param #number Distance The distance
--- @return #string The BR Text
-function POINT_VEC3:ToStringBR( AngleRadians, Distance )
-
- AngleRadians = UTILS.Round( UTILS.ToDegree( AngleRadians ), 0 )
- if self:IsMetric() then
- Distance = UTILS.Round( Distance / 1000, 2 )
- else
- Distance = UTILS.Round( UTILS.MetersToNM( Distance ), 2 )
- end
-
- local s = string.format( '%03d', AngleRadians ) .. ' for ' .. Distance
-
- s = s .. self:GetAltitudeText() -- When the POINT is a VEC2, there will be no altitude shown.
-
- return s
-end
-
---- Provides a Bearing / Range string
--- @param #POINT_VEC3 self
--- @param #number AngleRadians The angle in randians
--- @param #number Distance The distance
--- @return #string The BR Text
-function POINT_VEC3:ToStringLL( acc, DMS )
-
- acc = acc or 3
- local lat, lon = coord.LOtoLL( self:GetVec3() )
- return UTILS.tostringLL(lat, lon, acc, DMS)
-end
-
---- Return the altitude text of the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @return #string Altitude text.
-function POINT_VEC3:GetAltitudeText()
- if self:IsMetric() then
- return ' at ' .. UTILS.Round( self:GetY(), 0 )
- else
- return ' at ' .. UTILS.Round( UTILS.MetersToFeet( self:GetY() ), 0 )
- end
-end
-
---- Return a BR string from a POINT_VEC3 to the POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param #POINT_VEC3 TargetPointVec3 The target POINT_VEC3.
--- @return #string The BR text.
-function POINT_VEC3:GetBRText( TargetPointVec3 )
- local DirectionVec3 = self:GetDirectionVec3( TargetPointVec3 )
- local AngleRadians = self:GetDirectionRadians( DirectionVec3 )
- local Distance = self:Get2DDistance( TargetPointVec3 )
- return self:ToStringBR( AngleRadians, Distance )
-end
-
---- Sets the POINT_VEC3 metric or NM.
--- @param #POINT_VEC3 self
--- @param #boolean Metric true means metric, false means NM.
-function POINT_VEC3:SetMetric( Metric )
- self.Metric = Metric
-end
-
---- Gets if the POINT_VEC3 is metric or NM.
--- @param #POINT_VEC3 self
--- @return #boolean Metric true means metric, false means NM.
-function POINT_VEC3:IsMetric()
- return self.Metric
-end
-
---- Add a Distance in meters from the POINT_VEC3 horizontal plane, with the given angle, and calculate the new POINT_VEC3.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Distance Distance The Distance to be added in meters.
--- @param Dcs.DCSTypes#Angle Angle The Angle in degrees.
--- @return #POINT_VEC3 The new calculated POINT_VEC3.
-function POINT_VEC3:Translate( Distance, Angle )
- local SX = self:GetX()
- local SZ = self:GetZ()
- local Radians = Angle / 180 * math.pi
- local TX = Distance * math.cos( Radians ) + SX
- local TZ = Distance * math.sin( Radians ) + SZ
-
- return POINT_VEC3:New( TX, self:GetY(), TZ )
-end
-
-
-
---- Build an air type route point.
--- @param #POINT_VEC3 self
--- @param #POINT_VEC3.RoutePointAltType AltType The altitude type.
--- @param #POINT_VEC3.RoutePointType Type The route point type.
--- @param #POINT_VEC3.RoutePointAction Action The route point action.
--- @param Dcs.DCSTypes#Speed Speed Airspeed in km/h.
--- @param #boolean SpeedLocked true means the speed is locked.
--- @return #table The route point.
-function POINT_VEC3:RoutePointAir( AltType, Type, Action, Speed, SpeedLocked )
- self:F2( { AltType, Type, Action, Speed, SpeedLocked } )
-
- local RoutePoint = {}
- RoutePoint.x = self.x
- RoutePoint.y = self.z
- RoutePoint.alt = self.y
- RoutePoint.alt_type = AltType
-
- RoutePoint.type = Type
- RoutePoint.action = Action
-
- RoutePoint.speed = Speed / 3.6
- RoutePoint.speed_locked = true
-
--- ["task"] =
--- {
--- ["id"] = "ComboTask",
--- ["params"] =
--- {
--- ["tasks"] =
--- {
--- }, -- end of ["tasks"]
--- }, -- end of ["params"]
--- }, -- end of ["task"]
-
-
- RoutePoint.task = {}
- RoutePoint.task.id = "ComboTask"
- RoutePoint.task.params = {}
- RoutePoint.task.params.tasks = {}
-
-
- return RoutePoint
-end
-
---- Build an ground type route point.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Speed Speed Speed in km/h.
--- @param #POINT_VEC3.RoutePointAction Formation The route point Formation.
--- @return #table The route point.
-function POINT_VEC3:RoutePointGround( Speed, Formation )
- self:F2( { Formation, Speed } )
-
- local RoutePoint = {}
- RoutePoint.x = self.x
- RoutePoint.y = self.z
-
- RoutePoint.action = Formation or ""
-
-
- RoutePoint.speed = Speed / 3.6
- RoutePoint.speed_locked = true
-
--- ["task"] =
--- {
--- ["id"] = "ComboTask",
--- ["params"] =
--- {
--- ["tasks"] =
--- {
--- }, -- end of ["tasks"]
--- }, -- end of ["params"]
--- }, -- end of ["task"]
-
-
- RoutePoint.task = {}
- RoutePoint.task.id = "ComboTask"
- RoutePoint.task.params = {}
- RoutePoint.task.params.tasks = {}
-
-
- return RoutePoint
-end
-
---- Creates an explosion at the point of a certain intensity.
--- @param #POINT_VEC3 self
--- @param #number ExplosionIntensity
-function POINT_VEC3:Explosion( ExplosionIntensity )
- self:F2( { ExplosionIntensity } )
- trigger.action.explosion( self:GetVec3(), ExplosionIntensity )
-end
-
---- Creates an illumination bomb at the point.
--- @param #POINT_VEC3 self
-function POINT_VEC3:IlluminationBomb()
- self:F2()
- trigger.action.illuminationBomb( self:GetVec3() )
-end
-
-
---- Smokes the point in a color.
--- @param #POINT_VEC3 self
--- @param Utilities.Utils#SMOKECOLOR SmokeColor
-function POINT_VEC3:Smoke( SmokeColor )
- self:F2( { SmokeColor } )
- trigger.action.smoke( self:GetVec3(), SmokeColor )
-end
-
---- Smoke the POINT_VEC3 Green.
--- @param #POINT_VEC3 self
-function POINT_VEC3:SmokeGreen()
- self:F2()
- self:Smoke( SMOKECOLOR.Green )
-end
-
---- Smoke the POINT_VEC3 Red.
--- @param #POINT_VEC3 self
-function POINT_VEC3:SmokeRed()
- self:F2()
- self:Smoke( SMOKECOLOR.Red )
-end
-
---- Smoke the POINT_VEC3 White.
--- @param #POINT_VEC3 self
-function POINT_VEC3:SmokeWhite()
- self:F2()
- self:Smoke( SMOKECOLOR.White )
-end
-
---- Smoke the POINT_VEC3 Orange.
--- @param #POINT_VEC3 self
-function POINT_VEC3:SmokeOrange()
- self:F2()
- self:Smoke( SMOKECOLOR.Orange )
-end
-
---- Smoke the POINT_VEC3 Blue.
--- @param #POINT_VEC3 self
-function POINT_VEC3:SmokeBlue()
- self:F2()
- self:Smoke( SMOKECOLOR.Blue )
-end
-
---- Flares the point in a color.
--- @param #POINT_VEC3 self
--- @param Utilities.Utils#FLARECOLOR FlareColor
--- @param Dcs.DCSTypes#Azimuth (optional) Azimuth The azimuth of the flare direction. The default azimuth is 0.
-function POINT_VEC3:Flare( FlareColor, Azimuth )
- self:F2( { FlareColor } )
- trigger.action.signalFlare( self:GetVec3(), FlareColor, Azimuth and Azimuth or 0 )
-end
-
---- Flare the POINT_VEC3 White.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Azimuth (optional) Azimuth The azimuth of the flare direction. The default azimuth is 0.
-function POINT_VEC3:FlareWhite( Azimuth )
- self:F2( Azimuth )
- self:Flare( FLARECOLOR.White, Azimuth )
-end
-
---- Flare the POINT_VEC3 Yellow.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Azimuth (optional) Azimuth The azimuth of the flare direction. The default azimuth is 0.
-function POINT_VEC3:FlareYellow( Azimuth )
- self:F2( Azimuth )
- self:Flare( FLARECOLOR.Yellow, Azimuth )
-end
-
---- Flare the POINT_VEC3 Green.
--- @param #POINT_VEC3 self
--- @param Dcs.DCSTypes#Azimuth (optional) Azimuth The azimuth of the flare direction. The default azimuth is 0.
-function POINT_VEC3:FlareGreen( Azimuth )
- self:F2( Azimuth )
- self:Flare( FLARECOLOR.Green, Azimuth )
-end
-
---- Flare the POINT_VEC3 Red.
--- @param #POINT_VEC3 self
-function POINT_VEC3:FlareRed( Azimuth )
- self:F2( Azimuth )
- self:Flare( FLARECOLOR.Red, Azimuth )
-end
-
-end
-
-do -- POINT_VEC2
-
-
-
---- POINT_VEC2 constructor.
--- @param #POINT_VEC2 self
--- @param Dcs.DCSTypes#Distance x The x coordinate of the Vec3 point, pointing to the North.
--- @param Dcs.DCSTypes#Distance y The y coordinate of the Vec3 point, pointing to the Right.
--- @param Dcs.DCSTypes#Distance LandHeightAdd (optional) The default height if required to be evaluated will be the land height of the x, y coordinate. You can specify an extra height to be added to the land height.
--- @return Core.Point#POINT_VEC2
-function POINT_VEC2:New( x, y, LandHeightAdd )
-
- local LandHeight = land.getHeight( { ["x"] = x, ["y"] = y } )
-
- LandHeightAdd = LandHeightAdd or 0
- LandHeight = LandHeight + LandHeightAdd
-
- self = BASE:Inherit( self, POINT_VEC3:New( x, LandHeight, y ) )
- self:F2( self )
-
- return self
-end
-
---- Create a new POINT_VEC2 object from Vec2 coordinates.
--- @param #POINT_VEC2 self
--- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 point.
--- @return Core.Point#POINT_VEC2 self
-function POINT_VEC2:NewFromVec2( Vec2, LandHeightAdd )
-
- local LandHeight = land.getHeight( Vec2 )
-
- LandHeightAdd = LandHeightAdd or 0
- LandHeight = LandHeight + LandHeightAdd
-
- self = BASE:Inherit( self, POINT_VEC3:New( Vec2.x, LandHeight, Vec2.y ) )
- self:F2( self )
-
- return self
-end
-
---- Create a new POINT_VEC2 object from Vec3 coordinates.
--- @param #POINT_VEC2 self
--- @param Dcs.DCSTypes#Vec3 Vec3 The Vec3 point.
--- @return Core.Point#POINT_VEC2 self
-function POINT_VEC2:NewFromVec3( Vec3 )
-
- local self = BASE:Inherit( self, BASE:New() )
- local Vec2 = { x = Vec3.x, y = Vec3.z }
-
- local LandHeight = land.getHeight( Vec2 )
-
- self = BASE:Inherit( self, POINT_VEC3:New( Vec2.x, LandHeight, Vec2.y ) )
- self:F2( self )
-
- return self
-end
-
---- Return the x coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @return #number The x coodinate.
-function POINT_VEC2:GetX()
- return self.x
-end
-
---- Return the y coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @return #number The y coodinate.
-function POINT_VEC2:GetY()
- return self.z
-end
-
---- Return the altitude (height) of the land at the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @return #number The land altitude.
-function POINT_VEC2:GetAlt()
- return land.getHeight( { x = self.x, y = self.z } )
-end
-
---- Return Return the Lat(itude) coordinate of the POINT_VEC2 (ie: (parent)POINT_VEC3.x).
--- @param #POINT_VEC2 self
--- @return #number The x coodinate.
-function POINT_VEC2:GetLat()
- return self.x
-end
-
---- Return the Lon(gitude) coordinate of the POINT_VEC2 (ie: (parent)POINT_VEC3.z).
--- @param #POINT_VEC2 self
--- @return #number The y coodinate.
-function POINT_VEC2:GetLon()
- return self.z
-end
-
---- Set the x coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param #number x The x coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:SetX( x )
- self.x = x
- return self
-end
-
---- Set the y coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param #number y The y coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:SetY( y )
- self.z = y
- return self
-end
-
---- Set the Lat(itude) coordinate of the POINT_VEC2 (ie: POINT_VEC3.x).
--- @param #POINT_VEC2 self
--- @param #number x The x coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:SetLat( x )
- self.x = x
- return self
-end
-
---- Set the altitude of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param #number Altitude The land altitude. If nothing (nil) is given, then the current land altitude is set.
--- @return #POINT_VEC2
-function POINT_VEC2:SetAlt( Altitude )
- self.y = Altitude or land.getHeight( { x = self.x, y = self.z } )
- return self
-end
-
---- Set the Lon(gitude) coordinate of the POINT_VEC2 (ie: POINT_VEC3.z).
--- @param #POINT_VEC2 self
--- @param #number y The y coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:SetLon( z )
- self.z = z
- return self
-end
-
---- Add to the x coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param #number x The x coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:AddX( x )
- self.x = self.x + x
- return self
-end
-
---- Add to the y coordinate of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param #number y The y coordinate.
--- @return #POINT_VEC2
-function POINT_VEC2:AddY( y )
- self.z = self.z + y
- return self
-end
-
---- Add to the current land height an altitude.
--- @param #POINT_VEC2 self
--- @param #number Altitude The Altitude to add. If nothing (nil) is given, then the current land altitude is set.
--- @return #POINT_VEC2
-function POINT_VEC2:AddAlt( Altitude )
- self.y = land.getHeight( { x = self.x, y = self.z } ) + Altitude or 0
- return self
-end
-
-
-
---- Calculate the distance from a reference @{#POINT_VEC2}.
--- @param #POINT_VEC2 self
--- @param #POINT_VEC2 PointVec2Reference The reference @{#POINT_VEC2}.
--- @return Dcs.DCSTypes#Distance The distance from the reference @{#POINT_VEC2} in meters.
-function POINT_VEC2:DistanceFromPointVec2( PointVec2Reference )
- self:F2( PointVec2Reference )
-
- local Distance = ( ( PointVec2Reference:GetX() - self:GetX() ) ^ 2 + ( PointVec2Reference:GetY() - self:GetY() ) ^2 ) ^0.5
-
- self:T2( Distance )
- return Distance
-end
-
---- Calculate the distance from a reference @{DCSTypes#Vec2}.
--- @param #POINT_VEC2 self
--- @param Dcs.DCSTypes#Vec2 Vec2Reference The reference @{DCSTypes#Vec2}.
--- @return Dcs.DCSTypes#Distance The distance from the reference @{DCSTypes#Vec2} in meters.
-function POINT_VEC2:DistanceFromVec2( Vec2Reference )
- self:F2( Vec2Reference )
-
- local Distance = ( ( Vec2Reference.x - self:GetX() ) ^ 2 + ( Vec2Reference.y - self:GetY() ) ^2 ) ^0.5
-
- self:T2( Distance )
- return Distance
-end
-
-
---- Return no text for the altitude of the POINT_VEC2.
--- @param #POINT_VEC2 self
--- @return #string Empty string.
-function POINT_VEC2:GetAltitudeText()
- return ''
-end
-
---- Add a Distance in meters from the POINT_VEC2 orthonormal plane, with the given angle, and calculate the new POINT_VEC2.
--- @param #POINT_VEC2 self
--- @param Dcs.DCSTypes#Distance Distance The Distance to be added in meters.
--- @param Dcs.DCSTypes#Angle Angle The Angle in degrees.
--- @return #POINT_VEC2 The new calculated POINT_VEC2.
-function POINT_VEC2:Translate( Distance, Angle )
- local SX = self:GetX()
- local SY = self:GetY()
- local Radians = Angle / 180 * math.pi
- local TX = Distance * math.cos( Radians ) + SX
- local TY = Distance * math.sin( Radians ) + SY
-
- return POINT_VEC2:New( TX, TY )
-end
-
-end
-
-
---- **Core** - MESSAGE class takes are of the **real-time notifications** and **messages to players** during a simulation.
---
--- 
---
--- ===
---
--- # 1) @{Message#MESSAGE} class, extends @{Base#BASE}
---
--- Message System to display Messages to Clients, Coalitions or All.
--- Messages are shown on the display panel for an amount of seconds, and will then disappear.
--- Messages can contain a category which is indicating the category of the message.
---
--- ## 1.1) MESSAGE construction
---
--- Messages are created with @{Message#MESSAGE.New}. Note that when the MESSAGE object is created, no message is sent yet.
--- To send messages, you need to use the To functions.
---
--- ## 1.2) Send messages to an audience
---
--- Messages are sent:
---
--- * To a @{Client} using @{Message#MESSAGE.ToClient}().
--- * To a @{Group} using @{Message#MESSAGE.ToGroup}()
--- * To a coalition using @{Message#MESSAGE.ToCoalition}().
--- * To the red coalition using @{Message#MESSAGE.ToRed}().
--- * To the blue coalition using @{Message#MESSAGE.ToBlue}().
--- * To all Players using @{Message#MESSAGE.ToAll}().
---
--- ## 1.3) Send conditionally to an audience
---
--- Messages can be sent conditionally to an audience (when a condition is true):
---
--- * To all players using @{Message#MESSAGE.ToAllIf}().
--- * To a coalition using @{Message#MESSAGE.ToCoalitionIf}().
---
---
--- @module Message
-
---- The MESSAGE class
--- @type MESSAGE
--- @extends Core.Base#BASE
-MESSAGE = {
- ClassName = "MESSAGE",
- MessageCategory = 0,
- MessageID = 0,
-}
-
-
---- Creates a new MESSAGE object. Note that these MESSAGE objects are not yet displayed on the display panel. You must use the functions @{ToClient} or @{ToCoalition} or @{ToAll} to send these Messages to the respective recipients.
--- @param self
--- @param #string MessageText is the text of the Message.
--- @param #number MessageDuration is a number in seconds of how long the MESSAGE should be shown on the display panel.
--- @param #string MessageCategory (optional) is a string expressing the "category" of the Message. The category will be shown as the first text in the message followed by a ": ".
--- @return #MESSAGE
--- @usage
--- -- Create a series of new Messages.
--- -- MessageAll is meant to be sent to all players, for 25 seconds, and is classified as "Score".
--- -- MessageRED is meant to be sent to the RED players only, for 10 seconds, and is classified as "End of Mission", with ID "Win".
--- -- MessageClient1 is meant to be sent to a Client, for 25 seconds, and is classified as "Score", with ID "Score".
--- -- MessageClient1 is meant to be sent to a Client, for 25 seconds, and is classified as "Score", with ID "Score".
--- MessageAll = MESSAGE:New( "To all Players: BLUE has won! Each player of BLUE wins 50 points!", 25, "End of Mission" )
--- MessageRED = MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", 25, "Penalty" )
--- MessageClient1 = MESSAGE:New( "Congratulations, you've just hit a target", 25, "Score" )
--- MessageClient2 = MESSAGE:New( "Congratulations, you've just killed a target", 25, "Score")
-function MESSAGE:New( MessageText, MessageDuration, MessageCategory )
- local self = BASE:Inherit( self, BASE:New() )
- self:F( { MessageText, MessageDuration, MessageCategory } )
-
- -- When no MessageCategory is given, we don't show it as a title...
- if MessageCategory and MessageCategory ~= "" then
- if MessageCategory:sub(-1) ~= "\n" then
- self.MessageCategory = MessageCategory .. ": "
- else
- self.MessageCategory = MessageCategory:sub( 1, -2 ) .. ":\n"
- end
- else
- self.MessageCategory = ""
- end
-
- self.MessageDuration = MessageDuration or 5
- self.MessageTime = timer.getTime()
- self.MessageText = MessageText
-
- self.MessageSent = false
- self.MessageGroup = false
- self.MessageCoalition = false
-
- return self
-end
-
---- Sends a MESSAGE to a Client Group. Note that the Group needs to be defined within the ME with the skillset "Client" or "Player".
--- @param #MESSAGE self
--- @param Wrapper.Client#CLIENT Client is the Group of the Client.
--- @return #MESSAGE
--- @usage
--- -- Send the 2 messages created with the @{New} method to the Client Group.
--- -- Note that the Message of MessageClient2 is overwriting the Message of MessageClient1.
--- ClientGroup = Group.getByName( "ClientGroup" )
---
--- MessageClient1 = MESSAGE:New( "Congratulations, you've just hit a target", "Score", 25, "Score" ):ToClient( ClientGroup )
--- MessageClient2 = MESSAGE:New( "Congratulations, you've just killed a target", "Score", 25, "Score" ):ToClient( ClientGroup )
--- or
--- MESSAGE:New( "Congratulations, you've just hit a target", "Score", 25, "Score" ):ToClient( ClientGroup )
--- MESSAGE:New( "Congratulations, you've just killed a target", "Score", 25, "Score" ):ToClient( ClientGroup )
--- or
--- MessageClient1 = MESSAGE:New( "Congratulations, you've just hit a target", "Score", 25, "Score" )
--- MessageClient2 = MESSAGE:New( "Congratulations, you've just killed a target", "Score", 25, "Score" )
--- MessageClient1:ToClient( ClientGroup )
--- MessageClient2:ToClient( ClientGroup )
-function MESSAGE:ToClient( Client )
- self:F( Client )
-
- if Client and Client:GetClientGroupID() then
-
- local ClientGroupID = Client:GetClientGroupID()
- self:T( self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$","") .. " / " .. self.MessageDuration )
- trigger.action.outTextForGroup( ClientGroupID, self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$",""), self.MessageDuration )
- end
-
- return self
-end
-
---- Sends a MESSAGE to a Group.
--- @param #MESSAGE self
--- @param Wrapper.Group#GROUP Group is the Group.
--- @return #MESSAGE
-function MESSAGE:ToGroup( Group )
- self:F( Group.GroupName )
-
- if Group then
-
- self:T( self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$","") .. " / " .. self.MessageDuration )
- trigger.action.outTextForGroup( Group:GetID(), self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$",""), self.MessageDuration )
- end
-
- return self
-end
---- Sends a MESSAGE to the Blue coalition.
--- @param #MESSAGE self
--- @return #MESSAGE
--- @usage
--- -- Send a message created with the @{New} method to the BLUE coalition.
--- MessageBLUE = MESSAGE:New( "To the BLUE Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToBlue()
--- or
--- MESSAGE:New( "To the BLUE Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToBlue()
--- or
--- MessageBLUE = MESSAGE:New( "To the BLUE Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" )
--- MessageBLUE:ToBlue()
-function MESSAGE:ToBlue()
- self:F()
-
- self:ToCoalition( coalition.side.BLUE )
-
- return self
-end
-
---- Sends a MESSAGE to the Red Coalition.
--- @param #MESSAGE self
--- @return #MESSAGE
--- @usage
--- -- Send a message created with the @{New} method to the RED coalition.
--- MessageRED = MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToRed()
--- or
--- MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToRed()
--- or
--- MessageRED = MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" )
--- MessageRED:ToRed()
-function MESSAGE:ToRed( )
- self:F()
-
- self:ToCoalition( coalition.side.RED )
-
- return self
-end
-
---- Sends a MESSAGE to a Coalition.
--- @param #MESSAGE self
--- @param CoalitionSide needs to be filled out by the defined structure of the standard scripting engine @{coalition.side}.
--- @return #MESSAGE
--- @usage
--- -- Send a message created with the @{New} method to the RED coalition.
--- MessageRED = MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToCoalition( coalition.side.RED )
--- or
--- MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" ):ToCoalition( coalition.side.RED )
--- or
--- MessageRED = MESSAGE:New( "To the RED Players: You receive a penalty because you've killed one of your own units", "Penalty", 25, "Score" )
--- MessageRED:ToCoalition( coalition.side.RED )
-function MESSAGE:ToCoalition( CoalitionSide )
- self:F( CoalitionSide )
-
- if CoalitionSide then
- self:T( self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$","") .. " / " .. self.MessageDuration )
- trigger.action.outTextForCoalition( CoalitionSide, self.MessageCategory .. self.MessageText:gsub("\n$",""):gsub("\n$",""), self.MessageDuration )
- end
-
- return self
-end
-
---- Sends a MESSAGE to a Coalition if the given Condition is true.
--- @param #MESSAGE self
--- @param CoalitionSide needs to be filled out by the defined structure of the standard scripting engine @{coalition.side}.
--- @return #MESSAGE
-function MESSAGE:ToCoalitionIf( CoalitionSide, Condition )
- self:F( CoalitionSide )
-
- if Condition and Condition == true then
- self:ToCoalition( CoalitionSide )
- end
-
- return self
-end
-
---- Sends a MESSAGE to all players.
--- @param #MESSAGE self
--- @return #MESSAGE
--- @usage
--- -- Send a message created to all players.
--- MessageAll = MESSAGE:New( "To all Players: BLUE has won! Each player of BLUE wins 50 points!", "End of Mission", 25, "Win" ):ToAll()
--- or
--- MESSAGE:New( "To all Players: BLUE has won! Each player of BLUE wins 50 points!", "End of Mission", 25, "Win" ):ToAll()
--- or
--- MessageAll = MESSAGE:New( "To all Players: BLUE has won! Each player of BLUE wins 50 points!", "End of Mission", 25, "Win" )
--- MessageAll:ToAll()
-function MESSAGE:ToAll()
- self:F()
-
- self:ToCoalition( coalition.side.RED )
- self:ToCoalition( coalition.side.BLUE )
-
- return self
-end
-
-
---- Sends a MESSAGE to all players if the given Condition is true.
--- @param #MESSAGE self
--- @return #MESSAGE
-function MESSAGE:ToAllIf( Condition )
-
- if Condition and Condition == true then
- self:ToCoalition( coalition.side.RED )
- self:ToCoalition( coalition.side.BLUE )
- end
-
- return self
-end
---- **Core** - The **FSM** (**F**inite **S**tate **M**achine) class and derived **FSM\_** classes
--- are design patterns allowing efficient (long-lasting) processes and workflows.
---
--- 
---
--- ===
---
--- A FSM can only be in one of a finite number of states.
--- The machine is in only one state at a time; the state it is in at any given time is called the **current state**.
--- It can change from one state to another when initiated by an **__internal__ or __external__ triggering event**, which is called a **transition**.
--- An **FSM implementation** is defined by **a list of its states**, **its initial state**, and **the triggering events** for **each possible transition**.
--- An FSM implementation is composed out of **two parts**, a set of **state transition rules**, and an implementation set of **state transition handlers**, implementing those transitions.
---
--- The FSM class supports a **hierarchical implementation of a Finite State Machine**,
--- that is, it allows to **embed existing FSM implementations in a master FSM**.
--- FSM hierarchies allow for efficient FSM re-use, **not having to re-invent the wheel every time again** when designing complex processes.
---
--- 
---
--- The above diagram shows a graphical representation of a FSM implementation for a **Task**, which guides a Human towards a Zone,
--- orders him to destroy x targets and account the results.
--- Other examples of ready made FSM could be:
---
--- * route a plane to a zone flown by a human
--- * detect targets by an AI and report to humans
--- * account for destroyed targets by human players
--- * handle AI infantry to deploy from or embark to a helicopter or airplane or vehicle
--- * let an AI patrol a zone
---
--- The **MOOSE framework** uses extensively the FSM class and derived FSM\_ classes,
--- because **the goal of MOOSE is to simplify mission design complexity for mission building**.
--- By efficiently utilizing the FSM class and derived classes, MOOSE allows mission designers to quickly build processes.
--- **Ready made FSM-based implementations classes** exist within the MOOSE framework that **can easily be re-used,
--- and tailored** by mission designers through **the implementation of Transition Handlers**.
--- Each of these FSM implementation classes start either with:
---
--- * an acronym **AI\_**, which indicates an FSM implementation directing **AI controlled** @{GROUP} and/or @{UNIT}. These AI\_ classes derive the @{#FSM_CONTROLLABLE} class.
--- * an acronym **TASK\_**, which indicates an FSM implementation executing a @{TASK} executed by Groups of players. These TASK\_ classes derive the @{#FSM_TASK} class.
--- * an acronym **ACT\_**, which indicates an Sub-FSM implementation, directing **Humans actions** that need to be done in a @{TASK}, seated in a @{CLIENT} (slot) or a @{UNIT} (CA join). These ACT\_ classes derive the @{#FSM_PROCESS} class.
---
--- Detailed explanations and API specifics are further below clarified and FSM derived class specifics are described in those class documentation sections.
---
--- ##__Dislaimer:__
--- The FSM class development is based on a finite state machine implementation made by Conroy Kyle.
--- The state machine can be found on [github](https://github.com/kyleconroy/lua-state-machine)
--- I've reworked this development (taken the concept), and created a **hierarchical state machine** out of it, embedded within the DCS simulator.
--- Additionally, I've added extendability and created an API that allows seamless FSM implementation.
---
--- The following derived classes are available in the MOOSE framework, that implement a specialised form of a FSM:
---
--- * @{#FSM_TASK}: Models Finite State Machines for @{Task}s.
--- * @{#FSM_PROCESS}: Models Finite State Machines for @{Task} actions, which control @{Client}s.
--- * @{#FSM_CONTROLLABLE}: Models Finite State Machines for @{Controllable}s, which are @{Group}s, @{Unit}s, @{Client}s.
--- * @{#FSM_SET}: Models Finite State Machines for @{Set}s. Note that these FSMs control multiple objects!!! So State concerns here
--- for multiple objects or the position of the state machine in the process.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- YYYY-MM-DD: CLASS:**NewFunction**( Params ) replaces CLASS:_OldFunction_( Params )
--- YYYY-MM-DD: CLASS:**NewFunction( Params )** added
---
--- Hereby the change log:
---
--- * 2016-12-18: Released.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * [**Pikey**](https://forums.eagle.ru/member.php?u=62835): Review of documentation & advice for improvements.
---
--- ### Authors:
---
--- * [**FlightControl**](https://forums.eagle.ru/member.php?u=89536): Design & Programming & documentation.
---
--- @module Fsm
-
-do -- FSM
-
- --- @type FSM
- -- @extends Core.Base#BASE
-
-
- --- # 1) FSM class, extends @{Base#BASE}
- --
- -- 
- --
- -- The FSM class is the base class of all FSM\_ derived classes. It implements the main functionality to define and execute Finite State Machines.
- -- The derived FSM\_ classes extend the Finite State Machine functionality to run a workflow process for a specific purpose or component.
- --
- -- Finite State Machines have **Transition Rules**, **Transition Handlers** and **Event Triggers**.
- --
- -- The **Transition Rules** define the "Process Flow Boundaries", that is,
- -- the path that can be followed hopping from state to state upon triggered events.
- -- If an event is triggered, and there is no valid path found for that event,
- -- an error will be raised and the FSM will stop functioning.
- --
- -- The **Transition Handlers** are special methods that can be defined by the mission designer, following a defined syntax.
- -- If the FSM object finds a method of such a handler, then the method will be called by the FSM, passing specific parameters.
- -- The method can then define its own custom logic to implement the FSM workflow, and to conduct other actions.
- --
- -- The **Event Triggers** are methods that are defined by the FSM, which the mission designer can use to implement the workflow.
- -- Most of the time, these Event Triggers are used within the Transition Handler methods, so that a workflow is created running through the state machine.
- --
- -- As explained above, a FSM supports **Linear State Transitions** and **Hierarchical State Transitions**, and both can be mixed to make a comprehensive FSM implementation.
- -- The below documentation has a seperate chapter explaining both transition modes, taking into account the **Transition Rules**, **Transition Handlers** and **Event Triggers**.
- --
- -- ## 1.1) FSM Linear Transitions
- --
- -- Linear Transitions are Transition Rules allowing an FSM to transition from one or multiple possible **From** state(s) towards a **To** state upon a Triggered **Event**.
- -- The Lineair transition rule evaluation will always be done from the **current state** of the FSM.
- -- If no valid Transition Rule can be found in the FSM, the FSM will log an error and stop.
- --
- -- ### 1.1.1) FSM Transition Rules
- --
- -- The FSM has transition rules that it follows and validates, as it walks the process.
- -- These rules define when an FSM can transition from a specific state towards an other specific state upon a triggered event.
- --
- -- The method @{#FSM.AddTransition}() specifies a new possible Transition Rule for the FSM.
- --
- -- The initial state can be defined using the method @{#FSM.SetStartState}(). The default start state of an FSM is "None".
- --
- -- Find below an example of a Linear Transition Rule definition for an FSM.
- --
- -- local Fsm3Switch = FSM:New() -- #FsmDemo
- -- FsmSwitch:SetStartState( "Off" )
- -- FsmSwitch:AddTransition( "Off", "SwitchOn", "On" )
- -- FsmSwitch:AddTransition( "Off", "SwitchMiddle", "Middle" )
- -- FsmSwitch:AddTransition( "On", "SwitchOff", "Off" )
- -- FsmSwitch:AddTransition( "Middle", "SwitchOff", "Off" )
- --
- -- The above code snippet models a 3-way switch Linear Transition:
- --
- -- * It can be switched **On** by triggering event **SwitchOn**.
- -- * It can be switched to the **Middle** position, by triggering event **SwitchMiddle**.
- -- * It can be switched **Off** by triggering event **SwitchOff**.
- -- * Note that once the Switch is **On** or **Middle**, it can only be switched **Off**.
- --
- -- ### Some additional comments:
- --
- -- Note that Linear Transition Rules **can be declared in a few variations**:
- --
- -- * The From states can be **a table of strings**, indicating that the transition rule will be valid **if the current state** of the FSM will be **one of the given From states**.
- -- * The From state can be a **"*"**, indicating that **the transition rule will always be valid**, regardless of the current state of the FSM.
- --
- -- The below code snippet shows how the two last lines can be rewritten and consensed.
- --
- -- FsmSwitch:AddTransition( { "On", "Middle" }, "SwitchOff", "Off" )
- --
- -- ### 1.1.2) Transition Handling
- --
- -- 
- --
- -- An FSM transitions in **4 moments** when an Event is being triggered and processed.
- -- The mission designer can define for each moment specific logic within methods implementations following a defined API syntax.
- -- These methods define the flow of the FSM process; because in those methods the FSM Internal Events will be triggered.
- --
- -- * To handle **State** transition moments, create methods starting with OnLeave or OnEnter concatenated with the State name.
- -- * To handle **Event** transition moments, create methods starting with OnBefore or OnAfter concatenated with the Event name.
- --
- -- **The OnLeave and OnBefore transition methods may return false, which will cancel the transition!**
- --
- -- Transition Handler methods need to follow the above specified naming convention, but are also passed parameters from the FSM.
- -- These parameters are on the correct order: From, Event, To:
- --
- -- * From = A string containing the From state.
- -- * Event = A string containing the Event name that was triggered.
- -- * To = A string containing the To state.
- --
- -- On top, each of these methods can have a variable amount of parameters passed. See the example in section [1.1.3](#1.1.3\)-event-triggers).
- --
- -- ### 1.1.3) Event Triggers
- --
- -- 
- --
- -- The FSM creates for each Event two **Event Trigger methods**.
- -- There are two modes how Events can be triggered, which is **synchronous** and **asynchronous**:
- --
- -- * The method **FSM:Event()** triggers an Event that will be processed **synchronously** or **immediately**.
- -- * The method **FSM:__Event( __seconds__ )** triggers an Event that will be processed **asynchronously** over time, waiting __x seconds__.
- --
- -- The destinction between these 2 Event Trigger methods are important to understand. An asynchronous call will "log" the Event Trigger to be executed at a later time.
- -- Processing will just continue. Synchronous Event Trigger methods are useful to change states of the FSM immediately, but may have a larger processing impact.
- --
- -- The following example provides a little demonstration on the difference between synchronous and asynchronous Event Triggering.
- --
- -- function FSM:OnAfterEvent( From, Event, To, Amount )
- -- self:T( { Amount = Amount } )
- -- end
- --
- -- local Amount = 1
- -- FSM:__Event( 5, Amount )
- --
- -- Amount = Amount + 1
- -- FSM:Event( Text, Amount )
- --
- -- In this example, the **:OnAfterEvent**() Transition Handler implementation will get called when **Event** is being triggered.
- -- Before we go into more detail, let's look at the last 4 lines of the example.
- -- The last line triggers synchronously the **Event**, and passes Amount as a parameter.
- -- The 3rd last line of the example triggers asynchronously **Event**.
- -- Event will be processed after 5 seconds, and Amount is given as a parameter.
- --
- -- The output of this little code fragment will be:
- --
- -- * Amount = 2
- -- * Amount = 2
- --
- -- Because ... When Event was asynchronously processed after 5 seconds, Amount was set to 2. So be careful when processing and passing values and objects in asynchronous processing!
- --
- -- ### 1.1.4) Linear Transition Example
- --
- -- This example is fully implemented in the MOOSE test mission on GITHUB: [FSM-100 - Transition Explanation](https://github.com/FlightControl-Master/MOOSE/blob/master/Moose%20Test%20Missions/FSM%20-%20Finite%20State%20Machine/FSM-100%20-%20Transition%20Explanation/FSM-100%20-%20Transition%20Explanation.lua)
- --
- -- It models a unit standing still near Batumi, and flaring every 5 seconds while switching between a Green flare and a Red flare.
- -- The purpose of this example is not to show how exciting flaring is, but it demonstrates how a Linear Transition FSM can be build.
- -- Have a look at the source code. The source code is also further explained below in this section.
- --
- -- The example creates a new FsmDemo object from class FSM.
- -- It will set the start state of FsmDemo to state **Green**.
- -- Two Linear Transition Rules are created, where upon the event **Switch**,
- -- the FsmDemo will transition from state **Green** to **Red** and from **Red** back to **Green**.
- --
- -- 
- --
- -- local FsmDemo = FSM:New() -- #FsmDemo
- -- FsmDemo:SetStartState( "Green" )
- -- FsmDemo:AddTransition( "Green", "Switch", "Red" )
- -- FsmDemo:AddTransition( "Red", "Switch", "Green" )
- --
- -- In the above example, the FsmDemo could flare every 5 seconds a Green or a Red flare into the air.
- -- The next code implements this through the event handling method **OnAfterSwitch**.
- --
- -- 
- --
- -- function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
- -- self:T( { From, Event, To, FsmUnit } )
- --
- -- if From == "Green" then
- -- FsmUnit:Flare(FLARECOLOR.Green)
- -- else
- -- if From == "Red" then
- -- FsmUnit:Flare(FLARECOLOR.Red)
- -- end
- -- end
- -- self:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
- -- end
- --
- -- FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the first Switch event to happen in 5 seconds.
- --
- -- The OnAfterSwitch implements a loop. The last line of the code fragment triggers the Switch Event within 5 seconds.
- -- Upon the event execution (after 5 seconds), the OnAfterSwitch method is called of FsmDemo (cfr. the double point notation!!! ":").
- -- The OnAfterSwitch method receives from the FSM the 3 transition parameter details ( From, Event, To ),
- -- and one additional parameter that was given when the event was triggered, which is in this case the Unit that is used within OnSwitchAfter.
- --
- -- function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
- --
- -- For debugging reasons the received parameters are traced within the DCS.log.
- --
- -- self:T( { From, Event, To, FsmUnit } )
- --
- -- The method will check if the From state received is either "Green" or "Red" and will flare the respective color from the FsmUnit.
- --
- -- if From == "Green" then
- -- FsmUnit:Flare(FLARECOLOR.Green)
- -- else
- -- if From == "Red" then
- -- FsmUnit:Flare(FLARECOLOR.Red)
- -- end
- -- end
- --
- -- It is important that the Switch event is again triggered, otherwise, the FsmDemo would stop working after having the first Event being handled.
- --
- -- FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
- --
- -- The below code fragment extends the FsmDemo, demonstrating multiple **From states declared as a table**, adding a **Linear Transition Rule**.
- -- The new event **Stop** will cancel the Switching process.
- -- The transition for event Stop can be executed if the current state of the FSM is either "Red" or "Green".
- --
- -- local FsmDemo = FSM:New() -- #FsmDemo
- -- FsmDemo:SetStartState( "Green" )
- -- FsmDemo:AddTransition( "Green", "Switch", "Red" )
- -- FsmDemo:AddTransition( "Red", "Switch", "Green" )
- -- FsmDemo:AddTransition( { "Red", "Green" }, "Stop", "Stopped" )
- --
- -- The transition for event Stop can also be simplified, as any current state of the FSM is valid.
- --
- -- FsmDemo:AddTransition( "*", "Stop", "Stopped" )
- --
- -- So... When FsmDemo:Stop() is being triggered, the state of FsmDemo will transition from Red or Green to Stopped.
- -- And there is no transition handling method defined for that transition, thus, no new event is being triggered causing the FsmDemo process flow to halt.
- --
- -- ## 1.5) FSM Hierarchical Transitions
- --
- -- Hierarchical Transitions allow to re-use readily available and implemented FSMs.
- -- This becomes in very useful for mission building, where mission designers build complex processes and workflows,
- -- combining smaller FSMs to one single FSM.
- --
- -- The FSM can embed **Sub-FSMs** that will execute and return **multiple possible Return (End) States**.
- -- Depending upon **which state is returned**, the main FSM can continue the flow **triggering specific events**.
- --
- -- The method @{#FSM.AddProcess}() adds a new Sub-FSM to the FSM.
- --
- -- ===
- --
- -- @field #FSM FSM
- --
- FSM = {
- ClassName = "FSM",
- }
-
- --- Creates a new FSM object.
- -- @param #FSM self
- -- @return #FSM
- function FSM:New( FsmT )
-
- -- Inherits from BASE
- self = BASE:Inherit( self, BASE:New() )
-
- self.options = options or {}
- self.options.subs = self.options.subs or {}
- self.current = self.options.initial or 'none'
- self.Events = {}
- self.subs = {}
- self.endstates = {}
-
- self.Scores = {}
-
- self._StartState = "none"
- self._Transitions = {}
- self._Processes = {}
- self._EndStates = {}
- self._Scores = {}
- self._EventSchedules = {}
-
- self.CallScheduler = SCHEDULER:New( self )
-
-
- return self
- end
-
-
- --- Sets the start state of the FSM.
- -- @param #FSM self
- -- @param #string State A string defining the start state.
- function FSM:SetStartState( State )
-
- self._StartState = State
- self.current = State
- end
-
-
- --- Returns the start state of the FSM.
- -- @param #FSM self
- -- @return #string A string containing the start state.
- function FSM:GetStartState()
-
- return self._StartState or {}
- end
-
- --- Add a new transition rule to the FSM.
- -- A transition rule defines when and if the FSM can transition from a state towards another state upon a triggered event.
- -- @param #FSM self
- -- @param #table From Can contain a string indicating the From state or a table of strings containing multiple From states.
- -- @param #string Event The Event name.
- -- @param #string To The To state.
- function FSM:AddTransition( From, Event, To )
-
- local Transition = {}
- Transition.From = From
- Transition.Event = Event
- Transition.To = To
-
- self:T( Transition )
-
- self._Transitions[Transition] = Transition
- self:_eventmap( self.Events, Transition )
- end
-
-
- --- Returns a table of the transition rules defined within the FSM.
- -- @return #table
- function FSM:GetTransitions()
-
- return self._Transitions or {}
- end
-
- --- Set the default @{Process} template with key ProcessName providing the ProcessClass and the process object when it is assigned to a @{Controllable} by the task.
- -- @param #FSM self
- -- @param #table From Can contain a string indicating the From state or a table of strings containing multiple From states.
- -- @param #string Event The Event name.
- -- @param Core.Fsm#FSM_PROCESS Process An sub-process FSM.
- -- @param #table ReturnEvents A table indicating for which returned events of the SubFSM which Event must be triggered in the FSM.
- -- @return Core.Fsm#FSM_PROCESS The SubFSM.
- function FSM:AddProcess( From, Event, Process, ReturnEvents )
- self:T( { From, Event, Process, ReturnEvents } )
-
- local Sub = {}
- Sub.From = From
- Sub.Event = Event
- Sub.fsm = Process
- Sub.StartEvent = "Start"
- Sub.ReturnEvents = ReturnEvents
-
- self._Processes[Sub] = Sub
-
- self:_submap( self.subs, Sub, nil )
-
- self:AddTransition( From, Event, From )
-
- return Process
- end
-
-
- --- Returns a table of the SubFSM rules defined within the FSM.
- -- @return #table
- function FSM:GetProcesses()
-
- return self._Processes or {}
- end
-
- function FSM:GetProcess( From, Event )
-
- for ProcessID, Process in pairs( self:GetProcesses() ) do
- if Process.From == From and Process.Event == Event then
- return Process.fsm
- end
- end
-
- error( "Sub-Process from state " .. From .. " with event " .. Event .. " not found!" )
- end
-
- --- Adds an End state.
- function FSM:AddEndState( State )
-
- self._EndStates[State] = State
- self.endstates[State] = State
- end
-
- --- Returns the End states.
- function FSM:GetEndStates()
-
- return self._EndStates or {}
- end
-
-
- --- Adds a score for the FSM to be achieved.
- -- @param #FSM self
- -- @param #string State is the state of the process when the score needs to be given. (See the relevant state descriptions of the process).
- -- @param #string ScoreText is a text describing the score that is given according the status.
- -- @param #number Score is a number providing the score of the status.
- -- @return #FSM self
- function FSM:AddScore( State, ScoreText, Score )
- self:F( { State, ScoreText, Score } )
-
- self._Scores[State] = self._Scores[State] or {}
- self._Scores[State].ScoreText = ScoreText
- self._Scores[State].Score = Score
-
- return self
- end
-
- --- Adds a score for the FSM_PROCESS to be achieved.
- -- @param #FSM self
- -- @param #string From is the From State of the main process.
- -- @param #string Event is the Event of the main process.
- -- @param #string State is the state of the process when the score needs to be given. (See the relevant state descriptions of the process).
- -- @param #string ScoreText is a text describing the score that is given according the status.
- -- @param #number Score is a number providing the score of the status.
- -- @return #FSM self
- function FSM:AddScoreProcess( From, Event, State, ScoreText, Score )
- self:F( { From, Event, State, ScoreText, Score } )
-
- local Process = self:GetProcess( From, Event )
-
- Process._Scores[State] = Process._Scores[State] or {}
- Process._Scores[State].ScoreText = ScoreText
- Process._Scores[State].Score = Score
-
- self:T( Process._Scores )
-
- return Process
- end
-
- --- Returns a table with the scores defined.
- function FSM:GetScores()
-
- return self._Scores or {}
- end
-
- --- Returns a table with the Subs defined.
- function FSM:GetSubs()
-
- return self.options.subs
- end
-
-
- function FSM:LoadCallBacks( CallBackTable )
-
- for name, callback in pairs( CallBackTable or {} ) do
- self[name] = callback
- end
-
- end
-
- function FSM:_eventmap( Events, EventStructure )
-
- local Event = EventStructure.Event
- local __Event = "__" .. EventStructure.Event
- self[Event] = self[Event] or self:_create_transition(Event)
- self[__Event] = self[__Event] or self:_delayed_transition(Event)
- self:T( "Added methods: " .. Event .. ", " .. __Event )
- Events[Event] = self.Events[Event] or { map = {} }
- self:_add_to_map( Events[Event].map, EventStructure )
-
- end
-
- function FSM:_submap( subs, sub, name )
- self:F( { sub = sub, name = name } )
- subs[sub.From] = subs[sub.From] or {}
- subs[sub.From][sub.Event] = subs[sub.From][sub.Event] or {}
-
- -- Make the reference table weak.
- -- setmetatable( subs[sub.From][sub.Event], { __mode = "k" } )
-
- subs[sub.From][sub.Event][sub] = {}
- subs[sub.From][sub.Event][sub].fsm = sub.fsm
- subs[sub.From][sub.Event][sub].StartEvent = sub.StartEvent
- subs[sub.From][sub.Event][sub].ReturnEvents = sub.ReturnEvents or {} -- these events need to be given to find the correct continue event ... if none given, the processing will stop.
- subs[sub.From][sub.Event][sub].name = name
- subs[sub.From][sub.Event][sub].fsmparent = self
- end
-
-
- function FSM:_call_handler( handler, params, EventName )
-
- local ErrorHandler = function( errmsg )
-
- env.info( "Error in SCHEDULER function:" .. errmsg )
- if debug ~= nil then
- env.info( debug.traceback() )
- end
-
- return errmsg
- end
- if self[handler] then
- self:T( "Calling " .. handler )
- self._EventSchedules[EventName] = nil
- local Result, Value = xpcall( function() return self[handler]( self, unpack( params ) ) end, ErrorHandler )
- return Value
- end
- end
-
- function FSM._handler( self, EventName, ... )
-
- local Can, to = self:can( EventName )
-
- if to == "*" then
- to = self.current
- end
-
- if Can then
- local from = self.current
- local params = { from, EventName, to, ... }
-
- if self.Controllable then
- self:T( "FSM Transition for " .. self.Controllable.ControllableName .. " :" .. self.current .. " --> " .. EventName .. " --> " .. to )
- else
- self:T( "FSM Transition:" .. self.current .. " --> " .. EventName .. " --> " .. to )
- end
-
- if ( self:_call_handler("onbefore" .. EventName, params, EventName ) == false )
- or ( self:_call_handler("OnBefore" .. EventName, params, EventName ) == false )
- or ( self:_call_handler("onleave" .. from, params, EventName ) == false )
- or ( self:_call_handler("OnLeave" .. from, params, EventName ) == false ) then
- self:T( "Cancel Transition" )
- return false
- end
-
- self.current = to
-
- local execute = true
-
- local subtable = self:_gosub( from, EventName )
- for _, sub in pairs( subtable ) do
- --if sub.nextevent then
- -- self:F2( "nextevent = " .. sub.nextevent )
- -- self[sub.nextevent]( self )
- --end
- self:T( "calling sub start event: " .. sub.StartEvent )
- sub.fsm.fsmparent = self
- sub.fsm.ReturnEvents = sub.ReturnEvents
- sub.fsm[sub.StartEvent]( sub.fsm )
- execute = false
- end
-
- local fsmparent, Event = self:_isendstate( to )
- if fsmparent and Event then
- self:F2( { "end state: ", fsmparent, Event } )
- self:_call_handler("onenter" .. to, params, EventName )
- self:_call_handler("OnEnter" .. to, params, EventName )
- self:_call_handler("onafter" .. EventName, params, EventName )
- self:_call_handler("OnAfter" .. EventName, params, EventName )
- self:_call_handler("onstatechange", params, EventName )
- fsmparent[Event]( fsmparent )
- execute = false
- end
-
- if execute then
- -- only execute the call if the From state is not equal to the To state! Otherwise this function should never execute!
- --if from ~= to then
- self:_call_handler("onenter" .. to, params, EventName )
- self:_call_handler("OnEnter" .. to, params, EventName )
- --end
-
- self:_call_handler("onafter" .. EventName, params, EventName )
- self:_call_handler("OnAfter" .. EventName, params, EventName )
-
- self:_call_handler("onstatechange", params, EventName )
- end
- else
- self:T( "Cannot execute transition." )
- self:T( { From = self.current, Event = EventName, To = to, Can = Can } )
- end
-
- return nil
- end
-
- function FSM:_delayed_transition( EventName )
- return function( self, DelaySeconds, ... )
- self:T2( "Delayed Event: " .. EventName )
- local CallID = 0
- if DelaySeconds ~= nil then
- if DelaySeconds < 0 then -- Only call the event ONCE!
- DelaySeconds = math.abs( DelaySeconds )
- if not self._EventSchedules[EventName] then
- CallID = self.CallScheduler:Schedule( self, self._handler, { EventName, ... }, DelaySeconds or 1 )
- self._EventSchedules[EventName] = CallID
- else
- -- reschedule
- end
- else
- CallID = self.CallScheduler:Schedule( self, self._handler, { EventName, ... }, DelaySeconds or 1 )
- end
- else
- error( "FSM: An asynchronous event trigger requires a DelaySeconds parameter!!! This can be positive or negative! Sorry, but will not process this." )
- end
- self:T2( { CallID = CallID } )
- end
- end
-
- function FSM:_create_transition( EventName )
- return function( self, ... ) return self._handler( self, EventName , ... ) end
- end
-
- function FSM:_gosub( ParentFrom, ParentEvent )
- local fsmtable = {}
- if self.subs[ParentFrom] and self.subs[ParentFrom][ParentEvent] then
- self:T( { ParentFrom, ParentEvent, self.subs[ParentFrom], self.subs[ParentFrom][ParentEvent] } )
- return self.subs[ParentFrom][ParentEvent]
- else
- return {}
- end
- end
-
- function FSM:_isendstate( Current )
- local FSMParent = self.fsmparent
- if FSMParent and self.endstates[Current] then
- self:T( { state = Current, endstates = self.endstates, endstate = self.endstates[Current] } )
- FSMParent.current = Current
- local ParentFrom = FSMParent.current
- self:T( ParentFrom )
- self:T( self.ReturnEvents )
- local Event = self.ReturnEvents[Current]
- self:T( { ParentFrom, Event, self.ReturnEvents } )
- if Event then
- return FSMParent, Event
- else
- self:T( { "Could not find parent event name for state ", ParentFrom } )
- end
- end
-
- return nil
- end
-
- function FSM:_add_to_map( Map, Event )
- self:F3( { Map, Event } )
- if type(Event.From) == 'string' then
- Map[Event.From] = Event.To
- else
- for _, From in ipairs(Event.From) do
- Map[From] = Event.To
- end
- end
- self:T3( { Map, Event } )
- end
-
- function FSM:GetState()
- return self.current
- end
-
-
- function FSM:Is( State )
- return self.current == State
- end
-
- function FSM:is(state)
- return self.current == state
- end
-
- function FSM:can(e)
- local Event = self.Events[e]
- self:F3( { self.current, Event } )
- local To = Event and Event.map[self.current] or Event.map['*']
- return To ~= nil, To
- end
-
- function FSM:cannot(e)
- return not self:can(e)
- end
-
-end
-
-do -- FSM_CONTROLLABLE
-
- --- @type FSM_CONTROLLABLE
- -- @field Wrapper.Controllable#CONTROLLABLE Controllable
- -- @extends Core.Fsm#FSM
-
- --- # FSM_CONTROLLABLE, extends @{#FSM}
- --
- -- FSM_CONTROLLABLE class models Finite State Machines for @{Controllable}s, which are @{Group}s, @{Unit}s, @{Client}s.
- --
- -- ===
- --
- -- @field #FSM_CONTROLLABLE FSM_CONTROLLABLE
- --
- FSM_CONTROLLABLE = {
- ClassName = "FSM_CONTROLLABLE",
- }
-
- --- Creates a new FSM_CONTROLLABLE object.
- -- @param #FSM_CONTROLLABLE self
- -- @param #table FSMT Finite State Machine Table
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable (optional) The CONTROLLABLE object that the FSM_CONTROLLABLE governs.
- -- @return #FSM_CONTROLLABLE
- function FSM_CONTROLLABLE:New( FSMT, Controllable )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM:New( FSMT ) ) -- Core.Fsm#FSM_CONTROLLABLE
-
- if Controllable then
- self:SetControllable( Controllable )
- end
-
- self:AddTransition( "*", "Stop", "Stopped" )
-
- --- OnBefore Transition Handler for Event Stop.
- -- @function [parent=#FSM_CONTROLLABLE] OnBeforeStop
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Stop.
- -- @function [parent=#FSM_CONTROLLABLE] OnAfterStop
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Stop.
- -- @function [parent=#FSM_CONTROLLABLE] Stop
- -- @param #FSM_CONTROLLABLE self
-
- --- Asynchronous Event Trigger for Event Stop.
- -- @function [parent=#FSM_CONTROLLABLE] __Stop
- -- @param #FSM_CONTROLLABLE self
- -- @param #number Delay The delay in seconds.
-
- --- OnLeave Transition Handler for State Stopped.
- -- @function [parent=#FSM_CONTROLLABLE] OnLeaveStopped
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Stopped.
- -- @function [parent=#FSM_CONTROLLABLE] OnEnterStopped
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- return self
- end
-
- --- OnAfter Transition Handler for Event Stop.
- -- @function [parent=#FSM_CONTROLLABLE] OnAfterStop
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- function FSM_CONTROLLABLE:OnAfterStop(Controllable,From,Event,To)
-
- -- Clear all pending schedules
- self.CallScheduler:Clear()
- end
-
- --- Sets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.
- -- @param #FSM_CONTROLLABLE self
- -- @param Wrapper.Controllable#CONTROLLABLE FSMControllable
- -- @return #FSM_CONTROLLABLE
- function FSM_CONTROLLABLE:SetControllable( FSMControllable )
- self:F( FSMControllable )
- self.Controllable = FSMControllable
- end
-
- --- Gets the CONTROLLABLE object that the FSM_CONTROLLABLE governs.
- -- @param #FSM_CONTROLLABLE self
- -- @return Wrapper.Controllable#CONTROLLABLE
- function FSM_CONTROLLABLE:GetControllable()
- return self.Controllable
- end
-
- function FSM_CONTROLLABLE:_call_handler( handler, params, EventName )
-
- local ErrorHandler = function( errmsg )
-
- env.info( "Error in SCHEDULER function:" .. errmsg )
- if debug ~= nil then
- env.info( debug.traceback() )
- end
-
- return errmsg
- end
-
- if self[handler] then
- self:F3( "Calling " .. handler )
- self._EventSchedules[EventName] = nil
- local Result, Value = xpcall( function() return self[handler]( self, self.Controllable, unpack( params ) ) end, ErrorHandler )
- return Value
- --return self[handler]( self, self.Controllable, unpack( params ) )
- end
- end
-
-end
-
-do -- FSM_PROCESS
-
- --- @type FSM_PROCESS
- -- @field Tasking.Task#TASK Task
- -- @extends Core.Fsm#FSM_CONTROLLABLE
-
-
- --- # FSM_PROCESS, extends @{#FSM}
- --
- -- FSM_PROCESS class models Finite State Machines for @{Task} actions, which control @{Client}s.
- --
- -- ===
- --
- -- @field #FSM_PROCESS FSM_PROCESS
- --
- FSM_PROCESS = {
- ClassName = "FSM_PROCESS",
- }
-
- --- Creates a new FSM_PROCESS object.
- -- @param #FSM_PROCESS self
- -- @return #FSM_PROCESS
- function FSM_PROCESS:New( Controllable, Task )
-
- local self = BASE:Inherit( self, FSM_CONTROLLABLE:New() ) -- Core.Fsm#FSM_PROCESS
-
- self:F( Controllable, Task )
-
- self:Assign( Controllable, Task )
-
- return self
- end
-
- function FSM_PROCESS:Init( FsmProcess )
- self:T( "No Initialisation" )
- end
-
- function FSM_PROCESS:_call_handler( handler, params, EventName )
-
- local ErrorHandler = function( errmsg )
-
- env.info( "Error in FSM_PROCESS call handler:" .. errmsg )
- if debug ~= nil then
- env.info( debug.traceback() )
- end
-
- return errmsg
- end
-
- if self[handler] then
- self:F3( "Calling " .. handler )
- self._EventSchedules[EventName] = nil
- local Result, Value = xpcall( function() return self[handler]( self, self.Controllable, self.Task, unpack( params ) ) end, ErrorHandler )
- return Value
- --return self[handler]( self, self.Controllable, unpack( params ) )
- end
- end
-
- --- Creates a new FSM_PROCESS object based on this FSM_PROCESS.
- -- @param #FSM_PROCESS self
- -- @return #FSM_PROCESS
- function FSM_PROCESS:Copy( Controllable, Task )
- self:T( { self:GetClassNameAndID() } )
-
-
- local NewFsm = self:New( Controllable, Task ) -- Core.Fsm#FSM_PROCESS
-
- NewFsm:Assign( Controllable, Task )
-
- -- Polymorphic call to initialize the new FSM_PROCESS based on self FSM_PROCESS
- NewFsm:Init( self )
-
- -- Set Start State
- NewFsm:SetStartState( self:GetStartState() )
-
- -- Copy Transitions
- for TransitionID, Transition in pairs( self:GetTransitions() ) do
- NewFsm:AddTransition( Transition.From, Transition.Event, Transition.To )
- end
-
- -- Copy Processes
- for ProcessID, Process in pairs( self:GetProcesses() ) do
- self:E( { Process} )
- local FsmProcess = NewFsm:AddProcess( Process.From, Process.Event, Process.fsm:Copy( Controllable, Task ), Process.ReturnEvents )
- end
-
- -- Copy End States
- for EndStateID, EndState in pairs( self:GetEndStates() ) do
- self:T( EndState )
- NewFsm:AddEndState( EndState )
- end
-
- -- Copy the score tables
- for ScoreID, Score in pairs( self:GetScores() ) do
- self:T( Score )
- NewFsm:AddScore( ScoreID, Score.ScoreText, Score.Score )
- end
-
- return NewFsm
- end
-
- --- Removes an FSM_PROCESS object.
- -- @param #FSM_PROCESS self
- -- @return #FSM_PROCESS
- function FSM_PROCESS:Remove()
- self:T( { self:GetClassNameAndID() } )
-
- -- Copy Processes
- for ProcessID, Process in pairs( self:GetProcesses() ) do
- self:E( { Process} )
- Process.fsm:Remove()
- Process.fsm = nil
- end
-
- return self
- end
-
- --- Sets the task of the process.
- -- @param #FSM_PROCESS self
- -- @param Tasking.Task#TASK Task
- -- @return #FSM_PROCESS
- function FSM_PROCESS:SetTask( Task )
-
- self.Task = Task
-
- return self
- end
-
- --- Gets the task of the process.
- -- @param #FSM_PROCESS self
- -- @return Tasking.Task#TASK
- function FSM_PROCESS:GetTask()
-
- return self.Task
- end
-
- --- Gets the mission of the process.
- -- @param #FSM_PROCESS self
- -- @return Tasking.Mission#MISSION
- function FSM_PROCESS:GetMission()
-
- return self.Task.Mission
- end
-
- --- Gets the mission of the process.
- -- @param #FSM_PROCESS self
- -- @return Tasking.CommandCenter#COMMANDCENTER
- function FSM_PROCESS:GetCommandCenter()
-
- return self:GetTask():GetMission():GetCommandCenter()
- end
-
--- TODO: Need to check and fix that an FSM_PROCESS is only for a UNIT. Not for a GROUP.
-
- --- Send a message of the @{Task} to the Group of the Unit.
--- @param #FSM_PROCESS self
-function FSM_PROCESS:Message( Message )
- self:F( { Message = Message } )
-
- local CC = self:GetCommandCenter()
- local TaskGroup = self.Controllable:GetGroup()
-
- local PlayerName = self.Controllable:GetPlayerName() -- Only for a unit
- PlayerName = PlayerName and " (" .. PlayerName .. ")" or "" -- If PlayerName is nil, then keep it nil, otherwise add brackets.
- local Callsign = self.Controllable:GetCallsign()
- local Prefix = Callsign and " @ " .. Callsign .. PlayerName or ""
-
- Message = Prefix .. ": " .. Message
- CC:MessageToGroup( Message, TaskGroup )
-end
-
-
-
-
- --- Assign the process to a @{Unit} and activate the process.
- -- @param #FSM_PROCESS self
- -- @param Task.Tasking#TASK Task
- -- @param Wrapper.Unit#UNIT ProcessUnit
- -- @return #FSM_PROCESS self
- function FSM_PROCESS:Assign( ProcessUnit, Task )
- self:T( { Task, ProcessUnit } )
-
- self:SetControllable( ProcessUnit )
- self:SetTask( Task )
-
- --self.ProcessGroup = ProcessUnit:GetGroup()
-
- return self
- end
-
- function FSM_PROCESS:onenterAssigned( ProcessUnit )
- self:T( "Assign" )
-
- self.Task:Assign()
- end
-
- function FSM_PROCESS:onenterFailed( ProcessUnit )
- self:T( "Failed" )
-
- self.Task:Fail()
- end
-
- function FSM_PROCESS:onenterSuccess( ProcessUnit )
- self:T( "Success" )
-
- self.Task:Success()
- end
-
- --- StateMachine callback function for a FSM_PROCESS
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function FSM_PROCESS:onstatechange( ProcessUnit, Task, From, Event, To, Dummy )
- self:T( { ProcessUnit, From, Event, To, Dummy, self:IsTrace() } )
-
- if self:IsTrace() then
- MESSAGE:New( "@ Process " .. self:GetClassNameAndID() .. " : " .. Event .. " changed to state " .. To, 2 ):ToAll()
- end
-
- self:T( { Scores = self._Scores, To = To } )
- -- TODO: This needs to be reworked with a callback functions allocated within Task, and set within the mission script from the Task Objects...
- if self._Scores[To] then
-
- local Task = self.Task
- local Scoring = Task:GetScoring()
- if Scoring then
- Scoring:_AddMissionTaskScore( Task.Mission, ProcessUnit, self._Scores[To].ScoreText, self._Scores[To].Score )
- end
- end
- end
-
-end
-
-do -- FSM_TASK
-
- --- FSM_TASK class
- -- @type FSM_TASK
- -- @field Tasking.Task#TASK Task
- -- @extends #FSM
-
- --- # FSM_TASK, extends @{#FSM}
- --
- -- FSM_TASK class models Finite State Machines for @{Task}s.
- --
- -- ===
- --
- -- @field #FSM_TASK FSM_TASK
- --
- FSM_TASK = {
- ClassName = "FSM_TASK",
- }
-
- --- Creates a new FSM_TASK object.
- -- @param #FSM_TASK self
- -- @param #table FSMT
- -- @param Tasking.Task#TASK Task
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return #FSM_TASK
- function FSM_TASK:New( FSMT )
-
- local self = BASE:Inherit( self, FSM_CONTROLLABLE:New( FSMT ) ) -- Core.Fsm#FSM_TASK
-
- self["onstatechange"] = self.OnStateChange
-
- return self
- end
-
- function FSM_TASK:_call_handler( handler, params, EventName )
- if self[handler] then
- self:T( "Calling " .. handler )
- self._EventSchedules[EventName] = nil
- return self[handler]( self, unpack( params ) )
- end
- end
-
-end -- FSM_TASK
-
-do -- FSM_SET
-
- --- FSM_SET class
- -- @type FSM_SET
- -- @field Core.Set#SET_BASE Set
- -- @extends Core.Fsm#FSM
-
-
- --- # FSM_SET, extends @{#FSM}
- --
- -- FSM_SET class models Finite State Machines for @{Set}s. Note that these FSMs control multiple objects!!! So State concerns here
- -- for multiple objects or the position of the state machine in the process.
- --
- -- ===
- --
- -- @field #FSM_SET FSM_SET
- --
- FSM_SET = {
- ClassName = "FSM_SET",
- }
-
- --- Creates a new FSM_SET object.
- -- @param #FSM_SET self
- -- @param #table FSMT Finite State Machine Table
- -- @param Set_SET_BASE FSMSet (optional) The Set object that the FSM_SET governs.
- -- @return #FSM_SET
- function FSM_SET:New( FSMSet )
-
- -- Inherits from BASE
- self = BASE:Inherit( self, FSM:New() ) -- Core.Fsm#FSM_SET
-
- if FSMSet then
- self:Set( FSMSet )
- end
-
- return self
- end
-
- --- Sets the SET_BASE object that the FSM_SET governs.
- -- @param #FSM_SET self
- -- @param Core.Set#SET_BASE FSMSet
- -- @return #FSM_SET
- function FSM_SET:Set( FSMSet )
- self:F( FSMSet )
- self.Set = FSMSet
- end
-
- --- Gets the SET_BASE object that the FSM_SET governs.
- -- @param #FSM_SET self
- -- @return Core.Set#SET_BASE
- function FSM_SET:Get()
- return self.Controllable
- end
-
- function FSM_SET:_call_handler( handler, params, EventName )
- if self[handler] then
- self:T( "Calling " .. handler )
- self._EventSchedules[EventName] = nil
- return self[handler]( self, self.Set, unpack( params ) )
- end
- end
-
-end -- FSM_SET
-
---- **Core** - The RADIO class is responsible for **transmitting radio communications**.
---
--- --- bitmap
---
--- ===
---
--- What are radio communications in DCS ?
---
--- * Radio transmissions consist of **sound files** that are broadcasted on a specific **frequency** (e.g. 115MHz) and **modulation** (e.g. AM),
--- * They can be **subtitled** for a specific **duration**, the **power** in Watts of the transmiter's antenna can be set, and the transmission can be **looped**.
---
--- How to supply DCS my own Sound Files ?
---
--- * Your sound files need to be encoded in **.ogg** or .wav,
--- * Your sound files should be **as tiny as possible**. It is suggested you encode in .ogg with low bitrate and sampling settings,
--- * They need to be added in .\l10n\DEFAULT\ in you .miz file (wich can be decompressed like a .zip file),
--- * For simplicty sake, you can **let DCS' Mission Editor add the file** itself, by creating a new Trigger with the action "Sound to Country", and choosing your sound file and a country you don't use in your mission.
---
--- Due to weird DCS quirks, **radio communications behave differently** if sent by a @{Unit#UNIT} or a @{Group#GROUP} or by any other @{Positionable#POSITIONABLE}
---
--- * If the transmitter is a @{Unit#UNIT} or a @{Group#GROUP}, DCS will set the power of the transmission automatically,
--- * If the transmitter is any other @{Positionable#POSITIONABLE}, the transmisison can't be subtitled or looped.
---
--- Note that obviously, the **frequency** and the **modulation** of the transmission are important only if the players are piloting an **Advanced System Modelling** enabled aircraft,
--- like the A10C or the Mirage 2000C. They will **hear the transmission** if they are tuned on the **right frequency and modulation** (and if they are close enough - more on that below).
--- If a FC3 airacraft is used, it will **hear every communication, whatever the frequency and the modulation** is set to.
---
--- ===
---
--- ### Authors: Hugues "Grey_Echo" Bousquet
---
--- @module Radio
-
---- # 1) RADIO class, extends @{Base#BASE}
---
--- ## 1.1) RADIO usage
---
--- There are 3 steps to a successful radio transmission.
---
--- * First, you need to **"add" a @{#RADIO} object** to your @{Positionable#POSITIONABLE}. This is done using the @{Positionable#POSITIONABLE.GetRadio}() function,
--- * Then, you will **set the relevant parameters** to the transmission (see below),
--- * When done, you can actually **broadcast the transmission** (i.e. play the sound) with the @{Positionable#POSITIONABLE.Broadcast}() function.
---
--- Methods to set relevant parameters for both a @{Unit#UNIT} or a @{Group#GROUP} or any other @{Positionable#POSITIONABLE}
---
--- * @{#RADIO.SetFileName}() : Sets the file name of your sound file (e.g. "Noise.ogg"),
--- * @{#RADIO.SetFrequency}() : Sets the frequency of your transmission,
--- * @{#RADIO.SetModulation}() : Sets the modulation of your transmission.
---
--- Additional Methods to set relevant parameters if the transmiter is a @{Unit#UNIT} or a @{Group#GROUP}
---
--- * @{#RADIO.SetLoop}() : Choose if you want the transmission to be looped,
--- * @{#RADIO.SetSubtitle}() : Set both the subtitle and its duration,
--- * @{#RADIO.NewUnitTransmission}() : Shortcut to set all the relevant parameters in one method call
---
--- Additional Methods to set relevant parameters if the transmiter is any other @{Wrapper.Positionable#POSITIONABLE}
---
--- * @{#RADIO.SetPower}() : Sets the power of the antenna in Watts
--- * @{#RADIO.NewGenericTransmission}() : Shortcut to set all the relevant parameters in one method call
---
--- What is this power thing ?
---
--- * If your transmission is sent by a @{Positionable#POSITIONABLE} other than a @{Unit#UNIT} or a @{Group#GROUP}, you can set the power of the antenna,
--- * Otherwise, DCS sets it automatically, depending on what's available on your Unit,
--- * If the player gets **too far** from the transmiter, or if the antenna is **too weak**, the transmission will **fade** and **become noisyer**,
--- * This an automated DCS calculation you have no say on,
--- * For reference, a standard VOR station has a 100W antenna, a standard AA TACAN has a 120W antenna, and civilian ATC's antenna usually range between 300 and 500W,
--- * Note that if the transmission has a subtitle, it will be readable, regardless of the quality of the transmission.
---
--- @type RADIO
--- @field Wrapper.Positionable#POSITIONABLE Positionable The transmiter
--- @field #string FileName Name of the sound file
--- @field #number Frequency Frequency of the transmission in Hz
--- @field #number Modulation Modulation of the transmission (either radio.modulation.AM or radio.modulation.FM)
--- @field #string Subtitle Subtitle of the transmission
--- @field #number SubtitleDuration Duration of the Subtitle in seconds
--- @field #number Power Power of the antenna is Watts
--- @field #boolean Loop
--- @extends Core.Base#BASE
-RADIO = {
- ClassName = "RADIO",
- FileName = "",
- Frequency = 0,
- Modulation = radio.modulation.AM,
- Subtitle = "",
- SubtitleDuration = 0,
- Power = 100,
- Loop = 0,
-}
-
---- Create a new RADIO Object. This doesn't broadcast a transmission, though, use @{#RADIO.Broadcast} to actually broadcast
--- @param #RADIO self
--- @param Wrapper.Positionable#POSITIONABLE Positionable The @{Positionable} that will receive radio capabilities.
--- @return #RADIO Radio
--- @return #nil If Positionable is invalid
--- @usage
--- -- If you want to create a RADIO, you probably should use @{Positionable#POSITIONABLE.GetRadio}() instead
-function RADIO:New(Positionable)
- local self = BASE:Inherit( self, BASE:New() ) -- Core.Radio#RADIO
-
- self:F(Positionable)
-
- if Positionable:GetPointVec2() then -- It's stupid, but the only way I found to make sure positionable is valid
- self.Positionable = Positionable
- return self
- end
-
- self:E({"The passed positionable is invalid, no RADIO created", Positionable})
- return nil
-end
-
---- Check validity of the filename passed and sets RADIO.FileName
--- @param #RADIO self
--- @param #string FileName File name of the sound file (i.e. "Noise.ogg")
--- @return #RADIO self
-function RADIO:SetFileName(FileName)
- self:F2(FileName)
-
- if type(FileName) == "string" then
- if FileName:find(".ogg") or FileName:find(".wav") then
- if not FileName:find("l10n/DEFAULT/") then
- FileName = "l10n/DEFAULT/" .. FileName
- end
- self.FileName = FileName
- return self
- end
- end
-
- self:E({"File name invalid. Maybe something wrong with the extension ?", self.FileName})
- return self
-end
-
---- Check validity of the frequency passed and sets RADIO.Frequency
--- @param #RADIO self
--- @param #number Frequency in MHz (Ranges allowed for radio transmissions in DCS : 30-88 / 108-152 / 225-400MHz)
--- @return #RADIO self
-function RADIO:SetFrequency(Frequency)
- self:F2(Frequency)
- if type(Frequency) == "number" then
- -- If frequency is in range
- if (Frequency >= 30 and Frequency < 88) or (Frequency >= 108 and Frequency < 152) or (Frequency >= 225 and Frequency < 400) then
- self.Frequency = Frequency * 1000000 -- Conversion in Hz
- -- If the RADIO is attached to a UNIT or a GROUP, we need to send the DCS Command "SetFrequency" to change the UNIT or GROUP frequency
- if self.Positionable.ClassName == "UNIT" or self.Positionable.ClassName == "GROUP" then
- self.Positionable:SetCommand({
- id = "SetFrequency",
- params = {
- frequency = self.Frequency,
- modulation = self.Modulation,
- }
- })
- end
- return self
- end
- end
- self:E({"Frequency is outside of DCS Frequency ranges (30-80, 108-152, 225-400). Frequency unchanged.", self.Frequency})
- return self
-end
-
---- Check validity of the frequency passed and sets RADIO.Modulation
--- @param #RADIO self
--- @param #number Modulation either radio.modulation.AM or radio.modulation.FM
--- @return #RADIO self
-function RADIO:SetModulation(Modulation)
- self:F2(Modulation)
- if type(Modulation) == "number" then
- if Modulation == radio.modulation.AM or Modulation == radio.modulation.FM then --TODO Maybe make this future proof if ED decides to add an other modulation ?
- self.Modulation = Modulation
- return self
- end
- end
- self:E({"Modulation is invalid. Use DCS's enum radio.modulation. Modulation unchanged.", self.Modulation})
- return self
-end
-
---- Check validity of the power passed and sets RADIO.Power
--- @param #RADIO self
--- @param #number Power in W
--- @return #RADIO self
-function RADIO:SetPower(Power)
- self:F2(Power)
- if type(Power) == "number" then
- self.Power = math.floor(math.abs(Power)) --TODO Find what is the maximum power allowed by DCS and limit power to that
- return self
- end
- self:E({"Power is invalid. Power unchanged.", self.Power})
- return self
-end
-
---- Check validity of the loop passed and sets RADIO.Loop
--- @param #RADIO self
--- @param #boolean Loop
--- @return #RADIO self
--- @usage
-function RADIO:SetLoop(Loop)
- self:F2(Loop)
- if type(Loop) == "boolean" then
- self.Loop = Loop
- return self
- end
- self:E({"Loop is invalid. Loop unchanged.", self.Loop})
- return self
-end
-
---- Check validity of the subtitle and the subtitleDuration passed and sets RADIO.subtitle and RADIO.subtitleDuration
--- @param #RADIO self
--- @param #string Subtitle
--- @param #number SubtitleDuration in s
--- @return #RADIO self
--- @usage
--- -- Both parameters are mandatory, since it wouldn't make much sense to change the Subtitle and not its duration
-function RADIO:SetSubtitle(Subtitle, SubtitleDuration)
- self:F2({Subtitle, SubtitleDuration})
- if type(Subtitle) == "string" then
- self.Subtitle = Subtitle
- else
- self.Subtitle = ""
- self:E({"Subtitle is invalid. Subtitle reset.", self.Subtitle})
- end
- if type(SubtitleDuration) == "number" then
- if math.floor(math.abs(SubtitleDuration)) == SubtitleDuration then
- self.SubtitleDuration = SubtitleDuration
- return self
- end
- end
- self.SubtitleDuration = 0
- self:E({"SubtitleDuration is invalid. SubtitleDuration reset.", self.SubtitleDuration})
-end
-
---- Create a new transmission, that is to say, populate the RADIO with relevant data
--- @param #RADIO self
--- @param #string FileName
--- @param #number Frequency in MHz
--- @param #number Modulation either radio.modulation.AM or radio.modulation.FM
--- @param #number Power in W
--- @return #RADIO self
--- @usage
--- -- In this function the data is especially relevant if the broadcaster is anything but a UNIT or a GROUP,
--- but it will work with a UNIT or a GROUP anyway
--- -- Only the RADIO and the Filename are mandatory
-function RADIO:NewGenericTransmission(FileName, Frequency, Modulation, Power)
- self:F({FileName, Frequency, Modulation, Power})
-
- self:SetFileName(FileName)
- if Frequency then self:SetFrequency(Frequency) end
- if Modulation then self:SetModulation(Modulation) end
- if Power then self:SetPower(Power) end
-
- return self
-end
-
-
---- Create a new transmission, that is to say, populate the RADIO with relevant data
--- @param #RADIO self
--- @param #string FileName
--- @param #string Subtitle
--- @param #number SubtitleDuration in s
--- @param #number Frequency in MHz
--- @param #number Modulation either radio.modulation.AM or radio.modulation.FM
--- @param #boolean Loop
--- @return #RADIO self
--- @usage
--- -- In this function the data is especially relevant if the broadcaster is a UNIT or a GROUP,
--- but it will work for any POSITIONABLE
--- -- Only the RADIO and the Filename are mandatory
-function RADIO:NewUnitTransmission(FileName, Subtitle, SubtitleDuration, Frequency, Modulation, Loop)
- self:F({FileName, Subtitle, SubtitleDuration, Frequency, Modulation, Loop})
-
- self:SetFileName(FileName)
- if Subtitle then self:SetSubtitle(Subtitle) end
- if SubtitleDuration then self:SetSubtitleDuration(SubtitleDuration) end
- if Frequency then self:SetFrequency(Frequency) end
- if Modulation then self:SetModulation(Modulation) end
- if Loop then self:SetLoop(Loop) end
-
- return self
-end
-
---- Actually Broadcast the transmission
--- @param #RADIO self
--- @return #RADIO self
--- @usage
--- -- The Radio has to be populated with the new transmission before broadcasting.
--- -- Please use RADIO setters or either @{Radio#RADIO.NewGenericTransmission} or @{Radio#RADIO.NewUnitTransmission}
--- -- This class is in fact pretty smart, it determines the right DCS function to use depending on the type of POSITIONABLE
--- -- If the POSITIONABLE is not a UNIT or a GROUP, we use the generic (but limited) trigger.action.radioTransmission()
--- -- If the POSITIONABLE is a UNIT or a GROUP, we use the "TransmitMessage" Command
--- -- If your POSITIONABLE is a UNIT or a GROUP, the Power is ignored.
--- -- If your POSITIONABLE is not a UNIT or a GROUP, the Subtitle, SubtitleDuration and Loop are ignored
-function RADIO:Broadcast()
- self:F()
- -- If the POSITIONABLE is actually a UNIT or a GROUP, use the more complicated DCS command system
- if self.Positionable.ClassName == "UNIT" or self.Positionable.ClassName == "GROUP" then
- self:T2("Broadcasting from a UNIT or a GROUP")
- self.Positionable:SetCommand({
- id = "TransmitMessage",
- params = {
- file = self.FileName,
- duration = self.SubtitleDuration,
- subtitle = self.Subtitle,
- loop = self.Loop,
- }
- })
- else
- -- If the POSITIONABLE is anything else, we revert to the general singleton function
- self:T2("Broadcasting from a POSITIONABLE")
- trigger.action.radioTransmission(self.FileName, self.Positionable:GetPositionVec3(), self.Modulation, false, self.Frequency, self.Power)
- end
- return self
-end
-
---- Stops a transmission
--- @param #RADIO self
--- @return #RADIO self
--- @usage
--- -- Especially usefull to stop the broadcast of looped transmissions
--- -- Only works with broadcasts from UNIT or GROUP
-function RADIO:StopBroadcast()
- self:F()
- -- If the POSITIONABLE is a UNIT or a GROUP, stop the transmission with the DCS "StopTransmission" command
- if self.Positionable.ClassName == "UNIT" or self.Positionable.ClassName == "GROUP" then
- self.Positionable:SetCommand({
- id = "StopTransmission",
- params = {}
- })
- else
- self:E("This broadcast can't be stopped. It's not looped either, so please wait for the end of the sound file playback")
- end
- return self
-end--- This module contains the OBJECT class.
---
--- 1) @{Object#OBJECT} class, extends @{Base#BASE}
--- ===========================================================
--- The @{Object#OBJECT} class is a wrapper class to handle the DCS Object objects:
---
--- * Support all DCS Object APIs.
--- * Enhance with Object specific APIs not in the DCS Object API set.
--- * Manage the "state" of the DCS Object.
---
--- 1.1) OBJECT constructor:
--- ------------------------------
--- The OBJECT class provides the following functions to construct a OBJECT instance:
---
--- * @{Object#OBJECT.New}(): Create a OBJECT instance.
---
--- 1.2) OBJECT methods:
--- --------------------------
--- The following methods can be used to identify an Object object:
---
--- * @{Object#OBJECT.GetID}(): Returns the ID of the Object object.
---
--- ===
---
--- @module Object
-
---- The OBJECT class
--- @type OBJECT
--- @extends Core.Base#BASE
--- @field #string ObjectName The name of the Object.
-OBJECT = {
- ClassName = "OBJECT",
- ObjectName = "",
-}
-
---- A DCSObject
--- @type DCSObject
--- @field id_ The ID of the controllable in DCS
-
---- Create a new OBJECT from a DCSObject
--- @param #OBJECT self
--- @param Dcs.DCSWrapper.Object#Object ObjectName The Object name
--- @return #OBJECT self
-function OBJECT:New( ObjectName, Test )
- local self = BASE:Inherit( self, BASE:New() )
- self:F2( ObjectName )
- self.ObjectName = ObjectName
-
- return self
-end
-
-
---- Returns the unit's unique identifier.
--- @param Wrapper.Object#OBJECT self
--- @return Dcs.DCSWrapper.Object#Object.ID ObjectID
--- @return #nil The DCS Object is not existing or alive.
-function OBJECT:GetID()
- self:F2( self.ObjectName )
-
- local DCSObject = self:GetDCSObject()
-
- if DCSObject then
- local ObjectID = DCSObject:getID()
- return ObjectID
- end
-
- return nil
-end
-
---- Destroys the OBJECT.
--- @param #OBJECT self
--- @return #nil The DCS Unit is not existing or alive.
-function OBJECT:Destroy()
- self:F2( self.ObjectName )
-
- local DCSObject = self:GetDCSObject()
-
- if DCSObject then
-
- DCSObject:destroy()
- end
-
- return nil
-end
-
-
-
-
---- This module contains the IDENTIFIABLE class.
---
--- 1) @{#IDENTIFIABLE} class, extends @{Object#OBJECT}
--- ===============================================================
--- The @{#IDENTIFIABLE} class is a wrapper class to handle the DCS Identifiable objects:
---
--- * Support all DCS Identifiable APIs.
--- * Enhance with Identifiable specific APIs not in the DCS Identifiable API set.
--- * Manage the "state" of the DCS Identifiable.
---
--- 1.1) IDENTIFIABLE constructor:
--- ------------------------------
--- The IDENTIFIABLE class provides the following functions to construct a IDENTIFIABLE instance:
---
--- * @{#IDENTIFIABLE.New}(): Create a IDENTIFIABLE instance.
---
--- 1.2) IDENTIFIABLE methods:
--- --------------------------
--- The following methods can be used to identify an identifiable object:
---
--- * @{#IDENTIFIABLE.GetName}(): Returns the name of the Identifiable.
--- * @{#IDENTIFIABLE.IsAlive}(): Returns if the Identifiable is alive.
--- * @{#IDENTIFIABLE.GetTypeName}(): Returns the type name of the Identifiable.
--- * @{#IDENTIFIABLE.GetCoalition}(): Returns the coalition of the Identifiable.
--- * @{#IDENTIFIABLE.GetCountry}(): Returns the country of the Identifiable.
--- * @{#IDENTIFIABLE.GetDesc}(): Returns the descriptor structure of the Identifiable.
---
---
--- ===
---
--- @module Identifiable
-
---- The IDENTIFIABLE class
--- @type IDENTIFIABLE
--- @extends Wrapper.Object#OBJECT
--- @field #string IdentifiableName The name of the identifiable.
-IDENTIFIABLE = {
- ClassName = "IDENTIFIABLE",
- IdentifiableName = "",
-}
-
-local _CategoryName = {
- [Unit.Category.AIRPLANE] = "Airplane",
- [Unit.Category.HELICOPTER] = "Helicoper",
- [Unit.Category.GROUND_UNIT] = "Ground Identifiable",
- [Unit.Category.SHIP] = "Ship",
- [Unit.Category.STRUCTURE] = "Structure",
- }
-
---- Create a new IDENTIFIABLE from a DCSIdentifiable
--- @param #IDENTIFIABLE self
--- @param Dcs.DCSWrapper.Identifiable#Identifiable IdentifiableName The DCS Identifiable name
--- @return #IDENTIFIABLE self
-function IDENTIFIABLE:New( IdentifiableName )
- local self = BASE:Inherit( self, OBJECT:New( IdentifiableName ) )
- self:F2( IdentifiableName )
- self.IdentifiableName = IdentifiableName
- return self
-end
-
---- Returns if the Identifiable is alive.
--- If the Identifiable is not alive, nil is returned.
--- If the Identifiable is alive, true is returned.
--- @param #IDENTIFIABLE self
--- @return #boolean true if Identifiable is alive.
--- @return #nil if the Identifiable is not existing or is not alive.
-function IDENTIFIABLE:IsAlive()
- self:F3( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject() -- Dcs.DCSObject#Object
-
- if DCSIdentifiable then
- local IdentifiableIsAlive = DCSIdentifiable:isExist()
- return IdentifiableIsAlive
- end
-
- return false
-end
-
-
-
-
---- Returns DCS Identifiable object name.
--- The function provides access to non-activated objects too.
--- @param #IDENTIFIABLE self
--- @return #string The name of the DCS Identifiable.
--- @return #nil The DCS Identifiable is not existing or alive.
-function IDENTIFIABLE:GetName()
- self:F2( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableName = self.IdentifiableName
- return IdentifiableName
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
-
---- Returns the type name of the DCS Identifiable.
--- @param #IDENTIFIABLE self
--- @return #string The type name of the DCS Identifiable.
--- @return #nil The DCS Identifiable is not existing or alive.
-function IDENTIFIABLE:GetTypeName()
- self:F2( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableTypeName = DCSIdentifiable:getTypeName()
- self:T3( IdentifiableTypeName )
- return IdentifiableTypeName
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
-
---- Returns category of the DCS Identifiable.
--- @param #IDENTIFIABLE self
--- @return Dcs.DCSWrapper.Object#Object.Category The category ID
-function IDENTIFIABLE:GetCategory()
- self:F2( self.ObjectName )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- local ObjectCategory = DCSObject:getCategory()
- self:T3( ObjectCategory )
- return ObjectCategory
- end
-
- return nil
-end
-
-
---- Returns the DCS Identifiable category name as defined within the DCS Identifiable Descriptor.
--- @param #IDENTIFIABLE self
--- @return #string The DCS Identifiable Category Name
-function IDENTIFIABLE:GetCategoryName()
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableCategoryName = _CategoryName[ self:GetDesc().category ]
- return IdentifiableCategoryName
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
---- Returns coalition of the Identifiable.
--- @param #IDENTIFIABLE self
--- @return Dcs.DCSCoalitionWrapper.Object#coalition.side The side of the coalition.
--- @return #nil The DCS Identifiable is not existing or alive.
-function IDENTIFIABLE:GetCoalition()
- self:F2( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableCoalition = DCSIdentifiable:getCoalition()
- self:T3( IdentifiableCoalition )
- return IdentifiableCoalition
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
---- Returns country of the Identifiable.
--- @param #IDENTIFIABLE self
--- @return Dcs.DCScountry#country.id The country identifier.
--- @return #nil The DCS Identifiable is not existing or alive.
-function IDENTIFIABLE:GetCountry()
- self:F2( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableCountry = DCSIdentifiable:getCountry()
- self:T3( IdentifiableCountry )
- return IdentifiableCountry
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
-
-
---- Returns Identifiable descriptor. Descriptor type depends on Identifiable category.
--- @param #IDENTIFIABLE self
--- @return Dcs.DCSWrapper.Identifiable#Identifiable.Desc The Identifiable descriptor.
--- @return #nil The DCS Identifiable is not existing or alive.
-function IDENTIFIABLE:GetDesc()
- self:F2( self.IdentifiableName )
-
- local DCSIdentifiable = self:GetDCSObject()
-
- if DCSIdentifiable then
- local IdentifiableDesc = DCSIdentifiable:getDesc()
- self:T2( IdentifiableDesc )
- return IdentifiableDesc
- end
-
- self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" )
- return nil
-end
-
---- Gets the CallSign of the IDENTIFIABLE, which is a blank by default.
--- @param #IDENTIFIABLE self
--- @return #string The CallSign of the IDENTIFIABLE.
-function IDENTIFIABLE:GetCallsign()
- return ''
-end
-
-
-function IDENTIFIABLE:GetThreatLevel()
-
- return 0, "Scenery"
-end
---- This module contains the POSITIONABLE class.
---
--- 1) @{Positionable#POSITIONABLE} class, extends @{Identifiable#IDENTIFIABLE}
--- ===========================================================
--- The @{Positionable#POSITIONABLE} class is a wrapper class to handle the POSITIONABLE objects:
---
--- * Support all DCS APIs.
--- * Enhance with POSITIONABLE specific APIs not in the DCS API set.
--- * Manage the "state" of the POSITIONABLE.
---
--- 1.1) POSITIONABLE constructor:
--- ------------------------------
--- The POSITIONABLE class provides the following functions to construct a POSITIONABLE instance:
---
--- * @{Positionable#POSITIONABLE.New}(): Create a POSITIONABLE instance.
---
--- 1.2) POSITIONABLE methods:
--- --------------------------
--- The following methods can be used to identify an measurable object:
---
--- * @{Positionable#POSITIONABLE.GetID}(): Returns the ID of the measurable object.
--- * @{Positionable#POSITIONABLE.GetName}(): Returns the name of the measurable object.
---
--- ===
---
--- @module Positionable
-
---- The POSITIONABLE class
--- @type POSITIONABLE
--- @extends Wrapper.Identifiable#IDENTIFIABLE
--- @field #string PositionableName The name of the measurable.
-POSITIONABLE = {
- ClassName = "POSITIONABLE",
- PositionableName = "",
-}
-
---- A DCSPositionable
--- @type DCSPositionable
--- @field id_ The ID of the controllable in DCS
-
---- Create a new POSITIONABLE from a DCSPositionable
--- @param #POSITIONABLE self
--- @param Dcs.DCSWrapper.Positionable#Positionable PositionableName The POSITIONABLE name
--- @return #POSITIONABLE self
-function POSITIONABLE:New( PositionableName )
- local self = BASE:Inherit( self, IDENTIFIABLE:New( PositionableName ) )
-
- self.PositionableName = PositionableName
- return self
-end
-
---- Returns the @{DCSTypes#Position3} position vectors indicating the point and direction vectors in 3D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Position The 3D position vectors of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetPositionVec3()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionablePosition = DCSPositionable:getPosition().p
- self:T3( PositionablePosition )
- return PositionablePosition
- end
-
- return nil
-end
-
---- Returns the @{DCSTypes#Vec2} vector indicating the point in 2D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Vec2 The 2D point vector of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetVec2()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionableVec3 = DCSPositionable:getPosition().p
-
- local PositionableVec2 = {}
- PositionableVec2.x = PositionableVec3.x
- PositionableVec2.y = PositionableVec3.z
-
- self:T2( PositionableVec2 )
- return PositionableVec2
- end
-
- return nil
-end
-
---- Returns a POINT_VEC2 object indicating the point in 2D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Core.Point#POINT_VEC2 The 2D point vector of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetPointVec2()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionableVec3 = DCSPositionable:getPosition().p
-
- local PositionablePointVec2 = POINT_VEC2:NewFromVec3( PositionableVec3 )
-
- self:T2( PositionablePointVec2 )
- return PositionablePointVec2
- end
-
- return nil
-end
-
---- Returns a POINT_VEC3 object indicating the point in 3D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Core.Point#POINT_VEC3 The 3D point vector of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetPointVec3()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionableVec3 = self:GetPositionVec3()
-
- local PositionablePointVec3 = POINT_VEC3:NewFromVec3( PositionableVec3 )
-
- self:T2( PositionablePointVec3 )
- return PositionablePointVec3
- end
-
- return nil
-end
-
-
---- Returns a random @{DCSTypes#Vec3} vector within a range, indicating the point in 3D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @param #number Radius
--- @return Dcs.DCSTypes#Vec3 The 3D point vector of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
--- @usage
--- -- If Radius is ignored, returns the Dcs.DCSTypes#Vec3 of first UNIT of the GROUP
-function POSITIONABLE:GetRandomVec3( Radius )
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionablePointVec3 = DCSPositionable:getPosition().p
-
- if Radius then
- local PositionableRandomVec3 = {}
- local angle = math.random() * math.pi*2;
- PositionableRandomVec3.x = PositionablePointVec3.x + math.cos( angle ) * math.random() * Radius;
- PositionableRandomVec3.y = PositionablePointVec3.y
- PositionableRandomVec3.z = PositionablePointVec3.z + math.sin( angle ) * math.random() * Radius;
-
- self:T3( PositionableRandomVec3 )
- return PositionableRandomVec3
- else
- self:E("Radius is nil, returning the PointVec3 of the POSITIONABLE", PositionablePointVec3)
- return PositionablePointVec3
- end
- end
-
- return nil
-end
-
---- Returns the @{DCSTypes#Vec3} vector indicating the 3D vector of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Vec3 The 3D point vector of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetVec3()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionableVec3 = DCSPositionable:getPosition().p
- self:T3( PositionableVec3 )
- return PositionableVec3
- end
-
- return nil
-end
-
---- Returns the altitude of the POSITIONABLE.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Distance The altitude of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetAltitude()
- self:F2()
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionablePointVec3 = DCSPositionable:getPoint() --Dcs.DCSTypes#Vec3
- return PositionablePointVec3.y
- end
-
- return nil
-end
-
---- Returns if the Positionable is located above a runway.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return #boolean true if Positionable is above a runway.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:IsAboveRunway()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
-
- local Vec2 = self:GetVec2()
- local SurfaceType = land.getSurfaceType( Vec2 )
- local IsAboveRunway = SurfaceType == land.SurfaceType.RUNWAY
-
- self:T2( IsAboveRunway )
- return IsAboveRunway
- end
-
- return nil
-end
-
-
-
---- Returns the POSITIONABLE heading in degrees.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return #number The POSTIONABLE heading
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetHeading()
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
-
- local PositionablePosition = DCSPositionable:getPosition()
- if PositionablePosition then
- local PositionableHeading = math.atan2( PositionablePosition.x.z, PositionablePosition.x.x )
- if PositionableHeading < 0 then
- PositionableHeading = PositionableHeading + 2 * math.pi
- end
- PositionableHeading = PositionableHeading * 180 / math.pi
- self:T2( PositionableHeading )
- return PositionableHeading
- end
- end
-
- return nil
-end
-
-
---- Returns true if the POSITIONABLE is in the air.
--- Polymorphic, is overridden in GROUP and UNIT.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return #boolean true if in the air.
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:InAir()
- self:F2( self.PositionableName )
-
- return nil
-end
-
-
---- Returns the POSITIONABLE velocity vector.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Vec3 The velocity vector
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetVelocity()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionableVelocityVec3 = DCSPositionable:getVelocity()
- self:T3( PositionableVelocityVec3 )
- return PositionableVelocityVec3
- end
-
- return nil
-end
-
---- Returns the POSITIONABLE velocity in km/h.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return #number The velocity in km/h
--- @return #nil The POSITIONABLE is not existing or alive.
-function POSITIONABLE:GetVelocityKMH()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local VelocityVec3 = self:GetVelocity()
- local Velocity = ( VelocityVec3.x ^ 2 + VelocityVec3.y ^ 2 + VelocityVec3.z ^ 2 ) ^ 0.5 -- in meters / sec
- local Velocity = Velocity * 3.6 -- now it is in km/h.
- self:T3( Velocity )
- return Velocity
- end
-
- return nil
-end
-
---- Returns a message with the callsign embedded (if there is one).
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
--- @return Core.Message#MESSAGE
-function POSITIONABLE:GetMessage( Message, Duration, Name )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- Name = Name or self:GetTypeName()
- return MESSAGE:New( Message, Duration, self:GetCallsign() .. " (" .. Name .. ")" )
- end
-
- return nil
-end
-
---- Send a message to all coalitions.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToAll( Message, Duration, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToAll()
- end
-
- return nil
-end
-
---- Send a message to a coalition.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTYpes#Duration Duration The duration of the message.
--- @param Dcs.DCScoalition#coalition MessageCoalition The Coalition receiving the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToCoalition( Message, Duration, MessageCoalition, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToCoalition( MessageCoalition )
- end
-
- return nil
-end
-
-
---- Send a message to the red coalition.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTYpes#Duration Duration The duration of the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToRed( Message, Duration, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToRed()
- end
-
- return nil
-end
-
---- Send a message to the blue coalition.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToBlue( Message, Duration, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToBlue()
- end
-
- return nil
-end
-
---- Send a message to a client.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param Wrapper.Client#CLIENT Client The client object receiving the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToClient( Message, Duration, Client, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToClient( Client )
- end
-
- return nil
-end
-
---- Send a message to a @{Group}.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param Wrapper.Group#GROUP MessageGroup The GROUP object receiving the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:MessageToGroup( Message, Duration, MessageGroup, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- if DCSObject:isExist() then
- self:GetMessage( Message, Duration, Name ):ToGroup( MessageGroup )
- end
- end
-
- return nil
-end
-
---- Send a message to the players in the @{Group}.
--- The message will appear in the message area. The message will begin with the callsign of the group and the type of the first unit sending the message.
--- @param #POSITIONABLE self
--- @param #string Message The message text
--- @param Dcs.DCSTypes#Duration Duration The duration of the message.
--- @param #string Name (optional) The Name of the sender. If not provided, the Name is the type of the Positionable.
-function POSITIONABLE:Message( Message, Duration, Name )
- self:F2( { Message, Duration } )
-
- local DCSObject = self:GetDCSObject()
- if DCSObject then
- self:GetMessage( Message, Duration, Name ):ToGroup( self )
- end
-
- return nil
-end
-
---- Create a @{Radio#RADIO}, to allow radio transmission for this POSITIONABLE.
--- Set parameters with the methods provided, then use RADIO:Broadcast() to actually broadcast the message
--- @param #POSITIONABLE self
--- @return #RADIO Radio
-function POSITIONABLE:GetRadio()
- self:F2(self)
- return RADIO:New(self)
-end
---- This module contains the CONTROLLABLE class.
---
--- 1) @{Controllable#CONTROLLABLE} class, extends @{Positionable#POSITIONABLE}
--- ===========================================================
--- The @{Controllable#CONTROLLABLE} class is a wrapper class to handle the DCS Controllable objects:
---
--- * Support all DCS Controllable APIs.
--- * Enhance with Controllable specific APIs not in the DCS Controllable API set.
--- * Handle local Controllable Controller.
--- * Manage the "state" of the DCS Controllable.
---
--- 1.1) CONTROLLABLE constructor
--- -----------------------------
--- The CONTROLLABLE class provides the following functions to construct a CONTROLLABLE instance:
---
--- * @{#CONTROLLABLE.New}(): Create a CONTROLLABLE instance.
---
--- 1.2) CONTROLLABLE task methods
--- ------------------------------
--- Several controllable task methods are available that help you to prepare tasks.
--- These methods return a string consisting of the task description, which can then be given to either a @{Controllable#CONTROLLABLE.PushTask} or @{Controllable#SetTask} method to assign the task to the CONTROLLABLE.
--- Tasks are specific for the category of the CONTROLLABLE, more specific, for AIR, GROUND or AIR and GROUND.
--- Each task description where applicable indicates for which controllable category the task is valid.
--- There are 2 main subdivisions of tasks: Assigned tasks and EnRoute tasks.
---
--- ### 1.2.1) Assigned task methods
---
--- Assigned task methods make the controllable execute the task where the location of the (possible) targets of the task are known before being detected.
--- This is different from the EnRoute tasks, where the targets of the task need to be detected before the task can be executed.
---
--- Find below a list of the **assigned task** methods:
---
--- * @{#CONTROLLABLE.TaskAttackGroup}: (AIR) Attack a Controllable.
--- * @{#CONTROLLABLE.TaskAttackMapObject}: (AIR) Attacking the map object (building, structure, e.t.c).
--- * @{#CONTROLLABLE.TaskAttackUnit}: (AIR) Attack the Unit.
--- * @{#CONTROLLABLE.TaskBombing}: (AIR) Delivering weapon at the point on the ground.
--- * @{#CONTROLLABLE.TaskBombingRunway}: (AIR) Delivering weapon on the runway.
--- * @{#CONTROLLABLE.TaskEmbarking}: (AIR) Move the controllable to a Vec2 Point, wait for a defined duration and embark a controllable.
--- * @{#CONTROLLABLE.TaskEmbarkToTransport}: (GROUND) Embark to a Transport landed at a location.
--- * @{#CONTROLLABLE.TaskEscort}: (AIR) Escort another airborne controllable.
--- * @{#CONTROLLABLE.TaskFAC_AttackGroup}: (AIR + GROUND) The task makes the controllable/unit a FAC and orders the FAC to control the target (enemy ground controllable) destruction.
--- * @{#CONTROLLABLE.TaskFireAtPoint}: (GROUND) Fire some or all ammunition at a VEC2 point.
--- * @{#CONTROLLABLE.TaskFollow}: (AIR) Following another airborne controllable.
--- * @{#CONTROLLABLE.TaskHold}: (GROUND) Hold ground controllable from moving.
--- * @{#CONTROLLABLE.TaskHoldPosition}: (AIR) Hold position at the current position of the first unit of the controllable.
--- * @{#CONTROLLABLE.TaskLand}: (AIR HELICOPTER) Landing at the ground. For helicopters only.
--- * @{#CONTROLLABLE.TaskLandAtZone}: (AIR) Land the controllable at a @{Zone#ZONE_RADIUS).
--- * @{#CONTROLLABLE.TaskOrbitCircle}: (AIR) Orbit at the current position of the first unit of the controllable at a specified alititude.
--- * @{#CONTROLLABLE.TaskOrbitCircleAtVec2}: (AIR) Orbit at a specified position at a specified alititude during a specified duration with a specified speed.
--- * @{#CONTROLLABLE.TaskRefueling}: (AIR) Refueling from the nearest tanker. No parameters.
--- * @{#CONTROLLABLE.TaskRoute}: (AIR + GROUND) Return a Misson task to follow a given route defined by Points.
--- * @{#CONTROLLABLE.TaskRouteToVec2}: (AIR + GROUND) Make the Controllable move to a given point.
--- * @{#CONTROLLABLE.TaskRouteToVec3}: (AIR + GROUND) Make the Controllable move to a given point.
--- * @{#CONTROLLABLE.TaskRouteToZone}: (AIR + GROUND) Route the controllable to a given zone.
--- * @{#CONTROLLABLE.TaskReturnToBase}: (AIR) Route the controllable to an airbase.
---
--- ### 1.2.2) EnRoute task methods
---
--- EnRoute tasks require the targets of the task need to be detected by the controllable (using its sensors) before the task can be executed:
---
--- * @{#CONTROLLABLE.EnRouteTaskAWACS}: (AIR) Aircraft will act as an AWACS for friendly units (will provide them with information about contacts). No parameters.
--- * @{#CONTROLLABLE.EnRouteTaskEngageControllable}: (AIR) Engaging a controllable. The task does not assign the target controllable to the unit/controllable to attack now; it just allows the unit/controllable to engage the target controllable as well as other assigned targets.
--- * @{#CONTROLLABLE.EnRouteTaskEngageTargets}: (AIR) Engaging targets of defined types.
--- * @{#CONTROLLABLE.EnRouteTaskEWR}: (AIR) Attack the Unit.
--- * @{#CONTROLLABLE.EnRouteTaskFAC}: (AIR + GROUND) The task makes the controllable/unit a FAC and lets the FAC to choose a targets (enemy ground controllable) around as well as other assigned targets.
--- * @{#CONTROLLABLE.EnRouteTaskFAC_EngageControllable}: (AIR + GROUND) The task makes the controllable/unit a FAC and lets the FAC to choose the target (enemy ground controllable) as well as other assigned targets.
--- * @{#CONTROLLABLE.EnRouteTaskTanker}: (AIR) Aircraft will act as a tanker for friendly units. No parameters.
---
--- ### 1.2.3) Preparation task methods
---
--- There are certain task methods that allow to tailor the task behaviour:
---
--- * @{#CONTROLLABLE.TaskWrappedAction}: Return a WrappedAction Task taking a Command.
--- * @{#CONTROLLABLE.TaskCombo}: Return a Combo Task taking an array of Tasks.
--- * @{#CONTROLLABLE.TaskCondition}: Return a condition section for a controlled task.
--- * @{#CONTROLLABLE.TaskControlled}: Return a Controlled Task taking a Task and a TaskCondition.
---
--- ### 1.2.4) Obtain the mission from controllable templates
---
--- Controllable templates contain complete mission descriptions. Sometimes you want to copy a complete mission from a controllable and assign it to another:
---
--- * @{#CONTROLLABLE.TaskMission}: (AIR + GROUND) Return a mission task from a mission template.
---
--- 1.3) CONTROLLABLE Command methods
--- --------------------------
--- Controllable **command methods** prepare the execution of commands using the @{#CONTROLLABLE.SetCommand} method:
---
--- * @{#CONTROLLABLE.CommandDoScript}: Do Script command.
--- * @{#CONTROLLABLE.CommandSwitchWayPoint}: Perform a switch waypoint command.
---
--- 1.4) CONTROLLABLE Option methods
--- -------------------------
--- Controllable **Option methods** change the behaviour of the Controllable while being alive.
---
--- ### 1.4.1) Rule of Engagement:
---
--- * @{#CONTROLLABLE.OptionROEWeaponFree}
--- * @{#CONTROLLABLE.OptionROEOpenFire}
--- * @{#CONTROLLABLE.OptionROEReturnFire}
--- * @{#CONTROLLABLE.OptionROEEvadeFire}
---
--- To check whether an ROE option is valid for a specific controllable, use:
---
--- * @{#CONTROLLABLE.OptionROEWeaponFreePossible}
--- * @{#CONTROLLABLE.OptionROEOpenFirePossible}
--- * @{#CONTROLLABLE.OptionROEReturnFirePossible}
--- * @{#CONTROLLABLE.OptionROEEvadeFirePossible}
---
--- ### 1.4.2) Rule on thread:
---
--- * @{#CONTROLLABLE.OptionROTNoReaction}
--- * @{#CONTROLLABLE.OptionROTPassiveDefense}
--- * @{#CONTROLLABLE.OptionROTEvadeFire}
--- * @{#CONTROLLABLE.OptionROTVertical}
---
--- To test whether an ROT option is valid for a specific controllable, use:
---
--- * @{#CONTROLLABLE.OptionROTNoReactionPossible}
--- * @{#CONTROLLABLE.OptionROTPassiveDefensePossible}
--- * @{#CONTROLLABLE.OptionROTEvadeFirePossible}
--- * @{#CONTROLLABLE.OptionROTVerticalPossible}
---
--- ===
---
--- @module Controllable
-
---- The CONTROLLABLE class
--- @type CONTROLLABLE
--- @extends Wrapper.Positionable#POSITIONABLE
--- @field Dcs.DCSWrapper.Controllable#Controllable DCSControllable The DCS controllable class.
--- @field #string ControllableName The name of the controllable.
-CONTROLLABLE = {
- ClassName = "CONTROLLABLE",
- ControllableName = "",
- WayPointFunctions = {},
-}
-
---- Create a new CONTROLLABLE from a DCSControllable
--- @param #CONTROLLABLE self
--- @param Dcs.DCSWrapper.Controllable#Controllable ControllableName The DCS Controllable name
--- @return #CONTROLLABLE self
-function CONTROLLABLE:New( ControllableName )
- local self = BASE:Inherit( self, POSITIONABLE:New( ControllableName ) )
- self:F2( ControllableName )
- self.ControllableName = ControllableName
-
- self.TaskScheduler = SCHEDULER:New( self )
- return self
-end
-
--- DCS Controllable methods support.
-
---- Get the controller for the CONTROLLABLE.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSController#Controller
-function CONTROLLABLE:_GetController()
- self:F2( { self.ControllableName } )
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local ControllableController = DCSControllable:getController()
- self:T3( ControllableController )
- return ControllableController
- end
-
- return nil
-end
-
--- Get methods
-
---- Returns the UNITs wrappers of the DCS Units of the Controllable (default is a GROUP).
--- @param #CONTROLLABLE self
--- @return #list The UNITs wrappers.
-function CONTROLLABLE:GetUnits()
- self:F2( { self.ControllableName } )
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local DCSUnits = DCSControllable:getUnits()
- local Units = {}
- for Index, UnitData in pairs( DCSUnits ) do
- Units[#Units+1] = UNIT:Find( UnitData )
- end
- self:T3( Units )
- return Units
- end
-
- return nil
-end
-
-
---- Returns the health. Dead controllables have health <= 1.0.
--- @param #CONTROLLABLE self
--- @return #number The controllable health value (unit or group average).
--- @return #nil The controllable is not existing or alive.
-function CONTROLLABLE:GetLife()
- self:F2( self.ControllableName )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local UnitLife = 0
- local Units = self:GetUnits()
- if #Units == 1 then
- local Unit = Units[1] -- Wrapper.Unit#UNIT
- UnitLife = Unit:GetLife()
- else
- local UnitLifeTotal = 0
- for UnitID, Unit in pairs( Units ) do
- local Unit = Unit -- Wrapper.Unit#UNIT
- UnitLifeTotal = UnitLifeTotal + Unit:GetLife()
- end
- UnitLife = UnitLifeTotal / #Units
- end
- return UnitLife
- end
-
- return nil
-end
-
-
-
--- Tasks
-
---- Clear all tasks from the controllable.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE
-function CONTROLLABLE:ClearTasks()
- self:F2()
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local Controller = self:_GetController()
- Controller:resetTask()
- return self
- end
-
- return nil
-end
-
-
---- Popping current Task from the controllable.
--- @param #CONTROLLABLE self
--- @return Wrapper.Controllable#CONTROLLABLE self
-function CONTROLLABLE:PopCurrentTask()
- self:F2()
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local Controller = self:_GetController()
- Controller:popTask()
- return self
- end
-
- return nil
-end
-
---- Pushing Task on the queue from the controllable.
--- @param #CONTROLLABLE self
--- @return Wrapper.Controllable#CONTROLLABLE self
-function CONTROLLABLE:PushTask( DCSTask, WaitTime )
- self:F2()
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local Controller = self:_GetController()
-
- -- When a controllable SPAWNs, it takes about a second to get the controllable in the simulator. Setting tasks to unspawned controllables provides unexpected results.
- -- Therefore we schedule the functions to set the mission and options for the Controllable.
- -- Controller:pushTask( DCSTask )
-
- if WaitTime then
- self.TaskScheduler:Schedule( Controller, Controller.pushTask, { DCSTask }, WaitTime )
- else
- Controller:pushTask( DCSTask )
- end
-
- return self
- end
-
- return nil
-end
-
---- Clearing the Task Queue and Setting the Task on the queue from the controllable.
--- @param #CONTROLLABLE self
--- @return Wrapper.Controllable#CONTROLLABLE self
-function CONTROLLABLE:SetTask( DCSTask, WaitTime )
- self:F2( { DCSTask } )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
-
- local Controller = self:_GetController()
- self:T3( Controller )
-
- -- When a controllable SPAWNs, it takes about a second to get the controllable in the simulator. Setting tasks to unspawned controllables provides unexpected results.
- -- Therefore we schedule the functions to set the mission and options for the Controllable.
- -- Controller.setTask( Controller, DCSTask )
-
- if not WaitTime then
- Controller:setTask( DCSTask )
- else
- self.TaskScheduler:Schedule( Controller, Controller.setTask, { DCSTask }, WaitTime )
- end
-
- return self
- end
-
- return nil
-end
-
-
---- Return a condition section for a controlled task.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTime#Time time
--- @param #string userFlag
--- @param #boolean userFlagValue
--- @param #string condition
--- @param Dcs.DCSTime#Time duration
--- @param #number lastWayPoint
--- return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskCondition( time, userFlag, userFlagValue, condition, duration, lastWayPoint )
- self:F2( { time, userFlag, userFlagValue, condition, duration, lastWayPoint } )
-
- local DCSStopCondition = {}
- DCSStopCondition.time = time
- DCSStopCondition.userFlag = userFlag
- DCSStopCondition.userFlagValue = userFlagValue
- DCSStopCondition.condition = condition
- DCSStopCondition.duration = duration
- DCSStopCondition.lastWayPoint = lastWayPoint
-
- self:T3( { DCSStopCondition } )
- return DCSStopCondition
-end
-
---- Return a Controlled Task taking a Task and a TaskCondition.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTasking.Task#Task DCSTask
--- @param #DCSStopCondition DCSStopCondition
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskControlled( DCSTask, DCSStopCondition )
- self:F2( { DCSTask, DCSStopCondition } )
-
- local DCSTaskControlled
-
- DCSTaskControlled = {
- id = 'ControlledTask',
- params = {
- task = DCSTask,
- stopCondition = DCSStopCondition
- }
- }
-
- self:T3( { DCSTaskControlled } )
- return DCSTaskControlled
-end
-
---- Return a Combo Task taking an array of Tasks.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTasking.Task#TaskArray DCSTasks Array of @{DCSTasking.Task#Task}
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskCombo( DCSTasks )
- self:F2( { DCSTasks } )
-
- local DCSTaskCombo
-
- DCSTaskCombo = {
- id = 'ComboTask',
- params = {
- tasks = DCSTasks
- }
- }
-
- for TaskID, Task in ipairs( DCSTasks ) do
- self:E( Task )
- end
-
- self:T3( { DCSTaskCombo } )
- return DCSTaskCombo
-end
-
---- Return a WrappedAction Task taking a Command.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSCommand#Command DCSCommand
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskWrappedAction( DCSCommand, Index )
- self:F2( { DCSCommand } )
-
- local DCSTaskWrappedAction
-
- DCSTaskWrappedAction = {
- id = "WrappedAction",
- enabled = true,
- number = Index,
- auto = false,
- params = {
- action = DCSCommand,
- },
- }
-
- self:T3( { DCSTaskWrappedAction } )
- return DCSTaskWrappedAction
-end
-
---- Executes a command action
--- @param #CONTROLLABLE self
--- @param Dcs.DCSCommand#Command DCSCommand
--- @return #CONTROLLABLE self
-function CONTROLLABLE:SetCommand( DCSCommand )
- self:F2( DCSCommand )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local Controller = self:_GetController()
- Controller:setCommand( DCSCommand )
- return self
- end
-
- return nil
-end
-
---- Perform a switch waypoint command
--- @param #CONTROLLABLE self
--- @param #number FromWayPoint
--- @param #number ToWayPoint
--- @return Dcs.DCSTasking.Task#Task
--- @usage
--- --- This test demonstrates the use(s) of the SwitchWayPoint method of the GROUP class.
--- HeliGroup = GROUP:FindByName( "Helicopter" )
---
--- --- Route the helicopter back to the FARP after 60 seconds.
--- -- We use the SCHEDULER class to do this.
--- SCHEDULER:New( nil,
--- function( HeliGroup )
--- local CommandRTB = HeliGroup:CommandSwitchWayPoint( 2, 8 )
--- HeliGroup:SetCommand( CommandRTB )
--- end, { HeliGroup }, 90
--- )
-function CONTROLLABLE:CommandSwitchWayPoint( FromWayPoint, ToWayPoint )
- self:F2( { FromWayPoint, ToWayPoint } )
-
- local CommandSwitchWayPoint = {
- id = 'SwitchWaypoint',
- params = {
- fromWaypointIndex = FromWayPoint,
- goToWaypointIndex = ToWayPoint,
- },
- }
-
- self:T3( { CommandSwitchWayPoint } )
- return CommandSwitchWayPoint
-end
-
---- Create a stop route command, which returns a string containing the command.
--- Use the result in the method @{#CONTROLLABLE.SetCommand}().
--- A value of true will make the ground group stop, a value of false will make it continue.
--- Note that this can only work on GROUP level, although individual UNITs can be commanded, the whole GROUP will react.
---
--- Example missions:
---
--- * GRP-310
---
--- @param #CONTROLLABLE self
--- @param #boolean StopRoute true if the ground unit needs to stop, false if it needs to continue to move.
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:CommandStopRoute( StopRoute )
- self:F2( { StopRoute } )
-
- local CommandStopRoute = {
- id = 'StopRoute',
- params = {
- value = StopRoute,
- },
- }
-
- self:T3( { CommandStopRoute } )
- return CommandStopRoute
-end
-
-
--- TASKS FOR AIR CONTROLLABLES
-
-
---- (AIR) Attack a Controllable.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE AttackGroup The Controllable to be attacked.
--- @param #number WeaponType (optional) Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param Dcs.DCSTypes#Distance Altitude (optional) Desired attack start altitude. Controllable/aircraft will make its attacks from the altitude. If the altitude is too low or too high to use weapon aircraft/controllable will choose closest altitude to the desired attack start altitude. If the desired altitude is defined controllable/aircraft will not attack from safe altitude.
--- @param #boolean AttackQtyLimit (optional) The flag determines how to interpret attackQty parameter. If the flag is true then attackQty is a limit on maximal attack quantity for "AttackGroup" and "AttackUnit" tasks. If the flag is false then attackQty is a desired attack quantity for "Bombing" and "BombingRunway" tasks.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskAttackGroup( AttackGroup, WeaponType, WeaponExpend, AttackQty, Direction, Altitude, AttackQtyLimit )
- self:F2( { self.ControllableName, AttackGroup, WeaponType, WeaponExpend, AttackQty, Direction, Altitude, AttackQtyLimit } )
-
- -- AttackGroup = {
- -- id = 'AttackGroup',
- -- params = {
- -- groupId = Group.ID,
- -- weaponType = number,
- -- expend = enum AI.Task.WeaponExpend,
- -- attackQty = number,
- -- directionEnabled = boolean,
- -- direction = Azimuth,
- -- altitudeEnabled = boolean,
- -- altitude = Distance,
- -- attackQtyLimit = boolean,
- -- }
- -- }
-
- local DirectionEnabled = nil
- if Direction then
- DirectionEnabled = true
- end
-
- local AltitudeEnabled = nil
- if Altitude then
- AltitudeEnabled = true
- end
-
- local DCSTask
- DCSTask = { id = 'AttackGroup',
- params = {
- groupId = AttackGroup:GetID(),
- weaponType = WeaponType,
- expend = WeaponExpend,
- attackQty = AttackQty,
- directionEnabled = DirectionEnabled,
- direction = Direction,
- altitudeEnabled = AltitudeEnabled,
- altitude = Altitude,
- attackQtyLimit = AttackQtyLimit,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- (AIR) Attack the Unit.
--- @param #CONTROLLABLE self
--- @param Wrapper.Unit#UNIT AttackUnit The UNIT.
--- @param #boolean GroupAttack (optional) If true, all units in the group will attack the Unit when found.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param #number Altitude (optional) The altitude from where to attack.
--- @param #boolean Visible (optional) not a clue.
--- @param #number WeaponType (optional) The WeaponType.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskAttackUnit( AttackUnit, GroupAttack, WeaponExpend, AttackQty, Direction, Altitude, Visible, WeaponType )
- self:F2( { self.ControllableName, AttackUnit, GroupAttack, WeaponExpend, AttackQty, Direction, Altitude, Visible, WeaponType } )
-
- local DCSTask
- DCSTask = {
- id = 'AttackUnit',
- params = {
- unitId = AttackUnit:GetID(),
- groupAttack = GroupAttack or false,
- visible = Visible or false,
- expend = WeaponExpend or "Auto",
- directionEnabled = Direction and true or false,
- direction = Direction,
- altitudeEnabled = Altitude and true or false,
- altitude = Altitude or 30,
- attackQtyLimit = AttackQty and true or false,
- attackQty = AttackQty,
- weaponType = WeaponType
- }
- }
-
- self:E( DCSTask )
-
- return DCSTask
-end
-
-
---- (AIR) Delivering weapon at the point on the ground.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Vec2 2D-coordinates of the point to deliver weapon at.
--- @param #number WeaponType (optional) Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) Desired quantity of passes. The parameter is not the same in AttackGroup and AttackUnit tasks.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param #boolean ControllableAttack (optional) Flag indicates that the target must be engaged by all aircrafts of the controllable. Has effect only if the task is assigned to a controllable, not to a single aircraft.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskBombing( Vec2, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack )
- self:F2( { self.ControllableName, Vec2, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack } )
-
--- Bombing = {
--- id = 'Bombing',
--- params = {
--- point = Vec2,
--- weaponType = number,
--- expend = enum AI.Task.WeaponExpend,
--- attackQty = number,
--- direction = Azimuth,
--- controllableAttack = boolean,
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'Bombing',
- params = {
- point = Vec2,
- weaponType = WeaponType,
- expend = WeaponExpend,
- attackQty = AttackQty,
- direction = Direction,
- controllableAttack = ControllableAttack,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- (AIR) Orbit at a specified position at a specified alititude during a specified duration with a specified speed.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Point The point to hold the position.
--- @param #number Altitude The altitude to hold the position.
--- @param #number Speed The speed flying when holding the position.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:TaskOrbitCircleAtVec2( Point, Altitude, Speed )
- self:F2( { self.ControllableName, Point, Altitude, Speed } )
-
- -- pattern = enum AI.Task.OribtPattern,
- -- point = Vec2,
- -- point2 = Vec2,
- -- speed = Distance,
- -- altitude = Distance
-
- local LandHeight = land.getHeight( Point )
-
- self:T3( { LandHeight } )
-
- local DCSTask = { id = 'Orbit',
- params = { pattern = AI.Task.OrbitPattern.CIRCLE,
- point = Point,
- speed = Speed,
- altitude = Altitude + LandHeight
- }
- }
-
-
- -- local AITask = { id = 'ControlledTask',
- -- params = { task = { id = 'Orbit',
- -- params = { pattern = AI.Task.OrbitPattern.CIRCLE,
- -- point = Point,
- -- speed = Speed,
- -- altitude = Altitude + LandHeight
- -- }
- -- },
- -- stopCondition = { duration = Duration
- -- }
- -- }
- -- }
- -- )
-
- return DCSTask
-end
-
---- (AIR) Orbit at the current position of the first unit of the controllable at a specified alititude.
--- @param #CONTROLLABLE self
--- @param #number Altitude The altitude to hold the position.
--- @param #number Speed The speed flying when holding the position.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:TaskOrbitCircle( Altitude, Speed )
- self:F2( { self.ControllableName, Altitude, Speed } )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local ControllablePoint = self:GetVec2()
- return self:TaskOrbitCircleAtVec2( ControllablePoint, Altitude, Speed )
- end
-
- return nil
-end
-
-
-
---- (AIR) Hold position at the current position of the first unit of the controllable.
--- @param #CONTROLLABLE self
--- @param #number Duration The maximum duration in seconds to hold the position.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:TaskHoldPosition()
- self:F2( { self.ControllableName } )
-
- return self:TaskOrbitCircle( 30, 10 )
-end
-
-
-
-
---- (AIR) Attacking the map object (building, structure, e.t.c).
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Vec2 2D-coordinates of the point the map object is closest to. The distance between the point and the map object must not be greater than 2000 meters. Object id is not used here because Mission Editor doesn't support map object identificators.
--- @param #number WeaponType (optional) Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param #boolean ControllableAttack (optional) Flag indicates that the target must be engaged by all aircrafts of the controllable. Has effect only if the task is assigned to a controllable, not to a single aircraft.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskAttackMapObject( Vec2, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack )
- self:F2( { self.ControllableName, Vec2, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack } )
-
--- AttackMapObject = {
--- id = 'AttackMapObject',
--- params = {
--- point = Vec2,
--- weaponType = number,
--- expend = enum AI.Task.WeaponExpend,
--- attackQty = number,
--- direction = Azimuth,
--- controllableAttack = boolean,
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'AttackMapObject',
- params = {
- point = Vec2,
- weaponType = WeaponType,
- expend = WeaponExpend,
- attackQty = AttackQty,
- direction = Direction,
- controllableAttack = ControllableAttack,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Delivering weapon on the runway.
--- @param #CONTROLLABLE self
--- @param Wrapper.Airbase#AIRBASE Airbase Airbase to attack.
--- @param #number WeaponType (optional) Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param #boolean ControllableAttack (optional) Flag indicates that the target must be engaged by all aircrafts of the controllable. Has effect only if the task is assigned to a controllable, not to a single aircraft.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskBombingRunway( Airbase, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack )
- self:F2( { self.ControllableName, Airbase, WeaponType, WeaponExpend, AttackQty, Direction, ControllableAttack } )
-
--- BombingRunway = {
--- id = 'BombingRunway',
--- params = {
--- runwayId = AirdromeId,
--- weaponType = number,
--- expend = enum AI.Task.WeaponExpend,
--- attackQty = number,
--- direction = Azimuth,
--- controllableAttack = boolean,
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'BombingRunway',
- params = {
- point = Airbase:GetID(),
- weaponType = WeaponType,
- expend = WeaponExpend,
- attackQty = AttackQty,
- direction = Direction,
- controllableAttack = ControllableAttack,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Refueling from the nearest tanker. No parameters.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskRefueling()
- self:F2( { self.ControllableName } )
-
--- Refueling = {
--- id = 'Refueling',
--- params = {}
--- }
-
- local DCSTask
- DCSTask = { id = 'Refueling',
- params = {
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR HELICOPTER) Landing at the ground. For helicopters only.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Point The point where to land.
--- @param #number Duration The duration in seconds to stay on the ground.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:TaskLandAtVec2( Point, Duration )
- self:F2( { self.ControllableName, Point, Duration } )
-
--- Land = {
--- id= 'Land',
--- params = {
--- point = Vec2,
--- durationFlag = boolean,
--- duration = Time
--- }
--- }
-
- local DCSTask
- if Duration and Duration > 0 then
- DCSTask = { id = 'Land',
- params = {
- point = Point,
- durationFlag = true,
- duration = Duration,
- },
- }
- else
- DCSTask = { id = 'Land',
- params = {
- point = Point,
- durationFlag = false,
- },
- }
- end
-
- self:T3( DCSTask )
- return DCSTask
-end
-
---- (AIR) Land the controllable at a @{Zone#ZONE_RADIUS).
--- @param #CONTROLLABLE self
--- @param Core.Zone#ZONE Zone The zone where to land.
--- @param #number Duration The duration in seconds to stay on the ground.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:TaskLandAtZone( Zone, Duration, RandomPoint )
- self:F2( { self.ControllableName, Zone, Duration, RandomPoint } )
-
- local Point
- if RandomPoint then
- Point = Zone:GetRandomVec2()
- else
- Point = Zone:GetVec2()
- end
-
- local DCSTask = self:TaskLandAtVec2( Point, Duration )
-
- self:T3( DCSTask )
- return DCSTask
-end
-
-
-
---- (AIR) Following another airborne controllable.
--- The unit / controllable will follow lead unit of another controllable, wingmens of both controllables will continue following their leaders.
--- If another controllable is on land the unit / controllable will orbit around.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE FollowControllable The controllable to be followed.
--- @param Dcs.DCSTypes#Vec3 Vec3 Position of the unit / lead unit of the controllable relative lead unit of another controllable in frame reference oriented by course of lead unit of another controllable. If another controllable is on land the unit / controllable will orbit around.
--- @param #number LastWaypointIndex Detach waypoint of another controllable. Once reached the unit / controllable Follow task is finished.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskFollow( FollowControllable, Vec3, LastWaypointIndex )
- self:F2( { self.ControllableName, FollowControllable, Vec3, LastWaypointIndex } )
-
--- Follow = {
--- id = 'Follow',
--- params = {
--- groupId = Group.ID,
--- pos = Vec3,
--- lastWptIndexFlag = boolean,
--- lastWptIndex = number
--- }
--- }
-
- local LastWaypointIndexFlag = false
- if LastWaypointIndex then
- LastWaypointIndexFlag = true
- end
-
- local DCSTask
- DCSTask = {
- id = 'Follow',
- params = {
- groupId = FollowControllable:GetID(),
- pos = Vec3,
- lastWptIndexFlag = LastWaypointIndexFlag,
- lastWptIndex = LastWaypointIndex
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Escort another airborne controllable.
--- The unit / controllable will follow lead unit of another controllable, wingmens of both controllables will continue following their leaders.
--- The unit / controllable will also protect that controllable from threats of specified types.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE EscortControllable The controllable to be escorted.
--- @param Dcs.DCSTypes#Vec3 Vec3 Position of the unit / lead unit of the controllable relative lead unit of another controllable in frame reference oriented by course of lead unit of another controllable. If another controllable is on land the unit / controllable will orbit around.
--- @param #number LastWaypointIndex Detach waypoint of another controllable. Once reached the unit / controllable Follow task is finished.
--- @param #number EngagementDistanceMax Maximal distance from escorted controllable to threat. If the threat is already engaged by escort escort will disengage if the distance becomes greater than 1.5 * engagementDistMax.
--- @param Dcs.DCSTypes#AttributeNameArray TargetTypes Array of AttributeName that is contains threat categories allowed to engage.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskEscort( FollowControllable, Vec3, LastWaypointIndex, EngagementDistance, TargetTypes )
- self:F2( { self.ControllableName, FollowControllable, Vec3, LastWaypointIndex, EngagementDistance, TargetTypes } )
-
--- Escort = {
--- id = 'Escort',
--- params = {
--- groupId = Group.ID,
--- pos = Vec3,
--- lastWptIndexFlag = boolean,
--- lastWptIndex = number,
--- engagementDistMax = Distance,
--- targetTypes = array of AttributeName,
--- }
--- }
-
- local LastWaypointIndexFlag = false
- if LastWaypointIndex then
- LastWaypointIndexFlag = true
- end
-
- local DCSTask
- DCSTask = { id = 'Escort',
- params = {
- groupId = FollowControllable:GetID(),
- pos = Vec3,
- lastWptIndexFlag = LastWaypointIndexFlag,
- lastWptIndex = LastWaypointIndex,
- engagementDistMax = EngagementDistance,
- targetTypes = TargetTypes,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
--- GROUND TASKS
-
---- (GROUND) Fire at a VEC2 point until ammunition is finished.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Vec2 The point to fire at.
--- @param Dcs.DCSTypes#Distance Radius The radius of the zone to deploy the fire at.
--- @param #number AmmoCount (optional) Quantity of ammunition to expand (omit to fire until ammunition is depleted).
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskFireAtPoint( Vec2, Radius, AmmoCount )
- self:F2( { self.ControllableName, Vec2, Radius, AmmoCount } )
-
- -- FireAtPoint = {
- -- id = 'FireAtPoint',
- -- params = {
- -- point = Vec2,
- -- radius = Distance,
- -- expendQty = number,
- -- expendQtyEnabled = boolean,
- -- }
- -- }
-
- local DCSTask
- DCSTask = { id = 'FireAtPoint',
- params = {
- point = Vec2,
- radius = Radius,
- expendQty = 100, -- dummy value
- expendQtyEnabled = false,
- }
- }
-
- if AmmoCount then
- DCSTask.params.expendQty = AmmoCount
- DCSTask.params.expendQtyEnabled = true
- end
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- (GROUND) Hold ground controllable from moving.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskHold()
- self:F2( { self.ControllableName } )
-
--- Hold = {
--- id = 'Hold',
--- params = {
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'Hold',
- params = {
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
--- TASKS FOR AIRBORNE AND GROUND UNITS/CONTROLLABLES
-
---- (AIR + GROUND) The task makes the controllable/unit a FAC and orders the FAC to control the target (enemy ground controllable) destruction.
--- The killer is player-controlled allied CAS-aircraft that is in contact with the FAC.
--- If the task is assigned to the controllable lead unit will be a FAC.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE AttackGroup Target CONTROLLABLE.
--- @param #number WeaponType Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.Designation Designation (optional) Designation type.
--- @param #boolean Datalink (optional) Allows to use datalink to send the target information to attack aircraft. Enabled by default.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskFAC_AttackGroup( AttackGroup, WeaponType, Designation, Datalink )
- self:F2( { self.ControllableName, AttackGroup, WeaponType, Designation, Datalink } )
-
--- FAC_AttackGroup = {
--- id = 'FAC_AttackGroup',
--- params = {
--- groupId = Group.ID,
--- weaponType = number,
--- designation = enum AI.Task.Designation,
--- datalink = boolean
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'FAC_AttackGroup',
- params = {
- groupId = AttackGroup:GetID(),
- weaponType = WeaponType,
- designation = Designation,
- datalink = Datalink,
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
--- EN-ACT_ROUTE TASKS FOR AIRBORNE CONTROLLABLES
-
---- (AIR) Engaging targets of defined types.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Distance Distance Maximal distance from the target to a route leg. If the target is on a greater distance it will be ignored.
--- @param Dcs.DCSTypes#AttributeNameArray TargetTypes Array of target categories allowed to engage.
--- @param #number Priority All enroute tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskEngageTargets( Distance, TargetTypes, Priority )
- self:F2( { self.ControllableName, Distance, TargetTypes, Priority } )
-
--- EngageTargets ={
--- id = 'EngageTargets',
--- params = {
--- maxDist = Distance,
--- targetTypes = array of AttributeName,
--- priority = number
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'EngageTargets',
- params = {
- maxDist = Distance,
- targetTypes = TargetTypes,
- priority = Priority
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
-
---- (AIR) Engaging a targets of defined types at circle-shaped zone.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Vec2 2D-coordinates of the zone.
--- @param Dcs.DCSTypes#Distance Radius Radius of the zone.
--- @param Dcs.DCSTypes#AttributeNameArray TargetTypes Array of target categories allowed to engage.
--- @param #number Priority All en-route tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskEngageTargets( Vec2, Radius, TargetTypes, Priority )
- self:F2( { self.ControllableName, Vec2, Radius, TargetTypes, Priority } )
-
--- EngageTargetsInZone = {
--- id = 'EngageTargetsInZone',
--- params = {
--- point = Vec2,
--- zoneRadius = Distance,
--- targetTypes = array of AttributeName,
--- priority = number
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'EngageTargetsInZone',
- params = {
- point = Vec2,
- zoneRadius = Radius,
- targetTypes = TargetTypes,
- priority = Priority
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Engaging a controllable. The task does not assign the target controllable to the unit/controllable to attack now; it just allows the unit/controllable to engage the target controllable as well as other assigned targets.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE AttackGroup The Controllable to be attacked.
--- @param #number Priority All en-route tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @param #number WeaponType (optional) Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param Dcs.DCSTypes#Distance Altitude (optional) Desired attack start altitude. Controllable/aircraft will make its attacks from the altitude. If the altitude is too low or too high to use weapon aircraft/controllable will choose closest altitude to the desired attack start altitude. If the desired altitude is defined controllable/aircraft will not attack from safe altitude.
--- @param #boolean AttackQtyLimit (optional) The flag determines how to interpret attackQty parameter. If the flag is true then attackQty is a limit on maximal attack quantity for "AttackGroup" and "AttackUnit" tasks. If the flag is false then attackQty is a desired attack quantity for "Bombing" and "BombingRunway" tasks.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskEngageGroup( AttackGroup, Priority, WeaponType, WeaponExpend, AttackQty, Direction, Altitude, AttackQtyLimit )
- self:F2( { self.ControllableName, AttackGroup, Priority, WeaponType, WeaponExpend, AttackQty, Direction, Altitude, AttackQtyLimit } )
-
- -- EngageControllable = {
- -- id = 'EngageControllable ',
- -- params = {
- -- groupId = Group.ID,
- -- weaponType = number,
- -- expend = enum AI.Task.WeaponExpend,
- -- attackQty = number,
- -- directionEnabled = boolean,
- -- direction = Azimuth,
- -- altitudeEnabled = boolean,
- -- altitude = Distance,
- -- attackQtyLimit = boolean,
- -- priority = number,
- -- }
- -- }
-
- local DirectionEnabled = nil
- if Direction then
- DirectionEnabled = true
- end
-
- local AltitudeEnabled = nil
- if Altitude then
- AltitudeEnabled = true
- end
-
- local DCSTask
- DCSTask = { id = 'EngageControllable',
- params = {
- groupId = AttackGroup:GetID(),
- weaponType = WeaponType,
- expend = WeaponExpend,
- attackQty = AttackQty,
- directionEnabled = DirectionEnabled,
- direction = Direction,
- altitudeEnabled = AltitudeEnabled,
- altitude = Altitude,
- attackQtyLimit = AttackQtyLimit,
- priority = Priority,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Search and attack the Unit.
--- @param #CONTROLLABLE self
--- @param Wrapper.Unit#UNIT EngageUnit The UNIT.
--- @param #number Priority (optional) All en-route tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @param #boolean GroupAttack (optional) If true, all units in the group will attack the Unit when found.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend WeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number AttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth Direction (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
--- @param Dcs.DCSTypes#Distance Altitude (optional) Desired altitude to perform the unit engagement.
--- @param #boolean Visible (optional) Unit must be visible.
--- @param #boolean ControllableAttack (optional) Flag indicates that the target must be engaged by all aircrafts of the controllable. Has effect only if the task is assigned to a controllable, not to a single aircraft.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskEngageUnit( EngageUnit, Priority, GroupAttack, WeaponExpend, AttackQty, Direction, Altitude, Visible, ControllableAttack )
- self:F2( { self.ControllableName, EngageUnit, Priority, GroupAttack, WeaponExpend, AttackQty, Direction, Altitude, Visible, ControllableAttack } )
-
- -- EngageUnit = {
- -- id = 'EngageUnit',
- -- params = {
- -- unitId = Unit.ID,
- -- weaponType = number,
- -- expend = enum AI.Task.WeaponExpend
- -- attackQty = number,
- -- direction = Azimuth,
- -- attackQtyLimit = boolean,
- -- controllableAttack = boolean,
- -- priority = number,
- -- }
- -- }
-
- local DCSTask
- DCSTask = { id = 'EngageUnit',
- params = {
- unitId = EngageUnit:GetID(),
- priority = Priority or 1,
- groupAttack = GroupAttack or false,
- visible = Visible or false,
- expend = WeaponExpend or "Auto",
- directionEnabled = Direction and true or false,
- direction = Direction,
- altitudeEnabled = Altitude and true or false,
- altitude = Altitude,
- attackQtyLimit = AttackQty and true or false,
- attackQty = AttackQty,
- controllableAttack = ControllableAttack,
- },
- },
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
-
---- (AIR) Aircraft will act as an AWACS for friendly units (will provide them with information about contacts). No parameters.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskAWACS( )
- self:F2( { self.ControllableName } )
-
--- AWACS = {
--- id = 'AWACS',
--- params = {
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'AWACS',
- params = {
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR) Aircraft will act as a tanker for friendly units. No parameters.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskTanker( )
- self:F2( { self.ControllableName } )
-
--- Tanker = {
--- id = 'Tanker',
--- params = {
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'Tanker',
- params = {
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
--- En-route tasks for ground units/controllables
-
---- (GROUND) Ground unit (EW-radar) will act as an EWR for friendly units (will provide them with information about contacts). No parameters.
--- @param #CONTROLLABLE self
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskEWR( )
- self:F2( { self.ControllableName } )
-
--- EWR = {
--- id = 'EWR',
--- params = {
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'EWR',
- params = {
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
--- En-route tasks for airborne and ground units/controllables
-
---- (AIR + GROUND) The task makes the controllable/unit a FAC and lets the FAC to choose the target (enemy ground controllable) as well as other assigned targets.
--- The killer is player-controlled allied CAS-aircraft that is in contact with the FAC.
--- If the task is assigned to the controllable lead unit will be a FAC.
--- @param #CONTROLLABLE self
--- @param Wrapper.Controllable#CONTROLLABLE AttackGroup Target CONTROLLABLE.
--- @param #number Priority All en-route tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @param #number WeaponType Bitmask of weapon types those allowed to use. If parameter is not defined that means no limits on weapon usage.
--- @param Dcs.DCSTypes#AI.Task.Designation Designation (optional) Designation type.
--- @param #boolean Datalink (optional) Allows to use datalink to send the target information to attack aircraft. Enabled by default.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskFAC_EngageGroup( AttackGroup, Priority, WeaponType, Designation, Datalink )
- self:F2( { self.ControllableName, AttackGroup, WeaponType, Priority, Designation, Datalink } )
-
--- FAC_EngageControllable = {
--- id = 'FAC_EngageControllable',
--- params = {
--- groupId = Group.ID,
--- weaponType = number,
--- designation = enum AI.Task.Designation,
--- datalink = boolean,
--- priority = number,
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'FAC_EngageControllable',
- params = {
- groupId = AttackGroup:GetID(),
- weaponType = WeaponType,
- designation = Designation,
- datalink = Datalink,
- priority = Priority,
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
---- (AIR + GROUND) The task makes the controllable/unit a FAC and lets the FAC to choose a targets (enemy ground controllable) around as well as other assigned targets.
--- The killer is player-controlled allied CAS-aircraft that is in contact with the FAC.
--- If the task is assigned to the controllable lead unit will be a FAC.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Distance Radius The maximal distance from the FAC to a target.
--- @param #number Priority All en-route tasks have the priority parameter. This is a number (less value - higher priority) that determines actions related to what task will be performed first.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:EnRouteTaskFAC( Radius, Priority )
- self:F2( { self.ControllableName, Radius, Priority } )
-
--- FAC = {
--- id = 'FAC',
--- params = {
--- radius = Distance,
--- priority = number
--- }
--- }
-
- local DCSTask
- DCSTask = { id = 'FAC',
- params = {
- radius = Radius,
- priority = Priority
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
-
-
---- (AIR) Move the controllable to a Vec2 Point, wait for a defined duration and embark a controllable.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Point The point where to wait.
--- @param #number Duration The duration in seconds to wait.
--- @param #CONTROLLABLE EmbarkingControllable The controllable to be embarked.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure
-function CONTROLLABLE:TaskEmbarking( Point, Duration, EmbarkingControllable )
- self:F2( { self.ControllableName, Point, Duration, EmbarkingControllable.DCSControllable } )
-
- local DCSTask
- DCSTask = { id = 'Embarking',
- params = { x = Point.x,
- y = Point.y,
- duration = Duration,
- controllablesForEmbarking = { EmbarkingControllable.ControllableID },
- durationFlag = true,
- distributionFlag = false,
- distribution = {},
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- (GROUND) Embark to a Transport landed at a location.
-
---- Move to a defined Vec2 Point, and embark to a controllable when arrived within a defined Radius.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec2 Point The point where to wait.
--- @param #number Radius The radius of the embarking zone around the Point.
--- @return Dcs.DCSTasking.Task#Task The DCS task structure.
-function CONTROLLABLE:TaskEmbarkToTransport( Point, Radius )
- self:F2( { self.ControllableName, Point, Radius } )
-
- local DCSTask --Dcs.DCSTasking.Task#Task
- DCSTask = { id = 'EmbarkToTransport',
- params = { x = Point.x,
- y = Point.y,
- zoneRadius = Radius,
- }
- }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
-
-
---- (AIR + GROUND) Return a mission task from a mission template.
--- @param #CONTROLLABLE self
--- @param #table TaskMission A table containing the mission task.
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskMission( TaskMission )
- self:F2( Points )
-
- local DCSTask
- DCSTask = { id = 'Mission', params = { TaskMission, }, }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- Return a Misson task to follow a given route defined by Points.
--- @param #CONTROLLABLE self
--- @param #table Points A table of route points.
--- @return Dcs.DCSTasking.Task#Task
-function CONTROLLABLE:TaskRoute( Points )
- self:F2( Points )
-
- local DCSTask
- DCSTask = { id = 'Mission', params = { route = { points = Points, }, }, }
-
- self:T3( { DCSTask } )
- return DCSTask
-end
-
---- (AIR + GROUND) Make the Controllable move to fly to a given point.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec3 Point The destination point in Vec3 format.
--- @param #number Speed The speed to travel.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:RouteToVec2( Point, Speed )
- self:F2( { Point, Speed } )
-
- local ControllablePoint = self:GetUnit( 1 ):GetVec2()
-
- local PointFrom = {}
- PointFrom.x = ControllablePoint.x
- PointFrom.y = ControllablePoint.y
- PointFrom.type = "Turning Point"
- PointFrom.action = "Turning Point"
- PointFrom.speed = Speed
- PointFrom.speed_locked = true
- PointFrom.properties = {
- ["vnav"] = 1,
- ["scale"] = 0,
- ["angle"] = 0,
- ["vangle"] = 0,
- ["steer"] = 2,
- }
-
-
- local PointTo = {}
- PointTo.x = Point.x
- PointTo.y = Point.y
- PointTo.type = "Turning Point"
- PointTo.action = "Fly Over Point"
- PointTo.speed = Speed
- PointTo.speed_locked = true
- PointTo.properties = {
- ["vnav"] = 1,
- ["scale"] = 0,
- ["angle"] = 0,
- ["vangle"] = 0,
- ["steer"] = 2,
- }
-
-
- local Points = { PointFrom, PointTo }
-
- self:T3( Points )
-
- self:Route( Points )
-
- return self
-end
-
---- (AIR + GROUND) Make the Controllable move to a given point.
--- @param #CONTROLLABLE self
--- @param Dcs.DCSTypes#Vec3 Point The destination point in Vec3 format.
--- @param #number Speed The speed to travel.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:RouteToVec3( Point, Speed )
- self:F2( { Point, Speed } )
-
- local ControllableVec3 = self:GetUnit( 1 ):GetVec3()
-
- local PointFrom = {}
- PointFrom.x = ControllableVec3.x
- PointFrom.y = ControllableVec3.z
- PointFrom.alt = ControllableVec3.y
- PointFrom.alt_type = "BARO"
- PointFrom.type = "Turning Point"
- PointFrom.action = "Turning Point"
- PointFrom.speed = Speed
- PointFrom.speed_locked = true
- PointFrom.properties = {
- ["vnav"] = 1,
- ["scale"] = 0,
- ["angle"] = 0,
- ["vangle"] = 0,
- ["steer"] = 2,
- }
-
-
- local PointTo = {}
- PointTo.x = Point.x
- PointTo.y = Point.z
- PointTo.alt = Point.y
- PointTo.alt_type = "BARO"
- PointTo.type = "Turning Point"
- PointTo.action = "Fly Over Point"
- PointTo.speed = Speed
- PointTo.speed_locked = true
- PointTo.properties = {
- ["vnav"] = 1,
- ["scale"] = 0,
- ["angle"] = 0,
- ["vangle"] = 0,
- ["steer"] = 2,
- }
-
-
- local Points = { PointFrom, PointTo }
-
- self:T3( Points )
-
- self:Route( Points )
-
- return self
-end
-
-
-
---- Make the controllable to follow a given route.
--- @param #CONTROLLABLE self
--- @param #table GoPoints A table of Route Points.
--- @return #CONTROLLABLE self
-function CONTROLLABLE:Route( GoPoints )
- self:F2( GoPoints )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
- local Points = routines.utils.deepCopy( GoPoints )
- local MissionTask = { id = 'Mission', params = { route = { points = Points, }, }, }
- local Controller = self:_GetController()
- --Controller.setTask( Controller, MissionTask )
- self.TaskScheduler:Schedule( Controller, Controller.setTask, { MissionTask }, 1 )
- return self
- end
-
- return nil
-end
-
-
-
---- (AIR + GROUND) Route the controllable to a given zone.
--- The controllable final destination point can be randomized.
--- A speed can be given in km/h.
--- A given formation can be given.
--- @param #CONTROLLABLE self
--- @param Core.Zone#ZONE Zone The zone where to route to.
--- @param #boolean Randomize Defines whether to target point gets randomized within the Zone.
--- @param #number Speed The speed.
--- @param Base#FORMATION Formation The formation string.
-function CONTROLLABLE:TaskRouteToZone( Zone, Randomize, Speed, Formation )
- self:F2( Zone )
-
- local DCSControllable = self:GetDCSObject()
-
- if DCSControllable then
-
- local ControllablePoint = self:GetVec2()
-
- local PointFrom = {}
- PointFrom.x = ControllablePoint.x
- PointFrom.y = ControllablePoint.y
- PointFrom.type = "Turning Point"
- PointFrom.action = Formation or "Cone"
- PointFrom.speed = 20 / 1.6
-
-
- local PointTo = {}
- local ZonePoint
-
- if Randomize then
- ZonePoint = Zone:GetRandomVec2()
- else
- ZonePoint = Zone:GetVec2()
- end
-
- PointTo.x = ZonePoint.x
- PointTo.y = ZonePoint.y
- PointTo.type = "Turning Point"
-
- if Formation then
- PointTo.action = Formation
- else
- PointTo.action = "Cone"
- end
-
- if Speed then
- PointTo.speed = Speed
- else
- PointTo.speed = 20 / 1.6
- end
-
- local Points = { PointFrom, PointTo }
-
- self:T3( Points )
-
- self:Route( Points )
-
- return self
- end
-
- return nil
-end
-
-
--- Commands
-
---- Do Script command
--- @param #CONTROLLABLE self
--- @param #string DoScript
--- @return #DCSCommand
-function CONTROLLABLE:CommandDoScript( DoScript )
-
- local DCSDoScript = {
- id = "Script",
- params = {
- command = DoScript,
- },
- }
-
- self:T3( DCSDoScript )
- return DCSDoScript
-end
-
-
---- Return the mission template of the controllable.
--- @param #CONTROLLABLE self
--- @return #table The MissionTemplate
--- TODO: Rework the method how to retrieve a template ...
-function CONTROLLABLE:GetTaskMission()
- self:F2( self.ControllableName )
-
- return routines.utils.deepCopy( _DATABASE.Templates.Controllables[self.ControllableName].Template )
-end
-
---- Return the mission route of the controllable.
--- @param #CONTROLLABLE self
--- @return #table The mission route defined by points.
-function CONTROLLABLE:GetTaskRoute()
- self:F2( self.ControllableName )
-
- return routines.utils.deepCopy( _DATABASE.Templates.Controllables[self.ControllableName].Template.route.points )
-end
-
-
-
---- Return the route of a controllable by using the @{Database#DATABASE} class.
--- @param #CONTROLLABLE self
--- @param #number Begin The route point from where the copy will start. The base route point is 0.
--- @param #number End The route point where the copy will end. The End point is the last point - the End point. The last point has base 0.
--- @param #boolean Randomize Randomization of the route, when true.
--- @param #number Radius When randomization is on, the randomization is within the radius.
-function CONTROLLABLE:CopyRoute( Begin, End, Randomize, Radius )
- self:F2( { Begin, End } )
-
- local Points = {}
-
- -- Could be a Spawned Controllable
- local ControllableName = string.match( self:GetName(), ".*#" )
- if ControllableName then
- ControllableName = ControllableName:sub( 1, -2 )
- else
- ControllableName = self:GetName()
- end
-
- self:T3( { ControllableName } )
-
- local Template = _DATABASE.Templates.Controllables[ControllableName].Template
-
- if Template then
- if not Begin then
- Begin = 0
- end
- if not End then
- End = 0
- end
-
- for TPointID = Begin + 1, #Template.route.points - End do
- if Template.route.points[TPointID] then
- Points[#Points+1] = routines.utils.deepCopy( Template.route.points[TPointID] )
- if Randomize then
- if not Radius then
- Radius = 500
- end
- Points[#Points].x = Points[#Points].x + math.random( Radius * -1, Radius )
- Points[#Points].y = Points[#Points].y + math.random( Radius * -1, Radius )
- end
- end
- end
- return Points
- else
- error( "Template not found for Controllable : " .. ControllableName )
- end
-
- return nil
-end
-
-
---- Return the detected targets of the controllable.
--- The optional parametes specify the detection methods that can be applied.
--- If no detection method is given, the detection will use all the available methods by default.
--- @param Wrapper.Controllable#CONTROLLABLE self
--- @param #boolean DetectVisual (optional)
--- @param #boolean DetectOptical (optional)
--- @param #boolean DetectRadar (optional)
--- @param #boolean DetectIRST (optional)
--- @param #boolean DetectRWR (optional)
--- @param #boolean DetectDLINK (optional)
--- @return #table DetectedTargets
-function CONTROLLABLE:GetDetectedTargets( DetectVisual, DetectOptical, DetectRadar, DetectIRST, DetectRWR, DetectDLINK )
- self:F2( self.ControllableName )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local DetectionVisual = ( DetectVisual and DetectVisual == true ) and Controller.Detection.VISUAL or nil
- local DetectionOptical = ( DetectOptical and DetectOptical == true ) and Controller.Detection.OPTICAL or nil
- local DetectionRadar = ( DetectRadar and DetectRadar == true ) and Controller.Detection.RADAR or nil
- local DetectionIRST = ( DetectIRST and DetectIRST == true ) and Controller.Detection.IRST or nil
- local DetectionRWR = ( DetectRWR and DetectRWR == true ) and Controller.Detection.RWR or nil
- local DetectionDLINK = ( DetectDLINK and DetectDLINK == true ) and Controller.Detection.DLINK or nil
-
-
- return self:_GetController():getDetectedTargets( DetectionVisual, DetectionOptical, DetectionRadar, DetectionIRST, DetectionRWR, DetectionDLINK )
- end
-
- return nil
-end
-
-function CONTROLLABLE:IsTargetDetected( DCSObject )
- self:F2( self.ControllableName )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
-
- local TargetIsDetected, TargetIsVisible, TargetLastTime, TargetKnowType, TargetKnowDistance, TargetLastPos, TargetLastVelocity
- = self:_GetController().isTargetDetected( self:_GetController(), DCSObject,
- Controller.Detection.VISUAL,
- Controller.Detection.OPTIC,
- Controller.Detection.RADAR,
- Controller.Detection.IRST,
- Controller.Detection.RWR,
- Controller.Detection.DLINK
- )
- return TargetIsDetected, TargetIsVisible, TargetLastTime, TargetKnowType, TargetKnowDistance, TargetLastPos, TargetLastVelocity
- end
-
- return nil
-end
-
--- Options
-
---- Can the CONTROLLABLE hold their weapons?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROEHoldFirePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() or self:IsGround() or self:IsShip() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
---- Holding weapons.
--- @param Wrapper.Controllable#CONTROLLABLE self
--- @return Wrapper.Controllable#CONTROLLABLE self
-function CONTROLLABLE:OptionROEHoldFire()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.WEAPON_HOLD )
- elseif self:IsGround() then
- Controller:setOption( AI.Option.Ground.id.ROE, AI.Option.Ground.val.ROE.WEAPON_HOLD )
- elseif self:IsShip() then
- Controller:setOption( AI.Option.Naval.id.ROE, AI.Option.Naval.val.ROE.WEAPON_HOLD )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE attack returning on enemy fire?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROEReturnFirePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() or self:IsGround() or self:IsShip() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
---- Return fire.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROEReturnFire()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.RETURN_FIRE )
- elseif self:IsGround() then
- Controller:setOption( AI.Option.Ground.id.ROE, AI.Option.Ground.val.ROE.RETURN_FIRE )
- elseif self:IsShip() then
- Controller:setOption( AI.Option.Naval.id.ROE, AI.Option.Naval.val.ROE.RETURN_FIRE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE attack designated targets?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROEOpenFirePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() or self:IsGround() or self:IsShip() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
---- Openfire.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROEOpenFire()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.OPEN_FIRE )
- elseif self:IsGround() then
- Controller:setOption( AI.Option.Ground.id.ROE, AI.Option.Ground.val.ROE.OPEN_FIRE )
- elseif self:IsShip() then
- Controller:setOption( AI.Option.Naval.id.ROE, AI.Option.Naval.val.ROE.OPEN_FIRE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE attack targets of opportunity?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROEWeaponFreePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
---- Weapon free.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROEWeaponFree()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.ROE, AI.Option.Air.val.ROE.WEAPON_FREE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE ignore enemy fire?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROTNoReactionPossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
-
---- No evasion on enemy threats.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROTNoReaction()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.NO_REACTION )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE evade using passive defenses?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROTPassiveDefensePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
---- Evasion passive defense.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROTPassiveDefense()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.PASSIVE_DEFENCE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE evade on enemy fire?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROTEvadeFirePossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
-
---- Evade on fire.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROTEvadeFire()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.EVADE_FIRE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Can the CONTROLLABLE evade on fire using vertical manoeuvres?
--- @param #CONTROLLABLE self
--- @return #boolean
-function CONTROLLABLE:OptionROTVerticalPossible()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- if self:IsAir() then
- return true
- end
-
- return false
- end
-
- return nil
-end
-
-
---- Evade on fire using vertical manoeuvres.
--- @param #CONTROLLABLE self
--- @return #CONTROLLABLE self
-function CONTROLLABLE:OptionROTVertical()
- self:F2( { self.ControllableName } )
-
- local DCSControllable = self:GetDCSObject()
- if DCSControllable then
- local Controller = self:_GetController()
-
- if self:IsAir() then
- Controller:setOption( AI.Option.Air.id.REACTION_ON_THREAT, AI.Option.Air.val.REACTION_ON_THREAT.BYPASS_AND_ESCAPE )
- end
-
- return self
- end
-
- return nil
-end
-
---- Retrieve the controllable mission and allow to place function hooks within the mission waypoint plan.
--- Use the method @{Controllable#CONTROLLABLE:WayPointFunction} to define the hook functions for specific waypoints.
--- Use the method @{Controllable@CONTROLLABLE:WayPointExecute) to start the execution of the new mission plan.
--- Note that when WayPointInitialize is called, the Mission of the controllable is RESTARTED!
--- @param #CONTROLLABLE self
--- @param #table WayPoints If WayPoints is given, then use the route.
--- @return #CONTROLLABLE
-function CONTROLLABLE:WayPointInitialize( WayPoints )
- self:F( { WayPoints } )
-
- if WayPoints then
- self.WayPoints = WayPoints
- else
- self.WayPoints = self:GetTaskRoute()
- end
-
- return self
-end
-
---- Get the current WayPoints set with the WayPoint functions( Note that the WayPoints can be nil, although there ARE waypoints).
--- @param #CONTROLLABLE self
--- @return #table WayPoints If WayPoints is given, then return the WayPoints structure.
-function CONTROLLABLE:GetWayPoints()
- self:F( )
-
- if self.WayPoints then
- return self.WayPoints
- end
-
- return nil
-end
-
---- Registers a waypoint function that will be executed when the controllable moves over the WayPoint.
--- @param #CONTROLLABLE self
--- @param #number WayPoint The waypoint number. Note that the start waypoint on the route is WayPoint 1!
--- @param #number WayPointIndex When defining multiple WayPoint functions for one WayPoint, use WayPointIndex to set the sequence of actions.
--- @param #function WayPointFunction The waypoint function to be called when the controllable moves over the waypoint. The waypoint function takes variable parameters.
--- @return #CONTROLLABLE
-function CONTROLLABLE:WayPointFunction( WayPoint, WayPointIndex, WayPointFunction, ... )
- self:F2( { WayPoint, WayPointIndex, WayPointFunction } )
-
- table.insert( self.WayPoints[WayPoint].task.params.tasks, WayPointIndex )
- self.WayPoints[WayPoint].task.params.tasks[WayPointIndex] = self:TaskFunction( WayPoint, WayPointIndex, WayPointFunction, arg )
- return self
-end
-
-
-function CONTROLLABLE:TaskFunction( WayPoint, WayPointIndex, FunctionString, FunctionArguments )
- self:F2( { WayPoint, WayPointIndex, FunctionString, FunctionArguments } )
-
- local DCSTask
-
- local DCSScript = {}
- DCSScript[#DCSScript+1] = "local MissionControllable = GROUP:Find( ... ) "
-
- if FunctionArguments and #FunctionArguments > 0 then
- DCSScript[#DCSScript+1] = FunctionString .. "( MissionControllable, " .. table.concat( FunctionArguments, "," ) .. ")"
- else
- DCSScript[#DCSScript+1] = FunctionString .. "( MissionControllable )"
- end
-
- DCSTask = self:TaskWrappedAction(
- self:CommandDoScript(
- table.concat( DCSScript )
- ), WayPointIndex
- )
-
- self:T3( DCSTask )
-
- return DCSTask
-
-end
-
---- Executes the WayPoint plan.
--- The function gets a WayPoint parameter, that you can use to restart the mission at a specific WayPoint.
--- Note that when the WayPoint parameter is used, the new start mission waypoint of the controllable will be 1!
--- @param #CONTROLLABLE self
--- @param #number WayPoint The WayPoint from where to execute the mission.
--- @param #number WaitTime The amount seconds to wait before initiating the mission.
--- @return #CONTROLLABLE
-function CONTROLLABLE:WayPointExecute( WayPoint, WaitTime )
- self:F( { WayPoint, WaitTime } )
-
- if not WayPoint then
- WayPoint = 1
- end
-
- -- When starting the mission from a certain point, the TaskPoints need to be deleted before the given WayPoint.
- for TaskPointID = 1, WayPoint - 1 do
- table.remove( self.WayPoints, 1 )
- end
-
- self:T3( self.WayPoints )
-
- self:SetTask( self:TaskRoute( self.WayPoints ), WaitTime )
-
- return self
-end
-
--- Message APIs--- **Wrapper** -- GROUP is a wrapper class for the DCS Class Group.
---
--- ===
---
--- The @{#GROUP} class is a wrapper class to handle the DCS Group objects:
---
--- * Support all DCS Group APIs.
--- * Enhance with Group specific APIs not in the DCS Group API set.
--- * Handle local Group Controller.
--- * Manage the "state" of the DCS Group.
---
--- **IMPORTANT: ONE SHOULD NEVER SANATIZE these GROUP OBJECT REFERENCES! (make the GROUP object references nil).**
---
--- See the detailed documentation on the GROUP class.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-03-26: GROUP:**RouteRTB( RTBAirbase, Speed )** added.
---
--- 2017-03-07: GROUP:**HandleEvent( Event, EventFunction )** added.
--- 2017-03-07: GROUP:**UnHandleEvent( Event )** added.
---
--- 2017-01-24: GROUP:**SetAIOnOff( AIOnOff )** added.
---
--- 2017-01-24: GROUP:**SetAIOn()** added.
---
--- 2017-01-24: GROUP:**SetAIOff()** added.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * [**Entropy**](https://forums.eagle.ru/member.php?u=111471), **Afinegan**: Came up with the requirement for AIOnOff().
---
--- ### Authors:
---
--- * **FlightControl**: Design & Programming
---
--- @module Group
--- @author FlightControl
-
---- @type GROUP
--- @extends Wrapper.Controllable#CONTROLLABLE
--- @field #string GroupName The name of the group.
-
----
--- # GROUP class, extends @{Controllable#CONTROLLABLE}
---
--- For each DCS Group object alive within a running mission, a GROUP wrapper object (instance) will be created within the _@{DATABASE} object.
--- This is done at the beginning of the mission (when the mission starts), and dynamically when new DCS Group objects are spawned (using the @{SPAWN} class).
---
--- The GROUP class does not contain a :New() method, rather it provides :Find() methods to retrieve the object reference
--- using the DCS Group or the DCS GroupName.
---
--- Another thing to know is that GROUP objects do not "contain" the DCS Group object.
--- The GROUP methods will reference the DCS Group object by name when it is needed during API execution.
--- If the DCS Group object does not exist or is nil, the GROUP methods will return nil and log an exception in the DCS.log file.
---
--- The GROUP class provides the following functions to retrieve quickly the relevant GROUP instance:
---
--- * @{#GROUP.Find}(): Find a GROUP instance from the _DATABASE object using a DCS Group object.
--- * @{#GROUP.FindByName}(): Find a GROUP instance from the _DATABASE object using a DCS Group name.
---
--- ## GROUP task methods
---
--- A GROUP is a @{Controllable}. See the @{Controllable} task methods section for a description of the task methods.
---
--- ### Obtain the mission from group templates
---
--- Group templates contain complete mission descriptions. Sometimes you want to copy a complete mission from a group and assign it to another:
---
--- * @{Controllable#CONTROLLABLE.TaskMission}: (AIR + GROUND) Return a mission task from a mission template.
---
--- ## GROUP Command methods
---
--- A GROUP is a @{Controllable}. See the @{Controllable} command methods section for a description of the command methods.
---
--- ## GROUP option methods
---
--- A GROUP is a @{Controllable}. See the @{Controllable} option methods section for a description of the option methods.
---
--- ## GROUP Zone validation methods
---
--- The group can be validated whether it is completely, partly or not within a @{Zone}.
--- Use the following Zone validation methods on the group:
---
--- * @{#GROUP.IsCompletelyInZone}: Returns true if all units of the group are within a @{Zone}.
--- * @{#GROUP.IsPartlyInZone}: Returns true if some units of the group are within a @{Zone}.
--- * @{#GROUP.IsNotInZone}: Returns true if none of the group units of the group are within a @{Zone}.
---
--- The zone can be of any @{Zone} class derived from @{Zone#ZONE_BASE}. So, these methods are polymorphic to the zones tested on.
---
--- ## GROUP AI methods
---
--- A GROUP has AI methods to control the AI activation.
---
--- * @{#GROUP.SetAIOnOff}(): Turns the GROUP AI On or Off.
--- * @{#GROUP.SetAIOn}(): Turns the GROUP AI On.
--- * @{#GROUP.SetAIOff}(): Turns the GROUP AI Off.
---
--- @field #GROUP GROUP
-GROUP = {
- ClassName = "GROUP",
-}
-
---- Create a new GROUP from a DCSGroup
--- @param #GROUP self
--- @param Dcs.DCSWrapper.Group#Group GroupName The DCS Group name
--- @return #GROUP self
-function GROUP:Register( GroupName )
- self = BASE:Inherit( self, CONTROLLABLE:New( GroupName ) )
- self:F2( GroupName )
- self.GroupName = GroupName
-
- self:SetEventPriority( 4 )
- return self
-end
-
--- Reference methods.
-
---- Find the GROUP wrapper class instance using the DCS Group.
--- @param #GROUP self
--- @param Dcs.DCSWrapper.Group#Group DCSGroup The DCS Group.
--- @return #GROUP The GROUP.
-function GROUP:Find( DCSGroup )
-
- local GroupName = DCSGroup:getName() -- Wrapper.Group#GROUP
- local GroupFound = _DATABASE:FindGroup( GroupName )
- return GroupFound
-end
-
---- Find the created GROUP using the DCS Group Name.
--- @param #GROUP self
--- @param #string GroupName The DCS Group Name.
--- @return #GROUP The GROUP.
-function GROUP:FindByName( GroupName )
-
- local GroupFound = _DATABASE:FindGroup( GroupName )
- return GroupFound
-end
-
--- DCS Group methods support.
-
---- Returns the DCS Group.
--- @param #GROUP self
--- @return Dcs.DCSWrapper.Group#Group The DCS Group.
-function GROUP:GetDCSObject()
- local DCSGroup = Group.getByName( self.GroupName )
-
- if DCSGroup then
- return DCSGroup
- end
-
- return nil
-end
-
---- Returns the @{DCSTypes#Position3} position vectors indicating the point and direction vectors in 3D of the POSITIONABLE within the mission.
--- @param Wrapper.Positionable#POSITIONABLE self
--- @return Dcs.DCSTypes#Position The 3D position vectors of the POSITIONABLE.
--- @return #nil The POSITIONABLE is not existing or alive.
-function GROUP:GetPositionVec3() -- Overridden from POSITIONABLE:GetPositionVec3()
- self:F2( self.PositionableName )
-
- local DCSPositionable = self:GetDCSObject()
-
- if DCSPositionable then
- local PositionablePosition = DCSPositionable:getUnits()[1]:getPosition().p
- self:T3( PositionablePosition )
- return PositionablePosition
- end
-
- return nil
-end
-
---- Returns if the Group is alive.
--- The Group must:
---
--- * Exist at run-time.
--- * Has at least one unit.
---
--- When the first @{Unit} of the Group is active, it will return true.
--- If the first @{Unit} of the Group is inactive, it will return false.
---
--- @param #GROUP self
--- @return #boolean true if the Group is alive and active.
--- @return #boolean false if the Group is alive but inactive.
--- @return #nil if the group does not exist anymore.
-function GROUP:IsAlive()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject() -- Dcs.DCSGroup#Group
-
- if DCSGroup then
- if DCSGroup:isExist() then
- local DCSUnit = DCSGroup:getUnit(1) -- Dcs.DCSUnit#Unit
- if DCSUnit then
- local GroupIsAlive = DCSUnit:isActive()
- self:T3( GroupIsAlive )
- return GroupIsAlive
- end
- end
- end
-
- return nil
-end
-
---- Destroys the DCS Group and all of its DCS Units.
--- Note that this destroy method also raises a destroy event at run-time.
--- So all event listeners will catch the destroy event of this DCS Group.
--- @param #GROUP self
-function GROUP:Destroy()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- for Index, UnitData in pairs( DCSGroup:getUnits() ) do
- self:CreateEventCrash( timer.getTime(), UnitData )
- end
- DCSGroup:destroy()
- DCSGroup = nil
- end
-
- return nil
-end
-
---- Returns category of the DCS Group.
--- @param #GROUP self
--- @return Dcs.DCSWrapper.Group#Group.Category The category ID
-function GROUP:GetCategory()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
- if DCSGroup then
- local GroupCategory = DCSGroup:getCategory()
- self:T3( GroupCategory )
- return GroupCategory
- end
-
- return nil
-end
-
---- Returns the category name of the #GROUP.
--- @param #GROUP self
--- @return #string Category name = Helicopter, Airplane, Ground Unit, Ship
-function GROUP:GetCategoryName()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
- if DCSGroup then
- local CategoryNames = {
- [Group.Category.AIRPLANE] = "Airplane",
- [Group.Category.HELICOPTER] = "Helicopter",
- [Group.Category.GROUND] = "Ground Unit",
- [Group.Category.SHIP] = "Ship",
- }
- local GroupCategory = DCSGroup:getCategory()
- self:T3( GroupCategory )
-
- return CategoryNames[GroupCategory]
- end
-
- return nil
-end
-
-
---- Returns the coalition of the DCS Group.
--- @param #GROUP self
--- @return Dcs.DCSCoalitionWrapper.Object#coalition.side The coalition side of the DCS Group.
-function GROUP:GetCoalition()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
- if DCSGroup then
- local GroupCoalition = DCSGroup:getCoalition()
- self:T3( GroupCoalition )
- return GroupCoalition
- end
-
- return nil
-end
-
---- Returns the country of the DCS Group.
--- @param #GROUP self
--- @return Dcs.DCScountry#country.id The country identifier.
--- @return #nil The DCS Group is not existing or alive.
-function GROUP:GetCountry()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
- if DCSGroup then
- local GroupCountry = DCSGroup:getUnit(1):getCountry()
- self:T3( GroupCountry )
- return GroupCountry
- end
-
- return nil
-end
-
---- Returns the UNIT wrapper class with number UnitNumber.
--- If the underlying DCS Unit does not exist, the method will return nil. .
--- @param #GROUP self
--- @param #number UnitNumber The number of the UNIT wrapper class to be returned.
--- @return Wrapper.Unit#UNIT The UNIT wrapper class.
-function GROUP:GetUnit( UnitNumber )
- self:F2( { self.GroupName, UnitNumber } )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local UnitFound = UNIT:Find( DCSGroup:getUnit( UnitNumber ) )
- self:T2( UnitFound )
- return UnitFound
- end
-
- return nil
-end
-
---- Returns the DCS Unit with number UnitNumber.
--- If the underlying DCS Unit does not exist, the method will return nil. .
--- @param #GROUP self
--- @param #number UnitNumber The number of the DCS Unit to be returned.
--- @return Dcs.DCSWrapper.Unit#Unit The DCS Unit.
-function GROUP:GetDCSUnit( UnitNumber )
- self:F2( { self.GroupName, UnitNumber } )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local DCSUnitFound = DCSGroup:getUnit( UnitNumber )
- self:T3( DCSUnitFound )
- return DCSUnitFound
- end
-
- return nil
-end
-
---- Returns current size of the DCS Group.
--- If some of the DCS Units of the DCS Group are destroyed the size of the DCS Group is changed.
--- @param #GROUP self
--- @return #number The DCS Group size.
-function GROUP:GetSize()
- self:F2( { self.GroupName } )
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupSize = DCSGroup:getSize()
- self:T3( GroupSize )
- return GroupSize
- end
-
- return nil
-end
-
----
---- Returns the initial size of the DCS Group.
--- If some of the DCS Units of the DCS Group are destroyed, the initial size of the DCS Group is unchanged.
--- @param #GROUP self
--- @return #number The DCS Group initial size.
-function GROUP:GetInitialSize()
- self:F2( { self.GroupName } )
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupInitialSize = DCSGroup:getInitialSize()
- self:T3( GroupInitialSize )
- return GroupInitialSize
- end
-
- return nil
-end
-
-
---- Returns the DCS Units of the DCS Group.
--- @param #GROUP self
--- @return #table The DCS Units.
-function GROUP:GetDCSUnits()
- self:F2( { self.GroupName } )
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local DCSUnits = DCSGroup:getUnits()
- self:T3( DCSUnits )
- return DCSUnits
- end
-
- return nil
-end
-
-
---- Activates a GROUP.
--- @param #GROUP self
-function GROUP:Activate()
- self:F2( { self.GroupName } )
- trigger.action.activateGroup( self:GetDCSObject() )
- return self:GetDCSObject()
-end
-
-
---- Gets the type name of the group.
--- @param #GROUP self
--- @return #string The type name of the group.
-function GROUP:GetTypeName()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupTypeName = DCSGroup:getUnit(1):getTypeName()
- self:T3( GroupTypeName )
- return( GroupTypeName )
- end
-
- return nil
-end
-
---- Gets the CallSign of the first DCS Unit of the DCS Group.
--- @param #GROUP self
--- @return #string The CallSign of the first DCS Unit of the DCS Group.
-function GROUP:GetCallsign()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupCallSign = DCSGroup:getUnit(1):getCallsign()
- self:T3( GroupCallSign )
- return GroupCallSign
- end
-
- return nil
-end
-
---- Returns the current point (Vec2 vector) of the first DCS Unit in the DCS Group.
--- @param #GROUP self
--- @return Dcs.DCSTypes#Vec2 Current Vec2 point of the first DCS Unit of the DCS Group.
-function GROUP:GetVec2()
- self:F2( self.GroupName )
-
- local UnitPoint = self:GetUnit(1)
- UnitPoint:GetVec2()
- local GroupPointVec2 = UnitPoint:GetVec2()
- self:T3( GroupPointVec2 )
- return GroupPointVec2
-end
-
---- Returns the current Vec3 vector of the first DCS Unit in the GROUP.
--- @param #GROUP self
--- @return Dcs.DCSTypes#Vec3 Current Vec3 of the first DCS Unit of the GROUP.
-function GROUP:GetVec3()
- self:F2( self.GroupName )
-
- local GroupVec3 = self:GetUnit(1):GetVec3()
- self:T3( GroupVec3 )
- return GroupVec3
-end
-
---- Returns a POINT_VEC2 object indicating the point in 2D of the first UNIT of the GROUP within the mission.
--- @param #GROUP self
--- @return Core.Point#POINT_VEC2 The 2D point vector of the first DCS Unit of the GROUP.
--- @return #nil The first UNIT is not existing or alive.
-function GROUP:GetPointVec2()
- self:F2(self.GroupName)
-
- local FirstUnit = self:GetUnit(1)
-
- if FirstUnit then
- local FirstUnitPointVec2 = FirstUnit:GetPointVec2()
- self:T3(FirstUnitPointVec2)
- return FirstUnitPointVec2
- end
-
- return nil
-end
-
---- Returns a random @{DCSTypes#Vec3} vector (point in 3D of the UNIT within the mission) within a range around the first UNIT of the GROUP.
--- @param #GROUP self
--- @param #number Radius
--- @return Dcs.DCSTypes#Vec3 The random 3D point vector around the first UNIT of the GROUP.
--- @return #nil The GROUP is invalid or empty
--- @usage
--- -- If Radius is ignored, returns the Dcs.DCSTypes#Vec3 of first UNIT of the GROUP
-function GROUP:GetRandomVec3(Radius)
- self:F2(self.GroupName)
-
- local FirstUnit = self:GetUnit(1)
-
- if FirstUnit then
- local FirstUnitRandomPointVec3 = FirstUnit:GetRandomVec3(Radius)
- self:T3(FirstUnitRandomPointVec3)
- return FirstUnitRandomPointVec3
- end
-
- return nil
-end
-
---- Returns the mean heading of every UNIT in the GROUP in degrees
--- @param #GROUP self
--- @return #number mean heading of the GROUP
--- @return #nil The first UNIT is not existing or alive.
-function GROUP:GetHeading()
- self:F2(self.GroupName)
-
- local GroupSize = self:GetSize()
- local HeadingAccumulator = 0
-
- if GroupSize then
- for i = 1, GroupSize do
- HeadingAccumulator = HeadingAccumulator + self:GetUnit(i):GetHeading()
- end
- return math.floor(HeadingAccumulator / GroupSize)
- end
-
- return nil
-
-end
-
-do -- Is Zone methods
-
---- Returns true if all units of the group are within a @{Zone}.
--- @param #GROUP self
--- @param Core.Zone#ZONE_BASE Zone The zone to test.
--- @return #boolean Returns true if the Group is completely within the @{Zone#ZONE_BASE}
-function GROUP:IsCompletelyInZone( Zone )
- self:F2( { self.GroupName, Zone } )
-
- for UnitID, UnitData in pairs( self:GetUnits() ) do
- local Unit = UnitData -- Wrapper.Unit#UNIT
- if Zone:IsVec3InZone( Unit:GetVec3() ) then
- else
- return false
- end
- end
-
- return true
-end
-
---- Returns true if some units of the group are within a @{Zone}.
--- @param #GROUP self
--- @param Core.Zone#ZONE_BASE Zone The zone to test.
--- @return #boolean Returns true if the Group is partially within the @{Zone#ZONE_BASE}
-function GROUP:IsPartlyInZone( Zone )
- self:F2( { self.GroupName, Zone } )
-
- local PartlyInZone = false
-
- for UnitID, UnitData in pairs( self:GetUnits() ) do
- local Unit = UnitData -- Wrapper.Unit#UNIT
- if Zone:IsVec3InZone( Unit:GetVec3() ) then
- PartlyInZone = true
- else
- -- So, if there were groups in the zone found, and suddenly one NOT in the zone,
- -- then the group is partialy in the zone :-)
- if PartlyInZone == true then
- return true
- end
- end
- end
-
- return false
-end
-
---- Returns true if none of the group units of the group are within a @{Zone}.
--- @param #GROUP self
--- @param Core.Zone#ZONE_BASE Zone The zone to test.
--- @return #boolean Returns true if the Group is not within the @{Zone#ZONE_BASE}
-function GROUP:IsNotInZone( Zone )
- self:F2( { self.GroupName, Zone } )
-
- for UnitID, UnitData in pairs( self:GetUnits() ) do
- local Unit = UnitData -- Wrapper.Unit#UNIT
- if Zone:IsVec3InZone( Unit:GetVec3() ) then
- return false
- end
- end
-
- return true
-end
-
---- Returns if the group is of an air category.
--- If the group is a helicopter or a plane, then this method will return true, otherwise false.
--- @param #GROUP self
--- @return #boolean Air category evaluation result.
-function GROUP:IsAir()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local IsAirResult = DCSGroup:getCategory() == Group.Category.AIRPLANE or DCSGroup:getCategory() == Group.Category.HELICOPTER
- self:T3( IsAirResult )
- return IsAirResult
- end
-
- return nil
-end
-
---- Returns if the DCS Group contains Helicopters.
--- @param #GROUP self
--- @return #boolean true if DCS Group contains Helicopters.
-function GROUP:IsHelicopter()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupCategory = DCSGroup:getCategory()
- self:T2( GroupCategory )
- return GroupCategory == Group.Category.HELICOPTER
- end
-
- return nil
-end
-
---- Returns if the DCS Group contains AirPlanes.
--- @param #GROUP self
--- @return #boolean true if DCS Group contains AirPlanes.
-function GROUP:IsAirPlane()
- self:F2()
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupCategory = DCSGroup:getCategory()
- self:T2( GroupCategory )
- return GroupCategory == Group.Category.AIRPLANE
- end
-
- return nil
-end
-
---- Returns if the DCS Group contains Ground troops.
--- @param #GROUP self
--- @return #boolean true if DCS Group contains Ground troops.
-function GROUP:IsGround()
- self:F2()
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupCategory = DCSGroup:getCategory()
- self:T2( GroupCategory )
- return GroupCategory == Group.Category.GROUND
- end
-
- return nil
-end
-
---- Returns if the DCS Group contains Ships.
--- @param #GROUP self
--- @return #boolean true if DCS Group contains Ships.
-function GROUP:IsShip()
- self:F2()
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupCategory = DCSGroup:getCategory()
- self:T2( GroupCategory )
- return GroupCategory == Group.Category.SHIP
- end
-
- return nil
-end
-
---- Returns if all units of the group are on the ground or landed.
--- If all units of this group are on the ground, this function will return true, otherwise false.
--- @param #GROUP self
--- @return #boolean All units on the ground result.
-function GROUP:AllOnGround()
- self:F2()
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local AllOnGroundResult = true
-
- for Index, UnitData in pairs( DCSGroup:getUnits() ) do
- if UnitData:inAir() then
- AllOnGroundResult = false
- end
- end
-
- self:T3( AllOnGroundResult )
- return AllOnGroundResult
- end
-
- return nil
-end
-
-end
-
-do -- AI methods
-
- --- Turns the AI On or Off for the GROUP.
- -- @param #GROUP self
- -- @param #boolean AIOnOff The value true turns the AI On, the value false turns the AI Off.
- -- @return #GROUP The GROUP.
- function GROUP:SetAIOnOff( AIOnOff )
-
- local DCSGroup = self:GetDCSObject() -- Dcs.DCSGroup#Group
-
- if DCSGroup then
- local DCSController = DCSGroup:getController() -- Dcs.DCSController#Controller
- if DCSController then
- DCSController:setOnOff( AIOnOff )
- return self
- end
- end
-
- return nil
- end
-
- --- Turns the AI On for the GROUP.
- -- @param #GROUP self
- -- @return #GROUP The GROUP.
- function GROUP:SetAIOn()
-
- return self:SetAIOnOff( true )
- end
-
- --- Turns the AI Off for the GROUP.
- -- @param #GROUP self
- -- @return #GROUP The GROUP.
- function GROUP:SetAIOff()
-
- return self:SetAIOnOff( false )
- end
-
-end
-
-
-
---- Returns the current maximum velocity of the group.
--- Each unit within the group gets evaluated, and the maximum velocity (= the unit which is going the fastest) is returned.
--- @param #GROUP self
--- @return #number Maximum velocity found.
-function GROUP:GetMaxVelocity()
- self:F2()
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local GroupVelocityMax = 0
-
- for Index, UnitData in pairs( DCSGroup:getUnits() ) do
-
- local UnitVelocityVec3 = UnitData:getVelocity()
- local UnitVelocity = math.abs( UnitVelocityVec3.x ) + math.abs( UnitVelocityVec3.y ) + math.abs( UnitVelocityVec3.z )
-
- if UnitVelocity > GroupVelocityMax then
- GroupVelocityMax = UnitVelocity
- end
- end
-
- return GroupVelocityMax
- end
-
- return nil
-end
-
---- Returns the current minimum height of the group.
--- Each unit within the group gets evaluated, and the minimum height (= the unit which is the lowest elevated) is returned.
--- @param #GROUP self
--- @return #number Minimum height found.
-function GROUP:GetMinHeight()
- self:F2()
-
-end
-
---- Returns the current maximum height of the group.
--- Each unit within the group gets evaluated, and the maximum height (= the unit which is the highest elevated) is returned.
--- @param #GROUP self
--- @return #number Maximum height found.
-function GROUP:GetMaxHeight()
- self:F2()
-
-end
-
--- SPAWNING
-
---- Respawn the @{GROUP} using a (tweaked) template of the Group.
--- The template must be retrieved with the @{Group#GROUP.GetTemplate}() function.
--- The template contains all the definitions as declared within the mission file.
--- To understand templates, do the following:
---
--- * unpack your .miz file into a directory using 7-zip.
--- * browse in the directory created to the file **mission**.
--- * open the file and search for the country group definitions.
---
--- Your group template will contain the fields as described within the mission file.
---
--- This function will:
---
--- * Get the current position and heading of the group.
--- * When the group is alive, it will tweak the template x, y and heading coordinates of the group and the embedded units to the current units positions.
--- * Then it will destroy the current alive group.
--- * And it will respawn the group using your new template definition.
--- @param Wrapper.Group#GROUP self
--- @param #table Template The template of the Group retrieved with GROUP:GetTemplate()
-function GROUP:Respawn( Template )
-
- local Vec3 = self:GetVec3()
- Template.x = Vec3.x
- Template.y = Vec3.z
- --Template.x = nil
- --Template.y = nil
-
- self:E( #Template.units )
- for UnitID, UnitData in pairs( self:GetUnits() ) do
- local GroupUnit = UnitData -- Wrapper.Unit#UNIT
- self:E( GroupUnit:GetName() )
- if GroupUnit:IsAlive() then
- local GroupUnitVec3 = GroupUnit:GetVec3()
- local GroupUnitHeading = GroupUnit:GetHeading()
- Template.units[UnitID].alt = GroupUnitVec3.y
- Template.units[UnitID].x = GroupUnitVec3.x
- Template.units[UnitID].y = GroupUnitVec3.z
- Template.units[UnitID].heading = GroupUnitHeading
- self:E( { UnitID, Template.units[UnitID], Template.units[UnitID] } )
- end
- end
-
- self:Destroy()
- _DATABASE:Spawn( Template )
-end
-
---- Returns the group template from the @{DATABASE} (_DATABASE object).
--- @param #GROUP self
--- @return #table
-function GROUP:GetTemplate()
- local GroupName = self:GetName()
- self:E( GroupName )
- return _DATABASE:GetGroupTemplate( GroupName )
-end
-
---- Sets the controlled status in a Template.
--- @param #GROUP self
--- @param #boolean Controlled true is controlled, false is uncontrolled.
--- @return #table
-function GROUP:SetTemplateControlled( Template, Controlled )
- Template.uncontrolled = not Controlled
- return Template
-end
-
---- Sets the CountryID of the group in a Template.
--- @param #GROUP self
--- @param Dcs.DCScountry#country.id CountryID The country ID.
--- @return #table
-function GROUP:SetTemplateCountry( Template, CountryID )
- Template.CountryID = CountryID
- return Template
-end
-
---- Sets the CoalitionID of the group in a Template.
--- @param #GROUP self
--- @param Dcs.DCSCoalitionWrapper.Object#coalition.side CoalitionID The coalition ID.
--- @return #table
-function GROUP:SetTemplateCoalition( Template, CoalitionID )
- Template.CoalitionID = CoalitionID
- return Template
-end
-
-
-
-
---- Return the mission template of the group.
--- @param #GROUP self
--- @return #table The MissionTemplate
-function GROUP:GetTaskMission()
- self:F2( self.GroupName )
-
- return routines.utils.deepCopy( _DATABASE.Templates.Groups[self.GroupName].Template )
-end
-
---- Return the mission route of the group.
--- @param #GROUP self
--- @return #table The mission route defined by points.
-function GROUP:GetTaskRoute()
- self:F2( self.GroupName )
-
- return routines.utils.deepCopy( _DATABASE.Templates.Groups[self.GroupName].Template.route.points )
-end
-
---- Return the route of a group by using the @{Database#DATABASE} class.
--- @param #GROUP self
--- @param #number Begin The route point from where the copy will start. The base route point is 0.
--- @param #number End The route point where the copy will end. The End point is the last point - the End point. The last point has base 0.
--- @param #boolean Randomize Randomization of the route, when true.
--- @param #number Radius When randomization is on, the randomization is within the radius.
-function GROUP:CopyRoute( Begin, End, Randomize, Radius )
- self:F2( { Begin, End } )
-
- local Points = {}
-
- -- Could be a Spawned Group
- local GroupName = string.match( self:GetName(), ".*#" )
- if GroupName then
- GroupName = GroupName:sub( 1, -2 )
- else
- GroupName = self:GetName()
- end
-
- self:T3( { GroupName } )
-
- local Template = _DATABASE.Templates.Groups[GroupName].Template
-
- if Template then
- if not Begin then
- Begin = 0
- end
- if not End then
- End = 0
- end
-
- for TPointID = Begin + 1, #Template.route.points - End do
- if Template.route.points[TPointID] then
- Points[#Points+1] = routines.utils.deepCopy( Template.route.points[TPointID] )
- if Randomize then
- if not Radius then
- Radius = 500
- end
- Points[#Points].x = Points[#Points].x + math.random( Radius * -1, Radius )
- Points[#Points].y = Points[#Points].y + math.random( Radius * -1, Radius )
- end
- end
- end
- return Points
- else
- error( "Template not found for Group : " .. GroupName )
- end
-
- return nil
-end
-
---- Calculate the maxium A2G threat level of the Group.
--- @param #GROUP self
-function GROUP:CalculateThreatLevelA2G()
-
- local MaxThreatLevelA2G = 0
- for UnitName, UnitData in pairs( self:GetUnits() ) do
- local ThreatUnit = UnitData -- Wrapper.Unit#UNIT
- local ThreatLevelA2G = ThreatUnit:GetThreatLevel()
- if ThreatLevelA2G > MaxThreatLevelA2G then
- MaxThreatLevelA2G = ThreatLevelA2G
- end
- end
-
- self:T3( MaxThreatLevelA2G )
- return MaxThreatLevelA2G
-end
-
---- Returns true if the first unit of the GROUP is in the air.
--- @param Wrapper.Group#GROUP self
--- @return #boolean true if in the first unit of the group is in the air.
--- @return #nil The GROUP is not existing or not alive.
-function GROUP:InAir()
- self:F2( self.GroupName )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
- local DCSUnit = DCSGroup:getUnit(1)
- if DCSUnit then
- local GroupInAir = DCSGroup:getUnit(1):inAir()
- self:T3( GroupInAir )
- return GroupInAir
- end
- end
-
- return nil
-end
-
-do -- Route methods
-
- --- (AIR) Return the Group to an @{Airbase#AIRBASE}.
- -- The following things are to be taken into account:
- --
- -- * The group is respawned to achieve the RTB, there may be side artefacts as a result of this. (Like weapons suddenly come back).
- -- * A group consisting out of more than one unit, may rejoin formation when respawned.
- -- * A speed can be given in km/h. If no speed is specified, the maximum speed of the first unit will be taken to return to base.
- -- * When there is no @{Airbase} object specified, the group will return to the home base if the route of the group is pinned at take-off or at landing to a base.
- -- * When there is no @{Airbase} object specified and the group route is not pinned to any airbase, it will return to the nearest airbase.
- --
- -- @param #GROUP self
- -- @param Wrapper.Airbase#AIRBASE RTBAirbase (optional) The @{Airbase} to return to. If blank, the controllable will return to the nearest friendly airbase.
- -- @param #number Speed (optional) The Speed, if no Speed is given, the maximum Speed of the first unit is selected.
- -- @return #GROUP
- function GROUP:RouteRTB( RTBAirbase, Speed )
- self:F2( { RTBAirbase, Speed } )
-
- local DCSGroup = self:GetDCSObject()
-
- if DCSGroup then
-
- if RTBAirbase then
-
- local GroupPoint = self:GetVec2()
- local GroupVelocity = self:GetUnit(1):GetDesc().speedMax
-
- local PointFrom = {}
- PointFrom.x = GroupPoint.x
- PointFrom.y = GroupPoint.y
- PointFrom.type = "Turning Point"
- PointFrom.action = "Turning Point"
- PointFrom.speed = GroupVelocity
-
-
- local PointTo = {}
- local AirbasePointVec2 = RTBAirbase:GetPointVec2()
- local AirbaseAirPoint = AirbasePointVec2:RoutePointAir(
- POINT_VEC3.RoutePointAltType.BARO,
- "Land",
- "Landing",
- Speed or self:GetUnit(1):GetDesc().speedMax
- )
-
- AirbaseAirPoint["airdromeId"] = RTBAirbase:GetID()
- AirbaseAirPoint["speed_locked"] = true,
-
- self:E(AirbaseAirPoint )
-
- local Points = { PointFrom, AirbaseAirPoint }
-
- self:T3( Points )
-
- local Template = self:GetTemplate()
- Template.route.points = Points
- self:Respawn( Template )
-
- self:Route( Points )
-
- self:Respawn(Template)
- else
- self:ClearTasks()
- end
- end
-
- return self
- end
-
-end
-
-function GROUP:OnReSpawn( ReSpawnFunction )
-
- self.ReSpawnFunction = ReSpawnFunction
-end
-
-do -- Event Handling
-
- --- Subscribe to a DCS Event.
- -- @param #GROUP self
- -- @param Core.Event#EVENTS Event
- -- @param #function EventFunction (optional) The function to be called when the event occurs for the GROUP.
- -- @return #GROUP
- function GROUP:HandleEvent( Event, EventFunction )
-
- self:EventDispatcher():OnEventForGroup( self:GetName(), EventFunction, self, Event )
-
- return self
- end
-
- --- UnSubscribe to a DCS event.
- -- @param #GROUP self
- -- @param Core.Event#EVENTS Event
- -- @return #GROUP
- function GROUP:UnHandleEvent( Event )
-
- self:EventDispatcher():RemoveForGroup( self:GetName(), self, Event )
-
- return self
- end
-
-end
-
-do -- Players
-
- --- Get player names
- -- @param #GROUP self
- -- @return #table The group has players, an array of player names is returned.
- -- @return #nil The group has no players
- function GROUP:GetPlayerNames()
-
- local PlayerNames = nil
-
- local Units = self:GetUnits()
- for UnitID, UnitData in pairs( Units ) do
- local Unit = UnitData -- Wrapper.Unit#UNIT
- local PlayerName = Unit:GetPlayerName()
- if PlayerName and PlayerName ~= "" then
- PlayerNames = PlayerNames or {}
- table.insert( PlayerNames, PlayerName )
- end
- end
-
- self:F( PlayerNames )
- return PlayerNames
- end
-
-end--- **Wrapper** - UNIT is a wrapper class for the DCS Class Unit.
---
--- ===
---
--- The @{#UNIT} class is a wrapper class to handle the DCS Unit objects:
---
--- * Support all DCS Unit APIs.
--- * Enhance with Unit specific APIs not in the DCS Unit API set.
--- * Handle local Unit Controller.
--- * Manage the "state" of the DCS Unit.
---
--- @module Unit
-
---- @type UNIT
--- @extends Wrapper.Controllable#CONTROLLABLE
-
----
--- # UNIT class, extends @{Controllable#CONTROLLABLE}
---
--- For each DCS Unit object alive within a running mission, a UNIT wrapper object (instance) will be created within the _@{DATABASE} object.
--- This is done at the beginning of the mission (when the mission starts), and dynamically when new DCS Unit objects are spawned (using the @{SPAWN} class).
---
--- The UNIT class **does not contain a :New()** method, rather it provides **:Find()** methods to retrieve the object reference
--- using the DCS Unit or the DCS UnitName.
---
--- Another thing to know is that UNIT objects do not "contain" the DCS Unit object.
--- The UNIT methods will reference the DCS Unit object by name when it is needed during API execution.
--- If the DCS Unit object does not exist or is nil, the UNIT methods will return nil and log an exception in the DCS.log file.
---
--- The UNIT class provides the following functions to retrieve quickly the relevant UNIT instance:
---
--- * @{#UNIT.Find}(): Find a UNIT instance from the _DATABASE object using a DCS Unit object.
--- * @{#UNIT.FindByName}(): Find a UNIT instance from the _DATABASE object using a DCS Unit name.
---
--- IMPORTANT: ONE SHOULD NEVER SANATIZE these UNIT OBJECT REFERENCES! (make the UNIT object references nil).
---
--- ## DCS UNIT APIs
---
--- The DCS Unit APIs are used extensively within MOOSE. The UNIT class has for each DCS Unit API a corresponding method.
--- To be able to distinguish easily in your code the difference between a UNIT API call and a DCS Unit API call,
--- the first letter of the method is also capitalized. So, by example, the DCS Unit method @{DCSWrapper.Unit#Unit.getName}()
--- is implemented in the UNIT class as @{#UNIT.GetName}().
---
--- ## Smoke, Flare Units
---
--- The UNIT class provides methods to smoke or flare units easily.
--- The @{#UNIT.SmokeBlue}(), @{#UNIT.SmokeGreen}(),@{#UNIT.SmokeOrange}(), @{#UNIT.SmokeRed}(), @{#UNIT.SmokeRed}() methods
--- will smoke the unit in the corresponding color. Note that smoking a unit is done at the current position of the DCS Unit.
--- When the DCS Unit moves for whatever reason, the smoking will still continue!
--- The @{#UNIT.FlareGreen}(), @{#UNIT.FlareRed}(), @{#UNIT.FlareWhite}(), @{#UNIT.FlareYellow}()
--- methods will fire off a flare in the air with the corresponding color. Note that a flare is a one-off shot and its effect is of very short duration.
---
--- ## Location Position, Point
---
--- The UNIT class provides methods to obtain the current point or position of the DCS Unit.
--- The @{#UNIT.GetPointVec2}(), @{#UNIT.GetVec3}() will obtain the current **location** of the DCS Unit in a Vec2 (2D) or a **point** in a Vec3 (3D) vector respectively.
--- If you want to obtain the complete **3D position** including ori�ntation and direction vectors, consult the @{#UNIT.GetPositionVec3}() method respectively.
---
--- ## Test if alive
---
--- The @{#UNIT.IsAlive}(), @{#UNIT.IsActive}() methods determines if the DCS Unit is alive, meaning, it is existing and active.
---
--- ## Test for proximity
---
--- The UNIT class contains methods to test the location or proximity against zones or other objects.
---
--- ### Zones
---
--- To test whether the Unit is within a **zone**, use the @{#UNIT.IsInZone}() or the @{#UNIT.IsNotInZone}() methods. Any zone can be tested on, but the zone must be derived from @{Zone#ZONE_BASE}.
---
--- ### Units
---
--- Test if another DCS Unit is within a given radius of the current DCS Unit, use the @{#UNIT.OtherUnitInRadius}() method.
---
--- @field #UNIT UNIT
-UNIT = {
- ClassName="UNIT",
-}
-
-
---- Unit.SensorType
--- @type Unit.SensorType
--- @field OPTIC
--- @field RADAR
--- @field IRST
--- @field RWR
-
-
--- Registration.
-
---- Create a new UNIT from DCSUnit.
--- @param #UNIT self
--- @param #string UnitName The name of the DCS unit.
--- @return #UNIT
-function UNIT:Register( UnitName )
- local self = BASE:Inherit( self, CONTROLLABLE:New( UnitName ) )
- self.UnitName = UnitName
-
- self:SetEventPriority( 3 )
- return self
-end
-
--- Reference methods.
-
---- Finds a UNIT from the _DATABASE using a DCSUnit object.
--- @param #UNIT self
--- @param Dcs.DCSWrapper.Unit#Unit DCSUnit An existing DCS Unit object reference.
--- @return #UNIT self
-function UNIT:Find( DCSUnit )
-
- local UnitName = DCSUnit:getName()
- local UnitFound = _DATABASE:FindUnit( UnitName )
- return UnitFound
-end
-
---- Find a UNIT in the _DATABASE using the name of an existing DCS Unit.
--- @param #UNIT self
--- @param #string UnitName The Unit Name.
--- @return #UNIT self
-function UNIT:FindByName( UnitName )
-
- local UnitFound = _DATABASE:FindUnit( UnitName )
- return UnitFound
-end
-
---- Return the name of the UNIT.
--- @param #UNIT self
--- @return #string The UNIT name.
-function UNIT:Name()
-
- return self.UnitName
-end
-
-
---- @param #UNIT self
--- @return Dcs.DCSWrapper.Unit#Unit
-function UNIT:GetDCSObject()
-
- local DCSUnit = Unit.getByName( self.UnitName )
-
- if DCSUnit then
- return DCSUnit
- end
-
- return nil
-end
-
---- Respawn the @{Unit} using a (tweaked) template of the parent Group.
---
--- This function will:
---
--- * Get the current position and heading of the group.
--- * When the unit is alive, it will tweak the template x, y and heading coordinates of the group and the embedded units to the current units positions.
--- * Then it will respawn the re-modelled group.
---
--- @param #UNIT self
--- @param Dcs.DCSTypes#Vec3 SpawnVec3 The position where to Spawn the new Unit at.
--- @param #number Heading The heading of the unit respawn.
-function UNIT:ReSpawn( SpawnVec3, Heading )
-
- local SpawnGroupTemplate = UTILS.DeepCopy( _DATABASE:GetGroupTemplateFromUnitName( self:Name() ) )
- self:T( SpawnGroupTemplate )
-
- local SpawnGroup = self:GetGroup()
-
- if SpawnGroup then
-
- local Vec3 = SpawnGroup:GetVec3()
- SpawnGroupTemplate.x = SpawnVec3.x
- SpawnGroupTemplate.y = SpawnVec3.z
-
- self:E( #SpawnGroupTemplate.units )
- for UnitID, UnitData in pairs( SpawnGroup:GetUnits() ) do
- local GroupUnit = UnitData -- #UNIT
- self:E( GroupUnit:GetName() )
- if GroupUnit:IsAlive() then
- local GroupUnitVec3 = GroupUnit:GetVec3()
- local GroupUnitHeading = GroupUnit:GetHeading()
- SpawnGroupTemplate.units[UnitID].alt = GroupUnitVec3.y
- SpawnGroupTemplate.units[UnitID].x = GroupUnitVec3.x
- SpawnGroupTemplate.units[UnitID].y = GroupUnitVec3.z
- SpawnGroupTemplate.units[UnitID].heading = GroupUnitHeading
- self:E( { UnitID, SpawnGroupTemplate.units[UnitID], SpawnGroupTemplate.units[UnitID] } )
- end
- end
- end
-
- for UnitTemplateID, UnitTemplateData in pairs( SpawnGroupTemplate.units ) do
- self:T( UnitTemplateData.name )
- if UnitTemplateData.name == self:Name() then
- self:T("Adjusting")
- SpawnGroupTemplate.units[UnitTemplateID].alt = SpawnVec3.y
- SpawnGroupTemplate.units[UnitTemplateID].x = SpawnVec3.x
- SpawnGroupTemplate.units[UnitTemplateID].y = SpawnVec3.z
- SpawnGroupTemplate.units[UnitTemplateID].heading = Heading
- self:E( { UnitTemplateID, SpawnGroupTemplate.units[UnitTemplateID], SpawnGroupTemplate.units[UnitTemplateID] } )
- else
- self:E( SpawnGroupTemplate.units[UnitTemplateID].name )
- local GroupUnit = UNIT:FindByName( SpawnGroupTemplate.units[UnitTemplateID].name ) -- #UNIT
- if GroupUnit and GroupUnit:IsAlive() then
- local GroupUnitVec3 = GroupUnit:GetVec3()
- local GroupUnitHeading = GroupUnit:GetHeading()
- UnitTemplateData.alt = GroupUnitVec3.y
- UnitTemplateData.x = GroupUnitVec3.x
- UnitTemplateData.y = GroupUnitVec3.z
- UnitTemplateData.heading = GroupUnitHeading
- else
- if SpawnGroupTemplate.units[UnitTemplateID].name ~= self:Name() then
- self:T("nilling")
- SpawnGroupTemplate.units[UnitTemplateID].delete = true
- end
- end
- end
- end
-
- -- Remove obscolete units from the group structure
- local i = 1
- while i <= #SpawnGroupTemplate.units do
-
- local UnitTemplateData = SpawnGroupTemplate.units[i]
- self:T( UnitTemplateData.name )
-
- if UnitTemplateData.delete then
- table.remove( SpawnGroupTemplate.units, i )
- else
- i = i + 1
- end
- end
-
- _DATABASE:Spawn( SpawnGroupTemplate )
-end
-
-
-
---- Returns if the unit is activated.
--- @param #UNIT self
--- @return #boolean true if Unit is activated.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:IsActive()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
-
- local UnitIsActive = DCSUnit:isActive()
- return UnitIsActive
- end
-
- return nil
-end
-
---- Returns if the Unit is alive.
--- If the Unit is not alive, nil is returned.
--- If the Unit is alive and active, true is returned.
--- If the Unit is alive but not active, false is returned.
--- @param #UNIT self
--- @return #boolean true if Unit is alive and active.
--- @return #boolean false if Unit is alive but not active.
--- @return #nil if the Unit is not existing or is not alive.
-function UNIT:IsAlive()
- self:F3( self.UnitName )
-
- local DCSUnit = self:GetDCSObject() -- Dcs.DCSUnit#Unit
-
- if DCSUnit then
- local UnitIsAlive = DCSUnit:isExist() and DCSUnit:isActive()
- return UnitIsAlive
- end
-
- return nil
-end
-
-
-
---- Returns the Unit's callsign - the localized string.
--- @param #UNIT self
--- @return #string The Callsign of the Unit.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetCallsign()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitCallSign = DCSUnit:getCallsign()
- return UnitCallSign
- end
-
- self:E( self.ClassName .. " " .. self.UnitName .. " not found!" )
- return nil
-end
-
-
---- Returns name of the player that control the unit or nil if the unit is controlled by A.I.
--- @param #UNIT self
--- @return #string Player Name
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetPlayerName()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
-
- local PlayerName = DCSUnit:getPlayerName()
- if PlayerName == nil then
- PlayerName = ""
- end
- return PlayerName
- end
-
- return nil
-end
-
---- Returns the unit's number in the group.
--- The number is the same number the unit has in ME.
--- It may not be changed during the mission.
--- If any unit in the group is destroyed, the numbers of another units will not be changed.
--- @param #UNIT self
--- @return #number The Unit number.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetNumber()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitNumber = DCSUnit:getNumber()
- return UnitNumber
- end
-
- return nil
-end
-
---- Returns the unit's group if it exist and nil otherwise.
--- @param Wrapper.Unit#UNIT self
--- @return Wrapper.Group#GROUP The Group of the Unit.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetGroup()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitGroup = GROUP:Find( DCSUnit:getGroup() )
- return UnitGroup
- end
-
- return nil
-end
-
-
--- Need to add here functions to check if radar is on and which object etc.
-
---- Returns the prefix name of the DCS Unit. A prefix name is a part of the name before a '#'-sign.
--- DCS Units spawned with the @{SPAWN} class contain a '#'-sign to indicate the end of the (base) DCS Unit name.
--- The spawn sequence number and unit number are contained within the name after the '#' sign.
--- @param #UNIT self
--- @return #string The name of the DCS Unit.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetPrefix()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitPrefix = string.match( self.UnitName, ".*#" ):sub( 1, -2 )
- self:T3( UnitPrefix )
- return UnitPrefix
- end
-
- return nil
-end
-
---- Returns the Unit's ammunition.
--- @param #UNIT self
--- @return Dcs.DCSWrapper.Unit#Unit.Ammo
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetAmmo()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitAmmo = DCSUnit:getAmmo()
- return UnitAmmo
- end
-
- return nil
-end
-
---- Returns the unit sensors.
--- @param #UNIT self
--- @return Dcs.DCSWrapper.Unit#Unit.Sensors
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetSensors()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitSensors = DCSUnit:getSensors()
- return UnitSensors
- end
-
- return nil
-end
-
--- Need to add here a function per sensortype
--- unit:hasSensors(Unit.SensorType.RADAR, Unit.RadarType.AS)
-
---- Returns if the unit has sensors of a certain type.
--- @param #UNIT self
--- @return #boolean returns true if the unit has specified types of sensors. This function is more preferable than Unit.getSensors() if you don't want to get information about all the unit's sensors, and just want to check if the unit has specified types of sensors.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:HasSensors( ... )
- self:F2( arg )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local HasSensors = DCSUnit:hasSensors( unpack( arg ) )
- return HasSensors
- end
-
- return nil
-end
-
---- Returns if the unit is SEADable.
--- @param #UNIT self
--- @return #boolean returns true if the unit is SEADable.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:HasSEAD()
- self:F2()
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitSEADAttributes = DCSUnit:getDesc().attributes
-
- local HasSEAD = false
- if UnitSEADAttributes["RADAR_BAND1_FOR_ARM"] and UnitSEADAttributes["RADAR_BAND1_FOR_ARM"] == true or
- UnitSEADAttributes["RADAR_BAND2_FOR_ARM"] and UnitSEADAttributes["RADAR_BAND2_FOR_ARM"] == true then
- HasSEAD = true
- end
- return HasSEAD
- end
-
- return nil
-end
-
---- Returns two values:
---
--- * First value indicates if at least one of the unit's radar(s) is on.
--- * Second value is the object of the radar's interest. Not nil only if at least one radar of the unit is tracking a target.
--- @param #UNIT self
--- @return #boolean Indicates if at least one of the unit's radar(s) is on.
--- @return Dcs.DCSWrapper.Object#Object The object of the radar's interest. Not nil only if at least one radar of the unit is tracking a target.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetRadar()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitRadarOn, UnitRadarObject = DCSUnit:getRadar()
- return UnitRadarOn, UnitRadarObject
- end
-
- return nil, nil
-end
-
---- Returns relative amount of fuel (from 0.0 to 1.0) the unit has in its internal tanks. If there are additional fuel tanks the value may be greater than 1.0.
--- @param #UNIT self
--- @return #number The relative amount of fuel (from 0.0 to 1.0).
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetFuel()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitFuel = DCSUnit:getFuel()
- return UnitFuel
- end
-
- return nil
-end
-
---- Returns the UNIT in a UNIT list of one element.
--- @param #UNIT self
--- @return #list The UNITs wrappers.
-function UNIT:GetUnits()
- self:F2( { self.UnitName } )
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local DCSUnits = DCSUnit:getUnits()
- local Units = {}
- Units[1] = UNIT:Find( DCSUnit )
- self:T3( Units )
- return Units
- end
-
- return nil
-end
-
-
---- Returns the unit's health. Dead units has health <= 1.0.
--- @param #UNIT self
--- @return #number The Unit's health value.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetLife()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitLife = DCSUnit:getLife()
- return UnitLife
- end
-
- return nil
-end
-
---- Returns the Unit's initial health.
--- @param #UNIT self
--- @return #number The Unit's initial health value.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:GetLife0()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitLife0 = DCSUnit:getLife0()
- return UnitLife0
- end
-
- return nil
-end
-
---- Returns the category name of the #UNIT.
--- @param #UNIT self
--- @return #string Category name = Helicopter, Airplane, Ground Unit, Ship
-function UNIT:GetCategoryName()
- self:F3( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
- if DCSUnit then
- local CategoryNames = {
- [Unit.Category.AIRPLANE] = "Airplane",
- [Unit.Category.HELICOPTER] = "Helicopter",
- [Unit.Category.GROUND_UNIT] = "Ground Unit",
- [Unit.Category.SHIP] = "Ship",
- [Unit.Category.STRUCTURE] = "Structure",
- }
- local UnitCategory = DCSUnit:getDesc().category
- self:T3( UnitCategory )
-
- return CategoryNames[UnitCategory]
- end
-
- return nil
-end
-
-
---- Returns the Unit's A2G threat level on a scale from 1 to 10 ...
--- The following threat levels are foreseen:
---
--- * Threat level 0: Unit is unarmed.
--- * Threat level 1: Unit is infantry.
--- * Threat level 2: Unit is an infantry vehicle.
--- * Threat level 3: Unit is ground artillery.
--- * Threat level 4: Unit is a tank.
--- * Threat level 5: Unit is a modern tank or ifv with ATGM.
--- * Threat level 6: Unit is a AAA.
--- * Threat level 7: Unit is a SAM or manpad, IR guided.
--- * Threat level 8: Unit is a Short Range SAM, radar guided.
--- * Threat level 9: Unit is a Medium Range SAM, radar guided.
--- * Threat level 10: Unit is a Long Range SAM, radar guided.
--- @param #UNIT self
-function UNIT:GetThreatLevel()
-
- local Attributes = self:GetDesc().attributes
- self:T( Attributes )
-
- local ThreatLevel = 0
- local ThreatText = ""
-
- if self:IsGround() then
-
- self:T( "Ground" )
-
- local ThreatLevels = {
- "Unarmed",
- "Infantry",
- "Old Tanks & APCs",
- "Tanks & IFVs without ATGM",
- "Tanks & IFV with ATGM",
- "Modern Tanks",
- "AAA",
- "IR Guided SAMs",
- "SR SAMs",
- "MR SAMs",
- "LR SAMs"
- }
-
-
- if Attributes["LR SAM"] then ThreatLevel = 10
- elseif Attributes["MR SAM"] then ThreatLevel = 9
- elseif Attributes["SR SAM"] and
- not Attributes["IR Guided SAM"] then ThreatLevel = 8
- elseif ( Attributes["SR SAM"] or Attributes["MANPADS"] ) and
- Attributes["IR Guided SAM"] then ThreatLevel = 7
- elseif Attributes["AAA"] then ThreatLevel = 6
- elseif Attributes["Modern Tanks"] then ThreatLevel = 5
- elseif ( Attributes["Tanks"] or Attributes["IFV"] ) and
- Attributes["ATGM"] then ThreatLevel = 4
- elseif ( Attributes["Tanks"] or Attributes["IFV"] ) and
- not Attributes["ATGM"] then ThreatLevel = 3
- elseif Attributes["Old Tanks"] or Attributes["APC"] or Attributes["Artillery"] then ThreatLevel = 2
- elseif Attributes["Infantry"] then ThreatLevel = 1
- end
-
- ThreatText = ThreatLevels[ThreatLevel+1]
- end
-
- if self:IsAir() then
-
- self:T( "Air" )
-
- local ThreatLevels = {
- "Unarmed",
- "Tanker",
- "AWACS",
- "Transport Helicpter",
- "UAV",
- "Bomber",
- "Strategic Bomber",
- "Attack Helicopter",
- "Interceptor",
- "Multirole Fighter",
- "Fighter"
- }
-
-
- if Attributes["Fighters"] then ThreatLevel = 10
- elseif Attributes["Multirole fighters"] then ThreatLevel = 9
- elseif Attributes["Battleplanes"] then ThreatLevel = 8
- elseif Attributes["Attack helicopters"] then ThreatLevel = 7
- elseif Attributes["Strategic bombers"] then ThreatLevel = 6
- elseif Attributes["Bombers"] then ThreatLevel = 5
- elseif Attributes["UAVs"] then ThreatLevel = 4
- elseif Attributes["Transport helicopters"] then ThreatLevel = 3
- elseif Attributes["AWACS"] then ThreatLevel = 2
- elseif Attributes["Tankers"] then ThreatLevel = 1
- end
-
- ThreatText = ThreatLevels[ThreatLevel+1]
- end
-
- if self:IsShip() then
-
- self:T( "Ship" )
-
---["Aircraft Carriers"] = {"Heavy armed ships",},
---["Cruisers"] = {"Heavy armed ships",},
---["Destroyers"] = {"Heavy armed ships",},
---["Frigates"] = {"Heavy armed ships",},
---["Corvettes"] = {"Heavy armed ships",},
---["Heavy armed ships"] = {"Armed ships", "Armed Air Defence", "HeavyArmoredUnits",},
---["Light armed ships"] = {"Armed ships","NonArmoredUnits"},
---["Armed ships"] = {"Ships"},
---["Unarmed ships"] = {"Ships","HeavyArmoredUnits",},
-
- local ThreatLevels = {
- "Unarmed ship",
- "Light armed ships",
- "Corvettes",
- "",
- "Frigates",
- "",
- "Cruiser",
- "",
- "Destroyer",
- "",
- "Aircraft Carrier"
- }
-
-
- if Attributes["Aircraft Carriers"] then ThreatLevel = 10
- elseif Attributes["Destroyers"] then ThreatLevel = 8
- elseif Attributes["Cruisers"] then ThreatLevel = 6
- elseif Attributes["Frigates"] then ThreatLevel = 4
- elseif Attributes["Corvettes"] then ThreatLevel = 2
- elseif Attributes["Light armed ships"] then ThreatLevel = 1
- end
-
- ThreatText = ThreatLevels[ThreatLevel+1]
- end
-
- self:T2( ThreatLevel )
- return ThreatLevel, ThreatText
-
-end
-
-
--- Is functions
-
---- Returns true if the unit is within a @{Zone}.
--- @param #UNIT self
--- @param Core.Zone#ZONE_BASE Zone The zone to test.
--- @return #boolean Returns true if the unit is within the @{Zone#ZONE_BASE}
-function UNIT:IsInZone( Zone )
- self:F2( { self.UnitName, Zone } )
-
- if self:IsAlive() then
- local IsInZone = Zone:IsVec3InZone( self:GetVec3() )
-
- self:T( { IsInZone } )
- return IsInZone
- end
-
- return false
-end
-
---- Returns true if the unit is not within a @{Zone}.
--- @param #UNIT self
--- @param Core.Zone#ZONE_BASE Zone The zone to test.
--- @return #boolean Returns true if the unit is not within the @{Zone#ZONE_BASE}
-function UNIT:IsNotInZone( Zone )
- self:F2( { self.UnitName, Zone } )
-
- if self:IsAlive() then
- local IsInZone = not Zone:IsVec3InZone( self:GetVec3() )
-
- self:T( { IsInZone } )
- return IsInZone
- else
- return false
- end
-end
-
-
---- Returns true if there is an **other** DCS Unit within a radius of the current 2D point of the DCS Unit.
--- @param #UNIT self
--- @param #UNIT AwaitUnit The other UNIT wrapper object.
--- @param Radius The radius in meters with the DCS Unit in the centre.
--- @return true If the other DCS Unit is within the radius of the 2D point of the DCS Unit.
--- @return #nil The DCS Unit is not existing or alive.
-function UNIT:OtherUnitInRadius( AwaitUnit, Radius )
- self:F2( { self.UnitName, AwaitUnit.UnitName, Radius } )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitVec3 = self:GetVec3()
- local AwaitUnitVec3 = AwaitUnit:GetVec3()
-
- if (((UnitVec3.x - AwaitUnitVec3.x)^2 + (UnitVec3.z - AwaitUnitVec3.z)^2)^0.5 <= Radius) then
- self:T3( "true" )
- return true
- else
- self:T3( "false" )
- return false
- end
- end
-
- return nil
-end
-
-
-
---- Signal a flare at the position of the UNIT.
--- @param #UNIT self
--- @param Utilities.Utils#FLARECOLOR FlareColor
-function UNIT:Flare( FlareColor )
- self:F2()
- trigger.action.signalFlare( self:GetVec3(), FlareColor , 0 )
-end
-
---- Signal a white flare at the position of the UNIT.
--- @param #UNIT self
-function UNIT:FlareWhite()
- self:F2()
- trigger.action.signalFlare( self:GetVec3(), trigger.flareColor.White , 0 )
-end
-
---- Signal a yellow flare at the position of the UNIT.
--- @param #UNIT self
-function UNIT:FlareYellow()
- self:F2()
- trigger.action.signalFlare( self:GetVec3(), trigger.flareColor.Yellow , 0 )
-end
-
---- Signal a green flare at the position of the UNIT.
--- @param #UNIT self
-function UNIT:FlareGreen()
- self:F2()
- trigger.action.signalFlare( self:GetVec3(), trigger.flareColor.Green , 0 )
-end
-
---- Signal a red flare at the position of the UNIT.
--- @param #UNIT self
-function UNIT:FlareRed()
- self:F2()
- local Vec3 = self:GetVec3()
- if Vec3 then
- trigger.action.signalFlare( Vec3, trigger.flareColor.Red, 0 )
- end
-end
-
---- Smoke the UNIT.
--- @param #UNIT self
-function UNIT:Smoke( SmokeColor, Range )
- self:F2()
- if Range then
- trigger.action.smoke( self:GetRandomVec3( Range ), SmokeColor )
- else
- trigger.action.smoke( self:GetVec3(), SmokeColor )
- end
-
-end
-
---- Smoke the UNIT Green.
--- @param #UNIT self
-function UNIT:SmokeGreen()
- self:F2()
- trigger.action.smoke( self:GetVec3(), trigger.smokeColor.Green )
-end
-
---- Smoke the UNIT Red.
--- @param #UNIT self
-function UNIT:SmokeRed()
- self:F2()
- trigger.action.smoke( self:GetVec3(), trigger.smokeColor.Red )
-end
-
---- Smoke the UNIT White.
--- @param #UNIT self
-function UNIT:SmokeWhite()
- self:F2()
- trigger.action.smoke( self:GetVec3(), trigger.smokeColor.White )
-end
-
---- Smoke the UNIT Orange.
--- @param #UNIT self
-function UNIT:SmokeOrange()
- self:F2()
- trigger.action.smoke( self:GetVec3(), trigger.smokeColor.Orange )
-end
-
---- Smoke the UNIT Blue.
--- @param #UNIT self
-function UNIT:SmokeBlue()
- self:F2()
- trigger.action.smoke( self:GetVec3(), trigger.smokeColor.Blue )
-end
-
--- Is methods
-
---- Returns if the unit is of an air category.
--- If the unit is a helicopter or a plane, then this method will return true, otherwise false.
--- @param #UNIT self
--- @return #boolean Air category evaluation result.
-function UNIT:IsAir()
- self:F2()
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitDescriptor = DCSUnit:getDesc()
- self:T3( { UnitDescriptor.category, Unit.Category.AIRPLANE, Unit.Category.HELICOPTER } )
-
- local IsAirResult = ( UnitDescriptor.category == Unit.Category.AIRPLANE ) or ( UnitDescriptor.category == Unit.Category.HELICOPTER )
-
- self:T3( IsAirResult )
- return IsAirResult
- end
-
- return nil
-end
-
---- Returns if the unit is of an ground category.
--- If the unit is a ground vehicle or infantry, this method will return true, otherwise false.
--- @param #UNIT self
--- @return #boolean Ground category evaluation result.
-function UNIT:IsGround()
- self:F2()
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitDescriptor = DCSUnit:getDesc()
- self:T3( { UnitDescriptor.category, Unit.Category.GROUND_UNIT } )
-
- local IsGroundResult = ( UnitDescriptor.category == Unit.Category.GROUND_UNIT )
-
- self:T3( IsGroundResult )
- return IsGroundResult
- end
-
- return nil
-end
-
---- Returns if the unit is a friendly unit.
--- @param #UNIT self
--- @return #boolean IsFriendly evaluation result.
-function UNIT:IsFriendly( FriendlyCoalition )
- self:F2()
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitCoalition = DCSUnit:getCoalition()
- self:T3( { UnitCoalition, FriendlyCoalition } )
-
- local IsFriendlyResult = ( UnitCoalition == FriendlyCoalition )
-
- self:E( IsFriendlyResult )
- return IsFriendlyResult
- end
-
- return nil
-end
-
---- Returns if the unit is of a ship category.
--- If the unit is a ship, this method will return true, otherwise false.
--- @param #UNIT self
--- @return #boolean Ship category evaluation result.
-function UNIT:IsShip()
- self:F2()
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitDescriptor = DCSUnit:getDesc()
- self:T3( { UnitDescriptor.category, Unit.Category.SHIP } )
-
- local IsShipResult = ( UnitDescriptor.category == Unit.Category.SHIP )
-
- self:T3( IsShipResult )
- return IsShipResult
- end
-
- return nil
-end
-
---- Returns true if the UNIT is in the air.
--- @param Wrapper.Positionable#UNIT self
--- @return #boolean true if in the air.
--- @return #nil The UNIT is not existing or alive.
-function UNIT:InAir()
- self:F2( self.UnitName )
-
- local DCSUnit = self:GetDCSObject()
-
- if DCSUnit then
- local UnitInAir = DCSUnit:inAir()
- self:T3( UnitInAir )
- return UnitInAir
- end
-
- return nil
-end
-
-do -- Event Handling
-
- --- Subscribe to a DCS Event.
- -- @param #UNIT self
- -- @param Core.Event#EVENTS Event
- -- @param #function EventFunction (optional) The function to be called when the event occurs for the unit.
- -- @return #UNIT
- function UNIT:HandleEvent( Event, EventFunction )
-
- self:EventDispatcher():OnEventForUnit( self:GetName(), EventFunction, self, Event )
-
- return self
- end
-
- --- UnSubscribe to a DCS event.
- -- @param #UNIT self
- -- @param Core.Event#EVENTS Event
- -- @return #UNIT
- function UNIT:UnHandleEvent( Event )
-
- self:EventDispatcher():RemoveForUnit( self:GetName(), self, Event )
-
- return self
- end
-
-end--- This module contains the CLIENT class.
---
--- 1) @{Client#CLIENT} class, extends @{Unit#UNIT}
--- ===============================================
--- Clients are those **Units** defined within the Mission Editor that have the skillset defined as __Client__ or __Player__.
--- Note that clients are NOT the same as Units, they are NOT necessarily alive.
--- The @{Client#CLIENT} class is a wrapper class to handle the DCS Unit objects that have the skillset defined as __Client__ or __Player__:
---
--- * Wraps the DCS Unit objects with skill level set to Player or Client.
--- * Support all DCS Unit APIs.
--- * Enhance with Unit specific APIs not in the DCS Group API set.
--- * When player joins Unit, execute alive init logic.
--- * Handles messages to players.
--- * Manage the "state" of the DCS Unit.
---
--- Clients are being used by the @{MISSION} class to follow players and register their successes.
---
--- 1.1) CLIENT reference methods
--- -----------------------------
--- For each DCS Unit having skill level Player or Client, a CLIENT wrapper object (instance) will be created within the _@{DATABASE} object.
--- This is done at the beginning of the mission (when the mission starts).
---
--- The CLIENT class does not contain a :New() method, rather it provides :Find() methods to retrieve the object reference
--- using the DCS Unit or the DCS UnitName.
---
--- Another thing to know is that CLIENT objects do not "contain" the DCS Unit object.
--- The CLIENT methods will reference the DCS Unit object by name when it is needed during API execution.
--- If the DCS Unit object does not exist or is nil, the CLIENT methods will return nil and log an exception in the DCS.log file.
---
--- The CLIENT class provides the following functions to retrieve quickly the relevant CLIENT instance:
---
--- * @{#CLIENT.Find}(): Find a CLIENT instance from the _DATABASE object using a DCS Unit object.
--- * @{#CLIENT.FindByName}(): Find a CLIENT instance from the _DATABASE object using a DCS Unit name.
---
--- IMPORTANT: ONE SHOULD NEVER SANATIZE these CLIENT OBJECT REFERENCES! (make the CLIENT object references nil).
---
--- @module Client
-
---- The CLIENT class
--- @type CLIENT
--- @extends Wrapper.Unit#UNIT
-CLIENT = {
- ONBOARDSIDE = {
- NONE = 0,
- LEFT = 1,
- RIGHT = 2,
- BACK = 3,
- FRONT = 4
- },
- ClassName = "CLIENT",
- ClientName = nil,
- ClientAlive = false,
- ClientTransport = false,
- ClientBriefingShown = false,
- _Menus = {},
- _Tasks = {},
- Messages = {
- }
-}
-
-
---- Finds a CLIENT from the _DATABASE using the relevant DCS Unit.
--- @param #CLIENT self
--- @param #string ClientName Name of the DCS **Unit** as defined within the Mission Editor.
--- @param #string ClientBriefing Text that describes the briefing of the mission when a Player logs into the Client.
--- @return #CLIENT
--- @usage
--- -- Create new Clients.
--- local Mission = MISSIONSCHEDULER.AddMission( 'Russia Transport Troops SA-6', 'Operational', 'Transport troops from the control center to one of the SA-6 SAM sites to activate their operation.', 'Russia' )
--- Mission:AddGoal( DeploySA6TroopsGoal )
---
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*HOT-Deploy Troops 1' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*RAMP-Deploy Troops 3' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*HOT-Deploy Troops 2' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*RAMP-Deploy Troops 4' ):Transport() )
-function CLIENT:Find( DCSUnit, Error )
- local ClientName = DCSUnit:getName()
- local ClientFound = _DATABASE:FindClient( ClientName )
-
- if ClientFound then
- ClientFound:F( ClientName )
- return ClientFound
- end
-
- if not Error then
- error( "CLIENT not found for: " .. ClientName )
- end
-end
-
-
---- Finds a CLIENT from the _DATABASE using the relevant Client Unit Name.
--- As an optional parameter, a briefing text can be given also.
--- @param #CLIENT self
--- @param #string ClientName Name of the DCS **Unit** as defined within the Mission Editor.
--- @param #string ClientBriefing Text that describes the briefing of the mission when a Player logs into the Client.
--- @param #boolean Error A flag that indicates whether an error should be raised if the CLIENT cannot be found. By default an error will be raised.
--- @return #CLIENT
--- @usage
--- -- Create new Clients.
--- local Mission = MISSIONSCHEDULER.AddMission( 'Russia Transport Troops SA-6', 'Operational', 'Transport troops from the control center to one of the SA-6 SAM sites to activate their operation.', 'Russia' )
--- Mission:AddGoal( DeploySA6TroopsGoal )
---
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*HOT-Deploy Troops 1' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*RAMP-Deploy Troops 3' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*HOT-Deploy Troops 2' ):Transport() )
--- Mission:AddClient( CLIENT:FindByName( 'RU MI-8MTV2*RAMP-Deploy Troops 4' ):Transport() )
-function CLIENT:FindByName( ClientName, ClientBriefing, Error )
- local ClientFound = _DATABASE:FindClient( ClientName )
-
- if ClientFound then
- ClientFound:F( { ClientName, ClientBriefing } )
- ClientFound:AddBriefing( ClientBriefing )
- ClientFound.MessageSwitch = true
-
- return ClientFound
- end
-
- if not Error then
- error( "CLIENT not found for: " .. ClientName )
- end
-end
-
-function CLIENT:Register( ClientName )
- local self = BASE:Inherit( self, UNIT:Register( ClientName ) )
-
- self:F( ClientName )
- self.ClientName = ClientName
- self.MessageSwitch = true
- self.ClientAlive2 = false
-
- --self.AliveCheckScheduler = routines.scheduleFunction( self._AliveCheckScheduler, { self }, timer.getTime() + 1, 5 )
- self.AliveCheckScheduler = SCHEDULER:New( self, self._AliveCheckScheduler, { "Client Alive " .. ClientName }, 1, 5 )
-
- self:E( self )
- return self
-end
-
-
---- Transport defines that the Client is a Transport. Transports show cargo.
--- @param #CLIENT self
--- @return #CLIENT
-function CLIENT:Transport()
- self:F()
-
- self.ClientTransport = true
- return self
-end
-
---- AddBriefing adds a briefing to a CLIENT when a player joins a mission.
--- @param #CLIENT self
--- @param #string ClientBriefing is the text defining the Mission briefing.
--- @return #CLIENT self
-function CLIENT:AddBriefing( ClientBriefing )
- self:F( ClientBriefing )
- self.ClientBriefing = ClientBriefing
- self.ClientBriefingShown = false
-
- return self
-end
-
---- Show the briefing of a CLIENT.
--- @param #CLIENT self
--- @return #CLIENT self
-function CLIENT:ShowBriefing()
- self:F( { self.ClientName, self.ClientBriefingShown } )
-
- if not self.ClientBriefingShown then
- self.ClientBriefingShown = true
- local Briefing = ""
- if self.ClientBriefing then
- Briefing = Briefing .. self.ClientBriefing
- end
- Briefing = Briefing .. " Press [LEFT ALT]+[B] to view the complete mission briefing."
- self:Message( Briefing, 60, "Briefing" )
- end
-
- return self
-end
-
---- Show the mission briefing of a MISSION to the CLIENT.
--- @param #CLIENT self
--- @param #string MissionBriefing
--- @return #CLIENT self
-function CLIENT:ShowMissionBriefing( MissionBriefing )
- self:F( { self.ClientName } )
-
- if MissionBriefing then
- self:Message( MissionBriefing, 60, "Mission Briefing" )
- end
-
- return self
-end
-
-
-
---- Resets a CLIENT.
--- @param #CLIENT self
--- @param #string ClientName Name of the Group as defined within the Mission Editor. The Group must have a Unit with the type Client.
-function CLIENT:Reset( ClientName )
- self:F()
- self._Menus = {}
-end
-
--- Is Functions
-
---- Checks if the CLIENT is a multi-seated UNIT.
--- @param #CLIENT self
--- @return #boolean true if multi-seated.
-function CLIENT:IsMultiSeated()
- self:F( self.ClientName )
-
- local ClientMultiSeatedTypes = {
- ["Mi-8MT"] = "Mi-8MT",
- ["UH-1H"] = "UH-1H",
- ["P-51B"] = "P-51B"
- }
-
- if self:IsAlive() then
- local ClientTypeName = self:GetClientGroupUnit():GetTypeName()
- if ClientMultiSeatedTypes[ClientTypeName] then
- return true
- end
- end
-
- return false
-end
-
---- Checks for a client alive event and calls a function on a continuous basis.
--- @param #CLIENT self
--- @param #function CallBackFunction Create a function that will be called when a player joins the slot.
--- @return #CLIENT
-function CLIENT:Alive( CallBackFunction, ... )
- self:F()
-
- self.ClientCallBack = CallBackFunction
- self.ClientParameters = arg
-
- return self
-end
-
---- @param #CLIENT self
-function CLIENT:_AliveCheckScheduler( SchedulerName )
- self:F3( { SchedulerName, self.ClientName, self.ClientAlive2, self.ClientBriefingShown, self.ClientCallBack } )
-
- if self:IsAlive() then
- if self.ClientAlive2 == false then
- self:ShowBriefing()
- if self.ClientCallBack then
- self:T("Calling Callback function")
- self.ClientCallBack( self, unpack( self.ClientParameters ) )
- end
- self.ClientAlive2 = true
- end
- else
- if self.ClientAlive2 == true then
- self.ClientAlive2 = false
- end
- end
-
- return true
-end
-
---- Return the DCSGroup of a Client.
--- This function is modified to deal with a couple of bugs in DCS 1.5.3
--- @param #CLIENT self
--- @return Dcs.DCSWrapper.Group#Group
-function CLIENT:GetDCSGroup()
- self:F3()
-
--- local ClientData = Group.getByName( self.ClientName )
--- if ClientData and ClientData:isExist() then
--- self:T( self.ClientName .. " : group found!" )
--- return ClientData
--- else
--- return nil
--- end
-
- local ClientUnit = Unit.getByName( self.ClientName )
-
- local CoalitionsData = { AlivePlayersRed = coalition.getPlayers( coalition.side.RED ), AlivePlayersBlue = coalition.getPlayers( coalition.side.BLUE ) }
- for CoalitionId, CoalitionData in pairs( CoalitionsData ) do
- self:T3( { "CoalitionData:", CoalitionData } )
- for UnitId, UnitData in pairs( CoalitionData ) do
- self:T3( { "UnitData:", UnitData } )
- if UnitData and UnitData:isExist() then
-
- --self:E(self.ClientName)
- if ClientUnit then
- local ClientGroup = ClientUnit:getGroup()
- if ClientGroup then
- self:T3( "ClientGroup = " .. self.ClientName )
- if ClientGroup:isExist() and UnitData:getGroup():isExist() then
- if ClientGroup:getID() == UnitData:getGroup():getID() then
- self:T3( "Normal logic" )
- self:T3( self.ClientName .. " : group found!" )
- self.ClientGroupID = ClientGroup:getID()
- self.ClientGroupName = ClientGroup:getName()
- return ClientGroup
- end
- else
- -- Now we need to resolve the bugs in DCS 1.5 ...
- -- Consult the database for the units of the Client Group. (ClientGroup:getUnits() returns nil)
- self:T3( "Bug 1.5 logic" )
- local ClientGroupTemplate = _DATABASE.Templates.Units[self.ClientName].GroupTemplate
- self.ClientGroupID = ClientGroupTemplate.groupId
- self.ClientGroupName = _DATABASE.Templates.Units[self.ClientName].GroupName
- self:T3( self.ClientName .. " : group found in bug 1.5 resolvement logic!" )
- return ClientGroup
- end
- -- else
- -- error( "Client " .. self.ClientName .. " not found!" )
- end
- else
- --self:E( { "Client not found!", self.ClientName } )
- end
- end
- end
- end
-
- -- For non player clients
- if ClientUnit then
- local ClientGroup = ClientUnit:getGroup()
- if ClientGroup then
- self:T3( "ClientGroup = " .. self.ClientName )
- if ClientGroup:isExist() then
- self:T3( "Normal logic" )
- self:T3( self.ClientName .. " : group found!" )
- return ClientGroup
- end
- end
- end
-
- self.ClientGroupID = nil
- self.ClientGroupUnit = nil
-
- return nil
-end
-
-
--- TODO: Check Dcs.DCSTypes#Group.ID
---- Get the group ID of the client.
--- @param #CLIENT self
--- @return Dcs.DCSTypes#Group.ID
-function CLIENT:GetClientGroupID()
-
- local ClientGroup = self:GetDCSGroup()
-
- --self:E( self.ClientGroupID ) -- Determined in GetDCSGroup()
- return self.ClientGroupID
-end
-
-
---- Get the name of the group of the client.
--- @param #CLIENT self
--- @return #string
-function CLIENT:GetClientGroupName()
-
- local ClientGroup = self:GetDCSGroup()
-
- self:T( self.ClientGroupName ) -- Determined in GetDCSGroup()
- return self.ClientGroupName
-end
-
---- Returns the UNIT of the CLIENT.
--- @param #CLIENT self
--- @return Wrapper.Unit#UNIT
-function CLIENT:GetClientGroupUnit()
- self:F2()
-
- local ClientDCSUnit = Unit.getByName( self.ClientName )
-
- self:T( self.ClientDCSUnit )
- if ClientDCSUnit and ClientDCSUnit:isExist() then
- local ClientUnit = _DATABASE:FindUnit( self.ClientName )
- self:T2( ClientUnit )
- return ClientUnit
- end
-end
-
---- Returns the DCSUnit of the CLIENT.
--- @param #CLIENT self
--- @return Dcs.DCSTypes#Unit
-function CLIENT:GetClientGroupDCSUnit()
- self:F2()
-
- local ClientDCSUnit = Unit.getByName( self.ClientName )
-
- if ClientDCSUnit and ClientDCSUnit:isExist() then
- self:T2( ClientDCSUnit )
- return ClientDCSUnit
- end
-end
-
-
---- Evaluates if the CLIENT is a transport.
--- @param #CLIENT self
--- @return #boolean true is a transport.
-function CLIENT:IsTransport()
- self:F()
- return self.ClientTransport
-end
-
---- Shows the @{AI_Cargo#CARGO} contained within the CLIENT to the player as a message.
--- The @{AI_Cargo#CARGO} is shown using the @{Message#MESSAGE} distribution system.
--- @param #CLIENT self
-function CLIENT:ShowCargo()
- self:F()
-
- local CargoMsg = ""
-
- for CargoName, Cargo in pairs( CARGOS ) do
- if self == Cargo:IsLoadedInClient() then
- CargoMsg = CargoMsg .. Cargo.CargoName .. " Type:" .. Cargo.CargoType .. " Weight: " .. Cargo.CargoWeight .. "\n"
- end
- end
-
- if CargoMsg == "" then
- CargoMsg = "empty"
- end
-
- self:Message( CargoMsg, 15, "Co-Pilot: Cargo Status", 30 )
-
-end
-
--- TODO (1) I urgently need to revise this.
---- A local function called by the DCS World Menu system to switch off messages.
-function CLIENT.SwitchMessages( PrmTable )
- PrmTable[1].MessageSwitch = PrmTable[2]
-end
-
---- The main message driver for the CLIENT.
--- This function displays various messages to the Player logged into the CLIENT through the DCS World Messaging system.
--- @param #CLIENT self
--- @param #string Message is the text describing the message.
--- @param #number MessageDuration is the duration in seconds that the Message should be displayed.
--- @param #string MessageCategory is the category of the message (the title).
--- @param #number MessageInterval is the interval in seconds between the display of the @{Message#MESSAGE} when the CLIENT is in the air.
--- @param #string MessageID is the identifier of the message when displayed with intervals.
-function CLIENT:Message( Message, MessageDuration, MessageCategory, MessageInterval, MessageID )
- self:F( { Message, MessageDuration, MessageCategory, MessageInterval } )
-
- if self.MessageSwitch == true then
- if MessageCategory == nil then
- MessageCategory = "Messages"
- end
- if MessageID ~= nil then
- if self.Messages[MessageID] == nil then
- self.Messages[MessageID] = {}
- self.Messages[MessageID].MessageId = MessageID
- self.Messages[MessageID].MessageTime = timer.getTime()
- self.Messages[MessageID].MessageDuration = MessageDuration
- if MessageInterval == nil then
- self.Messages[MessageID].MessageInterval = 600
- else
- self.Messages[MessageID].MessageInterval = MessageInterval
- end
- MESSAGE:New( Message, MessageDuration, MessageCategory ):ToClient( self )
- else
- if self:GetClientGroupDCSUnit() and not self:GetClientGroupDCSUnit():inAir() then
- if timer.getTime() - self.Messages[MessageID].MessageTime >= self.Messages[MessageID].MessageDuration + 10 then
- MESSAGE:New( Message, MessageDuration , MessageCategory):ToClient( self )
- self.Messages[MessageID].MessageTime = timer.getTime()
- end
- else
- if timer.getTime() - self.Messages[MessageID].MessageTime >= self.Messages[MessageID].MessageDuration + self.Messages[MessageID].MessageInterval then
- MESSAGE:New( Message, MessageDuration, MessageCategory ):ToClient( self )
- self.Messages[MessageID].MessageTime = timer.getTime()
- end
- end
- end
- else
- MESSAGE:New( Message, MessageDuration, MessageCategory ):ToClient( self )
- end
- end
-end
---- This module contains the STATIC class.
---
--- 1) @{Static#STATIC} class, extends @{Positionable#POSITIONABLE}
--- ===============================================================
--- Statics are **Static Units** defined within the Mission Editor.
--- Note that Statics are almost the same as Units, but they don't have a controller.
--- The @{Static#STATIC} class is a wrapper class to handle the DCS Static objects:
---
--- * Wraps the DCS Static objects.
--- * Support all DCS Static APIs.
--- * Enhance with Static specific APIs not in the DCS API set.
---
--- 1.1) STATIC reference methods
--- -----------------------------
--- For each DCS Static will have a STATIC wrapper object (instance) within the _@{DATABASE} object.
--- This is done at the beginning of the mission (when the mission starts).
---
--- The STATIC class does not contain a :New() method, rather it provides :Find() methods to retrieve the object reference
--- using the Static Name.
---
--- Another thing to know is that STATIC objects do not "contain" the DCS Static object.
--- The STATIc methods will reference the DCS Static object by name when it is needed during API execution.
--- If the DCS Static object does not exist or is nil, the STATIC methods will return nil and log an exception in the DCS.log file.
---
--- The STATIc class provides the following functions to retrieve quickly the relevant STATIC instance:
---
--- * @{#STATIC.FindByName}(): Find a STATIC instance from the _DATABASE object using a DCS Static name.
---
--- IMPORTANT: ONE SHOULD NEVER SANATIZE these STATIC OBJECT REFERENCES! (make the STATIC object references nil).
---
--- @module Static
--- @author FlightControl
-
-
-
-
-
-
---- The STATIC class
--- @type STATIC
--- @extends Wrapper.Positionable#POSITIONABLE
-STATIC = {
- ClassName = "STATIC",
-}
-
-
---- Finds a STATIC from the _DATABASE using the relevant Static Name.
--- As an optional parameter, a briefing text can be given also.
--- @param #STATIC self
--- @param #string StaticName Name of the DCS **Static** as defined within the Mission Editor.
--- @param #boolean RaiseError Raise an error if not found.
--- @return #STATIC
-function STATIC:FindByName( StaticName, RaiseError )
- local StaticFound = _DATABASE:FindStatic( StaticName )
-
- self.StaticName = StaticName
-
- if StaticFound then
- StaticFound:F3( { StaticName } )
-
- return StaticFound
- end
-
- if RaiseError == nil or RaiseError == true then
- error( "STATIC not found for: " .. StaticName )
- end
-
- return nil
-end
-
-function STATIC:Register( StaticName )
- local self = BASE:Inherit( self, POSITIONABLE:New( StaticName ) )
- self.StaticName = StaticName
- return self
-end
-
-
-function STATIC:GetDCSObject()
- local DCSStatic = StaticObject.getByName( self.StaticName )
-
- if DCSStatic then
- return DCSStatic
- end
-
- return nil
-end
-
-function STATIC:GetThreatLevel()
-
- return 1, "Static"
-end--- This module contains the AIRBASE classes.
---
--- ===
---
--- 1) @{Airbase#AIRBASE} class, extends @{Positionable#POSITIONABLE}
--- =================================================================
--- The @{AIRBASE} class is a wrapper class to handle the DCS Airbase objects:
---
--- * Support all DCS Airbase APIs.
--- * Enhance with Airbase specific APIs not in the DCS Airbase API set.
---
---
--- 1.1) AIRBASE reference methods
--- ------------------------------
--- For each DCS Airbase object alive within a running mission, a AIRBASE wrapper object (instance) will be created within the _@{DATABASE} object.
--- This is done at the beginning of the mission (when the mission starts).
---
--- The AIRBASE class **does not contain a :New()** method, rather it provides **:Find()** methods to retrieve the object reference
--- using the DCS Airbase or the DCS AirbaseName.
---
--- Another thing to know is that AIRBASE objects do not "contain" the DCS Airbase object.
--- The AIRBASE methods will reference the DCS Airbase object by name when it is needed during API execution.
--- If the DCS Airbase object does not exist or is nil, the AIRBASE methods will return nil and log an exception in the DCS.log file.
---
--- The AIRBASE class provides the following functions to retrieve quickly the relevant AIRBASE instance:
---
--- * @{#AIRBASE.Find}(): Find a AIRBASE instance from the _DATABASE object using a DCS Airbase object.
--- * @{#AIRBASE.FindByName}(): Find a AIRBASE instance from the _DATABASE object using a DCS Airbase name.
---
--- IMPORTANT: ONE SHOULD NEVER SANATIZE these AIRBASE OBJECT REFERENCES! (make the AIRBASE object references nil).
---
--- 1.2) DCS AIRBASE APIs
--- ---------------------
--- The DCS Airbase APIs are used extensively within MOOSE. The AIRBASE class has for each DCS Airbase API a corresponding method.
--- To be able to distinguish easily in your code the difference between a AIRBASE API call and a DCS Airbase API call,
--- the first letter of the method is also capitalized. So, by example, the DCS Airbase method @{DCSWrapper.Airbase#Airbase.getName}()
--- is implemented in the AIRBASE class as @{#AIRBASE.GetName}().
---
--- More functions will be added
--- ----------------------------
--- During the MOOSE development, more functions will be added.
---
--- @module Airbase
--- @author FlightControl
-
-
-
-
-
---- The AIRBASE class
--- @type AIRBASE
--- @extends Wrapper.Positionable#POSITIONABLE
-AIRBASE = {
- ClassName="AIRBASE",
- CategoryName = {
- [Airbase.Category.AIRDROME] = "Airdrome",
- [Airbase.Category.HELIPAD] = "Helipad",
- [Airbase.Category.SHIP] = "Ship",
- },
- }
-
--- Registration.
-
---- Create a new AIRBASE from DCSAirbase.
--- @param #AIRBASE self
--- @param #string AirbaseName The name of the airbase.
--- @return Wrapper.Airbase#AIRBASE
-function AIRBASE:Register( AirbaseName )
-
- local self = BASE:Inherit( self, POSITIONABLE:New( AirbaseName ) )
- self.AirbaseName = AirbaseName
- return self
-end
-
--- Reference methods.
-
---- Finds a AIRBASE from the _DATABASE using a DCSAirbase object.
--- @param #AIRBASE self
--- @param Dcs.DCSWrapper.Airbase#Airbase DCSAirbase An existing DCS Airbase object reference.
--- @return Wrapper.Airbase#AIRBASE self
-function AIRBASE:Find( DCSAirbase )
-
- local AirbaseName = DCSAirbase:getName()
- local AirbaseFound = _DATABASE:FindAirbase( AirbaseName )
- return AirbaseFound
-end
-
---- Find a AIRBASE in the _DATABASE using the name of an existing DCS Airbase.
--- @param #AIRBASE self
--- @param #string AirbaseName The Airbase Name.
--- @return Wrapper.Airbase#AIRBASE self
-function AIRBASE:FindByName( AirbaseName )
-
- local AirbaseFound = _DATABASE:FindAirbase( AirbaseName )
- return AirbaseFound
-end
-
-function AIRBASE:GetDCSObject()
- local DCSAirbase = Airbase.getByName( self.AirbaseName )
-
- if DCSAirbase then
- return DCSAirbase
- end
-
- return nil
-end
-
-
-
---- This module contains the SCENERY class.
---
--- 1) @{Scenery#SCENERY} class, extends @{Positionable#POSITIONABLE}
--- ===============================================================
--- Scenery objects are defined on the map.
--- The @{Scenery#SCENERY} class is a wrapper class to handle the DCS Scenery objects:
---
--- * Wraps the DCS Scenery objects.
--- * Support all DCS Scenery APIs.
--- * Enhance with Scenery specific APIs not in the DCS API set.
---
--- @module Scenery
--- @author FlightControl
-
-
-
---- The SCENERY class
--- @type SCENERY
--- @extends Wrapper.Positionable#POSITIONABLE
-SCENERY = {
- ClassName = "SCENERY",
-}
-
-
-function SCENERY:Register( SceneryName, SceneryObject )
- local self = BASE:Inherit( self, POSITIONABLE:New( SceneryName ) )
- self.SceneryName = SceneryName
- self.SceneryObject = SceneryObject
- return self
-end
-
-function SCENERY:GetDCSObject()
- return self.SceneryObject
-end
-
-function SCENERY:GetThreatLevel()
-
- return 0, "Scenery"
-end
---- Single-Player:**Yes** / Multi-Player:**Yes** / Core:**Yes** -- **Administer the scoring of player achievements,
--- and create a CSV file logging the scoring events for use at team or squadron websites.**
---
--- 
---
--- ===
---
--- The @{#SCORING} class administers the scoring of player achievements,
--- and creates a CSV file logging the scoring events and results for use at team or squadron websites.
---
--- SCORING automatically calculates the threat level of the objects hit and destroyed by players,
--- which can be @{Unit}, @{Static) and @{Scenery} objects.
---
--- Positive score points are granted when enemy or neutral targets are destroyed.
--- Negative score points or penalties are given when a friendly target is hit or destroyed.
--- This brings a lot of dynamism in the scoring, where players need to take care to inflict damage on the right target.
--- By default, penalties weight heavier in the scoring, to ensure that players don't commit fratricide.
--- The total score of the player is calculated by **adding the scores minus the penalties**.
---
--- 
---
--- The score value is calculated based on the **threat level of the player** and the **threat level of the target**.
--- A calculated score takes the threat level of the target divided by a balanced threat level of the player unit.
--- As such, if the threat level of the target is high, and the player threat level is low, a higher score will be given than
--- if the threat level of the player would be high too.
---
--- 
---
--- When multiple players hit the same target, and finally succeed in destroying the target, then each player who contributed to the target
--- destruction, will receive a score. This is important for targets that require significant damage before it can be destroyed, like
--- ships or heavy planes.
---
--- 
---
--- Optionally, the score values can be **scaled** by a **scale**. Specific scales can be set for positive cores or negative penalties.
--- The default range of the scores granted is a value between 0 and 10. The default range of penalties given is a value between 0 and 30.
---
--- 
---
--- **Additional scores** can be granted to **specific objects**, when the player(s) destroy these objects.
---
--- 
---
--- Various @{Zone}s can be defined for which scores are also granted when objects in that @{Zone} are destroyed.
--- This is **specifically useful** to designate **scenery targets on the map** that will generate points when destroyed.
---
--- With a small change in MissionScripting.lua, the scoring results can also be logged in a **CSV file**.
--- These CSV files can be used to:
---
--- * Upload scoring to a database or a BI tool to publish the scoring results to the player community.
--- * Upload scoring in an (online) Excel like tool, using pivot tables and pivot charts to show mission results.
--- * Share scoring amoung players after the mission to discuss mission results.
---
--- Scores can be **reported**. **Menu options** are automatically added to **each player group** when a player joins a client slot or a CA unit.
--- Use the radio menu F10 to consult the scores while running the mission.
--- Scores can be reported for your user, or an overall score can be reported of all players currently active in the mission.
---
--- # 1) @{Scoring#SCORING} class, extends @{Base#BASE}
---
--- ## 1.1) Set the destroy score or penalty scale
---
--- Score scales can be set for scores granted when enemies or friendlies are destroyed.
--- Use the method @{#SCORING.SetScaleDestroyScore}() to set the scale of enemy destroys (positive destroys).
--- Use the method @{#SCORING.SetScaleDestroyPenalty}() to set the scale of friendly destroys (negative destroys).
---
--- local Scoring = SCORING:New( "Scoring File" )
--- Scoring:SetScaleDestroyScore( 10 )
--- Scoring:SetScaleDestroyPenalty( 40 )
---
--- The above sets the scale for valid scores to 10. So scores will be given in a scale from 0 to 10.
--- The penalties will be given in a scale from 0 to 40.
---
--- ## 1.2) Define special targets that will give extra scores.
---
--- Special targets can be set that will give extra scores to the players when these are destroyed.
--- Use the methods @{#SCORING.AddUnitScore}() and @{#SCORING.RemoveUnitScore}() to specify a special additional score for a specific @{Unit}s.
--- Use the methods @{#SCORING.AddStaticScore}() and @{#SCORING.RemoveStaticScore}() to specify a special additional score for a specific @{Static}s.
--- Use the method @{#SCORING.SetGroupGroup}() to specify a special additional score for a specific @{Group}s.
---
--- local Scoring = SCORING:New( "Scoring File" )
--- Scoring:AddUnitScore( UNIT:FindByName( "Unit #001" ), 200 )
--- Scoring:AddStaticScore( STATIC:FindByName( "Static #1" ), 100 )
---
--- The above grants an additional score of 200 points for Unit #001 and an additional 100 points of Static #1 if these are destroyed.
--- Note that later in the mission, one can remove these scores set, for example, when the a goal achievement time limit is over.
--- For example, this can be done as follows:
---
--- Scoring:RemoveUnitScore( UNIT:FindByName( "Unit #001" ) )
---
--- ## 1.3) Define destruction zones that will give extra scores.
---
--- Define zones of destruction. Any object destroyed within the zone of the given category will give extra points.
--- Use the method @{#SCORING.AddZoneScore}() to add a @{Zone} for additional scoring.
--- Use the method @{#SCORING.RemoveZoneScore}() to remove a @{Zone} for additional scoring.
--- There are interesting variations that can be achieved with this functionality. For example, if the @{Zone} is a @{Zone#ZONE_UNIT},
--- then the zone is a moving zone, and anything destroyed within that @{Zone} will generate points.
--- The other implementation could be to designate a scenery target (a building) in the mission editor surrounded by a @{Zone},
--- just large enough around that building.
---
--- ## 1.4) Add extra Goal scores upon an event or a condition.
---
--- A mission has goals and achievements. The scoring system provides an API to set additional scores when a goal or achievement event happens.
--- Use the method @{#SCORING.AddGoalScore}() to add a score for a Player at any time in your mission.
---
--- ## 1.5) Configure fratricide level.
---
--- When a player commits too much damage to friendlies, his penalty score will reach a certain level.
--- Use the method @{#SCORING.SetFratricide}() to define the level when a player gets kicked.
--- By default, the fratricide level is the default penalty mutiplier * 2 for the penalty score.
---
--- ## 1.6) Penalty score when a player changes the coalition.
---
--- When a player changes the coalition, he can receive a penalty score.
--- Use the method @{#SCORING.SetCoalitionChangePenalty}() to define the penalty when a player changes coalition.
--- By default, the penalty for changing coalition is the default penalty scale.
---
--- ## 1.8) Define output CSV files.
---
--- The CSV file is given the name of the string given in the @{#SCORING.New}{} constructor, followed by the .csv extension.
--- The file is incrementally saved in the **\\Saved Games\\DCS\\Logs** folder, and has a time stamp indicating each mission run.
--- See the following example:
---
--- local ScoringFirstMission = SCORING:New( "FirstMission" )
--- local ScoringSecondMission = SCORING:New( "SecondMission" )
---
--- The above documents that 2 Scoring objects are created, ScoringFirstMission and ScoringSecondMission.
---
--- ## 1.9) Configure messages.
---
--- When players hit or destroy targets, messages are sent.
--- Various methods exist to configure:
---
--- * Which messages are sent upon the event.
--- * Which audience receives the message.
---
--- ### 1.9.1) Configure the messages sent upon the event.
---
--- Use the following methods to configure when to send messages. By default, all messages are sent.
---
--- * @{#SCORING.SetMessagesHit}(): Configure to send messages after a target has been hit.
--- * @{#SCORING.SetMessagesDestroy}(): Configure to send messages after a target has been destroyed.
--- * @{#SCORING.SetMessagesAddon}(): Configure to send messages for additional score, after a target has been destroyed.
--- * @{#SCORING.SetMessagesZone}(): Configure to send messages for additional score, after a target has been destroyed within a given zone.
---
--- ### 1.9.2) Configure the audience of the messages.
---
--- Use the following methods to configure the audience of the messages. By default, the messages are sent to all players in the mission.
---
--- * @{#SCORING.SetMessagesToAll}(): Configure to send messages to all players.
--- * @{#SCORING.SetMessagesToCoalition}(): Configure to send messages to only those players within the same coalition as the player.
---
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-02-26: Initial class and API.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **Wingthor (TAW)**: Testing & Advice.
--- * **Dutch-Baron (TAW)**: Testing & Advice.
--- * **[Whisper](http://forums.eagle.ru/member.php?u=3829): Testing and Advice.
---
--- ### Authors:
---
--- * **FlightControl**: Concept, Design & Programming.
---
--- @module Scoring
-
-
---- The Scoring class
--- @type SCORING
--- @field Players A collection of the current players that have joined the game.
--- @extends Core.Base#BASE
-SCORING = {
- ClassName = "SCORING",
- ClassID = 0,
- Players = {},
-}
-
-local _SCORINGCoalition =
- {
- [1] = "Red",
- [2] = "Blue",
- }
-
-local _SCORINGCategory =
- {
- [Unit.Category.AIRPLANE] = "Plane",
- [Unit.Category.HELICOPTER] = "Helicopter",
- [Unit.Category.GROUND_UNIT] = "Vehicle",
- [Unit.Category.SHIP] = "Ship",
- [Unit.Category.STRUCTURE] = "Structure",
- }
-
---- Creates a new SCORING object to administer the scoring achieved by players.
--- @param #SCORING self
--- @param #string GameName The name of the game. This name is also logged in the CSV score file.
--- @return #SCORING self
--- @usage
--- -- Define a new scoring object for the mission Gori Valley.
--- ScoringObject = SCORING:New( "Gori Valley" )
-function SCORING:New( GameName )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, BASE:New() ) -- #SCORING
-
- if GameName then
- self.GameName = GameName
- else
- error( "A game name must be given to register the scoring results" )
- end
-
-
- -- Additional Object scores
- self.ScoringObjects = {}
-
- -- Additional Zone scores.
- self.ScoringZones = {}
-
- -- Configure Messages
- self:SetMessagesToAll()
- self:SetMessagesHit( true )
- self:SetMessagesDestroy( true )
- self:SetMessagesScore( true )
- self:SetMessagesZone( true )
-
- -- Scales
- self:SetScaleDestroyScore( 10 )
- self:SetScaleDestroyPenalty( 30 )
-
- -- Default fratricide penalty level (maximum penalty that can be assigned to a player before he gets kicked).
- self:SetFratricide( self.ScaleDestroyPenalty * 3 )
-
- -- Default penalty when a player changes coalition.
- self:SetCoalitionChangePenalty( self.ScaleDestroyPenalty )
-
- -- Event handlers
- self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash )
- self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash )
- self:HandleEvent( EVENTS.Hit, self._EventOnHit )
- self:HandleEvent( EVENTS.PlayerEnterUnit )
- self:HandleEvent( EVENTS.PlayerLeaveUnit )
-
- -- Create the CSV file.
- self:OpenCSV( GameName )
-
- return self
-
-end
-
---- Set the scale for scoring valid destroys (enemy destroys).
--- A default calculated score is a value between 1 and 10.
--- The scale magnifies the scores given to the players.
--- @param #SCORING self
--- @param #number Scale The scale of the score given.
-function SCORING:SetScaleDestroyScore( Scale )
-
- self.ScaleDestroyScore = Scale
-
- return self
-end
-
---- Set the scale for scoring penalty destroys (friendly destroys).
--- A default calculated penalty is a value between 1 and 10.
--- The scale magnifies the scores given to the players.
--- @param #SCORING self
--- @param #number Scale The scale of the score given.
--- @return #SCORING
-function SCORING:SetScaleDestroyPenalty( Scale )
-
- self.ScaleDestroyPenalty = Scale
-
- return self
-end
-
---- Add a @{Unit} for additional scoring when the @{Unit} is destroyed.
--- Note that if there was already a @{Unit} declared within the scoring with the same name,
--- then the old @{Unit} will be replaced with the new @{Unit}.
--- @param #SCORING self
--- @param Wrapper.Unit#UNIT ScoreUnit The @{Unit} for which the Score needs to be given.
--- @param #number Score The Score value.
--- @return #SCORING
-function SCORING:AddUnitScore( ScoreUnit, Score )
-
- local UnitName = ScoreUnit:GetName()
-
- self.ScoringObjects[UnitName] = Score
-
- return self
-end
-
---- Removes a @{Unit} for additional scoring when the @{Unit} is destroyed.
--- @param #SCORING self
--- @param Wrapper.Unit#UNIT ScoreUnit The @{Unit} for which the Score needs to be given.
--- @return #SCORING
-function SCORING:RemoveUnitScore( ScoreUnit )
-
- local UnitName = ScoreUnit:GetName()
-
- self.ScoringObjects[UnitName] = nil
-
- return self
-end
-
---- Add a @{Static} for additional scoring when the @{Static} is destroyed.
--- Note that if there was already a @{Static} declared within the scoring with the same name,
--- then the old @{Static} will be replaced with the new @{Static}.
--- @param #SCORING self
--- @param Wrapper.Static#UNIT ScoreStatic The @{Static} for which the Score needs to be given.
--- @param #number Score The Score value.
--- @return #SCORING
-function SCORING:AddStaticScore( ScoreStatic, Score )
-
- local StaticName = ScoreStatic:GetName()
-
- self.ScoringObjects[StaticName] = Score
-
- return self
-end
-
---- Removes a @{Static} for additional scoring when the @{Static} is destroyed.
--- @param #SCORING self
--- @param Wrapper.Static#UNIT ScoreStatic The @{Static} for which the Score needs to be given.
--- @return #SCORING
-function SCORING:RemoveStaticScore( ScoreStatic )
-
- local StaticName = ScoreStatic:GetName()
-
- self.ScoringObjects[StaticName] = nil
-
- return self
-end
-
-
---- Specify a special additional score for a @{Group}.
--- @param #SCORING self
--- @param Wrapper.Group#GROUP ScoreGroup The @{Group} for which each @{Unit} a Score is given.
--- @param #number Score The Score value.
--- @return #SCORING
-function SCORING:AddScoreGroup( ScoreGroup, Score )
-
- local ScoreUnits = ScoreGroup:GetUnits()
-
- for ScoreUnitID, ScoreUnit in pairs( ScoreUnits ) do
- local UnitName = ScoreUnit:GetName()
- self.ScoringObjects[UnitName] = Score
- end
-
- return self
-end
-
---- Add a @{Zone} to define additional scoring when any object is destroyed in that zone.
--- Note that if a @{Zone} with the same name is already within the scoring added, the @{Zone} (type) and Score will be replaced!
--- This allows for a dynamic destruction zone evolution within your mission.
--- @param #SCORING self
--- @param Core.Zone#ZONE_BASE ScoreZone The @{Zone} which defines the destruction score perimeters.
--- Note that a zone can be a polygon or a moving zone.
--- @param #number Score The Score value.
--- @return #SCORING
-function SCORING:AddZoneScore( ScoreZone, Score )
-
- local ZoneName = ScoreZone:GetName()
-
- self.ScoringZones[ZoneName] = {}
- self.ScoringZones[ZoneName].ScoreZone = ScoreZone
- self.ScoringZones[ZoneName].Score = Score
-
- return self
-end
-
---- Remove a @{Zone} for additional scoring.
--- The scoring will search if any @{Zone} is added with the given name, and will remove that zone from the scoring.
--- This allows for a dynamic destruction zone evolution within your mission.
--- @param #SCORING self
--- @param Core.Zone#ZONE_BASE ScoreZone The @{Zone} which defines the destruction score perimeters.
--- Note that a zone can be a polygon or a moving zone.
--- @return #SCORING
-function SCORING:RemoveZoneScore( ScoreZone )
-
- local ZoneName = ScoreZone:GetName()
-
- self.ScoringZones[ZoneName] = nil
-
- return self
-end
-
-
---- Configure to send messages after a target has been hit.
--- @param #SCORING self
--- @param #boolean OnOff If true is given, the messages are sent.
--- @return #SCORING
-function SCORING:SetMessagesHit( OnOff )
-
- self.MessagesHit = OnOff
- return self
-end
-
---- If to send messages after a target has been hit.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesHit()
-
- return self.MessagesHit
-end
-
---- Configure to send messages after a target has been destroyed.
--- @param #SCORING self
--- @param #boolean OnOff If true is given, the messages are sent.
--- @return #SCORING
-function SCORING:SetMessagesDestroy( OnOff )
-
- self.MessagesDestroy = OnOff
- return self
-end
-
---- If to send messages after a target has been destroyed.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesDestroy()
-
- return self.MessagesDestroy
-end
-
---- Configure to send messages after a target has been destroyed and receives additional scores.
--- @param #SCORING self
--- @param #boolean OnOff If true is given, the messages are sent.
--- @return #SCORING
-function SCORING:SetMessagesScore( OnOff )
-
- self.MessagesScore = OnOff
- return self
-end
-
---- If to send messages after a target has been destroyed and receives additional scores.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesScore()
-
- return self.MessagesScore
-end
-
---- Configure to send messages after a target has been hit in a zone, and additional score is received.
--- @param #SCORING self
--- @param #boolean OnOff If true is given, the messages are sent.
--- @return #SCORING
-function SCORING:SetMessagesZone( OnOff )
-
- self.MessagesZone = OnOff
- return self
-end
-
---- If to send messages after a target has been hit in a zone, and additional score is received.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesZone()
-
- return self.MessagesZone
-end
-
---- Configure to send messages to all players.
--- @param #SCORING self
--- @return #SCORING
-function SCORING:SetMessagesToAll()
-
- self.MessagesAudience = 1
- return self
-end
-
---- If to send messages to all players.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesToAll()
-
- return self.MessagesAudience == 1
-end
-
---- Configure to send messages to only those players within the same coalition as the player.
--- @param #SCORING self
--- @return #SCORING
-function SCORING:SetMessagesToCoalition()
-
- self.MessagesAudience = 2
- return self
-end
-
---- If to send messages to only those players within the same coalition as the player.
--- @param #SCORING self
--- @return #boolean
-function SCORING:IfMessagesToCoalition()
-
- return self.MessagesAudience == 2
-end
-
-
---- When a player commits too much damage to friendlies, his penalty score will reach a certain level.
--- Use this method to define the level when a player gets kicked.
--- By default, the fratricide level is the default penalty mutiplier * 2 for the penalty score.
--- @param #SCORING self
--- @param #number Fratricide The amount of maximum penalty that may be inflicted by a friendly player before he gets kicked.
--- @return #SCORING
-function SCORING:SetFratricide( Fratricide )
-
- self.Fratricide = Fratricide
- return self
-end
-
-
---- When a player changes the coalition, he can receive a penalty score.
--- Use the method @{#SCORING.SetCoalitionChangePenalty}() to define the penalty when a player changes coalition.
--- By default, the penalty for changing coalition is the default penalty scale.
--- @param #SCORING self
--- @param #number CoalitionChangePenalty The amount of penalty that is given.
--- @return #SCORING
-function SCORING:SetCoalitionChangePenalty( CoalitionChangePenalty )
-
- self.CoalitionChangePenalty = CoalitionChangePenalty
- return self
-end
-
-
---- Add a new player entering a Unit.
--- @param #SCORING self
--- @param Wrapper.Unit#UNIT UnitData
-function SCORING:_AddPlayerFromUnit( UnitData )
- self:F( UnitData )
-
- if UnitData:IsAlive() then
- local UnitName = UnitData:GetName()
- local PlayerName = UnitData:GetPlayerName()
- local UnitDesc = UnitData:GetDesc()
- local UnitCategory = UnitDesc.category
- local UnitCoalition = UnitData:GetCoalition()
- local UnitTypeName = UnitData:GetTypeName()
- local UnitThreatLevel, UnitThreatType = UnitData:GetThreatLevel()
-
- self:T( { PlayerName, UnitName, UnitCategory, UnitCoalition, UnitTypeName } )
-
- if self.Players[PlayerName] == nil then -- I believe this is the place where a Player gets a life in a mission when he enters a unit ...
- self.Players[PlayerName] = {}
- self.Players[PlayerName].Hit = {}
- self.Players[PlayerName].Destroy = {}
- self.Players[PlayerName].Goals = {}
- self.Players[PlayerName].Mission = {}
-
- -- for CategoryID, CategoryName in pairs( SCORINGCategory ) do
- -- self.Players[PlayerName].Hit[CategoryID] = {}
- -- self.Players[PlayerName].Destroy[CategoryID] = {}
- -- end
- self.Players[PlayerName].HitPlayers = {}
- self.Players[PlayerName].Score = 0
- self.Players[PlayerName].Penalty = 0
- self.Players[PlayerName].PenaltyCoalition = 0
- self.Players[PlayerName].PenaltyWarning = 0
- end
-
- if not self.Players[PlayerName].UnitCoalition then
- self.Players[PlayerName].UnitCoalition = UnitCoalition
- else
- if self.Players[PlayerName].UnitCoalition ~= UnitCoalition then
- self.Players[PlayerName].Penalty = self.Players[PlayerName].Penalty + 50
- self.Players[PlayerName].PenaltyCoalition = self.Players[PlayerName].PenaltyCoalition + 1
- MESSAGE:New( "Player '" .. PlayerName .. "' changed coalition from " .. _SCORINGCoalition[self.Players[PlayerName].UnitCoalition] .. " to " .. _SCORINGCoalition[UnitCoalition] ..
- "(changed " .. self.Players[PlayerName].PenaltyCoalition .. " times the coalition). 50 Penalty points added.",
- 2
- ):ToAll()
- self:ScoreCSV( PlayerName, "", "COALITION_PENALTY", 1, -50, self.Players[PlayerName].UnitName, _SCORINGCoalition[self.Players[PlayerName].UnitCoalition], _SCORINGCategory[self.Players[PlayerName].UnitCategory], self.Players[PlayerName].UnitType,
- UnitName, _SCORINGCoalition[UnitCoalition], _SCORINGCategory[UnitCategory], UnitData:GetTypeName() )
- end
- end
- self.Players[PlayerName].UnitName = UnitName
- self.Players[PlayerName].UnitCoalition = UnitCoalition
- self.Players[PlayerName].UnitCategory = UnitCategory
- self.Players[PlayerName].UnitType = UnitTypeName
- self.Players[PlayerName].UNIT = UnitData
- self.Players[PlayerName].ThreatLevel = UnitThreatLevel
- self.Players[PlayerName].ThreatType = UnitThreatType
-
- if self.Players[PlayerName].Penalty > self.Fratricide * 0.50 then
- if self.Players[PlayerName].PenaltyWarning < 1 then
- MESSAGE:New( "Player '" .. PlayerName .. "': WARNING! If you continue to commit FRATRICIDE and have a PENALTY score higher than " .. self.Fratricide .. ", you will be COURT MARTIALED and DISMISSED from this mission! \nYour total penalty is: " .. self.Players[PlayerName].Penalty,
- 30
- ):ToAll()
- self.Players[PlayerName].PenaltyWarning = self.Players[PlayerName].PenaltyWarning + 1
- end
- end
-
- if self.Players[PlayerName].Penalty > self.Fratricide then
- UnitData:Destroy()
- MESSAGE:New( "Player '" .. PlayerName .. "' committed FRATRICIDE, he will be COURT MARTIALED and is DISMISSED from this mission!",
- 10
- ):ToAll()
- end
-
- end
-end
-
-
---- Add a goal score for a player.
--- The method takes the PlayerUnit for which the Goal score needs to be set.
--- The GoalTag is a string or identifier that is taken into the CSV file scoring log to identify the goal.
--- A free text can be given that is shown to the players.
--- The Score can be both positive and negative.
--- @param #SCORING self
--- @param Wrapper.Unit#UNIT PlayerUnit The @{Unit} of the Player. Other Properties for the scoring are taken from this PlayerUnit, like coalition, type etc.
--- @param #string GoalTag The string or identifier that is used in the CSV file to identify the goal (sort or group later in Excel).
--- @param #string Text A free text that is shown to the players.
--- @param #number Score The score can be both positive or negative ( Penalty ).
-function SCORING:AddGoalScore( PlayerUnit, GoalTag, Text, Score )
-
- local PlayerName = PlayerUnit:GetPlayerName()
-
- self:E( { PlayerUnit.UnitName, PlayerName, GoalTag, Text, Score } )
-
- -- PlayerName can be nil, if the Unit with the player crashed or due to another reason.
- if PlayerName then
- local PlayerData = self.Players[PlayerName]
-
- PlayerData.Goals[GoalTag] = PlayerData.Goals[GoalTag] or { Score = 0 }
- PlayerData.Goals[GoalTag].Score = PlayerData.Goals[GoalTag].Score + Score
- PlayerData.Score = PlayerData.Score + Score
-
- MESSAGE:New( Text, 30 ):ToAll()
-
- self:ScoreCSV( PlayerName, "", "GOAL_" .. string.upper( GoalTag ), 1, Score, PlayerUnit:GetName() )
- end
-end
-
-
---- Registers Scores the players completing a Mission Task.
--- @param #SCORING self
--- @param Tasking.Mission#MISSION Mission
--- @param Wrapper.Unit#UNIT PlayerUnit
--- @param #string Text
--- @param #number Score
-function SCORING:_AddMissionTaskScore( Mission, PlayerUnit, Text, Score )
-
- local PlayerName = PlayerUnit:GetPlayerName()
- local MissionName = Mission:GetName()
-
- self:E( { Mission:GetName(), PlayerUnit.UnitName, PlayerName, Text, Score } )
-
- -- PlayerName can be nil, if the Unit with the player crashed or due to another reason.
- if PlayerName then
- local PlayerData = self.Players[PlayerName]
-
- if not PlayerData.Mission[MissionName] then
- PlayerData.Mission[MissionName] = {}
- PlayerData.Mission[MissionName].ScoreTask = 0
- PlayerData.Mission[MissionName].ScoreMission = 0
- end
-
- self:T( PlayerName )
- self:T( PlayerData.Mission[MissionName] )
-
- PlayerData.Score = self.Players[PlayerName].Score + Score
- PlayerData.Mission[MissionName].ScoreTask = self.Players[PlayerName].Mission[MissionName].ScoreTask + Score
-
- MESSAGE:New( "Player '" .. PlayerName .. "' has " .. Text .. " in Mission '" .. MissionName .. "'. " ..
- Score .. " task score!",
- 30 ):ToAll()
-
- self:ScoreCSV( PlayerName, "", "TASK_" .. MissionName:gsub( ' ', '_' ), 1, Score, PlayerUnit:GetName() )
- end
-end
-
-
---- Registers Mission Scores for possible multiple players that contributed in the Mission.
--- @param #SCORING self
--- @param Tasking.Mission#MISSION Mission
--- @param Wrapper.Unit#UNIT PlayerUnit
--- @param #string Text
--- @param #number Score
-function SCORING:_AddMissionScore( Mission, Text, Score )
-
- local MissionName = Mission:GetName()
-
- self:E( { Mission, Text, Score } )
- self:E( self.Players )
-
- for PlayerName, PlayerData in pairs( self.Players ) do
-
- self:E( PlayerData )
- if PlayerData.Mission[MissionName] then
-
- PlayerData.Score = PlayerData.Score + Score
- PlayerData.Mission[MissionName].ScoreMission = PlayerData.Mission[MissionName].ScoreMission + Score
-
- MESSAGE:New( "Player '" .. PlayerName .. "' has " .. Text .. " in Mission '" .. MissionName .. "'. " ..
- Score .. " mission score!",
- 60 ):ToAll()
-
- self:ScoreCSV( PlayerName, "", "MISSION_" .. MissionName:gsub( ' ', '_' ), 1, Score )
- end
- end
-end
-
-
---- Handles the OnPlayerEnterUnit event for the scoring.
--- @param #SCORING self
--- @param Core.Event#EVENTDATA Event
-function SCORING:OnEventPlayerEnterUnit( Event )
- if Event.IniUnit then
- self:_AddPlayerFromUnit( Event.IniUnit )
- local Menu = MENU_GROUP:New( Event.IniGroup, 'Scoring' )
- local ReportGroupSummary = MENU_GROUP_COMMAND:New( Event.IniGroup, 'Summary report players in group', Menu, SCORING.ReportScoreGroupSummary, self, Event.IniGroup )
- local ReportGroupDetailed = MENU_GROUP_COMMAND:New( Event.IniGroup, 'Detailed report players in group', Menu, SCORING.ReportScoreGroupDetailed, self, Event.IniGroup )
- local ReportToAllSummary = MENU_GROUP_COMMAND:New( Event.IniGroup, 'Summary report all players', Menu, SCORING.ReportScoreAllSummary, self, Event.IniGroup )
- self:SetState( Event.IniUnit, "ScoringMenu", Menu )
- end
-end
-
---- Handles the OnPlayerLeaveUnit event for the scoring.
--- @param #SCORING self
--- @param Core.Event#EVENTDATA Event
-function SCORING:OnEventPlayerLeaveUnit( Event )
- if Event.IniUnit then
- local Menu = self:GetState( Event.IniUnit, "ScoringMenu" ) -- Core.Menu#MENU_GROUP
- if Menu then
- -- TODO: Check if this fixes #281.
- --Menu:Remove()
- end
- end
-end
-
-
---- Handles the OnHit event for the scoring.
--- @param #SCORING self
--- @param Core.Event#EVENTDATA Event
-function SCORING:_EventOnHit( Event )
- self:F( { Event } )
-
- local InitUnit = nil
- local InitUNIT = nil
- local InitUnitName = ""
- local InitGroup = nil
- local InitGroupName = ""
- local InitPlayerName = nil
-
- local InitCoalition = nil
- local InitCategory = nil
- local InitType = nil
- local InitUnitCoalition = nil
- local InitUnitCategory = nil
- local InitUnitType = nil
-
- local TargetUnit = nil
- local TargetUNIT = nil
- local TargetUnitName = ""
- local TargetGroup = nil
- local TargetGroupName = ""
- local TargetPlayerName = nil
-
- local TargetCoalition = nil
- local TargetCategory = nil
- local TargetType = nil
- local TargetUnitCoalition = nil
- local TargetUnitCategory = nil
- local TargetUnitType = nil
-
- if Event.IniDCSUnit then
-
- InitUnit = Event.IniDCSUnit
- InitUNIT = Event.IniUnit
- InitUnitName = Event.IniDCSUnitName
- InitGroup = Event.IniDCSGroup
- InitGroupName = Event.IniDCSGroupName
- InitPlayerName = Event.IniPlayerName
-
- InitCoalition = Event.IniCoalition
- --TODO: Workaround Client DCS Bug
- --InitCategory = InitUnit:getCategory()
- --InitCategory = InitUnit:getDesc().category
- InitCategory = Event.IniCategory
- InitType = Event.IniTypeName
-
- InitUnitCoalition = _SCORINGCoalition[InitCoalition]
- InitUnitCategory = _SCORINGCategory[InitCategory]
- InitUnitType = InitType
-
- self:T( { InitUnitName, InitGroupName, InitPlayerName, InitCoalition, InitCategory, InitType , InitUnitCoalition, InitUnitCategory, InitUnitType } )
- end
-
-
- if Event.TgtDCSUnit then
-
- TargetUnit = Event.TgtDCSUnit
- TargetUNIT = Event.TgtUnit
- TargetUnitName = Event.TgtDCSUnitName
- TargetGroup = Event.TgtDCSGroup
- TargetGroupName = Event.TgtDCSGroupName
- TargetPlayerName = Event.TgtPlayerName
-
- TargetCoalition = Event.TgtCoalition
- --TODO: Workaround Client DCS Bug
- --TargetCategory = TargetUnit:getCategory()
- --TargetCategory = TargetUnit:getDesc().category
- TargetCategory = Event.TgtCategory
- TargetType = Event.TgtTypeName
-
- TargetUnitCoalition = _SCORINGCoalition[TargetCoalition]
- TargetUnitCategory = _SCORINGCategory[TargetCategory]
- TargetUnitType = TargetType
-
- self:T( { TargetUnitName, TargetGroupName, TargetPlayerName, TargetCoalition, TargetCategory, TargetType, TargetUnitCoalition, TargetUnitCategory, TargetUnitType } )
- end
-
- if InitPlayerName ~= nil then -- It is a player that is hitting something
- self:_AddPlayerFromUnit( InitUNIT )
- if self.Players[InitPlayerName] then -- This should normally not happen, but i'll test it anyway.
- if TargetPlayerName ~= nil then -- It is a player hitting another player ...
- self:_AddPlayerFromUnit( TargetUNIT )
- end
-
- self:T( "Hitting Something" )
-
- -- What is he hitting?
- if TargetCategory then
-
- -- A target got hit, score it.
- -- Player contains the score data from self.Players[InitPlayerName]
- local Player = self.Players[InitPlayerName]
-
- -- Ensure there is a hit table per TargetCategory and TargetUnitName.
- Player.Hit[TargetCategory] = Player.Hit[TargetCategory] or {}
- Player.Hit[TargetCategory][TargetUnitName] = Player.Hit[TargetCategory][TargetUnitName] or {}
-
- -- PlayerHit contains the score counters and data per unit that was hit.
- local PlayerHit = Player.Hit[TargetCategory][TargetUnitName]
-
- PlayerHit.Score = PlayerHit.Score or 0
- PlayerHit.Penalty = PlayerHit.Penalty or 0
- PlayerHit.ScoreHit = PlayerHit.ScoreHit or 0
- PlayerHit.PenaltyHit = PlayerHit.PenaltyHit or 0
- PlayerHit.TimeStamp = PlayerHit.TimeStamp or 0
- PlayerHit.UNIT = PlayerHit.UNIT or TargetUNIT
- PlayerHit.ThreatLevel, PlayerHit.ThreatType = PlayerHit.UNIT:GetThreatLevel()
-
- -- Only grant hit scores if there was more than one second between the last hit.
- if timer.getTime() - PlayerHit.TimeStamp > 1 then
- PlayerHit.TimeStamp = timer.getTime()
-
- if TargetPlayerName ~= nil then -- It is a player hitting another player ...
-
- -- Ensure there is a Player to Player hit reference table.
- Player.HitPlayers[TargetPlayerName] = true
- end
-
- local Score = 0
-
- if InitCoalition then -- A coalition object was hit.
- if InitCoalition == TargetCoalition then
- Player.Penalty = Player.Penalty + 10
- PlayerHit.Penalty = PlayerHit.Penalty + 10
- PlayerHit.PenaltyHit = PlayerHit.PenaltyHit + 1
-
- if TargetPlayerName ~= nil then -- It is a player hitting another player ...
- MESSAGE
- :New( "Player '" .. InitPlayerName .. "' hit friendly player '" .. TargetPlayerName .. "' " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.PenaltyHit .. " times. " ..
- "Penalty: -" .. PlayerHit.Penalty .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- else
- MESSAGE
- :New( "Player '" .. InitPlayerName .. "' hit a friendly target " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.PenaltyHit .. " times. " ..
- "Penalty: -" .. PlayerHit.Penalty .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- end
- self:ScoreCSV( InitPlayerName, TargetPlayerName, "HIT_PENALTY", 1, -10, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- else
- Player.Score = Player.Score + 1
- PlayerHit.Score = PlayerHit.Score + 1
- PlayerHit.ScoreHit = PlayerHit.ScoreHit + 1
- if TargetPlayerName ~= nil then -- It is a player hitting another player ...
- MESSAGE
- :New( "Player '" .. InitPlayerName .. "' hit enemy player '" .. TargetPlayerName .. "' " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.ScoreHit .. " times. " ..
- "Score: " .. PlayerHit.Score .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- else
- MESSAGE
- :New( "Player '" .. InitPlayerName .. "' hit an enemy target " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.ScoreHit .. " times. " ..
- "Score: " .. PlayerHit.Score .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- end
- self:ScoreCSV( InitPlayerName, TargetPlayerName, "HIT_SCORE", 1, 1, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- end
- else -- A scenery object was hit.
- MESSAGE
- :New( "Player '" .. InitPlayerName .. "' hit a scenery object.",
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- self:ScoreCSV( InitPlayerName, "", "HIT_SCORE", 1, 0, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, "", "Scenery", TargetUnitType )
- end
- end
- end
- end
- elseif InitPlayerName == nil then -- It is an AI hitting a player???
-
- end
-
- -- It is a weapon initiated by a player, that is hitting something
- -- This seems to occur only with scenery and static objects.
- if Event.WeaponPlayerName ~= nil then
- self:_AddPlayerFromUnit( Event.WeaponUNIT )
- if self.Players[Event.WeaponPlayerName] then -- This should normally not happen, but i'll test it anyway.
- if TargetPlayerName ~= nil then -- It is a player hitting another player ...
- self:_AddPlayerFromUnit( TargetUNIT )
- end
-
- self:T( "Hitting Scenery" )
-
- -- What is he hitting?
- if TargetCategory then
-
- -- A scenery or static got hit, score it.
- -- Player contains the score data from self.Players[WeaponPlayerName]
- local Player = self.Players[Event.WeaponPlayerName]
-
- -- Ensure there is a hit table per TargetCategory and TargetUnitName.
- Player.Hit[TargetCategory] = Player.Hit[TargetCategory] or {}
- Player.Hit[TargetCategory][TargetUnitName] = Player.Hit[TargetCategory][TargetUnitName] or {}
-
- -- PlayerHit contains the score counters and data per unit that was hit.
- local PlayerHit = Player.Hit[TargetCategory][TargetUnitName]
-
- PlayerHit.Score = PlayerHit.Score or 0
- PlayerHit.Penalty = PlayerHit.Penalty or 0
- PlayerHit.ScoreHit = PlayerHit.ScoreHit or 0
- PlayerHit.PenaltyHit = PlayerHit.PenaltyHit or 0
- PlayerHit.TimeStamp = PlayerHit.TimeStamp or 0
- PlayerHit.UNIT = PlayerHit.UNIT or TargetUNIT
- PlayerHit.ThreatLevel, PlayerHit.ThreatType = PlayerHit.UNIT:GetThreatLevel()
-
- -- Only grant hit scores if there was more than one second between the last hit.
- if timer.getTime() - PlayerHit.TimeStamp > 1 then
- PlayerHit.TimeStamp = timer.getTime()
-
- local Score = 0
-
- if InitCoalition then -- A coalition object was hit, probably a static.
- if InitCoalition == TargetCoalition then
- -- TODO: Penalty according scale
- Player.Penalty = Player.Penalty + 10
- PlayerHit.Penalty = PlayerHit.Penalty + 10
- PlayerHit.PenaltyHit = PlayerHit.PenaltyHit + 1
-
- MESSAGE
- :New( "Player '" .. Event.WeaponPlayerName .. "' hit a friendly target " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.PenaltyHit .. " times. " ..
- "Penalty: -" .. PlayerHit.Penalty .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( Event.WeaponCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- self:ScoreCSV( Event.WeaponPlayerName, TargetPlayerName, "HIT_PENALTY", 1, -10, Event.WeaponName, Event.WeaponCoalition, Event.WeaponCategory, Event.WeaponTypeName, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- else
- Player.Score = Player.Score + 1
- PlayerHit.Score = PlayerHit.Score + 1
- PlayerHit.ScoreHit = PlayerHit.ScoreHit + 1
- MESSAGE
- :New( "Player '" .. Event.WeaponPlayerName .. "' hit an enemy target " ..
- TargetUnitCategory .. " ( " .. TargetType .. " ) " .. PlayerHit.ScoreHit .. " times. " ..
- "Score: " .. PlayerHit.Score .. ". Score Total:" .. Player.Score - Player.Penalty,
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( Event.WeaponCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- self:ScoreCSV( Event.WeaponPlayerName, TargetPlayerName, "HIT_SCORE", 1, 1, Event.WeaponName, Event.WeaponCoalition, Event.WeaponCategory, Event.WeaponTypeName, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- end
- else -- A scenery object was hit.
- MESSAGE
- :New( "Player '" .. Event.WeaponPlayerName .. "' hit a scenery object.",
- 2
- )
- :ToAllIf( self:IfMessagesHit() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesHit() and self:IfMessagesToCoalition() )
- self:ScoreCSV( Event.WeaponPlayerName, "", "HIT_SCORE", 1, 0, Event.WeaponName, Event.WeaponCoalition, Event.WeaponCategory, Event.WeaponTypeName, TargetUnitName, "", "Scenery", TargetUnitType )
- end
- end
- end
- end
- end
-end
-
---- Track DEAD or CRASH events for the scoring.
--- @param #SCORING self
--- @param Core.Event#EVENTDATA Event
-function SCORING:_EventOnDeadOrCrash( Event )
- self:F( { Event } )
-
- local TargetUnit = nil
- local TargetGroup = nil
- local TargetUnitName = ""
- local TargetGroupName = ""
- local TargetPlayerName = ""
- local TargetCoalition = nil
- local TargetCategory = nil
- local TargetType = nil
- local TargetUnitCoalition = nil
- local TargetUnitCategory = nil
- local TargetUnitType = nil
-
- if Event.IniDCSUnit then
-
- TargetUnit = Event.IniUnit
- TargetUnitName = Event.IniDCSUnitName
- TargetGroup = Event.IniDCSGroup
- TargetGroupName = Event.IniDCSGroupName
- TargetPlayerName = Event.IniPlayerName
-
- TargetCoalition = Event.IniCoalition
- --TargetCategory = TargetUnit:getCategory()
- --TargetCategory = TargetUnit:getDesc().category -- Workaround
- TargetCategory = Event.IniCategory
- TargetType = Event.IniTypeName
-
- TargetUnitCoalition = _SCORINGCoalition[TargetCoalition]
- TargetUnitCategory = _SCORINGCategory[TargetCategory]
- TargetUnitType = TargetType
-
- self:T( { TargetUnitName, TargetGroupName, TargetPlayerName, TargetCoalition, TargetCategory, TargetType } )
- end
-
- -- Player contains the score and reference data for the player.
- for PlayerName, Player in pairs( self.Players ) do
- if Player then -- This should normally not happen, but i'll test it anyway.
- self:T( "Something got destroyed" )
-
- -- Some variables
- local InitUnitName = Player.UnitName
- local InitUnitType = Player.UnitType
- local InitCoalition = Player.UnitCoalition
- local InitCategory = Player.UnitCategory
- local InitUnitCoalition = _SCORINGCoalition[InitCoalition]
- local InitUnitCategory = _SCORINGCategory[InitCategory]
-
- self:T( { InitUnitName, InitUnitType, InitUnitCoalition, InitCoalition, InitUnitCategory, InitCategory } )
-
- local Destroyed = false
-
- -- What is the player destroying?
- if Player and Player.Hit and Player.Hit[TargetCategory] and Player.Hit[TargetCategory][TargetUnitName] and Player.Hit[TargetCategory][TargetUnitName].TimeStamp ~= 0 then -- Was there a hit for this unit for this player before registered???
-
- local TargetThreatLevel = Player.Hit[TargetCategory][TargetUnitName].ThreatLevel
- local TargetThreatType = Player.Hit[TargetCategory][TargetUnitName].ThreatType
-
- Player.Destroy[TargetCategory] = Player.Destroy[TargetCategory] or {}
- Player.Destroy[TargetCategory][TargetType] = Player.Destroy[TargetCategory][TargetType] or {}
-
- -- PlayerDestroy contains the destroy score data per category and target type of the player.
- local TargetDestroy = Player.Destroy[TargetCategory][TargetType]
- TargetDestroy.Score = TargetDestroy.Score or 0
- TargetDestroy.ScoreDestroy = TargetDestroy.ScoreDestroy or 0
- TargetDestroy.Penalty = TargetDestroy.Penalty or 0
- TargetDestroy.PenaltyDestroy = TargetDestroy.PenaltyDestroy or 0
-
- if TargetCoalition then
- if InitCoalition == TargetCoalition then
- local ThreatLevelTarget = TargetThreatLevel
- local ThreatTypeTarget = TargetThreatType
- local ThreatLevelPlayer = Player.ThreatLevel / 10 + 1
- local ThreatPenalty = math.ceil( ( ThreatLevelTarget / ThreatLevelPlayer ) * self.ScaleDestroyPenalty / 10 )
- self:E( { ThreatLevel = ThreatPenalty, ThreatLevelTarget = ThreatLevelTarget, ThreatTypeTarget = ThreatTypeTarget, ThreatLevelPlayer = ThreatLevelPlayer } )
-
- Player.Penalty = Player.Penalty + ThreatPenalty
- TargetDestroy.Penalty = TargetDestroy.Penalty + ThreatPenalty
- TargetDestroy.PenaltyDestroy = TargetDestroy.PenaltyDestroy + 1
-
- if Player.HitPlayers[TargetPlayerName] then -- A player destroyed another player
- MESSAGE
- :New( "Player '" .. PlayerName .. "' destroyed friendly player '" .. TargetPlayerName .. "' " ..
- TargetUnitCategory .. " ( " .. ThreatTypeTarget .. " ) " .. TargetDestroy.PenaltyDestroy .. " times. " ..
- "Penalty: -" .. TargetDestroy.Penalty .. ". Score Total:" .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesDestroy() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesDestroy() and self:IfMessagesToCoalition() )
- else
- MESSAGE
- :New( "Player '" .. PlayerName .. "' destroyed a friendly target " ..
- TargetUnitCategory .. " ( " .. ThreatTypeTarget .. " ) " .. TargetDestroy.PenaltyDestroy .. " times. " ..
- "Penalty: -" .. TargetDestroy.Penalty .. ". Score Total:" .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesDestroy() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesDestroy() and self:IfMessagesToCoalition() )
- end
-
- Destroyed = true
- self:ScoreCSV( PlayerName, TargetPlayerName, "DESTROY_PENALTY", 1, ThreatPenalty, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- else
-
- local ThreatLevelTarget = TargetThreatLevel
- local ThreatTypeTarget = TargetThreatType
- local ThreatLevelPlayer = Player.ThreatLevel / 10 + 1
- local ThreatScore = math.ceil( ( ThreatLevelTarget / ThreatLevelPlayer ) * self.ScaleDestroyScore / 10 )
-
- self:E( { ThreatLevel = ThreatScore, ThreatLevelTarget = ThreatLevelTarget, ThreatTypeTarget = ThreatTypeTarget, ThreatLevelPlayer = ThreatLevelPlayer } )
-
- Player.Score = Player.Score + ThreatScore
- TargetDestroy.Score = TargetDestroy.Score + ThreatScore
- TargetDestroy.ScoreDestroy = TargetDestroy.ScoreDestroy + 1
- if Player.HitPlayers[TargetPlayerName] then -- A player destroyed another player
- MESSAGE
- :New( "Player '" .. PlayerName .. "' destroyed enemy player '" .. TargetPlayerName .. "' " ..
- TargetUnitCategory .. " ( " .. ThreatTypeTarget .. " ) " .. TargetDestroy.ScoreDestroy .. " times. " ..
- "Score: " .. TargetDestroy.Score .. ". Score Total:" .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesDestroy() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesDestroy() and self:IfMessagesToCoalition() )
- else
- MESSAGE
- :New( "Player '" .. PlayerName .. "' destroyed an enemy " ..
- TargetUnitCategory .. " ( " .. ThreatTypeTarget .. " ) " .. TargetDestroy.ScoreDestroy .. " times. " ..
- "Score: " .. TargetDestroy.Score .. ". Total:" .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesDestroy() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesDestroy() and self:IfMessagesToCoalition() )
- end
- Destroyed = true
- self:ScoreCSV( PlayerName, TargetPlayerName, "DESTROY_SCORE", 1, ThreatScore, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
-
- local UnitName = TargetUnit:GetName()
- local Score = self.ScoringObjects[UnitName]
- if Score then
- Player.Score = Player.Score + Score
- TargetDestroy.Score = TargetDestroy.Score + Score
- MESSAGE
- :New( "Special target '" .. TargetUnitCategory .. " ( " .. ThreatTypeTarget .. " ) " .. " destroyed! " ..
- "Player '" .. PlayerName .. "' receives an extra " .. Score .. " points! Total: " .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesScore() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesScore() and self:IfMessagesToCoalition() )
- self:ScoreCSV( PlayerName, TargetPlayerName, "DESTROY_SCORE", 1, Score, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- Destroyed = true
- end
-
- -- Check if there are Zones where the destruction happened.
- for ZoneName, ScoreZoneData in pairs( self.ScoringZones ) do
- self:E( { ScoringZone = ScoreZoneData } )
- local ScoreZone = ScoreZoneData.ScoreZone -- Core.Zone#ZONE_BASE
- local Score = ScoreZoneData.Score
- if ScoreZone:IsVec2InZone( TargetUnit:GetVec2() ) then
- Player.Score = Player.Score + Score
- TargetDestroy.Score = TargetDestroy.Score + Score
- MESSAGE
- :New( "Target destroyed in zone '" .. ScoreZone:GetName() .. "'." ..
- "Player '" .. PlayerName .. "' receives an extra " .. Score .. " points! " ..
- "Total: " .. Player.Score - Player.Penalty,
- 15 )
- :ToAllIf( self:IfMessagesZone() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesZone() and self:IfMessagesToCoalition() )
- self:ScoreCSV( PlayerName, TargetPlayerName, "DESTROY_SCORE", 1, Score, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- Destroyed = true
- end
- end
-
- end
- else
- -- Check if there are Zones where the destruction happened.
- for ZoneName, ScoreZoneData in pairs( self.ScoringZones ) do
- self:E( { ScoringZone = ScoreZoneData } )
- local ScoreZone = ScoreZoneData.ScoreZone -- Core.Zone#ZONE_BASE
- local Score = ScoreZoneData.Score
- if ScoreZone:IsVec2InZone( TargetUnit:GetVec2() ) then
- Player.Score = Player.Score + Score
- TargetDestroy.Score = TargetDestroy.Score + Score
- MESSAGE
- :New( "Scenery destroyed in zone '" .. ScoreZone:GetName() .. "'." ..
- "Player '" .. PlayerName .. "' receives an extra " .. Score .. " points! " ..
- "Total: " .. Player.Score - Player.Penalty,
- 15
- )
- :ToAllIf( self:IfMessagesZone() and self:IfMessagesToAll() )
- :ToCoalitionIf( InitCoalition, self:IfMessagesZone() and self:IfMessagesToCoalition() )
- Destroyed = true
- self:ScoreCSV( PlayerName, "", "DESTROY_SCORE", 1, Score, InitUnitName, InitUnitCoalition, InitUnitCategory, InitUnitType, TargetUnitName, "", "Scenery", TargetUnitType )
- end
- end
- end
-
- -- Delete now the hit cache if the target was destroyed.
- -- Otherwise points will be granted every time a target gets killed by the players that hit that target.
- -- This is only relevant for player to player destroys.
- if Destroyed then
- Player.Hit[TargetCategory][TargetUnitName].TimeStamp = 0
- end
- end
- end
- end
-end
-
-
---- Produce detailed report of player hit scores.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @return #string The report.
-function SCORING:ReportDetailedPlayerHits( PlayerName )
-
- local ScoreMessage = ""
- local PlayerScore = 0
- local PlayerPenalty = 0
-
- local PlayerData = self.Players[PlayerName]
- if PlayerData then -- This should normally not happen, but i'll test it anyway.
- self:T( "Score Player: " .. PlayerName )
-
- -- Some variables
- local InitUnitCoalition = _SCORINGCoalition[PlayerData.UnitCoalition]
- local InitUnitCategory = _SCORINGCategory[PlayerData.UnitCategory]
- local InitUnitType = PlayerData.UnitType
- local InitUnitName = PlayerData.UnitName
-
- local ScoreMessageHits = ""
- for CategoryID, CategoryName in pairs( _SCORINGCategory ) do
- self:T( CategoryName )
- if PlayerData.Hit[CategoryID] then
- self:T( "Hit scores exist for player " .. PlayerName )
- local Score = 0
- local ScoreHit = 0
- local Penalty = 0
- local PenaltyHit = 0
- for UnitName, UnitData in pairs( PlayerData.Hit[CategoryID] ) do
- Score = Score + UnitData.Score
- ScoreHit = ScoreHit + UnitData.ScoreHit
- Penalty = Penalty + UnitData.Penalty
- PenaltyHit = UnitData.PenaltyHit
- end
- local ScoreMessageHit = string.format( "%s:%d ", CategoryName, Score - Penalty )
- self:T( ScoreMessageHit )
- ScoreMessageHits = ScoreMessageHits .. ScoreMessageHit
- PlayerScore = PlayerScore + Score
- PlayerPenalty = PlayerPenalty + Penalty
- else
- --ScoreMessageHits = ScoreMessageHits .. string.format( "%s:%d ", string.format(CategoryName, 1, 1), 0 )
- end
- end
- if ScoreMessageHits ~= "" then
- ScoreMessage = "Hits: " .. ScoreMessageHits
- end
- end
-
- return ScoreMessage, PlayerScore, PlayerPenalty
-end
-
-
---- Produce detailed report of player destroy scores.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @return #string The report.
-function SCORING:ReportDetailedPlayerDestroys( PlayerName )
-
- local ScoreMessage = ""
- local PlayerScore = 0
- local PlayerPenalty = 0
-
- local PlayerData = self.Players[PlayerName]
- if PlayerData then -- This should normally not happen, but i'll test it anyway.
- self:T( "Score Player: " .. PlayerName )
-
- -- Some variables
- local InitUnitCoalition = _SCORINGCoalition[PlayerData.UnitCoalition]
- local InitUnitCategory = _SCORINGCategory[PlayerData.UnitCategory]
- local InitUnitType = PlayerData.UnitType
- local InitUnitName = PlayerData.UnitName
-
- local ScoreMessageDestroys = ""
- for CategoryID, CategoryName in pairs( _SCORINGCategory ) do
- if PlayerData.Destroy[CategoryID] then
- self:T( "Destroy scores exist for player " .. PlayerName )
- local Score = 0
- local ScoreDestroy = 0
- local Penalty = 0
- local PenaltyDestroy = 0
-
- for UnitName, UnitData in pairs( PlayerData.Destroy[CategoryID] ) do
- self:E( { UnitData = UnitData } )
- if UnitData ~= {} then
- Score = Score + UnitData.Score
- ScoreDestroy = ScoreDestroy + UnitData.ScoreDestroy
- Penalty = Penalty + UnitData.Penalty
- PenaltyDestroy = PenaltyDestroy + UnitData.PenaltyDestroy
- end
- end
-
- local ScoreMessageDestroy = string.format( " %s:%d ", CategoryName, Score - Penalty )
- self:T( ScoreMessageDestroy )
- ScoreMessageDestroys = ScoreMessageDestroys .. ScoreMessageDestroy
-
- PlayerScore = PlayerScore + Score
- PlayerPenalty = PlayerPenalty + Penalty
- else
- --ScoreMessageDestroys = ScoreMessageDestroys .. string.format( "%s:%d ", string.format(CategoryName, 1, 1), 0 )
- end
- end
- if ScoreMessageDestroys ~= "" then
- ScoreMessage = "Destroys: " .. ScoreMessageDestroys
- end
- end
-
- return ScoreMessage, PlayerScore, PlayerPenalty
-end
-
---- Produce detailed report of player penalty scores because of changing the coalition.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @return #string The report.
-function SCORING:ReportDetailedPlayerCoalitionChanges( PlayerName )
-
- local ScoreMessage = ""
- local PlayerScore = 0
- local PlayerPenalty = 0
-
- local PlayerData = self.Players[PlayerName]
- if PlayerData then -- This should normally not happen, but i'll test it anyway.
- self:T( "Score Player: " .. PlayerName )
-
- -- Some variables
- local InitUnitCoalition = _SCORINGCoalition[PlayerData.UnitCoalition]
- local InitUnitCategory = _SCORINGCategory[PlayerData.UnitCategory]
- local InitUnitType = PlayerData.UnitType
- local InitUnitName = PlayerData.UnitName
-
- local ScoreMessageCoalitionChangePenalties = ""
- if PlayerData.PenaltyCoalition ~= 0 then
- ScoreMessageCoalitionChangePenalties = ScoreMessageCoalitionChangePenalties .. string.format( " -%d (%d changed)", PlayerData.Penalty, PlayerData.PenaltyCoalition )
- PlayerPenalty = PlayerPenalty + PlayerData.Penalty
- end
- if ScoreMessageCoalitionChangePenalties ~= "" then
- ScoreMessage = ScoreMessage .. "Coalition Penalties: " .. ScoreMessageCoalitionChangePenalties
- end
- end
-
- return ScoreMessage, PlayerScore, PlayerPenalty
-end
-
---- Produce detailed report of player goal scores.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @return #string The report.
-function SCORING:ReportDetailedPlayerGoals( PlayerName )
-
- local ScoreMessage = ""
- local PlayerScore = 0
- local PlayerPenalty = 0
-
- local PlayerData = self.Players[PlayerName]
- if PlayerData then -- This should normally not happen, but i'll test it anyway.
- self:T( "Score Player: " .. PlayerName )
-
- -- Some variables
- local InitUnitCoalition = _SCORINGCoalition[PlayerData.UnitCoalition]
- local InitUnitCategory = _SCORINGCategory[PlayerData.UnitCategory]
- local InitUnitType = PlayerData.UnitType
- local InitUnitName = PlayerData.UnitName
-
- local ScoreMessageGoal = ""
- local ScoreGoal = 0
- local ScoreTask = 0
- for GoalName, GoalData in pairs( PlayerData.Goals ) do
- ScoreGoal = ScoreGoal + GoalData.Score
- ScoreMessageGoal = ScoreMessageGoal .. "'" .. GoalName .. "':" .. GoalData.Score .. "; "
- end
- PlayerScore = PlayerScore + ScoreGoal
-
- if ScoreMessageGoal ~= "" then
- ScoreMessage = "Goals: " .. ScoreMessageGoal
- end
- end
-
- return ScoreMessage, PlayerScore, PlayerPenalty
-end
-
---- Produce detailed report of player penalty scores because of changing the coalition.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @return #string The report.
-function SCORING:ReportDetailedPlayerMissions( PlayerName )
-
- local ScoreMessage = ""
- local PlayerScore = 0
- local PlayerPenalty = 0
-
- local PlayerData = self.Players[PlayerName]
- if PlayerData then -- This should normally not happen, but i'll test it anyway.
- self:T( "Score Player: " .. PlayerName )
-
- -- Some variables
- local InitUnitCoalition = _SCORINGCoalition[PlayerData.UnitCoalition]
- local InitUnitCategory = _SCORINGCategory[PlayerData.UnitCategory]
- local InitUnitType = PlayerData.UnitType
- local InitUnitName = PlayerData.UnitName
-
- local ScoreMessageMission = ""
- local ScoreMission = 0
- local ScoreTask = 0
- for MissionName, MissionData in pairs( PlayerData.Mission ) do
- ScoreMission = ScoreMission + MissionData.ScoreMission
- ScoreTask = ScoreTask + MissionData.ScoreTask
- ScoreMessageMission = ScoreMessageMission .. "'" .. MissionName .. "'; "
- end
- PlayerScore = PlayerScore + ScoreMission + ScoreTask
-
- if ScoreMessageMission ~= "" then
- ScoreMessage = "Tasks: " .. ScoreTask .. " Mission: " .. ScoreMission .. " ( " .. ScoreMessageMission .. ")"
- end
- end
-
- return ScoreMessage, PlayerScore, PlayerPenalty
-end
-
-
---- Report Group Score Summary
--- @param #SCORING self
--- @param Wrapper.Group#GROUP PlayerGroup The player group.
-function SCORING:ReportScoreGroupSummary( PlayerGroup )
-
- local PlayerMessage = ""
-
- self:T( "Report Score Group Summary" )
-
- local PlayerUnits = PlayerGroup:GetUnits()
- for UnitID, PlayerUnit in pairs( PlayerUnits ) do
- local PlayerUnit = PlayerUnit -- Wrapper.Unit#UNIT
- local PlayerName = PlayerUnit:GetPlayerName()
-
- if PlayerName then
-
- local ReportHits, ScoreHits, PenaltyHits = self:ReportDetailedPlayerHits( PlayerName )
- ReportHits = ReportHits ~= "" and "\n- " .. ReportHits or ReportHits
- self:E( { ReportHits, ScoreHits, PenaltyHits } )
-
- local ReportDestroys, ScoreDestroys, PenaltyDestroys = self:ReportDetailedPlayerDestroys( PlayerName )
- ReportDestroys = ReportDestroys ~= "" and "\n- " .. ReportDestroys or ReportDestroys
- self:E( { ReportDestroys, ScoreDestroys, PenaltyDestroys } )
-
- local ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges = self:ReportDetailedPlayerCoalitionChanges( PlayerName )
- ReportCoalitionChanges = ReportCoalitionChanges ~= "" and "\n- " .. ReportCoalitionChanges or ReportCoalitionChanges
- self:E( { ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges } )
-
- local ReportGoals, ScoreGoals, PenaltyGoals = self:ReportDetailedPlayerGoals( PlayerName )
- ReportGoals = ReportGoals ~= "" and "\n- " .. ReportGoals or ReportGoals
- self:E( { ReportGoals, ScoreGoals, PenaltyGoals } )
-
- local ReportMissions, ScoreMissions, PenaltyMissions = self:ReportDetailedPlayerMissions( PlayerName )
- ReportMissions = ReportMissions ~= "" and "\n- " .. ReportMissions or ReportMissions
- self:E( { ReportMissions, ScoreMissions, PenaltyMissions } )
-
- local PlayerScore = ScoreHits + ScoreDestroys + ScoreCoalitionChanges + ScoreGoals + ScoreMissions
- local PlayerPenalty = PenaltyHits + PenaltyDestroys + PenaltyCoalitionChanges + ScoreGoals + PenaltyMissions
-
- PlayerMessage =
- string.format( "Player '%s' Score = %d ( %d Score, -%d Penalties )",
- PlayerName,
- PlayerScore - PlayerPenalty,
- PlayerScore,
- PlayerPenalty
- )
- MESSAGE:New( PlayerMessage, 30, "Player '" .. PlayerName .. "'" ):ToGroup( PlayerGroup )
- end
- end
-
-end
-
---- Report Group Score Detailed
--- @param #SCORING self
--- @param Wrapper.Group#GROUP PlayerGroup The player group.
-function SCORING:ReportScoreGroupDetailed( PlayerGroup )
-
- local PlayerMessage = ""
-
- self:T( "Report Score Group Detailed" )
-
- local PlayerUnits = PlayerGroup:GetUnits()
- for UnitID, PlayerUnit in pairs( PlayerUnits ) do
- local PlayerUnit = PlayerUnit -- Wrapper.Unit#UNIT
- local PlayerName = PlayerUnit:GetPlayerName()
-
- if PlayerName then
-
- local ReportHits, ScoreHits, PenaltyHits = self:ReportDetailedPlayerHits( PlayerName )
- ReportHits = ReportHits ~= "" and "\n- " .. ReportHits or ReportHits
- self:E( { ReportHits, ScoreHits, PenaltyHits } )
-
- local ReportDestroys, ScoreDestroys, PenaltyDestroys = self:ReportDetailedPlayerDestroys( PlayerName )
- ReportDestroys = ReportDestroys ~= "" and "\n- " .. ReportDestroys or ReportDestroys
- self:E( { ReportDestroys, ScoreDestroys, PenaltyDestroys } )
-
- local ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges = self:ReportDetailedPlayerCoalitionChanges( PlayerName )
- ReportCoalitionChanges = ReportCoalitionChanges ~= "" and "\n- " .. ReportCoalitionChanges or ReportCoalitionChanges
- self:E( { ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges } )
-
- local ReportGoals, ScoreGoals, PenaltyGoals = self:ReportDetailedPlayerGoals( PlayerName )
- ReportGoals = ReportGoals ~= "" and "\n- " .. ReportGoals or ReportGoals
- self:E( { ReportGoals, ScoreGoals, PenaltyGoals } )
-
- local ReportMissions, ScoreMissions, PenaltyMissions = self:ReportDetailedPlayerMissions( PlayerName )
- ReportMissions = ReportMissions ~= "" and "\n- " .. ReportMissions or ReportMissions
- self:E( { ReportMissions, ScoreMissions, PenaltyMissions } )
-
- local PlayerScore = ScoreHits + ScoreDestroys + ScoreCoalitionChanges + ScoreGoals + ScoreMissions
- local PlayerPenalty = PenaltyHits + PenaltyDestroys + PenaltyCoalitionChanges + ScoreGoals + PenaltyMissions
-
- PlayerMessage =
- string.format( "Player '%s' Score = %d ( %d Score, -%d Penalties )%s%s%s%s%s",
- PlayerName,
- PlayerScore - PlayerPenalty,
- PlayerScore,
- PlayerPenalty,
- ReportHits,
- ReportDestroys,
- ReportCoalitionChanges,
- ReportGoals,
- ReportMissions
- )
- MESSAGE:New( PlayerMessage, 30, "Player '" .. PlayerName .. "'" ):ToGroup( PlayerGroup )
- end
- end
-
-end
-
---- Report all players score
--- @param #SCORING self
--- @param Wrapper.Group#GROUP PlayerGroup The player group.
-function SCORING:ReportScoreAllSummary( PlayerGroup )
-
- local PlayerMessage = ""
-
- self:T( "Report Score All Players" )
-
- for PlayerName, PlayerData in pairs( self.Players ) do
-
- if PlayerName then
-
- local ReportHits, ScoreHits, PenaltyHits = self:ReportDetailedPlayerHits( PlayerName )
- ReportHits = ReportHits ~= "" and "\n- " .. ReportHits or ReportHits
- self:E( { ReportHits, ScoreHits, PenaltyHits } )
-
- local ReportDestroys, ScoreDestroys, PenaltyDestroys = self:ReportDetailedPlayerDestroys( PlayerName )
- ReportDestroys = ReportDestroys ~= "" and "\n- " .. ReportDestroys or ReportDestroys
- self:E( { ReportDestroys, ScoreDestroys, PenaltyDestroys } )
-
- local ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges = self:ReportDetailedPlayerCoalitionChanges( PlayerName )
- ReportCoalitionChanges = ReportCoalitionChanges ~= "" and "\n- " .. ReportCoalitionChanges or ReportCoalitionChanges
- self:E( { ReportCoalitionChanges, ScoreCoalitionChanges, PenaltyCoalitionChanges } )
-
- local ReportGoals, ScoreGoals, PenaltyGoals = self:ReportDetailedPlayerGoals( PlayerName )
- ReportGoals = ReportGoals ~= "" and "\n- " .. ReportGoals or ReportGoals
- self:E( { ReportGoals, ScoreGoals, PenaltyGoals } )
-
- local ReportMissions, ScoreMissions, PenaltyMissions = self:ReportDetailedPlayerMissions( PlayerName )
- ReportMissions = ReportMissions ~= "" and "\n- " .. ReportMissions or ReportMissions
- self:E( { ReportMissions, ScoreMissions, PenaltyMissions } )
-
- local PlayerScore = ScoreHits + ScoreDestroys + ScoreCoalitionChanges + ScoreGoals + ScoreMissions
- local PlayerPenalty = PenaltyHits + PenaltyDestroys + PenaltyCoalitionChanges + ScoreGoals + PenaltyMissions
-
- PlayerMessage =
- string.format( "Player '%s' Score = %d ( %d Score, -%d Penalties )",
- PlayerName,
- PlayerScore - PlayerPenalty,
- PlayerScore,
- PlayerPenalty
- )
- MESSAGE:New( PlayerMessage, 30, "Player '" .. PlayerName .. "'" ):ToGroup( PlayerGroup )
- end
- end
-
-end
-
-
-function SCORING:SecondsToClock(sSeconds)
- local nSeconds = sSeconds
- if nSeconds == 0 then
- --return nil;
- return "00:00:00";
- else
- nHours = string.format("%02.f", math.floor(nSeconds/3600));
- nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours*60)));
- nSecs = string.format("%02.f", math.floor(nSeconds - nHours*3600 - nMins *60));
- return nHours..":"..nMins..":"..nSecs
- end
-end
-
---- Opens a score CSV file to log the scores.
--- @param #SCORING self
--- @param #string ScoringCSV
--- @return #SCORING self
--- @usage
--- -- Open a new CSV file to log the scores of the game Gori Valley. Let the name of the CSV file begin with "Player Scores".
--- ScoringObject = SCORING:New( "Gori Valley" )
--- ScoringObject:OpenCSV( "Player Scores" )
-function SCORING:OpenCSV( ScoringCSV )
- self:F( ScoringCSV )
-
- if lfs and io and os then
- if ScoringCSV then
- self.ScoringCSV = ScoringCSV
- local fdir = lfs.writedir() .. [[Logs\]] .. self.ScoringCSV .. " " .. os.date( "%Y-%m-%d %H-%M-%S" ) .. ".csv"
-
- self.CSVFile, self.err = io.open( fdir, "w+" )
- if not self.CSVFile then
- error( "Error: Cannot open CSV file in " .. lfs.writedir() )
- end
-
- self.CSVFile:write( '"GameName","RunTime","Time","PlayerName","TargetPlayerName","ScoreType","PlayerUnitCoaltion","PlayerUnitCategory","PlayerUnitType","PlayerUnitName","TargetUnitCoalition","TargetUnitCategory","TargetUnitType","TargetUnitName","Times","Score"\n' )
-
- self.RunTime = os.date("%y-%m-%d_%H-%M-%S")
- else
- error( "A string containing the CSV file name must be given." )
- end
- else
- self:E( "The MissionScripting.lua file has not been changed to allow lfs, io and os modules to be used..." )
- end
- return self
-end
-
-
---- Registers a score for a player.
--- @param #SCORING self
--- @param #string PlayerName The name of the player.
--- @param #string TargetPlayerName The name of the target player.
--- @param #string ScoreType The type of the score.
--- @param #string ScoreTimes The amount of scores achieved.
--- @param #string ScoreAmount The score given.
--- @param #string PlayerUnitName The unit name of the player.
--- @param #string PlayerUnitCoalition The coalition of the player unit.
--- @param #string PlayerUnitCategory The category of the player unit.
--- @param #string PlayerUnitType The type of the player unit.
--- @param #string TargetUnitName The name of the target unit.
--- @param #string TargetUnitCoalition The coalition of the target unit.
--- @param #string TargetUnitCategory The category of the target unit.
--- @param #string TargetUnitType The type of the target unit.
--- @return #SCORING self
-function SCORING:ScoreCSV( PlayerName, TargetPlayerName, ScoreType, ScoreTimes, ScoreAmount, PlayerUnitName, PlayerUnitCoalition, PlayerUnitCategory, PlayerUnitType, TargetUnitName, TargetUnitCoalition, TargetUnitCategory, TargetUnitType )
- --write statistic information to file
- local ScoreTime = self:SecondsToClock( timer.getTime() )
- PlayerName = PlayerName:gsub( '"', '_' )
-
- TargetPlayerName = TargetPlayerName or ""
- TargetPlayerName = TargetPlayerName:gsub( '"', '_' )
-
- if PlayerUnitName and PlayerUnitName ~= '' then
- local PlayerUnit = Unit.getByName( PlayerUnitName )
-
- if PlayerUnit then
- if not PlayerUnitCategory then
- --PlayerUnitCategory = SCORINGCategory[PlayerUnit:getCategory()]
- PlayerUnitCategory = _SCORINGCategory[PlayerUnit:getDesc().category]
- end
-
- if not PlayerUnitCoalition then
- PlayerUnitCoalition = _SCORINGCoalition[PlayerUnit:getCoalition()]
- end
-
- if not PlayerUnitType then
- PlayerUnitType = PlayerUnit:getTypeName()
- end
- else
- PlayerUnitName = ''
- PlayerUnitCategory = ''
- PlayerUnitCoalition = ''
- PlayerUnitType = ''
- end
- else
- PlayerUnitName = ''
- PlayerUnitCategory = ''
- PlayerUnitCoalition = ''
- PlayerUnitType = ''
- end
-
- TargetUnitCoalition = TargetUnitCoalition or ""
- TargetUnitCategory = TargetUnitCategory or ""
- TargetUnitType = TargetUnitType or ""
- TargetUnitName = TargetUnitName or ""
-
- if lfs and io and os then
- self.CSVFile:write(
- '"' .. self.GameName .. '"' .. ',' ..
- '"' .. self.RunTime .. '"' .. ',' ..
- '' .. ScoreTime .. '' .. ',' ..
- '"' .. PlayerName .. '"' .. ',' ..
- '"' .. TargetPlayerName .. '"' .. ',' ..
- '"' .. ScoreType .. '"' .. ',' ..
- '"' .. PlayerUnitCoalition .. '"' .. ',' ..
- '"' .. PlayerUnitCategory .. '"' .. ',' ..
- '"' .. PlayerUnitType .. '"' .. ',' ..
- '"' .. PlayerUnitName .. '"' .. ',' ..
- '"' .. TargetUnitCoalition .. '"' .. ',' ..
- '"' .. TargetUnitCategory .. '"' .. ',' ..
- '"' .. TargetUnitType .. '"' .. ',' ..
- '"' .. TargetUnitName .. '"' .. ',' ..
- '' .. ScoreTimes .. '' .. ',' ..
- '' .. ScoreAmount
- )
-
- self.CSVFile:write( "\n" )
- end
-end
-
-
-function SCORING:CloseCSV()
- if lfs and io and os then
- self.CSVFile:close()
- end
-end
-
---- The CLEANUP class keeps an area clean of crashing or colliding airplanes. It also prevents airplanes from firing within this area.
--- @module CleanUp
--- @author Flightcontrol
-
-
-
-
-
-
-
---- The CLEANUP class.
--- @type CLEANUP
--- @extends Core.Base#BASE
-CLEANUP = {
- ClassName = "CLEANUP",
- ZoneNames = {},
- TimeInterval = 300,
- CleanUpList = {},
-}
-
---- Creates the main object which is handling the cleaning of the debris within the given Zone Names.
--- @param #CLEANUP self
--- @param #table ZoneNames Is a table of zone names where the debris should be cleaned. Also a single string can be passed with one zone name.
--- @param #number TimeInterval The interval in seconds when the clean activity takes place. The default is 300 seconds, thus every 5 minutes.
--- @return #CLEANUP
--- @usage
--- -- Clean these Zones.
--- CleanUpAirports = CLEANUP:New( { 'CLEAN Tbilisi', 'CLEAN Kutaisi' }, 150 )
--- or
--- CleanUpTbilisi = CLEANUP:New( 'CLEAN Tbilisi', 150 )
--- CleanUpKutaisi = CLEANUP:New( 'CLEAN Kutaisi', 600 )
-function CLEANUP:New( ZoneNames, TimeInterval )
-
- local self = BASE:Inherit( self, BASE:New() ) -- #CLEANUP
- self:F( { ZoneNames, TimeInterval } )
-
- if type( ZoneNames ) == 'table' then
- self.ZoneNames = ZoneNames
- else
- self.ZoneNames = { ZoneNames }
- end
- if TimeInterval then
- self.TimeInterval = TimeInterval
- end
-
- self:HandleEvent( EVENTS.Birth )
-
- self.CleanUpScheduler = SCHEDULER:New( self, self._CleanUpScheduler, {}, 1, TimeInterval )
-
- return self
-end
-
-
---- Destroys a group from the simulator, but checks first if it is still existing!
--- @param #CLEANUP self
--- @param Dcs.DCSWrapper.Group#Group GroupObject The object to be destroyed.
--- @param #string CleanUpGroupName The groupname...
-function CLEANUP:_DestroyGroup( GroupObject, CleanUpGroupName )
- self:F( { GroupObject, CleanUpGroupName } )
-
- if GroupObject then -- and GroupObject:isExist() then
- trigger.action.deactivateGroup(GroupObject)
- self:T( { "GroupObject Destroyed", GroupObject } )
- end
-end
-
---- Destroys a @{DCSWrapper.Unit#Unit} from the simulator, but checks first if it is still existing!
--- @param #CLEANUP self
--- @param Dcs.DCSWrapper.Unit#Unit CleanUpUnit The object to be destroyed.
--- @param #string CleanUpUnitName The Unit name ...
-function CLEANUP:_DestroyUnit( CleanUpUnit, CleanUpUnitName )
- self:F( { CleanUpUnit, CleanUpUnitName } )
-
- if CleanUpUnit then
- local CleanUpGroup = Unit.getGroup(CleanUpUnit)
- -- TODO Client bug in 1.5.3
- if CleanUpGroup and CleanUpGroup:isExist() then
- local CleanUpGroupUnits = CleanUpGroup:getUnits()
- if #CleanUpGroupUnits == 1 then
- local CleanUpGroupName = CleanUpGroup:getName()
- --self:CreateEventCrash( timer.getTime(), CleanUpUnit )
- CleanUpGroup:destroy()
- self:T( { "Destroyed Group:", CleanUpGroupName } )
- else
- CleanUpUnit:destroy()
- self:T( { "Destroyed Unit:", CleanUpUnitName } )
- end
- self.CleanUpList[CleanUpUnitName] = nil -- Cleaning from the list
- CleanUpUnit = nil
- end
- end
-end
-
--- TODO check Dcs.DCSTypes#Weapon
---- Destroys a missile from the simulator, but checks first if it is still existing!
--- @param #CLEANUP self
--- @param Dcs.DCSTypes#Weapon MissileObject
-function CLEANUP:_DestroyMissile( MissileObject )
- self:F( { MissileObject } )
-
- if MissileObject and MissileObject:isExist() then
- MissileObject:destroy()
- self:T( "MissileObject Destroyed")
- end
-end
-
---- @param #CLEANUP self
--- @param Core.Event#EVENTDATA EventData
-function CLEANUP:_OnEventBirth( EventData )
- self:F( { EventData } )
-
- self.CleanUpList[EventData.IniDCSUnitName] = {}
- self.CleanUpList[EventData.IniDCSUnitName].CleanUpUnit = EventData.IniDCSUnit
- self.CleanUpList[EventData.IniDCSUnitName].CleanUpGroup = EventData.IniDCSGroup
- self.CleanUpList[EventData.IniDCSUnitName].CleanUpGroupName = EventData.IniDCSGroupName
- self.CleanUpList[EventData.IniDCSUnitName].CleanUpUnitName = EventData.IniDCSUnitName
-
- EventData.IniUnit:HandleEvent( EVENTS.EngineShutdown , self._EventAddForCleanUp )
- EventData.IniUnit:HandleEvent( EVENTS.EngineStartup, self._EventAddForCleanUp )
- EventData.IniUnit:HandleEvent( EVENTS.Hit, self._EventAddForCleanUp )
- EventData.IniUnit:HandleEvent( EVENTS.PilotDead, self._EventCrash )
- EventData.IniUnit:HandleEvent( EVENTS.Dead, self._EventCrash )
- EventData.IniUnit:HandleEvent( EVENTS.Crash, self._EventCrash )
- EventData.IniUnit:HandleEvent( EVENTS.Shot, self._EventShot )
-
-end
-
---- Detects if a crash event occurs.
--- Crashed units go into a CleanUpList for removal.
--- @param #CLEANUP self
--- @param Dcs.DCSTypes#Event event
-function CLEANUP:_EventCrash( Event )
- self:F( { Event } )
-
- --TODO: This stuff is not working due to a DCS bug. Burning units cannot be destroyed.
- -- self:T("before getGroup")
- -- local _grp = Unit.getGroup(event.initiator)-- Identify the group that fired
- -- self:T("after getGroup")
- -- _grp:destroy()
- -- self:T("after deactivateGroup")
- -- event.initiator:destroy()
-
- self.CleanUpList[Event.IniDCSUnitName] = {}
- self.CleanUpList[Event.IniDCSUnitName].CleanUpUnit = Event.IniDCSUnit
- self.CleanUpList[Event.IniDCSUnitName].CleanUpGroup = Event.IniDCSGroup
- self.CleanUpList[Event.IniDCSUnitName].CleanUpGroupName = Event.IniDCSGroupName
- self.CleanUpList[Event.IniDCSUnitName].CleanUpUnitName = Event.IniDCSUnitName
-
-end
-
---- Detects if a unit shoots a missile.
--- If this occurs within one of the zones, then the weapon used must be destroyed.
--- @param #CLEANUP self
--- @param Dcs.DCSTypes#Event event
-function CLEANUP:_EventShot( Event )
- self:F( { Event } )
-
- -- Test if the missile was fired within one of the CLEANUP.ZoneNames.
- local CurrentLandingZoneID = 0
- CurrentLandingZoneID = routines.IsUnitInZones( Event.IniDCSUnit, self.ZoneNames )
- if ( CurrentLandingZoneID ) then
- -- Okay, the missile was fired within the CLEANUP.ZoneNames, destroy the fired weapon.
- --_SEADmissile:destroy()
- SCHEDULER:New( self, CLEANUP._DestroyMissile, { Event.Weapon }, 0.1 )
- end
-end
-
-
---- Detects if the Unit has an S_EVENT_HIT within the given ZoneNames. If this is the case, destroy the unit.
--- @param #CLEANUP self
--- @param Dcs.DCSTypes#Event event
-function CLEANUP:_EventHitCleanUp( Event )
- self:F( { Event } )
-
- if Event.IniDCSUnit then
- if routines.IsUnitInZones( Event.IniDCSUnit, self.ZoneNames ) ~= nil then
- self:T( { "Life: ", Event.IniDCSUnitName, ' = ', Event.IniDCSUnit:getLife(), "/", Event.IniDCSUnit:getLife0() } )
- if Event.IniDCSUnit:getLife() < Event.IniDCSUnit:getLife0() then
- self:T( "CleanUp: Destroy: " .. Event.IniDCSUnitName )
- SCHEDULER:New( self, CLEANUP._DestroyUnit, { Event.IniDCSUnit }, 0.1 )
- end
- end
- end
-
- if Event.TgtDCSUnit then
- if routines.IsUnitInZones( Event.TgtDCSUnit, self.ZoneNames ) ~= nil then
- self:T( { "Life: ", Event.TgtDCSUnitName, ' = ', Event.TgtDCSUnit:getLife(), "/", Event.TgtDCSUnit:getLife0() } )
- if Event.TgtDCSUnit:getLife() < Event.TgtDCSUnit:getLife0() then
- self:T( "CleanUp: Destroy: " .. Event.TgtDCSUnitName )
- SCHEDULER:New( self, CLEANUP._DestroyUnit, { Event.TgtDCSUnit }, 0.1 )
- end
- end
- end
-end
-
---- Add the @{DCSWrapper.Unit#Unit} to the CleanUpList for CleanUp.
-function CLEANUP:_AddForCleanUp( CleanUpUnit, CleanUpUnitName )
- self:F( { CleanUpUnit, CleanUpUnitName } )
-
- self.CleanUpList[CleanUpUnitName] = {}
- self.CleanUpList[CleanUpUnitName].CleanUpUnit = CleanUpUnit
- self.CleanUpList[CleanUpUnitName].CleanUpUnitName = CleanUpUnitName
- self.CleanUpList[CleanUpUnitName].CleanUpGroup = Unit.getGroup(CleanUpUnit)
- self.CleanUpList[CleanUpUnitName].CleanUpGroupName = Unit.getGroup(CleanUpUnit):getName()
- self.CleanUpList[CleanUpUnitName].CleanUpTime = timer.getTime()
- self.CleanUpList[CleanUpUnitName].CleanUpMoved = false
-
- self:T( { "CleanUp: Add to CleanUpList: ", Unit.getGroup(CleanUpUnit):getName(), CleanUpUnitName } )
-
-end
-
---- Detects if the Unit has an S_EVENT_ENGINE_SHUTDOWN or an S_EVENT_HIT within the given ZoneNames. If this is the case, add the Group to the CLEANUP List.
--- @param #CLEANUP self
--- @param Dcs.DCSTypes#Event event
-function CLEANUP:_EventAddForCleanUp( Event )
-
- if Event.IniDCSUnit then
- if self.CleanUpList[Event.IniDCSUnitName] == nil then
- if routines.IsUnitInZones( Event.IniDCSUnit, self.ZoneNames ) ~= nil then
- self:_AddForCleanUp( Event.IniDCSUnit, Event.IniDCSUnitName )
- end
- end
- end
-
- if Event.TgtDCSUnit then
- if self.CleanUpList[Event.TgtDCSUnitName] == nil then
- if routines.IsUnitInZones( Event.TgtDCSUnit, self.ZoneNames ) ~= nil then
- self:_AddForCleanUp( Event.TgtDCSUnit, Event.TgtDCSUnitName )
- end
- end
- end
-
-end
-
-local CleanUpSurfaceTypeText = {
- "LAND",
- "SHALLOW_WATER",
- "WATER",
- "ROAD",
- "RUNWAY"
- }
-
---- At the defined time interval, CleanUp the Groups within the CleanUpList.
--- @param #CLEANUP self
-function CLEANUP:_CleanUpScheduler()
- self:F( { "CleanUp Scheduler" } )
-
- local CleanUpCount = 0
- for CleanUpUnitName, UnitData in pairs( self.CleanUpList ) do
- CleanUpCount = CleanUpCount + 1
-
- self:T( { CleanUpUnitName, UnitData } )
- local CleanUpUnit = Unit.getByName(UnitData.CleanUpUnitName)
- local CleanUpGroupName = UnitData.CleanUpGroupName
- local CleanUpUnitName = UnitData.CleanUpUnitName
- if CleanUpUnit then
- self:T( { "CleanUp Scheduler", "Checking:", CleanUpUnitName } )
- if _DATABASE:GetStatusGroup( CleanUpGroupName ) ~= "ReSpawn" then
- local CleanUpUnitVec3 = CleanUpUnit:getPoint()
- --self:T( CleanUpUnitVec3 )
- local CleanUpUnitVec2 = {}
- CleanUpUnitVec2.x = CleanUpUnitVec3.x
- CleanUpUnitVec2.y = CleanUpUnitVec3.z
- --self:T( CleanUpUnitVec2 )
- local CleanUpSurfaceType = land.getSurfaceType(CleanUpUnitVec2)
- --self:T( CleanUpSurfaceType )
-
- if CleanUpUnit and CleanUpUnit:getLife() <= CleanUpUnit:getLife0() * 0.95 then
- if CleanUpSurfaceType == land.SurfaceType.RUNWAY then
- if CleanUpUnit:inAir() then
- local CleanUpLandHeight = land.getHeight(CleanUpUnitVec2)
- local CleanUpUnitHeight = CleanUpUnitVec3.y - CleanUpLandHeight
- self:T( { "CleanUp Scheduler", "Height = " .. CleanUpUnitHeight } )
- if CleanUpUnitHeight < 30 then
- self:T( { "CleanUp Scheduler", "Destroy " .. CleanUpUnitName .. " because below safe height and damaged." } )
- self:_DestroyUnit(CleanUpUnit, CleanUpUnitName)
- end
- else
- self:T( { "CleanUp Scheduler", "Destroy " .. CleanUpUnitName .. " because on runway and damaged." } )
- self:_DestroyUnit(CleanUpUnit, CleanUpUnitName)
- end
- end
- end
- -- Clean Units which are waiting for a very long time in the CleanUpZone.
- if CleanUpUnit then
- local CleanUpUnitVelocity = CleanUpUnit:getVelocity()
- local CleanUpUnitVelocityTotal = math.abs(CleanUpUnitVelocity.x) + math.abs(CleanUpUnitVelocity.y) + math.abs(CleanUpUnitVelocity.z)
- if CleanUpUnitVelocityTotal < 1 then
- if UnitData.CleanUpMoved then
- if UnitData.CleanUpTime + 180 <= timer.getTime() then
- self:T( { "CleanUp Scheduler", "Destroy due to not moving anymore " .. CleanUpUnitName } )
- self:_DestroyUnit(CleanUpUnit, CleanUpUnitName)
- end
- end
- else
- UnitData.CleanUpTime = timer.getTime()
- UnitData.CleanUpMoved = true
- end
- end
-
- else
- -- Do nothing ...
- self.CleanUpList[CleanUpUnitName] = nil -- Not anymore in the DCSRTE
- end
- else
- self:T( "CleanUp: Group " .. CleanUpUnitName .. " cannot be found in DCS RTE, removing ..." )
- self.CleanUpList[CleanUpUnitName] = nil -- Not anymore in the DCSRTE
- end
- end
- self:T(CleanUpCount)
-
- return true
-end
-
---- Single-Player:**Yes** / Multi-Player:**Yes** / AI:**Yes** / Human:**No** / Types:**All** --
--- **Spawn groups of units dynamically in your missions.**
---
--- 
---
--- ===
---
--- # 1) @{#SPAWN} class, extends @{Base#BASE}
---
--- The @{#SPAWN} class allows to spawn dynamically new groups, based on pre-defined initialization settings, modifying the behaviour when groups are spawned.
--- For each group to be spawned, within the mission editor, a group has to be created with the "late activation flag" set. We call this group the *"Spawn Template"* of the SPAWN object.
--- A reference to this Spawn Template needs to be provided when constructing the SPAWN object, by indicating the name of the group within the mission editor in the constructor methods.
---
--- Within the SPAWN object, there is an internal index that keeps track of which group from the internal group list was spawned.
--- When new groups get spawned by using the SPAWN methods (see below), it will be validated whether the Limits (@{#SPAWN.Limit}) of the SPAWN object are not reached.
--- When all is valid, a new group will be created by the spawning methods, and the internal index will be increased with 1.
---
--- Regarding the name of new spawned groups, a _SpawnPrefix_ will be assigned for each new group created.
--- If you want to have the Spawn Template name to be used as the _SpawnPrefix_ name, use the @{#SPAWN.New} constructor.
--- However, when the @{#SPAWN.NewWithAlias} constructor was used, the Alias name will define the _SpawnPrefix_ name.
--- Groups will follow the following naming structure when spawned at run-time:
---
--- 1. Spawned groups will have the name _SpawnPrefix_#ggg, where ggg is a counter from 0 to 999.
--- 2. Spawned units will have the name _SpawnPrefix_#ggg-uu, where uu is a counter from 0 to 99 for each new spawned unit belonging to the group.
---
--- Some additional notes that need to be remembered:
---
--- * Templates are actually groups defined within the mission editor, with the flag "Late Activation" set. As such, these groups are never used within the mission, but are used by the @{#SPAWN} module.
--- * It is important to defined BEFORE you spawn new groups, a proper initialization of the SPAWN instance is done with the options you want to use.
--- * When designing a mission, NEVER name groups using a "#" within the name of the group Spawn Template(s), or the SPAWN module logic won't work anymore.
---
--- ## 1.1) SPAWN construction methods
---
--- Create a new SPAWN object with the @{#SPAWN.New}() or the @{#SPAWN.NewWithAlias}() methods:
---
--- * @{#SPAWN.New}(): Creates a new SPAWN object taking the name of the group that represents the GROUP Template (definition).
--- * @{#SPAWN.NewWithAlias}(): Creates a new SPAWN object taking the name of the group that represents the GROUP Template (definition), and gives each spawned @{Group} an different name.
---
--- It is important to understand how the SPAWN class works internally. The SPAWN object created will contain internally a list of groups that will be spawned and that are already spawned.
--- The initialization methods will modify this list of groups so that when a group gets spawned, ALL information is already prepared when spawning. This is done for performance reasons.
--- So in principle, the group list will contain all parameters and configurations after initialization, and when groups get actually spawned, this spawning can be done quickly and efficient.
---
--- ## 1.2) SPAWN initialization methods
---
--- A spawn object will behave differently based on the usage of **initialization** methods, which all start with the **Init** prefix:
---
--- * @{#SPAWN.InitKeepUnitNames}(): Keeps the unit names as defined within the mission editor, but note that anything after a # mark is ignored, and any spaces before and after the resulting name are removed. IMPORTANT! This method MUST be the first used after :New !!!
--- * @{#SPAWN.InitLimit}(): Limits the amount of groups that can be alive at the same time and that can be dynamically spawned.
--- * @{#SPAWN.InitRandomizeRoute}(): Randomize the routes of spawned groups, and for air groups also optionally the height.
--- * @{#SPAWN.InitRandomizeTemplate}(): Randomize the group templates so that when a new group is spawned, a random group template is selected from one of the templates defined.
--- * @{#SPAWN.InitUnControlled}(): Spawn plane groups uncontrolled.
--- * @{#SPAWN.InitArray}(): Make groups visible before they are actually activated, and order these groups like a batallion in an array.
--- * @{#SPAWN.InitRepeat}(): Re-spawn groups when they land at the home base. Similar methods are @{#SPAWN.InitRepeatOnLanding} and @{#SPAWN.InitRepeatOnEngineShutDown}.
--- * @{#SPAWN.InitRandomizePosition}(): Randomizes the position of @{Group}s that are spawned within a **radius band**, given an Outer and Inner radius, from the point that the spawn happens.
--- * @{#SPAWN.InitRandomizeUnits}(): Randomizes the @{Unit}s in the @{Group} that is spawned within a **radius band**, given an Outer and Inner radius.
--- * @{#SPAWN.InitRandomizeZones}(): Randomizes the spawning between a predefined list of @{Zone}s that are declared using this function. Each zone can be given a probability factor.
--- * @{#SPAWN.InitAIOn}(): Turns the AI On when spawning the new @{Group} object.
--- * @{#SPAWN.InitAIOff}(): Turns the AI Off when spawning the new @{Group} object.
--- * @{#SPAWN.InitAIOnOff}(): Turns the AI On or Off when spawning the new @{Group} object.
---
--- ## 1.3) SPAWN spawning methods
---
--- Groups can be spawned at different times and methods:
---
--- * @{#SPAWN.Spawn}(): Spawn one new group based on the last spawned index.
--- * @{#SPAWN.ReSpawn}(): Re-spawn a group based on a given index.
--- * @{#SPAWN.SpawnScheduled}(): Spawn groups at scheduled but randomized intervals. You can use @{#SPAWN.SpawnScheduleStart}() and @{#SPAWN.SpawnScheduleStop}() to start and stop the schedule respectively.
--- * @{#SPAWN.SpawnFromVec3}(): Spawn a new group from a Vec3 coordinate. (The group will can be spawned at a point in the air).
--- * @{#SPAWN.SpawnFromVec2}(): Spawn a new group from a Vec2 coordinate. (The group will be spawned at land height ).
--- * @{#SPAWN.SpawnFromStatic}(): Spawn a new group from a structure, taking the position of a @{Static}.
--- * @{#SPAWN.SpawnFromUnit}(): Spawn a new group taking the position of a @{Unit}.
--- * @{#SPAWN.SpawnInZone}(): Spawn a new group in a @{Zone}.
---
--- Note that @{#SPAWN.Spawn} and @{#SPAWN.ReSpawn} return a @{GROUP#GROUP.New} object, that contains a reference to the DCSGroup object.
--- You can use the @{GROUP} object to do further actions with the DCSGroup.
---
--- ## 1.4) Retrieve alive GROUPs spawned by the SPAWN object
---
--- The SPAWN class administers which GROUPS it has reserved (in stock) or has created during mission execution.
--- Every time a SPAWN object spawns a new GROUP object, a reference to the GROUP object is added to an internal table of GROUPS.
--- SPAWN provides methods to iterate through that internal GROUP object reference table:
---
--- * @{#SPAWN.GetFirstAliveGroup}(): Will find the first alive GROUP it has spawned, and return the alive GROUP object and the first Index where the first alive GROUP object has been found.
--- * @{#SPAWN.GetNextAliveGroup}(): Will find the next alive GROUP object from a given Index, and return a reference to the alive GROUP object and the next Index where the alive GROUP has been found.
--- * @{#SPAWN.GetLastAliveGroup}(): Will find the last alive GROUP object, and will return a reference to the last live GROUP object and the last Index where the last alive GROUP object has been found.
---
--- You can use the methods @{#SPAWN.GetFirstAliveGroup}() and sequently @{#SPAWN.GetNextAliveGroup}() to iterate through the alive GROUPS within the SPAWN object, and to actions... See the respective methods for an example.
--- The method @{#SPAWN.GetGroupFromIndex}() will return the GROUP object reference from the given Index, dead or alive...
---
--- ## 1.5) SPAWN object cleaning
---
--- Sometimes, it will occur during a mission run-time, that ground or especially air objects get damaged, and will while being damged stop their activities, while remaining alive.
--- In such cases, the SPAWN object will just sit there and wait until that group gets destroyed, but most of the time it won't,
--- and it may occur that no new groups are or can be spawned as limits are reached.
--- To prevent this, a @{#SPAWN.InitCleanUp}() initialization method has been defined that will silently monitor the status of each spawned group.
--- Once a group has a velocity = 0, and has been waiting for a defined interval, that group will be cleaned or removed from run-time.
--- There is a catch however :-) If a damaged group has returned to an airbase within the coalition, that group will not be considered as "lost"...
--- In such a case, when the inactive group is cleaned, a new group will Re-spawned automatically.
--- This models AI that has succesfully returned to their airbase, to restart their combat activities.
--- Check the @{#SPAWN.InitCleanUp}() for further info.
---
--- ## 1.6) Catch the @{Group} spawn event in a callback function!
---
--- When using the SpawnScheduled method, new @{Group}s are created following the schedule timing parameters.
--- When a new @{Group} is spawned, you maybe want to execute actions with that group spawned at the spawn event.
--- To SPAWN class supports this functionality through the @{#SPAWN.OnSpawnGroup}( **function( SpawnedGroup ) end ** ) method, which takes a function as a parameter that you can define locally.
--- Whenever a new @{Group} is spawned, the given function is called, and the @{Group} that was just spawned, is given as a parameter.
--- As a result, your spawn event handling function requires one parameter to be declared, which will contain the spawned @{Group} object.
--- A coding example is provided at the description of the @{#SPAWN.OnSpawnGroup}( **function( SpawnedGroup ) end ** ) method.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-03-14: SPAWN:**InitKeepUnitNames()** added.
--- 2017-03-14: SPAWN:**InitRandomizePosition( RandomizePosition, OuterRadious, InnerRadius )** added.
---
--- 2017-02-04: SPAWN:InitUnControlled( **UnControlled** ) replaces SPAWN:InitUnControlled().
---
--- 2017-01-24: SPAWN:**InitAIOnOff( AIOnOff )** added.
---
--- 2017-01-24: SPAWN:**InitAIOn()** added.
---
--- 2017-01-24: SPAWN:**InitAIOff()** added.
---
--- 2016-08-15: SPAWN:**InitCleanUp**( SpawnCleanUpInterval ) replaces SPAWN:_CleanUp_( SpawnCleanUpInterval ).
---
--- 2016-08-15: SPAWN:**InitRandomizeZones( SpawnZones )** added.
---
--- 2016-08-14: SPAWN:**OnSpawnGroup**( SpawnCallBackFunction, ... ) replaces SPAWN:_SpawnFunction_( SpawnCallBackFunction, ... ).
---
--- 2016-08-14: SPAWN.SpawnInZone( Zone, __RandomizeGroup__, SpawnIndex ) replaces SpawnInZone( Zone, _RandomizeUnits, OuterRadius, InnerRadius,_ SpawnIndex ).
---
--- 2016-08-14: SPAWN.SpawnFromVec3( Vec3, SpawnIndex ) replaces SpawnFromVec3( Vec3, _RandomizeUnits, OuterRadius, InnerRadius,_ SpawnIndex ):
---
--- 2016-08-14: SPAWN.SpawnFromVec2( Vec2, SpawnIndex ) replaces SpawnFromVec2( Vec2, _RandomizeUnits, OuterRadius, InnerRadius,_ SpawnIndex ):
---
--- 2016-08-14: SPAWN.SpawnFromUnit( SpawnUnit, SpawnIndex ) replaces SpawnFromUnit( SpawnUnit, _RandomizeUnits, OuterRadius, InnerRadius,_ SpawnIndex ):
---
--- 2016-08-14: SPAWN.SpawnFromUnit( SpawnUnit, SpawnIndex ) replaces SpawnFromStatic( SpawnStatic, _RandomizeUnits, OuterRadius, InnerRadius,_ SpawnIndex ):
---
--- 2016-08-14: SPAWN.**InitRandomizeUnits( RandomizeUnits, OuterRadius, InnerRadius )** added:
---
--- 2016-08-14: SPAWN.**Init**Limit( SpawnMaxUnitsAlive, SpawnMaxGroups ) replaces SPAWN._Limit_( SpawnMaxUnitsAlive, SpawnMaxGroups ):
---
--- 2016-08-14: SPAWN.**Init**Array( SpawnAngle, SpawnWidth, SpawnDeltaX, SpawnDeltaY ) replaces SPAWN._Array_( SpawnAngle, SpawnWidth, SpawnDeltaX, SpawnDeltaY ).
---
--- 2016-08-14: SPAWN.**Init**RandomizeRoute( SpawnStartPoint, SpawnEndPoint, SpawnRadius, SpawnHeight ) replaces SPAWN._RandomizeRoute_( SpawnStartPoint, SpawnEndPoint, SpawnRadius, SpawnHeight ).
---
--- 2016-08-14: SPAWN.**Init**RandomizeTemplate( SpawnTemplatePrefixTable ) replaces SPAWN._RandomizeTemplate_( SpawnTemplatePrefixTable ).
---
--- 2016-08-14: SPAWN.**Init**UnControlled() replaces SPAWN._UnControlled_().
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **Aaron**: Posed the idea for Group position randomization at SpawnInZone and make the Unit randomization separate from the Group randomization.
--- * [**Entropy**](https://forums.eagle.ru/member.php?u=111471), **Afinegan**: Came up with the requirement for AIOnOff().
---
--- ### Authors:
---
--- * **FlightControl**: Design & Programming
---
--- @module Spawn
-
-
-
---- SPAWN Class
--- @type SPAWN
--- @extends Core.Base#BASE
--- @field ClassName
--- @field #string SpawnTemplatePrefix
--- @field #string SpawnAliasPrefix
--- @field #number AliveUnits
--- @field #number MaxAliveUnits
--- @field #number SpawnIndex
--- @field #number MaxAliveGroups
--- @field #SPAWN.SpawnZoneTable SpawnZoneTable
-SPAWN = {
- ClassName = "SPAWN",
- SpawnTemplatePrefix = nil,
- SpawnAliasPrefix = nil,
-}
-
-
---- @type SPAWN.SpawnZoneTable
--- @list SpawnZone
-
-
---- Creates the main object to spawn a @{Group} defined in the DCS ME.
--- @param #SPAWN self
--- @param #string SpawnTemplatePrefix is the name of the Group in the ME that defines the Template. Each new group will have the name starting with SpawnTemplatePrefix.
--- @return #SPAWN
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' )
--- @usage local Plane = SPAWN:New( "Plane" ) -- Creates a new local variable that can initiate new planes with the name "Plane#ddd" using the template "Plane" as defined within the ME.
-function SPAWN:New( SpawnTemplatePrefix )
- local self = BASE:Inherit( self, BASE:New() ) -- #SPAWN
- self:F( { SpawnTemplatePrefix } )
-
- local TemplateGroup = Group.getByName( SpawnTemplatePrefix )
- if TemplateGroup then
- self.SpawnTemplatePrefix = SpawnTemplatePrefix
- self.SpawnIndex = 0
- self.SpawnCount = 0 -- The internal counter of the amount of spawning the has happened since SpawnStart.
- self.AliveUnits = 0 -- Contains the counter how many units are currently alive
- self.SpawnIsScheduled = false -- Reflects if the spawning for this SpawnTemplatePrefix is going to be scheduled or not.
- self.SpawnTemplate = self._GetTemplate( self, SpawnTemplatePrefix ) -- Contains the template structure for a Group Spawn from the Mission Editor. Note that this group must have lateActivation always on!!!
- self.Repeat = false -- Don't repeat the group from Take-Off till Landing and back Take-Off by ReSpawning.
- self.UnControlled = false -- When working in UnControlled mode, all planes are Spawned in UnControlled mode before the scheduler starts.
- self.SpawnInitLimit = false -- By default, no InitLimit
- self.SpawnMaxUnitsAlive = 0 -- The maximum amount of groups that can be alive of SpawnTemplatePrefix at the same time.
- self.SpawnMaxGroups = 0 -- The maximum amount of groups that can be spawned.
- self.SpawnRandomize = false -- Sets the randomization flag of new Spawned units to false.
- self.SpawnVisible = false -- Flag that indicates if all the Groups of the SpawnGroup need to be visible when Spawned.
- self.AIOnOff = true -- The AI is on by default when spawning a group.
- self.SpawnUnControlled = false
- self.SpawnInitKeepUnitNames = false -- Overwrite unit names by default with group name.
-
- self.SpawnGroups = {} -- Array containing the descriptions of each Group to be Spawned.
- else
- error( "SPAWN:New: There is no group declared in the mission editor with SpawnTemplatePrefix = '" .. SpawnTemplatePrefix .. "'" )
- end
-
- self:SetEventPriority( 5 )
-
- return self
-end
-
---- Creates a new SPAWN instance to create new groups based on the defined template and using a new alias for each new group.
--- @param #SPAWN self
--- @param #string SpawnTemplatePrefix is the name of the Group in the ME that defines the Template.
--- @param #string SpawnAliasPrefix is the name that will be given to the Group at runtime.
--- @return #SPAWN
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- Spawn_BE_KA50 = SPAWN:NewWithAlias( 'BE KA-50@RAMP-Ground Defense', 'Helicopter Attacking a City' )
--- @usage local PlaneWithAlias = SPAWN:NewWithAlias( "Plane", "Bomber" ) -- Creates a new local variable that can instantiate new planes with the name "Bomber#ddd" using the template "Plane" as defined within the ME.
-function SPAWN:NewWithAlias( SpawnTemplatePrefix, SpawnAliasPrefix )
- local self = BASE:Inherit( self, BASE:New() )
- self:F( { SpawnTemplatePrefix, SpawnAliasPrefix } )
-
- local TemplateGroup = Group.getByName( SpawnTemplatePrefix )
- if TemplateGroup then
- self.SpawnTemplatePrefix = SpawnTemplatePrefix
- self.SpawnAliasPrefix = SpawnAliasPrefix
- self.SpawnIndex = 0
- self.SpawnCount = 0 -- The internal counter of the amount of spawning the has happened since SpawnStart.
- self.AliveUnits = 0 -- Contains the counter how many units are currently alive
- self.SpawnIsScheduled = false -- Reflects if the spawning for this SpawnTemplatePrefix is going to be scheduled or not.
- self.SpawnTemplate = self._GetTemplate( self, SpawnTemplatePrefix ) -- Contains the template structure for a Group Spawn from the Mission Editor. Note that this group must have lateActivation always on!!!
- self.Repeat = false -- Don't repeat the group from Take-Off till Landing and back Take-Off by ReSpawning.
- self.UnControlled = false -- When working in UnControlled mode, all planes are Spawned in UnControlled mode before the scheduler starts.
- self.SpawnInitLimit = false -- By default, no InitLimit
- self.SpawnMaxUnitsAlive = 0 -- The maximum amount of groups that can be alive of SpawnTemplatePrefix at the same time.
- self.SpawnMaxGroups = 0 -- The maximum amount of groups that can be spawned.
- self.SpawnRandomize = false -- Sets the randomization flag of new Spawned units to false.
- self.SpawnVisible = false -- Flag that indicates if all the Groups of the SpawnGroup need to be visible when Spawned.
- self.AIOnOff = true -- The AI is on by default when spawning a group.
- self.SpawnUnControlled = false
- self.SpawnInitKeepUnitNames = false -- Overwrite unit names by default with group name.
-
- self.SpawnGroups = {} -- Array containing the descriptions of each Group to be Spawned.
- else
- error( "SPAWN:New: There is no group declared in the mission editor with SpawnTemplatePrefix = '" .. SpawnTemplatePrefix .. "'" )
- end
-
- self:SetEventPriority( 5 )
-
- return self
-end
-
-
---- Limits the Maximum amount of Units that can be alive at the same time, and the maximum amount of groups that can be spawned.
--- Note that this method is exceptionally important to balance the performance of the mission. Depending on the machine etc, a mission can only process a maximum amount of units.
--- If the time interval must be short, but there should not be more Units or Groups alive than a maximum amount of units, then this method should be used...
--- When a @{#SPAWN.New} is executed and the limit of the amount of units alive is reached, then no new spawn will happen of the group, until some of these units of the spawn object will be destroyed.
--- @param #SPAWN self
--- @param #number SpawnMaxUnitsAlive The maximum amount of units that can be alive at runtime.
--- @param #number SpawnMaxGroups The maximum amount of groups that can be spawned. When the limit is reached, then no more actual spawns will happen of the group.
--- This parameter is useful to define a maximum amount of airplanes, ground troops, helicopters, ships etc within a supply area.
--- This parameter accepts the value 0, which defines that there are no maximum group limits, but there are limits on the maximum of units that can be alive at the same time.
--- @return #SPAWN self
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- -- This helicopter group consists of one Unit. So, this group will SPAWN maximum 2 groups simultaneously within the DCSRTE.
--- -- There will be maximum 24 groups spawned during the whole mission lifetime.
--- Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' ):InitLimit( 2, 24 )
-function SPAWN:InitLimit( SpawnMaxUnitsAlive, SpawnMaxGroups )
- self:F( { self.SpawnTemplatePrefix, SpawnMaxUnitsAlive, SpawnMaxGroups } )
-
- self.SpawnInitLimit = true
- self.SpawnMaxUnitsAlive = SpawnMaxUnitsAlive -- The maximum amount of groups that can be alive of SpawnTemplatePrefix at the same time.
- self.SpawnMaxGroups = SpawnMaxGroups -- The maximum amount of groups that can be spawned.
-
- for SpawnGroupID = 1, self.SpawnMaxGroups do
- self:_InitializeSpawnGroups( SpawnGroupID )
- end
-
- return self
-end
-
---- Keeps the unit names as defined within the mission editor,
--- but note that anything after a # mark is ignored,
--- and any spaces before and after the resulting name are removed.
--- IMPORTANT! This method MUST be the first used after :New !!!
--- @param #SPAWN self
--- @return #SPAWN self
-function SPAWN:InitKeepUnitNames()
- self:F( )
-
- self.SpawnInitKeepUnitNames = true
-
- return self
-end
-
-
---- Randomizes the defined route of the SpawnTemplatePrefix group in the ME. This is very useful to define extra variation of the behaviour of groups.
--- @param #SPAWN self
--- @param #number SpawnStartPoint is the waypoint where the randomization begins.
--- Note that the StartPoint = 0 equaling the point where the group is spawned.
--- @param #number SpawnEndPoint is the waypoint where the randomization ends counting backwards.
--- This parameter is useful to avoid randomization to end at a waypoint earlier than the last waypoint on the route.
--- @param #number SpawnRadius is the radius in meters in which the randomization of the new waypoints, with the original waypoint of the original template located in the middle ...
--- @param #number SpawnHeight (optional) Specifies the **additional** height in meters that can be added to the base height specified at each waypoint in the ME.
--- @return #SPAWN
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- -- The KA-50 has waypoints Start point ( =0 or SP ), 1, 2, 3, 4, End point (= 5 or DP).
--- -- Waypoints 2 and 3 will only be randomized. The others will remain on their original position with each new spawn of the helicopter.
--- -- The randomization of waypoint 2 and 3 will take place within a radius of 2000 meters.
--- Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' ):InitRandomizeRoute( 2, 2, 2000 )
-function SPAWN:InitRandomizeRoute( SpawnStartPoint, SpawnEndPoint, SpawnRadius, SpawnHeight )
- self:F( { self.SpawnTemplatePrefix, SpawnStartPoint, SpawnEndPoint, SpawnRadius, SpawnHeight } )
-
- self.SpawnRandomizeRoute = true
- self.SpawnRandomizeRouteStartPoint = SpawnStartPoint
- self.SpawnRandomizeRouteEndPoint = SpawnEndPoint
- self.SpawnRandomizeRouteRadius = SpawnRadius
- self.SpawnRandomizeRouteHeight = SpawnHeight
-
- for GroupID = 1, self.SpawnMaxGroups do
- self:_RandomizeRoute( GroupID )
- end
-
- return self
-end
-
---- Randomizes the position of @{Group}s that are spawned within a **radius band**, given an Outer and Inner radius, from the point that the spawn happens.
--- @param #SPAWN self
--- @param #boolean RandomizePosition If true, SPAWN will perform the randomization of the @{Group}s position between a given outer and inner radius.
--- @param Dcs.DCSTypes#Distance OuterRadius (optional) The outer radius in meters where the new group will be spawned.
--- @param Dcs.DCSTypes#Distance InnerRadius (optional) The inner radius in meters where the new group will NOT be spawned.
--- @return #SPAWN
-function SPAWN:InitRandomizePosition( RandomizePosition, OuterRadius, InnerRadius )
- self:F( { self.SpawnTemplatePrefix, RandomizePosition, OuterRadius, InnerRadius } )
-
- self.SpawnRandomizePosition = RandomizePosition or false
- self.SpawnRandomizePositionOuterRadius = OuterRadius or 0
- self.SpawnRandomizePositionInnerRadius = InnerRadius or 0
-
- for GroupID = 1, self.SpawnMaxGroups do
- self:_RandomizeRoute( GroupID )
- end
-
- return self
-end
-
-
---- Randomizes the UNITs that are spawned within a radius band given an Outer and Inner radius.
--- @param #SPAWN self
--- @param #boolean RandomizeUnits If true, SPAWN will perform the randomization of the @{UNIT}s position within the group between a given outer and inner radius.
--- @param Dcs.DCSTypes#Distance OuterRadius (optional) The outer radius in meters where the new group will be spawned.
--- @param Dcs.DCSTypes#Distance InnerRadius (optional) The inner radius in meters where the new group will NOT be spawned.
--- @return #SPAWN
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- -- The KA-50 has waypoints Start point ( =0 or SP ), 1, 2, 3, 4, End point (= 5 or DP).
--- -- Waypoints 2 and 3 will only be randomized. The others will remain on their original position with each new spawn of the helicopter.
--- -- The randomization of waypoint 2 and 3 will take place within a radius of 2000 meters.
--- Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' ):InitRandomizeRoute( 2, 2, 2000 )
-function SPAWN:InitRandomizeUnits( RandomizeUnits, OuterRadius, InnerRadius )
- self:F( { self.SpawnTemplatePrefix, RandomizeUnits, OuterRadius, InnerRadius } )
-
- self.SpawnRandomizeUnits = RandomizeUnits or false
- self.SpawnOuterRadius = OuterRadius or 0
- self.SpawnInnerRadius = InnerRadius or 0
-
- for GroupID = 1, self.SpawnMaxGroups do
- self:_RandomizeRoute( GroupID )
- end
-
- return self
-end
-
---- This method is rather complicated to understand. But I'll try to explain.
--- This method becomes useful when you need to spawn groups with random templates of groups defined within the mission editor,
--- but they will all follow the same Template route and have the same prefix name.
--- In other words, this method randomizes between a defined set of groups the template to be used for each new spawn of a group.
--- @param #SPAWN self
--- @param #string SpawnTemplatePrefixTable A table with the names of the groups defined within the mission editor, from which one will be choosen when a new group will be spawned.
--- @return #SPAWN
--- @usage
--- -- NATO Tank Platoons invading Gori.
--- -- Choose between 13 different 'US Tank Platoon' configurations for each new SPAWN the Group to be spawned for the
--- -- 'US Tank Platoon Left', 'US Tank Platoon Middle' and 'US Tank Platoon Right' SpawnTemplatePrefixes.
--- -- Each new SPAWN will randomize the route, with a defined time interval of 200 seconds with 40% time variation (randomization) and
--- -- with a limit set of maximum 12 Units alive simulteneously and 150 Groups to be spawned during the whole mission.
--- Spawn_US_Platoon = { 'US Tank Platoon 1', 'US Tank Platoon 2', 'US Tank Platoon 3', 'US Tank Platoon 4', 'US Tank Platoon 5',
--- 'US Tank Platoon 6', 'US Tank Platoon 7', 'US Tank Platoon 8', 'US Tank Platoon 9', 'US Tank Platoon 10',
--- 'US Tank Platoon 11', 'US Tank Platoon 12', 'US Tank Platoon 13' }
--- Spawn_US_Platoon_Left = SPAWN:New( 'US Tank Platoon Left' ):InitLimit( 12, 150 ):Schedule( 200, 0.4 ):InitRandomizeTemplate( Spawn_US_Platoon ):InitRandomizeRoute( 3, 3, 2000 )
--- Spawn_US_Platoon_Middle = SPAWN:New( 'US Tank Platoon Middle' ):InitLimit( 12, 150 ):Schedule( 200, 0.4 ):InitRandomizeTemplate( Spawn_US_Platoon ):InitRandomizeRoute( 3, 3, 2000 )
--- Spawn_US_Platoon_Right = SPAWN:New( 'US Tank Platoon Right' ):InitLimit( 12, 150 ):Schedule( 200, 0.4 ):InitRandomizeTemplate( Spawn_US_Platoon ):InitRandomizeRoute( 3, 3, 2000 )
-function SPAWN:InitRandomizeTemplate( SpawnTemplatePrefixTable )
- self:F( { self.SpawnTemplatePrefix, SpawnTemplatePrefixTable } )
-
- self.SpawnTemplatePrefixTable = SpawnTemplatePrefixTable
- self.SpawnRandomizeTemplate = true
-
- for SpawnGroupID = 1, self.SpawnMaxGroups do
- self:_RandomizeTemplate( SpawnGroupID )
- end
-
- return self
-end
-
---TODO: Add example.
---- This method provides the functionality to randomize the spawning of the Groups at a given list of zones of different types.
--- @param #SPAWN self
--- @param #table SpawnZoneTable A table with @{Zone} objects. If this table is given, then each spawn will be executed within the given list of @{Zone}s objects.
--- @return #SPAWN
--- @usage
--- -- NATO Tank Platoons invading Gori.
--- -- Choose between 3 different zones for each new SPAWN the Group to be executed, regardless of the zone type.
-function SPAWN:InitRandomizeZones( SpawnZoneTable )
- self:F( { self.SpawnTemplatePrefix, SpawnZoneTable } )
-
- self.SpawnZoneTable = SpawnZoneTable
- self.SpawnRandomizeZones = true
-
- for SpawnGroupID = 1, self.SpawnMaxGroups do
- self:_RandomizeZones( SpawnGroupID )
- end
-
- return self
-end
-
-
-
-
-
---- For planes and helicopters, when these groups go home and land on their home airbases and farps, they normally would taxi to the parking spot, shut-down their engines and wait forever until the Group is removed by the runtime environment.
--- This method is used to re-spawn automatically (so no extra call is needed anymore) the same group after it has landed.
--- This will enable a spawned group to be re-spawned after it lands, until it is destroyed...
--- Note: When the group is respawned, it will re-spawn from the original airbase where it took off.
--- So ensure that the routes for groups that respawn, always return to the original airbase, or players may get confused ...
--- @param #SPAWN self
--- @return #SPAWN self
--- @usage
--- -- RU Su-34 - AI Ship Attack
--- -- Re-SPAWN the Group(s) after each landing and Engine Shut-Down automatically.
--- SpawnRU_SU34 = SPAWN:New( 'TF1 RU Su-34 Krymsk@AI - Attack Ships' ):Schedule( 2, 3, 1800, 0.4 ):SpawnUncontrolled():InitRandomizeRoute( 1, 1, 3000 ):RepeatOnEngineShutDown()
-function SPAWN:InitRepeat()
- self:F( { self.SpawnTemplatePrefix, self.SpawnIndex } )
-
- self.Repeat = true
- self.RepeatOnEngineShutDown = false
- self.RepeatOnLanding = true
-
- return self
-end
-
---- Respawn group after landing.
--- @param #SPAWN self
--- @return #SPAWN self
-function SPAWN:InitRepeatOnLanding()
- self:F( { self.SpawnTemplatePrefix } )
-
- self:InitRepeat()
- self.RepeatOnEngineShutDown = false
- self.RepeatOnLanding = true
-
- return self
-end
-
-
---- Respawn after landing when its engines have shut down.
--- @param #SPAWN self
--- @return #SPAWN self
-function SPAWN:InitRepeatOnEngineShutDown()
- self:F( { self.SpawnTemplatePrefix } )
-
- self:InitRepeat()
- self.RepeatOnEngineShutDown = true
- self.RepeatOnLanding = false
-
- return self
-end
-
-
---- CleanUp groups when they are still alive, but inactive.
--- When groups are still alive and have become inactive due to damage and are unable to contribute anything, then this group will be removed at defined intervals in seconds.
--- @param #SPAWN self
--- @param #string SpawnCleanUpInterval The interval to check for inactive groups within seconds.
--- @return #SPAWN self
--- @usage Spawn_Helicopter:CleanUp( 20 ) -- CleanUp the spawning of the helicopters every 20 seconds when they become inactive.
-function SPAWN:InitCleanUp( SpawnCleanUpInterval )
- self:F( { self.SpawnTemplatePrefix, SpawnCleanUpInterval } )
-
- self.SpawnCleanUpInterval = SpawnCleanUpInterval
- self.SpawnCleanUpTimeStamps = {}
-
- local SpawnGroup, SpawnCursor = self:GetFirstAliveGroup()
- self:T( { "CleanUp Scheduler:", SpawnGroup } )
-
- --self.CleanUpFunction = routines.scheduleFunction( self._SpawnCleanUpScheduler, { self }, timer.getTime() + 1, SpawnCleanUpInterval )
- self.CleanUpScheduler = SCHEDULER:New( self, self._SpawnCleanUpScheduler, {}, 1, SpawnCleanUpInterval, 0.2 )
- return self
-end
-
-
-
---- Makes the groups visible before start (like a batallion).
--- The method will take the position of the group as the first position in the array.
--- @param #SPAWN self
--- @param #number SpawnAngle The angle in degrees how the groups and each unit of the group will be positioned.
--- @param #number SpawnWidth The amount of Groups that will be positioned on the X axis.
--- @param #number SpawnDeltaX The space between each Group on the X-axis.
--- @param #number SpawnDeltaY The space between each Group on the Y-axis.
--- @return #SPAWN self
--- @usage
--- -- Define an array of Groups.
--- Spawn_BE_Ground = SPAWN:New( 'BE Ground' ):InitLimit( 2, 24 ):InitArray( 90, "Diamond", 10, 100, 50 )
-function SPAWN:InitArray( SpawnAngle, SpawnWidth, SpawnDeltaX, SpawnDeltaY )
- self:F( { self.SpawnTemplatePrefix, SpawnAngle, SpawnWidth, SpawnDeltaX, SpawnDeltaY } )
-
- self.SpawnVisible = true -- When the first Spawn executes, all the Groups need to be made visible before start.
-
- local SpawnX = 0
- local SpawnY = 0
- local SpawnXIndex = 0
- local SpawnYIndex = 0
-
- for SpawnGroupID = 1, self.SpawnMaxGroups do
- self:T( { SpawnX, SpawnY, SpawnXIndex, SpawnYIndex } )
-
- self.SpawnGroups[SpawnGroupID].Visible = true
- self.SpawnGroups[SpawnGroupID].Spawned = false
-
- SpawnXIndex = SpawnXIndex + 1
- if SpawnWidth and SpawnWidth ~= 0 then
- if SpawnXIndex >= SpawnWidth then
- SpawnXIndex = 0
- SpawnYIndex = SpawnYIndex + 1
- end
- end
-
- local SpawnRootX = self.SpawnGroups[SpawnGroupID].SpawnTemplate.x
- local SpawnRootY = self.SpawnGroups[SpawnGroupID].SpawnTemplate.y
-
- self:_TranslateRotate( SpawnGroupID, SpawnRootX, SpawnRootY, SpawnX, SpawnY, SpawnAngle )
-
- self.SpawnGroups[SpawnGroupID].SpawnTemplate.lateActivation = true
- self.SpawnGroups[SpawnGroupID].SpawnTemplate.visible = true
-
- self.SpawnGroups[SpawnGroupID].Visible = true
-
- self:HandleEvent( EVENTS.Birth, self._OnBirth )
- self:HandleEvent( EVENTS.Dead, self._OnDeadOrCrash )
- self:HandleEvent( EVENTS.Crash, self._OnDeadOrCrash )
- if self.Repeat then
- self:HandleEvent( EVENTS.Takeoff, self._OnTakeOff )
- self:HandleEvent( EVENTS.Land, self._OnLand )
- end
- if self.RepeatOnEngineShutDown then
- self:HandleEvent( EVENTS.EngineShutdown, self._OnEngineShutDown )
- end
-
- self.SpawnGroups[SpawnGroupID].Group = _DATABASE:Spawn( self.SpawnGroups[SpawnGroupID].SpawnTemplate )
-
- SpawnX = SpawnXIndex * SpawnDeltaX
- SpawnY = SpawnYIndex * SpawnDeltaY
- end
-
- return self
-end
-
-do -- AI methods
- --- Turns the AI On or Off for the @{Group} when spawning.
- -- @param #SPAWN self
- -- @param #boolean AIOnOff A value of true sets the AI On, a value of false sets the AI Off.
- -- @return #SPAWN The SPAWN object
- function SPAWN:InitAIOnOff( AIOnOff )
-
- self.AIOnOff = AIOnOff
- return self
- end
-
- --- Turns the AI On for the @{Group} when spawning.
- -- @param #SPAWN self
- -- @return #SPAWN The SPAWN object
- function SPAWN:InitAIOn()
-
- return self:InitAIOnOff( true )
- end
-
- --- Turns the AI Off for the @{Group} when spawning.
- -- @param #SPAWN self
- -- @return #SPAWN The SPAWN object
- function SPAWN:InitAIOff()
-
- return self:InitAIOnOff( false )
- end
-
-end -- AI methods
-
---- Will spawn a group based on the internal index.
--- Note: Uses @{DATABASE} module defined in MOOSE.
--- @param #SPAWN self
--- @return Wrapper.Group#GROUP The group that was spawned. You can use this group for further actions.
-function SPAWN:Spawn()
- self:F( { self.SpawnTemplatePrefix, self.SpawnIndex, self.AliveUnits } )
-
- return self:SpawnWithIndex( self.SpawnIndex + 1 )
-end
-
---- Will re-spawn a group based on a given index.
--- Note: Uses @{DATABASE} module defined in MOOSE.
--- @param #SPAWN self
--- @param #string SpawnIndex The index of the group to be spawned.
--- @return Wrapper.Group#GROUP The group that was spawned. You can use this group for further actions.
-function SPAWN:ReSpawn( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex } )
-
- if not SpawnIndex then
- SpawnIndex = 1
- end
-
--- TODO: This logic makes DCS crash and i don't know why (yet).
- local SpawnGroup = self:GetGroupFromIndex( SpawnIndex )
- local WayPoints = SpawnGroup and SpawnGroup.WayPoints or nil
- if SpawnGroup then
- local SpawnDCSGroup = SpawnGroup:GetDCSObject()
- if SpawnDCSGroup then
- SpawnGroup:Destroy()
- end
- end
-
- local SpawnGroup = self:SpawnWithIndex( SpawnIndex )
- if SpawnGroup and WayPoints then
- -- If there were WayPoints set, then Re-Execute those WayPoints!
- SpawnGroup:WayPointInitialize( WayPoints )
- SpawnGroup:WayPointExecute( 1, 5 )
- end
-
- if SpawnGroup.ReSpawnFunction then
- SpawnGroup:ReSpawnFunction()
- end
-
- return SpawnGroup
-end
-
---- Will spawn a group with a specified index number.
--- Uses @{DATABASE} global object defined in MOOSE.
--- @param #SPAWN self
--- @param #string SpawnIndex The index of the group to be spawned.
--- @return Wrapper.Group#GROUP The group that was spawned. You can use this group for further actions.
-function SPAWN:SpawnWithIndex( SpawnIndex )
- self:F2( { SpawnTemplatePrefix = self.SpawnTemplatePrefix, SpawnIndex = SpawnIndex, AliveUnits = self.AliveUnits, SpawnMaxGroups = self.SpawnMaxGroups } )
-
- if self:_GetSpawnIndex( SpawnIndex ) then
-
- if self.SpawnGroups[self.SpawnIndex].Visible then
- self.SpawnGroups[self.SpawnIndex].Group:Activate()
- else
-
- local SpawnTemplate = self.SpawnGroups[self.SpawnIndex].SpawnTemplate
- self:T( SpawnTemplate.name )
-
- if SpawnTemplate then
-
- local PointVec3 = POINT_VEC3:New( SpawnTemplate.route.points[1].x, SpawnTemplate.route.points[1].alt, SpawnTemplate.route.points[1].y )
- self:T( { "Current point of ", self.SpawnTemplatePrefix, PointVec3 } )
-
- -- If RandomizePosition, then Randomize the formation in the zone band, keeping the template.
- if self.SpawnRandomizePosition then
- local RandomVec2 = PointVec3:GetRandomVec2InRadius( self.SpawnRandomizePositionOuterRadius, self.SpawnRandomizePositionInnerRadius )
- local CurrentX = SpawnTemplate.units[1].x
- local CurrentY = SpawnTemplate.units[1].y
- SpawnTemplate.x = RandomVec2.x
- SpawnTemplate.y = RandomVec2.y
- for UnitID = 1, #SpawnTemplate.units do
- SpawnTemplate.units[UnitID].x = SpawnTemplate.units[UnitID].x + ( RandomVec2.x - CurrentX )
- SpawnTemplate.units[UnitID].y = SpawnTemplate.units[UnitID].y + ( RandomVec2.y - CurrentY )
- self:T( 'SpawnTemplate.units['..UnitID..'].x = ' .. SpawnTemplate.units[UnitID].x .. ', SpawnTemplate.units['..UnitID..'].y = ' .. SpawnTemplate.units[UnitID].y )
- end
- end
-
- -- If RandomizeUnits, then Randomize the formation at the start point.
- if self.SpawnRandomizeUnits then
- for UnitID = 1, #SpawnTemplate.units do
- local RandomVec2 = PointVec3:GetRandomVec2InRadius( self.SpawnOuterRadius, self.SpawnInnerRadius )
- SpawnTemplate.units[UnitID].x = RandomVec2.x
- SpawnTemplate.units[UnitID].y = RandomVec2.y
- self:T( 'SpawnTemplate.units['..UnitID..'].x = ' .. SpawnTemplate.units[UnitID].x .. ', SpawnTemplate.units['..UnitID..'].y = ' .. SpawnTemplate.units[UnitID].y )
- end
- end
-
- if SpawnTemplate.CategoryID == Group.Category.HELICOPTER or SpawnTemplate.CategoryID == Group.Category.AIRPLANE then
- if SpawnTemplate.route.points[1].type == "TakeOffParking" then
- SpawnTemplate.uncontrolled = self.SpawnUnControlled
- end
- end
- end
-
- self:HandleEvent( EVENTS.Birth, self._OnBirth )
- self:HandleEvent( EVENTS.Dead, self._OnDeadOrCrash )
- self:HandleEvent( EVENTS.Crash, self._OnDeadOrCrash )
- if self.Repeat then
- self:HandleEvent( EVENTS.Takeoff, self._OnTakeOff )
- self:HandleEvent( EVENTS.Land, self._OnLand )
- end
- if self.RepeatOnEngineShutDown then
- self:HandleEvent( EVENTS.EngineShutdown, self._OnEngineShutDown )
- end
-
- self.SpawnGroups[self.SpawnIndex].Group = _DATABASE:Spawn( SpawnTemplate )
-
- local SpawnGroup = self.SpawnGroups[self.SpawnIndex].Group -- Wrapper.Group#GROUP
-
- --TODO: Need to check if this function doesn't need to be scheduled, as the group may not be immediately there!
- if SpawnGroup then
-
- SpawnGroup:SetAIOnOff( self.AIOnOff )
- end
-
- self:T3( SpawnTemplate.name )
-
- -- If there is a SpawnFunction hook defined, call it.
- if self.SpawnFunctionHook then
- self.SpawnFunctionHook( self.SpawnGroups[self.SpawnIndex].Group, unpack( self.SpawnFunctionArguments ) )
- end
- -- TODO: Need to fix this by putting an "R" in the name of the group when the group repeats.
- --if self.Repeat then
- -- _DATABASE:SetStatusGroup( SpawnTemplate.name, "ReSpawn" )
- --end
- end
-
- self.SpawnGroups[self.SpawnIndex].Spawned = true
- return self.SpawnGroups[self.SpawnIndex].Group
- else
- --self:E( { self.SpawnTemplatePrefix, "No more Groups to Spawn:", SpawnIndex, self.SpawnMaxGroups } )
- end
-
- return nil
-end
-
---- Spawns new groups at varying time intervals.
--- This is useful if you want to have continuity within your missions of certain (AI) groups to be present (alive) within your missions.
--- @param #SPAWN self
--- @param #number SpawnTime The time interval defined in seconds between each new spawn of new groups.
--- @param #number SpawnTimeVariation The variation to be applied on the defined time interval between each new spawn.
--- The variation is a number between 0 and 1, representing the %-tage of variation to be applied on the time interval.
--- @return #SPAWN self
--- @usage
--- -- NATO helicopters engaging in the battle field.
--- -- The time interval is set to SPAWN new helicopters between each 600 seconds, with a time variation of 50%.
--- -- The time variation in this case will be between 450 seconds and 750 seconds.
--- -- This is calculated as follows:
--- -- Low limit: 600 * ( 1 - 0.5 / 2 ) = 450
--- -- High limit: 600 * ( 1 + 0.5 / 2 ) = 750
--- -- Between these two values, a random amount of seconds will be choosen for each new spawn of the helicopters.
--- Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' ):Schedule( 600, 0.5 )
-function SPAWN:SpawnScheduled( SpawnTime, SpawnTimeVariation )
- self:F( { SpawnTime, SpawnTimeVariation } )
-
- if SpawnTime ~= nil and SpawnTimeVariation ~= nil then
- self.SpawnScheduler = SCHEDULER:New( self, self._Scheduler, {}, 1, SpawnTime, SpawnTimeVariation )
- end
-
- return self
-end
-
---- Will re-start the spawning scheduler.
--- Note: This method is only required to be called when the schedule was stopped.
-function SPAWN:SpawnScheduleStart()
- self:F( { self.SpawnTemplatePrefix } )
-
- self.SpawnScheduler:Start()
-end
-
---- Will stop the scheduled spawning scheduler.
-function SPAWN:SpawnScheduleStop()
- self:F( { self.SpawnTemplatePrefix } )
-
- self.SpawnScheduler:Stop()
-end
-
-
---- Allows to place a CallFunction hook when a new group spawns.
--- The provided method will be called when a new group is spawned, including its given parameters.
--- The first parameter of the SpawnFunction is the @{Group#GROUP} that was spawned.
--- @param #SPAWN self
--- @param #function SpawnCallBackFunction The function to be called when a group spawns.
--- @param SpawnFunctionArguments A random amount of arguments to be provided to the function when the group spawns.
--- @return #SPAWN
--- @usage
--- -- Declare SpawnObject and call a function when a new Group is spawned.
--- local SpawnObject = SPAWN
--- :New( "SpawnObject" )
--- :InitLimit( 2, 10 )
--- :OnSpawnGroup(
--- function( SpawnGroup )
--- SpawnGroup:E( "I am spawned" )
--- end
--- )
--- :SpawnScheduled( 300, 0.3 )
-function SPAWN:OnSpawnGroup( SpawnCallBackFunction, ... )
- self:F( "OnSpawnGroup" )
-
- self.SpawnFunctionHook = SpawnCallBackFunction
- self.SpawnFunctionArguments = {}
- if arg then
- self.SpawnFunctionArguments = arg
- end
-
- return self
-end
-
-
---- Will spawn a group from a Vec3 in 3D space.
--- This method is mostly advisable to be used if you want to simulate spawning units in the air, like helicopters or airplanes.
--- Note that each point in the route assigned to the spawning group is reset to the point of the spawn.
--- You can use the returned group to further define the route to be followed.
--- @param #SPAWN self
--- @param Dcs.DCSTypes#Vec3 Vec3 The Vec3 coordinates where to spawn the group.
--- @param #number SpawnIndex (optional) The index which group to spawn within the given zone.
--- @return Wrapper.Group#GROUP that was spawned.
--- @return #nil Nothing was spawned.
-function SPAWN:SpawnFromVec3( Vec3, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, Vec3, SpawnIndex } )
-
- local PointVec3 = POINT_VEC3:NewFromVec3( Vec3 )
- self:T2(PointVec3)
-
- if SpawnIndex then
- else
- SpawnIndex = self.SpawnIndex + 1
- end
-
- if self:_GetSpawnIndex( SpawnIndex ) then
-
- local SpawnTemplate = self.SpawnGroups[self.SpawnIndex].SpawnTemplate
-
- if SpawnTemplate then
-
- self:T( { "Current point of ", self.SpawnTemplatePrefix, Vec3 } )
-
- -- 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 = Vec3.x + ( SX - BX )
- local TY = Vec3.z + ( SY - BY )
- SpawnTemplate.units[UnitID].x = TX
- SpawnTemplate.units[UnitID].y = TY
- SpawnTemplate.units[UnitID].alt = Vec3.y
- self:T( 'After Translation SpawnTemplate.units['..UnitID..'].x = ' .. SpawnTemplate.units[UnitID].x .. ', SpawnTemplate.units['..UnitID..'].y = ' .. SpawnTemplate.units[UnitID].y )
- end
-
- SpawnTemplate.route.points[1].x = Vec3.x
- SpawnTemplate.route.points[1].y = Vec3.z
- SpawnTemplate.route.points[1].alt = Vec3.y
-
- SpawnTemplate.x = Vec3.x
- SpawnTemplate.y = Vec3.z
-
- return self:SpawnWithIndex( self.SpawnIndex )
- end
- end
-
- return nil
-end
-
---- Will spawn a group from a Vec2 in 3D space.
--- This method is mostly advisable to be used if you want to simulate spawning groups on the ground from air units, like vehicles.
--- Note that each point in the route assigned to the spawning group is reset to the point of the spawn.
--- You can use the returned group to further define the route to be followed.
--- @param #SPAWN self
--- @param Dcs.DCSTypes#Vec2 Vec2 The Vec2 coordinates where to spawn the group.
--- @param #number SpawnIndex (optional) The index which group to spawn within the given zone.
--- @return Wrapper.Group#GROUP that was spawned.
--- @return #nil Nothing was spawned.
-function SPAWN:SpawnFromVec2( Vec2, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, Vec2, SpawnIndex } )
-
- local PointVec2 = POINT_VEC2:NewFromVec2( Vec2 )
- return self:SpawnFromVec3( PointVec2:GetVec3(), SpawnIndex )
-end
-
-
---- Will spawn a group from a hosting unit. This method is mostly advisable to be used if you want to simulate spawning from air units, like helicopters, which are dropping infantry into a defined Landing Zone.
--- Note that each point in the route assigned to the spawning group is reset to the point of the spawn.
--- You can use the returned group to further define the route to be followed.
--- @param #SPAWN self
--- @param Wrapper.Unit#UNIT HostUnit The air or ground unit dropping or unloading the group.
--- @param #number SpawnIndex (optional) The index which group to spawn within the given zone.
--- @return Wrapper.Group#GROUP that was spawned.
--- @return #nil Nothing was spawned.
-function SPAWN:SpawnFromUnit( HostUnit, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, HostUnit, SpawnIndex } )
-
- if HostUnit and HostUnit:IsAlive() then -- and HostUnit:getUnit(1):inAir() == false then
- return self:SpawnFromVec3( HostUnit:GetVec3(), SpawnIndex )
- end
-
- return nil
-end
-
---- Will spawn a group from a hosting static. This method is mostly advisable to be used if you want to simulate spawning from buldings and structures (static buildings).
--- You can use the returned group to further define the route to be followed.
--- @param #SPAWN self
--- @param Wrapper.Static#STATIC HostStatic The static dropping or unloading the group.
--- @param #number SpawnIndex (optional) The index which group to spawn within the given zone.
--- @return Wrapper.Group#GROUP that was spawned.
--- @return #nil Nothing was spawned.
-function SPAWN:SpawnFromStatic( HostStatic, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, HostStatic, SpawnIndex } )
-
- if HostStatic and HostStatic:IsAlive() then
- return self:SpawnFromVec3( HostStatic:GetVec3(), SpawnIndex )
- end
-
- return nil
-end
-
---- Will spawn a Group within a given @{Zone}.
--- The @{Zone} can be of any type derived from @{Zone#ZONE_BASE}.
--- Once the @{Group} is spawned within the zone, the @{Group} will continue on its route.
--- The **first waypoint** (where the group is spawned) is replaced with the zone location coordinates.
--- @param #SPAWN self
--- @param Core.Zone#ZONE Zone The zone where the group is to be spawned.
--- @param #boolean RandomizeGroup (optional) Randomization of the @{Group} position in the zone.
--- @param #number SpawnIndex (optional) The index which group to spawn within the given zone.
--- @return Wrapper.Group#GROUP that was spawned.
--- @return #nil when nothing was spawned.
-function SPAWN:SpawnInZone( Zone, RandomizeGroup, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, Zone, RandomizeGroup, SpawnIndex } )
-
- if Zone then
- if RandomizeGroup then
- return self:SpawnFromVec2( Zone:GetRandomVec2(), SpawnIndex )
- else
- return self:SpawnFromVec2( Zone:GetVec2(), SpawnIndex )
- end
- end
-
- return nil
-end
-
---- (**AIR**) Will spawn a plane group in UnControlled or Controlled mode...
--- This will be similar to the uncontrolled flag setting in the ME.
--- You can use UnControlled mode to simulate planes startup and ready for take-off but aren't moving (yet).
--- ReSpawn the plane in Controlled mode, and the plane will move...
--- @param #SPAWN self
--- @param #boolean UnControlled true if UnControlled, false if Controlled.
--- @return #SPAWN self
-function SPAWN:InitUnControlled( UnControlled )
- self:F2( { self.SpawnTemplatePrefix, UnControlled } )
-
- self.SpawnUnControlled = UnControlled
-
- for SpawnGroupID = 1, self.SpawnMaxGroups do
- self.SpawnGroups[SpawnGroupID].UnControlled = UnControlled
- end
-
- return self
-end
-
-
-
---- Will return the SpawnGroupName either with with a specific count number or without any count.
--- @param #SPAWN self
--- @param #number SpawnIndex Is the number of the Group that is to be spawned.
--- @return #string SpawnGroupName
-function SPAWN:SpawnGroupName( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex } )
-
- local SpawnPrefix = self.SpawnTemplatePrefix
- if self.SpawnAliasPrefix then
- SpawnPrefix = self.SpawnAliasPrefix
- end
-
- if SpawnIndex then
- local SpawnName = string.format( '%s#%03d', SpawnPrefix, SpawnIndex )
- self:T( SpawnName )
- return SpawnName
- else
- self:T( SpawnPrefix )
- return SpawnPrefix
- end
-
-end
-
---- Will find the first alive @{Group} it has spawned, and return the alive @{Group} object and the first Index where the first alive @{Group} object has been found.
--- @param #SPAWN self
--- @return Wrapper.Group#GROUP, #number The @{Group} object found, the new Index where the group was found.
--- @return #nil, #nil When no group is found, #nil is returned.
--- @usage
--- -- Find the first alive @{Group} object of the SpawnPlanes SPAWN object @{Group} collection that it has spawned during the mission.
--- local GroupPlane, Index = SpawnPlanes:GetFirstAliveGroup()
--- while GroupPlane ~= nil do
--- -- Do actions with the GroupPlane object.
--- GroupPlane, Index = SpawnPlanes:GetNextAliveGroup( Index )
--- end
-function SPAWN:GetFirstAliveGroup()
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix } )
-
- for SpawnIndex = 1, self.SpawnCount do
- local SpawnGroup = self:GetGroupFromIndex( SpawnIndex )
- if SpawnGroup and SpawnGroup:IsAlive() then
- return SpawnGroup, SpawnIndex
- end
- end
-
- return nil, nil
-end
-
-
---- Will find the next alive @{Group} object from a given Index, and return a reference to the alive @{Group} object and the next Index where the alive @{Group} has been found.
--- @param #SPAWN self
--- @param #number SpawnIndexStart A Index holding the start position to search from. This method can also be used to find the first alive @{Group} object from the given Index.
--- @return Wrapper.Group#GROUP, #number The next alive @{Group} object found, the next Index where the next alive @{Group} object was found.
--- @return #nil, #nil When no alive @{Group} object is found from the start Index position, #nil is returned.
--- @usage
--- -- Find the first alive @{Group} object of the SpawnPlanes SPAWN object @{Group} collection that it has spawned during the mission.
--- local GroupPlane, Index = SpawnPlanes:GetFirstAliveGroup()
--- while GroupPlane ~= nil do
--- -- Do actions with the GroupPlane object.
--- GroupPlane, Index = SpawnPlanes:GetNextAliveGroup( Index )
--- end
-function SPAWN:GetNextAliveGroup( SpawnIndexStart )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnIndexStart } )
-
- SpawnIndexStart = SpawnIndexStart + 1
- for SpawnIndex = SpawnIndexStart, self.SpawnCount do
- local SpawnGroup = self:GetGroupFromIndex( SpawnIndex )
- if SpawnGroup and SpawnGroup:IsAlive() then
- return SpawnGroup, SpawnIndex
- end
- end
-
- return nil, nil
-end
-
---- Will find the last alive @{Group} object, and will return a reference to the last live @{Group} object and the last Index where the last alive @{Group} object has been found.
--- @param #SPAWN self
--- @return Wrapper.Group#GROUP, #number The last alive @{Group} object found, the last Index where the last alive @{Group} object was found.
--- @return #nil, #nil When no alive @{Group} object is found, #nil is returned.
--- @usage
--- -- Find the last alive @{Group} object of the SpawnPlanes SPAWN object @{Group} collection that it has spawned during the mission.
--- local GroupPlane, Index = SpawnPlanes:GetLastAliveGroup()
--- if GroupPlane then -- GroupPlane can be nil!!!
--- -- Do actions with the GroupPlane object.
--- end
-function SPAWN:GetLastAliveGroup()
- self:F( { self.SpawnTemplatePrefixself.SpawnAliasPrefix } )
-
- self.SpawnIndex = self:_GetLastIndex()
- for SpawnIndex = self.SpawnIndex, 1, -1 do
- local SpawnGroup = self:GetGroupFromIndex( SpawnIndex )
- if SpawnGroup and SpawnGroup:IsAlive() then
- self.SpawnIndex = SpawnIndex
- return SpawnGroup
- end
- end
-
- self.SpawnIndex = nil
- return nil
-end
-
-
-
---- Get the group from an index.
--- Returns the group from the SpawnGroups list.
--- If no index is given, it will return the first group in the list.
--- @param #SPAWN self
--- @param #number SpawnIndex The index of the group to return.
--- @return Wrapper.Group#GROUP self
-function SPAWN:GetGroupFromIndex( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnIndex } )
-
- if not SpawnIndex then
- SpawnIndex = 1
- end
-
- if self.SpawnGroups and self.SpawnGroups[SpawnIndex] then
- local SpawnGroup = self.SpawnGroups[SpawnIndex].Group
- return SpawnGroup
- else
- return nil
- end
-end
-
-
---- Return the prefix of a SpawnUnit.
--- The method will search for a #-mark, and will return the text before the #-mark.
--- It will return nil of no prefix was found.
--- @param #SPAWN self
--- @param Dcs.DCSWrapper.Unit#UNIT DCSUnit The @{DCSUnit} to be searched.
--- @return #string The prefix
--- @return #nil Nothing found
-function SPAWN:_GetPrefixFromGroup( SpawnGroup )
- self:F3( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnGroup } )
-
- local GroupName = SpawnGroup:GetName()
- if GroupName then
- local SpawnPrefix = string.match( GroupName, ".*#" )
- if SpawnPrefix then
- SpawnPrefix = SpawnPrefix:sub( 1, -2 )
- end
- return SpawnPrefix
- end
-
- return nil
-end
-
-
---- Get the index from a given group.
--- The function will search the name of the group for a #, and will return the number behind the #-mark.
-function SPAWN:GetSpawnIndexFromGroup( SpawnGroup )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnGroup } )
-
- local IndexString = string.match( SpawnGroup:GetName(), "#(%d*)$" ):sub( 2 )
- local Index = tonumber( IndexString )
-
- self:T3( IndexString, Index )
- return Index
-
-end
-
---- Return the last maximum index that can be used.
-function SPAWN:_GetLastIndex()
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix } )
-
- return self.SpawnMaxGroups
-end
-
---- Initalize the SpawnGroups collection.
--- @param #SPAWN self
-function SPAWN:_InitializeSpawnGroups( SpawnIndex )
- self:F3( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnIndex } )
-
- if not self.SpawnGroups[SpawnIndex] then
- self.SpawnGroups[SpawnIndex] = {}
- self.SpawnGroups[SpawnIndex].Visible = false
- self.SpawnGroups[SpawnIndex].Spawned = false
- self.SpawnGroups[SpawnIndex].UnControlled = false
- self.SpawnGroups[SpawnIndex].SpawnTime = 0
-
- self.SpawnGroups[SpawnIndex].SpawnTemplatePrefix = self.SpawnTemplatePrefix
- self.SpawnGroups[SpawnIndex].SpawnTemplate = self:_Prepare( self.SpawnGroups[SpawnIndex].SpawnTemplatePrefix, SpawnIndex )
- end
-
- self:_RandomizeTemplate( SpawnIndex )
- self:_RandomizeRoute( SpawnIndex )
- --self:_TranslateRotate( SpawnIndex )
-
- return self.SpawnGroups[SpawnIndex]
-end
-
-
-
---- Gets the CategoryID of the Group with the given SpawnPrefix
-function SPAWN:_GetGroupCategoryID( SpawnPrefix )
- local TemplateGroup = Group.getByName( SpawnPrefix )
-
- if TemplateGroup then
- return TemplateGroup:getCategory()
- else
- return nil
- end
-end
-
---- Gets the CoalitionID of the Group with the given SpawnPrefix
-function SPAWN:_GetGroupCoalitionID( SpawnPrefix )
- local TemplateGroup = Group.getByName( SpawnPrefix )
-
- if TemplateGroup then
- return TemplateGroup:getCoalition()
- else
- return nil
- end
-end
-
---- Gets the CountryID of the Group with the given SpawnPrefix
-function SPAWN:_GetGroupCountryID( SpawnPrefix )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnPrefix } )
-
- local TemplateGroup = Group.getByName( SpawnPrefix )
-
- if TemplateGroup then
- local TemplateUnits = TemplateGroup:getUnits()
- return TemplateUnits[1]:getCountry()
- else
- return nil
- end
-end
-
---- Gets the Group Template from the ME environment definition.
--- This method used the @{DATABASE} object, which contains ALL initial and new spawned object in MOOSE.
--- @param #SPAWN self
--- @param #string SpawnTemplatePrefix
--- @return @SPAWN self
-function SPAWN:_GetTemplate( SpawnTemplatePrefix )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix, SpawnTemplatePrefix } )
-
- local SpawnTemplate = nil
-
- SpawnTemplate = routines.utils.deepCopy( _DATABASE.Templates.Groups[SpawnTemplatePrefix].Template )
-
- if SpawnTemplate == nil then
- error( 'No Template returned for SpawnTemplatePrefix = ' .. SpawnTemplatePrefix )
- end
-
- --SpawnTemplate.SpawnCoalitionID = self:_GetGroupCoalitionID( SpawnTemplatePrefix )
- --SpawnTemplate.SpawnCategoryID = self:_GetGroupCategoryID( SpawnTemplatePrefix )
- --SpawnTemplate.SpawnCountryID = self:_GetGroupCountryID( SpawnTemplatePrefix )
-
- self:T3( { SpawnTemplate } )
- return SpawnTemplate
-end
-
---- Prepares the new Group Template.
--- @param #SPAWN self
--- @param #string SpawnTemplatePrefix
--- @param #number SpawnIndex
--- @return #SPAWN self
-function SPAWN:_Prepare( SpawnTemplatePrefix, SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, self.SpawnAliasPrefix } )
-
- local SpawnTemplate = self:_GetTemplate( SpawnTemplatePrefix )
- SpawnTemplate.name = self:SpawnGroupName( SpawnIndex )
-
- SpawnTemplate.groupId = nil
- --SpawnTemplate.lateActivation = false
- SpawnTemplate.lateActivation = false
-
- if SpawnTemplate.CategoryID == Group.Category.GROUND then
- self:T3( "For ground units, visible needs to be false..." )
- SpawnTemplate.visible = false
- end
-
- if self.SpawnInitKeepUnitNames == false then
- for UnitID = 1, #SpawnTemplate.units do
- SpawnTemplate.units[UnitID].name = string.format( SpawnTemplate.name .. '-%02d', UnitID )
- SpawnTemplate.units[UnitID].unitId = nil
- end
- else
- for UnitID = 1, #SpawnTemplate.units do
- local UnitPrefix, Rest = string.match( SpawnTemplate.units[UnitID].name, "^([^#]+)#?" ):gsub( "^%s*(.-)%s*$", "%1" )
- self:T( { UnitPrefix, Rest } )
-
- SpawnTemplate.units[UnitID].name = string.format( '%s#%03d-%02d', UnitPrefix, SpawnIndex, UnitID )
- SpawnTemplate.units[UnitID].unitId = nil
- end
- end
-
- self:T3( { "Template:", SpawnTemplate } )
- return SpawnTemplate
-
-end
-
---- Private method randomizing the routes.
--- @param #SPAWN self
--- @param #number SpawnIndex The index of the group to be spawned.
--- @return #SPAWN
-function SPAWN:_RandomizeRoute( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex, self.SpawnRandomizeRoute, self.SpawnRandomizeRouteStartPoint, self.SpawnRandomizeRouteEndPoint, self.SpawnRandomizeRouteRadius } )
-
- if self.SpawnRandomizeRoute then
- local SpawnTemplate = self.SpawnGroups[SpawnIndex].SpawnTemplate
- local RouteCount = #SpawnTemplate.route.points
-
- for t = self.SpawnRandomizeRouteStartPoint + 1, ( RouteCount - self.SpawnRandomizeRouteEndPoint ) do
-
- SpawnTemplate.route.points[t].x = SpawnTemplate.route.points[t].x + math.random( self.SpawnRandomizeRouteRadius * -1, self.SpawnRandomizeRouteRadius )
- SpawnTemplate.route.points[t].y = SpawnTemplate.route.points[t].y + math.random( self.SpawnRandomizeRouteRadius * -1, self.SpawnRandomizeRouteRadius )
-
- -- Manage randomization of altitude for airborne units ...
- if SpawnTemplate.CategoryID == Group.Category.AIRPLANE or SpawnTemplate.CategoryID == Group.Category.HELICOPTER then
- if SpawnTemplate.route.points[t].alt and self.SpawnRandomizeRouteHeight then
- SpawnTemplate.route.points[t].alt = SpawnTemplate.route.points[t].alt + math.random( 1, self.SpawnRandomizeRouteHeight )
- end
- else
- SpawnTemplate.route.points[t].alt = nil
- end
-
- self:T( 'SpawnTemplate.route.points[' .. t .. '].x = ' .. SpawnTemplate.route.points[t].x .. ', SpawnTemplate.route.points[' .. t .. '].y = ' .. SpawnTemplate.route.points[t].y )
- end
- end
-
- self:_RandomizeZones( SpawnIndex )
-
- return self
-end
-
---- Private method that randomizes the template of the group.
--- @param #SPAWN self
--- @param #number SpawnIndex
--- @return #SPAWN self
-function SPAWN:_RandomizeTemplate( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex, self.SpawnRandomizeTemplate } )
-
- if self.SpawnRandomizeTemplate then
- self.SpawnGroups[SpawnIndex].SpawnTemplatePrefix = self.SpawnTemplatePrefixTable[ math.random( 1, #self.SpawnTemplatePrefixTable ) ]
- self.SpawnGroups[SpawnIndex].SpawnTemplate = self:_Prepare( self.SpawnGroups[SpawnIndex].SpawnTemplatePrefix, SpawnIndex )
- self.SpawnGroups[SpawnIndex].SpawnTemplate.route = routines.utils.deepCopy( self.SpawnTemplate.route )
- self.SpawnGroups[SpawnIndex].SpawnTemplate.x = self.SpawnTemplate.x
- self.SpawnGroups[SpawnIndex].SpawnTemplate.y = self.SpawnTemplate.y
- self.SpawnGroups[SpawnIndex].SpawnTemplate.start_time = self.SpawnTemplate.start_time
- local OldX = self.SpawnGroups[SpawnIndex].SpawnTemplate.units[1].x
- local OldY = self.SpawnGroups[SpawnIndex].SpawnTemplate.units[1].y
- for UnitID = 1, #self.SpawnGroups[SpawnIndex].SpawnTemplate.units do
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].heading = self.SpawnTemplate.units[1].heading
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].x = self.SpawnTemplate.units[1].x + ( self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].x - OldX )
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].y = self.SpawnTemplate.units[1].y + ( self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].y - OldY )
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[UnitID].alt = self.SpawnTemplate.units[1].alt
- end
- end
-
- self:_RandomizeRoute( SpawnIndex )
-
- return self
-end
-
---- Private method that randomizes the @{Zone}s where the Group will be spawned.
--- @param #SPAWN self
--- @param #number SpawnIndex
--- @return #SPAWN self
-function SPAWN:_RandomizeZones( SpawnIndex )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex, self.SpawnRandomizeZones } )
-
- if self.SpawnRandomizeZones then
- local SpawnZone = nil -- Core.Zone#ZONE_BASE
- while not SpawnZone do
- self:T( { SpawnZoneTableCount = #self.SpawnZoneTable, self.SpawnZoneTable } )
- local ZoneID = math.random( #self.SpawnZoneTable )
- self:T( ZoneID )
- SpawnZone = self.SpawnZoneTable[ ZoneID ]:GetZoneMaybe()
- end
-
- self:T( "Preparing Spawn in Zone", SpawnZone:GetName() )
-
- local SpawnVec2 = SpawnZone:GetRandomVec2()
-
- self:T( { SpawnVec2 = SpawnVec2 } )
-
- local SpawnTemplate = self.SpawnGroups[SpawnIndex].SpawnTemplate
-
- self:T( { Route = SpawnTemplate.route } )
-
- for UnitID = 1, #SpawnTemplate.units do
- local UnitTemplate = SpawnTemplate.units[UnitID]
- self:T( 'Before Translation SpawnTemplate.units['..UnitID..'].x = ' .. UnitTemplate.x .. ', SpawnTemplate.units['..UnitID..'].y = ' .. UnitTemplate.y )
- local SX = UnitTemplate.x
- local SY = UnitTemplate.y
- local BX = SpawnTemplate.route.points[1].x
- local BY = SpawnTemplate.route.points[1].y
- local TX = SpawnVec2.x + ( SX - BX )
- local TY = SpawnVec2.y + ( SY - BY )
- UnitTemplate.x = TX
- UnitTemplate.y = TY
- -- TODO: Manage altitude based on landheight...
- --SpawnTemplate.units[UnitID].alt = SpawnVec2:
- self:T( 'After Translation SpawnTemplate.units['..UnitID..'].x = ' .. UnitTemplate.x .. ', SpawnTemplate.units['..UnitID..'].y = ' .. UnitTemplate.y )
- end
- SpawnTemplate.x = SpawnVec2.x
- SpawnTemplate.y = SpawnVec2.y
- SpawnTemplate.route.points[1].x = SpawnVec2.x
- SpawnTemplate.route.points[1].y = SpawnVec2.y
- end
-
- return self
-
-end
-
-function SPAWN:_TranslateRotate( SpawnIndex, SpawnRootX, SpawnRootY, SpawnX, SpawnY, SpawnAngle )
- self:F( { self.SpawnTemplatePrefix, SpawnIndex, SpawnRootX, SpawnRootY, SpawnX, SpawnY, SpawnAngle } )
-
- -- Translate
- local TranslatedX = SpawnX
- local TranslatedY = SpawnY
-
- -- Rotate
- -- From Wikipedia: https://en.wikipedia.org/wiki/Rotation_matrix#Common_rotations
- -- x' = x \cos \theta - y \sin \theta\
- -- y' = x \sin \theta + y \cos \theta\
- local RotatedX = - TranslatedX * math.cos( math.rad( SpawnAngle ) )
- + TranslatedY * math.sin( math.rad( SpawnAngle ) )
- local RotatedY = TranslatedX * math.sin( math.rad( SpawnAngle ) )
- + TranslatedY * math.cos( math.rad( SpawnAngle ) )
-
- -- Assign
- self.SpawnGroups[SpawnIndex].SpawnTemplate.x = SpawnRootX - RotatedX
- self.SpawnGroups[SpawnIndex].SpawnTemplate.y = SpawnRootY + RotatedY
-
-
- local SpawnUnitCount = table.getn( self.SpawnGroups[SpawnIndex].SpawnTemplate.units )
- for u = 1, SpawnUnitCount do
-
- -- Translate
- local TranslatedX = SpawnX
- local TranslatedY = SpawnY - 10 * ( u - 1 )
-
- -- Rotate
- local RotatedX = - TranslatedX * math.cos( math.rad( SpawnAngle ) )
- + TranslatedY * math.sin( math.rad( SpawnAngle ) )
- local RotatedY = TranslatedX * math.sin( math.rad( SpawnAngle ) )
- + TranslatedY * math.cos( math.rad( SpawnAngle ) )
-
- -- Assign
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[u].x = SpawnRootX - RotatedX
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[u].y = SpawnRootY + RotatedY
- self.SpawnGroups[SpawnIndex].SpawnTemplate.units[u].heading = self.SpawnGroups[SpawnIndex].SpawnTemplate.units[u].heading + math.rad( SpawnAngle )
- end
-
- return self
-end
-
---- Get the next index of the groups to be spawned. This method is complicated, as it is used at several spaces.
-function SPAWN:_GetSpawnIndex( SpawnIndex )
- self:F2( { self.SpawnTemplatePrefix, SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive, self.AliveUnits, #self.SpawnTemplate.units } )
-
- if ( self.SpawnMaxGroups == 0 ) or ( SpawnIndex <= self.SpawnMaxGroups ) then
- if ( self.SpawnMaxUnitsAlive == 0 ) or ( self.AliveUnits + #self.SpawnTemplate.units <= self.SpawnMaxUnitsAlive ) or self.UnControlled == true then
- if SpawnIndex and SpawnIndex >= self.SpawnCount + 1 then
- self.SpawnCount = self.SpawnCount + 1
- SpawnIndex = self.SpawnCount
- end
- self.SpawnIndex = SpawnIndex
- if not self.SpawnGroups[self.SpawnIndex] then
- self:_InitializeSpawnGroups( self.SpawnIndex )
- end
- else
- return nil
- end
- else
- return nil
- end
-
- return self.SpawnIndex
-end
-
-
--- TODO Need to delete this... _DATABASE does this now ...
-
---- @param #SPAWN self
--- @param Core.Event#EVENTDATA EventData
-function SPAWN:_OnBirth( EventData )
- self:F( self.SpawnTemplatePrefix )
-
- local SpawnGroup = EventData.IniGroup
-
- if SpawnGroup then
- local EventPrefix = self:_GetPrefixFromGroup( SpawnGroup )
- self:T( { "Birth Event:", EventPrefix, self.SpawnTemplatePrefix } )
- if EventPrefix == self.SpawnTemplatePrefix or ( self.SpawnAliasPrefix and EventPrefix == self.SpawnAliasPrefix ) then
- self.AliveUnits = self.AliveUnits + 1
- self:T( "Alive Units: " .. self.AliveUnits )
- end
- end
-
-end
-
---- Obscolete
--- @todo Need to delete this... _DATABASE does this now ...
-
---- @param #SPAWN self
--- @param Core.Event#EVENTDATA EventData
-function SPAWN:_OnDeadOrCrash( EventData )
- self:F( self.SpawnTemplatePrefix )
-
- local SpawnGroup = EventData.IniGroup
-
- if SpawnGroup then
- local EventPrefix = self:_GetPrefixFromGroup( SpawnGroup )
- self:T( { "Dead event: " .. EventPrefix } )
- if EventPrefix == self.SpawnTemplatePrefix or ( self.SpawnAliasPrefix and EventPrefix == self.SpawnAliasPrefix ) then
- self.AliveUnits = self.AliveUnits - 1
- self:T( "Alive Units: " .. self.AliveUnits )
- end
- end
-end
-
---- Will detect AIR Units taking off... When the event takes place, the spawned Group is registered as airborne...
--- This is needed to ensure that Re-SPAWNing only is done for landed AIR Groups.
--- @param #SPAWN self
--- @param Core.Event#EVENTDATA EventData
-function SPAWN:_OnTakeOff( EventData )
- self:F( self.SpawnTemplatePrefix )
-
- local SpawnGroup = EventData.IniGroup
- if SpawnGroup then
- local EventPrefix = self:_GetPrefixFromGroup( SpawnGroup )
- self:T( { "TakeOff event: " .. EventPrefix } )
- if EventPrefix == self.SpawnTemplatePrefix or ( self.SpawnAliasPrefix and EventPrefix == self.SpawnAliasPrefix ) then
- self:T( "self.Landed = false" )
- SpawnGroup:SetState( SpawnGroup, "Spawn_Landed", false )
- end
- end
-end
-
---- Will detect AIR Units landing... When the event takes place, the spawned Group is registered as landed.
--- This is needed to ensure that Re-SPAWNing is only done for landed AIR Groups.
--- @param #SPAWN self
--- @param Core.Event#EVENTDATA EventData
-function SPAWN:_OnLand( EventData )
- self:F( self.SpawnTemplatePrefix )
-
- local SpawnGroup = EventData.IniGroup
- if SpawnGroup then
- local EventPrefix = self:_GetPrefixFromGroup( SpawnGroup )
- self:T( { "Land event: " .. EventPrefix } )
- if EventPrefix == self.SpawnTemplatePrefix or ( self.SpawnAliasPrefix and EventPrefix == self.SpawnAliasPrefix ) then
- -- TODO: Check if this is the last unit of the group that lands.
- SpawnGroup:SetState( SpawnGroup, "Spawn_Landed", true )
- if self.RepeatOnLanding then
- local SpawnGroupIndex = self:GetSpawnIndexFromGroup( SpawnGroup )
- self:T( { "Landed:", "ReSpawn:", SpawnGroup:GetName(), SpawnGroupIndex } )
- self:ReSpawn( SpawnGroupIndex )
- end
- end
- end
-end
-
---- Will detect AIR Units shutting down their engines ...
--- When the event takes place, and the method @{RepeatOnEngineShutDown} was called, the spawned Group will Re-SPAWN.
--- But only when the Unit was registered to have landed.
--- @param #SPAWN self
--- @param Core.Event#EVENTDATA EventData
-function SPAWN:_OnEngineShutDown( EventData )
- self:F( self.SpawnTemplatePrefix )
-
- local SpawnGroup = EventData.IniGroup
- if SpawnGroup then
- local EventPrefix = self:_GetPrefixFromGroup( SpawnGroup )
- self:T( { "EngineShutdown event: " .. EventPrefix } )
- if EventPrefix == self.SpawnTemplatePrefix or ( self.SpawnAliasPrefix and EventPrefix == self.SpawnAliasPrefix ) then
- -- todo: test if on the runway
- local Landed = SpawnGroup:GetState( SpawnGroup, "Spawn_Landed" )
- if Landed and self.RepeatOnEngineShutDown then
- local SpawnGroupIndex = self:GetSpawnIndexFromGroup( SpawnGroup )
- self:T( { "EngineShutDown: ", "ReSpawn:", SpawnGroup:GetName(), SpawnGroupIndex } )
- self:ReSpawn( SpawnGroupIndex )
- end
- end
- end
-end
-
---- This function is called automatically by the Spawning scheduler.
--- It is the internal worker method SPAWNing new Groups on the defined time intervals.
-function SPAWN:_Scheduler()
- self:F2( { "_Scheduler", self.SpawnTemplatePrefix, self.SpawnAliasPrefix, self.SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive } )
-
- -- Validate if there are still groups left in the batch...
- self:Spawn()
-
- return true
-end
-
---- Schedules the CleanUp of Groups
--- @param #SPAWN self
--- @return #boolean True = Continue Scheduler
-function SPAWN:_SpawnCleanUpScheduler()
- self:F( { "CleanUp Scheduler:", self.SpawnTemplatePrefix } )
-
- local SpawnGroup, SpawnCursor = self:GetFirstAliveGroup()
- self:T( { "CleanUp Scheduler:", SpawnGroup, SpawnCursor } )
-
- while SpawnGroup do
-
- local SpawnUnits = SpawnGroup:GetUnits()
-
- for UnitID, UnitData in pairs( SpawnUnits ) do
-
- local SpawnUnit = UnitData -- Wrapper.Unit#UNIT
- local SpawnUnitName = SpawnUnit:GetName()
-
-
- self.SpawnCleanUpTimeStamps[SpawnUnitName] = self.SpawnCleanUpTimeStamps[SpawnUnitName] or {}
- local Stamp = self.SpawnCleanUpTimeStamps[SpawnUnitName]
- self:T( { SpawnUnitName, Stamp } )
-
- if Stamp.Vec2 then
- if SpawnUnit:InAir() == false and SpawnUnit:GetVelocityKMH() < 1 then
- local NewVec2 = SpawnUnit:GetVec2()
- if Stamp.Vec2.x == NewVec2.x and Stamp.Vec2.y == NewVec2.y then
- -- If the plane is not moving, and is on the ground, assign it with a timestamp...
- if Stamp.Time + self.SpawnCleanUpInterval < timer.getTime() then
- self:T( { "CleanUp Scheduler:", "ReSpawning:", SpawnGroup:GetName() } )
- self:ReSpawn( SpawnCursor )
- Stamp.Vec2 = nil
- Stamp.Time = nil
- end
- else
- Stamp.Time = timer.getTime()
- Stamp.Vec2 = SpawnUnit:GetVec2()
- end
- else
- Stamp.Vec2 = nil
- Stamp.Time = nil
- end
- else
- if SpawnUnit:InAir() == false then
- Stamp.Vec2 = SpawnUnit:GetVec2()
- if SpawnUnit:GetVelocityKMH() < 1 then
- Stamp.Time = timer.getTime()
- end
- else
- Stamp.Time = nil
- Stamp.Vec2 = nil
- end
- end
- end
-
- SpawnGroup, SpawnCursor = self:GetNextAliveGroup( SpawnCursor )
-
- self:T( { "CleanUp Scheduler:", SpawnGroup, SpawnCursor } )
-
- end
-
- return true -- Repeat
-
-end
---- Limit the simultaneous movement of Groups within a running Mission.
--- This module is defined to improve the performance in missions, and to bring additional realism for GROUND vehicles.
--- Performance: If in a DCSRTE there are a lot of moving GROUND units, then in a multi player mission, this WILL create lag if
--- the main DCS execution core of your CPU is fully utilized. So, this class will limit the amount of simultaneous moving GROUND units
--- on defined intervals (currently every minute).
--- @module Movement
-
---- the MOVEMENT class
--- @type MOVEMENT
--- @extends Core.Base#BASE
-MOVEMENT = {
- ClassName = "MOVEMENT",
-}
-
---- Creates the main object which is handling the GROUND forces movement.
--- @param table{string,...}|string MovePrefixes is a table of the Prefixes (names) of the GROUND Groups that need to be controlled by the MOVEMENT Object.
--- @param number MoveMaximum is a number that defines the maximum amount of GROUND Units to be moving during one minute.
--- @return MOVEMENT
--- @usage
--- -- Limit the amount of simultaneous moving units on the ground to prevent lag.
--- Movement_US_Platoons = MOVEMENT:New( { 'US Tank Platoon Left', 'US Tank Platoon Middle', 'US Tank Platoon Right', 'US CH-47D Troops' }, 15 )
-
-function MOVEMENT:New( MovePrefixes, MoveMaximum )
- local self = BASE:Inherit( self, BASE:New() ) -- #MOVEMENT
- self:F( { MovePrefixes, MoveMaximum } )
-
- if type( MovePrefixes ) == 'table' then
- self.MovePrefixes = MovePrefixes
- else
- self.MovePrefixes = { MovePrefixes }
- end
- self.MoveCount = 0 -- The internal counter of the amount of Moveing the has happened since MoveStart.
- self.MoveMaximum = MoveMaximum -- Contains the Maximum amount of units that are allowed to move...
- self.AliveUnits = 0 -- Contains the counter how many units are currently alive
- self.MoveUnits = {} -- Reflects if the Moving for this MovePrefixes is going to be scheduled or not.
-
- self:HandleEvent( EVENTS.Birth )
-
--- self:AddEvent( world.event.S_EVENT_BIRTH, self.OnBirth )
---
--- self:EnableEvents()
-
- self:ScheduleStart()
-
- return self
-end
-
---- Call this function to start the MOVEMENT scheduling.
-function MOVEMENT:ScheduleStart()
- self:F()
- --self.MoveFunction = routines.scheduleFunction( self._Scheduler, { self }, timer.getTime() + 1, 120 )
- self.MoveFunction = SCHEDULER:New( self, self._Scheduler, {}, 1, 120 )
-end
-
---- Call this function to stop the MOVEMENT scheduling.
--- @todo need to implement it ... Forgot.
-function MOVEMENT:ScheduleStop()
- self:F()
-
-end
-
---- Captures the birth events when new Units were spawned.
--- @todo This method should become obsolete. The new @{DATABASE} class will handle the collection administration.
--- @param #MOVEMENT self
--- @param Core.Event#EVENTDATA self
-function MOVEMENT:OnEventBirth( EventData )
- self:F( { EventData } )
-
- if timer.getTime0() < timer.getAbsTime() then -- dont need to add units spawned in at the start of the mission if mist is loaded in init line
- if EventData.IniDCSUnit then
- self:T( "Birth object : " .. EventData.IniDCSUnitName )
- if EventData.IniDCSGroup and EventData.IniDCSGroup:isExist() then
- for MovePrefixID, MovePrefix in pairs( self.MovePrefixes ) do
- if string.find( EventData.IniDCSUnitName, MovePrefix, 1, true ) then
- self.AliveUnits = self.AliveUnits + 1
- self.MoveUnits[EventData.IniDCSUnitName] = EventData.IniDCSGroupName
- self:T( self.AliveUnits )
- end
- end
- end
- end
-
- EventData.IniUnit:HandleEvent( EVENTS.DEAD, self.OnDeadOrCrash )
- end
-
-end
-
---- Captures the Dead or Crash events when Units crash or are destroyed.
--- @todo This method should become obsolete. The new @{DATABASE} class will handle the collection administration.
-function MOVEMENT:OnDeadOrCrash( Event )
- self:F( { Event } )
-
- if Event.IniDCSUnit then
- self:T( "Dead object : " .. Event.IniDCSUnitName )
- for MovePrefixID, MovePrefix in pairs( self.MovePrefixes ) do
- if string.find( Event.IniDCSUnitName, MovePrefix, 1, true ) then
- self.AliveUnits = self.AliveUnits - 1
- self.MoveUnits[Event.IniDCSUnitName] = nil
- self:T( self.AliveUnits )
- end
- end
- end
-end
-
---- This function is called automatically by the MOVEMENT scheduler. A new function is scheduled when MoveScheduled is true.
-function MOVEMENT:_Scheduler()
- self:F( { self.MovePrefixes, self.MoveMaximum, self.AliveUnits, self.MovementGroups } )
-
- if self.AliveUnits > 0 then
- local MoveProbability = ( self.MoveMaximum * 100 ) / self.AliveUnits
- self:T( 'Move Probability = ' .. MoveProbability )
-
- for MovementUnitName, MovementGroupName in pairs( self.MoveUnits ) do
- local MovementGroup = Group.getByName( MovementGroupName )
- if MovementGroup and MovementGroup:isExist() then
- local MoveOrStop = math.random( 1, 100 )
- self:T( 'MoveOrStop = ' .. MoveOrStop )
- if MoveOrStop <= MoveProbability then
- self:T( 'Group continues moving = ' .. MovementGroupName )
- trigger.action.groupContinueMoving( MovementGroup )
- else
- self:T( 'Group stops moving = ' .. MovementGroupName )
- trigger.action.groupStopMoving( MovementGroup )
- end
- else
- self.MoveUnits[MovementUnitName] = nil
- end
- end
- end
- return true
-end
---- Provides defensive behaviour to a set of SAM sites within a running Mission.
--- @module Sead
--- @author to be searched on the forum
--- @author (co) Flightcontrol (Modified and enriched with functionality)
-
---- The SEAD class
--- @type SEAD
--- @extends Core.Base#BASE
-SEAD = {
- ClassName = "SEAD",
- TargetSkill = {
- Average = { Evade = 50, DelayOff = { 10, 25 }, DelayOn = { 10, 30 } } ,
- Good = { Evade = 30, DelayOff = { 8, 20 }, DelayOn = { 20, 40 } } ,
- High = { Evade = 15, DelayOff = { 5, 17 }, DelayOn = { 30, 50 } } ,
- Excellent = { Evade = 10, DelayOff = { 3, 10 }, DelayOn = { 30, 60 } }
- },
- SEADGroupPrefixes = {}
-}
-
---- Creates the main object which is handling defensive actions for SA sites or moving SA vehicles.
--- When an anti radiation missile is fired (KH-58, KH-31P, KH-31A, KH-25MPU, HARM missiles), the SA will shut down their radars and will take evasive actions...
--- Chances are big that the missile will miss.
--- @param table{string,...}|string SEADGroupPrefixes which is a table of Prefixes of the SA Groups in the DCSRTE on which evasive actions need to be taken.
--- @return SEAD
--- @usage
--- -- CCCP SEAD Defenses
--- -- Defends the Russian SA installations from SEAD attacks.
--- SEAD_RU_SAM_Defenses = SEAD:New( { 'RU SA-6 Kub', 'RU SA-6 Defenses', 'RU MI-26 Troops', 'RU Attack Gori' } )
-function SEAD:New( SEADGroupPrefixes )
- local self = BASE:Inherit( self, BASE:New() )
- self:F( SEADGroupPrefixes )
- if type( SEADGroupPrefixes ) == 'table' then
- for SEADGroupPrefixID, SEADGroupPrefix in pairs( SEADGroupPrefixes ) do
- self.SEADGroupPrefixes[SEADGroupPrefix] = SEADGroupPrefix
- end
- else
- self.SEADGroupNames[SEADGroupPrefixes] = SEADGroupPrefixes
- end
-
- self:HandleEvent( EVENTS.Shot )
-
- return self
-end
-
---- Detects if an SA site was shot with an anti radiation missile. In this case, take evasive actions based on the skill level set within the ME.
--- @see SEAD
--- @param #SEAD
--- @param Core.Event#EVENTDATA EventData
-function SEAD:OnEventShot( EventData )
- self:F( { EventData } )
-
- local SEADUnit = EventData.IniDCSUnit
- local SEADUnitName = EventData.IniDCSUnitName
- local SEADWeapon = EventData.Weapon -- Identify the weapon fired
- local SEADWeaponName = EventData.WeaponName -- return weapon type
- -- Start of the 2nd loop
- self:T( "Missile Launched = " .. SEADWeaponName )
- if SEADWeaponName == "KH-58" or SEADWeaponName == "KH-25MPU" or SEADWeaponName == "AGM-88" or SEADWeaponName == "KH-31A" or SEADWeaponName == "KH-31P" then -- Check if the missile is a SEAD
- local _evade = math.random (1,100) -- random number for chance of evading action
- local _targetMim = EventData.Weapon:getTarget() -- Identify target
- local _targetMimname = Unit.getName(_targetMim)
- local _targetMimgroup = Unit.getGroup(Weapon.getTarget(SEADWeapon))
- local _targetMimgroupName = _targetMimgroup:getName()
- local _targetMimcont= _targetMimgroup:getController()
- local _targetskill = _DATABASE.Templates.Units[_targetMimname].Template.skill
- self:T( self.SEADGroupPrefixes )
- self:T( _targetMimgroupName )
- local SEADGroupFound = false
- for SEADGroupPrefixID, SEADGroupPrefix in pairs( self.SEADGroupPrefixes ) do
- if string.find( _targetMimgroupName, SEADGroupPrefix, 1, true ) then
- SEADGroupFound = true
- self:T( 'Group Found' )
- break
- end
- end
- if SEADGroupFound == true then
- if _targetskill == "Random" then -- when skill is random, choose a skill
- local Skills = { "Average", "Good", "High", "Excellent" }
- _targetskill = Skills[ math.random(1,4) ]
- end
- self:T( _targetskill )
- if self.TargetSkill[_targetskill] then
- if (_evade > self.TargetSkill[_targetskill].Evade) then
- self:T( string.format("Evading, target skill " ..string.format(_targetskill)) )
- local _targetMim = Weapon.getTarget(SEADWeapon)
- local _targetMimname = Unit.getName(_targetMim)
- local _targetMimgroup = Unit.getGroup(Weapon.getTarget(SEADWeapon))
- local _targetMimcont= _targetMimgroup:getController()
- routines.groupRandomDistSelf(_targetMimgroup,300,'Diamond',250,20) -- move randomly
- local SuppressedGroups1 = {} -- unit suppressed radar off for a random time
- local function SuppressionEnd1(id)
- id.ctrl:setOption(AI.Option.Ground.id.ALARM_STATE,AI.Option.Ground.val.ALARM_STATE.GREEN)
- SuppressedGroups1[id.groupName] = nil
- end
- local id = {
- groupName = _targetMimgroup,
- ctrl = _targetMimcont
- }
- local delay1 = math.random(self.TargetSkill[_targetskill].DelayOff[1], self.TargetSkill[_targetskill].DelayOff[2])
- if SuppressedGroups1[id.groupName] == nil then
- SuppressedGroups1[id.groupName] = {
- SuppressionEndTime1 = timer.getTime() + delay1,
- SuppressionEndN1 = SuppressionEndCounter1 --Store instance of SuppressionEnd() scheduled function
- }
- Controller.setOption(_targetMimcont, AI.Option.Ground.id.ALARM_STATE,AI.Option.Ground.val.ALARM_STATE.GREEN)
- timer.scheduleFunction(SuppressionEnd1, id, SuppressedGroups1[id.groupName].SuppressionEndTime1) --Schedule the SuppressionEnd() function
- --trigger.action.outText( string.format("Radar Off " ..string.format(delay1)), 20)
- end
-
- local SuppressedGroups = {}
- local function SuppressionEnd(id)
- id.ctrl:setOption(AI.Option.Ground.id.ALARM_STATE,AI.Option.Ground.val.ALARM_STATE.RED)
- SuppressedGroups[id.groupName] = nil
- end
- local id = {
- groupName = _targetMimgroup,
- ctrl = _targetMimcont
- }
- local delay = math.random(self.TargetSkill[_targetskill].DelayOn[1], self.TargetSkill[_targetskill].DelayOn[2])
- if SuppressedGroups[id.groupName] == nil then
- SuppressedGroups[id.groupName] = {
- SuppressionEndTime = timer.getTime() + delay,
- SuppressionEndN = SuppressionEndCounter --Store instance of SuppressionEnd() scheduled function
- }
- timer.scheduleFunction(SuppressionEnd, id, SuppressedGroups[id.groupName].SuppressionEndTime) --Schedule the SuppressionEnd() function
- --trigger.action.outText( string.format("Radar On " ..string.format(delay)), 20)
- end
- end
- end
- end
- end
-end
---- Taking the lead of AI escorting your flight.
---
--- @{#ESCORT} class
--- ================
--- The @{#ESCORT} class allows you to interact with escorting AI on your flight and take the lead.
--- Each escorting group can be commanded with a whole set of radio commands (radio menu in your flight, and then F10).
---
--- The radio commands will vary according the category of the group. The richest set of commands are with Helicopters and AirPlanes.
--- Ships and Ground troops will have a more limited set, but they can provide support through the bombing of targets designated by the other escorts.
---
--- RADIO MENUs that can be created:
--- ================================
--- Find a summary below of the current available commands:
---
--- Navigation ...:
--- ---------------
--- Escort group navigation functions:
---
--- * **"Join-Up and Follow at x meters":** The escort group fill follow you at about x meters, and they will follow you.
--- * **"Flare":** Provides menu commands to let the escort group shoot a flare in the air in a color.
--- * **"Smoke":** Provides menu commands to let the escort group smoke the air in a color. Note that smoking is only available for ground and naval troops.
---
--- Hold position ...:
--- ------------------
--- Escort group navigation functions:
---
--- * **"At current location":** Stops the escort group and they will hover 30 meters above the ground at the position they stopped.
--- * **"At client location":** Stops the escort group and they will hover 30 meters above the ground at the position they stopped.
---
--- Report targets ...:
--- -------------------
--- Report targets will make the escort group to report any target that it identifies within a 8km range. Any detected target can be attacked using the 4. Attack nearby targets function. (see below).
---
--- * **"Report now":** Will report the current detected targets.
--- * **"Report targets on":** Will make the escort group to report detected targets and will fill the "Attack nearby targets" menu list.
--- * **"Report targets off":** Will stop detecting targets.
---
--- Scan targets ...:
--- -----------------
--- Menu items to pop-up the escort group for target scanning. After scanning, the escort group will resume with the mission or defined task.
---
--- * **"Scan targets 30 seconds":** Scan 30 seconds for targets.
--- * **"Scan targets 60 seconds":** Scan 60 seconds for targets.
---
--- Attack targets ...:
--- -------------------
--- This menu item will list all detected targets within a 15km range. Depending on the level of detection (known/unknown) and visuality, the targets type will also be listed.
---
--- Request assistance from ...:
--- ----------------------------
--- This menu item will list all detected targets within a 15km range, as with the menu item **Attack Targets**.
--- This menu item allows to request attack support from other escorts supporting the current client group.
--- eg. the function allows a player to request support from the Ship escort to attack a target identified by the Plane escort with its Tomahawk missiles.
--- eg. the function allows a player to request support from other Planes escorting to bomb the unit with illumination missiles or bombs, so that the main plane escort can attack the area.
---
--- ROE ...:
--- --------
--- Sets the Rules of Engagement (ROE) of the escort group when in flight.
---
--- * **"Hold Fire":** The escort group will hold fire.
--- * **"Return Fire":** The escort group will return fire.
--- * **"Open Fire":** The escort group will open fire on designated targets.
--- * **"Weapon Free":** The escort group will engage with any target.
---
--- Evasion ...:
--- ------------
--- Will define the evasion techniques that the escort group will perform during flight or combat.
---
--- * **"Fight until death":** The escort group will have no reaction to threats.
--- * **"Use flares, chaff and jammers":** The escort group will use passive defense using flares and jammers. No evasive manoeuvres are executed.
--- * **"Evade enemy fire":** The rescort group will evade enemy fire before firing.
--- * **"Go below radar and evade fire":** The escort group will perform evasive vertical manoeuvres.
---
--- Resume Mission ...:
--- -------------------
--- Escort groups can have their own mission. This menu item will allow the escort group to resume their Mission from a given waypoint.
--- Note that this is really fantastic, as you now have the dynamic of taking control of the escort groups, and allowing them to resume their path or mission.
---
--- ESCORT construction methods.
--- ============================
--- Create a new SPAWN object with the @{#ESCORT.New} method:
---
--- * @{#ESCORT.New}: Creates a new ESCORT object from a @{Group#GROUP} for a @{Client#CLIENT}, with an optional briefing text.
---
--- ESCORT initialization methods.
--- ==============================
--- The following menus are created within the RADIO MENU (F10) of an active unit hosted by a player:
---
--- * @{#ESCORT.MenuFollowAt}: Creates a menu to make the escort follow the client.
--- * @{#ESCORT.MenuHoldAtEscortPosition}: Creates a menu to hold the escort at its current position.
--- * @{#ESCORT.MenuHoldAtLeaderPosition}: Creates a menu to hold the escort at the client position.
--- * @{#ESCORT.MenuScanForTargets}: Creates a menu so that the escort scans targets.
--- * @{#ESCORT.MenuFlare}: Creates a menu to disperse flares.
--- * @{#ESCORT.MenuSmoke}: Creates a menu to disparse smoke.
--- * @{#ESCORT.MenuReportTargets}: Creates a menu so that the escort reports targets.
--- * @{#ESCORT.MenuReportPosition}: Creates a menu so that the escort reports its current position from bullseye.
--- * @{#ESCORT.MenuAssistedAttack: Creates a menu so that the escort supportes assisted attack from other escorts with the client.
--- * @{#ESCORT.MenuROE: Creates a menu structure to set the rules of engagement of the escort.
--- * @{#ESCORT.MenuEvasion: Creates a menu structure to set the evasion techniques when the escort is under threat.
--- * @{#ESCORT.MenuResumeMission}: Creates a menu structure so that the escort can resume from a waypoint.
---
---
--- @usage
--- -- Declare a new EscortPlanes object as follows:
---
--- -- First find the GROUP object and the CLIENT object.
--- local EscortClient = CLIENT:FindByName( "Unit Name" ) -- The Unit Name is the name of the unit flagged with the skill Client in the mission editor.
--- local EscortGroup = GROUP:FindByName( "Group Name" ) -- The Group Name is the name of the group that will escort the Escort Client.
---
--- -- Now use these 2 objects to construct the new EscortPlanes object.
--- EscortPlanes = ESCORT:New( EscortClient, EscortGroup, "Desert", "Welcome to the mission. You are escorted by a plane with code name 'Desert', which can be instructed through the F10 radio menu." )
---
---
---
--- @module Escort
--- @author FlightControl
-
---- ESCORT class
--- @type ESCORT
--- @extends Core.Base#BASE
--- @field Wrapper.Client#CLIENT EscortClient
--- @field Wrapper.Group#GROUP EscortGroup
--- @field #string EscortName
--- @field #ESCORT.MODE EscortMode The mode the escort is in.
--- @field Core.Scheduler#SCHEDULER FollowScheduler The instance of the SCHEDULER class.
--- @field #number FollowDistance The current follow distance.
--- @field #boolean ReportTargets If true, nearby targets are reported.
--- @Field Dcs.DCSTypes#AI.Option.Air.val.ROE OptionROE Which ROE is set to the EscortGroup.
--- @field Dcs.DCSTypes#AI.Option.Air.val.REACTION_ON_THREAT OptionReactionOnThreat Which REACTION_ON_THREAT is set to the EscortGroup.
--- @field Core.Menu#MENU_CLIENT EscortMenuResumeMission
--- @field Functional.Detection#DETECTION_BASE Detection
-ESCORT = {
- ClassName = "ESCORT",
- EscortName = nil, -- The Escort Name
- EscortClient = nil,
- EscortGroup = nil,
- EscortMode = 1,
- MODE = {
- FOLLOW = 1,
- MISSION = 2,
- },
- Targets = {}, -- The identified targets
- FollowScheduler = nil,
- ReportTargets = true,
- OptionROE = AI.Option.Air.val.ROE.OPEN_FIRE,
- OptionReactionOnThreat = AI.Option.Air.val.REACTION_ON_THREAT.ALLOW_ABORT_MISSION,
- SmokeDirectionVector = false,
- TaskPoints = {}
-}
-
---- ESCORT.Mode class
--- @type ESCORT.MODE
--- @field #number FOLLOW
--- @field #number MISSION
-
---- MENUPARAM type
--- @type MENUPARAM
--- @field #ESCORT ParamSelf
--- @field #Distance ParamDistance
--- @field #function ParamFunction
--- @field #string ParamMessage
-
---- ESCORT class constructor for an AI group
--- @param #ESCORT self
--- @param Wrapper.Client#CLIENT EscortClient The client escorted by the EscortGroup.
--- @param Wrapper.Group#GROUP EscortGroup The group AI escorting the EscortClient.
--- @param #string EscortName Name of the escort.
--- @param #string EscortBriefing A text showing the ESCORT briefing to the player. Note that if no EscortBriefing is provided, the default briefing will be shown.
--- @return #ESCORT self
--- @usage
--- -- Declare a new EscortPlanes object as follows:
---
--- -- First find the GROUP object and the CLIENT object.
--- local EscortClient = CLIENT:FindByName( "Unit Name" ) -- The Unit Name is the name of the unit flagged with the skill Client in the mission editor.
--- local EscortGroup = GROUP:FindByName( "Group Name" ) -- The Group Name is the name of the group that will escort the Escort Client.
---
--- -- Now use these 2 objects to construct the new EscortPlanes object.
--- EscortPlanes = ESCORT:New( EscortClient, EscortGroup, "Desert", "Welcome to the mission. You are escorted by a plane with code name 'Desert', which can be instructed through the F10 radio menu." )
-function ESCORT:New( EscortClient, EscortGroup, EscortName, EscortBriefing )
-
- local self = BASE:Inherit( self, BASE:New() ) -- #ESCORT
- self:F( { EscortClient, EscortGroup, EscortName } )
-
- self.EscortClient = EscortClient -- Wrapper.Client#CLIENT
- self.EscortGroup = EscortGroup -- Wrapper.Group#GROUP
- self.EscortName = EscortName
- self.EscortBriefing = EscortBriefing
-
- self.EscortSetGroup = SET_GROUP:New()
- self.EscortSetGroup:AddObject( self.EscortGroup )
- self.EscortSetGroup:Flush()
- self.Detection = DETECTION_UNITS:New( self.EscortSetGroup, 15000 )
-
- self.EscortGroup.Detection = self.Detection
-
- -- Set EscortGroup known at EscortClient.
- if not self.EscortClient._EscortGroups then
- self.EscortClient._EscortGroups = {}
- end
-
- if not self.EscortClient._EscortGroups[EscortGroup:GetName()] then
- self.EscortClient._EscortGroups[EscortGroup:GetName()] = {}
- self.EscortClient._EscortGroups[EscortGroup:GetName()].EscortGroup = self.EscortGroup
- self.EscortClient._EscortGroups[EscortGroup:GetName()].EscortName = self.EscortName
- self.EscortClient._EscortGroups[EscortGroup:GetName()].Detection = self.EscortGroup.Detection
- end
-
- self.EscortMenu = MENU_CLIENT:New( self.EscortClient, self.EscortName )
-
- self.EscortGroup:WayPointInitialize(1)
-
- self.EscortGroup:OptionROTVertical()
- self.EscortGroup:OptionROEOpenFire()
-
- if not EscortBriefing then
- EscortGroup:MessageToClient( EscortGroup:GetCategoryName() .. " '" .. EscortName .. "' (" .. EscortGroup:GetCallsign() .. ") reporting! " ..
- "We're escorting your flight. " ..
- "Use the Radio Menu and F10 and use the options under + " .. EscortName .. "\n",
- 60, EscortClient
- )
- else
- EscortGroup:MessageToClient( EscortGroup:GetCategoryName() .. " '" .. EscortName .. "' (" .. EscortGroup:GetCallsign() .. ") " .. EscortBriefing,
- 60, EscortClient
- )
- end
-
- self.FollowDistance = 100
- self.CT1 = 0
- self.GT1 = 0
-
- self.FollowScheduler, self.FollowSchedule = SCHEDULER:New( self, self._FollowScheduler, {}, 1, .5, .01 )
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- self.EscortMode = ESCORT.MODE.MISSION
-
-
- return self
-end
-
---- Set a Detection method for the EscortClient to be reported upon.
--- Detection methods are based on the derived classes from DETECTION_BASE.
--- @param #ESCORT self
--- @param Function.Detection#DETECTION_BASE Detection
-function ESCORT:SetDetection( Detection )
-
- self.Detection = Detection
- self.EscortGroup.Detection = self.Detection
- self.EscortClient._EscortGroups[self.EscortGroup:GetName()].Detection = self.EscortGroup.Detection
-
- Detection:__Start( 1 )
-
-end
-
---- This function is for test, it will put on the frequency of the FollowScheduler a red smoke at the direction vector calculated for the escort to fly to.
--- This allows to visualize where the escort is flying to.
--- @param #ESCORT self
--- @param #boolean SmokeDirection If true, then the direction vector will be smoked.
-function ESCORT:TestSmokeDirectionVector( SmokeDirection )
- self.SmokeDirectionVector = ( SmokeDirection == true ) and true or false
-end
-
-
---- Defines the default menus
--- @param #ESCORT self
--- @return #ESCORT
-function ESCORT:Menus()
- self:F()
-
- self:MenuFollowAt( 100 )
- self:MenuFollowAt( 200 )
- self:MenuFollowAt( 300 )
- self:MenuFollowAt( 400 )
-
- self:MenuScanForTargets( 100, 60 )
-
- self:MenuHoldAtEscortPosition( 30 )
- self:MenuHoldAtLeaderPosition( 30 )
-
- self:MenuFlare()
- self:MenuSmoke()
-
- self:MenuReportTargets( 60 )
- self:MenuAssistedAttack()
- self:MenuROE()
- self:MenuEvasion()
- self:MenuResumeMission()
-
-
- return self
-end
-
-
-
---- Defines a menu slot to let the escort Join and Follow you at a certain distance.
--- This menu will appear under **Navigation**.
--- @param #ESCORT self
--- @param Dcs.DCSTypes#Distance Distance The distance in meters that the escort needs to follow the client.
--- @return #ESCORT
-function ESCORT:MenuFollowAt( Distance )
- self:F(Distance)
-
- if self.EscortGroup:IsAir() then
- if not self.EscortMenuReportNavigation then
- self.EscortMenuReportNavigation = MENU_CLIENT:New( self.EscortClient, "Navigation", self.EscortMenu )
- end
-
- if not self.EscortMenuJoinUpAndFollow then
- self.EscortMenuJoinUpAndFollow = {}
- end
-
- self.EscortMenuJoinUpAndFollow[#self.EscortMenuJoinUpAndFollow+1] = MENU_CLIENT_COMMAND:New( self.EscortClient, "Join-Up and Follow at " .. Distance, self.EscortMenuReportNavigation, ESCORT._JoinUpAndFollow, self, Distance )
-
- self.EscortMode = ESCORT.MODE.FOLLOW
- end
-
- return self
-end
-
---- Defines a menu slot to let the escort hold at their current position and stay low with a specified height during a specified time in seconds.
--- This menu will appear under **Hold position**.
--- @param #ESCORT self
--- @param Dcs.DCSTypes#Distance Height Optional parameter that sets the height in meters to let the escort orbit at the current location. The default value is 30 meters.
--- @param Dcs.DCSTypes#Time Seconds Optional parameter that lets the escort orbit at the current position for a specified time. (not implemented yet). The default value is 0 seconds, meaning, that the escort will orbit forever until a sequent command is given.
--- @param #string MenuTextFormat Optional parameter that shows the menu option text. The text string is formatted, and should contain two %d tokens in the string. The first for the Height, the second for the Time (if given). If no text is given, the default text will be displayed.
--- @return #ESCORT
--- TODO: Implement Seconds parameter. Challenge is to first develop the "continue from last activity" function.
-function ESCORT:MenuHoldAtEscortPosition( Height, Seconds, MenuTextFormat )
- self:F( { Height, Seconds, MenuTextFormat } )
-
- if self.EscortGroup:IsAir() then
-
- if not self.EscortMenuHold then
- self.EscortMenuHold = MENU_CLIENT:New( self.EscortClient, "Hold position", self.EscortMenu )
- end
-
- if not Height then
- Height = 30
- end
-
- if not Seconds then
- Seconds = 0
- end
-
- local MenuText = ""
- if not MenuTextFormat then
- if Seconds == 0 then
- MenuText = string.format( "Hold at %d meter", Height )
- else
- MenuText = string.format( "Hold at %d meter for %d seconds", Height, Seconds )
- end
- else
- if Seconds == 0 then
- MenuText = string.format( MenuTextFormat, Height )
- else
- MenuText = string.format( MenuTextFormat, Height, Seconds )
- end
- end
-
- if not self.EscortMenuHoldPosition then
- self.EscortMenuHoldPosition = {}
- end
-
- self.EscortMenuHoldPosition[#self.EscortMenuHoldPosition+1] = MENU_CLIENT_COMMAND
- :New(
- self.EscortClient,
- MenuText,
- self.EscortMenuHold,
- ESCORT._HoldPosition,
- self,
- self.EscortGroup,
- Height,
- Seconds
- )
- end
-
- return self
-end
-
-
---- Defines a menu slot to let the escort hold at the client position and stay low with a specified height during a specified time in seconds.
--- This menu will appear under **Navigation**.
--- @param #ESCORT self
--- @param Dcs.DCSTypes#Distance Height Optional parameter that sets the height in meters to let the escort orbit at the current location. The default value is 30 meters.
--- @param Dcs.DCSTypes#Time Seconds Optional parameter that lets the escort orbit at the current position for a specified time. (not implemented yet). The default value is 0 seconds, meaning, that the escort will orbit forever until a sequent command is given.
--- @param #string MenuTextFormat Optional parameter that shows the menu option text. The text string is formatted, and should contain one or two %d tokens in the string. The first for the Height, the second for the Time (if given). If no text is given, the default text will be displayed.
--- @return #ESCORT
--- TODO: Implement Seconds parameter. Challenge is to first develop the "continue from last activity" function.
-function ESCORT:MenuHoldAtLeaderPosition( Height, Seconds, MenuTextFormat )
- self:F( { Height, Seconds, MenuTextFormat } )
-
- if self.EscortGroup:IsAir() then
-
- if not self.EscortMenuHold then
- self.EscortMenuHold = MENU_CLIENT:New( self.EscortClient, "Hold position", self.EscortMenu )
- end
-
- if not Height then
- Height = 30
- end
-
- if not Seconds then
- Seconds = 0
- end
-
- local MenuText = ""
- if not MenuTextFormat then
- if Seconds == 0 then
- MenuText = string.format( "Rejoin and hold at %d meter", Height )
- else
- MenuText = string.format( "Rejoin and hold at %d meter for %d seconds", Height, Seconds )
- end
- else
- if Seconds == 0 then
- MenuText = string.format( MenuTextFormat, Height )
- else
- MenuText = string.format( MenuTextFormat, Height, Seconds )
- end
- end
-
- if not self.EscortMenuHoldAtLeaderPosition then
- self.EscortMenuHoldAtLeaderPosition = {}
- end
-
- self.EscortMenuHoldAtLeaderPosition[#self.EscortMenuHoldAtLeaderPosition+1] = MENU_CLIENT_COMMAND
- :New(
- self.EscortClient,
- MenuText,
- self.EscortMenuHold,
- ESCORT._HoldPosition,
- { ParamSelf = self,
- ParamOrbitGroup = self.EscortClient,
- ParamHeight = Height,
- ParamSeconds = Seconds
- }
- )
- end
-
- return self
-end
-
---- Defines a menu slot to let the escort scan for targets at a certain height for a certain time in seconds.
--- This menu will appear under **Scan targets**.
--- @param #ESCORT self
--- @param Dcs.DCSTypes#Distance Height Optional parameter that sets the height in meters to let the escort orbit at the current location. The default value is 30 meters.
--- @param Dcs.DCSTypes#Time Seconds Optional parameter that lets the escort orbit at the current position for a specified time. (not implemented yet). The default value is 0 seconds, meaning, that the escort will orbit forever until a sequent command is given.
--- @param #string MenuTextFormat Optional parameter that shows the menu option text. The text string is formatted, and should contain one or two %d tokens in the string. The first for the Height, the second for the Time (if given). If no text is given, the default text will be displayed.
--- @return #ESCORT
-function ESCORT:MenuScanForTargets( Height, Seconds, MenuTextFormat )
- self:F( { Height, Seconds, MenuTextFormat } )
-
- if self.EscortGroup:IsAir() then
- if not self.EscortMenuScan then
- self.EscortMenuScan = MENU_CLIENT:New( self.EscortClient, "Scan for targets", self.EscortMenu )
- end
-
- if not Height then
- Height = 100
- end
-
- if not Seconds then
- Seconds = 30
- end
-
- local MenuText = ""
- if not MenuTextFormat then
- if Seconds == 0 then
- MenuText = string.format( "At %d meter", Height )
- else
- MenuText = string.format( "At %d meter for %d seconds", Height, Seconds )
- end
- else
- if Seconds == 0 then
- MenuText = string.format( MenuTextFormat, Height )
- else
- MenuText = string.format( MenuTextFormat, Height, Seconds )
- end
- end
-
- if not self.EscortMenuScanForTargets then
- self.EscortMenuScanForTargets = {}
- end
-
- self.EscortMenuScanForTargets[#self.EscortMenuScanForTargets+1] = MENU_CLIENT_COMMAND
- :New(
- self.EscortClient,
- MenuText,
- self.EscortMenuScan,
- ESCORT._ScanTargets,
- self,
- 30
- )
- end
-
- return self
-end
-
-
-
---- Defines a menu slot to let the escort disperse a flare in a certain color.
--- This menu will appear under **Navigation**.
--- The flare will be fired from the first unit in the group.
--- @param #ESCORT self
--- @param #string MenuTextFormat Optional parameter that shows the menu option text. If no text is given, the default text will be displayed.
--- @return #ESCORT
-function ESCORT:MenuFlare( MenuTextFormat )
- self:F()
-
- if not self.EscortMenuReportNavigation then
- self.EscortMenuReportNavigation = MENU_CLIENT:New( self.EscortClient, "Navigation", self.EscortMenu )
- end
-
- local MenuText = ""
- if not MenuTextFormat then
- MenuText = "Flare"
- else
- MenuText = MenuTextFormat
- end
-
- if not self.EscortMenuFlare then
- self.EscortMenuFlare = MENU_CLIENT:New( self.EscortClient, MenuText, self.EscortMenuReportNavigation, ESCORT._Flare, self )
- self.EscortMenuFlareGreen = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release green flare", self.EscortMenuFlare, ESCORT._Flare, self, FLARECOLOR.Green, "Released a green flare!" )
- self.EscortMenuFlareRed = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release red flare", self.EscortMenuFlare, ESCORT._Flare, self, FLARECOLOR.Red, "Released a red flare!" )
- self.EscortMenuFlareWhite = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release white flare", self.EscortMenuFlare, ESCORT._Flare, self, FLARECOLOR.White, "Released a white flare!" )
- self.EscortMenuFlareYellow = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release yellow flare", self.EscortMenuFlare, ESCORT._Flare, self, FLARECOLOR.Yellow, "Released a yellow flare!" )
- end
-
- return self
-end
-
---- Defines a menu slot to let the escort disperse a smoke in a certain color.
--- This menu will appear under **Navigation**.
--- Note that smoke menu options will only be displayed for ships and ground units. Not for air units.
--- The smoke will be fired from the first unit in the group.
--- @param #ESCORT self
--- @param #string MenuTextFormat Optional parameter that shows the menu option text. If no text is given, the default text will be displayed.
--- @return #ESCORT
-function ESCORT:MenuSmoke( MenuTextFormat )
- self:F()
-
- if not self.EscortGroup:IsAir() then
- if not self.EscortMenuReportNavigation then
- self.EscortMenuReportNavigation = MENU_CLIENT:New( self.EscortClient, "Navigation", self.EscortMenu )
- end
-
- local MenuText = ""
- if not MenuTextFormat then
- MenuText = "Smoke"
- else
- MenuText = MenuTextFormat
- end
-
- if not self.EscortMenuSmoke then
- self.EscortMenuSmoke = MENU_CLIENT:New( self.EscortClient, "Smoke", self.EscortMenuReportNavigation, ESCORT._Smoke, self )
- self.EscortMenuSmokeGreen = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release green smoke", self.EscortMenuSmoke, ESCORT._Smoke, self, SMOKECOLOR.Green, "Releasing green smoke!" )
- self.EscortMenuSmokeRed = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release red smoke", self.EscortMenuSmoke, ESCORT._Smoke, self, SMOKECOLOR.Red, "Releasing red smoke!" )
- self.EscortMenuSmokeWhite = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release white smoke", self.EscortMenuSmoke, ESCORT._Smoke, self, SMOKECOLOR.White, "Releasing white smoke!" )
- self.EscortMenuSmokeOrange = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release orange smoke", self.EscortMenuSmoke, ESCORT._Smoke, self, SMOKECOLOR.Orange, "Releasing orange smoke!" )
- self.EscortMenuSmokeBlue = MENU_CLIENT_COMMAND:New( self.EscortClient, "Release blue smoke", self.EscortMenuSmoke, ESCORT._Smoke, self, SMOKECOLOR.Blue, "Releasing blue smoke!" )
- end
- end
-
- return self
-end
-
---- Defines a menu slot to let the escort report their current detected targets with a specified time interval in seconds.
--- This menu will appear under **Report targets**.
--- Note that if a report targets menu is not specified, no targets will be detected by the escort, and the attack and assisted attack menus will not be displayed.
--- @param #ESCORT self
--- @param Dcs.DCSTypes#Time Seconds Optional parameter that lets the escort report their current detected targets after specified time interval in seconds. The default time is 30 seconds.
--- @return #ESCORT
-function ESCORT:MenuReportTargets( Seconds )
- self:F( { Seconds } )
-
- if not self.EscortMenuReportNearbyTargets then
- self.EscortMenuReportNearbyTargets = MENU_CLIENT:New( self.EscortClient, "Report targets", self.EscortMenu )
- end
-
- if not Seconds then
- Seconds = 30
- end
-
- -- Report Targets
- self.EscortMenuReportNearbyTargetsNow = MENU_CLIENT_COMMAND:New( self.EscortClient, "Report targets now!", self.EscortMenuReportNearbyTargets, ESCORT._ReportNearbyTargetsNow, self )
- self.EscortMenuReportNearbyTargetsOn = MENU_CLIENT_COMMAND:New( self.EscortClient, "Report targets on", self.EscortMenuReportNearbyTargets, ESCORT._SwitchReportNearbyTargets, self, true )
- self.EscortMenuReportNearbyTargetsOff = MENU_CLIENT_COMMAND:New( self.EscortClient, "Report targets off", self.EscortMenuReportNearbyTargets, ESCORT._SwitchReportNearbyTargets, self, false )
-
- -- Attack Targets
- self.EscortMenuAttackNearbyTargets = MENU_CLIENT:New( self.EscortClient, "Attack targets", self.EscortMenu )
-
-
- self.ReportTargetsScheduler = SCHEDULER:New( self, self._ReportTargetsScheduler, {}, 1, Seconds )
-
- return self
-end
-
---- Defines a menu slot to let the escort attack its detected targets using assisted attack from another escort joined also with the client.
--- This menu will appear under **Request assistance from**.
--- Note that this method needs to be preceded with the method MenuReportTargets.
--- @param #ESCORT self
--- @return #ESCORT
-function ESCORT:MenuAssistedAttack()
- self:F()
-
- -- Request assistance from other escorts.
- -- This is very useful to let f.e. an escorting ship attack a target detected by an escorting plane...
- self.EscortMenuTargetAssistance = MENU_CLIENT:New( self.EscortClient, "Request assistance from", self.EscortMenu )
-
- return self
-end
-
---- Defines a menu to let the escort set its rules of engagement.
--- All rules of engagement will appear under the menu **ROE**.
--- @param #ESCORT self
--- @return #ESCORT
-function ESCORT:MenuROE( MenuTextFormat )
- self:F( MenuTextFormat )
-
- if not self.EscortMenuROE then
- -- Rules of Engagement
- self.EscortMenuROE = MENU_CLIENT:New( self.EscortClient, "ROE", self.EscortMenu )
- if self.EscortGroup:OptionROEHoldFirePossible() then
- self.EscortMenuROEHoldFire = MENU_CLIENT_COMMAND:New( self.EscortClient, "Hold Fire", self.EscortMenuROE, ESCORT._ROE, self, self.EscortGroup:OptionROEHoldFire(), "Holding weapons!" )
- end
- if self.EscortGroup:OptionROEReturnFirePossible() then
- self.EscortMenuROEReturnFire = MENU_CLIENT_COMMAND:New( self.EscortClient, "Return Fire", self.EscortMenuROE, ESCORT._ROE, self, self.EscortGroup:OptionROEReturnFire(), "Returning fire!" )
- end
- if self.EscortGroup:OptionROEOpenFirePossible() then
- self.EscortMenuROEOpenFire = MENU_CLIENT_COMMAND:New( self.EscortClient, "Open Fire", self.EscortMenuROE, ESCORT._ROE, self, self.EscortGroup:OptionROEOpenFire(), "Opening fire on designated targets!!" )
- end
- if self.EscortGroup:OptionROEWeaponFreePossible() then
- self.EscortMenuROEWeaponFree = MENU_CLIENT_COMMAND:New( self.EscortClient, "Weapon Free", self.EscortMenuROE, ESCORT._ROE, self, self.EscortGroup:OptionROEWeaponFree(), "Opening fire on targets of opportunity!" )
- end
- end
-
- return self
-end
-
-
---- Defines a menu to let the escort set its evasion when under threat.
--- All rules of engagement will appear under the menu **Evasion**.
--- @param #ESCORT self
--- @return #ESCORT
-function ESCORT:MenuEvasion( MenuTextFormat )
- self:F( MenuTextFormat )
-
- if self.EscortGroup:IsAir() then
- if not self.EscortMenuEvasion then
- -- Reaction to Threats
- self.EscortMenuEvasion = MENU_CLIENT:New( self.EscortClient, "Evasion", self.EscortMenu )
- if self.EscortGroup:OptionROTNoReactionPossible() then
- self.EscortMenuEvasionNoReaction = MENU_CLIENT_COMMAND:New( self.EscortClient, "Fight until death", self.EscortMenuEvasion, ESCORT._ROT, self, self.EscortGroup:OptionROTNoReaction(), "Fighting until death!" )
- end
- if self.EscortGroup:OptionROTPassiveDefensePossible() then
- self.EscortMenuEvasionPassiveDefense = MENU_CLIENT_COMMAND:New( self.EscortClient, "Use flares, chaff and jammers", self.EscortMenuEvasion, ESCORT._ROT, self, self.EscortGroup:OptionROTPassiveDefense(), "Defending using jammers, chaff and flares!" )
- end
- if self.EscortGroup:OptionROTEvadeFirePossible() then
- self.EscortMenuEvasionEvadeFire = MENU_CLIENT_COMMAND:New( self.EscortClient, "Evade enemy fire", self.EscortMenuEvasion, ESCORT._ROT, self, self.EscortGroup:OptionROTEvadeFire(), "Evading on enemy fire!" )
- end
- if self.EscortGroup:OptionROTVerticalPossible() then
- self.EscortMenuOptionEvasionVertical = MENU_CLIENT_COMMAND:New( self.EscortClient, "Go below radar and evade fire", self.EscortMenuEvasion, ESCORT._ROT, self, self.EscortGroup:OptionROTVertical(), "Evading on enemy fire with vertical manoeuvres!" )
- end
- end
- end
-
- return self
-end
-
---- Defines a menu to let the escort resume its mission from a waypoint on its route.
--- All rules of engagement will appear under the menu **Resume mission from**.
--- @param #ESCORT self
--- @return #ESCORT
-function ESCORT:MenuResumeMission()
- self:F()
-
- if not self.EscortMenuResumeMission then
- -- Mission Resume Menu Root
- self.EscortMenuResumeMission = MENU_CLIENT:New( self.EscortClient, "Resume mission from", self.EscortMenu )
- end
-
- return self
-end
-
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_HoldPosition( OrbitGroup, OrbitHeight, OrbitSeconds )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- local OrbitUnit = OrbitGroup:GetUnit(1) -- Wrapper.Unit#UNIT
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- local PointFrom = {}
- local GroupVec3 = EscortGroup:GetUnit(1):GetVec3()
- PointFrom = {}
- PointFrom.x = GroupVec3.x
- PointFrom.y = GroupVec3.z
- PointFrom.speed = 250
- PointFrom.type = AI.Task.WaypointType.TURNING_POINT
- PointFrom.alt = GroupVec3.y
- PointFrom.alt_type = AI.Task.AltitudeType.BARO
-
- local OrbitPoint = OrbitUnit:GetVec2()
- local PointTo = {}
- PointTo.x = OrbitPoint.x
- PointTo.y = OrbitPoint.y
- PointTo.speed = 250
- PointTo.type = AI.Task.WaypointType.TURNING_POINT
- PointTo.alt = OrbitHeight
- PointTo.alt_type = AI.Task.AltitudeType.BARO
- PointTo.task = EscortGroup:TaskOrbitCircleAtVec2( OrbitPoint, OrbitHeight, 0 )
-
- local Points = { PointFrom, PointTo }
-
- EscortGroup:OptionROEHoldFire()
- EscortGroup:OptionROTPassiveDefense()
-
- EscortGroup:SetTask( EscortGroup:TaskRoute( Points ) )
- EscortGroup:MessageToClient( "Orbiting at location.", 10, EscortClient )
-
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_JoinUpAndFollow( Distance )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- self.Distance = Distance
-
- self:JoinUpAndFollow( EscortGroup, EscortClient, self.Distance )
-end
-
---- JoinsUp and Follows a CLIENT.
--- @param Functional.Escort#ESCORT self
--- @param Wrapper.Group#GROUP EscortGroup
--- @param Wrapper.Client#CLIENT EscortClient
--- @param Dcs.DCSTypes#Distance Distance
-function ESCORT:JoinUpAndFollow( EscortGroup, EscortClient, Distance )
- self:F( { EscortGroup, EscortClient, Distance } )
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- EscortGroup:OptionROEHoldFire()
- EscortGroup:OptionROTPassiveDefense()
-
- self.EscortMode = ESCORT.MODE.FOLLOW
-
- self.CT1 = 0
- self.GT1 = 0
- self.FollowScheduler:Start( self.FollowSchedule )
-
- EscortGroup:MessageToClient( "Rejoining and Following at " .. Distance .. "!", 30, EscortClient )
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_Flare( Color, Message )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- EscortGroup:GetUnit(1):Flare( Color )
- EscortGroup:MessageToClient( Message, 10, EscortClient )
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_Smoke( Color, Message )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- EscortGroup:GetUnit(1):Smoke( Color )
- EscortGroup:MessageToClient( Message, 10, EscortClient )
-end
-
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_ReportNearbyTargetsNow()
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- self:_ReportTargetsScheduler()
-
-end
-
-function ESCORT:_SwitchReportNearbyTargets( ReportTargets )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- self.ReportTargets = ReportTargets
-
- if self.ReportTargets then
- if not self.ReportTargetsScheduler then
- self.ReportTargetsScheduler:Schedule( self, self._ReportTargetsScheduler, {}, 1, 30 )
- end
- else
- routines.removeFunction( self.ReportTargetsScheduler )
- self.ReportTargetsScheduler = nil
- end
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_ScanTargets( ScanDuration )
-
- local EscortGroup = self.EscortGroup -- Wrapper.Group#GROUP
- local EscortClient = self.EscortClient
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- if EscortGroup:IsHelicopter() then
- EscortGroup:PushTask(
- EscortGroup:TaskControlled(
- EscortGroup:TaskOrbitCircle( 200, 20 ),
- EscortGroup:TaskCondition( nil, nil, nil, nil, ScanDuration, nil )
- ), 1 )
- elseif EscortGroup:IsAirPlane() then
- EscortGroup:PushTask(
- EscortGroup:TaskControlled(
- EscortGroup:TaskOrbitCircle( 1000, 500 ),
- EscortGroup:TaskCondition( nil, nil, nil, nil, ScanDuration, nil )
- ), 1 )
- end
-
- EscortGroup:MessageToClient( "Scanning targets for " .. ScanDuration .. " seconds.", ScanDuration, EscortClient )
-
- if self.EscortMode == ESCORT.MODE.FOLLOW then
- self.FollowScheduler:Start( self.FollowSchedule )
- end
-
-end
-
---- @param Wrapper.Group#GROUP EscortGroup
-function _Resume( EscortGroup )
- env.info( '_Resume' )
-
- local Escort = EscortGroup:GetState( EscortGroup, "Escort" )
- env.info( "EscortMode = " .. Escort.EscortMode )
- if Escort.EscortMode == ESCORT.MODE.FOLLOW then
- Escort:JoinUpAndFollow( EscortGroup, Escort.EscortClient, Escort.Distance )
- end
-
-end
-
---- @param #ESCORT self
--- @param #number DetectedItemID
-function ESCORT:_AttackTarget( DetectedItemID )
-
- local EscortGroup = self.EscortGroup -- Wrapper.Group#GROUP
- self:E( EscortGroup )
-
- local EscortClient = self.EscortClient
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- if EscortGroup:IsAir() then
- EscortGroup:OptionROEOpenFire()
- EscortGroup:OptionROTPassiveDefense()
- EscortGroup:SetState( EscortGroup, "Escort", self )
-
- local DetectedSet = self.Detection:GetDetectedSet( DetectedItemID )
-
- local Tasks = {}
-
- DetectedSet:ForEachUnit(
- --- @param Wrapper.Unit#UNIT DetectedUnit
- function( DetectedUnit, Tasks )
- if DetectedUnit:IsAlive() then
- Tasks[#Tasks+1] = EscortGroup:TaskAttackUnit( DetectedUnit )
- end
- end, Tasks
- )
-
- Tasks[#Tasks+1] = EscortGroup:TaskFunction( 1, 2, "_Resume", { "''" } )
-
- EscortGroup:SetTask(
- EscortGroup:TaskCombo(
- Tasks
- ), 1
- )
-
- else
-
- local DetectedSet = self.Detection:GetDetectedSet( DetectedItemID )
-
- local Tasks = {}
-
- DetectedSet:ForEachUnit(
- --- @param Wrapper.Unit#UNIT DetectedUnit
- function( DetectedUnit, Tasks )
- if DetectedUnit:IsAlive() then
- Tasks[#Tasks+1] = EscortGroup:TaskFireAtPoint( DetectedUnit:GetVec2(), 50 )
- end
- end, Tasks
- )
-
- EscortGroup:SetTask(
- EscortGroup:TaskCombo(
- Tasks
- ), 1
- )
-
- end
-
- EscortGroup:MessageToClient( "Engaging Designated Unit!", 10, EscortClient )
-
-end
-
----
--- @param #number DetectedItemID
-function ESCORT:_AssistTarget( EscortGroupAttack, DetectedItemID )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- if EscortGroupAttack:IsAir() then
- EscortGroupAttack:OptionROEOpenFire()
- EscortGroupAttack:OptionROTVertical()
-
- local DetectedSet = self.Detection:GetDetectedSet( DetectedItemID )
-
- local Tasks = {}
-
- DetectedSet:ForEachUnit(
- --- @param Wrapper.Unit#UNIT DetectedUnit
- function( DetectedUnit, Tasks )
- if DetectedUnit:IsAlive() then
- Tasks[#Tasks+1] = EscortGroupAttack:TaskAttackUnit( DetectedUnit )
- end
- end, Tasks
- )
-
- Tasks[#Tasks+1] = EscortGroupAttack:TaskOrbitCircle( 500, 350 )
-
- EscortGroupAttack:SetTask(
- EscortGroupAttack:TaskCombo(
- Tasks
- ), 1
- )
-
- else
- local DetectedSet = self.Detection:GetDetectedSet( DetectedItemID )
-
- local Tasks = {}
-
- DetectedSet:ForEachUnit(
- --- @param Wrapper.Unit#UNIT DetectedUnit
- function( DetectedUnit, Tasks )
- if DetectedUnit:IsAlive() then
- Tasks[#Tasks+1] = EscortGroupAttack:TaskFireAtPoint( DetectedUnit:GetVec2(), 50 )
- end
- end, Tasks
- )
-
- EscortGroupAttack:SetTask(
- EscortGroupAttack:TaskCombo(
- Tasks
- ), 1
- )
-
- end
-
- EscortGroupAttack:MessageToClient( "Assisting with the destroying the enemy unit!", 10, EscortClient )
-
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_ROE( EscortROEFunction, EscortROEMessage )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- pcall( function() EscortROEFunction() end )
- EscortGroup:MessageToClient( EscortROEMessage, 10, EscortClient )
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_ROT( EscortROTFunction, EscortROTMessage )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- pcall( function() EscortROTFunction() end )
- EscortGroup:MessageToClient( EscortROTMessage, 10, EscortClient )
-end
-
---- @param #MENUPARAM MenuParam
-function ESCORT:_ResumeMission( WayPoint )
-
- local EscortGroup = self.EscortGroup
- local EscortClient = self.EscortClient
-
- self.FollowScheduler:Stop( self.FollowSchedule )
-
- local WayPoints = EscortGroup:GetTaskRoute()
- self:T( WayPoint, WayPoints )
-
- for WayPointIgnore = 1, WayPoint do
- table.remove( WayPoints, 1 )
- end
-
- SCHEDULER:New( EscortGroup, EscortGroup.SetTask, { EscortGroup:TaskRoute( WayPoints ) }, 1 )
-
- EscortGroup:MessageToClient( "Resuming mission from waypoint " .. WayPoint .. ".", 10, EscortClient )
-end
-
---- Registers the waypoints
--- @param #ESCORT self
--- @return #table
-function ESCORT:RegisterRoute()
- self:F()
-
- local EscortGroup = self.EscortGroup -- Wrapper.Group#GROUP
-
- local TaskPoints = EscortGroup:GetTaskRoute()
-
- self:T( TaskPoints )
-
- return TaskPoints
-end
-
---- @param Functional.Escort#ESCORT self
-function ESCORT:_FollowScheduler()
- self:F( { self.FollowDistance } )
-
- self:T( {self.EscortClient.UnitName, self.EscortGroup.GroupName } )
- if self.EscortGroup:IsAlive() and self.EscortClient:IsAlive() then
-
- local ClientUnit = self.EscortClient:GetClientGroupUnit()
- local GroupUnit = self.EscortGroup:GetUnit( 1 )
- local FollowDistance = self.FollowDistance
-
- self:T( {ClientUnit.UnitName, GroupUnit.UnitName } )
-
- if self.CT1 == 0 and self.GT1 == 0 then
- self.CV1 = ClientUnit:GetVec3()
- self:T( { "self.CV1", self.CV1 } )
- self.CT1 = timer.getTime()
- self.GV1 = GroupUnit:GetVec3()
- self.GT1 = timer.getTime()
- else
- local CT1 = self.CT1
- local CT2 = timer.getTime()
- local CV1 = self.CV1
- local CV2 = ClientUnit:GetVec3()
- self.CT1 = CT2
- self.CV1 = CV2
-
- local CD = ( ( CV2.x - CV1.x )^2 + ( CV2.y - CV1.y )^2 + ( CV2.z - CV1.z )^2 ) ^ 0.5
- local CT = CT2 - CT1
-
- local CS = ( 3600 / CT ) * ( CD / 1000 )
-
- self:T2( { "Client:", CS, CD, CT, CV2, CV1, CT2, CT1 } )
-
- local GT1 = self.GT1
- local GT2 = timer.getTime()
- local GV1 = self.GV1
- local GV2 = GroupUnit:GetVec3()
- self.GT1 = GT2
- self.GV1 = GV2
-
- local GD = ( ( GV2.x - GV1.x )^2 + ( GV2.y - GV1.y )^2 + ( GV2.z - GV1.z )^2 ) ^ 0.5
- local GT = GT2 - GT1
-
- local GS = ( 3600 / GT ) * ( GD / 1000 )
-
- self:T2( { "Group:", GS, GD, GT, GV2, GV1, GT2, GT1 } )
-
- -- Calculate the group direction vector
- local GV = { x = GV2.x - CV2.x, y = GV2.y - CV2.y, z = GV2.z - CV2.z }
-
- -- Calculate GH2, GH2 with the same height as CV2.
- local GH2 = { x = GV2.x, y = CV2.y, z = GV2.z }
-
- -- Calculate the angle of GV to the orthonormal plane
- local alpha = math.atan2( GV.z, GV.x )
-
- -- Now we calculate the intersecting vector between the circle around CV2 with radius FollowDistance and GH2.
- -- From the GeoGebra model: CVI = (x(CV2) + FollowDistance cos(alpha), y(GH2) + FollowDistance sin(alpha), z(CV2))
- local CVI = { x = CV2.x + FollowDistance * math.cos(alpha),
- y = GH2.y,
- z = CV2.z + FollowDistance * math.sin(alpha),
- }
-
- -- Calculate the direction vector DV of the escort group. We use CVI as the base and CV2 as the direction.
- local DV = { x = CV2.x - CVI.x, y = CV2.y - CVI.y, z = CV2.z - CVI.z }
-
- -- We now calculate the unary direction vector DVu, so that we can multiply DVu with the speed, which is expressed in meters / s.
- -- We need to calculate this vector to predict the point the escort group needs to fly to according its speed.
- -- The distance of the destination point should be far enough not to have the aircraft starting to swipe left to right...
- local DVu = { x = DV.x / FollowDistance, y = DV.y / FollowDistance, z = DV.z / FollowDistance }
-
- -- Now we can calculate the group destination vector GDV.
- local GDV = { x = DVu.x * CS * 8 + CVI.x, y = CVI.y, z = DVu.z * CS * 8 + CVI.z }
-
- if self.SmokeDirectionVector == true then
- trigger.action.smoke( GDV, trigger.smokeColor.Red )
- end
-
- self:T2( { "CV2:", CV2 } )
- self:T2( { "CVI:", CVI } )
- self:T2( { "GDV:", GDV } )
-
- -- Measure distance between client and group
- local CatchUpDistance = ( ( GDV.x - GV2.x )^2 + ( GDV.y - GV2.y )^2 + ( GDV.z - GV2.z )^2 ) ^ 0.5
-
- -- The calculation of the Speed would simulate that the group would take 30 seconds to overcome
- -- the requested Distance).
- local Time = 10
- local CatchUpSpeed = ( CatchUpDistance - ( CS * 8.4 ) ) / Time
-
- local Speed = CS + CatchUpSpeed
- if Speed < 0 then
- Speed = 0
- end
-
- self:T( { "Client Speed, Escort Speed, Speed, FollowDistance, Time:", CS, GS, Speed, FollowDistance, Time } )
-
- -- Now route the escort to the desired point with the desired speed.
- self.EscortGroup:RouteToVec3( GDV, Speed / 3.6 ) -- DCS models speed in Mps (Miles per second)
- end
-
- return true
- end
-
- return false
-end
-
-
---- Report Targets Scheduler.
--- @param #ESCORT self
-function ESCORT:_ReportTargetsScheduler()
- self:F( self.EscortGroup:GetName() )
-
- if self.EscortGroup:IsAlive() and self.EscortClient:IsAlive() then
-
- if true then
-
- local EscortGroupName = self.EscortGroup:GetName()
-
- self.EscortMenuAttackNearbyTargets:RemoveSubMenus()
-
- if self.EscortMenuTargetAssistance then
- self.EscortMenuTargetAssistance:RemoveSubMenus()
- end
-
- local DetectedItems = self.Detection:GetDetectedItems()
- self:E( DetectedItems )
-
- local DetectedTargets = false
-
- local DetectedMsgs = {}
-
- for ClientEscortGroupName, EscortGroupData in pairs( self.EscortClient._EscortGroups ) do
-
- local ClientEscortTargets = EscortGroupData.Detection
-
- for DetectedItemID, DetectedItem in ipairs( DetectedItems ) do
- self:E( { DetectedItemID, DetectedItem } )
- -- Remove the sub menus of the Attack menu of the Escort for the EscortGroup.
-
- local DetectedItemReportSummary = self.Detection:DetectedItemReportSummary( DetectedItemID )
-
- if ClientEscortGroupName == EscortGroupName then
-
- DetectedMsgs[#DetectedMsgs+1] = DetectedItemReportSummary
-
- MENU_CLIENT_COMMAND:New( self.EscortClient,
- DetectedItemReportSummary,
- self.EscortMenuAttackNearbyTargets,
- ESCORT._AttackTarget,
- self,
- DetectedItemID
- )
- else
- if self.EscortMenuTargetAssistance then
-
- self:T( DetectedItemReportSummary )
- local MenuTargetAssistance = MENU_CLIENT:New( self.EscortClient, EscortGroupData.EscortName, self.EscortMenuTargetAssistance )
- MENU_CLIENT_COMMAND:New( self.EscortClient,
- DetectedItemReportSummary,
- MenuTargetAssistance,
- ESCORT._AssistTarget,
- self,
- EscortGroupData.EscortGroup,
- DetectedItemID
- )
- end
- end
-
- DetectedTargets = true
-
- end
- end
- self:E( DetectedMsgs )
- if DetectedTargets then
- self.EscortGroup:MessageToClient( "Detected targets:\n" .. table.concat( DetectedMsgs, "\n" ), 20, self.EscortClient )
- else
- self.EscortGroup:MessageToClient( "No targets detected.", 10, self.EscortClient )
- end
-
- return true
- else
--- local EscortGroupName = self.EscortGroup:GetName()
--- local EscortTargets = self.EscortGroup:GetDetectedTargets()
---
--- local ClientEscortTargets = self.EscortClient._EscortGroups[EscortGroupName].Targets
---
--- local EscortTargetMessages = ""
--- for EscortTargetID, EscortTarget in pairs( EscortTargets ) do
--- local EscortObject = EscortTarget.object
--- self:T( EscortObject )
--- if EscortObject and EscortObject:isExist() and EscortObject.id_ < 50000000 then
---
--- local EscortTargetUnit = UNIT:Find( EscortObject )
--- local EscortTargetUnitName = EscortTargetUnit:GetName()
---
---
---
--- -- local EscortTargetIsDetected,
--- -- EscortTargetIsVisible,
--- -- EscortTargetLastTime,
--- -- EscortTargetKnowType,
--- -- EscortTargetKnowDistance,
--- -- EscortTargetLastPos,
--- -- EscortTargetLastVelocity
--- -- = self.EscortGroup:IsTargetDetected( EscortObject )
--- --
--- -- self:T( { EscortTargetIsDetected,
--- -- EscortTargetIsVisible,
--- -- EscortTargetLastTime,
--- -- EscortTargetKnowType,
--- -- EscortTargetKnowDistance,
--- -- EscortTargetLastPos,
--- -- EscortTargetLastVelocity } )
---
---
--- local EscortTargetUnitVec3 = EscortTargetUnit:GetVec3()
--- local EscortVec3 = self.EscortGroup:GetVec3()
--- local Distance = ( ( EscortTargetUnitVec3.x - EscortVec3.x )^2 +
--- ( EscortTargetUnitVec3.y - EscortVec3.y )^2 +
--- ( EscortTargetUnitVec3.z - EscortVec3.z )^2
--- ) ^ 0.5 / 1000
---
--- self:T( { self.EscortGroup:GetName(), EscortTargetUnit:GetName(), Distance, EscortTarget } )
---
--- if Distance <= 15 then
---
--- if not ClientEscortTargets[EscortTargetUnitName] then
--- ClientEscortTargets[EscortTargetUnitName] = {}
--- end
--- ClientEscortTargets[EscortTargetUnitName].AttackUnit = EscortTargetUnit
--- ClientEscortTargets[EscortTargetUnitName].visible = EscortTarget.visible
--- ClientEscortTargets[EscortTargetUnitName].type = EscortTarget.type
--- ClientEscortTargets[EscortTargetUnitName].distance = EscortTarget.distance
--- else
--- if ClientEscortTargets[EscortTargetUnitName] then
--- ClientEscortTargets[EscortTargetUnitName] = nil
--- end
--- end
--- end
--- end
---
--- self:T( { "Sorting Targets Table:", ClientEscortTargets } )
--- table.sort( ClientEscortTargets, function( a, b ) return a.Distance < b.Distance end )
--- self:T( { "Sorted Targets Table:", ClientEscortTargets } )
---
--- -- Remove the sub menus of the Attack menu of the Escort for the EscortGroup.
--- self.EscortMenuAttackNearbyTargets:RemoveSubMenus()
---
--- if self.EscortMenuTargetAssistance then
--- self.EscortMenuTargetAssistance:RemoveSubMenus()
--- end
---
--- --for MenuIndex = 1, #self.EscortMenuAttackTargets do
--- -- self:T( { "Remove Menu:", self.EscortMenuAttackTargets[MenuIndex] } )
--- -- self.EscortMenuAttackTargets[MenuIndex] = self.EscortMenuAttackTargets[MenuIndex]:Remove()
--- --end
---
---
--- if ClientEscortTargets then
--- for ClientEscortTargetUnitName, ClientEscortTargetData in pairs( ClientEscortTargets ) do
---
--- for ClientEscortGroupName, EscortGroupData in pairs( self.EscortClient._EscortGroups ) do
---
--- if ClientEscortTargetData and ClientEscortTargetData.AttackUnit:IsAlive() then
---
--- local EscortTargetMessage = ""
--- local EscortTargetCategoryName = ClientEscortTargetData.AttackUnit:GetCategoryName()
--- local EscortTargetCategoryType = ClientEscortTargetData.AttackUnit:GetTypeName()
--- if ClientEscortTargetData.type then
--- EscortTargetMessage = EscortTargetMessage .. EscortTargetCategoryName .. " (" .. EscortTargetCategoryType .. ") at "
--- else
--- EscortTargetMessage = EscortTargetMessage .. "Unknown target at "
--- end
---
--- local EscortTargetUnitVec3 = ClientEscortTargetData.AttackUnit:GetVec3()
--- local EscortVec3 = self.EscortGroup:GetVec3()
--- local Distance = ( ( EscortTargetUnitVec3.x - EscortVec3.x )^2 +
--- ( EscortTargetUnitVec3.y - EscortVec3.y )^2 +
--- ( EscortTargetUnitVec3.z - EscortVec3.z )^2
--- ) ^ 0.5 / 1000
---
--- self:T( { self.EscortGroup:GetName(), ClientEscortTargetData.AttackUnit:GetName(), Distance, ClientEscortTargetData.AttackUnit } )
--- if ClientEscortTargetData.visible == false then
--- EscortTargetMessage = EscortTargetMessage .. string.format( "%.2f", Distance ) .. " estimated km"
--- else
--- EscortTargetMessage = EscortTargetMessage .. string.format( "%.2f", Distance ) .. " km"
--- end
---
--- if ClientEscortTargetData.visible then
--- EscortTargetMessage = EscortTargetMessage .. ", visual"
--- end
---
--- if ClientEscortGroupName == EscortGroupName then
---
--- MENU_CLIENT_COMMAND:New( self.EscortClient,
--- EscortTargetMessage,
--- self.EscortMenuAttackNearbyTargets,
--- ESCORT._AttackTarget,
--- { ParamSelf = self,
--- ParamUnit = ClientEscortTargetData.AttackUnit
--- }
--- )
--- EscortTargetMessages = EscortTargetMessages .. "\n - " .. EscortTargetMessage
--- else
--- if self.EscortMenuTargetAssistance then
--- local MenuTargetAssistance = MENU_CLIENT:New( self.EscortClient, EscortGroupData.EscortName, self.EscortMenuTargetAssistance )
--- MENU_CLIENT_COMMAND:New( self.EscortClient,
--- EscortTargetMessage,
--- MenuTargetAssistance,
--- ESCORT._AssistTarget,
--- self,
--- EscortGroupData.EscortGroup,
--- ClientEscortTargetData.AttackUnit
--- )
--- end
--- end
--- else
--- ClientEscortTargetData = nil
--- end
--- end
--- end
---
--- if EscortTargetMessages ~= "" and self.ReportTargets == true then
--- self.EscortGroup:MessageToClient( "Detected targets within 15 km range:" .. EscortTargetMessages:gsub("\n$",""), 20, self.EscortClient )
--- else
--- self.EscortGroup:MessageToClient( "No targets detected!", 20, self.EscortClient )
--- end
--- end
---
--- if self.EscortMenuResumeMission then
--- self.EscortMenuResumeMission:RemoveSubMenus()
---
--- -- if self.EscortMenuResumeWayPoints then
--- -- for MenuIndex = 1, #self.EscortMenuResumeWayPoints do
--- -- self:T( { "Remove Menu:", self.EscortMenuResumeWayPoints[MenuIndex] } )
--- -- self.EscortMenuResumeWayPoints[MenuIndex] = self.EscortMenuResumeWayPoints[MenuIndex]:Remove()
--- -- end
--- -- end
---
--- local TaskPoints = self:RegisterRoute()
--- for WayPointID, WayPoint in pairs( TaskPoints ) do
--- local EscortVec3 = self.EscortGroup:GetVec3()
--- local Distance = ( ( WayPoint.x - EscortVec3.x )^2 +
--- ( WayPoint.y - EscortVec3.z )^2
--- ) ^ 0.5 / 1000
--- MENU_CLIENT_COMMAND:New( self.EscortClient, "Waypoint " .. WayPointID .. " at " .. string.format( "%.2f", Distance ).. "km", self.EscortMenuResumeMission, ESCORT._ResumeMission, { ParamSelf = self, ParamWayPoint = WayPointID } )
--- end
--- end
---
--- return true
- end
- end
-
- return false
-end
---- This module contains the MISSILETRAINER class.
---
--- ===
---
--- 1) @{MissileTrainer#MISSILETRAINER} class, extends @{Base#BASE}
--- ===============================================================
--- The @{#MISSILETRAINER} class uses the DCS world messaging system to be alerted of any missiles fired, and when a missile would hit your aircraft,
--- the class will destroy the missile within a certain range, to avoid damage to your aircraft.
--- It suports the following functionality:
---
--- * Track the missiles fired at you and other players, providing bearing and range information of the missiles towards the airplanes.
--- * Provide alerts of missile launches, including detailed information of the units launching, including bearing, range �
--- * Provide alerts when a missile would have killed your aircraft.
--- * Provide alerts when the missile self destructs.
--- * Enable / Disable and Configure the Missile Trainer using the various menu options.
---
--- When running a mission where MISSILETRAINER is used, the following radio menu structure ( 'Radio Menu' -> 'Other (F10)' -> 'MissileTrainer' ) options are available for the players:
---
--- * **Messages**: Menu to configure all messages.
--- * **Messages On**: Show all messages.
--- * **Messages Off**: Disable all messages.
--- * **Tracking**: Menu to configure missile tracking messages.
--- * **To All**: Shows missile tracking messages to all players.
--- * **To Target**: Shows missile tracking messages only to the player where the missile is targetted at.
--- * **Tracking On**: Show missile tracking messages.
--- * **Tracking Off**: Disable missile tracking messages.
--- * **Frequency Increase**: Increases the missile tracking message frequency with one second.
--- * **Frequency Decrease**: Decreases the missile tracking message frequency with one second.
--- * **Alerts**: Menu to configure alert messages.
--- * **To All**: Shows alert messages to all players.
--- * **To Target**: Shows alert messages only to the player where the missile is (was) targetted at.
--- * **Hits On**: Show missile hit alert messages.
--- * **Hits Off**: Disable missile hit alert messages.
--- * **Launches On**: Show missile launch messages.
--- * **Launches Off**: Disable missile launch messages.
--- * **Details**: Menu to configure message details.
--- * **Range On**: Shows range information when a missile is fired to a target.
--- * **Range Off**: Disable range information when a missile is fired to a target.
--- * **Bearing On**: Shows bearing information when a missile is fired to a target.
--- * **Bearing Off**: Disable bearing information when a missile is fired to a target.
--- * **Distance**: Menu to configure the distance when a missile needs to be destroyed when near to a player, during tracking. This will improve/influence hit calculation accuracy, but has the risk of damaging the aircraft when the missile reaches the aircraft before the distance is measured.
--- * **50 meter**: Destroys the missile when the distance to the aircraft is below or equal to 50 meter.
--- * **100 meter**: Destroys the missile when the distance to the aircraft is below or equal to 100 meter.
--- * **150 meter**: Destroys the missile when the distance to the aircraft is below or equal to 150 meter.
--- * **200 meter**: Destroys the missile when the distance to the aircraft is below or equal to 200 meter.
---
---
--- 1.1) MISSILETRAINER construction methods:
--- -----------------------------------------
--- Create a new MISSILETRAINER object with the @{#MISSILETRAINER.New} method:
---
--- * @{#MISSILETRAINER.New}: Creates a new MISSILETRAINER object taking the maximum distance to your aircraft to evaluate when a missile needs to be destroyed.
---
--- MISSILETRAINER will collect each unit declared in the mission with a skill level "Client" and "Player", and will monitor the missiles shot at those.
---
--- 1.2) MISSILETRAINER initialization methods:
--- -------------------------------------------
--- A MISSILETRAINER object will behave differently based on the usage of initialization methods:
---
--- * @{#MISSILETRAINER.InitMessagesOnOff}: Sets by default the display of any message to be ON or OFF.
--- * @{#MISSILETRAINER.InitTrackingToAll}: Sets by default the missile tracking report for all players or only for those missiles targetted to you.
--- * @{#MISSILETRAINER.InitTrackingOnOff}: Sets by default the display of missile tracking report to be ON or OFF.
--- * @{#MISSILETRAINER.InitTrackingFrequency}: Increases, decreases the missile tracking message display frequency with the provided time interval in seconds.
--- * @{#MISSILETRAINER.InitAlertsToAll}: Sets by default the display of alerts to be shown to all players or only to you.
--- * @{#MISSILETRAINER.InitAlertsHitsOnOff}: Sets by default the display of hit alerts ON or OFF.
--- * @{#MISSILETRAINER.InitAlertsLaunchesOnOff}: Sets by default the display of launch alerts ON or OFF.
--- * @{#MISSILETRAINER.InitRangeOnOff}: Sets by default the display of range information of missiles ON of OFF.
--- * @{#MISSILETRAINER.InitBearingOnOff}: Sets by default the display of bearing information of missiles ON of OFF.
--- * @{#MISSILETRAINER.InitMenusOnOff}: Allows to configure the options through the radio menu.
---
--- ===
---
--- CREDITS
--- =======
--- **Stuka (Danny)** Who you can search on the Eagle Dynamics Forums.
--- Working together with Danny has resulted in the MISSILETRAINER class.
--- Danny has shared his ideas and together we made a design.
--- Together with the **476 virtual team**, we tested the MISSILETRAINER class, and got much positive feedback!
---
--- @module MissileTrainer
--- @author FlightControl
-
-
---- The MISSILETRAINER class
--- @type MISSILETRAINER
--- @field Core.Set#SET_CLIENT DBClients
--- @extends Core.Base#BASE
-MISSILETRAINER = {
- ClassName = "MISSILETRAINER",
- TrackingMissiles = {},
-}
-
-function MISSILETRAINER._Alive( Client, self )
-
- if self.Briefing then
- Client:Message( self.Briefing, 15, "Trainer" )
- end
-
- if self.MenusOnOff == true then
- Client:Message( "Use the 'Radio Menu' -> 'Other (F10)' -> 'Missile Trainer' menu options to change the Missile Trainer settings (for all players).", 15, "Trainer" )
-
- Client.MainMenu = MENU_CLIENT:New( Client, "Missile Trainer", nil ) -- Menu#MENU_CLIENT
-
- Client.MenuMessages = MENU_CLIENT:New( Client, "Messages", Client.MainMenu )
- Client.MenuOn = MENU_CLIENT_COMMAND:New( Client, "Messages On", Client.MenuMessages, self._MenuMessages, { MenuSelf = self, MessagesOnOff = true } )
- Client.MenuOff = MENU_CLIENT_COMMAND:New( Client, "Messages Off", Client.MenuMessages, self._MenuMessages, { MenuSelf = self, MessagesOnOff = false } )
-
- Client.MenuTracking = MENU_CLIENT:New( Client, "Tracking", Client.MainMenu )
- Client.MenuTrackingToAll = MENU_CLIENT_COMMAND:New( Client, "To All", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingToAll = true } )
- Client.MenuTrackingToTarget = MENU_CLIENT_COMMAND:New( Client, "To Target", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingToAll = false } )
- Client.MenuTrackOn = MENU_CLIENT_COMMAND:New( Client, "Tracking On", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingOnOff = true } )
- Client.MenuTrackOff = MENU_CLIENT_COMMAND:New( Client, "Tracking Off", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingOnOff = false } )
- Client.MenuTrackIncrease = MENU_CLIENT_COMMAND:New( Client, "Frequency Increase", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingFrequency = -1 } )
- Client.MenuTrackDecrease = MENU_CLIENT_COMMAND:New( Client, "Frequency Decrease", Client.MenuTracking, self._MenuMessages, { MenuSelf = self, TrackingFrequency = 1 } )
-
- Client.MenuAlerts = MENU_CLIENT:New( Client, "Alerts", Client.MainMenu )
- Client.MenuAlertsToAll = MENU_CLIENT_COMMAND:New( Client, "To All", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsToAll = true } )
- Client.MenuAlertsToTarget = MENU_CLIENT_COMMAND:New( Client, "To Target", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsToAll = false } )
- Client.MenuHitsOn = MENU_CLIENT_COMMAND:New( Client, "Hits On", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsHitsOnOff = true } )
- Client.MenuHitsOff = MENU_CLIENT_COMMAND:New( Client, "Hits Off", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsHitsOnOff = false } )
- Client.MenuLaunchesOn = MENU_CLIENT_COMMAND:New( Client, "Launches On", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsLaunchesOnOff = true } )
- Client.MenuLaunchesOff = MENU_CLIENT_COMMAND:New( Client, "Launches Off", Client.MenuAlerts, self._MenuMessages, { MenuSelf = self, AlertsLaunchesOnOff = false } )
-
- Client.MenuDetails = MENU_CLIENT:New( Client, "Details", Client.MainMenu )
- Client.MenuDetailsDistanceOn = MENU_CLIENT_COMMAND:New( Client, "Range On", Client.MenuDetails, self._MenuMessages, { MenuSelf = self, DetailsRangeOnOff = true } )
- Client.MenuDetailsDistanceOff = MENU_CLIENT_COMMAND:New( Client, "Range Off", Client.MenuDetails, self._MenuMessages, { MenuSelf = self, DetailsRangeOnOff = false } )
- Client.MenuDetailsBearingOn = MENU_CLIENT_COMMAND:New( Client, "Bearing On", Client.MenuDetails, self._MenuMessages, { MenuSelf = self, DetailsBearingOnOff = true } )
- Client.MenuDetailsBearingOff = MENU_CLIENT_COMMAND:New( Client, "Bearing Off", Client.MenuDetails, self._MenuMessages, { MenuSelf = self, DetailsBearingOnOff = false } )
-
- Client.MenuDistance = MENU_CLIENT:New( Client, "Set distance to plane", Client.MainMenu )
- Client.MenuDistance50 = MENU_CLIENT_COMMAND:New( Client, "50 meter", Client.MenuDistance, self._MenuMessages, { MenuSelf = self, Distance = 50 / 1000 } )
- Client.MenuDistance100 = MENU_CLIENT_COMMAND:New( Client, "100 meter", Client.MenuDistance, self._MenuMessages, { MenuSelf = self, Distance = 100 / 1000 } )
- Client.MenuDistance150 = MENU_CLIENT_COMMAND:New( Client, "150 meter", Client.MenuDistance, self._MenuMessages, { MenuSelf = self, Distance = 150 / 1000 } )
- Client.MenuDistance200 = MENU_CLIENT_COMMAND:New( Client, "200 meter", Client.MenuDistance, self._MenuMessages, { MenuSelf = self, Distance = 200 / 1000 } )
- else
- if Client.MainMenu then
- Client.MainMenu:Remove()
- end
- end
-
- local ClientID = Client:GetID()
- self:T( ClientID )
- if not self.TrackingMissiles[ClientID] then
- self.TrackingMissiles[ClientID] = {}
- end
- self.TrackingMissiles[ClientID].Client = Client
- if not self.TrackingMissiles[ClientID].MissileData then
- self.TrackingMissiles[ClientID].MissileData = {}
- end
-end
-
---- Creates the main object which is handling missile tracking.
--- When a missile is fired a SCHEDULER is set off that follows the missile. When near a certain a client player, the missile will be destroyed.
--- @param #MISSILETRAINER self
--- @param #number Distance The distance in meters when a tracked missile needs to be destroyed when close to a player.
--- @param #string Briefing (Optional) Will show a text to the players when starting their mission. Can be used for briefing purposes.
--- @return #MISSILETRAINER
-function MISSILETRAINER:New( Distance, Briefing )
- local self = BASE:Inherit( self, BASE:New() )
- self:F( Distance )
-
- if Briefing then
- self.Briefing = Briefing
- end
-
- self.Schedulers = {}
- self.SchedulerID = 0
-
- self.MessageInterval = 2
- self.MessageLastTime = timer.getTime()
-
- self.Distance = Distance / 1000
-
- self:HandleEvent( EVENTS.Shot )
-
- self.DBClients = SET_CLIENT:New():FilterStart()
-
-
--- for ClientID, Client in pairs( self.DBClients.Database ) do
--- self:E( "ForEach:" .. Client.UnitName )
--- Client:Alive( self._Alive, self )
--- end
---
- self.DBClients:ForEachClient(
- function( Client )
- self:E( "ForEach:" .. Client.UnitName )
- Client:Alive( self._Alive, self )
- end
- )
-
-
-
--- self.DB:ForEachClient(
--- --- @param Wrapper.Client#CLIENT Client
--- function( Client )
---
--- ... actions ...
---
--- end
--- )
-
- self.MessagesOnOff = true
-
- self.TrackingToAll = false
- self.TrackingOnOff = true
- self.TrackingFrequency = 3
-
- self.AlertsToAll = true
- self.AlertsHitsOnOff = true
- self.AlertsLaunchesOnOff = true
-
- self.DetailsRangeOnOff = true
- self.DetailsBearingOnOff = true
-
- self.MenusOnOff = true
-
- self.TrackingMissiles = {}
-
- self.TrackingScheduler = SCHEDULER:New( self, self._TrackMissiles, {}, 0.5, 0.05, 0 )
-
- return self
-end
-
--- Initialization methods.
-
-
-
---- Sets by default the display of any message to be ON or OFF.
--- @param #MISSILETRAINER self
--- @param #boolean MessagesOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitMessagesOnOff( MessagesOnOff )
- self:F( MessagesOnOff )
-
- self.MessagesOnOff = MessagesOnOff
- if self.MessagesOnOff == true then
- MESSAGE:New( "Messages ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Messages OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the missile tracking report for all players or only for those missiles targetted to you.
--- @param #MISSILETRAINER self
--- @param #boolean TrackingToAll true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitTrackingToAll( TrackingToAll )
- self:F( TrackingToAll )
-
- self.TrackingToAll = TrackingToAll
- if self.TrackingToAll == true then
- MESSAGE:New( "Missile tracking to all players ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Missile tracking to all players OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of missile tracking report to be ON or OFF.
--- @param #MISSILETRAINER self
--- @param #boolean TrackingOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitTrackingOnOff( TrackingOnOff )
- self:F( TrackingOnOff )
-
- self.TrackingOnOff = TrackingOnOff
- if self.TrackingOnOff == true then
- MESSAGE:New( "Missile tracking ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Missile tracking OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Increases, decreases the missile tracking message display frequency with the provided time interval in seconds.
--- The default frequency is a 3 second interval, so the Tracking Frequency parameter specifies the increase or decrease from the default 3 seconds or the last frequency update.
--- @param #MISSILETRAINER self
--- @param #number TrackingFrequency Provide a negative or positive value in seconds to incraese or decrease the display frequency.
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitTrackingFrequency( TrackingFrequency )
- self:F( TrackingFrequency )
-
- self.TrackingFrequency = self.TrackingFrequency + TrackingFrequency
- if self.TrackingFrequency < 0.5 then
- self.TrackingFrequency = 0.5
- end
- if self.TrackingFrequency then
- MESSAGE:New( "Missile tracking frequency is " .. self.TrackingFrequency .. " seconds.", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of alerts to be shown to all players or only to you.
--- @param #MISSILETRAINER self
--- @param #boolean AlertsToAll true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitAlertsToAll( AlertsToAll )
- self:F( AlertsToAll )
-
- self.AlertsToAll = AlertsToAll
- if self.AlertsToAll == true then
- MESSAGE:New( "Alerts to all players ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Alerts to all players OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of hit alerts ON or OFF.
--- @param #MISSILETRAINER self
--- @param #boolean AlertsHitsOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitAlertsHitsOnOff( AlertsHitsOnOff )
- self:F( AlertsHitsOnOff )
-
- self.AlertsHitsOnOff = AlertsHitsOnOff
- if self.AlertsHitsOnOff == true then
- MESSAGE:New( "Alerts Hits ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Alerts Hits OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of launch alerts ON or OFF.
--- @param #MISSILETRAINER self
--- @param #boolean AlertsLaunchesOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitAlertsLaunchesOnOff( AlertsLaunchesOnOff )
- self:F( AlertsLaunchesOnOff )
-
- self.AlertsLaunchesOnOff = AlertsLaunchesOnOff
- if self.AlertsLaunchesOnOff == true then
- MESSAGE:New( "Alerts Launches ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Alerts Launches OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of range information of missiles ON of OFF.
--- @param #MISSILETRAINER self
--- @param #boolean DetailsRangeOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitRangeOnOff( DetailsRangeOnOff )
- self:F( DetailsRangeOnOff )
-
- self.DetailsRangeOnOff = DetailsRangeOnOff
- if self.DetailsRangeOnOff == true then
- MESSAGE:New( "Range display ON", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Range display OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Sets by default the display of bearing information of missiles ON of OFF.
--- @param #MISSILETRAINER self
--- @param #boolean DetailsBearingOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitBearingOnOff( DetailsBearingOnOff )
- self:F( DetailsBearingOnOff )
-
- self.DetailsBearingOnOff = DetailsBearingOnOff
- if self.DetailsBearingOnOff == true then
- MESSAGE:New( "Bearing display OFF", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Bearing display OFF", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
---- Enables / Disables the menus.
--- @param #MISSILETRAINER self
--- @param #boolean MenusOnOff true or false
--- @return #MISSILETRAINER self
-function MISSILETRAINER:InitMenusOnOff( MenusOnOff )
- self:F( MenusOnOff )
-
- self.MenusOnOff = MenusOnOff
- if self.MenusOnOff == true then
- MESSAGE:New( "Menus are ENABLED (only when a player rejoins a slot)", 15, "Menu" ):ToAll()
- else
- MESSAGE:New( "Menus are DISABLED", 15, "Menu" ):ToAll()
- end
-
- return self
-end
-
-
--- Menu functions
-
-function MISSILETRAINER._MenuMessages( MenuParameters )
-
- local self = MenuParameters.MenuSelf
-
- if MenuParameters.MessagesOnOff ~= nil then
- self:InitMessagesOnOff( MenuParameters.MessagesOnOff )
- end
-
- if MenuParameters.TrackingToAll ~= nil then
- self:InitTrackingToAll( MenuParameters.TrackingToAll )
- end
-
- if MenuParameters.TrackingOnOff ~= nil then
- self:InitTrackingOnOff( MenuParameters.TrackingOnOff )
- end
-
- if MenuParameters.TrackingFrequency ~= nil then
- self:InitTrackingFrequency( MenuParameters.TrackingFrequency )
- end
-
- if MenuParameters.AlertsToAll ~= nil then
- self:InitAlertsToAll( MenuParameters.AlertsToAll )
- end
-
- if MenuParameters.AlertsHitsOnOff ~= nil then
- self:InitAlertsHitsOnOff( MenuParameters.AlertsHitsOnOff )
- end
-
- if MenuParameters.AlertsLaunchesOnOff ~= nil then
- self:InitAlertsLaunchesOnOff( MenuParameters.AlertsLaunchesOnOff )
- end
-
- if MenuParameters.DetailsRangeOnOff ~= nil then
- self:InitRangeOnOff( MenuParameters.DetailsRangeOnOff )
- end
-
- if MenuParameters.DetailsBearingOnOff ~= nil then
- self:InitBearingOnOff( MenuParameters.DetailsBearingOnOff )
- end
-
- if MenuParameters.Distance ~= nil then
- self.Distance = MenuParameters.Distance
- MESSAGE:New( "Hit detection distance set to " .. self.Distance .. " meters", 15, "Menu" ):ToAll()
- end
-
-end
-
---- Detects if an SA site was shot with an anti radiation missile. In this case, take evasive actions based on the skill level set within the ME.
--- @param #MISSILETRAINER self
--- @param Core.Event#EVENTDATA EventData
-function MISSILETRAINER:OnEventShot( EVentData )
- self:F( { EVentData } )
-
- local TrainerSourceDCSUnit = EVentData.IniDCSUnit
- local TrainerSourceDCSUnitName = EVentData.IniDCSUnitName
- local TrainerWeapon = EVentData.Weapon -- Identify the weapon fired
- local TrainerWeaponName = EVentData.WeaponName -- return weapon type
-
- self:T( "Missile Launched = " .. TrainerWeaponName )
-
- local TrainerTargetDCSUnit = TrainerWeapon:getTarget() -- Identify target
- if TrainerTargetDCSUnit then
- local TrainerTargetDCSUnitName = Unit.getName( TrainerTargetDCSUnit )
- local TrainerTargetSkill = _DATABASE.Templates.Units[TrainerTargetDCSUnitName].Template.skill
-
- self:T(TrainerTargetDCSUnitName )
-
- local Client = self.DBClients:FindClient( TrainerTargetDCSUnitName )
- if Client then
-
- local TrainerSourceUnit = UNIT:Find( TrainerSourceDCSUnit )
- local TrainerTargetUnit = UNIT:Find( TrainerTargetDCSUnit )
-
- if self.MessagesOnOff == true and self.AlertsLaunchesOnOff == true then
-
- local Message = MESSAGE:New(
- string.format( "%s launched a %s",
- TrainerSourceUnit:GetTypeName(),
- TrainerWeaponName
- ) .. self:_AddRange( Client, TrainerWeapon ) .. self:_AddBearing( Client, TrainerWeapon ), 5, "Launch Alert" )
-
- if self.AlertsToAll then
- Message:ToAll()
- else
- Message:ToClient( Client )
- end
- end
-
- local ClientID = Client:GetID()
- self:T( ClientID )
- local MissileData = {}
- MissileData.TrainerSourceUnit = TrainerSourceUnit
- MissileData.TrainerWeapon = TrainerWeapon
- MissileData.TrainerTargetUnit = TrainerTargetUnit
- MissileData.TrainerWeaponTypeName = TrainerWeapon:getTypeName()
- MissileData.TrainerWeaponLaunched = true
- table.insert( self.TrackingMissiles[ClientID].MissileData, MissileData )
- --self:T( self.TrackingMissiles )
- end
- else
- -- TODO: some weapons don't know the target unit... Need to develop a workaround for this.
- if ( TrainerWeapon:getTypeName() == "9M311" ) then
- SCHEDULER:New( TrainerWeapon, TrainerWeapon.destroy, {}, 1 )
- else
- end
- end
-end
-
-function MISSILETRAINER:_AddRange( Client, TrainerWeapon )
-
- local RangeText = ""
-
- if self.DetailsRangeOnOff then
-
- local PositionMissile = TrainerWeapon:getPoint()
- local TargetVec3 = Client:GetVec3()
-
- local Range = ( ( PositionMissile.x - TargetVec3.x )^2 +
- ( PositionMissile.y - TargetVec3.y )^2 +
- ( PositionMissile.z - TargetVec3.z )^2
- ) ^ 0.5 / 1000
-
- RangeText = string.format( ", at %4.2fkm", Range )
- end
-
- return RangeText
-end
-
-function MISSILETRAINER:_AddBearing( Client, TrainerWeapon )
-
- local BearingText = ""
-
- if self.DetailsBearingOnOff then
-
- local PositionMissile = TrainerWeapon:getPoint()
- local TargetVec3 = Client:GetVec3()
-
- self:T2( { TargetVec3, PositionMissile })
-
- local DirectionVector = { x = PositionMissile.x - TargetVec3.x, y = PositionMissile.y - TargetVec3.y, z = PositionMissile.z - TargetVec3.z }
- local DirectionRadians = math.atan2( DirectionVector.z, DirectionVector.x )
- --DirectionRadians = DirectionRadians + routines.getNorthCorrection( PositionTarget )
- if DirectionRadians < 0 then
- DirectionRadians = DirectionRadians + 2 * math.pi
- end
- local DirectionDegrees = DirectionRadians * 180 / math.pi
-
- BearingText = string.format( ", %d degrees", DirectionDegrees )
- end
-
- return BearingText
-end
-
-
-function MISSILETRAINER:_TrackMissiles()
- self:F2()
-
-
- local ShowMessages = false
- if self.MessagesOnOff and self.MessageLastTime + self.TrackingFrequency <= timer.getTime() then
- self.MessageLastTime = timer.getTime()
- ShowMessages = true
- end
-
- -- ALERTS PART
-
- -- Loop for all Player Clients to check the alerts and deletion of missiles.
- for ClientDataID, ClientData in pairs( self.TrackingMissiles ) do
-
- local Client = ClientData.Client
- self:T2( { Client:GetName() } )
-
- for MissileDataID, MissileData in pairs( ClientData.MissileData ) do
- self:T3( MissileDataID )
-
- local TrainerSourceUnit = MissileData.TrainerSourceUnit
- local TrainerWeapon = MissileData.TrainerWeapon
- local TrainerTargetUnit = MissileData.TrainerTargetUnit
- local TrainerWeaponTypeName = MissileData.TrainerWeaponTypeName
- local TrainerWeaponLaunched = MissileData.TrainerWeaponLaunched
-
- if Client and Client:IsAlive() and TrainerSourceUnit and TrainerSourceUnit:IsAlive() and TrainerWeapon and TrainerWeapon:isExist() and TrainerTargetUnit and TrainerTargetUnit:IsAlive() then
- local PositionMissile = TrainerWeapon:getPosition().p
- local TargetVec3 = Client:GetVec3()
-
- local Distance = ( ( PositionMissile.x - TargetVec3.x )^2 +
- ( PositionMissile.y - TargetVec3.y )^2 +
- ( PositionMissile.z - TargetVec3.z )^2
- ) ^ 0.5 / 1000
-
- if Distance <= self.Distance then
- -- Hit alert
- TrainerWeapon:destroy()
- if self.MessagesOnOff == true and self.AlertsHitsOnOff == true then
-
- self:T( "killed" )
-
- local Message = MESSAGE:New(
- string.format( "%s launched by %s killed %s",
- TrainerWeapon:getTypeName(),
- TrainerSourceUnit:GetTypeName(),
- TrainerTargetUnit:GetPlayerName()
- ), 15, "Hit Alert" )
-
- if self.AlertsToAll == true then
- Message:ToAll()
- else
- Message:ToClient( Client )
- end
-
- MissileData = nil
- table.remove( ClientData.MissileData, MissileDataID )
- self:T(ClientData.MissileData)
- end
- end
- else
- if not ( TrainerWeapon and TrainerWeapon:isExist() ) then
- if self.MessagesOnOff == true and self.AlertsLaunchesOnOff == true then
- -- Weapon does not exist anymore. Delete from Table
- local Message = MESSAGE:New(
- string.format( "%s launched by %s self destructed!",
- TrainerWeaponTypeName,
- TrainerSourceUnit:GetTypeName()
- ), 5, "Tracking" )
-
- if self.AlertsToAll == true then
- Message:ToAll()
- else
- Message:ToClient( Client )
- end
- end
- MissileData = nil
- table.remove( ClientData.MissileData, MissileDataID )
- self:T( ClientData.MissileData )
- end
- end
- end
- end
-
- if ShowMessages == true and self.MessagesOnOff == true and self.TrackingOnOff == true then -- Only do this when tracking information needs to be displayed.
-
- -- TRACKING PART
-
- -- For the current client, the missile range and bearing details are displayed To the Player Client.
- -- For the other clients, the missile range and bearing details are displayed To the other Player Clients.
- -- To achieve this, a cross loop is done for each Player Client <-> Other Player Client missile information.
-
- -- Main Player Client loop
- for ClientDataID, ClientData in pairs( self.TrackingMissiles ) do
-
- local Client = ClientData.Client
- self:T2( { Client:GetName() } )
-
-
- ClientData.MessageToClient = ""
- ClientData.MessageToAll = ""
-
- -- Other Players Client loop
- for TrackingDataID, TrackingData in pairs( self.TrackingMissiles ) do
-
- for MissileDataID, MissileData in pairs( TrackingData.MissileData ) do
- self:T3( MissileDataID )
-
- local TrainerSourceUnit = MissileData.TrainerSourceUnit
- local TrainerWeapon = MissileData.TrainerWeapon
- local TrainerTargetUnit = MissileData.TrainerTargetUnit
- local TrainerWeaponTypeName = MissileData.TrainerWeaponTypeName
- local TrainerWeaponLaunched = MissileData.TrainerWeaponLaunched
-
- if Client and Client:IsAlive() and TrainerSourceUnit and TrainerSourceUnit:IsAlive() and TrainerWeapon and TrainerWeapon:isExist() and TrainerTargetUnit and TrainerTargetUnit:IsAlive() then
-
- if ShowMessages == true then
- local TrackingTo
- TrackingTo = string.format( " -> %s",
- TrainerWeaponTypeName
- )
-
- if ClientDataID == TrackingDataID then
- if ClientData.MessageToClient == "" then
- ClientData.MessageToClient = "Missiles to You:\n"
- end
- ClientData.MessageToClient = ClientData.MessageToClient .. TrackingTo .. self:_AddRange( ClientData.Client, TrainerWeapon ) .. self:_AddBearing( ClientData.Client, TrainerWeapon ) .. "\n"
- else
- if self.TrackingToAll == true then
- if ClientData.MessageToAll == "" then
- ClientData.MessageToAll = "Missiles to other Players:\n"
- end
- ClientData.MessageToAll = ClientData.MessageToAll .. TrackingTo .. self:_AddRange( ClientData.Client, TrainerWeapon ) .. self:_AddBearing( ClientData.Client, TrainerWeapon ) .. " ( " .. TrainerTargetUnit:GetPlayerName() .. " )\n"
- end
- end
- end
- end
- end
- end
-
- -- Once the Player Client and the Other Player Client tracking messages are prepared, show them.
- if ClientData.MessageToClient ~= "" or ClientData.MessageToAll ~= "" then
- local Message = MESSAGE:New( ClientData.MessageToClient .. ClientData.MessageToAll, 1, "Tracking" ):ToClient( Client )
- end
- end
- end
-
- return true
-end
---- This module contains the AIRBASEPOLICE classes.
---
--- ===
---
--- 1) @{AirbasePolice#AIRBASEPOLICE_BASE} class, extends @{Base#BASE}
--- ==================================================================
--- The @{AirbasePolice#AIRBASEPOLICE_BASE} class provides the main methods to monitor CLIENT behaviour at airbases.
--- CLIENTS should not be allowed to:
---
--- * Don't taxi faster than 40 km/h.
--- * Don't take-off on taxiways.
--- * Avoid to hit other planes on the airbase.
--- * Obey ground control orders.
---
--- 2) @{AirbasePolice#AIRBASEPOLICE_CAUCASUS} class, extends @{AirbasePolice#AIRBASEPOLICE_BASE}
--- =============================================================================================
--- All the airbases on the caucasus map can be monitored using this class.
--- If you want to monitor specific airbases, you need to use the @{#AIRBASEPOLICE_BASE.Monitor}() method, which takes a table or airbase names.
--- The following names can be given:
--- * AnapaVityazevo
--- * Batumi
--- * Beslan
--- * Gelendzhik
--- * Gudauta
--- * Kobuleti
--- * KrasnodarCenter
--- * KrasnodarPashkovsky
--- * Krymsk
--- * Kutaisi
--- * MaykopKhanskaya
--- * MineralnyeVody
--- * Mozdok
--- * Nalchik
--- * Novorossiysk
--- * SenakiKolkhi
--- * SochiAdler
--- * Soganlug
--- * SukhumiBabushara
--- * TbilisiLochini
--- * Vaziani
---
--- 3) @{AirbasePolice#AIRBASEPOLICE_NEVADA} class, extends @{AirbasePolice#AIRBASEPOLICE_BASE}
--- =============================================================================================
--- All the airbases on the NEVADA map can be monitored using this class.
--- If you want to monitor specific airbases, you need to use the @{#AIRBASEPOLICE_BASE.Monitor}() method, which takes a table or airbase names.
--- The following names can be given:
--- * Nellis
--- * McCarran
--- * Creech
--- * Groom Lake
---
--- ### Contributions: Dutch Baron - Concept & Testing
--- ### Author: FlightControl - Framework Design & Programming
---
--- @module AirbasePolice
-
-
-
-
-
---- @type AIRBASEPOLICE_BASE
--- @field Core.Set#SET_CLIENT SetClient
--- @extends Core.Base#BASE
-
-AIRBASEPOLICE_BASE = {
- ClassName = "AIRBASEPOLICE_BASE",
- SetClient = nil,
- Airbases = nil,
- AirbaseNames = nil,
-}
-
-
---- Creates a new AIRBASEPOLICE_BASE object.
--- @param #AIRBASEPOLICE_BASE self
--- @param SetClient A SET_CLIENT object that will contain the CLIENT objects to be monitored if they follow the rules of the airbase.
--- @param Airbases A table of Airbase Names.
--- @return #AIRBASEPOLICE_BASE self
-function AIRBASEPOLICE_BASE:New( SetClient, Airbases )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, BASE:New() )
- self:E( { self.ClassName, SetClient, Airbases } )
-
- self.SetClient = SetClient
- self.Airbases = Airbases
-
- for AirbaseID, Airbase in pairs( self.Airbases ) do
- Airbase.ZoneBoundary = ZONE_POLYGON_BASE:New( "Boundary", Airbase.PointsBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- for PointsRunwayID, PointsRunway in pairs( Airbase.PointsRunways ) do
- Airbase.ZoneRunways[PointsRunwayID] = ZONE_POLYGON_BASE:New( "Runway " .. PointsRunwayID, PointsRunway ):SmokeZone(SMOKECOLOR.Red):Flush()
- end
- end
-
--- -- Template
--- local TemplateBoundary = GROUP:FindByName( "Template Boundary" )
--- self.Airbases.Template.ZoneBoundary = ZONE_POLYGON:New( "Template Boundary", TemplateBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
---
--- local TemplateRunway1 = GROUP:FindByName( "Template Runway 1" )
--- self.Airbases.Template.ZoneRunways[1] = ZONE_POLYGON:New( "Template Runway 1", TemplateRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
-
- self.SetClient:ForEachClient(
- --- @param Wrapper.Client#CLIENT Client
- function( Client )
- Client:SetState( self, "Speeding", false )
- Client:SetState( self, "Warnings", 0)
- Client:SetState( self, "Taxi", false )
- end
- )
-
- self.AirbaseMonitor = SCHEDULER:New( self, self._AirbaseMonitor, {}, 0, 2, 0.05 )
-
- return self
-end
-
---- @type AIRBASEPOLICE_BASE.AirbaseNames
--- @list <#string>
-
---- Monitor a table of airbase names.
--- @param #AIRBASEPOLICE_BASE self
--- @param #AIRBASEPOLICE_BASE.AirbaseNames AirbaseNames A list of AirbaseNames to monitor. If this parameters is nil, then all airbases will be monitored.
--- @return #AIRBASEPOLICE_BASE self
-function AIRBASEPOLICE_BASE:Monitor( AirbaseNames )
-
- if AirbaseNames then
- if type( AirbaseNames ) == "table" then
- self.AirbaseNames = AirbaseNames
- else
- self.AirbaseNames = { AirbaseNames }
- end
- end
-end
-
---- @param #AIRBASEPOLICE_BASE self
-function AIRBASEPOLICE_BASE:_AirbaseMonitor()
-
- for AirbaseID, Airbase in pairs( self.Airbases ) do
-
- if not self.AirbaseNames or self.AirbaseNames[AirbaseID] then
-
- self:E( AirbaseID )
-
- self.SetClient:ForEachClientInZone( Airbase.ZoneBoundary,
-
- --- @param Wrapper.Client#CLIENT Client
- function( Client )
-
- self:E( Client.UnitName )
- if Client:IsAlive() then
- local NotInRunwayZone = true
- for ZoneRunwayID, ZoneRunway in pairs( Airbase.ZoneRunways ) do
- NotInRunwayZone = ( Client:IsNotInZone( ZoneRunway ) == true ) and NotInRunwayZone or false
- end
-
- if NotInRunwayZone then
- local Taxi = self:GetState( self, "Taxi" )
- self:E( Taxi )
- if Taxi == false then
- Client:Message( "Welcome at " .. AirbaseID .. ". The maximum taxiing speed is " .. Airbase.MaximumSpeed " km/h.", 20, "ATC" )
- self:SetState( self, "Taxi", true )
- end
-
- -- TODO: GetVelocityKMH function usage
- local VelocityVec3 = Client:GetVelocity()
- local Velocity = ( VelocityVec3.x ^ 2 + VelocityVec3.y ^ 2 + VelocityVec3.z ^ 2 ) ^ 0.5 -- in meters / sec
- local Velocity = Velocity * 3.6 -- now it is in km/h.
- -- MESSAGE:New( "Velocity = " .. Velocity, 1 ):ToAll()
- local IsAboveRunway = Client:IsAboveRunway()
- local IsOnGround = Client:InAir() == false
- self:T( IsAboveRunway, IsOnGround )
-
- if IsAboveRunway and IsOnGround then
-
- if Velocity > Airbase.MaximumSpeed then
- local IsSpeeding = Client:GetState( self, "Speeding" )
-
- if IsSpeeding == true then
- local SpeedingWarnings = Client:GetState( self, "Warnings" )
- self:T( SpeedingWarnings )
-
- if SpeedingWarnings <= 3 then
- Client:Message( "You are speeding on the taxiway! Slow down or you will be removed from this airbase! Your current velocity is " .. string.format( "%2.0f km/h", Velocity ), 5, "Warning " .. SpeedingWarnings .. " / 3" )
- Client:SetState( self, "Warnings", SpeedingWarnings + 1 )
- else
- MESSAGE:New( "Player " .. Client:GetPlayerName() .. " has been removed from the airbase, due to a speeding violation ...", 10, "Airbase Police" ):ToAll()
- Client:Destroy()
- trigger.action.setUserFlag( "AIRCRAFT_"..Client:GetID(), 100)
- Client:SetState( self, "Speeding", false )
- Client:SetState( self, "Warnings", 0 )
- end
-
- else
- Client:Message( "You are speeding on the taxiway, slow down now! Your current velocity is " .. string.format( "%2.0f km/h", Velocity ), 5, "Attention! " )
- Client:SetState( self, "Speeding", true )
- Client:SetState( self, "Warnings", 1 )
- end
-
- else
- Client:SetState( self, "Speeding", false )
- Client:SetState( self, "Warnings", 0 )
- end
- end
-
- else
- Client:SetState( self, "Speeding", false )
- Client:SetState( self, "Warnings", 0 )
- local Taxi = self:GetState( self, "Taxi" )
- if Taxi == true then
- Client:Message( "You have progressed to the runway ... Await take-off clearance ...", 20, "ATC" )
- self:SetState( self, "Taxi", false )
- end
- end
- end
- end
- )
- end
- end
-
- return true
-end
-
-
---- @type AIRBASEPOLICE_CAUCASUS
--- @field Core.Set#SET_CLIENT SetClient
--- @extends #AIRBASEPOLICE_BASE
-
-AIRBASEPOLICE_CAUCASUS = {
- ClassName = "AIRBASEPOLICE_CAUCASUS",
- Airbases = {
- AnapaVityazevo = {
- PointsBoundary = {
- [1]={["y"]=242234.85714287,["x"]=-6616.5714285726,},
- [2]={["y"]=241060.57142858,["x"]=-5585.142857144,},
- [3]={["y"]=243806.2857143,["x"]=-3962.2857142868,},
- [4]={["y"]=245240.57142858,["x"]=-4816.5714285726,},
- [5]={["y"]=244783.42857144,["x"]=-5630.8571428583,},
- [6]={["y"]=243800.57142858,["x"]=-5065.142857144,},
- [7]={["y"]=242232.00000001,["x"]=-6622.2857142868,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=242140.57142858,["x"]=-6478.8571428583,},
- [2]={["y"]=242188.57142858,["x"]=-6522.0000000011,},
- [3]={["y"]=244124.2857143,["x"]=-4344.0000000011,},
- [4]={["y"]=244068.2857143,["x"]=-4296.5714285726,},
- [5]={["y"]=242140.57142858,["x"]=-6480.0000000011,}
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Batumi = {
- PointsBoundary = {
- [1]={["y"]=617567.14285714,["x"]=-355313.14285715,},
- [2]={["y"]=616181.42857142,["x"]=-354800.28571429,},
- [3]={["y"]=616007.14285714,["x"]=-355128.85714286,},
- [4]={["y"]=618230,["x"]=-356914.57142858,},
- [5]={["y"]=618727.14285714,["x"]=-356166,},
- [6]={["y"]=617572.85714285,["x"]=-355308.85714286,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=616442.28571429,["x"]=-355090.28571429,},
- [2]={["y"]=618450.57142857,["x"]=-356522,},
- [3]={["y"]=618407.71428571,["x"]=-356584.85714286,},
- [4]={["y"]=618361.99999999,["x"]=-356554.85714286,},
- [5]={["y"]=618324.85714285,["x"]=-356599.14285715,},
- [6]={["y"]=618250.57142856,["x"]=-356543.42857143,},
- [7]={["y"]=618257.7142857,["x"]=-356496.28571429,},
- [8]={["y"]=618237.7142857,["x"]=-356459.14285715,},
- [9]={["y"]=616555.71428571,["x"]=-355258.85714286,},
- [10]={["y"]=616486.28571428,["x"]=-355280.57142858,},
- [11]={["y"]=616410.57142856,["x"]=-355227.71428572,},
- [12]={["y"]=616441.99999999,["x"]=-355179.14285715,},
- [13]={["y"]=616401.99999999,["x"]=-355147.71428572,},
- [14]={["y"]=616441.42857142,["x"]=-355092.57142858,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Beslan = {
- PointsBoundary = {
- [1]={["y"]=842082.57142857,["x"]=-148445.14285715,},
- [2]={["y"]=845237.71428572,["x"]=-148639.71428572,},
- [3]={["y"]=845232,["x"]=-148765.42857143,},
- [4]={["y"]=844220.57142857,["x"]=-149168.28571429,},
- [5]={["y"]=843274.85714286,["x"]=-149125.42857143,},
- [6]={["y"]=842077.71428572,["x"]=-148554,},
- [7]={["y"]=842083.42857143,["x"]=-148445.42857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=842104.57142857,["x"]=-148460.57142857,},
- [2]={["y"]=845225.71428572,["x"]=-148656,},
- [3]={["y"]=845220.57142858,["x"]=-148750,},
- [4]={["y"]=842098.85714286,["x"]=-148556.28571429,},
- [5]={["y"]=842104,["x"]=-148460.28571429,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Gelendzhik = {
- PointsBoundary = {
- [1]={["y"]=297856.00000001,["x"]=-51151.428571429,},
- [2]={["y"]=299044.57142858,["x"]=-49720.000000001,},
- [3]={["y"]=298861.71428572,["x"]=-49580.000000001,},
- [4]={["y"]=298198.85714286,["x"]=-49842.857142858,},
- [5]={["y"]=297990.28571429,["x"]=-50151.428571429,},
- [6]={["y"]=297696.00000001,["x"]=-51054.285714286,},
- [7]={["y"]=297850.28571429,["x"]=-51160.000000001,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=297834.00000001,["x"]=-51107.428571429,},
- [2]={["y"]=297786.57142858,["x"]=-51068.857142858,},
- [3]={["y"]=298946.57142858,["x"]=-49686.000000001,},
- [4]={["y"]=298993.14285715,["x"]=-49725.714285715,},
- [5]={["y"]=297835.14285715,["x"]=-51107.714285715,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Gudauta = {
- PointsBoundary = {
- [1]={["y"]=517246.57142857,["x"]=-197850.28571429,},
- [2]={["y"]=516749.42857142,["x"]=-198070.28571429,},
- [3]={["y"]=515755.14285714,["x"]=-197598.85714286,},
- [4]={["y"]=515369.42857142,["x"]=-196538.85714286,},
- [5]={["y"]=515623.71428571,["x"]=-195618.85714286,},
- [6]={["y"]=515946.57142857,["x"]=-195510.28571429,},
- [7]={["y"]=517243.71428571,["x"]=-197858.85714286,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=517096.57142857,["x"]=-197804.57142857,},
- [2]={["y"]=515880.85714285,["x"]=-195590.28571429,},
- [3]={["y"]=515812.28571428,["x"]=-195628.85714286,},
- [4]={["y"]=517036.57142857,["x"]=-197834.57142857,},
- [5]={["y"]=517097.99999999,["x"]=-197807.42857143,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Kobuleti = {
- PointsBoundary = {
- [1]={["y"]=634427.71428571,["x"]=-318290.28571429,},
- [2]={["y"]=635033.42857143,["x"]=-317550.2857143,},
- [3]={["y"]=635864.85714286,["x"]=-317333.14285715,},
- [4]={["y"]=636967.71428571,["x"]=-317261.71428572,},
- [5]={["y"]=637144.85714286,["x"]=-317913.14285715,},
- [6]={["y"]=634630.57142857,["x"]=-318687.42857144,},
- [7]={["y"]=634424.85714286,["x"]=-318290.2857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=634509.71428571,["x"]=-318339.42857144,},
- [2]={["y"]=636767.42857143,["x"]=-317516.57142858,},
- [3]={["y"]=636790,["x"]=-317575.71428572,},
- [4]={["y"]=634531.42857143,["x"]=-318398.00000001,},
- [5]={["y"]=634510.28571429,["x"]=-318339.71428572,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- KrasnodarCenter = {
- PointsBoundary = {
- [1]={["y"]=366680.28571429,["x"]=11699.142857142,},
- [2]={["y"]=366654.28571429,["x"]=11225.142857142,},
- [3]={["y"]=367497.14285715,["x"]=11082.285714285,},
- [4]={["y"]=368025.71428572,["x"]=10396.57142857,},
- [5]={["y"]=369854.28571429,["x"]=11367.999999999,},
- [6]={["y"]=369840.00000001,["x"]=11910.857142856,},
- [7]={["y"]=366682.57142858,["x"]=11697.999999999,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=369205.42857144,["x"]=11789.142857142,},
- [2]={["y"]=369209.71428572,["x"]=11714.857142856,},
- [3]={["y"]=366699.71428572,["x"]=11581.714285713,},
- [4]={["y"]=366698.28571429,["x"]=11659.142857142,},
- [5]={["y"]=369208.85714286,["x"]=11788.57142857,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- KrasnodarPashkovsky = {
- PointsBoundary = {
- [1]={["y"]=386754,["x"]=6476.5714285703,},
- [2]={["y"]=389182.57142858,["x"]=8722.2857142846,},
- [3]={["y"]=388832.57142858,["x"]=9086.5714285703,},
- [4]={["y"]=386961.14285715,["x"]=7707.9999999989,},
- [5]={["y"]=385404,["x"]=9179.4285714274,},
- [6]={["y"]=383239.71428572,["x"]=7386.5714285703,},
- [7]={["y"]=383954,["x"]=6486.5714285703,},
- [8]={["y"]=385775.42857143,["x"]=8097.9999999989,},
- [9]={["y"]=386804,["x"]=7319.4285714274,},
- [10]={["y"]=386375.42857143,["x"]=6797.9999999989,},
- [11]={["y"]=386746.85714286,["x"]=6472.2857142846,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=385891.14285715,["x"]=8416.5714285703,},
- [2]={["y"]=385842.28571429,["x"]=8467.9999999989,},
- [3]={["y"]=384180.85714286,["x"]=6917.1428571417,},
- [4]={["y"]=384228.57142858,["x"]=6867.7142857132,},
- [5]={["y"]=385891.14285715,["x"]=8416.5714285703,},
- },
- [2] = {
- [1]={["y"]=386714.85714286,["x"]=6674.857142856,},
- [2]={["y"]=386757.71428572,["x"]=6627.7142857132,},
- [3]={["y"]=389028.57142858,["x"]=8741.4285714275,},
- [4]={["y"]=388981.71428572,["x"]=8790.5714285703,},
- [5]={["y"]=386714.57142858,["x"]=6674.5714285703,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Krymsk = {
- PointsBoundary = {
- [1]={["y"]=293338.00000001,["x"]=-7575.4285714297,},
- [2]={["y"]=295199.42857144,["x"]=-5434.0000000011,},
- [3]={["y"]=295595.14285715,["x"]=-6239.7142857154,},
- [4]={["y"]=294152.2857143,["x"]=-8325.4285714297,},
- [5]={["y"]=293345.14285715,["x"]=-7596.8571428582,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=293522.00000001,["x"]=-7567.4285714297,},
- [2]={["y"]=293578.57142858,["x"]=-7616.0000000011,},
- [3]={["y"]=295246.00000001,["x"]=-5591.142857144,},
- [4]={["y"]=295187.71428573,["x"]=-5546.0000000011,},
- [5]={["y"]=293523.14285715,["x"]=-7568.2857142868,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Kutaisi = {
- PointsBoundary = {
- [1]={["y"]=682087.42857143,["x"]=-284512.85714286,},
- [2]={["y"]=685387.42857143,["x"]=-283662.85714286,},
- [3]={["y"]=685294.57142857,["x"]=-284977.14285715,},
- [4]={["y"]=682744.57142857,["x"]=-286505.71428572,},
- [5]={["y"]=682094.57142857,["x"]=-284527.14285715,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=682638,["x"]=-285202.28571429,},
- [2]={["y"]=685050.28571429,["x"]=-284507.42857144,},
- [3]={["y"]=685068.85714286,["x"]=-284578.85714286,},
- [4]={["y"]=682657.42857143,["x"]=-285264.28571429,},
- [5]={["y"]=682638.28571429,["x"]=-285202.85714286,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- MaykopKhanskaya = {
- PointsBoundary = {
- [1]={["y"]=456876.28571429,["x"]=-27665.42857143,},
- [2]={["y"]=457800,["x"]=-28392.857142858,},
- [3]={["y"]=459368.57142857,["x"]=-26378.571428573,},
- [4]={["y"]=459425.71428572,["x"]=-25242.857142858,},
- [5]={["y"]=458961.42857143,["x"]=-24964.285714287,},
- [6]={["y"]=456878.57142857,["x"]=-27667.714285715,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=457005.42857143,["x"]=-27668.000000001,},
- [2]={["y"]=459028.85714286,["x"]=-25168.857142858,},
- [3]={["y"]=459082.57142857,["x"]=-25216.857142858,},
- [4]={["y"]=457060,["x"]=-27714.285714287,},
- [5]={["y"]=457004.57142857,["x"]=-27669.714285715,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- MineralnyeVody = {
- PointsBoundary = {
- [1]={["y"]=703857.14285714,["x"]=-50226.000000002,},
- [2]={["y"]=707385.71428571,["x"]=-51911.714285716,},
- [3]={["y"]=707595.71428571,["x"]=-51434.857142859,},
- [4]={["y"]=707900,["x"]=-51568.857142859,},
- [5]={["y"]=707542.85714286,["x"]=-52326.000000002,},
- [6]={["y"]=706628.57142857,["x"]=-52568.857142859,},
- [7]={["y"]=705142.85714286,["x"]=-51790.285714288,},
- [8]={["y"]=703678.57142857,["x"]=-50611.714285716,},
- [9]={["y"]=703857.42857143,["x"]=-50226.857142859,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=703904,["x"]=-50352.571428573,},
- [2]={["y"]=707596.28571429,["x"]=-52094.571428573,},
- [3]={["y"]=707560.57142858,["x"]=-52161.714285716,},
- [4]={["y"]=703871.71428572,["x"]=-50420.571428573,},
- [5]={["y"]=703902,["x"]=-50352.000000002,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Mozdok = {
- PointsBoundary = {
- [1]={["y"]=832123.42857143,["x"]=-83608.571428573,},
- [2]={["y"]=835916.28571429,["x"]=-83144.285714288,},
- [3]={["y"]=835474.28571429,["x"]=-84170.571428573,},
- [4]={["y"]=832911.42857143,["x"]=-84470.571428573,},
- [5]={["y"]=832487.71428572,["x"]=-85565.714285716,},
- [6]={["y"]=831573.42857143,["x"]=-85351.42857143,},
- [7]={["y"]=832123.71428572,["x"]=-83610.285714288,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=832201.14285715,["x"]=-83699.428571431,},
- [2]={["y"]=832212.57142857,["x"]=-83780.571428574,},
- [3]={["y"]=835730.28571429,["x"]=-83335.714285717,},
- [4]={["y"]=835718.85714286,["x"]=-83246.571428574,},
- [5]={["y"]=832200.57142857,["x"]=-83700.000000002,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Nalchik = {
- PointsBoundary = {
- [1]={["y"]=759370,["x"]=-125502.85714286,},
- [2]={["y"]=761384.28571429,["x"]=-124177.14285714,},
- [3]={["y"]=761472.85714286,["x"]=-124325.71428572,},
- [4]={["y"]=761092.85714286,["x"]=-125048.57142857,},
- [5]={["y"]=760295.71428572,["x"]=-125685.71428572,},
- [6]={["y"]=759444.28571429,["x"]=-125734.28571429,},
- [7]={["y"]=759375.71428572,["x"]=-125511.42857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=759454.28571429,["x"]=-125551.42857143,},
- [2]={["y"]=759492.85714286,["x"]=-125610.85714286,},
- [3]={["y"]=761406.28571429,["x"]=-124304.28571429,},
- [4]={["y"]=761361.14285714,["x"]=-124239.71428572,},
- [5]={["y"]=759456,["x"]=-125552.57142857,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Novorossiysk = {
- PointsBoundary = {
- [1]={["y"]=278677.71428573,["x"]=-41656.571428572,},
- [2]={["y"]=278446.2857143,["x"]=-41453.714285715,},
- [3]={["y"]=278989.14285716,["x"]=-40188.000000001,},
- [4]={["y"]=279717.71428573,["x"]=-39968.000000001,},
- [5]={["y"]=280020.57142859,["x"]=-40208.000000001,},
- [6]={["y"]=278674.85714287,["x"]=-41660.857142858,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=278673.14285716,["x"]=-41615.142857144,},
- [2]={["y"]=278625.42857144,["x"]=-41570.571428572,},
- [3]={["y"]=279835.42857144,["x"]=-40226.000000001,},
- [4]={["y"]=279882.2857143,["x"]=-40270.000000001,},
- [5]={["y"]=278672.00000001,["x"]=-41614.857142858,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- SenakiKolkhi = {
- PointsBoundary = {
- [1]={["y"]=646036.57142857,["x"]=-281778.85714286,},
- [2]={["y"]=646045.14285714,["x"]=-281191.71428571,},
- [3]={["y"]=647032.28571429,["x"]=-280598.85714285,},
- [4]={["y"]=647669.42857143,["x"]=-281273.14285714,},
- [5]={["y"]=648323.71428571,["x"]=-281370.28571428,},
- [6]={["y"]=648520.85714286,["x"]=-281978.85714285,},
- [7]={["y"]=646039.42857143,["x"]=-281783.14285714,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=646060.85714285,["x"]=-281736,},
- [2]={["y"]=646056.57142857,["x"]=-281631.71428571,},
- [3]={["y"]=648442.28571428,["x"]=-281840.28571428,},
- [4]={["y"]=648432.28571428,["x"]=-281918.85714286,},
- [5]={["y"]=646063.71428571,["x"]=-281738.85714286,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- SochiAdler = {
- PointsBoundary = {
- [1]={["y"]=460642.28571428,["x"]=-164861.71428571,},
- [2]={["y"]=462820.85714285,["x"]=-163368.85714286,},
- [3]={["y"]=463649.42857142,["x"]=-163340.28571429,},
- [4]={["y"]=463835.14285714,["x"]=-164040.28571429,},
- [5]={["y"]=462535.14285714,["x"]=-165654.57142857,},
- [6]={["y"]=460678,["x"]=-165247.42857143,},
- [7]={["y"]=460635.14285714,["x"]=-164876,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=460831.42857143,["x"]=-165180,},
- [2]={["y"]=460878.57142857,["x"]=-165257.14285714,},
- [3]={["y"]=463663.71428571,["x"]=-163793.14285714,},
- [4]={["y"]=463612.28571428,["x"]=-163697.42857143,},
- [5]={["y"]=460831.42857143,["x"]=-165177.14285714,},
- },
- [2] = {
- [1]={["y"]=460831.42857143,["x"]=-165180,},
- [2]={["y"]=460878.57142857,["x"]=-165257.14285714,},
- [3]={["y"]=463663.71428571,["x"]=-163793.14285714,},
- [4]={["y"]=463612.28571428,["x"]=-163697.42857143,},
- [5]={["y"]=460831.42857143,["x"]=-165177.14285714,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Soganlug = {
- PointsBoundary = {
- [1]={["y"]=894530.85714286,["x"]=-316928.28571428,},
- [2]={["y"]=896422.28571428,["x"]=-318622.57142857,},
- [3]={["y"]=896090.85714286,["x"]=-318934,},
- [4]={["y"]=894019.42857143,["x"]=-317119.71428571,},
- [5]={["y"]=894533.71428571,["x"]=-316925.42857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=894525.71428571,["x"]=-316964,},
- [2]={["y"]=896363.14285714,["x"]=-318634.28571428,},
- [3]={["y"]=896299.14285714,["x"]=-318702.85714286,},
- [4]={["y"]=894464,["x"]=-317031.71428571,},
- [5]={["y"]=894524.57142857,["x"]=-316963.71428571,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- SukhumiBabushara = {
- PointsBoundary = {
- [1]={["y"]=562541.14285714,["x"]=-219852.28571429,},
- [2]={["y"]=562691.14285714,["x"]=-219395.14285714,},
- [3]={["y"]=564326.85714286,["x"]=-219523.71428571,},
- [4]={["y"]=566262.57142857,["x"]=-221166.57142857,},
- [5]={["y"]=566069.71428571,["x"]=-221580.85714286,},
- [6]={["y"]=562534,["x"]=-219873.71428571,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=562684,["x"]=-219779.71428571,},
- [2]={["y"]=562717.71428571,["x"]=-219718,},
- [3]={["y"]=566046.85714286,["x"]=-221376.57142857,},
- [4]={["y"]=566012.28571428,["x"]=-221446.57142857,},
- [5]={["y"]=562684.57142857,["x"]=-219782.57142857,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- TbilisiLochini = {
- PointsBoundary = {
- [1]={["y"]=895172.85714286,["x"]=-314667.42857143,},
- [2]={["y"]=895337.42857143,["x"]=-314143.14285714,},
- [3]={["y"]=895990.28571429,["x"]=-314036,},
- [4]={["y"]=897730.28571429,["x"]=-315284.57142857,},
- [5]={["y"]=897901.71428571,["x"]=-316284.57142857,},
- [6]={["y"]=897684.57142857,["x"]=-316618.85714286,},
- [7]={["y"]=895173.14285714,["x"]=-314667.42857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=895261.14285715,["x"]=-314652.28571428,},
- [2]={["y"]=897654.57142857,["x"]=-316523.14285714,},
- [3]={["y"]=897711.71428571,["x"]=-316450.28571429,},
- [4]={["y"]=895327.42857143,["x"]=-314568.85714286,},
- [5]={["y"]=895261.71428572,["x"]=-314656,},
- },
- [2] = {
- [1]={["y"]=895605.71428572,["x"]=-314724.57142857,},
- [2]={["y"]=897639.71428572,["x"]=-316148,},
- [3]={["y"]=897683.42857143,["x"]=-316087.14285714,},
- [4]={["y"]=895650,["x"]=-314660,},
- [5]={["y"]=895606,["x"]=-314724.85714286,}
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Vaziani = {
- PointsBoundary = {
- [1]={["y"]=902122,["x"]=-318163.71428572,},
- [2]={["y"]=902678.57142857,["x"]=-317594,},
- [3]={["y"]=903275.71428571,["x"]=-317405.42857143,},
- [4]={["y"]=903418.57142857,["x"]=-317891.14285714,},
- [5]={["y"]=904292.85714286,["x"]=-318748.28571429,},
- [6]={["y"]=904542,["x"]=-319740.85714286,},
- [7]={["y"]=904042,["x"]=-320166.57142857,},
- [8]={["y"]=902121.42857143,["x"]=-318164.85714286,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=902239.14285714,["x"]=-318190.85714286,},
- [2]={["y"]=904014.28571428,["x"]=-319994.57142857,},
- [3]={["y"]=904064.85714285,["x"]=-319945.14285715,},
- [4]={["y"]=902294.57142857,["x"]=-318146,},
- [5]={["y"]=902247.71428571,["x"]=-318190.85714286,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- },
-}
-
---- Creates a new AIRBASEPOLICE_CAUCASUS object.
--- @param #AIRBASEPOLICE_CAUCASUS self
--- @param SetClient A SET_CLIENT object that will contain the CLIENT objects to be monitored if they follow the rules of the airbase.
--- @return #AIRBASEPOLICE_CAUCASUS self
-function AIRBASEPOLICE_CAUCASUS:New( SetClient )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, AIRBASEPOLICE_BASE:New( SetClient, self.Airbases ) )
-
- -- -- AnapaVityazevo
- -- local AnapaVityazevoBoundary = GROUP:FindByName( "AnapaVityazevo Boundary" )
- -- self.Airbases.AnapaVityazevo.ZoneBoundary = ZONE_POLYGON:New( "AnapaVityazevo Boundary", AnapaVityazevoBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local AnapaVityazevoRunway1 = GROUP:FindByName( "AnapaVityazevo Runway 1" )
- -- self.Airbases.AnapaVityazevo.ZoneRunways[1] = ZONE_POLYGON:New( "AnapaVityazevo Runway 1", AnapaVityazevoRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Batumi
- -- local BatumiBoundary = GROUP:FindByName( "Batumi Boundary" )
- -- self.Airbases.Batumi.ZoneBoundary = ZONE_POLYGON:New( "Batumi Boundary", BatumiBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local BatumiRunway1 = GROUP:FindByName( "Batumi Runway 1" )
- -- self.Airbases.Batumi.ZoneRunways[1] = ZONE_POLYGON:New( "Batumi Runway 1", BatumiRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Beslan
- -- local BeslanBoundary = GROUP:FindByName( "Beslan Boundary" )
- -- self.Airbases.Beslan.ZoneBoundary = ZONE_POLYGON:New( "Beslan Boundary", BeslanBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local BeslanRunway1 = GROUP:FindByName( "Beslan Runway 1" )
- -- self.Airbases.Beslan.ZoneRunways[1] = ZONE_POLYGON:New( "Beslan Runway 1", BeslanRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Gelendzhik
- -- local GelendzhikBoundary = GROUP:FindByName( "Gelendzhik Boundary" )
- -- self.Airbases.Gelendzhik.ZoneBoundary = ZONE_POLYGON:New( "Gelendzhik Boundary", GelendzhikBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local GelendzhikRunway1 = GROUP:FindByName( "Gelendzhik Runway 1" )
- -- self.Airbases.Gelendzhik.ZoneRunways[1] = ZONE_POLYGON:New( "Gelendzhik Runway 1", GelendzhikRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Gudauta
- -- local GudautaBoundary = GROUP:FindByName( "Gudauta Boundary" )
- -- self.Airbases.Gudauta.ZoneBoundary = ZONE_POLYGON:New( "Gudauta Boundary", GudautaBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local GudautaRunway1 = GROUP:FindByName( "Gudauta Runway 1" )
- -- self.Airbases.Gudauta.ZoneRunways[1] = ZONE_POLYGON:New( "Gudauta Runway 1", GudautaRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Kobuleti
- -- local KobuletiBoundary = GROUP:FindByName( "Kobuleti Boundary" )
- -- self.Airbases.Kobuleti.ZoneBoundary = ZONE_POLYGON:New( "Kobuleti Boundary", KobuletiBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local KobuletiRunway1 = GROUP:FindByName( "Kobuleti Runway 1" )
- -- self.Airbases.Kobuleti.ZoneRunways[1] = ZONE_POLYGON:New( "Kobuleti Runway 1", KobuletiRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- KrasnodarCenter
- -- local KrasnodarCenterBoundary = GROUP:FindByName( "KrasnodarCenter Boundary" )
- -- self.Airbases.KrasnodarCenter.ZoneBoundary = ZONE_POLYGON:New( "KrasnodarCenter Boundary", KrasnodarCenterBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local KrasnodarCenterRunway1 = GROUP:FindByName( "KrasnodarCenter Runway 1" )
- -- self.Airbases.KrasnodarCenter.ZoneRunways[1] = ZONE_POLYGON:New( "KrasnodarCenter Runway 1", KrasnodarCenterRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- KrasnodarPashkovsky
- -- local KrasnodarPashkovskyBoundary = GROUP:FindByName( "KrasnodarPashkovsky Boundary" )
- -- self.Airbases.KrasnodarPashkovsky.ZoneBoundary = ZONE_POLYGON:New( "KrasnodarPashkovsky Boundary", KrasnodarPashkovskyBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local KrasnodarPashkovskyRunway1 = GROUP:FindByName( "KrasnodarPashkovsky Runway 1" )
- -- self.Airbases.KrasnodarPashkovsky.ZoneRunways[1] = ZONE_POLYGON:New( "KrasnodarPashkovsky Runway 1", KrasnodarPashkovskyRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- -- local KrasnodarPashkovskyRunway2 = GROUP:FindByName( "KrasnodarPashkovsky Runway 2" )
- -- self.Airbases.KrasnodarPashkovsky.ZoneRunways[2] = ZONE_POLYGON:New( "KrasnodarPashkovsky Runway 2", KrasnodarPashkovskyRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Krymsk
- -- local KrymskBoundary = GROUP:FindByName( "Krymsk Boundary" )
- -- self.Airbases.Krymsk.ZoneBoundary = ZONE_POLYGON:New( "Krymsk Boundary", KrymskBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local KrymskRunway1 = GROUP:FindByName( "Krymsk Runway 1" )
- -- self.Airbases.Krymsk.ZoneRunways[1] = ZONE_POLYGON:New( "Krymsk Runway 1", KrymskRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Kutaisi
- -- local KutaisiBoundary = GROUP:FindByName( "Kutaisi Boundary" )
- -- self.Airbases.Kutaisi.ZoneBoundary = ZONE_POLYGON:New( "Kutaisi Boundary", KutaisiBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local KutaisiRunway1 = GROUP:FindByName( "Kutaisi Runway 1" )
- -- self.Airbases.Kutaisi.ZoneRunways[1] = ZONE_POLYGON:New( "Kutaisi Runway 1", KutaisiRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- MaykopKhanskaya
- -- local MaykopKhanskayaBoundary = GROUP:FindByName( "MaykopKhanskaya Boundary" )
- -- self.Airbases.MaykopKhanskaya.ZoneBoundary = ZONE_POLYGON:New( "MaykopKhanskaya Boundary", MaykopKhanskayaBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local MaykopKhanskayaRunway1 = GROUP:FindByName( "MaykopKhanskaya Runway 1" )
- -- self.Airbases.MaykopKhanskaya.ZoneRunways[1] = ZONE_POLYGON:New( "MaykopKhanskaya Runway 1", MaykopKhanskayaRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- MineralnyeVody
- -- local MineralnyeVodyBoundary = GROUP:FindByName( "MineralnyeVody Boundary" )
- -- self.Airbases.MineralnyeVody.ZoneBoundary = ZONE_POLYGON:New( "MineralnyeVody Boundary", MineralnyeVodyBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local MineralnyeVodyRunway1 = GROUP:FindByName( "MineralnyeVody Runway 1" )
- -- self.Airbases.MineralnyeVody.ZoneRunways[1] = ZONE_POLYGON:New( "MineralnyeVody Runway 1", MineralnyeVodyRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Mozdok
- -- local MozdokBoundary = GROUP:FindByName( "Mozdok Boundary" )
- -- self.Airbases.Mozdok.ZoneBoundary = ZONE_POLYGON:New( "Mozdok Boundary", MozdokBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local MozdokRunway1 = GROUP:FindByName( "Mozdok Runway 1" )
- -- self.Airbases.Mozdok.ZoneRunways[1] = ZONE_POLYGON:New( "Mozdok Runway 1", MozdokRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Nalchik
- -- local NalchikBoundary = GROUP:FindByName( "Nalchik Boundary" )
- -- self.Airbases.Nalchik.ZoneBoundary = ZONE_POLYGON:New( "Nalchik Boundary", NalchikBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local NalchikRunway1 = GROUP:FindByName( "Nalchik Runway 1" )
- -- self.Airbases.Nalchik.ZoneRunways[1] = ZONE_POLYGON:New( "Nalchik Runway 1", NalchikRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Novorossiysk
- -- local NovorossiyskBoundary = GROUP:FindByName( "Novorossiysk Boundary" )
- -- self.Airbases.Novorossiysk.ZoneBoundary = ZONE_POLYGON:New( "Novorossiysk Boundary", NovorossiyskBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local NovorossiyskRunway1 = GROUP:FindByName( "Novorossiysk Runway 1" )
- -- self.Airbases.Novorossiysk.ZoneRunways[1] = ZONE_POLYGON:New( "Novorossiysk Runway 1", NovorossiyskRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- SenakiKolkhi
- -- local SenakiKolkhiBoundary = GROUP:FindByName( "SenakiKolkhi Boundary" )
- -- self.Airbases.SenakiKolkhi.ZoneBoundary = ZONE_POLYGON:New( "SenakiKolkhi Boundary", SenakiKolkhiBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local SenakiKolkhiRunway1 = GROUP:FindByName( "SenakiKolkhi Runway 1" )
- -- self.Airbases.SenakiKolkhi.ZoneRunways[1] = ZONE_POLYGON:New( "SenakiKolkhi Runway 1", SenakiKolkhiRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- SochiAdler
- -- local SochiAdlerBoundary = GROUP:FindByName( "SochiAdler Boundary" )
- -- self.Airbases.SochiAdler.ZoneBoundary = ZONE_POLYGON:New( "SochiAdler Boundary", SochiAdlerBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local SochiAdlerRunway1 = GROUP:FindByName( "SochiAdler Runway 1" )
- -- self.Airbases.SochiAdler.ZoneRunways[1] = ZONE_POLYGON:New( "SochiAdler Runway 1", SochiAdlerRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- -- local SochiAdlerRunway2 = GROUP:FindByName( "SochiAdler Runway 2" )
- -- self.Airbases.SochiAdler.ZoneRunways[2] = ZONE_POLYGON:New( "SochiAdler Runway 2", SochiAdlerRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Soganlug
- -- local SoganlugBoundary = GROUP:FindByName( "Soganlug Boundary" )
- -- self.Airbases.Soganlug.ZoneBoundary = ZONE_POLYGON:New( "Soganlug Boundary", SoganlugBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local SoganlugRunway1 = GROUP:FindByName( "Soganlug Runway 1" )
- -- self.Airbases.Soganlug.ZoneRunways[1] = ZONE_POLYGON:New( "Soganlug Runway 1", SoganlugRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- SukhumiBabushara
- -- local SukhumiBabusharaBoundary = GROUP:FindByName( "SukhumiBabushara Boundary" )
- -- self.Airbases.SukhumiBabushara.ZoneBoundary = ZONE_POLYGON:New( "SukhumiBabushara Boundary", SukhumiBabusharaBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local SukhumiBabusharaRunway1 = GROUP:FindByName( "SukhumiBabushara Runway 1" )
- -- self.Airbases.SukhumiBabushara.ZoneRunways[1] = ZONE_POLYGON:New( "SukhumiBabushara Runway 1", SukhumiBabusharaRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- TbilisiLochini
- -- local TbilisiLochiniBoundary = GROUP:FindByName( "TbilisiLochini Boundary" )
- -- self.Airbases.TbilisiLochini.ZoneBoundary = ZONE_POLYGON:New( "TbilisiLochini Boundary", TbilisiLochiniBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local TbilisiLochiniRunway1 = GROUP:FindByName( "TbilisiLochini Runway 1" )
- -- self.Airbases.TbilisiLochini.ZoneRunways[1] = ZONE_POLYGON:New( "TbilisiLochini Runway 1", TbilisiLochiniRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- -- local TbilisiLochiniRunway2 = GROUP:FindByName( "TbilisiLochini Runway 2" )
- -- self.Airbases.TbilisiLochini.ZoneRunways[2] = ZONE_POLYGON:New( "TbilisiLochini Runway 2", TbilisiLochiniRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
- -- -- Vaziani
- -- local VazianiBoundary = GROUP:FindByName( "Vaziani Boundary" )
- -- self.Airbases.Vaziani.ZoneBoundary = ZONE_POLYGON:New( "Vaziani Boundary", VazianiBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local VazianiRunway1 = GROUP:FindByName( "Vaziani Runway 1" )
- -- self.Airbases.Vaziani.ZoneRunways[1] = ZONE_POLYGON:New( "Vaziani Runway 1", VazianiRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
- --
- --
- --
-
-
- -- Template
- -- local TemplateBoundary = GROUP:FindByName( "Template Boundary" )
- -- self.Airbases.Template.ZoneBoundary = ZONE_POLYGON:New( "Template Boundary", TemplateBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
- --
- -- local TemplateRunway1 = GROUP:FindByName( "Template Runway 1" )
- -- self.Airbases.Template.ZoneRunways[1] = ZONE_POLYGON:New( "Template Runway 1", TemplateRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
-
- return self
-
-end
-
-
-
-
---- @type AIRBASEPOLICE_NEVADA
--- @extends Functional.AirbasePolice#AIRBASEPOLICE_BASE
-AIRBASEPOLICE_NEVADA = {
- ClassName = "AIRBASEPOLICE_NEVADA",
- Airbases = {
- Nellis = {
- PointsBoundary = {
- [1]={["y"]=-17814.714285714,["x"]=-399823.14285714,},
- [2]={["y"]=-16875.857142857,["x"]=-398763.14285714,},
- [3]={["y"]=-16251.571428571,["x"]=-398988.85714286,},
- [4]={["y"]=-16163,["x"]=-398693.14285714,},
- [5]={["y"]=-16328.714285714,["x"]=-398034.57142857,},
- [6]={["y"]=-15943,["x"]=-397571.71428571,},
- [7]={["y"]=-15711.571428571,["x"]=-397551.71428571,},
- [8]={["y"]=-15748.714285714,["x"]=-396806,},
- [9]={["y"]=-16288.714285714,["x"]=-396517.42857143,},
- [10]={["y"]=-16751.571428571,["x"]=-396308.85714286,},
- [11]={["y"]=-17263,["x"]=-396234.57142857,},
- [12]={["y"]=-17577.285714286,["x"]=-396640.28571429,},
- [13]={["y"]=-17614.428571429,["x"]=-397400.28571429,},
- [14]={["y"]=-19405.857142857,["x"]=-399428.85714286,},
- [15]={["y"]=-19234.428571429,["x"]=-399683.14285714,},
- [16]={["y"]=-18708.714285714,["x"]=-399408.85714286,},
- [17]={["y"]=-18397.285714286,["x"]=-399657.42857143,},
- [18]={["y"]=-17814.428571429,["x"]=-399823.42857143,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=-18687,["x"]=-399380.28571429,},
- [2]={["y"]=-18620.714285714,["x"]=-399436.85714286,},
- [3]={["y"]=-16217.857142857,["x"]=-396596.85714286,},
- [4]={["y"]=-16300.142857143,["x"]=-396530,},
- [5]={["y"]=-18687,["x"]=-399380.85714286,},
- },
- [2] = {
- [1]={["y"]=-18451.571428572,["x"]=-399580.57142857,},
- [2]={["y"]=-18392.142857143,["x"]=-399628.57142857,},
- [3]={["y"]=-16011,["x"]=-396806.85714286,},
- [4]={["y"]=-16074.714285714,["x"]=-396751.71428572,},
- [5]={["y"]=-18451.571428572,["x"]=-399580.85714285,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- McCarran = {
- PointsBoundary = {
- [1]={["y"]=-29455.285714286,["x"]=-416277.42857142,},
- [2]={["y"]=-28860.142857143,["x"]=-416492,},
- [3]={["y"]=-25044.428571429,["x"]=-416344.85714285,},
- [4]={["y"]=-24580.142857143,["x"]=-415959.14285714,},
- [5]={["y"]=-25073,["x"]=-415630.57142857,},
- [6]={["y"]=-25087.285714286,["x"]=-415130.57142857,},
- [7]={["y"]=-25830.142857143,["x"]=-414866.28571428,},
- [8]={["y"]=-26658.714285715,["x"]=-414880.57142857,},
- [9]={["y"]=-26973,["x"]=-415273.42857142,},
- [10]={["y"]=-27380.142857143,["x"]=-415187.71428571,},
- [11]={["y"]=-27715.857142857,["x"]=-414144.85714285,},
- [12]={["y"]=-27551.571428572,["x"]=-413473.42857142,},
- [13]={["y"]=-28630.142857143,["x"]=-413201.99999999,},
- [14]={["y"]=-29494.428571429,["x"]=-415437.71428571,},
- [15]={["y"]=-29455.571428572,["x"]=-416277.71428571,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=-29408.428571429,["x"]=-416016.28571428,},
- [2]={["y"]=-29408.142857144,["x"]=-416105.42857142,},
- [3]={["y"]=-24680.714285715,["x"]=-416003.14285713,},
- [4]={["y"]=-24681.857142858,["x"]=-415926.57142856,},
- [5]={["y"]=-29408.42857143,["x"]=-416016.57142856,},
- },
- [2] = {
- [1]={["y"]=-28575.571428572,["x"]=-416303.14285713,},
- [2]={["y"]=-28575.571428572,["x"]=-416382.57142856,},
- [3]={["y"]=-25111.000000001,["x"]=-416309.7142857,},
- [4]={["y"]=-25111.000000001,["x"]=-416249.14285713,},
- [5]={["y"]=-28575.571428572,["x"]=-416303.7142857,},
- },
- [3] = {
- [1]={["y"]=-29331.000000001,["x"]=-416275.42857141,},
- [2]={["y"]=-29259.000000001,["x"]=-416306.85714284,},
- [3]={["y"]=-28005.571428572,["x"]=-413449.7142857,},
- [4]={["y"]=-28068.714285715,["x"]=-413422.85714284,},
- [5]={["y"]=-29331.000000001,["x"]=-416275.7142857,},
- },
- [4] = {
- [1]={["y"]=-29073.285714286,["x"]=-416386.57142856,},
- [2]={["y"]=-28997.285714286,["x"]=-416417.42857141,},
- [3]={["y"]=-27697.571428572,["x"]=-413464.57142856,},
- [4]={["y"]=-27767.857142858,["x"]=-413434.28571427,},
- [5]={["y"]=-29073.000000001,["x"]=-416386.85714284,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- Creech = {
- PointsBoundary = {
- [1]={["y"]=-74522.714285715,["x"]=-360887.99999998,},
- [2]={["y"]=-74197,["x"]=-360556.57142855,},
- [3]={["y"]=-74402.714285715,["x"]=-359639.42857141,},
- [4]={["y"]=-74637,["x"]=-359279.42857141,},
- [5]={["y"]=-75759.857142857,["x"]=-359005.14285712,},
- [6]={["y"]=-75834.142857143,["x"]=-359045.14285712,},
- [7]={["y"]=-75902.714285714,["x"]=-359782.28571427,},
- [8]={["y"]=-76099.857142857,["x"]=-360399.42857141,},
- [9]={["y"]=-77314.142857143,["x"]=-360219.42857141,},
- [10]={["y"]=-77728.428571429,["x"]=-360445.14285713,},
- [11]={["y"]=-77585.571428571,["x"]=-360585.14285713,},
- [12]={["y"]=-76471.285714286,["x"]=-360819.42857141,},
- [13]={["y"]=-76325.571428571,["x"]=-360942.28571427,},
- [14]={["y"]=-74671.857142857,["x"]=-360927.7142857,},
- [15]={["y"]=-74522.714285714,["x"]=-360888.85714284,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=-74237.571428571,["x"]=-360591.7142857,},
- [2]={["y"]=-74234.428571429,["x"]=-360493.71428571,},
- [3]={["y"]=-77605.285714286,["x"]=-360399.14285713,},
- [4]={["y"]=-77608.714285715,["x"]=-360498.85714285,},
- [5]={["y"]=-74237.857142857,["x"]=-360591.7142857,},
- },
- [2] = {
- [1]={["y"]=-75807.571428572,["x"]=-359073.42857142,},
- [2]={["y"]=-74770.142857144,["x"]=-360581.71428571,},
- [3]={["y"]=-74641.285714287,["x"]=-360585.42857142,},
- [4]={["y"]=-75734.142857144,["x"]=-359023.14285714,},
- [5]={["y"]=-75807.285714287,["x"]=-359073.42857142,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- GroomLake = {
- PointsBoundary = {
- [1]={["y"]=-88916.714285714,["x"]=-289102.28571425,},
- [2]={["y"]=-87023.571428572,["x"]=-290388.57142857,},
- [3]={["y"]=-85916.428571429,["x"]=-290674.28571428,},
- [4]={["y"]=-87645.000000001,["x"]=-286567.14285714,},
- [5]={["y"]=-88380.714285715,["x"]=-286388.57142857,},
- [6]={["y"]=-89670.714285715,["x"]=-283524.28571428,},
- [7]={["y"]=-89797.857142858,["x"]=-283567.14285714,},
- [8]={["y"]=-88635.000000001,["x"]=-286749.99999999,},
- [9]={["y"]=-89177.857142858,["x"]=-287207.14285714,},
- [10]={["y"]=-89092.142857144,["x"]=-288892.85714285,},
- [11]={["y"]=-88917.000000001,["x"]=-289102.85714285,},
- },
- PointsRunways = {
- [1] = {
- [1]={["y"]=-86039.000000001,["x"]=-290606.28571428,},
- [2]={["y"]=-85965.285714287,["x"]=-290573.99999999,},
- [3]={["y"]=-87692.714285715,["x"]=-286634.85714285,},
- [4]={["y"]=-87756.714285715,["x"]=-286663.99999999,},
- [5]={["y"]=-86038.714285715,["x"]=-290606.85714285,},
- },
- [2] = {
- [1]={["y"]=-86808.428571429,["x"]=-290375.7142857,},
- [2]={["y"]=-86732.714285715,["x"]=-290344.28571427,},
- [3]={["y"]=-89672.714285714,["x"]=-283546.57142855,},
- [4]={["y"]=-89772.142857143,["x"]=-283587.71428569,},
- [5]={["y"]=-86808.142857143,["x"]=-290375.7142857,},
- },
- },
- ZoneBoundary = {},
- ZoneRunways = {},
- MaximumSpeed = 50,
- },
- },
-}
-
---- Creates a new AIRBASEPOLICE_NEVADA object.
--- @param #AIRBASEPOLICE_NEVADA self
--- @param SetClient A SET_CLIENT object that will contain the CLIENT objects to be monitored if they follow the rules of the airbase.
--- @return #AIRBASEPOLICE_NEVADA self
-function AIRBASEPOLICE_NEVADA:New( SetClient )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, AIRBASEPOLICE_BASE:New( SetClient, self.Airbases ) )
-
--- -- Nellis
--- local NellisBoundary = GROUP:FindByName( "Nellis Boundary" )
--- self.Airbases.Nellis.ZoneBoundary = ZONE_POLYGON:New( "Nellis Boundary", NellisBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
---
--- local NellisRunway1 = GROUP:FindByName( "Nellis Runway 1" )
--- self.Airbases.Nellis.ZoneRunways[1] = ZONE_POLYGON:New( "Nellis Runway 1", NellisRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local NellisRunway2 = GROUP:FindByName( "Nellis Runway 2" )
--- self.Airbases.Nellis.ZoneRunways[2] = ZONE_POLYGON:New( "Nellis Runway 2", NellisRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- -- McCarran
--- local McCarranBoundary = GROUP:FindByName( "McCarran Boundary" )
--- self.Airbases.McCarran.ZoneBoundary = ZONE_POLYGON:New( "McCarran Boundary", McCarranBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
---
--- local McCarranRunway1 = GROUP:FindByName( "McCarran Runway 1" )
--- self.Airbases.McCarran.ZoneRunways[1] = ZONE_POLYGON:New( "McCarran Runway 1", McCarranRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local McCarranRunway2 = GROUP:FindByName( "McCarran Runway 2" )
--- self.Airbases.McCarran.ZoneRunways[2] = ZONE_POLYGON:New( "McCarran Runway 2", McCarranRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local McCarranRunway3 = GROUP:FindByName( "McCarran Runway 3" )
--- self.Airbases.McCarran.ZoneRunways[3] = ZONE_POLYGON:New( "McCarran Runway 3", McCarranRunway3 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local McCarranRunway4 = GROUP:FindByName( "McCarran Runway 4" )
--- self.Airbases.McCarran.ZoneRunways[4] = ZONE_POLYGON:New( "McCarran Runway 4", McCarranRunway4 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- -- Creech
--- local CreechBoundary = GROUP:FindByName( "Creech Boundary" )
--- self.Airbases.Creech.ZoneBoundary = ZONE_POLYGON:New( "Creech Boundary", CreechBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
---
--- local CreechRunway1 = GROUP:FindByName( "Creech Runway 1" )
--- self.Airbases.Creech.ZoneRunways[1] = ZONE_POLYGON:New( "Creech Runway 1", CreechRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local CreechRunway2 = GROUP:FindByName( "Creech Runway 2" )
--- self.Airbases.Creech.ZoneRunways[2] = ZONE_POLYGON:New( "Creech Runway 2", CreechRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- -- Groom Lake
--- local GroomLakeBoundary = GROUP:FindByName( "GroomLake Boundary" )
--- self.Airbases.GroomLake.ZoneBoundary = ZONE_POLYGON:New( "GroomLake Boundary", GroomLakeBoundary ):SmokeZone(SMOKECOLOR.White):Flush()
---
--- local GroomLakeRunway1 = GROUP:FindByName( "GroomLake Runway 1" )
--- self.Airbases.GroomLake.ZoneRunways[1] = ZONE_POLYGON:New( "GroomLake Runway 1", GroomLakeRunway1 ):SmokeZone(SMOKECOLOR.Red):Flush()
---
--- local GroomLakeRunway2 = GROUP:FindByName( "GroomLake Runway 2" )
--- self.Airbases.GroomLake.ZoneRunways[2] = ZONE_POLYGON:New( "GroomLake Runway 2", GroomLakeRunway2 ):SmokeZone(SMOKECOLOR.Red):Flush()
-
-end
-
-
-
-
-
- --- **Functional** - DETECTION_ classes model the detection of enemy units by FACs or RECCEs and group them according various methods.
---
--- 
---
--- ===
---
--- DETECTION classes facilitate the detection of enemy units within the battle zone executed by FACs (Forward Air Controllers) or RECCEs (Reconnassance Units).
--- DETECTION uses the in-built detection capabilities of DCS World, but adds new functionalities.
---
--- Please watch this [youtube video](https://youtu.be/C7p81dUwP-E) that explains the detection concepts.
---
---
--- ### Contributions:
---
--- * Mechanist : Early concept of DETECTION_AREAS.
---
--- ### Authors:
---
--- * FlightControl : Analysis, Design, Programming, Testing
---
--- @module Detection
-
-
-do -- DETECTION_BASE
-
- --- # 1) DETECTION_BASE class, extends @{Fsm#FSM}
- --
- -- The DETECTION_BASE class defines the core functions to administer detected objects.
- -- The DETECTION_BASE class will detect objects within the battle zone for a list of @{Group}s detecting targets following (a) detection method(s).
- --
- -- ## 1.1) DETECTION_BASE constructor
- --
- -- Construct a new DETECTION_BASE instance using the @{#DETECTION_BASE.New}() method.
- --
- -- ## 1.2) DETECTION_BASE initialization
- --
- -- By default, detection will return detected objects with all the detection sensors available.
- -- However, you can ask how the objects were found with specific detection methods.
- -- If you use one of the below methods, the detection will work with the detection method specified.
- -- You can specify to apply multiple detection methods.
- --
- -- Use the following functions to report the objects it detected using the methods Visual, Optical, Radar, IRST, RWR, DLINK:
- --
- -- * @{#DETECTION_BASE.InitDetectVisual}(): Detected using Visual.
- -- * @{#DETECTION_BASE.InitDetectOptical}(): Detected using Optical.
- -- * @{#DETECTION_BASE.InitDetectRadar}(): Detected using Radar.
- -- * @{#DETECTION_BASE.InitDetectIRST}(): Detected using IRST.
- -- * @{#DETECTION_BASE.InitDetectRWR}(): Detected using RWR.
- -- * @{#DETECTION_BASE.InitDetectDLINK}(): Detected using DLINK.
- --
- -- ## 1.3) DETECTION_BASE derived classes group the detected units into a DetectedItems[] list
- --
- -- DETECTION_BASE derived classes build a list called DetectedItems[], which is essentially a first later
- -- of grouping of detected units. Each DetectedItem within the DetectedItems[] list contains
- -- a SET_UNIT object that contains the detected units that belong to that group.
- --
- -- Derived classes will apply different methods to group the detected units.
- -- Examples are per area, per quadrant, per distance, per type.
- -- See further the derived DETECTION classes on which grouping methods are currently supported.
- --
- -- Various methods exist how to retrieve the grouped items from a DETECTION_BASE derived class:
- --
- -- * The method @{Detection#DETECTION_BASE.GetDetectedItems}() retrieves the DetectedItems[] list.
- -- * A DetectedItem from the DetectedItems[] list can be retrieved using the method @{Detection#DETECTION_BASE.GetDetectedItem}( DetectedItemIndex ).
- -- Note that this method returns a DetectedItem element from the list, that contains a Set variable and further information
- -- about the DetectedItem that is set by the DETECTION_BASE derived classes, used to group the DetectedItem.
- -- * A DetectedSet from the DetectedItems[] list can be retrieved using the method @{Detection#DETECTION_BASE.GetDetectedSet}( DetectedItemIndex ).
- -- This method retrieves the Set from a DetectedItem element from the DetectedItem list (DetectedItems[ DetectedItemIndex ].Set ).
- --
- -- ## 1.4) Apply additional Filters to fine-tune the detected objects
- --
- -- By default, DCS World will return any object that is in LOS and within "visual reach", or detectable through one of the electronic detection means.
- -- That being said, the DCS World detection algorithm can sometimes be unrealistic.
- -- Especially for a visual detection, DCS World is able to report within 1 second a detailed detection of a group of 20 units (including types of the units) that are 10 kilometers away, using only visual capabilities.
- -- Additionally, trees and other obstacles are not accounted during the DCS World detection.
- --
- -- Therefore, an additional (optional) filtering has been built into the DETECTION_BASE class, that can be set for visual detected units.
- -- For electronic detection, this filtering is not applied, only for visually detected targets.
- --
- -- The following additional filtering can be applied for visual filtering:
- --
- -- * A probability factor per kilometer distance.
- -- * A probability factor based on the alpha angle between the detected object and the unit detecting.
- -- A detection from a higher altitude allows for better detection than when on the ground.
- -- * Define a probability factor for "cloudy zones", which are zones where forests or villages are located. In these zones, detection will be much more difficult.
- -- The mission designer needs to define these cloudy zones within the mission, and needs to register these zones in the DETECTION_ objects additing a probability factor per zone.
- --
- -- I advise however, that, when you first use the DETECTION derived classes, that you don't use these filters.
- -- Only when you experience unrealistic behaviour in your missions, these filters could be applied.
- --
- -- ### 1.4.1 ) Distance visual detection probability
- --
- -- Upon a **visual** detection, the further away a detected object is, the less likely it is to be detected properly.
- -- Also, the speed of accurate detection plays a role.
- --
- -- A distance probability factor between 0 and 1 can be given, that will model a linear extrapolated probability over 10 km distance.
- --
- -- For example, if a probability factor of 0.6 (60%) is given, the extrapolated probabilities over 15 kilometers would like like:
- -- 1 km: 96%, 2 km: 92%, 3 km: 88%, 4 km: 84%, 5 km: 80%, 6 km: 76%, 7 km: 72%, 8 km: 68%, 9 km: 64%, 10 km: 60%, 11 km: 56%, 12 km: 52%, 13 km: 48%, 14 km: 44%, 15 km: 40%.
- --
- -- Note that based on this probability factor, not only the detection but also the **type** of the unit will be applied!
- --
- -- Use the method @{Detection#DETECTION_BASE.SetDistanceProbability}() to set the probability factor upon a 10 km distance.
- --
- -- ### 1.4.2 ) Alpha Angle visual detection probability
- --
- -- Upon a **visual** detection, the higher the unit is during the detecting process, the more likely the detected unit is to be detected properly.
- -- A detection at a 90% alpha angle is the most optimal, a detection at 10% is less and a detection at 0% is less likely to be correct.
- --
- -- A probability factor between 0 and 1 can be given, that will model a progressive extrapolated probability if the target would be detected at a 0° angle.
- --
- -- For example, if a alpha angle probability factor of 0.7 is given, the extrapolated probabilities of the different angles would look like:
- -- 0°: 70%, 10°: 75,21%, 20°: 80,26%, 30°: 85%, 40°: 89,28%, 50°: 92,98%, 60°: 95,98%, 70°: 98,19%, 80°: 99,54%, 90°: 100%
- --
- -- Use the method @{Detection#DETECTION_BASE.SetAlphaAngleProbability}() to set the probability factor if 0°.
- --
- -- ### 1.4.3 ) Cloudy Zones detection probability
- --
- -- Upon a **visual** detection, the more a detected unit is within a cloudy zone, the less likely the detected unit is to be detected successfully.
- -- The Cloudy Zones work with the ZONE_BASE derived classes. The mission designer can define within the mission
- -- zones that reflect cloudy areas where detected units may not be so easily visually detected.
- --
- -- Use the method @{Detection#DETECTION_BASE.SetZoneProbability}() to set for a defined number of zones, the probability factors.
- --
- -- Note however, that the more zones are defined to be "cloudy" within a detection, the more performance it will take
- -- from the DETECTION_BASE to calculate the presence of the detected unit within each zone.
- -- Expecially for ZONE_POLYGON, try to limit the amount of nodes of the polygon!
- --
- -- Typically, this kind of filter would be applied for very specific areas were a detection needs to be very realisting for
- -- AI not to detect so easily targets within a forrest or village rich area.
- --
- -- ## 1.5 ) Accept / Reject detected units
- --
- -- DETECTION_BASE can accept or reject successful detections based on the location of the detected object,
- -- if it is located in range or located inside or outside of specific zones.
- --
- -- ### 1.5.1 ) Detection acceptance of within range limit
- --
- -- A range can be set that will limit a successful detection for a unit.
- -- Use the method @{Detection#DETECTION_BASE.SetAcceptRange}() to apply a range in meters till where detected units will be accepted.
- --
- -- local SetGroup = SET_GROUP:New():FilterPrefixes( "FAC" ):FilterStart() -- Build a SetGroup of Forward Air Controllers.
- --
- -- -- Build a detect object.
- -- local Detection = DETECTION_BASE:New( SetGroup )
- --
- -- -- This will accept detected units if the range is below 5000 meters.
- -- Detection:SetAcceptRange( 5000 )
- --
- -- -- Start the Detection.
- -- Detection:Start()
- --
- --
- -- ### 1.5.2 ) Detection acceptance if within zone(s).
- --
- -- Specific ZONE_BASE object(s) can be given as a parameter, which will only accept a detection if the unit is within the specified ZONE_BASE object(s).
- -- Use the method @{Detection#DETECTION_BASE.SetAcceptZones}() will accept detected units if they are within the specified zones.
- --
- -- local SetGroup = SET_GROUP:New():FilterPrefixes( "FAC" ):FilterStart() -- Build a SetGroup of Forward Air Controllers.
- --
- -- -- Search fo the zones where units are to be accepted.
- -- local ZoneAccept1 = ZONE:New( "AcceptZone1" )
- -- local ZoneAccept2 = ZONE:New( "AcceptZone2" )
- --
- -- -- Build a detect object.
- -- local Detection = DETECTION_BASE:New( SetGroup )
- --
- -- -- This will accept detected units by Detection when the unit is within ZoneAccept1 OR ZoneAccept2.
- -- Detection:SetAcceptZones( { ZoneAccept1, ZoneAccept2 } )
- --
- -- -- Start the Detection.
- -- Detection:Start()
- --
- -- ### 1.5.3 ) Detection rejectance if within zone(s).
- --
- -- Specific ZONE_BASE object(s) can be given as a parameter, which will reject detection if the unit is within the specified ZONE_BASE object(s).
- -- Use the method @{Detection#DETECTION_BASE.SetRejectZones}() will reject detected units if they are within the specified zones.
- -- An example of how to use the method is shown below.
- --
- -- local SetGroup = SET_GROUP:New():FilterPrefixes( "FAC" ):FilterStart() -- Build a SetGroup of Forward Air Controllers.
- --
- -- -- Search fo the zones where units are to be rejected.
- -- local ZoneReject1 = ZONE:New( "RejectZone1" )
- -- local ZoneReject2 = ZONE:New( "RejectZone2" )
- --
- -- -- Build a detect object.
- -- local Detection = DETECTION_BASE:New( SetGroup )
- --
- -- -- This will reject detected units by Detection when the unit is within ZoneReject1 OR ZoneReject2.
- -- Detection:SetRejectZones( { ZoneReject1, ZoneReject2 } )
- --
- -- -- Start the Detection.
- -- Detection:Start()
- --
- -- ## 1.6) DETECTION_BASE is a Finite State Machine
- --
- -- Various Events and State Transitions can be tailored using DETECTION_BASE.
- --
- -- ### 1.6.1) DETECTION_BASE States
- --
- -- * **Detecting**: The detection is running.
- -- * **Stopped**: The detection is stopped.
- --
- -- ### 1.6.2) DETECTION_BASE Events
- --
- -- * **Start**: Start the detection process.
- -- * **Detect**: Detect new units.
- -- * **Detected**: New units have been detected.
- -- * **Stop**: Stop the detection process.
- --
- -- @type DETECTION_BASE
- -- @field Core.Set#SET_GROUP DetectionSetGroup The @{Set} of GROUPs in the Forward Air Controller role.
- -- @field Dcs.DCSTypes#Distance DetectionRange The range till which targets are accepted to be detected.
- -- @field #DETECTION_BASE.DetectedObjects DetectedObjects The list of detected objects.
- -- @field #table DetectedObjectsIdentified Map of the DetectedObjects identified.
- -- @field #number DetectionRun
- -- @extends Core.Fsm#FSM
- DETECTION_BASE = {
- ClassName = "DETECTION_BASE",
- DetectionSetGroup = nil,
- DetectionRange = nil,
- DetectedObjects = {},
- DetectionRun = 0,
- DetectedObjectsIdentified = {},
- DetectedItems = {},
- }
-
- --- @type DETECTION_BASE.DetectedObjects
- -- @list <#DETECTION_BASE.DetectedObject>
-
- --- @type DETECTION_BASE.DetectedObject
- -- @field #string Name
- -- @field #boolean Visible
- -- @field #string Type
- -- @field #number Distance
- -- @field #boolean Identified
-
- --- @type DETECTION_BASE.DetectedItems
- -- @list <#DETECTION_BASE.DetectedItem>
-
- --- @type DETECTION_BASE.DetectedItem
- -- @field Core.Set#SET_UNIT Set
- -- @field Core.Set#SET_UNIT Set -- The Set of Units in the detected area.
- -- @field Core.Zone#ZONE_UNIT Zone -- The Zone of the detected area.
- -- @field #boolean Changed Documents if the detected area has changes.
- -- @field #table Changes A list of the changes reported on the detected area. (It is up to the user of the detected area to consume those changes).
- -- @field #number ItemID -- The identifier of the detected area.
- -- @field #boolean FriendliesNearBy Indicates if there are friendlies within the detected area.
- -- @field Wrapper.Unit#UNIT NearestFAC The nearest FAC near the Area.
-
-
- --- DETECTION constructor.
- -- @param #DETECTION_BASE self
- -- @param Core.Set#SET_GROUP DetectionSetGroup The @{Set} of GROUPs in the Forward Air Controller role.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:New( DetectionSetGroup )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM:New() ) -- #DETECTION_BASE
-
- self.DetectedItemCount = 0
- self.DetectedItemMax = 0
- self.DetectedItems = {}
-
- self.DetectionSetGroup = DetectionSetGroup
-
- self.DetectionInterval = 30
-
- self:InitDetectVisual( true )
- self:InitDetectOptical( false )
- self:InitDetectRadar( false )
- self:InitDetectRWR( false )
- self:InitDetectIRST( false )
- self:InitDetectDLINK( false )
-
- -- Create FSM transitions.
-
- self:SetStartState( "Stopped" )
- self.CountryID = DetectionSetGroup:GetFirst():GetCountry()
-
- self:AddTransition( "Stopped", "Start", "Detecting")
-
- --- OnLeave Transition Handler for State Stopped.
- -- @function [parent=#DETECTION_BASE] OnLeaveStopped
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Stopped.
- -- @function [parent=#DETECTION_BASE] OnEnterStopped
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnBefore Transition Handler for Event Start.
- -- @function [parent=#DETECTION_BASE] OnBeforeStart
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Start.
- -- @function [parent=#DETECTION_BASE] OnAfterStart
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Start.
- -- @function [parent=#DETECTION_BASE] Start
- -- @param #DETECTION_BASE self
-
- --- Asynchronous Event Trigger for Event Start.
- -- @function [parent=#DETECTION_BASE] __Start
- -- @param #DETECTION_BASE self
- -- @param #number Delay The delay in seconds.
-
- --- OnLeave Transition Handler for State Detecting.
- -- @function [parent=#DETECTION_BASE] OnLeaveDetecting
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Detecting.
- -- @function [parent=#DETECTION_BASE] OnEnterDetecting
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- self:AddTransition( "Detecting", "Detect", "Detecting" )
- self:AddTransition( "Detecting", "DetectionGroup", "Detecting" )
-
- --- OnBefore Transition Handler for Event Detect.
- -- @function [parent=#DETECTION_BASE] OnBeforeDetect
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Detect.
- -- @function [parent=#DETECTION_BASE] OnAfterDetect
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Detect.
- -- @function [parent=#DETECTION_BASE] Detect
- -- @param #DETECTION_BASE self
-
- --- Asynchronous Event Trigger for Event Detect.
- -- @function [parent=#DETECTION_BASE] __Detect
- -- @param #DETECTION_BASE self
- -- @param #number Delay The delay in seconds.
-
-
- self:AddTransition( "Detecting", "Detected", "Detecting" )
-
- --- OnBefore Transition Handler for Event Detected.
- -- @function [parent=#DETECTION_BASE] OnBeforeDetected
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Detected.
- -- @function [parent=#DETECTION_BASE] OnAfterDetected
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Detected.
- -- @function [parent=#DETECTION_BASE] Detected
- -- @param #DETECTION_BASE self
-
- --- Asynchronous Event Trigger for Event Detected.
- -- @function [parent=#DETECTION_BASE] __Detected
- -- @param #DETECTION_BASE self
- -- @param #number Delay The delay in seconds.
-
-
- self:AddTransition( "*", "Stop", "Stopped" )
-
- --- OnBefore Transition Handler for Event Stop.
- -- @function [parent=#DETECTION_BASE] OnBeforeStop
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Stop.
- -- @function [parent=#DETECTION_BASE] OnAfterStop
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Stop.
- -- @function [parent=#DETECTION_BASE] Stop
- -- @param #DETECTION_BASE self
-
- --- Asynchronous Event Trigger for Event Stop.
- -- @function [parent=#DETECTION_BASE] __Stop
- -- @param #DETECTION_BASE self
- -- @param #number Delay The delay in seconds.
-
- --- OnLeave Transition Handler for State Stopped.
- -- @function [parent=#DETECTION_BASE] OnLeaveStopped
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Stopped.
- -- @function [parent=#DETECTION_BASE] OnEnterStopped
- -- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- return self
- end
-
- do -- State Transition Handling
-
- --- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- function DETECTION_BASE:onafterStart(From,Event,To)
- self:__Detect(0.1)
- end
-
- --- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- function DETECTION_BASE:onafterDetect(From,Event,To)
- self:E( {From,Event,To})
-
- local DetectDelay = 0.1
- self.DetectionCount = 0
- self.DetectionRun = 0
- self:UnIdentifyAllDetectedObjects() -- Resets the DetectedObjectsIdentified table
-
- self.DetectionSetGroup:Flush()
-
- for DetectionGroupID, DetectionGroupData in pairs( self.DetectionSetGroup:GetSet() ) do
- self:E( {DetectionGroupData})
- self:__DetectionGroup( DetectDelay, DetectionGroupData ) -- Process each detection asynchronously.
- self.DetectionCount = self.DetectionCount + 1
- DetectDelay = DetectDelay + 0.1
- end
- end
-
- --- @param #DETECTION_BASE self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @param Wrapper.Group#GROUP DetectionGroup The Group detecting.
- function DETECTION_BASE:onafterDetectionGroup( From, Event, To, DetectionGroup )
- self:E( {From,Event,To})
-
- self.DetectionRun = self.DetectionRun + 1
-
- local HasDetectedObjects = false
-
- if DetectionGroup:IsAlive() then
-
- self:T( { "DetectionGroup is Alive", DetectionGroup:GetName() } )
-
- local DetectionGroupName = DetectionGroup:GetName()
-
- local DetectedUnits = {}
-
- local DetectedTargets = DetectionGroup:GetDetectedTargets(
- self.DetectVisual,
- self.DetectOptical,
- self.DetectRadar,
- self.DetectIRST,
- self.DetectRWR,
- self.DetectDLINK
- )
-
- self:T( DetectedTargets )
-
- for DetectionObjectID, Detection in pairs( DetectedTargets ) do
- local DetectedObject = Detection.object -- Dcs.DCSWrapper.Object#Object
- self:T2( DetectedObject )
-
- if DetectedObject and DetectedObject:isExist() and DetectedObject.id_ < 50000000 then
-
- local DetectionAccepted = true
-
- local DetectedObjectName = DetectedObject:getName()
-
- local DetectedObjectVec3 = DetectedObject:getPoint()
- local DetectedObjectVec2 = { x = DetectedObjectVec3.x, y = DetectedObjectVec3.z }
- local DetectionGroupVec3 = DetectionGroup:GetVec3()
- local DetectionGroupVec2 = { x = DetectionGroupVec3.x, y = DetectionGroupVec3.z }
-
- local Distance = ( ( DetectedObjectVec3.x - DetectionGroupVec3.x )^2 +
- ( DetectedObjectVec3.y - DetectionGroupVec3.y )^2 +
- ( DetectedObjectVec3.z - DetectionGroupVec3.z )^2
- ) ^ 0.5 / 1000
-
- self:T( { "Detected Target", DetectionGroupName, DetectedObjectName, Distance } )
-
- -- Calculate Acceptance
-
- if self.AcceptRange and Distance > self.AcceptRange then
- DetectionAccepted = false
- end
-
- if self.AcceptZones then
- for AcceptZoneID, AcceptZone in pairs( self.AcceptZones ) do
- local AcceptZone = AcceptZone -- Core.Zone#ZONE_BASE
- if AcceptZone:IsPointVec2InZone( DetectedObjectVec2 ) == false then
- DetectionAccepted = false
- end
- end
- end
-
- if self.RejectZones then
- for RejectZoneID, RejectZone in pairs( self.RejectZones ) do
- local RejectZone = RejectZone -- Core.Zone#ZONE_BASE
- if RejectZone:IsPointVec2InZone( DetectedObjectVec2 ) == true then
- DetectionAccepted = false
- end
- end
- end
-
- -- Calculate additional probabilities
-
- if not self.DetectedObjects[DetectedObjectName] and Detection.visible and self.DistanceProbability then
- local DistanceFactor = Distance / 4
- local DistanceProbabilityReversed = ( 1 - self.DistanceProbability ) * DistanceFactor
- local DistanceProbability = 1 - DistanceProbabilityReversed
- DistanceProbability = DistanceProbability * 30 / 300
- local Probability = math.random() -- Selects a number between 0 and 1
- self:T( { Probability, DistanceProbability } )
- if Probability > DistanceProbability then
- DetectionAccepted = false
- end
- end
-
- if not self.DetectedObjects[DetectedObjectName] and Detection.visible and self.AlphaAngleProbability then
- local NormalVec2 = { x = DetectedObjectVec2.x - DetectionGroupVec2.x, y = DetectedObjectVec2.y - DetectionGroupVec2.y }
- local AlphaAngle = math.atan2( NormalVec2.y, NormalVec2.x )
- local Sinus = math.sin( AlphaAngle )
- local AlphaAngleProbabilityReversed = ( 1 - self.AlphaAngleProbability ) * ( 1 - Sinus )
- local AlphaAngleProbability = 1 - AlphaAngleProbabilityReversed
-
- AlphaAngleProbability = AlphaAngleProbability * 30 / 300
-
- local Probability = math.random() -- Selects a number between 0 and 1
- self:T( { Probability, AlphaAngleProbability } )
- if Probability > AlphaAngleProbability then
- DetectionAccepted = false
- end
-
- end
-
- if not self.DetectedObjects[DetectedObjectName] and Detection.visible and self.ZoneProbability then
-
- for ZoneDataID, ZoneData in pairs( self.ZoneProbability ) do
- self:E({ZoneData})
- local ZoneObject = ZoneData[1] -- Core.Zone#ZONE_BASE
- local ZoneProbability = ZoneData[2] -- #number
- ZoneProbability = ZoneProbability * 30 / 300
-
- if ZoneObject:IsPointVec2InZone( DetectedObjectVec2 ) == true then
- local Probability = math.random() -- Selects a number between 0 and 1
- self:T( { Probability, ZoneProbability } )
- if Probability > ZoneProbability then
- DetectionAccepted = false
- break
- end
- end
- end
- end
-
- if DetectionAccepted then
-
- HasDetectedObjects = true
-
- if not self.DetectedObjects[DetectedObjectName] then
- self.DetectedObjects[DetectedObjectName] = {}
- end
- self.DetectedObjects[DetectedObjectName].Name = DetectedObjectName
- self.DetectedObjects[DetectedObjectName].Visible = Detection.visible
- self.DetectedObjects[DetectedObjectName].Type = Detection.type
- self.DetectedObjects[DetectedObjectName].Distance = Distance
-
- local DetectedUnit = UNIT:FindByName( DetectedObjectName )
-
- DetectedUnits[DetectedObjectName] = DetectedUnit
- else
- -- if beyond the DetectionRange then nullify...
- if self.DetectedObjects[DetectedObjectName] then
- self.DetectedObjects[DetectedObjectName] = nil
- end
- end
- end
-
- self:T2( self.DetectedObjects )
- end
-
- if HasDetectedObjects then
- self:__Detected( 0.1, DetectedUnits )
- end
-
- end
-
- if self.DetectionCount > 0 and self.DetectionRun == self.DetectionCount then
- self:__Detect( self.DetectionInterval )
-
- self:T( "--> Create Detection Sets" )
- self:CreateDetectionSets()
- end
-
- end
-
-
- end
-
- do -- Initialization methods
-
- --- Detect Visual.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectVisual
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectVisual( DetectVisual )
-
- self.DetectVisual = DetectVisual
- end
-
- --- Detect Optical.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectOptical
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectOptical( DetectOptical )
- self:F2()
-
- self.DetectOptical = DetectOptical
- end
-
- --- Detect Radar.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectRadar
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectRadar( DetectRadar )
- self:F2()
-
- self.DetectRadar = DetectRadar
- end
-
- --- Detect IRST.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectIRST
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectIRST( DetectIRST )
- self:F2()
-
- self.DetectIRST = DetectIRST
- end
-
- --- Detect RWR.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectRWR
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectRWR( DetectRWR )
- self:F2()
-
- self.DetectRWR = DetectRWR
- end
-
- --- Detect DLINK.
- -- @param #DETECTION_BASE self
- -- @param #boolean DetectDLINK
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:InitDetectDLINK( DetectDLINK )
- self:F2()
-
- self.DetectDLINK = DetectDLINK
- end
-
- end
-
- do
-
- --- Set the detection interval time in seconds.
- -- @param #DETECTION_BASE self
- -- @param #number DetectionInterval Interval in seconds.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetDetectionInterval( DetectionInterval )
- self:F2()
-
- self.DetectionInterval = DetectionInterval
-
- return self
- end
-
- end
-
- do -- Accept / Reject detected units
-
- --- Accept detections if within a range in meters.
- -- @param #DETECTION_BASE self
- -- @param #number AcceptRange Accept a detection if the unit is within the AcceptRange in meters.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetAcceptRange( AcceptRange )
- self:F2()
-
- self.AcceptRange = AcceptRange
-
- return self
- end
-
- --- Accept detections if within the specified zone(s).
- -- @param #DETECTION_BASE self
- -- @param AcceptZones Can be a list or ZONE_BASE objects, or a single ZONE_BASE object.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetAcceptZones( AcceptZones )
- self:F2()
-
- if type( AcceptZones ) == "table" then
- self.AcceptZones = AcceptZones
- else
- self.AcceptZones = { AcceptZones }
- end
-
- return self
- end
-
- --- Reject detections if within the specified zone(s).
- -- @param #DETECTION_BASE self
- -- @param RejectZones Can be a list or ZONE_BASE objects, or a single ZONE_BASE object.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetRejectZones( RejectZones )
- self:F2()
-
- if type( RejectZones ) == "table" then
- self.RejectZones = RejectZones
- else
- self.RejectZones = { RejectZones }
- end
-
- return self
- end
-
- end
-
- do -- Probability methods
-
- --- Upon a **visual** detection, the further away a detected object is, the less likely it is to be detected properly.
- -- Also, the speed of accurate detection plays a role.
- -- A distance probability factor between 0 and 1 can be given, that will model a linear extrapolated probability over 10 km distance.
- -- For example, if a probability factor of 0.6 (60%) is given, the extrapolated probabilities over 15 kilometers would like like:
- -- 1 km: 96%, 2 km: 92%, 3 km: 88%, 4 km: 84%, 5 km: 80%, 6 km: 76%, 7 km: 72%, 8 km: 68%, 9 km: 64%, 10 km: 60%, 11 km: 56%, 12 km: 52%, 13 km: 48%, 14 km: 44%, 15 km: 40%.
- -- @param #DETECTION_BASE self
- -- @param DistanceProbability The probability factor.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetDistanceProbability( DistanceProbability )
- self:F2()
-
- self.DistanceProbability = DistanceProbability
-
- return self
- end
-
-
- --- Upon a **visual** detection, the higher the unit is during the detecting process, the more likely the detected unit is to be detected properly.
- -- A detection at a 90% alpha angle is the most optimal, a detection at 10% is less and a detection at 0% is less likely to be correct.
- --
- -- A probability factor between 0 and 1 can be given, that will model a progressive extrapolated probability if the target would be detected at a 0° angle.
- --
- -- For example, if a alpha angle probability factor of 0.7 is given, the extrapolated probabilities of the different angles would look like:
- -- 0°: 70%, 10°: 75,21%, 20°: 80,26%, 30°: 85%, 40°: 89,28%, 50°: 92,98%, 60°: 95,98%, 70°: 98,19%, 80°: 99,54%, 90°: 100%
- -- @param #DETECTION_BASE self
- -- @param AlphaAngleProbability The probability factor.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetAlphaAngleProbability( AlphaAngleProbability )
- self:F2()
-
- self.AlphaAngleProbability = AlphaAngleProbability
-
- return self
- end
-
- --- Upon a **visual** detection, the more a detected unit is within a cloudy zone, the less likely the detected unit is to be detected successfully.
- -- The Cloudy Zones work with the ZONE_BASE derived classes. The mission designer can define within the mission
- -- zones that reflect cloudy areas where detected units may not be so easily visually detected.
- -- @param #DETECTION_BASE self
- -- @param ZoneArray Aray of a The ZONE_BASE object and a ZoneProbability pair..
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:SetZoneProbability( ZoneArray )
- self:F2()
-
- self.ZoneProbability = ZoneArray
-
- return self
- end
-
-
- end
-
- do -- Change processing
-
- --- Accepts changes from the detected item.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:AcceptChanges( DetectedItem )
-
- DetectedItem.Changed = false
- DetectedItem.Changes = {}
-
- return self
- end
-
- --- Add a change to the detected zone.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @param #string ChangeCode
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:AddChangeItem( DetectedItem, ChangeCode, ItemUnitType )
-
- DetectedItem.Changed = true
- local ItemID = DetectedItem.ItemID
-
- DetectedItem.Changes = DetectedItem.Changes or {}
- DetectedItem.Changes[ChangeCode] = DetectedItem.Changes[ChangeCode] or {}
- DetectedItem.Changes[ChangeCode].ItemID = ItemID
- DetectedItem.Changes[ChangeCode].ItemUnitType = ItemUnitType
-
- self:T( { "Change on Detection Item:", DetectedItem.ItemID, ChangeCode, ItemUnitType } )
-
- return self
- end
-
-
- --- Add a change to the detected zone.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @param #string ChangeCode
- -- @param #string ChangeUnitType
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:AddChangeUnit( DetectedItem, ChangeCode, ChangeUnitType )
-
- DetectedItem.Changed = true
- local ItemID = DetectedItem.ItemID
-
- DetectedItem.Changes = DetectedItem.Changes or {}
- DetectedItem.Changes[ChangeCode] = DetectedItem.Changes[ChangeCode] or {}
- DetectedItem.Changes[ChangeCode][ChangeUnitType] = DetectedItem.Changes[ChangeCode][ChangeUnitType] or 0
- DetectedItem.Changes[ChangeCode][ChangeUnitType] = DetectedItem.Changes[ChangeCode][ChangeUnitType] + 1
- DetectedItem.Changes[ChangeCode].ItemID = ItemID
-
- self:T( { "Change on Detection Item:", DetectedItem.ItemID, ChangeCode, ChangeUnitType } )
-
- return self
- end
-
-
- end
-
- do -- Threat
-
- --- Returns if there are friendlies nearby the FAC units ...
- -- @param #DETECTION_BASE self
- -- @return #boolean trhe if there are friendlies nearby
- function DETECTION_BASE:IsFriendliesNearBy( DetectedItem )
-
- self:T3( DetectedItem.FriendliesNearBy )
- return DetectedItem.FriendliesNearBy or false
- end
-
- --- Background worker function to determine if there are friendlies nearby ...
- -- @param #DETECTION_BASE self
- function DETECTION_BASE:ReportFriendliesNearBy( ReportGroupData )
- self:F2()
-
- local DetectedItem = ReportGroupData.DetectedItem -- Functional.Detection#DETECTION_BASE.DetectedItem
- local DetectedSet = ReportGroupData.DetectedItem.Set
- local DetectedUnit = DetectedSet:GetFirst()
-
- DetectedItem.FriendliesNearBy = false
-
- if DetectedUnit then
-
-
- local SphereSearch = {
- id = world.VolumeType.SPHERE,
- params = {
- point = DetectedUnit:GetVec3(),
- radius = 6000,
- }
-
- }
-
- --- @param Dcs.DCSWrapper.Unit#Unit FoundDCSUnit
- -- @param Wrapper.Group#GROUP ReportGroup
- -- @param Set#SET_GROUP ReportSetGroup
- local FindNearByFriendlies = function( FoundDCSUnit, ReportGroupData )
-
- local DetectedItem = ReportGroupData.DetectedItem -- Functional.Detection#DETECTION_BASE.DetectedItem
- local DetectedSet = ReportGroupData.DetectedItem.Set
- local DetectedUnit = DetectedSet:GetFirst() -- Wrapper.Unit#UNIT
- local ReportSetGroup = ReportGroupData.ReportSetGroup
-
- local EnemyCoalition = DetectedUnit:GetCoalition()
-
- local FoundUnitCoalition = FoundDCSUnit:getCoalition()
- local FoundUnitName = FoundDCSUnit:getName()
- local FoundUnitGroupName = FoundDCSUnit:getGroup():getName()
- local EnemyUnitName = DetectedUnit:GetName()
- local FoundUnitInReportSetGroup = ReportSetGroup:FindGroup( FoundUnitGroupName ) ~= nil
-
- self:T3( { "Friendlies search:", FoundUnitName, FoundUnitCoalition, EnemyUnitName, EnemyCoalition, FoundUnitInReportSetGroup } )
-
- if FoundUnitCoalition ~= EnemyCoalition and FoundUnitInReportSetGroup == false then
- DetectedItem.FriendliesNearBy = true
- return false
- end
-
- return true
- end
-
- world.searchObjects( Object.Category.UNIT, SphereSearch, FindNearByFriendlies, ReportGroupData )
- end
- end
-
- end
-
- --- Determines if a detected object has already been identified during detection processing.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedObject DetectedObject
- -- @return #boolean true if already identified.
- function DETECTION_BASE:IsDetectedObjectIdentified( DetectedObject )
- self:F3( DetectedObject.Name )
-
- local DetectedObjectName = DetectedObject.Name
- local DetectedObjectIdentified = self.DetectedObjectsIdentified[DetectedObjectName] == true
- self:T3( DetectedObjectIdentified )
- return DetectedObjectIdentified
- end
-
- --- Identifies a detected object during detection processing.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedObject DetectedObject
- function DETECTION_BASE:IdentifyDetectedObject( DetectedObject )
- self:F( { "Identified:", DetectedObject.Name } )
-
- local DetectedObjectName = DetectedObject.Name
- self.DetectedObjectsIdentified[DetectedObjectName] = true
- end
-
- --- UnIdentify a detected object during detection processing.
- -- @param #DETECTION_BASE self
- -- @param #DETECTION_BASE.DetectedObject DetectedObject
- function DETECTION_BASE:UnIdentifyDetectedObject( DetectedObject )
-
- local DetectedObjectName = DetectedObject.Name
- self.DetectedObjectsIdentified[DetectedObjectName] = false
- end
-
- --- UnIdentify all detected objects during detection processing.
- -- @param #DETECTION_BASE self
- function DETECTION_BASE:UnIdentifyAllDetectedObjects()
-
- self.DetectedObjectsIdentified = {} -- Table will be garbage collected.
- end
-
- --- Gets a detected object with a given name.
- -- @param #DETECTION_BASE self
- -- @param #string ObjectName
- -- @return #DETECTION_BASE.DetectedObject
- function DETECTION_BASE:GetDetectedObject( ObjectName )
- self:F( ObjectName )
-
- if ObjectName then
- local DetectedObject = self.DetectedObjects[ObjectName]
-
- -- Only return detected objects that are alive!
- local DetectedUnit = UNIT:FindByName( ObjectName )
- if DetectedUnit and DetectedUnit:IsAlive() then
- if self:IsDetectedObjectIdentified( DetectedObject ) == false then
- return DetectedObject
- end
- end
- end
-
- return nil
- end
-
-
- --- Adds a new DetectedItem to the DetectedItems list.
- -- The DetectedItem is a table and contains a SET_UNIT in the field Set.
- -- @param #DETECTION_BASE self
- -- @param #string DetectedItemIndex The index of the DetectedItem.
- -- @param Core.Set#SET_UNIT Set (optional) The Set of Units to be added.
- -- @return #DETECTION_BASE.DetectedItem
- function DETECTION_BASE:AddDetectedItem( DetectedItemIndex, Set )
-
- local DetectedItem = {}
- self.DetectedItemCount = self.DetectedItemCount + 1
- self.DetectedItemMax = self.DetectedItemMax + 1
-
- if DetectedItemIndex then
- self.DetectedItems[DetectedItemIndex] = DetectedItem
- else
- self.DetectedItems[self.DetectedItemCount] = DetectedItem
- end
-
- DetectedItem.Set = Set or SET_UNIT:New()
- DetectedItem.ItemID = self.DetectedItemMax
- DetectedItem.Removed = false
-
- return DetectedItem
- end
-
- --- Adds a new DetectedItem to the DetectedItems list.
- -- The DetectedItem is a table and contains a SET_UNIT in the field Set.
- -- @param #DETECTION_BASE self
- -- @param #string DetectedItemIndex The index of the DetectedItem.
- -- @param Core.Set#SET_UNIT Set (optional) The Set of Units to be added.
- -- @param Core.Zone#ZONE_UNIT Zone (optional) The Zone to be added where the Units are located.
- -- @return #DETECTION_BASE.DetectedItem
- function DETECTION_BASE:AddDetectedItemZone( DetectedItemIndex, Set, Zone )
-
- local DetectedItem = self:AddDetectedItem( DetectedItemIndex, Set )
-
- DetectedItem.Zone = Zone
-
- return DetectedItem
- end
-
- --- Removes an existing DetectedItem from the DetectedItems list.
- -- The DetectedItem is a table and contains a SET_UNIT in the field Set.
- -- @param #DETECTION_BASE self
- -- @param #number DetectedItemIndex The index or position in the DetectedItems list where the item needs to be removed.
- function DETECTION_BASE:RemoveDetectedItem( DetectedItemIndex )
-
- self.DetectedItemCount = self.DetectedItemCount - 1
- self.DetectedItems[DetectedItemIndex] = nil
- end
-
-
- --- Get the detected @{Set#SET_BASE}s.
- -- @param #DETECTION_BASE self
- -- @return #DETECTION_BASE.DetectedItems
- function DETECTION_BASE:GetDetectedItems()
-
- return self.DetectedItems
- end
-
- --- Get the amount of SETs with detected objects.
- -- @param #DETECTION_BASE self
- -- @return #number Count
- function DETECTION_BASE:GetDetectedItemsCount()
-
- local DetectedCount = self.DetectedItemCount
- return DetectedCount
- end
-
- --- Get a detected item using a given numeric index.
- -- @param #DETECTION_BASE self
- -- @param #number Index
- -- @return #DETECTION_BASE.DetectedItem
- function DETECTION_BASE:GetDetectedItem( Index )
-
- local DetectedItem = self.DetectedItems[Index]
- if DetectedItem then
- return DetectedItem
- end
-
- return nil
- end
-
- --- Get the @{Set#SET_UNIT} of a detecttion area using a given numeric index.
- -- @param #DETECTION_BASE self
- -- @param #number Index
- -- @return Core.Set#SET_UNIT DetectedSet
- function DETECTION_BASE:GetDetectedSet( Index )
-
- local DetectedItem = self:GetDetectedItem( Index )
- local DetectedSetUnit = DetectedItem.Set
- if DetectedSetUnit then
- return DetectedSetUnit
- end
-
- return nil
- end
-
- do -- Zones
-
- --- Get the @{Zone#ZONE_UNIT} of a detection area using a given numeric index.
- -- @param #DETECTION_BASE self
- -- @param #number Index
- -- @return Core.Zone#ZONE_UNIT DetectedZone
- function DETECTION_BASE:GetDetectedZone( Index )
-
- local DetectedZone = self.DetectedItems[Index].Zone
- if DetectedZone then
- return DetectedZone
- end
-
- return nil
- end
-
- end
-
-
- --- Report summary of a detected item using a given numeric index.
- -- @param #DETECTION_BASE self
- -- @param Index
- -- @return #string
- function DETECTION_BASE:DetectedItemReportSummary( Index )
- self:F( Index )
- return nil
- end
-
- --- Report detailed of a detectedion result.
- -- @param #DETECTION_BASE self
- -- @return #string
- function DETECTION_BASE:DetectedReportDetailed()
- self:F()
- return nil
- end
-
- --- Get the detection Groups.
- -- @param #DETECTION_BASE self
- -- @return Wrapper.Group#GROUP
- function DETECTION_BASE:GetDetectionSetGroup()
-
- local DetectionSetGroup = self.DetectionSetGroup
- return DetectionSetGroup
- end
-
- --- Make a DetectionSet table. This function will be overridden in the derived clsses.
- -- @param #DETECTION_BASE self
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:CreateDetectionSets()
- self:F2()
-
- self:E( "Error, in DETECTION_BASE class..." )
-
- end
-
-
- --- Schedule the DETECTION construction.
- -- @param #DETECTION_BASE self
- -- @param #number DelayTime The delay in seconds to wait the reporting.
- -- @param #number RepeatInterval The repeat interval in seconds for the reporting to happen repeatedly.
- -- @return #DETECTION_BASE self
- function DETECTION_BASE:Schedule( DelayTime, RepeatInterval )
- self:F2()
-
- self.ScheduleDelayTime = DelayTime
- self.ScheduleRepeatInterval = RepeatInterval
-
- self.DetectionScheduler = SCHEDULER:New( self, self._DetectionScheduler, { self, "Detection" }, DelayTime, RepeatInterval )
- return self
- end
-
-end
-
-do -- DETECTION_UNITS
-
- --- # 2) DETECTION_UNITS class, extends @{Detection#DETECTION_BASE}
- --
- -- The DETECTION_UNITS class will detect units within the battle zone.
- -- It will build a DetectedItems list filled with DetectedItems. Each DetectedItem will contain a field Set, which contains a @{Set#SET_UNIT} containing ONE @{UNIT} object reference.
- -- Beware that when the amount of units detected is large, the DetectedItems list will be large also.
- --
- -- @type DETECTION_UNITS
- -- @field Dcs.DCSTypes#Distance DetectionRange The range till which targets are detected.
- -- @extends #DETECTION_BASE
- DETECTION_UNITS = {
- ClassName = "DETECTION_UNITS",
- DetectionRange = nil,
- }
-
- --- DETECTION_UNITS constructor.
- -- @param Functional.Detection#DETECTION_UNITS self
- -- @param Core.Set#SET_GROUP DetectionSetGroup The @{Set} of GROUPs in the Forward Air Controller role.
- -- @return Functional.Detection#DETECTION_UNITS self
- function DETECTION_UNITS:New( DetectionSetGroup )
-
- -- Inherits from DETECTION_BASE
- local self = BASE:Inherit( self, DETECTION_BASE:New( DetectionSetGroup ) ) -- #DETECTION_UNITS
-
- self._SmokeDetectedUnits = false
- self._FlareDetectedUnits = false
- self._SmokeDetectedZones = false
- self._FlareDetectedZones = false
- self._BoundDetectedZones = false
-
- return self
- end
-
- --- Make text documenting the changes of the detected zone.
- -- @param #DETECTION_UNITS self
- -- @param #DETECTION_UNITS.DetectedItem DetectedItem
- -- @return #string The Changes text
- function DETECTION_UNITS:GetChangeText( DetectedItem )
- self:F( DetectedItem )
-
- local MT = {}
-
- for ChangeCode, ChangeData in pairs( DetectedItem.Changes ) do
-
- if ChangeCode == "AU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = " New target(s) detected: " .. table.concat( MTUT, ", " ) .. "."
- end
-
- if ChangeCode == "RU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = " Invisible or destroyed target(s): " .. table.concat( MTUT, ", " ) .. "."
- end
-
- end
-
- return table.concat( MT, "\n" )
-
- end
-
-
- --- Create the DetectedItems list from the DetectedObjects table.
- -- For each DetectedItem, a one field array is created containing the Unit detected.
- -- @param #DETECTION_UNITS self
- -- @return #DETECTION_UNITS self
- function DETECTION_UNITS:CreateDetectionSets()
- self:F2( #self.DetectedObjects )
-
- -- Loop the current detected items, and check if each object still exists and is detected.
-
- for DetectedItemID, DetectedItem in pairs( self.DetectedItems ) do
-
- local DetectedItemSet = DetectedItem.Set -- Core.Set#SET_UNIT
- local DetectedTypeName = DetectedItem.Type
-
- for DetectedUnitName, DetectedUnitData in pairs( DetectedItemSet:GetSet() ) do
- local DetectedUnit = DetectedUnitData -- Wrapper.Unit#UNIT
-
- local DetectedObject = nil
- self:E( DetectedUnit )
- if DetectedUnit:IsAlive() then
- --self:E(DetectedUnit:GetName())
- DetectedObject = self:GetDetectedObject( DetectedUnit:GetName() )
- end
- if DetectedObject then
-
- -- Yes, the DetectedUnit is still detected or exists. Flag as identified.
- self:IdentifyDetectedObject( DetectedObject )
- else
- -- There was no DetectedObject, remove DetectedUnit from the Set.
- self:AddChangeUnit( DetectedItem, "RU", DetectedUnitName )
- DetectedItemSet:Remove( DetectedUnitName )
- end
- end
- end
-
-
- -- Now we need to loop through the unidentified detected units and add these... These are all new items.
- for DetectedUnitName, DetectedObjectData in pairs( self.DetectedObjects ) do
-
- local DetectedObject = self:GetDetectedObject( DetectedUnitName )
- if DetectedObject then
- self:T( { "Detected Unit #", DetectedUnitName } )
-
- local DetectedUnit = UNIT:FindByName( DetectedUnitName ) -- Wrapper.Unit#UNIT
-
- if DetectedUnit then
- local DetectedTypeName = DetectedUnit:GetTypeName()
- local DetectedItem = self:GetDetectedItem( DetectedUnitName )
- if not DetectedItem then
- self:T( "Added new DetectedItem" )
- DetectedItem = self:AddDetectedItem( DetectedUnitName )
- DetectedItem.Type = DetectedUnit:GetTypeName()
- DetectedItem.Name = DetectedObjectData.Name
- DetectedItem.Visible = DetectedObjectData.Visible
- DetectedItem.Distance = DetectedObjectData.Distance
- end
-
- DetectedItem.Set:AddUnit( DetectedUnit )
- self:AddChangeUnit( DetectedItem, "AU", DetectedTypeName )
- end
- end
- end
-
- for DetectedItemID, DetectedItemData in pairs( self.DetectedItems ) do
-
- local DetectedItem = DetectedItemData -- #DETECTION_BASE.DetectedItem
- local DetectedSet = DetectedItem.Set
-
- self:ReportFriendliesNearBy( { DetectedItem = DetectedItem, ReportSetGroup = self.DetectionSetGroup } ) -- Fill the Friendlies table
- --self:NearestFAC( DetectedItem )
- end
-
- end
-
- --- Report summary of a DetectedItem using a given numeric index.
- -- @param #DETECTION_UNITS self
- -- @param Index
- -- @return #string
- function DETECTION_UNITS:DetectedItemReportSummary( Index )
- self:F( Index )
-
- local DetectedItem = self:GetDetectedItem( Index )
- local DetectedSet = self:GetDetectedSet( Index )
-
- self:T( DetectedSet )
- if DetectedSet then
- local ReportSummary = ""
- local UnitDistanceText = ""
- local UnitCategoryText = ""
-
- local DetectedItemUnit = DetectedSet:GetFirst() -- Wrapper.Unit#UNIT
-
- if DetectedItemUnit and DetectedItemUnit:IsAlive() then
- self:T(DetectedItemUnit)
-
- local UnitCategoryName = DetectedItemUnit:GetCategoryName() or ""
- local UnitCategoryType = DetectedItemUnit:GetTypeName() or ""
-
- if DetectedItem.Type and UnitCategoryName and UnitCategoryType then
- UnitCategoryText = UnitCategoryName .. " (" .. UnitCategoryType .. ") at "
- else
- UnitCategoryText = "Unknown target at "
- end
-
- if DetectedItem.Visible == false then
- UnitDistanceText = string.format( "%.2f", DetectedItem.Distance ) .. " estimated km"
- else
- UnitDistanceText = string.format( "%.2f", DetectedItem.Distance ) .. " km, visual contact"
- end
-
- local DetectedItemPointVec3 = DetectedItemUnit:GetPointVec3()
- local DetectedItemPointLL = DetectedItemPointVec3:ToStringLL( 3, true )
-
- local ThreatLevelA2G = DetectedItemUnit:GetThreatLevel( DetectedItem )
-
- ReportSummary = string.format(
- "%s - Threat [%s] (%2d) - %s%s",
- DetectedItemPointLL,
- string.rep( "â– ", ThreatLevelA2G ),
- ThreatLevelA2G,
- UnitCategoryText,
- UnitDistanceText
- )
- end
-
- self:T( ReportSummary )
-
- return ReportSummary
- end
- end
-
- --- Report detailed of a detection result.
- -- @param #DETECTION_UNITS self
- -- @return #string
- function DETECTION_UNITS:DetectedReportDetailed()
- self:F()
-
- local Report = REPORT:New( "Detected units:" )
- for DetectedItemID, DetectedItem in pairs( self.DetectedItems ) do
- local DetectedItem = DetectedItem -- #DETECTION_BASE.DetectedItem
- local ReportSummary = self:DetectedItemReportSummary( DetectedItemID )
- Report:Add( ReportSummary )
- end
-
- local ReportText = Report:Text()
-
- return ReportText
- end
-
-end
-
-do -- DETECTION_TYPES
-
- --- # 3) DETECTION_TYPES class, extends @{Detection#DETECTION_BASE}
- --
- -- The DETECTION_TYPES class will detect units within the battle zone.
- -- It will build a DetectedItems[] list filled with DetectedItems, grouped by the type of units detected.
- -- Each DetectedItem will contain a field Set, which contains a @{Set#SET_UNIT} containing ONE @{UNIT} object reference.
- -- Beware that when the amount of different types detected is large, the DetectedItems[] list will be large also.
- --
- -- @type DETECTION_TYPES
- -- @extends #DETECTION_BASE
- DETECTION_TYPES = {
- ClassName = "DETECTION_TYPES",
- DetectionRange = nil,
- }
-
- --- DETECTION_TYPES constructor.
- -- @param Functional.Detection#DETECTION_TYPES self
- -- @param Core.Set#SET_GROUP DetectionSetGroup The @{Set} of GROUPs in the Recce role.
- -- @return Functional.Detection#DETECTION_TYPES self
- function DETECTION_TYPES:New( DetectionSetGroup )
-
- -- Inherits from DETECTION_BASE
- local self = BASE:Inherit( self, DETECTION_BASE:New( DetectionSetGroup ) ) -- #DETECTION_TYPES
-
- self._SmokeDetectedUnits = false
- self._FlareDetectedUnits = false
- self._SmokeDetectedZones = false
- self._FlareDetectedZones = false
- self._BoundDetectedZones = false
-
- return self
- end
-
- --- Make text documenting the changes of the detected zone.
- -- @param #DETECTION_TYPES self
- -- @param #DETECTION_TYPES.DetectedItem DetectedItem
- -- @return #string The Changes text
- function DETECTION_TYPES:GetChangeText( DetectedItem )
- self:F( DetectedItem )
-
- local MT = {}
-
- for ChangeCode, ChangeData in pairs( DetectedItem.Changes ) do
-
- if ChangeCode == "AU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = " New target(s) detected: " .. table.concat( MTUT, ", " ) .. "."
- end
-
- if ChangeCode == "RU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = " Invisible or destroyed target(s): " .. table.concat( MTUT, ", " ) .. "."
- end
-
- end
-
- return table.concat( MT, "\n" )
-
- end
-
-
- --- Create the DetectedItems list from the DetectedObjects table.
- -- For each DetectedItem, a one field array is created containing the Unit detected.
- -- @param #DETECTION_TYPES self
- -- @return #DETECTION_TYPES self
- function DETECTION_TYPES:CreateDetectionSets()
- self:F2( #self.DetectedObjects )
-
- -- Loop the current detected items, and check if each object still exists and is detected.
-
- for DetectedItemID, DetectedItem in pairs( self.DetectedItems ) do
-
- local DetectedItemSet = DetectedItem.Set -- Core.Set#SET_UNIT
- local DetectedTypeName = DetectedItem.Type
-
- for DetectedUnitName, DetectedUnitData in pairs( DetectedItemSet:GetSet() ) do
- local DetectedUnit = DetectedUnitData -- Wrapper.Unit#UNIT
-
- local DetectedObject = nil
- if DetectedUnit:IsAlive() then
- --self:E(DetectedUnit:GetName())
- DetectedObject = self:GetDetectedObject( DetectedUnit:GetName() )
- end
- if DetectedObject then
-
- -- Yes, the DetectedUnit is still detected or exists. Flag as identified.
- self:IdentifyDetectedObject( DetectedObject )
- else
- -- There was no DetectedObject, remove DetectedUnit from the Set.
- self:AddChangeUnit( DetectedItem, "RU", DetectedUnitName )
- DetectedItemSet:Remove( DetectedUnitName )
- end
- end
- end
-
-
- -- Now we need to loop through the unidentified detected units and add these... These are all new items.
- for DetectedUnitName, DetectedObjectData in pairs( self.DetectedObjects ) do
-
- local DetectedObject = self:GetDetectedObject( DetectedUnitName )
- if DetectedObject then
- self:T( { "Detected Unit #", DetectedUnitName } )
-
- local DetectedUnit = UNIT:FindByName( DetectedUnitName ) -- Wrapper.Unit#UNIT
-
- if DetectedUnit then
- local DetectedTypeName = DetectedUnit:GetTypeName()
- local DetectedItem = self:GetDetectedItem( DetectedTypeName )
- if not DetectedItem then
- DetectedItem = self:AddDetectedItem( DetectedTypeName )
- DetectedItem.Type = DetectedUnit:GetTypeName()
- end
-
- DetectedItem.Set:AddUnit( DetectedUnit )
- self:AddChangeUnit( DetectedItem, "AU", DetectedTypeName )
- end
- end
- end
-
- for DetectedItemID, DetectedItemData in pairs( self.DetectedItems ) do
-
- local DetectedItem = DetectedItemData -- #DETECTION_BASE.DetectedItem
- local DetectedSet = DetectedItem.Set
-
- self:ReportFriendliesNearBy( { DetectedItem = DetectedItem, ReportSetGroup = self.DetectionSetGroup } ) -- Fill the Friendlies table
- --self:NearestFAC( DetectedItem )
- end
-
- end
-
- --- Report summary of a DetectedItem using a given numeric index.
- -- @param #DETECTION_TYPES self
- -- @param Index
- -- @return #string
- function DETECTION_TYPES:DetectedItemReportSummary( DetectedTypeName )
- self:F( DetectedTypeName )
-
- local DetectedItem = self:GetDetectedItem( DetectedTypeName )
- local DetectedSet = self:GetDetectedSet( DetectedTypeName )
-
- self:T( DetectedItem )
- if DetectedItem then
-
- local ThreatLevelA2G = DetectedSet:CalculateThreatLevelA2G()
- local DetectedItemsCount = DetectedSet:Count()
- local DetectedItemType = DetectedItem.Type
-
- local ReportSummary = string.format(
- "Threat [%s] (%2d) - %2d of %s",
- string.rep( "â– ", ThreatLevelA2G ),
- ThreatLevelA2G,
- DetectedItemsCount,
- DetectedItemType
- )
- self:T( ReportSummary )
-
- return ReportSummary
- end
- end
-
- --- Report detailed of a detection result.
- -- @param #DETECTION_TYPES self
- -- @return #string
- function DETECTION_TYPES:DetectedReportDetailed()
- self:F()
-
- local Report = REPORT:New( "Detected types:" )
- for DetectedItemTypeName, DetectedItem in pairs( self.DetectedItems ) do
- local DetectedItem = DetectedItem -- #DETECTION_BASE.DetectedItem
- local ReportSummary = self:DetectedItemReportSummary( DetectedItemTypeName )
- Report:Add( ReportSummary )
- end
-
- local ReportText = Report:Text()
-
- return ReportText
- end
-
-end
-
-
-do -- DETECTION_AREAS
-
- --- # 4) DETECTION_AREAS class, extends @{Detection#DETECTION_BASE}
- --
- -- The DETECTION_AREAS class will detect units within the battle zone for a list of @{Group}s detecting targets following (a) detection method(s),
- -- and will build a list (table) of @{Set#SET_UNIT}s containing the @{Unit#UNIT}s detected.
- -- The class is group the detected units within zones given a DetectedZoneRange parameter.
- -- A set with multiple detected zones will be created as there are groups of units detected.
- --
- -- ## 4.1) Retrieve the Detected Unit Sets and Detected Zones
- --
- -- The methods to manage the DetectedItems[].Set(s) are implemented in @{Detection#DECTECTION_BASE} and
- -- the methods to manage the DetectedItems[].Zone(s) is implemented in @{Detection#DETECTION_AREAS}.
- --
- -- Retrieve the DetectedItems[].Set with the method @{Detection#DETECTION_BASE.GetDetectedSet}(). A @{Set#SET_UNIT} object will be returned.
- --
- -- Retrieve the formed @{Zone@ZONE_UNIT}s as a result of the grouping the detected units within the DetectionZoneRange, use the method @{Detection#DETECTION_BASE.GetDetectionZones}().
- -- To understand the amount of zones created, use the method @{Detection#DETECTION_BASE.GetDetectionZoneCount}().
- -- If you want to obtain a specific zone from the DetectedZones, use the method @{Detection#DETECTION_BASE.GetDetectionZone}() with a given index.
- --
- -- ## 4.4) Flare or Smoke detected units
- --
- -- Use the methods @{Detection#DETECTION_AREAS.FlareDetectedUnits}() or @{Detection#DETECTION_AREAS.SmokeDetectedUnits}() to flare or smoke the detected units when a new detection has taken place.
- --
- -- ## 4.5) Flare or Smoke or Bound detected zones
- --
- -- Use the methods:
- --
- -- * @{Detection#DETECTION_AREAS.FlareDetectedZones}() to flare in a color
- -- * @{Detection#DETECTION_AREAS.SmokeDetectedZones}() to smoke in a color
- -- * @{Detection#DETECTION_AREAS.SmokeDetectedZones}() to bound with a tire with a white flag
- --
- -- the detected zones when a new detection has taken place.
- --
- -- @type DETECTION_AREAS
- -- @field Dcs.DCSTypes#Distance DetectionZoneRange The range till which targets are grouped upon the first detected target.
- -- @field #DETECTION_BASE.DetectedItems DetectedItems A list of areas containing the set of @{Unit}s, @{Zone}s, the center @{Unit} within the zone, and ID of each area that was detected within a DetectionZoneRange.
- -- @extends #DETECTION_BASE
- DETECTION_AREAS = {
- ClassName = "DETECTION_AREAS",
- DetectionZoneRange = nil,
- }
-
-
- --- DETECTION_AREAS constructor.
- -- @param #DETECTION_AREAS self
- -- @param Core.Set#SET_GROUP DetectionSetGroup The @{Set} of GROUPs in the Forward Air Controller role.
- -- @param Dcs.DCSTypes#Distance DetectionZoneRange The range till which targets are grouped upon the first detected target.
- -- @return #DETECTION_AREAS
- function DETECTION_AREAS:New( DetectionSetGroup, DetectionZoneRange )
-
- -- Inherits from DETECTION_BASE
- local self = BASE:Inherit( self, DETECTION_BASE:New( DetectionSetGroup ) )
-
- self.DetectionZoneRange = DetectionZoneRange
-
- self._SmokeDetectedUnits = false
- self._FlareDetectedUnits = false
- self._SmokeDetectedZones = false
- self._FlareDetectedZones = false
- self._BoundDetectedZones = false
-
- return self
- end
-
- --- Report summary of a detected item using a given numeric index.
- -- @param #DETECTION_AREAS self
- -- @param Index
- -- @return #string
- function DETECTION_AREAS:DetectedItemReportSummary( Index )
- self:F( Index )
-
- local DetectedItem = self:GetDetectedItem( Index )
- if DetectedItem then
- local DetectedSet = self:GetDetectedSet( Index )
- local ReportSummaryItem
-
- local DetectedZone = self:GetDetectedZone( Index )
- local DetectedItemPointVec3 = DetectedZone:GetPointVec3()
- local DetectedItemPointLL = DetectedItemPointVec3:ToStringLL( 3, true )
-
- local ThreatLevelA2G = self:GetTreatLevelA2G( DetectedItem )
- local DetectedItemsCount = DetectedSet:Count()
- local DetectedItemsTypes = DetectedSet:GetTypeNames()
-
- local ReportSummary = string.format(
- "%s - Threat [%s] (%2d) - %2d of %s",
- DetectedItemPointLL,
- string.rep( "â– ", ThreatLevelA2G ),
- ThreatLevelA2G,
- DetectedItemsCount,
- DetectedItemsTypes
- )
-
- return ReportSummary
- end
-
- return nil
- end
-
-
- --- Returns if there are friendlies nearby the FAC units ...
- -- @param #DETECTION_AREAS self
- -- @return #boolean trhe if there are friendlies nearby
- function DETECTION_AREAS:IsFriendliesNearBy( DetectedItem )
-
- self:T3( DetectedItem.FriendliesNearBy )
- return DetectedItem.FriendliesNearBy or false
- end
-
- --- Calculate the maxium A2G threat level of the DetectedItem.
- -- @param #DETECTION_AREAS self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- function DETECTION_AREAS:CalculateThreatLevelA2G( DetectedItem )
-
- local MaxThreatLevelA2G = 0
- for UnitName, UnitData in pairs( DetectedItem.Set:GetSet() ) do
- local ThreatUnit = UnitData -- Wrapper.Unit#UNIT
- local ThreatLevelA2G = ThreatUnit:GetThreatLevel()
- if ThreatLevelA2G > MaxThreatLevelA2G then
- MaxThreatLevelA2G = ThreatLevelA2G
- end
- end
-
- self:T3( MaxThreatLevelA2G )
- DetectedItem.MaxThreatLevelA2G = MaxThreatLevelA2G
-
- end
-
- --- Find the nearest FAC of the DetectedItem.
- -- @param #DETECTION_AREAS self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @return Wrapper.Unit#UNIT The nearest FAC unit
- function DETECTION_AREAS:NearestFAC( DetectedItem )
-
- local NearestFAC = nil
- local MinDistance = 1000000000 -- Units are not further than 1000000 km away from an area :-)
-
- for FACGroupName, FACGroupData in pairs( self.DetectionSetGroup:GetSet() ) do
- for FACUnit, FACUnitData in pairs( FACGroupData:GetUnits() ) do
- local FACUnit = FACUnitData -- Wrapper.Unit#UNIT
- if FACUnit:IsActive() then
- local Vec3 = FACUnit:GetVec3()
- local PointVec3 = POINT_VEC3:NewFromVec3( Vec3 )
- local Distance = PointVec3:Get2DDistance(POINT_VEC3:NewFromVec3( FACUnit:GetVec3() ) )
- if Distance < MinDistance then
- MinDistance = Distance
- NearestFAC = FACUnit
- end
- end
- end
- end
-
- DetectedItem.NearestFAC = NearestFAC
-
- end
-
- --- Returns the A2G threat level of the units in the DetectedItem
- -- @param #DETECTION_AREAS self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @return #number a scale from 0 to 10.
- function DETECTION_AREAS:GetTreatLevelA2G( DetectedItem )
-
- self:T3( DetectedItem.MaxThreatLevelA2G )
- return DetectedItem.MaxThreatLevelA2G
- end
-
-
-
- --- Smoke the detected units
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:SmokeDetectedUnits()
- self:F2()
-
- self._SmokeDetectedUnits = true
- return self
- end
-
- --- Flare the detected units
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:FlareDetectedUnits()
- self:F2()
-
- self._FlareDetectedUnits = true
- return self
- end
-
- --- Smoke the detected zones
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:SmokeDetectedZones()
- self:F2()
-
- self._SmokeDetectedZones = true
- return self
- end
-
- --- Flare the detected zones
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:FlareDetectedZones()
- self:F2()
-
- self._FlareDetectedZones = true
- return self
- end
-
- --- Bound the detected zones
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:BoundDetectedZones()
- self:F2()
-
- self._BoundDetectedZones = true
- return self
- end
-
- --- Make text documenting the changes of the detected zone.
- -- @param #DETECTION_AREAS self
- -- @param #DETECTION_BASE.DetectedItem DetectedItem
- -- @return #string The Changes text
- function DETECTION_AREAS:GetChangeText( DetectedItem )
- self:F( DetectedItem )
-
- local MT = {}
-
- for ChangeCode, ChangeData in pairs( DetectedItem.Changes ) do
-
- if ChangeCode == "AA" then
- MT[#MT+1] = "Detected new area " .. ChangeData.ItemID .. ". The center target is a " .. ChangeData.ItemUnitType .. "."
- end
-
- if ChangeCode == "RAU" then
- MT[#MT+1] = "Changed area " .. ChangeData.ItemID .. ". Removed the center target."
- end
-
- if ChangeCode == "AAU" then
- MT[#MT+1] = "Changed area " .. ChangeData.ItemID .. ". The new center target is a " .. ChangeData.ItemUnitType .. "."
- end
-
- if ChangeCode == "RA" then
- MT[#MT+1] = "Removed old area " .. ChangeData.ItemID .. ". No more targets in this area."
- end
-
- if ChangeCode == "AU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = "Detected for area " .. ChangeData.ItemID .. " new target(s) " .. table.concat( MTUT, ", " ) .. "."
- end
-
- if ChangeCode == "RU" then
- local MTUT = {}
- for ChangeUnitType, ChangeUnitCount in pairs( ChangeData ) do
- if ChangeUnitType ~= "ItemID" then
- MTUT[#MTUT+1] = ChangeUnitCount .. " of " .. ChangeUnitType
- end
- end
- MT[#MT+1] = "Removed for area " .. ChangeData.ItemID .. " invisible or destroyed target(s) " .. table.concat( MTUT, ", " ) .. "."
- end
-
- end
-
- return table.concat( MT, "\n" )
-
- end
-
-
- --- Make a DetectionSet table. This function will be overridden in the derived clsses.
- -- @param #DETECTION_AREAS self
- -- @return #DETECTION_AREAS self
- function DETECTION_AREAS:CreateDetectionSets()
- self:F2()
-
-
- self:T( "Checking Detected Items for new Detected Units ..." )
- -- First go through all detected sets, and check if there are new detected units, match all existing detected units and identify undetected units.
- -- Regroup when needed, split groups when needed.
- for DetectedItemID, DetectedItemData in pairs( self.DetectedItems ) do
-
- local DetectedItem = DetectedItemData -- #DETECTION_BASE.DetectedItem
- if DetectedItem then
-
- self:T( { "Detected Item ID:", DetectedItemID } )
-
-
- local DetectedSet = DetectedItem.Set
-
- local AreaExists = false -- This flag will determine of the detected area is still existing.
-
- -- First test if the center unit is detected in the detection area.
- self:T3( { "Zone Center Unit:", DetectedItem.Zone.ZoneUNIT.UnitName } )
- local DetectedZoneObject = self:GetDetectedObject( DetectedItem.Zone.ZoneUNIT.UnitName )
- self:T3( { "Detected Zone Object:", DetectedItem.Zone:GetName(), DetectedZoneObject } )
-
- if DetectedZoneObject then
-
- --self:IdentifyDetectedObject( DetectedZoneObject )
- AreaExists = true
-
-
-
- else
- -- The center object of the detected area has not been detected. Find an other unit of the set to become the center of the area.
- -- First remove the center unit from the set.
- DetectedSet:RemoveUnitsByName( DetectedItem.Zone.ZoneUNIT.UnitName )
-
- self:AddChangeItem( DetectedItem, 'RAU', "Dummy" )
-
- -- Then search for a new center area unit within the set. Note that the new area unit candidate must be within the area range.
- for DetectedUnitName, DetectedUnitData in pairs( DetectedSet:GetSet() ) do
-
- local DetectedUnit = DetectedUnitData -- Wrapper.Unit#UNIT
- local DetectedObject = self:GetDetectedObject( DetectedUnit.UnitName )
-
- -- The DetectedObject can be nil when the DetectedUnit is not alive anymore or it is not in the DetectedObjects map.
- -- If the DetectedUnit was already identified, DetectedObject will be nil.
- if DetectedObject then
- self:IdentifyDetectedObject( DetectedObject )
- AreaExists = true
-
- DetectedItem.Zone:BoundZone( 12, self.CountryID, true)
-
- -- Assign the Unit as the new center unit of the detected area.
- DetectedItem.Zone = ZONE_UNIT:New( DetectedUnit:GetName(), DetectedUnit, self.DetectionZoneRange )
-
- self:AddChangeItem( DetectedItem, "AAU", DetectedItem.Zone.ZoneUNIT:GetTypeName() )
-
- -- We don't need to add the DetectedObject to the area set, because it is already there ...
- break
- end
- end
- end
-
- -- Now we've determined the center unit of the area, now we can iterate the units in the detected area.
- -- Note that the position of the area may have moved due to the center unit repositioning.
- -- If no center unit was identified, then the detected area does not exist anymore and should be deleted, as there are no valid units that can be the center unit.
- if AreaExists then
-
- -- ok, we found the center unit of the area, now iterate through the detected area set and see which units are still within the center unit zone ...
- -- Those units within the zone are flagged as Identified.
- -- If a unit was not found in the set, remove it from the set. This may be added later to other existing or new sets.
- for DetectedUnitName, DetectedUnitData in pairs( DetectedSet:GetSet() ) do
-
- local DetectedUnit = DetectedUnitData -- Wrapper.Unit#UNIT
- local DetectedObject = nil
- if DetectedUnit:IsAlive() then
- --self:E(DetectedUnit:GetName())
- DetectedObject = self:GetDetectedObject( DetectedUnit:GetName() )
- end
- if DetectedObject then
-
- -- Check if the DetectedUnit is within the DetectedItem.Zone
- if DetectedUnit:IsInZone( DetectedItem.Zone ) then
-
- -- Yes, the DetectedUnit is within the DetectedItem.Zone, no changes, DetectedUnit can be kept within the Set.
- self:IdentifyDetectedObject( DetectedObject )
-
- else
- -- No, the DetectedUnit is not within the DetectedItem.Zone, remove DetectedUnit from the Set.
- DetectedSet:Remove( DetectedUnitName )
- self:AddChangeUnit( DetectedItem, "RU", DetectedUnit:GetTypeName() )
- end
-
- else
- -- There was no DetectedObject, remove DetectedUnit from the Set.
- self:AddChangeUnit( DetectedItem, "RU", "destroyed target" )
- DetectedSet:Remove( DetectedUnitName )
-
- -- The DetectedObject has been identified, because it does not exist ...
- -- self:IdentifyDetectedObject( DetectedObject )
- end
- end
- else
- DetectedItem.Zone:BoundZone( 12, self.CountryID, true)
- self:RemoveDetectedItem( DetectedItemID )
- self:AddChangeItem( DetectedItem, "RA" )
- end
- end
- end
-
- -- We iterated through the existing detection areas and:
- -- - We checked which units are still detected in each detection area. Those units were flagged as Identified.
- -- - We recentered the detection area to new center units where it was needed.
- --
- -- Now we need to loop through the unidentified detected units and see where they belong:
- -- - They can be added to a new detection area and become the new center unit.
- -- - They can be added to a new detection area.
- for DetectedUnitName, DetectedObjectData in pairs( self.DetectedObjects ) do
-
- local DetectedObject = self:GetDetectedObject( DetectedUnitName )
-
- if DetectedObject then
-
- -- We found an unidentified unit outside of any existing detection area.
- local DetectedUnit = UNIT:FindByName( DetectedUnitName ) -- Wrapper.Unit#UNIT
-
- local AddedToDetectionArea = false
-
- for DetectedItemID, DetectedItemData in pairs( self.DetectedItems ) do
-
- local DetectedItem = DetectedItemData -- #DETECTION_BASE.DetectedItem
- if DetectedItem then
- self:T( "Detection Area #" .. DetectedItem.ItemID )
- local DetectedSet = DetectedItem.Set
- if not self:IsDetectedObjectIdentified( DetectedObject ) and DetectedUnit:IsInZone( DetectedItem.Zone ) then
- self:IdentifyDetectedObject( DetectedObject )
- DetectedSet:AddUnit( DetectedUnit )
- AddedToDetectionArea = true
- self:AddChangeUnit( DetectedItem, "AU", DetectedUnit:GetTypeName() )
- end
- end
- end
-
- if AddedToDetectionArea == false then
-
- -- New detection area
- local DetectedItem = self:AddDetectedItemZone( nil,
- SET_UNIT:New(),
- ZONE_UNIT:New( DetectedUnitName, DetectedUnit, self.DetectionZoneRange )
- )
- --self:E( DetectedItem.Zone.ZoneUNIT.UnitName )
- DetectedItem.Set:AddUnit( DetectedUnit )
- self:AddChangeItem( DetectedItem, "AA", DetectedUnit:GetTypeName() )
- end
- end
- end
-
- -- Now all the tests should have been build, now make some smoke and flares...
- -- We also report here the friendlies within the detected areas.
-
- for DetectedItemID, DetectedItemData in pairs( self.DetectedItems ) do
-
- local DetectedItem = DetectedItemData -- #DETECTION_BASE.DetectedItem
- local DetectedSet = DetectedItem.Set
- local DetectedZone = DetectedItem.Zone
-
- self:ReportFriendliesNearBy( { DetectedItem = DetectedItem, ReportSetGroup = self.DetectionSetGroup } ) -- Fill the Friendlies table
- self:CalculateThreatLevelA2G( DetectedItem ) -- Calculate A2G threat level
- self:NearestFAC( DetectedItem )
-
- if DETECTION_AREAS._SmokeDetectedUnits or self._SmokeDetectedUnits then
- DetectedZone.ZoneUNIT:SmokeRed()
- end
- DetectedSet:ForEachUnit(
- --- @param Wrapper.Unit#UNIT DetectedUnit
- function( DetectedUnit )
- if DetectedUnit:IsAlive() then
- self:T( "Detected Set #" .. DetectedItem.ItemID .. ":" .. DetectedUnit:GetName() )
- if DETECTION_AREAS._FlareDetectedUnits or self._FlareDetectedUnits then
- DetectedUnit:FlareGreen()
- end
- if DETECTION_AREAS._SmokeDetectedUnits or self._SmokeDetectedUnits then
- DetectedUnit:SmokeGreen()
- end
- end
- end
- )
- if DETECTION_AREAS._FlareDetectedZones or self._FlareDetectedZones then
- DetectedZone:FlareZone( SMOKECOLOR.White, 30, math.random( 0,90 ) )
- end
- if DETECTION_AREAS._SmokeDetectedZones or self._SmokeDetectedZones then
- DetectedZone:SmokeZone( SMOKECOLOR.White, 30 )
- end
-
- if DETECTION_AREAS._BoundDetectedZones or self._BoundDetectedZones then
- DetectedZone:BoundZone( 12, self.CountryID )
- end
- end
-
- end
-
-end
---- Single-Player:**No** / Multi-Player:**Yes** / AI:**Yes** / Human:**No** / Types:**All** -- **AI Balancing will replace in multi player missions
--- non-occupied human slots with AI groups, in order to provide an engaging simulation environment,
--- even when there are hardly any players in the mission.**
---
--- 
---
--- ===
---
--- # 1) @{AI_Balancer#AI_BALANCER} class, extends @{Fsm#FSM_SET}
---
--- The @{AI_Balancer#AI_BALANCER} class monitors and manages as many replacement AI groups as there are
--- CLIENTS in a SET_CLIENT collection, which are not occupied by human players.
--- In other words, use AI_BALANCER to simulate human behaviour by spawning in replacement AI in multi player missions.
---
--- The parent class @{Fsm#FSM_SET} manages the functionality to control the Finite State Machine (FSM).
--- The mission designer can tailor the behaviour of the AI_BALANCER, by defining event and state transition methods.
--- An explanation about state and event transition methods can be found in the @{FSM} module documentation.
---
--- The mission designer can tailor the AI_BALANCER behaviour, by implementing a state or event handling method for the following:
---
--- * **@{#AI_BALANCER.OnAfterSpawned}**( AISet, From, Event, To, AIGroup ): Define to add extra logic when an AI is spawned.
---
--- ## 1.1) AI_BALANCER construction
---
--- Create a new AI_BALANCER object with the @{#AI_BALANCER.New}() method:
---
--- ## 1.2) AI_BALANCER is a FSM
---
--- 
---
--- ### 1.2.1) AI_BALANCER States
---
--- * **Monitoring** ( Set ): Monitoring the Set if all AI is spawned for the Clients.
--- * **Spawning** ( Set, ClientName ): There is a new AI group spawned with ClientName as the name of reference.
--- * **Spawned** ( Set, AIGroup ): A new AI has been spawned. You can handle this event to customize the AI behaviour with other AI FSMs or own processes.
--- * **Destroying** ( Set, AIGroup ): The AI is being destroyed.
--- * **Returning** ( Set, AIGroup ): The AI is returning to the airbase specified by the ReturnToAirbase methods. Handle this state to customize the return behaviour of the AI, if any.
---
--- ### 1.2.2) AI_BALANCER Events
---
--- * **Monitor** ( Set ): Every 10 seconds, the Monitor event is triggered to monitor the Set.
--- * **Spawn** ( Set, ClientName ): Triggers when there is a new AI group to be spawned with ClientName as the name of reference.
--- * **Spawned** ( Set, AIGroup ): Triggers when a new AI has been spawned. You can handle this event to customize the AI behaviour with other AI FSMs or own processes.
--- * **Destroy** ( Set, AIGroup ): The AI is being destroyed.
--- * **Return** ( Set, AIGroup ): The AI is returning to the airbase specified by the ReturnToAirbase methods.
---
--- ## 1.3) AI_BALANCER spawn interval for replacement AI
---
--- Use the method @{#AI_BALANCER.InitSpawnInterval}() to set the earliest and latest interval in seconds that is waited until a new replacement AI is spawned.
---
--- ## 1.4) AI_BALANCER returns AI to Airbases
---
--- By default, When a human player joins a slot that is AI_BALANCED, the AI group will be destroyed by default.
--- However, there are 2 additional options that you can use to customize the destroy behaviour.
--- When a human player joins a slot, you can configure to let the AI return to:
---
--- * @{#AI_BALANCER.ReturnToHomeAirbase}: Returns the AI to the **home** @{Airbase#AIRBASE}.
--- * @{#AI_BALANCER.ReturnToNearestAirbases}: Returns the AI to the **nearest friendly** @{Airbase#AIRBASE}.
---
--- Note that when AI returns to an airbase, the AI_BALANCER will trigger the **Return** event and the AI will return,
--- otherwise the AI_BALANCER will trigger a **Destroy** event, and the AI will be destroyed.
---
--- ===
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-01-17: There is still a problem with AI being destroyed, but not respawned. Need to check further upon that.
---
--- 2017-01-08: AI_BALANCER:**InitSpawnInterval( Earliest, Latest )** added.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **[Dutch_Baron](https://forums.eagle.ru/member.php?u=112075)**: Working together with James has resulted in the creation of the AI_BALANCER class. James has shared his ideas on balancing AI with air units, and together we made a first design which you can use now :-)
--- * **SNAFU**: Had a couple of mails with the guys to validate, if the same concept in the GCI/CAP script could be reworked within MOOSE. None of the script code has been used however within the new AI_BALANCER moose class.
---
--- ### Authors:
---
--- * FlightControl: Framework Design & Programming and Documentation.
---
--- @module AI_Balancer
-
---- AI_BALANCER class
--- @type AI_BALANCER
--- @field Core.Set#SET_CLIENT SetClient
--- @field Functional.Spawn#SPAWN SpawnAI
--- @field Wrapper.Group#GROUP Test
--- @extends Core.Fsm#FSM_SET
-AI_BALANCER = {
- ClassName = "AI_BALANCER",
- PatrolZones = {},
- AIGroups = {},
- Earliest = 5, -- Earliest a new AI can be spawned is in 5 seconds.
- Latest = 60, -- Latest a new AI can be spawned is in 60 seconds.
-}
-
-
-
---- Creates a new AI_BALANCER object
--- @param #AI_BALANCER self
--- @param Core.Set#SET_CLIENT SetClient A SET\_CLIENT object that will contain the CLIENT objects to be monitored if they are alive or not (joined by a player).
--- @param Functional.Spawn#SPAWN SpawnAI The default Spawn object to spawn new AI Groups when needed.
--- @return #AI_BALANCER
-function AI_BALANCER:New( SetClient, SpawnAI )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_SET:New( SET_GROUP:New() ) ) -- AI.AI_Balancer#AI_BALANCER
-
- -- TODO: Define the OnAfterSpawned event
- self:SetStartState( "None" )
- self:AddTransition( "*", "Monitor", "Monitoring" )
- self:AddTransition( "*", "Spawn", "Spawning" )
- self:AddTransition( "Spawning", "Spawned", "Spawned" )
- self:AddTransition( "*", "Destroy", "Destroying" )
- self:AddTransition( "*", "Return", "Returning" )
-
- self.SetClient = SetClient
- self.SetClient:FilterOnce()
- self.SpawnAI = SpawnAI
-
- self.SpawnQueue = {}
-
- self.ToNearestAirbase = false
- self.ToHomeAirbase = false
-
- self:__Monitor( 1 )
-
- return self
-end
-
---- Sets the earliest to the latest interval in seconds how long AI_BALANCER will wait to spawn a new AI.
--- Provide 2 identical seconds if the interval should be a fixed amount of seconds.
--- @param #AI_BALANCER self
--- @param #number Earliest The earliest a new AI can be spawned in seconds.
--- @param #number Latest The latest a new AI can be spawned in seconds.
--- @return self
-function AI_BALANCER:InitSpawnInterval( Earliest, Latest )
-
- self.Earliest = Earliest
- self.Latest = Latest
-
- return self
-end
-
---- Returns the AI to the nearest friendly @{Airbase#AIRBASE}.
--- @param #AI_BALANCER self
--- @param Dcs.DCSTypes#Distance ReturnTresholdRange If there is an enemy @{Client#CLIENT} within the ReturnTresholdRange given in meters, the AI will not return to the nearest @{Airbase#AIRBASE}.
--- @param Core.Set#SET_AIRBASE ReturnAirbaseSet The SET of @{Set#SET_AIRBASE}s to evaluate where to return to.
-function AI_BALANCER:ReturnToNearestAirbases( ReturnTresholdRange, ReturnAirbaseSet )
-
- self.ToNearestAirbase = true
- self.ReturnTresholdRange = ReturnTresholdRange
- self.ReturnAirbaseSet = ReturnAirbaseSet
-end
-
---- Returns the AI to the home @{Airbase#AIRBASE}.
--- @param #AI_BALANCER self
--- @param Dcs.DCSTypes#Distance ReturnTresholdRange If there is an enemy @{Client#CLIENT} within the ReturnTresholdRange given in meters, the AI will not return to the nearest @{Airbase#AIRBASE}.
-function AI_BALANCER:ReturnToHomeAirbase( ReturnTresholdRange )
-
- self.ToHomeAirbase = true
- self.ReturnTresholdRange = ReturnTresholdRange
-end
-
---- @param #AI_BALANCER self
--- @param Core.Set#SET_GROUP SetGroup
--- @param #string ClientName
--- @param Wrapper.Group#GROUP AIGroup
-function AI_BALANCER:onenterSpawning( SetGroup, From, Event, To, ClientName )
-
- -- OK, Spawn a new group from the default SpawnAI object provided.
- local AIGroup = self.SpawnAI:Spawn() -- Wrapper.Group#GROUP
- if AIGroup then
- AIGroup:E( "Spawning new AIGroup" )
- --TODO: need to rework UnitName thing ...
-
- SetGroup:Add( ClientName, AIGroup )
- self.SpawnQueue[ClientName] = nil
-
- -- Fire the Spawned event. The first parameter is the AIGroup just Spawned.
- -- Mission designers can catch this event to bind further actions to the AIGroup.
- self:Spawned( AIGroup )
- end
-end
-
---- @param #AI_BALANCER self
--- @param Core.Set#SET_GROUP SetGroup
--- @param Wrapper.Group#GROUP AIGroup
-function AI_BALANCER:onenterDestroying( SetGroup, From, Event, To, ClientName, AIGroup )
-
- AIGroup:Destroy()
- SetGroup:Flush()
- SetGroup:Remove( ClientName )
- SetGroup:Flush()
-end
-
---- @param #AI_BALANCER self
--- @param Core.Set#SET_GROUP SetGroup
--- @param Wrapper.Group#GROUP AIGroup
-function AI_BALANCER:onenterReturning( SetGroup, From, Event, To, AIGroup )
-
- local AIGroupTemplate = AIGroup:GetTemplate()
- if self.ToHomeAirbase == true then
- local WayPointCount = #AIGroupTemplate.route.points
- local SwitchWayPointCommand = AIGroup:CommandSwitchWayPoint( 1, WayPointCount, 1 )
- AIGroup:SetCommand( SwitchWayPointCommand )
- AIGroup:MessageToRed( "Returning to home base ...", 30 )
- else
- -- Okay, we need to send this Group back to the nearest base of the Coalition of the AI.
- --TODO: i need to rework the POINT_VEC2 thing.
- local PointVec2 = POINT_VEC2:New( AIGroup:GetVec2().x, AIGroup:GetVec2().y )
- local ClosestAirbase = self.ReturnAirbaseSet:FindNearestAirbaseFromPointVec2( PointVec2 )
- self:T( ClosestAirbase.AirbaseName )
- AIGroup:MessageToRed( "Returning to " .. ClosestAirbase:GetName().. " ...", 30 )
- local RTBRoute = AIGroup:RouteReturnToAirbase( ClosestAirbase )
- AIGroupTemplate.route = RTBRoute
- AIGroup:Respawn( AIGroupTemplate )
- end
-
-end
-
-
---- @param #AI_BALANCER self
-function AI_BALANCER:onenterMonitoring( SetGroup )
-
- self:T2( { self.SetClient:Count() } )
- --self.SetClient:Flush()
-
- self.SetClient:ForEachClient(
- --- @param Wrapper.Client#CLIENT Client
- function( Client )
- self:T3(Client.ClientName)
-
- local AIGroup = self.Set:Get( Client.UnitName ) -- Wrapper.Group#GROUP
- if Client:IsAlive() then
-
- if AIGroup and AIGroup:IsAlive() == true then
-
- if self.ToNearestAirbase == false and self.ToHomeAirbase == false then
- self:Destroy( Client.UnitName, AIGroup )
- else
- -- We test if there is no other CLIENT within the self.ReturnTresholdRange of the first unit of the AI group.
- -- If there is a CLIENT, the AI stays engaged and will not return.
- -- If there is no CLIENT within the self.ReturnTresholdRange, then the unit will return to the Airbase return method selected.
-
- local PlayerInRange = { Value = false }
- local RangeZone = ZONE_RADIUS:New( 'RangeZone', AIGroup:GetVec2(), self.ReturnTresholdRange )
-
- self:T2( RangeZone )
-
- _DATABASE:ForEachPlayer(
- --- @param Wrapper.Unit#UNIT RangeTestUnit
- function( RangeTestUnit, RangeZone, AIGroup, PlayerInRange )
- self:T2( { PlayerInRange, RangeTestUnit.UnitName, RangeZone.ZoneName } )
- if RangeTestUnit:IsInZone( RangeZone ) == true then
- self:T2( "in zone" )
- if RangeTestUnit:GetCoalition() ~= AIGroup:GetCoalition() then
- self:T2( "in range" )
- PlayerInRange.Value = true
- end
- end
- end,
-
- --- @param Core.Zone#ZONE_RADIUS RangeZone
- -- @param Wrapper.Group#GROUP AIGroup
- function( RangeZone, AIGroup, PlayerInRange )
- if PlayerInRange.Value == false then
- self:Return( AIGroup )
- end
- end
- , RangeZone, AIGroup, PlayerInRange
- )
-
- end
- self.Set:Remove( Client.UnitName )
- end
- else
- if not AIGroup or not AIGroup:IsAlive() == true then
- self:T( "Client " .. Client.UnitName .. " not alive." )
- if not self.SpawnQueue[Client.UnitName] then
- -- Spawn a new AI taking into account the spawn interval Earliest, Latest
- self:__Spawn( math.random( self.Earliest, self.Latest ), Client.UnitName )
- self.SpawnQueue[Client.UnitName] = true
- self:E( "New AI Spawned for Client " .. Client.UnitName )
- end
- end
- end
- return true
- end
- )
-
- self:__Monitor( 10 )
-end
-
-
-
---- **AI** -- **Air Patrolling or Staging.**
---
--- 
---
--- ===
---
--- AI PATROL classes makes AI Controllables execute an Patrol.
---
--- There are the following types of PATROL classes defined:
---
--- * @{#AI_PATROL_ZONE}: Perform a PATROL in a zone.
---
--- ====
---
--- # **OPEN ISSUES**
---
--- 2017-01-17: When Spawned AI is located at an airbase, it will be routed first back to the airbase after take-off.
---
--- 2016-01-17:
--- -- Fixed problem with AI returning to base too early and unexpected.
--- -- ReSpawning of AI will reset the AI_PATROL and derived classes.
--- -- Checked the correct workings of SCHEDULER, and it DOES work correctly.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-01-17: Rename of class: **AI\_PATROL\_ZONE** is the new name for the old _AI\_PATROLZONE_.
---
--- 2017-01-15: Complete revision. AI_PATROL_ZONE is the base class for other AI_PATROL like classes.
---
--- 2016-09-01: Initial class and API.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **[Dutch_Baron](https://forums.eagle.ru/member.php?u=112075)**: Working together with James has resulted in the creation of the AI_BALANCER class. James has shared his ideas on balancing AI with air units, and together we made a first design which you can use now :-)
--- * **[Pikey](https://forums.eagle.ru/member.php?u=62835)**: Testing and API concept review.
---
--- ### Authors:
---
--- * **FlightControl**: Design & Programming.
---
--- @module AI_Patrol
-
---- AI_PATROL_ZONE class
--- @type AI_PATROL_ZONE
--- @field Wrapper.Controllable#CONTROLLABLE AIControllable The @{Controllable} patrolling.
--- @field Core.Zone#ZONE_BASE PatrolZone The @{Zone} where the patrol needs to be executed.
--- @field Dcs.DCSTypes#Altitude PatrolFloorAltitude The lowest altitude in meters where to execute the patrol.
--- @field Dcs.DCSTypes#Altitude PatrolCeilingAltitude The highest altitude in meters where to execute the patrol.
--- @field Dcs.DCSTypes#Speed PatrolMinSpeed The minimum speed of the @{Controllable} in km/h.
--- @field Dcs.DCSTypes#Speed PatrolMaxSpeed The maximum speed of the @{Controllable} in km/h.
--- @field Functional.Spawn#SPAWN CoordTest
--- @extends Core.Fsm#FSM_CONTROLLABLE
-
---- # 1) @{#AI_PATROL_ZONE} class, extends @{Fsm#FSM_CONTROLLABLE}
---
--- The @{#AI_PATROL_ZONE} class implements the core functions to patrol a @{Zone} by an AI @{Controllable} or @{Group}.
---
--- 
---
--- The AI_PATROL_ZONE is assigned a @{Group} and this must be done before the AI_PATROL_ZONE process can be started using the **Start** event.
---
--- 
---
--- The AI will fly towards the random 3D point within the patrol zone, using a random speed within the given altitude and speed limits.
--- Upon arrival at the 3D point, a new random 3D point will be selected within the patrol zone using the given limits.
---
--- 
---
--- This cycle will continue.
---
--- 
---
--- During the patrol, the AI will detect enemy targets, which are reported through the **Detected** event.
---
--- 
---
----- Note that the enemy is not engaged! To model enemy engagement, either tailor the **Detected** event, or
--- use derived AI_ classes to model AI offensive or defensive behaviour.
---
--- 
---
--- Until a fuel or damage treshold has been reached by the AI, or when the AI is commanded to RTB.
--- When the fuel treshold has been reached, the airplane will fly towards the nearest friendly airbase and will land.
---
--- 
---
--- ## 1.1) AI_PATROL_ZONE constructor
---
--- * @{#AI_PATROL_ZONE.New}(): Creates a new AI_PATROL_ZONE object.
---
--- ## 1.2) AI_PATROL_ZONE is a FSM
---
--- 
---
--- ### 1.2.1) AI_PATROL_ZONE States
---
--- * **None** ( Group ): The process is not started yet.
--- * **Patrolling** ( Group ): The AI is patrolling the Patrol Zone.
--- * **Returning** ( Group ): The AI is returning to Base.
--- * **Stopped** ( Group ): The process is stopped.
--- * **Crashed** ( Group ): The AI has crashed or is dead.
---
--- ### 1.2.2) AI_PATROL_ZONE Events
---
--- * **Start** ( Group ): Start the process.
--- * **Stop** ( Group ): Stop the process.
--- * **Route** ( Group ): Route the AI to a new random 3D point within the Patrol Zone.
--- * **RTB** ( Group ): Route the AI to the home base.
--- * **Detect** ( Group ): The AI is detecting targets.
--- * **Detected** ( Group ): The AI has detected new targets.
--- * **Status** ( Group ): The AI is checking status (fuel and damage). When the tresholds have been reached, the AI will RTB.
---
--- ## 1.3) Set or Get the AI controllable
---
--- * @{#AI_PATROL_ZONE.SetControllable}(): Set the AIControllable.
--- * @{#AI_PATROL_ZONE.GetControllable}(): Get the AIControllable.
---
--- ## 1.4) Set the Speed and Altitude boundaries of the AI controllable
---
--- * @{#AI_PATROL_ZONE.SetSpeed}(): Set the patrol speed boundaries of the AI, for the next patrol.
--- * @{#AI_PATROL_ZONE.SetAltitude}(): Set altitude boundaries of the AI, for the next patrol.
---
--- ## 1.5) Manage the detection process of the AI controllable
---
--- The detection process of the AI controllable can be manipulated.
--- Detection requires an amount of CPU power, which has an impact on your mission performance.
--- Only put detection on when absolutely necessary, and the frequency of the detection can also be set.
---
--- * @{#AI_PATROL_ZONE.SetDetectionOn}(): Set the detection on. The AI will detect for targets.
--- * @{#AI_PATROL_ZONE.SetDetectionOff}(): Set the detection off, the AI will not detect for targets. The existing target list will NOT be erased.
---
--- The detection frequency can be set with @{#AI_PATROL_ZONE.SetDetectionInterval}( seconds ), where the amount of seconds specify how much seconds will be waited before the next detection.
--- Use the method @{#AI_PATROL_ZONE.GetDetectedUnits}() to obtain a list of the @{Unit}s detected by the AI.
---
--- The detection can be filtered to potential targets in a specific zone.
--- Use the method @{#AI_PATROL_ZONE.SetDetectionZone}() to set the zone where targets need to be detected.
--- Note that when the zone is too far away, or the AI is not heading towards the zone, or the AI is too high, no targets may be detected
--- according the weather conditions.
---
--- ## 1.6) Manage the "out of fuel" in the AI_PATROL_ZONE
---
--- When the AI is out of fuel, it is required that a new AI is started, before the old AI can return to the home base.
--- Therefore, with a parameter and a calculation of the distance to the home base, the fuel treshold is calculated.
--- When the fuel treshold is reached, the AI will continue for a given time its patrol task in orbit,
--- while a new AI is targetted to the AI_PATROL_ZONE.
--- Once the time is finished, the old AI will return to the base.
--- Use the method @{#AI_PATROL_ZONE.ManageFuel}() to have this proces in place.
---
--- ## 1.7) Manage "damage" behaviour of the AI in the AI_PATROL_ZONE
---
--- When the AI is damaged, it is required that a new AIControllable is started. However, damage cannon be foreseen early on.
--- Therefore, when the damage treshold is reached, the AI will return immediately to the home base (RTB).
--- Use the method @{#AI_PATROL_ZONE.ManageDamage}() to have this proces in place.
---
--- ===
---
--- @field #AI_PATROL_ZONE AI_PATROL_ZONE
---
-AI_PATROL_ZONE = {
- ClassName = "AI_PATROL_ZONE",
-}
-
---- Creates a new AI_PATROL_ZONE object
--- @param #AI_PATROL_ZONE self
--- @param Core.Zone#ZONE_BASE PatrolZone The @{Zone} where the patrol needs to be executed.
--- @param Dcs.DCSTypes#Altitude PatrolFloorAltitude The lowest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Altitude PatrolCeilingAltitude The highest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Speed PatrolMinSpeed The minimum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#Speed PatrolMaxSpeed The maximum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#AltitudeType PatrolAltType The altitude type ("RADIO"=="AGL", "BARO"=="ASL"). Defaults to RADIO
--- @return #AI_PATROL_ZONE self
--- @usage
--- -- Define a new AI_PATROL_ZONE Object. This PatrolArea will patrol an AIControllable within PatrolZone between 3000 and 6000 meters, with a variying speed between 600 and 900 km/h.
--- PatrolZone = ZONE:New( 'PatrolZone' )
--- PatrolSpawn = SPAWN:New( 'Patrol Group' )
--- PatrolArea = AI_PATROL_ZONE:New( PatrolZone, 3000, 6000, 600, 900 )
-function AI_PATROL_ZONE:New( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed, PatrolAltType )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_CONTROLLABLE:New() ) -- #AI_PATROL_ZONE
-
-
- self.PatrolZone = PatrolZone
- self.PatrolFloorAltitude = PatrolFloorAltitude
- self.PatrolCeilingAltitude = PatrolCeilingAltitude
- self.PatrolMinSpeed = PatrolMinSpeed
- self.PatrolMaxSpeed = PatrolMaxSpeed
-
- -- defafult PatrolAltType to "RADIO" if not specified
- self.PatrolAltType = PatrolAltType or "RADIO"
-
- self:SetDetectionInterval( 30 )
-
- self.CheckStatus = true
-
- self:ManageFuel( .2, 60 )
- self:ManageDamage( 1 )
-
-
- self.DetectedUnits = {} -- This table contains the targets detected during patrol.
-
- self:SetStartState( "None" )
-
- self:AddTransition( "*", "Stop", "Stopped" )
-
---- OnLeave Transition Handler for State Stopped.
--- @function [parent=#AI_PATROL_ZONE] OnLeaveStopped
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnEnter Transition Handler for State Stopped.
--- @function [parent=#AI_PATROL_ZONE] OnEnterStopped
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- OnBefore Transition Handler for Event Stop.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeStop
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Stop.
--- @function [parent=#AI_PATROL_ZONE] OnAfterStop
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Stop.
--- @function [parent=#AI_PATROL_ZONE] Stop
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Stop.
--- @function [parent=#AI_PATROL_ZONE] __Stop
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
- self:AddTransition( "None", "Start", "Patrolling" )
-
---- OnBefore Transition Handler for Event Start.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeStart
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Start.
--- @function [parent=#AI_PATROL_ZONE] OnAfterStart
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Start.
--- @function [parent=#AI_PATROL_ZONE] Start
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Start.
--- @function [parent=#AI_PATROL_ZONE] __Start
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
---- OnLeave Transition Handler for State Patrolling.
--- @function [parent=#AI_PATROL_ZONE] OnLeavePatrolling
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnEnter Transition Handler for State Patrolling.
--- @function [parent=#AI_PATROL_ZONE] OnEnterPatrolling
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
- self:AddTransition( "Patrolling", "Route", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
---- OnBefore Transition Handler for Event Route.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeRoute
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Route.
--- @function [parent=#AI_PATROL_ZONE] OnAfterRoute
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Route.
--- @function [parent=#AI_PATROL_ZONE] Route
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Route.
--- @function [parent=#AI_PATROL_ZONE] __Route
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Status", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
---- OnBefore Transition Handler for Event Status.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeStatus
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Status.
--- @function [parent=#AI_PATROL_ZONE] OnAfterStatus
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Status.
--- @function [parent=#AI_PATROL_ZONE] Status
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Status.
--- @function [parent=#AI_PATROL_ZONE] __Status
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Detect", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
---- OnBefore Transition Handler for Event Detect.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeDetect
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Detect.
--- @function [parent=#AI_PATROL_ZONE] OnAfterDetect
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Detect.
--- @function [parent=#AI_PATROL_ZONE] Detect
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Detect.
--- @function [parent=#AI_PATROL_ZONE] __Detect
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Detected", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
---- OnBefore Transition Handler for Event Detected.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeDetected
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event Detected.
--- @function [parent=#AI_PATROL_ZONE] OnAfterDetected
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event Detected.
--- @function [parent=#AI_PATROL_ZONE] Detected
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event Detected.
--- @function [parent=#AI_PATROL_ZONE] __Detected
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "RTB", "Returning" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
---- OnBefore Transition Handler for Event RTB.
--- @function [parent=#AI_PATROL_ZONE] OnBeforeRTB
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnAfter Transition Handler for Event RTB.
--- @function [parent=#AI_PATROL_ZONE] OnAfterRTB
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
---- Synchronous Event Trigger for Event RTB.
--- @function [parent=#AI_PATROL_ZONE] RTB
--- @param #AI_PATROL_ZONE self
-
---- Asynchronous Event Trigger for Event RTB.
--- @function [parent=#AI_PATROL_ZONE] __RTB
--- @param #AI_PATROL_ZONE self
--- @param #number Delay The delay in seconds.
-
---- OnLeave Transition Handler for State Returning.
--- @function [parent=#AI_PATROL_ZONE] OnLeaveReturning
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnEnter Transition Handler for State Returning.
--- @function [parent=#AI_PATROL_ZONE] OnEnterReturning
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
- self:AddTransition( "*", "Reset", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_PATROL_ZONE.
-
- self:AddTransition( "*", "Eject", "*" )
- self:AddTransition( "*", "Crash", "Crashed" )
- self:AddTransition( "*", "PilotDead", "*" )
-
- return self
-end
-
-
-
-
---- Sets (modifies) the minimum and maximum speed of the patrol.
--- @param #AI_PATROL_ZONE self
--- @param Dcs.DCSTypes#Speed PatrolMinSpeed The minimum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#Speed PatrolMaxSpeed The maximum speed of the @{Controllable} in km/h.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetSpeed( PatrolMinSpeed, PatrolMaxSpeed )
- self:F2( { PatrolMinSpeed, PatrolMaxSpeed } )
-
- self.PatrolMinSpeed = PatrolMinSpeed
- self.PatrolMaxSpeed = PatrolMaxSpeed
-end
-
-
-
---- Sets the floor and ceiling altitude of the patrol.
--- @param #AI_PATROL_ZONE self
--- @param Dcs.DCSTypes#Altitude PatrolFloorAltitude The lowest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Altitude PatrolCeilingAltitude The highest altitude in meters where to execute the patrol.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetAltitude( PatrolFloorAltitude, PatrolCeilingAltitude )
- self:F2( { PatrolFloorAltitude, PatrolCeilingAltitude } )
-
- self.PatrolFloorAltitude = PatrolFloorAltitude
- self.PatrolCeilingAltitude = PatrolCeilingAltitude
-end
-
--- * @{#AI_PATROL_ZONE.SetDetectionOn}(): Set the detection on. The AI will detect for targets.
--- * @{#AI_PATROL_ZONE.SetDetectionOff}(): Set the detection off, the AI will not detect for targets. The existing target list will NOT be erased.
-
---- Set the detection on. The AI will detect for targets.
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionOn()
- self:F2()
-
- self.DetectOn = true
-end
-
---- Set the detection off. The AI will NOT detect for targets.
--- However, the list of already detected targets will be kept and can be enquired!
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionOff()
- self:F2()
-
- self.DetectOn = false
-end
-
---- Set the status checking off.
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetStatusOff()
- self:F2()
-
- self.CheckStatus = false
-end
-
---- Activate the detection. The AI will detect for targets if the Detection is switched On.
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionActivated()
- self:F2()
-
- self:ClearDetectedUnits()
- self.DetectActivated = true
- self:__Detect( -self.DetectInterval )
-end
-
---- Deactivate the detection. The AI will NOT detect for targets.
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionDeactivated()
- self:F2()
-
- self:ClearDetectedUnits()
- self.DetectActivated = false
-end
-
---- Set the interval in seconds between each detection executed by the AI.
--- The list of already detected targets will be kept and updated.
--- Newly detected targets will be added, but already detected targets that were
--- not detected in this cycle, will NOT be removed!
--- The default interval is 30 seconds.
--- @param #AI_PATROL_ZONE self
--- @param #number Seconds The interval in seconds.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionInterval( Seconds )
- self:F2()
-
- if Seconds then
- self.DetectInterval = Seconds
- else
- self.DetectInterval = 30
- end
-end
-
---- Set the detection zone where the AI is detecting targets.
--- @param #AI_PATROL_ZONE self
--- @param Core.Zone#ZONE DetectionZone The zone where to detect targets.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:SetDetectionZone( DetectionZone )
- self:F2()
-
- if DetectionZone then
- self.DetectZone = DetectionZone
- else
- self.DetectZone = nil
- end
-end
-
---- Gets a list of @{Unit#UNIT}s that were detected by the AI.
--- No filtering is applied, so, ANY detected UNIT can be in this list.
--- It is up to the mission designer to use the @{Unit} class and methods to filter the targets.
--- @param #AI_PATROL_ZONE self
--- @return #table The list of @{Unit#UNIT}s
-function AI_PATROL_ZONE:GetDetectedUnits()
- self:F2()
-
- return self.DetectedUnits
-end
-
---- Clears the list of @{Unit#UNIT}s that were detected by the AI.
--- @param #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:ClearDetectedUnits()
- self:F2()
- self.DetectedUnits = {}
-end
-
---- When the AI is out of fuel, it is required that a new AI is started, before the old AI can return to the home base.
--- Therefore, with a parameter and a calculation of the distance to the home base, the fuel treshold is calculated.
--- When the fuel treshold is reached, the AI will continue for a given time its patrol task in orbit, while a new AIControllable is targetted to the AI_PATROL_ZONE.
--- Once the time is finished, the old AI will return to the base.
--- @param #AI_PATROL_ZONE self
--- @param #number PatrolFuelTresholdPercentage The treshold in percentage (between 0 and 1) when the AIControllable is considered to get out of fuel.
--- @param #number PatrolOutOfFuelOrbitTime The amount of seconds the out of fuel AIControllable will orbit before returning to the base.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:ManageFuel( PatrolFuelTresholdPercentage, PatrolOutOfFuelOrbitTime )
-
- self.PatrolManageFuel = true
- self.PatrolFuelTresholdPercentage = PatrolFuelTresholdPercentage
- self.PatrolOutOfFuelOrbitTime = PatrolOutOfFuelOrbitTime
-
- return self
-end
-
---- When the AI is damaged beyond a certain treshold, it is required that the AI returns to the home base.
--- However, damage cannot be foreseen early on.
--- Therefore, when the damage treshold is reached,
--- the AI will return immediately to the home base (RTB).
--- Note that for groups, the average damage of the complete group will be calculated.
--- So, in a group of 4 airplanes, 2 lost and 2 with damage 0.2, the damage treshold will be 0.25.
--- @param #AI_PATROL_ZONE self
--- @param #number PatrolDamageTreshold The treshold in percentage (between 0 and 1) when the AI is considered to be damaged.
--- @return #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:ManageDamage( PatrolDamageTreshold )
-
- self.PatrolManageDamage = true
- self.PatrolDamageTreshold = PatrolDamageTreshold
-
- return self
-end
-
---- Defines a new patrol route using the @{Process_PatrolZone} parameters and settings.
--- @param #AI_PATROL_ZONE self
--- @return #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_PATROL_ZONE:onafterStart( Controllable, From, Event, To )
- self:F2()
-
- self:__Route( 1 ) -- Route to the patrol point. The asynchronous trigger is important, because a spawned group and units takes at least one second to come live.
- self:__Status( 60 ) -- Check status status every 30 seconds.
- self:SetDetectionActivated()
-
- self:HandleEvent( EVENTS.PilotDead, self.OnPilotDead )
- self:HandleEvent( EVENTS.Crash, self.OnCrash )
- self:HandleEvent( EVENTS.Ejection, self.OnEjection )
-
- Controllable:OptionROEHoldFire()
- Controllable:OptionROTVertical()
-
- self.Controllable:OnReSpawn(
- function( PatrolGroup )
- self:E( "ReSpawn" )
- self:__Reset( 1 )
- self:__Route( 5 )
- end
- )
-
- self:SetDetectionOn()
-
-end
-
-
---- @param #AI_PATROL_ZONE self
---- @param Wrapper.Controllable#CONTROLLABLE Controllable
-function AI_PATROL_ZONE:onbeforeDetect( Controllable, From, Event, To )
-
- return self.DetectOn and self.DetectActivated
-end
-
---- @param #AI_PATROL_ZONE self
---- @param Wrapper.Controllable#CONTROLLABLE Controllable
-function AI_PATROL_ZONE:onafterDetect( Controllable, From, Event, To )
-
- local Detected = false
-
- local DetectedTargets = Controllable:GetDetectedTargets()
- for TargetID, Target in pairs( DetectedTargets or {} ) do
- local TargetObject = Target.object
-
- if TargetObject and TargetObject:isExist() and TargetObject.id_ < 50000000 then
-
- local TargetUnit = UNIT:Find( TargetObject )
- local TargetUnitName = TargetUnit:GetName()
-
- if self.DetectionZone then
- if TargetUnit:IsInZone( self.DetectionZone ) then
- self:T( {"Detected ", TargetUnit } )
- if self.DetectedUnits[TargetUnit] == nil then
- self.DetectedUnits[TargetUnit] = true
- end
- Detected = true
- end
- else
- if self.DetectedUnits[TargetUnit] == nil then
- self.DetectedUnits[TargetUnit] = true
- end
- Detected = true
- end
- end
- end
-
- self:__Detect( -self.DetectInterval )
-
- if Detected == true then
- self:__Detected( 1.5 )
- end
-
-end
-
---- @param Wrapper.Controllable#CONTROLLABLE AIControllable
--- This statis method is called from the route path within the last task at the last waaypoint of the Controllable.
--- Note that this method is required, as triggers the next route when patrolling for the Controllable.
-function AI_PATROL_ZONE:_NewPatrolRoute( AIControllable )
-
- local PatrolZone = AIControllable:GetState( AIControllable, "PatrolZone" ) -- PatrolCore.Zone#AI_PATROL_ZONE
- PatrolZone:__Route( 1 )
-end
-
-
---- Defines a new patrol route using the @{Process_PatrolZone} parameters and settings.
--- @param #AI_PATROL_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_PATROL_ZONE:onafterRoute( Controllable, From, Event, To )
-
- self:F2()
-
- -- When RTB, don't allow anymore the routing.
- if From == "RTB" then
- return
- end
-
-
- if self.Controllable:IsAlive() then
- -- Determine if the AIControllable is within the PatrolZone.
- -- If not, make a waypoint within the to that the AIControllable will fly at maximum speed to that point.
-
- local PatrolRoute = {}
-
- -- Calculate the current route point of the controllable as the start point of the route.
- -- However, when the controllable is not in the air,
- -- the controllable current waypoint is probably the airbase...
- -- Thus, if we would take the current waypoint as the startpoint, upon take-off, the controllable flies
- -- immediately back to the airbase, and this is not correct.
- -- Therefore, when on a runway, get as the current route point a random point within the PatrolZone.
- -- This will make the plane fly immediately to the patrol zone.
-
- if self.Controllable:InAir() == false then
- self:E( "Not in the air, finding route path within PatrolZone" )
- local CurrentVec2 = self.Controllable:GetVec2()
- --TODO: Create GetAltitude function for GROUP, and delete GetUnit(1).
- local CurrentAltitude = self.Controllable:GetUnit(1):GetAltitude()
- local CurrentPointVec3 = POINT_VEC3:New( CurrentVec2.x, CurrentAltitude, CurrentVec2.y )
- local ToPatrolZoneSpeed = self.PatrolMaxSpeed
- local CurrentRoutePoint = CurrentPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TakeOffParking,
- POINT_VEC3.RoutePointAction.FromParkingArea,
- ToPatrolZoneSpeed,
- true
- )
- PatrolRoute[#PatrolRoute+1] = CurrentRoutePoint
- else
- self:E( "In the air, finding route path within PatrolZone" )
- local CurrentVec2 = self.Controllable:GetVec2()
- --TODO: Create GetAltitude function for GROUP, and delete GetUnit(1).
- local CurrentAltitude = self.Controllable:GetUnit(1):GetAltitude()
- local CurrentPointVec3 = POINT_VEC3:New( CurrentVec2.x, CurrentAltitude, CurrentVec2.y )
- local ToPatrolZoneSpeed = self.PatrolMaxSpeed
- local CurrentRoutePoint = CurrentPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- ToPatrolZoneSpeed,
- true
- )
- PatrolRoute[#PatrolRoute+1] = CurrentRoutePoint
- end
-
-
- --- Define a random point in the @{Zone}. The AI will fly to that point within the zone.
-
- --- Find a random 2D point in PatrolZone.
- local ToTargetVec2 = self.PatrolZone:GetRandomVec2()
- self:T2( ToTargetVec2 )
-
- --- Define Speed and Altitude.
- local ToTargetAltitude = math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude )
- local ToTargetSpeed = math.random( self.PatrolMinSpeed, self.PatrolMaxSpeed )
- self:T2( { self.PatrolMinSpeed, self.PatrolMaxSpeed, ToTargetSpeed } )
-
- --- Obtain a 3D @{Point} from the 2D point + altitude.
- local ToTargetPointVec3 = POINT_VEC3:New( ToTargetVec2.x, ToTargetAltitude, ToTargetVec2.y )
-
- --- Create a route point of type air.
- local ToTargetRoutePoint = ToTargetPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- ToTargetSpeed,
- true
- )
-
- --self.CoordTest:SpawnFromVec3( ToTargetPointVec3:GetVec3() )
-
- --ToTargetPointVec3:SmokeRed()
-
- PatrolRoute[#PatrolRoute+1] = ToTargetRoutePoint
-
- --- Now we're going to do something special, we're going to call a function from a waypoint action at the AIControllable...
- self.Controllable:WayPointInitialize( PatrolRoute )
-
- --- Do a trick, link the NewPatrolRoute function of the PATROLGROUP object to the AIControllable in a temporary variable ...
- self.Controllable:SetState( self.Controllable, "PatrolZone", self )
- self.Controllable:WayPointFunction( #PatrolRoute, 1, "AI_PATROL_ZONE:_NewPatrolRoute" )
-
- --- NOW ROUTE THE GROUP!
- self.Controllable:WayPointExecute( 1, 2 )
- end
-
-end
-
---- @param #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:onbeforeStatus()
-
- return self.CheckStatus
-end
-
---- @param #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:onafterStatus()
- self:F2()
-
- if self.Controllable and self.Controllable:IsAlive() then
-
- local RTB = false
-
- local Fuel = self.Controllable:GetUnit(1):GetFuel()
- if Fuel < self.PatrolFuelTresholdPercentage then
- self:E( self.Controllable:GetName() .. " is out of fuel:" .. Fuel .. ", RTB!" )
- local OldAIControllable = self.Controllable
- local AIControllableTemplate = self.Controllable:GetTemplate()
-
- local OrbitTask = OldAIControllable:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed )
- local TimedOrbitTask = OldAIControllable:TaskControlled( OrbitTask, OldAIControllable:TaskCondition(nil,nil,nil,nil,self.PatrolOutOfFuelOrbitTime,nil ) )
- OldAIControllable:SetTask( TimedOrbitTask, 10 )
-
- RTB = true
- else
- end
-
- -- TODO: Check GROUP damage function.
- local Damage = self.Controllable:GetLife()
- if Damage <= self.PatrolDamageTreshold then
- self:E( self.Controllable:GetName() .. " is damaged:" .. Damage .. ", RTB!" )
- RTB = true
- end
-
- if RTB == true then
- self:RTB()
- else
- self:__Status( 60 ) -- Execute the Patrol event after 30 seconds.
- end
- end
-end
-
---- @param #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:onafterRTB()
- self:F2()
-
- if self.Controllable and self.Controllable:IsAlive() then
-
- self:SetDetectionOff()
- self.CheckStatus = false
-
- local PatrolRoute = {}
-
- --- Calculate the current route point.
- local CurrentVec2 = self.Controllable:GetVec2()
-
- --TODO: Create GetAltitude function for GROUP, and delete GetUnit(1).
- local CurrentAltitude = self.Controllable:GetUnit(1):GetAltitude()
- local CurrentPointVec3 = POINT_VEC3:New( CurrentVec2.x, CurrentAltitude, CurrentVec2.y )
- local ToPatrolZoneSpeed = self.PatrolMaxSpeed
- local CurrentRoutePoint = CurrentPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- ToPatrolZoneSpeed,
- true
- )
-
- PatrolRoute[#PatrolRoute+1] = CurrentRoutePoint
-
- --- Now we're going to do something special, we're going to call a function from a waypoint action at the AIControllable...
- self.Controllable:WayPointInitialize( PatrolRoute )
-
- --- NOW ROUTE THE GROUP!
- self.Controllable:WayPointExecute( 1, 1 )
-
- end
-
-end
-
---- @param #AI_PATROL_ZONE self
-function AI_PATROL_ZONE:onafterDead()
- self:SetDetectionOff()
- self:SetStatusOff()
-end
-
---- @param #AI_PATROL_ZONE self
--- @param Core.Event#EVENTDATA EventData
-function AI_PATROL_ZONE:OnCrash( EventData )
-
- if self.Controllable:IsAlive() and EventData.IniDCSGroupName == self.Controllable:GetName() then
- self:E( self.Controllable:GetUnits() )
- if #self.Controllable:GetUnits() == 1 then
- self:__Crash( 1, EventData )
- end
- end
-end
-
---- @param #AI_PATROL_ZONE self
--- @param Core.Event#EVENTDATA EventData
-function AI_PATROL_ZONE:OnEjection( EventData )
-
- if self.Controllable:IsAlive() and EventData.IniDCSGroupName == self.Controllable:GetName() then
- self:__Eject( 1, EventData )
- end
-end
-
---- @param #AI_PATROL_ZONE self
--- @param Core.Event#EVENTDATA EventData
-function AI_PATROL_ZONE:OnPilotDead( EventData )
-
- if self.Controllable:IsAlive() and EventData.IniDCSGroupName == self.Controllable:GetName() then
- self:__PilotDead( 1, EventData )
- end
-end
---- **AI** - **Execute Combat Air Patrol (CAP).**
---
--- 
---
--- ===
---
--- AI CAP classes makes AI Controllables execute a Combat Air Patrol.
---
--- There are the following types of CAP classes defined:
---
--- * @{#AI_CAP_ZONE}: Perform a CAP in a zone.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-01-15: Initial class and API.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **[Quax](https://forums.eagle.ru/member.php?u=90530)**: Concept, Advice & Testing.
--- * **[Pikey](https://forums.eagle.ru/member.php?u=62835)**: Concept, Advice & Testing.
--- * **[Gunterlund](http://forums.eagle.ru:8080/member.php?u=75036)**: Test case revision.
--- * **[Whisper](http://forums.eagle.ru/member.php?u=3829): Testing.
--- * **[Delta99](https://forums.eagle.ru/member.php?u=125166): Testing.
---
--- ### Authors:
---
--- * **FlightControl**: Concept, Design & Programming.
---
--- @module AI_Cap
-
-
---- @type AI_CAP_ZONE
--- @field Wrapper.Controllable#CONTROLLABLE AIControllable The @{Controllable} patrolling.
--- @field Core.Zone#ZONE_BASE TargetZone The @{Zone} where the patrol needs to be executed.
--- @extends AI.AI_Patrol#AI_PATROL_ZONE
-
-
---- # 1) @{#AI_CAP_ZONE} class, extends @{AI_CAP#AI_PATROL_ZONE}
---
--- The @{#AI_CAP_ZONE} class implements the core functions to patrol a @{Zone} by an AI @{Controllable} or @{Group}
--- and automatically engage any airborne enemies that are within a certain range or within a certain zone.
---
--- 
---
--- The AI_CAP_ZONE is assigned a @{Group} and this must be done before the AI_CAP_ZONE process can be started using the **Start** event.
---
--- 
---
--- The AI will fly towards the random 3D point within the patrol zone, using a random speed within the given altitude and speed limits.
--- Upon arrival at the 3D point, a new random 3D point will be selected within the patrol zone using the given limits.
---
--- 
---
--- This cycle will continue.
---
--- 
---
--- During the patrol, the AI will detect enemy targets, which are reported through the **Detected** event.
---
--- 
---
--- When enemies are detected, the AI will automatically engage the enemy.
---
--- 
---
--- Until a fuel or damage treshold has been reached by the AI, or when the AI is commanded to RTB.
--- When the fuel treshold has been reached, the airplane will fly towards the nearest friendly airbase and will land.
---
--- 
---
--- ## 1.1) AI_CAP_ZONE constructor
---
--- * @{#AI_CAP_ZONE.New}(): Creates a new AI_CAP_ZONE object.
---
--- ## 1.2) AI_CAP_ZONE is a FSM
---
--- 
---
--- ### 1.2.1) AI_CAP_ZONE States
---
--- * **None** ( Group ): The process is not started yet.
--- * **Patrolling** ( Group ): The AI is patrolling the Patrol Zone.
--- * **Engaging** ( Group ): The AI is engaging the bogeys.
--- * **Returning** ( Group ): The AI is returning to Base..
---
--- ### 1.2.2) AI_CAP_ZONE Events
---
--- * **@{AI_Patrol#AI_PATROL_ZONE.Start}**: Start the process.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Route}**: Route the AI to a new random 3D point within the Patrol Zone.
--- * **@{#AI_CAP_ZONE.Engage}**: Let the AI engage the bogeys.
--- * **@{#AI_CAP_ZONE.Abort}**: Aborts the engagement and return patrolling in the patrol zone.
--- * **@{AI_Patrol#AI_PATROL_ZONE.RTB}**: Route the AI to the home base.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Detect}**: The AI is detecting targets.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Detected}**: The AI has detected new targets.
--- * **@{#AI_CAP_ZONE.Destroy}**: The AI has destroyed a bogey @{Unit}.
--- * **@{#AI_CAP_ZONE.Destroyed}**: The AI has destroyed all bogeys @{Unit}s assigned in the CAS task.
--- * **Status** ( Group ): The AI is checking status (fuel and damage). When the tresholds have been reached, the AI will RTB.
---
--- ## 1.3) Set the Range of Engagement
---
--- 
---
--- An optional range can be set in meters,
--- that will define when the AI will engage with the detected airborne enemy targets.
--- The range can be beyond or smaller than the range of the Patrol Zone.
--- The range is applied at the position of the AI.
--- Use the method @{AI_CAP#AI_CAP_ZONE.SetEngageRange}() to define that range.
---
--- ## 1.4) Set the Zone of Engagement
---
--- 
---
--- An optional @{Zone} can be set,
--- that will define when the AI will engage with the detected airborne enemy targets.
--- Use the method @{AI_Cap#AI_CAP_ZONE.SetEngageZone}() to define that Zone.
---
--- ===
---
--- @field #AI_CAP_ZONE AI_CAP_ZONE
---
-AI_CAP_ZONE = {
- ClassName = "AI_CAP_ZONE",
-}
-
-
-
---- Creates a new AI_CAP_ZONE object
--- @param #AI_CAP_ZONE self
--- @param Core.Zone#ZONE_BASE PatrolZone The @{Zone} where the patrol needs to be executed.
--- @param Dcs.DCSTypes#Altitude PatrolFloorAltitude The lowest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Altitude PatrolCeilingAltitude The highest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Speed PatrolMinSpeed The minimum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#Speed PatrolMaxSpeed The maximum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#AltitudeType PatrolAltType The altitude type ("RADIO"=="AGL", "BARO"=="ASL"). Defaults to RADIO
--- @return #AI_CAP_ZONE self
-function AI_CAP_ZONE:New( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed, PatrolAltType )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, AI_PATROL_ZONE:New( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed, PatrolAltType ) ) -- #AI_CAP_ZONE
-
- self.Accomplished = false
- self.Engaging = false
-
- self:AddTransition( { "Patrolling", "Engaging" }, "Engage", "Engaging" ) -- FSM_CONTROLLABLE Transition for type #AI_CAP_ZONE.
-
- --- OnBefore Transition Handler for Event Engage.
- -- @function [parent=#AI_CAP_ZONE] OnBeforeEngage
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Engage.
- -- @function [parent=#AI_CAP_ZONE] OnAfterEngage
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Engage.
- -- @function [parent=#AI_CAP_ZONE] Engage
- -- @param #AI_CAP_ZONE self
-
- --- Asynchronous Event Trigger for Event Engage.
- -- @function [parent=#AI_CAP_ZONE] __Engage
- -- @param #AI_CAP_ZONE self
- -- @param #number Delay The delay in seconds.
-
---- OnLeave Transition Handler for State Engaging.
--- @function [parent=#AI_CAP_ZONE] OnLeaveEngaging
--- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnEnter Transition Handler for State Engaging.
--- @function [parent=#AI_CAP_ZONE] OnEnterEngaging
--- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
- self:AddTransition( "Engaging", "Fired", "Engaging" ) -- FSM_CONTROLLABLE Transition for type #AI_CAP_ZONE.
-
- --- OnBefore Transition Handler for Event Fired.
- -- @function [parent=#AI_CAP_ZONE] OnBeforeFired
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Fired.
- -- @function [parent=#AI_CAP_ZONE] OnAfterFired
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Fired.
- -- @function [parent=#AI_CAP_ZONE] Fired
- -- @param #AI_CAP_ZONE self
-
- --- Asynchronous Event Trigger for Event Fired.
- -- @function [parent=#AI_CAP_ZONE] __Fired
- -- @param #AI_CAP_ZONE self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Destroy", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_CAP_ZONE.
-
- --- OnBefore Transition Handler for Event Destroy.
- -- @function [parent=#AI_CAP_ZONE] OnBeforeDestroy
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Destroy.
- -- @function [parent=#AI_CAP_ZONE] OnAfterDestroy
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Destroy.
- -- @function [parent=#AI_CAP_ZONE] Destroy
- -- @param #AI_CAP_ZONE self
-
- --- Asynchronous Event Trigger for Event Destroy.
- -- @function [parent=#AI_CAP_ZONE] __Destroy
- -- @param #AI_CAP_ZONE self
- -- @param #number Delay The delay in seconds.
-
-
- self:AddTransition( "Engaging", "Abort", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_CAP_ZONE.
-
- --- OnBefore Transition Handler for Event Abort.
- -- @function [parent=#AI_CAP_ZONE] OnBeforeAbort
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Abort.
- -- @function [parent=#AI_CAP_ZONE] OnAfterAbort
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Abort.
- -- @function [parent=#AI_CAP_ZONE] Abort
- -- @param #AI_CAP_ZONE self
-
- --- Asynchronous Event Trigger for Event Abort.
- -- @function [parent=#AI_CAP_ZONE] __Abort
- -- @param #AI_CAP_ZONE self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "Engaging", "Accomplish", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_CAP_ZONE.
-
- --- OnBefore Transition Handler for Event Accomplish.
- -- @function [parent=#AI_CAP_ZONE] OnBeforeAccomplish
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Accomplish.
- -- @function [parent=#AI_CAP_ZONE] OnAfterAccomplish
- -- @param #AI_CAP_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Accomplish.
- -- @function [parent=#AI_CAP_ZONE] Accomplish
- -- @param #AI_CAP_ZONE self
-
- --- Asynchronous Event Trigger for Event Accomplish.
- -- @function [parent=#AI_CAP_ZONE] __Accomplish
- -- @param #AI_CAP_ZONE self
- -- @param #number Delay The delay in seconds.
-
- return self
-end
-
-
---- Set the Engage Zone which defines where the AI will engage bogies.
--- @param #AI_CAP_ZONE self
--- @param Core.Zone#ZONE EngageZone The zone where the AI is performing CAP.
--- @return #AI_CAP_ZONE self
-function AI_CAP_ZONE:SetEngageZone( EngageZone )
- self:F2()
-
- if EngageZone then
- self.EngageZone = EngageZone
- else
- self.EngageZone = nil
- end
-end
-
---- Set the Engage Range when the AI will engage with airborne enemies.
--- @param #AI_CAP_ZONE self
--- @param #number EngageRange The Engage Range.
--- @return #AI_CAP_ZONE self
-function AI_CAP_ZONE:SetEngageRange( EngageRange )
- self:F2()
-
- if EngageRange then
- self.EngageRange = EngageRange
- else
- self.EngageRange = nil
- end
-end
-
---- onafter State Transition for Event Start.
--- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onafterStart( Controllable, From, Event, To )
-
- -- Call the parent Start event handler
- self:GetParent(self).onafterStart( self, Controllable, From, Event, To )
- self:HandleEvent( EVENTS.Dead )
-
-end
-
--- todo: need to fix this global function
-
---- @param Wrapper.Controllable#CONTROLLABLE AIControllable
-function _NewEngageCapRoute( AIControllable )
-
- AIControllable:T( "NewEngageRoute" )
- local EngageZone = AIControllable:GetState( AIControllable, "EngageZone" ) -- AI.AI_Cap#AI_CAP_ZONE
- EngageZone:__Engage( 1 )
-end
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onbeforeEngage( Controllable, From, Event, To )
-
- if self.Accomplished == true then
- return false
- end
-end
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onafterDetected( Controllable, From, Event, To )
-
- if From ~= "Engaging" then
-
- local Engage = false
-
- for DetectedUnit, Detected in pairs( self.DetectedUnits ) do
-
- local DetectedUnit = DetectedUnit -- Wrapper.Unit#UNIT
- self:T( DetectedUnit )
- if DetectedUnit:IsAlive() and DetectedUnit:IsAir() then
- Engage = true
- break
- end
- end
-
- if Engage == true then
- self:E( 'Detected -> Engaging' )
- self:__Engage( 1 )
- end
- end
-end
-
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onafterAbort( Controllable, From, Event, To )
- Controllable:ClearTasks()
- self:__Route( 1 )
-end
-
-
-
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onafterEngage( Controllable, From, Event, To )
-
- if Controllable:IsAlive() then
-
- local EngageRoute = {}
-
- --- Calculate the current route point.
- local CurrentVec2 = self.Controllable:GetVec2()
-
- --TODO: Create GetAltitude function for GROUP, and delete GetUnit(1).
- local CurrentAltitude = self.Controllable:GetUnit(1):GetAltitude()
- local CurrentPointVec3 = POINT_VEC3:New( CurrentVec2.x, CurrentAltitude, CurrentVec2.y )
- local ToEngageZoneSpeed = self.PatrolMaxSpeed
- local CurrentRoutePoint = CurrentPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- ToEngageZoneSpeed,
- true
- )
-
- EngageRoute[#EngageRoute+1] = CurrentRoutePoint
-
-
- --- Find a random 2D point in PatrolZone.
- local ToTargetVec2 = self.PatrolZone:GetRandomVec2()
- self:T2( ToTargetVec2 )
-
- --- Define Speed and Altitude.
- local ToTargetAltitude = math.random( self.EngageFloorAltitude, self.EngageCeilingAltitude )
- local ToTargetSpeed = math.random( self.PatrolMinSpeed, self.PatrolMaxSpeed )
- self:T2( { self.PatrolMinSpeed, self.PatrolMaxSpeed, ToTargetSpeed } )
-
- --- Obtain a 3D @{Point} from the 2D point + altitude.
- local ToTargetPointVec3 = POINT_VEC3:New( ToTargetVec2.x, ToTargetAltitude, ToTargetVec2.y )
-
- --- Create a route point of type air.
- local ToPatrolRoutePoint = ToTargetPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- ToTargetSpeed,
- true
- )
-
- EngageRoute[#EngageRoute+1] = ToPatrolRoutePoint
-
- Controllable:OptionROEOpenFire()
- Controllable:OptionROTPassiveDefense()
-
- local AttackTasks = {}
-
- for DetectedUnit, Detected in pairs( self.DetectedUnits ) do
- local DetectedUnit = DetectedUnit -- Wrapper.Unit#UNIT
- self:T( { DetectedUnit, DetectedUnit:IsAlive(), DetectedUnit:IsAir() } )
- if DetectedUnit:IsAlive() and DetectedUnit:IsAir() then
- if self.EngageZone then
- if DetectedUnit:IsInZone( self.EngageZone ) then
- self:E( {"Within Zone and Engaging ", DetectedUnit } )
- AttackTasks[#AttackTasks+1] = Controllable:TaskAttackUnit( DetectedUnit )
- end
- else
- if self.EngageRange then
- if DetectedUnit:GetPointVec3():Get2DDistance(Controllable:GetPointVec3() ) <= self.EngageRange then
- self:E( {"Within Range and Engaging", DetectedUnit } )
- AttackTasks[#AttackTasks+1] = Controllable:TaskAttackUnit( DetectedUnit )
- end
- else
- AttackTasks[#AttackTasks+1] = Controllable:TaskAttackUnit( DetectedUnit )
- end
- end
- else
- self.DetectedUnits[DetectedUnit] = nil
- end
- end
-
- --- Now we're going to do something special, we're going to call a function from a waypoint action at the AIControllable...
- self.Controllable:WayPointInitialize( EngageRoute )
-
-
- if #AttackTasks == 0 then
- self:E("No targets found -> Going back to Patrolling")
- self:__Abort( 1 )
- self:__Route( 1 )
- self:SetDetectionActivated()
- else
- EngageRoute[1].task = Controllable:TaskCombo( AttackTasks )
-
- --- Do a trick, link the NewEngageRoute function of the object to the AIControllable in a temporary variable ...
- self.Controllable:SetState( self.Controllable, "EngageZone", self )
-
- self.Controllable:WayPointFunction( #EngageRoute, 1, "_NewEngageCapRoute" )
-
- self:SetDetectionDeactivated()
- end
-
- --- NOW ROUTE THE GROUP!
- self.Controllable:WayPointExecute( 1, 2 )
-
- end
-end
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAP_ZONE:onafterAccomplish( Controllable, From, Event, To )
- self.Accomplished = true
- self:SetDetectionOff()
-end
-
---- @param #AI_CAP_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @param Core.Event#EVENTDATA EventData
-function AI_CAP_ZONE:onafterDestroy( Controllable, From, Event, To, EventData )
-
- if EventData.IniUnit then
- self.DetectedUnits[EventData.IniUnit] = nil
- end
-end
-
---- @param #AI_CAP_ZONE self
--- @param Core.Event#EVENTDATA EventData
-function AI_CAP_ZONE:OnEventDead( EventData )
- self:F( { "EventDead", EventData } )
-
- if EventData.IniDCSUnit then
- if self.DetectedUnits and self.DetectedUnits[EventData.IniUnit] then
- self:__Destroy( 1, EventData )
- end
- end
-end
---- **AI** -- **Provide Close Air Support to friendly ground troops.**
---
--- 
---
--- ===
---
--- AI CAS classes makes AI Controllables execute a Close Air Support.
---
--- There are the following types of CAS classes defined:
---
--- * @{#AI_CAS_ZONE}: Perform a CAS in a zone.
---
--- ===
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-01-15: Initial class and API.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **[Quax](https://forums.eagle.ru/member.php?u=90530)**: Concept, Advice & Testing.
--- * **[Pikey](https://forums.eagle.ru/member.php?u=62835)**: Concept, Advice & Testing.
--- * **[Gunterlund](http://forums.eagle.ru:8080/member.php?u=75036)**: Test case revision.
---
--- ### Authors:
---
--- * **FlightControl**: Concept, Design & Programming.
---
--- @module AI_Cas
-
-
---- AI_CAS_ZONE class
--- @type AI_CAS_ZONE
--- @field Wrapper.Controllable#CONTROLLABLE AIControllable The @{Controllable} patrolling.
--- @field Core.Zone#ZONE_BASE TargetZone The @{Zone} where the patrol needs to be executed.
--- @extends AI.AI_Patrol#AI_PATROL_ZONE
-
---- # 1) @{#AI_CAS_ZONE} class, extends @{AI_Patrol#AI_PATROL_ZONE}
---
--- @{#AI_CAS_ZONE} derives from the @{AI_Patrol#AI_PATROL_ZONE}, inheriting its methods and behaviour.
---
--- The @{#AI_CAS_ZONE} class implements the core functions to provide Close Air Support in an Engage @{Zone} by an AIR @{Controllable} or @{Group}.
--- The AI_CAS_ZONE runs a process. It holds an AI in a Patrol Zone and when the AI is commanded to engage, it will fly to an Engage Zone.
---
--- 
---
--- The AI_CAS_ZONE is assigned a @{Group} and this must be done before the AI_CAS_ZONE process can be started through the **Start** event.
---
--- 
---
--- Upon started, The AI will **Route** itself towards the random 3D point within a patrol zone,
--- using a random speed within the given altitude and speed limits.
--- Upon arrival at the 3D point, a new random 3D point will be selected within the patrol zone using the given limits.
--- This cycle will continue until a fuel or damage treshold has been reached by the AI, or when the AI is commanded to RTB.
---
--- 
---
--- When the AI is commanded to provide Close Air Support (through the event **Engage**), the AI will fly towards the Engage Zone.
--- Any target that is detected in the Engage Zone will be reported and will be destroyed by the AI.
---
--- 
---
--- The AI will detect the targets and will only destroy the targets within the Engage Zone.
---
--- 
---
--- Every target that is destroyed, is reported< by the AI.
---
--- 
---
--- Note that the AI does not know when the Engage Zone is cleared, and therefore will keep circling in the zone.
---
--- 
---
--- Until it is notified through the event **Accomplish**, which is to be triggered by an observing party:
---
--- * a FAC
--- * a timed event
--- * a menu option selected by a human
--- * a condition
--- * others ...
---
--- 
---
--- When the AI has accomplished the CAS, it will fly back to the Patrol Zone.
---
--- 
---
--- It will keep patrolling there, until it is notified to RTB or move to another CAS Zone.
--- It can be notified to go RTB through the **RTB** event.
---
--- When the fuel treshold has been reached, the airplane will fly towards the nearest friendly airbase and will land.
---
--- 
---
--- # 1.1) AI_CAS_ZONE constructor
---
--- * @{#AI_CAS_ZONE.New}(): Creates a new AI_CAS_ZONE object.
---
--- ## 1.2) AI_CAS_ZONE is a FSM
---
--- 
---
--- ### 1.2.1) AI_CAS_ZONE States
---
--- * **None** ( Group ): The process is not started yet.
--- * **Patrolling** ( Group ): The AI is patrolling the Patrol Zone.
--- * **Engaging** ( Group ): The AI is engaging the targets in the Engage Zone, executing CAS.
--- * **Returning** ( Group ): The AI is returning to Base..
---
--- ### 1.2.2) AI_CAS_ZONE Events
---
--- * **@{AI_Patrol#AI_PATROL_ZONE.Start}**: Start the process.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Route}**: Route the AI to a new random 3D point within the Patrol Zone.
--- * **@{#AI_CAS_ZONE.Engage}**: Engage the AI to provide CAS in the Engage Zone, destroying any target it finds.
--- * **@{#AI_CAS_ZONE.Abort}**: Aborts the engagement and return patrolling in the patrol zone.
--- * **@{AI_Patrol#AI_PATROL_ZONE.RTB}**: Route the AI to the home base.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Detect}**: The AI is detecting targets.
--- * **@{AI_Patrol#AI_PATROL_ZONE.Detected}**: The AI has detected new targets.
--- * **@{#AI_CAS_ZONE.Destroy}**: The AI has destroyed a target @{Unit}.
--- * **@{#AI_CAS_ZONE.Destroyed}**: The AI has destroyed all target @{Unit}s assigned in the CAS task.
--- * **Status**: The AI is checking status (fuel and damage). When the tresholds have been reached, the AI will RTB.
---
--- ===
---
--- @field #AI_CAS_ZONE AI_CAS_ZONE
---
-AI_CAS_ZONE = {
- ClassName = "AI_CAS_ZONE",
-}
-
-
-
---- Creates a new AI_CAS_ZONE object
--- @param #AI_CAS_ZONE self
--- @param Core.Zone#ZONE_BASE PatrolZone The @{Zone} where the patrol needs to be executed.
--- @param Dcs.DCSTypes#Altitude PatrolFloorAltitude The lowest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Altitude PatrolCeilingAltitude The highest altitude in meters where to execute the patrol.
--- @param Dcs.DCSTypes#Speed PatrolMinSpeed The minimum speed of the @{Controllable} in km/h.
--- @param Dcs.DCSTypes#Speed PatrolMaxSpeed The maximum speed of the @{Controllable} in km/h.
--- @param Core.Zone#ZONE_BASE EngageZone The zone where the engage will happen.
--- @param Dcs.DCSTypes#AltitudeType PatrolAltType The altitude type ("RADIO"=="AGL", "BARO"=="ASL"). Defaults to RADIO
--- @return #AI_CAS_ZONE self
-function AI_CAS_ZONE:New( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed, EngageZone, PatrolAltType )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, AI_PATROL_ZONE:New( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed, PatrolAltType ) ) -- #AI_CAS_ZONE
-
- self.EngageZone = EngageZone
- self.Accomplished = false
-
- self:SetDetectionZone( self.EngageZone )
-
- self:AddTransition( { "Patrolling", "Engaging" }, "Engage", "Engaging" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- --- OnBefore Transition Handler for Event Engage.
- -- @function [parent=#AI_CAS_ZONE] OnBeforeEngage
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Engage.
- -- @function [parent=#AI_CAS_ZONE] OnAfterEngage
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Engage.
- -- @function [parent=#AI_CAS_ZONE] Engage
- -- @param #AI_CAS_ZONE self
- -- @param #number EngageSpeed (optional) The speed the Group will hold when engaging to the target zone.
- -- @param Dcs.DCSTypes#Distance EngageAltitude (optional) Desired altitude to perform the unit engagement.
- -- @param Dcs.DCSTypes#AI.Task.WeaponExpend EngageWeaponExpend (optional) Determines how much weapon will be released at each attack.
- -- If parameter is not defined the unit / controllable will choose expend on its own discretion.
- -- Use the structure @{DCSTypes#AI.Task.WeaponExpend} to define the amount of weapons to be release at each attack.
- -- @param #number EngageAttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
- -- @param Dcs.DCSTypes#Azimuth EngageDirection (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
-
- --- Asynchronous Event Trigger for Event Engage.
- -- @function [parent=#AI_CAS_ZONE] __Engage
- -- @param #AI_CAS_ZONE self
- -- @param #number Delay The delay in seconds.
- -- @param #number EngageSpeed (optional) The speed the Group will hold when engaging to the target zone.
- -- @param Dcs.DCSTypes#Distance EngageAltitude (optional) Desired altitude to perform the unit engagement.
- -- @param Dcs.DCSTypes#AI.Task.WeaponExpend EngageWeaponExpend (optional) Determines how much weapon will be released at each attack.
- -- If parameter is not defined the unit / controllable will choose expend on its own discretion.
- -- Use the structure @{DCSTypes#AI.Task.WeaponExpend} to define the amount of weapons to be release at each attack.
- -- @param #number EngageAttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
- -- @param Dcs.DCSTypes#Azimuth EngageDirection (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
-
---- OnLeave Transition Handler for State Engaging.
--- @function [parent=#AI_CAS_ZONE] OnLeaveEngaging
--- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @return #boolean Return false to cancel Transition.
-
---- OnEnter Transition Handler for State Engaging.
--- @function [parent=#AI_CAS_ZONE] OnEnterEngaging
--- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-
- self:AddTransition( "Engaging", "Target", "Engaging" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- self:AddTransition( "Engaging", "Fired", "Engaging" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- --- OnBefore Transition Handler for Event Fired.
- -- @function [parent=#AI_CAS_ZONE] OnBeforeFired
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Fired.
- -- @function [parent=#AI_CAS_ZONE] OnAfterFired
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Fired.
- -- @function [parent=#AI_CAS_ZONE] Fired
- -- @param #AI_CAS_ZONE self
-
- --- Asynchronous Event Trigger for Event Fired.
- -- @function [parent=#AI_CAS_ZONE] __Fired
- -- @param #AI_CAS_ZONE self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Destroy", "*" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- --- OnBefore Transition Handler for Event Destroy.
- -- @function [parent=#AI_CAS_ZONE] OnBeforeDestroy
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Destroy.
- -- @function [parent=#AI_CAS_ZONE] OnAfterDestroy
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Destroy.
- -- @function [parent=#AI_CAS_ZONE] Destroy
- -- @param #AI_CAS_ZONE self
-
- --- Asynchronous Event Trigger for Event Destroy.
- -- @function [parent=#AI_CAS_ZONE] __Destroy
- -- @param #AI_CAS_ZONE self
- -- @param #number Delay The delay in seconds.
-
-
- self:AddTransition( "Engaging", "Abort", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- --- OnBefore Transition Handler for Event Abort.
- -- @function [parent=#AI_CAS_ZONE] OnBeforeAbort
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Abort.
- -- @function [parent=#AI_CAS_ZONE] OnAfterAbort
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Abort.
- -- @function [parent=#AI_CAS_ZONE] Abort
- -- @param #AI_CAS_ZONE self
-
- --- Asynchronous Event Trigger for Event Abort.
- -- @function [parent=#AI_CAS_ZONE] __Abort
- -- @param #AI_CAS_ZONE self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "Engaging", "Accomplish", "Patrolling" ) -- FSM_CONTROLLABLE Transition for type #AI_CAS_ZONE.
-
- --- OnBefore Transition Handler for Event Accomplish.
- -- @function [parent=#AI_CAS_ZONE] OnBeforeAccomplish
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Accomplish.
- -- @function [parent=#AI_CAS_ZONE] OnAfterAccomplish
- -- @param #AI_CAS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Accomplish.
- -- @function [parent=#AI_CAS_ZONE] Accomplish
- -- @param #AI_CAS_ZONE self
-
- --- Asynchronous Event Trigger for Event Accomplish.
- -- @function [parent=#AI_CAS_ZONE] __Accomplish
- -- @param #AI_CAS_ZONE self
- -- @param #number Delay The delay in seconds.
-
- return self
-end
-
-
---- Set the Engage Zone where the AI is performing CAS. Note that if the EngageZone is changed, the AI needs to re-detect targets.
--- @param #AI_CAS_ZONE self
--- @param Core.Zone#ZONE EngageZone The zone where the AI is performing CAS.
--- @return #AI_CAS_ZONE self
-function AI_CAS_ZONE:SetEngageZone( EngageZone )
- self:F2()
-
- if EngageZone then
- self.EngageZone = EngageZone
- else
- self.EngageZone = nil
- end
-end
-
-
-
---- onafter State Transition for Event Start.
--- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAS_ZONE:onafterStart( Controllable, From, Event, To )
-
- -- Call the parent Start event handler
- self:GetParent(self).onafterStart( self, Controllable, From, Event, To )
- self:HandleEvent( EVENTS.Dead )
-
- self:SetDetectionDeactivated() -- When not engaging, set the detection off.
-end
-
---- @param Wrapper.Controllable#CONTROLLABLE AIControllable
-function _NewEngageRoute( AIControllable )
-
- AIControllable:T( "NewEngageRoute" )
- local EngageZone = AIControllable:GetState( AIControllable, "EngageZone" ) -- AI.AI_Cas#AI_CAS_ZONE
- EngageZone:__Engage( 1, EngageZone.EngageSpeed, EngageZone.EngageAltitude, EngageZone.EngageWeaponExpend, EngageZone.EngageAttackQty, EngageZone.EngageDirection )
-end
-
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAS_ZONE:onbeforeEngage( Controllable, From, Event, To )
-
- if self.Accomplished == true then
- return false
- end
-end
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAS_ZONE:onafterTarget( Controllable, From, Event, To )
- self:E("onafterTarget")
-
- if Controllable:IsAlive() then
-
- local AttackTasks = {}
-
- for DetectedUnit, Detected in pairs( self.DetectedUnits ) do
- local DetectedUnit = DetectedUnit -- Wrapper.Unit#UNIT
- if DetectedUnit:IsAlive() then
- if DetectedUnit:IsInZone( self.EngageZone ) then
- if Detected == true then
- self:E( {"Target: ", DetectedUnit } )
- self.DetectedUnits[DetectedUnit] = false
- local AttackTask = Controllable:TaskAttackUnit( DetectedUnit, false, self.EngageWeaponExpend, self.EngageAttackQty, self.EngageDirection, self.EngageAltitude, nil )
- self.Controllable:PushTask( AttackTask, 1 )
- end
- end
- else
- self.DetectedUnits[DetectedUnit] = nil
- end
- end
-
- self:__Target( -10 )
-
- end
-end
-
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAS_ZONE:onafterAbort( Controllable, From, Event, To )
- Controllable:ClearTasks()
- self:__Route( 1 )
-end
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @param #number EngageSpeed (optional) The speed the Group will hold when engaging to the target zone.
--- @param Dcs.DCSTypes#Distance EngageAltitude (optional) Desired altitude to perform the unit engagement.
--- @param Dcs.DCSTypes#AI.Task.WeaponExpend EngageWeaponExpend (optional) Determines how much weapon will be released at each attack. If parameter is not defined the unit / controllable will choose expend on its own discretion.
--- @param #number EngageAttackQty (optional) This parameter limits maximal quantity of attack. The aicraft/controllable will not make more attack than allowed even if the target controllable not destroyed and the aicraft/controllable still have ammo. If not defined the aircraft/controllable will attack target until it will be destroyed or until the aircraft/controllable will run out of ammo.
--- @param Dcs.DCSTypes#Azimuth EngageDirection (optional) Desired ingress direction from the target to the attacking aircraft. Controllable/aircraft will make its attacks from the direction. Of course if there is no way to attack from the direction due the terrain controllable/aircraft will choose another direction.
-function AI_CAS_ZONE:onafterEngage( Controllable, From, Event, To,
- EngageSpeed,
- EngageAltitude,
- EngageWeaponExpend,
- EngageAttackQty,
- EngageDirection )
- self:F("onafterEngage")
-
- self.EngageSpeed = EngageSpeed or 400
- self.EngageAltitude = EngageAltitude or 2000
- self.EngageWeaponExpend = EngageWeaponExpend
- self.EngageAttackQty = EngageAttackQty
- self.EngageDirection = EngageDirection
-
- if Controllable:IsAlive() then
-
- local EngageRoute = {}
-
- --- Calculate the current route point.
- local CurrentVec2 = self.Controllable:GetVec2()
-
- --TODO: Create GetAltitude function for GROUP, and delete GetUnit(1).
- local CurrentAltitude = self.Controllable:GetUnit(1):GetAltitude()
- local CurrentPointVec3 = POINT_VEC3:New( CurrentVec2.x, CurrentAltitude, CurrentVec2.y )
- local ToEngageZoneSpeed = self.PatrolMaxSpeed
- local CurrentRoutePoint = CurrentPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- self.EngageSpeed,
- true
- )
-
- EngageRoute[#EngageRoute+1] = CurrentRoutePoint
-
- local AttackTasks = {}
-
- for DetectedUnitID, DetectedUnit in pairs( self.DetectedUnits ) do
- local DetectedUnit = DetectedUnit -- Wrapper.Unit#UNIT
- self:T( DetectedUnit )
- if DetectedUnit:IsAlive() then
- if DetectedUnit:IsInZone( self.EngageZone ) then
- self:E( {"Engaging ", DetectedUnit } )
- AttackTasks[#AttackTasks+1] = Controllable:TaskAttackUnit( DetectedUnit,
- true,
- EngageWeaponExpend,
- EngageAttackQty,
- EngageDirection
- )
- end
- else
- self.DetectedUnits[DetectedUnit] = nil
- end
- end
-
- EngageRoute[1].task = Controllable:TaskCombo( AttackTasks )
-
- --- Define a random point in the @{Zone}. The AI will fly to that point within the zone.
-
- --- Find a random 2D point in EngageZone.
- local ToTargetVec2 = self.EngageZone:GetRandomVec2()
- self:T2( ToTargetVec2 )
-
- --- Obtain a 3D @{Point} from the 2D point + altitude.
- local ToTargetPointVec3 = POINT_VEC3:New( ToTargetVec2.x, self.EngageAltitude, ToTargetVec2.y )
-
- --- Create a route point of type air.
- local ToTargetRoutePoint = ToTargetPointVec3:RoutePointAir(
- self.PatrolAltType,
- POINT_VEC3.RoutePointType.TurningPoint,
- POINT_VEC3.RoutePointAction.TurningPoint,
- self.EngageSpeed,
- true
- )
-
- EngageRoute[#EngageRoute+1] = ToTargetRoutePoint
-
- --- Now we're going to do something special, we're going to call a function from a waypoint action at the AIControllable...
- Controllable:WayPointInitialize( EngageRoute )
-
- --- Do a trick, link the NewEngageRoute function of the object to the AIControllable in a temporary variable ...
- Controllable:SetState( Controllable, "EngageZone", self )
-
- Controllable:WayPointFunction( #EngageRoute, 1, "_NewEngageRoute" )
-
- --- NOW ROUTE THE GROUP!
- Controllable:WayPointExecute( 1 )
-
- Controllable:OptionROEOpenFire()
- Controllable:OptionROTVertical()
-
- self:SetDetectionInterval( 2 )
- self:SetDetectionActivated()
- self:__Target( -2 ) -- Start Targetting
- end
-end
-
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
-function AI_CAS_ZONE:onafterAccomplish( Controllable, From, Event, To )
- self.Accomplished = true
- self:SetDetectionDeactivated()
-end
-
-
---- @param #AI_CAS_ZONE self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable The Controllable Object managed by the FSM.
--- @param #string From The From State string.
--- @param #string Event The Event string.
--- @param #string To The To State string.
--- @param Core.Event#EVENTDATA EventData
-function AI_CAS_ZONE:onafterDestroy( Controllable, From, Event, To, EventData )
-
- if EventData.IniUnit then
- self.DetectedUnits[EventData.IniUnit] = nil
- end
-end
-
-
---- @param #AI_CAS_ZONE self
--- @param Core.Event#EVENTDATA EventData
-function AI_CAS_ZONE:OnEventDead( EventData )
- self:F( { "EventDead", EventData } )
-
- if EventData.IniDCSUnit then
- if self.DetectedUnits and self.DetectedUnits[EventData.IniUnit] then
- self:__Destroy( 1, EventData )
- end
- end
-end
-
-
----Single-Player:**Yes** / Multi-Player:**Yes** / AI:**Yes** / Human:**No** / Types:**Ground** --
--- **Management of logical cargo objects, that can be transported from and to transportation carriers.**
---
--- 
---
--- ===
---
--- Cargo can be of various forms, always are composed out of ONE object ( one unit or one static or one slingload crate ):
---
--- * AI_CARGO_UNIT, represented by a @{Unit} in a @{Group}: Cargo can be represented by a Unit in a Group. Destruction of the Unit will mean that the cargo is lost.
--- * CARGO_STATIC, represented by a @{Static}: Cargo can be represented by a Static. Destruction of the Static will mean that the cargo is lost.
--- * AI_CARGO_PACKAGE, contained in a @{Unit} of a @{Group}: Cargo can be contained within a Unit of a Group. The cargo can be **delivered** by the @{Unit}. If the Unit is destroyed, the cargo will be destroyed also.
--- * AI_CARGO_PACKAGE, Contained in a @{Static}: Cargo can be contained within a Static. The cargo can be **collected** from the @Static. If the @{Static} is destroyed, the cargo will be destroyed.
--- * CARGO_SLINGLOAD, represented by a @{Cargo} that is transportable: Cargo can be represented by a Cargo object that is transportable. Destruction of the Cargo will mean that the cargo is lost.
---
--- * AI_CARGO_GROUPED, represented by a Group of CARGO_UNITs.
---
--- # 1) @{#AI_CARGO} class, extends @{Fsm#FSM_PROCESS}
---
--- The @{#AI_CARGO} class defines the core functions that defines a cargo object within MOOSE.
--- A cargo is a logical object defined that is available for transport, and has a life status within a simulation.
---
--- The AI_CARGO is a state machine: it manages the different events and states of the cargo.
--- All derived classes from AI_CARGO follow the same state machine, expose the same cargo event functions, and provide the same cargo states.
---
--- ## 1.2.1) AI_CARGO Events:
---
--- * @{#AI_CARGO.Board}( ToCarrier ): Boards the cargo to a carrier.
--- * @{#AI_CARGO.Load}( ToCarrier ): Loads the cargo into a carrier, regardless of its position.
--- * @{#AI_CARGO.UnBoard}( ToPointVec2 ): UnBoard the cargo from a carrier. This will trigger a movement of the cargo to the option ToPointVec2.
--- * @{#AI_CARGO.UnLoad}( ToPointVec2 ): UnLoads the cargo from a carrier.
--- * @{#AI_CARGO.Dead}( Controllable ): The cargo is dead. The cargo process will be ended.
---
--- ## 1.2.2) AI_CARGO States:
---
--- * **UnLoaded**: The cargo is unloaded from a carrier.
--- * **Boarding**: The cargo is currently boarding (= running) into a carrier.
--- * **Loaded**: The cargo is loaded into a carrier.
--- * **UnBoarding**: The cargo is currently unboarding (=running) from a carrier.
--- * **Dead**: The cargo is dead ...
--- * **End**: The process has come to an end.
---
--- ## 1.2.3) AI_CARGO state transition methods:
---
--- State transition functions can be set **by the mission designer** customizing or improving the behaviour of the state.
--- There are 2 moments when state transition methods will be called by the state machine:
---
--- * **Leaving** the state.
--- The state transition method needs to start with the name **OnLeave + the name of the state**.
--- If the state transition method returns false, then the processing of the state transition will not be done!
--- If you want to change the behaviour of the AIControllable at this event, return false,
--- but then you'll need to specify your own logic using the AIControllable!
---
--- * **Entering** the state.
--- The state transition method needs to start with the name **OnEnter + the name of the state**.
--- These state transition methods need to provide a return value, which is specified at the function description.
---
--- # 2) #AI_CARGO_UNIT class
---
--- The AI_CARGO_UNIT class defines a cargo that is represented by a UNIT object within the simulator, and can be transported by a carrier.
--- Use the event functions as described above to Load, UnLoad, Board, UnBoard the AI_CARGO_UNIT objects to and from carriers.
---
--- # 5) #AI_CARGO_GROUPED class
---
--- The AI_CARGO_GROUPED class defines a cargo that is represented by a group of UNIT objects within the simulator, and can be transported by a carrier.
--- Use the event functions as described above to Load, UnLoad, Board, UnBoard the AI_CARGO_UNIT objects to and from carriers.
---
--- This module is still under construction, but is described above works already, and will keep working ...
---
--- @module Cargo
-
--- Events
-
--- Board
-
---- Boards the cargo to a Carrier. The event will create a movement (= running or driving) of the cargo to the Carrier.
--- The cargo must be in the **UnLoaded** state.
--- @function [parent=#AI_CARGO] Board
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE ToCarrier The Carrier that will hold the cargo.
-
---- Boards the cargo to a Carrier. The event will create a movement (= running or driving) of the cargo to the Carrier.
--- The cargo must be in the **UnLoaded** state.
--- @function [parent=#AI_CARGO] __Board
--- @param #AI_CARGO self
--- @param #number DelaySeconds The amount of seconds to delay the action.
--- @param Wrapper.Controllable#CONTROLLABLE ToCarrier The Carrier that will hold the cargo.
-
-
--- UnBoard
-
---- UnBoards the cargo to a Carrier. The event will create a movement (= running or driving) of the cargo from the Carrier.
--- The cargo must be in the **Loaded** state.
--- @function [parent=#AI_CARGO] UnBoard
--- @param #AI_CARGO self
--- @param Core.Point#POINT_VEC2 ToPointVec2 (optional) @{Point#POINT_VEC2) to where the cargo should run after onboarding. If not provided, the cargo will run to 60 meters behind the Carrier location.
-
---- UnBoards the cargo to a Carrier. The event will create a movement (= running or driving) of the cargo from the Carrier.
--- The cargo must be in the **Loaded** state.
--- @function [parent=#AI_CARGO] __UnBoard
--- @param #AI_CARGO self
--- @param #number DelaySeconds The amount of seconds to delay the action.
--- @param Core.Point#POINT_VEC2 ToPointVec2 (optional) @{Point#POINT_VEC2) to where the cargo should run after onboarding. If not provided, the cargo will run to 60 meters behind the Carrier location.
-
-
--- Load
-
---- Loads the cargo to a Carrier. The event will load the cargo into the Carrier regardless of its position. There will be no movement simulated of the cargo loading.
--- The cargo must be in the **UnLoaded** state.
--- @function [parent=#AI_CARGO] Load
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE ToCarrier The Carrier that will hold the cargo.
-
---- Loads the cargo to a Carrier. The event will load the cargo into the Carrier regardless of its position. There will be no movement simulated of the cargo loading.
--- The cargo must be in the **UnLoaded** state.
--- @function [parent=#AI_CARGO] __Load
--- @param #AI_CARGO self
--- @param #number DelaySeconds The amount of seconds to delay the action.
--- @param Wrapper.Controllable#CONTROLLABLE ToCarrier The Carrier that will hold the cargo.
-
-
--- UnLoad
-
---- UnLoads the cargo to a Carrier. The event will unload the cargo from the Carrier. There will be no movement simulated of the cargo loading.
--- The cargo must be in the **Loaded** state.
--- @function [parent=#AI_CARGO] UnLoad
--- @param #AI_CARGO self
--- @param Core.Point#POINT_VEC2 ToPointVec2 (optional) @{Point#POINT_VEC2) to where the cargo will be placed after unloading. If not provided, the cargo will be placed 60 meters behind the Carrier location.
-
---- UnLoads the cargo to a Carrier. The event will unload the cargo from the Carrier. There will be no movement simulated of the cargo loading.
--- The cargo must be in the **Loaded** state.
--- @function [parent=#AI_CARGO] __UnLoad
--- @param #AI_CARGO self
--- @param #number DelaySeconds The amount of seconds to delay the action.
--- @param Core.Point#POINT_VEC2 ToPointVec2 (optional) @{Point#POINT_VEC2) to where the cargo will be placed after unloading. If not provided, the cargo will be placed 60 meters behind the Carrier location.
-
--- State Transition Functions
-
--- UnLoaded
-
---- @function [parent=#AI_CARGO] OnLeaveUnLoaded
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
--- @return #boolean
-
---- @function [parent=#AI_CARGO] OnEnterUnLoaded
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
-
--- Loaded
-
---- @function [parent=#AI_CARGO] OnLeaveLoaded
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
--- @return #boolean
-
---- @function [parent=#AI_CARGO] OnEnterLoaded
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
-
--- Boarding
-
---- @function [parent=#AI_CARGO] OnLeaveBoarding
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
--- @return #boolean
-
---- @function [parent=#AI_CARGO] OnEnterBoarding
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
-
--- UnBoarding
-
---- @function [parent=#AI_CARGO] OnLeaveUnBoarding
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
--- @return #boolean
-
---- @function [parent=#AI_CARGO] OnEnterUnBoarding
--- @param #AI_CARGO self
--- @param Wrapper.Controllable#CONTROLLABLE Controllable
-
-
--- TODO: Find all Carrier objects and make the type of the Carriers Wrapper.Unit#UNIT in the documentation.
-
-CARGOS = {}
-
-do -- AI_CARGO
-
- --- @type AI_CARGO
- -- @extends Core.Fsm#FSM_PROCESS
- -- @field #string Type A string defining the type of the cargo. eg. Engineers, Equipment, Screwdrivers.
- -- @field #string Name A string defining the name of the cargo. The name is the unique identifier of the cargo.
- -- @field #number Weight A number defining the weight of the cargo. The weight is expressed in kg.
- -- @field #number ReportRadius (optional) A number defining the radius in meters when the cargo is signalling or reporting to a Carrier.
- -- @field #number NearRadius (optional) A number defining the radius in meters when the cargo is near to a Carrier, so that it can be loaded.
- -- @field Wrapper.Controllable#CONTROLLABLE CargoObject The alive DCS object representing the cargo. This value can be nil, meaning, that the cargo is not represented anywhere...
- -- @field Wrapper.Controllable#CONTROLLABLE CargoCarrier The alive DCS object carrying the cargo. This value can be nil, meaning, that the cargo is not contained anywhere...
- -- @field #boolean Slingloadable This flag defines if the cargo can be slingloaded.
- -- @field #boolean Moveable This flag defines if the cargo is moveable.
- -- @field #boolean Representable This flag defines if the cargo can be represented by a DCS Unit.
- -- @field #boolean Containable This flag defines if the cargo can be contained within a DCS Unit.
- AI_CARGO = {
- ClassName = "AI_CARGO",
- Type = nil,
- Name = nil,
- Weight = nil,
- CargoObject = nil,
- CargoCarrier = nil,
- Representable = false,
- Slingloadable = false,
- Moveable = false,
- Containable = false,
- }
-
---- @type AI_CARGO.CargoObjects
--- @map < #string, Wrapper.Positionable#POSITIONABLE > The alive POSITIONABLE objects representing the the cargo.
-
-
---- AI_CARGO Constructor. This class is an abstract class and should not be instantiated.
--- @param #AI_CARGO self
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO
-function AI_CARGO:New( Type, Name, Weight, ReportRadius, NearRadius )
-
- local self = BASE:Inherit( self, FSM:New() ) -- Core.Fsm#FSM_CONTROLLABLE
- self:F( { Type, Name, Weight, ReportRadius, NearRadius } )
-
- self:SetStartState( "UnLoaded" )
- self:AddTransition( "UnLoaded", "Board", "Boarding" )
- self:AddTransition( "Boarding", "Boarding", "Boarding" )
- self:AddTransition( "Boarding", "Load", "Loaded" )
- self:AddTransition( "UnLoaded", "Load", "Loaded" )
- self:AddTransition( "Loaded", "UnBoard", "UnBoarding" )
- self:AddTransition( "UnBoarding", "UnBoarding", "UnBoarding" )
- self:AddTransition( "UnBoarding", "UnLoad", "UnLoaded" )
- self:AddTransition( "Loaded", "UnLoad", "UnLoaded" )
-
-
- self.Type = Type
- self.Name = Name
- self.Weight = Weight
- self.ReportRadius = ReportRadius
- self.NearRadius = NearRadius
- self.CargoObject = nil
- self.CargoCarrier = nil
- self.Representable = false
- self.Slingloadable = false
- self.Moveable = false
- self.Containable = false
-
-
- self.CargoScheduler = SCHEDULER:New()
-
- CARGOS[self.Name] = self
-
- return self
-end
-
-
---- Template method to spawn a new representation of the AI_CARGO in the simulator.
--- @param #AI_CARGO self
--- @return #AI_CARGO
-function AI_CARGO:Spawn( PointVec2 )
- self:F()
-
-end
-
-
---- Check if CargoCarrier is near the Cargo to be Loaded.
--- @param #AI_CARGO self
--- @param Core.Point#POINT_VEC2 PointVec2
--- @return #boolean
-function AI_CARGO:IsNear( PointVec2 )
- self:F( { PointVec2 } )
-
- local Distance = PointVec2:DistanceFromPointVec2( self.CargoObject:GetPointVec2() )
- self:T( Distance )
-
- if Distance <= self.NearRadius then
- return true
- else
- return false
- end
-end
-
-end
-
-do -- AI_CARGO_REPRESENTABLE
-
- --- @type AI_CARGO_REPRESENTABLE
- -- @extends #AI_CARGO
- AI_CARGO_REPRESENTABLE = {
- ClassName = "AI_CARGO_REPRESENTABLE"
- }
-
---- AI_CARGO_REPRESENTABLE Constructor.
--- @param #AI_CARGO_REPRESENTABLE self
--- @param Wrapper.Controllable#Controllable CargoObject
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO_REPRESENTABLE
-function AI_CARGO_REPRESENTABLE:New( CargoObject, Type, Name, Weight, ReportRadius, NearRadius )
- local self = BASE:Inherit( self, AI_CARGO:New( Type, Name, Weight, ReportRadius, NearRadius ) ) -- #AI_CARGO
- self:F( { Type, Name, Weight, ReportRadius, NearRadius } )
-
- return self
-end
-
---- Route a cargo unit to a PointVec2.
--- @param #AI_CARGO_REPRESENTABLE self
--- @param Core.Point#POINT_VEC2 ToPointVec2
--- @param #number Speed
--- @return #AI_CARGO_REPRESENTABLE
-function AI_CARGO_REPRESENTABLE:RouteTo( ToPointVec2, Speed )
- self:F2( ToPointVec2 )
-
- local Points = {}
-
- local PointStartVec2 = self.CargoObject:GetPointVec2()
-
- Points[#Points+1] = PointStartVec2:RoutePointGround( Speed )
- Points[#Points+1] = ToPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoObject:TaskRoute( Points )
- self.CargoObject:SetTask( TaskRoute, 2 )
- return self
-end
-
-end -- AI_CARGO
-
-do -- AI_CARGO_UNIT
-
- --- @type AI_CARGO_UNIT
- -- @extends #AI_CARGO_REPRESENTABLE
- AI_CARGO_UNIT = {
- ClassName = "AI_CARGO_UNIT"
- }
-
---- AI_CARGO_UNIT Constructor.
--- @param #AI_CARGO_UNIT self
--- @param Wrapper.Unit#UNIT CargoUnit
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO_UNIT
-function AI_CARGO_UNIT:New( CargoUnit, Type, Name, Weight, ReportRadius, NearRadius )
- local self = BASE:Inherit( self, AI_CARGO_REPRESENTABLE:New( CargoUnit, Type, Name, Weight, ReportRadius, NearRadius ) ) -- #AI_CARGO_UNIT
- self:F( { Type, Name, Weight, ReportRadius, NearRadius } )
-
- self:T( CargoUnit )
- self.CargoObject = CargoUnit
-
- self:T( self.ClassName )
-
- return self
-end
-
---- Enter UnBoarding State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Core.Point#POINT_VEC2 ToPointVec2
-function AI_CARGO_UNIT:onenterUnBoarding( From, Event, To, ToPointVec2 )
- self:F()
-
- local Angle = 180
- local Speed = 10
- local DeployDistance = 5
- local RouteDistance = 60
-
- if From == "Loaded" then
-
- local CargoCarrierPointVec2 = self.CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = self.CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- local CargoDeployPointVec2 = CargoCarrierPointVec2:Translate( DeployDistance, CargoDeployHeading )
- local CargoRoutePointVec2 = CargoCarrierPointVec2:Translate( RouteDistance, CargoDeployHeading )
-
- -- if there is no ToPointVec2 given, then use the CargoRoutePointVec2
- ToPointVec2 = ToPointVec2 or CargoRoutePointVec2
-
- local FromPointVec2 = CargoCarrierPointVec2
-
- -- Respawn the group...
- if self.CargoObject then
- self.CargoObject:ReSpawn( CargoDeployPointVec2:GetVec3(), CargoDeployHeading )
- self.CargoCarrier = nil
-
- local Points = {}
- Points[#Points+1] = FromPointVec2:RoutePointGround( Speed )
- Points[#Points+1] = ToPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoObject:TaskRoute( Points )
- self.CargoObject:SetTask( TaskRoute, 1 )
-
- self:__UnBoarding( 1, ToPointVec2 )
- end
- end
-
-end
-
---- Leave UnBoarding State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Core.Point#POINT_VEC2 ToPointVec2
-function AI_CARGO_UNIT:onleaveUnBoarding( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- local Angle = 180
- local Speed = 10
- local Distance = 5
-
- if From == "UnBoarding" then
- if self:IsNear( ToPointVec2 ) then
- return true
- else
- self:__UnBoarding( 1, ToPointVec2 )
- end
- return false
- end
-
-end
-
---- UnBoard Event.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Core.Point#POINT_VEC2 ToPointVec2
-function AI_CARGO_UNIT:onafterUnBoarding( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- self.CargoInAir = self.CargoObject:InAir()
-
- self:T( self.CargoInAir )
-
- -- Only unboard the cargo when the carrier is not in the air.
- -- (eg. cargo can be on a oil derrick, moving the cargo on the oil derrick will drop the cargo on the sea).
- if not self.CargoInAir then
-
- end
-
- self:__UnLoad( 1, ToPointVec2 )
-
-end
-
-
-
---- Enter UnLoaded State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Core.Point#POINT_VEC2
-function AI_CARGO_UNIT:onenterUnLoaded( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- local Angle = 180
- local Speed = 10
- local Distance = 5
-
- if From == "Loaded" then
- local StartPointVec2 = self.CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = self.CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- local CargoDeployPointVec2 = StartPointVec2:Translate( Distance, CargoDeployHeading )
-
- ToPointVec2 = ToPointVec2 or POINT_VEC2:New( CargoDeployPointVec2:GetX(), CargoDeployPointVec2:GetY() )
-
- -- Respawn the group...
- if self.CargoObject then
- self.CargoObject:ReSpawn( ToPointVec2:GetVec3(), 0 )
- self.CargoCarrier = nil
- end
-
- end
-
- if self.OnUnLoadedCallBack then
- self.OnUnLoadedCallBack( self, unpack( self.OnUnLoadedParameters ) )
- self.OnUnLoadedCallBack = nil
- end
-
-end
-
-
-
---- Enter Boarding State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
-function AI_CARGO_UNIT:onenterBoarding( From, Event, To, CargoCarrier )
- self:F( { CargoCarrier.UnitName, From, Event, To } )
-
- local Speed = 10
- local Angle = 180
- local Distance = 5
-
- if From == "UnLoaded" then
- local CargoCarrierPointVec2 = CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- local CargoDeployPointVec2 = CargoCarrierPointVec2:Translate( Distance, CargoDeployHeading )
-
- local Points = {}
-
- local PointStartVec2 = self.CargoObject:GetPointVec2()
-
- Points[#Points+1] = PointStartVec2:RoutePointGround( Speed )
- Points[#Points+1] = CargoDeployPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoObject:TaskRoute( Points )
- self.CargoObject:SetTask( TaskRoute, 2 )
- end
-
-end
-
---- Leave Boarding State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
-function AI_CARGO_UNIT:onleaveBoarding( From, Event, To, CargoCarrier )
- self:F( { CargoCarrier.UnitName, From, Event, To } )
-
- if self:IsNear( CargoCarrier:GetPointVec2() ) then
- self:__Load( 1, CargoCarrier )
- return true
- else
- self:__Boarding( 1, CargoCarrier )
- end
- return false
-end
-
---- Loaded State.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
-function AI_CARGO_UNIT:onenterLoaded( From, Event, To, CargoCarrier )
- self:F()
-
- self.CargoCarrier = CargoCarrier
-
- -- Only destroy the CargoObject is if there is a CargoObject (packages don't have CargoObjects).
- if self.CargoObject then
- self:T("Destroying")
- self.CargoObject:Destroy()
- end
-end
-
-
---- Board Event.
--- @param #AI_CARGO_UNIT self
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_UNIT:onafterBoard( From, Event, To, CargoCarrier )
- self:F()
-
- self.CargoInAir = self.CargoObject:InAir()
-
- self:T( self.CargoInAir )
-
- -- Only move the group to the carrier when the cargo is not in the air
- -- (eg. cargo can be on a oil derrick, moving the cargo on the oil derrick will drop the cargo on the sea).
- if not self.CargoInAir then
- self:Load( CargoCarrier )
- end
-
-end
-
-end
-
-do -- AI_CARGO_PACKAGE
-
- --- @type AI_CARGO_PACKAGE
- -- @extends #AI_CARGO_REPRESENTABLE
- AI_CARGO_PACKAGE = {
- ClassName = "AI_CARGO_PACKAGE"
- }
-
---- AI_CARGO_PACKAGE Constructor.
--- @param #AI_CARGO_PACKAGE self
--- @param Wrapper.Unit#UNIT CargoCarrier The UNIT carrying the package.
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO_PACKAGE
-function AI_CARGO_PACKAGE:New( CargoCarrier, Type, Name, Weight, ReportRadius, NearRadius )
- local self = BASE:Inherit( self, AI_CARGO_REPRESENTABLE:New( CargoCarrier, Type, Name, Weight, ReportRadius, NearRadius ) ) -- #AI_CARGO_PACKAGE
- self:F( { Type, Name, Weight, ReportRadius, NearRadius } )
-
- self:T( CargoCarrier )
- self.CargoCarrier = CargoCarrier
-
- return self
-end
-
---- Board Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @param #number Speed
--- @param #number BoardDistance
--- @param #number Angle
-function AI_CARGO_PACKAGE:onafterOnBoard( From, Event, To, CargoCarrier, Speed, BoardDistance, LoadDistance, Angle )
- self:F()
-
- self.CargoInAir = self.CargoCarrier:InAir()
-
- self:T( self.CargoInAir )
-
- -- Only move the CargoCarrier to the New CargoCarrier when the New CargoCarrier is not in the air.
- if not self.CargoInAir then
-
- local Points = {}
-
- local StartPointVec2 = self.CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- self:T( { CargoCarrierHeading, CargoDeployHeading } )
- local CargoDeployPointVec2 = CargoCarrier:GetPointVec2():Translate( BoardDistance, CargoDeployHeading )
-
- Points[#Points+1] = StartPointVec2:RoutePointGround( Speed )
- Points[#Points+1] = CargoDeployPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoCarrier:TaskRoute( Points )
- self.CargoCarrier:SetTask( TaskRoute, 1 )
- end
-
- self:Boarded( CargoCarrier, Speed, BoardDistance, LoadDistance, Angle )
-
-end
-
---- Check if CargoCarrier is near the Cargo to be Loaded.
--- @param #AI_CARGO_PACKAGE self
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @return #boolean
-function AI_CARGO_PACKAGE:IsNear( CargoCarrier )
- self:F()
-
- local CargoCarrierPoint = CargoCarrier:GetPointVec2()
-
- local Distance = CargoCarrierPoint:DistanceFromPointVec2( self.CargoCarrier:GetPointVec2() )
- self:T( Distance )
-
- if Distance <= self.NearRadius then
- return true
- else
- return false
- end
-end
-
---- Boarded Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
-function AI_CARGO_PACKAGE:onafterOnBoarded( From, Event, To, CargoCarrier, Speed, BoardDistance, LoadDistance, Angle )
- self:F()
-
- if self:IsNear( CargoCarrier ) then
- self:__Load( 1, CargoCarrier, Speed, LoadDistance, Angle )
- else
- self:__Boarded( 1, CargoCarrier, Speed, BoardDistance, LoadDistance, Angle )
- end
-end
-
---- UnBoard Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param #number Speed
--- @param #number UnLoadDistance
--- @param #number UnBoardDistance
--- @param #number Radius
--- @param #number Angle
-function AI_CARGO_PACKAGE:onafterUnBoard( From, Event, To, CargoCarrier, Speed, UnLoadDistance, UnBoardDistance, Radius, Angle )
- self:F()
-
- self.CargoInAir = self.CargoCarrier:InAir()
-
- self:T( self.CargoInAir )
-
- -- Only unboard the cargo when the carrier is not in the air.
- -- (eg. cargo can be on a oil derrick, moving the cargo on the oil derrick will drop the cargo on the sea).
- if not self.CargoInAir then
-
- self:_Next( self.FsmP.UnLoad, UnLoadDistance, Angle )
-
- local Points = {}
-
- local StartPointVec2 = CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = self.CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- self:T( { CargoCarrierHeading, CargoDeployHeading } )
- local CargoDeployPointVec2 = StartPointVec2:Translate( UnBoardDistance, CargoDeployHeading )
-
- Points[#Points+1] = StartPointVec2:RoutePointGround( Speed )
- Points[#Points+1] = CargoDeployPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = CargoCarrier:TaskRoute( Points )
- CargoCarrier:SetTask( TaskRoute, 1 )
- end
-
- self:__UnBoarded( 1 , CargoCarrier, Speed )
-
-end
-
---- UnBoarded Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
-function AI_CARGO_PACKAGE:onafterUnBoarded( From, Event, To, CargoCarrier, Speed )
- self:F()
-
- if self:IsNear( CargoCarrier ) then
- self:__UnLoad( 1, CargoCarrier, Speed )
- else
- self:__UnBoarded( 1, CargoCarrier, Speed )
- end
-end
-
---- Load Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @param #number Speed
--- @param #number LoadDistance
--- @param #number Angle
-function AI_CARGO_PACKAGE:onafterLoad( From, Event, To, CargoCarrier, Speed, LoadDistance, Angle )
- self:F()
-
- self.CargoCarrier = CargoCarrier
-
- local StartPointVec2 = self.CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = self.CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- local CargoDeployPointVec2 = StartPointVec2:Translate( LoadDistance, CargoDeployHeading )
-
- local Points = {}
- Points[#Points+1] = StartPointVec2:RoutePointGround( Speed )
- Points[#Points+1] = CargoDeployPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoCarrier:TaskRoute( Points )
- self.CargoCarrier:SetTask( TaskRoute, 1 )
-
-end
-
---- UnLoad Event.
--- @param #AI_CARGO_PACKAGE self
--- @param #string Event
--- @param #string From
--- @param #string To
--- @param #number Distance
--- @param #number Angle
-function AI_CARGO_PACKAGE:onafterUnLoad( From, Event, To, CargoCarrier, Speed, Distance, Angle )
- self:F()
-
- local StartPointVec2 = self.CargoCarrier:GetPointVec2()
- local CargoCarrierHeading = self.CargoCarrier:GetHeading() -- Get Heading of object in degrees.
- local CargoDeployHeading = ( ( CargoCarrierHeading + Angle ) >= 360 ) and ( CargoCarrierHeading + Angle - 360 ) or ( CargoCarrierHeading + Angle )
- local CargoDeployPointVec2 = StartPointVec2:Translate( Distance, CargoDeployHeading )
-
- self.CargoCarrier = CargoCarrier
-
- local Points = {}
- Points[#Points+1] = StartPointVec2:RoutePointGround( Speed )
- Points[#Points+1] = CargoDeployPointVec2:RoutePointGround( Speed )
-
- local TaskRoute = self.CargoCarrier:TaskRoute( Points )
- self.CargoCarrier:SetTask( TaskRoute, 1 )
-
-end
-
-
-end
-
-do -- AI_CARGO_GROUP
-
- --- @type AI_CARGO_GROUP
- -- @extends AI.AI_Cargo#AI_CARGO
- -- @field Set#SET_BASE CargoSet A set of cargo objects.
- -- @field #string Name A string defining the name of the cargo group. The name is the unique identifier of the cargo.
- AI_CARGO_GROUP = {
- ClassName = "AI_CARGO_GROUP",
- }
-
---- AI_CARGO_GROUP constructor.
--- @param #AI_CARGO_GROUP self
--- @param Core.Set#Set_BASE CargoSet
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO_GROUP
-function AI_CARGO_GROUP:New( CargoSet, Type, Name, ReportRadius, NearRadius )
- local self = BASE:Inherit( self, AI_CARGO:New( Type, Name, 0, ReportRadius, NearRadius ) ) -- #AI_CARGO_GROUP
- self:F( { Type, Name, ReportRadius, NearRadius } )
-
- self.CargoSet = CargoSet
-
-
- return self
-end
-
-end -- AI_CARGO_GROUP
-
-do -- AI_CARGO_GROUPED
-
- --- @type AI_CARGO_GROUPED
- -- @extends AI.AI_Cargo#AI_CARGO_GROUP
- AI_CARGO_GROUPED = {
- ClassName = "AI_CARGO_GROUPED",
- }
-
---- AI_CARGO_GROUPED constructor.
--- @param #AI_CARGO_GROUPED self
--- @param Core.Set#Set_BASE CargoSet
--- @param #string Type
--- @param #string Name
--- @param #number Weight
--- @param #number ReportRadius (optional)
--- @param #number NearRadius (optional)
--- @return #AI_CARGO_GROUPED
-function AI_CARGO_GROUPED:New( CargoSet, Type, Name, ReportRadius, NearRadius )
- local self = BASE:Inherit( self, AI_CARGO_GROUP:New( CargoSet, Type, Name, ReportRadius, NearRadius ) ) -- #AI_CARGO_GROUPED
- self:F( { Type, Name, ReportRadius, NearRadius } )
-
- return self
-end
-
---- Enter Boarding State.
--- @param #AI_CARGO_GROUPED self
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onenterBoarding( From, Event, To, CargoCarrier )
- self:F( { CargoCarrier.UnitName, From, Event, To } )
-
- if From == "UnLoaded" then
-
- -- For each Cargo object within the AI_CARGO_GROUPED, route each object to the CargoLoadPointVec2
- self.CargoSet:ForEach(
- function( Cargo )
- Cargo:__Board( 1, CargoCarrier )
- end
- )
-
- self:__Boarding( 1, CargoCarrier )
- end
-
-end
-
---- Enter Loaded State.
--- @param #AI_CARGO_GROUPED self
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onenterLoaded( From, Event, To, CargoCarrier )
- self:F( { CargoCarrier.UnitName, From, Event, To } )
-
- if From == "UnLoaded" then
- -- For each Cargo object within the AI_CARGO_GROUPED, load each cargo to the CargoCarrier.
- for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
- Cargo:Load( CargoCarrier )
- end
- end
-end
-
---- Leave Boarding State.
--- @param #AI_CARGO_GROUPED self
--- @param Wrapper.Unit#UNIT CargoCarrier
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onleaveBoarding( From, Event, To, CargoCarrier )
- self:F( { CargoCarrier.UnitName, From, Event, To } )
-
- local Boarded = true
-
- -- For each Cargo object within the AI_CARGO_GROUPED, route each object to the CargoLoadPointVec2
- for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
- self:T( Cargo.current )
- if not Cargo:is( "Loaded" ) then
- Boarded = false
- end
- end
-
- if not Boarded then
- self:__Boarding( 1, CargoCarrier )
- else
- self:__Load( 1, CargoCarrier )
- end
- return Boarded
-end
-
---- Enter UnBoarding State.
--- @param #AI_CARGO_GROUPED self
--- @param Core.Point#POINT_VEC2 ToPointVec2
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onenterUnBoarding( From, Event, To, ToPointVec2 )
- self:F()
-
- local Timer = 1
-
- if From == "Loaded" then
-
- -- For each Cargo object within the AI_CARGO_GROUPED, route each object to the CargoLoadPointVec2
- self.CargoSet:ForEach(
- function( Cargo )
- Cargo:__UnBoard( Timer, ToPointVec2 )
- Timer = Timer + 10
- end
- )
-
- self:__UnBoarding( 1, ToPointVec2 )
- end
-
-end
-
---- Leave UnBoarding State.
--- @param #AI_CARGO_GROUPED self
--- @param Core.Point#POINT_VEC2 ToPointVec2
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onleaveUnBoarding( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- local Angle = 180
- local Speed = 10
- local Distance = 5
-
- if From == "UnBoarding" then
- local UnBoarded = true
-
- -- For each Cargo object within the AI_CARGO_GROUPED, route each object to the CargoLoadPointVec2
- for CargoID, Cargo in pairs( self.CargoSet:GetSet() ) do
- self:T( Cargo.current )
- if not Cargo:is( "UnLoaded" ) then
- UnBoarded = false
- end
- end
-
- if UnBoarded then
- return true
- else
- self:__UnBoarding( 1, ToPointVec2 )
- end
-
- return false
- end
-
-end
-
---- UnBoard Event.
--- @param #AI_CARGO_GROUPED self
--- @param Core.Point#POINT_VEC2 ToPointVec2
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onafterUnBoarding( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- self:__UnLoad( 1, ToPointVec2 )
-end
-
-
-
---- Enter UnLoaded State.
--- @param #AI_CARGO_GROUPED self
--- @param Core.Point#POINT_VEC2
--- @param #string Event
--- @param #string From
--- @param #string To
-function AI_CARGO_GROUPED:onenterUnLoaded( From, Event, To, ToPointVec2 )
- self:F( { ToPointVec2, From, Event, To } )
-
- if From == "Loaded" then
-
- -- For each Cargo object within the AI_CARGO_GROUPED, route each object to the CargoLoadPointVec2
- self.CargoSet:ForEach(
- function( Cargo )
- Cargo:UnLoad( ToPointVec2 )
- end
- )
-
- end
-
-end
-
-end -- AI_CARGO_GROUPED
-
-
-
---- (SP) (MP) (FSM) Accept or reject process for player (task) assignments.
---
--- ===
---
--- # @{#ACT_ASSIGN} FSM template class, extends @{Fsm#FSM_PROCESS}
---
--- ## ACT_ASSIGN state machine:
---
--- This class is a state machine: it manages a process that is triggered by events causing state transitions to occur.
--- All derived classes from this class will start with the class name, followed by a \_. See the relevant derived class descriptions below.
--- Each derived class follows exactly the same process, using the same events and following the same state transitions,
--- but will have **different implementation behaviour** upon each event or state transition.
---
--- ### ACT_ASSIGN **Events**:
---
--- These are the events defined in this class:
---
--- * **Start**: Start the tasking acceptance process.
--- * **Assign**: Assign the task.
--- * **Reject**: Reject the task..
---
--- ### ACT_ASSIGN **Event methods**:
---
--- Event methods are available (dynamically allocated by the state machine), that accomodate for state transitions occurring in the process.
--- There are two types of event methods, which you can use to influence the normal mechanisms in the state machine:
---
--- * **Immediate**: The event method has exactly the name of the event.
--- * **Delayed**: The event method starts with a __ + the name of the event. The first parameter of the event method is a number value, expressing the delay in seconds when the event will be executed.
---
--- ### ACT_ASSIGN **States**:
---
--- * **UnAssigned**: The player has not accepted the task.
--- * **Assigned (*)**: The player has accepted the task.
--- * **Rejected (*)**: The player has not accepted the task.
--- * **Waiting**: The process is awaiting player feedback.
--- * **Failed (*)**: The process has failed.
---
--- (*) End states of the process.
---
--- ### ACT_ASSIGN state transition methods:
---
--- State transition functions can be set **by the mission designer** customizing or improving the behaviour of the state.
--- There are 2 moments when state transition methods will be called by the state machine:
---
--- * **Before** the state transition.
--- The state transition method needs to start with the name **OnBefore + the name of the state**.
--- If the state transition method returns false, then the processing of the state transition will not be done!
--- If you want to change the behaviour of the AIControllable at this event, return false,
--- but then you'll need to specify your own logic using the AIControllable!
---
--- * **After** the state transition.
--- The state transition method needs to start with the name **OnAfter + the name of the state**.
--- These state transition methods need to provide a return value, which is specified at the function description.
---
--- ===
---
--- # 1) @{#ACT_ASSIGN_ACCEPT} class, extends @{Fsm.Assign#ACT_ASSIGN}
---
--- The ACT_ASSIGN_ACCEPT class accepts by default a task for a player. No player intervention is allowed to reject the task.
---
--- ## 1.1) ACT_ASSIGN_ACCEPT constructor:
---
--- * @{#ACT_ASSIGN_ACCEPT.New}(): Creates a new ACT_ASSIGN_ACCEPT object.
---
--- ===
---
--- # 2) @{#ACT_ASSIGN_MENU_ACCEPT} class, extends @{Fsm.Assign#ACT_ASSIGN}
---
--- The ACT_ASSIGN_MENU_ACCEPT class accepts a task when the player accepts the task through an added menu option.
--- This assignment type is useful to conditionally allow the player to choose whether or not he would accept the task.
--- The assignment type also allows to reject the task.
---
--- ## 2.1) ACT_ASSIGN_MENU_ACCEPT constructor:
--- -----------------------------------------
---
--- * @{#ACT_ASSIGN_MENU_ACCEPT.New}(): Creates a new ACT_ASSIGN_MENU_ACCEPT object.
---
--- ===
---
--- @module Assign
-
-
-do -- ACT_ASSIGN
-
- --- ACT_ASSIGN class
- -- @type ACT_ASSIGN
- -- @field Tasking.Task#TASK Task
- -- @field Wrapper.Unit#UNIT ProcessUnit
- -- @field Core.Zone#ZONE_BASE TargetZone
- -- @extends Core.Fsm#FSM_PROCESS
- ACT_ASSIGN = {
- ClassName = "ACT_ASSIGN",
- }
-
-
- --- Creates a new task assignment state machine. The process will accept the task by default, no player intervention accepted.
- -- @param #ACT_ASSIGN self
- -- @return #ACT_ASSIGN The task acceptance process.
- function ACT_ASSIGN:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_PROCESS:New( "ACT_ASSIGN" ) ) -- Core.Fsm#FSM_PROCESS
-
- self:AddTransition( "UnAssigned", "Start", "Waiting" )
- self:AddTransition( "Waiting", "Assign", "Assigned" )
- self:AddTransition( "Waiting", "Reject", "Rejected" )
- self:AddTransition( "*", "Fail", "Failed" )
-
- self:AddEndState( "Assigned" )
- self:AddEndState( "Rejected" )
- self:AddEndState( "Failed" )
-
- self:SetStartState( "UnAssigned" )
-
- return self
- end
-
-end -- ACT_ASSIGN
-
-
-
-do -- ACT_ASSIGN_ACCEPT
-
- --- ACT_ASSIGN_ACCEPT class
- -- @type ACT_ASSIGN_ACCEPT
- -- @field Tasking.Task#TASK Task
- -- @field Wrapper.Unit#UNIT ProcessUnit
- -- @field Core.Zone#ZONE_BASE TargetZone
- -- @extends #ACT_ASSIGN
- ACT_ASSIGN_ACCEPT = {
- ClassName = "ACT_ASSIGN_ACCEPT",
- }
-
-
- --- Creates a new task assignment state machine. The process will accept the task by default, no player intervention accepted.
- -- @param #ACT_ASSIGN_ACCEPT self
- -- @param #string TaskBriefing
- function ACT_ASSIGN_ACCEPT:New( TaskBriefing )
-
- local self = BASE:Inherit( self, ACT_ASSIGN:New() ) -- #ACT_ASSIGN_ACCEPT
-
- self.TaskBriefing = TaskBriefing
-
- return self
- end
-
- function ACT_ASSIGN_ACCEPT:Init( FsmAssign )
-
- self.TaskBriefing = FsmAssign.TaskBriefing
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIGN_ACCEPT self
- -- @param Wrapper.Unit#UNIT ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIGN_ACCEPT:onafterStart( ProcessUnit, From, Event, To )
- self:E( { ProcessUnit, From, Event, To } )
-
- self:__Assign( 1 )
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIGN_ACCEPT self
- -- @param Wrapper.Unit#UNIT ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIGN_ACCEPT:onenterAssigned( ProcessUnit, From, Event, To )
- env.info( "in here" )
- self:E( { ProcessUnit, From, Event, To } )
-
- local ProcessGroup = ProcessUnit:GetGroup()
-
- self:Message( "You are assigned to the task " .. self.Task:GetName() )
-
- self.Task:Assign( ProcessUnit, ProcessUnit:GetPlayerName() )
- end
-
-end -- ACT_ASSIGN_ACCEPT
-
-
-do -- ACT_ASSIGN_MENU_ACCEPT
-
- --- ACT_ASSIGN_MENU_ACCEPT class
- -- @type ACT_ASSIGN_MENU_ACCEPT
- -- @field Tasking.Task#TASK Task
- -- @field Wrapper.Unit#UNIT ProcessUnit
- -- @field Core.Zone#ZONE_BASE TargetZone
- -- @extends #ACT_ASSIGN
- ACT_ASSIGN_MENU_ACCEPT = {
- ClassName = "ACT_ASSIGN_MENU_ACCEPT",
- }
-
- --- Init.
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- -- @param #string TaskName
- -- @param #string TaskBriefing
- -- @return #ACT_ASSIGN_MENU_ACCEPT self
- function ACT_ASSIGN_MENU_ACCEPT:New( TaskName, TaskBriefing )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, ACT_ASSIGN:New() ) -- #ACT_ASSIGN_MENU_ACCEPT
-
- self.TaskName = TaskName
- self.TaskBriefing = TaskBriefing
-
- return self
- end
-
- function ACT_ASSIGN_MENU_ACCEPT:Init( FsmAssign )
-
- self.TaskName = FsmAssign.TaskName
- self.TaskBriefing = FsmAssign.TaskBriefing
- end
-
-
- --- Creates a new task assignment state machine. The process will request from the menu if it accepts the task, if not, the unit is removed from the simulator.
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- -- @param #string TaskName
- -- @param #string TaskBriefing
- -- @return #ACT_ASSIGN_MENU_ACCEPT self
- function ACT_ASSIGN_MENU_ACCEPT:Init( TaskName, TaskBriefing )
-
- self.TaskBriefing = TaskBriefing
- self.TaskName = TaskName
-
- return self
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIGN_MENU_ACCEPT:onafterStart( ProcessUnit, From, Event, To )
- self:E( { ProcessUnit, From, Event, To } )
-
- self:Message( "Access the radio menu to accept the task. You have 30 seconds or the assignment will be cancelled." )
-
- local ProcessGroup = ProcessUnit:GetGroup()
-
- self.Menu = MENU_GROUP:New( ProcessGroup, "Task " .. self.TaskName .. " acceptance" )
- self.MenuAcceptTask = MENU_GROUP_COMMAND:New( ProcessGroup, "Accept task " .. self.TaskName, self.Menu, self.MenuAssign, self )
- self.MenuRejectTask = MENU_GROUP_COMMAND:New( ProcessGroup, "Reject task " .. self.TaskName, self.Menu, self.MenuReject, self )
- end
-
- --- Menu function.
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- function ACT_ASSIGN_MENU_ACCEPT:MenuAssign()
- self:E( )
-
- self:__Assign( 1 )
- end
-
- --- Menu function.
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- function ACT_ASSIGN_MENU_ACCEPT:MenuReject()
- self:E( )
-
- self:__Reject( 1 )
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIGN_MENU_ACCEPT:onafterAssign( ProcessUnit, From, Event, To )
- self:E( { ProcessUnit.UnitNameFrom, Event, To } )
-
- self.Menu:Remove()
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIGN_MENU_ACCEPT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIGN_MENU_ACCEPT:onafterReject( ProcessUnit, From, Event, To )
- self:E( { ProcessUnit.UnitName, From, Event, To } )
-
- self.Menu:Remove()
- --TODO: need to resolve this problem ... it has to do with the events ...
- --self.Task:UnAssignFromUnit( ProcessUnit )needs to become a callback funtion call upon the event
- ProcessUnit:Destroy()
- end
-
-end -- ACT_ASSIGN_MENU_ACCEPT
---- (SP) (MP) (FSM) Route AI or players through waypoints or to zones.
---
--- ===
---
--- # @{#ACT_ROUTE} FSM class, extends @{Fsm#FSM_PROCESS}
---
--- ## ACT_ROUTE state machine:
---
--- This class is a state machine: it manages a process that is triggered by events causing state transitions to occur.
--- All derived classes from this class will start with the class name, followed by a \_. See the relevant derived class descriptions below.
--- Each derived class follows exactly the same process, using the same events and following the same state transitions,
--- but will have **different implementation behaviour** upon each event or state transition.
---
--- ### ACT_ROUTE **Events**:
---
--- These are the events defined in this class:
---
--- * **Start**: The process is started. The process will go into the Report state.
--- * **Report**: The process is reporting to the player the route to be followed.
--- * **Route**: The process is routing the controllable.
--- * **Pause**: The process is pausing the route of the controllable.
--- * **Arrive**: The controllable has arrived at a route point.
--- * **More**: There are more route points that need to be followed. The process will go back into the Report state.
--- * **NoMore**: There are no more route points that need to be followed. The process will go into the Success state.
---
--- ### ACT_ROUTE **Event methods**:
---
--- Event methods are available (dynamically allocated by the state machine), that accomodate for state transitions occurring in the process.
--- There are two types of event methods, which you can use to influence the normal mechanisms in the state machine:
---
--- * **Immediate**: The event method has exactly the name of the event.
--- * **Delayed**: The event method starts with a __ + the name of the event. The first parameter of the event method is a number value, expressing the delay in seconds when the event will be executed.
---
--- ### ACT_ROUTE **States**:
---
--- * **None**: The controllable did not receive route commands.
--- * **Arrived (*)**: The controllable has arrived at a route point.
--- * **Aborted (*)**: The controllable has aborted the route path.
--- * **Routing**: The controllable is understay to the route point.
--- * **Pausing**: The process is pausing the routing. AI air will go into hover, AI ground will stop moving. Players can fly around.
--- * **Success (*)**: All route points were reached.
--- * **Failed (*)**: The process has failed.
---
--- (*) End states of the process.
---
--- ### ACT_ROUTE state transition methods:
---
--- State transition functions can be set **by the mission designer** customizing or improving the behaviour of the state.
--- There are 2 moments when state transition methods will be called by the state machine:
---
--- * **Before** the state transition.
--- The state transition method needs to start with the name **OnBefore + the name of the state**.
--- If the state transition method returns false, then the processing of the state transition will not be done!
--- If you want to change the behaviour of the AIControllable at this event, return false,
--- but then you'll need to specify your own logic using the AIControllable!
---
--- * **After** the state transition.
--- The state transition method needs to start with the name **OnAfter + the name of the state**.
--- These state transition methods need to provide a return value, which is specified at the function description.
---
--- ===
---
--- # 1) @{#ACT_ROUTE_ZONE} class, extends @{Fsm.Route#ACT_ROUTE}
---
--- The ACT_ROUTE_ZONE class implements the core functions to route an AIR @{Controllable} player @{Unit} to a @{Zone}.
--- The player receives on perioding times messages with the coordinates of the route to follow.
--- Upon arrival at the zone, a confirmation of arrival is sent, and the process will be ended.
---
--- # 1.1) ACT_ROUTE_ZONE constructor:
---
--- * @{#ACT_ROUTE_ZONE.New}(): Creates a new ACT_ROUTE_ZONE object.
---
--- ===
---
--- @module Route
-
-
-do -- ACT_ROUTE
-
- --- ACT_ROUTE class
- -- @type ACT_ROUTE
- -- @field Tasking.Task#TASK TASK
- -- @field Wrapper.Unit#UNIT ProcessUnit
- -- @field Core.Zone#ZONE_BASE Zone
- -- @extends Core.Fsm#FSM_PROCESS
- ACT_ROUTE = {
- ClassName = "ACT_ROUTE",
- }
-
-
- --- Creates a new routing state machine. The process will route a CLIENT to a ZONE until the CLIENT is within that ZONE.
- -- @param #ACT_ROUTE self
- -- @return #ACT_ROUTE self
- function ACT_ROUTE:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_PROCESS:New( "ACT_ROUTE" ) ) -- Core.Fsm#FSM_PROCESS
-
- self:AddTransition( "None", "Start", "Routing" )
- self:AddTransition( "*", "Report", "Reporting" )
- self:AddTransition( "*", "Route", "Routing" )
- self:AddTransition( "Routing", "Pause", "Pausing" )
- self:AddTransition( "*", "Abort", "Aborted" )
- self:AddTransition( "Routing", "Arrive", "Arrived" )
- self:AddTransition( "Arrived", "Success", "Success" )
- self:AddTransition( "*", "Fail", "Failed" )
- self:AddTransition( "", "", "" )
- self:AddTransition( "", "", "" )
-
- self:AddEndState( "Arrived" )
- self:AddEndState( "Failed" )
-
- self:SetStartState( "None" )
-
- return self
- end
-
- --- Task Events
-
- --- StateMachine callback function
- -- @param #ACT_ROUTE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ROUTE:onafterStart( ProcessUnit, From, Event, To )
-
-
- self:__Route( 1 )
- end
-
- --- Check if the controllable has arrived.
- -- @param #ACT_ROUTE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @return #boolean
- function ACT_ROUTE:onfuncHasArrived( ProcessUnit )
- return false
- end
-
- --- StateMachine callback function
- -- @param #ACT_ROUTE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ROUTE:onbeforeRoute( ProcessUnit, From, Event, To )
- self:F( { "BeforeRoute 1", self.DisplayCount, self.DisplayInterval } )
-
- if ProcessUnit:IsAlive() then
- self:F( "BeforeRoute 2" )
- local HasArrived = self:onfuncHasArrived( ProcessUnit ) -- Polymorphic
- if self.DisplayCount >= self.DisplayInterval then
- self:T( { HasArrived = HasArrived } )
- if not HasArrived then
- self:Report()
- end
- self.DisplayCount = 1
- else
- self.DisplayCount = self.DisplayCount + 1
- end
-
- self:T( { DisplayCount = self.DisplayCount } )
-
- if HasArrived then
- self:__Arrive( 1 )
- else
- self:__Route( 1 )
- end
-
- return HasArrived -- if false, then the event will not be executed...
- end
-
- return false
-
- end
-
-end -- ACT_ROUTE
-
-
-do -- ACT_ROUTE_POINT
-
- --- ACT_ROUTE_POINT class
- -- @type ACT_ROUTE_POINT
- -- @field Tasking.Task#TASK TASK
- -- @extends #ACT_ROUTE
- ACT_ROUTE_POINT = {
- ClassName = "ACT_ROUTE_POINT",
- }
-
-
- --- Creates a new routing state machine.
- -- The task will route a controllable to a PointVec2 until the controllable is within the Range.
- -- @param #ACT_ROUTE_POINT self
- -- @param Core.Point#POINT_VEC2 The PointVec2 to Target.
- -- @param #number Range The Distance to Target.
- -- @param Core.Zone#ZONE_BASE Zone
- function ACT_ROUTE_POINT:New( PointVec2, Range )
- local self = BASE:Inherit( self, ACT_ROUTE:New() ) -- #ACT_ROUTE_POINT
-
- self.PointVec2 = PointVec2
- self.Range = Range or 0
-
- self.DisplayInterval = 30
- self.DisplayCount = 30
- self.DisplayMessage = true
- self.DisplayTime = 10 -- 10 seconds is the default
-
- return self
- end
-
- function ACT_ROUTE_POINT:Init( FsmRoute )
-
- self.PointVec2 = FsmRoute.PointVec2
- self.Range = FsmRoute.Range or 0
-
- self.DisplayInterval = 30
- self.DisplayCount = 30
- self.DisplayMessage = true
- self.DisplayTime = 10 -- 10 seconds is the default
- end
-
- --- Set PointVec2
- -- @param #ACT_ROUTE_POINT self
- -- @param Core.Point#POINT_VEC2 PointVec2 The PointVec2 to route to.
- function ACT_ROUTE_POINT:SetPointVec2( PointVec2 )
- self:F2( { PointVec2 } )
- self.PointVec2 = PointVec2
- end
-
- --- Get PointVec2
- -- @param #ACT_ROUTE_POINT self
- -- @return Core.Point#POINT_VEC2 PointVec2 The PointVec2 to route to.
- function ACT_ROUTE_POINT:GetPointVec2()
- self:F2( { self.PointVec2 } )
- return self.PointVec2
- end
-
- --- Set Range around PointVec2
- -- @param #ACT_ROUTE_POINT self
- -- @param #number Range The Range to consider the arrival. Default is 10000 meters.
- function ACT_ROUTE_POINT:SetRange( Range )
- self:F2( { self.Range } )
- self.Range = Range or 10000
- end
-
- --- Get Range around PointVec2
- -- @param #ACT_ROUTE_POINT self
- -- @return #number The Range to consider the arrival. Default is 10000 meters.
- function ACT_ROUTE_POINT:GetRange()
- return self.Range
- end
-
- --- Method override to check if the controllable has arrived.
- -- @param #ACT_ROUTE_POINT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @return #boolean
- function ACT_ROUTE_POINT:onfuncHasArrived( ProcessUnit )
-
- if ProcessUnit:IsAlive() then
- local Distance = self.PointVec2:Get2DDistance( ProcessUnit:GetPointVec2() )
-
- if Distance <= self.Range then
- local RouteText = "You have arrived."
- self:Message( RouteText )
- return true
- end
- end
-
- return false
- end
-
- --- Task Events
-
- --- StateMachine callback function
- -- @param #ACT_ROUTE_POINT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ROUTE_POINT:onenterReporting( ProcessUnit, From, Event, To )
-
- local TaskUnitPointVec2 = ProcessUnit:GetPointVec2()
- local RouteText = "Route to " .. TaskUnitPointVec2:GetBRText( self.PointVec2 ) .. " km."
- self:Message( RouteText )
- end
-
-end -- ACT_ROUTE_POINT
-
-
-do -- ACT_ROUTE_ZONE
-
- --- ACT_ROUTE_ZONE class
- -- @type ACT_ROUTE_ZONE
- -- @field Tasking.Task#TASK TASK
- -- @field Wrapper.Unit#UNIT ProcessUnit
- -- @field Core.Zone#ZONE_BASE Zone
- -- @extends #ACT_ROUTE
- ACT_ROUTE_ZONE = {
- ClassName = "ACT_ROUTE_ZONE",
- }
-
-
- --- Creates a new routing state machine. The task will route a controllable to a ZONE until the controllable is within that ZONE.
- -- @param #ACT_ROUTE_ZONE self
- -- @param Core.Zone#ZONE_BASE Zone
- function ACT_ROUTE_ZONE:New( Zone )
- local self = BASE:Inherit( self, ACT_ROUTE:New() ) -- #ACT_ROUTE_ZONE
-
- self.Zone = Zone
-
- self.DisplayInterval = 30
- self.DisplayCount = 30
- self.DisplayMessage = true
- self.DisplayTime = 10 -- 10 seconds is the default
-
- return self
- end
-
- function ACT_ROUTE_ZONE:Init( FsmRoute )
-
- self.Zone = FsmRoute.Zone
-
- self.DisplayInterval = 30
- self.DisplayCount = 30
- self.DisplayMessage = true
- self.DisplayTime = 10 -- 10 seconds is the default
- end
-
- --- Set Zone
- -- @param #ACT_ROUTE_ZONE self
- -- @param Core.Zone#ZONE_BASE Zone The Zone object where to route to.
- function ACT_ROUTE_ZONE:SetZone( Zone )
- self.Zone = Zone
- end
-
- --- Get Zone
- -- @param #ACT_ROUTE_ZONE self
- -- @return Core.Zone#ZONE_BASE Zone The Zone object where to route to.
- function ACT_ROUTE_ZONE:GetZone()
- return self.Zone
- end
-
- --- Method override to check if the controllable has arrived.
- -- @param #ACT_ROUTE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @return #boolean
- function ACT_ROUTE_ZONE:onfuncHasArrived( ProcessUnit )
-
- if ProcessUnit:IsInZone( self.Zone ) then
- local RouteText = "You have arrived within the zone."
- self:Message( RouteText )
- end
-
- return ProcessUnit:IsInZone( self.Zone )
- end
-
- --- Task Events
-
- --- StateMachine callback function
- -- @param #ACT_ROUTE_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ROUTE_ZONE:onenterReporting( ProcessUnit, From, Event, To )
-
- local ZoneVec2 = self.Zone:GetVec2()
- local ZonePointVec2 = POINT_VEC2:New( ZoneVec2.x, ZoneVec2.y )
- local TaskUnitVec2 = ProcessUnit:GetVec2()
- local TaskUnitPointVec2 = POINT_VEC2:New( TaskUnitVec2.x, TaskUnitVec2.y )
- local RouteText = "Route to " .. TaskUnitPointVec2:GetBRText( ZonePointVec2 ) .. " km."
- self:Message( RouteText )
- end
-
-end -- ACT_ROUTE_ZONE
---- **Actions** - ACT_ACCOUNT_ classes **account for** (detect, count & report) various DCS events occuring on @{Unit}s.
---
--- 
---
--- ===
---
--- @module Account
-
-
-do -- ACT_ACCOUNT
-
- --- # @{#ACT_ACCOUNT} FSM class, extends @{Fsm#FSM_PROCESS}
- --
- -- ## ACT_ACCOUNT state machine:
- --
- -- This class is a state machine: it manages a process that is triggered by events causing state transitions to occur.
- -- All derived classes from this class will start with the class name, followed by a \_. See the relevant derived class descriptions below.
- -- Each derived class follows exactly the same process, using the same events and following the same state transitions,
- -- but will have **different implementation behaviour** upon each event or state transition.
- --
- -- ### ACT_ACCOUNT States
- --
- -- * **Asigned**: The player is assigned.
- -- * **Waiting**: Waiting for an event.
- -- * **Report**: Reporting.
- -- * **Account**: Account for an event.
- -- * **Accounted**: All events have been accounted for, end of the process.
- -- * **Failed**: Failed the process.
- --
- -- ### ACT_ACCOUNT Events
- --
- -- * **Start**: Start the process.
- -- * **Wait**: Wait for an event.
- -- * **Report**: Report the status of the accounting.
- -- * **Event**: An event happened, process the event.
- -- * **More**: More targets.
- -- * **NoMore (*)**: No more targets.
- -- * **Fail (*)**: The action process has failed.
- --
- -- (*) End states of the process.
- --
- -- ### ACT_ACCOUNT state transition methods:
- --
- -- State transition functions can be set **by the mission designer** customizing or improving the behaviour of the state.
- -- There are 2 moments when state transition methods will be called by the state machine:
- --
- -- * **Before** the state transition.
- -- The state transition method needs to start with the name **OnBefore + the name of the state**.
- -- If the state transition method returns false, then the processing of the state transition will not be done!
- -- If you want to change the behaviour of the AIControllable at this event, return false,
- -- but then you'll need to specify your own logic using the AIControllable!
- --
- -- * **After** the state transition.
- -- The state transition method needs to start with the name **OnAfter + the name of the state**.
- -- These state transition methods need to provide a return value, which is specified at the function description.
- --
- -- @type ACT_ACCOUNT
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends Core.Fsm#FSM_PROCESS
- ACT_ACCOUNT = {
- ClassName = "ACT_ACCOUNT",
- TargetSetUnit = nil,
- }
-
- --- Creates a new DESTROY process.
- -- @param #ACT_ACCOUNT self
- -- @return #ACT_ACCOUNT
- function ACT_ACCOUNT:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_PROCESS:New() ) -- Core.Fsm#FSM_PROCESS
-
- self:AddTransition( "Assigned", "Start", "Waiting")
- self:AddTransition( "*", "Wait", "Waiting")
- self:AddTransition( "*", "Report", "Report")
- self:AddTransition( "*", "Event", "Account")
- self:AddTransition( "Account", "More", "Wait")
- self:AddTransition( "Account", "NoMore", "Accounted")
- self:AddTransition( "*", "Fail", "Failed")
-
- self:AddEndState( "Accounted" )
- self:AddEndState( "Failed" )
-
- self:SetStartState( "Assigned" )
-
- return self
- end
-
- --- Process Events
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT:onafterStart( ProcessUnit, From, Event, To )
-
- self:HandleEvent( EVENTS.Dead, self.onfuncEventDead )
-
- self:__Wait( 1 )
- end
-
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT:onenterWaiting( ProcessUnit, From, Event, To )
-
- if self.DisplayCount >= self.DisplayInterval then
- self:Report()
- self.DisplayCount = 1
- else
- self.DisplayCount = self.DisplayCount + 1
- end
-
- return true -- Process always the event.
- end
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT:onafterEvent( ProcessUnit, From, Event, To, Event )
-
- self:__NoMore( 1 )
- end
-
-end -- ACT_ACCOUNT
-
-do -- ACT_ACCOUNT_DEADS
-
- --- # @{#ACT_ACCOUNT_DEADS} FSM class, extends @{Fsm.Account#ACT_ACCOUNT}
- --
- -- The ACT_ACCOUNT_DEADS class accounts (detects, counts and reports) successful kills of DCS units.
- -- The process is given a @{Set} of units that will be tracked upon successful destruction.
- -- The process will end after each target has been successfully destroyed.
- -- Each successful dead will trigger an Account state transition that can be scored, modified or administered.
- --
- --
- -- ## ACT_ACCOUNT_DEADS constructor:
- --
- -- * @{#ACT_ACCOUNT_DEADS.New}(): Creates a new ACT_ACCOUNT_DEADS object.
- --
- -- @type ACT_ACCOUNT_DEADS
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends #ACT_ACCOUNT
- ACT_ACCOUNT_DEADS = {
- ClassName = "ACT_ACCOUNT_DEADS",
- TargetSetUnit = nil,
- }
-
-
- --- Creates a new DESTROY process.
- -- @param #ACT_ACCOUNT_DEADS self
- -- @param Set#SET_UNIT TargetSetUnit
- -- @param #string TaskName
- function ACT_ACCOUNT_DEADS:New( TargetSetUnit, TaskName )
- -- Inherits from BASE
- local self = BASE:Inherit( self, ACT_ACCOUNT:New() ) -- #ACT_ACCOUNT_DEADS
-
- self.TargetSetUnit = TargetSetUnit
- self.TaskName = TaskName
-
- self.DisplayInterval = 30
- self.DisplayCount = 30
- self.DisplayMessage = true
- self.DisplayTime = 10 -- 10 seconds is the default
- self.DisplayCategory = "HQ" -- Targets is the default display category
-
- return self
- end
-
- function ACT_ACCOUNT_DEADS:Init( FsmAccount )
-
- self.TargetSetUnit = FsmAccount.TargetSetUnit
- self.TaskName = FsmAccount.TaskName
- end
-
- --- Process Events
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT_DEADS self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT_DEADS:onenterReport( ProcessUnit, Task, From, Event, To )
- self:E( { ProcessUnit, From, Event, To } )
-
- self:Message( "Your group with assigned " .. self.TaskName .. " task has " .. self.TargetSetUnit:GetUnitTypesText() .. " targets left to be destroyed." )
- end
-
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT_DEADS self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT_DEADS:onenterAccount( ProcessUnit, Task, From, Event, To, EventData )
- self:T( { ProcessUnit, EventData, From, Event, To } )
-
- self:T({self.Controllable})
-
- self.TargetSetUnit:Flush()
-
- self:T( { "Before sending Message", EventData.IniUnitName, self.TargetSetUnit:FindUnit( EventData.IniUnitName ) } )
- if self.TargetSetUnit:FindUnit( EventData.IniUnitName ) then
- self:T( "Sending Message" )
- local TaskGroup = ProcessUnit:GetGroup()
- self.TargetSetUnit:Remove( EventData.IniUnitName )
- self:Message( "You hit a target. Your group with assigned " .. self.TaskName .. " task has " .. self.TargetSetUnit:Count() .. " targets ( " .. self.TargetSetUnit:GetUnitTypesText() .. " ) left to be destroyed." )
- end
- self:T( { "After sending Message" } )
- end
-
- --- StateMachine callback function
- -- @param #ACT_ACCOUNT_DEADS self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ACCOUNT_DEADS:onafterEvent( ProcessUnit, Task, From, Event, To )
-
- if self.TargetSetUnit:Count() > 0 then
- self:__More( 1 )
- else
- self:__NoMore( 1 )
- end
- end
-
- --- DCS Events
-
- --- @param #ACT_ACCOUNT_DEADS self
- -- @param Event#EVENTDATA EventData
- function ACT_ACCOUNT_DEADS:onfuncEventDead( EventData )
- self:T( { "EventDead", EventData } )
-
- if EventData.IniDCSUnit then
- self:Event( EventData )
- end
- end
-
-end -- ACT_ACCOUNT DEADS
---- (SP) (MP) (FSM) Route AI or players through waypoints or to zones.
---
--- ===
---
--- # @{#ACT_ASSIST} FSM class, extends @{Fsm#FSM_PROCESS}
---
--- ## ACT_ASSIST state machine:
---
--- This class is a state machine: it manages a process that is triggered by events causing state transitions to occur.
--- All derived classes from this class will start with the class name, followed by a \_. See the relevant derived class descriptions below.
--- Each derived class follows exactly the same process, using the same events and following the same state transitions,
--- but will have **different implementation behaviour** upon each event or state transition.
---
--- ### ACT_ASSIST **Events**:
---
--- These are the events defined in this class:
---
--- * **Start**: The process is started.
--- * **Next**: The process is smoking the targets in the given zone.
---
--- ### ACT_ASSIST **Event methods**:
---
--- Event methods are available (dynamically allocated by the state machine), that accomodate for state transitions occurring in the process.
--- There are two types of event methods, which you can use to influence the normal mechanisms in the state machine:
---
--- * **Immediate**: The event method has exactly the name of the event.
--- * **Delayed**: The event method starts with a __ + the name of the event. The first parameter of the event method is a number value, expressing the delay in seconds when the event will be executed.
---
--- ### ACT_ASSIST **States**:
---
--- * **None**: The controllable did not receive route commands.
--- * **AwaitSmoke (*)**: The process is awaiting to smoke the targets in the zone.
--- * **Smoking (*)**: The process is smoking the targets in the zone.
--- * **Failed (*)**: The process has failed.
---
--- (*) End states of the process.
---
--- ### ACT_ASSIST state transition methods:
---
--- State transition functions can be set **by the mission designer** customizing or improving the behaviour of the state.
--- There are 2 moments when state transition methods will be called by the state machine:
---
--- * **Before** the state transition.
--- The state transition method needs to start with the name **OnBefore + the name of the state**.
--- If the state transition method returns false, then the processing of the state transition will not be done!
--- If you want to change the behaviour of the AIControllable at this event, return false,
--- but then you'll need to specify your own logic using the AIControllable!
---
--- * **After** the state transition.
--- The state transition method needs to start with the name **OnAfter + the name of the state**.
--- These state transition methods need to provide a return value, which is specified at the function description.
---
--- ===
---
--- # 1) @{#ACT_ASSIST_SMOKE_TARGETS_ZONE} class, extends @{Fsm.Route#ACT_ASSIST}
---
--- The ACT_ASSIST_SMOKE_TARGETS_ZONE class implements the core functions to smoke targets in a @{Zone}.
--- The targets are smoked within a certain range around each target, simulating a realistic smoking behaviour.
--- At random intervals, a new target is smoked.
---
--- # 1.1) ACT_ASSIST_SMOKE_TARGETS_ZONE constructor:
---
--- * @{#ACT_ASSIST_SMOKE_TARGETS_ZONE.New}(): Creates a new ACT_ASSIST_SMOKE_TARGETS_ZONE object.
---
--- ===
---
--- @module Smoke
-
-do -- ACT_ASSIST
-
- --- ACT_ASSIST class
- -- @type ACT_ASSIST
- -- @extends Core.Fsm#FSM_PROCESS
- ACT_ASSIST = {
- ClassName = "ACT_ASSIST",
- }
-
- --- Creates a new target smoking state machine. The process will request from the menu if it accepts the task, if not, the unit is removed from the simulator.
- -- @param #ACT_ASSIST self
- -- @return #ACT_ASSIST
- function ACT_ASSIST:New()
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM_PROCESS:New( "ACT_ASSIST" ) ) -- Core.Fsm#FSM_PROCESS
-
- self:AddTransition( "None", "Start", "AwaitSmoke" )
- self:AddTransition( "AwaitSmoke", "Next", "Smoking" )
- self:AddTransition( "Smoking", "Next", "AwaitSmoke" )
- self:AddTransition( "*", "Stop", "Success" )
- self:AddTransition( "*", "Fail", "Failed" )
-
- self:AddEndState( "Failed" )
- self:AddEndState( "Success" )
-
- self:SetStartState( "None" )
-
- return self
- end
-
- --- Task Events
-
- --- StateMachine callback function
- -- @param #ACT_ASSIST self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIST:onafterStart( ProcessUnit, From, Event, To )
-
- local ProcessGroup = ProcessUnit:GetGroup()
- local MissionMenu = self:GetMission():GetMenu( ProcessGroup )
-
- local function MenuSmoke( MenuParam )
- self:E( MenuParam )
- local self = MenuParam.self
- local SmokeColor = MenuParam.SmokeColor
- self.SmokeColor = SmokeColor
- self:__Next( 1 )
- end
-
- self.Menu = MENU_GROUP:New( ProcessGroup, "Target acquisition", MissionMenu )
- self.MenuSmokeBlue = MENU_GROUP_COMMAND:New( ProcessGroup, "Drop blue smoke on targets", self.Menu, MenuSmoke, { self = self, SmokeColor = SMOKECOLOR.Blue } )
- self.MenuSmokeGreen = MENU_GROUP_COMMAND:New( ProcessGroup, "Drop green smoke on targets", self.Menu, MenuSmoke, { self = self, SmokeColor = SMOKECOLOR.Green } )
- self.MenuSmokeOrange = MENU_GROUP_COMMAND:New( ProcessGroup, "Drop Orange smoke on targets", self.Menu, MenuSmoke, { self = self, SmokeColor = SMOKECOLOR.Orange } )
- self.MenuSmokeRed = MENU_GROUP_COMMAND:New( ProcessGroup, "Drop Red smoke on targets", self.Menu, MenuSmoke, { self = self, SmokeColor = SMOKECOLOR.Red } )
- self.MenuSmokeWhite = MENU_GROUP_COMMAND:New( ProcessGroup, "Drop White smoke on targets", self.Menu, MenuSmoke, { self = self, SmokeColor = SMOKECOLOR.White } )
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIST self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIST:onafterStop( ProcessUnit, From, Event, To )
-
- self.Menu:Remove() -- When stopped, remove the menus
- end
-
-end
-
-do -- ACT_ASSIST_SMOKE_TARGETS_ZONE
-
- --- ACT_ASSIST_SMOKE_TARGETS_ZONE class
- -- @type ACT_ASSIST_SMOKE_TARGETS_ZONE
- -- @field Set#SET_UNIT TargetSetUnit
- -- @field Core.Zone#ZONE_BASE TargetZone
- -- @extends #ACT_ASSIST
- ACT_ASSIST_SMOKE_TARGETS_ZONE = {
- ClassName = "ACT_ASSIST_SMOKE_TARGETS_ZONE",
- }
-
--- function ACT_ASSIST_SMOKE_TARGETS_ZONE:_Destructor()
--- self:E("_Destructor")
---
--- self.Menu:Remove()
--- self:EventRemoveAll()
--- end
-
- --- Creates a new target smoking state machine. The process will request from the menu if it accepts the task, if not, the unit is removed from the simulator.
- -- @param #ACT_ASSIST_SMOKE_TARGETS_ZONE self
- -- @param Set#SET_UNIT TargetSetUnit
- -- @param Core.Zone#ZONE_BASE TargetZone
- function ACT_ASSIST_SMOKE_TARGETS_ZONE:New( TargetSetUnit, TargetZone )
- local self = BASE:Inherit( self, ACT_ASSIST:New() ) -- #ACT_ASSIST
-
- self.TargetSetUnit = TargetSetUnit
- self.TargetZone = TargetZone
-
- return self
- end
-
- function ACT_ASSIST_SMOKE_TARGETS_ZONE:Init( FsmSmoke )
-
- self.TargetSetUnit = FsmSmoke.TargetSetUnit
- self.TargetZone = FsmSmoke.TargetZone
- end
-
- --- Creates a new target smoking state machine. The process will request from the menu if it accepts the task, if not, the unit is removed from the simulator.
- -- @param #ACT_ASSIST_SMOKE_TARGETS_ZONE self
- -- @param Set#SET_UNIT TargetSetUnit
- -- @param Core.Zone#ZONE_BASE TargetZone
- -- @return #ACT_ASSIST_SMOKE_TARGETS_ZONE self
- function ACT_ASSIST_SMOKE_TARGETS_ZONE:Init( TargetSetUnit, TargetZone )
-
- self.TargetSetUnit = TargetSetUnit
- self.TargetZone = TargetZone
-
- return self
- end
-
- --- StateMachine callback function
- -- @param #ACT_ASSIST_SMOKE_TARGETS_ZONE self
- -- @param Wrapper.Controllable#CONTROLLABLE ProcessUnit
- -- @param #string Event
- -- @param #string From
- -- @param #string To
- function ACT_ASSIST_SMOKE_TARGETS_ZONE:onenterSmoking( ProcessUnit, From, Event, To )
-
- self.TargetSetUnit:ForEachUnit(
- --- @param Wrapper.Unit#UNIT SmokeUnit
- function( SmokeUnit )
- if math.random( 1, ( 100 * self.TargetSetUnit:Count() ) / 4 ) <= 100 then
- SCHEDULER:New( self,
- function()
- if SmokeUnit:IsAlive() then
- SmokeUnit:Smoke( self.SmokeColor, 150 )
- end
- end, {}, math.random( 10, 60 )
- )
- end
- end
- )
-
- end
-
-end--- A COMMANDCENTER is the owner of multiple missions within MOOSE.
--- A COMMANDCENTER governs multiple missions, the tasking and the reporting.
--- @module CommandCenter
-
-
-
---- The REPORT class
--- @type REPORT
--- @extends Core.Base#BASE
-REPORT = {
- ClassName = "REPORT",
-}
-
---- Create a new REPORT.
--- @param #REPORT self
--- @param #string Title
--- @return #REPORT
-function REPORT:New( Title )
-
- local self = BASE:Inherit( self, BASE:New() )
-
- self.Report = {}
- if Title then
- self.Report[#self.Report+1] = Title
- end
-
- return self
-end
-
---- Add a new line to a REPORT.
--- @param #REPORT self
--- @param #string Text
--- @return #REPORT
-function REPORT:Add( Text )
- self.Report[#self.Report+1] = Text
- return self.Report[#self.Report]
-end
-
---- Produces the text of the report, taking into account an optional delimeter, which is \n by default.
--- @param #REPORT self
--- @param #string Delimiter (optional) A delimiter text.
--- @return #string The report text.
-function REPORT:Text( Delimiter )
- Delimiter = Delimiter or "\n"
- local ReportText = table.concat( self.Report, Delimiter ) or ""
- return ReportText
-end
-
---- The COMMANDCENTER class
--- @type COMMANDCENTER
--- @field Wrapper.Group#GROUP HQ
--- @field Dcs.DCSCoalitionWrapper.Object#coalition CommandCenterCoalition
--- @list Missions
--- @extends Core.Base#BASE
-COMMANDCENTER = {
- ClassName = "COMMANDCENTER",
- CommandCenterName = "",
- CommandCenterCoalition = nil,
- CommandCenterPositionable = nil,
- Name = "",
-}
---- The constructor takes an IDENTIFIABLE as the HQ command center.
--- @param #COMMANDCENTER self
--- @param Wrapper.Positionable#POSITIONABLE CommandCenterPositionable
--- @param #string CommandCenterName
--- @return #COMMANDCENTER
-function COMMANDCENTER:New( CommandCenterPositionable, CommandCenterName )
-
- local self = BASE:Inherit( self, BASE:New() )
-
- self.CommandCenterPositionable = CommandCenterPositionable
- self.CommandCenterName = CommandCenterName or CommandCenterPositionable:GetName()
- self.CommandCenterCoalition = CommandCenterPositionable:GetCoalition()
-
- self.Missions = {}
-
- self:HandleEvent( EVENTS.Birth,
- --- @param #COMMANDCENTER self
- -- @param Core.Event#EVENTDATA EventData
- function( self, EventData )
- if EventData.IniObjectCategory == 1 then
- local EventGroup = GROUP:Find( EventData.IniDCSGroup )
- if EventGroup and self:HasGroup( EventGroup ) then
- local MenuReporting = MENU_GROUP:New( EventGroup, "Reporting", self.CommandCenterMenu )
- local MenuMissionsSummary = MENU_GROUP_COMMAND:New( EventGroup, "Missions Summary Report", MenuReporting, self.ReportSummary, self, EventGroup )
- local MenuMissionsDetails = MENU_GROUP_COMMAND:New( EventGroup, "Missions Details Report", MenuReporting, self.ReportDetails, self, EventGroup )
- self:ReportSummary( EventGroup )
- end
- local PlayerUnit = EventData.IniUnit
- for MissionID, Mission in pairs( self:GetMissions() ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- local PlayerGroup = EventData.IniGroup -- The GROUP object should be filled!
- Mission:JoinUnit( PlayerUnit, PlayerGroup )
- Mission:ReportDetails()
- end
- end
-
- end
- )
-
- -- When a player enters a client or a unit, the CommandCenter will check for each Mission and each Task in the Mission if the player has things to do.
- -- For these elements, it will=
- -- - Set the correct menu.
- -- - Assign the PlayerUnit to the Task if required.
- -- - Send a message to the other players in the group that this player has joined.
- self:HandleEvent( EVENTS.PlayerEnterUnit,
- --- @param #COMMANDCENTER self
- -- @param Core.Event#EVENTDATA EventData
- function( self, EventData )
- local PlayerUnit = EventData.IniUnit
- for MissionID, Mission in pairs( self:GetMissions() ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- local PlayerGroup = EventData.IniGroup -- The GROUP object should be filled!
- Mission:JoinUnit( PlayerUnit, PlayerGroup )
- Mission:ReportDetails()
- end
- end
- )
-
- -- Handle when a player leaves a slot and goes back to spectators ...
- -- The PlayerUnit will be UnAssigned from the Task.
- -- When there is no Unit left running the Task, the Task goes into Abort...
- self:HandleEvent( EVENTS.PlayerLeaveUnit,
- --- @param #TASK self
- -- @param Core.Event#EVENTDATA EventData
- function( self, EventData )
- local PlayerUnit = EventData.IniUnit
- for MissionID, Mission in pairs( self:GetMissions() ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- Mission:AbortUnit( PlayerUnit )
- end
- end
- )
-
- -- Handle when a player leaves a slot and goes back to spectators ...
- -- The PlayerUnit will be UnAssigned from the Task.
- -- When there is no Unit left running the Task, the Task goes into Abort...
- self:HandleEvent( EVENTS.Crash,
- --- @param #TASK self
- -- @param Core.Event#EVENTDATA EventData
- function( self, EventData )
- local PlayerUnit = EventData.IniUnit
- for MissionID, Mission in pairs( self:GetMissions() ) do
- Mission:CrashUnit( PlayerUnit )
- end
- end
- )
-
- return self
-end
-
---- Gets the name of the HQ command center.
--- @param #COMMANDCENTER self
--- @return #string
-function COMMANDCENTER:GetName()
-
- return self.CommandCenterName
-end
-
---- Gets the POSITIONABLE of the HQ command center.
--- @param #COMMANDCENTER self
--- @return Wrapper.Positionable#POSITIONABLE
-function COMMANDCENTER:GetPositionable()
- return self.CommandCenterPositionable
-end
-
---- Get the Missions governed by the HQ command center.
--- @param #COMMANDCENTER self
--- @return #list
-function COMMANDCENTER:GetMissions()
-
- return self.Missions
-end
-
---- Add a MISSION to be governed by the HQ command center.
--- @param #COMMANDCENTER self
--- @param Tasking.Mission#MISSION Mission
--- @return Tasking.Mission#MISSION
-function COMMANDCENTER:AddMission( Mission )
-
- self.Missions[Mission] = Mission
-
- return Mission
-end
-
---- Removes a MISSION to be governed by the HQ command center.
--- The given Mission is not nilified.
--- @param #COMMANDCENTER self
--- @param Tasking.Mission#MISSION Mission
--- @return Tasking.Mission#MISSION
-function COMMANDCENTER:RemoveMission( Mission )
-
- self.Missions[Mission] = nil
-
- return Mission
-end
-
---- Sets the menu structure of the Missions governed by the HQ command center.
--- @param #COMMANDCENTER self
-function COMMANDCENTER:SetMenu()
- self:F()
-
- self.CommandCenterMenu = self.CommandCenterMenu or MENU_COALITION:New( self.CommandCenterCoalition, "Command Center (" .. self:GetName() .. ")" )
-
- local MenuTime = timer.getTime()
- for MissionID, Mission in pairs( self:GetMissions() ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- Mission:SetMenu( MenuTime )
- end
-
- for MissionID, Mission in pairs( self:GetMissions() ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- Mission:RemoveMenu( MenuTime )
- end
-
-end
-
---- Gets the commandcenter menu structure governed by the HQ command center.
--- @param #COMMANDCENTER self
--- @return Core.Menu#MENU_COALITION
-function COMMANDCENTER:GetMenu()
- self:F()
- return self.CommandCenterMenu
-end
-
---- Checks of the COMMANDCENTER has a GROUP.
--- @param #COMMANDCENTER self
--- @param Wrapper.Group#GROUP
--- @return #boolean
-function COMMANDCENTER:HasGroup( MissionGroup )
-
- local Has = false
-
- for MissionID, Mission in pairs( self.Missions ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- if Mission:HasGroup( MissionGroup ) then
- Has = true
- break
- end
- end
-
- return Has
-end
-
---- Send a CC message to the coalition of the CC.
--- @param #COMMANDCENTER self
-function COMMANDCENTER:MessageToAll( Message )
-
- self:GetPositionable():MessageToAll( Message, 20, self:GetName() )
-
-end
-
---- Send a CC message to a GROUP.
--- @param #COMMANDCENTER self
--- @param #string Message
--- @param Wrapper.Group#GROUP TaskGroup
--- @param #sring Name (optional) The name of the Group used as a prefix for the message to the Group. If not provided, there will be nothing shown.
-function COMMANDCENTER:MessageToGroup( Message, TaskGroup, Name )
-
- local Prefix = "@ Group"
- Prefix = Prefix .. ( Name and " (" .. Name .. "): " or '' )
- Message = Prefix .. Message
- self:GetPositionable():MessageToGroup( Message , 20, TaskGroup, self:GetName() )
-
-end
-
---- Send a CC message to the coalition of the CC.
--- @param #COMMANDCENTER self
-function COMMANDCENTER:MessageToCoalition( Message )
-
- local CCCoalition = self:GetPositionable():GetCoalition()
- --TODO: Fix coalition bug!
- self:GetPositionable():MessageToCoalition( Message, 20, CCCoalition, self:GetName() )
-
-end
-
-
---- Report the status of all MISSIONs to a GROUP.
--- Each Mission is listed, with an indication how many Tasks are still to be completed.
--- @param #COMMANDCENTER self
-function COMMANDCENTER:ReportSummary( ReportGroup )
- self:E( ReportGroup )
-
- local Report = REPORT:New()
-
- for MissionID, Mission in pairs( self.Missions ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- Report:Add( " - " .. Mission:ReportOverview() )
- end
-
- self:GetPositionable():MessageToGroup( Report:Text(), 30, ReportGroup )
-
-end
-
---- Report the status of a Task to a Group.
--- Report the details of a Mission, listing the Mission, and all the Task details.
--- @param #COMMANDCENTER self
-function COMMANDCENTER:ReportDetails( ReportGroup, Task )
- self:E( ReportGroup )
-
- local Report = REPORT:New()
-
- for MissionID, Mission in pairs( self.Missions ) do
- local Mission = Mission -- Tasking.Mission#MISSION
- Report:Add( " - " .. Mission:ReportDetails() )
- end
-
- self:GetPositionable():MessageToGroup( Report:Text(), 30, ReportGroup )
-end
-
---- A MISSION is the main owner of a Mission orchestration within MOOSE . The Mission framework orchestrates @{CLIENT}s, @{TASK}s, @{STAGE}s etc.
--- A @{CLIENT} needs to be registered within the @{MISSION} through the function @{AddClient}. A @{TASK} needs to be registered within the @{MISSION} through the function @{AddTask}.
--- @module Mission
-
---- The MISSION class
--- @type MISSION
--- @field #MISSION.Clients _Clients
--- @field Core.Menu#MENU_COALITION MissionMenu
--- @field #string MissionBriefing
--- @extends Core.Fsm#FSM
-MISSION = {
- ClassName = "MISSION",
- Name = "",
- MissionStatus = "PENDING",
-}
-
---- This is the main MISSION declaration method. Each Mission is like the master or a Mission orchestration between, Clients, Tasks, Stages etc.
--- @param #MISSION self
--- @param Tasking.CommandCenter#COMMANDCENTER CommandCenter
--- @param #string MissionName is the name of the mission. This name will be used to reference the status of each mission by the players.
--- @param #string MissionPriority is a string indicating the "priority" of the Mission. f.e. "Primary", "Secondary" or "First", "Second". It is free format and up to the Mission designer to choose. There are no rules behind this field.
--- @param #string MissionBriefing is a string indicating the mission briefing to be shown when a player joins a @{CLIENT}.
--- @param Dcs.DCSCoalitionWrapper.Object#coalition MissionCoalition is a string indicating the coalition or party to which this mission belongs to. It is free format and can be chosen freely by the mission designer. Note that this field is not to be confused with the coalition concept of the ME. Examples of a Mission Coalition could be "NATO", "CCCP", "Intruders", "Terrorists"...
--- @return #MISSION self
-function MISSION:New( CommandCenter, MissionName, MissionPriority, MissionBriefing, MissionCoalition )
-
- local self = BASE:Inherit( self, FSM:New() ) -- Core.Fsm#FSM
-
- self:SetStartState( "Idle" )
-
- self:AddTransition( "Idle", "Start", "Ongoing" )
-
- --- OnLeave Transition Handler for State Idle.
- -- @function [parent=#MISSION] OnLeaveIdle
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Idle.
- -- @function [parent=#MISSION] OnEnterIdle
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnLeave Transition Handler for State Ongoing.
- -- @function [parent=#MISSION] OnLeaveOngoing
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Ongoing.
- -- @function [parent=#MISSION] OnEnterOngoing
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnBefore Transition Handler for Event Start.
- -- @function [parent=#MISSION] OnBeforeStart
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Start.
- -- @function [parent=#MISSION] OnAfterStart
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Start.
- -- @function [parent=#MISSION] Start
- -- @param #MISSION self
-
- --- Asynchronous Event Trigger for Event Start.
- -- @function [parent=#MISSION] __Start
- -- @param #MISSION self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "Ongoing", "Stop", "Idle" )
-
- --- OnLeave Transition Handler for State Idle.
- -- @function [parent=#MISSION] OnLeaveIdle
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Idle.
- -- @function [parent=#MISSION] OnEnterIdle
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnBefore Transition Handler for Event Stop.
- -- @function [parent=#MISSION] OnBeforeStop
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Stop.
- -- @function [parent=#MISSION] OnAfterStop
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Stop.
- -- @function [parent=#MISSION] Stop
- -- @param #MISSION self
-
- --- Asynchronous Event Trigger for Event Stop.
- -- @function [parent=#MISSION] __Stop
- -- @param #MISSION self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "Ongoing", "Complete", "Completed" )
-
- --- OnLeave Transition Handler for State Completed.
- -- @function [parent=#MISSION] OnLeaveCompleted
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Completed.
- -- @function [parent=#MISSION] OnEnterCompleted
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnBefore Transition Handler for Event Complete.
- -- @function [parent=#MISSION] OnBeforeComplete
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Complete.
- -- @function [parent=#MISSION] OnAfterComplete
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Complete.
- -- @function [parent=#MISSION] Complete
- -- @param #MISSION self
-
- --- Asynchronous Event Trigger for Event Complete.
- -- @function [parent=#MISSION] __Complete
- -- @param #MISSION self
- -- @param #number Delay The delay in seconds.
-
- self:AddTransition( "*", "Fail", "Failed" )
-
- --- OnLeave Transition Handler for State Failed.
- -- @function [parent=#MISSION] OnLeaveFailed
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnEnter Transition Handler for State Failed.
- -- @function [parent=#MISSION] OnEnterFailed
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- OnBefore Transition Handler for Event Fail.
- -- @function [parent=#MISSION] OnBeforeFail
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @return #boolean Return false to cancel Transition.
-
- --- OnAfter Transition Handler for Event Fail.
- -- @function [parent=#MISSION] OnAfterFail
- -- @param #MISSION self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
-
- --- Synchronous Event Trigger for Event Fail.
- -- @function [parent=#MISSION] Fail
- -- @param #MISSION self
-
- --- Asynchronous Event Trigger for Event Fail.
- -- @function [parent=#MISSION] __Fail
- -- @param #MISSION self
- -- @param #number Delay The delay in seconds.
-
- self:T( { MissionName, MissionPriority, MissionBriefing, MissionCoalition } )
-
- self.CommandCenter = CommandCenter
- CommandCenter:AddMission( self )
-
- self.Name = MissionName
- self.MissionPriority = MissionPriority
- self.MissionBriefing = MissionBriefing
- self.MissionCoalition = MissionCoalition
-
- self.Tasks = {}
-
- -- Private implementations
-
-
-
- return self
-end
-
--- FSM function for a MISSION
--- @param #MISSION self
--- @param #string From
--- @param #string Event
--- @param #string To
-function MISSION:onbeforeComplete( From, Event, To )
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- if not Task:IsStateSuccess() and not Task:IsStateFailed() and not Task:IsStateAborted() and not Task:IsStateCancelled() then
- return false -- Mission cannot be completed. Other Tasks are still active.
- end
- end
- return true -- Allow Mission completion.
-end
-
--- FSM function for a MISSION
--- @param #MISSION self
--- @param #string From
--- @param #string Event
--- @param #string To
-function MISSION:onenterCompleted( From, Event, To )
-
- self:GetCommandCenter():MessageToCoalition( "Mission " .. self:GetName() .. " has been completed! Good job guys!" )
-end
-
---- Gets the mission name.
--- @param #MISSION self
--- @return #MISSION self
-function MISSION:GetName()
- return self.Name
-end
-
---- Add a Unit to join the Mission.
--- For each Task within the Mission, the Unit is joined with the Task.
--- If the Unit was not part of a Task in the Mission, false is returned.
--- If the Unit is part of a Task in the Mission, true is returned.
--- @param #MISSION self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player joining the Mission.
--- @param Wrapper.Group#GROUP PlayerGroup The GROUP of the player joining the Mission.
--- @return #boolean true if Unit is part of a Task in the Mission.
-function MISSION:JoinUnit( PlayerUnit, PlayerGroup )
- self:F( { PlayerUnit = PlayerUnit, PlayerGroup = PlayerGroup } )
-
- local PlayerUnitAdded = false
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- if Task:JoinUnit( PlayerUnit, PlayerGroup ) then
- PlayerUnitAdded = true
- end
- end
-
- return PlayerUnitAdded
-end
-
---- Aborts a PlayerUnit from the Mission.
--- For each Task within the Mission, the PlayerUnit is removed from Task where it is assigned.
--- If the Unit was not part of a Task in the Mission, false is returned.
--- If the Unit is part of a Task in the Mission, true is returned.
--- @param #MISSION self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player joining the Mission.
--- @return #boolean true if Unit is part of a Task in the Mission.
-function MISSION:AbortUnit( PlayerUnit )
- self:F( { PlayerUnit = PlayerUnit } )
-
- local PlayerUnitRemoved = false
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- if Task:AbortUnit( PlayerUnit ) then
- PlayerUnitRemoved = true
- end
- end
-
- return PlayerUnitRemoved
-end
-
---- Handles a crash of a PlayerUnit from the Mission.
--- For each Task within the Mission, the PlayerUnit is removed from Task where it is assigned.
--- If the Unit was not part of a Task in the Mission, false is returned.
--- If the Unit is part of a Task in the Mission, true is returned.
--- @param #MISSION self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player crashing.
--- @return #boolean true if Unit is part of a Task in the Mission.
-function MISSION:CrashUnit( PlayerUnit )
- self:F( { PlayerUnit = PlayerUnit } )
-
- local PlayerUnitRemoved = false
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- if Task:CrashUnit( PlayerUnit ) then
- PlayerUnitRemoved = true
- end
- end
-
- return PlayerUnitRemoved
-end
-
---- Add a scoring to the mission.
--- @param #MISSION self
--- @return #MISSION self
-function MISSION:AddScoring( Scoring )
- self.Scoring = Scoring
- return self
-end
-
---- Get the scoring object of a mission.
--- @param #MISSION self
--- @return #SCORING Scoring
-function MISSION:GetScoring()
- return self.Scoring
-end
-
---- Get the groups for which TASKS are given in the mission
--- @param #MISSION self
--- @return Core.Set#SET_GROUP
-function MISSION:GetGroups()
-
- local SetGroup = SET_GROUP:New()
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- local GroupSet = Task:GetGroups()
- GroupSet:ForEachGroup(
- function( TaskGroup )
- SetGroup:Add( TaskGroup, TaskGroup )
- end
- )
- end
-
- return SetGroup
-
-end
-
-
---- Sets the Planned Task menu.
--- @param #MISSION self
--- @param #number MenuTime
-function MISSION:SetMenu( MenuTime )
- self:F()
-
- for _, TaskData in pairs( self:GetTasks() ) do
- local Task = TaskData -- Tasking.Task#TASK
- Task:SetMenu( MenuTime )
- end
-end
-
---- Removes the Planned Task menu.
--- @param #MISSION self
--- @param #number MenuTime
-function MISSION:RemoveMenu( MenuTime )
- self:F()
-
- for _, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- Task:RemoveMenu( MenuTime )
- end
-end
-
-
---- Gets the COMMANDCENTER.
--- @param #MISSION self
--- @return Tasking.CommandCenter#COMMANDCENTER
-function MISSION:GetCommandCenter()
- return self.CommandCenter
-end
-
-
---- Removes a Task menu.
--- @param #MISSION self
--- @param Tasking.Task#TASK Task
--- @return #MISSION self
-function MISSION:RemoveTaskMenu( Task )
-
- Task:RemoveMenu()
-end
-
-
---- Gets the mission menu for the coalition.
--- @param #MISSION self
--- @param Wrapper.Group#GROUP TaskGroup
--- @return Core.Menu#MENU_COALITION self
-function MISSION:GetMenu( TaskGroup )
-
- local CommandCenter = self:GetCommandCenter()
- local CommandCenterMenu = CommandCenter:GetMenu()
-
- local MissionName = self:GetName()
- local MissionMenu = CommandCenterMenu:GetMenu( MissionName )
-
- return MissionMenu
-end
-
-
---- Get the TASK identified by the TaskNumber from the Mission. This function is useful in GoalFunctions.
--- @param #string TaskName The Name of the @{Task} within the @{Mission}.
--- @return Tasking.Task#TASK The Task
--- @return #nil Returns nil if no task was found.
-function MISSION:GetTask( TaskName )
- self:F( { TaskName } )
-
- return self.Tasks[TaskName]
-end
-
-
---- Register a @{Task} to be completed within the @{Mission}.
--- Note that there can be multiple @{Task}s registered to be completed.
--- Each Task can be set a certain Goals. The Mission will not be completed until all Goals are reached.
--- @param #MISSION self
--- @param Tasking.Task#TASK Task is the @{Task} object.
--- @return Tasking.Task#TASK The task added.
-function MISSION:AddTask( Task )
-
- local TaskName = Task:GetTaskName()
- self:F( TaskName )
-
- self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
-
- self.Tasks[TaskName] = Task
-
- self:GetCommandCenter():SetMenu()
-
- return Task
-end
-
---- Removes a @{Task} to be completed within the @{Mission}.
--- Note that there can be multiple @{Task}s registered to be completed.
--- Each Task can be set a certain Goals. The Mission will not be completed until all Goals are reached.
--- @param #MISSION self
--- @param Tasking.Task#TASK Task is the @{Task} object.
--- @return #nil The cleaned Task reference.
-function MISSION:RemoveTask( Task )
-
- local TaskName = Task:GetTaskName()
-
- self:F( TaskName )
- self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
-
- -- Ensure everything gets garbarge collected.
- self.Tasks[TaskName] = nil
- Task = nil
-
- collectgarbage()
-
- self:GetCommandCenter():SetMenu()
-
- return nil
-end
-
---- Return the next @{Task} ID to be completed within the @{Mission}.
--- @param #MISSION self
--- @param Tasking.Task#TASK Task is the @{Task} object.
--- @return Tasking.Task#TASK The task added.
-function MISSION:GetNextTaskID( Task )
-
- local TaskName = Task:GetTaskName()
- self:F( TaskName )
- self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
-
- self.Tasks[TaskName].n = self.Tasks[TaskName].n + 1
-
- return self.Tasks[TaskName].n
-end
-
---- Is the @{Mission} **Completed**.
--- @param #MISSION self
--- @return #boolean
-function MISSION:IsCompleted()
- return self:Is( "Completed" )
-end
-
---- Is the @{Mission} **Idle**.
--- @param #MISSION self
--- @return #boolean
-function MISSION:IsIdle()
- return self:Is( "Idle" )
-end
-
---- Is the @{Mission} **Ongoing**.
--- @param #MISSION self
--- @return #boolean
-function MISSION:IsOngoing()
- return self:Is( "Ongoing" )
-end
-
---- Is the @{Mission} **Failed**.
--- @param #MISSION self
--- @return #boolean
-function MISSION:IsFailed()
- return self:Is( "Failed" )
-end
-
---- Is the @{Mission} **Hold**.
--- @param #MISSION self
--- @return #boolean
-function MISSION:IsHold()
- return self:Is( "Hold" )
-end
-
---- Validates if the Mission has a Group
--- @param #MISSION
--- @return #boolean true if the Mission has a Group.
-function MISSION:HasGroup( TaskGroup )
- local Has = false
-
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- if Task:HasGroup( TaskGroup ) then
- Has = true
- break
- end
- end
-
- return Has
-end
-
---- Create a summary report of the Mission (one line).
--- @param #MISSION self
--- @return #string
-function MISSION:ReportSummary()
-
- local Report = REPORT:New()
-
- -- List the name of the mission.
- local Name = self:GetName()
-
- -- Determine the status of the mission.
- local Status = self:GetState()
-
- -- Determine how many tasks are remaining.
- local TasksRemaining = 0
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- if Task:IsStateSuccess() or Task:IsStateFailed() then
- else
- TasksRemaining = TasksRemaining + 1
- end
- end
-
- Report:Add( "Mission " .. Name .. " - " .. Status .. " - " .. TasksRemaining .. " tasks remaining." )
-
- return Report:Text()
-end
-
---- Create a overview report of the Mission (multiple lines).
--- @param #MISSION self
--- @return #string
-function MISSION:ReportOverview()
-
- local Report = REPORT:New()
-
- -- List the name of the mission.
- local Name = self:GetName()
-
- -- Determine the status of the mission.
- local Status = self:GetState()
-
- Report:Add( "Mission " .. Name .. " - State '" .. Status .. "'" )
-
- -- Determine how many tasks are remaining.
- local TasksRemaining = 0
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- Report:Add( "- " .. Task:ReportSummary() )
- end
-
- return Report:Text()
-end
-
---- Create a detailed report of the Mission, listing all the details of the Task.
--- @param #MISSION self
--- @return #string
-function MISSION:ReportDetails()
-
- local Report = REPORT:New()
-
- -- List the name of the mission.
- local Name = self:GetName()
-
- -- Determine the status of the mission.
- local Status = self:GetState()
-
- Report:Add( "Mission " .. Name .. " - State '" .. Status .. "'" )
-
- -- Determine how many tasks are remaining.
- local TasksRemaining = 0
- for TaskID, Task in pairs( self:GetTasks() ) do
- local Task = Task -- Tasking.Task#TASK
- Report:Add( Task:ReportDetails() )
- end
-
- return Report:Text()
-end
-
---- Get all the TASKs from the Mission. This function is useful in GoalFunctions.
--- @return {TASK,...} Structure of TASKS with the @{TASK} number as the key.
--- @usage
--- -- Get Tasks from the Mission.
--- Tasks = Mission:GetTasks()
--- env.info( "Task 2 Completion = " .. Tasks[2]:GetGoalPercentage() .. "%" )
-function MISSION:GetTasks()
- self:F()
-
- return self.Tasks
-end
-
-
---- **Tasking** -- This module contains the TASK class.
---
--- ===
---
---
--- ===
---
--- ### Authors: FlightControl - Design and Programming
---
--- @module Task
-
---- @type TASK
--- @field Core.Scheduler#SCHEDULER TaskScheduler
--- @field Tasking.Mission#MISSION Mission
--- @field Core.Set#SET_GROUP SetGroup The Set of Groups assigned to the Task
--- @field Core.Fsm#FSM_PROCESS FsmTemplate
--- @field Tasking.Mission#MISSION Mission
--- @field Tasking.CommandCenter#COMMANDCENTER CommandCenter
--- @extends Core.Fsm#FSM_TASK
-
----
--- # TASK class, extends @{Base#BASE}
---
--- ## The TASK class implements the methods for task orchestration within MOOSE.
---
--- The class provides a couple of methods to:
---
--- * @{#TASK.AssignToGroup}():Assign a task to a group (of players).
--- * @{#TASK.AddProcess}():Add a @{Process} to a task.
--- * @{#TASK.RemoveProcesses}():Remove a running @{Process} from a running task.
--- * @{#TASK.SetStateMachine}():Set a @{Fsm} to a task.
--- * @{#TASK.RemoveStateMachine}():Remove @{Fsm} from a task.
--- * @{#TASK.HasStateMachine}():Enquire if the task has a @{Fsm}
--- * @{#TASK.AssignToUnit}(): Assign a task to a unit. (Needs to be implemented in the derived classes from @{#TASK}.
--- * @{#TASK.UnAssignFromUnit}(): Unassign the task from a unit.
--- * @{#TASK.SetTimeOut}(): Set timer in seconds before task gets cancelled if not assigned.
---
--- ## 1.2) Set and enquire task status (beyond the task state machine processing).
---
--- A task needs to implement as a minimum the following task states:
---
--- * **Success**: Expresses the successful execution and finalization of the task.
--- * **Failed**: Expresses the failure of a task.
--- * **Planned**: Expresses that the task is created, but not yet in execution and is not assigned yet.
--- * **Assigned**: Expresses that the task is assigned to a Group of players, and that the task is in execution mode.
---
--- A task may also implement the following task states:
---
--- * **Rejected**: Expresses that the task is rejected by a player, who was requested to accept the task.
--- * **Cancelled**: Expresses that the task is cancelled by HQ or through a logical situation where a cancellation of the task is required.
---
--- A task can implement more statusses than the ones outlined above. Please consult the documentation of the specific tasks to understand the different status modelled.
---
--- The status of tasks can be set by the methods **State** followed by the task status. An example is `StateAssigned()`.
--- The status of tasks can be enquired by the methods **IsState** followed by the task status name. An example is `if IsStateAssigned() then`.
---
--- ## 1.3) Add scoring when reaching a certain task status:
---
--- Upon reaching a certain task status in a task, additional scoring can be given. If the Mission has a scoring system attached, the scores will be added to the mission scoring.
--- Use the method @{#TASK.AddScore}() to add scores when a status is reached.
---
--- ## 1.4) Task briefing:
---
--- A task briefing can be given that is shown to the player when he is assigned to the task.
---
--- @field #TASK TASK
---
-TASK = {
- ClassName = "TASK",
- TaskScheduler = nil,
- ProcessClasses = {}, -- The container of the Process classes that will be used to create and assign new processes for the task to ProcessUnits.
- Processes = {}, -- The container of actual process objects instantiated and assigned to ProcessUnits.
- Players = nil,
- Scores = {},
- Menu = {},
- SetGroup = nil,
- FsmTemplate = nil,
- Mission = nil,
- CommandCenter = nil,
- TimeOut = 0,
-}
-
---- FSM PlayerAborted event handler prototype for TASK.
--- @function [parent=#TASK] OnAfterPlayerAborted
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The Unit of the Player when he went back to spectators or left the mission.
--- @param #string PlayerName The name of the Player.
-
---- FSM PlayerCrashed event handler prototype for TASK.
--- @function [parent=#TASK] OnAfterPlayerCrashed
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The Unit of the Player when he crashed in the mission.
--- @param #string PlayerName The name of the Player.
-
---- FSM PlayerDead event handler prototype for TASK.
--- @function [parent=#TASK] OnAfterPlayerDead
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The Unit of the Player when he died in the mission.
--- @param #string PlayerName The name of the Player.
-
---- FSM Fail synchronous event function for TASK.
--- Use this event to Fail the Task.
--- @function [parent=#TASK] Fail
--- @param #TASK self
-
---- FSM Fail asynchronous event function for TASK.
--- Use this event to Fail the Task.
--- @function [parent=#TASK] __Fail
--- @param #TASK self
-
---- FSM Abort synchronous event function for TASK.
--- Use this event to Abort the Task.
--- @function [parent=#TASK] Abort
--- @param #TASK self
-
---- FSM Abort asynchronous event function for TASK.
--- Use this event to Abort the Task.
--- @function [parent=#TASK] __Abort
--- @param #TASK self
-
---- FSM Success synchronous event function for TASK.
--- Use this event to make the Task a Success.
--- @function [parent=#TASK] Success
--- @param #TASK self
-
---- FSM Success asynchronous event function for TASK.
--- Use this event to make the Task a Success.
--- @function [parent=#TASK] __Success
--- @param #TASK self
-
---- FSM Cancel synchronous event function for TASK.
--- Use this event to Cancel the Task.
--- @function [parent=#TASK] Cancel
--- @param #TASK self
-
---- FSM Cancel asynchronous event function for TASK.
--- Use this event to Cancel the Task.
--- @function [parent=#TASK] __Cancel
--- @param #TASK self
-
---- FSM Replan synchronous event function for TASK.
--- Use this event to Replan the Task.
--- @function [parent=#TASK] Replan
--- @param #TASK self
-
---- FSM Replan asynchronous event function for TASK.
--- Use this event to Replan the Task.
--- @function [parent=#TASK] __Replan
--- @param #TASK self
-
-
---- Instantiates a new TASK. Should never be used. Interface Class.
--- @param #TASK self
--- @param Tasking.Mission#MISSION Mission The mission wherein the Task is registered.
--- @param Core.Set#SET_GROUP SetGroupAssign The set of groups for which the Task can be assigned.
--- @param #string TaskName The name of the Task
--- @param #string TaskType The type of the Task
--- @return #TASK self
-function TASK:New( Mission, SetGroupAssign, TaskName, TaskType )
-
- local self = BASE:Inherit( self, FSM_TASK:New() ) -- Core.Fsm#FSM_TASK
-
- self:SetStartState( "Planned" )
- self:AddTransition( "Planned", "Assign", "Assigned" )
- self:AddTransition( "Assigned", "AssignUnit", "Assigned" )
- self:AddTransition( "Assigned", "Success", "Success" )
- self:AddTransition( "Assigned", "Fail", "Failed" )
- self:AddTransition( "Assigned", "Abort", "Aborted" )
- self:AddTransition( "Assigned", "Cancel", "Cancelled" )
- self:AddTransition( "*", "PlayerCrashed", "*" )
- self:AddTransition( "*", "PlayerAborted", "*" )
- self:AddTransition( "*", "PlayerDead", "*" )
- self:AddTransition( { "Failed", "Aborted", "Cancelled" }, "Replan", "Planned" )
- self:AddTransition( "*", "TimeOut", "Cancelled" )
-
- self:E( "New TASK " .. TaskName )
-
- self.Processes = {}
- self.Fsm = {}
-
- self.Mission = Mission
- self.CommandCenter = Mission:GetCommandCenter()
-
- self.SetGroup = SetGroupAssign
-
- self:SetType( TaskType )
- self:SetName( TaskName )
- self:SetID( Mission:GetNextTaskID( self ) ) -- The Mission orchestrates the task sequences ..
-
- self.TaskBriefing = "You are invited for the task: " .. self.TaskName .. "."
-
- self.FsmTemplate = self.FsmTemplate or FSM_PROCESS:New()
-
-
- return self
-end
-
---- Get the Task FSM Process Template
--- @param #TASK self
--- @return Core.Fsm#FSM_PROCESS
-function TASK:GetUnitProcess( TaskUnit )
-
- if TaskUnit then
- return self:GetStateMachine( TaskUnit )
- else
- return self.FsmTemplate
- end
-end
-
---- Sets the Task FSM Process Template
--- @param #TASK self
--- @param Core.Fsm#FSM_PROCESS
-function TASK:SetUnitProcess( FsmTemplate )
-
- self.FsmTemplate = FsmTemplate
-end
-
---- Add a PlayerUnit to join the Task.
--- For each Group within the Task, the Unit is check if it can join the Task.
--- If the Unit was not part of the Task, false is returned.
--- If the Unit is part of the Task, true is returned.
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player joining the Mission.
--- @param Wrapper.Group#GROUP PlayerGroup The GROUP of the player joining the Mission.
--- @return #boolean true if Unit is part of the Task.
-function TASK:JoinUnit( PlayerUnit, PlayerGroup )
- self:F( { PlayerUnit = PlayerUnit, PlayerGroup = PlayerGroup } )
-
- local PlayerUnitAdded = false
-
- local PlayerGroups = self:GetGroups()
-
- -- Is the PlayerGroup part of the PlayerGroups?
- if PlayerGroups:IsIncludeObject( PlayerGroup ) then
-
- -- Check if the PlayerGroup is already assigned to the Task. If yes, the PlayerGroup is added to the Task.
- -- If the PlayerGroup is not assigned to the Task, the menu needs to be set. In that case, the PlayerUnit will become the GroupPlayer leader.
- if self:IsStatePlanned() or self:IsStateReplanned() then
- self:SetMenuForGroup( PlayerGroup )
- --self:MessageToGroups( PlayerUnit:GetPlayerName() .. " is planning to join Task " .. self:GetName() )
- end
- if self:IsStateAssigned() then
- local IsAssignedToGroup = self:IsAssignedToGroup( PlayerGroup )
- self:E( { IsAssignedToGroup = IsAssignedToGroup } )
- if IsAssignedToGroup then
- self:AssignToUnit( PlayerUnit )
- self:MessageToGroups( PlayerUnit:GetPlayerName() .. " joined Task " .. self:GetName() )
- end
- end
- end
-
- return PlayerUnitAdded
-end
-
---- Abort a PlayerUnit from a Task.
--- If the Unit was not part of the Task, false is returned.
--- If the Unit is part of the Task, true is returned.
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player aborting the Task.
--- @return #boolean true if Unit is part of the Task.
-function TASK:AbortUnit( PlayerUnit )
- self:F( { PlayerUnit = PlayerUnit } )
-
- local PlayerUnitAborted = false
-
- local PlayerGroups = self:GetGroups()
- local PlayerGroup = PlayerUnit:GetGroup()
-
- -- Is the PlayerGroup part of the PlayerGroups?
- if PlayerGroups:IsIncludeObject( PlayerGroup ) then
-
- -- Check if the PlayerGroup is already assigned to the Task. If yes, the PlayerGroup is aborted from the Task.
- -- If the PlayerUnit was the last unit of the PlayerGroup, the menu needs to be removed from the Group.
- if self:IsStateAssigned() then
- local IsAssignedToGroup = self:IsAssignedToGroup( PlayerGroup )
- self:E( { IsAssignedToGroup = IsAssignedToGroup } )
- if IsAssignedToGroup then
- self:UnAssignFromUnit( PlayerUnit )
- self:MessageToGroups( PlayerUnit:GetPlayerName() .. " aborted Task " .. self:GetName() )
- self:E( { TaskGroup = PlayerGroup:GetName(), GetUnits = PlayerGroup:GetUnits() } )
- if #PlayerGroup:GetUnits() == 1 then
- self:UnAssignFromGroup( PlayerGroup )
- PlayerGroup:SetState( PlayerGroup, "Assigned", nil )
- self:RemoveMenuForGroup( PlayerGroup )
- end
- self:Abort()
- end
- end
- end
-
- return PlayerUnitAborted
-end
-
---- A PlayerUnit crashed in a Task. Abort the Player.
--- If the Unit was not part of the Task, false is returned.
--- If the Unit is part of the Task, true is returned.
--- @param #TASK self
--- @param Wrapper.Unit#UNIT PlayerUnit The CLIENT or UNIT of the Player aborting the Task.
--- @return #boolean true if Unit is part of the Task.
-function TASK:CrashUnit( PlayerUnit )
- self:F( { PlayerUnit = PlayerUnit } )
-
- local PlayerUnitCrashed = false
-
- local PlayerGroups = self:GetGroups()
- local PlayerGroup = PlayerUnit:GetGroup()
-
- -- Is the PlayerGroup part of the PlayerGroups?
- if PlayerGroups:IsIncludeObject( PlayerGroup ) then
-
- -- Check if the PlayerGroup is already assigned to the Task. If yes, the PlayerGroup is aborted from the Task.
- -- If the PlayerUnit was the last unit of the PlayerGroup, the menu needs to be removed from the Group.
- if self:IsStateAssigned() then
- local IsAssignedToGroup = self:IsAssignedToGroup( PlayerGroup )
- self:E( { IsAssignedToGroup = IsAssignedToGroup } )
- if IsAssignedToGroup then
- self:UnAssignFromUnit( PlayerUnit )
- self:MessageToGroups( PlayerUnit:GetPlayerName() .. " crashed in Task " .. self:GetName() )
- self:E( { TaskGroup = PlayerGroup:GetName(), GetUnits = PlayerGroup:GetUnits() } )
- if #PlayerGroup:GetUnits() == 1 then
- PlayerGroup:SetState( PlayerGroup, "Assigned", nil )
- self:RemoveMenuForGroup( PlayerGroup )
- end
- self:PlayerCrashed( PlayerUnit )
- end
- end
- end
-
- return PlayerUnitCrashed
-end
-
-
-
---- Gets the Mission to where the TASK belongs.
--- @param #TASK self
--- @return Tasking.Mission#MISSION
-function TASK:GetMission()
-
- return self.Mission
-end
-
-
---- Gets the SET_GROUP assigned to the TASK.
--- @param #TASK self
--- @return Core.Set#SET_GROUP
-function TASK:GetGroups()
- return self.SetGroup
-end
-
-
-
---- Assign the @{Task} to a @{Group}.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @return #TASK
-function TASK:AssignToGroup( TaskGroup )
- self:F2( TaskGroup:GetName() )
-
- local TaskGroupName = TaskGroup:GetName()
-
- TaskGroup:SetState( TaskGroup, "Assigned", self )
-
- local Mission = self:GetMission()
- local MissionMenu = Mission:GetMenu( TaskGroup )
- MissionMenu:RemoveSubMenus()
-
- --self:RemoveMenuForGroup( TaskGroup )
- self:SetAssignedMenuForGroup( TaskGroup )
-
- local TaskUnits = TaskGroup:GetUnits()
- for UnitID, UnitData in pairs( TaskUnits ) do
- local TaskUnit = UnitData -- Wrapper.Unit#UNIT
- local PlayerName = TaskUnit:GetPlayerName()
- self:E(PlayerName)
- if PlayerName ~= nil or PlayerName ~= "" then
- self:AssignToUnit( TaskUnit )
- end
- end
-
- return self
-end
-
----
--- @param #TASK self
--- @param Wrapper.Group#GROUP FindGroup
--- @return #boolean
-function TASK:HasGroup( FindGroup )
-
- return self:GetGroups():IsIncludeObject( FindGroup )
-
-end
-
---- Assign the @{Task} to an alive @{Unit}.
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @return #TASK self
-function TASK:AssignToUnit( TaskUnit )
- self:F( TaskUnit:GetName() )
-
- local FsmTemplate = self:GetUnitProcess()
-
- -- Assign a new FsmUnit to TaskUnit.
- local FsmUnit = self:SetStateMachine( TaskUnit, FsmTemplate:Copy( TaskUnit, self ) ) -- Core.Fsm#FSM_PROCESS
- self:E({"Address FsmUnit", tostring( FsmUnit ) } )
-
- FsmUnit:SetStartState( "Planned" )
-
- FsmUnit:Accept() -- Each Task needs to start with an Accept event to start the flow.
-
- return self
-end
-
---- UnAssign the @{Task} from an alive @{Unit}.
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @return #TASK self
-function TASK:UnAssignFromUnit( TaskUnit )
- self:F( TaskUnit:GetName() )
-
- self:RemoveStateMachine( TaskUnit )
-
- return self
-end
-
---- Sets the TimeOut for the @{Task}. If @{Task} stayed planned for longer than TimeOut, it gets into Cancelled status.
--- @param #TASK self
--- @param #integer Timer in seconds
--- @return #TASK self
-function TASK:SetTimeOut ( Timer )
- self:F( Timer )
- self.TimeOut = Timer
- self:__TimeOut( self.TimeOut )
- return self
-end
-
---- Send a message of the @{Task} to the assigned @{Group}s.
--- @param #TASK self
-function TASK:MessageToGroups( Message )
- self:F( { Message = Message } )
-
- local Mission = self:GetMission()
- local CC = Mission:GetCommandCenter()
-
- for TaskGroupName, TaskGroup in pairs( self.SetGroup:GetSet() ) do
- local TaskGroup = TaskGroup -- Wrapper.Group#GROUP
- CC:MessageToGroup( Message, TaskGroup, TaskGroup:GetName() )
- end
-end
-
-
---- Send the briefng message of the @{Task} to the assigned @{Group}s.
--- @param #TASK self
-function TASK:SendBriefingToAssignedGroups()
- self:F2()
-
- for TaskGroupName, TaskGroup in pairs( self.SetGroup:GetSet() ) do
-
- if self:IsAssignedToGroup( TaskGroup ) then
- TaskGroup:Message( self.TaskBriefing, 60 )
- end
- end
-end
-
-
---- UnAssign the @{Task} from the @{Group}s.
--- @param #TASK self
-function TASK:UnAssignFromGroups()
- self:F2()
-
- for TaskGroupName, TaskGroup in pairs( self.SetGroup:GetSet() ) do
- self:UnAssignFromGroup( TaskGroup )
- end
-end
-
---- UnAssign the @{Task} from a @{Group}.
--- @param #TASK self
-function TASK:UnAssignFromGroup( TaskGroup )
- self:F2( { TaskGroup } )
-
- TaskGroup:SetState( TaskGroup, "Assigned", nil )
-
- self:RemoveAssignedMenuForGroup( TaskGroup )
-
- local TaskUnits = TaskGroup:GetUnits()
- for UnitID, UnitData in pairs( TaskUnits ) do
- local TaskUnit = UnitData -- Wrapper.Unit#UNIT
- local PlayerName = TaskUnit:GetPlayerName()
- if PlayerName ~= nil or PlayerName ~= "" then
- self:UnAssignFromUnit( TaskUnit )
- end
- end
-end
-
-
-
---- Returns if the @{Task} is assigned to the Group.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @return #boolean
-function TASK:IsAssignedToGroup( TaskGroup )
-
- local TaskGroupName = TaskGroup:GetName()
-
- if self:IsStateAssigned() then
- if TaskGroup:GetState( TaskGroup, "Assigned" ) == self then
- self:T( { "Task is assigned to:", TaskGroup:GetName() } )
- return true
- end
- end
-
- self:T( { "Task is not assigned to:", TaskGroup:GetName() } )
- return false
-end
-
---- Returns if the @{Task} has still alive and assigned Units.
--- @param #TASK self
--- @return #boolean
-function TASK:HasAliveUnits()
- self:F()
-
- for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
- if self:IsStateAssigned() then
- if self:IsAssignedToGroup( TaskGroup ) then
- for TaskUnitID, TaskUnit in pairs( TaskGroup:GetUnits() ) do
- if TaskUnit:IsAlive() then
- self:T( { HasAliveUnits = true } )
- return true
- end
- end
- end
- end
- end
-
- self:T( { HasAliveUnits = false } )
- return false
-end
-
---- Set the menu options of the @{Task} to all the groups in the SetGroup.
--- @param #TASK self
--- @param #number MenuTime
--- @return #TASK
-function TASK:SetMenu( MenuTime )
- self:F()
-
- self.SetGroup:Flush()
- for TaskGroupID, TaskGroupData in pairs( self.SetGroup:GetSet() ) do
- local TaskGroup = TaskGroupData -- Wrapper.Group#GROUP
- if TaskGroup:IsAlive() and TaskGroup:GetPlayerNames() then
- if self:IsStatePlanned() or self:IsStateReplanned() then
- self:SetMenuForGroup( TaskGroup, MenuTime )
- end
- end
- end
-end
-
-
-
---- Set the Menu for a Group
--- @param #TASK self
--- @param #number MenuTime
--- @return #TASK
-function TASK:SetMenuForGroup( TaskGroup, MenuTime )
-
- if not TaskGroup:GetState( TaskGroup, "Assigned" ) then
- self:SetPlannedMenuForGroup( TaskGroup, self:GetTaskName(), MenuTime )
- else
- if not self:IsAssignedToGroup( TaskGroup ) then
- self:SetAssignedMenuForGroup( TaskGroup, MenuTime )
- end
- end
-end
-
-
---- Set the planned menu option of the @{Task}.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @param #string MenuText The menu text.
--- @param #number MenuTime
--- @return #TASK self
-function TASK:SetPlannedMenuForGroup( TaskGroup, MenuText, MenuTime )
- self:E( TaskGroup:GetName() )
-
- local Mission = self:GetMission()
- local MissionName = Mission:GetName()
- local CommandCenter = Mission:GetCommandCenter()
- local CommandCenterMenu = CommandCenter:GetMenu()
-
- local MissionMenu = MENU_GROUP:New( TaskGroup, MissionName, CommandCenterMenu ):SetTime( MenuTime )
-
-
- local MissionMenu = Mission:GetMenu( TaskGroup )
-
- local TaskType = self:GetType()
- local TaskTypeMenu = MENU_GROUP:New( TaskGroup, TaskType, MissionMenu ):SetTime( MenuTime )
- local TaskMenu = MENU_GROUP_COMMAND:New( TaskGroup, MenuText, TaskTypeMenu, self.MenuAssignToGroup, { self = self, TaskGroup = TaskGroup } ):SetTime( MenuTime ):SetRemoveParent( true )
-
- return self
-end
-
---- Set the assigned menu options of the @{Task}.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @param #number MenuTime
--- @return #TASK self
-function TASK:SetAssignedMenuForGroup( TaskGroup, MenuTime )
- self:E( TaskGroup:GetName() )
-
- local Mission = self:GetMission()
- local MissionMenu = Mission:GetMenu( TaskGroup )
-
- self:E( { MissionMenu = MissionMenu } )
-
- local TaskTypeMenu = MENU_GROUP_COMMAND:New( TaskGroup, "Task Status", MissionMenu, self.MenuTaskStatus, self, TaskGroup ):SetTime( MenuTime )
- local TaskMenu = MENU_GROUP_COMMAND:New( TaskGroup, "Abort Task", MissionMenu, self.MenuTaskAbort, self, TaskGroup ):SetTime( MenuTime )
-
- return self
-end
-
---- Remove the menu options of the @{Task} to all the groups in the SetGroup.
--- @param #TASK self
--- @param #number MenuTime
--- @return #TASK
-function TASK:RemoveMenu( MenuTime )
- self:F()
-
- for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
- local TaskGroup = TaskGroup -- Wrapper.Group#GROUP
- if TaskGroup:IsAlive() and TaskGroup:GetPlayerNames() then
- if not self:IsAssignedToGroup( TaskGroup ) then
- self:RemovePlannedMenuForGroup( TaskGroup, MenuTime )
- end
- end
- end
-end
-
-
---- Remove the menu option of the @{Task} for a @{Group}.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @param #number MenuTime
--- @return #TASK self
-function TASK:RemovePlannedMenuForGroup( TaskGroup, MenuTime )
- self:F()
-
- local Mission = self:GetMission()
- local MissionName = Mission:GetName()
-
- local MissionMenu = Mission:GetMenu( TaskGroup )
-
- if MissionMenu then
- local TaskType = self:GetType()
- local TypeMenu = MissionMenu:GetMenu( TaskType )
-
- if TypeMenu then
- local TaskMenu = TypeMenu:GetMenu( self:GetTaskName() )
- if TaskMenu then
- TaskMenu:Remove( MenuTime )
- end
- end
- end
-
-end
-
---- Remove the assigned menu option of the @{Task} for a @{Group}.
--- @param #TASK self
--- @param Wrapper.Group#GROUP TaskGroup
--- @param #number MenuTime
--- @return #TASK self
-function TASK:RemoveAssignedMenuForGroup( TaskGroup )
- self:F()
-
- local Mission = self:GetMission()
- local MissionName = Mission:GetName()
-
- local MissionMenu = Mission:GetMenu( TaskGroup )
-
- if MissionMenu then
- MissionMenu:RemoveSubMenus()
- end
-
-end
-
-function TASK.MenuAssignToGroup( MenuParam )
-
- local self = MenuParam.self
- local TaskGroup = MenuParam.TaskGroup
-
- self:E( "Assigned menu selected")
-
- self:AssignToGroup( TaskGroup )
-end
-
---- Report the task status.
--- @param #TASK self
-function TASK:MenuTaskStatus( TaskGroup )
-
- local ReportText = self:ReportDetails()
-
- self:T( ReportText )
- self:GetMission():GetCommandCenter():MessageToGroup( ReportText, TaskGroup )
-
-end
-
---- Report the task status.
--- @param #TASK self
-function TASK:MenuTaskAbort( TaskGroup )
-
- self:Abort()
-end
-
-
-
---- Returns the @{Task} name.
--- @param #TASK self
--- @return #string TaskName
-function TASK:GetTaskName()
- return self.TaskName
-end
-
-
-
-
---- Get the default or currently assigned @{Process} template with key ProcessName.
--- @param #TASK self
--- @param #string ProcessName
--- @return Core.Fsm#FSM_PROCESS
-function TASK:GetProcessTemplate( ProcessName )
-
- local ProcessTemplate = self.ProcessClasses[ProcessName]
-
- return ProcessTemplate
-end
-
-
-
--- TODO: Obscolete?
---- Fail processes from @{Task} with key @{Unit}
--- @param #TASK self
--- @param #string TaskUnitName
--- @return #TASK self
-function TASK:FailProcesses( TaskUnitName )
-
- for ProcessID, ProcessData in pairs( self.Processes[TaskUnitName] ) do
- local Process = ProcessData
- Process.Fsm:Fail()
- end
-end
-
---- Add a FiniteStateMachine to @{Task} with key Task@{Unit}
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @param Core.Fsm#FSM_PROCESS Fsm
--- @return #TASK self
-function TASK:SetStateMachine( TaskUnit, Fsm )
- self:F2( { TaskUnit, self.Fsm[TaskUnit] ~= nil, Fsm:GetClassNameAndID() } )
-
- self.Fsm[TaskUnit] = Fsm
-
- return Fsm
-end
-
---- Gets the FiniteStateMachine of @{Task} with key Task@{Unit}
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @return Core.Fsm#FSM_PROCESS
-function TASK:GetStateMachine( TaskUnit )
- self:F2( { TaskUnit, self.Fsm[TaskUnit] ~= nil } )
-
- return self.Fsm[TaskUnit]
-end
-
---- Remove FiniteStateMachines from @{Task} with key Task@{Unit}
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @return #TASK self
-function TASK:RemoveStateMachine( TaskUnit )
- self:F( { TaskUnit, self.Fsm[TaskUnit] ~= nil } )
-
- self:E( self.Fsm )
- for TaskUnitT, Fsm in pairs( self.Fsm ) do
- self:E( TaskUnitT )
- end
-
- self.Fsm[TaskUnit] = nil
-
- collectgarbage()
- self:E( "Garbage Collected, Processes should be finalized now ...")
-end
-
---- Checks if there is a FiniteStateMachine assigned to Task@{Unit} for @{Task}
--- @param #TASK self
--- @param Wrapper.Unit#UNIT TaskUnit
--- @return #TASK self
-function TASK:HasStateMachine( TaskUnit )
- self:F( { TaskUnit, self.Fsm[TaskUnit] ~= nil } )
-
- return ( self.Fsm[TaskUnit] ~= nil )
-end
-
-
---- Gets the Scoring of the task
--- @param #TASK self
--- @return Functional.Scoring#SCORING Scoring
-function TASK:GetScoring()
- return self.Mission:GetScoring()
-end
-
-
---- Gets the Task Index, which is a combination of the Task type, the Task name.
--- @param #TASK self
--- @return #string The Task ID
-function TASK:GetTaskIndex()
-
- local TaskType = self:GetType()
- local TaskName = self:GetName()
-
- return TaskType .. "." .. TaskName
-end
-
---- Sets the Name of the Task
--- @param #TASK self
--- @param #string TaskName
-function TASK:SetName( TaskName )
- self.TaskName = TaskName
-end
-
---- Gets the Name of the Task
--- @param #TASK self
--- @return #string The Task Name
-function TASK:GetName()
- return self.TaskName
-end
-
---- Sets the Type of the Task
--- @param #TASK self
--- @param #string TaskType
-function TASK:SetType( TaskType )
- self.TaskType = TaskType
-end
-
---- Gets the Type of the Task
--- @param #TASK self
--- @return #string TaskType
-function TASK:GetType()
- return self.TaskType
-end
-
---- Sets the ID of the Task
--- @param #TASK self
--- @param #string TaskID
-function TASK:SetID( TaskID )
- self.TaskID = TaskID
-end
-
---- Gets the ID of the Task
--- @param #TASK self
--- @return #string TaskID
-function TASK:GetID()
- return self.TaskID
-end
-
-
---- Sets a @{Task} to status **Success**.
--- @param #TASK self
-function TASK:StateSuccess()
- self:SetState( self, "State", "Success" )
- return self
-end
-
---- Is the @{Task} status **Success**.
--- @param #TASK self
-function TASK:IsStateSuccess()
- return self:Is( "Success" )
-end
-
---- Sets a @{Task} to status **Failed**.
--- @param #TASK self
-function TASK:StateFailed()
- self:SetState( self, "State", "Failed" )
- return self
-end
-
---- Is the @{Task} status **Failed**.
--- @param #TASK self
-function TASK:IsStateFailed()
- return self:Is( "Failed" )
-end
-
---- Sets a @{Task} to status **Planned**.
--- @param #TASK self
-function TASK:StatePlanned()
- self:SetState( self, "State", "Planned" )
- return self
-end
-
---- Is the @{Task} status **Planned**.
--- @param #TASK self
-function TASK:IsStatePlanned()
- return self:Is( "Planned" )
-end
-
---- Sets a @{Task} to status **Aborted**.
--- @param #TASK self
-function TASK:StateAborted()
- self:SetState( self, "State", "Aborted" )
- return self
-end
-
---- Is the @{Task} status **Aborted**.
--- @param #TASK self
-function TASK:IsStateAborted()
- return self:Is( "Aborted" )
-end
-
---- Sets a @{Task} to status **Cancelled**.
--- @param #TASK self
-function TASK:StateCancelled()
- self:SetState( self, "State", "Cancelled" )
- return self
-end
-
---- Is the @{Task} status **Cancelled**.
--- @param #TASK self
-function TASK:IsStateCancelled()
- return self:Is( "Cancelled" )
-end
-
---- Sets a @{Task} to status **Assigned**.
--- @param #TASK self
-function TASK:StateAssigned()
- self:SetState( self, "State", "Assigned" )
- return self
-end
-
---- Is the @{Task} status **Assigned**.
--- @param #TASK self
-function TASK:IsStateAssigned()
- return self:Is( "Assigned" )
-end
-
---- Sets a @{Task} to status **Hold**.
--- @param #TASK self
-function TASK:StateHold()
- self:SetState( self, "State", "Hold" )
- return self
-end
-
---- Is the @{Task} status **Hold**.
--- @param #TASK self
-function TASK:IsStateHold()
- return self:Is( "Hold" )
-end
-
---- Sets a @{Task} to status **Replanned**.
--- @param #TASK self
-function TASK:StateReplanned()
- self:SetState( self, "State", "Replanned" )
- return self
-end
-
---- Is the @{Task} status **Replanned**.
--- @param #TASK self
-function TASK:IsStateReplanned()
- return self:Is( "Replanned" )
-end
-
---- Gets the @{Task} status.
--- @param #TASK self
-function TASK:GetStateString()
- return self:GetState( self, "State" )
-end
-
---- Sets a @{Task} briefing.
--- @param #TASK self
--- @param #string TaskBriefing
--- @return #TASK self
-function TASK:SetBriefing( TaskBriefing )
- self.TaskBriefing = TaskBriefing
- return self
-end
-
-
-
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string Event
--- @param #string From
--- @param #string To
-function TASK:onenterAssigned( From, Event, To, PlayerUnit, PlayerName )
-
- self:E( { "Task Assigned", self.Dispatcher } )
-
- self:MessageToGroups( "Task " .. self:GetName() .. " has been assigned to your group." )
-
- if self.Dispatcher then
- self:E( "Firing Assign event " )
- self.Dispatcher:Assign( self, PlayerUnit, PlayerName )
- end
-
- self:GetMission():__Start( 1 )
-end
-
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string Event
--- @param #string From
--- @param #string To
-function TASK:onenterSuccess( From, Event, To )
-
- self:E( "Task Success" )
-
- self:GetMission():GetCommandCenter():MessageToCoalition( "Task " .. self:GetName() .. " is successful! Good job!" )
- self:UnAssignFromGroups()
-
- self:GetMission():__Complete( 1 )
-
-end
-
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string From
--- @param #string Event
--- @param #string To
-function TASK:onenterAborted( From, Event, To )
-
- self:E( "Task Aborted" )
-
- self:GetMission():GetCommandCenter():MessageToCoalition( "Task " .. self:GetName() .. " has been aborted! Task may be replanned." )
-
- self:UnAssignFromGroups()
-
- self:__Replan( 5 )
-end
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string From
--- @param #string Event
--- @param #string To
-function TASK:onafterReplan( From, Event, To )
-
- self:E( "Task Replanned" )
-
- self:GetMission():GetCommandCenter():MessageToCoalition( "Replanning Task " .. self:GetName() .. "." )
-
- self:SetMenu()
-
-end
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string From
--- @param #string Event
--- @param #string To
-function TASK:onenterFailed( From, Event, To )
-
- self:E( "Task Failed" )
-
- self:GetMission():GetCommandCenter():MessageToCoalition( "Task " .. self:GetName() .. " has failed!" )
-
- self:UnAssignFromGroups()
-end
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string Event
--- @param #string From
--- @param #string To
-function TASK:onstatechange( From, Event, To )
-
- if self:IsTrace() then
- MESSAGE:New( "@ Task " .. self.TaskName .. " : " .. Event .. " changed to state " .. To, 2 ):ToAll()
- end
-
- if self.Scores[To] then
- local Scoring = self:GetScoring()
- if Scoring then
- self:E( { self.Scores[To].ScoreText, self.Scores[To].Score } )
- Scoring:_AddMissionScore( self.Mission, self.Scores[To].ScoreText, self.Scores[To].Score )
- end
- end
-
-end
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string Event
--- @param #string From
--- @param #string To
-function TASK:onenterPlanned( From, Event, To)
- if not self.TimeOut == 0 then
- self.__TimeOut( self.TimeOut )
- end
-end
-
---- FSM function for a TASK
--- @param #TASK self
--- @param #string Event
--- @param #string From
--- @param #string To
-function TASK:onbeforeTimeOut( From, Event, To )
- if From == "Planned" then
- self:RemoveMenu()
- return true
- end
- return false
-end
-
-do -- Dispatcher
-
- --- Set dispatcher of a task
- -- @param #TASK self
- -- @param Tasking.DetectionManager#DETECTION_MANAGER Dispatcher
- -- @return #TASK
- function TASK:SetDispatcher( Dispatcher )
- self.Dispatcher = Dispatcher
- end
-
-end
-
-do -- Reporting
-
---- Create a summary report of the Task.
--- List the Task Name and Status
--- @param #TASK self
--- @return #string
-function TASK:ReportSummary()
-
- local Report = REPORT:New()
-
- -- List the name of the Task.
- local Name = self:GetName()
-
- -- Determine the status of the Task.
- local State = self:GetState()
-
- Report:Add( "Task " .. Name .. " - State '" .. State )
-
- return Report:Text()
-end
-
-
---- Create a detailed report of the Task.
--- List the Task Status, and the Players assigned to the Task.
--- @param #TASK self
--- @return #string
-function TASK:ReportDetails()
-
- local Report = REPORT:New()
-
- -- List the name of the Task.
- local Name = self:GetName()
-
- -- Determine the status of the Task.
- local State = self:GetState()
-
- -- Loop each Unit active in the Task, and find Player Names.
- local PlayerNames = {}
- local PlayerReport = REPORT:New( " - Players:" )
- for PlayerGroupID, PlayerGroupData in pairs( self:GetGroups():GetSet() ) do
- local PlayerGroup = PlayerGroupData -- Wrapper.Group#GROUP
- PlayerNames = PlayerGroup:GetPlayerNames()
- if PlayerNames then
- PlayerReport:Add( " -- Group " .. PlayerGroup:GetCallsign() .. ": " .. table.concat( PlayerNames, ", " ) )
- end
- end
-
- -- Loop each Process in the Task, and find Reporting Details.
- Report:Add( string.format( " - Task %s\n -- State '%s'\n%s", Name, State, PlayerReport:Text() ) )
- return Report:Text()
-end
-
-
-end -- Reporting
---- This module contains the DETECTION_MANAGER class and derived classes.
---
--- ===
---
--- 1) @{DetectionManager#DETECTION_MANAGER} class, extends @{Fsm#FSM}
--- ====================================================================
--- The @{DetectionManager#DETECTION_MANAGER} class defines the core functions to report detected objects to groups.
--- Reportings can be done in several manners, and it is up to the derived classes if DETECTION_MANAGER to model the reporting behaviour.
---
--- 1.1) DETECTION_MANAGER constructor:
--- -----------------------------------
--- * @{DetectionManager#DETECTION_MANAGER.New}(): Create a new DETECTION_MANAGER instance.
---
--- 1.2) DETECTION_MANAGER reporting:
--- ---------------------------------
--- Derived DETECTION_MANAGER classes will reports detected units using the method @{DetectionManager#DETECTION_MANAGER.ReportDetected}(). This method implements polymorphic behaviour.
---
--- The time interval in seconds of the reporting can be changed using the methods @{DetectionManager#DETECTION_MANAGER.SetReportInterval}().
--- To control how long a reporting message is displayed, use @{DetectionManager#DETECTION_MANAGER.SetReportDisplayTime}().
--- Derived classes need to implement the method @{DetectionManager#DETECTION_MANAGER.GetReportDisplayTime}() to use the correct display time for displayed messages during a report.
---
--- Reporting can be started and stopped using the methods @{DetectionManager#DETECTION_MANAGER.StartReporting}() and @{DetectionManager#DETECTION_MANAGER.StopReporting}() respectively.
--- If an ad-hoc report is requested, use the method @{DetectionManager#DETECTION_MANAGER#ReportNow}().
---
--- The default reporting interval is every 60 seconds. The reporting messages are displayed 15 seconds.
---
--- ===
---
--- 2) @{DetectionManager#DETECTION_REPORTING} class, extends @{DetectionManager#DETECTION_MANAGER}
--- =========================================================================================
--- The @{DetectionManager#DETECTION_REPORTING} class implements detected units reporting. Reporting can be controlled using the reporting methods available in the @{DetectionManager#DETECTION_MANAGER} class.
---
--- 2.1) DETECTION_REPORTING constructor:
--- -------------------------------
--- The @{DetectionManager#DETECTION_REPORTING.New}() method creates a new DETECTION_REPORTING instance.
---
---
--- ===
---
--- ### Contributions: Mechanist, Prof_Hilactic, FlightControl - Concept & Testing
--- ### Author: FlightControl - Framework Design & Programming
---
--- @module DetectionManager
-
-do -- DETECTION MANAGER
-
- --- DETECTION_MANAGER class.
- -- @type DETECTION_MANAGER
- -- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
- -- @field Functional.Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
- -- @extends Core.Fsm#FSM
- DETECTION_MANAGER = {
- ClassName = "DETECTION_MANAGER",
- SetGroup = nil,
- Detection = nil,
- }
-
- --- FAC constructor.
- -- @param #DETECTION_MANAGER self
- -- @param Set#SET_GROUP SetGroup
- -- @param Functional.Detection#DETECTION_BASE Detection
- -- @return #DETECTION_MANAGER self
- function DETECTION_MANAGER:New( SetGroup, Detection )
-
- -- Inherits from BASE
- local self = BASE:Inherit( self, FSM:New() ) -- #DETECTION_MANAGER
-
- self.SetGroup = SetGroup
- self.Detection = Detection
-
- self:SetStartState( "Stopped" )
- self:AddTransition( "Stopped", "Start", "Started" )
- self:AddTransition( "Started", "Stop", "Stopped" )
- self:AddTransition( "Started", "Report", "Started" )
-
- self:SetReportInterval( 30 )
- self:SetReportDisplayTime( 25 )
-
- Detection:__Start( 1 )
-
- return self
- end
-
- function DETECTION_MANAGER:onafterStart( From, Event, To )
- self:Report()
- end
-
- function DETECTION_MANAGER:onafterReport( From, Event, To )
-
- self:E( "onafterReport" )
-
- self:__Report( -self._ReportInterval )
-
- self:ProcessDetected( self.Detection )
- end
-
- --- Set the reporting time interval.
- -- @param #DETECTION_MANAGER self
- -- @param #number ReportInterval The interval in seconds when a report needs to be done.
- -- @return #DETECTION_MANAGER self
- function DETECTION_MANAGER:SetReportInterval( ReportInterval )
- self:F2()
-
- self._ReportInterval = ReportInterval
- end
-
-
- --- Set the reporting message display time.
- -- @param #DETECTION_MANAGER self
- -- @param #number ReportDisplayTime The display time in seconds when a report needs to be done.
- -- @return #DETECTION_MANAGER self
- function DETECTION_MANAGER:SetReportDisplayTime( ReportDisplayTime )
- self:F2()
-
- self._ReportDisplayTime = ReportDisplayTime
- end
-
- --- Get the reporting message display time.
- -- @param #DETECTION_MANAGER self
- -- @return #number ReportDisplayTime The display time in seconds when a report needs to be done.
- function DETECTION_MANAGER:GetReportDisplayTime()
- self:F2()
-
- return self._ReportDisplayTime
- end
-
- --- Reports the detected items to the @{Set#SET_GROUP}.
- -- @param #DETECTION_MANAGER self
- -- @param Functional.Detection#DETECTION_BASE Detection
- -- @return #DETECTION_MANAGER self
- function DETECTION_MANAGER:ProcessDetected( Detection )
- self:E()
-
- end
-
-end
-
-
-do -- DETECTION_REPORTING
-
- --- DETECTION_REPORTING class.
- -- @type DETECTION_REPORTING
- -- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
- -- @field Functional.Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
- -- @extends #DETECTION_MANAGER
- DETECTION_REPORTING = {
- ClassName = "DETECTION_REPORTING",
- }
-
-
- --- DETECTION_REPORTING constructor.
- -- @param #DETECTION_REPORTING self
- -- @param Set#SET_GROUP SetGroup
- -- @param Functional.Detection#DETECTION_AREAS Detection
- -- @return #DETECTION_REPORTING self
- function DETECTION_REPORTING:New( SetGroup, Detection )
-
- -- Inherits from DETECTION_MANAGER
- local self = BASE:Inherit( self, DETECTION_MANAGER:New( SetGroup, Detection ) ) -- #DETECTION_REPORTING
-
- self:Schedule( 1, 30 )
- return self
- end
-
- --- Creates a string of the detected items in a @{Detection}.
- -- @param #DETECTION_MANAGER self
- -- @param Set#SET_UNIT DetectedSet The detected Set created by the @{Detection#DETECTION_BASE} object.
- -- @return #DETECTION_MANAGER self
- function DETECTION_REPORTING:GetDetectedItemsText( DetectedSet )
- self:F2()
-
- local MT = {} -- Message Text
- local UnitTypes = {}
-
- for DetectedUnitID, DetectedUnitData in pairs( DetectedSet:GetSet() ) do
- local DetectedUnit = DetectedUnitData -- Wrapper.Unit#UNIT
- if DetectedUnit:IsAlive() then
- local UnitType = DetectedUnit:GetTypeName()
-
- if not UnitTypes[UnitType] then
- UnitTypes[UnitType] = 1
- else
- UnitTypes[UnitType] = UnitTypes[UnitType] + 1
- end
- end
- end
-
- for UnitTypeID, UnitType in pairs( UnitTypes ) do
- MT[#MT+1] = UnitType .. " of " .. UnitTypeID
- end
-
- return table.concat( MT, ", " )
- end
-
-
-
- --- Reports the detected items to the @{Set#SET_GROUP}.
- -- @param #DETECTION_REPORTING self
- -- @param Wrapper.Group#GROUP Group The @{Group} object to where the report needs to go.
- -- @param Functional.Detection#DETECTION_AREAS Detection The detection created by the @{Detection#DETECTION_BASE} object.
- -- @return #boolean Return true if you want the reporting to continue... false will cancel the reporting loop.
- function DETECTION_REPORTING:ProcessDetected( Group, Detection )
- self:F2( Group )
-
- self:E( Group )
- local DetectedMsg = {}
- for DetectedAreaID, DetectedAreaData in pairs( Detection:GetDetectedAreas() ) do
- local DetectedArea = DetectedAreaData -- Functional.Detection#DETECTION_AREAS.DetectedArea
- DetectedMsg[#DetectedMsg+1] = " - Group #" .. DetectedAreaID .. ": " .. self:GetDetectedItemsText( DetectedArea.Set )
- end
- local FACGroup = Detection:GetDetectionGroups()
- FACGroup:MessageToGroup( "Reporting detected target groups:\n" .. table.concat( DetectedMsg, "\n" ), self:GetReportDisplayTime(), Group )
-
- return true
- end
-
-end
-
---- **Tasking** - The TASK_A2G_DISPATCHER creates and manages player TASK_A2G tasks based on detected targets.
---
--- ===
---
--- # 1) @{#TASK_A2G_DISPATCHER} class, extends @{#DETECTION_MANAGER}
---
--- The @{#TASK_A2G_DISPATCHER} class implements the dynamic dispatching of tasks upon groups of detected units determined a @{Set} of FAC (groups).
--- The FAC will detect units, will group them, and will dispatch @{Task}s to groups. Depending on the type of target detected, different tasks will be dispatched.
--- Find a summary below describing for which situation a task type is created:
---
--- * **CAS Task**: Is created when there are enemy ground units within range of the FAC, while there are friendly units in the FAC perimeter.
--- * **BAI Task**: Is created when there are enemy ground units within range of the FAC, while there are NO other friendly units within the FAC perimeter.
--- * **SEAD Task**: Is created when there are enemy ground units wihtin range of the FAC, with air search radars.
---
--- Other task types will follow...
---
--- 3.1) TASK_A2G_DISPATCHER constructor:
--- --------------------------------------
--- The @{#TASK_A2G_DISPATCHER.New}() method creates a new TASK_A2G_DISPATCHER instance.
---
--- ===
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-03-09: Initial class and API.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- ### Authors:
---
--- * **FlightControl**: Concept, Design & Programming.
---
--- @module Task_A2G_Dispatcher
-
-do -- TASK_A2G_DISPATCHER
-
- --- TASK_A2G_DISPATCHER class.
- -- @type TASK_A2G_DISPATCHER
- -- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
- -- @field Functional.Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
- -- @field Tasking.Mission#MISSION Mission
- -- @extends Tasking.DetectionManager#DETECTION_MANAGER
- TASK_A2G_DISPATCHER = {
- ClassName = "TASK_A2G_DISPATCHER",
- Mission = nil,
- Detection = nil,
- }
-
-
- --- TASK_A2G_DISPATCHER constructor.
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Tasking.Mission#MISSION Mission The mission for which the task dispatching is done.
- -- @param Set#SET_GROUP SetGroup The set of groups that can join the tasks within the mission.
- -- @param Functional.Detection#DETECTION_BASE Detection The detection results that are used to dynamically assign new tasks to human players.
- -- @return #TASK_A2G_DISPATCHER self
- function TASK_A2G_DISPATCHER:New( Mission, SetGroup, Detection )
-
- -- Inherits from DETECTION_MANAGER
- local self = BASE:Inherit( self, DETECTION_MANAGER:New( SetGroup, Detection ) ) -- #TASK_A2G_DISPATCHER
-
- self.Detection = Detection
- self.Mission = Mission
-
- self:AddTransition( "Started", "Assign", "Started" )
-
- --- OnAfter Transition Handler for Event Assign.
- -- @function [parent=#TASK_A2G_DISPATCHER] OnAfterAssign
- -- @param #TASK_A2G_DISPATCHER self
- -- @param #string From The From State string.
- -- @param #string Event The Event string.
- -- @param #string To The To State string.
- -- @param Tasking.Task_A2G#TASK_A2G Task
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param #string PlayerName
-
- self:__Start( 5 )
-
- return self
- end
-
-
- --- Creates a SEAD task when there are targets for it.
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Functional.Detection#DETECTION_AREAS.DetectedItem DetectedItem
- -- @return Set#SET_UNIT TargetSetUnit: The target set of units.
- -- @return #nil If there are no targets to be set.
- function TASK_A2G_DISPATCHER:EvaluateSEAD( DetectedItem )
- self:F( { DetectedItem.ItemID } )
-
- local DetectedSet = DetectedItem.Set
- local DetectedZone = DetectedItem.Zone
-
- -- Determine if the set has radar targets. If it does, construct a SEAD task.
- local RadarCount = DetectedSet:HasSEAD()
-
- if RadarCount > 0 then
-
- -- Here we're doing something advanced... We're copying the DetectedSet, but making a new Set only with SEADable Radar units in it.
- local TargetSetUnit = SET_UNIT:New()
- TargetSetUnit:SetDatabase( DetectedSet )
- TargetSetUnit:FilterHasSEAD()
- TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
-
- return TargetSetUnit
- end
-
- return nil
- end
-
- --- Creates a CAS task when there are targets for it.
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Functional.Detection#DETECTION_AREAS.DetectedItem DetectedItem
- -- @return Tasking.Task#TASK
- function TASK_A2G_DISPATCHER:EvaluateCAS( DetectedItem )
- self:F( { DetectedItem.ItemID } )
-
- local DetectedSet = DetectedItem.Set
- local DetectedZone = DetectedItem.Zone
-
-
- -- Determine if the set has radar targets. If it does, construct a SEAD task.
- local GroundUnitCount = DetectedSet:HasGroundUnits()
- local FriendliesNearBy = self.Detection:IsFriendliesNearBy( DetectedItem )
-
- if GroundUnitCount > 0 and FriendliesNearBy == true then
-
- -- Copy the Set
- local TargetSetUnit = SET_UNIT:New()
- TargetSetUnit:SetDatabase( DetectedSet )
- TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
-
- return TargetSetUnit
- end
-
- return nil
- end
-
- --- Creates a BAI task when there are targets for it.
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Functional.Detection#DETECTION_AREAS.DetectedItem DetectedItem
- -- @return Tasking.Task#TASK
- function TASK_A2G_DISPATCHER:EvaluateBAI( DetectedItem, FriendlyCoalition )
- self:F( { DetectedItem.ItemID } )
-
- local DetectedSet = DetectedItem.Set
- local DetectedZone = DetectedItem.Zone
-
-
- -- Determine if the set has radar targets. If it does, construct a SEAD task.
- local GroundUnitCount = DetectedSet:HasGroundUnits()
- local FriendliesNearBy = self.Detection:IsFriendliesNearBy( DetectedItem )
-
- if GroundUnitCount > 0 and FriendliesNearBy == false then
-
- -- Copy the Set
- local TargetSetUnit = SET_UNIT:New()
- TargetSetUnit:SetDatabase( DetectedSet )
- TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
-
- return TargetSetUnit
- end
-
- return nil
- end
-
- --- Evaluates the removal of the Task from the Mission.
- -- Can only occur when the DetectedItem is Changed AND the state of the Task is "Planned".
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Tasking.Mission#MISSION Mission
- -- @param Tasking.Task#TASK Task
- -- @param Functional.Detection#DETECTION_AREAS.DetectedItem DetectedItem
- -- @return Tasking.Task#TASK
- function TASK_A2G_DISPATCHER:EvaluateRemoveTask( Mission, Task, DetectedItem )
-
- if Task then
- if Task:IsStatePlanned() and DetectedItem.Changed == true then
- self:E( "Removing Tasking: " .. Task:GetTaskName() )
- Task = Mission:RemoveTask( Task )
- end
- end
-
- return Task
- end
-
-
- --- Assigns tasks in relation to the detected items to the @{Set#SET_GROUP}.
- -- @param #TASK_A2G_DISPATCHER self
- -- @param Functional.Detection#DETECTION_BASE Detection The detection created by the @{Detection#DETECTION_BASE} derived object.
- -- @return #boolean Return true if you want the task assigning to continue... false will cancel the loop.
- function TASK_A2G_DISPATCHER:ProcessDetected( Detection )
- self:E()
-
- local AreaMsg = {}
- local TaskMsg = {}
- local ChangeMsg = {}
-
- local Mission = self.Mission
- local ReportSEAD = REPORT:New( "- SEAD Tasks:")
- local ReportCAS = REPORT:New( "- CAS Tasks:")
- local ReportBAI = REPORT:New( "- BAI Tasks:")
- local ReportChanges = REPORT:New( " - Changes:" )
-
- --- First we need to the detected targets.
- for DetectedItemID, DetectedItem in pairs( Detection:GetDetectedItems() ) do
-
- local DetectedItem = DetectedItem -- Functional.Detection#DETECTION_BASE.DetectedItem
- local DetectedSet = DetectedItem.Set -- Functional.Detection#DETECTION_BASE.DetectedSet
- local DetectedZone = DetectedItem.Zone
- self:E( { "Targets in DetectedItem", DetectedItem.ItemID, DetectedSet:Count(), tostring( DetectedItem ) } )
- DetectedSet:Flush()
-
- local ItemID = DetectedItem.ItemID
-
- -- Evaluate SEAD Tasking
- local SEADTask = Mission:GetTask( string.format( "SEAD.%03d", ItemID ) )
- SEADTask = self:EvaluateRemoveTask( Mission, SEADTask, DetectedItem )
- if not SEADTask then
- local TargetSetUnit = self:EvaluateSEAD( DetectedItem ) -- Returns a SetUnit if there are targets to be SEADed...
- if TargetSetUnit then
- local Task = TASK_SEAD:New( Mission, self.SetGroup, string.format( "SEAD.%03d", ItemID ), TargetSetUnit )
- Task:SetTargetZone( DetectedZone )
- Task:SetDispatcher( self )
- SEADTask = Mission:AddTask( Task )
- end
- end
- if SEADTask and SEADTask:IsStatePlanned() then
- ReportSEAD:Add( string.format( " - %s.%02d - %s", "SEAD", ItemID, Detection:DetectedItemReportSummary(DetectedItemID) ) )
- end
-
- -- Evaluate CAS Tasking
- local CASTask = Mission:GetTask( string.format( "CAS.%03d", ItemID ) )
- CASTask = self:EvaluateRemoveTask( Mission, CASTask, DetectedItem )
- if not CASTask then
- local TargetSetUnit = self:EvaluateCAS( DetectedItem ) -- Returns a SetUnit if there are targets to be SEADed...
- if TargetSetUnit then
- local Task = TASK_CAS:New( Mission, self.SetGroup, string.format( "CAS.%03d", ItemID ), TargetSetUnit )
- Task:SetTargetZone( DetectedZone )
- Task:SetDispatcher( self )
- CASTask = Mission:AddTask( Task )
- end
- end
- if CASTask and CASTask:IsStatePlanned() then
- ReportCAS:Add( string.format( " - %s.%02d - %s", "CAS", ItemID, Detection:DetectedItemReportSummary(DetectedItemID) ) )
- end
-
- -- Evaluate BAI Tasking
- local BAITask = Mission:GetTask( string.format( "BAI.%03d", ItemID ) )
- BAITask = self:EvaluateRemoveTask( Mission, BAITask, DetectedItem )
- if not BAITask then
- local TargetSetUnit = self:EvaluateBAI( DetectedItem, self.Mission:GetCommandCenter():GetPositionable():GetCoalition() ) -- Returns a SetUnit if there are targets to be SEADed...
- if TargetSetUnit then
- local Task = TASK_BAI:New( Mission, self.SetGroup, string.format( "BAI.%03d", ItemID ), TargetSetUnit )
- Task:SetTargetZone( DetectedZone )
- Task:SetDispatcher( self )
- BAITask = Mission:AddTask( Task )
- end
- end
- if BAITask and BAITask:IsStatePlanned() then
- ReportBAI:Add( string.format( " - %s.%02d - %s", "BAI", ItemID, Detection:DetectedItemReportSummary(DetectedItemID) ) )
- end
-
-
- -- Loop through the changes ...
- local ChangeText = Detection:GetChangeText( DetectedItem )
- ReportChanges:Add( ChangeText )
-
-
- -- OK, so the tasking has been done, now delete the changes reported for the area.
- Detection:AcceptChanges( DetectedItem )
-
- end
-
- -- TODO set menus using the HQ coordinator
- Mission:GetCommandCenter():SetMenu()
-
- for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
- if not TaskGroup:GetState( TaskGroup, "Assigned" ) then
- Mission:GetCommandCenter():MessageToGroup(
- string.format( "HQ Reporting - Planned tasks for mission '%s':\n%s\n",
- self.Mission:GetName(),
- string.format( "%s\n\n%s\n\n%s\n\n%s", ReportSEAD:Text(), ReportCAS:Text(), ReportBAI:Text(), ReportChanges:Text()
- )
- ), TaskGroup
- )
- end
- end
-
- return true
- end
-
-end--- **Tasking** - The TASK_A2G models tasks for players in Air to Ground engagements.
---
--- 
---
---
--- # 1) @{Task_A2G#TASK_A2G} class, extends @{Task#TASK}
---
--- The @{#TASK_A2G} class defines Air To Ground tasks for a @{Set} of Target Units,
--- based on the tasking capabilities defined in @{Task#TASK}.
--- The TASK_A2G is implemented using a @{Statemachine#FSM_TASK}, and has the following statuses:
---
--- * **None**: Start of the process
--- * **Planned**: The A2G task is planned.
--- * **Assigned**: The A2G task is assigned to a @{Group#GROUP}.
--- * **Success**: The A2G task is successfully completed.
--- * **Failed**: The A2G task has failed. This will happen if the player exists the task early, without communicating a possible cancellation to HQ.
---
--- # 1.1) Set the scoring of achievements in an A2G attack.
---
--- Scoring or penalties can be given in the following circumstances:
---
--- * @{#TASK_A2G.SetScoreOnDestroy}(): Set a score when a target in scope of the A2G attack, has been destroyed.
--- * @{#TASK_A2G.SetScoreOnSuccess}(): Set a score when all the targets in scope of the A2G attack, have been destroyed.
--- * @{#TASK_A2G.SetPenaltyOnFailed}(): Set a penalty when the A2G attack has failed.
---
--- # 2) @{Task_A2G#TASK_SEAD} class, extends @{Task_A2G#TASK_A2G}
---
--- The @{#TASK_SEAD} class defines a SEAD task for a @{Set} of Target Units.
---
--- ===
---
--- # 3) @{Task_A2G#TASK_CAS} class, extends @{Task_A2G#TASK_A2G}
---
--- The @{#TASK_CAS} class defines a CAS task for a @{Set} of Target Units.
---
--- ===
---
--- # 4) @{Task_A2G#TASK_BAI} class, extends @{Task_A2G#TASK_A2G}
---
--- The @{#TASK_BAI} class defines a BAI task for a @{Set} of Target Units.
---
--- ====
---
--- # **API CHANGE HISTORY**
---
--- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
---
--- * **Added** parts are expressed in bold type face.
--- * _Removed_ parts are expressed in italic type face.
---
--- Hereby the change log:
---
--- 2017-03-09: Revised version.
---
--- ===
---
--- # **AUTHORS and CONTRIBUTIONS**
---
--- ### Contributions:
---
--- * **[WingThor]**: Concept, Advice & Testing.
---
--- ### Authors:
---
--- * **FlightControl**: Concept, Design & Programming.
---
--- @module Task_A2G
-
-do -- TASK_A2G
-
- --- The TASK_A2G class
- -- @type TASK_A2G
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends Tasking.Task#TASK
- TASK_A2G = {
- ClassName = "TASK_A2G",
- }
-
- --- Instantiates a new TASK_A2G.
- -- @param #TASK_A2G self
- -- @param Tasking.Mission#MISSION Mission
- -- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
- -- @param #string TaskName The name of the Task.
- -- @param Set#SET_UNIT UnitSetTargets
- -- @param #number TargetDistance The distance to Target when the Player is considered to have "arrived" at the engagement range.
- -- @param Core.Zone#ZONE_BASE TargetZone The target zone, if known.
- -- If the TargetZone parameter is specified, the player will be routed to the center of the zone where all the targets are assumed to be.
- -- @return #TASK_A2G self
- function TASK_A2G:New( Mission, SetGroup, TaskName, TargetSetUnit, TaskType )
- local self = BASE:Inherit( self, TASK:New( Mission, SetGroup, TaskName, TaskType ) ) -- Tasking.Task#TASK_A2G
- self:F()
-
- self.TargetSetUnit = TargetSetUnit
- self.TaskType = TaskType
-
- Mission:AddTask( self )
-
- local Fsm = self:GetUnitProcess()
-
-
- Fsm:AddProcess ( "Planned", "Accept", ACT_ASSIGN_ACCEPT:New( self.TaskBriefing ), { Assigned = "RouteToRendezVous", Rejected = "Reject" } )
-
- Fsm:AddTransition( "Assigned", "RouteToRendezVous", "RoutingToRendezVous" )
- Fsm:AddProcess ( "RoutingToRendezVous", "RouteToRendezVousPoint", ACT_ROUTE_POINT:New(), { Arrived = "ArriveAtRendezVous" } )
- Fsm:AddProcess ( "RoutingToRendezVous", "RouteToRendezVousZone", ACT_ROUTE_ZONE:New(), { Arrived = "ArriveAtRendezVous" } )
-
- Fsm:AddTransition( { "Arrived", "RoutingToRendezVous" }, "ArriveAtRendezVous", "ArrivedAtRendezVous" )
-
- Fsm:AddTransition( { "ArrivedAtRendezVous", "HoldingAtRendezVous" }, "Engage", "Engaging" )
- Fsm:AddTransition( { "ArrivedAtRendezVous", "HoldingAtRendezVous" }, "HoldAtRendezVous", "HoldingAtRendezVous" )
-
- Fsm:AddProcess ( "Engaging", "Account", ACT_ACCOUNT_DEADS:New( self.TargetSetUnit, self.TaskType ), { Accounted = "Success" } )
- Fsm:AddTransition( "Engaging", "RouteToTarget", "Engaging" )
- Fsm:AddProcess( "Engaging", "RouteToTargetZone", ACT_ROUTE_ZONE:New(), {} )
- Fsm:AddProcess( "Engaging", "RouteToTargetPoint", ACT_ROUTE_POINT:New(), {} )
- Fsm:AddTransition( "Engaging", "RouteToTargets", "Engaging" )
-
- Fsm:AddTransition( "Accounted", "DestroyedAll", "Accounted" )
- Fsm:AddTransition( "Accounted", "Success", "Success" )
- Fsm:AddTransition( "Rejected", "Reject", "Aborted" )
- Fsm:AddTransition( "Failed", "Fail", "Failed" )
-
-
- --- Test
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param Tasking.Task_A2G#TASK_A2G Task
- function Fsm:onafterRouteToRendezVous( TaskUnit, Task )
- self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
- -- Determine the first Unit from the self.RendezVousSetUnit
-
- if Task:GetRendezVousZone( TaskUnit ) then
- self:__RouteToRendezVousZone( 0.1 )
- else
- if Task:GetRendezVousPointVec2( TaskUnit ) then
- self:__RouteToRendezVousPoint( 0.1 )
- else
- self:__ArriveAtRendezVous( 0.1 )
- end
- end
- end
-
- --- Test
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param Tasking.Task#TASK_A2G Task
- function Fsm:OnAfterArriveAtRendezVous( TaskUnit, Task )
- self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
- -- Determine the first Unit from the self.TargetSetUnit
-
- self:__Engage( 0.1 )
- end
-
- --- Test
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param Tasking.Task#TASK_A2G Task
- function Fsm:onafterEngage( TaskUnit, Task )
- self:E( { self } )
- self:__Account( 0.1 )
- self:__RouteToTarget(0.1 )
- self:__RouteToTargets( -10 )
- end
-
- --- Test
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param Tasking.Task_A2G#TASK_A2G Task
- function Fsm:onafterRouteToTarget( TaskUnit, Task )
- self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
- -- Determine the first Unit from the self.TargetSetUnit
-
- if Task:GetTargetZone( TaskUnit ) then
- self:__RouteToTargetZone( 0.1 )
- else
- local TargetUnit = Task.TargetSetUnit:GetFirst() -- Wrapper.Unit#UNIT
- if TargetUnit then
- local PointVec2 = TargetUnit:GetPointVec2()
- self:T( { TargetPointVec2 = PointVec2, PointVec2:GetX(), PointVec2:GetAlt(), PointVec2:GetZ() } )
- Task:SetTargetPointVec2( TargetUnit:GetPointVec2(), TaskUnit )
- end
- self:__RouteToTargetPoint( 0.1 )
- end
- end
-
- --- Test
- -- @param #FSM_PROCESS self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @param Tasking.Task_A2G#TASK_A2G Task
- function Fsm:onafterRouteToTargets( TaskUnit, Task )
- self:E( { TaskUnit = TaskUnit, Task = Task and Task:GetClassNameAndID() } )
- local TargetUnit = Task.TargetSetUnit:GetFirst() -- Wrapper.Unit#UNIT
- if TargetUnit then
- Task:SetTargetPointVec2( TargetUnit:GetPointVec2(), TaskUnit )
- end
- self:__RouteToTargets( -10 )
- end
-
- return self
-
- end
-
- --- @param #TASK_A2G self
- function TASK_A2G:GetPlannedMenuText()
- return self:GetStateString() .. " - " .. self:GetTaskName() .. " ( " .. self.TargetSetUnit:GetUnitTypesText() .. " )"
- end
-
- --- @param #TASK_A2G self
- -- @param Core.Point#POINT_VEC2 RendezVousPointVec2 The PointVec2 object referencing to the 2D point where the RendezVous point is located on the map.
- -- @param #number RendezVousRange The RendezVousRange that defines when the player is considered to have arrived at the RendezVous point.
- -- @param Wrapper.Unit#UNIT TaskUnit
- function TASK_A2G:SetRendezVousPointVec2( RendezVousPointVec2, RendezVousRange, TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteRendezVous = ProcessUnit:GetProcess( "RoutingToRendezVous", "RouteToRendezVousPoint" ) -- Actions.Act_Route#ACT_ROUTE_POINT
- ActRouteRendezVous:SetPointVec2( RendezVousPointVec2 )
- ActRouteRendezVous:SetRange( RendezVousRange )
- end
-
- --- @param #TASK_A2G self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return Core.Point#POINT_VEC2 The PointVec2 object referencing to the 2D point where the RendezVous point is located on the map.
- -- @return #number The RendezVousRange that defines when the player is considered to have arrived at the RendezVous point.
- function TASK_A2G:GetRendezVousPointVec2( TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteRendezVous = ProcessUnit:GetProcess( "RoutingToRendezVous", "RouteToRendezVousPoint" ) -- Actions.Act_Route#ACT_ROUTE_POINT
- return ActRouteRendezVous:GetPointVec2(), ActRouteRendezVous:GetRange()
- end
-
-
-
- --- @param #TASK_A2G self
- -- @param Core.Zone#ZONE_BASE RendezVousZone The Zone object where the RendezVous is located on the map.
- -- @param Wrapper.Unit#UNIT TaskUnit
- function TASK_A2G:SetRendezVousZone( RendezVousZone, TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteRendezVous = ProcessUnit:GetProcess( "RoutingToRendezVous", "RouteToRendezVousZone" ) -- Actions.Act_Route#ACT_ROUTE_ZONE
- ActRouteRendezVous:SetZone( RendezVousZone )
- end
-
- --- @param #TASK_A2G self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return Core.Zone#ZONE_BASE The Zone object where the RendezVous is located on the map.
- function TASK_A2G:GetRendezVousZone( TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteRendezVous = ProcessUnit:GetProcess( "RoutingToRendezVous", "RouteToRendezVousZone" ) -- Actions.Act_Route#ACT_ROUTE_ZONE
- return ActRouteRendezVous:GetZone()
- end
-
- --- @param #TASK_A2G self
- -- @param Core.Point#POINT_VEC2 TargetPointVec2 The PointVec2 object where the Target is located on the map.
- -- @param Wrapper.Unit#UNIT TaskUnit
- function TASK_A2G:SetTargetPointVec2( TargetPointVec2, TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteTarget = ProcessUnit:GetProcess( "Engaging", "RouteToTargetPoint" ) -- Actions.Act_Route#ACT_ROUTE_POINT
- ActRouteTarget:SetPointVec2( TargetPointVec2 )
- end
-
-
- --- @param #TASK_A2G self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return Core.Point#POINT_VEC2 The PointVec2 object where the Target is located on the map.
- function TASK_A2G:GetTargetPointVec2( TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteTarget = ProcessUnit:GetProcess( "Engaging", "RouteToTargetPoint" ) -- Actions.Act_Route#ACT_ROUTE_POINT
- return ActRouteTarget:GetPointVec2()
- end
-
-
- --- @param #TASK_A2G self
- -- @param Core.Zone#ZONE_BASE TargetZone The Zone object where the Target is located on the map.
- -- @param Wrapper.Unit#UNIT TaskUnit
- function TASK_A2G:SetTargetZone( TargetZone, TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteTarget = ProcessUnit:GetProcess( "Engaging", "RouteToTargetZone" ) -- Actions.Act_Route#ACT_ROUTE_ZONE
- ActRouteTarget:SetZone( TargetZone )
- end
-
-
- --- @param #TASK_A2G self
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return Core.Zone#ZONE_BASE The Zone object where the Target is located on the map.
- function TASK_A2G:GetTargetZone( TaskUnit )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- local ActRouteTarget = ProcessUnit:GetProcess( "Engaging", "RouteToTargetZone" ) -- Actions.Act_Route#ACT_ROUTE_ZONE
- return ActRouteTarget:GetZone()
- end
-
- --- Set a score when a target in scope of the A2G attack, has been destroyed .
- -- @param #TASK_A2G self
- -- @param #string Text The text to display to the player, when the target has been destroyed.
- -- @param #number Score The score in points.
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return #TASK_A2G
- function TASK_A2G:SetScoreOnDestroy( Text, Score, TaskUnit )
- self:F( { Text, Score, TaskUnit } )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- ProcessUnit:AddScoreProcess( "Engaging", "Account", "Account", Text, Score )
-
- return self
- end
-
- --- Set a score when all the targets in scope of the A2G attack, have been destroyed.
- -- @param #TASK_A2G self
- -- @param #string Text The text to display to the player, when all targets hav been destroyed.
- -- @param #number Score The score in points.
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return #TASK_A2G
- function TASK_A2G:SetScoreOnSuccess( Text, Score, TaskUnit )
- self:F( { Text, Score, TaskUnit } )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- ProcessUnit:AddScore( "Success", Text, Score )
-
- return self
- end
-
- --- Set a penalty when the A2G attack has failed.
- -- @param #TASK_A2G self
- -- @param #string Text The text to display to the player, when the A2G attack has failed.
- -- @param #number Penalty The penalty in points.
- -- @param Wrapper.Unit#UNIT TaskUnit
- -- @return #TASK_A2G
- function TASK_A2G:SetPenaltyOnFailed( Text, Penalty, TaskUnit )
- self:F( { Text, Score, TaskUnit } )
-
- local ProcessUnit = self:GetUnitProcess( TaskUnit )
-
- ProcessUnit:AddScore( "Failed", Text, Penalty )
-
- return self
- end
-
-
-end
-
-
-do -- TASK_SEAD
-
- --- The TASK_SEAD class
- -- @type TASK_SEAD
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends Tasking.Task#TASK
- TASK_SEAD = {
- ClassName = "TASK_SEAD",
- }
-
- --- Instantiates a new TASK_SEAD.
- -- @param #TASK_SEAD self
- -- @param Tasking.Mission#MISSION Mission
- -- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
- -- @param #string TaskName The name of the Task.
- -- @param Set#SET_UNIT TargetSetUnit
- -- @return #TASK_SEAD self
- function TASK_SEAD:New( Mission, SetGroup, TaskName, TargetSetUnit )
- local self = BASE:Inherit( self, TASK_A2G:New( Mission, SetGroup, TaskName, TargetSetUnit, "SEAD" ) ) -- #TASK_SEAD
- self:F()
-
- return self
- end
-
-end
-
-do -- TASK_BAI
-
- --- The TASK_BAI class
- -- @type TASK_BAI
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends Tasking.Task#TASK
- TASK_BAI = {
- ClassName = "TASK_BAI",
- }
-
- --- Instantiates a new TASK_BAI.
- -- @param #TASK_BAI self
- -- @param Tasking.Mission#MISSION Mission
- -- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
- -- @param #string TaskName The name of the Task.
- -- @param Set#SET_UNIT UnitSetTargets
- -- @param #number TargetDistance The distance to Target when the Player is considered to have "arrived" at the engagement range.
- -- @param Core.Zone#ZONE_BASE TargetZone The target zone, if known.
- -- If the TargetZone parameter is specified, the player will be routed to the center of the zone where all the targets are assumed to be.
- -- @return #TASK_BAI self
- function TASK_BAI:New( Mission, SetGroup, TaskName, TargetSetUnit )
- local self = BASE:Inherit( self, TASK_A2G:New( Mission, SetGroup, TaskName, TargetSetUnit, "BAI" ) ) -- #TASK_BAI
- self:F()
-
- return self
- end
-
-end
-
-do -- TASK_CAS
-
- --- The TASK_CAS class
- -- @type TASK_CAS
- -- @field Set#SET_UNIT TargetSetUnit
- -- @extends Tasking.Task#TASK
- TASK_CAS = {
- ClassName = "TASK_CAS",
- }
-
- --- Instantiates a new TASK_CAS.
- -- @param #TASK_CAS self
- -- @param Tasking.Mission#MISSION Mission
- -- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
- -- @param #string TaskName The name of the Task.
- -- @param Set#SET_UNIT UnitSetTargets
- -- @param #number TargetDistance The distance to Target when the Player is considered to have "arrived" at the engagement range.
- -- @param Core.Zone#ZONE_BASE TargetZone The target zone, if known.
- -- If the TargetZone parameter is specified, the player will be routed to the center of the zone where all the targets are assumed to be.
- -- @return #TASK_CAS self
- function TASK_CAS:New( Mission, SetGroup, TaskName, TargetSetUnit )
- local self = BASE:Inherit( self, TASK_A2G:New( Mission, SetGroup, TaskName, TargetSetUnit, "CAS" ) ) -- #TASK_CAS
- self:F()
-
- return self
- end
-
-end
--- The order of the declarations is important here. Don't touch it.
-
---- Declare the event dispatcher based on the EVENT class
-_EVENTDISPATCHER = EVENT:New() -- Core.Event#EVENT
-
---- Declare the timer dispatcher based on the SCHEDULEDISPATCHER class
-_SCHEDULEDISPATCHER = SCHEDULEDISPATCHER:New() -- Core.Timer#SCHEDULEDISPATCHER
-
---- Declare the main database object, which is used internally by the MOOSE classes.
-_DATABASE = DATABASE:New() -- Database#DATABASE
-
-
-
-BASE:TraceOnOff( false )
+ env.info( "Moose: " .. IncludeFile .. " dynamically loaded from " .. __Moose.ProgramPath )
+ return f()
+ end
+ end
+end
+
+__Moose.ProgramPath = "Scripts/Moose/"
+
+__Moose.Includes = {}
+__Moose.Include( 'Utilities/Routines.lua' )
+__Moose.Include( 'Utilities/Utils.lua' )
+__Moose.Include( 'Core/Base.lua' )
+__Moose.Include( 'Core/Scheduler.lua' )
+__Moose.Include( 'Core/ScheduleDispatcher.lua' )
+__Moose.Include( 'Core/Event.lua' )
+__Moose.Include( 'Core/Menu.lua' )
+__Moose.Include( 'Core/Zone.lua' )
+__Moose.Include( 'Core/Database.lua' )
+__Moose.Include( 'Core/Set.lua' )
+__Moose.Include( 'Core/Point.lua' )
+__Moose.Include( 'Core/Message.lua' )
+__Moose.Include( 'Core/Fsm.lua' )
+__Moose.Include( 'Core/Radio.lua' )
+__Moose.Include( 'Wrapper/Object.lua' )
+__Moose.Include( 'Wrapper/Identifiable.lua' )
+__Moose.Include( 'Wrapper/Positionable.lua' )
+__Moose.Include( 'Wrapper/Controllable.lua' )
+__Moose.Include( 'Wrapper/Group.lua' )
+__Moose.Include( 'Wrapper/Unit.lua' )
+__Moose.Include( 'Wrapper/Client.lua' )
+__Moose.Include( 'Wrapper/Static.lua' )
+__Moose.Include( 'Wrapper/Airbase.lua' )
+__Moose.Include( 'Wrapper/Scenery.lua' )
+__Moose.Include( 'Functional/Scoring.lua' )
+__Moose.Include( 'Functional/CleanUp.lua' )
+__Moose.Include( 'Functional/Spawn.lua' )
+__Moose.Include( 'Functional/Movement.lua' )
+__Moose.Include( 'Functional/Sead.lua' )
+__Moose.Include( 'Functional/Escort.lua' )
+__Moose.Include( 'Functional/MissileTrainer.lua' )
+__Moose.Include( 'Functional/AirbasePolice.lua' )
+__Moose.Include( 'Functional/Detection.lua' )
+__Moose.Include( 'AI/AI_Balancer.lua' )
+__Moose.Include( 'AI/AI_Patrol.lua' )
+__Moose.Include( 'AI/AI_Cap.lua' )
+__Moose.Include( 'AI/AI_Cas.lua' )
+__Moose.Include( 'AI/AI_Cargo.lua' )
+__Moose.Include( 'Actions/Act_Assign.lua' )
+__Moose.Include( 'Actions/Act_Route.lua' )
+__Moose.Include( 'Actions/Act_Account.lua' )
+__Moose.Include( 'Actions/Act_Assist.lua' )
+__Moose.Include( 'Tasking/CommandCenter.lua' )
+__Moose.Include( 'Tasking/Mission.lua' )
+__Moose.Include( 'Tasking/Task.lua' )
+__Moose.Include( 'Tasking/DetectionManager.lua' )
+__Moose.Include( 'Tasking/Task_A2G_Dispatcher.lua' )
+__Moose.Include( 'Tasking/Task_A2G.lua' )
+__Moose.Include( 'Moose.lua' )
+BASE:TraceOnOff( true )
env.info( '*** MOOSE INCLUDE END *** ' )
diff --git a/Utils/lua/5.1/bin/lua.exe b/Utils/lua/5.1/bin/lua.exe
deleted file mode 100644
index d8566c515..000000000
Binary files a/Utils/lua/5.1/bin/lua.exe and /dev/null differ
diff --git a/Utils/lua/5.1/bin/lua51.dll b/Utils/lua/5.1/bin/lua51.dll
deleted file mode 100644
index 36418f3f3..000000000
Binary files a/Utils/lua/5.1/bin/lua51.dll and /dev/null differ
diff --git a/Utils/lua/5.1/bin/luac.exe b/Utils/lua/5.1/bin/luac.exe
deleted file mode 100644
index 9c64f9926..000000000
Binary files a/Utils/lua/5.1/bin/luac.exe and /dev/null differ
diff --git a/Utils/lua/5.1/bin/luadocumentor.bat b/Utils/lua/5.1/bin/luadocumentor.bat
deleted file mode 100644
index 3d449219a..000000000
--- a/Utils/lua/5.1/bin/luadocumentor.bat
+++ /dev/null
@@ -1,3 +0,0 @@
-@echo off
-"C:\Program Files\lua\5.1\bin\lua.exe" -e "package.path=\"C:\\Users\\svenv\\AppData\\Roaming/luarocks/share/lua/5.1/?.lua;C:\\Users\\svenv\\AppData\\Roaming/luarocks/share/lua/5.1/?/init.lua;c:\\program files\\lua\\5.1\\/share/lua/5.1/?.lua;c:\\program files\\lua\\5.1\\/share/lua/5.1/?/init.lua;C:\\Program Files (x86)\\LuaRocks\\lua\\?.lua;\"..package.path; package.cpath=\"C:\\Users\\svenv\\AppData\\Roaming/luarocks/lib/lua/5.1/?.dll;c:\\program files\\lua\\5.1\\/lib/lua/5.1/?.dll;\"..package.cpath" -e "local k,l,_=pcall(require,\"luarocks.loader\") _=k and l.add_context(\"luadocumentor\",\"0.1.5-1\")" "c:\program files\lua\5.1\\lib\luarocks\rocks\luadocumentor\0.1.5-1\bin\luadocumentor" %*
-exit /b %ERRORLEVEL%
diff --git a/Utils/lua/5.1/lib/liblua.a b/Utils/lua/5.1/lib/liblua.a
deleted file mode 100644
index f4d65be64..000000000
Binary files a/Utils/lua/5.1/lib/liblua.a and /dev/null differ
diff --git a/Utils/lua/5.1/lib/lua/5.1/checks.dll b/Utils/lua/5.1/lib/lua/5.1/checks.dll
deleted file mode 100644
index 98254c4f4..000000000
Binary files a/Utils/lua/5.1/lib/lua/5.1/checks.dll and /dev/null differ
diff --git a/Utils/lua/5.1/lib/lua/5.1/lfs.dll b/Utils/lua/5.1/lib/lua/5.1/lfs.dll
deleted file mode 100644
index 6e73ea2c2..000000000
Binary files a/Utils/lua/5.1/lib/lua/5.1/lfs.dll and /dev/null differ
diff --git a/Utils/lua/5.1/man/man1/lua.1 b/Utils/lua/5.1/man/man1/lua.1
deleted file mode 100644
index 24809cc6c..000000000
--- a/Utils/lua/5.1/man/man1/lua.1
+++ /dev/null
@@ -1,163 +0,0 @@
-.\" $Id: lua.man,v 1.11 2006/01/06 16:03:34 lhf Exp $
-.TH LUA 1 "$Date: 2006/01/06 16:03:34 $"
-.SH NAME
-lua \- Lua interpreter
-.SH SYNOPSIS
-.B lua
-[
-.I options
-]
-[
-.I script
-[
-.I args
-]
-]
-.SH DESCRIPTION
-.B lua
-is the stand-alone Lua interpreter.
-It loads and executes Lua programs,
-either in textual source form or
-in precompiled binary form.
-(Precompiled binaries are output by
-.BR luac ,
-the Lua compiler.)
-.B lua
-can be used as a batch interpreter and also interactively.
-.LP
-The given
-.I options
-(see below)
-are executed and then
-the Lua program in file
-.I script
-is loaded and executed.
-The given
-.I args
-are available to
-.I script
-as strings in a global table named
-.BR arg .
-If these arguments contain spaces or other characters special to the shell,
-then they should be quoted
-(but note that the quotes will be removed by the shell).
-The arguments in
-.B arg
-start at 0,
-which contains the string
-.RI ' script '.
-The index of the last argument is stored in
-.BR arg.n .
-The arguments given in the command line before
-.IR script ,
-including the name of the interpreter,
-are available in negative indices in
-.BR arg .
-.LP
-At the very start,
-before even handling the command line,
-.B lua
-executes the contents of the environment variable
-.BR LUA_INIT ,
-if it is defined.
-If the value of
-.B LUA_INIT
-is of the form
-.RI '@ filename ',
-then
-.I filename
-is executed.
-Otherwise, the string is assumed to be a Lua statement and is executed.
-.LP
-Options start with
-.B '\-'
-and are described below.
-You can use
-.B "'\--'"
-to signal the end of options.
-.LP
-If no arguments are given,
-then
-.B "\-v \-i"
-is assumed when the standard input is a terminal;
-otherwise,
-.B "\-"
-is assumed.
-.LP
-In interactive mode,
-.B lua
-prompts the user,
-reads lines from the standard input,
-and executes them as they are read.
-If a line does not contain a complete statement,
-then a secondary prompt is displayed and
-lines are read until a complete statement is formed or
-a syntax error is found.
-So, one way to interrupt the reading of an incomplete statement is
-to force a syntax error:
-adding a
-.B ';'
-in the middle of a statement is a sure way of forcing a syntax error
-(except inside multiline strings and comments; these must be closed explicitly).
-If a line starts with
-.BR '=' ,
-then
-.B lua
-displays the values of all the expressions in the remainder of the
-line. The expressions must be separated by commas.
-The primary prompt is the value of the global variable
-.BR _PROMPT ,
-if this value is a string;
-otherwise, the default prompt is used.
-Similarly, the secondary prompt is the value of the global variable
-.BR _PROMPT2 .
-So,
-to change the prompts,
-set the corresponding variable to a string of your choice.
-You can do that after calling the interpreter
-or on the command line
-(but in this case you have to be careful with quotes
-if the prompt string contains a space; otherwise you may confuse the shell.)
-The default prompts are "> " and ">> ".
-.SH OPTIONS
-.TP
-.B \-
-load and execute the standard input as a file,
-that is,
-not interactively,
-even when the standard input is a terminal.
-.TP
-.BI \-e " stat"
-execute statement
-.IR stat .
-You need to quote
-.I stat
-if it contains spaces, quotes,
-or other characters special to the shell.
-.TP
-.B \-i
-enter interactive mode after
-.I script
-is executed.
-.TP
-.BI \-l " name"
-call
-.BI require(' name ')
-before executing
-.IR script .
-Typically used to load libraries.
-.TP
-.B \-v
-show version information.
-.SH "SEE ALSO"
-.BR luac (1)
-.br
-http://www.lua.org/
-.SH DIAGNOSTICS
-Error messages should be self explanatory.
-.SH AUTHORS
-R. Ierusalimschy,
-L. H. de Figueiredo,
-and
-W. Celes
-.\" EOF
diff --git a/Utils/lua/5.1/man/man1/luac.1 b/Utils/lua/5.1/man/man1/luac.1
deleted file mode 100644
index d8146782d..000000000
--- a/Utils/lua/5.1/man/man1/luac.1
+++ /dev/null
@@ -1,136 +0,0 @@
-.\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $
-.TH LUAC 1 "$Date: 2006/01/06 16:03:34 $"
-.SH NAME
-luac \- Lua compiler
-.SH SYNOPSIS
-.B luac
-[
-.I options
-] [
-.I filenames
-]
-.SH DESCRIPTION
-.B luac
-is the Lua compiler.
-It translates programs written in the Lua programming language
-into binary files that can be later loaded and executed.
-.LP
-The main advantages of precompiling chunks are:
-faster loading,
-protecting source code from accidental user changes,
-and
-off-line syntax checking.
-.LP
-Pre-compiling does not imply faster execution
-because in Lua chunks are always compiled into bytecodes before being executed.
-.B luac
-simply allows those bytecodes to be saved in a file for later execution.
-.LP
-Pre-compiled chunks are not necessarily smaller than the corresponding source.
-The main goal in pre-compiling is faster loading.
-.LP
-The binary files created by
-.B luac
-are portable only among architectures with the same word size and byte order.
-.LP
-.B luac
-produces a single output file containing the bytecodes
-for all source files given.
-By default,
-the output file is named
-.BR luac.out ,
-but you can change this with the
-.B \-o
-option.
-.LP
-In the command line,
-you can mix
-text files containing Lua source and
-binary files containing precompiled chunks.
-This is useful to combine several precompiled chunks,
-even from different (but compatible) platforms,
-into a single precompiled chunk.
-.LP
-You can use
-.B "'\-'"
-to indicate the standard input as a source file
-and
-.B "'\--'"
-to signal the end of options
-(that is,
-all remaining arguments will be treated as files even if they start with
-.BR "'\-'" ).
-.LP
-The internal format of the binary files produced by
-.B luac
-is likely to change when a new version of Lua is released.
-So,
-save the source files of all Lua programs that you precompile.
-.LP
-.SH OPTIONS
-Options must be separate.
-.TP
-.B \-l
-produce a listing of the compiled bytecode for Lua's virtual machine.
-Listing bytecodes is useful to learn about Lua's virtual machine.
-If no files are given, then
-.B luac
-loads
-.B luac.out
-and lists its contents.
-.TP
-.BI \-o " file"
-output to
-.IR file ,
-instead of the default
-.BR luac.out .
-(You can use
-.B "'\-'"
-for standard output,
-but not on platforms that open standard output in text mode.)
-The output file may be a source file because
-all files are loaded before the output file is written.
-Be careful not to overwrite precious files.
-.TP
-.B \-p
-load files but do not generate any output file.
-Used mainly for syntax checking and for testing precompiled chunks:
-corrupted files will probably generate errors when loaded.
-Lua always performs a thorough integrity test on precompiled chunks.
-Bytecode that passes this test is completely safe,
-in the sense that it will not break the interpreter.
-However,
-there is no guarantee that such code does anything sensible.
-(None can be given, because the halting problem is unsolvable.)
-If no files are given, then
-.B luac
-loads
-.B luac.out
-and tests its contents.
-No messages are displayed if the file passes the integrity test.
-.TP
-.B \-s
-strip debug information before writing the output file.
-This saves some space in very large chunks,
-but if errors occur when running a stripped chunk,
-then the error messages may not contain the full information they usually do.
-For instance,
-line numbers and names of local variables are lost.
-.TP
-.B \-v
-show version information.
-.SH FILES
-.TP 15
-.B luac.out
-default output file
-.SH "SEE ALSO"
-.BR lua (1)
-.br
-http://www.lua.org/
-.SH DIAGNOSTICS
-Error messages should be self explanatory.
-.SH AUTHORS
-L. H. de Figueiredo,
-R. Ierusalimschy and
-W. Celes
-.\" EOF
diff --git a/Utils/luadocumentor.bat b/Utils/luadocumentor.bat
new file mode 100644
index 000000000..8f28e39a8
--- /dev/null
+++ b/Utils/luadocumentor.bat
@@ -0,0 +1,3 @@
+@echo off
+"./luarocks/lua5.1" -e "package.path=\"./luarocks/systree/share/lua/5.1/?.lua;./luarocks/systree/share/lua/5.1/?/init.lua;./luarocks/systree/share/lua/5.1/?.lua;./luarocks/systree/share/lua/5.1/?/init.lua;./luarocks/lua/?.lua;\"..package.path; package.cpath=\"./luarocks/lib/lua/5.1/?.dll;./luarocks/systree/lib/lua/5.1/?.dll;\"..package.cpath" -e "local k,l,_=pcall(require,\"luarocks.loader\") _=k and l.add_context(\"luadocumentor\",\"0.1.5-1\")" "./luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/bin/luadocumentor" -f doc -d "../docs/Documentation" -s "../docs/Stylesheet/stylesheet.css" "../Moose Development/Moose" %*
+exit /b %ERRORLEVEL%
\ No newline at end of file
diff --git a/Utils/luarocks/Microsoft.VC80.CRT.manifest b/Utils/luarocks/Microsoft.VC80.CRT.manifest
new file mode 100644
index 000000000..6a8a0e239
--- /dev/null
+++ b/Utils/luarocks/Microsoft.VC80.CRT.manifest
@@ -0,0 +1,8 @@
+
+
+
+
+ n9On8FItNsK/DmT8UQxu6jYDtWQ=
+ 0KJ/VTwP4OUHx98HlIW2AdW1kuY=
+ YJuB+9Os2oxW4mY+2oC/r8lICZE=
+
\ No newline at end of file
diff --git a/Utils/luarocks/bin2c5.1.exe b/Utils/luarocks/bin2c5.1.exe
new file mode 100644
index 000000000..ca81d4b61
Binary files /dev/null and b/Utils/luarocks/bin2c5.1.exe differ
diff --git a/Utils/luarocks/config-5.1.lua b/Utils/luarocks/config-5.1.lua
new file mode 100644
index 000000000..cf02193f5
--- /dev/null
+++ b/Utils/luarocks/config-5.1.lua
@@ -0,0 +1,14 @@
+rocks_trees = {
+ home..[[/luarocks]],
+ { name = [[user]],
+ root = home..[[/luarocks]],
+ },
+ { name = [[system]],
+ root = [[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\systree]],
+ },
+}
+variables = {
+ MSVCRT = 'MSVCR80',
+ LUALIB = 'lua5.1.lib'
+}
+verbose = false -- set to 'true' to enable verbose output
diff --git a/Utils/lua/5.1/include/lauxlib.h b/Utils/luarocks/include/lauxlib.h
similarity index 100%
rename from Utils/lua/5.1/include/lauxlib.h
rename to Utils/luarocks/include/lauxlib.h
diff --git a/Utils/lua/5.1/include/lua.h b/Utils/luarocks/include/lua.h
similarity index 98%
rename from Utils/lua/5.1/include/lua.h
rename to Utils/luarocks/include/lua.h
index a4b73e743..e4bdfd3b9 100644
--- a/Utils/lua/5.1/include/lua.h
+++ b/Utils/luarocks/include/lua.h
@@ -1,5 +1,5 @@
/*
-** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $
+** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $
** Lua - An Extensible Extension Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@@ -17,9 +17,9 @@
#define LUA_VERSION "Lua 5.1"
-#define LUA_RELEASE "Lua 5.1.5"
+#define LUA_RELEASE "Lua 5.1.4"
#define LUA_VERSION_NUM 501
-#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio"
+#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
@@ -362,7 +362,7 @@ struct lua_Debug {
/******************************************************************************
-* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved.
+* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
diff --git a/Utils/lua/5.1/include/lua.hpp b/Utils/luarocks/include/lua.hpp
similarity index 100%
rename from Utils/lua/5.1/include/lua.hpp
rename to Utils/luarocks/include/lua.hpp
diff --git a/Utils/lua/5.1/include/luaconf.h b/Utils/luarocks/include/luaconf.h
similarity index 98%
rename from Utils/lua/5.1/include/luaconf.h
rename to Utils/luarocks/include/luaconf.h
index e2cb26163..5e7b98be6 100644
--- a/Utils/lua/5.1/include/luaconf.h
+++ b/Utils/luarocks/include/luaconf.h
@@ -91,7 +91,7 @@
".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
#define LUA_CPATH_DEFAULT \
- ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
+ ".\\?.dll;" ".\\?51.dll;" LUA_CDIR"?.dll;" LUA_CDIR"?51.dll;" LUA_CDIR"clibs\\?.dll;" LUA_CDIR"clibs\\?51.dll;" LUA_CDIR"loadall.dll;" LUA_CDIR"clibs\\loadall.dll"
#else
#define LUA_ROOT "/usr/local/"
@@ -101,7 +101,7 @@
"./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \
LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua"
#define LUA_CPATH_DEFAULT \
- "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so"
+ "./?.so;" "./lib?51.so;" LUA_CDIR"?.so;" LUA_CDIR"lib?51.so;" LUA_CDIR"loadall.so"
#endif
diff --git a/Utils/lua/5.1/include/lualib.h b/Utils/luarocks/include/lualib.h
similarity index 100%
rename from Utils/lua/5.1/include/lualib.h
rename to Utils/luarocks/include/lualib.h
diff --git a/Utils/luarocks/lua.ico b/Utils/luarocks/lua.ico
new file mode 100644
index 000000000..56dc4fe15
Binary files /dev/null and b/Utils/luarocks/lua.ico differ
diff --git a/Utils/luarocks/lua/luarocks/add.lua b/Utils/luarocks/lua/luarocks/add.lua
new file mode 100644
index 000000000..f37d334d3
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/add.lua
@@ -0,0 +1,122 @@
+
+--- Module implementing the luarocks-admin "add" command.
+-- Adds a rock or rockspec to a rocks server.
+local add = {}
+package.loaded["luarocks.add"] = add
+
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local dir = require("luarocks.dir")
+local manif = require("luarocks.manif")
+local index = require("luarocks.index")
+local fs = require("luarocks.fs")
+local cache = require("luarocks.cache")
+
+util.add_run_function(add)
+add.help_summary = "Add a rock or rockspec to a rocks server."
+add.help_arguments = "[--server=] [--no-refresh] {|...}"
+add.help = [[
+Arguments are local files, which may be rockspecs or rocks.
+The flag --server indicates which server to use.
+If not given, the default server set in the upload_server variable
+from the configuration file is used instead.
+The flag --no-refresh indicates the local cache should not be refreshed
+prior to generation of the updated manifest.
+]]
+
+local function add_files_to_server(refresh, rockfiles, server, upload_server)
+ assert(type(refresh) == "boolean" or not refresh)
+ assert(type(rockfiles) == "table")
+ assert(type(server) == "string")
+ assert(type(upload_server) == "table" or not upload_server)
+
+ local download_url, login_url = cache.get_server_urls(server, upload_server)
+ local at = fs.current_dir()
+ local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url
+
+ local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password)
+ if not local_cache then
+ return nil, protocol
+ end
+ if protocol == "file" then
+ return nil, "Server "..server.." is not recognized, check your configuration."
+ end
+
+ if not login_url then
+ login_url = protocol.."://"..server_path
+ end
+
+ local ok, err = fs.change_dir(at)
+ if not ok then return nil, err end
+
+ local files = {}
+ for _, rockfile in ipairs(rockfiles) do
+ if fs.exists(rockfile) then
+ util.printout("Copying file "..rockfile.." to "..local_cache.."...")
+ local absolute = fs.absolute_name(rockfile)
+ fs.copy(absolute, local_cache, cfg.perm_read)
+ table.insert(files, dir.base_name(absolute))
+ else
+ util.printerr("File "..rockfile.." not found")
+ end
+ end
+ if #files == 0 then
+ return nil, "No files found"
+ end
+
+ local ok, err = fs.change_dir(local_cache)
+ if not ok then return nil, err end
+
+ util.printout("Updating manifest...")
+ manif.make_manifest(local_cache, "one", true)
+
+ manif.zip_manifests()
+
+ util.printout("Updating index.html...")
+ index.make_index(local_cache)
+
+ local login_info = ""
+ if user then login_info = " -u "..user end
+ if password then login_info = login_info..":"..password end
+ if not login_url:match("/$") then
+ login_url = login_url .. "/"
+ end
+
+ table.insert(files, "index.html")
+ table.insert(files, "manifest")
+ for ver in util.lua_versions() do
+ table.insert(files, "manifest-"..ver)
+ table.insert(files, "manifest-"..ver..".zip")
+ end
+
+ -- TODO abstract away explicit 'curl' call
+
+ local cmd
+ if protocol == "rsync" then
+ local srv, path = server_path:match("([^/]+)(/.+)")
+ cmd = cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/"
+ elseif upload_server and upload_server.sftp then
+ local part1, part2 = upload_server.sftp:match("^([^/]*)/(.*)$")
+ cmd = cfg.variables.SCP.." "..table.concat(files, " ").." "..user.."@"..part1..":/"..part2
+ else
+ cmd = cfg.variables.CURL.." "..login_info.." -T '{"..table.concat(files, ",").."}' "..login_url
+ end
+
+ util.printout(cmd)
+ fs.execute(cmd)
+
+ return true
+end
+
+function add.command(flags, ...)
+ local files = {...}
+ if #files < 1 then
+ return nil, "Argument missing. "..util.see_help("add", "luarocks-admin")
+ end
+ local server, server_table = cache.get_upload_server(flags["server"])
+ if not server then return nil, server_table end
+ return add_files_to_server(not flags["no-refresh"], files, server, server_table)
+end
+
+
+return add
diff --git a/Utils/luarocks/lua/luarocks/admin_remove.lua b/Utils/luarocks/lua/luarocks/admin_remove.lua
new file mode 100644
index 000000000..621f13178
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/admin_remove.lua
@@ -0,0 +1,92 @@
+
+--- Module implementing the luarocks-admin "remove" command.
+-- Removes a rock or rockspec from a rocks server.
+local admin_remove = {}
+package.loaded["luarocks.admin_remove"] = admin_remove
+
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local dir = require("luarocks.dir")
+local manif = require("luarocks.manif")
+local index = require("luarocks.index")
+local fs = require("luarocks.fs")
+local cache = require("luarocks.cache")
+
+util.add_run_function(admin_remove)
+admin_remove.help_summary = "Remove a rock or rockspec from a rocks server."
+admin_remove.help_arguments = "[--server=] [--no-refresh] {|...}"
+admin_remove.help = [[
+Arguments are local files, which may be rockspecs or rocks.
+The flag --server indicates which server to use.
+If not given, the default server set in the upload_server variable
+from the configuration file is used instead.
+The flag --no-refresh indicates the local cache should not be refreshed
+prior to generation of the updated manifest.
+]]
+
+local function remove_files_from_server(refresh, rockfiles, server, upload_server)
+ assert(type(refresh) == "boolean" or not refresh)
+ assert(type(rockfiles) == "table")
+ assert(type(server) == "string")
+ assert(type(upload_server) == "table" or not upload_server)
+
+ local download_url, login_url = cache.get_server_urls(server, upload_server)
+ local at = fs.current_dir()
+ local refresh_fn = refresh and cache.refresh_local_cache or cache.split_server_url
+
+ local local_cache, protocol, server_path, user, password = refresh_fn(server, download_url, cfg.upload_user, cfg.upload_password)
+ if not local_cache then
+ return nil, protocol
+ end
+ if protocol ~= "rsync" then
+ return nil, "This command requires 'rsync', check your configuration."
+ end
+
+ local ok, err = fs.change_dir(at)
+ if not ok then return nil, err end
+
+ local nr_files = 0
+ for _, rockfile in ipairs(rockfiles) do
+ local basename = dir.base_name(rockfile)
+ local file = dir.path(local_cache, basename)
+ util.printout("Removing file "..file.."...")
+ fs.delete(file)
+ if not fs.exists(file) then
+ nr_files = nr_files + 1
+ else
+ util.printerr("Failed removing "..file)
+ end
+ end
+ if nr_files == 0 then
+ return nil, "No files removed."
+ end
+
+ local ok, err = fs.change_dir(local_cache)
+ if not ok then return nil, err end
+
+ util.printout("Updating manifest...")
+ manif.make_manifest(local_cache, "one", true)
+ util.printout("Updating index.html...")
+ index.make_index(local_cache)
+
+ local srv, path = server_path:match("([^/]+)(/.+)")
+ local cmd = cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." --delete -e ssh "..local_cache.."/ "..user.."@"..srv..":"..path.."/"
+
+ util.printout(cmd)
+ fs.execute(cmd)
+
+ return true
+end
+
+function admin_remove.command(flags, ...)
+ local files = {...}
+ if #files < 1 then
+ return nil, "Argument missing. "..util.see_help("remove", "luarocks-admin")
+ end
+ local server, server_table = cache.get_upload_server(flags["server"])
+ if not server then return nil, server_table end
+ return remove_files_from_server(not flags["no-refresh"], files, server, server_table)
+end
+
+
+return admin_remove
diff --git a/Utils/luarocks/lua/luarocks/build.lua b/Utils/luarocks/lua/luarocks/build.lua
new file mode 100644
index 000000000..96b232ffb
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/build.lua
@@ -0,0 +1,415 @@
+
+--- Module implementing the LuaRocks "build" command.
+-- Builds a rock, compiling its C parts if any.
+local build = {}
+package.loaded["luarocks.build"] = build
+
+local pack = require("luarocks.pack")
+local path = require("luarocks.path")
+local util = require("luarocks.util")
+local repos = require("luarocks.repos")
+local fetch = require("luarocks.fetch")
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local deps = require("luarocks.deps")
+local manif = require("luarocks.manif")
+local remove = require("luarocks.remove")
+local cfg = require("luarocks.cfg")
+
+util.add_run_function(build)
+build.help_summary = "Build/compile a rock."
+build.help_arguments = "[--pack-binary-rock] [--keep] {|| []}"
+build.help = [[
+Build and install a rock, compiling its C parts if any.
+Argument may be a rockspec file, a source rock file
+or the name of a rock to be fetched from a repository.
+
+--pack-binary-rock Do not install rock. Instead, produce a .rock file
+ with the contents of compilation in the current
+ directory.
+
+--keep Do not remove previously installed versions of the
+ rock after building a new one. This behavior can
+ be made permanent by setting keep_other_versions=true
+ in the configuration file.
+
+--branch= Override the `source.branch` field in the loaded
+ rockspec. Allows to specify a different branch to
+ fetch. Particularly for SCM rocks.
+
+--only-deps Installs only the dependencies of the rock.
+
+]]..util.deps_mode_help()
+
+--- Install files to a given location.
+-- Takes a table where the array part is a list of filenames to be copied.
+-- In the hash part, other keys, if is_module_path is set, are identifiers
+-- in Lua module format, to indicate which subdirectory the file should be
+-- copied to. For example, install_files({["foo.bar"] = "src/bar.lua"}, "boo")
+-- will copy src/bar.lua to boo/foo.
+-- @param files table or nil: A table containing a list of files to copy in
+-- the format described above. If nil is passed, this function is a no-op.
+-- Directories should be delimited by forward slashes as in internet URLs.
+-- @param location string: The base directory files should be copied to.
+-- @param is_module_path boolean: True if string keys in files should be
+-- interpreted as dotted module paths.
+-- @param perms string: Permissions of the newly created files installed.
+-- Directories are always created with the default permissions.
+-- @return boolean or (nil, string): True if succeeded or
+-- nil and an error message.
+local function install_files(files, location, is_module_path, perms)
+ assert(type(files) == "table" or not files)
+ assert(type(location) == "string")
+ if files then
+ for k, file in pairs(files) do
+ local dest = location
+ local filename = dir.base_name(file)
+ if type(k) == "string" then
+ local modname = k
+ if is_module_path then
+ dest = dir.path(location, path.module_to_path(modname))
+ local ok, err = fs.make_dir(dest)
+ if not ok then return nil, err end
+ if filename:match("%.lua$") then
+ local basename = modname:match("([^.]+)$")
+ filename = basename..".lua"
+ end
+ else
+ dest = dir.path(location, dir.dir_name(modname))
+ local ok, err = fs.make_dir(dest)
+ if not ok then return nil, err end
+ filename = dir.base_name(modname)
+ end
+ else
+ local ok, err = fs.make_dir(dest)
+ if not ok then return nil, err end
+ end
+ local ok = fs.copy(dir.path(file), dir.path(dest, filename), perms)
+ if not ok then
+ return nil, "Failed copying "..file
+ end
+ end
+ end
+ return true
+end
+
+--- Write to the current directory the contents of a table,
+-- where each key is a file name and its value is the file content.
+-- @param files table: The table of files to be written.
+local function extract_from_rockspec(files)
+ for name, content in pairs(files) do
+ local fd = io.open(dir.path(fs.current_dir(), name), "w+")
+ fd:write(content)
+ fd:close()
+ end
+end
+
+--- Applies patches inlined in the build.patches section
+-- and extracts files inlined in the build.extra_files section
+-- of a rockspec.
+-- @param rockspec table: A rockspec table.
+-- @return boolean or (nil, string): True if succeeded or
+-- nil and an error message.
+function build.apply_patches(rockspec)
+ assert(type(rockspec) == "table")
+
+ local build_spec = rockspec.build
+ if build_spec.extra_files then
+ extract_from_rockspec(build_spec.extra_files)
+ end
+ if build_spec.patches then
+ extract_from_rockspec(build_spec.patches)
+ for patch, patchdata in util.sortedpairs(build_spec.patches) do
+ util.printout("Applying patch "..patch.."...")
+ local ok, err = fs.apply_patch(tostring(patch), patchdata)
+ if not ok then
+ return nil, "Failed applying patch "..patch
+ end
+ end
+ end
+ return true
+end
+
+local function install_default_docs(name, version)
+ local patterns = { "readme", "license", "copying", ".*%.md" }
+ local dest = dir.path(path.install_dir(name, version), "doc")
+ local has_dir = false
+ for file in fs.dir() do
+ for _, pattern in ipairs(patterns) do
+ if file:lower():match("^"..pattern) then
+ if not has_dir then
+ fs.make_dir(dest)
+ has_dir = true
+ end
+ fs.copy(file, dest, cfg.perm_read)
+ break
+ end
+ end
+ end
+end
+
+--- Build and install a rock given a rockspec.
+-- @param rockspec_file string: local or remote filename of a rockspec.
+-- @param need_to_fetch boolean: true if sources need to be fetched,
+-- false if the rockspec was obtained from inside a source rock.
+-- @param minimal_mode boolean: true if there's no need to fetch,
+-- unpack or change dir (this is used by "luarocks make"). Implies
+-- need_to_fetch = false.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for no trees.
+-- @param build_only_deps boolean: true to build the listed dependencies only.
+-- @return (string, string) or (nil, string, [string]): Name and version of
+-- installed rock if succeeded or nil and an error message followed by an error code.
+function build.build_rockspec(rockspec_file, need_to_fetch, minimal_mode, deps_mode, build_only_deps)
+ assert(type(rockspec_file) == "string")
+ assert(type(need_to_fetch) == "boolean")
+
+ local rockspec, err, errcode = fetch.load_rockspec(rockspec_file)
+ if err then
+ return nil, err, errcode
+ elseif not rockspec.build then
+ return nil, "Rockspec error: build table not specified"
+ elseif not rockspec.build.type then
+ return nil, "Rockspec error: build type not specified"
+ end
+
+ local ok
+ if not build_only_deps then
+ ok, err, errcode = deps.check_external_deps(rockspec, "build")
+ if err then
+ return nil, err, errcode
+ end
+ end
+
+ if deps_mode == "none" then
+ util.printerr("Warning: skipping dependency checks.")
+ else
+ local ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
+ if err then
+ return nil, err, errcode
+ end
+ end
+
+ local name, version = rockspec.name, rockspec.version
+ if build_only_deps then
+ util.printout("Stopping after installing dependencies for " ..name.." "..version)
+ util.printout()
+ return name, version
+ end
+
+ if repos.is_installed(name, version) then
+ repos.delete_version(name, version, deps_mode)
+ end
+
+ if not minimal_mode then
+ local source_dir
+ if need_to_fetch then
+ ok, source_dir, errcode = fetch.fetch_sources(rockspec, true)
+ if not ok then
+ return nil, source_dir, errcode
+ end
+ local ok, err = fs.change_dir(source_dir)
+ if not ok then return nil, err end
+ elseif rockspec.source.file then
+ local ok, err = fs.unpack_archive(rockspec.source.file)
+ if not ok then
+ return nil, err
+ end
+ end
+ fs.change_dir(rockspec.source.dir)
+ end
+
+ local dirs = {
+ lua = { name = path.lua_dir(name, version), is_module_path = true, perms = cfg.perm_read },
+ lib = { name = path.lib_dir(name, version), is_module_path = true, perms = cfg.perm_exec },
+ conf = { name = path.conf_dir(name, version), is_module_path = false, perms = cfg.perm_read },
+ bin = { name = path.bin_dir(name, version), is_module_path = false, perms = cfg.perm_exec },
+ }
+
+ for _, d in pairs(dirs) do
+ local ok, err = fs.make_dir(d.name)
+ if not ok then return nil, err end
+ end
+ local rollback = util.schedule_function(function()
+ fs.delete(path.install_dir(name, version))
+ fs.remove_dir_if_empty(path.versions_dir(name))
+ end)
+
+ local build_spec = rockspec.build
+
+ if not minimal_mode then
+ ok, err = build.apply_patches(rockspec)
+ if err then
+ return nil, err
+ end
+ end
+
+ if build_spec.type ~= "none" then
+
+ -- Temporary compatibility
+ if build_spec.type == "module" then
+ util.printout("Do not use 'module' as a build type. Use 'builtin' instead.")
+ build_spec.type = "builtin"
+ end
+
+ if cfg.accepted_build_types and util.array_contains(cfg.accepted_build_types, build_spec.type) then
+ return nil, "This rockspec uses the '"..build_spec.type.."' build type, which is blocked by the 'accepted_build_types' setting in your LuaRocks configuration."
+ end
+
+ local build_type
+ ok, build_type = pcall(require, "luarocks.build." .. build_spec.type)
+ if not ok or not type(build_type) == "table" then
+ return nil, "Failed initializing build back-end for build type '"..build_spec.type.."': "..build_type
+ end
+
+ ok, err = build_type.run(rockspec)
+ if not ok then
+ return nil, "Build error: " .. err
+ end
+ end
+
+ if build_spec.install then
+ for id, install_dir in pairs(dirs) do
+ ok, err = install_files(build_spec.install[id], install_dir.name, install_dir.is_module_path, install_dir.perms)
+ if not ok then
+ return nil, err
+ end
+ end
+ end
+
+ local copy_directories = build_spec.copy_directories
+ local copying_default = false
+ if not copy_directories then
+ copy_directories = {"doc"}
+ copying_default = true
+ end
+
+ local any_docs = false
+ for _, copy_dir in pairs(copy_directories) do
+ if fs.is_dir(copy_dir) then
+ local dest = dir.path(path.install_dir(name, version), copy_dir)
+ fs.make_dir(dest)
+ fs.copy_contents(copy_dir, dest)
+ any_docs = true
+ else
+ if not copying_default then
+ return nil, "Directory '"..copy_dir.."' not found"
+ end
+ end
+ end
+
+ if not any_docs then
+ install_default_docs(name, version)
+ end
+
+ for _, d in pairs(dirs) do
+ fs.remove_dir_if_empty(d.name)
+ end
+
+ fs.pop_dir()
+
+ fs.copy(rockspec.local_filename, path.rockspec_file(name, version), cfg.perm_read)
+ if need_to_fetch then
+ fs.pop_dir()
+ end
+
+ ok, err = manif.make_rock_manifest(name, version)
+ if err then return nil, err end
+
+ ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), deps_mode)
+ if err then return nil, err end
+
+ util.remove_scheduled_function(rollback)
+ rollback = util.schedule_function(function()
+ repos.delete_version(name, version, deps_mode)
+ end)
+
+ ok, err = repos.run_hook(rockspec, "post_install")
+ if err then return nil, err end
+
+ util.announce_install(rockspec)
+ util.remove_scheduled_function(rollback)
+ return name, version
+end
+
+--- Build and install a rock.
+-- @param rock_file string: local or remote filename of a rock.
+-- @param need_to_fetch boolean: true if sources need to be fetched,
+-- false if the rockspec was obtained from inside a source rock.
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+-- @param build_only_deps boolean: true to build the listed dependencies only.
+-- @return boolean or (nil, string, [string]): True if build was successful,
+-- or false and an error message and an optional error code.
+function build.build_rock(rock_file, need_to_fetch, deps_mode, build_only_deps)
+ assert(type(rock_file) == "string")
+ assert(type(need_to_fetch) == "boolean")
+
+ local ok, err, errcode
+ local unpack_dir
+ unpack_dir, err, errcode = fetch.fetch_and_unpack_rock(rock_file)
+ if not unpack_dir then
+ return nil, err, errcode
+ end
+ local rockspec_file = path.rockspec_name_from_rock(rock_file)
+ ok, err = fs.change_dir(unpack_dir)
+ if not ok then return nil, err end
+ ok, err, errcode = build.build_rockspec(rockspec_file, need_to_fetch, false, deps_mode, build_only_deps)
+ fs.pop_dir()
+ return ok, err, errcode
+end
+
+local function do_build(name, version, deps_mode, build_only_deps)
+ if name:match("%.rockspec$") then
+ return build.build_rockspec(name, true, false, deps_mode, build_only_deps)
+ elseif name:match("%.src%.rock$") then
+ return build.build_rock(name, false, deps_mode, build_only_deps)
+ elseif name:match("%.all%.rock$") then
+ local install = require("luarocks.install")
+ local install_fun = build_only_deps and install.install_binary_rock_deps or install.install_binary_rock
+ return install_fun(name, deps_mode)
+ elseif name:match("%.rock$") then
+ return build.build_rock(name, true, deps_mode, build_only_deps)
+ elseif not name:match(dir.separator) then
+ local search = require("luarocks.search")
+ return search.act_on_src_or_rockspec(do_build, name:lower(), version, nil, deps_mode, build_only_deps)
+ end
+ return nil, "Don't know what to do with "..name
+end
+
+--- Driver function for "build" command.
+-- @param name string: A local or remote rockspec or rock file.
+-- If a package name is given, forwards the request to "search" and,
+-- if returned a result, installs the matching rock.
+-- @param version string: When passing a package name, a version number may
+-- also be given.
+-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an
+-- error message otherwise. exitcode is optionally returned.
+function build.command(flags, name, version)
+ if type(name) ~= "string" then
+ return nil, "Argument missing. "..util.see_help("build")
+ end
+ assert(type(version) == "string" or not version)
+
+ if flags["pack-binary-rock"] then
+ return pack.pack_binary_rock(name, version, do_build, name, version, deps.get_deps_mode(flags))
+ else
+ local ok, err = fs.check_command_permissions(flags)
+ if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end
+ ok, err = do_build(name, version, deps.get_deps_mode(flags), flags["only-deps"])
+ if not ok then return nil, err end
+ name, version = ok, err
+
+ if (not flags["only-deps"]) and (not flags["keep"]) and not cfg.keep_other_versions then
+ local ok, err = remove.remove_other_versions(name, version, flags["force"], flags["force-fast"])
+ if not ok then util.printerr(err) end
+ end
+
+ manif.check_dependencies(nil, deps.get_deps_mode(flags))
+ return name, version
+ end
+end
+
+return build
diff --git a/Utils/luarocks/lua/luarocks/cache.lua b/Utils/luarocks/lua/luarocks/cache.lua
new file mode 100644
index 000000000..4a95f70e3
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/cache.lua
@@ -0,0 +1,78 @@
+
+--- Module handling the LuaRocks local cache.
+-- Adds a rock or rockspec to a rocks server.
+local cache = {}
+package.loaded["luarocks.cache"] = cache
+
+local fs = require("luarocks.fs")
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+function cache.get_upload_server(server)
+ if not server then server = cfg.upload_server end
+ if not server then
+ return nil, "No server specified and no default configured with upload_server."
+ end
+ return server, cfg.upload_servers and cfg.upload_servers[server]
+end
+
+function cache.get_server_urls(server, upload_server)
+ local download_url = server
+ local login_url = nil
+ if upload_server then
+ if upload_server.rsync then download_url = "rsync://"..upload_server.rsync
+ elseif upload_server.http then download_url = "http://"..upload_server.http
+ elseif upload_server.ftp then download_url = "ftp://"..upload_server.ftp
+ end
+
+ if upload_server.ftp then login_url = "ftp://"..upload_server.ftp
+ elseif upload_server.sftp then login_url = "sftp://"..upload_server.sftp
+ end
+ end
+ return download_url, login_url
+end
+
+function cache.split_server_url(server, url, user, password)
+ local protocol, server_path = dir.split_url(url)
+ if server_path:match("@") then
+ local credentials
+ credentials, server_path = server_path:match("([^@]*)@(.*)")
+ if credentials:match(":") then
+ user, password = credentials:match("([^:]*):(.*)")
+ else
+ user = credentials
+ end
+ end
+ local local_cache = cfg.local_cache .. "/" .. server
+ return local_cache, protocol, server_path, user, password
+end
+
+function cache.refresh_local_cache(server, url, user, password)
+ local local_cache, protocol, server_path, user, password = cache.split_server_url(server, url, user, password)
+ local ok, err = fs.make_dir(local_cache)
+ if not ok then
+ return nil, "Failed creating local cache dir: "..err
+ end
+ fs.change_dir(local_cache)
+ if not ok then return nil, err end
+ util.printout("Refreshing cache "..local_cache.."...")
+
+ -- TODO abstract away explicit 'wget' call
+ local ok = false
+ if protocol == "rsync" then
+ local srv, path = server_path:match("([^/]+)(/.+)")
+ ok = fs.execute(cfg.variables.RSYNC.." "..cfg.variables.RSYNCFLAGS.." -e ssh "..user.."@"..srv..":"..path.."/ "..local_cache.."/")
+ else
+ local login_info = ""
+ if user then login_info = " --user="..user end
+ if password then login_info = login_info .. " --password="..password end
+ ok = fs.execute(cfg.variables.WGET.." --no-cache -q -m -np -nd "..protocol.."://"..server_path..login_info)
+ end
+ if not ok then
+ return nil, "Failed downloading cache."
+ end
+ return local_cache, protocol, server_path, user, password
+end
+
+return cache
diff --git a/Utils/luarocks/lua/luarocks/cfg.lua b/Utils/luarocks/lua/luarocks/cfg.lua
new file mode 100644
index 000000000..c997c1240
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/cfg.lua
@@ -0,0 +1,760 @@
+--- Configuration for LuaRocks.
+-- Tries to load the user's configuration file and
+-- defines defaults for unset values. See the
+-- config
+-- file format documentation for details.
+--
+-- End-users shouldn't edit this file. They can override any defaults
+-- set in this file using their system-wide or user-specific configuration
+-- files. Run `luarocks` with no arguments to see the locations of
+-- these files in your platform.
+
+local rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION =
+ rawset, next, table, pairs, require, io, os, setmetatable, pcall, ipairs, package, tonumber, type, assert, _VERSION
+
+--module("luarocks.cfg")
+local cfg = {}
+package.loaded["luarocks.cfg"] = cfg
+
+local util = require("luarocks.util")
+
+cfg.lua_version = _VERSION:match(" (5%.[123])$") or "5.1"
+local version_suffix = cfg.lua_version:gsub("%.", "_")
+
+-- Load site-local global configurations
+local ok, site_config = pcall(require, "luarocks.site_config_"..version_suffix)
+if not ok then
+ ok, site_config = pcall(require, "luarocks.site_config")
+end
+if not ok then
+ io.stderr:write("Site-local luarocks/site_config.lua file not found. Incomplete installation?\n")
+ site_config = {}
+end
+
+cfg.program_version = "2.4.2"
+cfg.program_series = "2.4"
+cfg.major_version = (cfg.program_version:match("([^.]%.[^.])")) or cfg.program_series
+cfg.variables = {}
+cfg.rocks_trees = {}
+cfg.platforms = {}
+
+local persist = require("luarocks.persist")
+
+cfg.errorcodes = setmetatable({
+ OK = 0,
+ UNSPECIFIED = 1,
+ PERMISSIONDENIED = 2,
+ CONFIGFILE = 3,
+ CRASH = 99
+},{
+ __index = function(t, key)
+ local val = rawget(t, key)
+ if not val then
+ error("'"..tostring(key).."' is not a valid errorcode", 2)
+ end
+ return val
+ end
+})
+
+
+local popen_ok, popen_result = pcall(io.popen, "")
+if popen_ok then
+ if popen_result then
+ popen_result:close()
+ end
+else
+ io.stderr:write("Your version of Lua does not support io.popen,\n")
+ io.stderr:write("which is required by LuaRocks. Please check your Lua installation.\n")
+ os.exit(cfg.errorcodes.UNSPECIFIED)
+end
+
+-- System detection:
+
+-- A proper installation of LuaRocks will hardcode the system
+-- and proc values with site_config.LUAROCKS_UNAME_S and site_config.LUAROCKS_UNAME_M,
+-- so that this detection does not run every time. When it is
+-- performed, we use the Unix way to identify the system,
+-- even on Windows (assuming UnxUtils or Cygwin).
+local system = site_config.LUAROCKS_UNAME_S or io.popen("uname -s"):read("*l")
+local proc = site_config.LUAROCKS_UNAME_M or io.popen("uname -m"):read("*l")
+if proc:match("i[%d]86") then
+ cfg.target_cpu = "x86"
+elseif proc:match("amd64") or proc:match("x86_64") then
+ cfg.target_cpu = "x86_64"
+elseif proc:match("Power Macintosh") then
+ cfg.target_cpu = "powerpc"
+ else
+ cfg.target_cpu = proc
+end
+
+if system == "FreeBSD" then
+ cfg.platforms.unix = true
+ cfg.platforms.freebsd = true
+ cfg.platforms.bsd = true
+elseif system == "OpenBSD" then
+ cfg.platforms.unix = true
+ cfg.platforms.openbsd = true
+ cfg.platforms.bsd = true
+elseif system == "NetBSD" then
+ cfg.platforms.unix = true
+ cfg.platforms.netbsd = true
+ cfg.platforms.bsd = true
+elseif system == "Darwin" then
+ cfg.platforms.unix = true
+ cfg.platforms.macosx = true
+ cfg.platforms.bsd = true
+elseif system == "Linux" then
+ cfg.platforms.unix = true
+ cfg.platforms.linux = true
+elseif system == "SunOS" then
+ cfg.platforms.unix = true
+ cfg.platforms.solaris = true
+elseif system and system:match("^CYGWIN") then
+ cfg.platforms.unix = true
+ cfg.platforms.cygwin = true
+elseif system and system:match("^MSYS") then
+ cfg.platforms.unix = true
+ cfg.platforms.msys = true
+ cfg.platforms.cygwin = true
+elseif system and system:match("^Windows") then
+ cfg.platforms.windows = true
+ cfg.platforms.win32 = true
+elseif system and system:match("^MINGW") then
+ cfg.platforms.windows = true
+ cfg.platforms.mingw32 = true
+ cfg.platforms.win32 = true
+elseif system == "Haiku" then
+ cfg.platforms.unix = true
+ cfg.platforms.haiku = true
+else
+ cfg.platforms.unix = true
+ -- Fall back to Unix in unknown systems.
+end
+
+-- Set order for platform overrides.
+-- More general platform identifiers should be listed first,
+-- more specific ones later.
+local platform_order = {
+ -- Unixes
+ "unix",
+ "bsd",
+ "solaris",
+ "netbsd",
+ "openbsd",
+ "freebsd",
+ "linux",
+ "macosx",
+ "cygwin",
+ "msys",
+ "haiku",
+ -- Windows
+ "win32",
+ "mingw32",
+ "windows",
+}
+
+-- Path configuration:
+local sys_config_file, home_config_file
+local sys_config_file_default, home_config_file_default
+local sys_config_dir, home_config_dir
+local sys_config_ok, home_config_ok = false, false
+local extra_luarocks_module_dir
+sys_config_dir = site_config.LUAROCKS_SYSCONFDIR or site_config.LUAROCKS_PREFIX
+if cfg.platforms.windows then
+ cfg.home = os.getenv("APPDATA") or "c:"
+ sys_config_dir = sys_config_dir or "c:/luarocks"
+ home_config_dir = cfg.home.."/luarocks"
+ cfg.home_tree = cfg.home.."/luarocks/"
+else
+ cfg.home = os.getenv("HOME") or ""
+ sys_config_dir = sys_config_dir or "/etc/luarocks"
+ home_config_dir = cfg.home.."/.luarocks"
+ cfg.home_tree = (os.getenv("USER") ~= "root") and cfg.home.."/.luarocks/"
+end
+
+-- Create global environment for the config files;
+local env_for_config_file = function()
+ local e
+ e = {
+ home = cfg.home,
+ lua_version = cfg.lua_version,
+ platforms = util.make_shallow_copy(cfg.platforms),
+ processor = cfg.target_cpu, -- remains for compat reasons
+ target_cpu = cfg.target_cpu, -- replaces `processor`
+ os_getenv = os.getenv,
+ dump_env = function()
+ -- debug function, calling it from a config file will show all
+ -- available globals to that config file
+ print(util.show_table(e, "global environment"))
+ end,
+ }
+ return e
+end
+
+-- Merge values from config files read into the `cfg` table
+local merge_overrides = function(overrides)
+ -- remove some stuff we do not want to integrate
+ overrides.os_getenv = nil
+ overrides.dump_env = nil
+ -- remove tables to be copied verbatim instead of deeply merged
+ if overrides.rocks_trees then cfg.rocks_trees = nil end
+ if overrides.rocks_servers then cfg.rocks_servers = nil end
+ -- perform actual merge
+ util.deep_merge(cfg, overrides)
+end
+
+-- load config file from a list until first succesful one. Info is
+-- added to `cfg` module table, returns filepath of succesfully loaded
+-- file or nil if it failed
+local load_config_file = function(list)
+ for _, filepath in ipairs(list) do
+ local result, err, errcode = persist.load_into_table(filepath, env_for_config_file())
+ if (not result) and errcode ~= "open" then
+ -- errcode is either "load" or "run"; bad config file, so error out
+ io.stderr:write(err.."\n")
+ os.exit(cfg.errorcodes.CONFIGFILE)
+ end
+ if result then
+ -- succes in loading and running, merge contents and exit
+ merge_overrides(result)
+ return filepath
+ end
+ end
+ return nil -- nothing was loaded
+end
+
+
+-- Load system configuration file
+do
+ sys_config_file_default = sys_config_dir.."/config-"..cfg.lua_version..".lua"
+ sys_config_file = load_config_file({
+ site_config.LUAROCKS_SYSCONFIG or sys_config_file_default,
+ sys_config_dir.."/config.lua",
+ })
+ sys_config_ok = (sys_config_file ~= nil)
+end
+
+-- Load user configuration file (if allowed)
+if not site_config.LUAROCKS_FORCE_CONFIG then
+
+ home_config_file_default = home_config_dir.."/config-"..cfg.lua_version..".lua"
+
+ local config_env_var = "LUAROCKS_CONFIG_" .. version_suffix
+ local config_env_value = os.getenv(config_env_var)
+ if not config_env_value then
+ config_env_var = "LUAROCKS_CONFIG"
+ config_env_value = os.getenv(config_env_var)
+ end
+
+ -- first try environment provided file, so we can explicitly warn when it is missing
+ if config_env_value then
+ local list = { config_env_value }
+ home_config_file = load_config_file(list)
+ home_config_ok = (home_config_file ~= nil)
+ if not home_config_ok then
+ io.stderr:write("Warning: could not load configuration file `"..config_env_value.."` given in environment variable "..config_env_var.."\n")
+ end
+ end
+
+ -- try the alternative defaults if there was no environment specified file or it didn't work
+ if not home_config_ok then
+ local list = {
+ home_config_file_default,
+ home_config_dir.."/config.lua",
+ }
+ home_config_file = load_config_file(list)
+ home_config_ok = (home_config_file ~= nil)
+ end
+end
+
+
+if not next(cfg.rocks_trees) then
+ if cfg.home_tree then
+ table.insert(cfg.rocks_trees, { name = "user", root = cfg.home_tree } )
+ end
+ if site_config.LUAROCKS_ROCKS_TREE then
+ table.insert(cfg.rocks_trees, { name = "system", root = site_config.LUAROCKS_ROCKS_TREE } )
+ end
+end
+
+-- update platforms list; keyed -> array
+do
+ -- if explicitly given by user,
+ if cfg.platforms[1] then
+ local is_windows = cfg.platforms.windows
+ -- Clear auto-detected values
+ for k, _ in pairs(cfg.platforms) do
+ if type(k) == "string" then
+ cfg.platforms[k] = nil
+ end
+ end
+ -- and set the ones given by the user.
+ for _, plat in ipairs(cfg.platforms) do
+ cfg.platforms[plat] = true
+ end
+ -- If no major platform family was set by the user,
+ if not (cfg.platforms.unix or cfg.platforms.windows) then
+ -- set some fallback defaults in case the user provides an incomplete configuration.
+ -- LuaRocks expects a set of defaults to be available.
+ -- This is used for setting defaults here only; the platform overrides
+ -- will use only the user's list.
+ if is_windows then
+ cfg.platforms.windows = true
+ table.insert(cfg.platforms, "windows")
+ else
+ cfg.platforms.unix = true
+ table.insert(cfg.platforms, "unix")
+ end
+ end
+ else
+ -- Sort detected platform defaults
+ local order = {}
+ for i, v in ipairs(platform_order) do
+ order[v] = i
+ end
+ local entries = {}
+ for k, v in pairs(cfg.platforms) do
+ if type(k) == "string" and v == true then
+ table.insert(entries, k)
+ end
+ end
+ table.sort(entries, function(a, b) return order[a] < order[b] end)
+ util.deep_merge(cfg.platforms, entries)
+ end
+end
+
+-- Configure defaults:
+local defaults = {
+
+ local_by_default = false,
+ accept_unknown_fields = false,
+ fs_use_modules = true,
+ hooks_enabled = true,
+ deps_mode = "one",
+ check_certificates = false,
+ perm_read = "0644",
+ perm_exec = "0755",
+
+ lua_modules_path = "/share/lua/"..cfg.lua_version,
+ lib_modules_path = "/lib/lua/"..cfg.lua_version,
+ rocks_subdir = site_config.LUAROCKS_ROCKS_SUBDIR or "/lib/luarocks/rocks",
+
+ arch = "unknown",
+ lib_extension = "unknown",
+ obj_extension = "unknown",
+ link_lua_explicitly = false,
+
+ rocks_servers = {
+ {
+ "https://luarocks.org",
+ "https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/",
+ "http://luafr.org/moonrocks/",
+ "http://luarocks.logiceditor.com/rocks",
+ }
+ },
+ disabled_servers = {},
+
+ upload = {
+ server = "https://luarocks.org",
+ tool_version = "1.0.0",
+ api_version = "1",
+ },
+
+ lua_extension = "lua",
+ lua_interpreter = site_config.LUA_INTERPRETER or "lua",
+ downloader = site_config.LUAROCKS_DOWNLOADER or "wget",
+ md5checker = site_config.LUAROCKS_MD5CHECKER or "md5sum",
+ connection_timeout = 30, -- 0 = no timeout
+
+ variables = {
+ MAKE = "make",
+ CC = "cc",
+ LD = "ld",
+
+ CVS = "cvs",
+ GIT = "git",
+ SSCM = "sscm",
+ SVN = "svn",
+ HG = "hg",
+
+ RSYNC = "rsync",
+ WGET = "wget",
+ SCP = "scp",
+ CURL = "curl",
+
+ PWD = "pwd",
+ MKDIR = "mkdir",
+ RMDIR = "rmdir",
+ CP = "cp",
+ LS = "ls",
+ RM = "rm",
+ FIND = "find",
+ TEST = "test",
+ CHMOD = "chmod",
+ MKTEMP = "mktemp",
+
+ ZIP = "zip",
+ UNZIP = "unzip -n",
+ GUNZIP = "gunzip",
+ BUNZIP2 = "bunzip2",
+ TAR = "tar",
+
+ MD5SUM = "md5sum",
+ OPENSSL = "openssl",
+ MD5 = "md5",
+ STAT = "stat",
+ TOUCH = "touch",
+
+ CMAKE = "cmake",
+ SEVENZ = "7z",
+
+ RSYNCFLAGS = "--exclude=.git -Oavz",
+ STATFLAG = "-c '%a'",
+ CURLNOCERTFLAG = "",
+ WGETNOCERTFLAG = "",
+ },
+
+ external_deps_subdirs = site_config.LUAROCKS_EXTERNAL_DEPS_SUBDIRS or {
+ bin = "bin",
+ lib = "lib",
+ include = "include"
+ },
+ runtime_external_deps_subdirs = site_config.LUAROCKS_RUNTIME_EXTERNAL_DEPS_SUBDIRS or {
+ bin = "bin",
+ lib = "lib",
+ include = "include"
+ },
+
+ rocks_provided = {}
+}
+
+if cfg.platforms.windows then
+ local full_prefix = (site_config.LUAROCKS_PREFIX or (os.getenv("PROGRAMFILES")..[[\LuaRocks]]))
+ extra_luarocks_module_dir = full_prefix.."/lua/?.lua"
+
+ home_config_file = home_config_file and home_config_file:gsub("\\","/")
+ defaults.fs_use_modules = false
+ defaults.arch = "win32-"..cfg.target_cpu
+ defaults.lib_extension = "dll"
+ defaults.external_lib_extension = "dll"
+ defaults.obj_extension = "obj"
+ defaults.external_deps_dirs = { "c:/external/" }
+ defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR and site_config.LUA_BINDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/bin"
+ defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR and site_config.LUA_INCDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/include"
+ defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR and site_config.LUA_LIBDIR:gsub("\\", "/") or "c:/lua"..cfg.lua_version.."/lib"
+
+ defaults.makefile = "Makefile.win"
+ defaults.variables.MAKE = "nmake"
+ defaults.variables.CC = "cl"
+ defaults.variables.RC = "rc"
+ defaults.variables.WRAPPER = full_prefix.."\\rclauncher.c"
+ defaults.variables.LD = "link"
+ defaults.variables.MT = "mt"
+ defaults.variables.LUALIB = "lua"..cfg.lua_version..".lib"
+ defaults.variables.CFLAGS = "/nologo /MD /O2"
+ defaults.variables.LIBFLAG = "/nologo /dll"
+
+ local bins = { "SEVENZ", "CP", "FIND", "LS", "MD5SUM",
+ "MKDIR", "MV", "PWD", "RMDIR", "TEST", "UNAME", "WGET" }
+ for _, var in ipairs(bins) do
+ if defaults.variables[var] then
+ defaults.variables[var] = full_prefix.."\\tools\\"..defaults.variables[var]
+ end
+ end
+
+ defaults.external_deps_patterns = {
+ bin = { "?.exe", "?.bat" },
+ lib = { "?.lib", "?.dll", "lib?.dll" },
+ include = { "?.h" }
+ }
+ defaults.runtime_external_deps_patterns = {
+ bin = { "?.exe", "?.bat" },
+ lib = { "?.dll", "lib?.dll" },
+ include = { "?.h" }
+ }
+ defaults.export_path = "SET PATH=%s"
+ defaults.export_path_separator = ";"
+ defaults.export_lua_path = "SET LUA_PATH=%s"
+ defaults.export_lua_cpath = "SET LUA_CPATH=%s"
+ defaults.wrapper_suffix = ".bat"
+
+ local localappdata = os.getenv("LOCALAPPDATA")
+ if not localappdata then
+ -- for Windows versions below Vista
+ localappdata = os.getenv("USERPROFILE").."/Local Settings/Application Data"
+ end
+ defaults.local_cache = localappdata.."/LuaRocks/Cache"
+ defaults.web_browser = "start"
+end
+
+if cfg.platforms.mingw32 then
+ defaults.obj_extension = "o"
+ defaults.cmake_generator = "MinGW Makefiles"
+ defaults.variables.MAKE = "mingw32-make"
+ defaults.variables.CC = "mingw32-gcc"
+ defaults.variables.RC = "windres"
+ defaults.variables.LD = "mingw32-gcc"
+ defaults.variables.CFLAGS = "-O2"
+ defaults.variables.LIBFLAG = "-shared"
+ defaults.makefile = "Makefile"
+ defaults.external_deps_patterns = {
+ bin = { "?.exe", "?.bat" },
+ -- mingw lookup list from http://stackoverflow.com/a/15853231/1793220
+ -- ...should we keep ?.lib at the end? It's not in the above list.
+ lib = { "lib?.dll.a", "?.dll.a", "lib?.a", "cyg?.dll", "lib?.dll", "?.dll", "?.lib" },
+ include = { "?.h" }
+ }
+ defaults.runtime_external_deps_patterns = {
+ bin = { "?.exe", "?.bat" },
+ lib = { "cyg?.dll", "?.dll", "lib?.dll" },
+ include = { "?.h" }
+ }
+
+end
+
+if cfg.platforms.unix then
+ defaults.lib_extension = "so"
+ defaults.external_lib_extension = "so"
+ defaults.obj_extension = "o"
+ defaults.external_deps_dirs = { "/usr/local", "/usr" }
+ defaults.variables.LUA_BINDIR = site_config.LUA_BINDIR or "/usr/local/bin"
+ defaults.variables.LUA_INCDIR = site_config.LUA_INCDIR or "/usr/local/include"
+ defaults.variables.LUA_LIBDIR = site_config.LUA_LIBDIR or "/usr/local/lib"
+ defaults.variables.CFLAGS = "-O2"
+ defaults.cmake_generator = "Unix Makefiles"
+ defaults.variables.CC = "gcc"
+ defaults.variables.LD = "gcc"
+ defaults.gcc_rpath = true
+ defaults.variables.LIBFLAG = "-shared"
+ defaults.external_deps_patterns = {
+ bin = { "?" },
+ lib = { "lib?.a", "lib?.so", "lib?.so.*" },
+ include = { "?.h" }
+ }
+ defaults.runtime_external_deps_patterns = {
+ bin = { "?" },
+ lib = { "lib?.so", "lib?.so.*" },
+ include = { "?.h" }
+ }
+ defaults.export_path = "export PATH='%s'"
+ defaults.export_path_separator = ":"
+ defaults.export_lua_path = "export LUA_PATH='%s'"
+ defaults.export_lua_cpath = "export LUA_CPATH='%s'"
+ defaults.wrapper_suffix = ""
+ defaults.local_cache = cfg.home.."/.cache/luarocks"
+ if not defaults.variables.CFLAGS:match("-fPIC") then
+ defaults.variables.CFLAGS = defaults.variables.CFLAGS.." -fPIC"
+ end
+ defaults.web_browser = "xdg-open"
+end
+
+if cfg.platforms.cygwin then
+ defaults.lib_extension = "so" -- can be overridden in the config file for mingw builds
+ defaults.arch = "cygwin-"..cfg.target_cpu
+ defaults.cmake_generator = "Unix Makefiles"
+ defaults.variables.CC = "echo -llua | xargs gcc"
+ defaults.variables.LD = "echo -llua | xargs gcc"
+ defaults.variables.LIBFLAG = "-shared"
+ defaults.link_lua_explicitly = true
+end
+
+if cfg.platforms.msys then
+ -- msys is basically cygwin made out of mingw, meaning the subsytem is unixish
+ -- enough, yet we can freely mix with native win32
+ defaults.external_deps_patterns = {
+ bin = { "?.exe", "?.bat", "?" },
+ lib = { "lib?.so", "lib?.so.*", "lib?.dll.a", "?.dll.a",
+ "lib?.a", "lib?.dll", "?.dll", "?.lib" },
+ include = { "?.h" }
+ }
+ defaults.runtime_external_deps_patterns = {
+ bin = { "?.exe", "?.bat" },
+ lib = { "lib?.so", "?.dll", "lib?.dll" },
+ include = { "?.h" }
+ }
+end
+
+
+if cfg.platforms.bsd then
+ defaults.variables.MAKE = "gmake"
+ defaults.variables.STATFLAG = "-f '%OLp'"
+end
+
+if cfg.platforms.macosx then
+ defaults.variables.MAKE = "make"
+ defaults.external_lib_extension = "dylib"
+ defaults.arch = "macosx-"..cfg.target_cpu
+ defaults.variables.LIBFLAG = "-bundle -undefined dynamic_lookup -all_load"
+ defaults.variables.STAT = "/usr/bin/stat"
+ defaults.variables.STATFLAG = "-f '%A'"
+ local version = io.popen("sw_vers -productVersion"):read("*l")
+ version = tonumber(version and version:match("^[^.]+%.([^.]+)")) or 3
+ if version >= 10 then
+ version = 8
+ elseif version >= 5 then
+ version = 5
+ else
+ defaults.gcc_rpath = false
+ end
+ defaults.variables.CC = "env MACOSX_DEPLOYMENT_TARGET=10."..version.." gcc"
+ defaults.variables.LD = "env MACOSX_DEPLOYMENT_TARGET=10."..version.." gcc"
+ defaults.web_browser = "open"
+end
+
+if cfg.platforms.linux then
+ defaults.arch = "linux-"..cfg.target_cpu
+end
+
+if cfg.platforms.freebsd then
+ defaults.arch = "freebsd-"..cfg.target_cpu
+ defaults.gcc_rpath = false
+ defaults.variables.CC = "cc"
+ defaults.variables.LD = "cc"
+end
+
+if cfg.platforms.openbsd then
+ defaults.arch = "openbsd-"..cfg.target_cpu
+end
+
+if cfg.platforms.netbsd then
+ defaults.arch = "netbsd-"..cfg.target_cpu
+end
+
+if cfg.platforms.solaris then
+ defaults.arch = "solaris-"..cfg.target_cpu
+ --defaults.platforms = {"unix", "solaris"}
+ defaults.variables.MAKE = "gmake"
+end
+
+-- Expose some more values detected by LuaRocks for use by rockspec authors.
+defaults.variables.LIB_EXTENSION = defaults.lib_extension
+defaults.variables.OBJ_EXTENSION = defaults.obj_extension
+defaults.variables.LUAROCKS_PREFIX = site_config.LUAROCKS_PREFIX
+defaults.variables.LUA = site_config.LUA_DIR_SET and (defaults.variables.LUA_BINDIR.."/"..defaults.lua_interpreter) or defaults.lua_interpreter
+
+-- Add built-in modules to rocks_provided
+defaults.rocks_provided["lua"] = cfg.lua_version.."-1"
+
+if bit32 then -- Lua 5.2+
+ defaults.rocks_provided["bit32"] = cfg.lua_version.."-1"
+end
+
+if utf8 then -- Lua 5.3+
+ defaults.rocks_provided["utf8"] = cfg.lua_version.."-1"
+end
+
+if package.loaded.jit then
+ -- LuaJIT
+ local lj_version = package.loaded.jit.version:match("LuaJIT (.*)"):gsub("%-","")
+ --defaults.rocks_provided["luajit"] = lj_version.."-1"
+ defaults.rocks_provided["luabitop"] = lj_version.."-1"
+end
+
+-- Use defaults:
+
+-- Populate some arrays with values from their 'defaults' counterparts
+-- if they were not already set by user.
+for _, entry in ipairs({"variables", "rocks_provided"}) do
+ if not cfg[entry] then
+ cfg[entry] = {}
+ end
+ for k,v in pairs(defaults[entry]) do
+ if not cfg[entry][k] then
+ cfg[entry][k] = v
+ end
+ end
+end
+
+-- For values not set in the config file, use values from the 'defaults' table.
+local cfg_mt = {
+ __index = function(t, k)
+ local default = defaults[k]
+ if default then
+ rawset(t, k, default)
+ end
+ return default
+ end
+}
+setmetatable(cfg, cfg_mt)
+
+if not cfg.check_certificates then
+ cfg.variables.CURLNOCERTFLAG = "-k"
+ cfg.variables.WGETNOCERTFLAG = "--no-check-certificate"
+end
+
+function cfg.make_paths_from_tree(tree)
+ local lua_path, lib_path, bin_path
+ if type(tree) == "string" then
+ lua_path = tree..cfg.lua_modules_path
+ lib_path = tree..cfg.lib_modules_path
+ bin_path = tree.."/bin"
+ else
+ lua_path = tree.lua_dir or tree.root..cfg.lua_modules_path
+ lib_path = tree.lib_dir or tree.root..cfg.lib_modules_path
+ bin_path = tree.bin_dir or tree.root.."/bin"
+ end
+ return lua_path, lib_path, bin_path
+end
+
+function cfg.package_paths(current)
+ local new_path, new_cpath, new_bin = {}, {}, {}
+ local function add_tree_to_paths(tree)
+ local lua_path, lib_path, bin_path = cfg.make_paths_from_tree(tree)
+ table.insert(new_path, lua_path.."/?.lua")
+ table.insert(new_path, lua_path.."/?/init.lua")
+ table.insert(new_cpath, lib_path.."/?."..cfg.lib_extension)
+ table.insert(new_bin, bin_path)
+ end
+ if current then
+ add_tree_to_paths(current)
+ end
+ for _,tree in ipairs(cfg.rocks_trees) do
+ add_tree_to_paths(tree)
+ end
+ if extra_luarocks_module_dir then
+ table.insert(new_path, extra_luarocks_module_dir)
+ end
+ return table.concat(new_path, ";"), table.concat(new_cpath, ";"), table.concat(new_bin, cfg.export_path_separator)
+end
+
+function cfg.init_package_paths()
+ local lr_path, lr_cpath, lr_bin = cfg.package_paths()
+ package.path = util.remove_path_dupes(package.path .. ";" .. lr_path, ";")
+ package.cpath = util.remove_path_dupes(package.cpath .. ";" .. lr_cpath, ";")
+end
+
+function cfg.which_config()
+ local ret = {
+ system = {
+ file = sys_config_file or sys_config_file_default,
+ ok = sys_config_ok,
+ },
+ user = {
+ file = home_config_file or home_config_file_default,
+ ok = home_config_ok,
+ }
+ }
+ ret.nearest = (ret.user.ok and ret.user.file) or ret.system.file
+ return ret
+end
+
+cfg.user_agent = "LuaRocks/"..cfg.program_version.." "..cfg.arch
+
+cfg.http_proxy = os.getenv("http_proxy")
+cfg.https_proxy = os.getenv("https_proxy")
+cfg.no_proxy = os.getenv("no_proxy")
+
+--- Check if platform was detected
+-- @param query string: The platform name to check.
+-- @return boolean: true if LuaRocks is currently running on queried platform.
+function cfg.is_platform(query)
+ assert(type(query) == "string")
+
+ for _, platform in ipairs(cfg.platforms) do
+ if platform == query then
+ return true
+ end
+ end
+end
+
+return cfg
diff --git a/Utils/luarocks/lua/luarocks/command_line.lua b/Utils/luarocks/lua/luarocks/command_line.lua
new file mode 100644
index 000000000..ecf3a61b1
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/command_line.lua
@@ -0,0 +1,199 @@
+
+--- Functions for command-line scripts.
+local command_line = {}
+
+local unpack = unpack or table.unpack
+
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local path = require("luarocks.path")
+local dir = require("luarocks.dir")
+local deps = require("luarocks.deps")
+local fs = require("luarocks.fs")
+
+local program = util.this_program("luarocks")
+
+local function error_handler(err)
+ return debug.traceback("LuaRocks "..cfg.program_version..
+ " bug (please report at https://github.com/keplerproject/luarocks/issues).\n"..err, 2)
+end
+
+--- Display an error message and exit.
+-- @param message string: The error message.
+-- @param exitcode number: the exitcode to use
+local function die(message, exitcode)
+ assert(type(message) == "string")
+ util.printerr("\nError: "..message)
+
+ local ok, err = xpcall(util.run_scheduled_functions, error_handler)
+ if not ok then
+ util.printerr("\nError: "..err)
+ exitcode = cfg.errorcodes.CRASH
+ end
+
+ os.exit(exitcode or cfg.errorcodes.UNSPECIFIED)
+end
+
+local function replace_tree(flags, tree)
+ tree = dir.normalize(tree)
+ flags["tree"] = tree
+ path.use_tree(tree)
+end
+
+--- Main command-line processor.
+-- Parses input arguments and calls the appropriate driver function
+-- to execute the action requested on the command-line, forwarding
+-- to it any additional arguments passed by the user.
+-- Uses the global table "commands", which contains
+-- the loaded modules representing commands.
+-- @param ... string: Arguments given on the command-line.
+function command_line.run_command(...)
+ local args = {...}
+ local cmdline_vars = {}
+ for i = #args, 1, -1 do
+ local arg = args[i]
+ if arg:match("^[^-][^=]*=") then
+ local var, val = arg:match("^([A-Z_][A-Z0-9_]*)=(.*)")
+ if val then
+ cmdline_vars[var] = val
+ table.remove(args, i)
+ else
+ die("Invalid assignment: "..arg)
+ end
+ end
+ end
+ local nonflags = { util.parse_flags(unpack(args)) }
+ local flags = table.remove(nonflags, 1)
+ if flags.ERROR then
+ die(flags.ERROR.." See --help.")
+ end
+
+ if flags["from"] then flags["server"] = flags["from"] end
+ if flags["only-from"] then flags["only-server"] = flags["only-from"] end
+ if flags["only-sources-from"] then flags["only-sources"] = flags["only-sources-from"] end
+ if flags["to"] then flags["tree"] = flags["to"] end
+ if flags["nodeps"] then
+ flags["deps-mode"] = "none"
+ end
+
+ cfg.flags = flags
+
+ local command
+
+ if flags["verbose"] then -- setting it in the config file will kick-in earlier in the process
+ cfg.verbose = true
+ fs.verbose()
+ end
+
+ if flags["timeout"] then -- setting it in the config file will kick-in earlier in the process
+ local timeout = tonumber(flags["timeout"])
+ if timeout then
+ cfg.connection_timeout = timeout
+ else
+ die "Argument error: --timeout expects a numeric argument."
+ end
+ end
+
+ if flags["version"] then
+ util.printout(program.." "..cfg.program_version)
+ util.printout(program_description)
+ util.printout()
+ os.exit(cfg.errorcodes.OK)
+ elseif flags["help"] or #nonflags == 0 then
+ command = "help"
+ else
+ command = table.remove(nonflags, 1)
+ end
+ command = command:gsub("-", "_")
+
+ if cfg.local_by_default then
+ flags["local"] = true
+ end
+
+ if flags["deps-mode"] and not deps.check_deps_mode_flag(flags["deps-mode"]) then
+ die("Invalid entry for --deps-mode.")
+ end
+
+ if flags["branch"] then
+ cfg.branch = flags["branch"]
+ end
+
+ if flags["tree"] then
+ local named = false
+ for _, tree in ipairs(cfg.rocks_trees) do
+ if type(tree) == "table" and flags["tree"] == tree.name then
+ if not tree.root then
+ die("Configuration error: tree '"..tree.name.."' has no 'root' field.")
+ end
+ replace_tree(flags, tree.root)
+ named = true
+ break
+ end
+ end
+ if not named then
+ local root_dir = fs.absolute_name(flags["tree"])
+ replace_tree(flags, root_dir)
+ end
+ elseif flags["local"] then
+ if not cfg.home_tree then
+ die("The --local flag is meant for operating in a user's home directory.\n"..
+ "You are running as a superuser, which is intended for system-wide operation.\n"..
+ "To force using the superuser's home, use --tree explicitly.")
+ end
+ replace_tree(flags, cfg.home_tree)
+ else
+ local trees = cfg.rocks_trees
+ path.use_tree(trees[#trees])
+ end
+
+ if type(cfg.root_dir) == "string" then
+ cfg.root_dir = cfg.root_dir:gsub("/+$", "")
+ else
+ cfg.root_dir.root = cfg.root_dir.root:gsub("/+$", "")
+ end
+ cfg.rocks_dir = cfg.rocks_dir:gsub("/+$", "")
+ cfg.deploy_bin_dir = cfg.deploy_bin_dir:gsub("/+$", "")
+ cfg.deploy_lua_dir = cfg.deploy_lua_dir:gsub("/+$", "")
+ cfg.deploy_lib_dir = cfg.deploy_lib_dir:gsub("/+$", "")
+
+ cfg.variables.ROCKS_TREE = cfg.rocks_dir
+ cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir
+
+ if flags["server"] then
+ local protocol, path = dir.split_url(flags["server"])
+ table.insert(cfg.rocks_servers, 1, protocol.."://"..path)
+ end
+
+ if flags["only-server"] then
+ cfg.rocks_servers = { flags["only-server"] }
+ end
+
+ if flags["only-sources"] then
+ cfg.only_sources_from = flags["only-sources"]
+ end
+
+ if command ~= "help" then
+ for k, v in pairs(cmdline_vars) do
+ cfg.variables[k] = v
+ end
+ end
+
+ if not fs.current_dir() or fs.current_dir() == "" then
+ die("Current directory does not exist. Please run LuaRocks from an existing directory.")
+ end
+
+ if commands[command] then
+ local cmd = require(commands[command])
+ local call_ok, ok, err, exitcode = xpcall(function() return cmd.command(flags, unpack(nonflags)) end, error_handler)
+ if not call_ok then
+ die(ok, cfg.errorcodes.CRASH)
+ elseif not ok then
+ die(err, exitcode)
+ end
+ else
+ die("Unknown command: "..command)
+ end
+ util.run_scheduled_functions()
+end
+
+return command_line
diff --git a/Utils/luarocks/lua/luarocks/config_cmd.lua b/Utils/luarocks/lua/luarocks/config_cmd.lua
new file mode 100644
index 000000000..fe3cc637d
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/config_cmd.lua
@@ -0,0 +1,72 @@
+--- Module implementing the LuaRocks "config" command.
+-- Queries information about the LuaRocks configuration.
+local config_cmd = {}
+
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local dir = require("luarocks.dir")
+
+util.add_run_function(config_cmd)
+config_cmd.help_summary = "Query information about the LuaRocks configuration."
+config_cmd.help_arguments = ""
+config_cmd.help = [[
+--lua-incdir Path to Lua header files.
+
+--lua-libdir Path to Lua library files.
+
+--lua-ver Lua version (in major.minor format). e.g. 5.1
+
+--system-config Location of the system config file.
+
+--user-config Location of the user config file.
+
+--rock-trees Rocks trees in use. First the user tree, then the system tree.
+]]
+
+local function config_file(conf)
+ print(dir.normalize(conf.file))
+ if conf.ok then
+ return true
+ else
+ return nil, "file not found"
+ end
+end
+
+--- Driver function for "config" command.
+-- @return boolean: True if succeeded, nil on errors.
+function config_cmd.command(flags)
+ if flags["lua-incdir"] then
+ print(cfg.variables.LUA_INCDIR)
+ return true
+ end
+ if flags["lua-libdir"] then
+ print(cfg.variables.LUA_LIBDIR)
+ return true
+ end
+ if flags["lua-ver"] then
+ print(cfg.lua_version)
+ return true
+ end
+ local conf = cfg.which_config()
+ if flags["system-config"] then
+ return config_file(conf.system)
+ end
+ if flags["user-config"] then
+ return config_file(conf.user)
+ end
+ if flags["rock-trees"] then
+ for _, tree in ipairs(cfg.rocks_trees) do
+ if type(tree) == "string" then
+ util.printout(dir.normalize(tree))
+ else
+ local name = tree.name and "\t"..tree.name or ""
+ util.printout(dir.normalize(tree.root)..name)
+ end
+ end
+ return true
+ end
+
+ return nil, "Please provide a flag for querying configuration values. "..util.see_help("config")
+end
+
+return config_cmd
diff --git a/Utils/luarocks/lua/luarocks/deps.lua b/Utils/luarocks/lua/luarocks/deps.lua
new file mode 100644
index 000000000..dcebec9b2
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/deps.lua
@@ -0,0 +1,752 @@
+
+--- Dependency handling functions.
+-- Dependencies are represented in LuaRocks through strings with
+-- a package name followed by a comma-separated list of constraints.
+-- Each constraint consists of an operator and a version number.
+-- In this string format, version numbers are represented as
+-- naturally as possible, like they are used by upstream projects
+-- (e.g. "2.0beta3"). Internally, LuaRocks converts them to a purely
+-- numeric representation, allowing comparison following some
+-- "common sense" heuristics. The precise specification of the
+-- comparison criteria is the source code of this module, but the
+-- test/test_deps.lua file included with LuaRocks provides some
+-- insights on what these criteria are.
+local deps = {}
+package.loaded["luarocks.deps"] = deps
+
+local cfg = require("luarocks.cfg")
+local manif_core = require("luarocks.manif_core")
+local path = require("luarocks.path")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+local operators = {
+ ["=="] = "==",
+ ["~="] = "~=",
+ [">"] = ">",
+ ["<"] = "<",
+ [">="] = ">=",
+ ["<="] = "<=",
+ ["~>"] = "~>",
+ -- plus some convenience translations
+ [""] = "==",
+ ["="] = "==",
+ ["!="] = "~="
+}
+
+local deltas = {
+ scm = 1100,
+ cvs = 1000,
+ rc = -1000,
+ pre = -10000,
+ beta = -100000,
+ alpha = -1000000
+}
+
+local version_mt = {
+ --- Equality comparison for versions.
+ -- All version numbers must be equal.
+ -- If both versions have revision numbers, they must be equal;
+ -- otherwise the revision number is ignored.
+ -- @param v1 table: version table to compare.
+ -- @param v2 table: version table to compare.
+ -- @return boolean: true if they are considered equivalent.
+ __eq = function(v1, v2)
+ if #v1 ~= #v2 then
+ return false
+ end
+ for i = 1, #v1 do
+ if v1[i] ~= v2[i] then
+ return false
+ end
+ end
+ if v1.revision and v2.revision then
+ return (v1.revision == v2.revision)
+ end
+ return true
+ end,
+ --- Size comparison for versions.
+ -- All version numbers are compared.
+ -- If both versions have revision numbers, they are compared;
+ -- otherwise the revision number is ignored.
+ -- @param v1 table: version table to compare.
+ -- @param v2 table: version table to compare.
+ -- @return boolean: true if v1 is considered lower than v2.
+ __lt = function(v1, v2)
+ for i = 1, math.max(#v1, #v2) do
+ local v1i, v2i = v1[i] or 0, v2[i] or 0
+ if v1i ~= v2i then
+ return (v1i < v2i)
+ end
+ end
+ if v1.revision and v2.revision then
+ return (v1.revision < v2.revision)
+ end
+ return false
+ end
+}
+
+local version_cache = {}
+setmetatable(version_cache, {
+ __mode = "kv"
+})
+
+--- Parse a version string, converting to table format.
+-- A version table contains all components of the version string
+-- converted to numeric format, stored in the array part of the table.
+-- If the version contains a revision, it is stored numerically
+-- in the 'revision' field. The original string representation of
+-- the string is preserved in the 'string' field.
+-- Returned version tables use a metatable
+-- allowing later comparison through relational operators.
+-- @param vstring string: A version number in string format.
+-- @return table or nil: A version table or nil
+-- if the input string contains invalid characters.
+function deps.parse_version(vstring)
+ if not vstring then return nil end
+ assert(type(vstring) == "string")
+
+ local cached = version_cache[vstring]
+ if cached then
+ return cached
+ end
+
+ local version = {}
+ local i = 1
+
+ local function add_token(number)
+ version[i] = version[i] and version[i] + number/100000 or number
+ i = i + 1
+ end
+
+ -- trim leading and trailing spaces
+ vstring = vstring:match("^%s*(.*)%s*$")
+ version.string = vstring
+ -- store revision separately if any
+ local main, revision = vstring:match("(.*)%-(%d+)$")
+ if revision then
+ vstring = main
+ version.revision = tonumber(revision)
+ end
+ while #vstring > 0 do
+ -- extract a number
+ local token, rest = vstring:match("^(%d+)[%.%-%_]*(.*)")
+ if token then
+ add_token(tonumber(token))
+ else
+ -- extract a word
+ token, rest = vstring:match("^(%a+)[%.%-%_]*(.*)")
+ if not token then
+ util.printerr("Warning: version number '"..vstring.."' could not be parsed.")
+ version[i] = 0
+ break
+ end
+ version[i] = deltas[token] or (token:byte() / 1000)
+ end
+ vstring = rest
+ end
+ setmetatable(version, version_mt)
+ version_cache[vstring] = version
+ return version
+end
+
+--- Utility function to compare version numbers given as strings.
+-- @param a string: one version.
+-- @param b string: another version.
+-- @return boolean: True if a > b.
+function deps.compare_versions(a, b)
+ return deps.parse_version(a) > deps.parse_version(b)
+end
+
+--- Consumes a constraint from a string, converting it to table format.
+-- For example, a string ">= 1.0, > 2.0" is converted to a table in the
+-- format {op = ">=", version={1,0}} and the rest, "> 2.0", is returned
+-- back to the caller.
+-- @param input string: A list of constraints in string format.
+-- @return (table, string) or nil: A table representing the same
+-- constraints and the string with the unused input, or nil if the
+-- input string is invalid.
+local function parse_constraint(input)
+ assert(type(input) == "string")
+
+ local no_upgrade, op, version, rest = input:match("^(@?)([<>=~!]*)%s*([%w%.%_%-]+)[%s,]*(.*)")
+ local _op = operators[op]
+ version = deps.parse_version(version)
+ if not _op then
+ return nil, "Encountered bad constraint operator: '"..tostring(op).."' in '"..input.."'"
+ end
+ if not version then
+ return nil, "Could not parse version from constraint: '"..input.."'"
+ end
+ return { op = _op, version = version, no_upgrade = no_upgrade=="@" and true or nil }, rest
+end
+
+--- Convert a list of constraints from string to table format.
+-- For example, a string ">= 1.0, < 2.0" is converted to a table in the format
+-- {{op = ">=", version={1,0}}, {op = "<", version={2,0}}}.
+-- Version tables use a metatable allowing later comparison through
+-- relational operators.
+-- @param input string: A list of constraints in string format.
+-- @return table or nil: A table representing the same constraints,
+-- or nil if the input string is invalid.
+function deps.parse_constraints(input)
+ assert(type(input) == "string")
+
+ local constraints, constraint, oinput = {}, nil, input
+ while #input > 0 do
+ constraint, input = parse_constraint(input)
+ if constraint then
+ table.insert(constraints, constraint)
+ else
+ return nil, "Failed to parse constraint '"..tostring(oinput).."' with error: ".. input
+ end
+ end
+ return constraints
+end
+
+--- Convert a dependency from string to table format.
+-- For example, a string "foo >= 1.0, < 2.0"
+-- is converted to a table in the format
+-- {name = "foo", constraints = {{op = ">=", version={1,0}},
+-- {op = "<", version={2,0}}}}. Version tables use a metatable
+-- allowing later comparison through relational operators.
+-- @param dep string: A dependency in string format
+-- as entered in rockspec files.
+-- @return table or nil: A table representing the same dependency relation,
+-- or nil if the input string is invalid.
+function deps.parse_dep(dep)
+ assert(type(dep) == "string")
+
+ local name, rest = dep:match("^%s*([a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*(.*)")
+ if not name then return nil, "failed to extract dependency name from '"..tostring(dep).."'" end
+ local constraints, err = deps.parse_constraints(rest)
+ if not constraints then return nil, err end
+ return { name = name, constraints = constraints }
+end
+
+--- Convert a version table to a string.
+-- @param v table: The version table
+-- @param internal boolean or nil: Whether to display versions in their
+-- internal representation format or how they were specified.
+-- @return string: The dependency information pretty-printed as a string.
+function deps.show_version(v, internal)
+ assert(type(v) == "table")
+ assert(type(internal) == "boolean" or not internal)
+
+ return (internal
+ and table.concat(v, ":")..(v.revision and tostring(v.revision) or "")
+ or v.string)
+end
+
+--- Convert a dependency in table format to a string.
+-- @param dep table: The dependency in table format
+-- @param internal boolean or nil: Whether to display versions in their
+-- internal representation format or how they were specified.
+-- @return string: The dependency information pretty-printed as a string.
+function deps.show_dep(dep, internal)
+ assert(type(dep) == "table")
+ assert(type(internal) == "boolean" or not internal)
+
+ if #dep.constraints > 0 then
+ local pretty = {}
+ for _, c in ipairs(dep.constraints) do
+ table.insert(pretty, c.op .. " " .. deps.show_version(c.version, internal))
+ end
+ return dep.name.." "..table.concat(pretty, ", ")
+ else
+ return dep.name
+ end
+end
+
+--- A more lenient check for equivalence between versions.
+-- This returns true if the requested components of a version
+-- match and ignore the ones that were not given. For example,
+-- when requesting "2", then "2", "2.1", "2.3.5-9"... all match.
+-- When requesting "2.1", then "2.1", "2.1.3" match, but "2.2"
+-- doesn't.
+-- @param version string or table: Version to be tested; may be
+-- in string format or already parsed into a table.
+-- @param requested string or table: Version requested; may be
+-- in string format or already parsed into a table.
+-- @return boolean: True if the tested version matches the requested
+-- version, false otherwise.
+local function partial_match(version, requested)
+ assert(type(version) == "string" or type(version) == "table")
+ assert(type(requested) == "string" or type(version) == "table")
+
+ if type(version) ~= "table" then version = deps.parse_version(version) end
+ if type(requested) ~= "table" then requested = deps.parse_version(requested) end
+ if not version or not requested then return false end
+
+ for i, ri in ipairs(requested) do
+ local vi = version[i] or 0
+ if ri ~= vi then return false end
+ end
+ if requested.revision then
+ return requested.revision == version.revision
+ end
+ return true
+end
+
+--- Check if a version satisfies a set of constraints.
+-- @param version table: A version in table format
+-- @param constraints table: An array of constraints in table format.
+-- @return boolean: True if version satisfies all constraints,
+-- false otherwise.
+function deps.match_constraints(version, constraints)
+ assert(type(version) == "table")
+ assert(type(constraints) == "table")
+ local ok = true
+ setmetatable(version, version_mt)
+ for _, constr in pairs(constraints) do
+ if type(constr.version) == "string" then
+ constr.version = deps.parse_version(constr.version)
+ end
+ local constr_version, constr_op = constr.version, constr.op
+ setmetatable(constr_version, version_mt)
+ if constr_op == "==" then ok = version == constr_version
+ elseif constr_op == "~=" then ok = version ~= constr_version
+ elseif constr_op == ">" then ok = version > constr_version
+ elseif constr_op == "<" then ok = version < constr_version
+ elseif constr_op == ">=" then ok = version >= constr_version
+ elseif constr_op == "<=" then ok = version <= constr_version
+ elseif constr_op == "~>" then ok = partial_match(version, constr_version)
+ end
+ if not ok then break end
+ end
+ return ok
+end
+
+--- Attempt to match a dependency to an installed rock.
+-- @param dep table: A dependency parsed in table format.
+-- @param blacklist table: Versions that can't be accepted. Table where keys
+-- are program versions and values are 'true'.
+-- @return string or nil: latest installed version of the rock matching the dependency
+-- or nil if it could not be matched.
+local function match_dep(dep, blacklist, deps_mode)
+ assert(type(dep) == "table")
+
+ local versions
+ if cfg.rocks_provided[dep.name] then
+ -- provided rocks have higher priority than manifest's rocks
+ versions = { cfg.rocks_provided[dep.name] }
+ else
+ versions = manif_core.get_versions(dep.name, deps_mode)
+ end
+
+ local latest_version
+ for _, vstring in ipairs(versions) do
+ if not blacklist or not blacklist[vstring] then
+ local version = deps.parse_version(vstring)
+ if deps.match_constraints(version, dep.constraints) then
+ if not latest_version or version > latest_version then
+ latest_version = version
+ end
+ end
+ end
+ end
+
+ return latest_version and latest_version.string
+end
+
+--- Attempt to match dependencies of a rockspec to installed rocks.
+-- @param rockspec table: The rockspec loaded as a table.
+-- @param blacklist table or nil: Program versions to not use as valid matches.
+-- Table where keys are program names and values are tables where keys
+-- are program versions and values are 'true'.
+-- @return table, table, table: A table where keys are dependencies parsed
+-- in table format and values are tables containing fields 'name' and
+-- version' representing matches; a table of missing dependencies
+-- parsed as tables; and a table of "no-upgrade" missing dependencies
+-- (to be used in plugin modules so that a plugin does not force upgrade of
+-- its parent application).
+function deps.match_deps(rockspec, blacklist, deps_mode)
+ assert(type(rockspec) == "table")
+ assert(type(blacklist) == "table" or not blacklist)
+ local matched, missing, no_upgrade = {}, {}, {}
+
+ for _, dep in ipairs(rockspec.dependencies) do
+ local found = match_dep(dep, blacklist and blacklist[dep.name] or nil, deps_mode)
+ if found then
+ if not cfg.rocks_provided[dep.name] then
+ matched[dep] = {name = dep.name, version = found}
+ end
+ else
+ if dep.constraints[1] and dep.constraints[1].no_upgrade then
+ no_upgrade[dep.name] = dep
+ else
+ missing[dep.name] = dep
+ end
+ end
+ end
+ return matched, missing, no_upgrade
+end
+
+--- Return a set of values of a table.
+-- @param tbl table: The input table.
+-- @return table: The array of keys.
+local function values_set(tbl)
+ local set = {}
+ for _, v in pairs(tbl) do
+ set[v] = true
+ end
+ return set
+end
+
+local function rock_status(name, deps_mode)
+ local search = require("luarocks.search")
+ local installed = match_dep(search.make_query(name), nil, deps_mode)
+ local installation_type = cfg.rocks_provided[name] and "provided by VM" or "installed"
+ return installed and installed.." "..installation_type or "not installed"
+end
+
+--- Check depenendencies of a package and report any missing ones.
+-- @param name string: package name.
+-- @param version string: package version.
+-- @param dependencies table: array of dependencies.
+-- @param deps_mode string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+function deps.report_missing_dependencies(name, version, dependencies, deps_mode)
+ local first_missing_dep = true
+
+ for _, dep in ipairs(dependencies) do
+ if not match_dep(dep, nil, deps_mode) then
+ if first_missing_dep then
+ util.printout(("Missing dependencies for %s %s:"):format(name, version))
+ first_missing_dep = false
+ end
+
+ util.printout((" %s (%s)"):format(deps.show_dep(dep), rock_status(dep.name, deps_mode)))
+ end
+ end
+end
+
+--- Check dependencies of a rock and attempt to install any missing ones.
+-- Packages are installed using the LuaRocks "install" command.
+-- Aborts the program if a dependency could not be fulfilled.
+-- @param rockspec table: A rockspec in table format.
+-- @return boolean or (nil, string, [string]): True if no errors occurred, or
+-- nil and an error message if any test failed, followed by an optional
+-- error code.
+function deps.fulfill_dependencies(rockspec, deps_mode)
+
+ local search = require("luarocks.search")
+ local install = require("luarocks.install")
+
+ if rockspec.supported_platforms then
+ if not deps.platforms_set then
+ deps.platforms_set = values_set(cfg.platforms)
+ end
+ local supported = nil
+ for _, plat in pairs(rockspec.supported_platforms) do
+ local neg, plat = plat:match("^(!?)(.*)")
+ if neg == "!" then
+ if deps.platforms_set[plat] then
+ return nil, "This rockspec for "..rockspec.package.." does not support "..plat.." platforms."
+ end
+ else
+ if deps.platforms_set[plat] then
+ supported = true
+ else
+ if supported == nil then
+ supported = false
+ end
+ end
+ end
+ end
+ if supported == false then
+ local plats = table.concat(cfg.platforms, ", ")
+ return nil, "This rockspec for "..rockspec.package.." does not support "..plats.." platforms."
+ end
+ end
+
+ deps.report_missing_dependencies(rockspec.name, rockspec.version, rockspec.dependencies, deps_mode)
+
+ local first_missing_dep = true
+
+ for _, dep in ipairs(rockspec.dependencies) do
+ if not match_dep(dep, nil, deps_mode) then
+ if first_missing_dep then
+ util.printout()
+ first_missing_dep = false
+ end
+
+ util.printout(("%s %s depends on %s (%s)"):format(
+ rockspec.name, rockspec.version, deps.show_dep(dep), rock_status(dep.name, deps_mode)))
+
+ if dep.constraints[1] and dep.constraints[1].no_upgrade then
+ util.printerr("This version of "..rockspec.name.." is designed for use with")
+ util.printerr(deps.show_dep(dep)..", but is configured to avoid upgrading it")
+ util.printerr("automatically. Please upgrade "..dep.name.." with")
+ util.printerr(" luarocks install "..dep.name)
+ util.printerr("or choose an older version of "..rockspec.name.." with")
+ util.printerr(" luarocks search "..rockspec.name)
+ return nil, "Failed matching dependencies"
+ end
+
+ local url, search_err = search.find_suitable_rock(dep)
+ if not url then
+ return nil, "Could not satisfy dependency "..deps.show_dep(dep)..": "..search_err
+ end
+ util.printout("Installing "..url)
+ local ok, install_err, errcode = install.command({deps_mode = deps_mode}, url)
+ if not ok then
+ return nil, "Failed installing dependency: "..url.." - "..install_err, errcode
+ end
+ end
+ end
+
+ return true
+end
+
+--- If filename matches a pattern, return the capture.
+-- For example, given "libfoo.so" and "lib?.so" is a pattern,
+-- returns "foo" (which can then be used to build names
+-- based on other patterns.
+-- @param file string: a filename
+-- @param pattern string: a pattern, where ? is to be matched by the filename.
+-- @return string The pattern, if found, or nil.
+local function deconstruct_pattern(file, pattern)
+ local depattern = "^"..(pattern:gsub("%.", "%%."):gsub("%*", ".*"):gsub("?", "(.*)")).."$"
+ return (file:match(depattern))
+end
+
+--- Construct all possible patterns for a name and add to the files array.
+-- Run through the patterns array replacing all occurrences of "?"
+-- with the given file name and store them in the files array.
+-- @param file string A raw name (e.g. "foo")
+-- @param array of string An array of patterns with "?" as the wildcard
+-- (e.g. {"?.so", "lib?.so"})
+-- @param files The array of constructed names
+local function add_all_patterns(file, patterns, files)
+ for _, pattern in ipairs(patterns) do
+ table.insert(files, (pattern:gsub("?", file)))
+ end
+end
+
+--- Set up path-related variables for external dependencies.
+-- For each key in the external_dependencies table in the
+-- rockspec file, four variables are created: _DIR, _BINDIR,
+-- _INCDIR and _LIBDIR. These are not overwritten
+-- if already set (e.g. by the LuaRocks config file or through the
+-- command-line). Values in the external_dependencies table
+-- are tables that may contain a "header" or a "library" field,
+-- with filenames to be tested for existence.
+-- @param rockspec table: The rockspec table.
+-- @param mode string: if "build" is given, checks all files;
+-- if "install" is given, do not scan for headers.
+-- @return boolean or (nil, string): True if no errors occurred, or
+-- nil and an error message if any test failed.
+function deps.check_external_deps(rockspec, mode)
+ assert(type(rockspec) == "table")
+
+ local fs = require("luarocks.fs")
+
+ local vars = rockspec.variables
+ local patterns = cfg.external_deps_patterns
+ local subdirs = cfg.external_deps_subdirs
+ if mode == "install" then
+ patterns = cfg.runtime_external_deps_patterns
+ subdirs = cfg.runtime_external_deps_subdirs
+ end
+ if rockspec.external_dependencies then
+ for name, ext_files in util.sortedpairs(rockspec.external_dependencies) do
+ local ok = true
+ local failed_files = {program = {}, header = {}, library = {}}
+ local failed_dirname
+ local failed_testfile
+ for _, extdir in ipairs(cfg.external_deps_dirs) do
+ ok = true
+ local prefix = vars[name.."_DIR"]
+ local dirs = {
+ BINDIR = { subdir = subdirs.bin, testfile = "program", pattern = patterns.bin },
+ INCDIR = { subdir = subdirs.include, testfile = "header", pattern = patterns.include },
+ LIBDIR = { subdir = subdirs.lib, testfile = "library", pattern = patterns.lib }
+ }
+ if mode == "install" then
+ dirs.INCDIR = nil
+ end
+ if not prefix then
+ prefix = extdir
+ end
+ if type(prefix) == "table" then
+ if prefix.bin then
+ dirs.BINDIR.subdir = prefix.bin
+ end
+ if prefix.include then
+ if dirs.INCDIR then
+ dirs.INCDIR.subdir = prefix.include
+ end
+ end
+ if prefix.lib then
+ dirs.LIBDIR.subdir = prefix.lib
+ end
+ prefix = prefix.prefix
+ end
+ for dirname, dirdata in util.sortedpairs(dirs) do
+ local paths
+ local path_var_value = vars[name.."_"..dirname]
+ if path_var_value then
+ paths = { path_var_value }
+ elseif type(dirdata.subdir) == "table" then
+ paths = {}
+ for i,v in ipairs(dirdata.subdir) do
+ paths[i] = dir.path(prefix, v)
+ end
+ else
+ paths = { dir.path(prefix, dirdata.subdir) }
+ end
+ dirdata.dir = paths[1]
+ local file = ext_files[dirdata.testfile]
+ if file then
+ local files = {}
+ if not file:match("%.") then
+ add_all_patterns(file, dirdata.pattern, files)
+ else
+ for _, pattern in ipairs(dirdata.pattern) do
+ local matched = deconstruct_pattern(file, pattern)
+ if matched then
+ add_all_patterns(matched, dirdata.pattern, files)
+ end
+ end
+ table.insert(files, file)
+ end
+ local found = false
+ for _, f in ipairs(files) do
+
+ -- small convenience hack
+ if f:match("%.so$") or f:match("%.dylib$") or f:match("%.dll$") then
+ f = f:gsub("%.[^.]+$", "."..cfg.external_lib_extension)
+ end
+
+ local pattern
+ if f:match("%*") then
+ pattern = f:gsub("%.", "%%."):gsub("%*", ".*")
+ f = "matching "..f
+ end
+
+ for _, d in ipairs(paths) do
+ if pattern then
+ for entry in fs.dir(d) do
+ if entry:match(pattern) then
+ found = true
+ break
+ end
+ end
+ else
+ found = fs.is_file(dir.path(d, f))
+ end
+ if found then
+ dirdata.dir = d
+ break
+ else
+ table.insert(failed_files[dirdata.testfile], f.." in "..d)
+ end
+ end
+ if found then
+ break
+ end
+ end
+ if not found then
+ ok = false
+ failed_dirname = dirname
+ failed_testfile = dirdata.testfile
+ break
+ end
+ end
+ end
+ if ok then
+ for dirname, dirdata in pairs(dirs) do
+ vars[name.."_"..dirname] = dirdata.dir
+ end
+ vars[name.."_DIR"] = prefix
+ break
+ end
+ end
+ if not ok then
+ local lines = {"Could not find "..failed_testfile.." file for "..name}
+
+ local failed_paths = {}
+ for _, failed_file in ipairs(failed_files[failed_testfile]) do
+ if not failed_paths[failed_file] then
+ failed_paths[failed_file] = true
+ table.insert(lines, " No file "..failed_file)
+ end
+ end
+
+ table.insert(lines, "You may have to install "..name.." in your system and/or pass "..name.."_DIR or "..name.."_"..failed_dirname.." to the luarocks command.")
+ table.insert(lines, "Example: luarocks install "..rockspec.name.." "..name.."_DIR=/usr/local")
+
+ return nil, table.concat(lines, "\n"), "dependency"
+ end
+ end
+ end
+ return true
+end
+
+--- Recursively add satisfied dependencies of a package to a table,
+-- to build a transitive closure of all dependent packages.
+-- Additionally ensures that `dependencies` table of the manifest is up-to-date.
+-- @param results table: The results table being built, maps package names to versions.
+-- @param manifest table: The manifest table containing dependencies.
+-- @param name string: Package name.
+-- @param version string: Package version.
+function deps.scan_deps(results, manifest, name, version, deps_mode)
+ assert(type(results) == "table")
+ assert(type(manifest) == "table")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local fetch = require("luarocks.fetch")
+
+ if results[name] then
+ return
+ end
+ if not manifest.dependencies then manifest.dependencies = {} end
+ local dependencies = manifest.dependencies
+ if not dependencies[name] then dependencies[name] = {} end
+ local dependencies_name = dependencies[name]
+ local deplist = dependencies_name[version]
+ local rockspec, err
+ if not deplist then
+ rockspec, err = fetch.load_local_rockspec(path.rockspec_file(name, version), false)
+ if not rockspec then
+ util.printerr("Couldn't load rockspec for "..name.." "..version..": "..err)
+ return
+ end
+ dependencies_name[version] = rockspec.dependencies
+ else
+ rockspec = { dependencies = deplist }
+ end
+ local matched = deps.match_deps(rockspec, nil, deps_mode)
+ results[name] = version
+ for _, match in pairs(matched) do
+ deps.scan_deps(results, manifest, match.name, match.version, deps_mode)
+ end
+end
+
+local valid_deps_modes = {
+ one = true,
+ order = true,
+ all = true,
+ none = true,
+}
+
+function deps.check_deps_mode_flag(flag)
+ return valid_deps_modes[flag]
+end
+
+function deps.get_deps_mode(flags)
+ if flags["deps-mode"] then
+ return flags["deps-mode"]
+ else
+ return cfg.deps_mode
+ end
+end
+
+function deps.deps_mode_to_flag(deps_mode)
+ return "--deps-mode="..deps_mode
+end
+
+return deps
diff --git a/Utils/luarocks/lua/luarocks/dir.lua b/Utils/luarocks/lua/luarocks/dir.lua
new file mode 100644
index 000000000..f72ebd6c1
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/dir.lua
@@ -0,0 +1,74 @@
+
+--- Generic utilities for handling pathnames.
+local dir = {}
+package.loaded["luarocks.dir"] = dir
+
+dir.separator = "/"
+
+--- Strip the path off a path+filename.
+-- @param pathname string: A path+name, such as "/a/b/c"
+-- or "\a\b\c".
+-- @return string: The filename without its path, such as "c".
+function dir.base_name(pathname)
+ assert(type(pathname) == "string")
+
+ local base = pathname:gsub("[/\\]*$", ""):match(".*[/\\]([^/\\]*)")
+ return base or pathname
+end
+
+--- Strip the name off a path+filename.
+-- @param pathname string: A path+name, such as "/a/b/c".
+-- @return string: The filename without its path, such as "/a/b".
+-- For entries such as "/a/b/", "/a" is returned. If there are
+-- no directory separators in input, "" is returned.
+function dir.dir_name(pathname)
+ assert(type(pathname) == "string")
+ return (pathname:gsub("/*$", ""):match("(.*)[/]+[^/]*")) or ""
+end
+
+--- Describe a path in a cross-platform way.
+-- Use this function to avoid platform-specific directory
+-- separators in other modules. Removes trailing slashes from
+-- each component given, to avoid repeated separators.
+-- Separators inside strings are kept, to handle URLs containing
+-- protocols.
+-- @param ... strings representing directories
+-- @return string: a string with a platform-specific representation
+-- of the path.
+function dir.path(...)
+ local t = {...}
+ while t[1] == "" do
+ table.remove(t, 1)
+ end
+ return (table.concat(t, "/"):gsub("([^:])/+", "%1/"):gsub("^/+", "/"):gsub("/*$", ""))
+end
+
+--- Split protocol and path from an URL or local pathname.
+-- URLs should be in the "protocol://path" format.
+-- For local pathnames, "file" is returned as the protocol.
+-- @param url string: an URL or a local pathname.
+-- @return string, string: the protocol, and the pathname without the protocol.
+function dir.split_url(url)
+ assert(type(url) == "string")
+
+ local protocol, pathname = url:match("^([^:]*)://(.*)")
+ if not protocol then
+ protocol = "file"
+ pathname = url
+ end
+ return protocol, pathname
+end
+
+--- Normalize a url or local path.
+-- URLs should be in the "protocol://path" format. System independent
+-- forward slashes are used, removing trailing and double slashes
+-- @param url string: an URL or a local pathname.
+-- @return string: Normalized result.
+function dir.normalize(name)
+ local protocol, pathname = dir.split_url(name)
+ pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/")
+ if protocol ~= "file" then pathname = protocol .."://"..pathname end
+ return pathname
+end
+
+return dir
diff --git a/Utils/luarocks/lua/luarocks/doc.lua b/Utils/luarocks/lua/luarocks/doc.lua
new file mode 100644
index 000000000..423ebe027
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/doc.lua
@@ -0,0 +1,157 @@
+
+--- Module implementing the LuaRocks "doc" command.
+-- Shows documentation for an installed rock.
+local doc = {}
+package.loaded["luarocks.doc"] = doc
+
+local util = require("luarocks.util")
+local search = require("luarocks.search")
+local path = require("luarocks.path")
+local dir = require("luarocks.dir")
+local fetch = require("luarocks.fetch")
+local fs = require("luarocks.fs")
+local download = require("luarocks.download")
+
+util.add_run_function(doc)
+doc.help_summary = "Show documentation for an installed rock."
+
+doc.help = [[
+ is an existing package name.
+Without any flags, tries to load the documentation
+using a series of heuristics.
+With these flags, return only the desired information:
+
+--home Open the home page of project.
+--list List documentation files only.
+
+For more information about a rock, see the 'show' command.
+]]
+
+local function show_homepage(homepage, name, version)
+ if not homepage then
+ return nil, "No 'homepage' field in rockspec for "..name.." "..version
+ end
+ util.printout("Opening "..homepage.." ...")
+ fs.browser(homepage)
+ return true
+end
+
+local function try_to_open_homepage(name, version)
+ local temp_dir, err = fs.make_temp_dir("doc-"..name.."-"..(version or ""))
+ if not temp_dir then
+ return nil, "Failed creating temporary directory: "..err
+ end
+ util.schedule_function(fs.delete, temp_dir)
+ local ok, err = fs.change_dir(temp_dir)
+ if not ok then return nil, err end
+ local filename, err = download.download("rockspec", name, version)
+ if not filename then return nil, err end
+ local rockspec, err = fetch.load_local_rockspec(filename)
+ if not rockspec then return nil, err end
+ fs.pop_dir()
+ local descript = rockspec.description or {}
+ if not descript.homepage then return nil, "No homepage defined for "..name end
+ return show_homepage(descript.homepage, name, version)
+end
+
+--- Driver function for "doc" command.
+-- @param name or nil: an existing package name.
+-- @param version string or nil: a version may also be passed.
+-- @return boolean: True if succeeded, nil on errors.
+function doc.command(flags, name, version)
+ if not name then
+ return nil, "Argument missing. "..util.see_help("doc")
+ end
+
+ name = name:lower()
+
+ local iname, iversion, repo = search.pick_installed_rock(name, version, flags["tree"])
+ if not iname then
+ util.printout(name..(version and " "..version or "").." is not installed. Looking for it in the rocks servers...")
+ return try_to_open_homepage(name, version)
+ end
+ name, version = iname, iversion
+
+ local rockspec, err = fetch.load_local_rockspec(path.rockspec_file(name, version, repo))
+ if not rockspec then return nil,err end
+ local descript = rockspec.description or {}
+
+ if flags["home"] then
+ return show_homepage(descript.homepage, name, version)
+ end
+
+ local directory = path.install_dir(name,version,repo)
+
+ local docdir
+ local directories = { "doc", "docs" }
+ for _, d in ipairs(directories) do
+ local dirname = dir.path(directory, d)
+ if fs.is_dir(dirname) then
+ docdir = dirname
+ break
+ end
+ end
+ if not docdir then
+ if descript.homepage and not flags["list"] then
+ util.printout("Local documentation directory not found -- opening "..descript.homepage.." ...")
+ fs.browser(descript.homepage)
+ return true
+ end
+ return nil, "Documentation directory not found for "..name.." "..version
+ end
+
+ docdir = dir.normalize(docdir):gsub("/+", "/")
+ local files = fs.find(docdir)
+ local htmlpatt = "%.html?$"
+ local extensions = { htmlpatt, "%.md$", "%.txt$", "%.textile$", "" }
+ local basenames = { "index", "readme", "manual" }
+
+ local porcelain = flags["porcelain"]
+ if #files > 0 then
+ util.title("Documentation files for "..name.." "..version, porcelain)
+ if porcelain then
+ for _, file in ipairs(files) do
+ util.printout(docdir.."/"..file)
+ end
+ else
+ util.printout(docdir.."/")
+ for _, file in ipairs(files) do
+ util.printout("\t"..file)
+ end
+ end
+ end
+
+ if flags["list"] then
+ return true
+ end
+
+ for _, extension in ipairs(extensions) do
+ for _, basename in ipairs(basenames) do
+ local filename = basename..extension
+ local found
+ for _, file in ipairs(files) do
+ if file:lower():match(filename) and ((not found) or #file < #found) then
+ found = file
+ end
+ end
+ if found then
+ local pathname = dir.path(docdir, found)
+ util.printout()
+ util.printout("Opening "..pathname.." ...")
+ util.printout()
+ local ok = fs.browser(pathname)
+ if not ok and not pathname:match(htmlpatt) then
+ local fd = io.open(pathname, "r")
+ util.printout(fd:read("*a"))
+ fd:close()
+ end
+ return true
+ end
+ end
+ end
+
+ return true
+end
+
+
+return doc
diff --git a/Utils/luarocks/lua/luarocks/download.lua b/Utils/luarocks/lua/luarocks/download.lua
new file mode 100644
index 000000000..e434cdbb2
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/download.lua
@@ -0,0 +1,109 @@
+
+--- Module implementing the luarocks "download" command.
+-- Download a rock from the repository.
+local download = {}
+package.loaded["luarocks.download"] = download
+
+local util = require("luarocks.util")
+local path = require("luarocks.path")
+local fetch = require("luarocks.fetch")
+local search = require("luarocks.search")
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+
+util.add_run_function(download)
+download.help_summary = "Download a specific rock file from a rocks server."
+download.help_arguments = "[--all] [--arch= | --source | --rockspec] [ []]"
+
+download.help = [[
+--all Download all files if there are multiple matches.
+--source Download .src.rock if available.
+--rockspec Download .rockspec if available.
+--arch= Download rock for a specific architecture.
+]]
+
+local function get_file(filename)
+ local protocol, pathname = dir.split_url(filename)
+ if protocol == "file" then
+ local ok, err = fs.copy(pathname, fs.current_dir(), cfg.perm_read)
+ if ok then
+ return pathname
+ else
+ return nil, err
+ end
+ else
+ return fetch.fetch_url(filename)
+ end
+end
+
+function download.download(arch, name, version, all)
+ local query = search.make_query(name, version)
+ if arch then query.arch = arch end
+ local search_err
+
+ if all then
+ if name == "" then query.exact_name = false end
+ local results = search.search_repos(query)
+ local has_result = false
+ local all_ok = true
+ local any_err = ""
+ for name, result in pairs(results) do
+ for version, items in pairs(result) do
+ for _, item in ipairs(items) do
+ -- Ignore provided rocks.
+ if item.arch ~= "installed" then
+ has_result = true
+ local filename = path.make_url(item.repo, name, version, item.arch)
+ local ok, err = get_file(filename)
+ if not ok then
+ all_ok = false
+ any_err = any_err .. "\n" .. err
+ end
+ end
+ end
+ end
+ end
+
+ if has_result then
+ return all_ok, any_err
+ end
+ else
+ local url
+ url, search_err = search.find_suitable_rock(query)
+ if url then
+ return get_file(url)
+ end
+ end
+ return nil, "Could not find a result named "..name..(version and " "..version or "")..
+ (search_err and ": "..search_err or ".")
+end
+
+--- Driver function for the "download" command.
+-- @param name string: a rock name.
+-- @param version string or nil: if the name of a package is given, a
+-- version may also be passed.
+-- @return boolean or (nil, string): true if successful or nil followed
+-- by an error message.
+function download.command(flags, name, version)
+ assert(type(version) == "string" or not version)
+ if type(name) ~= "string" and not flags["all"] then
+ return nil, "Argument missing. "..util.see_help("download")
+ end
+ if not name then name, version = "", "" end
+
+ local arch
+
+ if flags["source"] then
+ arch = "src"
+ elseif flags["rockspec"] then
+ arch = "rockspec"
+ elseif flags["arch"] then
+ arch = flags["arch"]
+ end
+
+ local dl, err = download.download(arch, name:lower(), version, flags["all"])
+ return dl and true, err
+end
+
+return download
diff --git a/Utils/luarocks/lua/luarocks/fetch.lua b/Utils/luarocks/lua/luarocks/fetch.lua
new file mode 100644
index 000000000..76f366cd2
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch.lua
@@ -0,0 +1,394 @@
+
+--- Functions related to fetching and loading local and remote files.
+local fetch = {}
+package.loaded["luarocks.fetch"] = fetch
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local type_check = require("luarocks.type_check")
+local path = require("luarocks.path")
+local deps = require("luarocks.deps")
+local persist = require("luarocks.persist")
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+
+function fetch.is_basic_protocol(protocol, remote)
+ return protocol == "http" or protocol == "https" or protocol == "ftp" or (not remote and protocol == "file")
+end
+
+--- Fetch a local or remote file.
+-- Make a remote or local URL/pathname local, fetching the file if necessary.
+-- Other "fetch" and "load" functions use this function to obtain files.
+-- If a local pathname is given, it is returned as a result.
+-- @param url string: a local pathname or a remote URL.
+-- @param filename string or nil: this function attempts to detect the
+-- resulting local filename of the remote file as the basename of the URL;
+-- if that is not correct (due to a redirection, for example), the local
+-- filename can be given explicitly as this second argument.
+-- @return string or (nil, string, [string]): the absolute local pathname for the
+-- fetched file, or nil and a message in case of errors, followed by
+-- an optional error code.
+function fetch.fetch_url(url, filename, cache)
+ assert(type(url) == "string")
+ assert(type(filename) == "string" or not filename)
+
+ local protocol, pathname = dir.split_url(url)
+ if protocol == "file" then
+ return fs.absolute_name(pathname)
+ elseif fetch.is_basic_protocol(protocol, true) then
+ local ok, name = fs.download(url, filename, cache)
+ if not ok then
+ return nil, "Failed downloading "..url..(filename and " - "..filename or ""), "network"
+ end
+ return name
+ else
+ return nil, "Unsupported protocol "..protocol
+ end
+end
+
+--- For remote URLs, create a temporary directory and download URL inside it.
+-- This temporary directory will be deleted on program termination.
+-- For local URLs, just return the local pathname and its directory.
+-- @param url string: URL to be downloaded
+-- @param tmpname string: name pattern to use for avoiding conflicts
+-- when creating temporary directory.
+-- @param filename string or nil: local filename of URL to be downloaded,
+-- in case it can't be inferred from the URL.
+-- @return (string, string) or (nil, string, [string]): absolute local pathname of
+-- the fetched file and temporary directory name; or nil and an error message
+-- followed by an optional error code
+function fetch.fetch_url_at_temp_dir(url, tmpname, filename)
+ assert(type(url) == "string")
+ assert(type(tmpname) == "string")
+ assert(type(filename) == "string" or not filename)
+ filename = filename or dir.base_name(url)
+
+ local protocol, pathname = dir.split_url(url)
+ if protocol == "file" then
+ if fs.exists(pathname) then
+ return pathname, dir.dir_name(fs.absolute_name(pathname))
+ else
+ return nil, "File not found: "..pathname
+ end
+ else
+ local temp_dir, err = fs.make_temp_dir(tmpname)
+ if not temp_dir then
+ return nil, "Failed creating temporary directory "..tmpname..": "..err
+ end
+ util.schedule_function(fs.delete, temp_dir)
+ local ok, err = fs.change_dir(temp_dir)
+ if not ok then return nil, err end
+ local file, err, errcode = fetch.fetch_url(url, filename)
+ fs.pop_dir()
+ if not file then
+ return nil, "Error fetching file: "..err, errcode
+ end
+ return file, temp_dir
+ end
+end
+
+-- Determine base directory of a fetched URL by extracting its
+-- archive and looking for a directory in the root.
+-- @param file string: absolute local pathname of the fetched file
+-- @param temp_dir string: temporary directory in which URL was fetched.
+-- @param src_url string: URL to use when inferring base directory.
+-- @param src_dir string or nil: expected base directory (inferred
+-- from src_url if not given).
+-- @return (string, string) or (string, nil) or (nil, string):
+-- The inferred base directory and the one actually found (which may
+-- be nil if not found), or nil followed by an error message.
+-- The inferred dir is returned first to avoid confusion with errors,
+-- because it is never nil.
+function fetch.find_base_dir(file, temp_dir, src_url, src_dir)
+ local ok, err = fs.change_dir(temp_dir)
+ if not ok then return nil, err end
+ fs.unpack_archive(file)
+ local inferred_dir = src_dir or fetch.url_to_base_dir(src_url)
+ local found_dir = nil
+ if fs.exists(inferred_dir) then
+ found_dir = inferred_dir
+ else
+ util.printerr("Directory "..inferred_dir.." not found")
+ local files = fs.list_dir()
+ if files then
+ table.sort(files)
+ for i,filename in ipairs(files) do
+ if fs.is_dir(filename) then
+ util.printerr("Found "..filename)
+ found_dir = filename
+ break
+ end
+ end
+ end
+ end
+ fs.pop_dir()
+ return inferred_dir, found_dir
+end
+
+--- Obtain a rock and unpack it.
+-- If a directory is not given, a temporary directory will be created,
+-- which will be deleted on program termination.
+-- @param rock_file string: URL or filename of the rock.
+-- @param dest string or nil: if given, directory will be used as
+-- a permanent destination.
+-- @return string or (nil, string, [string]): the directory containing the contents
+-- of the unpacked rock.
+function fetch.fetch_and_unpack_rock(rock_file, dest)
+ assert(type(rock_file) == "string")
+ assert(type(dest) == "string" or not dest)
+
+ local name = dir.base_name(rock_file):match("(.*)%.[^.]*%.rock")
+
+ local rock_file, err, errcode = fetch.fetch_url_at_temp_dir(rock_file,"luarocks-rock-"..name)
+ if not rock_file then
+ return nil, "Could not fetch rock file: " .. err, errcode
+ end
+
+ rock_file = fs.absolute_name(rock_file)
+ local unpack_dir
+ if dest then
+ unpack_dir = dest
+ local ok, err = fs.make_dir(unpack_dir)
+ if not ok then
+ return nil, "Failed unpacking rock file: " .. err
+ end
+ else
+ unpack_dir = fs.make_temp_dir(name)
+ end
+ if not dest then
+ util.schedule_function(fs.delete, unpack_dir)
+ end
+ local ok, err = fs.change_dir(unpack_dir)
+ if not ok then return nil, err end
+ ok = fs.unzip(rock_file)
+ if not ok then
+ return nil, "Failed unpacking rock file: " .. rock_file
+ end
+ fs.pop_dir()
+ return unpack_dir
+end
+
+function fetch.url_to_base_dir(url)
+ -- for extensions like foo.tar.gz, "gz" is stripped first
+ local known_exts = {}
+ for _, ext in ipairs{"zip", "git", "tgz", "tar", "gz", "bz2"} do
+ known_exts[ext] = ""
+ end
+ local base = dir.base_name(url)
+ return (base:gsub("%.([^.]*)$", known_exts):gsub("%.tar", ""))
+end
+
+--- Back-end function that actually loads the local rockspec.
+-- Performs some validation and postprocessing of the rockspec contents.
+-- @param filename string: The local filename of the rockspec file.
+-- @param quick boolean: if true, skips some steps when loading
+-- rockspec.
+-- @return table or (nil, string): A table representing the rockspec
+-- or nil followed by an error message.
+function fetch.load_local_rockspec(filename, quick)
+ assert(type(filename) == "string")
+ filename = fs.absolute_name(filename)
+ local rockspec, err = persist.load_into_table(filename)
+ if not rockspec then
+ return nil, "Could not load rockspec file "..filename.." ("..err..")"
+ end
+ if cfg.branch and (type(rockspec.source) == "table") then
+ rockspec.source.branch = cfg.branch
+ end
+ local globals = err
+
+ if rockspec.rockspec_format then
+ if deps.compare_versions(rockspec.rockspec_format, type_check.rockspec_format) then
+ return nil, "Rockspec format "..rockspec.rockspec_format.." is not supported, please upgrade LuaRocks."
+ end
+ end
+
+ if not quick then
+ local ok, err = type_check.type_check_rockspec(rockspec, globals)
+ if not ok then
+ return nil, filename..": "..err
+ end
+ end
+
+ util.platform_overrides(rockspec.build)
+ util.platform_overrides(rockspec.dependencies)
+ util.platform_overrides(rockspec.external_dependencies)
+ util.platform_overrides(rockspec.source)
+ util.platform_overrides(rockspec.hooks)
+
+ local basename = dir.base_name(filename)
+ if basename == "rockspec" then
+ rockspec.name = rockspec.package:lower()
+ else
+ rockspec.name = basename:match("(.*)-[^-]*-[0-9]*")
+ if not rockspec.name then
+ return nil, "Expected filename in format 'name-version-revision.rockspec'."
+ end
+ end
+
+ local protocol, pathname = dir.split_url(rockspec.source.url)
+ if fetch.is_basic_protocol(protocol) then
+ rockspec.source.file = rockspec.source.file or dir.base_name(rockspec.source.url)
+ end
+ rockspec.source.protocol, rockspec.source.pathname = protocol, pathname
+
+ -- Temporary compatibility
+ if rockspec.source.cvs_module then rockspec.source.module = rockspec.source.cvs_module end
+ if rockspec.source.cvs_tag then rockspec.source.tag = rockspec.source.cvs_tag end
+
+ local name_version = rockspec.package:lower() .. "-" .. rockspec.version
+ if basename ~= "rockspec" and basename ~= name_version .. ".rockspec" then
+ return nil, "Inconsistency between rockspec filename ("..basename..") and its contents ("..name_version..".rockspec)."
+ end
+
+ rockspec.local_filename = filename
+ local filebase = rockspec.source.file or rockspec.source.url
+ local base = fetch.url_to_base_dir(filebase)
+ rockspec.source.dir = rockspec.source.dir
+ or rockspec.source.module
+ or ((filebase:match("%.lua$") or filebase:match("%.c$")) and ".")
+ or base
+ if rockspec.dependencies then
+ for i = 1, #rockspec.dependencies do
+ local parsed, err = deps.parse_dep(rockspec.dependencies[i])
+ if not parsed then
+ return nil, "Parse error processing dependency '"..rockspec.dependencies[i].."': "..tostring(err)
+ end
+ rockspec.dependencies[i] = parsed
+ end
+ else
+ rockspec.dependencies = {}
+ end
+ if not quick then
+ path.configure_paths(rockspec)
+ end
+
+ return rockspec
+end
+
+--- Load a local or remote rockspec into a table.
+-- This is the entry point for the LuaRocks tools.
+-- Only the LuaRocks runtime loader should use
+-- load_local_rockspec directly.
+-- @param filename string: Local or remote filename of a rockspec.
+-- @param location string or nil: Where to download. If not given,
+-- a temporary dir is created.
+-- @return table or (nil, string, [string]): A table representing the rockspec
+-- or nil followed by an error message and optional error code.
+function fetch.load_rockspec(filename, location)
+ assert(type(filename) == "string")
+
+ local name
+ local basename = dir.base_name(filename)
+ if basename == "rockspec" then
+ name = "rockspec"
+ else
+ name = basename:match("(.*)%.rockspec")
+ if not name then
+ return nil, "Filename '"..filename.."' does not look like a rockspec."
+ end
+ end
+
+ local err, errcode
+ if location then
+ local ok, err = fs.change_dir(location)
+ if not ok then return nil, err end
+ filename, err = fetch.fetch_url(filename)
+ fs.pop_dir()
+ else
+ filename, err, errcode = fetch.fetch_url_at_temp_dir(filename,"luarocks-rockspec-"..name)
+ end
+ if not filename then
+ return nil, err, errcode
+ end
+
+ return fetch.load_local_rockspec(filename)
+end
+
+--- Download sources for building a rock using the basic URL downloader.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Whether to extract the sources from
+-- the fetched source tarball or not.
+-- @param dest_dir string or nil: If set, will extract to the given directory;
+-- if not given, will extract to a temporary directory.
+-- @return (string, string) or (nil, string, [string]): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message and optional error code.
+function fetch.get_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(extract) == "boolean")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local url = rockspec.source.url
+ local name = rockspec.name.."-"..rockspec.version
+ local filename = rockspec.source.file
+ local source_file, store_dir
+ local ok, err, errcode
+ if dest_dir then
+ ok, err = fs.change_dir(dest_dir)
+ if not ok then return nil, err, "dest_dir" end
+ source_file, err, errcode = fetch.fetch_url(url, filename)
+ fs.pop_dir()
+ store_dir = dest_dir
+ else
+ source_file, store_dir, errcode = fetch.fetch_url_at_temp_dir(url, "luarocks-source-"..name, filename)
+ end
+ if not source_file then
+ return nil, err or store_dir, errcode
+ end
+ if rockspec.source.md5 then
+ if not fs.check_md5(source_file, rockspec.source.md5) then
+ return nil, "MD5 check for "..filename.." has failed.", "md5"
+ end
+ end
+ if extract then
+ local ok, err = fs.change_dir(store_dir)
+ if not ok then return nil, err end
+ ok, err = fs.unpack_archive(rockspec.source.file)
+ if not ok then return nil, err end
+ if not fs.exists(rockspec.source.dir) then
+ return nil, "Directory "..rockspec.source.dir.." not found inside archive "..rockspec.source.file, "source.dir", source_file, store_dir
+ end
+ fs.pop_dir()
+ end
+ return source_file, store_dir
+end
+
+--- Download sources for building a rock, calling the appropriate protocol method.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: When downloading compressed formats, whether to extract
+-- the sources from the fetched archive or not.
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- if not given, will extract to a temporary directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function fetch.fetch_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(extract) == "boolean")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local protocol = rockspec.source.protocol
+ local ok, proto
+ if fetch.is_basic_protocol(protocol) then
+ proto = fetch
+ else
+ ok, proto = pcall(require, "luarocks.fetch."..protocol:gsub("[+-]", "_"))
+ if not ok then
+ return nil, "Unknown protocol "..protocol
+ end
+ end
+
+ if cfg.only_sources_from
+ and rockspec.source.pathname
+ and #rockspec.source.pathname > 0 then
+ if #cfg.only_sources_from == 0 then
+ return nil, "Can't download "..rockspec.source.url.." -- download from remote servers disabled"
+ elseif rockspec.source.pathname:find(cfg.only_sources_from, 1, true) ~= 1 then
+ return nil, "Can't download "..rockspec.source.url.." -- only downloading from "..cfg.only_sources_from
+ end
+ end
+ return proto.get_sources(rockspec, extract, dest_dir)
+end
+
+return fetch
diff --git a/Utils/luarocks/lua/luarocks/fetch/cvs.lua b/Utils/luarocks/lua/luarocks/fetch/cvs.lua
new file mode 100644
index 000000000..ece711b6f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/cvs.lua
@@ -0,0 +1,55 @@
+
+--- Fetch back-end for retrieving sources from CVS.
+local cvs = {}
+
+local unpack = unpack or table.unpack
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+--- Download sources for building a rock, using CVS.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function cvs.get_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local cvs_cmd = rockspec.variables.CVS
+ local ok, err_msg = fs.is_tool_available(cvs_cmd, "CVS")
+ if not ok then
+ return nil, err_msg
+ end
+
+ local name_version = rockspec.name .. "-" .. rockspec.version
+ local module = rockspec.source.module or dir.base_name(rockspec.source.url)
+ local command = {cvs_cmd, "-d"..rockspec.source.pathname, "export", module}
+ if rockspec.source.tag then
+ table.insert(command, 4, "-r")
+ table.insert(command, 5, rockspec.source.tag)
+ end
+ local store_dir
+ if not dest_dir then
+ store_dir = fs.make_temp_dir(name_version)
+ if not store_dir then
+ return nil, "Failed creating temporary directory."
+ end
+ util.schedule_function(fs.delete, store_dir)
+ else
+ store_dir = dest_dir
+ end
+ local ok, err = fs.change_dir(store_dir)
+ if not ok then return nil, err end
+ if not fs.execute(unpack(command)) then
+ return nil, "Failed fetching files from CVS."
+ end
+ fs.pop_dir()
+ return module, store_dir
+end
+
+
+return cvs
diff --git a/Utils/luarocks/lua/luarocks/fetch/git.lua b/Utils/luarocks/lua/luarocks/fetch/git.lua
new file mode 100644
index 000000000..f61d89e9c
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/git.lua
@@ -0,0 +1,92 @@
+
+--- Fetch back-end for retrieving sources from GIT.
+local git = {}
+
+local unpack = unpack or table.unpack
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+--- Git >= 1.7.10 can clone a branch **or tag**, < 1.7.10 by branch only. We
+-- need to know this in order to build the appropriate command; if we can't
+-- clone by tag then we'll have to issue a subsequent command to check out the
+-- given tag.
+-- @return boolean: Whether Git can clone by tag.
+local function git_can_clone_by_tag(git_cmd)
+ local version_string = io.popen(fs.Q(git_cmd)..' --version'):read()
+ local major, minor, tiny = version_string:match('(%d-)%.(%d+)%.?(%d*)')
+ major, minor, tiny = tonumber(major), tonumber(minor), tonumber(tiny) or 0
+ local value = major > 1 or (major == 1 and (minor > 7 or (minor == 7 and tiny >= 10)))
+ git_can_clone_by_tag = function() return value end
+ return value
+end
+
+--- Download sources for building a rock, using git.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function git.get_sources(rockspec, extract, dest_dir, depth)
+ assert(type(rockspec) == "table")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local git_cmd = rockspec.variables.GIT
+ local name_version = rockspec.name .. "-" .. rockspec.version
+ local module = dir.base_name(rockspec.source.url)
+ -- Strip off .git from base name if present
+ module = module:gsub("%.git$", "")
+
+ local ok, err_msg = fs.is_tool_available(git_cmd, "Git")
+ if not ok then
+ return nil, err_msg
+ end
+
+ local store_dir
+ if not dest_dir then
+ store_dir = fs.make_temp_dir(name_version)
+ if not store_dir then
+ return nil, "Failed creating temporary directory."
+ end
+ util.schedule_function(fs.delete, store_dir)
+ else
+ store_dir = dest_dir
+ end
+ store_dir = fs.absolute_name(store_dir)
+ local ok, err = fs.change_dir(store_dir)
+ if not ok then return nil, err end
+
+ local command = {fs.Q(git_cmd), "clone", depth or "--depth=1", rockspec.source.url, module}
+ local tag_or_branch = rockspec.source.tag or rockspec.source.branch
+ -- If the tag or branch is explicitly set to "master" in the rockspec, then
+ -- we can avoid passing it to Git since it's the default.
+ if tag_or_branch == "master" then tag_or_branch = nil end
+ if tag_or_branch then
+ if git_can_clone_by_tag(git_cmd) then
+ -- The argument to `--branch` can actually be a branch or a tag as of
+ -- Git 1.7.10.
+ table.insert(command, 3, "--branch=" .. tag_or_branch)
+ end
+ end
+ if not fs.execute(unpack(command)) then
+ return nil, "Failed cloning git repository."
+ end
+ ok, err = fs.change_dir(module)
+ if not ok then return nil, err end
+ if tag_or_branch and not git_can_clone_by_tag() then
+ local checkout_command = {fs.Q(git_cmd), "checkout", tag_or_branch}
+ if not fs.execute(unpack(checkout_command)) then
+ return nil, 'Failed to check out the "' .. tag_or_branch ..'" tag or branch.'
+ end
+ end
+
+ fs.delete(dir.path(store_dir, module, ".git"))
+ fs.delete(dir.path(store_dir, module, ".gitignore"))
+ fs.pop_dir()
+ fs.pop_dir()
+ return module, store_dir
+end
+
+return git
diff --git a/Utils/luarocks/lua/luarocks/fetch/git_file.lua b/Utils/luarocks/lua/luarocks/fetch/git_file.lua
new file mode 100644
index 000000000..8d46bbca3
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/git_file.lua
@@ -0,0 +1,19 @@
+
+--- Fetch back-end for retrieving sources from local Git repositories.
+local git_file = {}
+
+local git = require("luarocks.fetch.git")
+
+--- Fetch sources for building a rock from a local Git repository.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function git_file.get_sources(rockspec, extract, dest_dir)
+ rockspec.source.url = rockspec.source.url:gsub("^git.file://", "")
+ return git.get_sources(rockspec, extract, dest_dir)
+end
+
+return git_file
diff --git a/Utils/luarocks/lua/luarocks/fetch/git_http.lua b/Utils/luarocks/lua/luarocks/fetch/git_http.lua
new file mode 100644
index 000000000..d85e2572d
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/git_http.lua
@@ -0,0 +1,26 @@
+
+--- Fetch back-end for retrieving sources from Git repositories
+-- that use http:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `git clone http://example.com/foo.git`
+-- you can use this in the rockspec:
+-- source = { url = "git+http://example.com/foo.git" }
+-- Prefer using the normal git:// fetch mode as it is more widely
+-- available in older versions of LuaRocks.
+local git_http = {}
+
+local git = require("luarocks.fetch.git")
+
+--- Fetch sources for building a rock from a local Git repository.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function git_http.get_sources(rockspec, extract, dest_dir)
+ rockspec.source.url = rockspec.source.url:gsub("^git.", "")
+ return git.get_sources(rockspec, extract, dest_dir, "--")
+end
+
+return git_http
diff --git a/Utils/luarocks/lua/luarocks/fetch/git_https.lua b/Utils/luarocks/lua/luarocks/fetch/git_https.lua
new file mode 100644
index 000000000..67f8ad6cc
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/git_https.lua
@@ -0,0 +1,7 @@
+--- Fetch back-end for retrieving sources from Git repositories
+-- that use https:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `git clone https://example.com/foo.git`
+-- you can use this in the rockspec:
+-- source = { url = "git+https://example.com/foo.git" }
+return require "luarocks.fetch.git_http"
diff --git a/Utils/luarocks/lua/luarocks/fetch/git_ssh.lua b/Utils/luarocks/lua/luarocks/fetch/git_ssh.lua
new file mode 100644
index 000000000..0c2c0750f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/git_ssh.lua
@@ -0,0 +1,32 @@
+--- Fetch back-end for retrieving sources from Git repositories
+-- that use ssh:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `git clone ssh://git@example.com/path/foo.git
+-- you can use this in the rockspec:
+-- source = { url = "git+ssh://git@example.com/path/foo.git" }
+-- It also handles scp-style ssh urls: git@example.com:path/foo.git,
+-- but you have to prepend the "git+ssh://" and why not use the "newer"
+-- style anyway?
+local git_ssh = {}
+
+local git = require("luarocks.fetch.git")
+
+--- Fetch sources for building a rock from a local Git repository.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function git_ssh.get_sources(rockspec, extract, dest_dir)
+ rockspec.source.url = rockspec.source.url:gsub("^git.", "")
+
+ -- Handle old-style scp-like git ssh urls
+ if rockspec.source.url:match("^ssh://[^/]+:[^%d]") then
+ rockspec.source.url = rockspec.source.url:gsub("^ssh://", "")
+ end
+
+ return git.get_sources(rockspec, extract, dest_dir, "--")
+end
+
+return git_ssh
diff --git a/Utils/luarocks/lua/luarocks/fetch/hg.lua b/Utils/luarocks/lua/luarocks/fetch/hg.lua
new file mode 100644
index 000000000..4cf8d0280
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/hg.lua
@@ -0,0 +1,65 @@
+
+--- Fetch back-end for retrieving sources from HG.
+local hg = {}
+
+local unpack = unpack or table.unpack
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+--- Download sources for building a rock, using hg.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function hg.get_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local hg_cmd = rockspec.variables.HG
+ local ok, err_msg = fs.is_tool_available(hg_cmd, "Mercurial")
+ if not ok then
+ return nil, err_msg
+ end
+
+ local name_version = rockspec.name .. "-" .. rockspec.version
+ -- Strip off special hg:// protocol type
+ local url = rockspec.source.url:gsub("^hg://", "")
+
+ local module = dir.base_name(url)
+
+ local command = {hg_cmd, "clone", url, module}
+ local tag_or_branch = rockspec.source.tag or rockspec.source.branch
+ if tag_or_branch then
+ command = {hg_cmd, "clone", "--rev", tag_or_branch, url, module}
+ end
+ local store_dir
+ if not dest_dir then
+ store_dir = fs.make_temp_dir(name_version)
+ if not store_dir then
+ return nil, "Failed creating temporary directory."
+ end
+ util.schedule_function(fs.delete, store_dir)
+ else
+ store_dir = dest_dir
+ end
+ local ok, err = fs.change_dir(store_dir)
+ if not ok then return nil, err end
+ if not fs.execute(unpack(command)) then
+ return nil, "Failed cloning hg repository."
+ end
+ ok, err = fs.change_dir(module)
+ if not ok then return nil, err end
+
+ fs.delete(dir.path(store_dir, module, ".hg"))
+ fs.delete(dir.path(store_dir, module, ".hgignore"))
+ fs.pop_dir()
+ fs.pop_dir()
+ return module, store_dir
+end
+
+
+return hg
diff --git a/Utils/luarocks/lua/luarocks/fetch/hg_http.lua b/Utils/luarocks/lua/luarocks/fetch/hg_http.lua
new file mode 100644
index 000000000..8f506daff
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/hg_http.lua
@@ -0,0 +1,24 @@
+
+--- Fetch back-end for retrieving sources from hg repositories
+-- that use http:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `hg clone http://example.com/foo`
+-- you can use this in the rockspec:
+-- source = { url = "hg+http://example.com/foo" }
+local hg_http = {}
+
+local hg = require("luarocks.fetch.hg")
+
+--- Download sources for building a rock, using hg over http.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function hg_http.get_sources(rockspec, extract, dest_dir)
+ rockspec.source.url = rockspec.source.url:gsub("^hg.", "")
+ return hg.get_sources(rockspec, extract, dest_dir)
+end
+
+return hg_http
diff --git a/Utils/luarocks/lua/luarocks/fetch/hg_https.lua b/Utils/luarocks/lua/luarocks/fetch/hg_https.lua
new file mode 100644
index 000000000..e67417fe8
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/hg_https.lua
@@ -0,0 +1,8 @@
+
+--- Fetch back-end for retrieving sources from hg repositories
+-- that use https:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `hg clone https://example.com/foo`
+-- you can use this in the rockspec:
+-- source = { url = "hg+https://example.com/foo" }
+return require "luarocks.fetch.hg_http"
diff --git a/Utils/luarocks/lua/luarocks/fetch/hg_ssh.lua b/Utils/luarocks/lua/luarocks/fetch/hg_ssh.lua
new file mode 100644
index 000000000..0c365fabe
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/hg_ssh.lua
@@ -0,0 +1,8 @@
+
+--- Fetch back-end for retrieving sources from hg repositories
+-- that use ssh:// transport. For example, for fetching a repository
+-- that requires the following command line:
+-- `hg clone ssh://example.com/foo`
+-- you can use this in the rockspec:
+-- source = { url = "hg+ssh://example.com/foo" }
+return require "luarocks.fetch.hg_http"
diff --git a/Utils/luarocks/lua/luarocks/fetch/sscm.lua b/Utils/luarocks/lua/luarocks/fetch/sscm.lua
new file mode 100644
index 000000000..5add10dba
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/sscm.lua
@@ -0,0 +1,44 @@
+
+--- Fetch back-end for retrieving sources from Surround SCM Server
+local sscm = {}
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+
+--- Download sources via Surround SCM Server for building a rock.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function sscm.get_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local sscm_cmd = rockspec.variables.SSCM
+ local module = rockspec.source.module or dir.base_name(rockspec.source.url)
+ local branch, repository = string.match(rockspec.source.pathname, "^([^/]*)/(.*)")
+ if not branch or not repository then
+ return nil, "Error retrieving branch and repository from rockspec."
+ end
+ -- Search for working directory.
+ local working_dir
+ local tmp = io.popen(string.format(sscm_cmd..[[ property "/" -d -b%s -p%s]], branch, repository))
+ for line in tmp:lines() do
+ --%c because a chr(13) comes in the end.
+ working_dir = string.match(line, "Working directory:[%s]*(.*)%c$")
+ if working_dir then break end
+ end
+ tmp:close()
+ if not working_dir then
+ return nil, "Error retrieving working directory from SSCM."
+ end
+ if not fs.execute(sscm_cmd, "get", "*", "-e" , "-r", "-b"..branch, "-p"..repository, "-tmodify", "-wreplace") then
+ return nil, "Failed fetching files from SSCM."
+ end
+ -- FIXME: This function does not honor the dest_dir parameter.
+ return module, working_dir
+end
+
+return sscm
diff --git a/Utils/luarocks/lua/luarocks/fetch/svn.lua b/Utils/luarocks/lua/luarocks/fetch/svn.lua
new file mode 100644
index 000000000..29bce1b5f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fetch/svn.lua
@@ -0,0 +1,64 @@
+
+--- Fetch back-end for retrieving sources from Subversion.
+local svn = {}
+
+local unpack = unpack or table.unpack
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+--- Download sources for building a rock, using Subversion.
+-- @param rockspec table: The rockspec table
+-- @param extract boolean: Unused in this module (required for API purposes.)
+-- @param dest_dir string or nil: If set, will extract to the given directory.
+-- @return (string, string) or (nil, string): The absolute pathname of
+-- the fetched source tarball and the temporary directory created to
+-- store it; or nil and an error message.
+function svn.get_sources(rockspec, extract, dest_dir)
+ assert(type(rockspec) == "table")
+ assert(type(dest_dir) == "string" or not dest_dir)
+
+ local svn_cmd = rockspec.variables.SVN
+ local ok, err_msg = fs.is_tool_available(svn_cmd, "--version", "Subversion")
+ if not ok then
+ return nil, err_msg
+ end
+
+ local name_version = rockspec.name .. "-" .. rockspec.version
+ local module = rockspec.source.module or dir.base_name(rockspec.source.url)
+ local url = rockspec.source.url:gsub("^svn://", "")
+ local command = {svn_cmd, "checkout", url, module}
+ if rockspec.source.tag then
+ table.insert(command, 5, "-r")
+ table.insert(command, 6, rockspec.source.tag)
+ end
+ local store_dir
+ if not dest_dir then
+ store_dir = fs.make_temp_dir(name_version)
+ if not store_dir then
+ return nil, "Failed creating temporary directory."
+ end
+ util.schedule_function(fs.delete, store_dir)
+ else
+ store_dir = dest_dir
+ end
+ local ok, err = fs.change_dir(store_dir)
+ if not ok then return nil, err end
+ if not fs.execute(unpack(command)) then
+ return nil, "Failed fetching files from Subversion."
+ end
+ ok, err = fs.change_dir(module)
+ if not ok then return nil, err end
+ for _, d in ipairs(fs.find(".")) do
+ if dir.base_name(d) == ".svn" then
+ fs.delete(dir.path(store_dir, module, d))
+ end
+ end
+ fs.pop_dir()
+ fs.pop_dir()
+ return module, store_dir
+end
+
+
+return svn
diff --git a/Utils/luarocks/lua/luarocks/fs.lua b/Utils/luarocks/lua/luarocks/fs.lua
new file mode 100644
index 000000000..54cc7d73d
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs.lua
@@ -0,0 +1,76 @@
+
+--- Proxy module for filesystem and platform abstractions.
+-- All code using "fs" code should require "luarocks.fs",
+-- and not the various platform-specific implementations.
+-- However, see the documentation of the implementation
+-- for the API reference.
+
+local pairs = pairs
+
+local fs = {}
+package.loaded["luarocks.fs"] = fs
+
+local cfg = require("luarocks.cfg")
+
+local pack = table.pack or function(...) return { n = select("#", ...), ... } end
+local unpack = table.unpack or unpack
+
+local old_popen, old_exec
+fs.verbose = function() -- patch io.popen and os.execute to display commands in verbose mode
+ if old_popen or old_exec then return end
+ old_popen = io.popen
+ io.popen = function(one, two)
+ if two == nil then
+ print("\nio.popen: ", one)
+ else
+ print("\nio.popen: ", one, "Mode:", two)
+ end
+ return old_popen(one, two)
+ end
+
+ old_exec = os.execute
+ os.execute = function(cmd)
+ -- redact api keys if present
+ print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."/" end)) )
+ local code = pack(old_exec(cmd))
+ print("Results: "..tostring(code.n))
+ for i = 1,code.n do
+ print(" "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i]))
+ end
+ return unpack(code, 1, code.n)
+ end
+end
+if cfg.verbose then fs.verbose() end
+
+local function load_fns(fs_table)
+ for name, fn in pairs(fs_table) do
+ if not fs[name] then
+ fs[name] = fn
+ end
+ end
+end
+
+-- Load platform-specific functions
+local loaded_platform = nil
+for _, platform in ipairs(cfg.platforms) do
+ local ok, fs_plat = pcall(require, "luarocks.fs."..platform)
+ if ok and fs_plat then
+ loaded_platform = platform
+ load_fns(fs_plat)
+ break
+ end
+end
+
+-- Load platform-independent pure-Lua functionality
+local fs_lua = require("luarocks.fs.lua")
+load_fns(fs_lua)
+
+-- Load platform-specific fallbacks for missing Lua modules
+local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools")
+if ok and fs_plat_tools then
+ load_fns(fs_plat_tools)
+ load_fns(require("luarocks.fs.tools"))
+end
+
+
+return fs
diff --git a/Utils/luarocks/lua/luarocks/fs/lua.lua b/Utils/luarocks/lua/luarocks/fs/lua.lua
new file mode 100644
index 000000000..41711eab6
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/lua.lua
@@ -0,0 +1,873 @@
+
+--- Native Lua implementation of filesystem and platform abstractions,
+-- using LuaFileSystem, LZLib, MD5 and LuaCurl.
+-- module("luarocks.fs.lua")
+local fs_lua = {}
+
+local fs = require("luarocks.fs")
+
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+local path = require("luarocks.path")
+
+local socket_ok, zip_ok, unzip_ok, lfs_ok, md5_ok, posix_ok, _
+local http, ftp, lrzip, luazip, lfs, md5, posix
+
+if cfg.fs_use_modules then
+ socket_ok, http = pcall(require, "socket.http")
+ _, ftp = pcall(require, "socket.ftp")
+ zip_ok, lrzip = pcall(require, "luarocks.tools.zip")
+ unzip_ok, luazip = pcall(require, "zip"); _G.zip = nil
+ lfs_ok, lfs = pcall(require, "lfs")
+ md5_ok, md5 = pcall(require, "md5")
+ posix_ok, posix = pcall(require, "posix")
+end
+
+local patch = require("luarocks.tools.patch")
+
+local dir_stack = {}
+
+local dir_separator = "/"
+
+--- Test is file/dir is writable.
+-- Warning: testing if a file/dir is writable does not guarantee
+-- that it will remain writable and therefore it is no replacement
+-- for checking the result of subsequent operations.
+-- @param file string: filename to test
+-- @return boolean: true if file exists, false otherwise.
+function fs_lua.is_writable(file)
+ assert(file)
+ file = dir.normalize(file)
+ local result
+ if fs.is_dir(file) then
+ local file2 = dir.path(file, '.tmpluarockstestwritable')
+ local fh = io.open(file2, 'wb')
+ result = fh ~= nil
+ if fh then fh:close() end
+ os.remove(file2)
+ else
+ local fh = io.open(file, 'r+b')
+ result = fh ~= nil
+ if fh then fh:close() end
+ end
+ return result
+end
+
+local function quote_args(command, ...)
+ local out = { command }
+ for _, arg in ipairs({...}) do
+ assert(type(arg) == "string")
+ out[#out+1] = fs.Q(arg)
+ end
+ return table.concat(out, " ")
+end
+
+--- Run the given command, quoting its arguments.
+-- The command is executed in the current directory in the dir stack.
+-- @param command string: The command to be executed. No quoting/escaping
+-- is applied.
+-- @param ... Strings containing additional arguments, which are quoted.
+-- @return boolean: true if command succeeds (status code 0), false
+-- otherwise.
+function fs_lua.execute(command, ...)
+ assert(type(command) == "string")
+ return fs.execute_string(quote_args(command, ...))
+end
+
+--- Run the given command, quoting its arguments, silencing its output.
+-- The command is executed in the current directory in the dir stack.
+-- Silencing is omitted if 'verbose' mode is enabled.
+-- @param command string: The command to be executed. No quoting/escaping
+-- is applied.
+-- @param ... Strings containing additional arguments, which will be quoted.
+-- @return boolean: true if command succeeds (status code 0), false
+-- otherwise.
+function fs_lua.execute_quiet(command, ...)
+ assert(type(command) == "string")
+ if cfg.verbose then -- omit silencing output
+ return fs.execute_string(quote_args(command, ...))
+ else
+ return fs.execute_string(fs.quiet(quote_args(command, ...)))
+ end
+end
+
+--- Checks if the given tool is available.
+-- The tool is executed using a flag, usually just to ask its version.
+-- @param tool_cmd string: The command to be used to check the tool's presence (e.g. hg in case of Mercurial)
+-- @param tool_name string: The actual name of the tool (e.g. Mercurial)
+-- @param arg string: The flag to pass to the tool. '--version' by default.
+function fs_lua.is_tool_available(tool_cmd, tool_name, arg)
+ assert(type(tool_cmd) == "string")
+ assert(type(tool_name) == "string")
+
+ arg = arg or "--version"
+ assert(type(arg) == "string")
+
+ if not fs.execute_quiet(fs.Q(tool_cmd), arg) then
+ local msg = "'%s' program not found. Make sure %s is installed and is available in your PATH " ..
+ "(or you may want to edit the 'variables.%s' value in file '%s')"
+ return nil, msg:format(tool_cmd, tool_name, tool_name:upper(), cfg.which_config().nearest)
+ else
+ return true
+ end
+end
+
+--- Check the MD5 checksum for a file.
+-- @param file string: The file to be checked.
+-- @param md5sum string: The string with the expected MD5 checksum.
+-- @return boolean: true if the MD5 checksum for 'file' equals 'md5sum', false + msg if not
+-- or if it could not perform the check for any reason.
+function fs_lua.check_md5(file, md5sum)
+ file = dir.normalize(file)
+ local computed, msg = fs.get_md5(file)
+ if not computed then
+ return false, msg
+ end
+ if computed:match("^"..md5sum) then
+ return true
+ else
+ return false, "Mismatch MD5 hash for file "..file
+ end
+end
+
+--- List the contents of a directory.
+-- @param at string or nil: directory to list (will be the current
+-- directory if none is given).
+-- @return table: an array of strings with the filenames representing
+-- the contents of a directory.
+function fs_lua.list_dir(at)
+ local result = {}
+ for file in fs.dir(at) do
+ result[#result+1] = file
+ end
+ return result
+end
+
+--- Iterate over the contents of a directory.
+-- @param at string or nil: directory to list (will be the current
+-- directory if none is given).
+-- @return function: an iterator function suitable for use with
+-- the for statement.
+function fs_lua.dir(at)
+ if not at then
+ at = fs.current_dir()
+ end
+ at = dir.normalize(at)
+ if not fs.is_dir(at) then
+ return function() end
+ end
+ return coroutine.wrap(function() fs.dir_iterator(at) end)
+end
+
+---------------------------------------------------------------------
+-- LuaFileSystem functions
+---------------------------------------------------------------------
+
+if lfs_ok then
+
+--- Run the given command.
+-- The command is executed in the current directory in the dir stack.
+-- @param cmd string: No quoting/escaping is applied to the command.
+-- @return boolean: true if command succeeds (status code 0), false
+-- otherwise.
+function fs_lua.execute_string(cmd)
+ local code = os.execute(cmd)
+ return (code == 0 or code == true)
+end
+
+--- Obtain current directory.
+-- Uses the module's internal dir stack.
+-- @return string: the absolute pathname of the current directory.
+function fs_lua.current_dir()
+ return lfs.currentdir()
+end
+
+--- Change the current directory.
+-- Uses the module's internal dir stack. This does not have exact
+-- semantics of chdir, as it does not handle errors the same way,
+-- but works well for our purposes for now.
+-- @param d string: The directory to switch to.
+function fs_lua.change_dir(d)
+ table.insert(dir_stack, lfs.currentdir())
+ d = dir.normalize(d)
+ return lfs.chdir(d)
+end
+
+--- Change directory to root.
+-- Allows leaving a directory (e.g. for deleting it) in
+-- a crossplatform way.
+function fs_lua.change_dir_to_root()
+ local current = lfs.currentdir()
+ if not current or current == "" then
+ return false
+ end
+ table.insert(dir_stack, current)
+ lfs.chdir("/") -- works on Windows too
+ return true
+end
+
+--- Change working directory to the previous in the dir stack.
+-- @return true if a pop ocurred, false if the stack was empty.
+function fs_lua.pop_dir()
+ local d = table.remove(dir_stack)
+ if d then
+ lfs.chdir(d)
+ return true
+ else
+ return false
+ end
+end
+
+--- Create a directory if it does not already exist.
+-- If any of the higher levels in the path name do not exist
+-- too, they are created as well.
+-- @param directory string: pathname of directory to create.
+-- @return boolean or (boolean, string): true on success or (false, error message) on failure.
+function fs_lua.make_dir(directory)
+ assert(type(directory) == "string")
+ directory = dir.normalize(directory)
+ local path = nil
+ if directory:sub(2, 2) == ":" then
+ path = directory:sub(1, 2)
+ directory = directory:sub(4)
+ else
+ if directory:match("^/") then
+ path = ""
+ end
+ end
+ for d in directory:gmatch("([^"..dir.separator.."]+)"..dir.separator.."*") do
+ path = path and path .. dir.separator .. d or d
+ local mode = lfs.attributes(path, "mode")
+ if not mode then
+ local ok, err = lfs.mkdir(path)
+ if not ok then
+ return false, err
+ end
+ ok, err = fs.chmod(path, cfg.perm_exec)
+ if not ok then
+ return false, err
+ end
+ elseif mode ~= "directory" then
+ return false, path.." is not a directory"
+ end
+ end
+ return true
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param d string: pathname of directory to remove.
+function fs_lua.remove_dir_if_empty(d)
+ assert(d)
+ d = dir.normalize(d)
+ lfs.rmdir(d)
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param d string: pathname of directory to remove.
+function fs_lua.remove_dir_tree_if_empty(d)
+ assert(d)
+ d = dir.normalize(d)
+ for i=1,10 do
+ lfs.rmdir(d)
+ d = dir.dir_name(d)
+ end
+end
+
+--- Copy a file.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @param perms string or nil: Permissions for destination file,
+-- or nil to use the source filename permissions
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function fs_lua.copy(src, dest, perms)
+ assert(src and dest)
+ src = dir.normalize(src)
+ dest = dir.normalize(dest)
+ local destmode = lfs.attributes(dest, "mode")
+ if destmode == "directory" then
+ dest = dir.path(dest, dir.base_name(src))
+ end
+ if not perms then perms = fs.get_permissions(src) end
+ local src_h, err = io.open(src, "rb")
+ if not src_h then return nil, err end
+ local dest_h, err = io.open(dest, "w+b")
+ if not dest_h then src_h:close() return nil, err end
+ while true do
+ local block = src_h:read(8192)
+ if not block then break end
+ dest_h:write(block)
+ end
+ src_h:close()
+ dest_h:close()
+ fs.chmod(dest, perms)
+ return true
+end
+
+--- Implementation function for recursive copy of directory contents.
+-- Assumes paths are normalized.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @param perms string or nil: Optional permissions.
+-- If not given, permissions of the source are copied over to the destination.
+-- @return boolean or (boolean, string): true on success, false on failure
+local function recursive_copy(src, dest, perms)
+ local srcmode = lfs.attributes(src, "mode")
+
+ if srcmode == "file" then
+ local ok = fs.copy(src, dest, perms)
+ if not ok then return false end
+ elseif srcmode == "directory" then
+ local subdir = dir.path(dest, dir.base_name(src))
+ local ok, err = fs.make_dir(subdir)
+ if not ok then return nil, err end
+ for file in lfs.dir(src) do
+ if file ~= "." and file ~= ".." then
+ local ok = recursive_copy(dir.path(src, file), subdir, perms)
+ if not ok then return false end
+ end
+ end
+ end
+ return true
+end
+
+--- Recursively copy the contents of a directory.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @param perms string or nil: Optional permissions.
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function fs_lua.copy_contents(src, dest, perms)
+ assert(src and dest)
+ src = dir.normalize(src)
+ dest = dir.normalize(dest)
+ assert(lfs.attributes(src, "mode") == "directory")
+
+ for file in lfs.dir(src) do
+ if file ~= "." and file ~= ".." then
+ local ok = recursive_copy(dir.path(src, file), dest, perms)
+ if not ok then
+ return false, "Failed copying "..src.." to "..dest
+ end
+ end
+ end
+ return true
+end
+
+--- Implementation function for recursive removal of directories.
+-- Assumes paths are normalized.
+-- @param name string: Pathname of file
+-- @return boolean or (boolean, string): true on success,
+-- or nil and an error message on failure.
+local function recursive_delete(name)
+ local ok = os.remove(name)
+ if ok then return true end
+ local pok, ok, err = pcall(function()
+ for file in lfs.dir(name) do
+ if file ~= "." and file ~= ".." then
+ local ok, err = recursive_delete(dir.path(name, file))
+ if not ok then return nil, err end
+ end
+ end
+ local ok, err = lfs.rmdir(name)
+ return ok, (not ok) and err
+ end)
+ if pok then
+ return ok, err
+ else
+ return pok, ok
+ end
+end
+
+--- Delete a file or a directory and all its contents.
+-- @param name string: Pathname of source
+-- @return nil
+function fs_lua.delete(name)
+ name = dir.normalize(name)
+ recursive_delete(name)
+end
+
+--- Internal implementation function for fs.dir.
+-- Yields a filename on each iteration.
+-- @param at string: directory to list
+-- @return nil
+function fs_lua.dir_iterator(at)
+ for file in lfs.dir(at) do
+ if file ~= "." and file ~= ".." then
+ coroutine.yield(file)
+ end
+ end
+end
+
+--- Implementation function for recursive find.
+-- Assumes paths are normalized.
+-- @param cwd string: Current working directory in recursion.
+-- @param prefix string: Auxiliary prefix string to form pathname.
+-- @param result table: Array of strings where results are collected.
+local function recursive_find(cwd, prefix, result)
+ for file in lfs.dir(cwd) do
+ if file ~= "." and file ~= ".." then
+ local item = prefix .. file
+ table.insert(result, item)
+ local pathname = dir.path(cwd, file)
+ if lfs.attributes(pathname, "mode") == "directory" then
+ recursive_find(pathname, item..dir_separator, result)
+ end
+ end
+ end
+end
+
+--- Recursively scan the contents of a directory.
+-- @param at string or nil: directory to scan (will be the current
+-- directory if none is given).
+-- @return table: an array of strings with the filenames representing
+-- the contents of a directory.
+function fs_lua.find(at)
+ assert(type(at) == "string" or not at)
+ if not at then
+ at = fs.current_dir()
+ end
+ at = dir.normalize(at)
+ if not fs.is_dir(at) then
+ return {}
+ end
+ local result = {}
+ recursive_find(at, "", result)
+ return result
+end
+
+--- Test for existance of a file.
+-- @param file string: filename to test
+-- @return boolean: true if file exists, false otherwise.
+function fs_lua.exists(file)
+ assert(file)
+ file = dir.normalize(file)
+ return type(lfs.attributes(file)) == "table"
+end
+
+--- Test is pathname is a directory.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a directory, false otherwise.
+function fs_lua.is_dir(file)
+ assert(file)
+ file = dir.normalize(file)
+ return lfs.attributes(file, "mode") == "directory"
+end
+
+--- Test is pathname is a regular file.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a file, false otherwise.
+function fs_lua.is_file(file)
+ assert(file)
+ file = dir.normalize(file)
+ return lfs.attributes(file, "mode") == "file"
+end
+
+function fs_lua.set_time(file, time)
+ file = dir.normalize(file)
+ return lfs.touch(file, time)
+end
+
+end
+
+---------------------------------------------------------------------
+-- LuaZip functions
+---------------------------------------------------------------------
+
+if zip_ok then
+
+function fs_lua.zip(zipfile, ...)
+ return lrzip.zip(zipfile, ...)
+end
+
+end
+
+if unzip_ok then
+--- Uncompress files from a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be extracted.
+-- @return boolean: true on success, false on failure.
+function fs_lua.unzip(zipfile)
+ local zipfile, err = luazip.open(zipfile)
+ if not zipfile then return nil, err end
+ local files = zipfile:files()
+ local file = files()
+ repeat
+ if file.filename:sub(#file.filename) == "/" then
+ local ok, err = fs.make_dir(dir.path(fs.current_dir(), file.filename))
+ if not ok then return nil, err end
+ else
+ local base = dir.dir_name(file.filename)
+ if base ~= "" then
+ base = dir.path(fs.current_dir(), base)
+ if not fs.is_dir(base) then
+ local ok, err = fs.make_dir(base)
+ if not ok then return nil, err end
+ end
+ end
+ local rf, err = zipfile:open(file.filename)
+ if not rf then zipfile:close(); return nil, err end
+ local contents = rf:read("*a")
+ rf:close()
+ local wf, err = io.open(dir.path(fs.current_dir(), file.filename), "wb")
+ if not wf then zipfile:close(); return nil, err end
+ wf:write(contents)
+ wf:close()
+ end
+ file = files()
+ until not file
+ zipfile:close()
+ return true
+end
+
+end
+
+---------------------------------------------------------------------
+-- LuaSocket functions
+---------------------------------------------------------------------
+
+if socket_ok then
+
+local ltn12 = require("ltn12")
+local luasec_ok, https = pcall(require, "ssl.https")
+
+local redirect_protocols = {
+ http = http,
+ https = luasec_ok and https,
+}
+
+local function request(url, method, http, loop_control)
+ local result = {}
+
+ local proxy = cfg.http_proxy
+ if type(proxy) ~= "string" then proxy = nil end
+ -- LuaSocket's http.request crashes when given URLs missing the scheme part.
+ if proxy and not proxy:find("://") then
+ proxy = "http://" .. proxy
+ end
+
+ if cfg.show_downloads then
+ io.write(method.." "..url.." ...\n")
+ end
+ local dots = 0
+ if cfg.connection_timeout and cfg.connection_timeout > 0 then
+ http.TIMEOUT = cfg.connection_timeout
+ end
+ local res, status, headers, err = http.request {
+ url = url,
+ proxy = proxy,
+ method = method,
+ redirect = false,
+ sink = ltn12.sink.table(result),
+ step = cfg.show_downloads and function(...)
+ io.write(".")
+ io.flush()
+ dots = dots + 1
+ if dots == 70 then
+ io.write("\n")
+ dots = 0
+ end
+ return ltn12.pump.step(...)
+ end,
+ headers = {
+ ["user-agent"] = cfg.user_agent.." via LuaSocket"
+ },
+ }
+ if cfg.show_downloads then
+ io.write("\n")
+ end
+ if not res then
+ return nil, status
+ elseif status == 301 or status == 302 then
+ local location = headers.location
+ if location then
+ local protocol, rest = dir.split_url(location)
+ if redirect_protocols[protocol] then
+ if not loop_control then
+ loop_control = {}
+ elseif loop_control[location] then
+ return nil, "Redirection loop -- broken URL?"
+ end
+ loop_control[url] = true
+ return request(location, method, redirect_protocols[protocol], loop_control)
+ else
+ return nil, "URL redirected to unsupported protocol - install luasec to get HTTPS support.", "https"
+ end
+ end
+ return nil, err
+ elseif status ~= 200 then
+ return nil, err
+ else
+ return result, status, headers, err
+ end
+end
+
+local function http_request(url, http, cached)
+ if cached then
+ local tsfd = io.open(cached..".timestamp", "r")
+ if tsfd then
+ local timestamp = tsfd:read("*a")
+ tsfd:close()
+ local result, status, headers, err = request(url, "HEAD", http)
+ if status == 200 and headers["last-modified"] == timestamp then
+ return true
+ end
+ if not result then
+ return nil, status, headers
+ end
+ end
+ end
+ local result, status, headers, err = request(url, "GET", http)
+ if result then
+ if cached and headers["last-modified"] then
+ local tsfd = io.open(cached..".timestamp", "w")
+ if tsfd then
+ tsfd:write(headers["last-modified"])
+ tsfd:close()
+ end
+ end
+ return table.concat(result)
+ else
+ return nil, status, headers
+ end
+end
+
+local downloader_warning = false
+
+--- Download a remote file.
+-- @param url string: URL to be fetched.
+-- @param filename string or nil: this function attempts to detect the
+-- resulting local filename of the remote file as the basename of the URL;
+-- if that is not correct (due to a redirection, for example), the local
+-- filename can be given explicitly as this second argument.
+-- @return (boolean, string): true and the filename on success,
+-- false and the error message on failure.
+function fs_lua.download(url, filename, cache)
+ assert(type(url) == "string")
+ assert(type(filename) == "string" or not filename)
+
+ filename = fs.absolute_name(filename or dir.base_name(url))
+
+ -- delegate to the configured downloader so we don't have to deal with whitelists
+ if cfg.no_proxy then
+ return fs.use_downloader(url, filename, cache)
+ end
+
+ local content, err, https_err
+ if util.starts_with(url, "http:") then
+ content, err, https_err = http_request(url, http, cache and filename)
+ elseif util.starts_with(url, "ftp:") then
+ content, err = ftp.get(url)
+ elseif util.starts_with(url, "https:") then
+ -- skip LuaSec when proxy is enabled since it is not supported
+ if luasec_ok and not cfg.https_proxy then
+ content, err = http_request(url, https, cache and filename)
+ else
+ https_err = true
+ end
+ else
+ err = "Unsupported protocol"
+ end
+ if https_err then
+ if not downloader_warning then
+ util.printerr("Warning: falling back to "..cfg.downloader.." - install luasec to get native HTTPS support")
+ downloader_warning = true
+ end
+ return fs.use_downloader(url, filename, cache)
+ end
+ if cache and content == true then
+ return true, filename
+ end
+ if not content then
+ return false, tostring(err)
+ end
+ local file = io.open(filename, "wb")
+ if not file then return false end
+ file:write(content)
+ file:close()
+ return true, filename
+end
+
+else --...if socket_ok == false then
+
+function fs_lua.download(url, filename, cache)
+ return fs.use_downloader(url, filename, cache)
+end
+
+end
+---------------------------------------------------------------------
+-- MD5 functions
+---------------------------------------------------------------------
+
+if md5_ok then
+
+-- Support the interface of lmd5 by lhf in addition to md5 by Roberto
+-- and the keplerproject.
+if not md5.sumhexa and md5.digest then
+ md5.sumhexa = function(msg)
+ return md5.digest(msg)
+ end
+end
+
+--- Get the MD5 checksum for a file.
+-- @param file string: The file to be computed.
+-- @return string: The MD5 checksum or nil + error
+function fs_lua.get_md5(file)
+ file = fs.absolute_name(file)
+ local file_handler = io.open(file, "rb")
+ if not file_handler then return nil, "Failed to open file for reading: "..file end
+ local computed = md5.sumhexa(file_handler:read("*a"))
+ file_handler:close()
+ if computed then return computed end
+ return nil, "Failed to compute MD5 hash for file "..file
+end
+
+end
+
+---------------------------------------------------------------------
+-- POSIX functions
+---------------------------------------------------------------------
+
+if posix_ok then
+
+local octal_to_rwx = {
+ ["0"] = "---",
+ ["1"] = "--x",
+ ["2"] = "-w-",
+ ["3"] = "-wx",
+ ["4"] = "r--",
+ ["5"] = "r-x",
+ ["6"] = "rw-",
+ ["7"] = "rwx",
+}
+
+function fs_lua.chmod(file, mode)
+ -- LuaPosix (as of 5.1.15) does not support octal notation...
+ if mode:sub(1,1) == "0" then
+ local new_mode = {}
+ for c in mode:sub(-3):gmatch(".") do
+ table.insert(new_mode, octal_to_rwx[c])
+ end
+ mode = table.concat(new_mode)
+ end
+ local err = posix.chmod(file, mode)
+ return err == 0
+end
+
+function fs_lua.get_permissions(file)
+ return posix.stat(file, "mode")
+end
+
+--- Create a temporary directory.
+-- @param name string: name pattern to use for avoiding conflicts
+-- when creating temporary directory.
+-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure.
+function fs_lua.make_temp_dir(name)
+ assert(type(name) == "string")
+ name = dir.normalize(name)
+
+ return posix.mkdtemp((os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX")
+end
+
+end
+
+---------------------------------------------------------------------
+-- Other functions
+---------------------------------------------------------------------
+
+--- Apply a patch.
+-- @param patchname string: The filename of the patch.
+-- @param patchdata string or nil: The actual patch as a string.
+function fs_lua.apply_patch(patchname, patchdata)
+ local p, all_ok = patch.read_patch(patchname, patchdata)
+ if not all_ok then
+ return nil, "Failed reading patch "..patchname
+ end
+ if p then
+ return patch.apply_patch(p, 1)
+ end
+end
+
+--- Move a file.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @param perms string or nil: Permissions for destination file,
+-- or nil to use the source filename permissions.
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function fs_lua.move(src, dest, perms)
+ assert(src and dest)
+ if fs.exists(dest) and not fs.is_dir(dest) then
+ return false, "File already exists: "..dest
+ end
+ local ok, err = fs.copy(src, dest, perms)
+ if not ok then
+ return false, err
+ end
+ fs.delete(src)
+ if fs.exists(src) then
+ return false, "Failed move: could not delete "..src.." after copy."
+ end
+ return true
+end
+
+--- Check if user has write permissions for the command.
+-- Assumes the configuration variables under cfg have been previously set up.
+-- @param flags table: the flags table passed to run() drivers.
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function fs_lua.check_command_permissions(flags)
+ local root_dir = path.root_dir(cfg.rocks_dir)
+ local ok = true
+ local err = ""
+ for _, dir in ipairs { cfg.rocks_dir, root_dir } do
+ if fs.exists(dir) and not fs.is_writable(dir) then
+ ok = false
+ err = "Your user does not have write permissions in " .. dir
+ break
+ end
+ end
+ if ok and not fs.exists(root_dir) then
+ local root = fs.root_of(root_dir)
+ local parent = root_dir
+ repeat
+ parent = dir.dir_name(parent)
+ if parent == "" then
+ parent = root
+ end
+ until parent == root or fs.exists(parent)
+ if not fs.is_writable(parent) then
+ ok = false
+ err = root_dir.." does not exist and your user does not have write permissions in " .. parent
+ end
+ end
+ if ok then
+ return true
+ else
+ if flags["local"] then
+ err = err .. " \n-- please check your permissions."
+ else
+ err = err .. " \n-- you may want to run as a privileged user or use your local tree with --local."
+ end
+ return nil, err
+ end
+end
+
+--- Check whether a file is a Lua script
+-- When the file can be succesfully compiled by the configured
+-- Lua interpreter, it's considered to be a valid Lua file.
+-- @param name filename of file to check
+-- @return boolean true, if it is a Lua script, false otherwise
+function fs_lua.is_lua(name)
+ name = name:gsub([[%\]],"/") -- normalize on fw slash to prevent escaping issues
+ local lua = fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)) -- get lua interpreter configured
+ -- execute on configured interpreter, might not be the same as the interpreter LR is run on
+ local result = fs.execute_string(lua..[[ -e "if loadfile(']]..name..[[') then os.exit() else os.exit(1) end"]])
+ return (result == true)
+end
+
+return fs_lua
diff --git a/Utils/luarocks/lua/luarocks/fs/tools.lua b/Utils/luarocks/lua/luarocks/fs/tools.lua
new file mode 100644
index 000000000..ed51b5458
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/tools.lua
@@ -0,0 +1,156 @@
+
+--- Common fs operations implemented with third-party tools.
+local tools = {}
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+
+local vars = cfg.variables
+
+local dir_stack = {}
+
+--- Obtain current directory.
+-- Uses the module's internal directory stack.
+-- @return string: the absolute pathname of the current directory.
+function tools.current_dir()
+ local current = cfg.cache_pwd
+ if not current then
+ local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD)))
+ current = pipe:read("*l")
+ pipe:close()
+ cfg.cache_pwd = current
+ end
+ for _, directory in ipairs(dir_stack) do
+ current = fs.absolute_name(directory, current)
+ end
+ return current
+end
+
+--- Change the current directory.
+-- Uses the module's internal directory stack. This does not have exact
+-- semantics of chdir, as it does not handle errors the same way,
+-- but works well for our purposes for now.
+-- @param directory string: The directory to switch to.
+-- @return boolean or (nil, string): true if successful, (nil, error message) if failed.
+function tools.change_dir(directory)
+ assert(type(directory) == "string")
+ if fs.is_dir(directory) then
+ table.insert(dir_stack, directory)
+ return true
+ end
+ return nil, "directory not found: "..directory
+end
+
+--- Change directory to root.
+-- Allows leaving a directory (e.g. for deleting it) in
+-- a crossplatform way.
+function tools.change_dir_to_root()
+ table.insert(dir_stack, "/")
+end
+
+--- Change working directory to the previous in the directory stack.
+function tools.pop_dir()
+ local directory = table.remove(dir_stack)
+ return directory ~= nil
+end
+
+--- Run the given command.
+-- The command is executed in the current directory in the directory stack.
+-- @param cmd string: No quoting/escaping is applied to the command.
+-- @return boolean: true if command succeeds (status code 0), false
+-- otherwise.
+function tools.execute_string(cmd)
+ local current = fs.current_dir()
+ if not current then return false end
+ cmd = fs.command_at(current, cmd)
+ local code = os.execute(cmd)
+ if code == 0 or code == true then
+ return true
+ else
+ return false
+ end
+end
+
+--- Internal implementation function for fs.dir.
+-- Yields a filename on each iteration.
+-- @param at string: directory to list
+-- @return nil
+function tools.dir_iterator(at)
+ local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS)))
+ for file in pipe:lines() do
+ if file ~= "." and file ~= ".." then
+ coroutine.yield(file)
+ end
+ end
+ pipe:close()
+end
+
+--- Download a remote file.
+-- @param url string: URL to be fetched.
+-- @param filename string or nil: this function attempts to detect the
+-- resulting local filename of the remote file as the basename of the URL;
+-- if that is not correct (due to a redirection, for example), the local
+-- filename can be given explicitly as this second argument.
+-- @return (boolean, string): true and the filename on success,
+-- false and the error message on failure.
+function tools.use_downloader(url, filename, cache)
+ assert(type(url) == "string")
+ assert(type(filename) == "string" or not filename)
+
+ filename = fs.absolute_name(filename or dir.base_name(url))
+
+ local ok
+ if cfg.downloader == "wget" then
+ local wget_cmd = fs.Q(vars.WGET).." "..vars.WGETNOCERTFLAG.." --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet "
+ if cfg.connection_timeout and cfg.connection_timeout > 0 then
+ wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 "
+ end
+ if cache then
+ -- --timestamping is incompatible with --output-document,
+ -- but that's not a problem for our use cases.
+ fs.change_dir(dir.dir_name(filename))
+ ok = fs.execute_quiet(wget_cmd.." --timestamping ", url)
+ fs.pop_dir()
+ elseif filename then
+ ok = fs.execute_quiet(wget_cmd.." --output-document ", filename, url)
+ else
+ ok = fs.execute_quiet(wget_cmd, url)
+ end
+ elseif cfg.downloader == "curl" then
+ local curl_cmd = fs.Q(vars.CURL).." "..vars.CURLNOCERTFLAG.." -f -L --user-agent \""..cfg.user_agent.." via curl\" "
+ if cfg.connection_timeout and cfg.connection_timeout > 0 then
+ curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." "
+ end
+ ok = fs.execute_string(fs.quiet_stderr(curl_cmd..fs.Q(url).." > "..fs.Q(filename)))
+ end
+ if ok then
+ return true, filename
+ else
+ return false
+ end
+end
+
+local md5_cmd = {
+ md5sum = fs.Q(vars.MD5SUM),
+ openssl = fs.Q(vars.OPENSSL).." md5",
+ md5 = fs.Q(vars.MD5),
+}
+
+--- Get the MD5 checksum for a file.
+-- @param file string: The file to be computed.
+-- @return string: The MD5 checksum or nil + message
+function tools.get_md5(file)
+ local cmd = md5_cmd[cfg.md5checker]
+ if not cmd then return nil, "no MD5 checker command configured" end
+ local pipe = io.popen(cmd.." "..fs.Q(fs.absolute_name(file)))
+ local computed = pipe:read("*a")
+ pipe:close()
+ if computed then
+ computed = computed:match("("..("%x"):rep(32)..")")
+ end
+ if computed then return computed end
+ return nil, "Failed to compute MD5 hash for file "..tostring(fs.absolute_name(file))
+end
+
+return tools
diff --git a/Utils/luarocks/lua/luarocks/fs/unix.lua b/Utils/luarocks/lua/luarocks/fs/unix.lua
new file mode 100644
index 000000000..e2bdc7b8f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/unix.lua
@@ -0,0 +1,135 @@
+
+--- Unix implementation of filesystem and platform abstractions.
+local unix = {}
+
+local fs = require("luarocks.fs")
+
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+--- Annotate command string for quiet execution.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line, with silencing annotation.
+function unix.quiet(cmd)
+ return cmd.." 1> /dev/null 2> /dev/null"
+end
+
+--- Annotate command string for execution with quiet stderr.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line, with stderr silencing annotation.
+function unix.quiet_stderr(cmd)
+ return cmd.." 2> /dev/null"
+end
+
+--- Quote argument for shell processing.
+-- Adds single quotes and escapes.
+-- @param arg string: Unquoted argument.
+-- @return string: Quoted argument.
+function unix.Q(arg)
+ assert(type(arg) == "string")
+ return "'" .. arg:gsub("'", "'\\''") .. "'"
+end
+
+--- Return an absolute pathname from a potentially relative one.
+-- @param pathname string: pathname to convert.
+-- @param relative_to string or nil: path to prepend when making
+-- pathname absolute, or the current dir in the dir stack if
+-- not given.
+-- @return string: The pathname converted to absolute.
+function unix.absolute_name(pathname, relative_to)
+ assert(type(pathname) == "string")
+ assert(type(relative_to) == "string" or not relative_to)
+
+ relative_to = relative_to or fs.current_dir()
+ if pathname:sub(1,1) == "/" then
+ return pathname
+ else
+ return relative_to .. "/" .. pathname
+ end
+end
+
+--- Return the root directory for the given path.
+-- In Unix, root is always "/".
+-- @param pathname string: pathname to use.
+-- @return string: The root of the given pathname.
+function unix.root_of(_)
+ return "/"
+end
+
+--- Create a wrapper to make a script executable from the command-line.
+-- @param file string: Pathname of script to be made executable.
+-- @param dest string: Directory where to put the wrapper.
+-- @param name string: rock name to be used in loader context.
+-- @param version string: rock version to be used in loader context.
+-- @return boolean or (nil, string): True if succeeded, or nil and
+-- an error message.
+function unix.wrap_script(file, dest, name, version)
+ assert(type(file) == "string")
+ assert(type(dest) == "string")
+
+ local base = dir.base_name(file)
+ local wrapname = fs.is_dir(dest) and dest.."/"..base or dest
+ local lpath, lcpath = cfg.package_paths()
+ local wrapper = io.open(wrapname, "w")
+ if not wrapper then
+ return nil, "Could not open "..wrapname.." for writing."
+ end
+ wrapper:write("#!/bin/sh\n\n")
+ local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)
+ local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath"
+ local addctx = "local k,l,_=pcall(require,"..util.LQ("luarocks.loader")..") _=k and l.add_context("..util.LQ(name)..","..util.LQ(version)..")"
+ wrapper:write('exec '..fs.Q(lua)..' -e '..fs.Q(ppaths)..' -e '..fs.Q(addctx)..' '..fs.Q(file)..' "$@"\n')
+ wrapper:close()
+ if fs.chmod(wrapname, cfg.perm_exec) then
+ return true
+ else
+ return nil, "Could not make "..wrapname.." executable."
+ end
+end
+
+--- Check if a file (typically inside path.bin_dir) is an actual binary
+-- or a Lua wrapper.
+-- @param filename string: the file name with full path.
+-- @return boolean: returns true if file is an actual binary
+-- (or if it couldn't check) or false if it is a Lua wrapper.
+function unix.is_actual_binary(filename)
+ if filename:match("%.lua$") then
+ return false
+ end
+ local file = io.open(filename)
+ if not file then
+ return true
+ end
+ local first = file:read(2)
+ file:close()
+ if not first then
+ util.printerr("Warning: could not read "..filename)
+ return true
+ end
+ return first ~= "#!"
+end
+
+function unix.copy_binary(filename, dest)
+ return fs.copy(filename, dest, cfg.perm_exec)
+end
+
+--- Move a file on top of the other.
+-- The new file ceases to exist under its original name,
+-- and takes over the name of the old file.
+-- On Unix this is done through a single rename operation.
+-- @param old_file The name of the original file,
+-- which will be the new name of new_file.
+-- @param new_file The name of the new file,
+-- which will replace old_file.
+-- @return boolean or (nil, string): True if succeeded, or nil and
+-- an error message.
+function unix.replace_file(old_file, new_file)
+ return os.rename(new_file, old_file)
+end
+
+function unix.tmpname()
+ return os.tmpname()
+end
+
+return unix
diff --git a/Utils/luarocks/lua/luarocks/fs/unix/tools.lua b/Utils/luarocks/lua/luarocks/fs/unix/tools.lua
new file mode 100644
index 000000000..d0802725a
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/unix/tools.lua
@@ -0,0 +1,237 @@
+
+--- fs operations implemented with third-party tools for Unix platform abstractions.
+local tools = {}
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+
+local vars = cfg.variables
+
+--- Adds prefix to command to make it run from a directory.
+-- @param directory string: Path to a directory.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line with prefix.
+function tools.command_at(directory, cmd)
+ return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd
+end
+
+--- Create a directory if it does not already exist.
+-- If any of the higher levels in the path name does not exist
+-- too, they are created as well.
+-- @param directory string: pathname of directory to create.
+-- @return boolean: true on success, false on failure.
+function tools.make_dir(directory)
+ assert(directory)
+ local ok, err = fs.execute(vars.MKDIR.." -p", directory)
+ if not ok then
+ err = "failed making directory "..directory
+ end
+ return ok, err
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param directory string: pathname of directory to remove.
+function tools.remove_dir_if_empty(directory)
+ assert(directory)
+ fs.execute_quiet(vars.RMDIR, directory)
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param directory string: pathname of directory to remove.
+function tools.remove_dir_tree_if_empty(directory)
+ assert(directory)
+ fs.execute_quiet(vars.RMDIR, "-p", directory)
+end
+
+--- Copy a file.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @param perm string or nil: Permissions for destination file,
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function tools.copy(src, dest, perm)
+ assert(src and dest)
+ if fs.execute(vars.CP, src, dest) then
+ if perm then
+ if fs.is_dir(dest) then
+ dest = dir.path(dest, dir.base_name(src))
+ end
+ if fs.chmod(dest, perm) then
+ return true
+ else
+ return false, "Failed setting permissions of "..dest
+ end
+ end
+ return true
+ else
+ return false, "Failed copying "..src.." to "..dest
+ end
+end
+
+--- Recursively copy the contents of a directory.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function tools.copy_contents(src, dest)
+ assert(src and dest)
+ if fs.execute_quiet(vars.CP.." -pPR "..fs.Q(src).."/* "..fs.Q(dest)) then
+ return true
+ else
+ return false, "Failed copying "..src.." to "..dest
+ end
+end
+--- Delete a file or a directory and all its contents.
+-- For safety, this only accepts absolute paths.
+-- @param arg string: Pathname of source
+-- @return nil
+function tools.delete(arg)
+ assert(arg)
+ assert(arg:sub(1,1) == "/")
+ fs.execute_quiet(vars.RM, "-rf", arg)
+end
+
+--- Recursively scan the contents of a directory.
+-- @param at string or nil: directory to scan (will be the current
+-- directory if none is given).
+-- @return table: an array of strings with the filenames representing
+-- the contents of a directory.
+function tools.find(at)
+ assert(type(at) == "string" or not at)
+ if not at then
+ at = fs.current_dir()
+ end
+ if not fs.is_dir(at) then
+ return {}
+ end
+ local result = {}
+ local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(vars.FIND.." *")))
+ for file in pipe:lines() do
+ table.insert(result, file)
+ end
+ pipe:close()
+ return result
+end
+
+--- Compress files in a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be created.
+-- @param ... Filenames to be stored in the archive are given as
+-- additional arguments.
+-- @return boolean: true on success, false on failure.
+function tools.zip(zipfile, ...)
+ return fs.execute(vars.ZIP.." -r", zipfile, ...)
+end
+
+--- Uncompress files from a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be extracted.
+-- @return boolean: true on success, false on failure.
+function tools.unzip(zipfile)
+ assert(zipfile)
+ return fs.execute_quiet(vars.UNZIP, zipfile)
+end
+
+--- Test is file/directory exists
+-- @param file string: filename to test
+-- @return boolean: true if file exists, false otherwise.
+function tools.exists(file)
+ assert(file)
+ return fs.execute(vars.TEST, "-e", file)
+end
+
+--- Test is pathname is a directory.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a directory, false otherwise.
+function tools.is_dir(file)
+ assert(file)
+ return fs.execute(vars.TEST, "-d", file)
+end
+
+--- Test is pathname is a regular file.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a regular file, false otherwise.
+function tools.is_file(file)
+ assert(file)
+ return fs.execute(vars.TEST, "-f", file)
+end
+
+function tools.chmod(pathname, mode)
+ if mode then
+ return fs.execute(vars.CHMOD, mode, pathname)
+ else
+ return false
+ end
+end
+
+--- Unpack an archive.
+-- Extract the contents of an archive, detecting its format by
+-- filename extension.
+-- @param archive string: Filename of archive.
+-- @return boolean or (boolean, string): true on success, false and an error message on failure.
+function tools.unpack_archive(archive)
+ assert(type(archive) == "string")
+
+ local pipe_to_tar = " | "..vars.TAR.." -xf -"
+
+ if not cfg.verbose then
+ pipe_to_tar = " 2> /dev/null"..fs.quiet(pipe_to_tar)
+ end
+
+ local ok
+ if archive:match("%.tar%.gz$") or archive:match("%.tgz$") then
+ ok = fs.execute_string(vars.GUNZIP.." -c "..fs.Q(archive)..pipe_to_tar)
+ elseif archive:match("%.tar%.bz2$") then
+ ok = fs.execute_string(vars.BUNZIP2.." -c "..fs.Q(archive)..pipe_to_tar)
+ elseif archive:match("%.zip$") then
+ ok = fs.execute_quiet(vars.UNZIP, archive)
+ elseif archive:match("%.lua$") or archive:match("%.c$") then
+ -- Ignore .lua and .c files; they don't need to be extracted.
+ return true
+ else
+ return false, "Couldn't extract archive "..archive..": unrecognized filename extension"
+ end
+ if not ok then
+ return false, "Failed extracting "..archive
+ end
+ return true
+end
+
+function tools.get_permissions(filename)
+ local pipe = io.popen(vars.STAT.." "..vars.STATFLAG.." "..fs.Q(filename))
+ local ret = pipe:read("*l")
+ pipe:close()
+ return ret
+end
+
+function tools.browser(url)
+ return fs.execute(cfg.web_browser, url)
+end
+
+function tools.set_time(file, time)
+ file = dir.normalize(file)
+ return fs.execute(vars.TOUCH, "-d", "@"..tostring(time), file)
+end
+
+--- Create a temporary directory.
+-- @param name string: name pattern to use for avoiding conflicts
+-- when creating temporary directory.
+-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure.
+function tools.make_temp_dir(name)
+ assert(type(name) == "string")
+ name = dir.normalize(name)
+
+ local template = (os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX"
+ local pipe = io.popen(vars.MKTEMP.." -d "..fs.Q(template))
+ local dirname = pipe:read("*l")
+ pipe:close()
+ if dirname and dirname:match("^/") then
+ return dirname
+ end
+ return nil, "Failed to create temporary directory "..tostring(dirname)
+end
+
+return tools
diff --git a/Utils/luarocks/lua/luarocks/fs/win32.lua b/Utils/luarocks/lua/luarocks/fs/win32.lua
new file mode 100644
index 000000000..cfc28d35f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/win32.lua
@@ -0,0 +1,266 @@
+--- Windows implementation of filesystem and platform abstractions.
+-- Download http://unxutils.sourceforge.net/ for Windows GNU utilities
+-- used by this module.
+local win32 = {}
+
+local fs = require("luarocks.fs")
+
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+math.randomseed(os.time())
+
+-- Monkey patch io.popen and os.execute to make sure quoting
+-- works as expected.
+-- See http://lua-users.org/lists/lua-l/2013-11/msg00367.html
+local _prefix = "type NUL && "
+local _popen, _execute = io.popen, os.execute
+io.popen = function(cmd, ...) return _popen(_prefix..cmd, ...) end
+os.execute = function(cmd, ...) return _execute(_prefix..cmd, ...) end
+
+--- Annotate command string for quiet execution.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line, with silencing annotation.
+function win32.quiet(cmd)
+ return cmd.." 2> NUL 1> NUL"
+end
+
+--- Annotate command string for execution with quiet stderr.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line, with stderr silencing annotation.
+function win32.quiet_stderr(cmd)
+ return cmd.." 2> NUL"
+end
+
+-- Split path into root and the rest.
+-- Root part consists of an optional drive letter (e.g. "C:")
+-- and an optional directory separator.
+local function split_root(path)
+ local root = ""
+
+ if path:match("^.:") then
+ root = path:sub(1, 2)
+ path = path:sub(3)
+ end
+
+ if path:match("^[\\/]") then
+ root = path:sub(1, 1)
+ path = path:sub(2)
+ end
+
+ return root, path
+end
+
+--- Quote argument for shell processing. Fixes paths on Windows.
+-- Adds double quotes and escapes.
+-- @param arg string: Unquoted argument.
+-- @return string: Quoted argument.
+function win32.Q(arg)
+ assert(type(arg) == "string")
+ -- Use Windows-specific directory separator for paths.
+ -- Paths should be converted to absolute by now.
+ if split_root(arg) ~= "" then
+ arg = arg:gsub("/", "\\")
+ end
+ if arg == "\\" then
+ return '\\' -- CHDIR needs special handling for root dir
+ end
+ -- URLs and anything else
+ arg = arg:gsub('\\(\\*)"', '\\%1%1"')
+ arg = arg:gsub('\\+$', '%0%0')
+ arg = arg:gsub('"', '\\"')
+ arg = arg:gsub('(\\*)%%', '%1%1"%%"')
+ return '"' .. arg .. '"'
+end
+
+--- Quote argument for shell processing in batch files.
+-- Adds double quotes and escapes.
+-- @param arg string: Unquoted argument.
+-- @return string: Quoted argument.
+function win32.Qb(arg)
+ assert(type(arg) == "string")
+ -- Use Windows-specific directory separator for paths.
+ -- Paths should be converted to absolute by now.
+ if split_root(arg) ~= "" then
+ arg = arg:gsub("/", "\\")
+ end
+ if arg == "\\" then
+ return '\\' -- CHDIR needs special handling for root dir
+ end
+ -- URLs and anything else
+ arg = arg:gsub('\\(\\*)"', '\\%1%1"')
+ arg = arg:gsub('\\+$', '%0%0')
+ arg = arg:gsub('"', '\\"')
+ arg = arg:gsub('%%', '%%%%')
+ return '"' .. arg .. '"'
+end
+
+--- Return an absolute pathname from a potentially relative one.
+-- @param pathname string: pathname to convert.
+-- @param relative_to string or nil: path to prepend when making
+-- pathname absolute, or the current dir in the dir stack if
+-- not given.
+-- @return string: The pathname converted to absolute.
+function win32.absolute_name(pathname, relative_to)
+ assert(type(pathname) == "string")
+ assert(type(relative_to) == "string" or not relative_to)
+
+ relative_to = relative_to or fs.current_dir()
+ local root, rest = split_root(pathname)
+ if root:match("[\\/]$") then
+ -- It's an absolute path already.
+ return pathname
+ else
+ -- It's a relative path, join it with base path.
+ -- This drops drive letter from paths like "C:foo".
+ return relative_to .. "/" .. rest
+ end
+end
+
+--- Return the root directory for the given path.
+-- For example, for "c:\hello", returns "c:\"
+-- @param pathname string: pathname to use.
+-- @return string: The root of the given pathname.
+function win32.root_of(pathname)
+ return (split_root(fs.absolute_name(pathname)))
+end
+
+--- Create a wrapper to make a script executable from the command-line.
+-- @param file string: Pathname of script to be made executable.
+-- @param dest string: Directory where to put the wrapper.
+-- @param name string: rock name to be used in loader context.
+-- @param version string: rock version to be used in loader context.
+-- @return boolean or (nil, string): True if succeeded, or nil and
+-- an error message.
+function win32.wrap_script(file, dest, name, version)
+ assert(type(file) == "string")
+ assert(type(dest) == "string")
+
+ local base = dir.base_name(file)
+ local wrapname = fs.is_dir(dest) and dest.."/"..base or dest
+ wrapname = wrapname..".bat"
+ local lpath, lcpath = cfg.package_paths()
+ lpath = util.remove_path_dupes(lpath, ";")
+ lcpath = util.remove_path_dupes(lcpath, ";")
+ local wrapper = io.open(wrapname, "w")
+ if not wrapper then
+ return nil, "Could not open "..wrapname.." for writing."
+ end
+ wrapper:write("@echo off\n")
+ local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)
+ local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath"
+ local addctx = "local k,l,_=pcall(require,"..util.LQ("luarocks.loader")..") _=k and l.add_context("..util.LQ(name)..","..util.LQ(version)..")"
+ wrapper:write(fs.Qb(lua)..' -e '..fs.Qb(ppaths)..' -e '..fs.Qb(addctx)..' '..fs.Qb(file)..' %*\n')
+ wrapper:write("exit /b %ERRORLEVEL%\n")
+ wrapper:close()
+ return true
+end
+
+function win32.is_actual_binary(name)
+ name = name:lower()
+ if name:match("%.bat$") or name:match("%.exe$") then
+ return true
+ end
+ return false
+end
+
+function win32.copy_binary(filename, dest)
+ local ok, err = fs.copy(filename, dest)
+ if not ok then
+ return nil, err
+ end
+ local exe_pattern = "%.[Ee][Xx][Ee]$"
+ local base = dir.base_name(filename)
+ dest = dir.dir_name(dest)
+ if base:match(exe_pattern) then
+ base = base:gsub(exe_pattern, ".lua")
+ local helpname = dest.."/"..base
+ local helper = io.open(helpname, "w")
+ if not helper then
+ return nil, "Could not open "..helpname.." for writing."
+ end
+ helper:write('package.path=\"'..package.path:gsub("\\","\\\\")..';\"..package.path\n')
+ helper:write('package.cpath=\"'..package.path:gsub("\\","\\\\")..';\"..package.cpath\n')
+ helper:close()
+ end
+ return true
+end
+
+function win32.chmod(filename, mode)
+ return true
+end
+
+function win32.get_permissions(filename)
+ return ""
+end
+
+--- Move a file on top of the other.
+-- The new file ceases to exist under its original name,
+-- and takes over the name of the old file.
+-- On Windows this is done by removing the original file and
+-- renaming the new file to its original name.
+-- @param old_file The name of the original file,
+-- which will be the new name of new_file.
+-- @param new_file The name of the new file,
+-- which will replace old_file.
+-- @return boolean or (nil, string): True if succeeded, or nil and
+-- an error message.
+function win32.replace_file(old_file, new_file)
+ os.remove(old_file)
+ return os.rename(new_file, old_file)
+end
+
+--- Test is file/dir is writable.
+-- Warning: testing if a file/dir is writable does not guarantee
+-- that it will remain writable and therefore it is no replacement
+-- for checking the result of subsequent operations.
+-- @param file string: filename to test
+-- @return boolean: true if file exists, false otherwise.
+function win32.is_writable(file)
+ assert(file)
+ file = dir.normalize(file)
+ local result
+ local tmpname = 'tmpluarockstestwritable.deleteme'
+ if fs.is_dir(file) then
+ local file2 = dir.path(file, tmpname)
+ local fh = io.open(file2, 'wb')
+ result = fh ~= nil
+ if fh then fh:close() end
+ if result then
+ -- the above test might give a false positive when writing to
+ -- c:\program files\ because of VirtualStore redirection on Vista and up
+ -- So check whether it's really there
+ result = fs.exists(file2)
+ end
+ os.remove(file2)
+ else
+ local fh = io.open(file, 'r+b')
+ result = fh ~= nil
+ if fh then fh:close() end
+ end
+ return result
+end
+
+--- Create a temporary directory.
+-- @param name string: name pattern to use for avoiding conflicts
+-- when creating temporary directory.
+-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure.
+function win32.make_temp_dir(name)
+ assert(type(name) == "string")
+ name = dir.normalize(name)
+
+ local temp_dir = os.getenv("TMP") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000))
+ local ok, err = fs.make_dir(temp_dir)
+ if ok then
+ return temp_dir
+ else
+ return nil, err
+ end
+end
+
+function win32.tmpname()
+ return os.getenv("TMP")..os.tmpname()
+end
+
+return win32
diff --git a/Utils/luarocks/lua/luarocks/fs/win32/tools.lua b/Utils/luarocks/lua/luarocks/fs/win32/tools.lua
new file mode 100644
index 000000000..4adc78d1e
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/fs/win32/tools.lua
@@ -0,0 +1,227 @@
+
+--- fs operations implemented with third-party tools for Windows platform abstractions.
+-- Download http://unxutils.sourceforge.net/ for Windows GNU utilities
+-- used by this module.
+local tools = {}
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+
+local vars = cfg.variables
+
+--- Adds prefix to command to make it run from a directory.
+-- @param directory string: Path to a directory.
+-- @param cmd string: A command-line string.
+-- @return string: The command-line with prefix.
+function tools.command_at(directory, cmd)
+ local drive = directory:match("^([A-Za-z]:)")
+ cmd = "cd " .. fs.Q(directory) .. " & " .. cmd
+ if drive then
+ cmd = drive .. " & " .. cmd
+ end
+ return cmd
+end
+
+--- Create a directory if it does not already exist.
+-- If any of the higher levels in the path name does not exist
+-- too, they are created as well.
+-- @param directory string: pathname of directory to create.
+-- @return boolean: true on success, false on failure.
+function tools.make_dir(directory)
+ assert(directory)
+ directory = dir.normalize(directory)
+ fs.execute_quiet(fs.Q(vars.MKDIR).." -p ", directory)
+ if not fs.is_dir(directory) then
+ return false, "failed making directory "..directory
+ end
+ return true
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param directory string: pathname of directory to remove.
+function tools.remove_dir_if_empty(directory)
+ assert(directory)
+ fs.execute_quiet(fs.Q(vars.RMDIR), directory)
+end
+
+--- Remove a directory if it is empty.
+-- Does not return errors (for example, if directory is not empty or
+-- if already does not exist)
+-- @param directory string: pathname of directory to remove.
+function tools.remove_dir_tree_if_empty(directory)
+ assert(directory)
+ fs.execute_quiet(fs.Q(vars.RMDIR), directory)
+end
+
+--- Copy a file.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function tools.copy(src, dest)
+ assert(src and dest)
+ if dest:match("[/\\]$") then dest = dest:sub(1, -2) end
+ local ok = fs.execute(fs.Q(vars.CP), src, dest)
+ if ok then
+ return true
+ else
+ return false, "Failed copying "..src.." to "..dest
+ end
+end
+
+--- Recursively copy the contents of a directory.
+-- @param src string: Pathname of source
+-- @param dest string: Pathname of destination
+-- @return boolean or (boolean, string): true on success, false on failure,
+-- plus an error message.
+function tools.copy_contents(src, dest)
+ assert(src and dest)
+ if fs.execute_quiet(fs.Q(vars.CP), "-dR", src.."\\*.*", dest) then
+ return true
+ else
+ return false, "Failed copying "..src.." to "..dest
+ end
+end
+
+--- Delete a file or a directory and all its contents.
+-- For safety, this only accepts absolute paths.
+-- @param arg string: Pathname of source
+-- @return nil
+function tools.delete(arg)
+ assert(arg)
+ assert(arg:match("^[a-zA-Z]?:?[\\/]"))
+ fs.execute_quiet("if exist "..fs.Q(arg.."\\").." ( RMDIR /S /Q "..fs.Q(arg).." ) else ( DEL /Q /F "..fs.Q(arg).." )")
+end
+
+--- Recursively scan the contents of a directory.
+-- @param at string or nil: directory to scan (will be the current
+-- directory if none is given).
+-- @return table: an array of strings with the filenames representing
+-- the contents of a directory. Paths are returned with forward slashes.
+function tools.find(at)
+ assert(type(at) == "string" or not at)
+ if not at then
+ at = fs.current_dir()
+ end
+ if not fs.is_dir(at) then
+ return {}
+ end
+ local result = {}
+ local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND))))
+ for file in pipe:lines() do
+ -- Windows find is a bit different
+ local first_two = file:sub(1,2)
+ if first_two == ".\\" or first_two == "./" then file=file:sub(3) end
+ if file ~= "." then
+ table.insert(result, (file:gsub("\\", "/")))
+ end
+ end
+ pipe:close()
+ return result
+end
+
+--- Compress files in a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be created.
+-- @param ... Filenames to be stored in the archive are given as
+-- additional arguments.
+-- @return boolean: true on success, false on failure.
+function tools.zip(zipfile, ...)
+ return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa a -tzip", zipfile, ...)
+end
+
+--- Uncompress files from a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be extracted.
+-- @return boolean: true on success, false on failure.
+function tools.unzip(zipfile)
+ assert(zipfile)
+ return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", zipfile)
+end
+
+--- Test is pathname is a directory.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a directory, false otherwise.
+function tools.is_dir(file)
+ assert(file)
+ return fs.execute_quiet("if not exist " .. fs.Q(file.."\\").." invalidcommandname")
+end
+
+--- Test is pathname is a regular file.
+-- @param file string: pathname to test
+-- @return boolean: true if it is a regular file, false otherwise.
+function tools.is_file(file)
+ assert(file)
+ return fs.execute(fs.Q(vars.TEST).." -f", file)
+end
+
+--- Strip the last extension of a filename.
+-- Example: "foo.tar.gz" becomes "foo.tar".
+-- If filename has no dots, returns it unchanged.
+-- @param filename string: The file name to strip.
+-- @return string: The stripped name.
+local function strip_extension(filename)
+ assert(type(filename) == "string")
+ return (filename:gsub("%.[^.]+$", "")) or filename
+end
+
+--- Uncompress gzip file.
+-- @param archive string: Filename of archive.
+-- @return boolean : success status
+local function gunzip(archive)
+ return fs.execute_quiet(fs.Q(vars.SEVENZ).." -aoa x", archive)
+end
+
+--- Unpack an archive.
+-- Extract the contents of an archive, detecting its format by
+-- filename extension.
+-- @param archive string: Filename of archive.
+-- @return boolean or (boolean, string): true on success, false and an error message on failure.
+function tools.unpack_archive(archive)
+ assert(type(archive) == "string")
+
+ local ok
+ local sevenzx = fs.Q(vars.SEVENZ).." -aoa x"
+ if archive:match("%.tar%.gz$") then
+ ok = gunzip(archive)
+ if ok then
+ ok = fs.execute_quiet(sevenzx, strip_extension(archive))
+ end
+ elseif archive:match("%.tgz$") then
+ ok = gunzip(archive)
+ if ok then
+ ok = fs.execute_quiet(sevenzx, strip_extension(archive)..".tar")
+ end
+ elseif archive:match("%.tar%.bz2$") then
+ ok = fs.execute_quiet(sevenzx, archive)
+ if ok then
+ ok = fs.execute_quiet(sevenzx, strip_extension(archive))
+ end
+ elseif archive:match("%.zip$") then
+ ok = fs.execute_quiet(sevenzx, archive)
+ elseif archive:match("%.lua$") or archive:match("%.c$") then
+ -- Ignore .lua and .c files; they don't need to be extracted.
+ return true
+ else
+ return false, "Couldn't extract archive "..archive..": unrecognized filename extension"
+ end
+ if not ok then
+ return false, "Failed extracting "..archive
+ end
+ return true
+end
+
+--- Test for existance of a file.
+-- @param file string: filename to test
+-- @return boolean: true if file exists, false otherwise.
+function tools.exists(file)
+ assert(file)
+ return fs.execute_quiet("if not exist " .. fs.Q(file) .. " invalidcommandname")
+end
+
+function tools.browser(url)
+ return fs.execute(cfg.web_browser..' "Starting docs..." '..fs.Q(url))
+end
+
+return tools
diff --git a/Utils/luarocks/lua/luarocks/help.lua b/Utils/luarocks/lua/luarocks/help.lua
new file mode 100644
index 000000000..871e97e99
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/help.lua
@@ -0,0 +1,118 @@
+
+--- Module implementing the LuaRocks "help" command.
+-- This is a generic help display module, which
+-- uses a global table called "commands" to find commands
+-- to show help for; each command should be represented by a
+-- table containing "help" and "help_summary" fields.
+local help = {}
+
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+
+local program = util.this_program("luarocks")
+
+util.add_run_function(help)
+help.help_summary = "Help on commands. Type '"..program.." help ' for more."
+
+help.help_arguments = "[]"
+help.help = [[
+ is the command to show help for.
+]]
+
+local function print_banner()
+ util.printout("\nLuaRocks "..cfg.program_version..", a module deployment system for Lua")
+end
+
+local function print_section(section)
+ util.printout("\n"..section)
+end
+
+local function get_status(status)
+ if status then
+ return "ok"
+ else
+ return "not found"
+ end
+end
+
+--- Driver function for the "help" command.
+-- @param command string or nil: command to show help for; if not
+-- given, help summaries for all commands are shown.
+-- @return boolean or (nil, string): true if there were no errors
+-- or nil and an error message if an invalid command was requested.
+function help.command(flags, command)
+ if not command then
+ local conf = cfg.which_config()
+ print_banner()
+ print_section("NAME")
+ util.printout("\t"..program..[[ - ]]..program_description)
+ print_section("SYNOPSIS")
+ util.printout("\t"..program..[[ [--from= | --only-from=] [--to=] [VAR=VALUE]... [] ]])
+ print_section("GENERAL OPTIONS")
+ util.printout([[
+ These apply to all commands, as appropriate:
+
+ --server= Fetch rocks/rockspecs from this server
+ (takes priority over config file)
+ --only-server= Fetch rocks/rockspecs from this server only
+ (overrides any entries in the config file)
+ --only-sources= Restrict downloads to paths matching the
+ given URL.
+ --tree= Which tree to operate on.
+ --local Use the tree in the user's home directory.
+ To enable it, see ']]..program..[[ help path'.
+ --verbose Display verbose output of commands executed.
+ --timeout= Timeout on network operations, in seconds.
+ 0 means no timeout (wait forever).
+ Default is ]]..tostring(cfg.connection_timeout)..[[.]])
+ print_section("VARIABLES")
+ util.printout([[
+ Variables from the "variables" table of the configuration file
+ can be overriden with VAR=VALUE assignments.]])
+ print_section("COMMANDS")
+ for name, command in util.sortedpairs(commands) do
+ local cmd = require(command)
+ util.printout("", name)
+ util.printout("\t", cmd.help_summary)
+ end
+ print_section("CONFIGURATION")
+ util.printout("\tLua version: " .. cfg.lua_version)
+ util.printout("\tConfiguration files:")
+ util.printout("\t\tSystem: ".. dir.normalize(conf.system.file) .. " (" .. get_status(conf.system.ok) ..")")
+ if conf.user.file then
+ util.printout("\t\tUser : ".. dir.normalize(conf.user.file) .. " (" .. get_status(conf.user.ok) ..")\n")
+ else
+ util.printout("\t\tUser : disabled in this LuaRocks installation.\n")
+ end
+ util.printout("\tRocks trees in use: ")
+ for _, tree in ipairs(cfg.rocks_trees) do
+ if type(tree) == "string" then
+ util.printout("\t\t"..dir.normalize(tree))
+ else
+ local name = tree.name and " (\""..tree.name.."\")" or ""
+ util.printout("\t\t"..dir.normalize(tree.root)..name)
+ end
+ end
+ else
+ command = command:gsub("-", "_")
+ local cmd = commands[command] and require(commands[command])
+ if cmd then
+ local arguments = cmd.help_arguments or ""
+ print_banner()
+ print_section("NAME")
+ util.printout("\t"..program.." "..command.." - "..cmd.help_summary)
+ print_section("SYNOPSIS")
+ util.printout("\t"..program.." "..command.." "..arguments)
+ print_section("DESCRIPTION")
+ util.printout("",(cmd.help:gsub("\n","\n\t"):gsub("\n\t$","")))
+ print_section("SEE ALSO")
+ util.printout("","'"..program.." help' for general options and configuration.\n")
+ else
+ return nil, "Unknown command: "..command
+ end
+ end
+ return true
+end
+
+return help
diff --git a/Utils/luarocks/lua/luarocks/index.lua b/Utils/luarocks/lua/luarocks/index.lua
new file mode 100644
index 000000000..e1f563ef2
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/index.lua
@@ -0,0 +1,186 @@
+
+--- Module which builds the index.html page to be used in rocks servers.
+local index = {}
+package.loaded["luarocks.index"] = index
+
+local util = require("luarocks.util")
+local fs = require("luarocks.fs")
+local deps = require("luarocks.deps")
+local persist = require("luarocks.persist")
+local dir = require("luarocks.dir")
+local manif = require("luarocks.manif")
+
+local ext_url_target = ' target="_blank"'
+
+local index_header = [[
+
+
+
+Available rocks
+
+
+
+
+
Available rocks
+
+Lua modules available from this location for use with LuaRocks:
+
+
+
+]]
+
+function index.format_external_dependencies(rockspec)
+ if rockspec.external_dependencies then
+ local deplist = {}
+ local listed_set = {}
+ local plats = nil
+ for name, desc in util.sortedpairs(rockspec.external_dependencies) do
+ if name ~= "platforms" then
+ table.insert(deplist, name:lower())
+ listed_set[name] = true
+ else
+ plats = desc
+ end
+ end
+ if plats then
+ for plat, entries in util.sortedpairs(plats) do
+ for name, desc in util.sortedpairs(entries) do
+ if not listed_set[name] then
+ table.insert(deplist, name:lower() .. " (on "..plat..")")
+ end
+ end
+ end
+ end
+ return '
'
+ else
+ return ""
+ end
+end
+
+function index.make_index(repo)
+ if not fs.is_dir(repo) then
+ return nil, "Cannot access repository at "..repo
+ end
+ local manifest = manif.load_manifest(repo)
+ local out = io.open(dir.path(repo, "index.html"), "w")
+
+ out:write(index_header)
+ for package, version_list in util.sortedpairs(manifest.repository) do
+ local latest_rockspec = nil
+ local output = index_package_begin
+ for version, data in util.sortedpairs(version_list, deps.compare_versions) do
+ local versions = {}
+ output = output..version..': '
+ table.sort(data, function(a,b) return a.arch < b.arch end)
+ for _, item in ipairs(data) do
+ local file
+ if item.arch == 'rockspec' then
+ file = ("%s-%s.rockspec"):format(package, version)
+ if not latest_rockspec then latest_rockspec = file end
+ else
+ file = ("%s-%s.%s.rock"):format(package, version, item.arch)
+ end
+ table.insert(versions, ''..item.arch..'')
+ end
+ output = output .. table.concat(versions, ', ') .. ' '
+ end
+ output = output .. index_package_end
+ if latest_rockspec then
+ local rockspec = persist.load_into_table(dir.path(repo, latest_rockspec))
+ local descript = rockspec.description or {}
+ local vars = {
+ anchor = package,
+ package = rockspec.package,
+ original = rockspec.source.url,
+ summary = descript.summary or "",
+ detailed = descript.detailed or "",
+ license = descript.license or "N/A",
+ homepage = descript.homepage and ('| project homepage') or "",
+ externaldependencies = index.format_external_dependencies(rockspec)
+ }
+ vars.detailed = vars.detailed:gsub("\n\n", "
"):gsub("%s+", " ")
+ vars.detailed = vars.detailed:gsub("(https?://[a-zA-Z0-9%.%%-_%+%[%]=%?&/$@;:]+)", '%1')
+ output = output:gsub("$(%w+)", vars)
+ else
+ output = output:gsub("$anchor", package)
+ output = output:gsub("$package", package)
+ output = output:gsub("$(%w+)", "")
+ end
+ out:write(output)
+ end
+ out:write(index_footer_begin)
+ for ver in util.lua_versions() do
+ out:write((index_manifest_ver:gsub("$VER", ver)))
+ end
+ out:write(index_footer_end)
+ out:close()
+end
+
+return index
diff --git a/Utils/luarocks/lua/luarocks/install.lua b/Utils/luarocks/lua/luarocks/install.lua
new file mode 100644
index 000000000..e28c24f08
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/install.lua
@@ -0,0 +1,188 @@
+--- Module implementing the LuaRocks "install" command.
+-- Installs binary rocks.
+local install = {}
+package.loaded["luarocks.install"] = install
+
+local path = require("luarocks.path")
+local repos = require("luarocks.repos")
+local fetch = require("luarocks.fetch")
+local util = require("luarocks.util")
+local fs = require("luarocks.fs")
+local deps = require("luarocks.deps")
+local manif = require("luarocks.manif")
+local remove = require("luarocks.remove")
+local cfg = require("luarocks.cfg")
+
+util.add_run_function(install)
+install.help_summary = "Install a rock."
+
+install.help_arguments = "{| []}"
+
+install.help = [[
+Argument may be the name of a rock to be fetched from a repository
+or a filename of a locally available rock.
+
+--keep Do not remove previously installed versions of the
+ rock after installing a new one. This behavior can
+ be made permanent by setting keep_other_versions=true
+ in the configuration file.
+
+--only-deps Installs only the dependencies of the rock.
+]]..util.deps_mode_help()
+
+
+--- Install a binary rock.
+-- @param rock_file string: local or remote filename of a rock.
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+-- @return (string, string) or (nil, string, [string]): Name and version of
+-- installed rock if succeeded or nil and an error message followed by an error code.
+function install.install_binary_rock(rock_file, deps_mode)
+ assert(type(rock_file) == "string")
+
+ local name, version, arch = path.parse_name(rock_file)
+ if not name then
+ return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
+ end
+
+ if arch ~= "all" and arch ~= cfg.arch then
+ return nil, "Incompatible architecture "..arch, "arch"
+ end
+ if repos.is_installed(name, version) then
+ repos.delete_version(name, version, deps_mode)
+ end
+
+ local rollback = util.schedule_function(function()
+ fs.delete(path.install_dir(name, version))
+ fs.remove_dir_if_empty(path.versions_dir(name))
+ end)
+
+ local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
+ if not ok then return nil, err, errcode end
+
+ local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
+ if err then
+ return nil, "Failed loading rockspec for installed package: "..err, errcode
+ end
+
+ if deps_mode == "none" then
+ util.printerr("Warning: skipping dependency checks.")
+ else
+ ok, err, errcode = deps.check_external_deps(rockspec, "install")
+ if err then return nil, err, errcode end
+ end
+
+ -- For compatibility with .rock files built with LuaRocks 1
+ if not fs.exists(path.rock_manifest_file(name, version)) then
+ ok, err = manif.make_rock_manifest(name, version)
+ if err then return nil, err end
+ end
+
+ if deps_mode ~= "none" then
+ ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
+ if err then return nil, err, errcode end
+ end
+
+ ok, err = repos.deploy_files(name, version, repos.should_wrap_bin_scripts(rockspec), deps_mode)
+ if err then return nil, err end
+
+ util.remove_scheduled_function(rollback)
+ rollback = util.schedule_function(function()
+ repos.delete_version(name, version, deps_mode)
+ end)
+
+ ok, err = repos.run_hook(rockspec, "post_install")
+ if err then return nil, err end
+
+ util.announce_install(rockspec)
+ util.remove_scheduled_function(rollback)
+ return name, version
+end
+
+--- Installs the dependencies of a binary rock.
+-- @param rock_file string: local or remote filename of a rock.
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+-- @return (string, string) or (nil, string, [string]): Name and version of
+-- the rock whose dependencies were installed if succeeded or nil and an error message
+-- followed by an error code.
+function install.install_binary_rock_deps(rock_file, deps_mode)
+ assert(type(rock_file) == "string")
+
+ local name, version, arch = path.parse_name(rock_file)
+ if not name then
+ return nil, "Filename "..rock_file.." does not match format 'name-version-revision.arch.rock'."
+ end
+
+ if arch ~= "all" and arch ~= cfg.arch then
+ return nil, "Incompatible architecture "..arch, "arch"
+ end
+
+ local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, path.install_dir(name, version))
+ if not ok then return nil, err, errcode end
+
+ local rockspec, err, errcode = fetch.load_rockspec(path.rockspec_file(name, version))
+ if err then
+ return nil, "Failed loading rockspec for installed package: "..err, errcode
+ end
+
+ ok, err, errcode = deps.fulfill_dependencies(rockspec, deps_mode)
+ if err then return nil, err, errcode end
+
+ util.printout()
+ util.printout("Successfully installed dependencies for " ..name.." "..version)
+
+ return name, version
+end
+
+--- Driver function for the "install" command.
+-- @param name string: name of a binary rock. If an URL or pathname
+-- to a binary rock is given, fetches and installs it. If a rockspec or a
+-- source rock is given, forwards the request to the "build" command.
+-- If a package name is given, forwards the request to "search" and,
+-- if returned a result, installs the matching rock.
+-- @param version string: When passing a package name, a version number
+-- may also be given.
+-- @return boolean or (nil, string, exitcode): True if installation was
+-- successful, nil and an error message otherwise. exitcode is optionally returned.
+function install.command(flags, name, version)
+ if type(name) ~= "string" then
+ return nil, "Argument missing. "..util.see_help("install")
+ end
+
+ local ok, err = fs.check_command_permissions(flags)
+ if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end
+
+ if name:match("%.rockspec$") or name:match("%.src%.rock$") then
+ local build = require("luarocks.build")
+ return build.command(flags, name)
+ elseif name:match("%.rock$") then
+ if flags["only-deps"] then
+ ok, err = install.install_binary_rock_deps(name, deps.get_deps_mode(flags))
+ else
+ ok, err = install.install_binary_rock(name, deps.get_deps_mode(flags))
+ end
+ if not ok then return nil, err end
+ name, version = ok, err
+
+ if (not flags["only-deps"]) and (not flags["keep"]) and not cfg.keep_other_versions then
+ local ok, err = remove.remove_other_versions(name, version, flags["force"], flags["force-fast"])
+ if not ok then util.printerr(err) end
+ end
+
+ manif.check_dependencies(nil, deps.get_deps_mode(flags))
+ return name, version
+ else
+ local search = require("luarocks.search")
+ local url, err = search.find_suitable_rock(search.make_query(name:lower(), version))
+ if not url then
+ return nil, err
+ end
+ util.printout("Installing "..url)
+ return install.command(flags, url)
+ end
+end
+
+return install
diff --git a/Utils/luarocks/lua/luarocks/lint.lua b/Utils/luarocks/lua/luarocks/lint.lua
new file mode 100644
index 000000000..0fd81a203
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/lint.lua
@@ -0,0 +1,55 @@
+
+--- Module implementing the LuaRocks "lint" command.
+-- Utility function that checks syntax of the rockspec.
+local lint = {}
+package.loaded["luarocks.lint"] = lint
+
+local util = require("luarocks.util")
+local download = require("luarocks.download")
+local fetch = require("luarocks.fetch")
+
+util.add_run_function(lint)
+lint.help_summary = "Check syntax of a rockspec."
+lint.help_arguments = ""
+lint.help = [[
+This is a utility function that checks the syntax of a rockspec.
+
+It returns success or failure if the text of a rockspec is
+syntactically correct.
+]]
+
+function lint.command(flags, input)
+ if not input then
+ return nil, "Argument missing. "..util.see_help("lint")
+ end
+
+ local filename = input
+ if not input:match(".rockspec$") then
+ local err
+ filename, err = download.download("rockspec", input:lower())
+ if not filename then
+ return nil, err
+ end
+ end
+
+ local rs, err = fetch.load_local_rockspec(filename)
+ if not rs then
+ return nil, "Failed loading rockspec: "..err
+ end
+
+ local ok = true
+
+ -- This should have been done in the type checker,
+ -- but it would break compatibility of other commands.
+ -- Making 'lint' alone be stricter shouldn't be a problem,
+ -- because extra-strict checks is what lint-type commands
+ -- are all about.
+ if not rs.description.license then
+ util.printerr("Rockspec has no license field.")
+ ok = false
+ end
+
+ return ok, ok or filename.." failed consistency checks."
+end
+
+return lint
diff --git a/Utils/luarocks/lua/luarocks/list.lua b/Utils/luarocks/lua/luarocks/list.lua
new file mode 100644
index 000000000..c65e058f9
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/list.lua
@@ -0,0 +1,97 @@
+
+--- Module implementing the LuaRocks "list" command.
+-- Lists currently installed rocks.
+local list = {}
+package.loaded["luarocks.list"] = list
+
+local search = require("luarocks.search")
+local deps = require("luarocks.deps")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local path = require("luarocks.path")
+
+util.add_run_function(list)
+list.help_summary = "List currently installed rocks."
+list.help_arguments = "[--porcelain] "
+list.help = [[
+ is a substring of a rock name to filter by.
+
+--outdated List only rocks for which there is a
+ higher version available in the rocks server.
+
+--porcelain Produce machine-friendly output.
+]]
+
+local function check_outdated(trees, query)
+ local results_installed = {}
+ for _, tree in ipairs(trees) do
+ search.manifest_search(results_installed, path.rocks_dir(tree), query)
+ end
+ local outdated = {}
+ for name, versions in util.sortedpairs(results_installed) do
+ versions = util.keys(versions)
+ table.sort(versions, deps.compare_versions)
+ local latest_installed = versions[1]
+
+ local query_available = search.make_query(name:lower())
+ query.exact_name = true
+ local results_available, err = search.search_repos(query_available)
+
+ if results_available[name] then
+ local available_versions = util.keys(results_available[name])
+ table.sort(available_versions, deps.compare_versions)
+ local latest_available = available_versions[1]
+ local latest_available_repo = results_available[name][latest_available][1].repo
+
+ if deps.compare_versions(latest_available, latest_installed) then
+ table.insert(outdated, { name = name, installed = latest_installed, available = latest_available, repo = latest_available_repo })
+ end
+ end
+ end
+ return outdated
+end
+
+local function list_outdated(trees, query, porcelain)
+ util.title("Outdated rocks:", porcelain)
+ local outdated = check_outdated(trees, query)
+ for _, item in ipairs(outdated) do
+ if porcelain then
+ util.printout(item.name, item.installed, item.available, item.repo)
+ else
+ util.printout(item.name)
+ util.printout(" "..item.installed.." < "..item.available.." at "..item.repo)
+ util.printout()
+ end
+ end
+ return true
+end
+
+--- Driver function for "list" command.
+-- @param filter string or nil: A substring of a rock name to filter by.
+-- @param version string or nil: a version may also be passed.
+-- @return boolean: True if succeeded, nil on errors.
+function list.command(flags, filter, version)
+ local query = search.make_query(filter and filter:lower() or "", version)
+ query.exact_name = false
+ local trees = cfg.rocks_trees
+ if flags["tree"] then
+ trees = { flags["tree"] }
+ end
+
+ if flags["outdated"] then
+ return list_outdated(trees, query, flags["porcelain"])
+ end
+
+ local results = {}
+ for _, tree in ipairs(trees) do
+ local ok, err, errcode = search.manifest_search(results, path.rocks_dir(tree), query)
+ if not ok and errcode ~= "open" then
+ util.warning(err)
+ end
+ end
+ util.title("Installed rocks:", flags["porcelain"])
+ search.print_results(results, flags["porcelain"])
+ return true
+end
+
+return list
diff --git a/Utils/luarocks/lua/luarocks/loader.lua b/Utils/luarocks/lua/luarocks/loader.lua
new file mode 100644
index 000000000..874bc10c3
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/loader.lua
@@ -0,0 +1,249 @@
+
+--- A module which installs a Lua package loader that is LuaRocks-aware.
+-- This loader uses dependency information from the LuaRocks tree to load
+-- correct versions of modules. It does this by constructing a "context"
+-- table in the environment, which records which versions of packages were
+-- used to load previous modules, so that the loader chooses versions
+-- that are declared to be compatible with the ones loaded earlier.
+local loaders = package.loaders or package.searchers
+local package, require, ipairs, table, type, next, tostring, error =
+ package, require, ipairs, table, type, next, tostring, error
+local unpack = unpack or table.unpack
+
+--module("luarocks.loader")
+local loader = {}
+package.loaded["luarocks.loader"] = loader
+
+local cfg = require("luarocks.cfg")
+cfg.init_package_paths()
+
+local path = require("luarocks.path")
+local manif_core = require("luarocks.manif_core")
+local deps = require("luarocks.deps")
+local util = require("luarocks.util")
+
+-- Workaround for wrappers produced by older versions of LuaRocks
+local temporary_global = false
+if luarocks then
+ -- The site_config.lua file generated by old versions uses module(),
+ -- so it produces a global `luarocks` table. Since we have the table,
+ -- add the `loader` field to make the old wrappers happy.
+ luarocks.loader = loader
+else
+ -- When a new version is installed on top of an old version,
+ -- site_config.lua may be replaced, and then it no longer creates
+ -- a global.
+ -- Detect when being called via -lluarocks.loader; this is
+ -- most likely a wrapper.
+ local info = debug.getinfo(2, "nS")
+ if info.what == "C" and not info.name then
+ luarocks = { loader = loader }
+ temporary_global = true
+ -- For the other half of this hack,
+ -- see the next use of `temporary_global` below.
+ end
+end
+
+loader.context = {}
+
+-- Contains a table when rocks trees are loaded,
+-- or 'false' to indicate rocks trees failed to load.
+-- 'nil' indicates rocks trees were not attempted to be loaded yet.
+loader.rocks_trees = nil
+
+local function load_rocks_trees()
+ local any_ok = false
+ local trees = {}
+ for _, tree in ipairs(cfg.rocks_trees) do
+ local manifest, err = manif_core.load_local_manifest(path.rocks_dir(tree))
+ if manifest then
+ any_ok = true
+ table.insert(trees, {tree=tree, manifest=manifest})
+ end
+ end
+ if not any_ok then
+ loader.rocks_trees = false
+ return false
+ end
+ loader.rocks_trees = trees
+ return true
+end
+
+--- Process the dependencies of a package to determine its dependency
+-- chain for loading modules.
+-- @param name string: The name of an installed rock.
+-- @param version string: The version of the rock, in string format
+function loader.add_context(name, version)
+ -- assert(type(name) == "string")
+ -- assert(type(version) == "string")
+
+ if temporary_global then
+ -- The first thing a wrapper does is to call add_context.
+ -- From here on, it's safe to clean the global environment.
+ luarocks = nil
+ temporary_global = false
+ end
+
+ if loader.context[name] then
+ return
+ end
+ loader.context[name] = version
+
+ if not loader.rocks_trees and not load_rocks_trees() then
+ return nil
+ end
+
+ for _, tree in ipairs(loader.rocks_trees) do
+ local manifest = tree.manifest
+
+ local pkgdeps
+ if manifest.dependencies and manifest.dependencies[name] then
+ pkgdeps = manifest.dependencies[name][version]
+ end
+ if not pkgdeps then
+ return nil
+ end
+ for _, dep in ipairs(pkgdeps) do
+ local pkg, constraints = dep.name, dep.constraints
+
+ for _, tree in ipairs(loader.rocks_trees) do
+ local entries = tree.manifest.repository[pkg]
+ if entries then
+ for version, pkgs in util.sortedpairs(entries, deps.compare_versions) do
+ if (not constraints) or deps.match_constraints(deps.parse_version(version), constraints) then
+ loader.add_context(pkg, version)
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+--- Internal sorting function.
+-- @param a table: A provider table.
+-- @param b table: Another provider table.
+-- @return boolean: True if the version of a is greater than that of b.
+local function sort_versions(a,b)
+ return a.version > b.version
+end
+
+--- Request module to be loaded through other loaders,
+-- once the proper name of the module has been determined.
+-- For example, in case the module "socket.core" has been requested
+-- to the LuaRocks loader and it determined based on context that
+-- the version 2.0.2 needs to be loaded and it is not the current
+-- version, the module requested for the other loaders will be
+-- "socket.core_2_0_2".
+-- @param module The module name requested by the user, such as "socket.core"
+-- @param name The rock name, such as "luasocket"
+-- @param version The rock version, such as "2.0.2-1"
+-- @param module_name The actual module name, such as "socket.core" or "socket.core_2_0_2".
+-- @return table or (nil, string): The module table as returned by some other loader,
+-- or nil followed by an error message if no other loader managed to load the module.
+local function call_other_loaders(module, name, version, module_name)
+ for i, a_loader in ipairs(loaders) do
+ if a_loader ~= loader.luarocks_loader then
+ local results = { a_loader(module_name) }
+ if type(results[1]) == "function" then
+ return unpack(results)
+ end
+ end
+ end
+ return "Failed loading module "..module.." in LuaRocks rock "..name.." "..version
+end
+
+--- Search for a module in the rocks trees
+-- @param module string: module name (eg. "socket.core")
+-- @param filter_file_name function(string, string, string, string, number):
+-- a function that takes the module file name (eg "socket/core.so"), the rock name
+-- (eg "luasocket"), the version (eg "2.0.2-1"), the path of the rocks tree
+-- (eg "/usr/local"), and the numeric index of the matching entry, so the
+-- filter function can know if the matching module was the first entry or not.
+-- @return string, string, string, (string or table):
+-- * name of the rock containing the module (eg. "luasocket")
+-- * version of the rock (eg. "2.0.2-1")
+-- * return value of filter_file_name
+-- * tree of the module (string or table in `rocks_trees` format)
+local function select_module(module, filter_file_name)
+ --assert(type(module) == "string")
+ --assert(type(filter_module_name) == "function")
+
+ if not loader.rocks_trees and not load_rocks_trees() then
+ return nil
+ end
+
+ local providers = {}
+ for _, tree in ipairs(loader.rocks_trees) do
+ local entries = tree.manifest.modules[module]
+ if entries then
+ for i, entry in ipairs(entries) do
+ local name, version = entry:match("^([^/]*)/(.*)$")
+ local file_name = tree.manifest.repository[name][version][1].modules[module]
+ if type(file_name) ~= "string" then
+ error("Invalid data in manifest file for module "..tostring(module).." (invalid data for "..tostring(name).." "..tostring(version)..")")
+ end
+ file_name = filter_file_name(file_name, name, version, tree.tree, i)
+ if loader.context[name] == version then
+ return name, version, file_name
+ end
+ version = deps.parse_version(version)
+ table.insert(providers, {name = name, version = version, module_name = file_name, tree = tree})
+ end
+ end
+ end
+
+ if next(providers) then
+ table.sort(providers, sort_versions)
+ local first = providers[1]
+ return first.name, first.version.string, first.module_name, first.tree
+ end
+end
+
+--- Search for a module
+-- @param module string: module name (eg. "socket.core")
+-- @return string, string, string, (string or table):
+-- * name of the rock containing the module (eg. "luasocket")
+-- * version of the rock (eg. "2.0.2-1")
+-- * name of the module (eg. "socket.core", or "socket.core_2_0_2" if file is stored versioned).
+-- * tree of the module (string or table in `rocks_trees` format)
+local function pick_module(module)
+ return
+ select_module(module, function(file_name, name, version, tree, i)
+ if i > 1 then
+ file_name = path.versioned_name(file_name, "", name, version)
+ end
+ return path.path_to_module(file_name)
+ end)
+end
+
+--- Return the pathname of the file that would be loaded for a module.
+-- @param module string: module name (eg. "socket.core")
+-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
+function loader.which(module)
+ local _, _, file_name = select_module(module, path.which_i)
+ return file_name
+end
+
+--- Package loader for LuaRocks support.
+-- A module is searched in installed rocks that match the
+-- current LuaRocks context. If module is not part of the
+-- context, or if a context has not yet been set, the module
+-- in the package with the highest version is used.
+-- @param module string: The module name, like in plain require().
+-- @return table: The module table (typically), like in plain
+-- require(). See require()
+-- in the Lua reference manual for details.
+function loader.luarocks_loader(module)
+ local name, version, module_name = pick_module(module)
+ if not name then
+ return "No LuaRocks module found for "..module
+ else
+ loader.add_context(name, version)
+ return call_other_loaders(module, name, version, module_name)
+ end
+end
+
+table.insert(loaders, 1, loader.luarocks_loader)
+
+return loader
diff --git a/Utils/luarocks/lua/luarocks/make.lua b/Utils/luarocks/lua/luarocks/make.lua
new file mode 100644
index 000000000..15167c1eb
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/make.lua
@@ -0,0 +1,92 @@
+
+--- Module implementing the LuaRocks "make" command.
+-- Builds sources in the current directory, but unlike "build",
+-- it does not fetch sources, etc., assuming everything is
+-- available in the current directory.
+local make = {}
+package.loaded["luarocks.make"] = make
+
+local build = require("luarocks.build")
+local fs = require("luarocks.fs")
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local fetch = require("luarocks.fetch")
+local pack = require("luarocks.pack")
+local remove = require("luarocks.remove")
+local deps = require("luarocks.deps")
+local manif = require("luarocks.manif")
+
+util.add_run_function(make)
+make.help_summary = "Compile package in current directory using a rockspec."
+make.help_arguments = "[--pack-binary-rock] []"
+make.help = [[
+Builds sources in the current directory, but unlike "build",
+it does not fetch sources, etc., assuming everything is
+available in the current directory. If no argument is given,
+it looks for a rockspec in the current directory and in "rockspec/"
+and "rockspecs/" subdirectories, picking the rockspec with newest version
+or without version name. If rockspecs for different rocks are found
+or there are several rockspecs without version, you must specify which to use,
+through the command-line.
+
+This command is useful as a tool for debugging rockspecs.
+To install rocks, you'll normally want to use the "install" and
+"build" commands. See the help on those for details.
+
+--pack-binary-rock Do not install rock. Instead, produce a .rock file
+ with the contents of compilation in the current
+ directory.
+
+--keep Do not remove previously installed versions of the
+ rock after installing a new one. This behavior can
+ be made permanent by setting keep_other_versions=true
+ in the configuration file.
+
+--branch= Override the `source.branch` field in the loaded
+ rockspec. Allows to specify a different branch to
+ fetch. Particularly for SCM rocks.
+
+]]
+
+--- Driver function for "make" command.
+-- @param name string: A local rockspec.
+-- @return boolean or (nil, string, exitcode): True if build was successful; nil and an
+-- error message otherwise. exitcode is optionally returned.
+function make.command(flags, rockspec)
+ assert(type(rockspec) == "string" or not rockspec)
+
+ if not rockspec then
+ local err
+ rockspec, err = util.get_default_rockspec()
+ if not rockspec then
+ return nil, err
+ end
+ end
+ if not rockspec:match("rockspec$") then
+ return nil, "Invalid argument: 'make' takes a rockspec as a parameter. "..util.see_help("make")
+ end
+
+ if flags["pack-binary-rock"] then
+ local rspec, err, errcode = fetch.load_rockspec(rockspec)
+ if not rspec then
+ return nil, err
+ end
+ return pack.pack_binary_rock(rspec.name, rspec.version, build.build_rockspec, rockspec, false, true, deps.get_deps_mode(flags))
+ else
+ local ok, err = fs.check_command_permissions(flags)
+ if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end
+ ok, err = build.build_rockspec(rockspec, false, true, deps.get_deps_mode(flags))
+ if not ok then return nil, err end
+ local name, version = ok, err
+
+ if (not flags["keep"]) and not cfg.keep_other_versions then
+ local ok, err = remove.remove_other_versions(name, version, flags["force"], flags["force-fast"])
+ if not ok then util.printerr(err) end
+ end
+
+ manif.check_dependencies(nil, deps.get_deps_mode(flags))
+ return name, version
+ end
+end
+
+return make
diff --git a/Utils/luarocks/lua/luarocks/make_manifest.lua b/Utils/luarocks/lua/luarocks/make_manifest.lua
new file mode 100644
index 000000000..c39c2939f
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/make_manifest.lua
@@ -0,0 +1,53 @@
+
+--- Module implementing the luarocks-admin "make_manifest" command.
+-- Compile a manifest file for a repository.
+local make_manifest = {}
+package.loaded["luarocks.make_manifest"] = make_manifest
+
+local manif = require("luarocks.manif")
+local index = require("luarocks.index")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local deps = require("luarocks.deps")
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+
+util.add_run_function(make_manifest)
+make_manifest.help_summary = "Compile a manifest file for a repository."
+
+make_manifest.help = [[
+, if given, is a local repository pathname.
+
+--local-tree If given, do not write versioned versions of the manifest file.
+ Use this when rebuilding the manifest of a local rocks tree.
+]]
+
+--- Driver function for "make_manifest" command.
+-- @param repo string or nil: Pathname of a local repository. If not given,
+-- the default local repository configured as cfg.rocks_dir is used.
+-- @return boolean or (nil, string): True if manifest was generated,
+-- or nil and an error message.
+function make_manifest.command(flags, repo)
+ assert(type(repo) == "string" or not repo)
+ repo = repo or cfg.rocks_dir
+
+ util.printout("Making manifest for "..repo)
+
+ if repo:match("/lib/luarocks") and not flags["local-tree"] then
+ util.warning("This looks like a local rocks tree, but you did not pass --local-tree.")
+ end
+
+ local ok, err = manif.make_manifest(repo, deps.get_deps_mode(flags), not flags["local-tree"])
+ if ok and not flags["local-tree"] then
+ util.printout("Generating index.html for "..repo)
+ index.make_index(repo)
+ end
+ if flags["local-tree"] then
+ for luaver in util.lua_versions() do
+ fs.delete(dir.path(repo, "manifest-"..luaver))
+ end
+ end
+ return ok, err
+end
+
+return make_manifest
diff --git a/Utils/luarocks/lua/luarocks/manif.lua b/Utils/luarocks/lua/luarocks/manif.lua
new file mode 100644
index 000000000..88a13f107
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/manif.lua
@@ -0,0 +1,628 @@
+--- Module for handling manifest files and tables.
+-- Manifest files describe the contents of a LuaRocks tree or server.
+-- They are loaded into manifest tables, which are then used for
+-- performing searches, matching dependencies, etc.
+local manif = {}
+package.loaded["luarocks.manif"] = manif
+
+local manif_core = require("luarocks.manif_core")
+local persist = require("luarocks.persist")
+local fetch = require("luarocks.fetch")
+local dir = require("luarocks.dir")
+local fs = require("luarocks.fs")
+local search = require("luarocks.search")
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local path = require("luarocks.path")
+local repos = require("luarocks.repos")
+local deps = require("luarocks.deps")
+
+manif.rock_manifest_cache = {}
+
+--- Commit a table to disk in given local path.
+-- @param where string: The directory where the table should be saved.
+-- @param name string: The filename.
+-- @param tbl table: The table to be saved.
+-- @return boolean or (nil, string): true if successful, or nil and a
+-- message in case of errors.
+local function save_table(where, name, tbl)
+ assert(type(where) == "string")
+ assert(type(name) == "string")
+ assert(type(tbl) == "table")
+
+ local filename = dir.path(where, name)
+ local ok, err = persist.save_from_table(filename..".tmp", tbl)
+ if ok then
+ ok, err = fs.replace_file(filename, filename..".tmp")
+ end
+ return ok, err
+end
+
+function manif.load_rock_manifest(name, version, root)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local name_version = name.."/"..version
+ if manif.rock_manifest_cache[name_version] then
+ return manif.rock_manifest_cache[name_version].rock_manifest
+ end
+ local pathname = path.rock_manifest_file(name, version, root)
+ local rock_manifest = persist.load_into_table(pathname)
+ if not rock_manifest then return nil end
+ manif.rock_manifest_cache[name_version] = rock_manifest
+ return rock_manifest.rock_manifest
+end
+
+function manif.make_rock_manifest(name, version)
+ local install_dir = path.install_dir(name, version)
+ local tree = {}
+ for _, file in ipairs(fs.find(install_dir)) do
+ local full_path = dir.path(install_dir, file)
+ local walk = tree
+ local last
+ local last_name
+ for name in file:gmatch("[^/]+") do
+ local next = walk[name]
+ if not next then
+ next = {}
+ walk[name] = next
+ end
+ last = walk
+ last_name = name
+ walk = next
+ end
+ if fs.is_file(full_path) then
+ local sum, err = fs.get_md5(full_path)
+ if not sum then
+ return nil, "Failed producing checksum: "..tostring(err)
+ end
+ last[last_name] = sum
+ end
+ end
+ local rock_manifest = { rock_manifest=tree }
+ manif.rock_manifest_cache[name.."/"..version] = rock_manifest
+ save_table(install_dir, "rock_manifest", rock_manifest )
+end
+
+local function fetch_manifest_from(repo_url, filename)
+ local url = dir.path(repo_url, filename)
+ local name = repo_url:gsub("[/:]","_")
+ local cache_dir = dir.path(cfg.local_cache, name)
+ local ok = fs.make_dir(cache_dir)
+ if not ok then
+ return nil, "Failed creating temporary cache directory "..cache_dir
+ end
+ local file, err, errcode = fetch.fetch_url(url, dir.path(cache_dir, filename), true)
+ if not file then
+ return nil, "Failed fetching manifest for "..repo_url..(err and " - "..err or ""), errcode
+ end
+ return file
+end
+
+--- Load a local or remote manifest describing a repository.
+-- All functions that use manifest tables assume they were obtained
+-- through either this function or load_local_manifest.
+-- @param repo_url string: URL or pathname for the repository.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @return table or (nil, string, [string]): A table representing the manifest,
+-- or nil followed by an error message and an optional error code.
+function manif.load_manifest(repo_url, lua_version)
+ assert(type(repo_url) == "string")
+ assert(type(lua_version) == "string" or not lua_version)
+ lua_version = lua_version or cfg.lua_version
+
+ local cached_manifest = manif_core.get_cached_manifest(repo_url, lua_version)
+ if cached_manifest then
+ return cached_manifest
+ end
+
+ local filenames = {
+ "manifest-"..lua_version..".zip",
+ "manifest-"..lua_version,
+ "manifest",
+ }
+
+ local protocol, repodir = dir.split_url(repo_url)
+ local pathname
+ if protocol == "file" then
+ for _, filename in ipairs(filenames) do
+ pathname = dir.path(repodir, filename)
+ if fs.exists(pathname) then
+ break
+ end
+ end
+ else
+ local err, errcode
+ for _, filename in ipairs(filenames) do
+ pathname, err, errcode = fetch_manifest_from(repo_url, filename)
+ if pathname then
+ break
+ end
+ end
+ if not pathname then
+ return nil, err, errcode
+ end
+ end
+ if pathname:match(".*%.zip$") then
+ pathname = fs.absolute_name(pathname)
+ local dir = dir.dir_name(pathname)
+ fs.change_dir(dir)
+ local nozip = pathname:match("(.*)%.zip$")
+ fs.delete(nozip)
+ local ok = fs.unzip(pathname)
+ fs.pop_dir()
+ if not ok then
+ fs.delete(pathname)
+ fs.delete(pathname..".timestamp")
+ return nil, "Failed extracting manifest file"
+ end
+ pathname = nozip
+ end
+ return manif_core.manifest_loader(pathname, repo_url, lua_version)
+end
+
+--- Update storage table to account for items provided by a package.
+-- @param storage table: a table storing items in the following format:
+-- keys are item names and values are arrays of packages providing each item,
+-- where a package is specified as string `name/version`.
+-- @param items table: a table mapping item names to paths.
+-- @param name string: package name.
+-- @param version string: package version.
+local function store_package_items(storage, name, version, items)
+ assert(type(storage) == "table")
+ assert(type(items) == "table")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local package_identifier = name.."/"..version
+
+ for item_name, path in pairs(items) do
+ if not storage[item_name] then
+ storage[item_name] = {}
+ end
+
+ table.insert(storage[item_name], package_identifier)
+ end
+end
+
+--- Update storage table removing items provided by a package.
+-- @param storage table: a table storing items in the following format:
+-- keys are item names and values are arrays of packages providing each item,
+-- where a package is specified as string `name/version`.
+-- @param items table: a table mapping item names to paths.
+-- @param name string: package name.
+-- @param version string: package version.
+local function remove_package_items(storage, name, version, items)
+ assert(type(storage) == "table")
+ assert(type(items) == "table")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local package_identifier = name.."/"..version
+
+ for item_name, path in pairs(items) do
+ local all_identifiers = storage[item_name]
+
+ for i, identifier in ipairs(all_identifiers) do
+ if identifier == package_identifier then
+ table.remove(all_identifiers, i)
+ break
+ end
+ end
+
+ if #all_identifiers == 0 then
+ storage[item_name] = nil
+ end
+ end
+end
+
+--- Sort function for ordering rock identifiers in a manifest's
+-- modules table. Rocks are ordered alphabetically by name, and then
+-- by version which greater first.
+-- @param a string: Version to compare.
+-- @param b string: Version to compare.
+-- @return boolean: The comparison result, according to the
+-- rule outlined above.
+local function sort_pkgs(a, b)
+ assert(type(a) == "string")
+ assert(type(b) == "string")
+
+ local na, va = a:match("(.*)/(.*)$")
+ local nb, vb = b:match("(.*)/(.*)$")
+
+ return (na == nb) and deps.compare_versions(va, vb) or na < nb
+end
+
+--- Sort items of a package matching table by version number (higher versions first).
+-- @param tbl table: the package matching table: keys should be strings
+-- and values arrays of strings with packages names in "name/version" format.
+local function sort_package_matching_table(tbl)
+ assert(type(tbl) == "table")
+
+ if next(tbl) then
+ for item, pkgs in pairs(tbl) do
+ if #pkgs > 1 then
+ table.sort(pkgs, sort_pkgs)
+ -- Remove duplicates from the sorted array.
+ local prev = nil
+ local i = 1
+ while pkgs[i] do
+ local curr = pkgs[i]
+ if curr == prev then
+ table.remove(pkgs, i)
+ else
+ prev = curr
+ i = i + 1
+ end
+ end
+ end
+ end
+ end
+end
+
+--- Process the dependencies of a manifest table to determine its dependency
+-- chains for loading modules. The manifest dependencies information is filled
+-- and any dependency inconsistencies or missing dependencies are reported to
+-- standard error.
+-- @param manifest table: a manifest table.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for no trees.
+local function update_dependencies(manifest, deps_mode)
+ assert(type(manifest) == "table")
+ assert(type(deps_mode) == "string")
+
+ for pkg, versions in pairs(manifest.repository) do
+ for version, repositories in pairs(versions) do
+ for _, repo in ipairs(repositories) do
+ if repo.arch == "installed" then
+ repo.dependencies = {}
+ deps.scan_deps(repo.dependencies, manifest, pkg, version, deps_mode)
+ repo.dependencies[pkg] = nil
+ end
+ end
+ end
+ end
+end
+
+--- Filter manifest table by Lua version, removing rockspecs whose Lua version
+-- does not match.
+-- @param manifest table: a manifest table.
+-- @param lua_version string or nil: filter by Lua version
+-- @param repodir string: directory of repository being scanned
+-- @param cache table: temporary rockspec cache table
+local function filter_by_lua_version(manifest, lua_version, repodir, cache)
+ assert(type(manifest) == "table")
+ assert(type(repodir) == "string")
+ assert((not cache) or type(cache) == "table")
+
+ cache = cache or {}
+ lua_version = deps.parse_version(lua_version)
+ for pkg, versions in pairs(manifest.repository) do
+ local to_remove = {}
+ for version, repositories in pairs(versions) do
+ for _, repo in ipairs(repositories) do
+ if repo.arch == "rockspec" then
+ local pathname = dir.path(repodir, pkg.."-"..version..".rockspec")
+ local rockspec, err = cache[pathname]
+ if not rockspec then
+ rockspec, err = fetch.load_local_rockspec(pathname, true)
+ end
+ if rockspec then
+ cache[pathname] = rockspec
+ for _, dep in ipairs(rockspec.dependencies) do
+ if dep.name == "lua" then
+ if not deps.match_constraints(lua_version, dep.constraints) then
+ table.insert(to_remove, version)
+ end
+ break
+ end
+ end
+ else
+ util.printerr("Error loading rockspec for "..pkg.." "..version..": "..err)
+ end
+ end
+ end
+ end
+ if next(to_remove) then
+ for _, incompat in ipairs(to_remove) do
+ versions[incompat] = nil
+ end
+ if not next(versions) then
+ manifest.repository[pkg] = nil
+ end
+ end
+ end
+end
+
+--- Store search results in a manifest table.
+-- @param results table: The search results as returned by search.disk_search.
+-- @param manifest table: A manifest table (must contain repository, modules, commands tables).
+-- It will be altered to include the search results.
+-- @return boolean or (nil, string): true in case of success, or nil followed by an error message.
+local function store_results(results, manifest)
+ assert(type(results) == "table")
+ assert(type(manifest) == "table")
+
+ for name, versions in pairs(results) do
+ local pkgtable = manifest.repository[name] or {}
+ for version, entries in pairs(versions) do
+ local versiontable = {}
+ for _, entry in ipairs(entries) do
+ local entrytable = {}
+ entrytable.arch = entry.arch
+ if entry.arch == "installed" then
+ local rock_manifest = manif.load_rock_manifest(name, version)
+ if not rock_manifest then
+ return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?"
+ end
+
+ entrytable.modules = repos.package_modules(name, version)
+ store_package_items(manifest.modules, name, version, entrytable.modules)
+ entrytable.commands = repos.package_commands(name, version)
+ store_package_items(manifest.commands, name, version, entrytable.commands)
+ end
+ table.insert(versiontable, entrytable)
+ end
+ pkgtable[version] = versiontable
+ end
+ manifest.repository[name] = pkgtable
+ end
+ sort_package_matching_table(manifest.modules)
+ sort_package_matching_table(manifest.commands)
+ return true
+end
+
+--- Scan a LuaRocks repository and output a manifest file.
+-- A file called 'manifest' will be written in the root of the given
+-- repository directory.
+-- @param repo A local repository directory.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for the default dependency mode from the configuration.
+-- @param remote boolean: 'true' if making a manifest for a rocks server.
+-- @return boolean or (nil, string): True if manifest was generated,
+-- or nil and an error message.
+function manif.make_manifest(repo, deps_mode, remote)
+ assert(type(repo) == "string")
+ assert(type(deps_mode) == "string")
+
+ if deps_mode == "none" then deps_mode = cfg.deps_mode end
+
+ if not fs.is_dir(repo) then
+ return nil, "Cannot access repository at "..repo
+ end
+
+ local query = search.make_query("")
+ query.exact_name = false
+ query.arch = "any"
+ local results = search.disk_search(repo, query)
+ local manifest = { repository = {}, modules = {}, commands = {} }
+
+ manif_core.cache_manifest(repo, nil, manifest)
+
+ local ok, err = store_results(results, manifest)
+ if not ok then return nil, err end
+
+ if remote then
+ local cache = {}
+ for luaver in util.lua_versions() do
+ local vmanifest = { repository = {}, modules = {}, commands = {} }
+ local ok, err = store_results(results, vmanifest)
+ filter_by_lua_version(vmanifest, luaver, repo, cache)
+ save_table(repo, "manifest-"..luaver, vmanifest)
+ end
+ else
+ update_dependencies(manifest, deps_mode)
+ end
+
+ return save_table(repo, "manifest", manifest)
+end
+
+--- Update manifest file for a local repository
+-- adding information about a version of a package installed in that repository.
+-- @param name string: Name of a package from the repository.
+-- @param version string: Version of a package from the repository.
+-- @param repo string or nil: Pathname of a local repository. If not given,
+-- the default local repository is used.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for using the default dependency mode from the configuration.
+-- @return boolean or (nil, string): True if manifest was updated successfully,
+-- or nil and an error message.
+function manif.add_to_manifest(name, version, repo, deps_mode)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ local rocks_dir = path.rocks_dir(repo or cfg.root_dir)
+ assert(type(deps_mode) == "string")
+
+ if deps_mode == "none" then deps_mode = cfg.deps_mode end
+
+ local manifest, err = manif_core.load_local_manifest(rocks_dir)
+ if not manifest then
+ util.printerr("No existing manifest. Attempting to rebuild...")
+ -- Manifest built by `manif.make_manifest` should already
+ -- include information about given name and version,
+ -- no need to update it.
+ return manif.make_manifest(rocks_dir, deps_mode)
+ end
+
+ local results = {[name] = {[version] = {{arch = "installed", repo = rocks_dir}}}}
+
+ local ok, err = store_results(results, manifest)
+ if not ok then return nil, err end
+
+ update_dependencies(manifest, deps_mode)
+ return save_table(rocks_dir, "manifest", manifest)
+end
+
+--- Update manifest file for a local repository
+-- removing information about a version of a package.
+-- @param name string: Name of a package removed from the repository.
+-- @param version string: Version of a package removed from the repository.
+-- @param repo string or nil: Pathname of a local repository. If not given,
+-- the default local repository is used.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for using the default dependency mode from the configuration.
+-- @return boolean or (nil, string): True if manifest was updated successfully,
+-- or nil and an error message.
+function manif.remove_from_manifest(name, version, repo, deps_mode)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ local rocks_dir = path.rocks_dir(repo or cfg.root_dir)
+ assert(type(deps_mode) == "string")
+
+ if deps_mode == "none" then deps_mode = cfg.deps_mode end
+
+ local manifest, err = manif_core.load_local_manifest(rocks_dir)
+ if not manifest then
+ util.printerr("No existing manifest. Attempting to rebuild...")
+ -- Manifest built by `manif.make_manifest` should already
+ -- include up-to-date information, no need to update it.
+ return manif.make_manifest(rocks_dir, deps_mode)
+ end
+
+ local package_entry = manifest.repository[name]
+
+ local version_entry = package_entry[version][1]
+ remove_package_items(manifest.modules, name, version, version_entry.modules)
+ remove_package_items(manifest.commands, name, version, version_entry.commands)
+
+ package_entry[version] = nil
+ manifest.dependencies[name][version] = nil
+
+ if not next(package_entry) then
+ -- No more versions of this package.
+ manifest.repository[name] = nil
+ manifest.dependencies[name] = nil
+ end
+
+ update_dependencies(manifest, deps_mode)
+ return save_table(rocks_dir, "manifest", manifest)
+end
+
+--- Report missing dependencies for all rocks installed in a repository.
+-- @param repo string or nil: Pathname of a local repository. If not given,
+-- the default local repository is used.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for using the default dependency mode from the configuration.
+function manif.check_dependencies(repo, deps_mode)
+ local rocks_dir = path.rocks_dir(repo or cfg.root_dir)
+ assert(type(deps_mode) == "string")
+ if deps_mode == "none" then deps_mode = cfg.deps_mode end
+
+ local manifest = manif_core.load_local_manifest(rocks_dir)
+ if not manifest then
+ return
+ end
+
+ for name, versions in util.sortedpairs(manifest.repository) do
+ for version, version_entries in util.sortedpairs(versions, deps.compare_versions) do
+ for _, entry in ipairs(version_entries) do
+ if entry.arch == "installed" then
+ if manifest.dependencies[name] and manifest.dependencies[name][version] then
+ deps.report_missing_dependencies(name, version, manifest.dependencies[name][version], deps_mode)
+ end
+ end
+ end
+ end
+ end
+end
+
+function manif.zip_manifests()
+ for ver in util.lua_versions() do
+ local file = "manifest-"..ver
+ local zip = file..".zip"
+ fs.delete(dir.path(fs.current_dir(), zip))
+ fs.zip(zip, file)
+ end
+end
+
+--- Get type and name of an item (a module or a command) provided by a file.
+-- @param deploy_type string: rock manifest subtree the file comes from ("bin", "lua", or "lib").
+-- @param file_path string: path to the file relatively to deploy_type subdirectory.
+-- @return (string, string): item type ("module" or "command") and name.
+function manif.get_provided_item(deploy_type, file_path)
+ assert(type(deploy_type) == "string")
+ assert(type(file_path) == "string")
+ local item_type = deploy_type == "bin" and "command" or "module"
+ local item_name = item_type == "command" and file_path or path.path_to_module(file_path)
+ return item_type, item_name
+end
+
+local function get_providers(item_type, item_name, repo)
+ assert(type(item_type) == "string")
+ assert(type(item_name) == "string")
+ local rocks_dir = path.rocks_dir(repo or cfg.root_dir)
+ local manifest = manif_core.load_local_manifest(rocks_dir)
+ return manifest and manifest[item_type .. "s"][item_name]
+end
+
+--- Given a name of a module or a command, figure out which rock name and version
+-- correspond to it in the rock tree manifest.
+-- @param item_type string: "module" or "command".
+-- @param item_name string: module or command name.
+-- @param root string or nil: A local root dir for a rocks tree. If not given, the default is used.
+-- @return (string, string) or nil: name and version of the provider rock or nil if there
+-- is no provider.
+function manif.get_current_provider(item_type, item_name, repo)
+ local providers = get_providers(item_type, item_name, repo)
+ if providers then
+ return providers[1]:match("([^/]*)/([^/]*)")
+ end
+end
+
+function manif.get_next_provider(item_type, item_name, repo)
+ local providers = get_providers(item_type, item_name, repo)
+ if providers and providers[2] then
+ return providers[2]:match("([^/]*)/([^/]*)")
+ end
+end
+
+--- Given a name of a module or a command provided by a package, figure out
+-- which file provides it.
+-- @param name string: package name.
+-- @param version string: package version.
+-- @param item_type string: "module" or "command".
+-- @param item_name string: module or command name.
+-- @param root string or nil: A local root dir for a rocks tree. If not given, the default is used.
+-- @return (string, string): rock manifest subtree the file comes from ("bin", "lua", or "lib")
+-- and path to the providing file relatively to that subtree.
+function manif.get_providing_file(name, version, item_type, item_name, repo)
+ local rocks_dir = path.rocks_dir(repo or cfg.root_dir)
+ local manifest = manif_core.load_local_manifest(rocks_dir)
+
+ local entry_table = manifest.repository[name][version][1]
+ local file_path = entry_table[item_type .. "s"][item_name]
+
+ if item_type == "command" then
+ return "bin", file_path
+ end
+
+ -- A module can be in "lua" or "lib". Decide based on extension first:
+ -- most likely Lua modules are in "lua/" and C modules are in "lib/".
+ if file_path:match("%." .. cfg.lua_extension .. "$") then
+ return "lua", file_path
+ elseif file_path:match("%." .. cfg.lib_extension .. "$") then
+ return "lib", file_path
+ end
+
+ -- Fallback to rock manifest scanning.
+ local rock_manifest = manif.load_rock_manifest(name, version)
+ local subtree = rock_manifest.lib
+
+ for path_part in file_path:gmatch("[^/]+") do
+ if type(subtree) == "table" then
+ subtree = subtree[path_part]
+ else
+ -- Assume it's in "lua/" if it's not in "lib/".
+ return "lua", file_path
+ end
+ end
+
+ return type(subtree) == "string" and "lib" or "lua", file_path
+end
+
+return manif
diff --git a/Utils/luarocks/lua/luarocks/manif_core.lua b/Utils/luarocks/lua/luarocks/manif_core.lua
new file mode 100644
index 000000000..82e7ea4de
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/manif_core.lua
@@ -0,0 +1,106 @@
+
+--- Core functions for querying manifest files.
+-- This module requires no specific 'fs' functionality.
+local manif_core = {}
+package.loaded["luarocks.manif_core"] = manif_core
+
+local persist = require("luarocks.persist")
+local type_check = require("luarocks.type_check")
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+local path = require("luarocks.path")
+
+-- Table with repository identifiers as keys and tables mapping
+-- Lua versions to cached loaded manifests as values.
+local manifest_cache = {}
+
+--- Cache a loaded manifest.
+-- @param repo_url string: The repository identifier.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @param manifest table: the manifest to be cached.
+function manif_core.cache_manifest(repo_url, lua_version, manifest)
+ lua_version = lua_version or cfg.lua_version
+ manifest_cache[repo_url] = manifest_cache[repo_url] or {}
+ manifest_cache[repo_url][lua_version] = manifest
+end
+
+--- Attempt to get cached loaded manifest.
+-- @param repo_url string: The repository identifier.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @return table or nil: loaded manifest or nil if cache is empty.
+function manif_core.get_cached_manifest(repo_url, lua_version)
+ lua_version = lua_version or cfg.lua_version
+ return manifest_cache[repo_url] and manifest_cache[repo_url][lua_version]
+end
+
+--- Back-end function that actually loads the manifest
+-- and stores it in the manifest cache.
+-- @param file string: The local filename of the manifest file.
+-- @param repo_url string: The repository identifier.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @param quick boolean: If given, skips type checking.
+-- @return table or (nil, string, string): the manifest or nil,
+-- error message and error code ("open", "load", "run" or "type").
+function manif_core.manifest_loader(file, repo_url, lua_version, quick)
+ local manifest, err, errcode = persist.load_into_table(file)
+ if not manifest then
+ return nil, "Failed loading manifest for "..repo_url..": "..err, errcode
+ end
+ local globals = err
+ if not quick then
+ local ok, err = type_check.type_check_manifest(manifest, globals)
+ if not ok then
+ return nil, "Error checking manifest: "..err, "type"
+ end
+ end
+
+ manif_core.cache_manifest(repo_url, lua_version, manifest)
+ return manifest
+end
+
+--- Load a local manifest describing a repository.
+-- All functions that use manifest tables assume they were obtained
+-- through either this function or load_manifest.
+-- @param repo_url string: URL or pathname for the repository.
+-- @return table or (nil, string, string): A table representing the manifest,
+-- or nil followed by an error message and an error code, see manifest_loader.
+function manif_core.load_local_manifest(repo_url)
+ assert(type(repo_url) == "string")
+
+ local cached_manifest = manif_core.get_cached_manifest(repo_url)
+ if cached_manifest then
+ return cached_manifest
+ end
+
+ local pathname = dir.path(repo_url, "manifest")
+ return manif_core.manifest_loader(pathname, repo_url, nil, true)
+end
+
+--- Get all versions of a package listed in a manifest file.
+-- @param name string: a package name.
+-- @param deps_mode string: "one", to use only the currently
+-- configured tree; "order" to select trees based on order
+-- (use the current tree and all trees below it on the list)
+-- or "all", to use all trees.
+-- @return table: An array of strings listing installed
+-- versions of a package.
+function manif_core.get_versions(name, deps_mode)
+ assert(type(name) == "string")
+ assert(type(deps_mode) == "string")
+
+ local version_set = {}
+ path.map_trees(deps_mode, function(tree)
+ local manifest = manif_core.load_local_manifest(path.rocks_dir(tree))
+
+ if manifest and manifest.repository[name] then
+ for version in pairs(manifest.repository[name]) do
+ version_set[version] = true
+ end
+ end
+ end)
+
+ return util.keys(version_set)
+end
+
+return manif_core
diff --git a/Utils/luarocks/lua/luarocks/new_version.lua b/Utils/luarocks/lua/luarocks/new_version.lua
new file mode 100644
index 000000000..91f7607c3
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/new_version.lua
@@ -0,0 +1,200 @@
+
+--- Module implementing the LuaRocks "new_version" command.
+-- Utility function that writes a new rockspec, updating data from a previous one.
+local new_version = {}
+
+local util = require("luarocks.util")
+local download = require("luarocks.download")
+local fetch = require("luarocks.fetch")
+local persist = require("luarocks.persist")
+local fs = require("luarocks.fs")
+local type_check = require("luarocks.type_check")
+
+util.add_run_function(new_version)
+new_version.help_summary = "Auto-write a rockspec for a new version of a rock."
+new_version.help_arguments = "[--tag=] [|] [] []"
+new_version.help = [[
+This is a utility function that writes a new rockspec, updating data
+from a previous one.
+
+If a package name is given, it downloads the latest rockspec from the
+default server. If a rockspec is given, it uses it instead. If no argument
+is given, it looks for a rockspec same way 'luarocks make' does.
+
+If the version number is not given and tag is passed using --tag,
+it is used as the version, with 'v' removed from beginning.
+Otherwise, it only increments the revision number of the given
+(or downloaded) rockspec.
+
+If a URL is given, it replaces the one from the old rockspec with the
+given URL. If a URL is not given and a new version is given, it tries
+to guess the new URL by replacing occurrences of the version number
+in the URL or tag. It also tries to download the new URL to determine
+the new MD5 checksum.
+
+If a tag is given, it replaces the one from the old rockspec. If there is
+an old tag but no new one passed, it is guessed in the same way URL is.
+
+WARNING: it writes the new rockspec to the current directory,
+overwriting the file if it already exists.
+]]
+
+local function try_replace(tbl, field, old, new)
+ if not tbl[field] then
+ return false
+ end
+ local old_field = tbl[field]
+ local new_field = tbl[field]:gsub(old, new)
+ if new_field ~= old_field then
+ util.printout("Guessing new '"..field.."' field as "..new_field)
+ tbl[field] = new_field
+ return true
+ end
+ return false
+end
+
+-- Try to download source file using URL from a rockspec.
+-- If it specified MD5, update it.
+-- @return (true, false) if MD5 was not specified or it stayed same,
+-- (true, true) if MD5 changed, (nil, string) on error.
+local function check_url_and_update_md5(out_rs)
+ local file, temp_dir = fetch.fetch_url_at_temp_dir(out_rs.source.url, "luarocks-new-version-"..out_rs.package)
+ if not file then
+ util.printerr("Warning: invalid URL - "..temp_dir)
+ return true, false
+ end
+
+ local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, out_rs.source.url, out_rs.source.dir)
+ if not inferred_dir then
+ return nil, found_dir
+ end
+
+ if found_dir and found_dir ~= inferred_dir then
+ out_rs.source.dir = found_dir
+ end
+
+ if file then
+ if out_rs.source.md5 then
+ util.printout("File successfully downloaded. Updating MD5 checksum...")
+ local new_md5, err = fs.get_md5(file)
+ if not new_md5 then
+ return nil, err
+ end
+ local old_md5 = out_rs.source.md5
+ out_rs.source.md5 = new_md5
+ return true, new_md5 ~= old_md5
+ else
+ util.printout("File successfully downloaded.")
+ return true, false
+ end
+ end
+end
+
+local function update_source_section(out_rs, url, tag, old_ver, new_ver)
+ if tag then
+ out_rs.source.tag = tag
+ end
+ if url then
+ out_rs.source.url = url
+ return check_url_and_update_md5(out_rs)
+ end
+ if new_ver == old_ver then
+ return true
+ end
+ if out_rs.source.dir then
+ try_replace(out_rs.source, "dir", old_ver, new_ver)
+ end
+ if out_rs.source.file then
+ try_replace(out_rs.source, "file", old_ver, new_ver)
+ end
+ if try_replace(out_rs.source, "url", old_ver, new_ver) then
+ return check_url_and_update_md5(out_rs)
+ end
+ if tag or try_replace(out_rs.source, "tag", old_ver, new_ver) then
+ return true
+ end
+ -- Couldn't replace anything significant, use the old URL.
+ local ok, md5_changed = check_url_and_update_md5(out_rs)
+ if not ok then
+ return nil, md5_changed
+ end
+ if md5_changed then
+ util.printerr("Warning: URL is the same, but MD5 has changed. Old rockspec is broken.")
+ end
+ return true
+end
+
+function new_version.command(flags, input, version, url)
+ if not input then
+ local err
+ input, err = util.get_default_rockspec()
+ if not input then
+ return nil, err
+ end
+ end
+ assert(type(input) == "string")
+
+ local filename, err
+ if input:match("rockspec$") then
+ filename, err = fetch.fetch_url(input)
+ if not filename then
+ return nil, err
+ end
+ else
+ filename, err = download.download("rockspec", input:lower())
+ if not filename then
+ return nil, err
+ end
+ end
+
+ local valid_rs, err = fetch.load_rockspec(filename)
+ if not valid_rs then
+ return nil, err
+ end
+
+ local old_ver, old_rev = valid_rs.version:match("(.*)%-(%d+)$")
+ local new_ver, new_rev
+
+ if flags.tag and not version then
+ version = flags.tag:gsub("^v", "")
+ end
+
+ if version then
+ new_ver, new_rev = version:match("(.*)%-(%d+)$")
+ new_rev = tonumber(new_rev)
+ if not new_rev then
+ new_ver = version
+ new_rev = 1
+ end
+ else
+ new_ver = old_ver
+ new_rev = tonumber(old_rev) + 1
+ end
+ local new_rockver = new_ver:gsub("-", "")
+
+ local out_rs, err = persist.load_into_table(filename)
+ local out_name = out_rs.package:lower()
+ out_rs.version = new_rockver.."-"..new_rev
+
+ local ok, err = update_source_section(out_rs, url, flags.tag, old_ver, new_ver)
+ if not ok then return nil, err end
+
+ if out_rs.build and out_rs.build.type == "module" then
+ out_rs.build.type = "builtin"
+ end
+
+ local out_filename = out_name.."-"..new_rockver.."-"..new_rev..".rockspec"
+
+ persist.save_from_table(out_filename, out_rs, type_check.rockspec_order)
+
+ util.printout("Wrote "..out_filename)
+
+ local valid_out_rs, err = fetch.load_local_rockspec(out_filename)
+ if not valid_out_rs then
+ return nil, "Failed loading generated rockspec: "..err
+ end
+
+ return true
+end
+
+return new_version
diff --git a/Utils/luarocks/lua/luarocks/pack.lua b/Utils/luarocks/lua/luarocks/pack.lua
new file mode 100644
index 000000000..35bbe8380
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/pack.lua
@@ -0,0 +1,197 @@
+
+--- Module implementing the LuaRocks "pack" command.
+-- Creates a rock, packing sources or binaries.
+local pack = {}
+package.loaded["luarocks.pack"] = pack
+
+local unpack = unpack or table.unpack
+
+local path = require("luarocks.path")
+local repos = require("luarocks.repos")
+local fetch = require("luarocks.fetch")
+local fs = require("luarocks.fs")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local dir = require("luarocks.dir")
+local manif = require("luarocks.manif")
+local search = require("luarocks.search")
+
+util.add_run_function(pack)
+pack.help_summary = "Create a rock, packing sources or binaries."
+pack.help_arguments = "{| []}"
+pack.help = [[
+Argument may be a rockspec file, for creating a source rock,
+or the name of an installed package, for creating a binary rock.
+In the latter case, the app version may be given as a second
+argument.
+]]
+
+--- Create a source rock.
+-- Packages a rockspec and its required source files in a rock
+-- file with the .src.rock extension, which can later be built and
+-- installed with the "build" command.
+-- @param rockspec_file string: An URL or pathname for a rockspec file.
+-- @return string or (nil, string): The filename of the resulting
+-- .src.rock file; or nil and an error message.
+function pack.pack_source_rock(rockspec_file)
+ assert(type(rockspec_file) == "string")
+
+ local rockspec, err = fetch.load_rockspec(rockspec_file)
+ if err then
+ return nil, "Error loading rockspec: "..err
+ end
+ rockspec_file = rockspec.local_filename
+
+ local name_version = rockspec.name .. "-" .. rockspec.version
+ local rock_file = fs.absolute_name(name_version .. ".src.rock")
+
+ local source_file, source_dir = fetch.fetch_sources(rockspec, false)
+ if not source_file then
+ return nil, source_dir
+ end
+ local ok, err = fs.change_dir(source_dir)
+ if not ok then return nil, err end
+
+ fs.delete(rock_file)
+ fs.copy(rockspec_file, source_dir, cfg.perm_read)
+ if not fs.zip(rock_file, dir.base_name(rockspec_file), dir.base_name(source_file)) then
+ return nil, "Failed packing "..rock_file
+ end
+ fs.pop_dir()
+
+ return rock_file
+end
+
+local function copy_back_files(name, version, file_tree, deploy_dir, pack_dir, perms)
+ local ok, err = fs.make_dir(pack_dir)
+ if not ok then return nil, err end
+ for file, sub in pairs(file_tree) do
+ local source = dir.path(deploy_dir, file)
+ local target = dir.path(pack_dir, file)
+ if type(sub) == "table" then
+ local ok, err = copy_back_files(name, version, sub, source, target)
+ if not ok then return nil, err end
+ else
+ local versioned = path.versioned_name(source, deploy_dir, name, version)
+ if fs.exists(versioned) then
+ fs.copy(versioned, target, perms)
+ else
+ fs.copy(source, target, perms)
+ end
+ end
+ end
+ return true
+end
+
+-- @param name string: Name of package to pack.
+-- @param version string or nil: A version number may also be passed.
+-- @param tree string or nil: An optional tree to pick the package from.
+-- @return string or (nil, string): The filename of the resulting
+-- .src.rock file; or nil and an error message.
+local function do_pack_binary_rock(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string" or not version)
+
+ local repo, repo_url
+ name, version, repo, repo_url = search.pick_installed_rock(name, version, tree)
+ if not name then
+ return nil, version
+ end
+
+ local root = path.root_dir(repo_url)
+ local prefix = path.install_dir(name, version, root)
+ if not fs.exists(prefix) then
+ return nil, "'"..name.." "..version.."' does not seem to be an installed rock."
+ end
+
+ local rock_manifest = manif.load_rock_manifest(name, version, root)
+ if not rock_manifest then
+ return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?"
+ end
+
+ local name_version = name .. "-" .. version
+ local rock_file = fs.absolute_name(name_version .. "."..cfg.arch..".rock")
+
+ local temp_dir = fs.make_temp_dir("pack")
+ fs.copy_contents(prefix, temp_dir)
+
+ local is_binary = false
+ if rock_manifest.lib then
+ local ok, err = copy_back_files(name, version, rock_manifest.lib, path.deploy_lib_dir(root), dir.path(temp_dir, "lib"), cfg.perm_exec)
+ if not ok then return nil, "Failed copying back files: " .. err end
+ is_binary = true
+ end
+ if rock_manifest.lua then
+ local ok, err = copy_back_files(name, version, rock_manifest.lua, path.deploy_lua_dir(root), dir.path(temp_dir, "lua"), cfg.perm_read)
+ if not ok then return nil, "Failed copying back files: " .. err end
+ end
+
+ local ok, err = fs.change_dir(temp_dir)
+ if not ok then return nil, err end
+ if not is_binary and not repos.has_binaries(name, version) then
+ rock_file = rock_file:gsub("%."..cfg.arch:gsub("%-","%%-").."%.", ".all.")
+ end
+ fs.delete(rock_file)
+ if not fs.zip(rock_file, unpack(fs.list_dir())) then
+ return nil, "Failed packing "..rock_file
+ end
+ fs.pop_dir()
+ fs.delete(temp_dir)
+ return rock_file
+end
+
+function pack.pack_binary_rock(name, version, cmd, ...)
+
+ -- The --pack-binary-rock option for "luarocks build" basically performs
+ -- "luarocks build" on a temporary tree and then "luarocks pack". The
+ -- alternative would require refactoring parts of luarocks.build and
+ -- luarocks.pack, which would save a few file operations: the idea would be
+ -- to shave off the final deploy steps from the build phase and the initial
+ -- collect steps from the pack phase.
+
+ local temp_dir, err = fs.make_temp_dir("luarocks-build-pack-"..dir.base_name(name))
+ if not temp_dir then
+ return nil, "Failed creating temporary directory: "..err
+ end
+ util.schedule_function(fs.delete, temp_dir)
+
+ path.use_tree(temp_dir)
+ local ok, err = cmd(...)
+ if not ok then
+ return nil, err
+ end
+ local rname, rversion = path.parse_name(name)
+ if not rname then
+ rname, rversion = name, version
+ end
+ return do_pack_binary_rock(rname, rversion, temp_dir)
+end
+
+--- Driver function for the "pack" command.
+-- @param arg string: may be a rockspec file, for creating a source rock,
+-- or the name of an installed package, for creating a binary rock.
+-- @param version string or nil: if the name of a package is given, a
+-- version may also be passed.
+-- @return boolean or (nil, string): true if successful or nil followed
+-- by an error message.
+function pack.command(flags, arg, version)
+ assert(type(version) == "string" or not version)
+ if type(arg) ~= "string" then
+ return nil, "Argument missing. "..util.see_help("pack")
+ end
+
+ local file, err
+ if arg:match(".*%.rockspec") then
+ file, err = pack.pack_source_rock(arg)
+ else
+ file, err = do_pack_binary_rock(arg:lower(), version, flags["tree"])
+ end
+ if err then
+ return nil, err
+ else
+ util.printout("Packed: "..file)
+ return true
+ end
+end
+
+return pack
diff --git a/Utils/luarocks/lua/luarocks/path.lua b/Utils/luarocks/lua/luarocks/path.lua
new file mode 100644
index 000000000..dafc64e7c
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/path.lua
@@ -0,0 +1,388 @@
+
+--- LuaRocks-specific path handling functions.
+-- All paths are configured in this module, making it a single
+-- point where the layout of the local installation is defined in LuaRocks.
+local path = {}
+
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+
+--- Infer rockspec filename from a rock filename.
+-- @param rock_name string: Pathname of a rock file.
+-- @return string: Filename of the rockspec, without path.
+function path.rockspec_name_from_rock(rock_name)
+ assert(type(rock_name) == "string")
+ local base_name = dir.base_name(rock_name)
+ return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec"
+end
+
+function path.rocks_dir(tree)
+ if type(tree) == "string" then
+ return dir.path(tree, cfg.rocks_subdir)
+ else
+ assert(type(tree) == "table")
+ return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir)
+ end
+end
+
+function path.root_dir(rocks_dir)
+ assert(type(rocks_dir) == "string")
+ return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$")
+end
+
+function path.rocks_tree_to_string(tree)
+ if type(tree) == "string" then
+ return tree
+ else
+ assert(type(tree) == "table")
+ return tree.root
+ end
+end
+
+function path.deploy_bin_dir(tree)
+ if type(tree) == "string" then
+ return dir.path(tree, "bin")
+ else
+ assert(type(tree) == "table")
+ return tree.bin_dir or dir.path(tree.root, "bin")
+ end
+end
+
+function path.deploy_lua_dir(tree)
+ if type(tree) == "string" then
+ return dir.path(tree, cfg.lua_modules_path)
+ else
+ assert(type(tree) == "table")
+ return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
+ end
+end
+
+function path.deploy_lib_dir(tree)
+ if type(tree) == "string" then
+ return dir.path(tree, cfg.lib_modules_path)
+ else
+ assert(type(tree) == "table")
+ return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
+ end
+end
+
+function path.manifest_file(tree)
+ if type(tree) == "string" then
+ return dir.path(tree, cfg.rocks_subdir, "manifest")
+ else
+ assert(type(tree) == "table")
+ return (tree.rocks_dir and dir.path(tree.rocks_dir, "manifest")) or dir.path(tree.root, cfg.rocks_subdir, "manifest")
+ end
+end
+
+--- Get the directory for all versions of a package in a tree.
+-- @param name string: The package name.
+-- @return string: The resulting path -- does not guarantee that
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- the package (and by extension, the path) exists.
+function path.versions_dir(name, tree)
+ assert(type(name) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name)
+end
+
+--- Get the local installation directory (prefix) for a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.install_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version)
+end
+
+--- Get the local filename of the rockspec of an installed rock.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the file) exists.
+function path.rockspec_file(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec")
+end
+
+--- Get the local filename of the rock_manifest file of an installed rock.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the file) exists.
+function path.rock_manifest_file(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "rock_manifest")
+end
+
+--- Get the local installation directory for C libraries of a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.lib_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "lib")
+end
+
+--- Get the local installation directory for Lua modules of a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.lua_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "lua")
+end
+
+--- Get the local installation directory for documentation of a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.doc_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "doc")
+end
+
+--- Get the local installation directory for configuration files of a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.conf_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "conf")
+end
+
+--- Get the local installation directory for command-line scripts
+-- of a package.
+-- @param name string: The package name.
+-- @param version string: The package version.
+-- @param tree string or nil: If given, specifies the local tree to use.
+-- @return string: The resulting path -- does not guarantee that
+-- the package (and by extension, the path) exists.
+function path.bin_dir(name, version, tree)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ tree = tree or cfg.root_dir
+ return dir.path(path.rocks_dir(tree), name, version, "bin")
+end
+
+--- Extract name, version and arch of a rock filename,
+-- or name, version and "rockspec" from a rockspec name.
+-- @param file_name string: pathname of a rock or rockspec
+-- @return (string, string, string) or nil: name, version and arch
+-- or nil if name could not be parsed
+function path.parse_name(file_name)
+ assert(type(file_name) == "string")
+ if file_name:match("%.rock$") then
+ return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$")
+ else
+ return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)")
+ end
+end
+
+--- Make a rockspec or rock URL.
+-- @param pathname string: Base URL or pathname.
+-- @param name string: Package name.
+-- @param version string: Package version.
+-- @param arch string: Architecture identifier, or "rockspec" or "installed".
+-- @return string: A URL or pathname following LuaRocks naming conventions.
+function path.make_url(pathname, name, version, arch)
+ assert(type(pathname) == "string")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ assert(type(arch) == "string")
+
+ local filename = name.."-"..version
+ if arch == "installed" then
+ filename = dir.path(name, version, filename..".rockspec")
+ elseif arch == "rockspec" then
+ filename = filename..".rockspec"
+ else
+ filename = filename.."."..arch..".rock"
+ end
+ return dir.path(pathname, filename)
+end
+
+--- Convert a pathname to a module identifier.
+-- In Unix, for example, a path "foo/bar/baz.lua" is converted to
+-- "foo.bar.baz"; "bla/init.lua" returns "bla"; "foo.so" returns "foo".
+-- @param file string: Pathname of module
+-- @return string: The module identifier, or nil if given path is
+-- not a conformant module path (the function does not check if the
+-- path actually exists).
+function path.path_to_module(file)
+ assert(type(file) == "string")
+
+ local name = file:match("(.*)%."..cfg.lua_extension.."$")
+ if name then
+ name = name:gsub(dir.separator, ".")
+ local init = name:match("(.*)%.init$")
+ if init then
+ name = init
+ end
+ else
+ name = file:match("(.*)%."..cfg.lib_extension.."$")
+ if name then
+ name = name:gsub(dir.separator, ".")
+ end
+ end
+ if not name then name = file end
+ name = name:gsub("^%.+", ""):gsub("%.+$", "")
+ return name
+end
+
+--- Obtain the directory name where a module should be stored.
+-- For example, on Unix, "foo.bar.baz" will return "foo/bar".
+-- @param mod string: A module name in Lua dot-separated format.
+-- @return string: A directory name using the platform's separator.
+function path.module_to_path(mod)
+ assert(type(mod) == "string")
+ return (mod:gsub("[^.]*$", ""):gsub("%.", dir.separator))
+end
+
+--- Set up path-related variables for a given rock.
+-- Create a "variables" table in the rockspec table, containing
+-- adjusted variables according to the configuration file.
+-- @param rockspec table: The rockspec table.
+function path.configure_paths(rockspec)
+ assert(type(rockspec) == "table")
+ local vars = {}
+ for k,v in pairs(cfg.variables) do
+ vars[k] = v
+ end
+ local name, version = rockspec.name, rockspec.version
+ vars.PREFIX = path.install_dir(name, version)
+ vars.LUADIR = path.lua_dir(name, version)
+ vars.LIBDIR = path.lib_dir(name, version)
+ vars.CONFDIR = path.conf_dir(name, version)
+ vars.BINDIR = path.bin_dir(name, version)
+ vars.DOCDIR = path.doc_dir(name, version)
+ rockspec.variables = vars
+end
+
+--- Produce a versioned version of a filename.
+-- @param file string: filename (must start with prefix)
+-- @param prefix string: Path prefix for file
+-- @param name string: Rock name
+-- @param version string: Rock version
+-- @return string: a pathname with the same directory parts and a versioned basename.
+function path.versioned_name(file, prefix, name, version)
+ assert(type(file) == "string")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local rest = file:sub(#prefix+1):gsub("^/*", "")
+ local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
+ return dir.path(prefix, name_version.."-"..rest)
+end
+
+function path.use_tree(tree)
+ cfg.root_dir = tree
+ cfg.rocks_dir = path.rocks_dir(tree)
+ cfg.deploy_bin_dir = path.deploy_bin_dir(tree)
+ cfg.deploy_lua_dir = path.deploy_lua_dir(tree)
+ cfg.deploy_lib_dir = path.deploy_lib_dir(tree)
+end
+
+--- Apply a given function to the active rocks trees based on chosen dependency mode.
+-- @param deps_mode string: Dependency mode: "one" for the current default tree,
+-- "all" for all trees, "order" for all trees with priority >= the current default,
+-- "none" for no trees (this function becomes a nop).
+-- @param fn function: function to be applied, with the tree dir (string) as the first
+-- argument and the remaining varargs of map_trees as the following arguments.
+-- @return a table with all results of invocations of fn collected.
+function path.map_trees(deps_mode, fn, ...)
+ local result = {}
+ if deps_mode == "one" then
+ table.insert(result, (fn(cfg.root_dir, ...)) or 0)
+ elseif deps_mode == "all" or deps_mode == "order" then
+ local use = false
+ if deps_mode == "all" then
+ use = true
+ end
+ for _, tree in ipairs(cfg.rocks_trees) do
+ if dir.normalize(path.rocks_tree_to_string(tree)) == dir.normalize(path.rocks_tree_to_string(cfg.root_dir)) then
+ use = true
+ end
+ if use then
+ table.insert(result, (fn(tree, ...)) or 0)
+ end
+ end
+ end
+ return result
+end
+
+local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
+
+--- Return the pathname of the file that would be loaded for a module, indexed.
+-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
+-- @param name string: name of the package (eg. "luasocket")
+-- @param version string: version number (eg. "2.0.2-1")
+-- @param tree string: repository path (eg. "/usr/local")
+-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
+-- This is done this way for use by select_module in luarocks.loader.
+-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
+function path.which_i(file_name, name, version, tree, i)
+ local deploy_dir
+ local extension = file_name:match("%.[a-z]+$")
+ if is_src_extension[extension] then
+ deploy_dir = path.deploy_lua_dir(tree)
+ file_name = dir.path(deploy_dir, file_name)
+ else
+ deploy_dir = path.deploy_lib_dir(tree)
+ file_name = dir.path(deploy_dir, file_name)
+ end
+ if i > 1 then
+ file_name = path.versioned_name(file_name, deploy_dir, name, version)
+ end
+ return file_name
+end
+
+--- Return the pathname of the file that would be loaded for a module,
+-- returning the versioned pathname if given version is not the default version
+-- in the given manifest.
+-- @param module_name string: module name (eg. "socket.core")
+-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
+-- @param name string: name of the package (eg. "luasocket")
+-- @param version string: version number (eg. "2.0.2-1")
+-- @param tree string: repository path (eg. "/usr/local")
+-- @param manifest table: the manifest table for the tree.
+-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
+function path.which(module_name, file_name, name, version, tree, manifest)
+ local versions = manifest.modules[module_name]
+ assert(versions)
+ for i, name_version in ipairs(versions) do
+ if name_version == name.."/"..version then
+ return path.which_i(file_name, name, version, tree, i):gsub("//", "/")
+ end
+ end
+ assert(false)
+end
+
+return path
diff --git a/Utils/luarocks/lua/luarocks/path_cmd.lua b/Utils/luarocks/lua/luarocks/path_cmd.lua
new file mode 100644
index 000000000..eba85d46d
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/path_cmd.lua
@@ -0,0 +1,69 @@
+
+--- @module luarocks.path_cmd
+-- Driver for the `luarocks path` command.
+local path_cmd = {}
+
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+
+util.add_run_function(path_cmd)
+path_cmd.help_summary = "Return the currently configured package path."
+path_cmd.help_arguments = ""
+path_cmd.help = [[
+Returns the package path currently configured for this installation
+of LuaRocks, formatted as shell commands to update LUA_PATH and LUA_CPATH.
+
+--bin Adds the system path to the output
+
+--append Appends the paths to the existing paths. Default is to prefix
+ the LR paths to the existing paths.
+
+--lr-path Exports the Lua path (not formatted as shell command)
+
+--lr-cpath Exports the Lua cpath (not formatted as shell command)
+
+--lr-bin Exports the system path (not formatted as shell command)
+
+
+On Unix systems, you may run:
+ eval `luarocks path`
+And on Windows:
+ luarocks path > "%temp%\_lrp.bat" && call "%temp%\_lrp.bat" && del "%temp%\_lrp.bat"
+]]
+
+--- Driver function for "path" command.
+-- @return boolean This function always succeeds.
+function path_cmd.command(flags)
+ local lr_path, lr_cpath, lr_bin = cfg.package_paths(flags["tree"])
+ local path_sep = cfg.export_path_separator
+
+ if flags["lr-path"] then
+ util.printout(util.remove_path_dupes(lr_path, ';'))
+ return true
+ elseif flags["lr-cpath"] then
+ util.printout(util.remove_path_dupes(lr_cpath, ';'))
+ return true
+ elseif flags["lr-bin"] then
+ util.printout(util.remove_path_dupes(lr_bin, path_sep))
+ return true
+ end
+
+ if flags["append"] then
+ lr_path = package.path .. ";" .. lr_path
+ lr_cpath = package.cpath .. ";" .. lr_cpath
+ lr_bin = os.getenv("PATH") .. path_sep .. lr_bin
+ else
+ lr_path = lr_path.. ";" .. package.path
+ lr_cpath = lr_cpath .. ";" .. package.cpath
+ lr_bin = lr_bin .. path_sep .. os.getenv("PATH")
+ end
+
+ util.printout(cfg.export_lua_path:format(util.remove_path_dupes(lr_path, ';')))
+ util.printout(cfg.export_lua_cpath:format(util.remove_path_dupes(lr_cpath, ';')))
+ if flags["bin"] then
+ util.printout(cfg.export_path:format(util.remove_path_dupes(lr_bin, path_sep)))
+ end
+ return true
+end
+
+return path_cmd
diff --git a/Utils/luarocks/lua/luarocks/persist.lua b/Utils/luarocks/lua/luarocks/persist.lua
new file mode 100644
index 000000000..708f07bed
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/persist.lua
@@ -0,0 +1,210 @@
+
+--- Utility module for loading files into tables and
+-- saving tables into files.
+-- Implemented separately to avoid interdependencies,
+-- as it is used in the bootstrapping stage of the cfg module.
+local persist = {}
+package.loaded["luarocks.persist"] = persist
+
+local util = require("luarocks.util")
+
+--- Load and run a Lua file in an environment.
+-- @param filename string: the name of the file.
+-- @param env table: the environment table.
+-- @return (true, any) or (nil, string, string): true and the return value
+-- of the file, or nil, an error message and an error code ("open", "load"
+-- or "run") in case of errors.
+local function run_file(filename, env)
+ local fd, err = io.open(filename)
+ if not fd then
+ return nil, err, "open"
+ end
+ local str, err = fd:read("*a")
+ fd:close()
+ if not str then
+ return nil, err, "open"
+ end
+ str = str:gsub("^#![^\n]*\n", "")
+ local chunk, ran
+ if _VERSION == "Lua 5.1" then -- Lua 5.1
+ chunk, err = loadstring(str, filename)
+ if chunk then
+ setfenv(chunk, env)
+ ran, err = pcall(chunk)
+ end
+ else -- Lua 5.2
+ chunk, err = load(str, filename, "t", env)
+ if chunk then
+ ran, err = pcall(chunk)
+ end
+ end
+ if not chunk then
+ return nil, "Error loading file: "..err, "load"
+ end
+ if not ran then
+ return nil, "Error running file: "..err, "run"
+ end
+ return true, err
+end
+
+--- Load a Lua file containing assignments, storing them in a table.
+-- The global environment is not propagated to the loaded file.
+-- @param filename string: the name of the file.
+-- @param tbl table or nil: if given, this table is used to store
+-- loaded values.
+-- @return (table, table) or (nil, string, string): a table with the file's assignments
+-- as fields and set of undefined globals accessed in file,
+-- or nil, an error message and an error code ("open"; couldn't open the file,
+-- "load"; compile-time error, or "run"; run-time error)
+-- in case of errors.
+function persist.load_into_table(filename, tbl)
+ assert(type(filename) == "string")
+ assert(type(tbl) == "table" or not tbl)
+
+ local result = tbl or {}
+ local globals = {}
+ local globals_mt = {
+ __index = function(t, k)
+ globals[k] = true
+ end
+ }
+ local save_mt = getmetatable(result)
+ setmetatable(result, globals_mt)
+
+ local ok, err, errcode = run_file(filename, result)
+
+ setmetatable(result, save_mt)
+
+ if not ok then
+ return nil, err, errcode
+ end
+ return result, globals
+end
+
+local write_table
+
+--- Write a value as Lua code.
+-- This function handles only numbers and strings, invoking write_table
+-- to write tables.
+-- @param out table or userdata: a writer object supporting :write() method.
+-- @param v: the value to be written.
+-- @param level number: the indentation level
+-- @param sub_order table: optional prioritization table
+-- @see write_table
+local function write_value(out, v, level, sub_order)
+ if type(v) == "table" then
+ write_table(out, v, level + 1, sub_order)
+ elseif type(v) == "string" then
+ if v:match("[\r\n]") then
+ local open, close = "[[", "]]"
+ local equals = 0
+ local v_with_bracket = v.."]"
+ while v_with_bracket:find(close, 1, true) do
+ equals = equals + 1
+ local eqs = ("="):rep(equals)
+ open, close = "["..eqs.."[", "]"..eqs.."]"
+ end
+ out:write(open.."\n"..v..close)
+ else
+ out:write(("%q"):format(v))
+ end
+ else
+ out:write(tostring(v))
+ end
+end
+
+--- Write a table as Lua code in curly brackets notation to a writer object.
+-- Only numbers, strings and tables (containing numbers, strings
+-- or other recursively processed tables) are supported.
+-- @param out table or userdata: a writer object supporting :write() method.
+-- @param tbl table: the table to be written.
+-- @param level number: the indentation level
+-- @param field_order table: optional prioritization table
+write_table = function(out, tbl, level, field_order)
+ out:write("{")
+ local sep = "\n"
+ local indentation = " "
+ local indent = true
+ local i = 1
+ for k, v, sub_order in util.sortedpairs(tbl, field_order) do
+ out:write(sep)
+ if indent then
+ for n = 1,level do out:write(indentation) end
+ end
+
+ if k == i then
+ i = i + 1
+ else
+ if type(k) == "string" and k:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then
+ out:write(k)
+ else
+ out:write("[")
+ write_value(out, k, level)
+ out:write("]")
+ end
+
+ out:write(" = ")
+ end
+
+ write_value(out, v, level, sub_order)
+ if type(v) == "number" then
+ sep = ", "
+ indent = false
+ else
+ sep = ",\n"
+ indent = true
+ end
+ end
+ if sep ~= "\n" then
+ out:write("\n")
+ for n = 1,level-1 do out:write(indentation) end
+ end
+ out:write("}")
+end
+
+--- Write a table as series of assignments to a writer object.
+-- @param out table or userdata: a writer object supporting :write() method.
+-- @param tbl table: the table to be written.
+-- @param field_order table: optional prioritization table
+local function write_table_as_assignments(out, tbl, field_order)
+ for k, v, sub_order in util.sortedpairs(tbl, field_order) do
+ out:write(k.." = ")
+ write_value(out, v, 0, sub_order)
+ out:write("\n")
+ end
+end
+
+--- Save the contents of a table to a string.
+-- Each element of the table is saved as a global assignment.
+-- Only numbers, strings and tables (containing numbers, strings
+-- or other recursively processed tables) are supported.
+-- @param tbl table: the table containing the data to be written
+-- @param field_order table: an optional array indicating the order of top-level fields.
+-- @return string
+function persist.save_from_table_to_string(tbl, field_order)
+ local out = {buffer = {}}
+ function out:write(data) table.insert(self.buffer, data) end
+ write_table_as_assignments(out, tbl, field_order)
+ return table.concat(out.buffer)
+end
+
+--- Save the contents of a table in a file.
+-- Each element of the table is saved as a global assignment.
+-- Only numbers, strings and tables (containing numbers, strings
+-- or other recursively processed tables) are supported.
+-- @param filename string: the output filename
+-- @param tbl table: the table containing the data to be written
+-- @param field_order table: an optional array indicating the order of top-level fields.
+-- @return boolean or (nil, string): true if successful, or nil and a
+-- message in case of errors.
+function persist.save_from_table(filename, tbl, field_order)
+ local out = io.open(filename, "w")
+ if not out then
+ return nil, "Cannot create file at "..filename
+ end
+ write_table_as_assignments(out, tbl, field_order)
+ out:close()
+ return true
+end
+
+return persist
diff --git a/Utils/luarocks/lua/luarocks/purge.lua b/Utils/luarocks/lua/luarocks/purge.lua
new file mode 100644
index 000000000..17724e847
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/purge.lua
@@ -0,0 +1,79 @@
+
+--- Module implementing the LuaRocks "purge" command.
+-- Remove all rocks from a given tree.
+local purge = {}
+package.loaded["luarocks.purge"] = purge
+
+local util = require("luarocks.util")
+local fs = require("luarocks.fs")
+local path = require("luarocks.path")
+local search = require("luarocks.search")
+local deps = require("luarocks.deps")
+local repos = require("luarocks.repos")
+local manif = require("luarocks.manif")
+local cfg = require("luarocks.cfg")
+local remove = require("luarocks.remove")
+
+util.add_run_function(purge)
+purge.help_summary = "Remove all installed rocks from a tree."
+purge.help_arguments = "--tree= [--old-versions]"
+purge.help = [[
+This command removes rocks en masse from a given tree.
+By default, it removes all rocks from a tree.
+
+The --tree argument is mandatory: luarocks purge does not
+assume a default tree.
+
+--old-versions Keep the highest-numbered version of each
+ rock and remove the other ones. By default
+ it only removes old versions if they are
+ not needed as dependencies. This can be
+ overridden with the flag --force.
+]]
+
+function purge.command(flags)
+ local tree = flags["tree"]
+
+ if type(tree) ~= "string" then
+ return nil, "The --tree argument is mandatory. "..util.see_help("purge")
+ end
+
+ local results = {}
+ local query = search.make_query("")
+ query.exact_name = false
+ if not fs.is_dir(tree) then
+ return nil, "Directory not found: "..tree
+ end
+
+ local ok, err = fs.check_command_permissions(flags)
+ if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end
+
+ search.manifest_search(results, path.rocks_dir(tree), query)
+
+ local sort = function(a,b) return deps.compare_versions(b,a) end
+ if flags["old-versions"] then
+ sort = deps.compare_versions
+ end
+
+ for package, versions in util.sortedpairs(results) do
+ for version, repositories in util.sortedpairs(versions, sort) do
+ if flags["old-versions"] then
+ util.printout("Keeping "..package.." "..version.."...")
+ local ok, err = remove.remove_other_versions(package, version, flags["force"], flags["force-fast"])
+ if not ok then
+ util.printerr(err)
+ end
+ break
+ else
+ util.printout("Removing "..package.." "..version.."...")
+ local ok, err = repos.delete_version(package, version, "none", true)
+ if not ok then
+ util.printerr(err)
+ end
+ end
+ end
+ end
+ return manif.make_manifest(cfg.rocks_dir, "one")
+end
+
+return purge
diff --git a/Utils/luarocks/lua/luarocks/refresh_cache.lua b/Utils/luarocks/lua/luarocks/refresh_cache.lua
new file mode 100644
index 000000000..bbfd1f4d8
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/refresh_cache.lua
@@ -0,0 +1,33 @@
+
+--- Module implementing the luarocks-admin "refresh_cache" command.
+local refresh_cache = {}
+package.loaded["luarocks.refresh_cache"] = refresh_cache
+
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local cache = require("luarocks.cache")
+
+util.add_run_function(refresh_cache)
+refresh_cache.help_summary = "Refresh local cache of a remote rocks server."
+refresh_cache.help_arguments = "[--from=]"
+refresh_cache.help = [[
+The flag --from indicates which server to use.
+If not given, the default server set in the upload_server variable
+from the configuration file is used instead.
+]]
+
+function refresh_cache.command(flags)
+ local server, upload_server = cache.get_upload_server(flags["server"])
+ if not server then return nil, upload_server end
+ local download_url = cache.get_server_urls(server, upload_server)
+
+ local ok, err = cache.refresh_local_cache(server, download_url, cfg.upload_user, cfg.upload_password)
+ if not ok then
+ return nil, err
+ else
+ return true
+ end
+end
+
+
+return refresh_cache
diff --git a/Utils/luarocks/lua/luarocks/remove.lua b/Utils/luarocks/lua/luarocks/remove.lua
new file mode 100644
index 000000000..514c6dfa1
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/remove.lua
@@ -0,0 +1,174 @@
+
+--- Module implementing the LuaRocks "remove" command.
+-- Uninstalls rocks.
+local remove = {}
+package.loaded["luarocks.remove"] = remove
+
+local search = require("luarocks.search")
+local deps = require("luarocks.deps")
+local fetch = require("luarocks.fetch")
+local repos = require("luarocks.repos")
+local path = require("luarocks.path")
+local util = require("luarocks.util")
+local cfg = require("luarocks.cfg")
+local fs = require("luarocks.fs")
+local manif = require("luarocks.manif")
+
+util.add_run_function(remove)
+remove.help_summary = "Uninstall a rock."
+remove.help_arguments = "[--force|--force-fast] []"
+remove.help = [[
+Argument is the name of a rock to be uninstalled.
+If a version is not given, try to remove all versions at once.
+Will only perform the removal if it does not break dependencies.
+To override this check and force the removal, use --force.
+To perform a forced removal without reporting dependency issues,
+use --force-fast.
+
+]]..util.deps_mode_help()
+
+--- Obtain a list of packages that depend on the given set of packages
+-- (where all packages of the set are versions of one program).
+-- @param name string: the name of a program
+-- @param versions array of string: the versions to be deleted.
+-- @return array of string: an empty table if no packages depend on any
+-- of the given list, or an array of strings in "name/version" format.
+local function check_dependents(name, versions, deps_mode)
+ local dependents = {}
+ local blacklist = {}
+ blacklist[name] = {}
+ for version, _ in pairs(versions) do
+ blacklist[name][version] = true
+ end
+ local local_rocks = {}
+ local query_all = search.make_query("")
+ query_all.exact_name = false
+ search.manifest_search(local_rocks, cfg.rocks_dir, query_all)
+ local_rocks[name] = nil
+ for rock_name, rock_versions in pairs(local_rocks) do
+ for rock_version, _ in pairs(rock_versions) do
+ local rockspec, err = fetch.load_rockspec(path.rockspec_file(rock_name, rock_version))
+ if rockspec then
+ local _, missing = deps.match_deps(rockspec, blacklist, deps_mode)
+ if missing[name] then
+ table.insert(dependents, { name = rock_name, version = rock_version })
+ end
+ end
+ end
+ end
+ return dependents
+end
+
+--- Delete given versions of a program.
+-- @param name string: the name of a program
+-- @param versions array of string: the versions to be deleted.
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+-- @return boolean or (nil, string): true on success or nil and an error message.
+local function delete_versions(name, versions, deps_mode)
+
+ for version, _ in pairs(versions) do
+ util.printout("Removing "..name.." "..version.."...")
+ local ok, err = repos.delete_version(name, version, deps_mode)
+ if not ok then return nil, err end
+ end
+
+ return true
+end
+
+function remove.remove_search_results(results, name, deps_mode, force, fast)
+ local versions = results[name]
+
+ local version = next(versions)
+ local second = next(versions, version)
+
+ local dependents = {}
+ if not fast then
+ util.printout("Checking stability of dependencies in the absence of")
+ util.printout(name.." "..table.concat(util.keys(versions), ", ").."...")
+ util.printout()
+ dependents = check_dependents(name, versions, deps_mode)
+ end
+
+ if #dependents > 0 then
+ if force or fast then
+ util.printerr("The following packages may be broken by this forced removal:")
+ for _, dependent in ipairs(dependents) do
+ util.printerr(dependent.name.." "..dependent.version)
+ end
+ util.printerr()
+ else
+ if not second then
+ util.printerr("Will not remove "..name.." "..version..".")
+ util.printerr("Removing it would break dependencies for: ")
+ else
+ util.printerr("Will not remove installed versions of "..name..".")
+ util.printerr("Removing them would break dependencies for: ")
+ end
+ for _, dependent in ipairs(dependents) do
+ util.printerr(dependent.name.." "..dependent.version)
+ end
+ util.printerr()
+ util.printerr("Use --force to force removal (warning: this may break modules).")
+ return nil, "Failed removing."
+ end
+ end
+
+ local ok, err = delete_versions(name, versions, deps_mode)
+ if not ok then return nil, err end
+
+ util.printout("Removal successful.")
+ return true
+end
+
+function remove.remove_other_versions(name, version, force, fast)
+ local results = {}
+ search.manifest_search(results, cfg.rocks_dir, { name = name, exact_name = true, constraints = {{ op = "~=", version = version}} })
+ if results[name] then
+ return remove.remove_search_results(results, name, cfg.deps_mode, force, fast)
+ end
+ return true
+end
+
+--- Driver function for the "remove" command.
+-- @param name string: name of a rock. If a version is given, refer to
+-- a specific version; otherwise, try to remove all versions.
+-- @param version string: When passing a package name, a version number
+-- may also be given.
+-- @return boolean or (nil, string, exitcode): True if removal was
+-- successful, nil and an error message otherwise. exitcode is optionally returned.
+function remove.command(flags, name, version)
+ if type(name) ~= "string" then
+ return nil, "Argument missing. "..util.see_help("remove")
+ end
+
+ local deps_mode = flags["deps-mode"] or cfg.deps_mode
+
+ local ok, err = fs.check_command_permissions(flags)
+ if not ok then return nil, err, cfg.errorcodes.PERMISSIONDENIED end
+
+ local rock_type = name:match("%.(rock)$") or name:match("%.(rockspec)$")
+ local filename = name
+ if rock_type then
+ name, version = path.parse_name(filename)
+ if not name then return nil, "Invalid "..rock_type.." filename: "..filename end
+ end
+
+ local results = {}
+ name = name:lower()
+ search.manifest_search(results, cfg.rocks_dir, search.make_query(name, version))
+ if not results[name] then
+ return nil, "Could not find rock '"..name..(version and " "..version or "").."' in "..path.rocks_tree_to_string(cfg.root_dir)
+ end
+
+ local ok, err = remove.remove_search_results(results, name, deps_mode, flags["force"], flags["force-fast"])
+ if not ok then
+ return nil, err
+ end
+
+ manif.check_dependencies(nil, deps.get_deps_mode(flags))
+ return true
+end
+
+return remove
diff --git a/Utils/luarocks/lua/luarocks/repos.lua b/Utils/luarocks/lua/luarocks/repos.lua
new file mode 100644
index 000000000..5d5eac708
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/repos.lua
@@ -0,0 +1,422 @@
+
+--- Functions for managing the repository on disk.
+local repos = {}
+package.loaded["luarocks.repos"] = repos
+
+local fs = require("luarocks.fs")
+local path = require("luarocks.path")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local dir = require("luarocks.dir")
+local manif = require("luarocks.manif")
+local deps = require("luarocks.deps")
+
+-- Tree of files installed by a package are stored
+-- in its rock manifest. Some of these files have to
+-- be deployed to locations where Lua can load them as
+-- modules or where they can be used as commands.
+-- These files are characterised by pair
+-- (deploy_type, file_path), where deploy_type is the first
+-- component of the file path and file_path is the rest of the
+-- path. Only files with deploy_type in {"lua", "lib", "bin"}
+-- are deployed somewhere.
+-- Each deployed file provides an "item". An item is
+-- characterised by pair (item_type, item_name).
+-- item_type is "command" for files with deploy_type
+-- "bin" and "module" for deploy_type in {"lua", "lib"}.
+-- item_name is same as file_path for commands
+-- and is produced using path.path_to_module(file_path)
+-- for modules.
+
+--- Get all installed versions of a package.
+-- @param name string: a package name.
+-- @return table or nil: An array of strings listing installed
+-- versions of a package, or nil if none is available.
+local function get_installed_versions(name)
+ assert(type(name) == "string")
+
+ local dirs = fs.list_dir(path.versions_dir(name))
+ return (dirs and #dirs > 0) and dirs or nil
+end
+
+--- Check if a package exists in a local repository.
+-- Version numbers are compared as exact string comparison.
+-- @param name string: name of package
+-- @param version string: package version in string format
+-- @return boolean: true if a package is installed,
+-- false otherwise.
+function repos.is_installed(name, version)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ return fs.is_dir(path.install_dir(name, version))
+end
+
+local function recurse_rock_manifest_tree(file_tree, action)
+ assert(type(file_tree) == "table")
+ assert(type(action) == "function")
+ local function do_recurse_rock_manifest_tree(tree, parent_path, parent_module)
+
+ for file, sub in pairs(tree) do
+ if type(sub) == "table" then
+ local ok, err = do_recurse_rock_manifest_tree(sub, parent_path..file.."/", parent_module..file..".")
+ if not ok then return nil, err end
+ else
+ local ok, err = action(parent_path, parent_module, file)
+ if not ok then return nil, err end
+ end
+ end
+ return true
+ end
+ return do_recurse_rock_manifest_tree(file_tree, "", "")
+end
+
+local function store_package_data(result, name, file_tree)
+ if not file_tree then return end
+ return recurse_rock_manifest_tree(file_tree,
+ function(parent_path, parent_module, file)
+ local pathname = parent_path..file
+ result[path.path_to_module(pathname)] = pathname
+ return true
+ end
+ )
+end
+
+--- Obtain a list of modules within an installed package.
+-- @param package string: The package name; for example "luasocket"
+-- @param version string: The exact version number including revision;
+-- for example "2.0.1-1".
+-- @return table: A table of modules where keys are module identifiers
+-- in "foo.bar" format and values are pathnames in architecture-dependent
+-- "foo/bar.so" format. If no modules are found or if package or version
+-- are invalid, an empty table is returned.
+function repos.package_modules(package, version)
+ assert(type(package) == "string")
+ assert(type(version) == "string")
+
+ local result = {}
+ local rock_manifest = manif.load_rock_manifest(package, version)
+ store_package_data(result, package, rock_manifest.lib)
+ store_package_data(result, package, rock_manifest.lua)
+ return result
+end
+
+--- Obtain a list of command-line scripts within an installed package.
+-- @param package string: The package name; for example "luasocket"
+-- @param version string: The exact version number including revision;
+-- for example "2.0.1-1".
+-- @return table: A table of items where keys are command names
+-- as strings and values are pathnames in architecture-dependent
+-- ".../bin/foo" format. If no modules are found or if package or version
+-- are invalid, an empty table is returned.
+function repos.package_commands(package, version)
+ assert(type(package) == "string")
+ assert(type(version) == "string")
+
+ local result = {}
+ local rock_manifest = manif.load_rock_manifest(package, version)
+ store_package_data(result, package, rock_manifest.bin)
+ return result
+end
+
+
+--- Check if a rock contains binary executables.
+-- @param name string: name of an installed rock
+-- @param version string: version of an installed rock
+-- @return boolean: returns true if rock contains platform-specific
+-- binary executables, or false if it is a pure-Lua rock.
+function repos.has_binaries(name, version)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+
+ local rock_manifest = manif.load_rock_manifest(name, version)
+ if rock_manifest.bin then
+ for name, md5 in pairs(rock_manifest.bin) do
+ -- TODO verify that it is the same file. If it isn't, find the actual command.
+ if fs.is_actual_binary(dir.path(cfg.deploy_bin_dir, name)) then
+ return true
+ end
+ end
+ end
+ return false
+end
+
+function repos.run_hook(rockspec, hook_name)
+ assert(type(rockspec) == "table")
+ assert(type(hook_name) == "string")
+
+ local hooks = rockspec.hooks
+ if not hooks then
+ return true
+ end
+
+ if cfg.hooks_enabled == false then
+ return nil, "This rockspec contains hooks, which are blocked by the 'hooks_enabled' setting in your LuaRocks configuration."
+ end
+
+ if not hooks.substituted_variables then
+ util.variable_substitutions(hooks, rockspec.variables)
+ hooks.substituted_variables = true
+ end
+ local hook = hooks[hook_name]
+ if hook then
+ util.printout(hook)
+ if not fs.execute(hook) then
+ return nil, "Failed running "..hook_name.." hook."
+ end
+ end
+ return true
+end
+
+function repos.should_wrap_bin_scripts(rockspec)
+ assert(type(rockspec) == "table")
+
+ if cfg.wrap_bin_scripts ~= nil then
+ return cfg.wrap_bin_scripts
+ end
+ if rockspec.deploy and rockspec.deploy.wrap_bin_scripts == false then
+ return false
+ end
+ return true
+end
+
+local function find_suffixed(file, suffix)
+ local filenames = {file}
+ if suffix and suffix ~= "" then
+ table.insert(filenames, 1, file .. suffix)
+ end
+
+ for _, filename in ipairs(filenames) do
+ if fs.exists(filename) then
+ return filename
+ end
+ end
+
+ return nil, table.concat(filenames, ", ") .. " not found"
+end
+
+local function move_suffixed(from_file, to_file, suffix)
+ local suffixed_from_file, err = find_suffixed(from_file, suffix)
+ if not suffixed_from_file then
+ return nil, "Could not move " .. from_file .. " to " .. to_file .. ": " .. err
+ end
+
+ suffix = suffixed_from_file:sub(#from_file + 1)
+ local suffixed_to_file = to_file .. suffix
+ return fs.move(suffixed_from_file, suffixed_to_file)
+end
+
+local function delete_suffixed(file, suffix)
+ local suffixed_file, err = find_suffixed(file, suffix)
+ if not suffixed_file then
+ return nil, "Could not remove " .. file .. ": " .. err
+ end
+
+ fs.delete(suffixed_file)
+ if fs.exists(suffixed_file) then
+ return nil, "Failed deleting " .. suffixed_file .. ": file still exists"
+ end
+
+ return true
+end
+
+-- Files can be deployed using versioned and non-versioned names.
+-- Several items with same type and name can exist if they are
+-- provided by different packages or versions. In any case
+-- item from the newest version of lexicographically smallest package
+-- is deployed using non-versioned name and others use versioned names.
+
+local function get_deploy_paths(name, version, deploy_type, file_path)
+ local deploy_dir = cfg["deploy_" .. deploy_type .. "_dir"]
+ local non_versioned = dir.path(deploy_dir, file_path)
+ local versioned = path.versioned_name(non_versioned, deploy_dir, name, version)
+ return non_versioned, versioned
+end
+
+local function prepare_target(name, version, deploy_type, file_path, suffix)
+ local non_versioned, versioned = get_deploy_paths(name, version, deploy_type, file_path)
+ local item_type, item_name = manif.get_provided_item(deploy_type, file_path)
+ local cur_name, cur_version = manif.get_current_provider(item_type, item_name)
+
+ if not cur_name then
+ return non_versioned
+ elseif name < cur_name or (name == cur_name and deps.compare_versions(version, cur_version)) then
+ -- New version has priority. Move currently provided version back using versioned name.
+ local cur_deploy_type, cur_file_path = manif.get_providing_file(cur_name, cur_version, item_type, item_name)
+ local cur_non_versioned, cur_versioned = get_deploy_paths(cur_name, cur_version, cur_deploy_type, cur_file_path)
+
+ local dir_ok, dir_err = fs.make_dir(dir.dir_name(cur_versioned))
+ if not dir_ok then return nil, dir_err end
+
+ local move_ok, move_err = move_suffixed(cur_non_versioned, cur_versioned, suffix)
+ if not move_ok then return nil, move_err end
+
+ return non_versioned
+ else
+ -- Current version has priority, deploy new version using versioned name.
+ return versioned
+ end
+end
+
+--- Deploy a package from the rocks subdirectory.
+-- @param name string: name of package
+-- @param version string: exact package version in string format
+-- @param wrap_bin_scripts bool: whether commands written in Lua should be wrapped.
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+function repos.deploy_files(name, version, wrap_bin_scripts, deps_mode)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ assert(type(wrap_bin_scripts) == "boolean")
+
+ local rock_manifest = manif.load_rock_manifest(name, version)
+
+ local function deploy_file_tree(deploy_type, source_dir, move_fn, suffix)
+ if not rock_manifest[deploy_type] then
+ return true
+ end
+
+ return recurse_rock_manifest_tree(rock_manifest[deploy_type], function(parent_path, parent_module, file)
+ local file_path = parent_path .. file
+ local source = dir.path(source_dir, file_path)
+
+ local target, prepare_err = prepare_target(name, version, deploy_type, file_path, suffix)
+ if not target then return nil, prepare_err end
+
+ local dir_ok, dir_err = fs.make_dir(dir.dir_name(target))
+ if not dir_ok then return nil, dir_err end
+
+ local suffixed_target, mover = move_fn(source, target)
+ if fs.exists(suffixed_target) then
+ local backup = suffixed_target
+ repeat
+ backup = backup.."~"
+ until not fs.exists(backup) -- Slight race condition here, but shouldn't be a problem.
+
+ util.printerr("Warning: "..suffixed_target.." is not tracked by this installation of LuaRocks. Moving it to "..backup)
+ local move_ok, move_err = fs.move(suffixed_target, backup)
+ if not move_ok then return nil, move_err end
+ end
+
+ local move_ok, move_err = mover()
+ if not move_ok then return nil, move_err end
+
+ fs.remove_dir_tree_if_empty(dir.dir_name(source))
+ return true
+ end)
+ end
+
+ local function install_binary(source, target)
+ if wrap_bin_scripts and fs.is_lua(source) then
+ return target .. (cfg.wrapper_suffix or ""), function() return fs.wrap_script(source, target, name, version) end
+ else
+ return target, function() return fs.copy_binary(source, target) end
+ end
+ end
+
+ local function make_mover(perms)
+ return function(source, target)
+ return target, function() return fs.move(source, target, perms) end
+ end
+ end
+
+ local ok, err = deploy_file_tree("bin", path.bin_dir(name, version), install_binary, cfg.wrapper_suffix)
+ if not ok then return nil, err end
+
+ ok, err = deploy_file_tree("lua", path.lua_dir(name, version), make_mover(cfg.perm_read))
+ if not ok then return nil, err end
+
+ ok, err = deploy_file_tree("lib", path.lib_dir(name, version), make_mover(cfg.perm_exec))
+ if not ok then return nil, err end
+
+ return manif.add_to_manifest(name, version, nil, deps_mode)
+end
+
+--- Delete a package from the local repository.
+-- @param name string: name of package
+-- @param version string: exact package version in string format
+-- @param deps_mode: string: Which trees to check dependencies for:
+-- "one" for the current default tree, "all" for all trees,
+-- "order" for all trees with priority >= the current default, "none" for no trees.
+-- @param quick boolean: do not try to fix the versioned name
+-- of another version that provides the same module that
+-- was deleted. This is used during 'purge', as every module
+-- will be eventually deleted.
+function repos.delete_version(name, version, deps_mode, quick)
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ assert(type(deps_mode) == "string")
+
+ local rock_manifest = manif.load_rock_manifest(name, version)
+ if not rock_manifest then
+ return nil, "rock_manifest file not found for "..name.." "..version.." - not a LuaRocks 2 tree?"
+ end
+
+ local function delete_deployed_file_tree(deploy_type, suffix)
+ if not rock_manifest[deploy_type] then
+ return true
+ end
+
+ return recurse_rock_manifest_tree(rock_manifest[deploy_type], function(parent_path, parent_module, file)
+ local file_path = parent_path .. file
+ local non_versioned, versioned = get_deploy_paths(name, version, deploy_type, file_path)
+
+ -- Figure out if the file is deployed using versioned or non-versioned name.
+ local target
+ local item_type, item_name = manif.get_provided_item(deploy_type, file_path)
+ local cur_name, cur_version = manif.get_current_provider(item_type, item_name)
+
+ if cur_name == name and cur_version == version then
+ -- This package has highest priority, should be in non-versioned location.
+ target = non_versioned
+ else
+ target = versioned
+ end
+
+ local ok, err = delete_suffixed(target, suffix)
+ if not ok then return nil, err end
+
+ if not quick and target == non_versioned then
+ -- If another package provides this file, move its version
+ -- into non-versioned location instead.
+ local next_name, next_version = manif.get_next_provider(item_type, item_name)
+
+ if next_name then
+ local next_deploy_type, next_file_path = manif.get_providing_file(next_name, next_version, item_type, item_name)
+ local next_non_versioned, next_versioned = get_deploy_paths(next_name, next_version, next_deploy_type, next_file_path)
+
+ local move_ok, move_err = move_suffixed(next_versioned, next_non_versioned, suffix)
+ if not move_ok then return nil, move_err end
+
+ fs.remove_dir_tree_if_empty(dir.dir_name(next_versioned))
+ end
+ end
+
+ fs.remove_dir_tree_if_empty(dir.dir_name(target))
+ return true
+ end)
+ end
+
+ local ok, err = delete_deployed_file_tree("bin", cfg.wrapper_suffix)
+ if not ok then return nil, err end
+
+ ok, err = delete_deployed_file_tree("lua")
+ if not ok then return nil, err end
+
+ ok, err = delete_deployed_file_tree("lib")
+ if not ok then return nil, err end
+
+ fs.delete(path.install_dir(name, version))
+ if not get_installed_versions(name) then
+ fs.delete(dir.path(cfg.rocks_dir, name))
+ end
+
+ if quick then
+ return true
+ end
+
+ return manif.remove_from_manifest(name, version, nil, deps_mode)
+end
+
+return repos
diff --git a/Utils/luarocks/lua/luarocks/require.lua b/Utils/luarocks/lua/luarocks/require.lua
new file mode 100644
index 000000000..902bd1a3c
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/require.lua
@@ -0,0 +1,2 @@
+--- Retained for compatibility reasons only. Use luarocks.loader instead.
+return require("luarocks.loader")
diff --git a/Utils/luarocks/lua/luarocks/search.lua b/Utils/luarocks/lua/luarocks/search.lua
new file mode 100644
index 000000000..d22c2a185
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/search.lua
@@ -0,0 +1,483 @@
+
+--- Module implementing the LuaRocks "search" command.
+-- Queries LuaRocks servers.
+local search = {}
+package.loaded["luarocks.search"] = search
+
+local dir = require("luarocks.dir")
+local path = require("luarocks.path")
+local manif = require("luarocks.manif")
+local deps = require("luarocks.deps")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+
+util.add_run_function(search)
+search.help_summary = "Query the LuaRocks servers."
+search.help_arguments = "[--source] [--binary] { [] | --all }"
+search.help = [[
+--source Return only rockspecs and source rocks,
+ to be used with the "build" command.
+--binary Return only pure Lua and binary rocks (rocks that can be used
+ with the "install" command without requiring a C toolchain).
+--all List all contents of the server that are suitable to
+ this platform, do not filter by name.
+]]
+
+--- Convert the arch field of a query table to table format.
+-- @param query table: A query table.
+local function query_arch_as_table(query)
+ local format = type(query.arch)
+ if format == "table" then
+ return
+ elseif format == "nil" then
+ local accept = {}
+ accept["src"] = true
+ accept["all"] = true
+ accept["rockspec"] = true
+ accept["installed"] = true
+ accept[cfg.arch] = true
+ query.arch = accept
+ elseif format == "string" then
+ local accept = {}
+ for a in query.arch:gmatch("[%w_-]+") do
+ accept[a] = true
+ end
+ query.arch = accept
+ end
+end
+
+--- Store a search result (a rock or rockspec) in the results table.
+-- @param results table: The results table, where keys are package names and
+-- values are tables matching version strings to arrays of
+-- tables with fields "arch" and "repo".
+-- @param name string: Package name.
+-- @param version string: Package version.
+-- @param arch string: Architecture of rock ("all", "src" or platform
+-- identifier), "rockspec" or "installed"
+-- @param repo string: Pathname of a local repository of URL of
+-- rocks server.
+local function store_result(results, name, version, arch, repo)
+ assert(type(results) == "table")
+ assert(type(name) == "string")
+ assert(type(version) == "string")
+ assert(type(arch) == "string")
+ assert(type(repo) == "string")
+
+ if not results[name] then results[name] = {} end
+ if not results[name][version] then results[name][version] = {} end
+ table.insert(results[name][version], {
+ arch = arch,
+ repo = repo
+ })
+end
+
+--- Test the name field of a query.
+-- If query has a boolean field exact_name set to false,
+-- then substring match is performed; otherwise, exact string
+-- comparison is done.
+-- @param query table: A query in dependency table format.
+-- @param name string: A package name.
+-- @return boolean: True if names match, false otherwise.
+local function match_name(query, name)
+ assert(type(query) == "table")
+ assert(type(name) == "string")
+ if query.exact_name == false then
+ return name:find(query.name, 0, true) and true or false
+ else
+ return name == query.name
+ end
+end
+
+--- Store a match in a results table if version matches query.
+-- Name, version, arch and repository path are stored in a given
+-- table, optionally checking if version and arch (if given) match
+-- a query.
+-- @param results table: The results table, where keys are package names and
+-- values are tables matching version strings to arrays of
+-- tables with fields "arch" and "repo".
+-- @param repo string: URL or pathname of the repository.
+-- @param name string: The name of the package being tested.
+-- @param version string: The version of the package being tested.
+-- @param arch string: The arch of the package being tested.
+-- @param query table: A table describing the query in dependency
+-- format (for example, {name = "filesystem", exact_name = false,
+-- constraints = {op = "~>", version = {1,0}}}, arch = "rockspec").
+-- If the arch field is omitted, the local architecture (cfg.arch)
+-- is used. The special value "any" is also recognized, returning all
+-- matches regardless of architecture.
+local function store_if_match(results, repo, name, version, arch, query)
+ if match_name(query, name) then
+ if query.arch[arch] or query.arch["any"] then
+ if deps.match_constraints(deps.parse_version(version), query.constraints) then
+ store_result(results, name, version, arch, repo)
+ end
+ end
+ end
+end
+
+--- Perform search on a local repository.
+-- @param repo string: The pathname of the local repository.
+-- @param query table: A table describing the query in dependency
+-- format (for example, {name = "filesystem", exact_name = false,
+-- constraints = {op = "~>", version = {1,0}}}, arch = "rockspec").
+-- If the arch field is omitted, the local architecture (cfg.arch)
+-- is used. The special value "any" is also recognized, returning all
+-- matches regardless of architecture.
+-- @param results table or nil: If given, this table will store the
+-- results; if not given, a new table will be created.
+-- @return table: The results table, where keys are package names and
+-- values are tables matching version strings to arrays of
+-- tables with fields "arch" and "repo".
+-- If a table was given in the "results" parameter, that is the result value.
+function search.disk_search(repo, query, results)
+ assert(type(repo) == "string")
+ assert(type(query) == "table")
+ assert(type(results) == "table" or not results)
+
+ local fs = require("luarocks.fs")
+
+ if not results then
+ results = {}
+ end
+ query_arch_as_table(query)
+
+ for name in fs.dir(repo) do
+ local pathname = dir.path(repo, name)
+ local rname, rversion, rarch = path.parse_name(name)
+
+ if rname and (pathname:match(".rockspec$") or pathname:match(".rock$")) then
+ store_if_match(results, repo, rname, rversion, rarch, query)
+ elseif fs.is_dir(pathname) then
+ for version in fs.dir(pathname) do
+ if version:match("-%d+$") then
+ store_if_match(results, repo, name, version, "installed", query)
+ end
+ end
+ end
+ end
+ return results
+end
+
+--- Perform search on a rocks server or tree.
+-- @param results table: The results table, where keys are package names and
+-- values are tables matching version strings to arrays of
+-- tables with fields "arch" and "repo".
+-- @param repo string: The URL of a rocks server or
+-- the pathname of a rocks tree (as returned by path.rocks_dir()).
+-- @param query table: A table describing the query in dependency
+-- format (for example, {name = "filesystem", exact_name = false,
+-- constraints = {op = "~>", version = {1,0}}}, arch = "rockspec").
+-- If the arch field is omitted, the local architecture (cfg.arch)
+-- is used. The special value "any" is also recognized, returning all
+-- matches regardless of architecture.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @return true or, in case of errors, nil, an error message and an optional error code.
+function search.manifest_search(results, repo, query, lua_version)
+ assert(type(results) == "table")
+ assert(type(repo) == "string")
+ assert(type(query) == "table")
+
+ query_arch_as_table(query)
+ local manifest, err, errcode = manif.load_manifest(repo, lua_version)
+ if not manifest then
+ return nil, err, errcode
+ end
+ for name, versions in pairs(manifest.repository) do
+ for version, items in pairs(versions) do
+ for _, item in ipairs(items) do
+ store_if_match(results, repo, name, version, item.arch, query)
+ end
+ end
+ end
+ return true
+end
+
+--- Search on all configured rocks servers.
+-- @param query table: A dependency query.
+-- @param lua_version string: Lua version in "5.x" format, defaults to installed version.
+-- @return table: A table where keys are package names
+-- and values are tables matching version strings to arrays of
+-- tables with fields "arch" and "repo".
+function search.search_repos(query, lua_version)
+ assert(type(query) == "table")
+
+ local results = {}
+ for _, repo in ipairs(cfg.rocks_servers) do
+ if not cfg.disabled_servers[repo] then
+ if type(repo) == "string" then
+ repo = { repo }
+ end
+ for _, mirror in ipairs(repo) do
+ local protocol, pathname = dir.split_url(mirror)
+ if protocol == "file" then
+ mirror = pathname
+ end
+ local ok, err, errcode = search.manifest_search(results, mirror, query, lua_version)
+ if errcode == "network" then
+ cfg.disabled_servers[repo] = true
+ end
+ if ok then
+ break
+ else
+ util.warning("Failed searching manifest: "..err)
+ end
+ end
+ end
+ end
+ -- search through rocks in cfg.rocks_provided
+ local provided_repo = "provided by VM or rocks_provided"
+ for name, versions in pairs(cfg.rocks_provided) do
+ store_if_match(results, provided_repo, name, versions, "installed", query)
+ end
+ return results
+end
+
+--- Prepare a query in dependency table format.
+-- @param name string: The query name.
+-- @param version string or nil:
+-- @return table: A query in table format
+function search.make_query(name, version)
+ assert(type(name) == "string")
+ assert(type(version) == "string" or not version)
+
+ local query = {
+ name = name,
+ constraints = {}
+ }
+ if version then
+ table.insert(query.constraints, { op = "==", version = deps.parse_version(version)})
+ end
+ return query
+end
+
+--- Get the URL for the latest in a set of versions.
+-- @param name string: The package name to be used in the URL.
+-- @param versions table: An array of version informations, as stored
+-- in search results tables.
+-- @return string or nil: the URL for the latest version if one could
+-- be picked, or nil.
+local function pick_latest_version(name, versions)
+ assert(type(name) == "string")
+ assert(type(versions) == "table")
+
+ local vtables = {}
+ for v, _ in pairs(versions) do
+ table.insert(vtables, deps.parse_version(v))
+ end
+ table.sort(vtables)
+ local version = vtables[#vtables].string
+ local items = versions[version]
+ if items then
+ local pick = 1
+ for i, item in ipairs(items) do
+ if (item.arch == 'src' and items[pick].arch == 'rockspec')
+ or (item.arch ~= 'src' and item.arch ~= 'rockspec') then
+ pick = i
+ end
+ end
+ return path.make_url(items[pick].repo, name, version, items[pick].arch)
+ end
+ return nil
+end
+
+-- Find out which other Lua versions provide rock versions matching a query,
+-- @param query table: A dependency query matching a single rock.
+-- @return table: array of Lua versions supported, in "5.x" format.
+local function supported_lua_versions(query)
+ local results = {}
+
+ for lua_version in util.lua_versions() do
+ if lua_version ~= cfg.lua_version then
+ if search.search_repos(query, lua_version)[query.name] then
+ table.insert(results, lua_version)
+ end
+ end
+ end
+
+ return results
+end
+
+--- Attempt to get a single URL for a given search for a rock.
+-- @param query table: A dependency query matching a single rock.
+-- @return string or (nil, string): URL for latest matching version
+-- of the rock if it was found, or nil followed by an error message.
+function search.find_suitable_rock(query)
+ assert(type(query) == "table")
+
+ local results = search.search_repos(query)
+ local first_rock = next(results)
+ if not first_rock then
+ if cfg.rocks_provided[query.name] == nil then
+ -- Check if constraints are satisfiable with other Lua versions.
+ local lua_versions = supported_lua_versions(query)
+
+ if #lua_versions ~= 0 then
+ -- Build a nice message in "only Lua 5.x and 5.y but not 5.z." format
+ for i, lua_version in ipairs(lua_versions) do
+ lua_versions[i] = "Lua "..lua_version
+ end
+
+ local versions_message = "only "..table.concat(lua_versions, " and ")..
+ " but not Lua "..cfg.lua_version.."."
+
+ if #query.constraints == 0 then
+ return nil, query.name.." supports "..versions_message
+ elseif #query.constraints == 1 and query.constraints[1].op == "==" then
+ return nil, query.name.." "..query.constraints[1].version.string.." supports "..versions_message
+ else
+ return nil, "Matching "..query.name.." versions support "..versions_message
+ end
+ end
+ end
+
+ return nil, "No results matching query were found."
+ elseif next(results, first_rock) then
+ -- Shouldn't happen as query must match only one package.
+ return nil, "Several rocks matched query."
+ elseif cfg.rocks_provided[query.name] ~= nil then
+ -- Do not install versions listed in cfg.rocks_provided.
+ return nil, "Rock "..query.name.." "..cfg.rocks_provided[query.name]..
+ " was found but it is provided by VM or 'rocks_provided' in the config file."
+ else
+ return pick_latest_version(query.name, results[first_rock])
+ end
+end
+
+--- Print a list of rocks/rockspecs on standard output.
+-- @param results table: A table where keys are package names and versions
+-- are tables matching version strings to an array of rocks servers.
+-- @param porcelain boolean or nil: A flag to force machine-friendly output.
+function search.print_results(results, porcelain)
+ assert(type(results) == "table")
+ assert(type(porcelain) == "boolean" or not porcelain)
+
+ for package, versions in util.sortedpairs(results) do
+ if not porcelain then
+ util.printout(package)
+ end
+ for version, repos in util.sortedpairs(versions, deps.compare_versions) do
+ for _, repo in ipairs(repos) do
+ repo.repo = dir.normalize(repo.repo)
+ if porcelain then
+ util.printout(package, version, repo.arch, repo.repo)
+ else
+ util.printout(" "..version.." ("..repo.arch..") - "..repo.repo)
+ end
+ end
+ end
+ if not porcelain then
+ util.printout()
+ end
+ end
+end
+
+--- Splits a list of search results into two lists, one for "source" results
+-- to be used with the "build" command, and one for "binary" results to be
+-- used with the "install" command.
+-- @param results table: A search results table.
+-- @return (table, table): Two tables, one for source and one for binary
+-- results.
+local function split_source_and_binary_results(results)
+ local sources, binaries = {}, {}
+ for name, versions in pairs(results) do
+ for version, repositories in pairs(versions) do
+ for _, repo in ipairs(repositories) do
+ local where = sources
+ if repo.arch == "all" or repo.arch == cfg.arch then
+ where = binaries
+ end
+ store_result(where, name, version, repo.arch, repo.repo)
+ end
+ end
+ end
+ return sources, binaries
+end
+
+--- Given a name and optionally a version, try to find in the rocks
+-- servers a single .src.rock or .rockspec file that satisfies
+-- the request, and run the given function on it; or display to the
+-- user possibilities if it couldn't narrow down a single match.
+-- @param action function: A function that takes a .src.rock or
+-- .rockspec URL as a parameter.
+-- @param name string: A rock name
+-- @param version string or nil: A version number may also be given.
+-- @return The result of the action function, or nil and an error message.
+function search.act_on_src_or_rockspec(action, name, version, ...)
+ assert(type(action) == "function")
+ assert(type(name) == "string")
+ assert(type(version) == "string" or not version)
+
+ local query = search.make_query(name, version)
+ query.arch = "src|rockspec"
+ local url, err = search.find_suitable_rock(query)
+ if not url then
+ return nil, "Could not find a result named "..name..(version and " "..version or "")..": "..err
+ end
+ return action(url, ...)
+end
+
+function search.pick_installed_rock(name, version, given_tree)
+ local results = {}
+ local query = search.make_query(name, version)
+ query.exact_name = true
+ local tree_map = {}
+ local trees = cfg.rocks_trees
+ if given_tree then
+ trees = { given_tree }
+ end
+ for _, tree in ipairs(trees) do
+ local rocks_dir = path.rocks_dir(tree)
+ tree_map[rocks_dir] = tree
+ search.manifest_search(results, rocks_dir, query)
+ end
+
+ if not next(results) then --
+ return nil,"cannot find package "..name.." "..(version or "").."\nUse 'list' to find installed rocks."
+ end
+
+ version = nil
+ local repo_url
+ local package, versions = util.sortedpairs(results)()
+ --question: what do we do about multiple versions? This should
+ --give us the latest version on the last repo (which is usually the global one)
+ for vs, repositories in util.sortedpairs(versions, deps.compare_versions) do
+ if not version then version = vs end
+ for _, rp in ipairs(repositories) do repo_url = rp.repo end
+ end
+
+ local repo = tree_map[repo_url]
+ return name, version, repo, repo_url
+end
+
+--- Driver function for "search" command.
+-- @param name string: A substring of a rock name to search.
+-- @param version string or nil: a version may also be passed.
+-- @return boolean or (nil, string): True if build was successful; nil and an
+-- error message otherwise.
+function search.command(flags, name, version)
+ if flags["all"] then
+ name, version = "", nil
+ end
+
+ if type(name) ~= "string" and not flags["all"] then
+ return nil, "Enter name and version or use --all. "..util.see_help("search")
+ end
+
+ local query = search.make_query(name:lower(), version)
+ query.exact_name = false
+ local results, err = search.search_repos(query)
+ local porcelain = flags["porcelain"]
+ util.title("Search results:", porcelain, "=")
+ local sources, binaries = split_source_and_binary_results(results)
+ if next(sources) and not flags["binary"] then
+ util.title("Rockspecs and source rocks:", porcelain)
+ search.print_results(sources, porcelain)
+ end
+ if next(binaries) and not flags["source"] then
+ util.title("Binary and pure-Lua rocks:", porcelain)
+ search.print_results(binaries, porcelain)
+ end
+ return true
+end
+
+return search
diff --git a/Utils/luarocks/lua/luarocks/show.lua b/Utils/luarocks/lua/luarocks/show.lua
new file mode 100644
index 000000000..85c7edcbd
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/show.lua
@@ -0,0 +1,152 @@
+--- Module implementing the LuaRocks "show" command.
+-- Shows information about an installed rock.
+local show = {}
+package.loaded["luarocks.show"] = show
+
+local search = require("luarocks.search")
+local cfg = require("luarocks.cfg")
+local util = require("luarocks.util")
+local path = require("luarocks.path")
+local deps = require("luarocks.deps")
+local fetch = require("luarocks.fetch")
+local manif = require("luarocks.manif")
+
+util.add_run_function(show)
+show.help_summary = "Show information about an installed rock."
+
+show.help = [[
+ is an existing package name.
+Without any flags, show all module information.
+With these flags, return only the desired information:
+
+--home home page of project
+--modules all modules provided by this package as used by require()
+--deps packages this package depends on
+--rockspec the full path of the rockspec file
+--mversion the package version
+--rock-tree local tree where rock is installed
+--rock-dir data directory of the installed rock
+]]
+
+local function keys_as_string(t, sep)
+ local keys = util.keys(t)
+ table.sort(keys)
+ return table.concat(keys, sep or " ")
+end
+
+local function word_wrap(line)
+ local width = tonumber(os.getenv("COLUMNS")) or 80
+ if width > 80 then width = 80 end
+ if #line > width then
+ local brk = width
+ while brk > 0 and line:sub(brk, brk) ~= " " do
+ brk = brk - 1
+ end
+ if brk > 0 then
+ return line:sub(1, brk-1) .. "\n" .. word_wrap(line:sub(brk+1))
+ end
+ end
+ return line
+end
+
+local function format_text(text)
+ text = text:gsub("^%s*",""):gsub("%s$", ""):gsub("\n[ \t]+","\n"):gsub("([^\n])\n([^\n])","%1 %2")
+ local paragraphs = util.split_string(text, "\n\n")
+ for n, line in ipairs(paragraphs) do
+ paragraphs[n] = word_wrap(line)
+ end
+ return (table.concat(paragraphs, "\n\n"):gsub("%s$", ""))
+end
+
+local function installed_rock_label(name, tree)
+ local installed, version
+ if cfg.rocks_provided[name] then
+ installed, version = true, cfg.rocks_provided[name]
+ else
+ installed, version = search.pick_installed_rock(name, nil, tree)
+ end
+ return installed and "(using "..version..")" or "(missing)"
+end
+
+--- Driver function for "show" command.
+-- @param name or nil: an existing package name.
+-- @param version string or nil: a version may also be passed.
+-- @return boolean: True if succeeded, nil on errors.
+function show.command(flags, name, version)
+ if not name then
+ return nil, "Argument missing. "..util.see_help("show")
+ end
+
+ local repo, repo_url
+ name, version, repo, repo_url = search.pick_installed_rock(name:lower(), version, flags["tree"])
+ if not name then
+ return nil, version
+ end
+
+ local directory = path.install_dir(name,version,repo)
+ local rockspec_file = path.rockspec_file(name, version, repo)
+ local rockspec, err = fetch.load_local_rockspec(rockspec_file)
+ if not rockspec then return nil,err end
+
+ local descript = rockspec.description or {}
+ local manifest, err = manif.load_manifest(repo_url)
+ if not manifest then return nil,err end
+ local minfo = manifest.repository[name][version][1]
+
+ if flags["rock-tree"] then util.printout(path.rocks_tree_to_string(repo))
+ elseif flags["rock-dir"] then util.printout(directory)
+ elseif flags["home"] then util.printout(descript.homepage)
+ elseif flags["modules"] then util.printout(keys_as_string(minfo.modules, "\n"))
+ elseif flags["deps"] then util.printout(keys_as_string(minfo.dependencies))
+ elseif flags["rockspec"] then util.printout(rockspec_file)
+ elseif flags["mversion"] then util.printout(version)
+ else
+ util.printout()
+ util.printout(rockspec.package.." "..rockspec.version.." - "..(descript.summary or ""))
+ util.printout()
+ if descript.detailed then
+ util.printout(format_text(descript.detailed))
+ util.printout()
+ end
+ if descript.license then
+ util.printout("License: ", descript.license)
+ end
+ if descript.homepage then
+ util.printout("Homepage: ", descript.homepage)
+ end
+ util.printout("Installed in: ", path.rocks_tree_to_string(repo))
+ if next(minfo.modules) then
+ util.printout()
+ util.printout("Modules:")
+ for mod, filename in util.sortedpairs(minfo.modules) do
+ util.printout("\t"..mod.." ("..path.which(mod, filename, name, version, repo, manifest)..")")
+ end
+ end
+ local direct_deps = {}
+ if #rockspec.dependencies > 0 then
+ util.printout()
+ util.printout("Depends on:")
+ for _, dep in ipairs(rockspec.dependencies) do
+ direct_deps[dep.name] = true
+ util.printout("\t"..deps.show_dep(dep).." "..installed_rock_label(dep.name, flags["tree"]))
+ end
+ end
+ local has_indirect_deps
+ for dep_name in util.sortedpairs(minfo.dependencies) do
+ if not direct_deps[dep_name] then
+ if not has_indirect_deps then
+ util.printout()
+ util.printout("Indirectly pulling:")
+ has_indirect_deps = true
+ end
+
+ util.printout("\t"..dep_name.." "..installed_rock_label(dep_name, flags["tree"]))
+ end
+ end
+ util.printout()
+ end
+ return true
+end
+
+
+return show
diff --git a/Utils/luarocks/lua/luarocks/site_config_5_1.lua b/Utils/luarocks/lua/luarocks/site_config_5_1.lua
new file mode 100644
index 000000000..85ab3ddf4
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/site_config_5_1.lua
@@ -0,0 +1,14 @@
+local site_config = {}
+site_config.LUA_INCDIR=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\include]]
+site_config.LUA_LIBDIR=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks]]
+site_config.LUA_BINDIR=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks]]
+site_config.LUA_INTERPRETER=[[lua5.1]]
+site_config.LUAROCKS_UNAME_S=[[MINGW]]
+site_config.LUAROCKS_UNAME_M=[[x86]]
+site_config.LUAROCKS_ROCKS_TREE=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\systree]]
+site_config.LUAROCKS_PREFIX=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks]]
+site_config.LUAROCKS_DOWNLOADER=[[wget]]
+site_config.LUAROCKS_MD5CHECKER=[[md5sum]]
+site_config.LUAROCKS_FORCE_CONFIG=true
+site_config.LUAROCKS_SYSCONFDIR=[[C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks]]
+return site_config
diff --git a/Utils/luarocks/lua/luarocks/tools/patch.lua b/Utils/luarocks/lua/luarocks/tools/patch.lua
new file mode 100644
index 000000000..44d00ef89
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/tools/patch.lua
@@ -0,0 +1,700 @@
+--- Patch utility to apply unified diffs.
+--
+-- http://lua-users.org/wiki/LuaPatch
+--
+-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license).
+-- Code is heavilly based on the Python-based patch.py version 8.06-1
+-- Copyright (c) 2008 rainforce.org, MIT License
+-- Project home: http://code.google.com/p/python-patch/ .
+-- Version 0.1
+
+local patch = {}
+
+local fs = require("luarocks.fs")
+local util = require("luarocks.util")
+
+local io = io
+local os = os
+local string = string
+local table = table
+local format = string.format
+
+-- logging
+local debugmode = false
+local function debug(_) end
+local function info(_) end
+local function warning(s) io.stderr:write(s .. '\n') end
+
+-- Returns boolean whether string s2 starts with string s.
+local function startswith(s, s2)
+ return s:sub(1, #s2) == s2
+end
+
+-- Returns boolean whether string s2 ends with string s.
+local function endswith(s, s2)
+ return #s >= #s2 and s:sub(#s-#s2+1) == s2
+end
+
+-- Returns string s after filtering out any new-line characters from end.
+local function endlstrip(s)
+ return s:gsub('[\r\n]+$', '')
+end
+
+-- Returns shallow copy of table t.
+local function table_copy(t)
+ local t2 = {}
+ for k,v in pairs(t) do t2[k] = v end
+ return t2
+end
+
+local function exists(filename)
+ local fh = io.open(filename)
+ local result = fh ~= nil
+ if fh then fh:close() end
+ return result
+end
+local function isfile() return true end --FIX?
+
+local function read_file(filename)
+ local fh, data, err, oserr
+ fh, err, oserr = io.open(filename, 'rb')
+ if not fh then return fh, err, oserr end
+ data, err, oserr = fh:read'*a'
+ fh:close()
+ if not data then return nil, err, oserr end
+ return data
+end
+
+local function write_file(filename, data)
+ local fh, status, err, oserr
+ fh, err, oserr = io.open(filename 'wb')
+ if not fh then return fh, err, oserr end
+ status, err, oserr = fh:write(data)
+ fh:close()
+ if not status then return nil, err, oserr end
+ return true
+end
+
+local function file_copy(src, dest)
+ local data, status, err, oserr
+ data, err, oserr = read_file(src)
+ if not data then return data, err, oserr end
+ status, err, oserr = write_file(dest)
+ if not status then return status, err, oserr end
+ return true
+end
+
+local function string_as_file(s)
+ return {
+ at = 0,
+ str = s,
+ len = #s,
+ eof = false,
+ read = function(self, n)
+ if self.eof then return nil end
+ local chunk = self.str:sub(self.at, self.at + n - 1)
+ self.at = self.at + n
+ if self.at > self.len then
+ self.eof = true
+ end
+ return chunk
+ end,
+ close = function(self)
+ self.eof = true
+ end,
+ }
+end
+
+--
+-- file_lines(f) is similar to f:lines() for file f.
+-- The main difference is that read_lines includes
+-- new-line character sequences ("\n", "\r\n", "\r"),
+-- if any, at the end of each line. Embedded "\0" are also handled.
+-- Caution: The newline behavior can depend on whether f is opened
+-- in binary or ASCII mode.
+-- (file_lines - version 20080913)
+--
+local function file_lines(f)
+ local CHUNK_SIZE = 1024
+ local buffer = ""
+ local pos_beg = 1
+ return function()
+ local pos, chars
+ while 1 do
+ pos, chars = buffer:match('()([\r\n].)', pos_beg)
+ if pos or not f then
+ break
+ elseif f then
+ local chunk = f:read(CHUNK_SIZE)
+ if chunk then
+ buffer = buffer:sub(pos_beg) .. chunk
+ pos_beg = 1
+ else
+ f = nil
+ end
+ end
+ end
+ if not pos then
+ pos = #buffer
+ elseif chars == '\r\n' then
+ pos = pos + 1
+ end
+ local line = buffer:sub(pos_beg, pos)
+ pos_beg = pos + 1
+ if #line > 0 then
+ return line
+ end
+ end
+end
+
+local function match_linerange(line)
+ local m1, m2, m3, m4 = line:match("^@@ %-(%d+),(%d+) %+(%d+),(%d+)")
+ if not m1 then m1, m3, m4 = line:match("^@@ %-(%d+) %+(%d+),(%d+)") end
+ if not m1 then m1, m2, m3 = line:match("^@@ %-(%d+),(%d+) %+(%d+)") end
+ if not m1 then m1, m3 = line:match("^@@ %-(%d+) %+(%d+)") end
+ return m1, m2, m3, m4
+end
+
+function patch.read_patch(filename, data)
+ -- define possible file regions that will direct the parser flow
+ local state = 'header'
+ -- 'header' - comments before the patch body
+ -- 'filenames' - lines starting with --- and +++
+ -- 'hunkhead' - @@ -R +R @@ sequence
+ -- 'hunkbody'
+ -- 'hunkskip' - skipping invalid hunk mode
+
+ local all_ok = true
+ local lineends = {lf=0, crlf=0, cr=0}
+ local files = {source={}, target={}, hunks={}, fileends={}, hunkends={}}
+ local nextfileno = 0
+ local nexthunkno = 0 --: even if index starts with 0 user messages
+ -- number hunks from 1
+
+ -- hunkinfo holds parsed values, hunkactual - calculated
+ local hunkinfo = {
+ startsrc=nil, linessrc=nil, starttgt=nil, linestgt=nil,
+ invalid=false, text={}
+ }
+ local hunkactual = {linessrc=nil, linestgt=nil}
+
+ info(format("reading patch %s", filename))
+
+ local fp
+ if data then
+ fp = string_as_file(data)
+ else
+ fp = filename == '-' and io.stdin or assert(io.open(filename, "rb"))
+ end
+ local lineno = 0
+
+ for line in file_lines(fp) do
+ lineno = lineno + 1
+ if state == 'header' then
+ if startswith(line, "--- ") then
+ state = 'filenames'
+ end
+ -- state is 'header' or 'filenames'
+ end
+ if state == 'hunkbody' then
+ -- skip hunkskip and hunkbody code until definition of hunkhead read
+
+ if line:match"^[\r\n]*$" then
+ -- prepend space to empty lines to interpret them as context properly
+ line = " " .. line
+ end
+
+ -- process line first
+ if line:match"^[- +\\]" then
+ -- gather stats about line endings
+ local he = files.hunkends[nextfileno]
+ if endswith(line, "\r\n") then
+ he.crlf = he.crlf + 1
+ elseif endswith(line, "\n") then
+ he.lf = he.lf + 1
+ elseif endswith(line, "\r") then
+ he.cr = he.cr + 1
+ end
+ if startswith(line, "-") then
+ hunkactual.linessrc = hunkactual.linessrc + 1
+ elseif startswith(line, "+") then
+ hunkactual.linestgt = hunkactual.linestgt + 1
+ elseif startswith(line, "\\") then
+ -- nothing
+ else
+ hunkactual.linessrc = hunkactual.linessrc + 1
+ hunkactual.linestgt = hunkactual.linestgt + 1
+ end
+ table.insert(hunkinfo.text, line)
+ -- todo: handle \ No newline cases
+ else
+ warning(format("invalid hunk no.%d at %d for target file %s",
+ nexthunkno, lineno, files.target[nextfileno]))
+ -- add hunk status node
+ table.insert(files.hunks[nextfileno], table_copy(hunkinfo))
+ files.hunks[nextfileno][nexthunkno].invalid = true
+ all_ok = false
+ state = 'hunkskip'
+ end
+
+ -- check exit conditions
+ if hunkactual.linessrc > hunkinfo.linessrc or
+ hunkactual.linestgt > hunkinfo.linestgt
+ then
+ warning(format("extra hunk no.%d lines at %d for target %s",
+ nexthunkno, lineno, files.target[nextfileno]))
+ -- add hunk status node
+ table.insert(files.hunks[nextfileno], table_copy(hunkinfo))
+ files.hunks[nextfileno][nexthunkno].invalid = true
+ state = 'hunkskip'
+ elseif hunkinfo.linessrc == hunkactual.linessrc and
+ hunkinfo.linestgt == hunkactual.linestgt
+ then
+ table.insert(files.hunks[nextfileno], table_copy(hunkinfo))
+ state = 'hunkskip'
+
+ -- detect mixed window/unix line ends
+ local ends = files.hunkends[nextfileno]
+ if (ends.cr~=0 and 1 or 0) + (ends.crlf~=0 and 1 or 0) +
+ (ends.lf~=0 and 1 or 0) > 1
+ then
+ warning(format("inconsistent line ends in patch hunks for %s",
+ files.source[nextfileno]))
+ end
+ if debugmode then
+ local debuglines = {crlf=ends.crlf, lf=ends.lf, cr=ends.cr,
+ file=files.target[nextfileno], hunk=nexthunkno}
+ debug(format("crlf: %(crlf)d lf: %(lf)d cr: %(cr)d\t " ..
+ "- file: %(file)s hunk: %(hunk)d", debuglines))
+ end
+ end
+ -- state is 'hunkbody' or 'hunkskip'
+ end
+
+ if state == 'hunkskip' then
+ if match_linerange(line) then
+ state = 'hunkhead'
+ elseif startswith(line, "--- ") then
+ state = 'filenames'
+ if debugmode and #files.source > 0 then
+ debug(format("- %2d hunks for %s", #files.hunks[nextfileno],
+ files.source[nextfileno]))
+ end
+ end
+ -- state is 'hunkskip', 'hunkhead', or 'filenames'
+ end
+ local advance
+ if state == 'filenames' then
+ if startswith(line, "--- ") then
+ if util.array_contains(files.source, nextfileno) then
+ all_ok = false
+ warning(format("skipping invalid patch for %s",
+ files.source[nextfileno+1]))
+ table.remove(files.source, nextfileno+1)
+ -- double source filename line is encountered
+ -- attempt to restart from this second line
+ end
+ -- Accept a space as a terminator, like GNU patch does.
+ -- Breaks patches containing filenames with spaces...
+ -- FIXME Figure out what does GNU patch do in those cases.
+ local match = line:match("^%-%-%- ([^ \t\r\n]+)")
+ if not match then
+ all_ok = false
+ warning(format("skipping invalid filename at line %d", lineno+1))
+ state = 'header'
+ else
+ table.insert(files.source, match)
+ end
+ elseif not startswith(line, "+++ ") then
+ if util.array_contains(files.source, nextfileno) then
+ all_ok = false
+ warning(format("skipping invalid patch with no target for %s",
+ files.source[nextfileno+1]))
+ table.remove(files.source, nextfileno+1)
+ else
+ -- this should be unreachable
+ warning("skipping invalid target patch")
+ end
+ state = 'header'
+ else
+ if util.array_contains(files.target, nextfileno) then
+ all_ok = false
+ warning(format("skipping invalid patch - double target at line %d",
+ lineno+1))
+ table.remove(files.source, nextfileno+1)
+ table.remove(files.target, nextfileno+1)
+ nextfileno = nextfileno - 1
+ -- double target filename line is encountered
+ -- switch back to header state
+ state = 'header'
+ else
+ -- Accept a space as a terminator, like GNU patch does.
+ -- Breaks patches containing filenames with spaces...
+ -- FIXME Figure out what does GNU patch do in those cases.
+ local re_filename = "^%+%+%+ ([^ \t\r\n]+)"
+ local match = line:match(re_filename)
+ if not match then
+ all_ok = false
+ warning(format(
+ "skipping invalid patch - no target filename at line %d",
+ lineno+1))
+ state = 'header'
+ else
+ table.insert(files.target, match)
+ nextfileno = nextfileno + 1
+ nexthunkno = 0
+ table.insert(files.hunks, {})
+ table.insert(files.hunkends, table_copy(lineends))
+ table.insert(files.fileends, table_copy(lineends))
+ state = 'hunkhead'
+ advance = true
+ end
+ end
+ end
+ -- state is 'filenames', 'header', or ('hunkhead' with advance)
+ end
+ if not advance and state == 'hunkhead' then
+ local m1, m2, m3, m4 = match_linerange(line)
+ if not m1 then
+ if not util.array_contains(files.hunks, nextfileno-1) then
+ all_ok = false
+ warning(format("skipping invalid patch with no hunks for file %s",
+ files.target[nextfileno]))
+ end
+ state = 'header'
+ else
+ hunkinfo.startsrc = tonumber(m1)
+ hunkinfo.linessrc = tonumber(m2 or 1)
+ hunkinfo.starttgt = tonumber(m3)
+ hunkinfo.linestgt = tonumber(m4 or 1)
+ hunkinfo.invalid = false
+ hunkinfo.text = {}
+
+ hunkactual.linessrc = 0
+ hunkactual.linestgt = 0
+
+ state = 'hunkbody'
+ nexthunkno = nexthunkno + 1
+ end
+ -- state is 'header' or 'hunkbody'
+ end
+ end
+ if state ~= 'hunkskip' then
+ warning(format("patch file incomplete - %s", filename))
+ all_ok = false
+ -- os.exit(?)
+ else
+ -- duplicated message when an eof is reached
+ if debugmode and #files.source > 0 then
+ debug(format("- %2d hunks for %s", #files.hunks[nextfileno],
+ files.source[nextfileno]))
+ end
+ end
+
+ local sum = 0; for _,hset in ipairs(files.hunks) do sum = sum + #hset end
+ info(format("total files: %d total hunks: %d", #files.source, sum))
+ fp:close()
+ return files, all_ok
+end
+
+local function find_hunk(file, h, hno)
+ for fuzz=0,2 do
+ local lineno = h.startsrc
+ for i=0,#file do
+ local found = true
+ local location = lineno
+ for l, hline in ipairs(h.text) do
+ if l > fuzz then
+ -- todo: \ No newline at the end of file
+ if startswith(hline, " ") or startswith(hline, "-") then
+ local line = file[lineno]
+ lineno = lineno + 1
+ if not line or #line == 0 then
+ found = false
+ break
+ end
+ if endlstrip(line) ~= endlstrip(hline:sub(2)) then
+ found = false
+ break
+ end
+ end
+ end
+ end
+ if found then
+ local offset = location - h.startsrc - fuzz
+ if offset ~= 0 then
+ warning(format("Hunk %d found at offset %d%s...", hno, offset, fuzz == 0 and "" or format(" (fuzz %d)", fuzz)))
+ end
+ h.startsrc = location
+ h.starttgt = h.starttgt + offset
+ for _=1,fuzz do
+ table.remove(h.text, 1)
+ table.remove(h.text, #h.text)
+ end
+ return true
+ end
+ lineno = i
+ end
+ end
+ return false
+end
+
+local function load_file(filename)
+ local fp = assert(io.open(filename))
+ local file = {}
+ local readline = file_lines(fp)
+ while true do
+ local line = readline()
+ if not line then break end
+ table.insert(file, line)
+ end
+ fp:close()
+ return file
+end
+
+local function find_hunks(file, hunks)
+ for hno, h in ipairs(hunks) do
+ find_hunk(file, h, hno)
+ end
+end
+
+local function check_patched(file, hunks)
+ local lineno = 1
+ local ok, err = pcall(function()
+ if #file == 0 then
+ error('nomatch', 0)
+ end
+ for hno, h in ipairs(hunks) do
+ -- skip to line just before hunk starts
+ if #file < h.starttgt then
+ error('nomatch', 0)
+ end
+ lineno = h.starttgt
+ for _, hline in ipairs(h.text) do
+ -- todo: \ No newline at the end of file
+ if not startswith(hline, "-") and not startswith(hline, "\\") then
+ local line = file[lineno]
+ lineno = lineno + 1
+ if #line == 0 then
+ error('nomatch', 0)
+ end
+ if endlstrip(line) ~= endlstrip(hline:sub(2)) then
+ warning(format("file is not patched - failed hunk: %d", hno))
+ error('nomatch', 0)
+ end
+ end
+ end
+ end
+ end)
+ -- todo: display failed hunk, i.e. expected/found
+ return err ~= 'nomatch'
+end
+
+local function patch_hunks(srcname, tgtname, hunks)
+ local src = assert(io.open(srcname, "rb"))
+ local tgt = assert(io.open(tgtname, "wb"))
+
+ local src_readline = file_lines(src)
+
+ -- todo: detect linefeeds early - in apply_files routine
+ -- to handle cases when patch starts right from the first
+ -- line and no lines are processed. At the moment substituted
+ -- lineends may not be the same at the start and at the end
+ -- of patching. Also issue a warning about mixed lineends
+
+ local srclineno = 1
+ local lineends = {['\n']=0, ['\r\n']=0, ['\r']=0}
+ for hno, h in ipairs(hunks) do
+ debug(format("processing hunk %d for file %s", hno, tgtname))
+ -- skip to line just before hunk starts
+ while srclineno < h.startsrc do
+ local line = src_readline()
+ -- Python 'U' mode works only with text files
+ if endswith(line, "\r\n") then
+ lineends["\r\n"] = lineends["\r\n"] + 1
+ elseif endswith(line, "\n") then
+ lineends["\n"] = lineends["\n"] + 1
+ elseif endswith(line, "\r") then
+ lineends["\r"] = lineends["\r"] + 1
+ end
+ tgt:write(line)
+ srclineno = srclineno + 1
+ end
+
+ for _,hline in ipairs(h.text) do
+ -- todo: check \ No newline at the end of file
+ if startswith(hline, "-") or startswith(hline, "\\") then
+ src_readline()
+ srclineno = srclineno + 1
+ else
+ if not startswith(hline, "+") then
+ src_readline()
+ srclineno = srclineno + 1
+ end
+ local line2write = hline:sub(2)
+ -- detect if line ends are consistent in source file
+ local sum = 0
+ for _,v in pairs(lineends) do if v > 0 then sum=sum+1 end end
+ if sum == 1 then
+ local newline
+ for k,v in pairs(lineends) do if v ~= 0 then newline = k end end
+ tgt:write(endlstrip(line2write) .. newline)
+ else -- newlines are mixed or unknown
+ tgt:write(line2write)
+ end
+ end
+ end
+ end
+ for line in src_readline do
+ tgt:write(line)
+ end
+ tgt:close()
+ src:close()
+ return true
+end
+
+local function strip_dirs(filename, strip)
+ if strip == nil then return filename end
+ for _=1,strip do
+ filename=filename:gsub("^[^/]*/", "")
+ end
+ return filename
+end
+
+function patch.apply_patch(the_patch, strip)
+ local all_ok = true
+ local total = #the_patch.source
+ for fileno, filename in ipairs(the_patch.source) do
+ filename = strip_dirs(filename, strip)
+ local continue
+ local f2patch = filename
+ if not exists(f2patch) then
+ f2patch = strip_dirs(the_patch.target[fileno], strip)
+ f2patch = fs.absolute_name(f2patch)
+ if not exists(f2patch) then --FIX:if f2patch nil
+ warning(format("source/target file does not exist\n--- %s\n+++ %s",
+ filename, f2patch))
+ all_ok = false
+ continue = true
+ end
+ end
+ if not continue and not isfile(f2patch) then
+ warning(format("not a file - %s", f2patch))
+ all_ok = false
+ continue = true
+ end
+ if not continue then
+
+ filename = f2patch
+
+ info(format("processing %d/%d:\t %s", fileno, total, filename))
+
+ -- validate before patching
+ local hunks = the_patch.hunks[fileno]
+ local file = load_file(filename)
+ local hunkno = 1
+ local hunk = hunks[hunkno]
+ local hunkfind = {}
+ local validhunks = 0
+ local canpatch = false
+ local hunklineno
+ local isbreak
+ local lineno = 0
+
+ find_hunks(file, hunks)
+
+ for _, line in ipairs(file) do
+ lineno = lineno + 1
+ local continue
+ if not hunk or lineno < hunk.startsrc then
+ continue = true
+ elseif lineno == hunk.startsrc then
+ hunkfind = {}
+ for _,x in ipairs(hunk.text) do
+ if x:sub(1,1) == ' ' or x:sub(1,1) == '-' then
+ hunkfind[#hunkfind+1] = endlstrip(x:sub(2))
+ end
+ end
+ hunklineno = 1
+
+ -- todo \ No newline at end of file
+ end
+ -- check hunks in source file
+ if not continue and lineno < hunk.startsrc + #hunkfind - 1 then
+ if endlstrip(line) == hunkfind[hunklineno] then
+ hunklineno = hunklineno + 1
+ else
+ debug(format("hunk no.%d doesn't match source file %s",
+ hunkno, filename))
+ -- file may be already patched, but check other hunks anyway
+ hunkno = hunkno + 1
+ if hunkno <= #hunks then
+ hunk = hunks[hunkno]
+ continue = true
+ else
+ isbreak = true; break
+ end
+ end
+ end
+ -- check if processed line is the last line
+ if not continue and lineno == hunk.startsrc + #hunkfind - 1 then
+ debug(format("file %s hunk no.%d -- is ready to be patched",
+ filename, hunkno))
+ hunkno = hunkno + 1
+ validhunks = validhunks + 1
+ if hunkno <= #hunks then
+ hunk = hunks[hunkno]
+ else
+ if validhunks == #hunks then
+ -- patch file
+ canpatch = true
+ isbreak = true; break
+ end
+ end
+ end
+ end
+ if not isbreak then
+ if hunkno <= #hunks then
+ warning(format("premature end of source file %s at hunk %d",
+ filename, hunkno))
+ all_ok = false
+ end
+ end
+ if validhunks < #hunks then
+ if check_patched(file, hunks) then
+ warning(format("already patched %s", filename))
+ else
+ warning(format("source file is different - %s", filename))
+ all_ok = false
+ end
+ end
+ if canpatch then
+ local backupname = filename .. ".orig"
+ if exists(backupname) then
+ warning(format("can't backup original file to %s - aborting",
+ backupname))
+ all_ok = false
+ else
+ assert(os.rename(filename, backupname))
+ if patch_hunks(backupname, filename, hunks) then
+ warning(format("successfully patched %s", filename))
+ assert(os.remove(backupname))
+ else
+ warning(format("error patching file %s", filename))
+ assert(file_copy(filename, filename .. ".invalid"))
+ warning(format("invalid version is saved to %s",
+ filename .. ".invalid"))
+ -- todo: proper rejects
+ assert(os.rename(backupname, filename))
+ all_ok = false
+ end
+ end
+ end
+
+ end -- if not continue
+ end -- for
+ -- todo: check for premature eof
+ return all_ok
+end
+
+return patch
diff --git a/Utils/luarocks/lua/luarocks/tools/tar.lua b/Utils/luarocks/lua/luarocks/tools/tar.lua
new file mode 100644
index 000000000..637a6c953
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/tools/tar.lua
@@ -0,0 +1,150 @@
+
+--- A pure-Lua implementation of untar (unpacking .tar archives)
+local tar = {}
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local util = require("luarocks.util")
+
+local blocksize = 512
+
+local function get_typeflag(flag)
+ if flag == "0" or flag == "\0" then return "file"
+ elseif flag == "1" then return "link"
+ elseif flag == "2" then return "symlink" -- "reserved" in POSIX, "symlink" in GNU
+ elseif flag == "3" then return "character"
+ elseif flag == "4" then return "block"
+ elseif flag == "5" then return "directory"
+ elseif flag == "6" then return "fifo"
+ elseif flag == "7" then return "contiguous" -- "reserved" in POSIX, "contiguous" in GNU
+ elseif flag == "x" then return "next file"
+ elseif flag == "g" then return "global extended header"
+ elseif flag == "L" then return "long name"
+ elseif flag == "K" then return "long link name"
+ end
+ return "unknown"
+end
+
+local function octal_to_number(octal)
+ local exp = 0
+ local number = 0
+ for i = #octal,1,-1 do
+ local digit = tonumber(octal:sub(i,i))
+ if digit then
+ number = number + (digit * 8^exp)
+ exp = exp + 1
+ end
+ end
+ return number
+end
+
+local function checksum_header(block)
+ local sum = 256
+ for i = 1,148 do
+ sum = sum + block:byte(i)
+ end
+ for i = 157,500 do
+ sum = sum + block:byte(i)
+ end
+ return sum
+end
+
+local function nullterm(s)
+ return s:match("^[^%z]*")
+end
+
+local function read_header_block(block)
+ local header = {}
+ header.name = nullterm(block:sub(1,100))
+ header.mode = nullterm(block:sub(101,108))
+ header.uid = octal_to_number(nullterm(block:sub(109,116)))
+ header.gid = octal_to_number(nullterm(block:sub(117,124)))
+ header.size = octal_to_number(nullterm(block:sub(125,136)))
+ header.mtime = octal_to_number(nullterm(block:sub(137,148)))
+ header.chksum = octal_to_number(nullterm(block:sub(149,156)))
+ header.typeflag = get_typeflag(block:sub(157,157))
+ header.linkname = nullterm(block:sub(158,257))
+ header.magic = block:sub(258,263)
+ header.version = block:sub(264,265)
+ header.uname = nullterm(block:sub(266,297))
+ header.gname = nullterm(block:sub(298,329))
+ header.devmajor = octal_to_number(nullterm(block:sub(330,337)))
+ header.devminor = octal_to_number(nullterm(block:sub(338,345)))
+ header.prefix = block:sub(346,500)
+ if header.magic ~= "ustar " and header.magic ~= "ustar\0" then
+ return false, "Invalid header magic "..header.magic
+ end
+ if header.version ~= "00" and header.version ~= " \0" then
+ return false, "Unknown version "..header.version
+ end
+ if not checksum_header(block) == header.chksum then
+ return false, "Failed header checksum"
+ end
+ return header
+end
+
+function tar.untar(filename, destdir)
+ assert(type(filename) == "string")
+ assert(type(destdir) == "string")
+
+ local tar_handle = io.open(filename, "r")
+ if not tar_handle then return nil, "Error opening file "..filename end
+
+ local long_name, long_link_name
+ while true do
+ local block
+ repeat
+ block = tar_handle:read(blocksize)
+ until (not block) or checksum_header(block) > 256
+ if not block then break end
+ local header, err = read_header_block(block)
+ if not header then
+ util.printerr(err)
+ end
+
+ local file_data = tar_handle:read(math.ceil(header.size / blocksize) * blocksize):sub(1,header.size)
+
+ if header.typeflag == "long name" then
+ long_name = nullterm(file_data)
+ elseif header.typeflag == "long link name" then
+ long_link_name = nullterm(file_data)
+ else
+ if long_name then
+ header.name = long_name
+ long_name = nil
+ end
+ if long_link_name then
+ header.name = long_link_name
+ long_link_name = nil
+ end
+ end
+ local pathname = dir.path(destdir, header.name)
+ if header.typeflag == "directory" then
+ local ok, err = fs.make_dir(pathname)
+ if not ok then return nil, err end
+ elseif header.typeflag == "file" then
+ local dirname = dir.dir_name(pathname)
+ if dirname ~= "" then
+ local ok, err = fs.make_dir(dirname)
+ if not ok then return nil, err end
+ end
+ local file_handle = io.open(pathname, "wb")
+ file_handle:write(file_data)
+ file_handle:close()
+ fs.set_time(pathname, header.mtime)
+ if fs.chmod then
+ fs.chmod(pathname, header.mode)
+ end
+ end
+ --[[
+ for k,v in pairs(header) do
+ util.printout("[\""..tostring(k).."\"] = "..(type(v)=="number" and v or "\""..v:gsub("%z", "\\0").."\""))
+ end
+ util.printout()
+ --]]
+ end
+ tar_handle:close()
+ return true
+end
+
+return tar
diff --git a/Utils/luarocks/lua/luarocks/tools/zip.lua b/Utils/luarocks/lua/luarocks/tools/zip.lua
new file mode 100644
index 000000000..e6d9e36ae
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/tools/zip.lua
@@ -0,0 +1,264 @@
+
+--- A Lua implementation of .zip file archiving (used for creating .rock files),
+-- using only lzlib or lua-lzib.
+local zip = {}
+
+local zlib = require("zlib")
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+
+-- zlib module can be provided by both lzlib and lua-lzib packages.
+-- Create a compatibility layer.
+local zlib_compress, zlib_crc32
+if zlib._VERSION:match "^lua%-zlib" then
+ function zlib_compress(data)
+ return (zlib.deflate()(data, "finish"))
+ end
+
+ function zlib_crc32(data)
+ return zlib.crc32()(data)
+ end
+elseif zlib._VERSION:match "^lzlib" then
+ function zlib_compress(data)
+ return zlib.compress(data)
+ end
+
+ function zlib_crc32(data)
+ return zlib.crc32(zlib.crc32(), data)
+ end
+else
+ error("unknown zlib library", 0)
+end
+
+local function number_to_bytestring(number, nbytes)
+ local out = {}
+ for _ = 1, nbytes do
+ local byte = number % 256
+ table.insert(out, string.char(byte))
+ number = (number - byte) / 256
+ end
+ return table.concat(out)
+end
+
+--- Begin a new file to be stored inside the zipfile.
+-- @param self handle of the zipfile being written.
+-- @param filename filenome of the file to be added to the zipfile.
+-- @return true if succeeded, nil in case of failure.
+local function zipwriter_open_new_file_in_zip(self, filename)
+ if self.in_open_file then
+ self:close_file_in_zip()
+ return nil
+ end
+ local lfh = {}
+ self.local_file_header = lfh
+ lfh.last_mod_file_time = 0 -- TODO
+ lfh.last_mod_file_date = 0 -- TODO
+ lfh.file_name_length = #filename
+ lfh.extra_field_length = 0
+ lfh.file_name = filename:gsub("\\", "/")
+ lfh.external_attr = 0 -- TODO properly store permissions
+ self.in_open_file = true
+ return true
+end
+
+--- Write data to the file currently being stored in the zipfile.
+-- @param self handle of the zipfile being written.
+-- @param data string containing full contents of the file.
+-- @return true if succeeded, nil in case of failure.
+local function zipwriter_write_file_in_zip(self, data)
+ if not self.in_open_file then
+ return nil
+ end
+ local lfh = self.local_file_header
+ local compressed = zlib_compress(data):sub(3, -5)
+ lfh.crc32 = zlib_crc32(data)
+ lfh.compressed_size = #compressed
+ lfh.uncompressed_size = #data
+ self.data = compressed
+ return true
+end
+
+--- Complete the writing of a file stored in the zipfile.
+-- @param self handle of the zipfile being written.
+-- @return true if succeeded, nil in case of failure.
+local function zipwriter_close_file_in_zip(self)
+ local zh = self.ziphandle
+
+ if not self.in_open_file then
+ return nil
+ end
+
+ -- Local file header
+ local lfh = self.local_file_header
+ lfh.offset = zh:seek()
+ zh:write(number_to_bytestring(0x04034b50, 4)) -- signature
+ zh:write(number_to_bytestring(20, 2)) -- version needed to extract: 2.0
+ zh:write(number_to_bytestring(0, 2)) -- general purpose bit flag
+ zh:write(number_to_bytestring(8, 2)) -- compression method: deflate
+ zh:write(number_to_bytestring(lfh.last_mod_file_time, 2))
+ zh:write(number_to_bytestring(lfh.last_mod_file_date, 2))
+ zh:write(number_to_bytestring(lfh.crc32, 4))
+ zh:write(number_to_bytestring(lfh.compressed_size, 4))
+ zh:write(number_to_bytestring(lfh.uncompressed_size, 4))
+ zh:write(number_to_bytestring(lfh.file_name_length, 2))
+ zh:write(number_to_bytestring(lfh.extra_field_length, 2))
+ zh:write(lfh.file_name)
+
+ -- File data
+ zh:write(self.data)
+
+ -- Data descriptor
+ zh:write(number_to_bytestring(lfh.crc32, 4))
+ zh:write(number_to_bytestring(lfh.compressed_size, 4))
+ zh:write(number_to_bytestring(lfh.uncompressed_size, 4))
+
+ table.insert(self.files, lfh)
+ self.in_open_file = false
+
+ return true
+end
+
+-- @return boolean or (boolean, string): true on success,
+-- false and an error message on failure.
+local function zipwriter_add(self, file)
+ local fin
+ local ok, err = self:open_new_file_in_zip(file)
+ if not ok then
+ err = "error in opening "..file.." in zipfile"
+ else
+ fin = io.open(fs.absolute_name(file), "rb")
+ if not fin then
+ ok = false
+ err = "error opening "..file.." for reading"
+ end
+ end
+ if ok then
+ local data = fin:read("*a")
+ if not data then
+ err = "error reading "..file
+ ok = false
+ else
+ ok = self:write_file_in_zip(data)
+ if not ok then
+ err = "error in writing "..file.." in the zipfile"
+ end
+ end
+ end
+ if fin then
+ fin:close()
+ end
+ if ok then
+ ok = self:close_file_in_zip()
+ if not ok then
+ err = "error in writing "..file.." in the zipfile"
+ end
+ end
+ return ok == true, err
+end
+
+--- Complete the writing of the zipfile.
+-- @param self handle of the zipfile being written.
+-- @return true if succeeded, nil in case of failure.
+local function zipwriter_close(self)
+ local zh = self.ziphandle
+
+ local central_directory_offset = zh:seek()
+
+ local size_of_central_directory = 0
+ -- Central directory structure
+ for _, lfh in ipairs(self.files) do
+ zh:write(number_to_bytestring(0x02014b50, 4)) -- signature
+ zh:write(number_to_bytestring(3, 2)) -- version made by: UNIX
+ zh:write(number_to_bytestring(20, 2)) -- version needed to extract: 2.0
+ zh:write(number_to_bytestring(0, 2)) -- general purpose bit flag
+ zh:write(number_to_bytestring(8, 2)) -- compression method: deflate
+ zh:write(number_to_bytestring(lfh.last_mod_file_time, 2))
+ zh:write(number_to_bytestring(lfh.last_mod_file_date, 2))
+ zh:write(number_to_bytestring(lfh.crc32, 4))
+ zh:write(number_to_bytestring(lfh.compressed_size, 4))
+ zh:write(number_to_bytestring(lfh.uncompressed_size, 4))
+ zh:write(number_to_bytestring(lfh.file_name_length, 2))
+ zh:write(number_to_bytestring(lfh.extra_field_length, 2))
+ zh:write(number_to_bytestring(0, 2)) -- file comment length
+ zh:write(number_to_bytestring(0, 2)) -- disk number start
+ zh:write(number_to_bytestring(0, 2)) -- internal file attributes
+ zh:write(number_to_bytestring(lfh.external_attr, 4)) -- external file attributes
+ zh:write(number_to_bytestring(lfh.offset, 4)) -- relative offset of local header
+ zh:write(lfh.file_name)
+ size_of_central_directory = size_of_central_directory + 46 + lfh.file_name_length
+ end
+
+ -- End of central directory record
+ zh:write(number_to_bytestring(0x06054b50, 4)) -- signature
+ zh:write(number_to_bytestring(0, 2)) -- number of this disk
+ zh:write(number_to_bytestring(0, 2)) -- number of disk with start of central directory
+ zh:write(number_to_bytestring(#self.files, 2)) -- total number of entries in the central dir on this disk
+ zh:write(number_to_bytestring(#self.files, 2)) -- total number of entries in the central dir
+ zh:write(number_to_bytestring(size_of_central_directory, 4))
+ zh:write(number_to_bytestring(central_directory_offset, 4))
+ zh:write(number_to_bytestring(0, 2)) -- zip file comment length
+ zh:close()
+
+ return true
+end
+
+--- Return a zip handle open for writing.
+-- @param name filename of the zipfile to be created.
+-- @return a zip handle, or nil in case of error.
+function zip.new_zipwriter(name)
+
+ local zw = {}
+
+ zw.ziphandle = io.open(fs.absolute_name(name), "wb")
+ if not zw.ziphandle then
+ return nil
+ end
+ zw.files = {}
+ zw.in_open_file = false
+
+ zw.add = zipwriter_add
+ zw.close = zipwriter_close
+ zw.open_new_file_in_zip = zipwriter_open_new_file_in_zip
+ zw.write_file_in_zip = zipwriter_write_file_in_zip
+ zw.close_file_in_zip = zipwriter_close_file_in_zip
+
+ return zw
+end
+
+--- Compress files in a .zip archive.
+-- @param zipfile string: pathname of .zip archive to be created.
+-- @param ... Filenames to be stored in the archive are given as
+-- additional arguments.
+-- @return boolean or (boolean, string): true on success,
+-- false and an error message on failure.
+function zip.zip(zipfile, ...)
+ local zw = zip.new_zipwriter(zipfile)
+ if not zw then
+ return nil, "error opening "..zipfile
+ end
+
+ local ok, err
+ for _, file in pairs({...}) do
+ if fs.is_dir(file) then
+ for _, entry in pairs(fs.find(file)) do
+ local fullname = dir.path(file, entry)
+ if fs.is_file(fullname) then
+ ok, err = zw:add(fullname)
+ if not ok then break end
+ end
+ end
+ else
+ ok, err = zw:add(file)
+ if not ok then break end
+ end
+ end
+
+ ok = zw:close()
+ if not ok then
+ return false, "error closing "..zipfile
+ end
+ return ok, err
+end
+
+
+return zip
diff --git a/Utils/luarocks/lua/luarocks/type_check.lua b/Utils/luarocks/lua/luarocks/type_check.lua
new file mode 100644
index 000000000..63c59ca21
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/type_check.lua
@@ -0,0 +1,344 @@
+--- Type-checking functions.
+-- Functions and definitions for doing a basic lint check on files
+-- loaded by LuaRocks.
+local type_check = {}
+package.loaded["luarocks.type_check"] = type_check
+
+local cfg = require("luarocks.cfg")
+local deps = require("luarocks.deps")
+
+type_check.rockspec_format = "1.1"
+
+local string_1 = { _type = "string" }
+local number_1 = { _type = "number" }
+local mandatory_string_1 = { _type = "string", _mandatory = true }
+
+-- Syntax for type-checking tables:
+--
+-- A type-checking table describes typing data for a value.
+-- Any key starting with an underscore has a special meaning:
+-- _type (string) is the Lua type of the value. Default is "table".
+-- _version (string) is the minimum rockspec_version that supports this value. Default is "1.0".
+-- _mandatory (boolean) indicates if the value is a mandatory key in its container table. Default is false.
+-- For "string" types only:
+-- _pattern (string) is the string-matching pattern, valid for string types only. Default is ".*".
+-- For "table" types only:
+-- _any (table) is the type-checking table for unspecified keys, recursively checked.
+-- _more (boolean) indicates that the table accepts unspecified keys and does not type-check them.
+-- Any other string keys that don't start with an underscore represent known keys and are type-checking tables, recursively checked.
+
+local rockspec_types = {
+ rockspec_format = string_1,
+ package = mandatory_string_1,
+ version = { _type = "string", _pattern = "[%w.]+-[%d]+", _mandatory = true },
+ description = {
+ summary = string_1,
+ detailed = string_1,
+ homepage = string_1,
+ license = string_1,
+ maintainer = string_1,
+ },
+ dependencies = {
+ platforms = {}, -- recursively defined below
+ _any = string_1,
+ },
+ supported_platforms = {
+ _any = string_1,
+ },
+ external_dependencies = {
+ platforms = {}, -- recursively defined below
+ _any = {
+ program = string_1,
+ header = string_1,
+ library = string_1,
+ }
+ },
+ source = {
+ _mandatory = true,
+ platforms = {}, -- recursively defined below
+ url = mandatory_string_1,
+ md5 = string_1,
+ file = string_1,
+ dir = string_1,
+ tag = string_1,
+ branch = string_1,
+ module = string_1,
+ cvs_tag = string_1,
+ cvs_module = string_1,
+ },
+ build = {
+ platforms = {}, -- recursively defined below
+ type = string_1,
+ install = {
+ lua = {
+ _more = true
+ },
+ lib = {
+ _more = true
+ },
+ conf = {
+ _more = true
+ },
+ bin = {
+ _more = true
+ }
+ },
+ copy_directories = {
+ _any = string_1,
+ },
+ _more = true,
+ _mandatory = true
+ },
+ hooks = {
+ platforms = {}, -- recursively defined below
+ post_install = string_1,
+ },
+ deploy = {
+ _version = "1.1",
+ wrap_bin_scripts = { _type = "boolean", _version = "1.1" },
+ }
+}
+
+type_check.rockspec_order = {"rockspec_format", "package", "version",
+ { "source", { "url", "tag", "branch", "md5" } },
+ { "description", {"summary", "detailed", "homepage", "license" } },
+ "supported_platforms", "dependencies", "external_dependencies",
+ { "build", {"type", "modules", "copy_directories", "platforms"} },
+ "hooks"}
+
+rockspec_types.build.platforms._any = rockspec_types.build
+rockspec_types.dependencies.platforms._any = rockspec_types.dependencies
+rockspec_types.external_dependencies.platforms._any = rockspec_types.external_dependencies
+rockspec_types.source.platforms._any = rockspec_types.source
+rockspec_types.hooks.platforms._any = rockspec_types.hooks
+
+local manifest_types = {
+ repository = {
+ _mandatory = true,
+ -- packages
+ _any = {
+ -- versions
+ _any = {
+ -- items
+ _any = {
+ arch = mandatory_string_1,
+ modules = { _any = string_1 },
+ commands = { _any = string_1 },
+ dependencies = { _any = string_1 },
+ -- TODO: to be extended with more metadata.
+ }
+ }
+ }
+ },
+ modules = {
+ _mandatory = true,
+ -- modules
+ _any = {
+ -- providers
+ _any = string_1
+ }
+ },
+ commands = {
+ _mandatory = true,
+ -- modules
+ _any = {
+ -- commands
+ _any = string_1
+ }
+ },
+ dependencies = {
+ -- each module
+ _any = {
+ -- each version
+ _any = {
+ -- each dependency
+ _any = {
+ name = string_1,
+ constraints = {
+ _any = {
+ no_upgrade = { _type = "boolean" },
+ op = string_1,
+ version = {
+ string = string_1,
+ _any = number_1,
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+local function check_version(version, typetbl, context)
+ local typetbl_version = typetbl._version or "1.0"
+ if deps.compare_versions(typetbl_version, version) then
+ if context == "" then
+ return nil, "Invalid rockspec_format version number in rockspec? Please fix rockspec accordingly."
+ else
+ return nil, context.." is not supported in rockspec format "..version.." (requires version "..typetbl_version.."), please fix the rockspec_format field accordingly."
+ end
+ end
+ return true
+end
+
+local type_check_table
+
+--- Type check an object.
+-- The object is compared against an archetypical value
+-- matching the expected type -- the actual values don't matter,
+-- only their types. Tables are type checked recursively.
+-- @param version string: The version of the item.
+-- @param item any: The object being checked.
+-- @param typetbl any: The type-checking table for the object.
+-- @param context string: A string indicating the "context" where the
+-- error occurred (the full table path), for error messages.
+-- @return boolean or (nil, string): true if type checking
+-- succeeded, or nil and an error message if it failed.
+-- @see type_check_table
+local function type_check_item(version, item, typetbl, context)
+ assert(type(version) == "string")
+
+ local ok, err = check_version(version, typetbl, context)
+ if not ok then
+ return nil, err
+ end
+
+ local item_type = type(item) or "nil"
+ local expected_type = typetbl._type or "table"
+
+ if expected_type == "number" then
+ if not tonumber(item) then
+ return nil, "Type mismatch on field "..context..": expected a number"
+ end
+ elseif expected_type == "string" then
+ if item_type ~= "string" then
+ return nil, "Type mismatch on field "..context..": expected a string, got "..item_type
+ end
+ if typetbl._pattern then
+ if not item:match("^"..typetbl._pattern.."$") then
+ return nil, "Type mismatch on field "..context..": invalid value "..item.." does not match '"..typetbl._pattern.."'"
+ end
+ end
+ elseif expected_type == "table" then
+ if item_type ~= expected_type then
+ return nil, "Type mismatch on field "..context..": expected a table"
+ else
+ return type_check_table(version, item, typetbl, context)
+ end
+ elseif item_type ~= expected_type then
+ return nil, "Type mismatch on field "..context..": expected "..expected_type
+ end
+ return true
+end
+
+local function mkfield(context, field)
+ if context == "" then
+ return tostring(field)
+ elseif type(field) == "string" then
+ return context.."."..field
+ else
+ return context.."["..tostring(field).."]"
+ end
+end
+
+--- Type check the contents of a table.
+-- The table's contents are compared against a reference table,
+-- which contains the recognized fields, with archetypical values
+-- matching the expected types -- the actual values of items in the
+-- reference table don't matter, only their types (ie, for field x
+-- in tbl that is correctly typed, type(tbl.x) == type(types.x)).
+-- If the reference table contains a field called MORE, then
+-- unknown fields in the checked table are accepted.
+-- If it contains a field called ANY, then its type will be
+-- used to check any unknown fields. If a field is prefixed
+-- with MUST_, it is mandatory; its absence from the table is
+-- a type error.
+-- Tables are type checked recursively.
+-- @param version string: The version of tbl.
+-- @param tbl table: The table to be type checked.
+-- @param typetbl table: The type-checking table, containing
+-- values for recognized fields in the checked table.
+-- @param context string: A string indicating the "context" where the
+-- error occurred (such as the name of the table the item is a part of),
+-- to be used by error messages.
+-- @return boolean or (nil, string): true if type checking
+-- succeeded, or nil and an error message if it failed.
+type_check_table = function(version, tbl, typetbl, context)
+ assert(type(version) == "string")
+ assert(type(tbl) == "table")
+ assert(type(typetbl) == "table")
+
+ local ok, err = check_version(version, typetbl, context)
+ if not ok then
+ return nil, err
+ end
+
+ for k, v in pairs(tbl) do
+ local t = typetbl[k] or typetbl._any
+ if t then
+ local ok, err = type_check_item(version, v, t, mkfield(context, k))
+ if not ok then return nil, err end
+ elseif typetbl._more then
+ -- Accept unknown field
+ else
+ if not cfg.accept_unknown_fields then
+ return nil, "Unknown field "..k
+ end
+ end
+ end
+ for k, v in pairs(typetbl) do
+ if k:sub(1,1) ~= "_" and v._mandatory then
+ if not tbl[k] then
+ return nil, "Mandatory field "..mkfield(context, k).." is missing."
+ end
+ end
+ end
+ return true
+end
+
+local function check_undeclared_globals(globals, typetbl)
+ local undeclared = {}
+ for glob, _ in pairs(globals) do
+ if not (typetbl[glob] or typetbl["MUST_"..glob]) then
+ table.insert(undeclared, glob)
+ end
+ end
+ if #undeclared == 1 then
+ return nil, "Unknown variable: "..undeclared[1]
+ elseif #undeclared > 1 then
+ return nil, "Unknown variables: "..table.concat(undeclared, ", ")
+ end
+ return true
+end
+
+--- Type check a rockspec table.
+-- Verify the correctness of elements from a
+-- rockspec table, reporting on unknown fields and type
+-- mismatches.
+-- @return boolean or (nil, string): true if type checking
+-- succeeded, or nil and an error message if it failed.
+function type_check.type_check_rockspec(rockspec, globals)
+ assert(type(rockspec) == "table")
+ if not rockspec.rockspec_format then
+ rockspec.rockspec_format = "1.0"
+ end
+ local ok, err = check_undeclared_globals(globals, rockspec_types)
+ if not ok then return nil, err end
+ return type_check_table(rockspec.rockspec_format, rockspec, rockspec_types, "")
+end
+
+--- Type check a manifest table.
+-- Verify the correctness of elements from a
+-- manifest table, reporting on unknown fields and type
+-- mismatches.
+-- @return boolean or (nil, string): true if type checking
+-- succeeded, or nil and an error message if it failed.
+function type_check.type_check_manifest(manifest, globals)
+ assert(type(manifest) == "table")
+ local ok, err = check_undeclared_globals(globals, manifest_types)
+ if not ok then return nil, err end
+ return type_check_table("1.0", manifest, manifest_types, "")
+end
+
+return type_check
diff --git a/Utils/luarocks/lua/luarocks/unpack.lua b/Utils/luarocks/lua/luarocks/unpack.lua
new file mode 100644
index 000000000..0922f9b90
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/unpack.lua
@@ -0,0 +1,166 @@
+
+--- Module implementing the LuaRocks "unpack" command.
+-- Unpack the contents of a rock.
+local unpack = {}
+package.loaded["luarocks.unpack"] = unpack
+
+local fetch = require("luarocks.fetch")
+local fs = require("luarocks.fs")
+local util = require("luarocks.util")
+local build = require("luarocks.build")
+local dir = require("luarocks.dir")
+local cfg = require("luarocks.cfg")
+
+util.add_run_function(unpack)
+unpack.help_summary = "Unpack the contents of a rock."
+unpack.help_arguments = "[--force] {| []}"
+unpack.help = [[
+Unpacks the contents of a rock in a newly created directory.
+Argument may be a rock file, or the name of a rock in a rocks server.
+In the latter case, the app version may be given as a second argument.
+
+--force Unpack files even if the output directory already exists.
+]]
+
+--- Load a rockspec file to the given directory, fetches the source
+-- files specified in the rockspec, and unpack them inside the directory.
+-- @param rockspec_file string: The URL for a rockspec file.
+-- @param dir_name string: The directory where to store and unpack files.
+-- @return table or (nil, string): the loaded rockspec table or
+-- nil and an error message.
+local function unpack_rockspec(rockspec_file, dir_name)
+ assert(type(rockspec_file) == "string")
+ assert(type(dir_name) == "string")
+
+ local rockspec, err = fetch.load_rockspec(rockspec_file)
+ if not rockspec then
+ return nil, "Failed loading rockspec "..rockspec_file..": "..err
+ end
+ local ok, err = fs.change_dir(dir_name)
+ if not ok then return nil, err end
+ local ok, sources_dir = fetch.fetch_sources(rockspec, true, ".")
+ if not ok then
+ return nil, sources_dir
+ end
+ ok, err = fs.change_dir(sources_dir)
+ if not ok then return nil, err end
+ ok, err = build.apply_patches(rockspec)
+ fs.pop_dir()
+ if not ok then return nil, err end
+ return rockspec
+end
+
+--- Load a .rock file to the given directory and unpack it inside it.
+-- @param rock_file string: The URL for a .rock file.
+-- @param dir_name string: The directory where to unpack.
+-- @param kind string: the kind of rock file, as in the second-level
+-- extension in the rock filename (eg. "src", "all", "linux-x86")
+-- @return table or (nil, string): the loaded rockspec table or
+-- nil and an error message.
+local function unpack_rock(rock_file, dir_name, kind)
+ assert(type(rock_file) == "string")
+ assert(type(dir_name) == "string")
+
+ local ok, err, errcode = fetch.fetch_and_unpack_rock(rock_file, dir_name)
+ if not ok then
+ return nil, "Failed unzipping rock "..rock_file, errcode
+ end
+ ok, err = fs.change_dir(dir_name)
+ if not ok then return nil, err end
+ local rockspec_file = dir_name..".rockspec"
+ local rockspec, err = fetch.load_rockspec(rockspec_file)
+ if not rockspec then
+ return nil, "Failed loading rockspec "..rockspec_file..": "..err
+ end
+ if kind == "src" then
+ if rockspec.source.file then
+ local ok, err = fs.unpack_archive(rockspec.source.file)
+ if not ok then
+ return nil, err
+ end
+ ok, err = fs.change_dir(rockspec.source.dir)
+ if not ok then return nil, err end
+ ok, err = build.apply_patches(rockspec)
+ fs.pop_dir()
+ if not ok then return nil, err end
+ end
+ end
+ return rockspec
+end
+
+--- Create a directory and perform the necessary actions so that
+-- the sources for the rock and its rockspec are unpacked inside it,
+-- laid out properly so that the 'make' command is able to build the module.
+-- @param file string: A rockspec or .rock URL.
+-- @return boolean or (nil, string): true if successful or nil followed
+-- by an error message.
+local function run_unpacker(file, force)
+ assert(type(file) == "string")
+
+ local base_name = dir.base_name(file)
+ local dir_name, kind, extension = base_name:match("(.*)%.([^.]+)%.(rock)$")
+ if not extension then
+ dir_name, extension = base_name:match("(.*)%.(rockspec)$")
+ kind = "rockspec"
+ end
+ if not extension then
+ return nil, file.." does not seem to be a valid filename."
+ end
+
+ local exists = fs.exists(dir_name)
+ if exists and not force then
+ return nil, "Directory "..dir_name.." already exists."
+ end
+ if not exists then
+ local ok, err = fs.make_dir(dir_name)
+ if not ok then return nil, err end
+ end
+ local rollback = util.schedule_function(fs.delete, fs.absolute_name(dir_name))
+
+ local rockspec, err
+ if extension == "rock" then
+ rockspec, err = unpack_rock(file, dir_name, kind)
+ elseif extension == "rockspec" then
+ rockspec, err = unpack_rockspec(file, dir_name)
+ end
+ if not rockspec then
+ return nil, err
+ end
+ if kind == "src" or kind == "rockspec" then
+ if rockspec.source.dir ~= "." then
+ local ok = fs.copy(rockspec.local_filename, rockspec.source.dir, cfg.perm_read)
+ if not ok then
+ return nil, "Failed copying unpacked rockspec into unpacked source directory."
+ end
+ end
+ util.printout()
+ util.printout("Done. You may now enter directory ")
+ util.printout(dir.path(dir_name, rockspec.source.dir))
+ util.printout("and type 'luarocks make' to build.")
+ end
+ util.remove_scheduled_function(rollback)
+ return true
+end
+
+--- Driver function for the "unpack" command.
+-- @param name string: may be a rock filename, for unpacking a
+-- rock file or the name of a rock to be fetched and unpacked.
+-- @param version string or nil: if the name of a package is given, a
+-- version may also be passed.
+-- @return boolean or (nil, string): true if successful or nil followed
+-- by an error message.
+function unpack.command(flags, name, version)
+ assert(type(version) == "string" or not version)
+ if type(name) ~= "string" then
+ return nil, "Argument missing. "..util.see_help("unpack")
+ end
+
+ if name:match(".*%.rock") or name:match(".*%.rockspec") then
+ return run_unpacker(name, flags["force"])
+ else
+ local search = require("luarocks.search")
+ return search.act_on_src_or_rockspec(run_unpacker, name:lower(), version)
+ end
+end
+
+return unpack
diff --git a/Utils/luarocks/lua/luarocks/upload.lua b/Utils/luarocks/lua/luarocks/upload.lua
new file mode 100644
index 000000000..7c0c416ce
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/upload.lua
@@ -0,0 +1,95 @@
+
+local upload = {}
+
+local util = require("luarocks.util")
+local fetch = require("luarocks.fetch")
+local pack = require("luarocks.pack")
+local cfg = require("luarocks.cfg")
+local Api = require("luarocks.upload.api")
+
+util.add_run_function(upload)
+upload.help_summary = "Upload a rockspec to the public rocks repository."
+upload.help_arguments = "[--skip-pack] [--api-key=] [--force] "
+upload.help = [[
+ Pack a source rock file (.src.rock extension),
+ upload rockspec and source rock to server.
+--skip-pack Do not pack and send source rock.
+--api-key= Give it an API key. It will be stored for subsequent uses.
+--force Replace existing rockspec if the same revision of
+ a module already exists. This should be used only
+ in case of upload mistakes: when updating a rockspec,
+ increment the revision number instead.
+]]
+
+function upload.command(flags, fname)
+ if not fname then
+ return nil, "Missing rockspec. "..util.see_help("upload")
+ end
+
+ local api, err = Api.new(flags)
+ if not api then
+ return nil, err
+ end
+ if cfg.verbose then
+ api.debug = true
+ end
+
+ local rockspec, err, errcode = fetch.load_rockspec(fname)
+ if err then
+ return nil, err, errcode
+ end
+
+ util.printout("Sending " .. tostring(fname) .. " ...")
+ local res, err = api:method("check_rockspec", {
+ package = rockspec.package,
+ version = rockspec.version
+ })
+ if not res then return nil, err end
+
+ if not res.module then
+ util.printout("Will create new module (" .. tostring(rockspec.package) .. ")")
+ end
+ if res.version and not flags["force"] then
+ return nil, "Revision "..rockspec.version.." already exists on the server. "..util.see_help("upload")
+ end
+
+ local rock_fname
+ if not flags["skip-pack"] and not rockspec.version:match("^scm") then
+ util.printout("Packing " .. tostring(rockspec.package))
+ rock_fname, err = pack.pack_source_rock(fname)
+ if not rock_fname then
+ return nil, err
+ end
+ end
+
+ local multipart = require("luarocks.upload.multipart")
+
+ res, err = api:method("upload", nil, {
+ rockspec_file = multipart.new_file(fname)
+ })
+ if not res then return nil, err end
+
+ if res.is_new and #res.manifests == 0 then
+ util.printerr("Warning: module not added to root manifest due to name taken.")
+ end
+
+ local module_url = res.module_url
+
+ if rock_fname then
+ if (not res.version) or (not res.version.id) then
+ return nil, "Invalid response from server."
+ end
+ util.printout(("Sending " .. tostring(rock_fname) .. " ..."))
+ res, err = api:method("upload_rock/" .. ("%d"):format(res.version.id), nil, {
+ rock_file = multipart.new_file(rock_fname)
+ })
+ if not res then return nil, err end
+ end
+
+ util.printout()
+ util.printout("Done: " .. tostring(module_url))
+ util.printout()
+ return true
+end
+
+return upload
diff --git a/Utils/luarocks/lua/luarocks/upload/api.lua b/Utils/luarocks/lua/luarocks/upload/api.lua
new file mode 100644
index 000000000..ba657186a
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/upload/api.lua
@@ -0,0 +1,284 @@
+
+local api = {}
+
+local cfg = require("luarocks.cfg")
+local fs = require("luarocks.fs")
+local util = require("luarocks.util")
+local persist = require("luarocks.persist")
+local multipart = require("luarocks.upload.multipart")
+
+local Api = {}
+
+local function upload_config_file()
+ local conf = cfg.which_config()
+ if not conf.user.file then
+ return nil
+ end
+ return (conf.user.file:gsub("/[^/]+$", "/upload_config.lua"))
+end
+
+function Api:load_config()
+ local upload_conf = upload_config_file()
+ if not upload_conf then return nil end
+ local cfg, err = persist.load_into_table(upload_conf)
+ return cfg
+end
+
+function Api:save_config()
+ -- Test configuration before saving it.
+ local res, err = self:raw_method("status")
+ if not res then
+ return nil, err
+ end
+ if res.errors then
+ util.printerr("Server says: " .. tostring(res.errors[1]))
+ return
+ end
+ local upload_conf = upload_config_file()
+ if not upload_conf then return nil end
+ persist.save_from_table(upload_conf, self.config)
+ fs.chmod(upload_conf, "0600")
+end
+
+function Api:check_version()
+ if not self._server_tool_version then
+ local tool_version = cfg.upload.tool_version
+ local res, err = self:request(tostring(self.config.server) .. "/api/tool_version", {
+ current = tool_version
+ })
+ if not res then
+ return nil, err
+ end
+ if not res.version then
+ return nil, "failed to fetch tool version"
+ end
+ self._server_tool_version = res.version
+ if res.force_update then
+ return nil, "Your upload client is too out of date to continue, please upgrade LuaRocks."
+ end
+ if res.version ~= tool_version then
+ util.printerr("Warning: Your LuaRocks is out of date, consider upgrading.")
+ end
+ end
+ return true
+end
+
+function Api:method(...)
+ local res, err = self:raw_method(...)
+ if not res then
+ return nil, err
+ end
+ if res.errors then
+ if res.errors[1] == "Invalid key" then
+ return nil, res.errors[1] .. " (use the --api-key flag to change)"
+ end
+ local msg = table.concat(res.errors, ", ")
+ return nil, "API Failed: " .. msg
+ end
+ return res
+end
+
+function Api:raw_method(path, ...)
+ self:check_version()
+ local url = tostring(self.config.server) .. "/api/" .. tostring(cfg.upload.api_version) .. "/" .. tostring(self.config.key) .. "/" .. tostring(path)
+ return self:request(url, ...)
+end
+
+local function encode_query_string(t, sep)
+ if sep == nil then
+ sep = "&"
+ end
+ local i = 0
+ local buf = { }
+ for k, v in pairs(t) do
+ if type(k) == "number" and type(v) == "table" then
+ k, v = v[1], v[2]
+ end
+ buf[i + 1] = multipart.url_escape(k)
+ buf[i + 2] = "="
+ buf[i + 3] = multipart.url_escape(v)
+ buf[i + 4] = sep
+ i = i + 4
+ end
+ buf[i] = nil
+ return table.concat(buf)
+end
+
+-- An ode to the multitude of JSON libraries out there...
+local function require_json()
+ local list = { "cjson", "dkjson", "json" }
+ for _, lib in ipairs(list) do
+ local json_ok, json = pcall(require, lib)
+ if json_ok then
+ pcall(json.use_lpeg) -- optional feature in dkjson
+ return json_ok, json
+ end
+ end
+ local errmsg = "Failed loading "
+ for i, name in ipairs(list) do
+ if i == #list then
+ errmsg = errmsg .."and '"..name.."'. Use 'luarocks search ' to search for a library and 'luarocks install ' to install one."
+ else
+ errmsg = errmsg .."'"..name.."', "
+ end
+ end
+ return nil, errmsg
+end
+
+local function redact_api_url(url)
+ url = tostring(url)
+ return (url:gsub(".*/api/[^/]+/[^/]+", "")) or ""
+end
+
+local ltn12_ok, ltn12 = pcall(require, "ltn12")
+if not ltn12_ok then -- If not using LuaSocket and/or LuaSec...
+
+function Api:request(url, params, post_params)
+ local vars = cfg.variables
+ local json_ok, json = require_json()
+ if not json_ok then return nil, "A JSON library is required for this command. "..json end
+
+ if cfg.downloader == "wget" then
+ local curl_ok, err = fs.is_tool_available(vars.CURL, "curl")
+ if not curl_ok then
+ return nil, err
+ end
+ end
+
+ if not self.config.key then
+ return nil, "Must have API key before performing any actions."
+ end
+ if params and next(params) then
+ url = url .. ("?" .. encode_query_string(params))
+ end
+ local method = "GET"
+ local out
+ local tmpfile = fs.tmpname()
+ if post_params then
+ method = "POST"
+ local curl_cmd = fs.Q(vars.CURL).." -f -k -L --silent --user-agent \""..cfg.user_agent.." via curl\" "
+ for k,v in pairs(post_params) do
+ local var = v
+ if type(v) == "table" then
+ var = "@"..v.fname
+ end
+ curl_cmd = curl_cmd .. "--form \""..k.."="..var.."\" "
+ end
+ if cfg.connection_timeout and cfg.connection_timeout > 0 then
+ curl_cmd = curl_cmd .. "--connect-timeout "..tonumber(cfg.connection_timeout).." "
+ end
+ local ok = fs.execute_string(curl_cmd..fs.Q(url).." -o "..fs.Q(tmpfile))
+ if not ok then
+ return nil, "API failure: " .. redact_api_url(url)
+ end
+ else
+ local ok, err = fs.download(url, tmpfile)
+ if not ok then
+ return nil, "API failure: " .. tostring(err) .. " - " .. redact_api_url(url)
+ end
+ end
+
+ local tmpfd = io.open(tmpfile)
+ if not tmpfd then
+ os.remove(tmpfile)
+ return nil, "API failure reading temporary file - " .. redact_api_url(url)
+ end
+ out = tmpfd:read("*a")
+ tmpfd:close()
+ os.remove(tmpfile)
+
+ if self.debug then
+ util.printout("[" .. tostring(method) .. " via curl] " .. redact_api_url(url) .. " ... ")
+ end
+
+ return json.decode(out)
+end
+
+else -- use LuaSocket and LuaSec
+
+local warned_luasec = false
+
+function Api:request(url, params, post_params)
+ local json_ok, json = require_json()
+ if not json_ok then return nil, "A JSON library is required for this command. "..json end
+ local server = tostring(self.config.server)
+ local http_ok, http
+ local via = "luasocket"
+ if server:match("^https://") then
+ http_ok, http = pcall(require, "ssl.https")
+ if http_ok then
+ via = "luasec"
+ else
+ if not warned_luasec then
+ util.printerr("LuaSec is not available; using plain HTTP. Install 'luasec' to enable HTTPS.")
+ warned_luasec = true
+ end
+ http_ok, http = pcall(require, "socket.http")
+ url = url:gsub("^https", "http")
+ via = "luasocket"
+ end
+ else
+ http_ok, http = pcall(require, "socket.http")
+ end
+ if not http_ok then
+ return nil, "Failed loading socket library!"
+ end
+
+ if not self.config.key then
+ return nil, "Must have API key before performing any actions."
+ end
+ local body
+ local headers = {}
+ if params and next(params) then
+ url = url .. ("?" .. encode_query_string(params))
+ end
+ if post_params then
+ local boundary
+ body, boundary = multipart.encode(post_params)
+ headers["Content-length"] = #body
+ headers["Content-type"] = "multipart/form-data; boundary=" .. tostring(boundary)
+ end
+ local method = post_params and "POST" or "GET"
+ if self.debug then
+ util.printout("[" .. tostring(method) .. " via "..via.."] " .. redact_api_url(url) .. " ... ")
+ end
+ local out = {}
+ local _, status = http.request({
+ url = url,
+ headers = headers,
+ method = method,
+ sink = ltn12.sink.table(out),
+ source = body and ltn12.source.string(body)
+ })
+ if self.debug then
+ util.printout(tostring(status))
+ end
+ if status ~= 200 then
+ return nil, "API returned " .. tostring(status) .. " - " .. redact_api_url(url)
+ end
+ return json.decode(table.concat(out))
+end
+
+end
+
+function api.new(flags)
+ local self = {}
+ setmetatable(self, { __index = Api })
+ self.config = self:load_config() or {}
+ self.config.server = flags["server"] or self.config.server or cfg.upload.server
+ self.config.version = self.config.version or cfg.upload.version
+ self.config.key = flags["api-key"] or self.config.key
+ self.debug = flags["debug"]
+ if not self.config.key then
+ return nil, "You need an API key to upload rocks.\n" ..
+ "Navigate to "..self.config.server.."/settings to get a key\n" ..
+ "and then pass it through the --api-key= flag."
+ end
+ if flags["api-key"] then
+ self:save_config()
+ end
+ return self
+end
+
+return api
+
diff --git a/Utils/luarocks/lua/luarocks/upload/multipart.lua b/Utils/luarocks/lua/luarocks/upload/multipart.lua
new file mode 100644
index 000000000..aad2e4398
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/upload/multipart.lua
@@ -0,0 +1,111 @@
+
+local multipart = {}
+
+local File = {}
+
+local unpack = unpack or table.unpack
+
+math.randomseed(os.time())
+
+-- socket.url.escape(s) from LuaSocket 3.0rc1
+function multipart.url_escape(s)
+ return (string.gsub(s, "([^A-Za-z0-9_])", function(c)
+ return string.format("%%%02x", string.byte(c))
+ end))
+end
+
+function File:mime()
+ if not self.mimetype then
+ local mimetypes_ok, mimetypes = pcall(require, "mimetypes")
+ if mimetypes_ok then
+ self.mimetype = mimetypes.guess(self.fname)
+ end
+ self.mimetype = self.mimetype or "application/octet-stream"
+ end
+ return self.mimetype
+end
+
+function File:content()
+ local fd = io.open(self.fname, "rb")
+ if not fd then
+ return nil, "Failed to open file: "..self.fname
+ end
+ local data = fd:read("*a")
+ fd:close()
+ return data
+end
+
+local function rand_string(len)
+ local shuffled = {}
+ for i = 1, len do
+ local r = math.random(97, 122)
+ if math.random() >= 0.5 then
+ r = r - 32
+ end
+ shuffled[i] = r
+ end
+ return string.char(unpack(shuffled))
+end
+
+-- multipart encodes params
+-- returns encoded string,boundary
+-- params is an a table of tuple tables:
+-- params = {
+-- {key1, value2},
+-- {key2, value2},
+-- key3: value3
+-- }
+function multipart.encode(params)
+ local tuples = { }
+ for i = 1, #params do
+ tuples[i] = params[i]
+ end
+ for k,v in pairs(params) do
+ if type(k) == "string" then
+ table.insert(tuples, {k, v})
+ end
+ end
+ local chunks = {}
+ for _, tuple in ipairs(tuples) do
+ local k,v = unpack(tuple)
+ k = multipart.url_escape(k)
+ local buffer = { 'Content-Disposition: form-data; name="' .. k .. '"' }
+ local content
+ if type(v) == "table" and v.__class == File then
+ buffer[1] = buffer[1] .. ('; filename="' .. v.fname:gsub(".*/", "") .. '"')
+ table.insert(buffer, "Content-type: " .. v:mime())
+ content = v:content()
+ else
+ content = v
+ end
+ table.insert(buffer, "")
+ table.insert(buffer, content)
+ table.insert(chunks, table.concat(buffer, "\r\n"))
+ end
+ local boundary
+ while not boundary do
+ boundary = "Boundary" .. rand_string(16)
+ for _, chunk in ipairs(chunks) do
+ if chunk:find(boundary) then
+ boundary = nil
+ break
+ end
+ end
+ end
+ local inner = "\r\n--" .. boundary .. "\r\n"
+ return table.concat({ "--", boundary, "\r\n",
+ table.concat(chunks, inner),
+ "\r\n", "--", boundary, "--", "\r\n" }), boundary
+end
+
+function multipart.new_file(fname, mime)
+ local self = {}
+ setmetatable(self, { __index = File })
+ self.__class = File
+ self.fname = fname
+ self.mimetype = mime
+ return self
+end
+
+return multipart
+
diff --git a/Utils/luarocks/lua/luarocks/util.lua b/Utils/luarocks/lua/luarocks/util.lua
new file mode 100644
index 000000000..c9fb7d637
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/util.lua
@@ -0,0 +1,714 @@
+
+--- Assorted utilities for managing tables, plus a scheduler for rollback functions.
+-- Does not requires modules directly (only as locals
+-- inside specific functions) to avoid interdependencies,
+-- as this is used in the bootstrapping stage of luarocks.cfg.
+
+local util = {}
+
+local unpack = unpack or table.unpack
+
+local scheduled_functions = {}
+local debug = require("debug")
+
+--- Schedule a function to be executed upon program termination.
+-- This is useful for actions such as deleting temporary directories
+-- or failure rollbacks.
+-- @param f function: Function to be executed.
+-- @param ... arguments to be passed to function.
+-- @return table: A token representing the scheduled execution,
+-- which can be used to remove the item later from the list.
+function util.schedule_function(f, ...)
+ assert(type(f) == "function")
+
+ local item = { fn = f, args = {...} }
+ table.insert(scheduled_functions, item)
+ return item
+end
+
+--- Unschedule a function.
+-- This is useful for cancelling a rollback of a completed operation.
+-- @param item table: The token representing the scheduled function that was
+-- returned from the schedule_function call.
+function util.remove_scheduled_function(item)
+ for k, v in pairs(scheduled_functions) do
+ if v == item then
+ table.remove(scheduled_functions, k)
+ return
+ end
+ end
+end
+
+--- Execute scheduled functions.
+-- Some calls create temporary files and/or directories and register
+-- corresponding cleanup functions. Calling this function will run
+-- these function, erasing temporaries.
+-- Functions are executed in the inverse order they were scheduled.
+function util.run_scheduled_functions()
+ local fs = require("luarocks.fs")
+ fs.change_dir_to_root()
+ for i = #scheduled_functions, 1, -1 do
+ local item = scheduled_functions[i]
+ item.fn(unpack(item.args))
+ end
+end
+
+--- Produce a Lua pattern that matches precisely the given string
+-- (this is suitable to be concatenating to other patterns,
+-- so it does not include beginning- and end-of-string markers (^$)
+-- @param s string: The input string
+-- @return string: The equivalent pattern
+function util.matchquote(s)
+ return (s:gsub("[?%-+*%[%].%%()$^]","%%%1"))
+end
+
+--- List of supported arguments.
+-- Arguments that take no parameters are marked with the boolean true.
+-- Arguments that take a parameter are marked with a descriptive string.
+-- Arguments that may take an empty string are described in quotes,
+-- (as in the value for --detailed="").
+-- For all other string values, it means the parameter is mandatory.
+local supported_flags = {
+ ["all"] = true,
+ ["api-key"] = "",
+ ["append"] = true,
+ ["arch"] = "",
+ ["bin"] = true,
+ ["binary"] = true,
+ ["branch"] = "",
+ ["debug"] = true,
+ ["deps"] = true,
+ ["deps-mode"] = "",
+ ["detailed"] = "\"\"",
+ ["force"] = true,
+ ["force-fast"] = true,
+ ["from"] = "",
+ ["help"] = true,
+ ["home"] = true,
+ ["homepage"] = "\"\"",
+ ["keep"] = true,
+ ["lib"] = "",
+ ["license"] = "\"\"",
+ ["list"] = true,
+ ["local"] = true,
+ ["local-tree"] = true,
+ ["lr-bin"] = true,
+ ["lr-cpath"] = true,
+ ["lr-path"] = true,
+ ["lua-version"] = "",
+ ["lua-ver"] = true,
+ ["lua-incdir"] = true,
+ ["lua-libdir"] = true,
+ ["modules"] = true,
+ ["mversion"] = true,
+ ["no-refresh"] = true,
+ ["nodeps"] = true,
+ ["old-versions"] = true,
+ ["only-deps"] = true,
+ ["only-from"] = "",
+ ["only-server"] = "",
+ ["only-sources"] = "",
+ ["only-sources-from"] = "",
+ ["outdated"] = true,
+ ["output"] = "",
+ ["pack-binary-rock"] = true,
+ ["porcelain"] = true,
+ ["quick"] = true,
+ ["rock-dir"] = true,
+ ["rock-tree"] = true,
+ ["rock-trees"] = true,
+ ["rockspec"] = true,
+ ["rockspec-format"] = "",
+ ["server"] = "",
+ ["skip-pack"] = true,
+ ["source"] = true,
+ ["summary"] = "\"\"",
+ ["system-config"] = true,
+ ["tag"] = "",
+ ["timeout"] = "",
+ ["to"] = "",
+ ["tree"] = "",
+ ["user-config"] = true,
+ ["verbose"] = true,
+ ["version"] = true,
+}
+
+--- Extract flags from an arguments list.
+-- Given string arguments, extract flag arguments into a flags set.
+-- For example, given "foo", "--tux=beep", "--bla", "bar", "--baz",
+-- it would return the following:
+-- {["bla"] = true, ["tux"] = "beep", ["baz"] = true}, "foo", "bar".
+function util.parse_flags(...)
+ local args = {...}
+ local flags = {}
+ local i = 1
+ local out = {}
+ local ignore_flags = false
+ while i <= #args do
+ local flag = args[i]:match("^%-%-(.*)")
+ if flag == "--" then
+ ignore_flags = true
+ end
+ if flag and not ignore_flags then
+ local var,val = flag:match("([a-z_%-]*)=(.*)")
+ if val then
+ local vartype = supported_flags[var]
+ if type(vartype) == "string" then
+ if val == "" and vartype:sub(1,1) ~= '"' then
+ return { ERROR = "Invalid argument: parameter to flag --"..var.."="..vartype.." cannot be empty." }
+ end
+ flags[var] = val
+ else
+ if vartype then
+ return { ERROR = "Invalid argument: flag --"..var.." does not take an parameter." }
+ else
+ return { ERROR = "Invalid argument: unknown flag --"..var.."." }
+ end
+ end
+ else
+ local var = flag
+ local vartype = supported_flags[var]
+ if type(vartype) == "string" then
+ i = i + 1
+ local val = args[i]
+ if not val then
+ return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter." }
+ end
+ if val:match("^%-%-.*") then
+ return { ERROR = "Invalid argument: flag --"..var.."="..vartype.." expects a parameter (if you really want to pass "..val.." as an argument to --"..var..", use --"..var.."="..val..")." }
+ else
+ if val == "" and vartype:sub(1,1) ~= '"' then
+ return { ERROR = "Invalid argument: parameter to flag --"..var.."="..vartype.." cannot be empty." }
+ end
+ flags[var] = val
+ end
+ elseif vartype == true then
+ flags[var] = true
+ else
+ return { ERROR = "Invalid argument: unknown flag --"..var.."." }
+ end
+ end
+ else
+ table.insert(out, args[i])
+ end
+ i = i + 1
+ end
+ return flags, unpack(out)
+end
+
+-- Adds legacy 'run' function to a command module.
+-- @param command table: command module with 'command' function,
+-- the added 'run' function calls it after parseing command-line arguments.
+function util.add_run_function(command)
+ command.run = function(...) return command.command(util.parse_flags(...)) end
+end
+
+--- Merges contents of src on top of dst's contents.
+-- @param dst Destination table, which will receive src's contents.
+-- @param src Table which provides new contents to dst.
+-- @see platform_overrides
+function util.deep_merge(dst, src)
+ for k, v in pairs(src) do
+ if type(v) == "table" then
+ if not dst[k] then
+ dst[k] = {}
+ end
+ if type(dst[k]) == "table" then
+ util.deep_merge(dst[k], v)
+ else
+ dst[k] = v
+ end
+ else
+ dst[k] = v
+ end
+ end
+end
+
+--- Perform platform-specific overrides on a table.
+-- Overrides values of table with the contents of the appropriate
+-- subset of its "platforms" field. The "platforms" field should
+-- be a table containing subtables keyed with strings representing
+-- platform names. Names that match the contents of the global
+-- cfg.platforms setting are used. For example, if
+-- cfg.platforms= {"foo"}, then the fields of
+-- tbl.platforms.foo will overwrite those of tbl with the same
+-- names. For table values, the operation is performed recursively
+-- (tbl.platforms.foo.x.y.z overrides tbl.x.y.z; other contents of
+-- tbl.x are preserved).
+-- @param tbl table or nil: Table which may contain a "platforms" field;
+-- if it doesn't (or if nil is passed), this function does nothing.
+function util.platform_overrides(tbl)
+ assert(type(tbl) == "table" or not tbl)
+
+ local cfg = require("luarocks.cfg")
+
+ if not tbl then return end
+
+ if tbl.platforms then
+ for _, platform in ipairs(cfg.platforms) do
+ local platform_tbl = tbl.platforms[platform]
+ if platform_tbl then
+ util.deep_merge(tbl, platform_tbl)
+ end
+ end
+ end
+ tbl.platforms = nil
+end
+
+local var_format_pattern = "%$%((%a[%a%d_]+)%)"
+
+--- Create a new shallow copy of a table: a new table with
+-- the same keys and values. Keys point to the same objects as
+-- the original table (ie, does not copy recursively).
+-- @param tbl table: the input table
+-- @return table: a new table with the same contents.
+function util.make_shallow_copy(tbl)
+ local copy = {}
+ for k,v in pairs(tbl) do
+ copy[k] = v
+ end
+ return copy
+end
+
+-- Check if a set of needed variables are referenced
+-- somewhere in a list of definitions, warning the user
+-- about any unused ones. Each key in needed_set should
+-- appear as a $(XYZ) variable at least once as a
+-- substring of some value of var_defs.
+-- @param var_defs: a table with string keys and string
+-- values, containing variable definitions.
+-- @param needed_set: a set where keys are the names of
+-- needed variables.
+-- @param msg string: the warning message to display.
+function util.warn_if_not_used(var_defs, needed_set, msg)
+ needed_set = util.make_shallow_copy(needed_set)
+ for _, val in pairs(var_defs) do
+ for used in val:gmatch(var_format_pattern) do
+ needed_set[used] = nil
+ end
+ end
+ for var, _ in pairs(needed_set) do
+ util.warning(msg:format(var))
+ end
+end
+
+-- Output any entries that might remain in $(XYZ) format,
+-- warning the user that substitutions have failed.
+-- @param line string: the input string
+local function warn_failed_matches(line)
+ local any_failed = false
+ if line:match(var_format_pattern) then
+ for unmatched in line:gmatch(var_format_pattern) do
+ util.warning("unmatched variable " .. unmatched)
+ any_failed = true
+ end
+ end
+ return any_failed
+end
+
+--- Perform make-style variable substitutions on string values of a table.
+-- For every string value tbl.x which contains a substring of the format
+-- "$(XYZ)" will have this substring replaced by vars["XYZ"], if that field
+-- exists in vars. Only string values are processed; this function
+-- does not scan subtables recursively.
+-- @param tbl table: Table to have its string values modified.
+-- @param vars table: Table containing string-string key-value pairs
+-- representing variables to replace in the strings values of tbl.
+function util.variable_substitutions(tbl, vars)
+ assert(type(tbl) == "table")
+ assert(type(vars) == "table")
+
+ local updated = {}
+ for k, v in pairs(tbl) do
+ if type(v) == "string" then
+ updated[k] = v:gsub(var_format_pattern, vars)
+ if warn_failed_matches(updated[k]) then
+ updated[k] = updated[k]:gsub(var_format_pattern, "")
+ end
+ end
+ end
+ for k, v in pairs(updated) do
+ tbl[k] = v
+ end
+end
+
+--- Return an array of keys of a table.
+-- @param tbl table: The input table.
+-- @return table: The array of keys.
+function util.keys(tbl)
+ local ks = {}
+ for k,_ in pairs(tbl) do
+ table.insert(ks, k)
+ end
+ return ks
+end
+
+local function default_sort(a, b)
+ local ta = type(a)
+ local tb = type(b)
+ if ta == "number" and tb == "number" then
+ return a < b
+ elseif ta == "number" then
+ return true
+ elseif tb == "number" then
+ return false
+ else
+ return tostring(a) < tostring(b)
+ end
+end
+
+--- A table iterator generator that returns elements sorted by key,
+-- to be used in "for" loops.
+-- @param tbl table: The table to be iterated.
+-- @param sort_function function or table or nil: An optional comparison function
+-- to be used by table.sort when sorting keys, or an array listing an explicit order
+-- for keys. If a value itself is an array, it is taken so that the first element
+-- is a string representing the field name, and the second element is a priority table
+-- for that key, which is returned by the iterator as the third value after the key
+-- and the value.
+-- @return function: the iterator function.
+function util.sortedpairs(tbl, sort_function)
+ sort_function = sort_function or default_sort
+ local keys = util.keys(tbl)
+ local sub_orders = {}
+
+ if type(sort_function) == "function" then
+ table.sort(keys, sort_function)
+ else
+ local order = sort_function
+ local ordered_keys = {}
+ local all_keys = keys
+ keys = {}
+
+ for _, order_entry in ipairs(order) do
+ local key, sub_order
+ if type(order_entry) == "table" then
+ key = order_entry[1]
+ sub_order = order_entry[2]
+ else
+ key = order_entry
+ end
+
+ if tbl[key] then
+ ordered_keys[key] = true
+ sub_orders[key] = sub_order
+ table.insert(keys, key)
+ end
+ end
+
+ table.sort(all_keys, default_sort)
+ for _, key in ipairs(all_keys) do
+ if not ordered_keys[key] then
+ table.insert(keys, key)
+ end
+ end
+ end
+
+ local i = 1
+ return function()
+ local key = keys[i]
+ i = i + 1
+ return key, tbl[key], sub_orders[key]
+ end
+end
+
+function util.lua_versions()
+ local versions = { "5.1", "5.2", "5.3" }
+ local i = 0
+ return function()
+ i = i + 1
+ return versions[i]
+ end
+end
+
+function util.starts_with(s, prefix)
+ return s:sub(1,#prefix) == prefix
+end
+
+--- Print a line to standard output
+function util.printout(...)
+ io.stdout:write(table.concat({...},"\t"))
+ io.stdout:write("\n")
+end
+
+--- Print a line to standard error
+function util.printerr(...)
+ io.stderr:write(table.concat({...},"\t"))
+ io.stderr:write("\n")
+end
+
+--- Display a warning message.
+-- @param msg string: the warning message
+function util.warning(msg)
+ util.printerr("Warning: "..msg)
+end
+
+function util.title(msg, porcelain, underline)
+ if porcelain then return end
+ util.printout()
+ util.printout(msg)
+ util.printout((underline or "-"):rep(#msg))
+ util.printout()
+end
+
+function util.this_program(default)
+ local i = 1
+ local last, cur = default, default
+ while i do
+ local dbg = debug.getinfo(i,"S")
+ if not dbg then break end
+ last = cur
+ cur = dbg.source
+ i=i+1
+ end
+ return last:sub(2)
+end
+
+function util.deps_mode_help(program)
+ local cfg = require("luarocks.cfg")
+ return [[
+--deps-mode= How to handle dependencies. Four modes are supported:
+ * all - use all trees from the rocks_trees list
+ for finding dependencies
+ * one - use only the current tree (possibly set
+ with --tree)
+ * order - use trees based on order (use the current
+ tree and all trees below it on the rocks_trees list)
+ * none - ignore dependencies altogether.
+ The default mode may be set with the deps_mode entry
+ in the configuration file.
+ The current default is "]]..cfg.deps_mode..[[".
+ Type ']]..util.this_program(program or "luarocks")..[[' with no arguments to see
+ your list of rocks trees.
+]]
+end
+
+function util.see_help(command, program)
+ return "See '"..util.this_program(program or "luarocks")..' help'..(command and " "..command or "").."'."
+end
+
+function util.announce_install(rockspec)
+ local cfg = require("luarocks.cfg")
+ local path = require("luarocks.path")
+
+ local suffix = ""
+ if rockspec.description and rockspec.description.license then
+ suffix = " (license: "..rockspec.description.license..")"
+ end
+
+ local root_dir = path.root_dir(cfg.rocks_dir)
+ util.printout(rockspec.name.." "..rockspec.version.." is now installed in "..root_dir..suffix)
+ util.printout()
+end
+
+--- Collect rockspecs located in a subdirectory.
+-- @param versions table: A table mapping rock names to newest rockspec versions.
+-- @param paths table: A table mapping rock names to newest rockspec paths.
+-- @param unnamed_paths table: An array of rockspec paths that don't contain rock
+-- name and version in regular format.
+-- @param subdir string: path to subdirectory.
+local function collect_rockspecs(versions, paths, unnamed_paths, subdir)
+ local fs = require("luarocks.fs")
+ local dir = require("luarocks.dir")
+ local path = require("luarocks.path")
+ local deps = require("luarocks.deps")
+
+ if fs.is_dir(subdir) then
+ for file in fs.dir(subdir) do
+ file = dir.path(subdir, file)
+
+ if file:match("rockspec$") and fs.is_file(file) then
+ local rock, version = path.parse_name(file)
+
+ if rock then
+ if not versions[rock] or deps.compare_versions(version, versions[rock]) then
+ versions[rock] = version
+ paths[rock] = file
+ end
+ else
+ table.insert(unnamed_paths, file)
+ end
+ end
+ end
+ end
+end
+
+--- Get default rockspec name for commands that take optional rockspec name.
+-- @return string or (nil, string): path to the rockspec or nil and error message.
+function util.get_default_rockspec()
+ local versions, paths, unnamed_paths = {}, {}, {}
+ -- Look for rockspecs in some common locations.
+ collect_rockspecs(versions, paths, unnamed_paths, ".")
+ collect_rockspecs(versions, paths, unnamed_paths, "rockspec")
+ collect_rockspecs(versions, paths, unnamed_paths, "rockspecs")
+
+ if #unnamed_paths > 0 then
+ -- There are rockspecs not following "name-version.rockspec" format.
+ -- More than one are ambiguous.
+ if #unnamed_paths > 1 then
+ return nil, "Please specify which rockspec file to use."
+ else
+ return unnamed_paths[1]
+ end
+ else
+ local rock = next(versions)
+
+ if rock then
+ -- If there are rockspecs for multiple rocks it's ambiguous.
+ if next(versions, rock) then
+ return nil, "Please specify which rockspec file to use."
+ else
+ return paths[rock]
+ end
+ else
+ return nil, "Argument missing: please specify a rockspec to use on current directory."
+ end
+ end
+end
+
+-- from http://lua-users.org/wiki/SplitJoin
+-- by PhilippeLhoste
+function util.split_string(str, delim, maxNb)
+ -- Eliminate bad cases...
+ if string.find(str, delim) == nil then
+ return { str }
+ end
+ if maxNb == nil or maxNb < 1 then
+ maxNb = 0 -- No limit
+ end
+ local result = {}
+ local pat = "(.-)" .. delim .. "()"
+ local nb = 0
+ local lastPos
+ for part, pos in string.gmatch(str, pat) do
+ nb = nb + 1
+ result[nb] = part
+ lastPos = pos
+ if nb == maxNb then break end
+ end
+ -- Handle the last field
+ if nb ~= maxNb then
+ result[nb + 1] = string.sub(str, lastPos)
+ end
+ return result
+end
+
+--- Remove repeated entries from a path-style string.
+-- Example: given ("a;b;c;a;b;d", ";"), returns "a;b;c;d".
+-- @param list string: A path string (from $PATH or package.path)
+-- @param sep string: The separator
+function util.remove_path_dupes(list, sep)
+ assert(type(list) == "string")
+ assert(type(sep) == "string")
+ local parts = util.split_string(list, sep)
+ local final, entries = {}, {}
+ for _, part in ipairs(parts) do
+ part = part:gsub("//", "/")
+ if not entries[part] then
+ table.insert(final, part)
+ entries[part] = true
+ end
+ end
+ return table.concat(final, sep)
+end
+
+---
+-- Formats tables with cycles recursively to any depth.
+-- References to other tables are shown as values.
+-- Self references are indicated.
+-- The string returned is "Lua code", which can be procesed
+-- (in the case in which indent is composed by spaces or "--").
+-- Userdata and function keys and values are shown as strings,
+-- which logically are exactly not equivalent to the original code.
+-- This routine can serve for pretty formating tables with
+-- proper indentations, apart from printing them:
+-- io.write(table.show(t, "t")) -- a typical use
+-- Written by Julio Manuel Fernandez-Diaz,
+-- Heavily based on "Saving tables with cycles", PIL2, p. 113.
+-- @param t table: is the table.
+-- @param name string: is the name of the table (optional)
+-- @param indent string: is a first indentation (optional).
+-- @return string: the pretty-printed table
+function util.show_table(t, name, indent)
+ local cart -- a container
+ local autoref -- for self references
+
+ local function isemptytable(t) return next(t) == nil end
+
+ local function basicSerialize (o)
+ local so = tostring(o)
+ if type(o) == "function" then
+ local info = debug.getinfo(o, "S")
+ -- info.name is nil because o is not a calling level
+ if info.what == "C" then
+ return ("%q"):format(so .. ", C function")
+ else
+ -- the information is defined through lines
+ return ("%q"):format(so .. ", defined in (" .. info.linedefined .. "-" .. info.lastlinedefined .. ")" .. info.source)
+ end
+ elseif type(o) == "number" then
+ return so
+ else
+ return ("%q"):format(so)
+ end
+ end
+
+ local function addtocart (value, name, indent, saved, field)
+ indent = indent or ""
+ saved = saved or {}
+ field = field or name
+
+ cart = cart .. indent .. field
+
+ if type(value) ~= "table" then
+ cart = cart .. " = " .. basicSerialize(value) .. ";\n"
+ else
+ if saved[value] then
+ cart = cart .. " = {}; -- " .. saved[value] .. " (self reference)\n"
+ autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
+ else
+ saved[value] = name
+ --if tablecount(value) == 0 then
+ if isemptytable(value) then
+ cart = cart .. " = {};\n"
+ else
+ cart = cart .. " = {\n"
+ for k, v in pairs(value) do
+ k = basicSerialize(k)
+ local fname = ("%s[%s]"):format(name, k)
+ field = ("[%s]"):format(k)
+ -- three spaces between levels
+ addtocart(v, fname, indent .. " ", saved, field)
+ end
+ cart = cart .. indent .. "};\n"
+ end
+ end
+ end
+ end
+
+ name = name or "__unnamed__"
+ if type(t) ~= "table" then
+ return name .. " = " .. basicSerialize(t)
+ end
+ cart, autoref = "", ""
+ addtocart(t, name, indent)
+ return cart .. autoref
+end
+
+function util.array_contains(tbl, value)
+ for _, v in ipairs(tbl) do
+ if v == value then
+ return true
+ end
+ end
+ return false
+end
+
+-- Quote Lua string, analogous to fs.Q.
+-- @param s A string, such as "hello"
+-- @return string: A quoted string, such as '"hello"'
+function util.LQ(s)
+ return ("%q"):format(s)
+end
+
+return util
diff --git a/Utils/luarocks/lua/luarocks/validate.lua b/Utils/luarocks/lua/luarocks/validate.lua
new file mode 100644
index 000000000..c4570aa4b
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/validate.lua
@@ -0,0 +1,159 @@
+
+--- Sandboxed test of build/install of all packages in a repository (unfinished and disabled).
+local validate = {}
+package.loaded["luarocks.validate"] = validate
+
+local fs = require("luarocks.fs")
+local dir = require("luarocks.dir")
+local path = require("luarocks.path")
+local cfg = require("luarocks.cfg")
+local build = require("luarocks.build")
+local install = require("luarocks.install")
+local util = require("luarocks.util")
+
+util.add_run_function(validate)
+validate.help_summary = "Sandboxed test of build/install of all packages in a repository."
+
+validate.help = [[
+, if given, is a local repository pathname.
+]]
+
+local function save_settings(repo)
+ local protocol, path = dir.split_url(repo)
+ table.insert(cfg.rocks_servers, 1, protocol.."://"..path)
+ return {
+ root_dir = cfg.root_dir,
+ rocks_dir = cfg.rocks_dir,
+ deploy_bin_dir = cfg.deploy_bin_dir,
+ deploy_lua_dir = cfg.deploy_lua_dir,
+ deploy_lib_dir = cfg.deploy_lib_dir,
+ }
+end
+
+local function restore_settings(settings)
+ cfg.root_dir = settings.root_dir
+ cfg.rocks_dir = settings.rocks_dir
+ cfg.deploy_bin_dir = settings.deploy_bin_dir
+ cfg.deploy_lua_dir = settings.deploy_lua_dir
+ cfg.deploy_lib_dir = settings.deploy_lib_dir
+ cfg.variables.ROCKS_TREE = settings.rocks_dir
+ cfg.variables.SCRIPTS_DIR = settings.deploy_bin_dir
+ table.remove(cfg.rocks_servers, 1)
+end
+
+local function prepare_sandbox(file)
+ local root_dir = fs.make_temp_dir(file):gsub("/+$", "")
+ cfg.root_dir = root_dir
+ cfg.rocks_dir = path.rocks_dir(root_dir)
+ cfg.deploy_bin_dir = path.deploy_bin_dir(root_dir)
+ cfg.variables.ROCKS_TREE = cfg.rocks_dir
+ cfg.variables.SCRIPTS_DIR = cfg.deploy_bin_dir
+ return root_dir
+end
+
+local function validate_rockspec(file)
+ local ok, err, errcode = build.build_rockspec(file, true, "one")
+ if not ok then
+ util.printerr(err)
+ end
+ return ok, err, errcode
+end
+
+local function validate_src_rock(file)
+ local ok, err, errcode = build.build_rock(file, false, "one")
+ if not ok then
+ util.printerr(err)
+ end
+ return ok, err, errcode
+end
+
+local function validate_rock(file)
+ local ok, err, errcode = install.install_binary_rock(file, "one")
+ if not ok then
+ util.printerr(err)
+ end
+ return ok, err, errcode
+end
+
+function validate.command(flags, repo)
+ repo = repo or cfg.rocks_dir
+
+ util.printout("Verifying contents of "..repo)
+
+ local results = {
+ ok = {}
+ }
+ local settings = save_settings(repo)
+ local sandbox
+ if flags["quick"] then
+ sandbox = prepare_sandbox("luarocks_validate")
+ end
+ if not fs.exists(repo) then
+ return nil, repo.." is not a local repository."
+ end
+ for file in fs.dir(repo) do for _=1,1 do
+ if file == "manifest" or file == "index.html" then
+ break -- continue for
+ end
+ local pathname = fs.absolute_name(dir.path(repo, file))
+ if not flags["quick"] then
+ sandbox = prepare_sandbox(file)
+ end
+ local ok, err, errcode
+ util.printout()
+ util.printout("Verifying "..pathname)
+ if file:match("%.rockspec$") then
+ ok, err, errcode = validate_rockspec(pathname, "one")
+ elseif file:match("%.src%.rock$") then
+ ok, err, errcode = validate_src_rock(pathname)
+ elseif file:match("%.rock$") then
+ ok, err, errcode = validate_rock(pathname)
+ end
+ if ok then
+ table.insert(results.ok, {file=file} )
+ else
+ if not errcode then
+ errcode = "misc"
+ end
+ if not results[errcode] then
+ results[errcode] = {}
+ end
+ table.insert(results[errcode], {file=file, err=err} )
+ end
+ util.run_scheduled_functions()
+ if not flags["quick"] then
+ fs.delete(sandbox)
+ end
+ repeat until not fs.pop_dir()
+ end end
+ if flags["quick"] then
+ fs.delete(sandbox)
+ end
+ restore_settings(settings)
+ util.title("Results:")
+ util.printout("OK: "..tostring(#results.ok))
+ for _, entry in ipairs(results.ok) do
+ util.printout(entry.file)
+ end
+ for errcode, errors in pairs(results) do
+ if errcode ~= "ok" then
+ util.printout()
+ util.printout(errcode.." errors: "..tostring(#errors))
+ for _, entry in ipairs(errors) do
+ util.printout(entry.file, entry.err)
+ end
+ end
+ end
+
+ util.title("Summary:")
+ local total = 0
+ for errcode, errors in pairs(results) do
+ util.printout(errcode..": "..tostring(#errors))
+ total = total + #errors
+ end
+ util.printout("Total: "..total)
+ return true
+end
+
+
+return validate
diff --git a/Utils/luarocks/lua/luarocks/write_rockspec.lua b/Utils/luarocks/lua/luarocks/write_rockspec.lua
new file mode 100644
index 000000000..33edeb1b4
--- /dev/null
+++ b/Utils/luarocks/lua/luarocks/write_rockspec.lua
@@ -0,0 +1,375 @@
+
+local write_rockspec = {}
+package.loaded["luarocks.write_rockspec"] = write_rockspec
+
+local cfg = require("luarocks.cfg")
+local dir = require("luarocks.dir")
+local fetch = require("luarocks.fetch")
+local fs = require("luarocks.fs")
+local path = require("luarocks.path")
+local persist = require("luarocks.persist")
+local type_check = require("luarocks.type_check")
+local util = require("luarocks.util")
+
+util.add_run_function(write_rockspec)
+write_rockspec.help_summary = "Write a template for a rockspec file."
+write_rockspec.help_arguments = "[--output= ...] [] [] [|]"
+write_rockspec.help = [[
+This command writes an initial version of a rockspec file,
+based on a name, a version, and a location (an URL or a local path).
+If only two arguments are given, the first one is considered the name and the
+second one is the location.
+If only one argument is given, it must be the location.
+If no arguments are given, current directory is used as location.
+LuaRocks will attempt to infer name and version if not given,
+using 'scm' as default version.
+
+Note that the generated file is a _starting point_ for writing a
+rockspec, and is not guaranteed to be complete or correct.
+
+--output= Write the rockspec with the given filename.
+ If not given, a file is written in the current
+ directory with a filename based on given name and version.
+--license="" A license string, such as "MIT/X11" or "GNU GPL v3".
+--summary="" A short one-line description summary.
+--detailed="" A longer description string.
+--homepage= Project homepage.
+--lua-version= Supported Lua versions. Accepted values are "5.1", "5.2",
+ "5.3", "5.1,5.2", "5.2,5.3", or "5.1,5.2,5.3".
+--rockspec-format= Rockspec format version, such as "1.0" or "1.1".
+--tag= Tag to use. Will attempt to extract version number from it.
+--lib=[,] A comma-separated list of libraries that C files need to
+ link to.
+]]
+
+local function open_file(name)
+ return io.open(dir.path(fs.current_dir(), name), "r")
+end
+
+local function get_url(rockspec)
+ local file, temp_dir, err_code, err_file, err_temp_dir = fetch.fetch_sources(rockspec, false)
+ if err_code == "source.dir" then
+ file, temp_dir = err_file, err_temp_dir
+ elseif not file then
+ util.warning("Could not fetch sources - "..temp_dir)
+ return false
+ end
+ util.printout("File successfully downloaded. Making checksum and checking base dir...")
+ if fetch.is_basic_protocol(rockspec.source.protocol) then
+ rockspec.source.md5 = fs.get_md5(file)
+ end
+ local inferred_dir, found_dir = fetch.find_base_dir(file, temp_dir, rockspec.source.url)
+ return true, found_dir or inferred_dir, temp_dir
+end
+
+local function configure_lua_version(rockspec, luaver)
+ if luaver == "5.1" then
+ table.insert(rockspec.dependencies, "lua ~> 5.1")
+ elseif luaver == "5.2" then
+ table.insert(rockspec.dependencies, "lua ~> 5.2")
+ elseif luaver == "5.3" then
+ table.insert(rockspec.dependencies, "lua ~> 5.3")
+ elseif luaver == "5.1,5.2" then
+ table.insert(rockspec.dependencies, "lua >= 5.1, < 5.3")
+ elseif luaver == "5.2,5.3" then
+ table.insert(rockspec.dependencies, "lua >= 5.2, < 5.4")
+ elseif luaver == "5.1,5.2,5.3" then
+ table.insert(rockspec.dependencies, "lua >= 5.1, < 5.4")
+ else
+ util.warning("Please specify supported Lua version with --lua-version=. "..util.see_help("write_rockspec"))
+ end
+end
+
+local function detect_description()
+ local fd = open_file("README.md") or open_file("README")
+ if not fd then return end
+ local data = fd:read("*a")
+ fd:close()
+ local paragraph = data:match("\n\n([^%[].-)\n\n")
+ if not paragraph then paragraph = data:match("\n\n(.*)") end
+ local summary, detailed
+ if paragraph then
+ detailed = paragraph
+
+ if #paragraph < 80 then
+ summary = paragraph:gsub("\n", "")
+ else
+ summary = paragraph:gsub("\n", " "):match("([^.]*%.) ")
+ end
+ end
+ return summary, detailed
+end
+
+local function detect_mit_license(data)
+ local strip_copyright = (data:gsub("Copyright [^\n]*\n", ""))
+ local sum = 0
+ for i = 1, #strip_copyright do
+ local num = string.byte(strip_copyright:sub(i,i))
+ if num > 32 and num <= 128 then
+ sum = sum + num
+ end
+ end
+ return sum == 78656
+end
+
+local simple_scm_protocols = {
+ git = true, ["git+http"] = true, ["git+https"] = true,
+ hg = true, ["hg+http"] = true, ["hg+https"] = true
+}
+
+local function detect_url_from_command(program, args, directory)
+ local command = fs.Q(cfg.variables[program:upper()]).. " "..args
+ local pipe = io.popen(fs.command_at(directory, fs.quiet_stderr(command)))
+ if not pipe then return nil end
+ local url = pipe:read("*a"):match("^([^\r\n]+)")
+ pipe:close()
+ if not url then return nil end
+ if not util.starts_with(url, program.."://") then
+ url = program.."+"..url
+ end
+
+ if simple_scm_protocols[dir.split_url(url)] then
+ return url
+ end
+end
+
+local function detect_scm_url(directory)
+ return detect_url_from_command("git", "config --get remote.origin.url", directory) or
+ detect_url_from_command("hg", "paths default", directory)
+end
+
+local function show_license(rockspec)
+ local fd = open_file("COPYING") or open_file("LICENSE") or open_file("MIT-LICENSE.txt")
+ if not fd then return nil end
+ local data = fd:read("*a")
+ fd:close()
+ local is_mit = detect_mit_license(data)
+ util.title("License for "..rockspec.package..":")
+ util.printout(data)
+ util.printout()
+ return is_mit
+end
+
+local function get_cmod_name(file)
+ local fd = open_file(file)
+ if not fd then return nil end
+ local data = fd:read("*a")
+ fd:close()
+ return (data:match("int%s+luaopen_([a-zA-Z0-9_]+)"))
+end
+
+local luamod_blacklist = {
+ test = true,
+ tests = true,
+}
+
+local function fill_as_builtin(rockspec, libs)
+ rockspec.build.type = "builtin"
+ rockspec.build.modules = {}
+ local prefix = ""
+
+ for _, parent in ipairs({"src", "lua"}) do
+ if fs.is_dir(parent) then
+ fs.change_dir(parent)
+ prefix = parent.."/"
+ break
+ end
+ end
+
+ local incdirs, libdirs
+ if libs then
+ incdirs, libdirs = {}, {}
+ for _, lib in ipairs(libs) do
+ local upper = lib:upper()
+ incdirs[#incdirs+1] = "$("..upper.."_INCDIR)"
+ libdirs[#libdirs+1] = "$("..upper.."_LIBDIR)"
+ end
+ end
+
+ for _, file in ipairs(fs.find()) do
+ local luamod = file:match("(.*)%.lua$")
+ if luamod and not luamod_blacklist[luamod] then
+ rockspec.build.modules[path.path_to_module(file)] = prefix..file
+ else
+ local cmod = file:match("(.*)%.c$")
+ if cmod then
+ local modname = get_cmod_name(file) or path.path_to_module(file:gsub("%.c$", ".lua"))
+ rockspec.build.modules[modname] = {
+ sources = prefix..file,
+ libraries = libs,
+ incdirs = incdirs,
+ libdirs = libdirs,
+ }
+ end
+ end
+ end
+
+ for _, directory in ipairs({ "doc", "docs", "samples", "tests" }) do
+ if fs.is_dir(directory) then
+ if not rockspec.build.copy_directories then
+ rockspec.build.copy_directories = {}
+ end
+ table.insert(rockspec.build.copy_directories, directory)
+ end
+ end
+
+ if prefix ~= "" then
+ fs.pop_dir()
+ end
+end
+
+local function rockspec_cleanup(rockspec)
+ rockspec.source.file = nil
+ rockspec.source.protocol = nil
+ rockspec.variables = nil
+ rockspec.name = nil
+end
+
+function write_rockspec.command(flags, name, version, url_or_dir)
+ if not name then
+ url_or_dir = "."
+ elseif not version then
+ url_or_dir = name
+ name = nil
+ elseif not url_or_dir then
+ url_or_dir = version
+ version = nil
+ end
+
+ if flags["tag"] then
+ if not version then
+ version = flags["tag"]:gsub("^v", "")
+ end
+ end
+
+ local protocol, pathname = dir.split_url(url_or_dir)
+ if protocol == "file" then
+ if pathname == "." then
+ name = name or dir.base_name(fs.current_dir())
+ end
+ elseif fetch.is_basic_protocol(protocol) then
+ local filename = dir.base_name(url_or_dir)
+ local newname, newversion = filename:match("(.*)-([^-]+)")
+ if newname then
+ name = name or newname
+ version = version or newversion:gsub("%.[a-z]+$", ""):gsub("%.tar$", "")
+ end
+ else
+ name = name or dir.base_name(url_or_dir):gsub("%.[^.]+$", "")
+ end
+
+ if not name then
+ return nil, "Could not infer rock name. "..util.see_help("write_rockspec")
+ end
+ version = version or "scm"
+
+ local filename = flags["output"] or dir.path(fs.current_dir(), name:lower().."-"..version.."-1.rockspec")
+
+ local rockspec = {
+ rockspec_format = flags["rockspec-format"],
+ package = name,
+ name = name:lower(),
+ version = version.."-1",
+ source = {
+ url = "*** please add URL for source tarball, zip or repository here ***",
+ tag = flags["tag"],
+ },
+ description = {
+ summary = flags["summary"] or "*** please specify description summary ***",
+ detailed = flags["detailed"] or "*** please enter a detailed description ***",
+ homepage = flags["homepage"] or "*** please enter a project homepage ***",
+ license = flags["license"] or "*** please specify a license ***",
+ },
+ dependencies = {},
+ build = {},
+ }
+ path.configure_paths(rockspec)
+ rockspec.source.protocol = protocol
+
+ configure_lua_version(rockspec, flags["lua-version"])
+
+ local local_dir = url_or_dir
+
+ if url_or_dir:match("://") then
+ rockspec.source.url = url_or_dir
+ rockspec.source.file = dir.base_name(url_or_dir)
+ rockspec.source.dir = "dummy"
+ if not fetch.is_basic_protocol(rockspec.source.protocol) then
+ if version ~= "scm" then
+ rockspec.source.tag = flags["tag"] or "v" .. version
+ end
+ end
+ rockspec.source.dir = nil
+ local ok, base_dir, temp_dir = get_url(rockspec)
+ if ok then
+ if base_dir ~= dir.base_name(url_or_dir) then
+ rockspec.source.dir = base_dir
+ end
+ end
+ if base_dir then
+ local_dir = dir.path(temp_dir, base_dir)
+ else
+ local_dir = nil
+ end
+ else
+ rockspec.source.url = detect_scm_url(local_dir) or rockspec.source.url
+ end
+
+ if not local_dir then
+ local_dir = "."
+ end
+
+ if not flags["homepage"] then
+ local url_protocol, url_path = dir.split_url(rockspec.source.url)
+
+ if simple_scm_protocols[url_protocol] then
+ for _, domain in ipairs({"github.com", "bitbucket.org", "gitlab.com"}) do
+ if util.starts_with(url_path, domain) then
+ rockspec.description.homepage = "https://"..url_path:gsub("%.git$", "")
+ break
+ end
+ end
+ end
+ end
+
+ local libs = nil
+ if flags["lib"] then
+ libs = {}
+ rockspec.external_dependencies = {}
+ for lib in flags["lib"]:gmatch("([^,]+)") do
+ table.insert(libs, lib)
+ rockspec.external_dependencies[lib:upper()] = {
+ library = lib
+ }
+ end
+ end
+
+ local ok, err = fs.change_dir(local_dir)
+ if not ok then return nil, "Failed reaching files from project - error entering directory "..local_dir end
+
+ if (not flags["summary"]) or (not flags["detailed"]) then
+ local summary, detailed = detect_description()
+ rockspec.description.summary = flags["summary"] or summary
+ rockspec.description.detailed = flags["detailed"] or detailed
+ end
+
+ local is_mit = show_license(rockspec)
+
+ if is_mit and not flags["license"] then
+ rockspec.description.license = "MIT"
+ end
+
+ fill_as_builtin(rockspec, libs)
+
+ rockspec_cleanup(rockspec)
+
+ persist.save_from_table(filename, rockspec, type_check.rockspec_order)
+
+ util.printout()
+ util.printout("Wrote template at "..filename.." -- you should now edit and finish it.")
+ util.printout()
+
+ return true
+end
+
+return write_rockspec
diff --git a/Utils/luarocks/lua5.1.dll b/Utils/luarocks/lua5.1.dll
new file mode 100644
index 000000000..b87f3b661
Binary files /dev/null and b/Utils/luarocks/lua5.1.dll differ
diff --git a/Utils/luarocks/lua5.1.exe b/Utils/luarocks/lua5.1.exe
new file mode 100644
index 000000000..dc1c2c35c
Binary files /dev/null and b/Utils/luarocks/lua5.1.exe differ
diff --git a/Utils/luarocks/lua5.1.lib b/Utils/luarocks/lua5.1.lib
new file mode 100644
index 000000000..df9882765
Binary files /dev/null and b/Utils/luarocks/lua5.1.lib differ
diff --git a/Utils/luarocks/lua51.dll b/Utils/luarocks/lua51.dll
new file mode 100644
index 000000000..27ab26542
Binary files /dev/null and b/Utils/luarocks/lua51.dll differ
diff --git a/Utils/luarocks/lua51.lib b/Utils/luarocks/lua51.lib
new file mode 100644
index 000000000..74bcf3fb4
Binary files /dev/null and b/Utils/luarocks/lua51.lib differ
diff --git a/Utils/luarocks/luac5.1.exe b/Utils/luarocks/luac5.1.exe
new file mode 100644
index 000000000..6f87524e2
Binary files /dev/null and b/Utils/luarocks/luac5.1.exe differ
diff --git a/Utils/luarocks/luarocks-admin.bat b/Utils/luarocks/luarocks-admin.bat
new file mode 100644
index 000000000..e7bd4d12b
--- /dev/null
+++ b/Utils/luarocks/luarocks-admin.bat
@@ -0,0 +1,43 @@
+@ECHO OFF
+SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
+
+SET "LUA_PATH=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH%"
+IF NOT "%LUA_PATH_5_2%"=="" (
+ SET "LUA_PATH_5_2=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH_5_2%"
+)
+IF NOT "%LUA_PATH_5_3%"=="" (
+ SET "LUA_PATH_5_3=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH_5_3%"
+)
+SET "PATH=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks;%PATH%"
+"C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua5.1" "C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\luarocks-admin.lua" %*
+SET EXITCODE=%ERRORLEVEL%
+IF NOT "%EXITCODE%"=="2" GOTO EXITLR
+
+REM Permission denied error, try and auto elevate...
+REM already an admin? (checking to prevent loops)
+NET SESSION >NUL 2>&1
+IF "%ERRORLEVEL%"=="0" GOTO EXITLR
+
+REM Do we have PowerShell available?
+PowerShell /? >NUL 2>&1
+IF NOT "%ERRORLEVEL%"=="0" GOTO EXITLR
+
+:GETTEMPNAME
+SET TMPFILE=%TEMP%\LuaRocks-Elevator-%RANDOM%.bat
+IF EXIST "%TMPFILE%" GOTO :GETTEMPNAME
+
+ECHO @ECHO OFF > "%TMPFILE%"
+ECHO CHDIR /D %CD% >> "%TMPFILE%"
+ECHO ECHO %0 %* >> "%TMPFILE%"
+ECHO ECHO. >> "%TMPFILE%"
+ECHO CALL %0 %* >> "%TMPFILE%"
+ECHO ECHO. >> "%TMPFILE%"
+ECHO ECHO Press any key to close this window... >> "%TMPFILE%"
+ECHO PAUSE ^> NUL >> "%TMPFILE%"
+ECHO DEL "%TMPFILE%" >> "%TMPFILE%"
+
+ECHO Now retrying as a privileged user...
+PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('%TMPFILE%', '', '', 'runas')
+
+:EXITLR
+exit /b %EXITCODE%
diff --git a/Utils/luarocks/luarocks-admin.lua b/Utils/luarocks/luarocks-admin.lua
new file mode 100644
index 000000000..2890d1f19
--- /dev/null
+++ b/Utils/luarocks/luarocks-admin.lua
@@ -0,0 +1,19 @@
+#!/usr/bin/env lua
+
+-- this should be loaded first.
+local cfg = require("luarocks.cfg")
+
+local loader = require("luarocks.loader")
+local command_line = require("luarocks.command_line")
+
+program_description = "LuaRocks repository administration interface"
+
+commands = {
+ help = "luarocks.help",
+ make_manifest = "luarocks.make_manifest",
+ add = "luarocks.add",
+ remove = "luarocks.admin_remove",
+ refresh_cache = "luarocks.refresh_cache",
+}
+
+command_line.run_command(...)
diff --git a/Utils/luarocks/luarocks.bat b/Utils/luarocks/luarocks.bat
new file mode 100644
index 000000000..373068966
--- /dev/null
+++ b/Utils/luarocks/luarocks.bat
@@ -0,0 +1,43 @@
+@ECHO OFF
+SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
+
+SET "LUA_PATH=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH%"
+IF NOT "%LUA_PATH_5_2%"=="" (
+ SET "LUA_PATH_5_2=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH_5_2%"
+)
+IF NOT "%LUA_PATH_5_3%"=="" (
+ SET "LUA_PATH_5_3=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua\?\init.lua;%LUA_PATH_5_3%"
+)
+SET "PATH=C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks;%PATH%"
+"C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\lua5.1" "C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\luarocks.lua" %*
+SET EXITCODE=%ERRORLEVEL%
+IF NOT "%EXITCODE%"=="2" GOTO EXITLR
+
+REM Permission denied error, try and auto elevate...
+REM already an admin? (checking to prevent loops)
+NET SESSION >NUL 2>&1
+IF "%ERRORLEVEL%"=="0" GOTO EXITLR
+
+REM Do we have PowerShell available?
+PowerShell /? >NUL 2>&1
+IF NOT "%ERRORLEVEL%"=="0" GOTO EXITLR
+
+:GETTEMPNAME
+SET TMPFILE=%TEMP%\LuaRocks-Elevator-%RANDOM%.bat
+IF EXIST "%TMPFILE%" GOTO :GETTEMPNAME
+
+ECHO @ECHO OFF > "%TMPFILE%"
+ECHO CHDIR /D %CD% >> "%TMPFILE%"
+ECHO ECHO %0 %* >> "%TMPFILE%"
+ECHO ECHO. >> "%TMPFILE%"
+ECHO CALL %0 %* >> "%TMPFILE%"
+ECHO ECHO. >> "%TMPFILE%"
+ECHO ECHO Press any key to close this window... >> "%TMPFILE%"
+ECHO PAUSE ^> NUL >> "%TMPFILE%"
+ECHO DEL "%TMPFILE%" >> "%TMPFILE%"
+
+ECHO Now retrying as a privileged user...
+PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('%TMPFILE%', '', '', 'runas')
+
+:EXITLR
+exit /b %EXITCODE%
diff --git a/Utils/luarocks/luarocks.lua b/Utils/luarocks/luarocks.lua
new file mode 100644
index 000000000..be6c2b811
--- /dev/null
+++ b/Utils/luarocks/luarocks.lua
@@ -0,0 +1,33 @@
+#!/usr/bin/env lua
+
+-- this should be loaded first.
+local cfg = require("luarocks.cfg")
+
+local loader = require("luarocks.loader")
+local command_line = require("luarocks.command_line")
+
+program_description = "LuaRocks main command-line interface"
+
+commands = {
+ help = "luarocks.help",
+ pack = "luarocks.pack",
+ unpack = "luarocks.unpack",
+ build = "luarocks.build",
+ install = "luarocks.install",
+ search = "luarocks.search",
+ list = "luarocks.list",
+ remove = "luarocks.remove",
+ make = "luarocks.make",
+ download = "luarocks.download",
+ path = "luarocks.path_cmd",
+ show = "luarocks.show",
+ new_version = "luarocks.new_version",
+ lint = "luarocks.lint",
+ write_rockspec = "luarocks.write_rockspec",
+ purge = "luarocks.purge",
+ doc = "luarocks.doc",
+ upload = "luarocks.upload",
+ config = "luarocks.config_cmd",
+}
+
+command_line.run_command(...)
diff --git a/Utils/luarocks/luarocksw.bat b/Utils/luarocks/luarocksw.bat
new file mode 100644
index 000000000..8ac029209
--- /dev/null
+++ b/Utils/luarocks/luarocksw.bat
@@ -0,0 +1,49 @@
+@echo off
+setlocal
+SET MYPATH=%~dp0
+
+IF NOT [%1]==[] GOTO LETSGO
+ECHO Same as 'luarocks' command, except this
+ECHO command will pause after completion, allowing for
+ECHO examination of output.
+ECHO.
+ECHO For LuaRocks help use:
+ECHO LUAROCKS HELP
+ECHO.
+ECHO OPTIONS specific for LUAROCKSW:
+ECHO REMOVEALL is a command specific to this batch file
+ECHO the option takes a FULL ROCKSPEC filename and then
+ECHO it will strip path, version and extension info from
+ECHO it before executing the LUAROCKS REMOVE command
+ECHO Example:
+ECHO luarocksw remove "c:\somedir\modulename-1.0-1.rockspec"
+ECHO will execute:
+ECHO luarocks remove "c:\somedir\modulename-1.0-1.rockspec"
+ECHO and will only remove the specific version 1.0 from the
+ECHO system.
+ECHO luarocksw removeall "c:\somedir\modulename-1.0-1.rockspec"
+ECHO will execute:
+ECHO luarocks remove modulename
+ECHO and will remove all versions of this package
+ECHO.
+GOTO END
+
+:LETSGO
+REM if REMOVEALL command then info must be stripped from the parameter
+if [%1]==[removeall] goto REMOVEALL
+
+REM execute LuaRocks and wait for results
+echo executing: luarocks %*
+call "%MYPATH%luarocks" %*
+pause
+goto END
+
+:REMOVEALL
+for /f "delims=-" %%a in ("%~n2") do (
+ echo executing: luarocks remove %%a
+ "%MYPATH%luarocks" remove "%%a"
+ pause
+ goto END
+)
+
+:END
\ No newline at end of file
diff --git a/Utils/luarocks/msvcm80.dll b/Utils/luarocks/msvcm80.dll
new file mode 100644
index 000000000..c751385bd
Binary files /dev/null and b/Utils/luarocks/msvcm80.dll differ
diff --git a/Utils/luarocks/msvcp80.dll b/Utils/luarocks/msvcp80.dll
new file mode 100644
index 000000000..f0b52ebf1
Binary files /dev/null and b/Utils/luarocks/msvcp80.dll differ
diff --git a/Utils/luarocks/msvcr80.dll b/Utils/luarocks/msvcr80.dll
new file mode 100644
index 000000000..53c005efc
Binary files /dev/null and b/Utils/luarocks/msvcr80.dll differ
diff --git a/Utils/luarocks/rclauncher.c b/Utils/luarocks/rclauncher.c
new file mode 100644
index 000000000..77459f464
--- /dev/null
+++ b/Utils/luarocks/rclauncher.c
@@ -0,0 +1,139 @@
+
+/*
+** Simple Lua interpreter.
+** This program is used to run a Lua file embedded as a resource.
+** It creates a Lua state, opens all its standard libraries, and run
+** the Lua file in a protected environment just to redirect the error
+** messages to stdout and stderr.
+**
+** $Id: rclauncher.c,v 1.1 2008/06/30 14:29:59 carregal Exp $
+*/
+
+#include
+#include
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include
+#include
+#include
+
+/*
+** Report error message.
+** Assumes that the error message is on top of the stack.
+*/
+static int report (lua_State *L) {
+ fprintf (stderr, "lua: fatal error: `%s'\n", lua_tostring (L, -1));
+ fflush (stderr);
+ printf ("Content-type: text/plain\n\nConfiguration fatal error: see error log!\n");
+ printf ("%s", lua_tostring(L, -1));
+ return 1;
+}
+
+static int runlua (lua_State *L, const char *lua_string, int argc, char *argv[]) {
+ int err_func;
+ int err;
+
+ lua_getglobal(L, "debug");
+ lua_pushliteral(L, "traceback");
+ lua_gettable(L, -2);
+ err_func = lua_gettop (L);
+ err = luaL_loadstring (L, lua_string);
+ if(!err) {
+ int i;
+ // fill global arg table
+ lua_getglobal(L, "arg");
+ for(i = 1; i < argc; i++)
+ {
+ lua_pushstring(L, argv[i]);
+ lua_rawseti(L, -2, i);
+ }
+ lua_pop(L, 1);
+ // fill parameters (in vararg '...')
+ for(i = 1; i < argc; i++)
+ lua_pushstring(L, argv[i]);
+ return lua_pcall (L, argc - 1, LUA_MULTRET, err_func);
+ } else return err;
+}
+
+static DWORD GetModulePath( HINSTANCE hInst, LPTSTR pszBuffer, DWORD dwSize )
+//
+// Return the size of the path in bytes.
+{
+ DWORD dwLength = GetModuleFileName( hInst, pszBuffer, dwSize );
+ if( dwLength )
+ {
+ while( dwLength && pszBuffer[ dwLength ] != '.' )
+ {
+ dwLength--;
+ }
+
+ if( dwLength )
+ pszBuffer[ dwLength ] = '\000';
+ }
+ return dwLength;
+}
+
+
+/*
+** MAIN
+*/
+int main (int argc, char *argv[]) {
+ char name[ MAX_PATH ];
+ DWORD dwLength;
+ int size;
+ luaL_Buffer b;
+ int i;
+#ifdef UNICODE
+ TCHAR lua_wstring[4098];
+#endif
+ char lua_string[4098];
+ lua_State *L = luaL_newstate();
+ (void)argc; /* avoid "unused parameter" warning */
+ luaL_openlibs(L);
+ lua_newtable(L); // create arg table
+ lua_pushstring(L, argv[0]); // add interpreter to arg table
+ lua_rawseti(L, -2, -1);
+ dwLength = GetModulePath( NULL, name, MAX_PATH );
+ if(dwLength) { /* Optional bootstrap */
+ strcat(name, ".lua");
+ lua_pushstring(L, name); // add lua script to arg table
+ lua_rawseti(L, -2, 0);
+ lua_setglobal(L,"arg"); // set global arg table
+ if(!luaL_loadfile (L, name)) {
+ if(lua_pcall (L, 0, LUA_MULTRET, 0)) {
+ report (L);
+ lua_close (L);
+ return EXIT_FAILURE;
+ }
+ }
+ }
+ else
+ {
+ lua_pushstring(L, argv[0]); // no lua script, so add interpreter again, now as lua script
+ lua_rawseti(L, -2, 0);
+ lua_setglobal(L,"arg"); // set global arg table
+ }
+
+ luaL_buffinit(L, &b);
+ for(i = 1; ; i++) {
+#ifdef UNICODE
+ size = LoadString(GetModuleHandle(NULL), i, lua_wstring,
+ sizeof(lua_string)/sizeof(TCHAR));
+ if(size > 0) wcstombs(lua_string, lua_wstring, size + 1);
+#else
+ size = LoadString(GetModuleHandle(NULL), i, lua_string,
+ sizeof(lua_string)/sizeof(char));
+#endif
+ if(size) luaL_addlstring(&b, lua_string, size); else break;
+ }
+ luaL_pushresult(&b);
+ if (runlua (L, lua_tostring(L, -1), argc, argv)) {
+ report (L);
+ lua_close (L);
+ return EXIT_FAILURE;
+ }
+ lua_close (L);
+ return EXIT_SUCCESS;
+}
diff --git a/Utils/luarocks/systree/bin/luadocumentor.bat b/Utils/luarocks/systree/bin/luadocumentor.bat
new file mode 100644
index 000000000..e16f6f7e9
--- /dev/null
+++ b/Utils/luarocks/systree/bin/luadocumentor.bat
@@ -0,0 +1,3 @@
+@echo off
+"C:\Users\Hugues\Documents\GitHub\MOOSE\Utils\luarocks\lua5.1" -e "package.path=\"C:\\Users\\Hugues\\AppData\\Roaming/luarocks/share/lua/5.1/?.lua;C:\\Users\\Hugues\\AppData\\Roaming/luarocks/share/lua/5.1/?/init.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\\systree/share/lua/5.1/?.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\\systree/share/lua/5.1/?/init.lua;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks/lua/?.lua;\"..package.path; package.cpath=\"C:\\Users\\Hugues\\AppData\\Roaming/luarocks/lib/lua/5.1/?.dll;C:/Users/Hugues/Documents/GitHub/MOOSE/Utils/luarocks\\systree/lib/lua/5.1/?.dll;\"..package.cpath" -e "local k,l,_=pcall(require,\"luarocks.loader\") _=k and l.add_context(\"luadocumentor\",\"0.1.5-1\")" "C:\Users\Hugues\Documents\GitHub\MOOSE\Utils\luarocks\systree\lib\luarocks\rocks\luadocumentor\0.1.5-1\bin\luadocumentor" %*
+exit /b %ERRORLEVEL%
diff --git a/Utils/luarocks/systree/lib/lua/5.1/checks.dll b/Utils/luarocks/systree/lib/lua/5.1/checks.dll
new file mode 100644
index 000000000..9959cdc01
Binary files /dev/null and b/Utils/luarocks/systree/lib/lua/5.1/checks.dll differ
diff --git a/Utils/luarocks/systree/lib/lua/5.1/lfs.dll b/Utils/luarocks/systree/lib/lua/5.1/lfs.dll
new file mode 100644
index 000000000..9249ec0eb
Binary files /dev/null and b/Utils/luarocks/systree/lib/lua/5.1/lfs.dll differ
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/checks/1.0-1/checks-1.0-1.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/checks/1.0-1/checks-1.0-1.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/checks/1.0-1/checks-1.0-1.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/checks/1.0-1/checks-1.0-1.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/checks/1.0-1/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/checks/1.0-1/rock_manifest
similarity index 63%
rename from Utils/lua/5.1/lib/luarocks/rocks/checks/1.0-1/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/checks/1.0-1/rock_manifest
index 07ae2133a..b45de952e 100644
--- a/Utils/lua/5.1/lib/luarocks/rocks/checks/1.0-1/rock_manifest
+++ b/Utils/luarocks/systree/lib/luarocks/rocks/checks/1.0-1/rock_manifest
@@ -1,6 +1,6 @@
rock_manifest = {
["checks-1.0-1.rockspec"] = "70af2f5dd173774a16b9e7cfdfd12ecd",
lib = {
- ["checks.dll"] = "5342726d76176ca95ebe25c7b07ca6ac"
+ ["checks.dll"] = "2f86fd2ab34447e9e211836fd56939ae"
}
}
diff --git a/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/bin/luadocumentor b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/bin/luadocumentor
new file mode 100644
index 000000000..dccc8df18
--- /dev/null
+++ b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/bin/luadocumentor
@@ -0,0 +1,178 @@
+#!/usr/bin/lua
+--------------------------------------------------------------------------------
+-- Copyright (c) 2012-2014 Sierra Wireless.
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+-- Kevin KIN-FOO
+-- - initial API and implementation and initial documentation
+--------------------------------------------------------------------------------
+
+-- Check interpreter version
+if _VERSION ~= "Lua 5.1" then
+ print("Luadocumentor is only compatible with Lua 5.1")
+ return
+end
+
+--
+-- Defining help message.
+--
+
+-- This message is compliant to 'lapp', which will match options and arguments
+-- from command line.
+local help = [[luadocumentor v0.1.4: tool for Lua Documentation Language
+ -f, --format (default doc) Define output format :
+ * doc: Will produce HTML documentation from specified file(s) or directories.
+ * api: Will produce API file(s) from specified file(s) or directories.
+ -d, --dir (default docs) Define an output directory. If the given directory doesn't exist, it will be created.
+ -h, --help Display the help.
+ -n, --noheuristic Do not use code analysis, use only comments to generate documentation.
+ -s, --style (default !) The path of your own css file, if you don't want to use the default one. (usefull only for the doc format)
+ [directories|files] Define the paths or the directories of inputs files. Only Lua or C files containing a @module tag will be considered.
+]]
+local docgenerator = require 'docgenerator'
+local lddextractor = require 'lddextractor'
+local lapp = require 'pl.lapp'
+local args = lapp( help )
+
+if not args or #args < 1 then
+ print('No directory provided')
+ return
+elseif args.help then
+ -- Just print help
+ print( help )
+ return
+end
+
+--
+-- define css file name
+--
+local cssfilename = "stylesheet.css"
+
+--
+-- Parse files from given folders
+--
+
+-- Check if all folders exist
+local fs = require 'fs.lfs'
+local allpresent, missing = fs.checkdirectory(args)
+
+-- Some of given directories are absent
+if missing then
+ -- List missing directories
+ print 'Unable to open'
+ for _, file in ipairs( missing ) do
+ print('\t'.. file)
+ end
+ return
+end
+
+-- Get files from given directories
+local filestoparse, error = fs.filelist( args )
+if not filestoparse then
+ print ( error )
+ return
+end
+
+--
+-- Generate documentation only files
+--
+if args.format == 'api' then
+ for _, filename in ipairs( filestoparse ) do
+
+ -- Loading file content
+ print('Dealing with "'..filename..'".')
+ local file, error = io.open(filename, 'r')
+ if not file then
+ print ('Unable to open "'..filename.."'.\n"..error)
+ else
+ local code = file:read('*all')
+ file:close()
+
+ --
+ -- Creating comment file
+ --
+ local commentfile, error = lddextractor.generatecommentfile(filename, code)
+
+ -- Getting module name
+ -- Optimize me
+ local module, moduleerror = lddextractor.generateapimodule(filename, code)
+ if not commentfile then
+ print('Unable to create documentation file for "'..filename..'"\n'..error)
+ elseif not module or not module.name then
+ local error = moduleerror and '\n'..moduleerror or ''
+ print('Unable to compute module name for "'..filename..'".'..error)
+ else
+ --
+ -- Flush documentation file on disk
+ --
+ local path = args.dir..fs.separator..module.name..'.lua'
+ local status, err = fs.fill(path, commentfile)
+ if not status then
+ print(err)
+ end
+ end
+ end
+ end
+ print('Done')
+ return
+end
+
+-- Deal only supported output types
+if args.format ~= 'doc' then
+ print ('"'..args.format..'" format is not handled.')
+ return
+end
+-- Generate html form files
+local parsedfiles, unparsed = docgenerator.generatedocforfiles(filestoparse, cssfilename,args.noheuristic)
+
+-- Show warnings on unparsed files
+if #unparsed > 0 then
+ for _, faultyfile in ipairs( unparsed ) do
+ print( faultyfile )
+ end
+end
+-- This loop is just for counting parsed files
+-- TODO: Find a more elegant way to do it
+local parsedfilescount = 0
+for _, p in pairs(parsedfiles) do
+ parsedfilescount = parsedfilescount + 1
+end
+print (parsedfilescount .. ' file(s) parsed.')
+
+-- Create html files
+local generated = 0
+for _, apifile in pairs ( parsedfiles ) do
+ local status, err = fs.fill(args.dir..fs.separator..apifile.name..'.html', apifile.body)
+ if status then
+ generated = generated + 1
+ else
+ print( 'Unable to create '..apifile.name..'.html on disk.')
+ end
+end
+print (generated .. ' file(s) generated.')
+
+-- Copying css
+local csscontent
+if args.style == '!' then
+ csscontent = require 'defaultcss'
+else
+ local css, error = io.open(args.style, 'r')
+ if not css then
+ print('Unable to open "'..args.style .. '".\n'..error)
+ return
+ end
+ csscontent = css:read("*all")
+ css:close()
+end
+
+local status, error = fs.fill(args.dir..fs.separator..cssfilename, csscontent)
+if not status then
+ print(error)
+ return
+end
+print('Adding css')
+print('Done')
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/LICENSE b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/LICENSE
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/LICENSE
rename to Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/LICENSE
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/README.md b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/README.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/README.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/doc/README.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/luadocumentor-0.1.5-1.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/luadocumentor-0.1.5-1.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/luadocumentor-0.1.5-1.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/luadocumentor-0.1.5-1.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/rock_manifest
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luadocumentor/0.1.5-1/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/luadocumentor/0.1.5-1/rock_manifest
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/doc.css b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/doc.css
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/doc.css
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/doc.css
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/examples.html b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/examples.html
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/examples.html
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/examples.html
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/index.html b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/index.html
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/index.html
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/index.html
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/license.html b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/license.html
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/license.html
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/license.html
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/luafilesystem.png b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/luafilesystem.png
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/luafilesystem.png
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/luafilesystem.png
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/manual.html b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/manual.html
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/manual.html
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/doc/us/manual.html
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/luafilesystem-1.6.3-2.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/luafilesystem-1.6.3-2.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/luafilesystem-1.6.3-2.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/luafilesystem-1.6.3-2.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest
similarity index 91%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest
index b0f7b6332..1b646b631 100644
--- a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest
+++ b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/rock_manifest
@@ -10,7 +10,7 @@ rock_manifest = {
}
},
lib = {
- ["lfs.dll"] = "165694685cffa6014be4ca8cbb7d920b"
+ ["lfs.dll"] = "bb5e7f0d82350913063363393fafb3e3"
},
["luafilesystem-1.6.3-2.rockspec"] = "eb0ef7c190516892eb8357af799eea5f",
tests = {
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/tests/test.lua b/Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/tests/test.lua
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/luafilesystem/1.6.3-2/tests/test.lua
rename to Utils/luarocks/systree/lib/luarocks/rocks/luafilesystem/1.6.3-2/tests/test.lua
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/manifest b/Utils/luarocks/systree/lib/luarocks/rocks/manifest
similarity index 99%
rename from Utils/lua/5.1/lib/luarocks/rocks/manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/manifest
index 3b022bd4f..b3bae3c19 100644
--- a/Utils/lua/5.1/lib/luarocks/rocks/manifest
+++ b/Utils/luarocks/systree/lib/luarocks/rocks/manifest
@@ -31,7 +31,8 @@ dependencies = {
}
},
name = "lua"
- }, {
+ },
+ {
constraints = {
{
op = "~>",
@@ -41,7 +42,8 @@ dependencies = {
}
},
name = "luafilesystem"
- }, {
+ },
+ {
constraints = {
{
op = "~>",
@@ -51,7 +53,8 @@ dependencies = {
}
},
name = "markdown"
- }, {
+ },
+ {
constraints = {
{
op = "~>",
@@ -61,7 +64,8 @@ dependencies = {
}
},
name = "metalua-compiler"
- }, {
+ },
+ {
constraints = {
{
op = "~>",
@@ -116,7 +120,8 @@ dependencies = {
}
},
name = "lua"
- }, {
+ },
+ {
constraints = {
{
op = "~>",
@@ -126,7 +131,8 @@ dependencies = {
}
},
name = "luafilesystem"
- }, {
+ },
+ {
constraints = {
{
op = ">=",
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/markdown/0.32-2/markdown-0.32-2.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/markdown/0.32-2/markdown-0.32-2.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/markdown/0.32-2/markdown-0.32-2.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/markdown/0.32-2/markdown-0.32-2.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/markdown/0.32-2/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/markdown/0.32-2/rock_manifest
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/markdown/0.32-2/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/markdown/0.32-2/rock_manifest
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-compiler.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-compiler.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-compiler.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-compiler.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-parser.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-parser.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-parser.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README-parser.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/doc/README.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/metalua-compiler-0.7.3-1.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/metalua-compiler-0.7.3-1.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/metalua-compiler-0.7.3-1.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/metalua-compiler-0.7.3-1.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/rock_manifest
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-compiler/0.7.3-1/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-compiler/0.7.3-1/rock_manifest
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-compiler.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-compiler.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-compiler.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-compiler.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-parser.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-parser.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-parser.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README-parser.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README.md b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README.md
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README.md
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/doc/README.md
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/metalua-parser-0.7.3-2.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/metalua-parser-0.7.3-2.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/metalua-parser-0.7.3-2.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/metalua-parser-0.7.3-2.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/rock_manifest
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/metalua-parser/0.7.3-2/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/metalua-parser/0.7.3-2/rock_manifest
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/penlight/0.9.8-1/penlight-0.9.8-1.rockspec b/Utils/luarocks/systree/lib/luarocks/rocks/penlight/0.9.8-1/penlight-0.9.8-1.rockspec
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/penlight/0.9.8-1/penlight-0.9.8-1.rockspec
rename to Utils/luarocks/systree/lib/luarocks/rocks/penlight/0.9.8-1/penlight-0.9.8-1.rockspec
diff --git a/Utils/lua/5.1/lib/luarocks/rocks/penlight/0.9.8-1/rock_manifest b/Utils/luarocks/systree/lib/luarocks/rocks/penlight/0.9.8-1/rock_manifest
similarity index 100%
rename from Utils/lua/5.1/lib/luarocks/rocks/penlight/0.9.8-1/rock_manifest
rename to Utils/luarocks/systree/lib/luarocks/rocks/penlight/0.9.8-1/rock_manifest
diff --git a/Utils/lua/5.1/share/lua/5.1/defaultcss.lua b/Utils/luarocks/systree/share/lua/5.1/defaultcss.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/defaultcss.lua
rename to Utils/luarocks/systree/share/lua/5.1/defaultcss.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/docgenerator.lua b/Utils/luarocks/systree/share/lua/5.1/docgenerator.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/docgenerator.lua
rename to Utils/luarocks/systree/share/lua/5.1/docgenerator.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/extractors.lua b/Utils/luarocks/systree/share/lua/5.1/extractors.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/extractors.lua
rename to Utils/luarocks/systree/share/lua/5.1/extractors.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/fs/lfs.lua b/Utils/luarocks/systree/share/lua/5.1/fs/lfs.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/fs/lfs.lua
rename to Utils/luarocks/systree/share/lua/5.1/fs/lfs.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/lddextractor.lua b/Utils/luarocks/systree/share/lua/5.1/lddextractor.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/lddextractor.lua
rename to Utils/luarocks/systree/share/lua/5.1/lddextractor.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/markdown.lua b/Utils/luarocks/systree/share/lua/5.1/markdown.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/markdown.lua
rename to Utils/luarocks/systree/share/lua/5.1/markdown.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/ast_to_src.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/ast_to_src.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/ast_to_src.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/ast_to_src.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/compile.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/compile.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/compile.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/compile.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/lcode.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/lcode.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/lcode.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/lcode.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/ldump.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/ldump.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/ldump.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/ldump.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/lopcodes.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/lopcodes.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/bytecode/lopcodes.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/bytecode/lopcodes.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/globals.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/globals.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/globals.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/globals.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/annot/generator.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/annot/generator.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/annot/generator.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/annot/generator.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/annot/grammar.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/annot/grammar.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/annot/grammar.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/annot/grammar.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/expr.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/expr.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/expr.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/expr.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/ext.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/ext.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/ext.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/ext.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/lexer.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/lexer.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/lexer.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/lexer.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/meta.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/meta.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/meta.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/meta.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/misc.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/misc.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/misc.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/misc.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/stat.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/stat.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/stat.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/stat.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/table.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/table.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/compiler/parser/table.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/compiler/parser/table.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/extension/comprehension.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/extension/comprehension.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/extension/comprehension.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/extension/comprehension.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/extension/match.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/extension/match.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/extension/match.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/extension/match.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/grammar/generator.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/grammar/generator.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/grammar/generator.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/grammar/generator.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/grammar/lexer.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/grammar/lexer.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/grammar/lexer.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/grammar/lexer.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/loader.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/loader.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/loader.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/loader.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/pprint.lua b/Utils/luarocks/systree/share/lua/5.1/metalua/pprint.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/pprint.lua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/pprint.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/repl.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/repl.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/repl.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/repl.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/treequery.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/treequery.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/treequery.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/treequery.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/metalua/treequery/walk.mlua b/Utils/luarocks/systree/share/lua/5.1/metalua/treequery/walk.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/metalua/treequery/walk.mlua
rename to Utils/luarocks/systree/share/lua/5.1/metalua/treequery/walk.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/models/apimodel.lua b/Utils/luarocks/systree/share/lua/5.1/models/apimodel.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/models/apimodel.lua
rename to Utils/luarocks/systree/share/lua/5.1/models/apimodel.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/models/apimodelbuilder.lua b/Utils/luarocks/systree/share/lua/5.1/models/apimodelbuilder.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/models/apimodelbuilder.lua
rename to Utils/luarocks/systree/share/lua/5.1/models/apimodelbuilder.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/models/internalmodel.lua b/Utils/luarocks/systree/share/lua/5.1/models/internalmodel.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/models/internalmodel.lua
rename to Utils/luarocks/systree/share/lua/5.1/models/internalmodel.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/models/internalmodelbuilder.mlua b/Utils/luarocks/systree/share/lua/5.1/models/internalmodelbuilder.mlua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/models/internalmodelbuilder.mlua
rename to Utils/luarocks/systree/share/lua/5.1/models/internalmodelbuilder.mlua
diff --git a/Utils/lua/5.1/share/lua/5.1/models/ldparser.lua b/Utils/luarocks/systree/share/lua/5.1/models/ldparser.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/models/ldparser.lua
rename to Utils/luarocks/systree/share/lua/5.1/models/ldparser.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/Date.lua b/Utils/luarocks/systree/share/lua/5.1/pl/Date.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/Date.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/Date.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/List.lua b/Utils/luarocks/systree/share/lua/5.1/pl/List.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/List.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/List.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/Map.lua b/Utils/luarocks/systree/share/lua/5.1/pl/Map.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/Map.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/Map.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/MultiMap.lua b/Utils/luarocks/systree/share/lua/5.1/pl/MultiMap.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/MultiMap.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/MultiMap.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/OrderedMap.lua b/Utils/luarocks/systree/share/lua/5.1/pl/OrderedMap.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/OrderedMap.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/OrderedMap.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/Set.lua b/Utils/luarocks/systree/share/lua/5.1/pl/Set.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/Set.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/Set.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/app.lua b/Utils/luarocks/systree/share/lua/5.1/pl/app.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/app.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/app.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/array2d.lua b/Utils/luarocks/systree/share/lua/5.1/pl/array2d.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/array2d.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/array2d.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/class.lua b/Utils/luarocks/systree/share/lua/5.1/pl/class.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/class.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/class.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/comprehension.lua b/Utils/luarocks/systree/share/lua/5.1/pl/comprehension.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/comprehension.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/comprehension.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/config.lua b/Utils/luarocks/systree/share/lua/5.1/pl/config.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/config.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/config.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/data.lua b/Utils/luarocks/systree/share/lua/5.1/pl/data.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/data.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/data.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/dir.lua b/Utils/luarocks/systree/share/lua/5.1/pl/dir.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/dir.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/dir.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/file.lua b/Utils/luarocks/systree/share/lua/5.1/pl/file.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/file.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/file.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/func.lua b/Utils/luarocks/systree/share/lua/5.1/pl/func.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/func.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/func.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/init.lua b/Utils/luarocks/systree/share/lua/5.1/pl/init.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/init.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/init.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/input.lua b/Utils/luarocks/systree/share/lua/5.1/pl/input.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/input.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/input.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/lapp.lua b/Utils/luarocks/systree/share/lua/5.1/pl/lapp.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/lapp.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/lapp.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/lexer.lua b/Utils/luarocks/systree/share/lua/5.1/pl/lexer.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/lexer.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/lexer.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/luabalanced.lua b/Utils/luarocks/systree/share/lua/5.1/pl/luabalanced.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/luabalanced.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/luabalanced.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/operator.lua b/Utils/luarocks/systree/share/lua/5.1/pl/operator.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/operator.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/operator.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/path.lua b/Utils/luarocks/systree/share/lua/5.1/pl/path.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/path.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/path.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/permute.lua b/Utils/luarocks/systree/share/lua/5.1/pl/permute.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/permute.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/permute.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/platf/luajava.lua b/Utils/luarocks/systree/share/lua/5.1/pl/platf/luajava.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/platf/luajava.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/platf/luajava.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/pretty.lua b/Utils/luarocks/systree/share/lua/5.1/pl/pretty.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/pretty.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/pretty.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/seq.lua b/Utils/luarocks/systree/share/lua/5.1/pl/seq.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/seq.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/seq.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/sip.lua b/Utils/luarocks/systree/share/lua/5.1/pl/sip.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/sip.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/sip.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/strict.lua b/Utils/luarocks/systree/share/lua/5.1/pl/strict.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/strict.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/strict.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/stringio.lua b/Utils/luarocks/systree/share/lua/5.1/pl/stringio.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/stringio.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/stringio.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/stringx.lua b/Utils/luarocks/systree/share/lua/5.1/pl/stringx.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/stringx.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/stringx.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/tablex.lua b/Utils/luarocks/systree/share/lua/5.1/pl/tablex.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/tablex.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/tablex.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/template.lua b/Utils/luarocks/systree/share/lua/5.1/pl/template.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/template.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/template.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/test.lua b/Utils/luarocks/systree/share/lua/5.1/pl/test.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/test.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/test.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/text.lua b/Utils/luarocks/systree/share/lua/5.1/pl/text.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/text.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/text.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/utils.lua b/Utils/luarocks/systree/share/lua/5.1/pl/utils.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/utils.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/utils.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/pl/xml.lua b/Utils/luarocks/systree/share/lua/5.1/pl/xml.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/pl/xml.lua
rename to Utils/luarocks/systree/share/lua/5.1/pl/xml.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/file.lua b/Utils/luarocks/systree/share/lua/5.1/template/file.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/file.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/file.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/index.lua b/Utils/luarocks/systree/share/lua/5.1/template/index.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/index.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/index.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/index/recordtypedef.lua b/Utils/luarocks/systree/share/lua/5.1/template/index/recordtypedef.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/index/recordtypedef.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/index/recordtypedef.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/item.lua b/Utils/luarocks/systree/share/lua/5.1/template/item.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/item.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/item.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/page.lua b/Utils/luarocks/systree/share/lua/5.1/template/page.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/page.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/page.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/recordtypedef.lua b/Utils/luarocks/systree/share/lua/5.1/template/recordtypedef.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/recordtypedef.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/recordtypedef.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/usage.lua b/Utils/luarocks/systree/share/lua/5.1/template/usage.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/usage.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/usage.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/template/utils.lua b/Utils/luarocks/systree/share/lua/5.1/template/utils.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/template/utils.lua
rename to Utils/luarocks/systree/share/lua/5.1/template/utils.lua
diff --git a/Utils/lua/5.1/share/lua/5.1/templateengine.lua b/Utils/luarocks/systree/share/lua/5.1/templateengine.lua
similarity index 100%
rename from Utils/lua/5.1/share/lua/5.1/templateengine.lua
rename to Utils/luarocks/systree/share/lua/5.1/templateengine.lua
diff --git a/Utils/luarocks/tools/7z.dll b/Utils/luarocks/tools/7z.dll
new file mode 100644
index 000000000..c0ff7fbe6
Binary files /dev/null and b/Utils/luarocks/tools/7z.dll differ
diff --git a/Utils/luarocks/tools/7z.exe b/Utils/luarocks/tools/7z.exe
new file mode 100644
index 000000000..5e3d6f9cf
Binary files /dev/null and b/Utils/luarocks/tools/7z.exe differ
diff --git a/Utils/luarocks/tools/cp.exe b/Utils/luarocks/tools/cp.exe
new file mode 100644
index 000000000..0ef4fe85e
Binary files /dev/null and b/Utils/luarocks/tools/cp.exe differ
diff --git a/Utils/luarocks/tools/find.exe b/Utils/luarocks/tools/find.exe
new file mode 100644
index 000000000..85192fbf4
Binary files /dev/null and b/Utils/luarocks/tools/find.exe differ
diff --git a/Utils/luarocks/tools/libeay32.dll b/Utils/luarocks/tools/libeay32.dll
new file mode 100644
index 000000000..8d31f8668
Binary files /dev/null and b/Utils/luarocks/tools/libeay32.dll differ
diff --git a/Utils/luarocks/tools/libiconv2.dll b/Utils/luarocks/tools/libiconv2.dll
new file mode 100644
index 000000000..544dd92f5
Binary files /dev/null and b/Utils/luarocks/tools/libiconv2.dll differ
diff --git a/Utils/luarocks/tools/libintl3.dll b/Utils/luarocks/tools/libintl3.dll
new file mode 100644
index 000000000..ec11e6b19
Binary files /dev/null and b/Utils/luarocks/tools/libintl3.dll differ
diff --git a/Utils/luarocks/tools/libssl32.dll b/Utils/luarocks/tools/libssl32.dll
new file mode 100644
index 000000000..a30ff0e9e
Binary files /dev/null and b/Utils/luarocks/tools/libssl32.dll differ
diff --git a/Utils/luarocks/tools/ls.exe b/Utils/luarocks/tools/ls.exe
new file mode 100644
index 000000000..96ff2e574
Binary files /dev/null and b/Utils/luarocks/tools/ls.exe differ
diff --git a/Utils/luarocks/tools/md5sum.exe b/Utils/luarocks/tools/md5sum.exe
new file mode 100644
index 000000000..4ae9f74ff
Binary files /dev/null and b/Utils/luarocks/tools/md5sum.exe differ
diff --git a/Utils/luarocks/tools/mkdir.exe b/Utils/luarocks/tools/mkdir.exe
new file mode 100644
index 000000000..83e57d97a
Binary files /dev/null and b/Utils/luarocks/tools/mkdir.exe differ
diff --git a/Utils/luarocks/tools/mv.exe b/Utils/luarocks/tools/mv.exe
new file mode 100644
index 000000000..9fb65bb97
Binary files /dev/null and b/Utils/luarocks/tools/mv.exe differ
diff --git a/Utils/luarocks/tools/pwd.exe b/Utils/luarocks/tools/pwd.exe
new file mode 100644
index 000000000..7dd114de1
Binary files /dev/null and b/Utils/luarocks/tools/pwd.exe differ
diff --git a/Utils/luarocks/tools/rmdir.exe b/Utils/luarocks/tools/rmdir.exe
new file mode 100644
index 000000000..6a85c3c4b
Binary files /dev/null and b/Utils/luarocks/tools/rmdir.exe differ
diff --git a/Utils/luarocks/tools/test.exe b/Utils/luarocks/tools/test.exe
new file mode 100644
index 000000000..94c95f9e4
Binary files /dev/null and b/Utils/luarocks/tools/test.exe differ
diff --git a/Utils/luarocks/tools/uname.exe b/Utils/luarocks/tools/uname.exe
new file mode 100644
index 000000000..3e2f4cf8f
Binary files /dev/null and b/Utils/luarocks/tools/uname.exe differ
diff --git a/Utils/luarocks/tools/wget.exe b/Utils/luarocks/tools/wget.exe
new file mode 100644
index 000000000..54b372e6b
Binary files /dev/null and b/Utils/luarocks/tools/wget.exe differ
diff --git a/Utils/luarocks/wlua5.1.exe b/Utils/luarocks/wlua5.1.exe
new file mode 100644
index 000000000..7be8c3a71
Binary files /dev/null and b/Utils/luarocks/wlua5.1.exe differ
diff --git a/docs/Documentation/AI_Balancer.html b/docs/Documentation/AI_Balancer.html
index 2b5f48f01..3c6b3b617 100644
--- a/docs/Documentation/AI_Balancer.html
+++ b/docs/Documentation/AI_Balancer.html
@@ -31,6 +31,21 @@
TASK.SetTimeOut(): Set timer in seconds before task gets cancelled if not assigned.
-
-
-
1.2) Set and enquire task status (beyond the task state machine processing).
-
A task needs to implement as a minimum the following task states:
-
-
-
Success: Expresses the successful execution and finalization of the task.
-
Failed: Expresses the failure of a task.
-
Planned: Expresses that the task is created, but not yet in execution and is not assigned yet.
-
Assigned: Expresses that the task is assigned to a Group of players, and that the task is in execution mode.
-
-
-
A task may also implement the following task states:
-
-
-
Rejected: Expresses that the task is rejected by a player, who was requested to accept the task.
-
Cancelled: Expresses that the task is cancelled by HQ or through a logical situation where a cancellation of the task is required.
-
-
-
A task can implement more statusses than the ones outlined above. Please consult the documentation of the specific tasks to understand the different status modelled.
-
-
The status of tasks can be set by the methods State followed by the task status. An example is StateAssigned().
-The status of tasks can be enquired by the methods IsState followed by the task status name. An example is if IsStateAssigned() then.
-
-
1.3) Add scoring when reaching a certain task status:
-
Upon reaching a certain task status in a task, additional scoring can be given. If the Mission has a scoring system attached, the scores will be added to the mission scoring.
-Use the method TASK.AddScore() to add scores when a status is reached.
-
-
1.4) Task briefing:
-
A task briefing can be given that is shown to the player when he is assigned to the task.
@@ -132,7 +108,9 @@ Use the method TASK.AddScore() to add scores whe
TASK.SetTimeOut(): Set timer in seconds before task gets cancelled if not assigned.
+
+
+
1.2) Set and enquire task status (beyond the task state machine processing).
+
+
A task needs to implement as a minimum the following task states:
+
+
+
Success: Expresses the successful execution and finalization of the task.
+
Failed: Expresses the failure of a task.
+
Planned: Expresses that the task is created, but not yet in execution and is not assigned yet.
+
Assigned: Expresses that the task is assigned to a Group of players, and that the task is in execution mode.
+
+
+
A task may also implement the following task states:
+
+
+
Rejected: Expresses that the task is rejected by a player, who was requested to accept the task.
+
Cancelled: Expresses that the task is cancelled by HQ or through a logical situation where a cancellation of the task is required.
+
+
+
A task can implement more statusses than the ones outlined above. Please consult the documentation of the specific tasks to understand the different status modelled.
+
+
The status of tasks can be set by the methods State followed by the task status. An example is StateAssigned().
+The status of tasks can be enquired by the methods IsState followed by the task status name. An example is if IsStateAssigned() then.
+
+
1.3) Add scoring when reaching a certain task status:
+
+
Upon reaching a certain task status in a task, additional scoring can be given. If the Mission has a scoring system attached, the scores will be added to the mission scoring.
+Use the method TASK.AddScore() to add scores when a status is reached.
+
+
1.4) Task briefing:
+
+
A task briefing can be given that is shown to the player when he is assigned to the task.
@@ -776,10 +769,7 @@ Use the method TASK.AddScore() to add scores whe
#number TargetDistance :
-The distance to Target when the Player is considered to have "arrived" at the engagement range.
-
-
-
-
-
Core.Zone#ZONE_BASE TargetZone :
-The target zone, if known.
-If the TargetZone parameter is specified, the player will be routed to the center of the zone where all the targets are assumed to be.