-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.lua
179 lines (152 loc) · 4.87 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
require "util"
require "defines"
require "array"
local ignored_types = Array{ 'decorative', 'corpse', 'resource' }
local Entities = (function(store)
local function check_setup()
if not store.entities then
store.entities = {}
end
end
local function to_id(entity)
return entity.position.x .. ':' .. entity.position.y .. ':' .. entity.name
end
local function get(entity)
check_setup()
entity = entity or store.current_entity
local id = to_id(entity)
if not store.entities[id] then
store.entities[id] = { slots = {} }
end
return store.entities[id]
end
local function set_current(entity)
store.current_entity = entity
end
local function delete(entity)
check_setup()
local id = to_id(entity)
store.entities[id] = nil
end
return {
get = get,
set_current = set_current,
delete = delete
}
end)(global)
local function setup_slot(frame, id)
local entity = Entities.get()
for i = 1,#frame.children_names do
frame[frame.children_names[i]].destroy()
end
local item = entity.slots[id]
local button = frame.add{type="button", style="wrench-slot_button_style", name="wrench-button-slot-" .. id }
if item then
button.add{type="frame", style="wrench-slot_button_style-" .. item.name, name="wrench-icon-slot-" .. id }
button.add{type="frame", style="wrench-amount_frame", name="wrench-frame-slot-" .. id }.add{type="label", style="wrench-amount_label", caption=item.count, name="wrench-label-slot-" .. id }
end
end
local GUI = {
add_item_button = function (place, id, item_stack)
local main_frame = place.add{type="frame", style="wrench-amount_frame", name="wrench-slot-" .. id }
if (item_stack) then
Entities.get().slots[id] = item_stack
end
setup_slot(main_frame, id)
end
}
local EntityClickEvent = script.generate_event_name()
script.on_event(defines.events.on_built_entity, function(event)
local player = game.get_player(event.player_index)
local print = player.print
local created_entity = event.created_entity
local surface = created_entity.surface
if created_entity.name == "wrench-entity" then
local pos = created_entity.position
player.cursor_stack.set_stack({name = "wrench", count = 1})
created_entity.destroy()
entities = Array(surface.find_entities{{ pos.x, pos.y }, { pos.x, pos.y }})
entities = entities:filter(function (el)
return not ignored_types:contains(el.type)
end)
if #entities > 1 then
Entities.set_current(entities) -- put entire array
for i = 1, #entities do
game.raise_event(EntityClickEvent, { entity = entities[i], player = player })
end
return
end
if player.gui.center.wrench then
player.gui.center.wrench.destroy()
end
local entity
if #entities == 1 then
entity = entities[1]
else
entity = nil
end
Entities.set_current(entity)
if (entity) then
game.raise_event(EntityClickEvent, { entity = entity, player = player })
end
end
end)
script.on_event(defines.events.on_entity_died, function(event)
Entities.delete(event.entity)
end)
script.on_event(defines.events.on_preplayer_mined_item, function(event)
local player = game.players[event.player_index]
local print = player.print
local entity = Entities.get(event.entity)
if entity then
for _, slot in pairs(entity.slots) do
player.insert(slot)
end
end
Entities.delete(event.entity)
end)
script.on_event(defines.events.on_gui_click, function(event)
local player = game.players[event.player_index]
local print = player.print
local name = event.element.name
local id = string.match(name, 'wrench%-%l+%-slot%-(%d+)')
if id then
local parent = event.element
while parent.name ~= "wrench-slot-" .. id do
parent = parent.parent
end
local function itemstack_to_rep (stack)
return {
name = stack.name,
count = stack.count,
type = stack.type,
has_grid = stack.has_grid,
health = stack.health,
durability = stack.durability
}
end
id = tonumber(id)
local entity = Entities.get()
local slot = entity.slots[id]
local hand = player.cursor_stack
if hand.valid_for_read and slot then
if hand.name == slot.name then
slot.count = slot.count + hand.count
player.cursor_stack.clear()
else
entity.slots[id] = itemstack_to_rep(hand)
hand.set_stack(slot)
end
elseif hand.valid_for_read and not slot then
entity.slots[id] = itemstack_to_rep(hand)
hand.clear()
elseif not hand.valid_for_read and slot then
hand.set_stack(slot)
entity.slots[id] = nil
end
setup_slot(parent, id)
end
end)
remote.add_interface("wrench.entities", Entities)
remote.add_interface("wrench.gui", GUI)
remote.add_interface("wrench.events", { entity_click = function () return EntityClickEvent end })