-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
221 lines (206 loc) · 7.14 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
local util = require("util")
local function show_gui(player)
local frame = player.gui.relative.add({
type = "frame",
name = "pp-main-frame",
direction = "vertical",
anchor = {
gui = defines.relative_gui_type.production_gui,
position = defines.relative_gui_position.right
}
})
build_titlebar(frame)
frame.style.minimal_width = 352
local inner_frame = frame.add({
type = "frame",
style = "inside_deep_frame"
})
local scroll_pane = inner_frame.add({
type = "scroll-pane"
})
scroll_pane.style.padding = { 4, 4, 4, 4 }
scroll_pane.style.vertically_stretchable = true
local sorted_potential = get_production_sorted_by_potential(player.force)
local items = {}
for _, item in ipairs(sorted_potential) do
local flow = scroll_pane.add({
type = "flow",
direction = "horizontal"
})
flow.style.vertical_align = "center"
flow.add({
type = "sprite",
sprite = (item.type == "item" and "item/" or "fluid/") .. item.name,
tooltip = (item.type == "item" and game.item_prototypes or game.fluid_prototypes)[item.name].localised_name
})
local bar = flow.add({
type = "progressbar",
style = "statistics_progressbar",
value = item.potential
})
bar.style.horizontally_stretchable = true
local label = flow.add({
type = "label",
style = "electric_usage_label",
caption = util.format_number(item.actual_per_minute, true) .. "/m"
})
label.style.horizontal_align = "right"
table.insert(items, { name = item.name, gui = flow })
end
global[player.index] = global[player.index] or {}
global[player.index].items = items
end
function build_titlebar(frame)
local flow = frame.add({
name = "titlebar",
type = "flow",
direction = "horizontal",
})
flow.add({
type = "label",
style = "frame_title",
caption = "Potential",
ignored_by_interaction = true
})
flow.style.horizontal_spacing = 8
flow.style.bottom_padding = 4
local filler = flow.add({
type = "empty-widget",
style = "draggable_space_header",
ignored_by_interaction = true
})
filler.style.right_margin = 4
filler.style.height = 24
filler.style.horizontally_stretchable = true
flow.add({
type = "sprite-button",
style = "frame_action_button",
sprite = "utility/search_white",
hovered_sprite = "utility/search_black",
tags = { action = "pp-toggle-search" }
})
end
function get_production_sorted_by_potential(force)
local potential_production = get_potential_production(force)
local sorted_potential = {}
for _, item in pairs(potential_production) do
local actual = get_actual_production(force, item.name, item.type)
local actual_per_minute = actual / 10
table.insert(sorted_potential, {
name = item.name,
type = item.type,
actual_per_minute = actual_per_minute,
potential = actual_per_minute / (item.potential_per_second * 60)
})
end
table.sort(sorted_potential, function(item1, item2)
return item1.potential > item2.potential
end)
return sorted_potential
end
function get_potential_production(force)
local potential_production = {}
local function add_products(products, productivity_bonus, seconds)
for _, output in pairs(products) do
local product_amount = util.product_amount(output)
local amount_per_second = (product_amount - (output.catalyst_amount or 0)) * (1 + productivity_bonus) / seconds
if amount_per_second > 0 then
if not potential_production[output.name] then
potential_production[output.name] = {
name = output.name,
type = output.type,
potential_per_second = 0
}
end
potential_production[output.name].potential_per_second = amount_per_second + potential_production[output.name].potential_per_second
end
end
end
for _, surface in pairs(game.surfaces) do
local entities = surface.find_entities_filtered({type = {"furnace", "assembling-machine", "rocket-silo"}, force = force})
for _, machine in pairs(entities) do
local recipe = machine.get_recipe()
if recipe and not recipe.hidden_from_flow_stats then
local recipe_time = recipe.energy
local actual_time = recipe_time / machine.crafting_speed
local productivity_bonus = machine.productivity_bonus
add_products(recipe.products, productivity_bonus, actual_time)
end
end
local mining_entities = surface.find_entities_filtered({type = {"mining-drill"}, force = force})
for _, miner in pairs(mining_entities) do
if miner.mining_target then
local mineable_properties = miner.mining_target.prototype.mineable_properties
local recipe_time = mineable_properties.mining_time
local crafting_speed = miner.prototype.mining_speed * (1 + miner.speed_bonus)
local actual_time = recipe_time / crafting_speed
local productivity_bonus = miner.productivity_bonus
add_products(mineable_properties.products, productivity_bonus, actual_time)
end
end
end
return potential_production
end
function get_actual_production(force, name, type)
local statistics
if type == "item" then
statistics = force.item_production_statistics
elseif type == "fluid" then
statistics = force.fluid_production_statistics
else
return 0
end
return statistics.get_flow_count({
name = name,
input = true, -- input == true is production
precision_index = defines.flow_precision_index.ten_minutes,
count = true
})
end
script.on_event({defines.events.on_gui_click, defines.events.on_gui_text_changed}, function(event)
local player = game.get_player(event.player_index)
local frame = player.gui.relative["pp-main-frame"]
if not frame or not frame.valid then return end
local action = event.element.tags.action
if action == "pp-toggle-search" then
if frame["titlebar"]["search-textfield"] then
frame["titlebar"]["search-textfield"].destroy()
apply_filter(event.player_index, "")
else
local search_field = frame["titlebar"].add({
name = "search-textfield",
type = "textfield",
style = "titlebar_search_textfield",
tags = { action = "pp-set-search-term" },
index = 3
})
search_field.focus()
end
elseif action == "pp-set-search-term" then
local filter = event.element.text
apply_filter(event.player_index, filter)
end
end)
function apply_filter(player_index, filter)
for _, item in ipairs(global[player_index].items) do
item.gui.visible = string.find(item.name, filter:lower()) and true or false
end
end
script.on_event(defines.events.on_gui_opened, function(event)
if event.gui_type == defines.gui_type.production then
local player = game.get_player(event.player_index)
local frame = player.gui.relative["pp-main-frame"]
if not frame or not frame.valid then
show_gui(player)
end
end
end)
script.on_event(defines.events.on_gui_closed, function(event)
if event.gui_type == defines.gui_type.production then
local player = game.get_player(event.player_index)
local frame = player.gui.relative["pp-main-frame"]
if frame and frame.valid then
frame.destroy()
end
end
end)