-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.lua
3282 lines (3102 loc) · 149 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 "util"
mod_gui = require("mod-gui")
require("control-util")
require("api")
require("bp")
require("priority-util")
require("tsm_commands")
MOD_NAME = "TrainManager"
get_sprite_button = function(player)
-- debugp("in get_sprite_button")
local button_flow = mod_gui.get_button_flow(player)
local button = button_flow.tm_sprite_button
if button then
if player.force.technologies["train-manager"].researched ~= true then
button.destroy()
end
return
end
if not button then
button = button_flow.add
{
type = "sprite-button",
name = "tm_sprite_button",
sprite = "train_manager",
style = mod_gui.button_style,
-- tooltip = {"gui-trainps.button-tooltip"}
}
-- button.style.visible = any
if player.mod_settings["ps-tooltip"].value == true then
button.tooltip = { "gui-trainps.button-tooltip" }
end
end
return button
end
script.on_event(defines.events.on_surface_created, function(event)
local surface = game.get_surface(event.surface_index)
if surface then
storage.newcounters = storage.newcounters or {}
storage.newcounters[surface.name] = storage.newcounters[surface.name] or {}
storage.newpriority = storage.newpriority or {}
storage.newpriority[surface.name] = storage.newpriority[surface.name] or {}
storage.newpublishers = storage.newpublishers or {}
storage.newpublishers[surface.name] = storage.newpublishers[surface.name] or {}
end
end)
script.on_event(defines.events.on_research_finished, function(event)
if event.research.name == 'train-manager' then
for _, player in pairs(game.players) do
get_sprite_button(player)
end
end
end)
local function on_player_created(event)
storage.player = storage.player or {}
storage.player[event.player_index] = storage.player[event.player_index] or {}
if game.players[event.player_index].force.technologies["train-manager"].researched == true then
get_sprite_button(game.players[event.player_index])
end
end
local function on_player_joined_game(event)
storage.player = storage.player or {}
storage.player[event.player_index] = storage.player[event.player_index] or {}
-- debugp("Player joined game")
if game.players[event.player_index].force.technologies["train-manager"].researched == true then
get_sprite_button(game.players[event.player_index])
end
end
local function init_globals()
if game then
storage.newcounters = storage.newcounters or {}
storage.newpriority = storage.newpriority or {}
storage.newpublishers = storage.newpublishers or {}
for _, surface in pairs(game.surfaces) do
storage.newcounters[surface.name] = storage.newcounters[surface.name] or {}
storage.newpriority[surface.name] = storage.newpriority[surface.name] or {}
storage.newpublishers[surface.name] = storage.newpublishers[surface.name] or {}
end
end
end
local function convert_version_to_number(old, new)
local old_value, new_value = 0, 0
if old then
local old_major, old_minor, old_patch = string.match(old, "(%d+)%.(%d+)%.(%d+)")
if old_major and old_minor and old_patch then
old_value = (old_major * 1000 * 1000) + (old_minor * 1000) + old_patch
end
end
if new then
local new_major, new_minor, new_patch = string.match(new, "(%d+)%.(%d+)%.(%d+)")
if new_major and new_minor and new_patch then
new_value = (new_major * 1000 * 1000) + (new_minor * 1000) + new_patch
end
end
return old_value, new_value
end
-- Fuel handling
local function on_configuration_changed(modlist)
--storage.removeFuelStop = storage.removeFuelStop or {}
getEnergyList()
init_globals()
if modlist.mod_changes["train-pubsub"] then
local old = modlist.mod_changes["train-pubsub"].old_version
local new = modlist.mod_changes["train-pubsub"].new_version
-- 2.31.1 becomes the number -- 2,031,001
old, new = convert_version_to_number(old, new)
if old < 3007 then
storage.trains = storage.trains or {}
for _, surface in pairs(game.surfaces) do
local trains = game.train_manager.get_trains({ surface = surface.name })
for _, train in pairs(trains) do
storage.trains[train.id] = train
end
end
end
if old < 3013 then
for _, force in pairs(game.forces) do
if (force.technologies["train-manager"].researched) then
force.recipes["train-config"].enabled = true
end
end
end
end
end
local function onLoad()
--storage.removeFuelStop = storage.removeFuelStop or {}
init_globals()
getEnergyList()
nth_tick()
end
function getEnergyList()
storage.EnergyList = storage.EnergyList or {}
for _, item in pairs(prototypes.item) do
if item.fuel_category then
--table.insert(storage.EnergyList,{name=item.name,fuel_value=item.fuel_value})
storage.EnergyList[item.name] = item.fuel_value
end
end
end
function lowFuel(loc)
local loc_inv = loc.get_fuel_inventory()
if not loc_inv then return false end
local contents = loc_inv.get_contents()
local min_fuel = settings.global['min-fuel-amount'].value * loc.prototype.get_max_energy_usage() * 800
min_fuel = min_fuel / loc.prototype.burner_prototype.effectivity
-- log(loc.name .. " max_energy_usage" .. tostring(loc.prototype.max_energy_usage) .. " burner effectivity" .. loc.prototype.burner_prototype.effectivity)
if getEnergy(contents) < min_fuel then
return true
else
return false
end
end
function getEnergy(list)
local e = 0
for idx in pairs(list) do
-- for _,item in pairs(storage.EnergyList) do
-- if item.name == name then
row = list[idx]
e = e + (storage.EnergyList[row.name] * row.count)
-- break
-- end
-- end
end
return e
end
function onInit()
storage.PubSubOpened = storage.PubSubOpened or {}
for _, player in pairs(game.players) do
get_sprite_button(player)
end
getEnergyList()
end
function map_ping(player, x, y, text)
--debugp(x .. " : " .. " : " .. text)
-- player.force.add_chart_tag(player.surface,{icon={type="item",name="train-publisher"},position={x=x+5,y=y},text=text})
-- local gui = player.gui.center
-- if gui.ping_map then gui.ping_map.destroy() end
-- local ping_map = gui.add{type = "frame",name = "ping_map", caption = text}
-- ping_map.add{type = "minimap", name = "ping_loc", position={x=x+5,y=y}}
player.force.print("[gps=" .. tostring(x) .. "," .. tostring(y) .. "," .. tostring(player.surface.name) .. "]")
end
-- script.on_event("remove-ping-map",function(event)
-- local player = game.players[event.player_index]
-- local gui = player.gui.center
-- if gui.ping_map then gui.ping_map.destroy() end
-- end)
local function gui_open_frame(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.tm_button_frame
if frame then
frame.add {
type = "sprite-button",
name = "es_button",
sprite = "sub_train_stop",
style = mod_gui.button_style
}
frame.add {
type = "sprite-button",
name = "publish_button",
sprite = "publisher",
style = mod_gui.button_style
}
frame.add {
type = "sprite-button",
name = "priority_button",
sprite = "priority",
style = mod_gui.button_style
}
frame.add {
type = "sprite-button",
name = "subscribe_button",
sprite = "trains",
style = mod_gui.button_style
}
frame.add {
type = "sprite-button",
name = "match_button",
sprite = "key",
style = mod_gui.button_style
}
frame.add {
type = "sprite-button",
name = "rq_button",
sprite = "x",
style = mod_gui.button_style
}
-- helpers.write_file("ps_setting",serpent.block(player.mod_settings["ps-tooltip"]),{comment=false})
-- debugp(tostring(player.mod_settings["ps-tooltip"]))
if player.mod_settings["ps-tooltip"].value == true then
--debugp("mod setting true")
frame.subscribe_button.tooltip = { "gui-trainps.s-tooltip" }
frame.publish_button.tooltip = { "gui-trainps.r-tooltip" }
frame.priority_button.tooltip = { "gui-trainps.pri-tooltip" }
frame.match_button.tooltip = { "gui-trainps.k-tooltip" }
frame.es_button.tooltip = { "gui-trainps.es-tooltip" }
frame.rq_button.tooltip = { "gui-trainps.rq-tooltip" }
end
end
end
local function gui_open_subtable(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.sub_table
if not frame then return end
frame.clear()
frame.add { type = "label", caption = { "subscriptions-title" }, style = "caption_label" }
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-supplier-height"].value
local subscriptions = scroll.add { type = "table", name = "subscriptions", column_count = 2,
style = "PubSub_table_style" }
storage.subscriptions = storage.subscriptions or {}
for i, subs in pairs(storage.subscriptions) do
local status, err = pcall(function()
subscriptions.add { type = "label", caption = subs }
subscriptions.add { type = "label", caption = i }
-- subscriptions.add{type = "label", caption = subs.train.id}
end)
if not status then
for _, players in pairs(game.players) do
players.print(err)
storage.subscriptions[i] = nil
end
end
end
end
local function gui_open_pubtable(player, search)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.pub_table
if not frame then return end
frame.clear()
local heading = frame.add { type = "table", name = "heading", column_count = 2 }
heading.add { type = "label", caption = { "publish-title" }, style = "caption_label" }
local resource_search
if search == nil then
resource_search = heading.add { type = "choose-elem-button", name = "resource_search", elem_type = "signal" }
else
resource_search = heading.add { type = "choose-elem-button", name = "resource_search", elem_type = "signal", signal = search }
end
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-requester-height"].value
-- scroll.vertical_scroll_policy = "auto-and-reserve-space"
local requests = scroll.add { type = "table", name = "requests", column_count = 6, style = "PubSub_table_style" }
if storage.newpublishers then
if storage.newpublishers[player.surface.name] then
for keyi, publishers in spairs(storage.newpublishers[player.surface.name]) do
for n, req in pairs(publishers) do
-- requests.add{}
local pass = false
if resource_search.elem_value == nil then
pass = true
elseif req.priority.resource ~= nil then
if resource_search.elem_value.name == req.priority.resource.name then
pass = true
end
end
if pass == true then
local ping = requests.add { type = "button", name = "pub_ping" .. keyi .. ":" .. n,
style = "PubSub_edit_button_style", caption = { "requester.p" } }
local edit = requests.add { type = "button", name = "pub_edit" .. keyi .. ":" .. n,
style = "PubSub_edit_button_style", caption = { "requester.e" } }
requests.add { type = "label", caption = req.proc_priority }
if req.priority ~= nil then
if req.priority.resource ~= nil and req.priority.id ~= nil then
if req.priority.resource ~= {} and req.priority.id ~= {} then
local resource = requests.add { type = "choose-elem-button",
name = "Resource" .. keyi .. ":" .. n, elem_type = "signal",
signal = {
type = req.priority.resource.type,
name = req.priority.resource.name
} }
resource.locked = true
if req.priority.id.name == nil then
requests.add { type = "label", caption = " " }
else
local id = requests.add { type = "choose-elem-button",
name = "Id" .. keyi .. ":" .. n, elem_type = "signal",
signal = { type = req.priority.id.type, name = req.priority.id.name } }
id.locked = true
end
else
requests.add { type = "label", caption = { "train-controller.not-defined" } }
requests.add { type = "label", caption = " " }
end
else
requests.add { type = "label", caption = { "train-controller.not-defined" } }
requests.add { type = "label", caption = " " }
end
else
requests.add { type = "label", caption = { "train-controller.not-defined" } }
requests.add { type = "label", caption = " " }
end
requests.add { type = "label", caption = keyi }
-- requests.add{type = "label", caption = subs.train.id}
if player.mod_settings["ps-tooltip"].value == true then
ping.tooltip = { "gui-trainps.ping-tooltip" }
edit.tooltip = { "gui-trainps.pedit-tooltip" }
end
end
end
end
end
end
end
local function gui_open_estable(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.es_table
if not frame then return end
frame.clear()
frame.add { type = "label", caption = { "es-title" }, style = "caption_label" }
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-supplierstations-height"].value
local es = scroll.add { type = "table", name = "es", column_count = 1, style = "PubSub_table_style" }
-- for _,rows in pairs(storage.entitystation) do
-- es.add{type = "label", caption = rows.name}
-- es.add{type = "label", caption = rows.backer_name}
-- end
for _, station in pairs(player.surface.find_entities_filtered { type = "train-stop", name = "subscriber-train-stop" }) do
-- es.add{type = "label", caption = station.name}
es.add { type = "label", caption = station.backer_name }
end
end
function gui_open_rqtable(player, search)
local gui = mod_gui.get_frame_flow(player)
if storage.player[player.index].unhide == nil then storage.player[player.index].unhide = false end
local frame = gui.rq_table
if not frame then return end
frame.clear()
local rqheading = frame.add { type = "table", name = "rqheading", column_count = 3, style = "PubSub_table_style" }
rqheading.add { type = "label", caption = { "rq-title" }, style = "caption_label" }
rqheading.add { type = "checkbox", name = "rqunhide", caption = { "requester.unhide" },
state = storage.player[player.index].unhide }
local resource_search
if search == nil then
resource_search = rqheading.add { type = "choose-elem-button", name = "resource_search3", elem_type = "signal" }
else
resource_search = rqheading.add { type = "choose-elem-button", name = "resource_search3", elem_type = "signal",
signal = search }
end
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-outstandingrequester-height"].value
local rq = scroll.add { type = "table", name = "rq", column_count = 7, style = "PubSub_table_style" }
if storage.newrequests then
if storage.newrequests[player.surface.name] then
local threshold = player.mod_settings["outstanding-threshold"].value
for keyn, requests in pairs(storage.newrequests[player.surface.name]) do
for n, rows in pairs(requests) do
if resource_search.elem_value == nil or
resource_search.elem_value.name == rows.priority.resource.name then
if rows.hide ~= true or storage.player[player.index].unhide == true then
if rows.priority.resource == nil then
elseif rows.priority.resource == {} then
elseif rows.priority.resource.type == nil then
elseif rows.priority.id == nil then
elseif rows.priority.id == {} then
elseif rows.priority.id.type == nil then
else
-- point of interest - for time threshold implementation
-- game.print(rows.tick .. " : " .. n)
local minutes = math.floor((game.tick - rows.tick) / 3600)
if minutes >= threshold then
local ping = rq.add { type = "button", name = "rq_ping" .. keyn .. ":" .. n,
style = "PubSub_edit_button_style", caption = "p" }
rq.add { type = "label", caption = rows.proc_priority }
rq.add { type = "choose-elem-button", name = "Res_Resource" .. keyn .. ":" .. n,
elem_type = "signal",
signal = {
type = rows.priority.resource.type,
name = rows.priority.resource.name
} }
rq.add { type = "choose-elem-button", name = "Resource" .. keyn .. ":" .. n,
elem_type = "signal",
signal = { type = rows.priority.id.type, name = rows.priority.id.name } }
rq.add { type = "label", caption = tostring(rows.backer_name) }
-- local minutes = math.floor((game.tick - rows.tick) / 3600)
if minutes < 0 then minutes = 0 end
rq.add { type = "label", caption = { "mins" } }
rq.add { type = "label", caption = tostring(minutes) }
if player.mod_settings["ps-tooltip"].value == true then
ping.tooltip = { "gui-trainps.ping-tooltip" }
end
end
end
end
end
end
end
end
if storage.direct_out then
if storage.direct_out[player.surface.name] then
if table_size(storage.direct_out[player.surface.name]) > 0 then
for i, directs in pairs(storage.direct_out[player.surface.name]) do
for j, direct in pairs(directs) do
if resource_search.elem_value == nil or
resource_search.elem_value.name == direct.signal.signal.name then
local ping = rq.add { type = "button", name = "dc_ping" .. i .. ":" .. j,
style = "PubSub_edit_button_style", caption = "p" }
rq.add { type = "label", caption = "dc" }
rq.add { type = "choose-elem-button", name = "dcRes_Resource" .. i .. ":" .. j,
elem_type = "signal",
signal = { type = direct.signal.signal.type, name = direct.signal.signal.name } }
rq.add { type = "choose-elem-button", name = "dcResource" .. i .. ":" .. j,
elem_type = "signal",
signal = { type = direct.signal.signal.type, name = direct.signal.signal.name } }
rq.add { type = "label", caption = tostring(direct.entity.backer_name) }
rq.add { type = "label", caption = { "mins" } }
rq.add { type = "label", caption = " dc" }
if player.mod_settings["ps-tooltip"].value == true then
ping.tooltip = { "gui-trainps.ping-tooltip" }
end
end
end
end
end
end
end
end
end
function auto_update()
for _, player in pairs(game.players) do
local gui = mod_gui.get_frame_flow(player)
local frame = gui.rq_table
if frame then
frame.clear()
gui_open_rqtable(player, storage.player[player.index].search3)
end
end
end
local function gui_open_key_frame(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.key_frame
if frame then
frame.add { type = "label", caption = { "key-title" }, style = "caption_label" }
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-keytrain-height"].value
local key_s = scroll.add { type = "table", name = "key_s", column_count = 2, style = "PubSub_table_style" }
storage.sub_index = storage.sub_index or {}
storage.subscriptions = storage.subscriptions or {}
for k, subs in pairs(storage.sub_index) do
key_s.add { type = "label", caption = k }
key_s.add { type = "label", caption = subs }
end
end
end
local function stationlist(priority)
local temp = ""
-- temp = table.concat(priority.station)
if priority ~= nil then
if priority.station then
for _, station in ipairs(priority.station) do
if temp == "" then
temp = station[2]
else
temp = temp .. "\n" .. station[2]
end
-- temp = temp .. station[2] .. " : "
end
end
end
return temp
end
function gui_open_station_frame(player, mode)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.station_frame
local surface = player.surface.name
if not frame then return end
frame.clear()
local wc = {}
local new = ""
if mode == "add" then
local resource = storage.player[player.index].resource.name
local id = storage.player[player.index].id.name
wc = {
rb_or = true,
rb_and = false,
inc_ef = true,
empty = true,
full = false,
inactivity = true,
inact_int = 5,
wait_timer = false,
wait_int = 30,
count = false,
count_amt = 1000,
count_ddn = 1
}
new = "new_"
elseif mode == "add+" then
new = "new_"
wc = storage.player[player.index].wc
else
local resource = storage.player[player.index].resource
local id = storage.player[player.index].id
if storage.newpriority[surface][resource][id].wc == nil then
storage.newpriority[surface][resource][id].wc = {
rb_or = true,
rb_and = false,
inc_ef = true,
empty = true,
full = false,
inactivity = true,
inact_int = 5,
wait_timer = false,
wait_int = 30,
count = false,
count_amt = 1000,
count_ddn = 1
}
end
wc = storage.newpriority[surface][resource][id].wc
if wc.count == nil then
wc.count = false
wc.count_amt = 1000
wc.count_ddn = 1
storage.newpriority[surface][resource][id].wc = wc
end
end
frame.add { type = "label", caption = { "wc.wait-conditions" }, style = "caption_label" }
local andor_table = frame.add { type = "table", name = "andortable", column_count = 2, style = "PubSub_table_style" }
andor_table.add { type = "radiobutton", name = new .. "rb_or", caption = { "wc.or" }, state = wc.rb_or }
andor_table.add { type = "radiobutton", name = new .. "rb_and", caption = { "wc.and" }, state = wc.rb_and }
local ef_table = frame.add { type = "table", name = "eftable", column_count = 3, style = "PubSub_table_style" }
ef_table.add { type = "checkbox", name = new .. "efinc", caption = { "wc.include" }, state = wc.inc_ef }
ef_table.add { type = "radiobutton", name = new .. "empty", caption = { "wc.empty" }, state = wc.empty }
ef_table.add { type = "radiobutton", name = new .. "full", caption = { "wc.full" }, state = wc.full }
local inact_table = frame.add { type = "table", name = "inact_table", column_count = 2 }
inact_table.add { type = "checkbox", name = new .. "inactivity", caption = { "wc.inactivity" }, state = wc
.inactivity }
local inact_int = inact_table.add { type = "textfield", name = new .. "inact_int", caption = { "wc.period" },
text = wc.inact_int }
inact_int.style.maximal_width = 40
frame.add { type = "slider", name = new .. "inact_slider", minimum_value = 1, maximum_value = 200,
value = wc.inact_int }
local rb_table = frame.add { type = "table", name = "rbtable", column_count = 2 }
rb_table.add { type = "checkbox", name = new .. "wait_timer", caption = { "wc.wait-timer" }, state = wc.wait_timer }
local wait_int = rb_table.add { type = "textfield", name = new .. "wait_int", caption = { "wc.wait" },
text = wc.wait_int, numeric = true, allow_decimal = false, allow_negative = false }
wait_int.style.maximal_width = 40
frame.add { type = "slider", name = new .. "wait_slider", minimum_value = 1, maximum_value = 200, value = wc
.wait_int }
local count_table = frame.add { type = "table", name = "count_table", column_count = 3 }
count_table.add { type = "checkbox", name = new .. "count", caption = { "wc.count_lbl" }, state = wc.count }
local count_ddn = count_table.add { type = "drop-down", name = new .. "count_ddn",
items = { "<", ">", "=", "≥", "≤", "≠" }, selected_index = wc.count_ddn }
count_ddn.style.maximal_width = 40
local count_amt = count_table.add { type = "textfield", name = new .. "count_amt", text = wc.count_amt,
numeric = true, allow_decimal = false, allow_negative = false }
count_amt.style.maximal_width = 64
-- end
frame.add { type = "label", caption = { "train-controller.select-stations" }, style = "caption_label" }
for _, stationX in ipairs(storage.player[player.index].station) do
frame.add { type = "label", caption = stationX[2] }
end
local resource_search2 = gui.pri_frame.heading.resource_search2.elem_value
local dd_list = storage.player[player.index].list
if resource_search2 ~= nil then
local el_type = resource_search2.type
local el_name = resource_search2.name
dd_list = {}
for k, v in ipairs(storage.player[player.index].list) do
if string.find(v, el_type, nil, true) then
if string.find(v, el_name, nil, true) then
table.insert(dd_list, v)
end
end
end
end
frame.add { type = "drop-down", name = "station_dd", items = dd_list }
if mode == "add" or mode == "add+" then
storage.player[player.index].bname = "tsm_save_station_button"
storage.player[player.index].bcaption = { "train-controller.save" }
storage.player[player.index].wc = wc
storage.player[player.index].mode = "add+"
elseif mode == "edit" then
storage.player[player.index].bname = "tsm_update_station_button"
storage.player[player.index].bcaption = { "train-controller.update" }
storage.player[player.index].mode = "edit"
end
frame.add {
type = "button",
name = storage.player[player.index].bname,
style = mod_gui.button_style,
caption = storage.player[player.index].bcaption
}
end
local function gui_open_pri_frame(player, search)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.pri_frame
if not frame then return end
frame.clear()
local heading = frame.add { type = "table", name = "heading", column_count = 2 }
heading.add { type = "label", caption = { "priorities-title" }, style = "caption_label" }
local resource_search
if search == nil then
resource_search = heading.add { type = "choose-elem-button", name = "resource_search2", elem_type = "signal" }
else
resource_search = heading.add { type = "choose-elem-button", name = "resource_search2", elem_type = "signal",
signal = search }
end
local scroll = frame.add { type = "scroll-pane", name = "scroll" }
scroll.style.maximal_height = player.mod_settings["max-priority-height"].value
scroll.style.maximal_width = player.mod_settings["max-priority-width"].value
local ptable = scroll.add { type = "table", name = "ptable", column_count = 6, style = "PubSub_table_style" }
ptable.add { type = "label", caption = " " }
ptable.add { type = "label", caption = " " }
ptable.add { type = "label", caption = " " }
ptable.add { type = "label", caption = { "train-controller.resource" } }
ptable.add { type = "label", caption = { "train-controller.id" } }
ptable.add { type = "label", caption = { "train-controller.station-list" } }
storage.newpriority = storage.newpriority or {}
storage.newpriority[player.surface.name] = storage.newpriority[player.surface.name] or {}
storage.newpriority[player.surface.name] = storage.newpriority[player.surface.name] or {}
-- helpers.write_file("priorities",serpent.block(storage.newpriority[player.surface.name]),{comment=false})
for keyi, priority_res in pairs(storage.newpriority[player.surface.name]) do
for n, priority in pairs(priority_res) do
if resource_search.elem_value == nil or resource_search.elem_value.name == priority.resource.name then
local premove = ptable.add { type = "button", name = "ps_remove" .. keyi .. ":" .. n,
style = "PubSub_edit_button_style", caption = "-" }
local delete = ptable.add { type = "button", name = "ps_delete" .. keyi .. ":" .. n,
style = "PubSub_edit_button_style", caption = "x" }
local edit = ptable.add { type = "button", name = "ps_edit" .. keyi .. ":" .. n,
style = "PubSub_edit_button_style", caption = "e" }
if priority.resource ~= nil then
local resource = ptable.add { type = "choose-elem-button", name = "Resource" .. keyi .. ":" .. n,
elem_type = "signal", signal = { type = priority.resource.type, name = priority.resource.name } }
resource.locked = true
local id = ptable.add { type = "choose-elem-button", name = "Id" .. keyi .. ":" .. n,
elem_type = "signal", signal = { type = priority.id.type, name = priority.id.name } }
id.locked = true
ptable.add { type = "label", name = "Stations" .. keyi .. ":" .. n, caption = stationlist(priority) }
if player.mod_settings["ps-tooltip"].value == true then
premove.tooltip = { "gui-trainps.premove-tooltip" }
delete.tooltip = { "gui-trainps.delete-tooltip" }
edit.tooltip = { "gui-trainps.edit-tooltip" }
end
end
end
end
end
ptable.add { type = "label", caption = " " }
ptable.add { type = "label", caption = " " }
ptable.add { type = "label", caption = " " }
ptable.add { type = "choose-elem-button", name = "Resource", elem_type = "signal" }
ptable.add { type = "choose-elem-button", name = "Id", elem_type = "signal" }
ptable.add {
type = "button",
name = "select_station_button",
style = mod_gui.button_style,
caption = { "train-controller.select" }
}
end
function on_gui_elem_changed(event)
local mod = event.element.get_mod()
if mod == nil then return end
if mod ~= "train-pubsub" then return end
local element = event.element
local player = game.players[event.player_index]
storage.player[event.player_index] = storage.player[event.player_index] or {}
storage.player[event.player_index].resource = storage.player[event.player_index].resource or {}
storage.player[event.player_index].id = storage.player[event.player_index].id or {}
if storage.newrequests == nil then
storage.newrequests = {}
storage.newrequests[player.surface.name] = {}
end
if element.name == "Resource" then
-- resource = table.deepcopy(element)
if element.elem_value == nil then
storage.player[event.player_index].resource = {}
else
storage.player[event.player_index].resource = {}
storage.player[event.player_index].resource.elem_type = element.elem_type
storage.player[event.player_index].resource.type = element.elem_value.type
storage.player[event.player_index].resource.name = element.elem_value.name
end
elseif element.name == "resource_search" then
gui_open_pubtable(player, element.elem_value)
elseif element.name == "resource_search2" then
gui_open_pri_frame(player, element.elem_value)
elseif element.name == "resource_search3" then
storage.player[event.player_index].search3 = element.elem_value
gui_open_rqtable(player, element.elem_value)
elseif element.name == "Id" then
-- id = table.deepcopy(element)
if element.elem_value == nil then
storage.player[event.player_index].id = {}
else
storage.player[event.player_index].id = {}
storage.player[event.player_index].id.elem_type = element.elem_type
storage.player[event.player_index].id.type = element.elem_value.type
storage.player[event.player_index].id.name = element.elem_value.name
end
-- debugp(event.player_index)
elseif element.name == "Res_Schema" then
local psn = player.surface.name
local pidx = player.index
if element.elem_value == nil then
if storage.newrequests[psn] ~= nil then
if storage.newrequests[psn][storage.cur_publisher[pidx].backer_name] ~= nil then
if storage.newrequests[psn][storage.cur_publisher[pidx].backer_name][storage.cur_publisher[pidx].key] ~= nil then
storage.newrequests[psn][storage.cur_publisher[pidx].backer_name][storage.cur_publisher[pidx].key] = nil
end
end
end
storage.newpublishers[psn][storage.cur_publisher[pidx].backer_name][storage.cur_publisher[pidx].key].priority.resource = {}
storage.newpublishers[psn][storage.cur_publisher[pidx].backer_name][storage.cur_publisher[pidx].key].request = false
else
-- todo something broke here
local cur_pub = storage.cur_publisher[pidx]
if cur_pub then
local cur_pub_name = storage.cur_publisher[pidx].backer_name
local cur_pub_key = storage.cur_publisher[pidx].key
if cur_pub_name and cur_pub_key then
storage.newpublishers[psn][cur_pub_name][cur_pub_key].priority.resource = {
elem_type = element.elem_type,
type = element.elem_value.type,
name = element.elem_value.name
}
end
end
end
gui_open(player,
storage.newpublishers[psn][storage.cur_publisher[pidx].backer_name][storage.cur_publisher[pidx].key].entity)
elseif element.name == "Schema" then
if element.elem_value == nil then
if storage.newrequests[player.surface.name] ~= nil then
if storage.newrequests[player.surface.name][storage.cur_publisher[player.index].backer_name] ~= nil then
if storage.newrequests[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key] ~= nil then
storage.newrequests[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key] = nil
end
end
end
storage.newpublishers[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key].priority.id = {}
storage.newpublishers[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key].request = false
else
storage.newpublishers[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key].priority.id = {
elem_type = element.elem_type,
type = element.elem_value.type,
name = element.elem_value.name
}
end
gui_open(player,
storage.newpublishers[player.surface.name][storage.cur_publisher[player.index].backer_name][
storage.cur_publisher[player.index].key].entity)
end
-- end)
end
local function on_gui_selection_state_changed(event)
local mod = event.element.get_mod()
if mod == nil then return end
if mod ~= "train-pubsub" then return end
local element = event.element
local player = game.players[event.player_index]
-- local status,err = pcall(function()
-- debugp(element.name)
if element.name == "station_dd" then
local item = { "", element.items[element.selected_index] }
table.insert(storage.player[event.player_index].station, item)
-- helpers.write_file("apend_stations",serpent.block(station),{comment=false})
gui_open_station_frame(player, storage.player[player.index].mode)
return
end
if element.name == "count_ddn" then
storage.newpriority[player.surface.name][storage.player[event.player_index].resource][
storage.player[event.player_index].id].wc.count_ddn = element.selected_index
return
elseif element.name == "new_count_ddn" then
storage.player[player.index].wc.count_ddn = element.selected_index
end
-- end)
end
local function copy_icon(event)
local player = game.players[event.player_index]
helpers.write_file("cur_publisher", serpent.block(storage.cur_publisher))
storage.player[event.player_index].id = {}
storage.player[event.player_index].id.elem_type = storage.player[event.player_index].resource.elem_type
storage.player[event.player_index].id.name = storage.player[event.player_index].resource.name
local surface = storage.cur_publisher[player.index].surface
local backer = storage.cur_publisher[player.index].backer_name
local key = storage.cur_publisher[player.index].key
storage.newpublishers[surface][backer][key].priority.id = storage.newpublishers[surface][backer][key].priority.id or
{}
storage.newpublishers[surface][backer][key].priority.id = storage.newpublishers[surface][backer][key].priority
.resource
gui_open(player, storage.newpublishers[surface][backer][key].entity)
end
local function resource_id(s, r)
local row = string.sub(s, r + 1)
local divider = string.find(row, ":")
local resource = string.sub(row, 1, divider - 1)
local id = string.sub(row, divider + 1)
return resource, id
end
local function on_gui_click(event)
local mod = event.element.get_mod()
if mod == nil then return end
if mod ~= "train-pubsub" then return end
local element = event.element
local player = game.players[event.player_index]
local gui = mod_gui.get_frame_flow(player)
local frame = gui.tm_button_frame
-- local curlist = nil
storage.player = storage.player or {}
storage.player[event.player_index] = storage.player[event.player_index] or {}
if element.name == "tm_sprite_button" then
if frame then
if gui.sub_table then gui.sub_table.destroy() end
if gui.pub_table then gui.pub_table.destroy() end
if gui.es_table then gui.es_table.destroy() end
if gui.rq_table then gui.rq_table.destroy() end
if gui.pri_frame then gui.pri_frame.destroy() end
if gui.station_frame then gui.station_frame.destroy() end
if gui.key_frame then gui.key_frame.destroy() end
frame.destroy()
gui_close_any(player)
return
end
gui.add {
type = "frame",
name = "tm_button_frame",
direction = "horizontal",
style = mod_gui.frame_style
}
gui_open_frame(player)
elseif element.name == "subscribe_button" then
local subtable = gui.sub_table
if subtable then
subtable.destroy()
return
end
subtable = gui.add {
type = "frame",
name = "sub_table",
direction = "vertical",
style = mod_gui.frame_style
}
gui_open_subtable(player)
elseif element.name == "publish_button" then
local pubtable = gui.pub_table
if pubtable then
pubtable.destroy()
return
end
gui.add {
type = "frame",
name = "pub_table",
direction = "vertical",
style = mod_gui.frame_style
}
gui_open_pubtable(player)
elseif element.name == "es_button" then
local estable = gui.es_table
if estable then
estable.destroy()
return
end
estable = gui.add {
type = "frame",
name = "es_table",
direction = "vertical",
style = mod_gui.frame_style
}
gui_open_estable(player)
elseif element.name == "rq_button" then
local rqtable = gui.rq_table
if rqtable then
rqtable.destroy()
return
end
gui.add {
type = "frame",
name = "rq_table",
direction = "vertical",
style = mod_gui.frame_style
}
gui_open_rqtable(player, storage.player[player.index].search3)
elseif element.name == "priority_button" then
local pri_frame = gui.pri_frame
if pri_frame then
pri_frame.destroy()
if gui.station_frame then
gui.station_frame.destroy()
end
return
end
gui.add {
type = "frame",
name = "pri_frame",
direction = "vertical",
style = mod_gui.frame_style
}