Client and Event updates

This commit is contained in:
Frank 2020-12-31 00:51:54 +01:00
parent a4f15a0830
commit f725039da5
4 changed files with 87 additions and 38 deletions

View File

@ -676,6 +676,13 @@ do -- Event Handling
-- @param #BASE self -- @param #BASE self
-- @param Core.Event#EVENTDATA EventData The EventData structure. -- @param Core.Event#EVENTDATA EventData The EventData structure.
--- Occurs when a player enters a slot and takes control of an aircraft.
-- **NOTE**: This is a workaround of a long standing DCS bug with the PLAYER_ENTER_UNIT event.
-- initiator : The unit that is being taken control of.
-- @function [parent=#BASE] OnEventPlayerEnterAircraft
-- @param #BASE self
-- @param Core.Event#EVENTDATA EventData The EventData structure.
end end
@ -765,7 +772,7 @@ function BASE:CreateEventTakeoff( EventTime, Initiator )
world.onEvent( Event ) world.onEvent( Event )
end end
--- Creation of a S_EVENT_PLAYER_ENTER_AIRCRAFT event. --- Creation of a `S_EVENT_PLAYER_ENTER_AIRCRAFT` event.
-- @param #BASE self -- @param #BASE self
-- @param Wrapper.Unit#UNIT PlayerUnit The aircraft unit the player entered. -- @param Wrapper.Unit#UNIT PlayerUnit The aircraft unit the player entered.
function BASE:CreateEventPlayerEnterAircraft( PlayerUnit ) function BASE:CreateEventPlayerEnterAircraft( PlayerUnit )

View File

@ -24,7 +24,7 @@
-- === -- ===
-- --
-- ### Author: **FlightControl** -- ### Author: **FlightControl**
-- ### Contributions: -- ### Contributions: **funkyfranky**
-- --
-- === -- ===
-- --
@ -33,6 +33,9 @@
--- @type DATABASE --- @type DATABASE
-- @field #string ClassName Name of the class.
-- @field #table Templates Templates: Units, Groups, Statics, ClientsByName, ClientsByID.
-- @field #table CLIENTS Clients.
-- @extends Core.Base#BASE -- @extends Core.Base#BASE
--- Contains collections of wrapper objects defined within MOOSE that reflect objects within the simulator. --- Contains collections of wrapper objects defined within MOOSE that reflect objects within the simulator.
@ -126,8 +129,6 @@ function DATABASE:New()
self:HandleEvent( EVENTS.DeleteCargo ) self:HandleEvent( EVENTS.DeleteCargo )
self:HandleEvent( EVENTS.NewZone ) self:HandleEvent( EVENTS.NewZone )
self:HandleEvent( EVENTS.DeleteZone ) self:HandleEvent( EVENTS.DeleteZone )
-- Follow alive players and clients
--self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit ) -- This is not working anymore!, handling this through the birth event. --self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit ) -- This is not working anymore!, handling this through the birth event.
self:HandleEvent( EVENTS.PlayerLeaveUnit, self._EventOnPlayerLeaveUnit ) self:HandleEvent( EVENTS.PlayerLeaveUnit, self._EventOnPlayerLeaveUnit )
@ -135,8 +136,8 @@ function DATABASE:New()
self:_RegisterGroupsAndUnits() self:_RegisterGroupsAndUnits()
self:_RegisterClients() self:_RegisterClients()
self:_RegisterStatics() self:_RegisterStatics()
--self:_RegisterPlayers()
self:_RegisterAirbases() self:_RegisterAirbases()
--self:_RegisterPlayers()
self.UNITS_Position = 0 self.UNITS_Position = 0
@ -156,14 +157,22 @@ end
--- Adds a Unit based on the Unit Name in the DATABASE. --- Adds a Unit based on the Unit Name in the DATABASE.
-- @param #DATABASE self -- @param #DATABASE self
-- @param #string DCSUnitName Unit name.
-- @return Wrapper.Unit#UNIT The added unit.
function DATABASE:AddUnit( DCSUnitName ) function DATABASE:AddUnit( DCSUnitName )
if not self.UNITS[DCSUnitName] then if not self.UNITS[DCSUnitName] then
-- Debug info.
self:T( { "Add UNIT:", DCSUnitName } ) self:T( { "Add UNIT:", DCSUnitName } )
local UnitRegister = UNIT:Register( DCSUnitName )
self.UNITS[DCSUnitName] = UNIT:Register( DCSUnitName ) --local UnitRegister = UNIT:Register( DCSUnitName )
-- Register unit
self.UNITS[DCSUnitName]=UNIT:Register(DCSUnitName)
table.insert( self.UNITS_Index, DCSUnitName ) -- This is not used anywhere in MOOSE as far as I can see so I remove it until there comes an error somewhere.
--table.insert(self.UNITS_Index, DCSUnitName )
end end
return self.UNITS[DCSUnitName] return self.UNITS[DCSUnitName]
@ -179,6 +188,8 @@ end
--- Adds a Static based on the Static Name in the DATABASE. --- Adds a Static based on the Static Name in the DATABASE.
-- @param #DATABASE self -- @param #DATABASE self
-- @param #string DCSStaticName Name of the static.
-- @return Wrapper.Static#STATIC The static object.
function DATABASE:AddStatic( DCSStaticName ) function DATABASE:AddStatic( DCSStaticName )
if not self.STATICS[DCSStaticName] then if not self.STATICS[DCSStaticName] then
@ -594,6 +605,9 @@ function DATABASE:Spawn( SpawnTemplate )
end end
--- Set a status to a Group within the Database, this to check crossing events for example. --- Set a status to a Group within the Database, this to check crossing events for example.
-- @param #DATABASE self
-- @param #string GroupName Group name.
-- @param #string Status Status.
function DATABASE:SetStatusGroup( GroupName, Status ) function DATABASE:SetStatusGroup( GroupName, Status )
self:F2( Status ) self:F2( Status )
@ -601,8 +615,11 @@ function DATABASE:SetStatusGroup( GroupName, Status )
end end
--- Get a status to a Group within the Database, this to check crossing events for example. --- Get a status to a Group within the Database, this to check crossing events for example.
-- @param #DATABASE self
-- @param #string GroupName Group name.
-- @return #string Status or an empty string "".
function DATABASE:GetStatusGroup( GroupName ) function DATABASE:GetStatusGroup( GroupName )
self:F2( Status ) self:F2( GroupName )
if self.Templates.Groups[GroupName] then if self.Templates.Groups[GroupName] then
return self.Templates.Groups[GroupName].Status return self.Templates.Groups[GroupName].Status
@ -616,7 +633,8 @@ end
-- @param #table GroupTemplate -- @param #table GroupTemplate
-- @param DCS#coalition.side CoalitionSide The coalition.side of the object. -- @param DCS#coalition.side CoalitionSide The coalition.side of the object.
-- @param DCS#Object.Category CategoryID The Object.category of the object. -- @param DCS#Object.Category CategoryID The Object.category of the object.
-- @param DCS#country.id CountryID the country.id of the object -- @param DCS#country.id CountryID the country ID of the object.
-- @param #string GroupName (Optional) The name of the group. Default is `GroupTemplate.name`.
-- @return #DATABASE self -- @return #DATABASE self
function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, CategoryID, CountryID, GroupName ) function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, CategoryID, CountryID, GroupName )
@ -681,6 +699,10 @@ function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, Category
) )
end end
--- Get group template.
-- @param #DATABASE self
-- @param #string GroupName Group name.
-- @return #table Group template table.
function DATABASE:GetGroupTemplate( GroupName ) function DATABASE:GetGroupTemplate( GroupName )
local GroupTemplate = self.Templates.Groups[GroupName].Template local GroupTemplate = self.Templates.Groups[GroupName].Template
GroupTemplate.SpawnCoalitionID = self.Templates.Groups[GroupName].CoalitionID GroupTemplate.SpawnCoalitionID = self.Templates.Groups[GroupName].CoalitionID
@ -691,7 +713,10 @@ end
--- Private method that registers new Static Templates within the DATABASE Object. --- Private method that registers new Static Templates within the DATABASE Object.
-- @param #DATABASE self -- @param #DATABASE self
-- @param #table StaticTemplate -- @param #table StaticTemplate Template table.
-- @param #number CoalitionID Coalition ID.
-- @param #number CategoryID Category ID.
-- @param #number CountryID Country ID.
-- @return #DATABASE self -- @return #DATABASE self
function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, CategoryID, CountryID ) function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, CategoryID, CountryID )
@ -721,10 +746,13 @@ function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, Category
self:AddStatic( StaticTemplateName ) self:AddStatic( StaticTemplateName )
return self
end end
--- Get static group template.
--- @param #DATABASE self -- @param #DATABASE self
-- @param #string StaticName Name of the static
-- @return #table Static template table.
function DATABASE:GetStaticGroupTemplate( StaticName ) function DATABASE:GetStaticGroupTemplate( StaticName )
local StaticTemplate = self.Templates.Statics[StaticName].GroupTemplate local StaticTemplate = self.Templates.Statics[StaticName].GroupTemplate
return StaticTemplate, self.Templates.Statics[StaticName].CoalitionID, self.Templates.Statics[StaticName].CategoryID, self.Templates.Statics[StaticName].CountryID return StaticTemplate, self.Templates.Statics[StaticName].CoalitionID, self.Templates.Statics[StaticName].CategoryID, self.Templates.Statics[StaticName].CountryID
@ -913,20 +941,28 @@ function DATABASE:_EventOnBirth( Event )
self:F( { Event } ) self:F( { Event } )
if Event.IniDCSUnit then if Event.IniDCSUnit then
if Event.IniObjectCategory == 3 then if Event.IniObjectCategory == 3 then
self:AddStatic( Event.IniDCSUnitName ) self:AddStatic( Event.IniDCSUnitName )
else else
if Event.IniObjectCategory == 1 then if Event.IniObjectCategory == 1 then
self:AddUnit( Event.IniDCSUnitName ) self:AddUnit( Event.IniDCSUnitName )
self:AddGroup( Event.IniDCSGroupName ) self:AddGroup( Event.IniDCSGroupName )
-- Add airbase if it was spawned later in the mission. -- Add airbase if it was spawned later in the mission.
local DCSAirbase = Airbase.getByName(Event.IniDCSUnitName) local DCSAirbase = Airbase.getByName(Event.IniDCSUnitName)
if DCSAirbase then if DCSAirbase then
self:I(string.format("Adding airbase %s", tostring(Event.IniDCSUnitName))) self:I(string.format("Adding airbase %s", tostring(Event.IniDCSUnitName)))
self:AddAirbase(Event.IniDCSUnitName) self:AddAirbase(Event.IniDCSUnitName)
end end
end end
end end
if Event.IniObjectCategory == 1 then if Event.IniObjectCategory == 1 then
Event.IniUnit = self:FindUnit( Event.IniDCSUnitName ) Event.IniUnit = self:FindUnit( Event.IniDCSUnitName )
@ -936,17 +972,28 @@ function DATABASE:_EventOnBirth( Event )
local PlayerName = Event.IniUnit:GetPlayerName() local PlayerName = Event.IniUnit:GetPlayerName()
if PlayerName then if PlayerName then
self:I( { "Player Joined:", PlayerName } )
-- Debug info.
self:I(string.format("Player %s joint unit %s of group %s", tostring(PlayerName), tostring(Event.IniDCSUnitName), tostring(Event.IniDCSGroupName)))
-- Add client.
self:AddClient( Event.IniDCSUnitName ) self:AddClient( Event.IniDCSUnitName )
-- Add player.
if not self.PLAYERS[PlayerName] then if not self.PLAYERS[PlayerName] then
self:AddPlayer( Event.IniUnitName, PlayerName ) self:AddPlayer( Event.IniUnitName, PlayerName )
end end
-- Player settings.
local Settings = SETTINGS:Set( PlayerName ) local Settings = SETTINGS:Set( PlayerName )
Settings:SetPlayerMenu( Event.IniUnit ) Settings:SetPlayerMenu(Event.IniUnit)
end end
_DATABASE:CreateEventBirth(-EventTime,Initiator,IniUnitName,place,subplace) if PlayerName or self.CLIENTS[Event.IniDCSUnitName] then
self:CreateEventPlayerEnterAircraft(Event.IniUnit)
end
end end
end end
@ -1348,19 +1395,13 @@ function DATABASE:_RegisterTemplates()
for group_num, Template in pairs(obj_type_data.group) do for group_num, Template in pairs(obj_type_data.group) do
if obj_type_name ~= "static" and Template and Template.units and type(Template.units) == 'table' then --making sure again- this is a valid group if obj_type_name ~= "static" and Template and Template.units and type(Template.units) == 'table' then --making sure again- this is a valid group
self:_RegisterGroupTemplate(
Template, self:_RegisterGroupTemplate(Template, CoalitionSide, _DATABASECategory[string.lower(CategoryName)], CountryID)
CoalitionSide,
_DATABASECategory[string.lower(CategoryName)],
CountryID
)
else else
self:_RegisterStaticTemplate(
Template, self:_RegisterStaticTemplate(Template, CoalitionSide, _DATABASECategory[string.lower(CategoryName)], CountryID)
CoalitionSide,
_DATABASECategory[string.lower(CategoryName)],
CountryID
)
end --if GroupTemplate and GroupTemplate.units then end --if GroupTemplate and GroupTemplate.units then
end --for group_num, GroupTemplate in pairs(obj_type_data.group) do 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 ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then

