aboutsummaryrefslogtreecommitdiff
path: root/advtrains/helpers.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2019-12-04 10:08:03 +0100
committerorwell96 <orwell@bleipb.de>2019-12-04 10:09:19 +0100
commit8d794525b3232dbe326043ee3d219ecabee04636 (patch)
tree21ceda1f78b5c943ade8309cd05d4eb6ec993530 /advtrains/helpers.lua
parente0662b2971e7d138fafff4ad709829656c8782e0 (diff)
downloadadvtrains-8d794525b3232dbe326043ee3d219ecabee04636.tar.gz
advtrains-8d794525b3232dbe326043ee3d219ecabee04636.tar.bz2
advtrains-8d794525b3232dbe326043ee3d219ecabee04636.zip
Fix repeated log file opening/closing (H#136)
Diffstat (limited to 'advtrains/helpers.lua')
0 files changed, 0 insertions, 0 deletions
ef='#n133'>133
-- scheduler.lua
-- Implementation of a Railway time schedule queue
-- In contrast to the LuaATC interrupt queue, this one can handle many different
-- event receivers. This is done by registering a callback with the scheduler

local ln = advtrains.lines
local sched = {}

local UNITS_THRESH = 10
local MAX_PER_ITER = 10

local callbacks = {}

-- Register a handler callback to handle scheduler items.
-- e - a handler identifier (corresponds to "handler" in enqueue() )
-- func - a function(evtdata) to be executed when a schedule item expires
--        evtdata - arbitrary data that has been passed into enqueue()
function sched.register_callback(e, func)
	callbacks[e] = func
end

--[[
{
	t = <railway time in seconds>
	e = <handler callback>
	d = <data table>
	u = <unit identifier>
}
The "unit identifier" is there to prevent schedule overflows. It can be, for example, the position hash
of a node or a train ID. If the number of schedules for a unit exceeds UNITS_THRESH, further schedules are
blocked.
]]--
local queue = {}

local units_cnt = {}

function sched.load(data)
	if data then
		for i,elem in ipairs(data) do
			table.insert(queue, elem)
			units_cnt[elem.u] = (units_cnt[elem.u] or 0) + 1
		end
		atlog("[lines][scheduler] Loaded the schedule queue,",#data,"items.")
	end
end
function sched.save()