-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontrol.lua
More file actions
296 lines (263 loc) · 7.59 KB
/
control.lua
File metadata and controls
296 lines (263 loc) · 7.59 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
require("lib")
-- remote.call("FuelTrainStop", "exclude_from_fuel_schedule", locomotive-name)
remote.add_interface("FuelTrainStop",{exclude_from_fuel_schedule = function(name) AddTrainIgnore(name) end})
local train_ignore_list = {}
function Init()
global.TrainStop = {}
global.TrainStopName = "Fuel Stop"
global.EnergyList = {}
global.FinishTrain = {}
global.TrainList = {}
end
function EnableFuelTrainStop()
for _,force in pairs(game.forces) do
local tech = force.technologies['automated-rail-transportation']
if tech and tech.researched then
force.recipes['fuel-train-stop'].enabled = true
end
end
end
function CreateEnergyList()
for _,item in pairs(game.item_prototypes) do
if item.fuel_category then
global.EnergyList[item.name] = item.fuel_value
end
end
end
function CreateTrainList()
for _,surface in pairs(game.surfaces) do
local trains = surface.get_trains()
for _,train in pairs(trains) do
for _,carriage in pairs(train.carriages) do
if Contains(train_ignore_list,carriage.name) then goto continue end
end
global.TrainList[train.id] = train
::continue::
end
end
end
function ON_INIT()
Init()
EnableFuelTrainStop()
CreateEnergyList()
CreateTrainList()
end
script.on_init(ON_INIT)
function ON_CONFIGURATION_CHANGED(data)
global.EnergyList = {}
CreateEnergyList()
local mod_name = "FuelTrainStop"
if NeedMigration(data,mod_name) then
local old_version = GetOldVersion(data,mod_name)
if old_version < "00.17.02" then
ON_INIT()
for _,surface in pairs(game.surfaces) do
local train_stops = surface.find_entities_filtered{name="fuel-train-stop"}
for _,stop in pairs(train_stops) do
table.insert(global.TrainStop,stop)
end
end
if Count(global.TrainStop) > 0 then
global.TrainStopName = global.TrainStop[1].backer_name or "Fuel Stop"
end
for _,train in pairs(global.TrainList) do
local schedule = train.schedule
if schedule then
for i,record in pairs(schedule.records) do
if record.station == global.TrainStopName then
table.remove(schedule.records,i)
if i > Count(schedule.records) then
schedule.current = 1
else
schedule.current = i
end
break
end
end
train.schedule = schedule
end
end
end
end
end
script.on_configuration_changed(ON_CONFIGURATION_CHANGED)
function AddTrainIgnore(name)
if not Contains(train_ignore_list,name) then
table.insert(train_ignore_list,name)
CheckTrainList()
end
end
function CheckTrainList()
if global.TrainList then
for i,train in pairs(global.TrainList) do
if train and train.valid then
for _,carriage in pairs(train.carriages) do
if Contains(train_ignore_list,carriage.name) then
global.TrainList[i] = nil
break;
end
end
else
global.TrainList[i] = nil
end
end
end
end
function ON_BUILT_ENTITY(event)
local entity = event.created_entity
if entity and entity.valid then
if entity.name == "fuel-train-stop" then
table.insert(global.TrainStop,entity)
entity.backer_name = global.TrainStopName
end
end
end
script.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity,defines.events.script_raised_built},ON_BUILT_ENTITY)
function ON_REMOVE_ENTITY(event)
local entity = event.entity
if entity and entity.valid then
if entity.name == "fuel-train-stop" then
for i,stop in pairs(global.TrainStop) do
if stop == entity then
table.remove(global.TrainStop,index)
break
end
end
end
end
end
script.on_event({defines.events.on_pre_player_mined_item,defines.events.on_robot_pre_mined,defines.events.on_entity_died,defines.events.script_raised_destroy},ON_REMOVE_ENTITY)
function ON_ENTITY_RENAMED(event)
if not event.by_script and event.entity.name == "fuel-train-stop" then
global.TrainStopName = event.entity.backer_name
for _,stop in pairs(global.TrainStop) do
stop.backer_name = global.TrainStopName
end
end
end
script.on_event(defines.events.on_entity_renamed,ON_ENTITY_RENAMED)
function ON_TRAIN_CREATED(event)
local train = event.train
local old_train_id_1 = event.old_train_id_1
local old_train_id_2 = event.old_train_id_2
for _,carriage in pairs(train.carriages) do
if Contains(train_ignore_list,carriage.name) then
if old_train_id_1 then
global.TrainList[old_train_id_1] = nil
end
if old_train_id_2 then
global.TrainList[old_train_id_2] = nil
end
goto continue
end
end
global.TrainList[train.id] = train
if old_train_id_1 then
global.TrainList[old_train_id_1] = nil
if global.FinishTrain[old_train_id_1] then
global.FinishTrain[old_train_id_1] = nil
global.FinishTrain[train.id] = train
end
end
if old_train_id_2 then
global.TrainList[old_train_id_2] = nil
if global.FinishTrain[old_train_id_2] then
global.FinishTrain[old_train_id_2] = nil
global.FinishTrain[train.id] = train
end
end
::continue::
end
script.on_event(defines.events.on_train_created,ON_TRAIN_CREATED)
function ON_TRAIN_CHANGED_STATE(event)
local train = event.train
if train.state == defines.train_state.wait_station then
if train.station and train.station.backer_name == global.TrainStopName then
global.FinishTrain[train.id] = train
end
end
end
script.on_event(defines.events.on_train_changed_state,ON_TRAIN_CHANGED_STATE)
function GetEnergy(fuel_list)
local e = 0
for name,amount in pairs(fuel_list) do
e = e + global.EnergyList[name] * amount
end
return e
end
function LowFuel(locomotive)
local inventory = locomotive.get_fuel_inventory()
if not inventory then return false end
local contents = inventory.get_contents()
local min_fuel = settings.global['min-fuel-amount'].value * locomotive.prototype.max_energy_usage * 800
min_fuel = min_fuel / locomotive.prototype.burner_prototype.effectivity
if GetEnergy(contents) < min_fuel then
return true
else
return false
end
end
function AddSchedule(train)
local schedule = train.schedule or {}
if not train.schedule then
schedule.records = {}
end
for _,record in pairs(schedule.records) do
if record.station == global.TrainStopName then return end
end
local record = {station = global.TrainStopName, wait_conditions = {}}
record.wait_conditions[#record.wait_conditions+1] = {type = "inactivity", compare_type = "and", ticks = 120 }
local current = schedule.current or 0
table.insert(schedule.records,current+1,record)
train.schedule = schedule
end
function ON_1200TH_TICK()
if Count(global.TrainStop) > 0 then
for i,train in pairs(global.TrainList) do
if not train.valid then
global.TrainList[i] = nil
goto continue
end
if train.manual_mode then goto continue end
for _,carriage in pairs(train.carriages) do
if carriage.type == "locomotive" then
if LowFuel(carriage) then
AddSchedule(train)
goto continue
end
end
end
::continue::
end
end
end
script.on_nth_tick(1200,ON_1200TH_TICK)
function ON_300TH_TICK()
if Count(global.FinishTrain) > 0 then
for i,train in pairs(global.FinishTrain) do
if not train.valid then
global.FinishTrain[i] = nil
else
if not (train.station and train.station.backer_name == global.TrainStopName) then
local schedule = train.schedule
if(schedule) then
for i,record in pairs(schedule.records) do
if record.station == global.TrainStopName then
table.remove(schedule.records,i)
if i > Count(schedule.records) then
schedule.current = 1
else
schedule.current = i
end
break
end
end
train.schedule = schedule
end
global.FinishTrain[i] = nil
end
end
end
end
end
script.on_nth_tick(300,ON_300TH_TICK)