summaryrefslogtreecommitdiff
path: root/m4/init_code.lua
blob: 81333115e8b04c750c1cb2c1a02f84d5485cd01e (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
--env-m4.lua
if S.trains == nil then S.trains = {} end
if S.d == nil then S.d = {} end
if S.datetime == nil then S.datetime = "" end
if S.stop_display == nil then S.stop_display = false end

F.print = function (str) if F.debug then print("".. (str or "nil") ) end end
F.isempty = function (s) return s == nil or s == "" end
F.get_rc_safe = function() return get_rc() or "" end
F.get_line_safe = function() return get_line() or "" end
F.get_train_length_safe = function() return train_length() or 0 end
F.avg = function(t)
  local sum = 0
  local count = 0
  for k,v in pairs(t) do
    if type(v) == "number" then
      sum = sum + v
      count = count + 1
    end
  end
  return (sum / count)
end

if event.init then
  F.debug = true
  F.printAllTrainsInfo = true
  F.max_displays = 4
  F.print("Initialized")
end

F.does_train_have_rc = function(wanted_rc)
  local rc = F.get_rc_safe()
  if rc:match(wanted_rc) then return true end
end

--old splitter and cpl for demo
function F.cpllooparound(ln)
  if not (event.train) then
    return
  end
	if not (F.get_line_safe() == ln) then return end

	if "ENGINE" == F.get_rc_safe() then
		atc_send("CplS0WRSM")
		set_rc("")
		return
	end

	split_at_index(F.get_train_length_safe()-1,"A0S0") --Stopping the wagons is handled here by SO
	set_rc("ENGINE")
	atc_send("A1")

end

--new ones
function F.split_looparound(ln)
  if not (event.train) then return end
  if not (F.get_line_safe() == ln) then return end

  split_at_index(F.get_train_length_safe()-1,"A0S0OC") --Stopping the wagons is handled here by SO
  set_rc("ENGINE")
  atc_send("A1")

end

function F.split_looparound_left(ln)
  if not (event.train) then return end
  if not (F.get_line_safe() == ln) then return end

  split_at_index(F.get_train_length_safe()-1,"A0S0OL") --Stopping the wagons is handled here by SO
  set_rc("ENGINE")
  atc_send("A1")

end
function F.split_looparound_right(ln)
  if not (event.train) then return end
  if not (F.get_line_safe() == ln) then return end

  split_at_index(F.get_train_length_safe()-1,"A0S0OR") --Stopping the wagons is handled here by SO
  set_rc("ENGINE")
  atc_send("A1")

end

function F.cpl_looparound(ln)
  if not (event.train) then return end
  if not (F.get_line_safe() == ln) then return end

  if "ENGINE" == F.get_rc_safe() then
    atc_send("CplS0WD2RS4")
    set_rc("")
    return
  end

end
--end of new cpl functions