forked from rk84/autofill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.lua
447 lines (385 loc) · 13.6 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
require "util"
MOD = { NAME = "Autofill", IF = "af" }
require "loader"
loader.addItemArray "settings/vanilla-items"
loader.addSets "settings/vanilla-sets"
loader.addItemArray "settings/bob-newitems" --bobwarfare added Artillery and Laser Ammo
loader.extendItemArray "settings/bob-items" -- Bob Warfare Bullets, Cannon Shells - TODO have it prio extended items
loader.addSets "settings/bob-sets" -- Bobwarfare added tanks 2 and 3
loader.addSets "settings/farl-sets"
loader.extendItemArray "settings/ammobox-items"
loader.addSets "settings/ammobox-sets"
loader.addSets "settings/yuoki-ind-sets"
loader.addSets "settings/shuttle-train-sets" -- Autofill for shuttle trains
loader.extendItemArray "settings/aircraft-items"
loader.addSets "settings/aircraft-sets"
loader.addSets "settings/5dim-sets"
loader.extendItemArray "settings/up-items" -- Uranium Power Bullets and Cannon Shells -UNTESTED but based on the same concepts as the other additions so it should work.
--flying text colors
local RED = {r = 0.9}
local GREEN = {g = 0.7}
local YELLOW = {r = 0.8, g = 0.8}
local order = {
default = 1,
opposite = 2,
itemcount = 3
}
--
--Events
--
script.on_configuration_changed(function()
initMod()
end)
script.on_init(function()
initMod()
end)
script.on_event(defines.events.on_built_entity, function(event)
local player = game.players[event.player_index]
local global = global
if global.personalsets[player.name] and global.personalsets[player.name].active then
local fillset = global.personalsets[player.name][event.created_entity.name] or global.defaultsets[event.created_entity.name]
if fillset ~= 0 and fillset ~= nil then
autoFill(event.created_entity, player, fillset)
end
end
end)
script.on_event(defines.events.on_player_created, function(event)
local username = game.players[event.player_index].name
if global.personalsets[username] == nil then
global.personalsets[username] = { active = true }
end
end)
--
--Funtions
--
function autoFill(entity, player, fillset)
local textpos = entity.position
local maininv = player.get_inventory(defines.inventory.player_main)
local vehicleinv
local ok, err = pcall(function() vehicleinv = player.vehicle.get_inventory(2) end)
if not ok then vehicleinv = false end
local quickbar = player.get_inventory(defines.inventory.player_quickbar)
local array, item, count, groupsize, slots, totalitemcount, color, inserted, removed
for i=1, #fillset do
array = fillset[i]
item = false
count = 0
color = RED
if fillset.priority == order.itemcount then -- Pick item with highest count
for j = 1, #array do
if vehicleinv then
if maininv.get_item_count(array[j]) + vehicleinv.get_item_count(array[j]) > count then
item = array[j]
count = maininv.get_item_count(array[j]) + vehicleinv.get_item_count(array[j])
end
else
if maininv.get_item_count(array[j]) > count then
item = array[j]
count = maininv.get_item_count(array[j])
end
end
end
elseif fillset.priority == order.opposite then --Pick last available item
for j = #array, 1, -1 do
if maininv.get_item_count(array[j]) > 0 or vehicleinv and vehicleinv.get_item_count(array[j]) > 0 then
item = array[j]
count = maininv.get_item_count(array[j])
count = not vehicleinv and count or count + vehicleinv.get_item_count(array[j])
break
end
end
else --Pick first available item
for j = 1, #array do
if game.item_prototypes[array[j]] then
if maininv.get_item_count(array[j]) > 0 or vehicleinv and vehicleinv.get_item_count(array[j]) > 0 then
item = array[j]
count = maininv.get_item_count(array[j])
count = not vehicleinv and count or count + vehicleinv.get_item_count(array[j])
break
end
end
end
end
if not item or count < 1 then
if array[1] ~= nil and game.item_prototypes[array[1]] then
text({"autofill.out-of-item", game.item_prototypes[array[1]].localised_name }, textpos, color)
textpos.y = textpos.y + 1
end
else
-- Divide stack between group (only items in quickbar are part of group)
if fillset.group then
if player.cursor_stack.valid_for_read then
groupsize = player.cursor_stack.count + 1
else
groupsize = 1
end
for k,v in pairs(global.personalsets[player.name]) do
if type(v) == "table" and v.group == fillset.group then
groupsize = groupsize + quickbar.get_item_count(k)
end
end
for k,v in pairs(global.defaultsets) do
if not global.personalsets[player.name][k] and v.group == fillset.group then
groupsize = groupsize + quickbar.get_item_count(k)
end
end
totalitemcount = 0
for j=1, #array do
totalitemcount = totalitemcount + maininv.get_item_count(array[j])
end
if vehicleinv then
for j=1, #array do
totalitemcount = totalitemcount + vehicleinv.get_item_count(array[j])
end
end
count = math.max( 1, math.min( count, math.floor(totalitemcount / groupsize) ) )
end
-- Limit insertion if has limit value
if fillset.limits and fillset.limits[i] then
if count > fillset.limits[i] then
count = fillset.limits[i]
end
end
-- Determine insertable stack count if has slot count
if fillset.slots and fillset.slots[i] then
slots = fillset.slots[i]
else
slots = 1
end
if count < game.item_prototypes[item].stack_size * slots then
color = YELLOW
else
count = game.item_prototypes[item].stack_size * slots
color = GREEN
end
-- Insert, count the difference and remove the difference
inserted = entity.get_item_count(item)
entity.insert({name=item, count=count})
inserted = entity.get_item_count(item) - inserted
if inserted > 0 then
if vehicleinv then
removed = vehicleinv.get_item_count(item)
vehicleinv.remove({name=item, count=inserted})
removed = removed - vehicleinv.get_item_count(item)
if inserted > removed then
maininv.remove({name=item, count=inserted - removed})
end
else
maininv.remove({name=item, count=inserted})
end
if removed then
text({"autofill.insertion-from-vehicle", inserted, game.item_prototypes[item].localised_name, removed, game.entity_prototypes[player.vehicle.name].localised_name}, textpos, color)
textpos.y = textpos.y + 1
else
text({"autofill.insertion", inserted, game.item_prototypes[item].localised_name }, textpos, color)
textpos.y = textpos.y + 1
end
end
end -- if not item or count < 1 then
end -- for i=1, #fillset do
end
function getDefaultSets()
return {
["car"] = {priority=order.default, global.fuels.all, global.ammo.bullets },
["tank"] = {priority=order.default, slots={2,1,1}, global.fuels.all, global.ammo.bullets, global.ammo.shells },
["diesel-locomotive"] = {priority=order.default, slots={1}, global.fuels.high},
["boiler"] = {priority=order.default, group="burners", limits={5}, global.fuels.high},
["burner-inserter"]= {priority=order.default, group="burners", limits={1}, global.fuels.high},
["burner-mining-drill"] = {priority=order.default, group="burners", limits={5}, global.fuels.high},
["stone-furnace"] = {priority=order.default, group="burners", limits={5}, global.fuels.high},
["steel-furnace"] = {priority=order.default, group="burners", limits={5}, global.fuels.high},
["gun-turret"]= {priority=order.default, group="turrets", limits= {10}, global.ammo.bullets }
} -- if group is defined, then insertable items are divided for buildable
-- items in quickbar (and in hand).
end
function getLiteSets()
return {
["burner-inserter"]= {priority=order.default, group="burners", limits={1}, global.fuels.high},
["gun-turret"]= {priority=order.default, group="turrets", limits= {10}, global.ammo.bullets }
}
end
function makePersonalLiteSets()
local personalsets = {}
for k, v in pairs(global.defaultsets) do
personalsets[k] = 0
end
personalsets["burner-inserter"] = { priority=order.default, group="burners", limits={1}, global.item_arrays["fuels-high"] }
personalsets["gun-turret"] = { priority=order.default, group="turrets", limits= {10}, global.item_arrays["ammo-bullets"] }
return personalsets
end
function globalPrint(msg)
local players = game.players
msg = { "autofill.msg-template", msg }
for i=1, #players do
players[i].print(msg)
end
end
function initMod(reset)
if not global.defaultsets or not global.personalsets or not global.item_arrays or reset then
global = {} -- Clears global
loader.loadBackup()
global.personalsets = {}
for k, player in pairs(game.players) do
global.personalsets[player.name] = { active = true }
end
global.has_init = true
else
loader.updateFuelArrays(global.item_arrays)
end
end
function isValidEntity(name)
if game.entity_prototypes[name] then
return true
end
globalPrint( {"autofill.invalid-entityname", tostring(name)} )
return false
end
-- This fuction not just verifies the set, but also links strings into item arrays (replaces).
function isValidSet(set)
if set == nil or set == 0 then
return true
elseif type(set) == "table" then
for i = 1, #set do
if type(set[i]) == "string" then
if global.item_arrays[set[i]] then -- replace name with array
set[i] = global.item_arrays[set[i]]
else
if game.item_prototypes[set[i]] then
set[i] = { set[i] }
else
globalPrint( {"autofill.invalid-itemname", tostring(set[i])} )
return false
end
end
elseif type(set[i]) == "table" then
for j = 1, #set[i] do
if game.item_prototypes[set[i][j]] == nil then
globalPrint( {"autofill.invalid-itemname", tostring(set[i][j])} )
return false
end
end
else
globalPrint( {"autofill.invalid-form"} )
return false
end
end -- for i = 1, #set do
return true
end
globalPrint( {"autofill.invalid-parameter"} )
return false
end
function isValidUser(name)
local players = game.players
for i=1, #players do
if players[i].name == name then
return players[i].name
end
end
if game.player then -- for single player game
return game.player.name
end
globalPrint( {"autofill.invalid-username", tostring(name)} )
return false
end
function printToFile(line, path)
path = path or "log"
path = table.concat({ MOD.IF, "/", path, ".txt" })
game.makefile( path, line)
end
function text(line, pos, colour) --colour as optional
local surface = game.surfaces[1]
if colour == nil then
surface.create_entity({name="flying-text", position=pos, text=line})
else
surface.create_entity({name="flying-text", position=pos, text=line, color=colour})
end
end
--
-- Mod interface
--
remote.add_interface(MOD.IF,
{
insertset = function(username, entityname, set)--this fuction is for inserting personal sets.
username = isValidUser(username)
if username and isValidEntity(entityname) and isValidSet(set) then
global.personalsets[username][entityname] = set
end
end,
addToDefaultSets = function(entityname, set)
if isValidEntity(entityname) and isValidSet(set) then
global.defaultsets[entityname] = set
end
end,
getDefaultSets = function()
local sets = table.deepcopy(global.defaultsets)
for entity_name, set in pairs(sets) do
for i=1, #set do
for name, array in pairs(global.item_arrays) do
if global.defaultsets[entity_name][i] == array then
set[i] = name
break
end
end
end
end
return sets
end,
setDefaultSets = function(sets)
for entity_name, set in pairs(sets) do
if not isValidSet(set) then
return
end
end
global.defaultsets = sets
end,
getBackupLog = function()
local tbl = loader.getBackupLog()
local block = table.concat(tbl, "\n")
log(block)
end,
getItemArray = function(name)
return global.item_arrays[name]
end,
setItemArray = function(name, new_array)
if global.item_arrays[name] == nil then
global.item_arrays[name] = new_array
else -- replaces content of table without creating new table to maintain referers
local old_array = global.item_arrays[name]
local max = #old_array < #new_array and #new_array or #old_array
for i=1, max do
old_array[i] = new_array[i]
end
end
end,
logGlobal = function(key)
key = key or "global"
if _G[key] then
log( serpent.block(_G[key]) )
else
globalPrint("Global not found.")
end
end,
resetMod = function()
initMod(true)
end,
resetUser = function(setname, username)
username = isValidUser(username)
if username then
if setname == "lite" then
global.personalsets[username] = makePersonalLiteSets()
else
global.personalsets[username] = { active = true }
end
end
end,
toggleUsage = function(username)
username = isValidUser(username)
if username then
if global.personalsets[username] then
global.personalsets[username].active = not global.personalsets[username].active
else
global.personalsets[username] = { active = true }
end
end
end
})