-
Notifications
You must be signed in to change notification settings - Fork 8
/
control.lua
1446 lines (1271 loc) · 50.4 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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "defines"
require "util"
MOD_NAME = "TheFatController"
local function init_global()
global = global or {}
global.guiSettings = global.guiSettings or {}
global.trainsByForce = global.trainsByForce or {}
global.character = global.charactor or {}
global.unlocked = global.unlocked or false
global.unlockedByForce = global.unlockedByForce or {}
global.version = "0.3.1"
end
defaultGuiSettings = { alarm={active=false,noPath=true,timeAtSignal=true,timeToStation=true},
displayCount=9,
fatControllerButtons = {},
fatControllerGui = {},
page = 2,
pageCount = 5,
stationFilterList = {}
}
local function init_player(player)
global.guiSettings[player.index] = global.guiSettings[player.index] or util.table.deepcopy(defaultGuiSettings)
if global.unlockedByForce[player.force.name] then
init_gui(player)
end
end
local function init_players()
for i,player in pairs(game.players) do
init_player(player)
end
end
local function init_force(force)
global.trainsByForce[force.name] = global.trainsByForce[force.name] or {}
if force.technologies["rail-signals"].researched then
global.unlockedByForce[force.name] = true
global.unlocked = true
register_events()
for i,p in pairs(force.players) do
init_player(p)
end
end
end
local function init_forces()
for i, force in pairs(game.forces) do
init_force(force)
end
end
local function on_init()
init_global()
init_forces()
end
local function on_load()
if global.unlocked then
register_events()
end
end
-- run once
local function on_configuration_changed(data)
if not data or not data.mod_changes then
return
end
if data.mod_changes[MOD_NAME] then
local newVersion = data.mod_changes[MOD_NAME].new_version
local oldVersion = data.mod_changes[MOD_NAME].old_version
if oldVersion then
if oldVersion < "0.3.14" then
-- Kill all old versions of TFC
if global.fatControllerGui ~= nil or global.fatControllerButtons ~= nil then
destroyGui(global.fatControllerGui)
destroyGui(global.fatControllerButtons)
end
for i,p in pairs(game.players) do
destroyGui(p.gui.top.fatControllerButtons)
destroyGui(p.gui.left.fatController)
end
global = nil
end
end
init_global()
init_forces()
init_players()
if not oldVersion or oldVersion < "0.3.14" then
findTrains()
end
global.version = newVersion
end
--check for other mods
end
local function on_player_created(event)
init_player(game.players[event.player_index])
end
local function on_force_created(event)
init_force(event.force)
end
local function on_forces_merging(event)
end
script.on_init(on_init)
script.on_load(on_load)
script.on_configuration_changed(on_configuration_changed)
script.on_event(defines.events.on_player_created, on_player_created)
script.on_event(defines.events.on_force_created, on_force_created)
script.on_event(defines.events.on_forces_merging, on_forces_merging)
function register_events()
script.on_event(defines.events.on_train_changed_state, on_train_changed_state)
script.on_event(defines.events.on_tick, onTickAfterUnlocked)
script.on_event(defines.events.on_gui_click, on_gui_click)
end
--Called if tech is unlocked
function init_gui(player)
debugLog("Init: " .. player.name .. " - " .. player.force.name)
if player.gui.top.fatControllerButtons ~= nil then
return
end
local guiSettings = global.guiSettings[player.index]
local forceName = player.force.name
guiSettings.stationFilterList = buildStationFilterList(global.trainsByForce[player.force.name])
debugLog("create guis")
if player.gui.left.fatController == nil then
guiSettings.fatControllerGui = player.gui.left.add({ type="flow", name="fatController", direction="vertical"})--, style="fatcontroller_thin_flow"}) --caption="Fat Controller",
else
guiSettings.fatControllerGui = player.gui.left.fatController
end
if player.gui.top.fatControllerButtons == nil then
guiSettings.fatControllerButtons = player.gui.top.add({ type="flow", name="fatControllerButtons", direction="horizontal", style="fatcontroller_thin_flow"})
else
guiSettings.fatControllerButtons = player.gui.top.fatControllerButtons
end
if guiSettings.fatControllerButtons.toggleTrainInfo == nil then
guiSettings.fatControllerButtons.add({type="button", name="toggleTrainInfo", caption = {"text-trains-collapsed"}, style="fatcontroller_button_style"})
end
if guiSettings.fatControllerGui.trainInfo ~= nil then
newTrainInfoWindow(guiSettings)
updateTrains(global.trainsByForce[forceName])
end
guiSettings.pageCount = getPageCount(global.trainsByForce[forceName], guiSettings)
filterTrainInfoList(global.trainsByForce[forceName], guiSettings.activeFilterList)
updateTrains(global.trainsByForce[forceName])
refreshTrainInfoGui(global.trainsByForce[forceName], guiSettings, player.character)
return guiSettings
end
script.on_event(defines.events.on_research_finished, function(event)
local status, err = pcall(on_research_finished, event)
if err then debugDump(err,true) end
end)
function on_research_finished(event)
if event.research.name == "rail-signals" then
global.unlockedByForce[event.research.force.name] = true
global.unlocked = true
register_events() ;
for _, p in pairs(event.research.force.players) do
init_gui(p)
end
end
end
onTickAfterUnlocked = function(event)
local status, err = pcall(function()
if event.tick%60==13 then
local updateGui = false
for i,guiSettings in pairs(global.guiSettings) do
if guiSettings.fatControllerGui ~= nil and guiSettings.fatControllerGui.trainInfo ~= nil then
updateGui = true
end
end
if updateGui then
debugLog("updateGUI")
for _,trains in pairs(global.trainsByForce) do
updateTrains(trains)
end
refreshAllTrainInfoGuis(global.trainsByForce, global.guiSettings, game.players, false)
for i,player in pairs(game.players) do
refreshTrainInfoGui(global.trainsByForce[player.force.name], global.guiSettings[i], player.character)
global.guiSettings[i].pageCount = getPageCount(global.trainsByForce[player.force.name], global.guiSettings[i])
end
end
end
--handle remote camera
for i,guiSettings in pairs(global.guiSettings) do
if guiSettings.followEntity ~= nil then -- Are we in remote camera mode?
if not guiSettings.followEntity.valid or game.players[i].vehicle == nil then
swapPlayer(game.players[i], global.character[i])
if guiSettings.fatControllerButtons ~= nil and guiSettings.fatControllerButtons.returnToPlayer ~= nil then
guiSettings.fatControllerButtons.returnToPlayer.destroy()
end
if not guiSettings.followEntity.valid then
removeTrainInfoFromEntity(global.trainsByForce[game.players[i].force.name], guiSettings.followEntity)
newTrainInfoWindow(guiSettings)
refreshTrainInfoGui(global.trainsByForce[game.players[i].force.name], guiSettings, game.players[i].character)
end
global.character[i] = nil
guiSettings.followEntity = nil
elseif global.character[i] and global.character[i].valid and guiSettings.fatControllerButtons ~= nil and guiSettings.fatControllerButtons.returnToPlayer == nil then
guiSettings.fatControllerButtons.add({ type="button", name="returnToPlayer", caption={"text-player"}, style = "fatcontroller_selected_button"})
--game.players[1].teleport(global.followEntity.position)
elseif global.character[i] ~= nil and not global.character[i].valid then
game.set_game_state({gamefinished=true, playerwon=false})
end
end
end
-- on_the_path = 0,
-- -- had path and lost it - must stop
-- path_lost = 1,
-- -- doesn't have anywhere to go
-- no_schedule = 2,
-- has no path and is stopped
-- no_path = 3,
-- -- braking before the railSignal
-- arrive_signal = 4,
-- wait_signal = 5,
-- -- braking before the station
-- arrive_station = 6,
-- wait_station = 7,
-- -- switched to the manual control and has to stop
-- manual_control_stop = 8,
-- -- can move if user explicitly sits in and rides the train
-- manual_control = 9,
-- -- train was switched to auto control but it is moving and needs to be stopped
-- stop_for_auto_control = 10
if event.tick%120 == 37 then
local alarmState = {}
alarmState.timeToStation = false
alarmState.timeAtSignal = false
alarmState.noPath = false
local newAlarm = false
for forceName,trains in pairs(global.trainsByForce) do
for i,trainInfo in pairs(trains) do
local alarmSet = false
if trainInfo.lastState == 1 or trainInfo.lastState == 3 then
--game.players[1].print("No Path " .. i .. " " .. game.tick)
if not trainInfo.alarm then
alarmState.noPath = true
newAlarm = true
trainInfo.updated = true
end
alarmSet = true
trainInfo.alarm = true
end
-- 36000, 10 minutes
if trainInfo.lastState ~= 7 and trainInfo.lastStateStation ~= nil and (trainInfo.lastStateStation + 36000 < game.tick and (trainInfo.lastState ~= 2 or trainInfo.lastState ~= 8 or trainInfo.lastState ~= 9)) then
if not trainInfo.alarm then
alarmState.timeToStation = true
newAlarm = true
trainInfo.updated = true
end
alarmSet = true
trainInfo.alarm = true
end
-- 72002 minutes lol, wtf?
if trainInfo.lastState == 5 and (trainInfo.lastStateTick ~= nil and trainInfo.lastStateTick + 7200 < game.tick ) then
if not trainInfo.alarm then
alarmState.timeAtSignal = true
newAlarm = true
trainInfo.updated = true
end
alarmSet = true
trainInfo.alarm = true
end
if not alarmSet then
if trainInfo.alarm then
trainInfo.updated = true
end
trainInfo.alarm = false
end
end
end
for i,guiSettings in pairs(global.guiSettings) do
if guiSettings.alarm == nil or guiSettings.alarm.noPath == nil then
guiSettings.alarm = {}
guiSettings.alarm.timeToStation = true
guiSettings.alarm.timeAtSignal = true
guiSettings.alarm.noPath = true
end
if (guiSettings.alarm.timeToStation or guiSettings.alarm.timeAtSignal or guiSettings.alarm.noPath) and newAlarm then
if guiSettings.alarm.timeToStation and alarmState.timeToStation then
guiSettings.alarm.active = true
alertPlayer(game.players[i], guiSettings, game.tick, ({"msg-alarm-tolongtostation"}))
end
if guiSettings.alarm.timeAtSignal and alarmState.timeAtSignal then
guiSettings.alarm.active = true
alertPlayer(game.players[i], guiSettings, game.tick, ({"msg-alarm-tolongatsignal"}))
end
if guiSettings.alarm.noPath and alarmState.noPath then
guiSettings.alarm.active = true
alertPlayer(game.players[i], guiSettings, game.tick, ({"msg-alarm-nopath"}))
end
else
guiSettings.alarm.active = false
end
end
end
end)
if err then debugDump(err,true) end
--if event.tick%30==4 and global.fatControllerGui.trainInfo ~= nil then
--refreshTrainInfoGui(global.trains, global.fatControllerGui.trainInfo, global.guiSettings, game.players[1].character)
--end
end
function alertPlayer(player,guiSettings,tick,message)
if player ~= nil and guiSettings ~= nil and guiSettings.alarm ~= nil and guiSettings.alarm.active and (guiSettings.alarm.lastMessage == nil or guiSettings.alarm.lastMessage + 120 < tick) then
guiSettings.lastMessage = tick
player.print(message)
end
end
function destroyGui(guiA)
if guiA ~= nil and guiA.valid then
guiA.destroy()
end
end
function buildStationFilterList(trains)
local newList = {}
if trains ~= nil then
for i, trainInfo in pairs(trains) do
-- if trainInfo.group ~= nil then
-- newList[trainInfo.group] = true
-- end
if trainInfo.stations ~= nil then
for station, value in pairs(trainInfo.stations) do
--debugLog(station)
newList[station] = true
end
end
end
end
return newList
end
function getLocomotives(train)
if train ~= nil and train.valid then
local locos = {}
for i,carriage in pairs(train.carriages) do
if carriage ~= nil and carriage.valid and isTrainType(carriage.type) then
table.insert(locos, carriage)
end
end
return locos
end
end
function getNewTrainInfo(train)
if train ~= nil then
local carriages = train.carriages
if carriages ~= nil and carriages[1] ~= nil and carriages[1].valid then
local newTrainInfo = {}
newTrainInfo.train = train
--newTrainInfo.firstCarriage = getFirstCarriage(train)
newTrainInfo.locomotives = getLocomotives(train)
--newTrainInfo.display = true
return newTrainInfo
end
end
end
entityBuilt = function(event)
local entity = event.created_entity
if entity.type == "locomotive" and global.unlocked then --or entity.type == "cargo-wagon"
getTrainInfoOrNewFromEntity(global.trainsByForce[entity.force.name], entity)
end
end
script.on_event(defines.events.on_built_entity, entityBuilt)
script.on_event(defines.events.on_robot_built_entity, entityBuilt)
-- -- normal state - following the path
-- on_the_path = 0,
-- -- had path and lost it - must stop
-- path_lost = 1,
-- -- doesn't have anywhere to go
-- no_schedule = 2,
-- -- has no path and is stopped
-- no_path = 3,
-- -- braking before the railSignal
-- arrive_signal = 4,
-- wait_signal = 5,
-- -- braking before the station
-- arrive_station = 6,
-- wait_station = 7,
-- -- switched to the manual control and has to stop
-- manual_control_stop = 8,
-- -- can move if user explicitly sits in and rides the train
-- manual_control = 9,
-- -- train was switched to auto control but it is moving and needs to be stopped
-- stop_for_auto_control = 10
function on_train_changed_state(event)
local status, err = pcall(function()
--debugLog("State Change - " .. game.tick)
local train = event.train
local entity = train.carriages[1]
local trains = global.trainsByForce[entity.force.name]
local trainInfo = getTrainInfoOrNewFromEntity(trains, entity)
if trainInfo ~= nil then
local newtrain = false
if trainInfo.updated == nil then
newtrain = true
else
end
updateTrainInfo(trainInfo,game.tick)
if newtrain then
for i,player in pairs(game.players) do
global.guiSettings[i].pageCount = getPageCount(trains, global.guiSettings[i])
end
end
refreshAllTrainInfoGuis(global.trainsByForce, global.guiSettings, game.players, newtrain)
end
end)
if err then debugDump(err,true) end
end
function getTrainInfoOrNewFromTrain(trains, train)
if trains ~= nil then
for i, trainInfo in pairs(trains) do
if trainInfo ~= nil and trainInfo.train and trainInfo.train.valid and train == trainInfo.train then
return trainInfo
end
end
local newTrainInfo = getNewTrainInfo(train)
table.insert(trains, newTrainInfo)
return newTrainInfo
end
end
function getTrainInfoOrNewFromEntity(trains, entity)
local trainInfo = getTrainInfoFromEntity(trains, entity)
if trainInfo == nil then
local newTrainInfo = getNewTrainInfo(entity.train)
table.insert(trains, newTrainInfo)
return newTrainInfo
else
return trainInfo
end
end
function on_gui_click(event)
local status, err = pcall(function()
local refreshGui = false
local newInfoWindow = false
local rematchStationList = false
local guiSettings = global.guiSettings[event.element.player_index]
local player = game.players[event.element.player_index]
local trains = global.trainsByForce[player.force.name]
debugLog("CLICK! " .. event.element.name .. game.tick)
if guiSettings.alarm == nil then
guiSettings.alarm = {}
end
if event.element.name == "toggleTrainInfo" then
if guiSettings.fatControllerGui.trainInfo == nil then
newInfoWindow = true
refreshGui = true
event.element.caption = {"text-trains"}
else
guiSettings.fatControllerGui.trainInfo.destroy()
event.element.caption = {"text-trains-collapsed"}
end
elseif event.element.name == "returnToPlayer" then
if global.character[event.element.player_index] ~= nil then
if player.vehicle ~= nil then
player.vehicle.passenger = nil
end
swapPlayer(player, global.character[event.element.player_index])
global.character[event.element.player_index] = nil
event.element.destroy()
guiSettings.followEntity = nil
end
elseif endsWith(event.element.name,"_toggleManualMode") then
local trainInfo = getTrainInfoFromElementName(trains, event.element.name)
if trainInfo ~= nil and trainInfo.train ~= nil and trainInfo.train.valid then
trainInfo.train.manual_mode = not trainInfo.train.manual_mode
swapCaption(event.element, "ll", ">")
end
elseif endsWith(event.element.name,"_toggleFollowMode") then
local trainInfo = getTrainInfoFromElementName(trains, event.element.name)
if trainInfo ~= nil and trainInfo.train ~= nil and trainInfo.train.valid then
if global.character[event.element.player_index] == nil then --Move to train
if trainInfo.train.carriages[1].passenger ~= nil then
player.print({"msg-intrain"})
else
global.character[event.element.player_index] = player.character
guiSettings.followEntity = trainInfo.train.carriages[1] -- HERE
--fatControllerEntity =
swapPlayer(player,newFatControllerEntity(player))
--event.element.style = "fatcontroller_selected_button"
event.element.caption = "X"
trainInfo.train.carriages[1].passenger = player.character
end
elseif guiSettings.followEntity ~= nil and trainInfo.train ~= nil and trainInfo.train.valid then
if player.vehicle ~= nil then
player.vehicle.passenger = nil
end
if guiSettings.followEntity == trainInfo.train.carriages[1] or trainInfo.train.carriages[1].passenger ~= nil then --Go back to player
swapPlayer(player, global.character[event.element.player_index])
--event.element.style = "fatcontroller_button_style"
event.element.caption = "c"
if guiSettings.fatControllerButtons ~= nil and guiSettings.fatControllerButtons.returnToPlayer ~= nil then
guiSettings.fatControllerButtons.returnToPlayer.destroy()
end
global.character[event.element.player_index] = nil
guiSettings.followEntity = nil
else -- Go to different train
guiSettings.followEntity = trainInfo.train.carriages[1] -- AND HERE
--event.element.style = "fatcontroller_selected_button"
event.element.caption = "X"
trainInfo.train.carriages[1].passenger = player.character
--game.players[1].vehicle = trainInfo.train.carriages[1]
end
end
end
elseif event.element.name == "page_back" then
if guiSettings.page > 1 then
guiSettings.page = guiSettings.page - 1
--global.fatControllerGui.trainInfo.trainInfoControls.page_number.caption = global.guiSettings.page
--newTrainInfoWindow(global.fatControllerGui, global.guiSettings)
--refreshTrainInfoGui(global.trains, global.fatControllerGui.trainInfo, global.guiSettings)
newInfoWindow = true
refreshGui = true
end
elseif event.element.name == "page_forward" then
if guiSettings.page < getPageCount(trains, guiSettings) then
guiSettings.page = guiSettings.page + 1
--debugLog(global.guiSettings.page)
--newTrainInfoWindow(global.fatControllerGui, global.guiSettings)
--refreshTrainInfoGui(global.trains, global.fatControllerGui.trainInfo, global.guiSettings)
newInfoWindow = true
refreshGui = true
end
elseif event.element.name == "page_number" then
togglePageSelectWindow(player.gui.center, guiSettings)
elseif event.element.name == "pageSelectOK" then
local gui = player.gui.center.pageSelect
if gui ~= nil then
local newInt = tonumber(gui.pageSelectValue.text)
if newInt then
if newInt < 1 then
newInt = 1
elseif newInt > 50 then
newInt = 50
end
guiSettings.displayCount = newInt
guiSettings.pageCount = getPageCount(trains, guiSettings)
guiSettings.page = 1
refreshGui = true
newInfoWindow = true
else
player.print({"msg-notanumber"})
end
gui.destroy()
end
elseif event.element.name == "toggleStationFilter" then
guiSettings.stationFilterList = buildStationFilterList(trains)
toggleStationFilterWindow(player.gui.center, guiSettings)
elseif event.element.name == "clearStationFilter" or event.element.name == "stationFilterClear" then
if guiSettings.activeFilterList ~= nil then
guiSettings.activeFilterList = nil
rematchStationList = true
newInfoWindow = true
refreshGui = true
end
if player.gui.center.stationFilterWindow ~= nil then
player.gui.center.stationFilterWindow.destroy()
end
elseif event.element.name == "stationFilterOK" then
local gui = player.gui.center.stationFilterWindow
if gui ~= nil and gui.checkboxGroup ~= nil then
local newFilter = {}
local listEmpty = true
for station,value in pairs(guiSettings.stationFilterList) do
local checkboxA = gui.checkboxGroup[station .. "_stationFilter"]
if checkboxA ~= nil and checkboxA.state then
listEmpty = false
--debugLog(station)
newFilter[station] = true
end
end
if not listEmpty then
guiSettings.activeFilterList = newFilter
--glo.filteredTrains = buildFilteredTrainInfoList(global.trains, global.guiSettings.activeFilterList)
else
guiSettings.activeFilterList = nil
end
gui.destroy()
guiSettings.page = 1
rematchStationList = true
newInfoWindow = true
refreshGui = true
end
elseif endsWith(event.element.name,"_stationFilter") then
local stationName = string.gsub(event.element.name, "_stationFilter", "")
if event.element.state then
if guiSettings.activeFilterList == nil then
guiSettings.activeFilterList = {}
end
guiSettings.activeFilterList[stationName] = true
elseif guiSettings.activeFilterList ~= nil then
guiSettings.activeFilterList[stationName] = nil
if tableIsEmpty(guiSettings.activeFilterList) then
guiSettings.activeFilterList = nil
end
end
--debugLog(event.element.name)
guiSettings.page = 1
rematchStationList = true
newInfoWindow = true
refreshGui = true
--alarmOK alarmTimeToStation alarmTimeAtSignal alarmNoPath alarmButton
elseif event.element.name == "alarmButton" or event.element.name == "alarmOK" then
toggleAlarmWindow(player.gui.center, guiSettings)
elseif event.element.name == "alarmTimeToStation" then
guiSettings.alarm.timeToStation = event.element.state
elseif event.element.name == "alarmTimeAtSignal" then
guiSettings.alarm.timeAtSignal = event.element.state
elseif event.element.name == "alarmNoPath" then
guiSettings.alarm.noPath = event.element.state
end
if rematchStationList then
filterTrainInfoList(trains, guiSettings.activeFilterList)
guiSettings.pageCount = getPageCount(trains, guiSettings)
end
if newInfoWindow then
newTrainInfoWindow(guiSettings)
end
if refreshGui then
refreshTrainInfoGui(trains, guiSettings, player.character)
end
end)
if err then debugDump(err,true) end
end
-- function swapStyle(guiElement, styleA, styleB)
-- if guiElement ~= nil and styleA ~= nil and styleB ~= nil then
-- if guiElement.style == styleA then
-- guiElement.style = styleB
-- elseif guiElement.style == styleB then
-- guiElement.style = styleA
-- end
-- end
-- end
function swapCaption(guiElement, captionA, captionB)
if guiElement ~= nil and captionA ~= nil and captionB ~= nil then
if guiElement.caption == captionA then
guiElement.caption = captionB
elseif guiElement.caption == captionB then
guiElement.caption = captionA
end
end
end
function tableIsEmpty(tableA)
if tableA ~= nil then
for i,v in pairs(tableA) do
return false
end
end
return true
end
function toggleStationFilterWindow(gui, guiSettings)
if gui ~= nil then
if gui.stationFilterWindow == nil then
--local sortedList = table.sort(a)
local window = gui.add({type="frame", name="stationFilterWindow", caption={"msg-stationFilter"}, direction="vertical" }) --style="fatcontroller_thin_frame"})
window.add({type="table", name="checkboxGroup", colspan=3})
for name, value in pairsByKeys(guiSettings.stationFilterList) do
if guiSettings.activeFilterList ~= nil and guiSettings.activeFilterList[name] then
window.checkboxGroup.add({type="checkbox", name=name .. "_stationFilter", caption=name, state=true}) --style="filter_group_button_style"})
else
window.checkboxGroup.add({type="checkbox", name=name .. "_stationFilter", caption=name, state=false}) --style="filter_group_button_style"})
end
end
window.add({type="flow", name="buttonFlow"})
window.buttonFlow.add({type="button", name="stationFilterClear", caption={"msg-Clear"}})
window.buttonFlow.add({type="button", name="stationFilterOK", caption={"msg-OK"}})
else
gui.stationFilterWindow.destroy()
end
end
end
function togglePageSelectWindow(gui, guiSettings)
if gui ~= nil then
if gui.pageSelect == nil then
local window = gui.add({type="frame", name="pageSelect", caption={"msg-displayCount"}, direction="vertical" }) --style="fatcontroller_thin_frame"})
window.add({type="textfield", name="pageSelectValue", text=guiSettings.displayCount .. ""})
window.pageSelectValue.text = guiSettings.displayCount .. ""
window.add({type="button", name="pageSelectOK", caption={"msg-OK"}})
else
gui.pageSelect.destroy()
end
end
end
function toggleAlarmWindow(gui, guiSettings)
if gui ~= nil then
if gui.alarmWindow == nil then
local window = gui.add({type="frame",name="alarmWindow", caption={"text-alarmwindow"}, direction="vertical" })
local stateTimeToStation = true
if guiSettings.alarm ~= nil and not guiSettings.alarm.timeToStation then
stateTimeToStation = false
end
window.add({type="checkbox", name="alarmTimeToStation", caption={"text-alarmtimetostation"}, state=stateTimeToStation}) --style="filter_group_button_style"})
local stateTimeAtSignal = true
if guiSettings.alarm ~= nil and not guiSettings.alarm.timeAtSignal then
stateTimeAtSignal = false
end
window.add({type="checkbox", name="alarmTimeAtSignal", caption={"text-alarmtimeatsignal"}, state=stateTimeAtSignal}) --style="filter_group_button_style"})
local stateNoPath = true
if guiSettings.alarm ~= nil and not guiSettings.alarm.noPath then
stateNoPath = false
end
window.add({type="checkbox", name="alarmNoPath", caption={"text-alarmtimenopath"}, state=stateNoPath}) --style="filter_group_button_style"})
window.add({type="button", name="alarmOK", caption={"msg-OK"}})
else
gui.alarmWindow.destroy()
end
end
end
function getPageCount(trains, guiSettings)
local trainCount = 0
for i,trainInfo in pairs(trains) do
if guiSettings.activeFilterList == nil or trainInfo.matchesStationFilter then
trainCount = trainCount + 1
end
end
return math.floor((trainCount - 1) / guiSettings.displayCount) + 1
end
local onEntityDied = function (event)
if global.unlocked and global.guiSettings ~= nil then
for forceName,trains in pairs(global.trainsByForce) do
updateTrains(trains)
end
for i, player in pairs(game.players) do
local guiSettings = global.guiSettings[i]
if guiSettings.followEntity ~= nil and guiSettings.followEntity == event.entity then --Go back to player
if game.players[i].vehicle ~= nil then
game.players[i].vehicle.passenger = nil
end
swapPlayer(game.players[i], global.character[i])
if guiSettings.fatControllerButtons.returnToPlayer ~= nil then
guiSettings.fatControllerButtons.returnToPlayer.destroy()
end
global.character[i] = nil
guiSettings.followEntity = nil
end
end
refreshAllTrainInfoGuis(global.trainsByForce, global.guiSettings, game.players, true)
end
end
function refreshAllTrainInfoGuis(trainsByForce, guiSettings, players, destroy)
for i,player in pairs(players) do
--local trainInfo = guiSettings[i].fatControllerGui.trainInfo
if guiSettings[i] ~= nil and guiSettings[i].fatControllerGui.trainInfo ~= nil then
if destroy then
guiSettings[i].fatControllerGui.trainInfo.destroy()
newTrainInfoWindow(guiSettings[i])
end
refreshTrainInfoGui(trainsByForce[player.force.name], guiSettings[i], player.character)
end
end
end
script.on_event(defines.events.on_entity_died, onEntityDied)
script.on_event(defines.events.on_preplayer_mined_item, onEntityDied)
function swapPlayer(player, character)
--player.teleport(character.position)
if player.character ~= nil and player.character.valid and player.character.name == "fatcontroller" then
player.character.destroy()
end
player.character = character
end
function isTrainType(type)
if type == "locomotive" then
return true
end
return false
end
function getTrainInfoFromElementName(trains, elementName)
for i, trainInfo in pairs(trains) do
if trainInfo ~= nil and trainInfo.guiName ~= nil and startsWith(elementName, trainInfo.guiName .. "_") then
return trainInfo
end
end
end
function getTrainInfoFromEntity(trains, entity)
if trains ~= nil then
for i, trainInfo in pairs(trains) do
if trainInfo ~= nil and trainInfo.train ~= nil and trainInfo.train.valid and entity == trainInfo.train.carriages[1] then
return trainInfo
end
end
end
end
function removeTrainInfoFromElementName(trains, elementName)
for i, trainInfo in pairs(trains) do
if trainInfo ~= nil and trainInfo.guiName ~= nil and startsWith(elementName, trainInfo.guiName .. "_") then
table.remove(trains, i)
return
end
end
end
function removeTrainInfoFromEntity(trains, entity)
for i, trainInfo in pairs(trains) do
if trainInfo ~= nil and trainInfo.train ~= nil and trainInfo.train.valid and trainInfo.train.carriages[1] == entity then
table.remove(trains, i)
return
end
end
end
function getHighestInventoryCount(trainInfo)
local inventory = nil
if trainInfo ~= nil and trainInfo.train ~= nil and trainInfo.train.valid and trainInfo.train.carriages ~= nil then
local itemsCount = 0
local largestItem = {}
local items = trainInfo.train.get_contents() or {}
for i, carriage in pairs(trainInfo.train.carriages) do
if carriage and carriage.valid and carriage.name == "rail-tanker" then
debugLog("Looking for Oil!")
local liquid = remote.call("railtanker","getLiquidByWagon",carriage)
if liquid then
debugLog("Liquid!")
local name = liquid.type
local count = math.floor(liquid.amount)
if name then
if not items[name] then
items[name] = 0
end
items[name] = items[name] + count
end
end
end
end
for name, count in pairs(items) do
if largestItem.count == nil or largestItem.count < items[name] then
largestItem.name = name
largestItem.count = items[name]
end
itemsCount = itemsCount + 1
end
if largestItem.name ~= nil then
local isItem = game.item_prototypes[largestItem.name]
local displayName = isItem and isItem.localised_name or {"", largestItem.name}
local suffix = itemsCount > 1 and "..." or ""
inventory = {"", displayName,": ",largestItem.count, suffix}
else
inventory = ""
end
end
return inventory
end
function newFatControllerEntity(player)
return player.surface.create_entity({name="fatcontroller", position=player.position, force=player.force})
-- local entities = game.find_entities_filtered({area={{position.x, position.y},{position.x, position.y}}, name="fatcontroller"})
-- if entities[1] ~= nil then
-- return entities[1]
-- end
end
function newTrainInfoWindow(guiSettings)
if guiSettings == nil then
guiSettings = util.table.deepcopy({ displayCount = 9, page = 1})
end
local gui = guiSettings.fatControllerGui
if gui ~= nil and gui.trainInfo ~= nil then
gui.trainInfo.destroy()
end
local newGui
if gui ~= nil and gui.trainInfo ~= nil then
newGui = gui.trainInfo
else
newGui = gui.add({ type="flow", name="trainInfo", direction="vertical", style="fatcontroller_thin_flow"})
end
if newGui.trainInfoControls == nil then
newGui.add({type = "frame", name="trainInfoControls", direction="horizontal", style="fatcontroller_thin_frame"})
end
if newGui.trainInfoControls.pageButtons == nil then
newGui.trainInfoControls.add({type = "flow", name="pageButtons", direction="horizontal", style="fatcontroller_button_flow"})
end
if newGui.trainInfoControls.pageButtons.page_back == nil then
if guiSettings.page > 1 then
newGui.trainInfoControls.pageButtons.add({type="button", name="page_back", caption="<", style="fatcontroller_button_style"})
else
newGui.trainInfoControls.pageButtons.add({type="button", name="page_back", caption="<", style="fatcontroller_disabled_button"})
end
end
-- if guiSettings.page > 1 then
-- newGui.trainInfoControls.pageButtons.page_back.style = "fatcontroller_button_style"
-- else
-- newGui.trainInfoControls.pageButtons.page_back.style = "fatcontroller_disabled_button"
-- end
if newGui.trainInfoControls.pageButtons.page_number == nil then
newGui.trainInfoControls.pageButtons.add({type="button", name="page_number", caption=guiSettings.page .. "/" .. guiSettings.pageCount, style="fatcontroller_button_style"})
else
newGui.trainInfoControls.pageButtons.page_number.caption = guiSettings.page .. "/" .. guiSettings.pageCount
end
if newGui.trainInfoControls.pageButtons.page_forward == nil then
if guiSettings.page < guiSettings.pageCount then
newGui.trainInfoControls.pageButtons.add({type="button", name="page_forward", caption=">", style="fatcontroller_button_style"})
else
newGui.trainInfoControls.pageButtons.add({type="button", name="page_forward", caption=">", style="fatcontroller_disabled_button"})
end
end
-- if guiSettings.page < guiSettings.pageCount then
-- newGui.trainInfoControls.pageButtons.page_forward.style = "fatcontroller_button_style"
-- else
-- newGui.trainInfoControls.pageButtons.page_forward.style = "fatcontroller_disabled_button"
-- end
if newGui.trainInfoControls.filterButtons == nil then
newGui.trainInfoControls.add({type = "flow", name="filterButtons", direction="horizontal", style="fatcontroller_button_flow"})
end
if newGui.trainInfoControls.filterButtons.toggleStationFilter == nil then
if guiSettings.activeFilterList ~= nil then
newGui.trainInfoControls.filterButtons.add({type="button", name="toggleStationFilter", caption="s", style="fatcontroller_selected_button"})
else
newGui.trainInfoControls.filterButtons.add({type="button", name="toggleStationFilter", caption="s", style="fatcontroller_button_style"})
end
end
if newGui.trainInfoControls.filterButtons.clearStationFilter == nil then
--if guiSettings.activeFilterList ~= nil then
--newGui.trainInfoControls.filterButtons.add({type="button", name="clearStationFilter", caption="x", style="fatcontroller_selected_button"})