Fixed bug in Scheduler (a real nasty one) and SET_UNIT

This commit is contained in:
FlightControl
2016-07-23 20:26:08 +02:00
parent 906045a027
commit c10e293ff7
49 changed files with 104 additions and 63 deletions

View File

@@ -58,27 +58,27 @@ function SCHEDULER:New( TimeEventObject, TimeEventFunction, TimeEventFunctionArg
local self = BASE:Inherit( self, BASE:New() )
self:F2( { TimeEventObject, TimeEventFunction, TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds } )
self.TimeEventObject = TimeEventObject
self.TimeEventFunction = TimeEventFunction
self:Schedule( TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds )
self:Start()
self:Schedule( TimeEventObject, TimeEventFunction, TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds )
return self
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 TimeEventObject 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 TimeEventFunction The event function to be called when a timer event occurs. The event function needs to accept the parameters specified in TimeEventFunctionArguments.
-- @param #table TimeEventFunctionArguments Optional arguments that can be given as part of scheduler. The arguments need to be given as a table { param1, param 2, ... }.
-- @param #number StartSeconds Specifies the amount of seconds that will be waited before the scheduling is started, and the event function is called.
-- @param #number RepeatSecondsInterval Specifies the interval in seconds when the scheduler will call the event function.
-- @param #number RandomizationFactor Specifies a randomization factor between 0 and 1 to randomize the RepeatSecondsInterval.
-- @param #number StopSeconds Specifies the amount of seconds when the scheduler will be stopped.
-- @return #SCHEDULER self
function SCHEDULER:Schedule( TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds )
function SCHEDULER:Schedule( TimeEventObject, TimeEventFunction, TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds )
self:F2( { TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds } )
self.TimeEventObject = TimeEventObject
self.TimeEventFunction = TimeEventFunction
self.TimeEventFunctionArguments = TimeEventFunctionArguments
self.StartSeconds = StartSeconds
self.Repeat = false
@@ -88,6 +88,8 @@ function SCHEDULER:Schedule( TimeEventFunctionArguments, StartSeconds, RepeatSec
self.StartTime = timer.getTime()
self:Start()
return self
end
@@ -102,6 +104,9 @@ function SCHEDULER:Start()
end
if self.StartSeconds then
if self.ScheduleID then
timer.removeFunction( self.ScheduleID )
end
self.ScheduleID = timer.scheduleFunction( self._Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
end
@@ -139,6 +144,13 @@ function SCHEDULER:_Scheduler()
return errmsg
end
local StartTime = self.StartTime
local StopSeconds = self.StopSeconds
local Repeat = self.Repeat
local RandomizationFactor = self.RandomizationFactor
local RepeatSecondsInterval = self.RepeatSecondsInterval
local ScheduleID = self.ScheduleID
local Status, Result
if self.TimeEventObject then
@@ -147,26 +159,26 @@ function SCHEDULER:_Scheduler()
Status, Result = xpcall( function() return self.TimeEventFunction( unpack( self.TimeEventFunctionArguments ) ) end, ErrorHandler )
end
self:T( { self.TimeEventFunctionArguments, Status, Result, self.StartTime, self.RepeatSecondsInterval, self.RandomizationFactor, self.StopSeconds } )
self:T( { "Timer Event2 .. " .. self.ScheduleID, Status, Result, StartTime, RepeatSecondsInterval, RandomizationFactor, StopSeconds } )
if Status and ( ( Result == nil ) or ( Result and Result ~= false ) ) then
if self.Repeat and ( not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) ) then
if Repeat and ( not StopSeconds or ( StopSeconds and timer.getTime() <= StartTime + StopSeconds ) ) then
local ScheduleTime =
timer.getTime() +
self.RepeatSecondsInterval +
math.random(
- ( self.RandomizationFactor * self.RepeatSecondsInterval / 2 ),
( self.RandomizationFactor * self.RepeatSecondsInterval / 2 )
- ( RandomizationFactor * RepeatSecondsInterval / 2 ),
( RandomizationFactor * RepeatSecondsInterval / 2 )
) +
0.01
self:T( { self.TimeEventFunctionArguments, "Repeat:", timer.getTime(), ScheduleTime } )
return ScheduleTime -- returns the next time the function needs to be called.
else
timer.removeFunction( self.ScheduleID )
timer.removeFunction( ScheduleID )
self.ScheduleID = nil
end
else
timer.removeFunction( self.ScheduleID )
timer.removeFunction( ScheduleID )
self.ScheduleID = nil
end