-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstash.lua
More file actions
236 lines (217 loc) · 8.2 KB
/
stash.lua
File metadata and controls
236 lines (217 loc) · 8.2 KB
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
function is_player_valid(player)
return player
and player.valid
and player.connected
and player.character
and player.character.valid
and player.controller_type == defines.controllers.character
end
function is_inventory_empty(inventory)
if not inventory then return true end
return inventory.get_item_count() == 0
end
local function quality_id_from_stack(stack)
if not stack then
return nil
end
local quality = stack.quality
if quality == nil then
return nil
end
if type(quality) == "string" then
return quality
end
if quality.name then
return quality.name
end
return nil
end
local function stack_definition_from_stack(stack)
local def = { name = stack.name, count = stack.count }
local quality = quality_id_from_stack(stack)
if quality ~= nil then
def.quality = quality
end
return def
end
function ensure_empty_stash(player_index, stash_name, size)
if storage.PData[player_index][stash_name] then
local stash = storage.PData[player_index][stash_name]
if not stash.is_empty() then
return false
end
return true
else
storage.PData[player_index][stash_name] = game.create_inventory(size)
return true
end
end
function stash_inventory(source_inv, stash_inv)
if not source_inv or not stash_inv then return false, false end
local stashed_anything = false
local stash_full = false
for i = 1, #source_inv do
local stack = source_inv[i]
if stack.valid_for_read then
local stack_to_insert = stack_definition_from_stack(stack)
if stash_inv.can_insert(stack_to_insert) then
local inserted_count = stash_inv.insert(stack_to_insert)
if inserted_count > 0 then
if inserted_count < stack.count then
stack.count = stack.count - inserted_count
stash_full = true
else
stack.clear()
end
stashed_anything = true
else
stash_full = true
end
else
stash_full = true
end
end
end
return stashed_anything, stash_full
end
function unstash_inventory(stash_inv, target_inv)
if not stash_inv or not target_inv then return false, false end
local unstashed_anything = false
local player_inventory_full = false
for i = 1, #stash_inv do
local stack = stash_inv[i]
if stack.valid_for_read then
local stack_to_insert = stack_definition_from_stack(stack)
if target_inv.can_insert(stack_to_insert) then
local inserted_count = target_inv.insert(stack_to_insert)
if inserted_count > 0 then
if inserted_count < stack.count then
stack.count = stack.count - inserted_count
player_inventory_full = true
else
stack.clear()
end
unstashed_anything = true
else
player_inventory_full = true
end
else
player_inventory_full = true
end
end
end
return unstashed_anything, player_inventory_full
end
function stash_armor(player)
local armor_inventory = player.get_inventory(defines.inventory.character_armor)
if not armor_inventory or armor_inventory.is_empty() then
return false, false
end
local stashed_anything = false
local stash_full = false
for i = 1, #armor_inventory do
local stack = armor_inventory[i]
if stack.valid_for_read then
-- If it's armor with equipment, store equipment data
if stack.grid and stack.grid.valid then
local equipment_data = {}
for _, eq in pairs(stack.grid.equipment) do
equipment_data[#equipment_data + 1] = {
name = eq.name,
position = eq.position,
energy = eq.energy
}
end
table.sort(equipment_data, function(a, b)
if a.position.y ~= b.position.y then
return a.position.y < b.position.y
end
if a.position.x ~= b.position.x then
return a.position.x < b.position.x
end
return a.name < b.name
end)
storage.PData[player.index].armor_equipment_data = equipment_data
else
storage.PData[player.index].armor_equipment_data = nil
end
local armor_stash = storage.PData[player.index].armor_stash
local stack_to_insert = stack_definition_from_stack(stack)
if armor_stash.can_insert(stack_to_insert) then
local inserted_count = armor_stash.insert(stack_to_insert)
if inserted_count > 0 then
if inserted_count < stack.count then
stack.count = stack.count - inserted_count
stash_full = true
else
stack.clear()
end
stashed_anything = true
else
stash_full = true
end
else
stash_full = true
end
end
end
return stashed_anything, stash_full
end
function unstash_armor(player)
local armor_inventory = player.get_inventory(defines.inventory.character_armor)
local armor_stash = storage.PData[player.index].armor_stash
if not armor_stash then return false, false end
local unstashed_anything = false
local player_inventory_full = false
for i = 1, #armor_stash do
local stack = armor_stash[i]
if stack.valid_for_read then
local stack_to_insert = stack_definition_from_stack(stack)
if armor_inventory.can_insert(stack_to_insert) then
local inserted_count = armor_inventory.insert(stack_to_insert)
if inserted_count > 0 then
if inserted_count < stack.count then
stack.count = stack.count - inserted_count
player_inventory_full = true
else
stack.clear()
end
unstashed_anything = true
-- Restore equipment if we have any saved
if storage.PData[player.index].armor_equipment_data then
local new_armor_stack = armor_inventory[1] -- The inserted armor should be here
if new_armor_stack and new_armor_stack.valid_for_read and new_armor_stack.grid then
for _, eq_data in ipairs(storage.PData[player.index].armor_equipment_data) do
local eq = new_armor_stack.grid.put({ name = eq_data.name, position = eq_data.position })
if eq then
eq.energy = eq_data.energy
end
end
end
storage.PData[player.index].armor_equipment_data = nil
end
else
player_inventory_full = true
end
else
player_inventory_full = true
end
end
end
return unstashed_anything, player_inventory_full
end
function move_items_to_inventory_or_leave(source_inv, target_inv)
if not source_inv or not target_inv then return end
for i = 1, #source_inv do
local stack = source_inv[i]
if stack.valid_for_read then
local stack_to_transfer = stack_definition_from_stack(stack)
if target_inv.can_insert(stack_to_transfer) then
local inserted = target_inv.insert(stack_to_transfer)
if inserted > 0 then
stack.clear()
end
end
end
end
end