forked from jakimfett/autofill
-
Notifications
You must be signed in to change notification settings - Fork 6
/
autofill.lua
252 lines (229 loc) · 10.7 KB
/
autofill.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
local Position = require("stdlib/area/position")
local Iter = require("stdlib/utils/iter")
local priorities = {["qty"] = "qty", ["max"] = "max", ["min"] = "min"}
local function flying_text(line, color, pos, surface)
color = color or defines.color.red
line = line or "missing text" --If we for some reason didn't pass a message make a message
if not pos then
for _, p in pairs(game.players) do
p.surface.create_entity({name = "flying-text", position = p.position, text = line, color = color})
end
return
else
if surface then
surface.create_entity({name = "flying-text", position = pos, text = line, color = color})
end
end
end
-- return false if there is a recipe and it is not enabled
-- TODO Make this better, Some items don't have the same recipe name and could
-- therefore not be enabled.
local function is_enabled(item, force)
if force and force.recipes[item] then
if force.recipes[item].enabled then
return true
else
return false
end
end
return true
end
local function _sort(t, a, b)
return t[b] < t[a]
end
local function get_highest_value(tbl, force)
for item in Iter.spairs(tbl, _sort) do
if game.item_prototypes[item] and is_enabled(item, force) then
return item, game.item_prototypes[item].stack_size
end
end
return nil, 0
end
local function autofill(event)
local entity = event.created_entity
local player, pdata = game.players[event.player_index], global.players[event.player_index]
local ghost = entity.name == "entity-ghost" and player.mod_settings["autofill-ghost"].value
local set = (ghost and pdata.sets.fill_sets[entity.ghost_name]) or (pdata.sets.fill_sets[entity.name] and player.mod_settings["autofill-entity"].value)
if set and global.enabled and pdata.enabled then
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
--Increment y position everytime text_pos is called
local text_pos = Position(entity.position):increment(0, 1)
--local fuel_slots = pcall(#entity.get_inventory(defines.inventory.fuel))
-- local duplicate_counts = {}
--Get inventories
local main_inventory = player.get_inventory(defines.inventory.player_main)
local quickbar = player.get_inventory(defines.inventory.player_quickbar)
local vehicle_inventory = player.vehicle and player.vehicle.get_inventory(defines.inventory.car_trunk)
local slot_counts = {}
--further divide amongst same type slot counts
for _, slot in ipairs(set.slots) do
slot_counts[slot.category] = slot_counts[slot.category] or 0 + 1
end
--Loop through each slot in the set.
for _, slot in ipairs(set.slots) do
local color = defines.color.red -- Color for fying text
local item = false -- The item to insert
local item_count = 0 -- Total count of item in inventory and car inventory
local insert_count = 0 -- Count of items to insert
local group_count = 1 -- Num of items in the group. (Hand + Quickbar)
--verify or set existing priority
local priority = priorities[slot.priority] or "qty"
-- START Item Count --------------------------------------------------------
--Get the item list from player or default if no player
--TODO META TF this up
local item_list = table.dictionary_merge(pdata.sets.item_sets[slot.type][slot.category])
--No item list or item list is not a table.
if not item_list or type(item_list) ~= "table" then
flying_text({"autofill.invalid-category"}, color, text_pos(), entity.surface)
goto break_out
end
if player.cheat_mode then
item = get_highest_value(item_list, player.force)
item_count = 32000000
elseif priority == "qty" or ghost then
for item_name, _ in pairs(item_list) do
local proto = game.item_prototypes[item_name]
if proto and is_enabled(item_name, player.force) then
local mi = main_inventory.get_item_count(item_name)
local vi = vehicle_inventory and vehicle_inventory.get_item_count(item_name) or 0
local total = mi + vi
if (total > 0 and total > item_count) then
item = item_name
item_count = total
end
end
if not item and ghost then
item, item_count = get_highest_value(item_list, player.force)
end
end
elseif priority == "max" then
for item_name, _ in iter.spairs(
item_list,
function(t, a, b)
return t[b] < t[a]
end
) do
local proto = game.item_prototypes[item_name]
if proto and is_enabled(item_name, player.force) then
local mi = main_inventory.get_item_count(item_name)
local vi = vehicle_inventory and vehicle_inventory.get_item_count(item_name) or 0
local total = mi + vi
if total > 0 then
item = item_name
item_count = total
break
end
end
end
elseif priority == "min" then
for item_name, _ in iter.spairs(
item_list,
function(t, a, b)
return t[b] > t[a]
end
) do
local proto = game.item_prototypes[item_name]
if proto and is_enabled(item_name, player.force) then
local mi = main_inventory.get_item_count(item_name)
local vi = vehicle_inventory and vehicle_inventory.get_item_count(item_name) or 0
local total = mi + vi
if total > 0 then
item = item_name
item_count = total
break
end
end
end
end
if not ghost then
if not item or item_count < 1 then
local key_table = table.keys(item_list)
if key_table[1] ~= nil and game.item_prototypes[key_table[1]] then
flying_text({"autofill.out-of-item", slot.type}, color, text_pos(), entity.surface)
elseif key_table[1] ~= nil then
flying_text({"autofill.invalid-itemname", key_table[1]}, color, text_pos(), entity.surface)
end
goto break_out
end
-- END Item Count --We now have an item, and a count of the amount of items we have.
-- START Groups -------------------------------------------------------------
--Divide stack between group items (only items in quickbar and hand are part of the group)
--Get count in hands
if (set.group and pdata.groups) then
if player.cursor_stack.valid_for_read then
group_count = group_count + player.cursor_stack.count
end
for entity_name, set_table in pairs(pdata.sets) do
if type(set_table) == "table" and set_table.group == set.group then
group_count = group_count + quickbar.get_item_count(entity_name)
end
end
end
end
--END Groups--
local slot_count = slot_counts[slot.category] or 1 - 1
if slot_count < 1 then
slot_count = 1
end
local stack_size = game.item_prototypes[item].stack_size
insert_count =
insert_count + min(max(1, min(item_count, floor(item_count / ceil(group_count / slot_count)))), (pdata.limits and slot.limit) or stack_size)
-- START inesertion -------------------------------------------------
if ghost then
local requests = entity.item_requests or {}
requests[item] = (requests[item] or 0) + insert_count
entity.item_requests = requests
else
local removed, inserted
inserted =
entity.insert {
name = item,
count = insert_count
}
if inserted > 0 then
if vehicle_inventory then
removed =
vehicle_inventory.remove {
name = item,
count = inserted
}
if inserted > removed then
main_inventory.remove {
name = item,
count = inserted - removed
}
end
else
main_inventory.remove {
name = item,
count = inserted
}
end
if inserted < stack_size then
color = defines.color.yellow
elseif insert_count >= stack_size then
color = defines.color.green
end
if removed then
flying_text(
{
"autofill.insertion-from-vehicle",
inserted,
game.item_prototypes[item].localised_name,
removed,
game.entity_prototypes[player.vehicle.name].localised_name
},
color,
text_pos(),
entity.surface
)
else
flying_text({"autofill.insertion", inserted, game.item_prototypes[item].localised_name}, color, text_pos(), entity.surface)
end
end
end
::break_out::
end
end
end
return autofill