View File

@ -646,7 +646,7 @@ end
-- @param #function EventFunction The function to be called when the event occurs for the unit. -- @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 EventClass The instance of the class for which the event is.
-- @param #function OnEventFunction -- @param #function OnEventFunction
-- @return #EVENT -- @return #EVENT self
function EVENT:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EventID ) function EVENT:OnEventForTemplate( EventTemplate, EventFunction, EventClass, EventID )
self:F2( EventTemplate.name ) self:F2( EventTemplate.name )
@ -693,8 +693,9 @@ end
-- @param #string GroupName The name of the GROUP. -- @param #string GroupName The name of the GROUP.
-- @param #function EventFunction The function to be called when the event occurs for 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 Core.Base#BASE EventClass The self instance of the class for which the event is.
-- @param EventID -- @param #number EventID Event ID.
-- @return #EVENT -- @param ... Optional arguments passed to the event function.
-- @return #EVENT self
function EVENT:OnEventForGroup( GroupName, EventFunction, EventClass, EventID, ... ) function EVENT:OnEventForGroup( GroupName, EventFunction, EventClass, EventID, ... )
local Event = self:Init( EventID, EventClass ) local Event = self:Init( EventID, EventClass )
@ -711,7 +712,7 @@ do -- OnBirth
-- @param Wrapper.Group#GROUP EventGroup -- @param Wrapper.Group#GROUP EventGroup
-- @param #function EventFunction The function to be called when the event occurs for the unit. -- @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. -- @param EventClass The self instance of the class for which the event is.
-- @return #EVENT -- @return #EVENT self
function EVENT:OnBirthForTemplate( EventTemplate, EventFunction, EventClass ) function EVENT:OnBirthForTemplate( EventTemplate, EventFunction, EventClass )
self:F2( EventTemplate.name ) self:F2( EventTemplate.name )

