-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
432 lines (386 loc) · 15.2 KB
/
control.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
local knownsignals = {
parseSchedule = {
richtext = {name="signal-stopname-richtext",type="virtual"},
schedulerail = {name="signal-schedule-rail",type="virtual"},
X = {name="signal-X",type="virtual"},
Y = {name="signal-Y",type="virtual"},
wait_time = {name="signal-wait-time",type="virtual"},
wait_inactivity = {name="signal-wait-inactivity",type="virtual"},
wait_empty = {name="signal-wait-empty",type="virtual"},
wait_full = {name="signal-wait-full",type="virtual"},
wait_passenger = {name="signal-wait-passenger",type="virtual"},
wait_circuit = {name="signal-wait-circuit",type="virtual"},
wait_robots = {name="signal-wait-robots",type="virtual"},
info = {name="signal-info",type="virtual"},
},
updateStation = {
richtext = {name="signal-stopname-richtext",type="virtual"},
schedule = {name="signal-schedule",type="virtual"},
stopname = {name="signal-stopname",type="virtual"},
go = {name="signal-goto",type="virtual"},
}
}
local events = {
on_renamed_station = script.generate_event_name(),
}
local function get_signals_filtered(filters,signals)
-- filters = {
-- SignalID,
-- }
local results = {}
local numfilters = table_size(filters)
local count = 0
for _,sig in pairs(signals) do
for i,f in pairs(filters) do
if f.name and sig.signal.type == f.type and sig.signal.name == f.name then
results[i] = sig.count
count = count + 1
if count == numfilters then return results end
end
end
end
return results
end
-- signal-info used to report errors
-- 0000|0000|0000|0000||0000|0000|0000|0000
-- | | | || | |xxxx|xxxx number of omitted wait conditions
-- | | | 1|| | | | contains item_count wait condition
-- | | | 1 || | | | contains fluid_count wait condition
-- | | | 1 || | | | contains circuit wait condition other than black!=0
-- | | |1 || | | | contains contradictory passenger conditions
-- | 1| | || | | | contains "OR" conditions
local function reportScheduleEntry(schedule)
local outsignals = {}
if schedule.rail then
local position = schedule.rail.position
outsignals[#outsignals+1]={index=#outsignals+1,count=position.x,signal=knownsignals.parseSchedule.X}
outsignals[#outsignals+1]={index=#outsignals+1,count=position.y,signal=knownsignals.parseSchedule.Y}
elseif schedule.station then
outsignals = remote.call('signalstrings','string_to_signals', schedule.station)
end
local waits = {}
local waittype = {
-- no arguments
full = function() waits.wait_full = 1 end,
empty = function() waits.wait_empty = 1 end,
robots_inactive = function() waits.wait_robots = 1 end,
-- value is time in ticks
time = function(wait) waits.wait_time = wait.ticks end,
inactivity = function(wait) waits.wait_inactivity = wait.ticks end,
-- no signal for these, report as info
item_count = function(wait)
wait.info = bit32.bor((wait.info or 0)+1, 0x10000)
end,
fluid_count = function(wait)
wait.info = bit32.bor((wait.info or 0)+1, 0x20000)
end,
-- no signal format for circuit conditions except black!=0, report those as an error bit on info too
circuit = function(wait)
local condition = wait.condition
if condition.first_signal and condition.first_signal.name == "signal-black" and
condition.comparator == "≠" and
not condition.second_signal and condition.constant == 0 then
waits.wait_circuit = 1
else
wait.info = bit32.bor((wait.info or 0)+1, 0x40000)
end
end,
-- these share a signal, set an info bit if both present.
passenger_present = function(wait)
if not waits.wait_passenger then
waits.wait_passenger = 1
elseif waits.wait_passenger == -1 then
wait.info = bit32.bor((wait.info or 0)+1, 0x80000)
end
end,
passenger_not_present = function(wait)
if not waits.wait_passenger then
waits.wait_passenger = -1
elseif waits.wait_passenger == 1 then
wait.info = bit32.bor((wait.info or 0)+1, 0x80000)
end
end,
}
for i,wait in pairs(schedule.wait_conditions) do
if wait.compare_type == "or" then
wait.info = bit32.bor((wait.info or 0)+1+(#wait - i), 0x01000000)
break
else
waittype[wait.type](wait)
end
end
for name,value in pairs(waits) do
outsignals[#outsignals+1]={index=#outsignals+1,count=value,signal=knownsignals.parseSchedule[name]}
end
--Array of frames, for future expansion to multi-frame reports of OR groups
return {outsignals}
end
local function parseScheduleEntry(signals,surface)
local knownsigs = get_signals_filtered(knownsignals.parseSchedule,signals)
local schedule = {wait_conditions = {}}
local userail = knownsigs.schedulerail or 0
if userail ~= 0 then
local x = knownsigs.X or 0
local y = knownsigs.Y or 0
x = x - (x % 2)
y = y - (y % 2)
if surface and surface.valid then
local rails = surface.find_entities_filtered{type={"straight-rail","curved-rail"},area={{x,y},{x+1,y+1}}}
if rails and rails[1] then
schedule.rail = rails[1]
else
-- list as "Invalid"
schedule.station = ""
end
else
-- list as "Invalid"
schedule.station = ""
end
else
local string = remote.call('signalstrings','signals_to_string',signals,knownsigs.richtext or false)
schedule.station = string
end
local sigwaitt = knownsigs.wait_time or 0
if sigwaitt > 0 then
table.insert(schedule.wait_conditions, {
type="time",
compare_type="and",
ticks = sigwaitt
})
end
local sigwaiti = knownsigs.wait_inactivity or 0
if sigwaiti > 0 then
table.insert(schedule.wait_conditions, {
type="inactivity",
compare_type="and",
ticks = sigwaiti
})
end
local sigwaite = knownsigs.wait_empty or 0
if sigwaite > 0 then
table.insert(schedule.wait_conditions, {
type="empty",
compare_type="and",
})
end
local sigwaitf = knownsigs.wait_full or 0
if sigwaitf > 0 then
table.insert(schedule.wait_conditions, {
type="full",
compare_type="and",
})
end
local sigwaitc = knownsigs.wait_circuit or 0
if sigwaitc > 0 then
table.insert(schedule.wait_conditions, {
type="circuit",
compare_type="and",
condition = { first_signal = {name="signal-black",type="virtual"}, comparator = "≠" }
})
end
local sigwaitp = knownsigs.wait_passenger or 0
if sigwaitp > 0 then
table.insert(schedule.wait_conditions, {
type="passenger_present",
compare_type="and",
})
elseif sigwaitp < 0 then
table.insert(schedule.wait_conditions, {
type="passenger_not_present",
compare_type="and",
})
end
local sigwaitr = knownsigs.wait_robots or 0
if sigwaitr > 0 then
table.insert(schedule.wait_conditions, {
type="robots_inactive",
compare_type="and",
})
end
return schedule
end
local function addDTSToTable(entity)
global.stringyStations[entity.unit_number] = { entity = entity, schedule = {}}
end
local function renameStringyStation(entity, stationNewName)
-- we can't just rename the station directly because if it is the only stop
-- of that name, it will edit existing schedules to replace the old name with new
local create_order = {
name = entity.name,
position = entity.position,
direction = entity.direction,
force = entity.force,
}
local stationUnitNumber = entity.unit_number
local stationCircuits = entity.circuit_connection_definitions
local stationControlBehavior = entity.get_control_behavior()
local stationSendToTrain = stationControlBehavior.send_to_train
local stationReadFromTrain = stationControlBehavior.read_from_train
local stationReadStoppedTrain = stationControlBehavior.read_stopped_train
local stationSetTrainsLimit = stationControlBehavior.set_trains_limit
local stationReadTrainsCount = stationControlBehavior.read_trains_count
local stationEnableDisable = stationControlBehavior.enable_disable
local stationStoppedTrainSignal = stationControlBehavior.stopped_train_signal
local stationTrainsCountSignal = stationControlBehavior.trains_count_signal
local stationTrainsLimitSignal = stationControlBehavior.trains_limit_signal
local stationCircuitCondition = stationControlBehavior.circuit_condition
local stationLogisticCondition = stationControlBehavior.logistic_condition
local stationConnectToLogisticNetwork = stationControlBehavior.connect_to_logistic_network
local surface = entity.surface
local trainLimit = entity.trains_limit
local stationColor = entity.color
-- raising destroyed here prevents rename of multiple stations connected together,
-- or possibly other updates of multiple unconnected stations in the same tick
entity.destroy()
local newStation = surface.create_entity(create_order)
newStation.trains_limit = trainLimit
newStation.color = stationColor
for i, wire in pairs(stationCircuits) do
newStation.connect_neighbour(wire)
end
local newStationControlBehavior = newStation.get_control_behavior()
newStationControlBehavior.send_to_train = stationSendToTrain
newStationControlBehavior.read_from_train = stationReadFromTrain
newStationControlBehavior.read_stopped_train = stationReadStoppedTrain
newStationControlBehavior.set_trains_limit = stationSetTrainsLimit
newStationControlBehavior.read_trains_count = stationReadTrainsCount
newStationControlBehavior.enable_disable = stationEnableDisable
newStationControlBehavior.stopped_train_signal = stationStoppedTrainSignal
newStationControlBehavior.trains_count_signal = stationTrainsCountSignal
newStationControlBehavior.trains_limit_signal = stationTrainsLimitSignal
newStationControlBehavior.circuit_condition = stationCircuitCondition
newStationControlBehavior.logistic_condition = stationLogisticCondition
newStationControlBehavior.connect_to_logistic_network = stationConnectToLogisticNetwork
addDTSToTable(newStation)
newStation.backer_name = stationNewName
script.raise_event(events.on_renamed_station,{old_unit_number = stationUnitNumber, entity = newStation})
return true
end
local function updateStringyStation(station)
local entity = station.entity
if not entity.valid then return true end
local signals = entity.get_merged_signals()
if signals and #signals >0 then
local knownsigs = get_signals_filtered(knownsignals.updateStation,signals)
if (knownsigs.stopname or 0) == 1 then
-- rename station
local string = remote.call('signalstrings','signals_to_string',signals,knownsigs.richtext or false)
if string ~= entity.backer_name then
return renameStringyStation(entity, string)
end
return
end
local sigsched = knownsigs.schedule or 0
if sigsched > 0 then
-- build schedule
local schedule = parseScheduleEntry(signals,entity.surface)
station.schedule[sigsched] = schedule
return
elseif sigsched == -1 then
-- set schedule, send to first
if station.schedule[1] then
for _,train in pairs(entity.surface.find_entities_filtered{area={{x=entity.position.x-2,y=entity.position.y-2},{x=entity.position.x+2,y=entity.position.y+2}},type='locomotive'}) do
if train.train.state == defines.train_state.wait_station and train.train.station == entity then
train.train.manual_mode = true
train.train.schedule = { current = 1, records = station.schedule}
train.train.manual_mode = false
station.schedule = {}
return
end
end
end
return
end
if (knownsigs.go or 0) ~= 0 then
-- send train to named station
for _,train in pairs(entity.surface.find_entities_filtered{area={{x=entity.position.x-2,y=entity.position.y-2},{x=entity.position.x+2,y=entity.position.y+2}},type='locomotive'}) do
if train.train.state == defines.train_state.wait_station and train.train.station == entity then
local string = remote.call('signalstrings','signals_to_string',signals,knownsigs.richtext or false)
train.train.manual_mode = true
train.train.schedule = { current = 1, records = {{station=string}}}
train.train.manual_mode = false
return
end
end
return
end
end
end
script.on_event(defines.events.on_built_entity, function(event)
if (event.created_entity.name == "stringy-train-stop") then
addDTSToTable(event.created_entity)
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.on_robot_built_entity, function(event)
if (event.created_entity.name == "stringy-train-stop") then
addDTSToTable(event.created_entity)
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.on_entity_cloned, function(event)
if (event.created_entity.name == "stringy-train-stop") then
addDTSToTable(event.destination)
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.script_raised_built, function(event)
if (event.created_entity.name == "stringy-train-stop") then
addDTSToTable(event.entity)
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.script_raised_revive, function(event)
if (event.created_entity.name == "stringy-train-stop") then
addDTSToTable(event.entity)
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.on_pre_player_mined_item, function(event)
if (event.entity.name == "stringy-train-stop") then
global.stringyStations[event.entity.unit_number] = nil
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.on_robot_pre_mined, function(event)
if (event.entity.name == "stringy-train-stop") then
global.stringyStations[event.entity.unit_number] = nil
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.on_entity_died, function(event)
if (event.entity.name == "stringy-train-stop") then
global.stringyStations[event.entity.unit_number] = nil
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_event(defines.events.script_raised_destroy, function(event)
if (event.entity.name == "stringy-train-stop") then
global.stringyStations[event.entity.unit_number] = nil
end
end, {{filter="name",name="stringy-train-stop"}})
script.on_init(function()
global = {
stringyStations = {
-- [unit_number] = {entity=entity,schedule={}}
},
}
end)
script.on_configuration_changed(function(event)
if global.schedules then
local data = {}
for _,entity in pairs(global.stringyStations) do
if entity.valid then
data[entity.unit_number] = { entity = entity, schedule = global.schedules[entity.unit_number] or {} }
end
end
global.stringyStations = data
global.schedules = nil
elseif not global.stringyStations then
global.stringyStations = {}
end
end)
script.on_event(defines.events.on_tick, function()
for id, station in pairs(global.stringyStations) do
if updateStringyStation(station) then
global.stringyStations[id] = nil
end
end
end)
remote.add_interface("stringy-train-stop",{
reportScheduleEntry = reportScheduleEntry,
parseScheduleEntry = parseScheduleEntry,
getEvents = function() return events end,
})