summaryrefslogtreecommitdiff
path: root/Crossroads/init_code.lua
blob: 185eef7a42b7b450ebfcb86be3f22c482994adf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
--[[
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.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
side       = Door opening side
optime     = Length of time before train departs
reverse    = Reverse train before departing
acc        = Departure Speed
out        = Change outside text
reventry   = Allow trains to pass from the reverse direction
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 atc_arrow then
      local t_start = os.clock()
      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 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
      end
    end
  end
  if event.int then
    if atc_id ~= nil 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
      end
      ]]
      --atc_set_text_inside(intext.."\nCorrection: "..S.corrratio)
      atc_set_text_inside(intext)
    end
  end
end

-- F.hst - preset function for small stations (Haltestelle)
function F.hst(cur, nxt, side, spd, out, trk)
  F.stnbasic(cur, side, 10, false, spd, out, true, nil, nil, nxt, trk)
end

-- F.bhf - preset function for large stations (Bahnhof)
function F.bhf(cur, nxt, side, spd, out, trk)
  F.stnbasic(cur, side, 15, false, spd, out, true, nil, nil, nxt, trk)
end

-- F.kbhf - preset function for termini
function F.kbhf(cur, nxt, side, spd, out, trk)
  F.stnbasic(cur, side, 15, true, spd, out, true, nil, nil, nxt, trk)
end

-- F.timing - station with timing
function F.timing(d_off, d_int, cur, nxt, side, spd, out, trk, term, pre, post)
		  local timenow = os.time()
	  	local timesincelast = (timenow+d_off) % d_int
  local wait = d_int - timesincelast
  F.stnbasic(cur, side, wait, term, spd, out, true, pre, post, nxt, trk)
end

-- F.brk(direction) - sends ATC B2 command
function F.brk(dir)
  if event.type == "train" then
    if atc_arrow == dir then
      atc_send("B2S2")
    end
  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
]]
function F.eval(expr, rettrue, retfalse)
  if expr then return rettrue else return retfalse end
end

function F.checkpoint(name,arrow,opp)
  if event.train then
    S.trains[atc_id] = "C "..name.." "
    if atc_arrow then
      S.trains[atc_id] = S.trains[atc_id]..arrow
    else
      S.trains[atc_id] = S.trains[atc_id]..opp
    end
  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",
  counterclockwise = "Counterclockwise Route",
  cras             = "Crossroads ARSE7's Shop",
  crbfost          = "Crossroads Station East",
  crbfsm           = "Crossroads Station St. Central",
  crbfso           = "Crossroads Station St. East",
  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",
  crrathaus        = "Crossroads City Hall",
  crsmacker        = "Crossroads Smacker's Station",
  crwm             = "Crossroads-West Mountains",
  crzn             = "Crossroads-Zentrum/Nordstadt",
  elchateau        = "Erstaziland-Chateau d'Erstazi",
  elgp             = "Erstaziland-Greener Pastures",
  elsf             = "Erstaziland-Salt Factory",
  evo              = "EVO",
  mushroom         = "Mushroom Land",
  neverbuild       = "Neverbuild",
  nvbcentral       = "Neverbuild Central",
  nvbold           = "Neverbuild Old Termius",
  nvboutskirts     = "Neverbuild Outskirts",
  oc               = "Ocean City",
  occh             = "Ocean City, City Hall",
  occrt            = "Ocean City, CRT Office",
  ocmushroom       = "Ocean City, Mushroom Market",
  ocoutskirts      = "Ocean City Outskirts",
  phwest           = "Personhood, West",
  scc              = "Silver Coast Central",
  scn              = "Silver Coast North",
  scs              = "Silver Coast South",
  thecube          = "The Cube",
}
F.errlist = {
  ["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)
    else
      m = string.format("%s%02d:%02d", m, time.hour, time.min + 1)
    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
  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
  end
end