View File

@ -66,8 +66,7 @@ CLIENT = {
ClientBriefingShown = false, ClientBriefingShown = false,
_Menus = {}, _Menus = {},
_Tasks = {}, _Tasks = {},
Messages = { Messages = {},
}
} }
@ -137,7 +136,7 @@ end
--- Transport defines that the Client is a Transport. Transports show cargo. --- Transport defines that the Client is a Transport. Transports show cargo.
-- @param #CLIENT self -- @param #CLIENT self
-- @param #string ClientName Name of the client unit. -- @param #string ClientName Name of the client unit.
-- @return #CLIENT -- @return #CLIENT self
function CLIENT:Register( ClientName ) function CLIENT:Register( ClientName )
local self = BASE:Inherit( self, UNIT:Register( ClientName ) ) -- #CLIENT local self = BASE:Inherit( self, UNIT:Register( ClientName ) ) -- #CLIENT
@ -158,7 +157,7 @@ end
--- Transport defines that the Client is a Transport. Transports show cargo. --- Transport defines that the Client is a Transport. Transports show cargo.
-- @param #CLIENT self -- @param #CLIENT self
-- @return #CLIENT -- @return #CLIENT self
function CLIENT:Transport() function CLIENT:Transport()
self:F() self:F()
@ -245,6 +244,7 @@ end
--- Checks for a client alive event and calls a function on a continuous basis. --- Checks for a client alive event and calls a function on a continuous basis.
-- @param #CLIENT self -- @param #CLIENT self
-- @param #function CallBackFunction Create a function that will be called when a player joins the slot. -- @param #function CallBackFunction Create a function that will be called when a player joins the slot.
-- @param ... (Optional) Arguments for callback function as comma separated list.
-- @return #CLIENT -- @return #CLIENT
function CLIENT:Alive( CallBackFunction, ... ) function CLIENT:Alive( CallBackFunction, ... )
self:F() self:F()