-
Notifications
You must be signed in to change notification settings - Fork 5
/
Train.lua
695 lines (620 loc) · 30.3 KB
/
Train.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
Train = {
new = function(train, id)
if train.valid then
local new = {
train = train,
ID = id,
locomotives = {},
line = false,
lineVersion = 0,
settings = {},
waiting = false,
refueling = false,
advancedState = false,
last_fuel_update = 0,
direction = 0, -- 0 = front, 1 back (lookup direction for trainstop)
passengers = 0,
}
new.settings.autoRefuel = settings.global.smart_trains_set_autorefuel.value
setmetatable(new, {__index = Train})
new:update(train, id)
return new
end
end,
getLocomotives = function(self)
local locomotives = {}
if self.train.locomotives then
for _, loco in pairs(self.train.locomotives.front_movers) do
table.insert(locomotives, loco)
end
for _, loco in pairs(self.train.locomotives.back_movers) do
table.insert(locomotives, loco)
end
end
return locomotives
end,
getName = function(self)
local train = self.train
if train.locomotives ~= nil and (#train.locomotives.front_movers > 0 or #train.locomotives.back_movers > 0) then
if self.train.locomotives.front_movers[1] then
return train.locomotives.front_movers[1].backer_name
elseif train.locomotives.back_movers[1] then
return train.locomotives.back_movers[1].backer_name
end
end
return ""
end,
getType = function(self)
local parts = {}
local found
for _,c in pairs(self.train.carriages) do
found = false
if c.type == "locomotive" then
for i, fm in pairs(self.train.locomotives.front_movers) do
if fm == c then
table.insert(parts,'L')
found = true
break
end
end
if not found then
for i, bm in pairs(self.train.locomotives.back_movers) do
if bm == c then
table.insert(parts,'L')
break
end
end
end
else
table.insert(parts, 'C')
end
end
local type = table.concat(parts,'')
type = type:gsub("LC","L-C"):gsub("CL", "C-L")
return string.gsub(string.gsub(type, "^-", ""), "-$", "")
end,
update = function(self, train, id)
self.ID = id
self.train = train
self.locomotives = self:getLocomotives()
self.type = self:getType()
self.name = self:getName()
self.passengers = 0
for _, c in pairs(train.carriages) do
if c.get_driver() and c.get_driver().name ~= "fatcontroller" then
self.passengers = self.passengers + 1
end
end
end,
printName = function(self)
debugDump(self.name, true)
end,
get_first_matching_station = function(self, value, current)
local stations = global.stationMap[self.train.carriages[1].force.name][value]
if not stations then
return false
end
local schedule = self.train.schedule
local records = schedule and schedule.records
if not records then
return false
end
current = current or schedule.current
local num_records = #records
local index
for i = 0, num_records - 2 do
index = (current + i) % num_records + 1
if stations[records[index].station] then
return index
end
end
return false
end,
nextStation = function(self, force, index)
local train = self.train
if self.settings.autoRefuel then
if self:lowestFuel() < (global.settings.refuel.rangeMin) and not inSchedule(self:refuelStation(), train.schedule) then
train.schedule = addStation(self:refuelStation(), train.schedule, global.settings.refuel.time)
if global.showFlyingText then
self:flyingText("Refuel station added", colors.YELLOW)
end
end
end
if train.manual_mode == false or force then
local schedule = train.schedule
if schedule and schedule.records then
local countRecords = #schedule.records
local tmp = (schedule.current % countRecords) + 1
if index and index > 0 and index <= countRecords then
tmp = index
end
if global.showFlyingText then
self:flyingText("Going to "..schedule.records[tmp].station, colors.YELLOW, {offset = -1}) --TODO localisation
end
--all below is needed to make a train go to another station, don't change!
train.manual_mode = true
schedule.current = tmp
train.schedule = schedule
train.manual_mode = false
end
end
end,
isValidScheduleIndex = function(self, index)
local records = self.train.schedule and self.train.schedule.records
if index and index > 0 and records and index <= #records then
return index
end
return false
end,
refuelStation = function(self)
local station = global.settings.refuel.station
local lType = self:getType()
-- First thing first, check for an explicit match for the
-- train's configuration and specific refueling station.
local refuelStation = station.." "..lType
local force = self.train.carriages[1].force.name
local full_match = global.stationCount[force][refuelStation] and global.stationCount[force][refuelStation] > 0
if full_match then
--debugDump("full_match: train="..lType.." @ station="..refuelStation, true)
return refuelStation
end
-- Since there wasn't a specific match, we now need to search
-- through all of the stations trying to find one that matches
-- the locomotive configuration.
-- Loco L-CC cannot refuel at Refuel L-C-L (issue #41).
-- See comment below.
local best_fit = { len=0, name="" }
lType = string.gsub(lType, "%-", "")
local pattern = "^"..station.."%s+([LC-]+)$"
for name, c in pairs(global.stationCount[force]) do
if name == nil then name = "" end
local _, _, sType = string.find(name, pattern)
if sType then
-- This station matches the auto-refuel pattern. We'll perform
-- subset checks on the train/station and station/train to ensure
-- the proper alignment.
sType = string.gsub(sType, "%-", "")
-- Is the train configuration a subset of the refuel station?
if string.find(sType, "^"..lType) then
--debugDump("subset 1: train="..self:getType().." @ station="..name, true)
return name
end
-- Is the refuel station a subset of the train? We cannot
-- take this immediately since there may be better,
-- more-apt stations to choose from.
if string.find(lType, "^"..sType) then
-- Track the length of the station that's a subset of the train.
-- Longer is better, but we can short-circuit when the train
-- and station lengths match.
local llen = string.len (lType)
local slen = string.len (sType)
-- The short-circuit.
if slen == llen then
return name
end
if slen > best_fit.len then
best_fit = { len=slen, name=name }
--debugDump("subset 2: updated train="..self:getType().." @ station="..name, true)
end
end
end
end
-- If we've found a best fit, return that one.
if best_fit.len > 0 then
return best_fit.name
end
-- Default to the base station.
--debugDump("default: train="..self:getType().." @ station="..station, true)
return station
end,
startRefueling = function(self)
if global.showFlyingText then
self:flyingText("refueling", colors.YELLOW)
end
local tick = game.tick + global.settings.intervals.inactivity
self.refueling = tick
insertInTable(global.refueling, tick, self)
end,
isRefueling = function(self)
return self.refueling and self.settings.autoRefuel
end,
isRefuelingDone = function(self)
if self:lowestFuel() >= global.settings.refuel.rangeMax or self:isFuelInventoryFull() then
return true
end
end,
refuelingDone = function(self, done)
if done then
if global.showFlyingText then
self:flyingText("Refueling done", colors.YELLOW, {offset = 1})
end
self.refueling = false
self:nextStation()
end
end,
removeRefuelStation = function(self)
local records = self.train.schedule.records
if inSchedule_reverse(self:refuelStation(), self.train.schedule) and records and #records >= 3 then
self.train.schedule = removeStation(self:refuelStation(), self.train.schedule)
if global.showFlyingText then
self:flyingText("Refuel station removed", colors.YELLOW, {offset=1}) --TODO localisation
end
end
end,
currentStation = function(self)
if self.train.valid and self.train.schedule and type(self.train.schedule.records) == "table" and self.train.schedule.records[self.train.schedule.current] then
return self.train.schedule.records[self.train.schedule.current].station
else
return false
end
end,
getStationName = function(self, index)
if not self.train.schedule then return false end
index = index or self.train.schedule.current
if self.train.valid and index and self:isValidScheduleIndex(index) and type(self.train.schedule.records) == "table" then
return tostring(self.train.schedule.records[index].station)
else
return false
end
end,
get_rules = function(self, current)
current = current or self.train.schedule.current
local line = (self.line and global.trainLines[self.line] and global.trainLines[self.line].records) and global.trainLines[self.line] or false
--log(line.records[current].station)
--local rules = line and line.records[current].rules or false
local rules = line and line.rules[current] or false
-- use copied rules when waiting
if self.train.state == defines.train_state.wait_station then
rules = self.rules
end
if rules and rules.station == self:getStationName(current) then
local defaultRule = {jumpToCircuit = true, jumpTo = true}
for k, _ in pairs(defaultRule) do
if rules[k] then
return rules
end
end
end
return false
end,
get_rule = function(self, name, current)
local rules = self:get_rules(current)
if rules then
return rules[name]
end
return false
end,
getWaitingTime = function(self)
local records = self.train.schedule and self.train.schedule.records
if self.train.schedule and self.train.schedule.records and records and #records > 0 then
local conditions = self.train.schedule.records[self.train.schedule.current].wait_conditions or {}
for _, condition in pairs(conditions) do
if condition.type == "time" then
return condition.ticks
end
end
end
return 2^32-1 --TODO what to return when no waiting time set?
end,
-- return true when at a smart train stop
setWaitingStation = function(self)
if self.waiting then
return
end
local current_tick = game.tick
local rules = self:get_rules()
local station = findTrainStopByTrain(self)
if station and station.backer_name ~= self:getStationName() then
log(game.tick .. " station name mismatch")
log("Actual: " .. station.backer_name .. " Self: " .. self:getStationName())
log("Actual type: " .. type(station.backer_name) .. " Self type: " .. type(self:getStationName()))
return
end
if not station then --temporary station
return
end
self.waitingStation = findSmartTrainStopByTrain(self, self:getStationName())
if not self.waitingStation and rules and rules.jumpToCircuit then
--TODO proper error message
--TODO localisation
debugDump("No smart trainstop with go to signal# rule. Line: " .. self.line .. " @ station " .. self.train.schedule.records[self.train.schedule.current].station, true)
return
end
--LOGGERS.main.log(serpent.line(rules, {comment=false}))
local nextUpdate = current_tick + global.settings.intervals.write
if self:getWaitingTime() == 10 then
nextUpdate = current_tick + 2
end
self.waiting = true
self.rules = table.deepcopy(rules)
-- update cargo (only if smart stop or full/empty/inactivity rule set
-- write to combinator (only if smart stop)
if self.waitingStation then
self:setCircuitSignal()
insertInTable(global.update_cargo, nextUpdate, self)
self.update_cargo = nextUpdate
end
end,
resetWaitingStation = function(self, destination)
self:resetCircuitSignal(destination)
self.waitingStation = false
self.waiting = false
self.rules = false
self.refueling = false
self.departAt = false
self.update_cargo = false
end,
getCircuitValue = function(self)
if self.waitingStation and self.waitingStation.signalProxy and self.waitingStation.signalProxy.valid then
local behavior = self.waitingStation.signalProxy.get_control_behavior()
if behavior then
local condition = behavior.circuit_condition.condition
local signal = (condition and condition.first_signal and condition.first_signal.name) and condition.first_signal or false
if signal and signal.name then
local sum = 0
local green = behavior.get_circuit_network(defines.wire_type.red, defines.circuit_connector_id.lamp)
if green then
sum = green.get_signal(signal) or 0
end
local red = behavior.get_circuit_network(defines.wire_type.green, defines.circuit_connector_id.lamp)
if red then
sum = sum + (red.get_signal(signal) or 0)
end
--TODO add logistics network value?
-- adds logistics value to signal if connect is set and signal is an actual item
-- if behavior.connect_to_logistic_network and self.waitingStation.signalProxy.logistic_network then
-- sum = sum + self.waitingStation.signalProxy.logistic_network.get_item_count(signal.name)
-- end
-- log(sum)
return sum
end
end
end
return false
end,
setCircuitSignal = function(self, destination)
if self.waitingStation and self.waitingStation.station.valid and self.waitingStation.cargo and self.waitingStation.cargo.valid then
local cargoProxy = self.waitingStation.cargo
local parameters={}
local min_fuel = self:lowestFuel(true)
local station_number = global.stationNumbers[cargoProxy.force.name][tostring(self.waitingStation.station.backer_name)] or false
if station_number and station_number ~= 0 then
-- can change if a mapping is un-/assigned or a line is created/edited/deleted
parameters[combinator_index.station_number]={signal={type = "virtual", name = "signal-station-number"}, count = station_number, index = combinator_index.station_number}
end
-- static, doesn't need updating
parameters[combinator_index.train_at_station]={signal={type = "virtual", name = "signal-train-at-station"}, count = 1, index = combinator_index.train_at_station}
-- static, doesn't need updating
parameters[combinator_index.locomotives] = {
signal={type = "virtual", name = "signal-locomotives"}, count = #self.train.locomotives.front_movers + #self.train.locomotives.back_movers,
index = combinator_index.locomotives
}
-- static, doesn't need updating
parameters[combinator_index.cargowagons] = {
signal={type = "virtual", name = "signal-cargowagons"}, count = #self.train.cargo_wagons + #self.train.fluid_wagons,
index = combinator_index.cargowagons
}
-- can be updated from within on_player_driving_changed_state
parameters[combinator_index.passenger]={signal={type = "virtual", name = "signal-passenger"}, count = self.passengers, index = combinator_index.passenger}
-- needs updating in on_tick
parameters[combinator_index.lowest_fuel]={signal={type = "virtual", name = "signal-lowest-fuel"}, count = min_fuel, index = combinator_index.lowest_fuel}
local trainLine = self.line and global.trainLines[self.line] or false
if trainLine and trainLine.settings.number ~= 0 then
parameters[combinator_index.line]={signal={type = "virtual", name = "signal-line"}, count = trainLine.settings.number, index = combinator_index.line}
end
if destination then
local destNumber
if trainLine and trainLine.settings.useMapping and self.train.schedule then
destNumber = global.stationNumbers[cargoProxy.force.name][tostring(self.train.schedule.records[self.train.schedule.current].station)] or false
end
--log(game.tick .. " Train: "..self.name .. " setting destination signal: " .. (destNumber or self.train.schedule.current))
--only needs to be set when a train leaves a station
parameters[combinator_index.destination] = {
signal = { type = "virtual", name = "signal-destination"},
count = destNumber or self.train.schedule.current,
index = combinator_index.destination
}
end
local behaviour = cargoProxy.get_control_behavior()
if behaviour then
behaviour.parameters = parameters
end
end
end,
updatePassengerSignal = function(self)
if self.waitingStation and self.waitingStation.station.valid
and self.waitingStation.cargo and self.waitingStation.cargo.valid
and self.train and self.train.valid
and self.train.state == defines.train_state.wait_station then
local behaviour = self.waitingStation.cargo.get_or_create_control_behavior()
behaviour.set_signal(combinator_index.passenger, {signal={type = "virtual", name = "signal-passenger"}, count = self.passengers})
end
end,
resetCircuitSignal = function(self, destination)
if self.waitingStation and self.waitingStation.cargo and self.waitingStation.cargo.valid then
if self.train and self.train.valid then
self:setCircuitSignal(destination)
end
global.reset_signals[game.tick+1] = global.reset_signals[game.tick+1] or {}
table.insert(global.reset_signals[game.tick+1], {cargo = self.waitingStation.cargo, station = self.waitingStation.station})
end
end,
isFuelInventoryFull = function(self)
local locos = (self.train and self.train.valid) and self.train.locomotives or false
local isFull = true
if locos then
local inventory, contents
for _, carriage in pairs(locos.front_movers) do
inventory = carriage.get_fuel_inventory()
if inventory then
if inventory.is_empty() or #inventory == 0 then
return false
end
contents = inventory.get_contents()
for item, _ in pairs(contents) do
isFull = isFull and (not inventory.can_insert(item))
if not isFull then
return false
end
end
end
end
for _, carriage in pairs(locos.back_movers) do
inventory = carriage.get_fuel_inventory()
if inventory then
if inventory.is_empty() or #inventory == 0 then
return false
end
contents = inventory.get_contents()
for item, _ in pairs(contents) do
isFull = isFull and (not inventory.can_insert(item))
if not isFull then
return false
end
end
end
end
end
return isFull
end,
--returns fuelvalue (in MJ)
lowestFuel = function(self, exact)
if self.last_fuel_update + 60 <= game.tick or exact then
self.last_fuel_update = game.tick
local locos = (self.train and self.train.valid) and self.train.locomotives or false
if locos then
local minfuel
local c
local contents
local fuel_inventory
for _, carriage in pairs(locos.front_movers) do
fuel_inventory = carriage.get_fuel_inventory()
contents = fuel_inventory and fuel_inventory.get_contents()
c = contents and self:calcFuel(contents) or global.settings.refuel.rangeMax
if minfuel == nil or c < minfuel then
minfuel = c
end
end
for _, carriage in pairs(locos.back_movers) do
fuel_inventory = carriage.get_fuel_inventory()
contents = fuel_inventory and fuel_inventory.get_contents()
c = contents and self:calcFuel(contents) or global.settings.refuel.rangeMax
if minfuel == nil or c < minfuel then
minfuel = c
end
end
self.minFuel = minfuel
else
self.minFuel = 0
end
end
return self.minFuel
end,
calcFuel = function(self, contents)
local value = 0
--/c game.player.print(game.player.character.vehicle.train.locomotives.front_movers[1].energy)
for i, c in pairs(contents) do
value = value + c*fuelvalue(i)
end
return value
end,
updateState = function(self)
--debugDump(util.formattime(game.tick,true).."@ "..getKeyByValue(defines.train_state, self.train.state),true)
self.previousState = self.state
self.state = self.train.state
local ts = defines.train_state
if self.previousState == ts.wait_station and
(self.state == ts.on_the_path or self.state == ts.arrive_signal
or self.state == ts.path_lost or self.state == ts.no_path or self.state == ts.destination_full)
then
self.advancedState = train_state.left_station
--debugDump(game.tick.." left_station",true)
else
self.advancedState = false
end
end,
--- Update a trainline
-- @return #boolean whether the line was updated
updateLine = function(self)
local oldmode = self.train.manual_mode
if not self.line then
return true
end
-- line was deleted
if (self.line and not global.trainLines[self.line]) then
if global.showFlyingText then
self:flyingText("Dettached from line", colors.RED) --TODO localisation
end
local schedule = self.train.schedule
--LOGGERS.main.log("Train detached from line " .. self.line .. "\t\t train: " .. self.name)
self.line = false
self.lineVersion = false
self.rules = nil
self.train.manual_mode = true
self.train.schedule = schedule
self.train.manual_mode = oldmode
return true
end
local trainLine = global.trainLines[self.line]
-- Already updated
if trainLine and trainLine.changed <= self.lineVersion then
--log(self.name .. " Up to date")
return true
end
local records = self.train.schedule and self.train.schedule.records
-- Skip when refueling
if self.settings.autoRefuel and records and #records == inSchedule_reverse(self:refuelStation(), self.train.schedule) then
if global.showFlyingText then
self:flyingText("Skipping line update, refueling", colors.YELLOW)
end
--log(self.name .. " refueling")
return false
end
--debugDump("updating line "..self.line.." train: "..self.train.carriages[1].backer_name,true)
if global.showFlyingText and self.lineVersion >= 0 then
self:flyingText("updating schedule", colors.YELLOW) --TODO localisation
--log(self.name .. " Updating line")
end
local waitingAt = records and records[self.train.schedule.current] or {station=""}
local schedule = {
records= util.table.deepcopy(trainLine.records)
}
local inLine = inSchedule(waitingAt.station, schedule)
--log(self.name .. " inline " .. serpent.line(inLine, {comment=false}))
self.settings.autoRefuel = trainLine.settings.autoRefuel
self.rules = table.deepcopy(trainLine.rules)
self.lineVersion = trainLine.changed
if inLine then
schedule.current = inLine
self.train.schedule = schedule
--log(self.name .. " Updated line (inline)")
else
schedule.current = 1
self.train.manual_mode = true
self.train.schedule = schedule
self.train.manual_mode = oldmode
--log(self.name .. " Updated line (not inline)")
end
--LOGGERS.main.log("Train updated schedule for line " .. self.line .. "\t\t train: " .. self.name)
return true
end,
flyingText = function(self, msg, color, tbl)
local s = global.showFlyingText
local offset = 0
if type(tbl) == "table" then
s = tbl.show or s
offset = tbl.offset or offset
end
local vehicle = (self.direction and self.direction == 0) and self.train.carriages[1] or self.train.carriages[#self.train.carriages]
local pos = vehicle.position
if type(offset) == "table" then
pos = offset
elseif type(offset) == "number" then
pos.y = pos.y + offset
end
if s then self.train.carriages[1].surface.create_entity({name="flying-text", position=pos, text=msg, color=color}) end
end
}
Train.__eq = function(trainA, trainB)
return trainA.train.carriages[1] == trainB.train.carriages[1]
end