Merge pull request #41 from FlightControl-Master/Rework-Scheduling-with-SCHEDULER-class

Rework scheduling with scheduler class
This commit is contained in:
Sven Van de Velde
2016-05-10 15:49:37 +02:00
4 changed files with 92 additions and 110 deletions

View File

@@ -6443,6 +6443,7 @@ SCHEDULER = {
--- SCHEDULER constructor. --- SCHEDULER constructor.
-- @param #SCHEDULER self -- @param #SCHEDULER self
-- @param #table TimeEventObject
-- @param #function TimeEventFunction -- @param #function TimeEventFunction
-- @param #table TimeEventFunctionArguments -- @param #table TimeEventFunctionArguments
-- @param #number StartSeconds -- @param #number StartSeconds
@@ -6474,13 +6475,12 @@ function SCHEDULER:New( TimeEventObject, TimeEventFunction, TimeEventFunctionArg
if StopSeconds then if StopSeconds then
self.StopSeconds = StopSeconds self.StopSeconds = StopSeconds
end end
self.Repeat = false
self.StartTime = timer.getTime() self.StartTime = timer.getTime()
self:T("Calling function " .. timer.getTime() + self.StartSeconds ) self:Start()
timer.scheduleFunction( self.Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
return self return self
end end
@@ -6506,7 +6506,7 @@ function SCHEDULER:Scheduler()
self:T( { Status, Result } ) self:T( { Status, Result } )
if Status and Status == true and Result and Result == true then if Status and Status == true and Result and Result == true then
if not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) then if self.Repeat and ( not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) ) then
timer.scheduleFunction( timer.scheduleFunction(
self.Scheduler, self.Scheduler,
self, self,
@@ -6517,6 +6517,21 @@ function SCHEDULER:Scheduler()
end end
function SCHEDULER:Start()
self:F( self.TimeEventObject )
self.Repeat = true
timer.scheduleFunction( self.Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
return self
end
function SCHEDULER:Stop()
self:F( self.TimeEventObject )
self.Repeat = false
end
@@ -12267,6 +12282,7 @@ Include.File( "Database" )
Include.File( "Group" ) Include.File( "Group" )
Include.File( "Zone" ) Include.File( "Zone" )
Include.File( "Event" ) Include.File( "Event" )
Include.File( "Scheduler" )
--- SPAWN Class --- SPAWN Class
-- @type SPAWN -- @type SPAWN
@@ -12510,8 +12526,8 @@ function SPAWN:CleanUp( SpawnCleanUpInterval )
self.SpawnCleanUpInterval = SpawnCleanUpInterval self.SpawnCleanUpInterval = SpawnCleanUpInterval
self.SpawnCleanUpTimeStamps = {} self.SpawnCleanUpTimeStamps = {}
self.CleanUpFunction = routines.scheduleFunction( self._SpawnCleanUpScheduler, { self }, timer.getTime() + 1, SpawnCleanUpInterval ) --self.CleanUpFunction = routines.scheduleFunction( self._SpawnCleanUpScheduler, { self }, timer.getTime() + 1, SpawnCleanUpInterval )
self.CleanUpScheduler = SCHEDULER:New( self, self._SpawnCleanUpScheduler, {}, 1, SpawnCleanUpInterval, 0.2 )
return self return self
end end
@@ -12686,23 +12702,29 @@ end
function SPAWN:SpawnScheduled( SpawnTime, SpawnTimeVariation ) function SPAWN:SpawnScheduled( SpawnTime, SpawnTimeVariation )
self:F( { SpawnTime, SpawnTimeVariation } ) self:F( { SpawnTime, SpawnTimeVariation } )
self.SpawnCurrentTimer = 0 -- The internal timer counter to trigger a scheduled spawning of SpawnTemplatePrefix.
self.SpawnSetTimer = 0 -- The internal timer value when a scheduled spawning of SpawnTemplatePrefix occurs.
self.AliveFactor = 1 --
self.SpawnLowTimer = 0
self.SpawnHighTimer = 0
if SpawnTime ~= nil and SpawnTimeVariation ~= nil then if SpawnTime ~= nil and SpawnTimeVariation ~= nil then
self.SpawnLowTimer = SpawnTime - SpawnTime / 2 * SpawnTimeVariation self.SpawnScheduler = SCHEDULER:New( self, self._Scheduler, {}, 1, SpawnTime, SpawnTimeVariation )
self.SpawnHighTimer = SpawnTime + SpawnTime / 2 * SpawnTimeVariation
self:SpawnScheduleStart()
end end
self:T( { self.SpawnLowTimer, self.SpawnHighTimer } )
return self return self
end end
--- Will re-start the spawning scheduler.
-- Note: This function 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. --- Allows to place a CallFunction hook when a new group spawns.
-- The provided function will be called when a new group is spawned, including its given parameters. -- The provided function 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. -- The first parameter of the SpawnFunction is the @{Group#GROUP} that was spawned.
@@ -12724,30 +12746,6 @@ end
--- Will start the spawning scheduler.
-- Note: This function is called automatically when @{#SPAWN.Scheduled} is called.
function SPAWN:SpawnScheduleStart()
self:F( { self.SpawnTemplatePrefix } )
--local ClientUnit = #AlivePlayerUnits()
self.AliveFactor = 10 -- ( 10 - ClientUnit ) / 10
if self.SpawnIsScheduled == false then
self.SpawnIsScheduled = true
self.SpawnInit = true
self.SpawnSetTimer = math.random( self.SpawnLowTimer * self.AliveFactor / 10 , self.SpawnHighTimer * self.AliveFactor / 10 )
self.SpawnFunction = routines.scheduleFunction( self._Scheduler, { self }, timer.getTime() + 1, 1 )
end
end
--- Will stop the scheduled spawning scheduler.
function SPAWN:SpawnScheduleStop()
self:F( { self.SpawnTemplatePrefix } )
self.SpawnIsScheduled = false
end
--- Will spawn a group from a hosting unit. This function 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. --- Will spawn a group from a hosting unit. This function 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. -- Note that each point in the route assigned to the spawning group is reset to the point of the spawn.
@@ -13427,19 +13425,10 @@ end
function SPAWN:_Scheduler() function SPAWN:_Scheduler()
self:F( { "_Scheduler", self.SpawnTemplatePrefix, self.SpawnAliasPrefix, self.SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive } ) self:F( { "_Scheduler", self.SpawnTemplatePrefix, self.SpawnAliasPrefix, self.SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive } )
if self.SpawnInit or self.SpawnCurrentTimer == self.SpawnSetTimer then -- Validate if there are still groups left in the batch...
-- Validate if there are still groups left in the batch... self:Spawn()
self:Spawn()
self.SpawnInit = false return true
if self.SpawnIsScheduled == true then
--local ClientUnit = #AlivePlayerUnits()
self.AliveFactor = 1 -- ( 10 - ClientUnit ) / 10
self.SpawnCurrentTimer = 0
self.SpawnSetTimer = math.random( self.SpawnLowTimer * self.AliveFactor , self.SpawnHighTimer * self.AliveFactor )
end
else
self.SpawnCurrentTimer = self.SpawnCurrentTimer + 1
end
end end
function SPAWN:_SpawnCleanUpScheduler() function SPAWN:_SpawnCleanUpScheduler()
@@ -13471,6 +13460,8 @@ function SPAWN:_SpawnCleanUpScheduler()
end end
return true -- Repeat
end end
--- Limit the simultaneous movement of Groups within a running Mission. --- 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. -- This module is defined to improve the performance in missions, and to bring additional realism for GROUND vehicles.

View File

@@ -18,6 +18,7 @@ SCHEDULER = {
--- SCHEDULER constructor. --- SCHEDULER constructor.
-- @param #SCHEDULER self -- @param #SCHEDULER self
-- @param #table TimeEventObject
-- @param #function TimeEventFunction -- @param #function TimeEventFunction
-- @param #table TimeEventFunctionArguments -- @param #table TimeEventFunctionArguments
-- @param #number StartSeconds -- @param #number StartSeconds
@@ -49,13 +50,12 @@ function SCHEDULER:New( TimeEventObject, TimeEventFunction, TimeEventFunctionArg
if StopSeconds then if StopSeconds then
self.StopSeconds = StopSeconds self.StopSeconds = StopSeconds
end end
self.Repeat = false
self.StartTime = timer.getTime() self.StartTime = timer.getTime()
self:T("Calling function " .. timer.getTime() + self.StartSeconds ) self:Start()
timer.scheduleFunction( self.Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
return self return self
end end
@@ -81,7 +81,7 @@ function SCHEDULER:Scheduler()
self:T( { Status, Result } ) self:T( { Status, Result } )
if Status and Status == true and Result and Result == true then if Status and Status == true and Result and Result == true then
if not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) then if self.Repeat and ( not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) ) then
timer.scheduleFunction( timer.scheduleFunction(
self.Scheduler, self.Scheduler,
self, self,
@@ -92,6 +92,21 @@ function SCHEDULER:Scheduler()
end end
function SCHEDULER:Start()
self:F( self.TimeEventObject )
self.Repeat = true
timer.scheduleFunction( self.Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
return self
end
function SCHEDULER:Stop()
self:F( self.TimeEventObject )
self.Repeat = false
end

View File

@@ -80,6 +80,7 @@ Include.File( "Database" )
Include.File( "Group" ) Include.File( "Group" )
Include.File( "Zone" ) Include.File( "Zone" )
Include.File( "Event" ) Include.File( "Event" )
Include.File( "Scheduler" )
--- SPAWN Class --- SPAWN Class
-- @type SPAWN -- @type SPAWN
@@ -323,8 +324,8 @@ function SPAWN:CleanUp( SpawnCleanUpInterval )
self.SpawnCleanUpInterval = SpawnCleanUpInterval self.SpawnCleanUpInterval = SpawnCleanUpInterval
self.SpawnCleanUpTimeStamps = {} self.SpawnCleanUpTimeStamps = {}
self.CleanUpFunction = routines.scheduleFunction( self._SpawnCleanUpScheduler, { self }, timer.getTime() + 1, SpawnCleanUpInterval ) --self.CleanUpFunction = routines.scheduleFunction( self._SpawnCleanUpScheduler, { self }, timer.getTime() + 1, SpawnCleanUpInterval )
self.CleanUpScheduler = SCHEDULER:New( self, self._SpawnCleanUpScheduler, {}, 1, SpawnCleanUpInterval, 0.2 )
return self return self
end end
@@ -499,23 +500,29 @@ end
function SPAWN:SpawnScheduled( SpawnTime, SpawnTimeVariation ) function SPAWN:SpawnScheduled( SpawnTime, SpawnTimeVariation )
self:F( { SpawnTime, SpawnTimeVariation } ) self:F( { SpawnTime, SpawnTimeVariation } )
self.SpawnCurrentTimer = 0 -- The internal timer counter to trigger a scheduled spawning of SpawnTemplatePrefix.
self.SpawnSetTimer = 0 -- The internal timer value when a scheduled spawning of SpawnTemplatePrefix occurs.
self.AliveFactor = 1 --
self.SpawnLowTimer = 0
self.SpawnHighTimer = 0
if SpawnTime ~= nil and SpawnTimeVariation ~= nil then if SpawnTime ~= nil and SpawnTimeVariation ~= nil then
self.SpawnLowTimer = SpawnTime - SpawnTime / 2 * SpawnTimeVariation self.SpawnScheduler = SCHEDULER:New( self, self._Scheduler, {}, 1, SpawnTime, SpawnTimeVariation )
self.SpawnHighTimer = SpawnTime + SpawnTime / 2 * SpawnTimeVariation
self:SpawnScheduleStart()
end end
self:T( { self.SpawnLowTimer, self.SpawnHighTimer } )
return self return self
end end
--- Will re-start the spawning scheduler.
-- Note: This function 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. --- Allows to place a CallFunction hook when a new group spawns.
-- The provided function will be called when a new group is spawned, including its given parameters. -- The provided function 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. -- The first parameter of the SpawnFunction is the @{Group#GROUP} that was spawned.
@@ -537,30 +544,6 @@ end
--- Will start the spawning scheduler.
-- Note: This function is called automatically when @{#SPAWN.Scheduled} is called.
function SPAWN:SpawnScheduleStart()
self:F( { self.SpawnTemplatePrefix } )
--local ClientUnit = #AlivePlayerUnits()
self.AliveFactor = 10 -- ( 10 - ClientUnit ) / 10
if self.SpawnIsScheduled == false then
self.SpawnIsScheduled = true
self.SpawnInit = true
self.SpawnSetTimer = math.random( self.SpawnLowTimer * self.AliveFactor / 10 , self.SpawnHighTimer * self.AliveFactor / 10 )
self.SpawnFunction = routines.scheduleFunction( self._Scheduler, { self }, timer.getTime() + 1, 1 )
end
end
--- Will stop the scheduled spawning scheduler.
function SPAWN:SpawnScheduleStop()
self:F( { self.SpawnTemplatePrefix } )
self.SpawnIsScheduled = false
end
--- Will spawn a group from a hosting unit. This function 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. --- Will spawn a group from a hosting unit. This function 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. -- Note that each point in the route assigned to the spawning group is reset to the point of the spawn.
@@ -1240,19 +1223,10 @@ end
function SPAWN:_Scheduler() function SPAWN:_Scheduler()
self:F( { "_Scheduler", self.SpawnTemplatePrefix, self.SpawnAliasPrefix, self.SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive } ) self:F( { "_Scheduler", self.SpawnTemplatePrefix, self.SpawnAliasPrefix, self.SpawnIndex, self.SpawnMaxGroups, self.SpawnMaxUnitsAlive } )
if self.SpawnInit or self.SpawnCurrentTimer == self.SpawnSetTimer then -- Validate if there are still groups left in the batch...
-- Validate if there are still groups left in the batch... self:Spawn()
self:Spawn()
self.SpawnInit = false return true
if self.SpawnIsScheduled == true then
--local ClientUnit = #AlivePlayerUnits()
self.AliveFactor = 1 -- ( 10 - ClientUnit ) / 10
self.SpawnCurrentTimer = 0
self.SpawnSetTimer = math.random( self.SpawnLowTimer * self.AliveFactor , self.SpawnHighTimer * self.AliveFactor )
end
else
self.SpawnCurrentTimer = self.SpawnCurrentTimer + 1
end
end end
function SPAWN:_SpawnCleanUpScheduler() function SPAWN:_SpawnCleanUpScheduler()
@@ -1284,4 +1258,6 @@ function SPAWN:_SpawnCleanUpScheduler()
end end
return true -- Repeat
end end