mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-10-29 16:58:06 +00:00
Documentation, and test mission video + optimizations
This commit is contained in:
162
Moose/Client.lua
162
Moose/Client.lua
@@ -4,15 +4,36 @@
|
||||
-- ================
|
||||
-- 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} 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.
|
||||
--
|
||||
-- CLIENT construction methods:
|
||||
-- ============================
|
||||
-- Create a new CLIENT object with the @{#CLIENT.New} method:
|
||||
--
|
||||
-- * @{#CLIENT.New}: Creates a new CLIENT object taking the name of the **DCSUnit** that is a client as defined within the mission editor.
|
||||
--
|
||||
-- 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
|
||||
-- @author FlightControl
|
||||
|
||||
@@ -45,7 +66,35 @@ CLIENT = {
|
||||
}
|
||||
|
||||
|
||||
--- Use this method to register new Clients within a mission.
|
||||
--- 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 )
|
||||
local ClientName = DCSUnit:getName()
|
||||
local ClientFound = _DATABASE:FindClient( ClientName )
|
||||
|
||||
if ClientFound then
|
||||
ClientFound:F( ClientName )
|
||||
return ClientFound
|
||||
end
|
||||
|
||||
error( "CLIENT not found for: " .. ClientName )
|
||||
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.
|
||||
@@ -55,26 +104,34 @@ CLIENT = {
|
||||
-- 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:New( 'RU MI-8MTV2*HOT-Deploy Troops 1' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:New( 'RU MI-8MTV2*RAMP-Deploy Troops 3' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:New( 'RU MI-8MTV2*HOT-Deploy Troops 2' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:New( 'RU MI-8MTV2*RAMP-Deploy Troops 4' ):Transport() )
|
||||
function CLIENT:New( ClientName, ClientBriefing )
|
||||
self = _DATABASE:FindClient( ClientName )
|
||||
-- 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 )
|
||||
local ClientFound = _DATABASE:FindClient( ClientName )
|
||||
|
||||
self:F( ClientName, ClientBriefing )
|
||||
self.ClientName = ClientName
|
||||
self:AddBriefing( ClientBriefing )
|
||||
self.MessageSwitch = true
|
||||
if ClientFound then
|
||||
ClientFound:F( { ClientName, ClientBriefing } )
|
||||
ClientFound:AddBriefing( ClientBriefing )
|
||||
ClientFound.MessageSwitch = true
|
||||
|
||||
return self
|
||||
return ClientFound
|
||||
end
|
||||
|
||||
error( "CLIENT not found for: " .. ClientName )
|
||||
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 )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -92,13 +149,38 @@ 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
|
||||
-- @return #CLIENT self
|
||||
function CLIENT:AddBriefing( ClientBriefing )
|
||||
self:F()
|
||||
self:F( ClientBriefing )
|
||||
self.ClientBriefing = ClientBriefing
|
||||
self.ClientBriefingShown = false
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Show the briefing of the MISSION to the 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.MissionBriefing then
|
||||
Briefing = Briefing .. self.MissionBriefing
|
||||
end
|
||||
if self.ClientBriefing then
|
||||
Briefing = Briefing .. "\n" .. self.ClientBriefing
|
||||
end
|
||||
Briefing = Briefing .. "\nPress [LEFT ALT]+[B] to view the complete mission briefing."
|
||||
self:Message( Briefing, 30, self.ClientName .. '/MissionBriefing', "Briefing" )
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Resets a CLIENT.
|
||||
-- @param #CLIENT self
|
||||
@@ -108,21 +190,6 @@ function CLIENT:Reset( ClientName )
|
||||
self._Menus = {}
|
||||
end
|
||||
|
||||
--- Checks for a client alive event and calls a function on a continuous basis.
|
||||
-- @param #CLIENT self
|
||||
-- @param #function CallBack Function.
|
||||
-- @return #CLIENT
|
||||
function CLIENT:Alive( CallBack, ... )
|
||||
self:F()
|
||||
|
||||
self.ClientAlive2 = false
|
||||
self.ClientCallBack = CallBack
|
||||
self.ClientParameters = arg
|
||||
self.AliveCheckScheduler = routines.scheduleFunction( self._AliveCheckScheduler, { self }, timer.getTime() + 1, 5 )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
-- Is Functions
|
||||
|
||||
--- Checks if the CLIENT is a multi-seated UNIT.
|
||||
@@ -147,15 +214,30 @@ function CLIENT:IsMultiSeated()
|
||||
return false
|
||||
end
|
||||
|
||||
--- Checks for a client alive event and calls a function on a continuous basis.
|
||||
-- @param #CLIENT self
|
||||
-- @param #function CallBack Function.
|
||||
-- @return #CLIENT
|
||||
function CLIENT:Alive( CallBack, ... )
|
||||
self:F()
|
||||
|
||||
self.ClientCallBack = CallBack
|
||||
self.ClientParameters = arg
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- @param #CLIENT self
|
||||
function CLIENT:_AliveCheckScheduler()
|
||||
self:F( { self.ClientName, self.ClientAlive2 } )
|
||||
self:F( { self.ClientName, self.ClientAlive2, self.ClientBriefingShown } )
|
||||
|
||||
if self:IsAlive() then -- Polymorphic call of UNIT
|
||||
if self.ClientAlive2 == false then
|
||||
self:T("Calling Callback function")
|
||||
self.ClientCallBack( self, unpack( self.ClientParameters ) )
|
||||
self:ShowBriefing()
|
||||
if self.ClientCallBack then
|
||||
self:T("Calling Callback function")
|
||||
self.ClientCallBack( self, unpack( self.ClientParameters ) )
|
||||
end
|
||||
self.ClientAlive2 = true
|
||||
end
|
||||
else
|
||||
@@ -276,7 +358,7 @@ function CLIENT:GetClientGroupUnit()
|
||||
|
||||
self:T( self.ClientDCSUnit )
|
||||
if ClientDCSUnit and ClientDCSUnit:isExist() then
|
||||
local ClientUnit = _DATABASE.Units[ self.ClientName ]
|
||||
local ClientUnit = _DATABASE:FindUnit( self.ClientName )
|
||||
self:T2( ClientUnit )
|
||||
return ClientUnit
|
||||
end
|
||||
@@ -342,7 +424,7 @@ end
|
||||
-- @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.
|
||||
function CLIENT:Message( Message, MessageDuration, MessageId, MessageCategory, MessageInterval )
|
||||
self:F()
|
||||
self:F( { Message, MessageDuration, MessageId, MessageCategory, MessageInterval } )
|
||||
|
||||
if not self.MenuMessages then
|
||||
if self:GetClientGroupID() then
|
||||
|
||||
@@ -83,13 +83,13 @@ DATABASE = {
|
||||
},
|
||||
DCSUnits = {},
|
||||
DCSGroups = {},
|
||||
Units = {},
|
||||
Groups = {},
|
||||
UNITS = {},
|
||||
GROUPS = {},
|
||||
NavPoints = {},
|
||||
Statics = {},
|
||||
Players = {},
|
||||
PlayersAlive = {},
|
||||
Clients = {},
|
||||
CLIENTS = {},
|
||||
ClientsAlive = {},
|
||||
Filter = {
|
||||
Coalitions = nil,
|
||||
@@ -163,7 +163,7 @@ end
|
||||
-- @return Unit#UNIT The found Unit.
|
||||
function DATABASE:FindUnit( UnitName )
|
||||
|
||||
local UnitFound = self.Units[UnitName]
|
||||
local UnitFound = self.UNITS[UnitName]
|
||||
return UnitFound
|
||||
end
|
||||
|
||||
@@ -172,7 +172,7 @@ end
|
||||
function DATABASE:AddUnit( DCSUnit, DCSUnitName )
|
||||
|
||||
self.DCSUnits[DCSUnitName] = DCSUnit
|
||||
self.Units[DCSUnitName] = UNIT:Register( DCSUnitName )
|
||||
self.UNITS[DCSUnitName] = UNIT:Register( DCSUnitName )
|
||||
end
|
||||
|
||||
--- Deletes a Unit from the DATABASE based on the Unit Name.
|
||||
@@ -188,7 +188,7 @@ end
|
||||
-- @return Client#CLIENT The found CLIENT.
|
||||
function DATABASE:FindClient( ClientName )
|
||||
|
||||
local ClientFound = self.Clients[ClientName]
|
||||
local ClientFound = self.CLIENTS[ClientName]
|
||||
return ClientFound
|
||||
end
|
||||
|
||||
@@ -196,7 +196,26 @@ end
|
||||
-- @param #DATABASE self
|
||||
function DATABASE:AddClient( ClientName )
|
||||
|
||||
self.Clients[ClientName] = CLIENT:Register( ClientName )
|
||||
self.CLIENTS[ClientName] = CLIENT:Register( ClientName )
|
||||
self:E( self.CLIENTS[ClientName]:GetClassNameAndID() )
|
||||
end
|
||||
|
||||
--- Finds a GROUP based on the GroupName.
|
||||
-- @param #DATABASE self
|
||||
-- @param #string GroupName
|
||||
-- @return 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( DCSGroup, GroupName )
|
||||
|
||||
self.DCSGroups[GroupName] = DCSGroup
|
||||
self.GROUPS[GroupName] = GROUP:Register( GroupName )
|
||||
end
|
||||
|
||||
--- Instantiate new Groups within the DCSRTE.
|
||||
@@ -230,7 +249,7 @@ function DATABASE:Spawn( SpawnTemplate )
|
||||
SpawnTemplate.SpawnCategoryID = SpawnCategoryID
|
||||
|
||||
|
||||
local SpawnGroup = GROUP:New( Group.getByName( SpawnTemplate.name ) )
|
||||
local SpawnGroup = GROUP:Register( SpawnTemplate.name )
|
||||
return SpawnGroup
|
||||
end
|
||||
|
||||
@@ -333,8 +352,7 @@ function DATABASE:_RegisterDatabase()
|
||||
local DCSGroupName = DCSGroup:getName()
|
||||
|
||||
self:E( { "Register Group:", DCSGroup, DCSGroupName } )
|
||||
self.DCSGroups[DCSGroupName] = DCSGroup
|
||||
self.Groups[DCSGroupName] = GROUP:New( DCSGroup )
|
||||
self:AddGroup( DCSGroup, DCSGroupName )
|
||||
|
||||
for DCSUnitId, DCSUnit in pairs( DCSGroup:getUnits() ) do
|
||||
|
||||
@@ -368,12 +386,7 @@ function DATABASE:_EventOnBirth( Event )
|
||||
if Event.IniDCSUnit then
|
||||
if self:_IsIncludeDCSUnit( Event.IniDCSUnit ) then
|
||||
self:AddUnit( Event.IniDCSUnit, Event.IniDCSUnitName )
|
||||
|
||||
--if not self.DCSGroups[Event.IniDCSGroupName] then
|
||||
-- self.DCSGroups[Event.IniDCSGroupName] = Event.IniDCSGroupName
|
||||
-- self.DCSGroupsAlive[Event.IniDCSGroupName] = Event.IniDCSGroupName
|
||||
-- self.Groups[Event.IniDCSGroupName] = GROUP:New( Event.IniDCSGroup )
|
||||
--end
|
||||
self:AddGroup( Event.IniDCSGroup, Event.IniDCSGroupName )
|
||||
self:_EventOnPlayerEnterUnit( Event )
|
||||
end
|
||||
end
|
||||
@@ -388,6 +401,7 @@ function DATABASE:_EventOnDeadOrCrash( Event )
|
||||
if Event.IniDCSUnit then
|
||||
if self.DCSUnits[Event.IniDCSUnitName] then
|
||||
self:DeleteUnit( Event.IniDCSUnitName )
|
||||
-- add logic to correctly remove a group once all units are destroyed...
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -403,7 +417,7 @@ function DATABASE:_EventOnPlayerEnterUnit( Event )
|
||||
if not self.PlayersAlive[Event.IniDCSUnitName] then
|
||||
self:E( { "Add player for unit:", Event.IniDCSUnitName, Event.IniDCSUnit:getPlayerName() } )
|
||||
self.PlayersAlive[Event.IniDCSUnitName] = Event.IniDCSUnit:getPlayerName()
|
||||
self.ClientsAlive[Event.IniDCSUnitName] = _DATABASE.Clients[ Event.IniDCSUnitName ]
|
||||
self.ClientsAlive[Event.IniDCSUnitName] = self.CLIENTS[ Event.IniDCSUnitName ]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -503,7 +517,7 @@ end
|
||||
function DATABASE:ForEachClient( IteratorFunction, ... )
|
||||
self:F( arg )
|
||||
|
||||
self:ForEach( IteratorFunction, arg, self.Clients )
|
||||
self:ForEach( IteratorFunction, arg, self.CLIENTS )
|
||||
|
||||
return self
|
||||
end
|
||||
@@ -513,7 +527,7 @@ function DATABASE:ScanEnvironment()
|
||||
self:F()
|
||||
|
||||
self.Navpoints = {}
|
||||
self.Units = {}
|
||||
self.UNITS = {}
|
||||
--Build routines.db.units and self.Navpoints
|
||||
for coa_name, coa_data in pairs(env.mission.coalition) do
|
||||
|
||||
|
||||
1414
Moose/Group.lua
1414
Moose/Group.lua
File diff suppressed because it is too large
Load Diff
@@ -231,25 +231,6 @@ function MISSION:AddGoalFunction( GoalFunction )
|
||||
self.GoalFunction = GoalFunction
|
||||
end
|
||||
|
||||
--- Show the briefing of the MISSION to the CLIENT.
|
||||
-- @param CLIENT Client to show briefing to.
|
||||
-- @return CLIENT
|
||||
function MISSION:ShowBriefing( Client )
|
||||
self:F( { Client.ClientName } )
|
||||
|
||||
if not Client.ClientBriefingShown then
|
||||
Client.ClientBriefingShown = true
|
||||
local Briefing = self.MissionBriefing
|
||||
if Client.ClientBriefing then
|
||||
Briefing = Briefing .. "\n" .. Client.ClientBriefing
|
||||
end
|
||||
Briefing = Briefing .. "\n (Press [LEFT ALT]+[B] to view the graphical documentation.)"
|
||||
Client:Message( Briefing, 30, self.Name .. '/MissionBriefing', "Command: Mission Briefing" )
|
||||
end
|
||||
|
||||
return Client
|
||||
end
|
||||
|
||||
--- Register a new @{CLIENT} to participate within the mission.
|
||||
-- @param CLIENT Client is the @{CLIENT} object. The object must have been instantiated with @{CLIENT:New}.
|
||||
-- @return CLIENT
|
||||
|
||||
@@ -308,7 +308,7 @@ function SET:FilterStart()
|
||||
|
||||
self:E( { "Adding Unit:", DCSUnitName } )
|
||||
self.DCSUnits[DCSUnitName] = _DATABASE.DCSUnits[DCSUnitName]
|
||||
self.Units[DCSUnitName] = _DATABASE.Units[DCSUnitName]
|
||||
self.Units[DCSUnitName] = _DATABASE:FindUnit( DCSUnitName )
|
||||
|
||||
if _DATABASE.DCSUnitsAlive[DCSUnitName] then
|
||||
self.DCSUnitsAlive[DCSUnitName] = _DATABASE.DCSUnitsAlive[DCSUnitName]
|
||||
@@ -323,7 +323,7 @@ function SET:FilterStart()
|
||||
--if self:_IsIncludeDCSGroup( DCSGroup ) then
|
||||
self:E( { "Adding Group:", DCSGroupName } )
|
||||
self.DCSGroups[DCSGroupName] = _DATABASE.DCSGroups[DCSGroupName]
|
||||
self.Groups[DCSGroupName] = _DATABASE.Groups[DCSGroupName]
|
||||
self.Groups[DCSGroupName] = _DATABASE:FindGroups( DCSGroupName )
|
||||
--end
|
||||
|
||||
if _DATABASE.DCSGroupsAlive[DCSGroupName] then
|
||||
@@ -332,7 +332,7 @@ function SET:FilterStart()
|
||||
end
|
||||
end
|
||||
|
||||
for DCSUnitName, Client in pairs( _DATABASE.Clients ) do
|
||||
for DCSUnitName, Client in pairs( _DATABASE.CLIENTS ) do
|
||||
self:E( { "Adding Client for Unit:", DCSUnitName } )
|
||||
self.Clients[DCSUnitName] = _DATABASE.Clients[DCSUnitName]
|
||||
end
|
||||
|
||||
@@ -69,7 +69,7 @@ end
|
||||
function STAGEBRIEF:Execute( Mission, Client, Task )
|
||||
local Valid = BASE:Inherited(self):Execute( Mission, Client, Task )
|
||||
self:F()
|
||||
Mission:ShowBriefing( Client )
|
||||
Client:ShowBriefing()
|
||||
self.StageBriefingTime = timer.getTime()
|
||||
return Valid
|
||||
end
|
||||
|
||||
161
Moose/Unit.lua
161
Moose/Unit.lua
@@ -7,7 +7,7 @@
|
||||
-- * 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 objects.
|
||||
-- * Manage the "state" of the DCS Unit.
|
||||
--
|
||||
--
|
||||
-- UNIT reference methods
|
||||
@@ -15,7 +15,7 @@
|
||||
-- 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
|
||||
-- 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.
|
||||
@@ -25,10 +25,51 @@
|
||||
-- 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 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 @{DCSUnit#Unit.getName}()
|
||||
-- is implemented in the UNIT class as @{#UNIT.GetName}().
|
||||
--
|
||||
-- Additional UNIT APIs
|
||||
-- ====================
|
||||
-- The UNIT class comes with additional methods. Find below a summary.
|
||||
--
|
||||
-- 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.
|
||||
--
|
||||
-- Position, Point
|
||||
-- ---------------
|
||||
-- The UNIT class provides methods to obtain the current point or position of the DCS Unit.
|
||||
-- The @{#UNIT.GetPointVec2}(), @{#UNIT.GetPointVec3}() will obtain the current location of the DCS Unit in a Vec2 (2D) or 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.
|
||||
--
|
||||
-- Alive
|
||||
-- -----
|
||||
-- The @{#UNIT.IsAlive}(), @{#UNIT.IsActive}() methods determines if the DCS Unit is alive, meaning, it is existing and active.
|
||||
--
|
||||
-- Test for other units in radius
|
||||
-- ------------------------------
|
||||
-- One can test if another DCS Unit is within a given radius of the current DCS Unit, by using the @{#UNIT.OtherUnitInRadius}() method.
|
||||
--
|
||||
-- More functions will be added
|
||||
-- ----------------------------
|
||||
-- During the MOOSE development, more functions will be added. A complete list of the current functions is below.
|
||||
--
|
||||
--
|
||||
--
|
||||
--
|
||||
-- @module Unit
|
||||
-- @author FlightControl
|
||||
|
||||
@@ -38,7 +79,7 @@ Include.File( "Message" )
|
||||
|
||||
--- The UNIT class
|
||||
-- @type UNIT
|
||||
-- @Extends Base#BASE
|
||||
-- @extends Base#BASE
|
||||
-- @field #UNIT.FlareColor FlareColor
|
||||
-- @field #UNIT.SmokeColor SmokeColor
|
||||
UNIT = {
|
||||
@@ -79,6 +120,8 @@ UNIT = {
|
||||
-- @field White
|
||||
-- @field Orange
|
||||
-- @field Blue
|
||||
|
||||
-- Registration.
|
||||
|
||||
--- Create a new UNIT from DCSUnit.
|
||||
-- @param #UNIT self
|
||||
@@ -88,11 +131,12 @@ UNIT = {
|
||||
function UNIT:Register( UnitName )
|
||||
|
||||
local self = BASE:Inherit( self, BASE:New() )
|
||||
self:F( UnitName )
|
||||
self:F2( UnitName )
|
||||
self.UnitName = UnitName
|
||||
return self
|
||||
end
|
||||
|
||||
-- Reference methods.
|
||||
|
||||
--- Finds a UNIT from the _DATABASE using a DCSUnit object.
|
||||
-- @param #UNIT self
|
||||
@@ -110,10 +154,9 @@ end
|
||||
-- @param #string UnitName The Unit Name.
|
||||
-- @return Unit#UNIT self
|
||||
function UNIT:FindByName( UnitName )
|
||||
-- self:F( UnitName )
|
||||
|
||||
local FoundUnit = _DATABASE:FindUnit( UnitName )
|
||||
return FoundUnit
|
||||
local UnitFound = _DATABASE:FindUnit( UnitName )
|
||||
return UnitFound
|
||||
end
|
||||
|
||||
function UNIT:GetDCSUnit()
|
||||
@@ -131,13 +174,13 @@ end
|
||||
-- @return DCSCoalitionObject#coalition.side The side of the coalition.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetCoalition()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitCoalition = DCSUnit:getCoalition()
|
||||
self:T( UnitCoalition )
|
||||
self:T3( UnitCoalition )
|
||||
return UnitCoalition
|
||||
end
|
||||
|
||||
@@ -149,13 +192,13 @@ end
|
||||
-- @return DCScountry#country.id The country identifier.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetCountry()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitCountry = DCSUnit:getCountry()
|
||||
self:T( UnitCountry )
|
||||
self:T3( UnitCountry )
|
||||
return UnitCountry
|
||||
end
|
||||
|
||||
@@ -169,7 +212,7 @@ end
|
||||
-- @return #string The name of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetName()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -187,7 +230,7 @@ end
|
||||
-- @return #boolean true if Unit is alive.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:IsAlive()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -204,7 +247,7 @@ end
|
||||
-- @return #boolean true if Unit is activated.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:IsActive()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -222,7 +265,7 @@ end
|
||||
-- @return #string Player Name
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetPlayerName()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -243,7 +286,7 @@ end
|
||||
-- @return DCSUnit#Unit.ID Unit ID
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetID()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -263,7 +306,7 @@ end
|
||||
-- @return #number The Unit number.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetNumber()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -280,7 +323,7 @@ end
|
||||
-- @return Group#GROUP The Group of the Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetGroup()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -298,7 +341,7 @@ end
|
||||
-- @return #string The Callsign of the Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetCallSign()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -315,7 +358,7 @@ end
|
||||
-- @return #number The Unit's health value.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetLife()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -332,7 +375,7 @@ end
|
||||
-- @return #number The Unit's initial health value.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetLife0()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -349,7 +392,7 @@ end
|
||||
-- @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:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -366,7 +409,7 @@ end
|
||||
-- @return DCSUnit#Unit.Ammo
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetAmmo()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -383,7 +426,7 @@ end
|
||||
-- @return DCSUnit#Unit.Sensors
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetSensors()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -407,7 +450,7 @@ end
|
||||
-- @return DCSObject#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:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -426,7 +469,7 @@ end
|
||||
-- @return DCSUnit#Unit.Desc The Unit descriptor.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetDesc()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -444,13 +487,13 @@ end
|
||||
-- @return #string The type name of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetTypeName()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitTypeName = DCSUnit:getTypeName()
|
||||
self:T( UnitTypeName )
|
||||
self:T3( UnitTypeName )
|
||||
return UnitTypeName
|
||||
end
|
||||
|
||||
@@ -466,13 +509,13 @@ end
|
||||
-- @return #string The name of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetPrefix()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitPrefix = string.match( self.UnitName, ".*#" ):sub( 1, -2 )
|
||||
self:T( UnitPrefix )
|
||||
self:T3( UnitPrefix )
|
||||
return UnitPrefix
|
||||
end
|
||||
|
||||
@@ -486,7 +529,7 @@ end
|
||||
-- @return DCSTypes#Vec2 The 2D point vector of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetPointVec2()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -497,7 +540,7 @@ function UNIT:GetPointVec2()
|
||||
UnitPointVec2.x = UnitPointVec3.x
|
||||
UnitPointVec2.y = UnitPointVec3.z
|
||||
|
||||
self:T( UnitPointVec2 )
|
||||
self:T3( UnitPointVec2 )
|
||||
return UnitPointVec2
|
||||
end
|
||||
|
||||
@@ -510,13 +553,13 @@ end
|
||||
-- @return DCSTypes#Vec3 The 3D point vector of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetPointVec3()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitPointVec3 = DCSUnit:getPosition().p
|
||||
self:T( UnitPointVec3 )
|
||||
self:T3( UnitPointVec3 )
|
||||
return UnitPointVec3
|
||||
end
|
||||
|
||||
@@ -528,13 +571,13 @@ end
|
||||
-- @return DCSTypes#Position The 3D position vectors of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetPositionVec3()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitPosition = DCSUnit:getPosition()
|
||||
self:T( UnitPosition )
|
||||
self:T3( UnitPosition )
|
||||
return UnitPosition
|
||||
end
|
||||
|
||||
@@ -546,13 +589,13 @@ end
|
||||
-- @return DCSTypes#Vec3 The velocity vector
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetVelocity()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitVelocityVec3 = DCSUnit:getVelocity()
|
||||
self:T( UnitVelocityVec3 )
|
||||
self:T3( UnitVelocityVec3 )
|
||||
return UnitVelocityVec3
|
||||
end
|
||||
|
||||
@@ -564,13 +607,13 @@ end
|
||||
-- @return #boolean true if in the air.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:InAir()
|
||||
self:F( self.UnitName )
|
||||
self:F2( self.UnitName )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
if DCSUnit then
|
||||
local UnitInAir = DCSUnit:inAir()
|
||||
self:T( UnitInAir )
|
||||
self:T3( UnitInAir )
|
||||
return UnitInAir
|
||||
end
|
||||
|
||||
@@ -582,7 +625,7 @@ end
|
||||
-- @return DCSTypes#Distance The altitude of the DCS Unit.
|
||||
-- @return #nil The DCS Unit is not existing or alive.
|
||||
function UNIT:GetAltitude()
|
||||
self:F()
|
||||
self:F2()
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -601,7 +644,7 @@ end
|
||||
-- @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:F( { self.UnitName, AwaitUnit.UnitName, Radius } )
|
||||
self:F2( { self.UnitName, AwaitUnit.UnitName, Radius } )
|
||||
|
||||
local DCSUnit = self:GetDCSUnit()
|
||||
|
||||
@@ -610,10 +653,10 @@ function UNIT:OtherUnitInRadius( AwaitUnit, Radius )
|
||||
local AwaitUnitPos = AwaitUnit:GetPointVec3()
|
||||
|
||||
if (((UnitPos.x - AwaitUnitPos.x)^2 + (UnitPos.z - AwaitUnitPos.z)^2)^0.5 <= Radius) then
|
||||
self:T( "true" )
|
||||
self:T3( "true" )
|
||||
return true
|
||||
else
|
||||
self:T( "false" )
|
||||
self:T3( "false" )
|
||||
return false
|
||||
end
|
||||
end
|
||||
@@ -638,77 +681,77 @@ end
|
||||
--- Signal a flare at the position of the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:Flare( FlareColor )
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.signalFlare( self:GetPointVec3(), FlareColor , 0 )
|
||||
end
|
||||
|
||||
--- Signal a white flare at the position of the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:FlareWhite()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.signalFlare( self:GetPointVec3(), trigger.flareColor.White , 0 )
|
||||
end
|
||||
|
||||
--- Signal a yellow flare at the position of the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:FlareYellow()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.signalFlare( self:GetPointVec3(), trigger.flareColor.Yellow , 0 )
|
||||
end
|
||||
|
||||
--- Signal a green flare at the position of the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:FlareGreen()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.signalFlare( self:GetPointVec3(), trigger.flareColor.Green , 0 )
|
||||
end
|
||||
|
||||
--- Signal a red flare at the position of the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:FlareRed()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.signalFlare( self:GetPointVec3(), trigger.flareColor.Red, 0 )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT.
|
||||
-- @param #UNIT self
|
||||
function UNIT:Smoke( SmokeColor )
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), SmokeColor )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT Green.
|
||||
-- @param #UNIT self
|
||||
function UNIT:SmokeGreen()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), trigger.smokeColor.Green )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT Red.
|
||||
-- @param #UNIT self
|
||||
function UNIT:SmokeRed()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), trigger.smokeColor.Red )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT White.
|
||||
-- @param #UNIT self
|
||||
function UNIT:SmokeWhite()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), trigger.smokeColor.White )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT Orange.
|
||||
-- @param #UNIT self
|
||||
function UNIT:SmokeOrange()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), trigger.smokeColor.Orange )
|
||||
end
|
||||
|
||||
--- Smoke the UNIT Blue.
|
||||
-- @param #UNIT self
|
||||
function UNIT:SmokeBlue()
|
||||
self:F()
|
||||
self:F2()
|
||||
trigger.action.smoke( self:GetPointVec3(), trigger.smokeColor.Blue )
|
||||
end
|
||||
|
||||
@@ -719,14 +762,14 @@ end
|
||||
-- @param #UNIT self
|
||||
-- @return #boolean Air category evaluation result.
|
||||
function UNIT:IsAir()
|
||||
self:F()
|
||||
self:F2()
|
||||
|
||||
local UnitDescriptor = self.DCSUnit:getDesc()
|
||||
self:T( { UnitDescriptor.category, Unit.Category.AIRPLANE, Unit.Category.HELICOPTER } )
|
||||
self:T3( { UnitDescriptor.category, Unit.Category.AIRPLANE, Unit.Category.HELICOPTER } )
|
||||
|
||||
local IsAirResult = ( UnitDescriptor.category == Unit.Category.AIRPLANE ) or ( UnitDescriptor.category == Unit.Category.HELICOPTER )
|
||||
|
||||
self:T( IsAirResult )
|
||||
self:T3( IsAirResult )
|
||||
return IsAirResult
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user