forked from 3RaGaming/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravestone.lua
266 lines (252 loc) · 10 KB
/
gravestone.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
--Gravestone [based on Hazzard's Gravestone Mod]
--A 3Ra Gaming revision
--[[ list of inventories to save - constants from api reference]] --
local storeinventories = {
defines.inventory.player_vehicle,
defines.inventory.player_armor,
defines.inventory.player_tools,
defines.inventory.player_guns,
defines.inventory.player_ammo,
defines.inventory.player_quickbar,
defines.inventory.player_main,
defines.inventory.player_trash,
}
--[[ name of inventories to print on report ]] --
local storeinventoriesstring = {
"Vehicle",
"Armor",
"Tools",
"Guns",
"Ammo",
"Quickbar",
"Main",
"Trash",
}
local save_craft_queue = true
local function spawn_chest(player, chestname)
local savechest = nil
if player ~= nil then
local playersurface = game.surfaces[player.surface.name]
if playersurface ~= nil then
local chestposition = playersurface.find_non_colliding_position("steel-chest", player.position, 100, 1)
if chestposition ~= nil then
savechest = playersurface.create_entity({
name = chestname,
position = chestposition,
force = game.forces.neutral
})
if savechest ~= nil then
savechest.destructible = false
savechest.last_user = player
end
end
end
end
return savechest
end
local function on_player_died(event)
local player = game.players[event.player_index]
if player ~= nil then
local transfered = 0
local chestId = 1
local savechest = spawn_chest(player, "steel-chest")
if savechest ~= nil then
local chestinventory = savechest.get_inventory(defines.inventory.chest)
--[[ save all predefined inventories ]] --
for i = 1, #storeinventories, 1 do
local inventoryid = storeinventories[i]
local playerinventory = player.get_inventory(inventoryid)
if playerinventory ~= nil and chestinventory ~= nil then
player.print("Storing items from inventory '" .. storeinventoriesstring[i] .. "(" .. tostring(inventoryid) .. ")' to chest #" .. tostring(chestId))
--[[ Get all items in current inventory ]] --
for j = 1, #playerinventory, 1 do
local inserted = 0
if playerinventory[j].valid and playerinventory[j].valid_for_read then
local item = playerinventory[j]
if storeinventories[i] == defines.inventory.player_guns and item.name == "pistol" then
--[[ Do nothing, do not store a pistol in the chest. Prevents infinite pistols (Although who the hell would abuse that anyway) ]] --
else
if storeinventories[i] == defines.inventory.player_ammo and item.name == "firearm-magazine" then
if item.count > 10 then
item.count = item.count - 10
end
end
if chestinventory ~= nil and chestinventory.can_insert(item) then
inserted = chestinventory.insert(item)
transfered = transfered + 1
else --[[ If item cannot be inserted into current chest, create new chest]] --
savechest = spawn_chest(player, "steel-chest")
chestinventory = nil
if savechest ~= nil then
chestinventory = savechest.get_inventory(defines.inventory.chest)
if chestinventory ~= nil then
inserted = chestinventory.insert(item)
transfered = transfered + 1
chestId = chestId + 1
player.print("Storing items from inventory '" .. storeinventoriesstring[i] .. "(" .. tostring(inventoryid) .. ")' to chest #" .. tostring(chestId))
end
else --[[ break if unable to spawn new chest ]] --
break
end
end
--[[ If the entire item stack was not inserted, decrease the count and add the remainder into a new chest]] --
if item.count > inserted then
item.count = item.count - inserted
savechest = spawn_chest(player, "steel-chest")
chestinventory = nil
if savechest ~= nil then
chestinventory = savechest.get_inventory(defines.inventory.chest)
if chestinventory ~= nil then
inserted = chestinventory.insert(item)
transfered = transfered + 1
chestId = chestId + 1
player.print("Storing items from inventory '" .. storeinventoriesstring[i] .. "(" .. tostring(inventoryid) .. ")' to chest #" .. tostring(chestId))
end
else --[[ break if unable to spawn new chest ]] --
break
end
end
end
if item.grid then
for k = 1, #chestinventory, 1 do
local itemstack = chestinventory[k]
if itemstack.valid and itemstack.valid_for_read and itemstack.grid and itemstack.name == item.name and next(itemstack.grid.equipment) == nil then
local fail = false
for _,equip in ipairs(item.grid.equipment) do
local name = equip.name
local pos = equip.position
if not itemstack.grid.put{name = name, position = pos} then fail = true end
end
if fail then player.print("Failed to save modules for armor " + item.name) end
break
end
end
end
if item.name == "blueprint" and item.is_blueprint_setup() then
for k = 1, #chestinventory, 1 do
local chestitem = chestinventory[k]
if chestitem.valid and chestitem.valid_for_read and chestitem.name == "blueprint" and (not chestitem.is_blueprint_setup()) then
chestitem.set_blueprint_entities(item.get_blueprint_entities())
chestitem.set_blueprint_tiles(item.get_blueprint_tiles())
chestitem.blueprint_icons = item.blueprint_icons
if item.label then
chestitem.label = item.label
end
if chestitem.label_color then
chestitem.label_color = item.label_color
end
break;
end
end
end
end
end --[[ end for #playerinventory ]] --
else --[[ break if unable to spawn new chest ]] --
if savechest == nil then
break
end
end
end --[[ end for #storeinventories ]] --
if savechest ~= nil then
if savechest.get_inventory(defines.inventory.chest).is_empty() then
savechest.destroy()
end
end
--[[ save craft queue ]] --
if save_craft_queue == true then
local maininventory = player.get_inventory(defines.inventory.player_main)
local toolbar = player.get_inventory(defines.inventory.player_quickbar)
local queue = player.crafting_queue
local craftchestId = 1
local crafttransfered = 0
if maininventory ~= nil and toolbar ~= nil and #queue > 0 then
savechest = spawn_chest(player, "steel-chest")
if savechest ~= nil then
chestitems = 0
--[[ canceled queue mats are dropped to main inventory ]] --
maininventory.clear()
--[[ complete products, even if they are intermediate are dropped into toolbar, if they are placeable - eg. factories for example ]] --
toolbar.clear()
chestinventory = savechest.get_inventory(defines.inventory.chest)
local cnt = player.crafting_queue_size
while cnt > 0 do
local craftitem = queue[cnt]
player.print("Canceling craft of " .. tostring(craftitem.count) .. " piece(s) of " .. craftitem.recipe .. " , index #" .. tostring(craftitem.index))
local cancelparam = { index = craftitem.index, count = craftitem.count }
player.cancel_crafting(cancelparam)
--[[ canceling craft cancels also intermediate crafts ]] --
cnt = player.crafting_queue_size
end
player.print("Storing items from queue to craft chest #" .. tostring(craftchestId))
for j = 1, #maininventory, 1 do
if maininventory[j].valid and maininventory[j].valid_for_read then
local item = maininventory[j]
if chestinventory ~= nill and chestinventory.can_insert(item) then
chestitems = chestitems + 1
chestinventory[chestitems].set_stack(item)
crafttransfered = crafttransfered + 1
else
savechest = spawn_chest(player, "steel-chest")
if savechest ~= nil then
chestitems = 0
chestinventory = savechest.get_inventory(1)
if chestinventory ~= nil then
chestitems = 1
chestinventory[chestitems].set_stack(item)
crafttransfered = crafttransfered + 1
craftchestId = craftchestId + 1
player.print("Storing items from queue to craft chest #" .. tostring(craftchestId))
end
else --[[ break if unable to spawn new chest ]] --
break
end
end
end
end --[[ end for #maininventory ]] --
for j = 1, #toolbar, 1 do
if toolbar[j].valid and toolbar[j].valid_for_read then
local item = toolbar[j]
if chestinventory ~= nil and chestinventory.can_insert(item) then
chestitems = chestitems + 1
chestinventory[chestitems].set_stack(item)
crafttransfered = crafttransfered + 1
else
savechest = spawn_chest(player, "steel-chest")
if savechest ~= nil then
chestitems = 0
chestinventory = savechest.get_inventory(1)
if chestinventory ~= nil then
chestitems = 1
chestinventory[chestitems].set_stack(item)
crafttransfered = crafttransfered + 1
craftchestId = craftchestId + 1
player.print("Storing items from queue to craft chest #" .. tostring(craftchestId))
end
else --[[ break if unable to spawn new chest ]] --
break
end
end
end
end --[[ end for #toolbar ]] --
end
end
local message = "No craft queue items were saved"
if crafttransfered > 0 then
message = "Saved " .. tostring(crafttransfered) .. " craft queue item(s) into " .. tostring(craftchestId) .. " craft box(es)"
end
player.print(message)
end
end
local message = "No stacks were saved"
if transfered > 0 then
message = "Saved " .. tostring(transfered) .. " stack(s) into " .. tostring(chestId) .. " box(es)"
end
player.print(message)
end
end
local function on_pre_player_died(event)
local player = game.players[event.player_index]
player.clean_cursor()
end
Event.register(defines.events.on_pre_player_died, on_pre_player_died)
Event.register(defines.events.on_player_died, on_player_died)