diff options
author | autocommitter <autocommitter@linux-forks.de> | 2024-04-22 14:52:07 +0200 |
---|---|---|
committer | autocommitter <autocommitter@linux-forks.de> | 2024-04-22 14:52:07 +0200 |
commit | c21692e0a830e434675014dd7b54736d180ce71d (patch) | |
tree | e928ac06b872bb5fa7697736f8cb78873e1424d2 /Crossroads/init_code.lua | |
parent | c6612d4e4b59a03d097a494eee4c714f8b88f123 (diff) | |
download | luaatc_envs-c21692e0a830e434675014dd7b54736d180ce71d.tar.gz luaatc_envs-c21692e0a830e434675014dd7b54736d180ce71d.tar.bz2 luaatc_envs-c21692e0a830e434675014dd7b54736d180ce71d.zip |
State at 2020-06-24
Diffstat (limited to 'Crossroads/init_code.lua')
-rw-r--r-- | Crossroads/init_code.lua | 319 |
1 files changed, 210 insertions, 109 deletions
diff --git a/Crossroads/init_code.lua b/Crossroads/init_code.lua index 185eef7..ec07d95 100644 --- a/Crossroads/init_code.lua +++ b/Crossroads/init_code.lua @@ -1,24 +1,17 @@ ---[[ -This environment has programs that can be separated to three parts: -I) Common functions -II) Common variables -III) ATL/CRT Bronze Line -]] - --- I) Common Functions if S.trains == nil then S.trains = {} end -if S.corrratio == nil then S.corrratio = 1 end -if S.corrratio > 1 then S.corrratio = 1 end -if S.corrratio <= 0 then S.corrratio = 1 end + +F.odj = {} -- F.dst - Get summer time function F.dst() if os.date().isdst then return "CEST" else return "CET" end end + -- F.error - Send error with error code function F.error(errorcode) error(F.errlist[errorcode]) end + --[[ F.stnbasic - Basis for all station functions: (By default, nil is defined as false or "no changes") stn = Station Code @@ -32,48 +25,53 @@ predepart = Function to execute before departure postdepart = Function to execute after departure next = Next stop ]] + function F.stnbasic(stn, side, optime, reverse, acc, out, reventry, predepart, postdepart, next,track) - if event.train then + if not event then return end + if event.type == "train" then if atc_arrow then - local t_start = os.clock() + if (get_rc() or ""):match ("FREIGHT") then + atc_send (reverse and "RSM" or "SM") + return + end local cmdstr="" - local corr = S.corrratio - local t_sched = ((optime or 15)+1)-- commented out due to trouble *corr - if out~=nil then atc_set_text_outside(out) end + local t_sched = ((optime or 15) + 1) + if out then + atc_set_text_outside (out) + end local intext = "Unknown Station" - if stn and F.stnlist[stn] then intext = F.stnlist[stn] end - --intext = intext .. "\nCorrection: " .. S.corrratio - atc_set_text_inside(intext) - cmdstr="BBWO"..(side or "R") - if predepart~=nil then predepart() end - S.trains[atc_id] = "P "..stn.." "..(track or "x") - cmdstr=cmdstr.."D"..math.floor((optime or 15))-- comment out due to trouble *S.corrratio) - cmdstr=cmdstr.."OCD1" - if reverse then cmdstr=cmdstr.."R" end - cmdstr=cmdstr.."S"..(acc or "M") - atc_send(cmdstr) - interrupt(t_sched,{sched=t_sched,start=t_start,corr=corr}) - else - if reventry==nil then F.error("wrong_direction") - else - if not reventry then F.error("wrong_direction") end + if stn then + intext = F.stnlist[stn] or stn + end + atc_set_text_inside (intext) + cmdstr = "BBWO" .. (side or "R") + if type (predepart) == "function" then + predepart () + end + S.trains[atc_id] = { + v = 1, + l = (get_line() or ""), + s = stn, + t = (trk or "N/A"), + } + cmdstr = cmdstr .."D" .. math.floor((optime or 15)) .. "OCD1" + if reverse then + cmdstr = cmdstr .. "R" end + cmdstr = cmdstr .. "S" .. (acc or "M") + atc_send (cmdstr) + interrupt (t_sched,"") end - end - if event.int then - if atc_id ~= nil then + elseif event.type == "int" then + if atc_id then local intext = "" - if next ~= nil then intext = "Next stop: "..(F.stnlist[next] or "???") end - if postdepart~=nil then postdepart() end - --[[ - if event.msg and event.msg.start and event.msg.sched and event.msg.corr then - local t_end = os.clock() - S.corrratio = event.msg.sched/event.msg.corr/(t_end-event.msg.start) - if S.corrratio > 1 then S.corrratio = 1 end + if next then + intext = "Next stop: " .. (F.stnlist[next] or next) end - ]] - --atc_set_text_inside(intext.."\nCorrection: "..S.corrratio) - atc_set_text_inside(intext) + if type (postdepart) == "function" then + postdepart () + end + atc_set_text_inside (intext) end end end @@ -110,11 +108,76 @@ function F.brk(dir) end end ---[[ F.eval - similar to the ? : operator in C -expr = the expression to evaluate -rettrue = the value to return if expr is true -retfalse = the value to return if expr is false -]] +-- Second version of the station function +function F.stn2gen(stn, trk, door, ret, chout) + if not stn then return end + if not trk then return end + if not door then return end + if door~="L" and door~="R" and door~="C" then return end + if event.type~="train" then return end + if not atc_arrow then return end + if not F.stndet[stn] then return end + if not F.stndet[stn][trk] then return end + local t = F.stndet[stn][trk] + local stop = false + if #t==0 then return end + local l = get_line() + if (not l) or l==" " then return end + if (get_rc() or ""):match("FREIGHT") then + atc_send (ret and "BBWRSM" or "SM") + return + end + for i = 1, #t, 1 do + if t[i][1]==l then + stop = true + atc_send("BBWO"..door.."D15OCD1"..(ret and "R" or "").."SM") + local intext = (F.stnlist[stn] or stn).."\n"..(F.stnlist[t[i][2]] and "Next station: "..F.stnlist[t[i][2]]..(t[i][3] and " (track "..t[i][3]..")" or "") or "End of Line") + local outtext = string.format("%s (%06d)",F.lines[l].name or l, atc_id) + if t[i][4] then outtext = outtext.." - "..(F.stnlist[t[i][4]] or t[i][4]) end + atc_set_text_inside(intext) + if chout then atc_set_text_outside(outtext) end + S.trains[atc_id] = { + v = 2, + l = (get_line() or ""), + s = stn, + t = (trk or "N/A"), + } + break + end + end + if (not stop) then + atc_send(ret and "BBWRSM" or "SM") + end +end + +function F.disp2gen(stn) + if not stn then return end + if not F.stndet[stn] then return end + local s = F.stndet[stn] + local d={{(F.stnlist[stn] or stn).."\nTrack Line\n", os.date("%Y-%m-%d %H:%M %z", os.time()).."\nDestination\n"}} + local c=2 + local t={} + for i,_ in pairs(s) do t[#t+1]=i end + table.sort(t) + for i = 1,#t,1 do + local trkname = t[i] + local trk = s[trkname] + for j = 1,#trk,1 do + local det = trk[j] + local r = (c%4==0) and (c/4+1) or ((c-c%4)/4+1) + if not d[r] then d[r]={"",""} end + d[r][1] = d[r][1] .. string.format("%-8s %s\n", tostring(trkname), F.lines[det[1]].name) + d[r][2] = d[r][2] .. (F.lines[det[1]].ring and "Ring Line" or (F.stnlist[det[4]] or det[4] or "")).."\n" + c=c+1 + end + end + for i = 1, #d, 1 do + for j = 1, #d[1], 1 do + digiline_send("d_"..tostring(i).."_"..tostring(j), d[i][j]) + end + end +end + function F.eval(expr, rettrue, retfalse) if expr then return rettrue else return retfalse end end @@ -130,11 +193,7 @@ function F.checkpoint(name,arrow,opp) end end ---[[ II) Common Variables -F.stnlist = List of stations -F.errlist = List of errors -F.depint = Departure intervals -]] + F.stnlist = { cg = "Colored Grasses", clockwise = "Clockwise Route", @@ -146,12 +205,12 @@ F.stnlist = { crbfsw = "Crossroads Station St. West", crch = "Crossroads City Hall", crchs = "Crossroads City Hall South", - crmtrail = "Crossroads Mountain Railway Termius", - crnsw = "CR-North Station St. 9th Alley", + crmtrail = "Crossroads Mountain Railway Terminus", + crnsw = "CR North Station St. 9th Alley", crrathaus = "Crossroads City Hall", crsmacker = "Crossroads Smacker's Station", - crwm = "Crossroads-West Mountains", - crzn = "Crossroads-Zentrum/Nordstadt", + crwm = "Crossroads West Mountains", + crzn = "Crossroads Central/North", elchateau = "Erstaziland-Chateau d'Erstazi", elgp = "Erstaziland-Greener Pastures", elsf = "Erstaziland-Salt Factory", @@ -159,81 +218,123 @@ F.stnlist = { mushroom = "Mushroom Land", neverbuild = "Neverbuild", nvbcentral = "Neverbuild Central", - nvbold = "Neverbuild Old Termius", + nvbold = "Neverbuild Old Terminus", nvboutskirts = "Neverbuild Outskirts", oc = "Ocean City", - occh = "Ocean City, City Hall", - occrt = "Ocean City, CRT Office", - ocmushroom = "Ocean City, Mushroom Market", + occh = "Ocean City City Hall", + occrt = "Ocean City CRT Office", + ocmushroom = "Ocean City Mushroom Market", ocoutskirts = "Ocean City Outskirts", - phwest = "Personhood, West", + phsc = "Personhood Southern Crossing", + phwest = "Personhood West", scc = "Silver Coast Central", scn = "Silver Coast North", scs = "Silver Coast South", thecube = "The Cube", } + +F.lines = { + ["AB"] = { + name = "ATL Bronze Line", + ring = false, + [1] = { {"crch","A1"}, --[[ {"crwm","3"}, ]] {"scs","N"}, {"scc","N"}, {"scn","N"}, {"cg","1"}, {"thecube","4"}, {"phsc","N"}, {"phwest","7"} }, + [2] = { {"phwest","7"}, {"phsc","S"}, {"thecube","1"}, {"cg","2"}, {"scn","S"}, {"scc","S"}, {"scs","S"}, --[[ {"crwm","4"}, ]] {"crch","A1"} }, + }, + ["AZ"] = { + name = "ATL Zinc Line", + ring = false, + [1] = { {"evo","N/A"}, {"scs","N"}, {"scc","N"}, {"scn","N"}, {"oc","N"}, {"elgp","N"}, {"elsf","E"}, {"elchateau","S"} }, + [2] = { {"elchateau","S"}, {"elsf","W"}, {"elgp","S"}, {"oc","S"}, {"scn","S"}, {"scc","S"}, {"scs","S"}, {"evo","N/A"} }, + }, + ["CRT1"] = { + name = "CRT 1", + ring = false, + [1] = { {"crsmacker", "R3"}, {"crch", "U1"}, {"crchs", "W"}, {"cras", "N"} }, + [2] = { {"cras", "N"}, {"crchs", "E"}, {"crch", "U2"}, {"crsmacker", "R3"} }, + }, +} + +F.stndet = {} +for i, l in pairs(F.lines) do + for j = 1, #l, 1 do + local dir = l[j] + for k = 1, #dir, 1 do + local s = dir[k][1] + local t = dir[k][2] + if not F.stndet[s] then F.stndet[s] = {} end + if not F.stndet[s][t] then F.stndet[s][t] = {} end + if k~=#dir or l.ring then + local nxt = dir[k==#dir and 1 or k+1] + F.stndet[s][t][#F.stndet[s][t]+1] = {i, nxt[1], nxt[2], dir[#dir][1]} + else + local addent = true + for m = 1, #l, 1 do addent = addent and (l[m][1]==dir[k]) end + if addent then F.stndet[s][t][#F.stndet[s][t]+1] = {i, nil, nil, s} end + end + end + end +end + F.errlist = { + ["incorrect_setup"] = "Incorrect LuaATC track setup!", ["runaway_train"] = "Runaway train found!", ["train_disappeared"] = "Train has disappeared!", ["unexpected_train"] = "Train is not expected to pass!", ["wrong_direction"] = "Train passed in wrong direction!", } + F.depint = { ["AB"] = 60, ["AG"] = 120, ["CRT1"] = 30, } --- ATL/CRT Bronze Line - --- Colored Grasses -F.ab_cgw="--:-- "..F.dst() -F.ab_cge="--:-- "..F.dst() -F.ab_status = "Irregular operation" - -F.cg_disp = function() - if event.digiline then - if event.digiline then if not event.channel=="upd" then return end end - local t=os.date() - local cg="Colored Grasses\nATL/CRT Bronze Line\n" - local str = string.format("%26s\n%26s\n",os.date("%Y-%m-%d %H:%M %z",os.time()),F.ab_status) - digiline_send("bstg1-1",cg.."<-- Personhood West\n[1] via The Cube") - digiline_send("bstg1-2",string.format("%s%27s\n%27s",str,"Crossroads City Hall -->","via Silver Coast [2]")) - digiline_send("bstg2-1",cg.."<-- Crossroads City Hall\n[2] via Silver Coast") - digiline_send("bstg2-2",string.format("%s%27s\n%27s",str,"Personhood West -->","via The Cube [1]")) - digiline_send("bstg3","Scheduled departure (Track\nScheduled departure (Track") - digiline_send("bstg4",os.date("1): %H:%M %z%n2): %H:%M %z",os.time()+60)) - return - end - return -end -function F.cg_stn(gleis) - if event.train and atc_arrow then - if get_line()~="AB" then atc_send("SM") return end - atc_send("B0 W OR") - local time=os.date() - if time.sec>50 then time.sec=time.sec-11 end - local m="" - if time.min>58 then - m = string.format("%s%02d:%02d", m, F.eval(time.hour > 22, time.hour - 24, time.hour) + 1, time.min - 59) +F.dpts = { + ['CRT1'] = POS(1835,5,1000), + ['AG'] = POS(1399,4,2020), +} + +function F.odd(ln) + if event.type=='ext_int' then + if atc_id then + set_rc(event.message.rc) + atc_send('SM') else - m = string.format("%s%02d:%02d", m, time.hour, time.min + 1) + if type(F.odj[ln])~='table' then F.odj[ln] = {} end + for i = 1,(#F.odj[ln]) do + if F.odj[ln][i]==event then return end + end + table.insert(F.odj[ln],1,event) end - m = m .. " " .. F.dst() - atc_set_text_inside("Colored Grasses\nThis train will depart at: "..m) - interrupt(60-time.sec,"depart") - if gleis==1 then F.ab_cgw=m - else F.ab_cge=m + end + if event.type=='train' then + if F.odj[ln] and #F.odj[ln]~=0 then + ev = table.remove(F.odj[ln]) + if ev then + set_rc(ev.message.rc) + atc_send('BBWRSM') + return + else + F.odj[ln] = {} + end end + atc_send('BBWR') end - if event.int and event.message=="depart" then - atc_set_text_inside("") - time = os.date() - local nxt = string.format("%02d:%02d %s", F.eval(time.min>58, F.eval(time.hour>22, time.hour-23, time.hour+1), time.hour), F.eval(time.min>58, time.min-59, time.min+1), F.dst()) - atc_send("OC D1 SM") - if gleis==1 then F.ab_cgw=nxt - else F.ab_cge=nxt +end + +function F.ods(rc, side, rev) + if event.type=='train' then + if atc_arrow then + if string.find((get_rc() or ''),(rc or '')) then + atc_send('BBWO' .. (side or 'R') .. 'D10OCD1' .. ((rev or string.find((get_rc() or ''), 'R' .. rc)) and 'R' or '')..'SM') + elseif rev then + atc_send('BBWRSM') + end end end +end + +function F.odc(ln, rc) + interrupt_pos(F.dpts[ln],{['rc']=rc}) end
\ No newline at end of file |