2017-01-10 08:29:09 +01:00
DigtronLayout = { }
DigtronLayout.__index = DigtronLayout
2017-10-13 06:53:30 +02:00
local modpath_awards = minetest.get_modpath ( " awards " )
2017-01-10 08:29:09 +01:00
-------------------------------------------------------------------------
-- Creation
local get_node_image = function ( pos , node )
local node_image = { node = node , pos = { x = pos.x , y = pos.y , z = pos.z } }
2017-09-20 07:13:31 +02:00
local node_def = minetest.registered_nodes [ node.name ]
node_image.paramtype2 = node_def.paramtype2
2017-01-10 08:29:09 +01:00
local meta = minetest.get_meta ( pos )
node_image.meta = meta : to_table ( )
2017-10-15 08:44:12 +02:00
if node_image.meta ~= nil and node_def._digtron_formspec ~= nil then
2017-10-22 06:29:18 +02:00
node_image.meta . fields.formspec = node_def._digtron_formspec ( pos , meta ) -- causes formspec to be automatically upgraded whenever Digtron moves
2017-09-20 07:13:31 +02:00
end
2017-01-10 08:29:09 +01:00
2019-01-13 11:02:02 +01:00
local group = minetest.get_item_group ( node.name , " digtron " )
-- group 1 has no special metadata
if group > 1 and group < 10 then
assert ( node_image ~= nil and node_image.meta ~= nil , " [Digtron] Digtron failed to get a metadata table for a Digtron node in group "
.. tostring ( group ) .. " . This error should not be possible. Please see https://github.com/minetest/minetest/issues/8067 " )
-- These groups have inventories
if group == 2 or ( group > 3 and group < 8 ) then
assert ( node_image.meta . inventory ~= nil , " [Digtron] Digtron failed to get a metadata inventory table for a Digtron node in group "
.. tostring ( group ) .. " . This error should not be possible. Please see https://github.com/minetest/minetest/issues/8067 " )
end
end
2017-01-10 08:29:09 +01:00
-- Record what kind of thing we've got in a builder node so its facing can be rotated properly
2019-01-13 11:02:02 +01:00
if group == 4 then
2018-05-09 10:48:01 +02:00
local build_item = " "
if node_image.meta . inventory.main then
build_item = node_image.meta . inventory.main [ 1 ]
end
2018-01-07 21:38:25 +01:00
if build_item ~= " " then
local build_item_def = minetest.registered_nodes [ ItemStack ( build_item ) : get_name ( ) ]
if build_item_def ~= nil then
node_image.build_item_paramtype2 = build_item_def.paramtype2
2017-01-12 08:50:17 +01:00
end
2017-01-10 08:29:09 +01:00
end
end
return node_image
end
2019-01-09 08:20:02 +01:00
-- temporary pointsets used while searching
local to_test = Pointset.create ( )
local tested = Pointset.create ( )
2017-01-10 08:29:09 +01:00
function DigtronLayout . create ( pos , player )
local self = { }
2017-01-16 00:12:07 +01:00
setmetatable ( self , DigtronLayout )
2017-01-10 08:29:09 +01:00
--initialize. We're assuming that the start position is a controller digtron, should be a safe assumption since only the controller node should call this
self.traction = 0
self.all = { }
self.water_touching = false
self.lava_touching = false
self.protected = Pointset.create ( ) -- if any nodes we look at are protected, make note of that. That way we don't need to keep re-testing protection state later.
self.old_pos_pointset = Pointset.create ( ) -- For tracking original location of nodes if we do transformations on the Digtron
self.nodes_dug = Pointset.create ( ) -- For tracking adjacent nodes that will have been dug by digger heads in future
self.contains_protected_node = false -- used to indicate if at least one node in this digtron array is protected from the player.
self.controller = { x = pos.x , y = pos.y , z = pos.z } --Make a deep copy of the pos parameter just in case the calling code wants to play silly buggers with it
2017-02-05 07:08:38 +01:00
table.insert ( self.all , get_node_image ( pos , minetest.get_node ( pos ) ) ) -- We never visit the source node, so insert it into the all table a priori. Revisit this design decision if a controller node is created that contains fuel or inventory or whatever.
2017-01-10 08:29:09 +01:00
2019-01-13 11:02:02 +01:00
self.extents_max_x = pos.x
self.extents_min_x = pos.x
self.extents_max_y = pos.y
self.extents_min_y = pos.y
self.extents_max_z = pos.z
self.extents_min_z = pos.z
2017-01-10 08:29:09 +01:00
tested : set ( pos.x , pos.y , pos.z , true )
to_test : set ( pos.x + 1 , pos.y , pos.z , true )
to_test : set ( pos.x - 1 , pos.y , pos.z , true )
to_test : set ( pos.x , pos.y + 1 , pos.z , true )
to_test : set ( pos.x , pos.y - 1 , pos.z , true )
to_test : set ( pos.x , pos.y , pos.z + 1 , true )
to_test : set ( pos.x , pos.y , pos.z - 1 , true )
if minetest.is_protected ( pos , player : get_player_name ( ) ) and not minetest.check_player_privs ( player , " protection_bypass " ) then
self.protected : set ( pos.x , pos.y , pos.z , true )
self.contains_protected_node = true
end
-- Do a loop on to_test positions, adding new to_test positions as we find digtron nodes. This is a flood fill operation
-- that follows node faces (no diagonals)
local testpos , _ = to_test : pop ( )
while testpos ~= nil do
tested : set ( testpos.x , testpos.y , testpos.z , true ) -- track nodes we've looked at to prevent infinite loops
local node = minetest.get_node ( testpos )
if node.name == " ignore " then
2019-01-14 00:02:41 +01:00
--digtron array is next to unloaded nodes, too dangerous to do anything. Abort.
self.ignore_touching = true
2017-01-10 08:29:09 +01:00
end
if minetest.get_item_group ( node.name , " water " ) ~= 0 then
self.water_touching = true
elseif minetest.get_item_group ( node.name , " lava " ) ~= 0 then
self.lava_touching = true
2017-09-12 08:20:52 +02:00
if digtron.config . lava_impassible then
2017-01-10 08:29:09 +01:00
self.protected : set ( testpos.x , testpos.y , testpos.z , true )
end
end
2017-10-01 18:19:14 +02:00
local is_protected = false
if minetest.is_protected ( testpos , player : get_player_name ( ) ) and not minetest.check_player_privs ( player , " protection_bypass " ) then
self.protected : set ( testpos.x , testpos.y , testpos.z , true )
is_protected = true
end
2017-01-10 08:29:09 +01:00
local group_number = minetest.get_item_group ( node.name , " digtron " )
if group_number > 0 then
--found one. Add it to the digtrons output
local node_image = get_node_image ( testpos , node )
table.insert ( self.all , node_image )
-- add a reference to this node's position to special node lists
if group_number == 2 then
2019-01-05 06:06:12 +01:00
self.inventories = self.inventories or { }
2017-01-10 08:29:09 +01:00
table.insert ( self.inventories , node_image )
elseif group_number == 3 then
2019-01-05 06:06:12 +01:00
self.diggers = self.diggers or { }
2017-01-10 08:29:09 +01:00
table.insert ( self.diggers , node_image )
elseif group_number == 4 then
2019-01-05 06:06:12 +01:00
self.builders = self.builders or { }
2017-01-10 08:29:09 +01:00
table.insert ( self.builders , node_image )
elseif group_number == 5 then
2019-01-05 06:06:12 +01:00
self.fuelstores = self.fuelstores or { }
2017-01-10 08:29:09 +01:00
table.insert ( self.fuelstores , node_image )
elseif group_number == 6 then
2019-01-05 06:06:12 +01:00
self.inventories = self.inventories or { }
self.fuelstores = self.fuelstores or { }
2017-01-10 08:29:09 +01:00
table.insert ( self.inventories , node_image )
table.insert ( self.fuelstores , node_image )
2019-01-05 06:06:12 +01:00
elseif group_number == 7 then -- technic batteries
self.battery_holders = self.battery_holders or { }
2017-10-10 21:02:35 +02:00
table.insert ( self.battery_holders , node_image )
2017-10-12 11:06:24 +02:00
elseif group_number == 8 then
2019-01-05 06:06:12 +01:00
self.power_connectors = self.power_connectors or { }
table.insert ( self.power_connectors , node_image ) -- technic power connectors
2017-10-22 06:29:18 +02:00
elseif group_number == 9 and node_image.meta . fields [ " autoeject " ] == " true " then
2019-01-05 06:06:12 +01:00
self.auto_ejectors = self.auto_ejectors or { }
2017-10-22 06:29:18 +02:00
table.insert ( self.auto_ejectors , node_image )
2017-01-10 08:29:09 +01:00
end
2017-10-01 18:19:14 +02:00
if is_protected then
2017-01-10 08:29:09 +01:00
self.contains_protected_node = true
end
-- update extents
2019-01-13 11:02:02 +01:00
self.extents_max_x = math.max ( self.extents_max_x , testpos.x )
self.extents_min_x = math.min ( self.extents_min_x , testpos.x )
self.extents_max_y = math.max ( self.extents_max_y , testpos.y )
self.extents_min_y = math.min ( self.extents_min_y , testpos.y )
self.extents_max_z = math.max ( self.extents_max_z , testpos.z )
self.extents_min_z = math.min ( self.extents_min_z , testpos.z )
2017-01-10 08:29:09 +01:00
--queue up potential new test points adjacent to this digtron node
to_test : set_if_not_in ( tested , testpos.x + 1 , testpos.y , testpos.z , true )
to_test : set_if_not_in ( tested , testpos.x - 1 , testpos.y , testpos.z , true )
to_test : set_if_not_in ( tested , testpos.x , testpos.y + 1 , testpos.z , true )
to_test : set_if_not_in ( tested , testpos.x , testpos.y - 1 , testpos.z , true )
to_test : set_if_not_in ( tested , testpos.x , testpos.y , testpos.z + 1 , true )
to_test : set_if_not_in ( tested , testpos.x , testpos.y , testpos.z - 1 , true )
2017-10-13 06:53:30 +02:00
elseif not minetest.registered_nodes [ node.name ] or minetest.registered_nodes [ node.name ] . buildable_to ~= true then
2017-01-10 08:29:09 +01:00
-- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction.
2017-10-13 06:53:30 +02:00
-- Allowing unknown nodes to provide traction, since they're not buildable_to either
2017-01-10 08:29:09 +01:00
self.traction = self.traction + 1
end
testpos , _ = to_test : pop ( )
end
2017-01-19 07:42:36 +01:00
2018-12-24 03:06:49 +01:00
digtron.award_layout ( self , player ) -- hook for achievements mod
2017-01-19 07:42:36 +01:00
2019-01-09 08:20:02 +01:00
to_test : clear ( )
tested : clear ( )
2017-01-10 08:29:09 +01:00
return self
end
------------------------------------------------------------------------
-- Rotation
2017-01-08 09:23:10 +01:00
local facedir_rotate = {
[ ' x ' ] = {
[ - 1 ] = { [ 0 ] = 4 , 5 , 6 , 7 , 22 , 23 , 20 , 21 , 0 , 1 , 2 , 3 , 13 , 14 , 15 , 12 , 19 , 16 , 17 , 18 , 10 , 11 , 8 , 9 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 8 , 9 , 10 , 11 , 0 , 1 , 2 , 3 , 22 , 23 , 20 , 21 , 15 , 12 , 13 , 14 , 17 , 18 , 19 , 16 , 6 , 7 , 4 , 5 } , -- 90 degrees
} ,
[ ' y ' ] = {
[ - 1 ] = { [ 0 ] = 3 , 0 , 1 , 2 , 19 , 16 , 17 , 18 , 15 , 12 , 13 , 14 , 7 , 4 , 5 , 6 , 11 , 8 , 9 , 10 , 21 , 22 , 23 , 20 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 1 , 2 , 3 , 0 , 13 , 14 , 15 , 12 , 17 , 18 , 19 , 16 , 9 , 10 , 11 , 8 , 5 , 6 , 7 , 4 , 23 , 20 , 21 , 22 } , -- 90 degrees
} ,
[ ' z ' ] = {
[ - 1 ] = { [ 0 ] = 16 , 17 , 18 , 19 , 5 , 6 , 7 , 4 , 11 , 8 , 9 , 10 , 0 , 1 , 2 , 3 , 20 , 21 , 22 , 23 , 12 , 13 , 14 , 15 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 12 , 13 , 14 , 15 , 7 , 4 , 5 , 6 , 9 , 10 , 11 , 8 , 20 , 21 , 22 , 23 , 0 , 1 , 2 , 3 , 16 , 17 , 18 , 19 } , -- 90 degrees
}
}
local wallmounted_rotate = {
[ ' x ' ] = {
[ - 1 ] = { [ 0 ] = 4 , 5 , 2 , 3 , 1 , 0 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 5 , 4 , 2 , 3 , 0 , 1 } , -- 90 degrees
} ,
[ ' y ' ] = {
[ - 1 ] = { [ 0 ] = 0 , 1 , 4 , 5 , 3 , 2 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 0 , 1 , 5 , 4 , 2 , 3 } , -- 90 degrees
} ,
[ ' z ' ] = {
[ - 1 ] = { [ 0 ] = 3 , 2 , 0 , 1 , 4 , 5 } , -- 270 degrees
[ 1 ] = { [ 0 ] = 2 , 3 , 1 , 0 , 4 , 5 } , -- 90 degrees
}
}
2019-01-05 06:06:12 +01:00
--90 degrees CW about x-axis: (x, y, z) -> (x, -z, y)
--90 degrees CCW about x-axis: (x, y, z) -> (x, z, -y)
--90 degrees CW about y-axis: (x, y, z) -> (-z, y, x)
--90 degrees CCW about y-axis: (x, y, z) -> (z, y, -x)
--90 degrees CW about z-axis: (x, y, z) -> (y, -x, z)
--90 degrees CCW about z-axis: (x, y, z) -> (-y, x, z)
-- operates directly on the pos vector
2017-01-08 09:23:10 +01:00
local rotate_pos = function ( axis , direction , pos )
if axis == " x " then
if direction < 0 then
2019-01-05 06:06:12 +01:00
local temp_z = pos.z
pos.z = pos.y
pos.y = - temp_z
2017-01-08 09:23:10 +01:00
else
2019-01-05 06:06:12 +01:00
local temp_z = pos.z
pos.z = - pos.y
pos.y = temp_z
2017-01-08 09:23:10 +01:00
end
elseif axis == " y " then
if direction < 0 then
2019-01-05 06:06:12 +01:00
local temp_x = pos.x
pos.x = - pos.z
pos.z = temp_x
2017-01-08 09:23:10 +01:00
else
2019-01-05 06:06:12 +01:00
local temp_x = pos.x
pos.x = pos.z
pos.z = - temp_x
2017-01-08 09:23:10 +01:00
end
else
if direction < 0 then
2019-01-05 06:06:12 +01:00
local temp_x = pos.x
pos.x = - pos.y
pos.y = temp_x
2017-01-08 09:23:10 +01:00
else
2019-01-05 06:06:12 +01:00
local temp_x = pos.x
pos.x = pos.y
pos.y = - temp_x
2017-01-08 09:23:10 +01:00
end
end
2019-01-05 06:06:12 +01:00
return pos
end
-- operates directly on the pos vector
local subtract_in_place = function ( pos , subtract )
pos.x = pos.x - subtract.x
pos.y = pos.y - subtract.y
pos.z = pos.z - subtract.z
return pos
end
local add_in_place = function ( pos , add )
pos.x = pos.x + add.x
pos.y = pos.y + add.y
pos.z = pos.z + add.z
return pos
2017-01-08 09:23:10 +01:00
end
local rotate_node_image = function ( node_image , origin , axis , direction , old_pos_pointset )
-- Facings
if node_image.paramtype2 == " wallmounted " then
node_image.node . param2 = wallmounted_rotate [ axis ] [ direction ] [ node_image.node . param2 ]
elseif node_image.paramtype2 == " facedir " then
node_image.node . param2 = facedir_rotate [ axis ] [ direction ] [ node_image.node . param2 ]
end
if node_image.build_item_paramtype2 == " wallmounted " then
2017-01-08 09:50:07 +01:00
node_image.meta . fields.build_facing = wallmounted_rotate [ axis ] [ direction ] [ tonumber ( node_image.meta . fields.build_facing ) ]
2017-01-08 09:23:10 +01:00
elseif node_image.build_item_paramtype2 == " facedir " then
2017-01-08 09:50:07 +01:00
node_image.meta . fields.build_facing = facedir_rotate [ axis ] [ direction ] [ tonumber ( node_image.meta . fields.build_facing ) ]
2017-01-08 09:23:10 +01:00
end
node_image.meta . fields.waiting = nil -- If we're rotating a controller that's in the "waiting" state, clear it. Otherwise it may stick like that.
-- record the old location so we can destroy the old node if the rotation operation is possible
old_pos_pointset : set ( node_image.pos . x , node_image.pos . y , node_image.pos . z , true )
-- position in space relative to origin
2019-01-05 06:06:12 +01:00
local pos = subtract_in_place ( node_image.pos , origin )
2017-01-08 09:23:10 +01:00
pos = rotate_pos ( axis , direction , pos )
-- Move back to original reference frame
2019-01-05 06:06:12 +01:00
node_image.pos = add_in_place ( pos , origin )
2017-01-08 09:23:10 +01:00
return node_image
end
2019-01-05 06:06:12 +01:00
local top = {
[ 0 ] = { axis = " y " , dir =- 1 } ,
{ axis = " z " , dir = 1 } ,
{ axis = " z " , dir =- 1 } ,
{ axis = " x " , dir = 1 } ,
{ axis = " x " , dir =- 1 } ,
{ axis = " y " , dir = 1 } ,
}
2017-01-09 07:52:36 +01:00
-- Rotates 90 degrees widdershins around the axis defined by facedir (which in this case is pointing out the front of the node, so it needs to be converted into an upward-pointing axis internally)
2017-01-10 08:29:09 +01:00
function DigtronLayout . rotate_layout_image ( self , facedir )
2019-01-13 11:02:02 +01:00
if self == nil or self.all == nil or self.controller == nil or self.old_pos_pointset == nil then
-- this should not be possible, but if it is then abort.
return false
end
2017-01-13 09:17:05 +01:00
-- To convert this into the direction the "top" of the axle node is pointing in:
2017-01-08 09:23:10 +01:00
-- 0, 1, 2, 3 == (0,1,0)
-- 4, 5, 6, 7 == (0,0,1)
-- 8, 9, 10, 11 == (0,0,-1)
-- 12, 13, 14, 15 == (1,0,0)
-- 16, 17, 18, 19 == (-1,0,0)
-- 20, 21, 22, 23== (0,-1,0)
local params = top [ math.floor ( facedir / 4 ) ]
2017-01-10 08:29:09 +01:00
for k , node_image in pairs ( self.all ) do
rotate_node_image ( node_image , self.controller , params.axis , params.dir , self.old_pos_pointset )
2017-01-08 09:23:10 +01:00
end
2017-01-10 08:29:09 +01:00
return self
2017-01-08 09:23:10 +01:00
end
2017-01-10 08:29:09 +01:00
-----------------------------------------------------------------------------------------------
-- Translation
2017-01-15 00:58:37 +01:00
function DigtronLayout . move_layout_image ( self , dir )
2019-01-13 11:02:02 +01:00
self.extents_max_x = self.extents_max_x + dir.x
self.extents_min_x = self.extents_min_x + dir.x
self.extents_max_y = self.extents_max_y + dir.y
self.extents_min_y = self.extents_min_y + dir.y
self.extents_max_z = self.extents_max_z + dir.z
self.extents_min_z = self.extents_min_z + dir.z
2017-01-15 00:58:37 +01:00
2017-01-10 08:29:09 +01:00
for k , node_image in pairs ( self.all ) do
self.old_pos_pointset : set ( node_image.pos . x , node_image.pos . y , node_image.pos . z , true )
2019-01-05 06:06:12 +01:00
node_image.pos = add_in_place ( node_image.pos , dir )
2017-01-10 08:29:09 +01:00
self.nodes_dug : set ( node_image.pos . x , node_image.pos . y , node_image.pos . z , false ) -- we've moved a digtron node into this space, mark it so that we don't dig it.
2017-01-09 07:52:36 +01:00
end
end
2017-01-10 08:29:09 +01:00
-----------------------------------------------------------------------------------------------
-- Writing to world
function DigtronLayout . can_write_layout_image ( self )
for k , node_image in pairs ( self.all ) do
2017-01-09 09:19:35 +01:00
--check if we're moving into a protected node
2017-01-12 09:10:14 +01:00
if self.protected : get ( node_image.pos . x , node_image.pos . y , node_image.pos . z ) then
return false
end
-- check if the target node is buildable_to or is marked as part of the digtron that's moving
if not (
self.old_pos_pointset : get ( node_image.pos . x , node_image.pos . y , node_image.pos . z )
or minetest.registered_nodes [ minetest.get_node ( node_image.pos ) . name ] . buildable_to
) then
2017-01-08 09:23:10 +01:00
return false
end
end
return true
end
2018-01-07 21:38:25 +01:00
-- We need to call on_dignode and on_placenode for dug and placed nodes,
-- but that triggers falling nodes (sand and whatnot) and destroys Digtrons
-- if done during mid-write. So we need to defer the calls until after the
-- Digtron has been fully written.
2017-10-15 08:33:54 +02:00
2019-01-05 06:06:12 +01:00
-- using local counters and shared tables like this allows us to avoid some needless allocating and garbage-collecting of tables
local dug_nodes_count = 0
local dug_node_pos = { }
local dug_node = { }
local dug_node_meta = { }
local placed_nodes_count = 0
local placed_node_pos = { }
local placed_new_node = { }
local placed_old_node = { }
local node_callbacks = function ( player )
if dug_nodes_count > 0 then
for i = 1 , dug_nodes_count do
local old_pos = dug_node_pos [ i ]
local old_node = dug_node [ i ]
local old_meta = dug_node_meta [ i ]
2017-10-15 08:33:54 +02:00
2019-01-05 06:06:12 +01:00
for _ , callback in ipairs ( minetest.registered_on_dignodes ) do
-- Copy pos and node because callback can modify them
local pos_copy = { x = old_pos.x , y = old_pos.y , z = old_pos.z }
local oldnode_copy = { name = old_node.name , param1 = old_node.param1 , param2 = old_node.param2 }
callback ( pos_copy , oldnode_copy , digtron.fake_player )
end
local old_def = minetest.registered_nodes [ old_node.name ]
if old_def ~= nil and old_def.after_dig_node ~= nil then
old_def.after_dig_node ( old_pos , old_node , old_meta , player )
end
2017-10-12 11:06:24 +02:00
end
2019-01-05 06:06:12 +01:00
end
2017-10-12 11:06:24 +02:00
2019-01-05 06:06:12 +01:00
if placed_nodes_count > 0 then
for i = 1 , placed_nodes_count do
local new_pos = placed_node_pos [ i ]
local new_node = placed_new_node [ i ]
local old_node = placed_old_node [ i ]
for _ , callback in ipairs ( minetest.registered_on_placenodes ) do
-- Copy pos and node because callback can modify them
local pos_copy = { x = new_pos.x , y = new_pos.y , z = new_pos.z }
local oldnode_copy = { name = old_node.name , param1 = old_node.param1 , param2 = old_node.param2 }
local newnode_copy = { name = new_node.name , param1 = new_node.param1 , param2 = new_node.param2 }
callback ( pos_copy , newnode_copy , digtron.fake_player , oldnode_copy )
end
local new_def = minetest.registered_nodes [ new_node.name ]
if new_def ~= nil and new_def.after_place_node ~= nil then
new_def.after_place_node ( new_pos , player )
end
2017-01-08 09:23:10 +01:00
end
2018-01-07 21:38:25 +01:00
end
end
2017-10-15 08:33:54 +02:00
2018-01-07 21:38:25 +01:00
local set_node_with_retry = function ( pos , node )
local start_time = minetest.get_us_time ( )
while not minetest.set_node ( pos , node ) do
if minetest.get_us_time ( ) - start_time > 1000000 then -- 1 second in useconds
return false
end
end
return true
end
local set_meta_with_retry = function ( meta , meta_table )
local start_time = minetest.get_us_time ( )
while not meta : from_table ( meta_table ) do
if minetest.get_us_time ( ) - start_time > 1000000 then -- 1 second in useconds
return false
end
2017-01-08 09:23:10 +01:00
end
2018-01-07 21:38:25 +01:00
return true
end
2019-01-05 06:06:12 +01:00
local air_node = { name = " air " }
2018-01-07 21:38:25 +01:00
function DigtronLayout . write_layout_image ( self , player )
-- destroy the old digtron
local oldpos , _ = self.old_pos_pointset : pop ( )
while oldpos ~= nil do
local old_node = minetest.get_node ( oldpos )
2020-07-31 18:33:09 +02:00
local old_meta = minetest.get_meta ( oldpos ) : to_table ( )
2018-01-07 21:38:25 +01:00
2019-01-05 06:06:12 +01:00
if not set_node_with_retry ( oldpos , air_node ) then
2018-01-07 21:38:25 +01:00
minetest.log ( " error " , " DigtronLayout.write_layout_image failed to destroy old Digtron node, aborting write. " )
return false
end
2019-01-05 06:06:12 +01:00
dug_nodes_count = dug_nodes_count + 1
dug_node_pos [ dug_nodes_count ] = oldpos
dug_node [ dug_nodes_count ] = old_node
dug_node_meta [ dug_nodes_count ] = old_meta
2018-01-07 21:38:25 +01:00
oldpos , _ = self.old_pos_pointset : pop ( )
end
-- create the new one
for k , node_image in pairs ( self.all ) do
local new_pos = node_image.pos
local new_node = node_image.node
local old_node = minetest.get_node ( new_pos )
if not ( set_node_with_retry ( new_pos , new_node ) and set_meta_with_retry ( minetest.get_meta ( new_pos ) , node_image.meta ) ) then
minetest.log ( " error " , " DigtronLayout.write_layout_image failed to write a Digtron node, aborting write. " )
return false
end
2019-01-05 06:06:12 +01:00
placed_nodes_count = placed_nodes_count + 1
placed_node_pos [ placed_nodes_count ] = new_pos
placed_new_node [ placed_nodes_count ] = new_node
placed_old_node [ placed_nodes_count ] = old_node
2018-01-07 21:38:25 +01:00
end
-- fake_player will be passed to callbacks to prevent actual player from "taking the blame" for this action.
-- For example, the hunger mod shouldn't be making the player hungry when he moves Digtron.
digtron.fake_player : update ( self.controller , player : get_player_name ( ) )
-- note that the actual player is still passed to the per-node after_place_node and after_dig_node, should they exist.
2019-01-05 06:06:12 +01:00
node_callbacks ( player )
dug_nodes_count = 0
placed_nodes_count = 0
2018-01-07 21:38:25 +01:00
return true
2017-01-08 09:23:10 +01:00
end
2017-01-16 00:12:07 +01:00
---------------------------------------------------------------------------------------------
-- Serialization. Currently only serializes the data that is needed by the crate, upgrade this function if more is needed
function DigtronLayout . serialize ( self )
-- serialize can't handle ItemStack objects, convert them to strings.
for _ , node_image in pairs ( self.all ) do
for k , inv in pairs ( node_image.meta . inventory ) do
for index , item in pairs ( inv ) do
inv [ index ] = item : to_string ( )
end
end
end
return minetest.serialize ( { controller = self.controller , all = self.all } )
end
function DigtronLayout . deserialize ( layout_string )
local self = { }
setmetatable ( self , DigtronLayout )
if not layout_string or layout_string == " " then
return nil
end
2018-08-28 20:25:13 +02:00
local deserialized_layout = minetest.deserialize ( layout_string )
2017-01-16 00:12:07 +01:00
self.all = deserialized_layout.all
self.controller = deserialized_layout.controller
self.old_pos_pointset = Pointset.create ( ) -- needed by the write_layout method, leave empty
return self
2018-05-09 10:48:01 +02:00
end