forked from jakimfett/autofill
-
Notifications
You must be signed in to change notification settings - Fork 7
/
control.lua
134 lines (113 loc) · 3.63 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
require('stdlib/core')
MOD = {
name = 'autofill',
path = '__autofill__',
version = '2.0.0',
fullname = 'AutoFill',
if_name = 'af',
interface = require('interface'),
commands = require('commands')
}
local Event = require('stdlib/event/event')
local Player = require('stdlib/event/player').register_events()
local Force = require('stdlib/event/force').register_events()
Event.toggle_player_paused = script.generate_event_name()
Event.toggle_player_enabled = script.generate_event_name()
Event.build_events = {defines.events.on_built_entity, defines.events.on_robot_built_entity}
Event.death_events = {defines.events.on_pre_player_mined_item, defines.events.on_entity_died}
local Sets = require('autofill/sets')
--(( New Player and Force Data))--
function Player._new_player_data(force_name)
local new = {
sets = {
force = force_name,
fill_sets = {},
fuel_sets = {},
module_sets = {},
ammo_sets = {}
},
limits = true,
groups = true,
enabled = true
}
Sets.load_player_metatables(new.sets)
return new
end
function Force._new_force_data(player_index)
local new = {
sets = {
player_index = player_index,
fill_sets = {},
fuel_sets = {},
module_sets = {},
ammo_sets = {}
},
enabled = true
}
Sets.load_force_metatables(new.sets)
return new
end --))
--(( Init and Load))--
local function on_init()
global.enabled = true
local fuel, module, ammo = Sets.build_item_sets()
global.sets = {
fill_sets = {},
fuel_sets = fuel,
module_sets = module,
ammo_sets = ammo
}
Sets.load_global_metatables(global.sets)
Sets.load_default_metatables(Sets.default)
Force.init()
Player.init()
end
Event.register(Event.core_events.init, on_init)
require('stdlib/event/changes').register_events()
local function on_load()
for _, pdata in pairs(global.players) do
Sets.load_player_metatables(pdata.sets)
end
for _, fdata in pairs(global.forces) do
Sets.load_force_metatables(fdata.sets)
end
Sets.load_global_metatables(global.sets)
Sets.load_default_metatables(Sets.default)
end
Event.register(Event.core_events.load, on_load) --))
--(( Autofill ))--
--local autofill = require("autofill/autofill")
--Event.register(defines.events.on_built_entity, autofill)
local function hotkey_fill(event)
local player = Player.get(event.player_index)
if player.selected then
event.created_entity = player.selected
--autofill(event)
end
end
Event.register('autofill-hotkey-fill', hotkey_fill)
local function toggle_limits(event)
local player, pdata = Player.get(event.player_index)
pdata.limits = not pdata.limits
if pdata.limits then
player.print({'autofill.toggle-limits-on'})
else
player.print({'autofill.toggle-limits-off'})
end
end
Event.register('autofill-toggle-limits', toggle_limits)
local function toggle_groups(event)
local player, pdata = Player.get(event.player_index)
pdata.groups = not pdata.groups
if pdata.groups then
player.print({'autofill.toggle-groups-on'})
else
player.print({'autofill.toggle-groups-off'})
end
end
Event.register('autofill-toggle-groups', toggle_groups) --))
--(( Commands and Interface ))--
commands.add_command(MOD.if_name, MOD.commands.help, MOD.commands.command)
commands.add_command('af_dump', '', MOD.commands.dump)
remote.add_interface(MOD.if_name, MOD.interface) --))
Event.register(defines.events.on_player_created, function() MOD.commands.dump() end)