2018-07-17 21:02:01 +02:00
-- Setting and clearing routes
2018-07-21 16:31:00 +02:00
-- TODO duplicate
local lntrans = { " A " , " B " }
local function sigd_to_string ( sigd )
return minetest.pos_to_string ( sigd.p ) .. " / " .. lntrans [ sigd.s ]
end
2018-10-29 20:06:04 +01:00
local asp_generic_free = {
2020-01-30 08:45:16 +01:00
main = - 1 ,
shunt = false ,
dst = false ,
2018-10-29 20:06:04 +01:00
info = { }
}
2018-07-21 16:31:00 +02:00
local ildb = advtrains.interlocking . db
2018-07-17 21:02:01 +02:00
local ilrs = { }
2018-10-29 20:06:04 +01:00
local sigd_equal = advtrains.interlocking . sigd_equal
2018-07-21 16:31:00 +02:00
-- table containing locked points
-- also manual locks (maintenance a.s.o.) are recorded here
-- [pts] = {
-- [n] = { [by = <ts_id>], rsn = <human-readable text>, [origin = <sigd>] }
-- }
ilrs.rte_locks = { }
2018-08-03 19:20:10 +02:00
ilrs.rte_callbacks = {
ts = { } ,
lck = { }
}
2018-07-17 21:02:01 +02:00
2018-07-21 16:31:00 +02:00
-- main route setting. First checks if everything can be set as designated,
-- then (if "try" is not set) actually sets it
-- returns:
-- true - route can be/was successfully set
2018-08-03 19:20:10 +02:00
-- false, message, cbts, cblk - something went wrong, what is contained in the message.
2018-08-12 13:23:36 +02:00
-- cbts: the ts id of the conflicting ts, cblk: the pts of the conflicting component
2018-07-21 16:31:00 +02:00
function ilrs . set_route ( signal , route , try )
if not try then
2018-08-12 13:23:36 +02:00
local tsuc , trsn , cbts , cblk = ilrs.set_route ( signal , route , true )
2018-07-21 16:31:00 +02:00
if not tsuc then
2018-08-12 13:23:36 +02:00
return false , trsn , cbts , cblk
2018-07-21 16:31:00 +02:00
end
end
2018-11-19 23:53:42 +01:00
2018-07-21 16:31:00 +02:00
-- we start at the tc designated by signal
local c_sigd = signal
local first = true
local i = 1
local rtename = route.name
local signalname = ildb.get_tcbs ( signal ) . signal_name
local c_tcbs , c_ts_id , c_ts , c_rseg , c_lckp
while c_sigd and i <=# route do
c_tcbs = ildb.get_tcbs ( c_sigd )
2019-02-12 22:17:43 +01:00
if not c_tcbs then
if not try then atwarn ( " Did not find TCBS " , c_sigd , " while setting route " , rtename , " of " , signal ) end
return false , " No TCB found at " .. sigd_to_string ( c_sigd ) .. " . Please reconfigure route! "
end
2018-07-21 16:31:00 +02:00
c_ts_id = c_tcbs.ts_id
if not c_ts_id then
if not try then atwarn ( " Encountered End-Of-Interlocking while setting route " , rtename , " of " , signal ) end
2018-08-12 13:23:36 +02:00
return false , " No track section adjacent to " .. sigd_to_string ( c_sigd ) .. " . Please reconfigure route! "
2018-07-21 16:31:00 +02:00
end
c_ts = ildb.get_ts ( c_ts_id )
c_rseg = route [ i ]
c_lckp = { }
if c_ts.route then
2018-08-12 13:23:36 +02:00
if not try then atwarn ( " Encountered ts lock during a real run of routesetting routine, at ts= " , c_ts_id , " while setting route " , rtename , " of " , signal ) end
2018-10-25 22:38:44 +02:00
return false , " Section ' " .. c_ts.name .. " ' already has route set from " .. sigd_to_string ( c_ts.route . origin ) .. " : \n " .. c_ts.route . rsn , c_ts_id , nil
2018-07-21 16:31:00 +02:00
end
if c_ts.trains and # c_ts.trains > 0 then
2018-08-12 13:23:36 +02:00
if not try then atwarn ( " Encountered ts occupied during a real run of routesetting routine, at ts= " , c_ts_id , " while setting route " , rtename , " of " , signal ) end
return false , " Section ' " .. c_ts.name .. " ' is occupied! " , c_ts_id , nil
2018-07-21 16:31:00 +02:00
end
for pts , state in pairs ( c_rseg.locks ) do
local confl = ilrs.has_route_lock ( pts , state )
local pos = minetest.string_to_pos ( pts )
2018-08-16 19:18:03 +02:00
if advtrains.is_passive ( pos ) then
local cstate = advtrains.getstate ( pos )
2018-07-21 16:31:00 +02:00
if cstate ~= state then
local confl = ilrs.has_route_lock ( pts )
if confl then
if not try then atwarn ( " Encountered route lock while a real run of routesetting routine, at position " , pts , " while setting route " , rtename , " of " , signal ) end
2018-08-12 13:23:36 +02:00
return false , " Lock conflict at " .. pts .. " , Held locked by: \n " .. confl , nil , pts
2018-07-21 16:31:00 +02:00
elseif not try then
2018-08-16 19:18:03 +02:00
advtrains.setstate ( pos , state )
2018-07-21 16:31:00 +02:00
end
end
if not try then
ilrs.add_route_lock ( pts , c_ts_id , " Route ' " .. rtename .. " ' from signal ' " .. signalname .. " ' " , signal )
c_lckp [ # c_lckp + 1 ] = pts
end
else
if not try then atwarn ( " Encountered route lock misconfiguration (no passive component) while a real run of routesetting routine, at position " , pts , " while setting route " , rtename , " of " , signal ) end
2018-08-12 13:23:36 +02:00
return false , " No passive component at " .. pts .. " . Please reconfigure route! "
2018-07-21 16:31:00 +02:00
end
end
-- reserve ts and write locks
if not try then
2018-10-29 20:06:04 +01:00
local nvar = c_rseg.next
if not route [ i + 1 ] then
-- We shouldn't use the "next" value of the final route segment, because this can lead to accidental route-cancelling of already set routes from another signal.
nvar = nil
end
2018-07-21 16:31:00 +02:00
c_ts.route = {
origin = signal ,
entry = c_sigd ,
rsn = " Route ' " .. rtename .. " ' from signal ' " .. signalname .. " ', segment # " .. i ,
first = first ,
}
c_ts.route_post = {
locks = c_lckp ,
2018-10-29 20:06:04 +01:00
next = nvar ,
2018-07-21 16:31:00 +02:00
}
2018-10-29 20:06:04 +01:00
if c_tcbs.signal then
c_tcbs.route_committed = true
2018-11-19 23:53:42 +01:00
c_tcbs.aspect = route.aspect or asp_generic_free
2018-10-29 20:06:04 +01:00
c_tcbs.route_origin = signal
advtrains.interlocking . update_signal_aspect ( c_tcbs )
end
2018-07-21 16:31:00 +02:00
end
-- advance
first = nil
c_sigd = c_rseg.next
i = i + 1
end
return true
end
-- Checks whether there is a route lock that prohibits setting the component
-- to the wanted state. returns string with reasons on conflict
function ilrs . has_route_lock ( pts )
-- look this up
local e = ilrs.rte_locks [ pts ]
if not e then return nil
elseif # e == 0 then
ilrs.rte_locks [ pts ] = nil
return nil
end
local txts = { }
for _ , ent in ipairs ( e ) do
txts [ # txts + 1 ] = ent.rsn
end
return table.concat ( txts , " \n " )
end
-- adds route lock for position
function ilrs . add_route_lock ( pts , ts , rsn , origin )
ilrs.free_route_locks_indiv ( pts , ts , true )
local elm = { by = ts , rsn = rsn , origin = origin }
if not ilrs.rte_locks [ pts ] then
ilrs.rte_locks [ pts ] = { elm }
else
table.insert ( ilrs.rte_locks [ pts ] , elm )
end
end
-- adds route lock for position
function ilrs . add_manual_route_lock ( pts , rsn )
local elm = { rsn = rsn }
if not ilrs.rte_locks [ pts ] then
ilrs.rte_locks [ pts ] = { elm }
else
table.insert ( ilrs.rte_locks [ pts ] , elm )
end
end
-- frees route locking for all points (components) that were set by this ts
function ilrs . free_route_locks ( ts , lcks , nocallbacks )
for _ , pts in pairs ( lcks ) do
ilrs.free_route_locks_indiv ( pts , ts , nocallbacks )
end
end
function ilrs . free_route_locks_indiv ( pts , ts , nocallbacks )
local e = ilrs.rte_locks [ pts ]
if not e then return nil
elseif # e == 0 then
ilrs.rte_locks [ pts ] = nil
return nil
end
local i = 1
while i <= # e do
if e [ i ] . by == ts then
2018-10-07 22:21:49 +02:00
--atdebug("free_route_locks_indiv",pts,"clearing entry",e[i].by,e[i].rsn)
2018-07-21 16:31:00 +02:00
table.remove ( e , i )
else
i = i + 1
end
end
2018-08-12 13:23:36 +02:00
-- This must be delayed, because this code is executed in-between a train step
-- TODO use luaautomation timers?
2018-08-13 11:27:38 +02:00
if not nocallbacks then
minetest.after ( 0 , ilrs.update_waiting , " lck " , pts )
2020-07-07 14:25:47 +02:00
minetest.after ( 0.5 , advtrains.set_fallback_state , minetest.string_to_pos ( pts ) )
2018-08-13 11:27:38 +02:00
end
2018-07-21 16:31:00 +02:00
end
-- frees all route locks, even manual ones set with the tool, at a specific position
function ilrs . remove_route_locks ( pts , nocallbacks )
ilrs.rte_locks [ pts ] = nil
2018-08-16 19:18:03 +02:00
-- This must be delayed, because this code is executed in-between a train step
-- TODO use luaautomation timers?
if not nocallbacks then
minetest.after ( 0 , ilrs.update_waiting , " lck " , pts )
end
2018-07-21 16:31:00 +02:00
end
-- starting from the designated sigd, clears all subsequent route and route_post
-- information from the track sections.
-- note that this does not clear the routesetting status from the entry signal,
-- only from the ts's
function ilrs . cancel_route_from ( sigd )
-- we start at the tc designated by signal
local c_sigd = sigd
local c_tcbs , c_ts_id , c_ts , c_rseg , c_lckp
while c_sigd do
2018-10-29 20:06:04 +01:00
--atdebug("cancel_route_from: at sigd",c_sigd)
2018-07-21 16:31:00 +02:00
c_tcbs = ildb.get_tcbs ( c_sigd )
2019-01-22 12:07:53 +01:00
if not c_tcbs then
atwarn ( " Failed to cancel route, no TCBS at " , c_sigd )
return false
end
2019-03-15 16:40:23 +01:00
--atdebug("cancelling",c_ts.route.rsn)
-- clear signal aspect and routesetting state
c_tcbs.route_committed = nil
c_tcbs.aspect = nil
c_tcbs.routeset = nil
c_tcbs.route_auto = nil
c_tcbs.route_origin = nil
advtrains.interlocking . update_signal_aspect ( c_tcbs )
2018-07-21 16:31:00 +02:00
c_ts_id = c_tcbs.ts_id
2019-01-22 12:07:53 +01:00
if not c_tcbs then
atwarn ( " Failed to cancel route, end of interlocking at " , c_sigd )
return false
end
2018-07-21 16:31:00 +02:00
c_ts = ildb.get_ts ( c_ts_id )
if not c_ts
or not c_ts.route
or not sigd_equal ( c_ts.route . entry , c_sigd ) then
2018-10-29 20:06:04 +01:00
--atdebug("cancel_route_from: abort (eoi/no route):")
2019-01-22 12:07:53 +01:00
return false
2018-07-21 16:31:00 +02:00
end
c_ts.route = nil
if c_ts.route_post then
advtrains.interlocking . route.free_route_locks ( c_ts_id , c_ts.route_post . locks )
c_sigd = c_ts.route_post . next
else
c_sigd = nil
end
c_ts.route_post = nil
2018-08-12 13:23:36 +02:00
minetest.after ( 0 , advtrains.interlocking . route.update_waiting , " ts " , c_ts_id )
2018-07-21 16:31:00 +02:00
end
2018-10-29 20:06:04 +01:00
--atdebug("cancel_route_from: done (no final sigd)")
2019-01-22 12:07:53 +01:00
return true
2018-07-21 16:31:00 +02:00
end
2018-08-03 19:20:10 +02:00
-- TCBS Routesetting helper: generic update function for
-- route setting
2019-01-15 17:23:03 +01:00
-- Call this function to set and cancel routes!
-- sigd, tcbs: self-explanatory
-- newrte: If a new route should be set, the route index of it (in tcbs.routes). nil otherwise
-- cancel: true in combination with newrte=nil causes cancellation of the current route.
2018-08-03 19:20:10 +02:00
function ilrs . update_route ( sigd , tcbs , newrte , cancel )
2018-10-29 20:06:04 +01:00
--atdebug("Update_Route for",sigd,tcbs.signal_name)
2020-06-05 13:39:34 +02:00
local has_changed_aspect = false
2018-10-29 20:06:04 +01:00
if tcbs.route_origin and not sigd_equal ( tcbs.route_origin , sigd ) then
--atdebug("Signal not in control, held by",tcbs.signal_name)
return
end
2018-08-03 19:20:10 +02:00
if ( newrte and tcbs.routeset and tcbs.routeset ~= newrte ) or cancel then
if tcbs.route_committed then
2018-10-07 22:21:49 +02:00
--atdebug("Cancelling:",tcbs.routeset)
2018-08-03 19:20:10 +02:00
advtrains.interlocking . route.cancel_route_from ( sigd )
end
tcbs.route_committed = nil
2018-10-29 20:06:04 +01:00
tcbs.aspect = nil
2020-06-05 13:39:34 +02:00
has_changed_aspect = true
2018-10-29 20:06:04 +01:00
tcbs.routeset = nil
2018-08-12 16:58:47 +02:00
tcbs.route_auto = nil
2018-10-29 20:06:04 +01:00
tcbs.route_rsn = nil
2018-08-03 19:20:10 +02:00
end
if newrte or tcbs.routeset then
2018-10-30 16:19:37 +01:00
if tcbs.route_committed then
return
end
2018-08-03 19:20:10 +02:00
if newrte then tcbs.routeset = newrte end
2018-10-07 22:21:49 +02:00
--atdebug("Setting:",tcbs.routeset)
2018-08-03 19:20:10 +02:00
local succ , rsn , cbts , cblk = ilrs.set_route ( sigd , tcbs.routes [ tcbs.routeset ] )
if not succ then
tcbs.route_rsn = rsn
2018-10-07 22:21:49 +02:00
--atdebug("Routesetting failed:",rsn)
2018-08-03 19:20:10 +02:00
-- add cbts or cblk to callback table
2018-08-12 13:23:36 +02:00
if cbts then
2018-10-07 22:21:49 +02:00
--atdebug("cbts =",cbts)
2018-08-12 13:23:36 +02:00
if not ilrs.rte_callbacks . ts [ cbts ] then ilrs.rte_callbacks . ts [ cbts ] = { } end
advtrains.insert_once ( ilrs.rte_callbacks . ts [ cbts ] , sigd , sigd_equal )
end
if cblk then
2018-10-07 22:21:49 +02:00
--atdebug("cblk =",cblk)
2018-08-12 13:23:36 +02:00
if not ilrs.rte_callbacks . lck [ cblk ] then ilrs.rte_callbacks . lck [ cblk ] = { } end
advtrains.insert_once ( ilrs.rte_callbacks . lck [ cblk ] , sigd , sigd_equal )
end
2018-08-03 19:20:10 +02:00
else
2018-10-07 22:21:49 +02:00
--atdebug("Committed Route:",tcbs.routeset)
2020-06-05 13:39:34 +02:00
has_changed_aspect = true
2018-08-12 13:23:36 +02:00
end
end
2020-06-05 13:39:34 +02:00
if has_changed_aspect then
-- FIX: prevent an minetest.after() loop caused by update_signal_aspect dispatching path invalidation, which in turn calls ARS again
advtrains.interlocking . update_signal_aspect ( tcbs )
end
2019-02-05 11:40:24 +01:00
advtrains.interlocking . update_player_forms ( sigd )
2018-08-12 13:23:36 +02:00
end
-- Try to re-set routes that conflicted with this point
-- sys can be one of "ts" and "lck"
-- key is then ts_id or pts respectively
function ilrs . update_waiting ( sys , key )
2018-10-07 22:21:49 +02:00
--atdebug("update_waiting:",sys,".",key)
2018-08-12 13:23:36 +02:00
local t = ilrs.rte_callbacks [ sys ] [ key ]
ilrs.rte_callbacks [ sys ] [ key ] = nil
if t then
for _ , sigd in ipairs ( t ) do
2018-10-07 22:21:49 +02:00
--atdebug("Updating", sigd)
2018-08-12 13:23:36 +02:00
-- While these are run, the table we cleared before may be populated again, which is in our interest.
-- (that's the reason we needed to copy it)
local tcbs = ildb.get_tcbs ( sigd )
if tcbs then
ilrs.update_route ( sigd , tcbs )
end
2018-08-03 19:20:10 +02:00
end
end
end
2018-07-21 16:31:00 +02:00
advtrains.interlocking . route = ilrs