forked from jamesofarrell/TheFatController
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAlerts.lua
152 lines (143 loc) · 5.41 KB
/
Alerts.lua
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
--- Alerts
-- @module Alerts
--- Alerts
-- @type Alerts
local function temporary_station_name(record)
if record.temporary then
if record.rail and record.rail.valid then
return {"gui-train.temporary", util.positiontostr(record.rail.position)}
else
return "Temporary"
end
else
return record.station
end
end
Alerts = {}--luacheck: allow defined top
Alerts.electric_locomotives = {
["electric-locomotive-mk1"] = true,
["electric-locomotive-mk2"] = true,
["electric-locomotive-mk3"] = true,
["hybrid-train"] = true
}
Alerts.set_alert = function(trainInfo, type, time, skipUpdate)
trainInfo.alarm.active = true
trainInfo.alarm.type = type
trainInfo.alarm.message = time and ({"msg-alarm-" .. type, time}) or {"msg-alarm-" .. type}
local schedule = trainInfo.train.schedule
local records = schedule and schedule.records
if records and table_size(records) > 0 then
local current = schedule.current
local current_record = records[current]
if type == "noPath" then
local station = temporary_station_name(current_record)
trainInfo.alarm.message = {"msg-alarm-" .. type, station}
end
if type == "timeToStation" then
local prev = current == 1 and table_size(schedule.records) or current - 1
trainInfo.alarm.message = {"msg-alarm-" .. type, time, temporary_station_name(records[prev]), temporary_station_name(records[current])}
end
end
if not skipUpdate then
Alerts.update_filters()
end
if type ~= "noFuel" or trainInfo.alarm.last_message + 600 < game.tick then
Alerts.alert_force(trainInfo.force, trainInfo)
end
end
Alerts.check_alerts = function(trainInfo)
local force = trainInfo.force
local update = false
if trainInfo.alarm.arrived_at_signal then
local signalDuration = global.force_settings[force.name].signalDuration
if trainInfo.last_state == defines.train_state.wait_signal and trainInfo.alarm.arrived_at_signal == game.tick - signalDuration then
Alerts.set_alert(trainInfo, "timeAtSignal", signalDuration / 3600)
update = true
end
end
if trainInfo.alarm.left_station then
local stationDuration = global.force_settings[force.name].stationDuration
if trainInfo.alarm.left_station + stationDuration <= game.tick then
Alerts.set_alert(trainInfo, "timeToStation", stationDuration / 3600)
update = true
end
end
return update
end
Alerts.check_noFuel = function(trainInfo, skipUpdate)
if not trainInfo.train or not trainInfo.train.valid then
return false
end
local noFuel = false
local locos = trainInfo.train.locomotives
local electric
local fuel_inventory
for _, carriage in pairs(locos.front_movers) do
electric = Alerts.electric_locomotives[carriage.name]
fuel_inventory = carriage.get_fuel_inventory()
if (not electric and fuel_inventory and fuel_inventory.is_empty()) or (electric and carriage.energy == 0) then
noFuel = true
break
end
end
if not noFuel then
for _, carriage in pairs(locos.back_movers) do
electric = Alerts.electric_locomotives[carriage.name]
fuel_inventory = carriage.get_fuel_inventory()
if (not electric and fuel_inventory and fuel_inventory.is_empty())
or (electric and carriage.energy == 0)
then
noFuel = true
break
end
end
end
local old_noFuel = trainInfo.noFuel
if noFuel then
Alerts.set_alert(trainInfo, "noFuel", false, skipUpdate)
else
if trainInfo.alarm.active and trainInfo.alarm.type == "noFuel" then
trainInfo.alarm.active = false
trainInfo.alarm.type = false
if not skipUpdate then
Alerts.update_filters()
end
end
end
trainInfo.noFuel = noFuel
return old_noFuel ~= trainInfo.noFuel
end
Alerts.reset_alarm = function(trainInfo)
trainInfo.alarm.active = false
trainInfo.alarm.type = false
trainInfo.alarm.left_station = false
trainInfo.alarm.arrived_at_signal = false
trainInfo.alarm.last_message = 0
TickTable.remove_by_train("updateAlarms", trainInfo.train)
Alerts.update_filters()
end
Alerts.update_filters = function()
for _, player in pairs(game.players) do
if player.connected then
local guiSettings = global.gui[player.index]
if guiSettings.filter_alarms then
guiSettings.filtered_trains = TrainList.get_filtered_trains(player.force, guiSettings)
guiSettings.pageCount = getPageCount(guiSettings, player)
if guiSettings.fatControllerGui and guiSettings.fatControllerGui.valid and guiSettings.fatControllerGui.trainInfo then
GUI.newTrainInfoWindow(guiSettings, player)
GUI.refreshTrainInfoGui(guiSettings, player)
end
end
end
end
end
Alerts.alert_force = function(force, trainInfo)
local alarm_type = trainInfo.alarm.type
for _, player in pairs(force.players) do
local guiSettings = global.gui[player.index]
if guiSettings and guiSettings.alarm[alarm_type] then
player.print(trainInfo.alarm.message)
end
end
trainInfo.alarm.last_message = game.tick
end