2019-08-18 07:12:12 +02:00
|
|
|
local mod_meta = minetest.get_mod_storage()
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
digtron.layout = {}
|
|
|
|
digtron.adjacent = {}
|
|
|
|
|
2019-08-18 10:23:08 +02:00
|
|
|
------------------------------------------------------------------------------------
|
|
|
|
-- Inventory
|
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
-- indexed by digtron_id, set to true whenever the detached inventory's contents change
|
2019-08-18 10:23:08 +02:00
|
|
|
local dirty_inventories = {}
|
|
|
|
|
2019-08-18 07:12:12 +02:00
|
|
|
local detached_inventory_callbacks = {
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Called when a player wants to move items inside the inventory.
|
|
|
|
-- Return value: number of items allowed to move.
|
|
|
|
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
--allow anything in "main"
|
|
|
|
if to_list == "main" then
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
|
|
|
--only allow fuel items in "fuel"
|
|
|
|
local stack = inv:get_stack(from_list, from_index)
|
|
|
|
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end,
|
|
|
|
|
|
|
|
-- Called when a player wants to put something into the inventory.
|
|
|
|
-- Return value: number of items allowed to put.
|
|
|
|
-- Return value -1: Allow and don't modify item count in inventory.
|
|
|
|
allow_put = function(inv, listname, index, stack, player)
|
|
|
|
-- Only allow fuel items to be placed in fuel
|
|
|
|
if listname == "fuel" then
|
2019-08-18 07:12:12 +02:00
|
|
|
if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
|
|
|
|
return stack:get_count()
|
2019-08-18 21:02:42 +02:00
|
|
|
else
|
|
|
|
return 0
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
2019-08-18 21:02:42 +02:00
|
|
|
end
|
|
|
|
return stack:get_count() -- otherwise, allow all drops
|
|
|
|
end,
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Called when a player wants to take something out of the inventory.
|
|
|
|
-- Return value: number of items allowed to take.
|
|
|
|
-- Return value -1: Allow and don't modify item count in inventory.
|
|
|
|
allow_take = function(inv, listname, index, stack, player)
|
|
|
|
return stack:get_count()
|
|
|
|
end,
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Called after the actual action has happened, according to what was
|
|
|
|
-- allowed.
|
|
|
|
-- No return value.
|
|
|
|
on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
dirty_inventories[inv:get_location().name] = true
|
|
|
|
end,
|
|
|
|
on_put = function(inv, listname, index, stack, player)
|
|
|
|
dirty_inventories[inv:get_location().name] = true
|
|
|
|
end,
|
|
|
|
on_take = function(inv, listname, index, stack, player)
|
|
|
|
dirty_inventories[inv:get_location().name] = true
|
|
|
|
end,
|
|
|
|
}
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 10:23:08 +02:00
|
|
|
-- If the detached inventory doesn't exist, reads saved metadata version of the inventory and creates it
|
|
|
|
-- Doesn't do anything if the detached inventory already exists, the detached inventory is authoritative
|
2019-08-18 23:29:15 +02:00
|
|
|
digtron.retrieve_inventory = function(digtron_id)
|
|
|
|
local inv = minetest.get_inventory({type="detached", name=digtron_id})
|
2019-08-18 10:23:08 +02:00
|
|
|
if inv == nil then
|
2019-08-18 23:29:15 +02:00
|
|
|
inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
|
|
|
local inv_string = mod_meta:get_string("inv_"..digtron_id)
|
2019-08-18 10:23:08 +02:00
|
|
|
if inv_string ~= "" then
|
|
|
|
local inventory_table = minetest.deserialize(inv_string)
|
|
|
|
for listname, invlist in pairs(inventory_table) do
|
|
|
|
inv:set_size(listname, #invlist)
|
|
|
|
inv:set_list(listname, invlist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-08-18 21:02:42 +02:00
|
|
|
return inv
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Stores contents of detached inventory as a metadata string
|
2019-08-18 23:29:15 +02:00
|
|
|
local persist_inventory = function(digtron_id)
|
|
|
|
local inv = minetest.get_inventory({type="detached", name=digtron_id})
|
2019-08-18 10:23:08 +02:00
|
|
|
if inv == nil then
|
|
|
|
minetest.log("error", "[Digtron] persist_inventory attempted to record a nonexistent inventory "
|
2019-08-18 23:29:15 +02:00
|
|
|
.. digtron_id)
|
2019-08-18 10:23:08 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local lists = inv:get_lists()
|
|
|
|
|
|
|
|
local persist = {}
|
|
|
|
for listname, invlist in pairs(lists) do
|
|
|
|
local inventory = {}
|
|
|
|
for i, stack in ipairs(invlist) do
|
|
|
|
table.insert(inventory, stack:to_string()) -- convert into strings for serialization
|
|
|
|
end
|
|
|
|
persist[listname] = inventory
|
|
|
|
end
|
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
mod_meta:set_string("inv_"..digtron_id, minetest.serialize(persist))
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 10:23:08 +02:00
|
|
|
minetest.register_globalstep(function(dtime)
|
2019-08-18 23:29:15 +02:00
|
|
|
for digtron_id, _ in pairs(dirty_inventories) do
|
|
|
|
persist_inventory(digtron_id)
|
|
|
|
dirty_inventories[digtron_id] = nil
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local create_new_id = function()
|
2019-08-18 07:12:12 +02:00
|
|
|
local last_id = mod_meta:get_int("last_id") -- returns 0 when uninitialized, so 0 will never be a valid digtron_id.
|
|
|
|
local new_id = last_id + 1
|
|
|
|
mod_meta:set_int("last_id", new_id) -- ensure each call to this method gets a unique number
|
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
local digtron_id = "digtron_id_" .. tostring(new_id)
|
|
|
|
local inv = minetest.create_detached_inventory(digtron_id, detached_inventory_callbacks)
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
return digtron_id, inv
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 10:23:08 +02:00
|
|
|
-- Deletes a Digtron record. Note: just throws everything away, this is not digtron.deconstruct.
|
2019-08-18 23:29:15 +02:00
|
|
|
local dispose_id = function(digtron_id)
|
|
|
|
minetest.remove_detached_inventory(digtron_id)
|
|
|
|
digtron.layout[digtron_id] = nil
|
|
|
|
digtron.adjacent[digtron_id] = nil
|
|
|
|
mod_meta:set_string("inv_"..digtron_id, "")
|
|
|
|
mod_meta:set_string("layout_"..digtron_id, "")
|
|
|
|
mod_meta:set_string("adjacent_"..digtron_id, "")
|
|
|
|
mod_meta:set_string("name_"..digtron_id, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------------------
|
|
|
|
-- Name
|
|
|
|
|
|
|
|
digtron.get_name = function(digtron_id)
|
|
|
|
return mod_meta:get_string("name_"..digtron_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
digtron.set_name = function(digtron_id, digtron_name)
|
|
|
|
mod_meta:set_string("name_"..digtron_id, digtron_name)
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------------------------------
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Layout
|
2019-08-18 10:23:08 +02:00
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
local get_persist_table_function = function(identifier)
|
2019-08-18 23:29:15 +02:00
|
|
|
return function(digtron_id, tbl)
|
|
|
|
mod_meta:set_string(identifier .."_"..digtron_id, minetest.serialize(tbl))
|
|
|
|
digtron[identifier][digtron_id] = tbl
|
2019-08-18 21:02:42 +02:00
|
|
|
end
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
local get_retrieve_table_function = function(identifier)
|
2019-08-18 23:29:15 +02:00
|
|
|
return function(digtron_id)
|
|
|
|
local current = digtron[identifier][digtron_id]
|
2019-08-18 21:02:42 +02:00
|
|
|
if current then
|
|
|
|
return current
|
|
|
|
end
|
2019-08-18 23:29:15 +02:00
|
|
|
local tbl_string = mod_meta:get_string(identifier.."_"..digtron_id)
|
2019-08-18 21:02:42 +02:00
|
|
|
if tbl_string ~= "" then
|
|
|
|
current = minetest.deserialize(tbl_string)
|
|
|
|
if current then
|
2019-08-18 23:29:15 +02:00
|
|
|
digtron[identifier][digtron_id] = current
|
2019-08-18 21:02:42 +02:00
|
|
|
end
|
|
|
|
return current
|
|
|
|
end
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
local persist_layout = get_persist_table_function("layout")
|
|
|
|
local retrieve_layout = get_retrieve_table_function("layout")
|
|
|
|
local persist_adjacent = get_persist_table_function("adjacent")
|
|
|
|
local retrieve_adjacent = get_retrieve_table_function("adjacent")
|
2019-08-18 07:12:12 +02:00
|
|
|
|
|
|
|
local cardinal_directions = {
|
|
|
|
{x=1,y=0,z=0},
|
|
|
|
{x=-1,y=0,z=0},
|
|
|
|
{x=0,y=1,z=0},
|
|
|
|
{x=0,y=-1,z=0},
|
|
|
|
{x=0,y=0,z=1},
|
|
|
|
{x=0,y=0,z=-1},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- recursive function searches out all connected unassigned digtron nodes
|
|
|
|
local get_all_adjacent_digtron_nodes
|
2019-08-18 21:02:42 +02:00
|
|
|
get_all_adjacent_digtron_nodes = function(pos, digtron_nodes, digtron_adjacent, player_name)
|
2019-08-18 07:12:12 +02:00
|
|
|
for _, dir in ipairs(cardinal_directions) do
|
|
|
|
local test_pos = vector.add(pos, dir)
|
|
|
|
local test_hash = minetest.hash_node_position(test_pos)
|
2019-08-18 21:02:42 +02:00
|
|
|
if not (digtron_nodes[test_hash] or digtron_adjacent[test_hash]) then -- don't test twice
|
2019-08-18 07:12:12 +02:00
|
|
|
local test_node = minetest.get_node(test_pos)
|
|
|
|
local group_value = minetest.get_item_group(test_node.name, "digtron")
|
|
|
|
if group_value > 0 then
|
|
|
|
local meta = minetest.get_meta(test_pos)
|
|
|
|
if meta:contains("digtron_id") then
|
|
|
|
-- Node is part of an existing digtron, don't incorporate it
|
2019-08-18 21:02:42 +02:00
|
|
|
digtron_adjacent[test_hash] = true
|
2019-08-18 07:12:12 +02:00
|
|
|
--elseif TODO test for protected node status using player_name
|
|
|
|
else
|
|
|
|
--test_node.group_value = group_value -- for later ease of reference
|
|
|
|
digtron_nodes[test_hash] = test_node
|
2019-08-18 21:02:42 +02:00
|
|
|
get_all_adjacent_digtron_nodes(test_pos, digtron_nodes, digtron_adjacent, player_name) -- recurse
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- don't record details, the content of this node will change as the digtron moves
|
2019-08-18 21:02:42 +02:00
|
|
|
digtron_adjacent[test_hash] = true
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
--------------------------------------------------------------------------------------------------------
|
|
|
|
-- Construct and deconstruct
|
|
|
|
|
|
|
|
local origin_hash = minetest.hash_node_position({x=0,y=0,z=0})
|
|
|
|
|
2019-08-18 07:12:12 +02:00
|
|
|
-- Returns the id of the new Digtron record, or nil on failure
|
2019-08-18 23:29:15 +02:00
|
|
|
digtron.construct = function(root_pos, player_name)
|
|
|
|
local node = minetest.get_node(root_pos)
|
2019-08-18 07:12:12 +02:00
|
|
|
-- TODO: a more generic test? Not needed with the more generic controller design, as far as I can tell
|
|
|
|
if node.name ~= "digtron:controller" then
|
|
|
|
-- Called on an incorrect node
|
2019-08-18 23:29:15 +02:00
|
|
|
minetest.log("error", "[Digtron] digtron.construct called with pos " .. minetest.pos_to_string(root_pos)
|
2019-08-18 07:12:12 +02:00
|
|
|
.. " but the node at this location was " .. node.name)
|
|
|
|
return nil
|
|
|
|
end
|
2019-08-18 23:29:15 +02:00
|
|
|
local root_meta = minetest.get_meta(root_pos)
|
|
|
|
if root_meta:contains("digtron_id") then
|
2019-08-18 07:12:12 +02:00
|
|
|
-- Already constructed. TODO: validate that the digtron_id actually exists as well
|
2019-08-18 23:29:15 +02:00
|
|
|
minetest.log("error", "[Digtron] digtron.construct called with pos " .. minetest.pos_to_string(root_pos)
|
2019-08-18 07:12:12 +02:00
|
|
|
.. " but the controller at this location was already part of a constructed Digtron.")
|
|
|
|
return nil
|
|
|
|
end
|
2019-08-18 23:29:15 +02:00
|
|
|
local root_hash = minetest.hash_node_position(root_pos)
|
2019-08-18 07:12:12 +02:00
|
|
|
local digtron_nodes = {[root_hash] = node} -- Nodes that are part of Digtron
|
2019-08-18 21:02:42 +02:00
|
|
|
local digtron_adjacent = {} -- Nodes that are adjacent to Digtron but not a part of it
|
2019-08-18 23:29:15 +02:00
|
|
|
get_all_adjacent_digtron_nodes(root_pos, digtron_nodes, digtron_adjacent, player_name)
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
local digtron_id, digtron_inv = create_new_id(root_pos)
|
2019-08-18 07:12:12 +02:00
|
|
|
|
|
|
|
local layout = {}
|
|
|
|
|
|
|
|
for hash, node in pairs(digtron_nodes) do
|
|
|
|
local relative_hash = hash - root_hash + origin_hash
|
2019-08-18 23:29:15 +02:00
|
|
|
local current_meta
|
2019-08-18 07:12:12 +02:00
|
|
|
if hash == root_hash then
|
2019-08-18 23:29:15 +02:00
|
|
|
current_meta = root_meta -- we're processing the controller, we already have a reference to its meta
|
2019-08-18 07:12:12 +02:00
|
|
|
else
|
2019-08-18 23:29:15 +02:00
|
|
|
current_meta = minetest.get_meta(minetest.get_position_from_hash(hash))
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
local current_meta_table = current_meta:to_table()
|
2019-08-18 07:12:12 +02:00
|
|
|
|
2019-08-18 23:29:15 +02:00
|
|
|
if current_meta_table.fields.digtron_id then
|
2019-08-18 07:12:12 +02:00
|
|
|
-- Trying to incorporate part of an existing digtron, should be impossible.
|
|
|
|
minetest.log("error", "[Digtron] digtron.construct tried to incorporate a Digtron node of type "
|
|
|
|
.. node.name .. " at " .. minetest.pos_to_string(minetest.get_position_from_hash(hash))
|
2019-08-18 23:29:15 +02:00
|
|
|
.. " that was already assigned to digtron id " .. current_meta_table.fields.digtron_id)
|
2019-08-18 07:12:12 +02:00
|
|
|
dispose_id(digtron_id)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
-- Process inventories specially
|
|
|
|
-- TODO Builder inventory gets turned into an itemname in a special key in the builder's meta
|
|
|
|
-- fuel and main get added to corresponding detached inventory lists
|
2019-08-18 23:29:15 +02:00
|
|
|
for listname, items in pairs(current_meta_table.inventory) do
|
2019-08-18 07:12:12 +02:00
|
|
|
local count = #items
|
|
|
|
-- increase the corresponding detached inventory size
|
|
|
|
digtron_inv:set_size(listname, digtron_inv:get_size(listname) + count)
|
|
|
|
for _, stack in ipairs(items) do
|
|
|
|
digtron_inv:add_item(listname, stack)
|
|
|
|
end
|
2019-08-18 21:02:42 +02:00
|
|
|
-- erase actual items from stored layout metadata, the detached inventory is authoritative
|
|
|
|
-- store the inventory size so the inventory can be easily recreated
|
2019-08-18 23:29:15 +02:00
|
|
|
current_meta_table.inventory[listname] = #items
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
2019-08-18 21:02:42 +02:00
|
|
|
|
2019-08-18 07:12:12 +02:00
|
|
|
node.param1 = nil -- we don't care about param1, wipe it to save space
|
2019-08-18 23:29:15 +02:00
|
|
|
layout[relative_hash] = {meta = current_meta_table, node = node}
|
2019-08-18 07:12:12 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 23:51:25 +02:00
|
|
|
digtron.set_name(digtron_id, root_meta:get_string("infotext"))
|
2019-08-18 10:23:08 +02:00
|
|
|
persist_inventory(digtron_id)
|
|
|
|
persist_layout(digtron_id, layout)
|
2019-08-18 21:02:42 +02:00
|
|
|
persist_adjacent(digtron_id, digtron_adjacent)
|
2019-08-18 10:23:08 +02:00
|
|
|
|
|
|
|
-- Wipe out the inventories of all in-world nodes, it's stored in the mod_meta now.
|
|
|
|
-- Wait until now to do it in case the above loop fails partway through.
|
|
|
|
for hash, node in pairs(digtron_nodes) do
|
|
|
|
local digtron_meta
|
|
|
|
if hash == root_hash then
|
2019-08-18 23:29:15 +02:00
|
|
|
digtron_meta = root_meta -- we're processing the controller, we already have a reference to its meta
|
2019-08-18 10:23:08 +02:00
|
|
|
else
|
|
|
|
digtron_meta = minetest.get_meta(minetest.get_position_from_hash(hash))
|
|
|
|
end
|
|
|
|
local inv = digtron_meta:get_inventory()
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
for listname, items in pairs(inv:get_lists()) do
|
|
|
|
for i = 1, #items do
|
|
|
|
inv:set_stack(listname, i, ItemStack(""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
digtron_meta:set_string("digtron_id", digtron_id)
|
2019-08-18 10:23:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.debug("constructed id " .. digtron_id)
|
2019-08-18 07:12:12 +02:00
|
|
|
return digtron_id
|
|
|
|
end
|
|
|
|
|
2019-08-19 02:23:51 +02:00
|
|
|
|
|
|
|
-- Returns pos, node, and meta for the digtron node provided the in-world node matches the layout
|
|
|
|
-- returns nil otherwise
|
|
|
|
local get_valid_data = function(digtron_id, root_hash, hash, data, function_name)
|
|
|
|
local ipos = minetest.get_position_from_hash(hash + root_hash - origin_hash)
|
|
|
|
local node = minetest.get_node(ipos)
|
|
|
|
local imeta = minetest.get_meta(ipos)
|
|
|
|
|
|
|
|
if data.node.name ~= node.name then
|
|
|
|
minetest.log("error", "[Digtron] " .. function_name .. " tried interacting with one of ".. digtron_id .. "'s "
|
|
|
|
.. data.node.name .. "s at " .. minetest.pos_to_string(ipos) .. " but the node at that location was of type "
|
|
|
|
.. node.name)
|
|
|
|
elseif imeta:get_string("digtron_id") ~= digtron_id then
|
|
|
|
minetest.log("error", "[Digtron] " .. function_name .. " tried interacting with ".. digtron_id .. "'s "
|
|
|
|
.. data.node.name .. " at " .. minetest.pos_to_string(ipos)
|
|
|
|
.. " but the node at that location had a non-matching digtron_id value of \""
|
|
|
|
.. imeta:get_string("digtron_id") .. "\"")
|
|
|
|
else
|
|
|
|
return ipos, node, imeta
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Turns the Digtron back into pieces
|
|
|
|
digtron.deconstruct = function(digtron_id, root_pos, player_name)
|
2019-08-18 23:29:15 +02:00
|
|
|
local root_meta = minetest.get_meta(root_pos)
|
2019-08-18 23:51:25 +02:00
|
|
|
root_meta:set_string("infotext", digtron.get_name(digtron_id))
|
2019-08-18 23:29:15 +02:00
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
local layout = retrieve_layout(digtron_id)
|
|
|
|
local inv = digtron.retrieve_inventory(digtron_id)
|
2019-08-18 23:29:15 +02:00
|
|
|
local root_hash = minetest.hash_node_position(root_pos)
|
2019-08-18 10:23:08 +02:00
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
-- Write metadata and inventory to in-world node at this location
|
|
|
|
for hash, data in pairs(layout) do
|
2019-08-19 02:23:51 +02:00
|
|
|
local ipos, node, imeta = get_valid_data(digtron_id, root_hash, hash, data, "digtron.deconstruct")
|
|
|
|
|
|
|
|
if ipos then
|
2019-08-18 21:02:42 +02:00
|
|
|
local iinv = imeta:get_inventory()
|
|
|
|
for listname, size in pairs(data.meta.inventory) do
|
|
|
|
iinv:set_size(listname, size)
|
|
|
|
for i, itemstack in ipairs(inv:get_list(listname)) do
|
|
|
|
-- add everything, putting leftovers back in the main inventory
|
|
|
|
inv:set_stack(listname, i, iinv:add_item(listname, itemstack))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: special handling for builder node inventories
|
|
|
|
|
|
|
|
-- Ensure node metadata fields are all set, too
|
|
|
|
for field, value in pairs(data.meta.fields) do
|
|
|
|
imeta:set_string(field, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Clear digtron_id, this node is no longer part of an active digtron
|
|
|
|
imeta:set_string("digtron_id", "")
|
|
|
|
end
|
|
|
|
end
|
2019-08-18 10:23:08 +02:00
|
|
|
|
|
|
|
dispose_id(digtron_id)
|
2019-08-18 21:02:42 +02:00
|
|
|
end
|
|
|
|
|
2019-08-19 02:23:51 +02:00
|
|
|
-- Removes the in-world nodes of a digtron
|
|
|
|
-- Does not destroy its layout info
|
|
|
|
digtron.remove_from_world = function(digtron_id, root_pos, player_name)
|
|
|
|
local layout = retrieve_layout(digtron_id)
|
|
|
|
local root_hash = minetest.hash_node_position(root_pos)
|
|
|
|
local nodes_to_destroy = {}
|
|
|
|
for hash, data in pairs(layout) do
|
|
|
|
local ipos, node, imeta = get_valid_data(digtron_id, root_hash, hash, data, "digtron.destroy")
|
|
|
|
if ipos then
|
|
|
|
table.insert(nodes_to_destroy, ipos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: voxelmanip might be better here?
|
|
|
|
minetest.bulk_set_node(nodes_to_destroy, {name="air"})
|
|
|
|
end
|
|
|
|
|
|
|
|
digtron.build_to_world = function(digtron_id, root_pos, player_name)
|
|
|
|
local layout = retrieve_layout(digtron_id)
|
|
|
|
local root_hash = minetest.hash_node_position(root_pos)
|
|
|
|
local nodes_to_create = {}
|
|
|
|
|
|
|
|
local permitted = true
|
|
|
|
for hash, data in pairs(layout) do
|
|
|
|
local ipos = minetest.get_position_from_hash(hash + root_hash - origin_hash)
|
|
|
|
local node = minetest.get_node(ipos)
|
|
|
|
local node_def = minetest.registered_nodes[node.name]
|
|
|
|
-- TODO: lots of testing needed here
|
|
|
|
if not (node_def and node_def.buildable_to) then
|
|
|
|
minetest.chat_send_all("not permitted due to " .. node.name .. " at " .. minetest.pos_to_string(ipos))
|
|
|
|
permitted = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if permitted then
|
|
|
|
-- TODO: voxelmanip might be better here, less likely than with destroy though since metadata needs to be written
|
|
|
|
for hash, data in pairs(layout) do
|
|
|
|
local ipos = minetest.get_position_from_hash(hash + root_hash - origin_hash)
|
|
|
|
minetest.set_node(ipos, data.node)
|
|
|
|
local meta = minetest.get_meta(ipos)
|
|
|
|
meta:set_string("digtron_id", digtron_id)
|
|
|
|
for field, value in pairs(data.meta.fields) do
|
|
|
|
meta:set_string(field, value)
|
|
|
|
end
|
|
|
|
-- Not needed - local inventories not used by active digtron, will be restored if deconstructed
|
|
|
|
-- local inv = meta:get_inventory()
|
|
|
|
-- for listname, size in pairs(data.meta.inventory) do
|
|
|
|
-- inv:set_size(listname, size)
|
|
|
|
-- end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-19 02:37:47 +02:00
|
|
|
return permitted
|
2019-08-19 02:23:51 +02:00
|
|
|
end
|
|
|
|
|
2019-08-18 21:02:42 +02:00
|
|
|
---------------------------------------------------------------------------------
|
|
|
|
-- Misc
|
|
|
|
|
|
|
|
digtron.can_dig = function(pos, digger)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if meta:get_string("digtron_id") ~= "" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|