mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-10-29 16:58:06 +00:00
New Event Dispatching based on priorities
-- First _DATABASE -- then SETS -- then the rest -- changed AIRBASEPOLICE
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
EVENT = {
|
||||
ClassName = "EVENT",
|
||||
ClassID = 0,
|
||||
SortedEvents = {},
|
||||
}
|
||||
|
||||
local _EVENTCODES = {
|
||||
@@ -50,6 +51,33 @@ local _EVENTCODES = {
|
||||
"S_EVENT_MAX",
|
||||
}
|
||||
|
||||
local _EVENTORDER = {
|
||||
[world.event.S_EVENT_SHOT] = 1,
|
||||
[world.event.S_EVENT_HIT] = 1,
|
||||
[world.event.S_EVENT_TAKEOFF] = 1,
|
||||
[world.event.S_EVENT_LAND] = 1,
|
||||
[world.event.S_EVENT_CRASH] = -1,
|
||||
[world.event.S_EVENT_EJECTION] = -1,
|
||||
[world.event.S_EVENT_REFUELING] = 1,
|
||||
[world.event.S_EVENT_DEAD] = -1,
|
||||
[world.event.S_EVENT_PILOT_DEAD] = -1,
|
||||
[world.event.S_EVENT_BASE_CAPTURED] = 1,
|
||||
[world.event.S_EVENT_MISSION_START] = 1,
|
||||
[world.event.S_EVENT_MISSION_END] = -1,
|
||||
[world.event.S_EVENT_TOOK_CONTROL] = 1,
|
||||
[world.event.S_EVENT_REFUELING_STOP] = 1,
|
||||
[world.event.S_EVENT_BIRTH] = 1,
|
||||
[world.event.S_EVENT_HUMAN_FAILURE] = 1,
|
||||
[world.event.S_EVENT_ENGINE_STARTUP] = 1,
|
||||
[world.event.S_EVENT_ENGINE_SHUTDOWN] = 1,
|
||||
[world.event.S_EVENT_PLAYER_ENTER_UNIT] = 1,
|
||||
[world.event.S_EVENT_PLAYER_LEAVE_UNIT] = -1,
|
||||
[world.event.S_EVENT_PLAYER_COMMENT] = 1,
|
||||
[world.event.S_EVENT_SHOOTING_START] = 1,
|
||||
[world.event.S_EVENT_SHOOTING_END] = 1,
|
||||
[world.event.S_EVENT_MAX] = 1,
|
||||
}
|
||||
|
||||
--- The Event structure
|
||||
-- @type EVENTDATA
|
||||
-- @field id
|
||||
@@ -102,7 +130,7 @@ function EVENT:Init( EventID, 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" } )
|
||||
|
||||
self.SortedEvents[EventID] = setmetatable( {}, { __mode = "k" } )
|
||||
end
|
||||
|
||||
if not self.Events[EventID][EventClass] then
|
||||
@@ -121,6 +149,12 @@ function EVENT:Remove( EventClass, EventID )
|
||||
|
||||
local EventClass = EventClass
|
||||
self.Events[EventID][EventClass] = nil
|
||||
|
||||
self.SortedEvents[EventID] = nil
|
||||
self.SortedEvents[EventID] = {}
|
||||
for EventClass, Event in pairs(self.Events[EventID]) do table.insert( self.SortedEvents[EventID], Event) end
|
||||
table.sort( self.SortedEvents[EventID], function( Event1, Event2 ) return Event1.EventTime < Event2.EventTime end )
|
||||
|
||||
end
|
||||
|
||||
--- Clears all event subscriptions for a @{Core.Base#BASE} derived object.
|
||||
@@ -132,6 +166,7 @@ function EVENT:RemoveAll( EventObject )
|
||||
local EventClass = EventObject:GetClassNameAndID()
|
||||
for EventID, EventData in pairs( self.Events ) do
|
||||
self.Events[EventID][EventClass] = nil
|
||||
self.SortedEvents[EventID] = nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -165,6 +200,13 @@ function EVENT:OnEventGeneric( EventFunction, EventClass, EventID )
|
||||
local Event = self:Init( EventID, EventClass )
|
||||
Event.EventFunction = EventFunction
|
||||
Event.EventClass = EventClass
|
||||
Event.EventTime = EventClass.EventPriority and EventClass.EventPriority or 10
|
||||
|
||||
self.SortedEvents[EventID] = nil
|
||||
self.SortedEvents[EventID] = {}
|
||||
for EventClass, Event in pairs(self.Events[EventID]) do table.insert( self.SortedEvents[EventID], Event) end
|
||||
table.sort( self.SortedEvents[EventID], function( Event1, Event2 ) return Event1.EventTime < Event2.EventTime end )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -741,6 +783,8 @@ function EVENT:onEvent( Event )
|
||||
Event.IniDCSGroupName = ""
|
||||
if Event.IniDCSGroup and Event.IniDCSGroup:isExist() then
|
||||
Event.IniDCSGroupName = Event.IniDCSGroup:getName()
|
||||
Event.IniGroup = GROUP:FindByName( Event.IniDCSGroupName )
|
||||
self:E( { IniGroup = Event.IniGroup } )
|
||||
end
|
||||
end
|
||||
if Event.target then
|
||||
@@ -763,11 +807,27 @@ function EVENT:onEvent( Event )
|
||||
end
|
||||
self:E( { _EVENTCODES[Event.id], Event, Event.IniDCSUnitName, Event.TgtDCSUnitName } )
|
||||
|
||||
-- Okay, we got the event from DCS. Now loop the self.Events[] 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] ) do
|
||||
local function pairsByEventSorted( EventSorted, Order )
|
||||
local i = Order == -1 and #EventSorted or 0
|
||||
local iter = function()
|
||||
i = i + Order
|
||||
if EventSorted[i] == nil then
|
||||
return nil
|
||||
else
|
||||
return EventSorted[i].EventClass, EventSorted[i]
|
||||
end
|
||||
end
|
||||
return iter
|
||||
end
|
||||
|
||||
self:E( { Order = _EVENTORDER[Event.id] } )
|
||||
|
||||
-- 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 pairsByEventSorted( self.SortedEvents[Event.id], _EVENTORDER[Event.id] ) do
|
||||
-- If the EventData is for a UNIT, the call directly the EventClass EventFunction for that UNIT.
|
||||
if Event.IniDCSUnitName and EventData.IniUnit and EventData.IniUnit[Event.IniDCSUnitName] then
|
||||
self:T( { "Calling EventFunction for Class ", EventClass:GetClassNameAndID(), ", Unit ", Event.IniUnitName } )
|
||||
self:E( { "Calling EventFunction for Class ", EventClass:GetClassNameAndID(), ", Unit ", Event.IniUnitName, EventData.EventTime } )
|
||||
Event.IniGroup = GROUP:FindByName( Event.IniDCSGroupName )
|
||||
local Result, Value = xpcall( function() return EventData.IniUnit[Event.IniDCSUnitName].EventFunction( EventData.IniUnit[Event.IniDCSUnitName].EventClass, Event ) end, ErrorHandler )
|
||||
--EventData.IniUnit[Event.IniDCSUnitName].EventFunction( EventData.IniUnit[Event.IniDCSUnitName].EventClass, Event )
|
||||
else
|
||||
@@ -775,7 +835,8 @@ function EVENT:onEvent( Event )
|
||||
-- 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 and not EventData.IniUnit then
|
||||
if EventClass == EventData.EventClass then
|
||||
self:T( { "Calling EventFunction for Class ", EventClass:GetClassNameAndID() } )
|
||||
self:E( { "Calling EventFunction for Class ", EventClass:GetClassNameAndID(), EventData.EventTime } )
|
||||
Event.IniGroup = GROUP:FindByName( Event.IniDCSGroupName )
|
||||
local Result, Value = xpcall( function() return EventData.EventFunction( EventData.EventClass, Event ) end, ErrorHandler )
|
||||
--EventData.EventFunction( EventData.EventClass, Event )
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user