summaryrefslogtreecommitdiff
path: root/far/init_code.lua
blob: 571e2c4e925d09f923c967b15b0eb484e42adca3 (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
-- environment_far.lua
if S.trains == nil then S.trains = {} 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.max_displays = 15
  F.print("Initialized")
end

--[[
  EXAMPLE: F.has_rc("LILSHUNTER", F.get_rc_safe() )
  Merged F.has_rc and F.does_train_have_rc
  F.does_train_have_rc is deprecated
]]
F.has_rc = function(query,rc_list) -- query = string, single entry
  for word in rc_list:gmatch("[^%s]+") do
    if word == query then return true end
  end
  return false
end

F.send_route = function(passive_name, route, show_print)
  local message = ""
  local return_value = false
  if can_set_route(passive_name, route) then
    set_route(passive_name, route)
    message = passive_name .. " has been set to " .. route
    return_value = true
  else
    message = route .. " cannot be set for " .. passive_name .. ". Try another."
    return_value = false
  end
  if show_print == true then F.print(message) end
  return return_value
end

F.save_train = function(pos)
  if not atc_id then return end
  if S.trains then
    if F.isempty(pos) then
      pos_string = ""
    else
      pos_string = pos["x"] .. "," .. pos["y"] .. "," .. pos["z"]
    end
    S.trains[atc_id] = { ["id"] = atc_id, ["rc"] = F.get_rc_safe(), ["ln"] = F.get_line_safe(), ["cars_count"] = F.get_train_length_safe(), ["pos"] = pos_string }
    -- F.print("Train ID: " .. S.trains[atc_id]["id"] .. " at " .. S.trains[atc_id]["pos"])
  end
end

F.get_real_split_count = function(train_length_count, split_count)
  if split_count then
    if split_count == "all" then
      return 2
    else
      F.print("train_length_count (" .. train_length_count .. ") - split_count (" .. split_count .. ")")
      train_length_count = train_length_count + 1
      split_count = train_length_count - split_count
      return split_count
    end
  else
    return nil
  end
end

F.delete_train_info = function(train_id)
  if S.trains[train_id] then
    S.trains[train_id] = nil
    F.print("Deleted train id: " .. train_id)
  end
end

F.clear_main_depot_displays = function() for i = 1, F.max_displays, 1 do digiline_send("train_display" .. i, " ") end end

F.list_trains = function(number_of_displays)
  if S.trains then
    if number_of_displays == nil then number_of_displays = F.max_displays end
    F.clear_main_depot_displays()
    number_of_displays = number_of_displays + 1
    count_keys = 0
    trains_table = {}
    for k in pairs(S.trains) do
      table.insert(trains_table, k)
      count_keys = count_keys + 1
    end
    table.sort(trains_table)
    -- x = number_of_displays - count_keys
    x = 1
    for _, k in ipairs(trains_table) do
      if S.trains[k] then
        v = S.trains[k]
        if F.has_rc("LILSHUNTER", v["rc"]) or F.has_rc("LIL", v["rc"]) then
          F.delete_train_info(v["id"])
        else
          if v["ln"] == nil or v["ln"] == "" then
            line_number = ""
          else
            line_number = "| LN: [" .. v["ln"] .. "]"
          end
          if v["pos"] == nil or v["pos"] == "" then
            pos_string = ""
          else
            pos_string = "| POS: [" .. v["pos"] .. "]"
          end
          if v["rc"] == nil or v["rc"] == "" then
            rc_display = ""
          else
            rc_list = v["rc"]
            rc_list_cleansed = ""
            rc_list_unknown = ""
            rc_list_table = {}
            if F.has_rc("ERSTAZI", rc_list) and F.has_rc("FREIGHT", rc_list) then
              rc_list_cleansed = "ERSTAZI FREIGHT |"
            else
              rc_list_cleansed = "NO E,F |"
            end
            for rc in rc_list:gmatch("[^%s]+") do
              if rc == "ERSTAZI" or rc == "FREIGHT" then
                -- leaving for future use
                do_nothing = true
              else
                rc_list_unknown = rc_list_unknown .. " " .. rc
              end
            end
            rc_display = ""
            if not F.isempty(rc_list_unknown) then
              rc_display = rc_display .. "| RC:" .. rc_list_unknown
            end
          end
          if v["cars_count"] == nil or v["cars_count"] == "" then
            cars_count_display = " Len: 0"
          else
            cars_count = tonumber(v["cars_count"])
            cars_count_display = " Len: " .. cars_count
          end
          message = " ID: " .. v["id"] .. cars_count_display .. rc_display ..  line_number .. pos_string
          if x > 0 then digiline_send("train_display" .. x, message) end
          F.print(x .. ": " .. message)
          x = x + 1
        end
      end
    end
  else
    F.print("no trains saved in S.trains")
  end
end

F.slow_train_down = function(id)
  result = atc_send_to_train(id, "B1")
  if result == false then
    F.print("Train ID " .. id .. " does not exist")
  else
    F.print("Train ID " .. id .. " is slowed down to B1")
  end
end