diff options
Diffstat (limited to 'auto_yards/init_code.lua')
-rw-r--r-- | auto_yards/init_code.lua | 132 |
1 files changed, 97 insertions, 35 deletions
diff --git a/auto_yards/init_code.lua b/auto_yards/init_code.lua index 92b69bd..0a4312c 100644 --- a/auto_yards/init_code.lua +++ b/auto_yards/init_code.lua @@ -4,39 +4,79 @@ if event.init then S.yards = { --[[ yard info template - yard_id = { + yard_id = { + yard_name = string, active_indicator_pos = POS(), dir_indicator_pos = POS(), error_indicator_pos = POS(), headshunt_max = number, notify = empty table, notify_pos = pos, + + -- reserved vars -- + last_id = string (the id of the last train that entered the yard last) + arrival_length = number + arrival_time = string (RWT) + departure_length = number or "?" (set when train arrives, until train departs) + departure_time = string (RWT) + rts = bool (whether the entering train will exit the way it came in) }, ]]-- TY = { + yard_name = "Trisiston", active_indicator_pos = POS(-4025,14,-2659), dir_indicator_pos = POS(-4025,12,-2665), error_indicator_pos = POS(-4025,13,-2671), headshunt_max = 5, - + notify = {}, + notify_pos = POS(-4023,12,-2660), + arrival_time = rwt.now(), + departure_time = rwt.now() }, BY = { + yard_name = "Banach", active_indicator_pos = POS(-2002,3,-1099), dir_indicator_pos = POS(-2009,3,-1101), error_indicator_pos = POS(-1999,3,-1099), headshunt_max = 5, notify = {}, - notify_pos = POS(-2004,2,-1101) + notify_pos = POS(-2004,2,-1101), + arrival_time = rwt.now(), + departure_time = rwt.now() + }, + ARC = { + yard_name = "Arcadius", + active_indicator_pos = POS(-1952,16,840), + dir_indicator_pos = POS(-1950,16,840), + error_indicator_pos = POS(-1948,16,840), + headshunt_max = 5, + notify = {}, + notify_pos = POS(-1946,16,840), + arrival_time = rwt.now(), + departure_time = rwt.now() }, IP = { + yard_name = "Ipswich", active_indicator_pos = POS(1179,16,3848), dir_indicator_pos = POS(1177,16,3855), error_indicator_pos = POS(1179,16,3850), headshunt_max = 5, notify = {}, - notify_pos = POS(1178,16,3851) - + notify_pos = POS(1178,16,3851), + arrival_time = rwt.now(), + departure_time = rwt.now() }, + CAN = { + yard_name = "Cannery", + active_indicator_pos = POS(-594,26,2486), + dir_indicator_pos = POS(-594,26,2484), + error_indicator_pos = POS(-594,26,2482), + headshunt_max = 2, + --notify = {}, + --notify_pos = POS(-594,26,2485), + arrival_time = rwt.now(), + departure_time = rwt.now() + } -- HY = { -- active_indicator_pos = POS(-4025,14,-2659), -- dir_indicator_pos = POS(-4025,13,-2665), @@ -75,12 +115,12 @@ F.has_rc_match = function(query,rc_list) -- query = pattern string, single entry if not atc_id then return false end if rc_list == "" or query == nil or query=="" then return false end if not rc_list then rc_list = F.get_rc_safe() end - + local rc = {} for v in rc_list:gmatch("("..query..")") do table.insert(rc,v) end - + if rc[1] == true then return true, rc else @@ -102,11 +142,11 @@ F.remove_rc = function(rc_list,arrow_mode) -- rc_list = string eg: "rc1 rc2 rc3" -- true: with arrow direction -- false: against arrow direction -- nil: ignores arrow direction - + if not atc_id then return false end if not rc_list then return false end - - if (arrow_mode == nil) or (atc_arrow == arrow_mode) then + + if (arrow_mode == nil) or (atc_arrow == arrow_mode) then -- prep rc_list to useable format local rc_remove = {} if type(rc_list) == "string" then @@ -118,7 +158,7 @@ F.remove_rc = function(rc_list,arrow_mode) -- rc_list = string eg: "rc1 rc2 rc3" rc_remove[word] = true end end - + -- remove codes from train's rc local rc = F.get_rc_safe() local reinsert = {} @@ -146,7 +186,8 @@ end ---------------------------------------------------------------------------------------------- -- Trackside Functions -F.yard_arrival = function(yard_id,this_dir) -- arrow points towards yard +-- this_dir = points towards Origin/Junction Yard +F.yard_arrival = function(yard_id,this_dir, force_rts) -- arrow points towards yard local yard = S.yards[yard_id] --yard ref if F.has_rc(yard_id.."_NOSHUNT") then return end @@ -160,16 +201,28 @@ F.yard_arrival = function(yard_id,this_dir) -- arrow points towards yard F.indicator(yard.active_indicator_pos,true) F.add_rc({yard_id.."_ARRIVE"}) local rts = false - if F.has_rc(yard_id.."_RTS") then --save the RTS flag as it's removed during the arrival procedure + if force_rts then -- yard is designated as a terminus yard. all trains MUST rts + F.add_rc(yard_id.."_RTS") + rts = true + elseif F.has_rc(yard_id.."_RTS") then --save the RTS flag as it's removed during the arrival procedure F.add_rc({yard_id.."_HAS_RTS"}) rts = true end atc_set_ars_disable(false) atc_send("S6") - - print(rwt.to_string(rwt.now())) - print("YARD "..yard_id..": Train "..atc_id.." enters from the "..tostring(this_dir).." direction and "..((rts and "will") or "won't").." return in the same direction") - print("YARD "..yard_id..": Length "..train_length()) + + S.yards[yard_id].last_id = atc_id + S.yards[yard_id].arrival_length = train_length() + S.yards[yard_id].arrival_time = rwt.now() + S.yards[yard_id].departure_length = "?" + S.yards[yard_id].departure_time = rwt.now() + S.yards[yard_id].rts = rts + + if S.print_debug then + print(rwt.to_string(rwt.now())) + print("YARD "..yard_id..": Train "..atc_id.." enters from the "..tostring(this_dir).." direction and will exit in the "..tostring(rts).." direction") + print("YARD "..yard_id..": Length "..train_length()) + end return end @@ -182,14 +235,16 @@ F.yard_arrival = function(yard_id,this_dir) -- arrow points towards yard if event.train and atc_arrow then if F.indicator(yard.active_indicator_pos) then - print(rwt.to_string(rwt.now())) - print("YARD "..yard_id..": Train "..atc_id.." has arrived and has to wait for the yard to deactivate.") + if S.print_debug then + print(rwt.to_string(rwt.now())) + print("YARD "..yard_id..": Train "..atc_id.." has arrived from the "..tostring(this_dir).." direction and has to wait for the yard to deactivate.") + end schedule_in(";10","recheck") return else enter_yard() return true - end + end end if event.schedule then @@ -208,7 +263,7 @@ F.classification = function(yard_id, this_dir) -- arrow points towards headshunt if not F.indicator(yard.active_indicator_pos) then return end if F.has_rc(yard_id.."_NOSHUNT") then return end - + -- this_dir == true for north end, false for south end if F.indicator(yard.active_indicator_pos) then if atc_arrow then -- loco is at working end @@ -235,8 +290,8 @@ F.classification = function(yard_id, this_dir) -- arrow points towards headshunt F.indicator(yard.dir_indicator_pos,not F.indicator(yard.dir_indicator_pos)) F.remove_rc({yard_id.."_RTS"}) F.add_rc({yard_id.."_AROUND"}) -- send loco around to the other end - atc_set_ars_disable(false) end + atc_set_ars_disable(false) return -- train has departed for headshunt/around end --else train has arrived from wagon count and headshunt/around F.remove_rc({yard_id.."_ARRIVE"}) @@ -262,6 +317,7 @@ F.classification = function(yard_id, this_dir) -- arrow points towards headshunt unset_autocouple() end else -- train entering from the far end. set autocouple so it pushes all the way through to the bounce + atc_set_ars_disable(true) set_autocouple() end end @@ -302,7 +358,7 @@ F.headshunt_yard = function(yard_id,this_dir) -- arrow points toward yard return end end - + --else let train proceed forward to the exit controller without a rake end --else train has already collected rake. let it exit yard else --train has bounced @@ -314,7 +370,7 @@ F.headshunt_yard = function(yard_id,this_dir) -- arrow points toward yard return end --if at wrong end, do nothing. train will follow <yard_id>_AROUND ARS to relevant headshunt end -- if doesn't have <yard_id>_AROUND then let ARS classify the train - + atc_set_ars_disable(false) end return @@ -338,7 +394,7 @@ end F.headshunt_exit = function(yard_id,this_dir) -- arrow points out of yard local yard = S.yards[yard_id] --yard ref - + __approach_callback_mode = 1 if event.approach and not event.has_entered then atc_set_ars_disable(true) @@ -348,22 +404,25 @@ F.headshunt_exit = function(yard_id,this_dir) -- arrow points out of yard if not F.indicator(yard.active_indicator_pos) then return end if F.has_rc(yard_id.."_NOSHUNT") then return end - + if event.train then if atc_arrow and (F.indicator(yard.dir_indicator_pos) == this_dir) then if F.has_rc(yard_id.."_DEPART") then - print(rwt.to_string(rwt.now())) - print("YARD "..yard_id..": Train "..atc_id.." has a length of "..train_length()) - print("YARD "..yard_id..": It will depart in the "..tostring(this_dir).." direction.") + if S.print_debug then + print(rwt.to_string(rwt.now())) + print("YARD "..yard_id..": Train "..atc_id.." has a length of "..train_length()) + print("YARD "..yard_id..": It will depart in the "..tostring(this_dir).." direction.") + end + S.yards[yard_id].departure_length = train_length() unset_autocouple() F.remove_rc({yard_id.."_FINAL_COLLECT",yard_id.."_DEPART"}) F.remove_rc_match(yard_id.."_LOCOS_%d+") F.remove_rc_match(yard_id.."_WAGONS_%d+") - + if yard.notify_pos then interrupt_pos(yard.notify_pos,"notify") end - + if F.has_rc(yard_id.."_HAS_RTS") then --reinsert the RTS flag to fully return RC list to starting F.remove_rc(yard_id.."_HAS_RTS") F.add_rc({yard_id.."_RTS"}) @@ -384,8 +443,11 @@ F.headshunt_exit = function(yard_id,this_dir) -- arrow points out of yard if event.msg == "deactivate_check" then if not atc_id then F.indicator(yard.active_indicator_pos,false) - print(rwt.to_string(rwt.now())) - print("YARD "..yard_id..": yard Disabled") + S.yards[yard_id].departure_time = rwt.now() + if S.print_debug then + print(rwt.to_string(rwt.now())) + print("YARD "..yard_id..": yard Disabled") + end else schedule_in(";05","deactivate_check") return @@ -399,7 +461,7 @@ F.lane_EOL = function(yard_id,this_dir) -- arrow points towards headshunt if not F.indicator(yard.active_indicator_pos) then return end if F.has_rc(yard_id.."_NOSHUNT") then return end - + if atc_arrow then if F.indicator(yard.dir_indicator_pos) == this_dir then --train has bounced and needs to leave the rake or depart with it if F.has_rc(yard_id.."_LAST_CLASS") then -- this is the last clasification move @@ -413,7 +475,7 @@ F.lane_EOL = function(yard_id,this_dir) -- arrow points towards headshunt end --if has FINAL_COLLECT then don't split off the loco. take the full rake F.add_rc({yard_id.."_HEADSHUNT"}) else --train needs to bounce - atc_send("B0WRD1S4") + atc_send("S0WRD1S4") end else if F.indicator(yard.dir_indicator_pos) == this_dir then |