-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
157 lines (128 loc) · 4.46 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
local flib_dictionary = require "__flib__/dictionary"
local flib_gui = require "__flib__/gui"
local flib_table = require "__flib__/table"
local fs_util = require "fs_util"
local save = require "manage/save"
local pre_solve = require "manage/pre_solve"
local virtual = require "manage/virtual"
local common = require "ui/common"
local main_window = require "ui/main_window"
script.on_init(function()
flib_dictionary.on_init()
storage.virtuals = virtual.create_virtuals()
storage.players = {}
for _, player in pairs(game.players) do
save.init_player_data(player.index)
if __DebugAdapter then
player.cheat_mode = true
end
end
storage.forces = {}
for _, force in pairs(game.forces) do
save.init_force_data(force.index)
if __DebugAdapter then
for _, quality in pairs(prototypes.quality) do
force.unlock_quality(quality)
end
end
end
if __DebugAdapter then
if remote.interfaces["freeplay"] then
remote.call("freeplay", "set_skip_intro", true)
remote.call("freeplay", "set_disable_crashsite", true)
end
end
end)
script.on_load(function()
for _, force in pairs(storage.forces) do
save.resetup_force_data_metatable(force)
end
end)
script.on_configuration_changed(function(event)
flib_dictionary.on_configuration_changed()
storage.virtuals = virtual.create_virtuals()
for _, player in pairs(game.players) do
save.reinit_player_data(player.index)
-- TODO reset gui
end
for _, force in pairs(game.forces) do
save.reinit_force_data(force.index)
end
end)
script.on_event(defines.events.on_player_created, function(event)
save.init_player_data(event.player_index)
if __DebugAdapter then
game.players[event.player_index].cheat_mode = true
end
end)
script.on_event(defines.events.on_player_changed_force, function(event)
save.init_force_data(event.force.index)
if __DebugAdapter then
for _, quality in pairs(prototypes.quality) do
event.force.unlock_quality(quality)
end
end
end)
script.on_event(defines.events.on_player_removed, function(event)
storage.players[event.player_index] = nil
end)
script.on_event(defines.events.on_force_created, function(event)
save.init_force_data(event.force.index)
end)
script.on_event(defines.events.on_force_reset, function(event)
save.reinit_force_data(event.force.index)
end)
script.on_event(defines.events.on_forces_merged, function(event)
local destination_force_data = storage.forces[event.destination.index]
local source_force_data = storage.forces[event.source_index]
destination_force_data.solutions = flib_table.array_merge {
destination_force_data.solutions,
source_force_data.solutions
}
storage.forces[event.source_index] = nil
end)
script.on_event(defines.events.on_research_finished, function(event)
save.reinit_force_data(event.research.force.index)
end)
script.on_event(defines.events.on_research_reversed, function(event)
save.reinit_force_data(event.research.force.index)
end)
script.on_event(defines.events.on_tick, function(event)
flib_dictionary.on_tick()
local solution = pre_solve.find_the_need_for_solve()
if solution then
pre_solve.forwerd_solve(solution)
for _, player in pairs(game.players) do
local window = player.gui.screen["factory_solver_main_window"]
if window then
fs_util.dispatch_to_subtree(window, "on_calculation_changed")
end
end
end
end)
---@param player_index integer
local function toggle_main_window(player_index)
local player = game.players[player_index]
local window = player.gui.screen["factory_solver_main_window"]
if window == nil then
common.open_gui(player_index, false, main_window)
else
common.on_close_self {
element = window,
name = "on_close_toggle",
player_index = player_index,
tick = game.tick,
mod_name = window.get_mod()
}
end
end
script.on_event("factory-solver-toggle-main-window", function(event)
toggle_main_window(event.player_index)
end)
script.on_event(defines.events.on_lua_shortcut, function(event)
if event.prototype_name == "factory-solver-toggle-main-window" then
toggle_main_window(event.player_index)
end
end)
flib_dictionary.handle_events()
flib_gui.handle_events()