-
Notifications
You must be signed in to change notification settings - Fork 5
/
filter_updaters.lua
298 lines (290 loc) · 10.4 KB
/
filter_updaters.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
local fh_util = require("fh_util")
local function get_storage_inventory(entity)
local inventory = entity.get_output_inventory()
if inventory then
return inventory
end
for _, inventory_type in pairs { "chest", "car_trunk", "cargo_wagon", "spider_trunk" } do
inventory = entity.get_inventory(defines.inventory[inventory_type])
if inventory then
return inventory
end
end
end
local filtered_inventory_updater = {
condition = function(entity)
local inventory = get_storage_inventory(entity)
return inventory and inventory.supports_filters()
end,
button_description = { "fh.tooltip-container-filters" },
get_active_items = function(entity)
local inventory = get_storage_inventory(entity)
local active_items = {}
for i = 1, #inventory do
fh_util.add_item_to_table(active_items, inventory.get_filter(i))
end
return active_items
end,
add = function(entity, clicked_item, modifiers)
local inventory = get_storage_inventory(entity)
if modifiers.shift and not modifiers.control then
for i = 1, #inventory do
if not inventory.get_filter(i) and inventory[i].valid_for_read and fh_util.is_same_item({ name = inventory[i].name, quality = inventory[i].quality.name }, clicked_item) then
inventory.set_filter(i, clicked_item)
end
end
return
end
if modifiers.control and not modifiers.shift then
for i = 1, #inventory do
if not inventory.get_filter(i) and (not inventory[i].valid_for_read or fh_util.is_same_item(inventory[i], clicked_item)) then
inventory.set_filter(i, clicked_item)
end
end
return
end
if modifiers.control and modifiers.shift then
for i = 1, #inventory do
inventory.set_filter(i, clicked_item)
end
return
end
local found_index
for i = 1, #inventory do
if not inventory.get_filter(i) then
if inventory[i].valid_for_read then
if fh_util.is_same_item(inventory[i], clicked_item) then
found_index = i
break
end
elseif not found_index then
found_index = i
end
end
end
if found_index then
inventory.set_filter(found_index, clicked_item)
return
end
return { "fh.filters-full" }
end,
remove = function(entity, clicked_item, modifiers)
local inventory = get_storage_inventory(entity)
local found_index
if modifiers.shift or modifiers.control then
for i = 1, #inventory do
if fh_util.is_same_item(inventory.get_filter(i), clicked_item) then
inventory.set_filter(i, nil)
end
end
return
end
for i = #inventory, 1, -1 do
if fh_util.is_same_item(inventory.get_filter(i), clicked_item) then
if not inventory[i].valid_for_read then
found_index = i
break
elseif not found_index then
found_index = i
end
end
end
if found_index then
inventory.set_filter(found_index, nil)
return
end
return { "fh.filters-empty" }
end,
}
local function find_section(entity)
for _, section in pairs(entity.get_requester_point().sections) do
if section.is_manual then
return section
end
end
end
local logistic_chest_updater = {
condition = function(entity)
return entity.type == "logistic-container" and (entity.prototype.logistic_mode == "buffer" or entity.prototype.logistic_mode == "requester")
end,
button_description = { "fh.tooltip-requests" },
get_active_items = function(entity)
local active_items = {}
local found_section = find_section(entity)
if not found_section then
return active_items
end
for _, filter in pairs(found_section.filters) do
local value = filter.value
if value and (value.type == "item" or not value.type) then
fh_util.add_item_to_table(active_items, value)
end
end
return active_items
end,
add = function(entity, clicked_item, modifiers)
local found_section = find_section(entity)
if not found_section then
return
end
local found_filter
local new_filters = found_section.filters
for _, filter in pairs(new_filters) do
local value = filter.value
if value then
if fh_util.is_same_item(value, clicked_item) then
found_filter = filter
break
end
elseif not found_filter then
found_filter = filter
end
end
local stack_size = prototypes.item[clicked_item.name].stack_size
local found_count = (found_filter and found_filter.min) or 0
local amount_to_set = found_count + stack_size
if modifiers.shift then
amount_to_set = found_count + 5 * stack_size
end
if modifiers.control then
amount_to_set = stack_size * #entity.get_output_inventory()
end
if found_filter then
found_filter.min = amount_to_set
found_filter.value = { name = clicked_item.name, quality = clicked_item.quality }
else
table.insert(new_filters, {
value = { name = clicked_item.name, quality = clicked_item.quality },
min = amount_to_set,
})
end
found_section.filters = new_filters
return
end,
remove = function(entity, clicked_item, modifiers)
local found_section = find_section(entity)
if not found_section then
return
end
local stack_size = prototypes.item[clicked_item.name].stack_size
local amount_to_remove = stack_size
if modifiers.shift then
amount_to_remove = 5 * stack_size
end
local new_filters = found_section.filters
for _, filter in pairs(new_filters) do
local value = filter.value
if value and fh_util.is_same_item(value, clicked_item) then
local new_count = filter.min - amount_to_remove
if new_count > 0 and not modifiers.control then
filter.min = new_count
else
for v in pairs(filter) do
filter[v] = nil
end
end
found_section.filters = new_filters
return
end
end
return { "fh.requests-empty" }
end,
}
local one_filter_updater = {
condition = function(entity)
return entity.filter_slot_count == 1 and entity.type ~= "infinity-container"
end,
button_description = { "fh.tooltip-filters" },
get_active_items = function(entity)
local active_items = {}
local filter = entity.get_filter(1)
fh_util.add_item_to_table(active_items, filter)
return active_items
end,
add = function(entity, clicked_item)
entity.set_filter(1, clicked_item)
end,
remove = function(entity, clicked_item)
entity.set_filter(1, nil)
end,
}
local many_filters_updater = {
condition = function(entity)
return entity.filter_slot_count > 1
end,
button_description = { "fh.tooltip-filters" },
get_active_items = function(entity)
local active_items = {}
for i = 1, entity.filter_slot_count do
fh_util.add_item_to_table(active_items, entity.get_filter(i))
end
return active_items
end,
add = function(entity, clicked_item)
local found_slot
for i = 1, entity.filter_slot_count do
local found_filter = entity.get_filter(i)
if found_filter then
if fh_util.is_same_item(found_filter, clicked_item) then
return
end
elseif not found_slot then
found_slot = i
end
end
if found_slot then
local filter_to_set = entity.type == "mining-drill" and clicked_item.name or clicked_item
entity.set_filter(found_slot, filter_to_set)
return
end
return { "fh.filters-full" }
end,
remove = function(entity, clicked_item)
for i = 1, entity.filter_slot_count do
local filter = entity.get_filter(i)
if filter and fh_util.is_same_item(filter, clicked_item) then
entity.set_filter(i, nil)
return
end
end
return { "fh.filters-empty" }
end,
}
local splitter_filter_updater = {
condition = function(entity)
return entity.type == "splitter"
end,
button_description = { "fh.tooltip-filters" },
get_active_items = function(entity)
local active_items = {}
fh_util.add_item_to_table(active_items, entity.splitter_filter)
return active_items
end,
add = function(entity, clicked_item)
entity.splitter_filter = { name = clicked_item.name, quality = clicked_item.quality }
if entity.splitter_output_priority == "none" then
entity.splitter_output_priority = "left"
end
end,
remove = function(entity, clicked_item)
entity.splitter_filter = nil
end,
}
return function(entity)
for _, updater in pairs { logistic_chest_updater, filtered_inventory_updater, many_filters_updater, one_filter_updater, splitter_filter_updater } do
if updater.condition(entity) then
return {
get_active_items = function()
return updater.get_active_items(entity)
end,
add = function(clicked_item, modifiers)
return updater.add(entity, clicked_item, modifiers)
end,
remove = function(clicked_item, modifiers)
return updater.remove(entity, clicked_item, modifiers)
end,
button_description = updater.button_description
}
end
end
end