2018-04-07 18:32:41 +00:00
|
|
|
-- Storage disks
|
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks = {}
|
|
|
|
holostorage.disks.memcache = {}
|
2018-04-07 18:32:41 +00:00
|
|
|
|
|
|
|
local function inv_to_table(inv)
|
|
|
|
local t = {}
|
|
|
|
for listname, list in pairs(inv:get_lists()) do
|
|
|
|
local size = inv:get_size(listname)
|
|
|
|
if size then
|
|
|
|
t[listname] = {}
|
|
|
|
for i = 1, size, 1 do
|
|
|
|
t[listname][i] = inv:get_stack(listname, i):to_table()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
local function table_to_inv(inv, t)
|
|
|
|
for listname, list in pairs(t) do
|
|
|
|
for i, stack in pairs(list) do
|
|
|
|
inv:set_stack(listname, i, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.register_disk(index, desc, capacity)
|
2018-04-07 18:32:41 +00:00
|
|
|
local mod = minetest.get_current_modname()
|
|
|
|
minetest.register_craftitem(mod..":storage_disk"..index, {
|
|
|
|
description = desc.."\nStores "..capacity.." Stacks",
|
2018-04-08 14:11:39 +00:00
|
|
|
inventory_image = "holostorage_disk"..index..".png",
|
|
|
|
groups = {holostorage_disk = 1},
|
|
|
|
holostorage_capacity = capacity,
|
|
|
|
holostorage_name = "disk"..index,
|
2018-04-07 18:32:41 +00:00
|
|
|
stack_max = 1,
|
|
|
|
on_secondary_use = function (itemstack, user, pointed_thing)
|
|
|
|
return stack
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
local function create_invref(ptr, capacity)
|
|
|
|
local inv = minetest.create_detached_inventory(ptr, {})
|
|
|
|
inv:set_size("main", capacity)
|
|
|
|
return inv
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.ensure_disk_inventory(stack, pstr)
|
2018-04-08 09:39:06 +00:00
|
|
|
local meta = stack:get_meta()
|
|
|
|
local tag = meta:get_string("storage_tag")
|
2018-04-08 14:11:39 +00:00
|
|
|
local cap = minetest.registered_items[stack:get_name()].holostorage_capacity
|
2018-04-08 09:39:06 +00:00
|
|
|
|
|
|
|
if not tag or tag == "" then
|
|
|
|
local rnd = PseudoRandom(os.clock())
|
|
|
|
local rndint = rnd.next(rnd)
|
|
|
|
local diskid = "d"..pstr.."-"..rndint
|
|
|
|
meta:set_string("storage_tag", diskid)
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks.memcache[diskid] = create_invref(diskid, cap)
|
2018-04-08 09:39:06 +00:00
|
|
|
end
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
return stack
|
|
|
|
end
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.load_disk_from_file(stack, diskptr)
|
2018-04-08 09:39:06 +00:00
|
|
|
local world = minetest.get_worldpath()
|
2018-04-08 14:11:39 +00:00
|
|
|
local directory = world.."/holostorage"
|
|
|
|
local cap = minetest.registered_items[stack:get_name()].holostorage_capacity
|
2018-04-08 09:39:06 +00:00
|
|
|
local inv = create_invref(diskptr, cap)
|
|
|
|
minetest.mkdir(directory)
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
local filetag = minetest.sha1(diskptr)..".invref"
|
|
|
|
local file = io.open(directory.."/"..filetag)
|
|
|
|
|
|
|
|
if not file then
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks.memcache[diskptr] = inv
|
2018-04-08 09:39:06 +00:00
|
|
|
return diskptr
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
local str = ""
|
|
|
|
for line in file:lines() do
|
|
|
|
str = str..line
|
|
|
|
end
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
file:close()
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
table_to_inv(inv, minetest.deserialize(str))
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks.memcache[diskptr] = inv
|
2018-04-08 09:39:06 +00:00
|
|
|
return diskptr
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.save_disk_to_file(diskptr)
|
|
|
|
if not holostorage.disks.memcache[diskptr] then return nil end
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
local world = minetest.get_worldpath()
|
2018-04-08 14:11:39 +00:00
|
|
|
local directory = world.."/holostorage"
|
2018-04-08 09:39:06 +00:00
|
|
|
local filetag = minetest.sha1(diskptr)..".invref"
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
minetest.mkdir(directory)
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
local inv = holostorage.disks.memcache[diskptr]
|
2018-04-08 09:39:06 +00:00
|
|
|
local data = minetest.serialize(inv_to_table(inv))
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
minetest.safe_file_write(directory.."/"..filetag, data)
|
|
|
|
return diskptr
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.save_disks_to_file()
|
|
|
|
for diskptr in pairs(holostorage.disks.memcache) do
|
|
|
|
holostorage.disks.save_disk_to_file(diskptr)
|
2018-04-08 09:39:06 +00:00
|
|
|
end
|
|
|
|
end
|
2018-04-07 18:32:41 +00:00
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
-- Make sure stack is disk
|
2018-04-08 14:11:39 +00:00
|
|
|
function holostorage.disks.is_valid_disk(stack)
|
2018-04-08 09:39:06 +00:00
|
|
|
local stack_name = stack:get_name()
|
2018-04-08 14:11:39 +00:00
|
|
|
return minetest.get_item_group(stack_name, "holostorage_disk") > 0
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|
|
|
|
|
2018-04-08 09:39:06 +00:00
|
|
|
-- Save disks on shutdown
|
|
|
|
minetest.register_on_shutdown(function ()
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks.save_disks_to_file()
|
2018-04-08 09:39:06 +00:00
|
|
|
end)
|
|
|
|
|
2018-04-07 18:32:41 +00:00
|
|
|
local capacities = {1000, 8000, 16000, 32000, 64000}
|
|
|
|
local descriptions = {"1K Disk", "8K Disk", "16K Disk", "32K Disk", "64K Disk"}
|
|
|
|
for i = 1, 5 do
|
2018-04-08 14:11:39 +00:00
|
|
|
holostorage.disks.register_disk(i, descriptions[i], capacities[i])
|
2018-04-07 18:32:41 +00:00
|
|
|
end
|