-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
403 lines (342 loc) · 15.4 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
script.on_init(function()
initVariables()
init_gui()
end)
script.on_load(function()
initVariables()
end)
-- Calculate module level, minimum 1 maximum 100
function modulelevel()
local exponent = settings.startup["alien-module-level-exponent"].value
if (global.killcount < 10000) then
return math.min(math.max(math.log((global.killcount + 1) * 0.1) * math.pow((global.killcount), exponent), 1) * math.sqrt((global.killcount * 0.0001)), 100)
else
return math.min(math.max(math.log((global.killcount + 1) * 0.1) * math.pow((global.killcount), exponent), 1), 100)
end
end
function roundModuleLevel()
return math.floor(modulelevel() * 1000 + 0.5) / 1000
end
function initVariables()
if global.currentmodulelevel == nil then
global.currentmodulelevel = 1
end
if global.modulelevel == nil then
global.modulelevel = 1
end
if global.killcount == nil then
global.killcount = 0
end
end
function init_gui()
for _, player in pairs(game.players) do
player.gui.top.add { type = "frame", name = "alienmodule", direction = "vertical" }
player.gui.top.alienmodule.add { type = "label", name = "killcount", caption = "TEST" }
player.gui.top.alienmodule.add { type = "progressbar", name = "killbar" }
player.gui.top.alienmodule.killbar.value = math.max(roundModuleLevel() - global.modulelevel, 0)
end
end
-- pretty print a variable var
function pp(key, param)
for _, player in pairs(game.players) do
if type(key) == "string" then
player.print({ key, param })
end
end
end
function update_gui()
for _, player in pairs(game.players) do
if player.gui.top.killcount ~= nil then
player.gui.top.killcount.destroy()
end
if player.gui.top.killbar ~= nil then
player.gui.top.killbar.destroy()
end
if player.gui.top.alienmodule == nil then
player.gui.top.add { type = "frame", name = "alienmodule", direction = "vertical" }
end
if player.gui.top.alienmodule.killcount == nil then
player.gui.top.alienmodule.add { type = "label", name = "killcount", caption = "0" }
end
if player.gui.top.alienmodule.killbar == nil then
player.gui.top.alienmodule.add { type = "progressbar", name = "killbar" }
end
player.gui.top.alienmodule.killcount.caption = { 'gui.label', roundModuleLevel(), global.killcount }
player.gui.top.alienmodule.killbar.value = math.max(roundModuleLevel() - global.modulelevel, 0)
end
end
function update_modules(entities, entityType)
for _, entity in pairs(entities) do
local inventory --what type of inventory does this entity have?
if entityType == "chest" then
inventory = entity.get_inventory(defines.inventory.chest) --grab a chest's inventory
for i = 1, entity.request_slot_count do
local slot = entity.get_request_slot(i)
if slot ~= nil and slot.name == "alien-hyper-module-" .. global.currentmodulelevel - 1 then
entity.set_request_slot({ name = "alien-hyper-module-" .. global.currentmodulelevel, count = slot.count }, i)
end
if settings.startup["alien-module-hyper-ammo-enabled"].value and slot ~= nil and slot.name == "alien-hyper-magazine-" .. global.currentmodulelevel - 1 then
entity.set_request_slot({ name = "alien-hyper-magazine-" .. global.currentmodulelevel, count = slot.count }, i)
end
end
elseif entityType == "machine" then
inventory = entity.get_module_inventory() --grab a machine's inventory
elseif entityType == "player" then
inventory = entity.get_main_inventory(defines.inventory.player_main) --grab a player's inventory
-- update currently held items
if entity.cursor_stack ~= nil and entity.cursor_stack.valid_for_read then
if string.find(entity.cursor_stack.name, "^alien%-hyper%-module") then
--if theres a module in this inventory slot
if tonumber(string.match(entity.cursor_stack.name, "%d+$")) < global.currentmodulelevel then
--and its level is less than the "current" one
local stacksize = entity.cursor_stack.count --record amount
entity.cursor_stack.clear() --clear the slot
entity.cursor_stack.set_stack({ name = "alien-hyper-module-" .. math.min(global.currentmodulelevel, 100), count = stacksize }) --add the updated level modules with whatever amount we recorded
end
end
if string.find(entity.cursor_stack.name, "^alien%-hyper%-magazine") then
--if theres a module in this inventory slot
if tonumber(string.match(entity.cursor_stack.name, "%d+$")) < global.currentmodulelevel then
--and its level is less than the "current" one
local stacksize = entity.cursor_stack.count --record amount
entity.cursor_stack.clear() --clear the slot
entity.cursor_stack.set_stack({ name = "alien-hyper-magazine-" .. math.min(global.currentmodulelevel, 100), count = stacksize }) --add the updated level modules with whatever amount we recorded
end
end
end
else
return --error entity type not defined
end
if inventory == nil then
return
end
for i = 1, #inventory, 1 do
--loop through all of the entity's inventory slots
local status, err = pcall(function()
if string.find(inventory[i].name, "^alien%-hyper%-module") then
--if theres a module in this inventory slot
if tonumber(string.match(inventory[i].name, "%d+$")) < global.currentmodulelevel then
--and its level is less than the "current" one
local stacksize = inventory[i].count --record amount
inventory[i].clear() --clear the slot
if entityType == "player" and inventory.get_filter(i) ~= nil then
-- check if slot is filtered
inventory.set_filter(i, "alien-hyper-module-" .. math.min(global.currentmodulelevel, 100)) --update filter
end
inventory[i].set_stack({ name = "alien-hyper-module-" .. math.min(global.currentmodulelevel, 100), count = stacksize }) --add the updated level modules with whatever amount we recorded
end
end
if string.find(inventory[i].name, "^alien%-hyper%-magazine") then
--if theres a module in this inventory slot
if tonumber(string.match(inventory[i].name, "%d+$")) < global.currentmodulelevel then
--and its level is less than the "current" one
local stacksize = inventory[i].count --record amount
inventory[i].clear() --clear the slot
if entityType == "player" and inventory.get_filter(i) ~= nil then
-- check if slot is filtered
inventory.set_filter(i, "alien-hyper-magazine-" .. math.min(global.currentmodulelevel, 100)) --update filter
end
inventory[i].set_stack({ name = "alien-hyper-magazine-" .. math.min(global.currentmodulelevel, 100), count = stacksize })
--add the updated level modules with whatever amount we recorded
end
end
end)
end
end
end
function update_ammo(turrets)
for _, entity in pairs(turrets) do
local inventory = entity.get_inventory(defines.inventory.chest) --grab a chest's inventory
if inventory == nil then
return
end
for i = 1, #inventory, 1 do
--loop through all of the entity's inventory slots
local status, err = pcall(function()
if string.find(inventory[i].name, "^alien%-hyper%-magazine") then
--if theres hyper ammo in the inventory slot
if tonumber(string.match(inventory[i].name, "%d+$")) < global.currentmodulelevel then
--and its level is less than the "current" one
local stacksize = inventory[i].count --record amount
inventory[i].clear() --clear the slot
inventory[i].set_stack({ name = "alien-hyper-magazine-" .. math.min(global.currentmodulelevel, 100), count = stacksize })
end
end
end)
end
end
end
function update_recipes(assemblers, force)
for _, entity in ipairs(assemblers) do
if entity.get_recipe() ~= nil then
--if the assembler has a set recipe
if string.find(entity.get_recipe().name, "^alien%-hyper%-module") then
--and its one of ours
local plates_to_refund = 0
-- Save the number of modules in the output slot, crafting progress and bonus progress
local finished_module_count = entity.get_inventory(defines.inventory.assembling_machine_output).get_item_count("alien-hyper-module-" .. global.currentmodulelevel - 1)
local crafting_progress = entity.crafting_progress
local bonus_progress = entity.bonus_progress
entity.set_recipe(force.recipes["alien-hyper-module-" .. global.currentmodulelevel]) --set it to the updated recipe
-- Add the modules back
if finished_module_count > 0 then
entity.get_inventory(defines.inventory.assembling_machine_output).insert { name = "alien-hyper-module-" .. global.currentmodulelevel, count = finished_module_count }
end
-- Restore previous progress
entity.crafting_progress = crafting_progress
entity.bonus_progress = bonus_progress
end
if settings.startup["alien-module-hyper-ammo-enabled"].value and string.find(entity.get_recipe().name, "^alien%-hyper%-magazine") then
local finished_module_count = entity.get_inventory(defines.inventory.assembling_machine_output).get_item_count("alien-hyper-module-" .. global.currentmodulelevel - 1)
entity.set_recipe(force.recipes["alien-hyper-magazine-" .. global.currentmodulelevel]) --set it to the updated recipe
if finished_module_count > 0 then
entity.get_inventory(defines.inventory.assembling_machine_output).insert {
name = "alien-hyper-magazine-" .. global.currentmodulelevel,
count = finished_module_count
}
end
end
end
end
end
function update_quickbar(players)
for _, player in pairs(players) do
for i = 1, 100 do
local slot = player.get_quick_bar_slot(i)
if slot ~= nil and slot.name == "alien-hyper-module-" .. global.currentmodulelevel - 1 then
player.set_quick_bar_slot(i, "alien-hyper-module-" .. global.currentmodulelevel)
end
end
end
end
function update_logistic_slots(players)
for _, player in pairs(players) do
if player.character ~= nil then
for i = 1, player.character.request_slot_count do
local slot = player.get_personal_logistic_slot(i)
if slot ~= nil and slot.name == "alien-hyper-module-" .. global.currentmodulelevel - 1 then
player.set_personal_logistic_slot(i, { name = "alien-hyper-module-" .. global.currentmodulelevel, min = slot.min, max = slot.max })
end
if settings.startup["alien-module-hyper-ammo-enabled"].value and slot ~= nil and slot.name == "alien-hyper-magazine-" .. global.currentmodulelevel - 1 then
player.set_personal_logistic_slot(i, { name = "alien-hyper-magazine-" .. global.currentmodulelevel, min = slot.min, max = slot.max })
end
end
end
end
end
--[[function update_trash_slots(players)
for _, player in pairs(players) do
local old_trash = player.auto_trash_filters
local new_trash = {}
for key, value in ipairs(old_trash) do
player.print(key)
player.print(value)
if key ~= nil and key == "alien-hyper-module-" .. global.currentmodulelevel - 1 then
new_trash.insert("alien-hyper-module-" .. global.currentmodulelevel, value)
else
new_trash.insert(key, value)
end
end
player.print(new_trash)
player.auto_trash_filters = new_trash
end
end]]
function update_enabled_recipe()
for _, force in pairs(game.forces) do
if force.technologies["automation"].researched then
if global.currentmodulelevel > 1 then
force.recipes["alien-hyper-module-1"].enabled = false
force.recipes["alien-hyper-module-" .. global.currentmodulelevel - 1].enabled = false
force.recipes["alien-hyper-module-" .. global.currentmodulelevel].enabled = true
end
end
if force.technologies["military"].researched and settings.startup["alien-module-hyper-ammo-enabled"].value then
if global.currentmodulelevel > 1 then
force.recipes["alien-hyper-magazine-1"].enabled = false
force.recipes["alien-hyper-magazine-" .. global.currentmodulelevel - 1].enabled = false
force.recipes["alien-hyper-magazine-" .. global.currentmodulelevel].enabled = true
else
force.recipes["alien-hyper-magazine-1"].enabled = true
end
end
end
end
-- not in use yet, prototype for later use
function update_modules_on_surface(surface)
local names = {}
local modulesOnGround = surface.find_entities_filtered { name = 'item-on-ground' }
local current_module_name = 'alien-hyper-module-' .. tostring(math.min(global.currentmodulelevel, 100))
local current_magazine_name = 'alien-hyper-magazine-' .. tostring(math.min(global.currentmodulelevel, 100))
for index, module_on_ground in pairs(modulesOnGround) do
--game.print(module_on_ground.stack.name)
local real_name = module_on_ground.stack.name
local module_pos = module_on_ground.position
local item_count = module_on_ground.stack.count
if (string.find(real_name, "^alien%-hyper%-module") and real_name ~= current_module_name) then
module_on_ground.destroy()
surface.create_entity { name = 'item-on-ground', position = module_pos, stack = { name = current_module_name, count = item_count } }
end
if settings.startup["alien-module-hyper-ammo-enabled"].value and (string.find(real_name, "^alien%-hyper%-magazine") and real_name ~= current_magazine_name) then
module_on_ground.destroy()
surface.create_entity { name = 'item-on-ground', position = module_pos, stack = { name = current_magazine_name, count = item_count } }
end
end
end
-- if an entity is killed, raise killcount
script.on_event(defines.events.on_entity_died, function(event)
if (event.entity.type == "unit" and event.entity.force.name == "enemy") then
global.killcount = global.killcount + 1
end
end)
-- Every 2 seconds: calculate the module level and upgrade hyper modules if level floor value changed
script.on_nth_tick(120, function(event)
global.modulelevel = math.max(math.floor(modulelevel()), 1)
update_gui()
-- if the modulelevel is raised by the kill, increase the level of all hyper modules by finding and replacing them
if (global.modulelevel > global.currentmodulelevel) then
global.currentmodulelevel = global.currentmodulelevel + 1
--update what module recipe is enabled
update_enabled_recipe()
for _, surface in pairs(game.surfaces) do
local assemblers = surface.find_entities_filtered { type = "assembling-machine" }
local miners = surface.find_entities_filtered { type = "mining-drill" }
local labs = surface.find_entities_filtered { type = "lab" }
local furnaces = surface.find_entities_filtered { type = "furnace" }
local rocketSilos = surface.find_entities_filtered { name = "rocket-silo" }
local chests = surface.find_entities_filtered { type = "container" }
local logisticChests = surface.find_entities_filtered { type = "logistic-container" }
local beacons = surface.find_entities_filtered { type = "beacon" }
local turrets = surface.find_entities_filtered { type = "ammo-turret" }
update_modules(assemblers, "machine")
update_modules(miners, "machine")
update_modules(labs, "machine")
update_modules(furnaces, "machine")
update_modules(rocketSilos, "machine")
update_modules(chests, "chest")
update_modules(logisticChests, "chest")
update_modules(beacons, "machine")
if settings.startup["alien-module-hyper-ammo-enabled"].value then
update_ammo(turrets)
end
for _, force in pairs(game.forces) do
update_recipes(assemblers, force)
end
update_modules_on_surface(surface)
end
local players = game.players
update_modules(players, "player")
update_quickbar(players)
update_logistic_slots(players)
-- update_trash_slots(players)
pp('gui.module-upgraded', global.modulelevel)
-- play level up sound
for _, player in pairs(game.players) do
player.play_sound { path = 'alien-level-up' }
end
else
--every 10 seconds update what module recipe is enabled
if event.tick % 600 == 0 then
update_enabled_recipe()
end
end
end)