diff options
Diffstat (limited to 'util_layout.lua')
-rw-r--r-- | util_layout.lua | 213 |
1 files changed, 91 insertions, 122 deletions
diff --git a/util_layout.lua b/util_layout.lua index 5077944..292b23f 100644 --- a/util_layout.lua +++ b/util_layout.lua @@ -1,118 +1,3 @@ -digtron.get_all_digtron_neighbours = function(pos, player) - -- returns table containing a list of all digtron node locations, lists of special digtron node types, a table of the coordinate extents of the digtron array, a Pointset of protected nodes, and a number to determine how many adjacent solid non-digtron nodes there are (for traction) - - local layout = {} - --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 - layout.traction = 0 - layout.all = {} - layout.inventories = {} - layout.fuelstores = {} - layout.diggers = {} - layout.builders = {} - layout.extents = {} - layout.water_touching = false - layout.lava_touching = false - layout.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. - layout.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 - - table.insert(layout.all, layout.controller) - layout.extents.max_x = pos.x - layout.extents.min_x = pos.x - layout.extents.max_y = pos.y - layout.extents.min_y = pos.y - layout.extents.max_z = pos.z - layout.extents.min_z = pos.z - - -- temporary pointsets used while searching - local to_test = Pointset.create() - local tested = Pointset.create() - - 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 - layout.protected:set(pos.x, pos.y, pos.z, 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 - --buildtron array is next to unloaded nodes, too dangerous to do anything. Abort. - layout.all = nil - return layout - end - - if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then - layout.protected:set(testpos.x, testpos.y, testpos.z, true) - end - - if minetest.get_item_group(node.name, "water") ~= 0 then - layout.water_touching = true - elseif minetest.get_item_group(node.name, "lava") ~= 0 then - layout.lava_touching = true - if digtron.lava_impassible == true then - layout.protected:set(testpos.x, testpos.y, testpos.z, true) - end - end - - local group_number = minetest.get_item_group(node.name, "digtron") - if group_number > 0 then - --found one. Add it to the digtrons output - table.insert(layout.all, testpos) - - -- update extents - layout.extents.max_x = math.max(layout.extents.max_x, testpos.x) - layout.extents.min_x = math.min(layout.extents.min_x, testpos.x) - layout.extents.max_y = math.max(layout.extents.max_y, testpos.y) - layout.extents.min_y = math.min(layout.extents.min_y, testpos.y) - layout.extents.max_z = math.max(layout.extents.max_z, testpos.z) - layout.extents.min_z = math.min(layout.extents.min_z, testpos.z) - - -- add a reference to this node's position to special node lists - if group_number == 2 then - table.insert(layout.inventories, testpos) - elseif group_number == 3 then - table.insert(layout.diggers, testpos) - elseif group_number == 4 then - table.insert(layout.builders, testpos) - elseif group_number == 5 then - table.insert(layout.fuelstores, testpos) - elseif group_number == 6 then - table.insert(layout.inventories, testpos) - table.insert(layout.fuelstores, testpos) - end - - --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) - elseif minetest.registered_nodes[node.name].buildable_to ~= true then - -- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction. - layout.traction = layout.traction + 1 - end - - testpos, _ = to_test:pop() - end - - return layout -end - --- Rotation magic --------------------------------------------------------------------------------------------------------- - 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 @@ -216,6 +101,7 @@ local rotate_node_image = function(node_image, origin, axis, direction, old_pos_ return node_image end +-- 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) digtron.rotate_layout_image = function(layout_image, facedir) -- To convert this into the direction the "top" of the axel node is pointing in: -- 0, 1, 2, 3 == (0,1,0) @@ -234,8 +120,6 @@ digtron.rotate_layout_image = function(layout_image, facedir) {axis="y", dir=1}, } local params = top[math.floor(facedir/4)] - - layout_image.old_pos_pointset = Pointset:create() for k, node_image in pairs(layout_image.all) do rotate_node_image(node_image, layout_image.controller, params.axis, params.dir, layout_image.old_pos_pointset) @@ -243,6 +127,51 @@ digtron.rotate_layout_image = function(layout_image, facedir) return layout_image end +digtron.move_layout_image = function(layout_image, facing, player_name) + local extents = layout_image.extents + local dir = digtron.facedir_to_dir_map[facing] + local increment + local filter + if dir == 1 then -- z+ + filter = "z" + increment = 1 + extents.max_z = extents.max_z + 1 + extents.min_z = extents.min_z + 1 + elseif dir == 2 then -- x+ + filter = "x" + increment = 1 + extents.max_x = extents.max_x + 1 + extents.min_x = extents.min_x + 1 + elseif dir == 3 then -- z- + filter = "z" + increment = -1 + extents.max_z = extents.max_z - 1 + extents.min_z = extents.min_z - 1 + elseif dir == 4 then -- x- + filter = "x" + increment = -1 + extents.max_x = extents.max_x - 1 + extents.min_x = extents.min_x - 1 + elseif dir == 5 then -- y- + filter = "y" + increment = -1 + extents.max_y = extents.max_y - 1 + extents.min_y = extents.min_y - 1 + elseif dir == 6 then -- y+ + filter = "y" + increment = 1 + extents.max_y = extents.max_y + 1 + extents.min_y = extents.min_y + 1 + end + + for k, node_image in pairs(layout_image.all) do + layout_image.old_pos_pointset:set(node_image.pos.x, node_image.pos.y, node_image.pos.z, true) + node_image.pos[filter] = node_image.pos[filter] + increment + layout_image.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. + -- TODO: log + end +end + digtron.can_write_layout_image = function(layout_image, player) for k, node_image in pairs(layout_image.all) do if not layout_image.old_pos_pointset:get(node_image.pos.x, node_image.pos.y, node_image.pos.z) @@ -279,17 +208,28 @@ digtron.write_layout_image = function(layout_image) end end --- Similar to get_layout, but far more comprehensive. This produces a data structure plus a set of temporary inventories that will allow the digtron to be rotated and then recreated. +-- Similar to get_layout, but far more comprehensive. This produces a data structure that will allow the digtron to be rotated and then recreated in its entirety. digtron.get_layout_image = function(pos, player) local image = {} --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 + + image.traction = 0 image.all = {} + image.inventories = {} + image.fuelstores = {} + image.diggers = {} + image.builders = {} image.extents = {} - image.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 + image.water_touching = false + image.lava_touching = false + image.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. + image.old_pos_pointset = Pointset.create() -- For tracking original location of nodes if we do transformations on the Digtron + image.nodes_dug = Pointset.create() -- For tracking adjacent nodes that will have been dug by digger heads in future image.contains_protected_node = false -- used to indicate if at least one node in this digtron array is protected from the player. - - table.insert(image.all, get_node_image(pos, minetest.get_node(pos))) + image.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 + + table.insert(image.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 if a controller node is created that contains fuel or inventory or whatever. image.extents.max_x = pos.x image.extents.min_x = pos.x @@ -311,6 +251,7 @@ digtron.get_layout_image = function(pos, player) 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 + image.protected:set(pos.x, pos.y, pos.z, true) image.contains_protected_node = true end @@ -326,11 +267,36 @@ digtron.get_layout_image = function(pos, player) return nil end + if minetest.get_item_group(node.name, "water") ~= 0 then + image.water_touching = true + elseif minetest.get_item_group(node.name, "lava") ~= 0 then + image.lava_touching = true + if digtron.lava_impassible == true then + image.protected:set(testpos.x, testpos.y, testpos.z, true) + end + end + local group_number = minetest.get_item_group(node.name, "digtron") if group_number > 0 then --found one. Add it to the digtrons output - table.insert(image.all, get_node_image(testpos, node)) + local node_image = get_node_image(testpos, node) + + table.insert(image.all, node_image) + -- add a reference to this node's position to special node lists + if group_number == 2 then + table.insert(image.inventories, node_image) + elseif group_number == 3 then + table.insert(image.diggers, node_image) + elseif group_number == 4 then + table.insert(image.builders, node_image) + elseif group_number == 5 then + table.insert(image.fuelstores, node_image) + elseif group_number == 6 then + table.insert(image.inventories, node_image) + table.insert(image.fuelstores, node_image) + end + if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then image.contains_protected_node = true end @@ -350,6 +316,9 @@ digtron.get_layout_image = function(pos, player) 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) + elseif minetest.registered_nodes[node.name].buildable_to ~= true then + -- Tracks whether the digtron is hovering in mid-air. If any part of the digtron array touches something solid it gains traction. + image.traction = image.traction + 1 end testpos, _ = to_test:pop() |