From eaae4276fcd1ae82ba0fdd39d7901c3cd9513d5f Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sun, 8 Jan 2017 23:52:36 -0700 Subject: removing the old get_all_digtron_neighbours This is basically working, but still needs revisions to take advantage of the new information being stored in the image data. It should be possible to do all the temporary inventory work directly on the image file so that no put-back-in-inventory steps will be needed any more. Much nicer. --- util.lua | 8 +- util_execute_cycle.lua | 62 +++++++------- util_layout.lua | 213 +++++++++++++++++++++---------------------------- 3 files changed, 123 insertions(+), 160 deletions(-) diff --git a/util.lua b/util.lua index dba9db8..73d6d22 100644 --- a/util.lua +++ b/util.lua @@ -103,7 +103,7 @@ digtron.place_in_inventory = function(itemname, inventory_positions, fallback_po --tries placing the item in each inventory node in turn. If there's no room, drop it at fallback_pos local itemstack = ItemStack(itemname) for k, location in pairs(inventory_positions) do - local inv = minetest.get_inventory({type="node", pos=location}) + local inv = minetest.get_inventory({type="node", pos=location.pos}) itemstack = inv:add_item("main", itemstack) if itemstack:is_empty() then return nil @@ -131,10 +131,10 @@ digtron.take_from_inventory = function(itemname, inventory_positions) --tries to take an item from each inventory node in turn. Returns location of inventory item was taken from on success, nil on failure local itemstack = ItemStack(itemname) for k, location in pairs(inventory_positions) do - local inv = minetest.get_inventory({type="node", pos=location}) + local inv = minetest.get_inventory({type="node", pos=location.pos}) local output = inv:remove_item("main", itemstack) if not output:is_empty() then - return location + return location.pos end end return nil @@ -162,7 +162,7 @@ digtron.burn = function(fuelstore_positions, target, test) if current_burned > target then break end - local inv = minetest.get_inventory({type="node", pos=location}) + local inv = minetest.get_inventory({type="node", pos=location.pos}) local invlist = inv:get_list("fuel") for i, itemstack in pairs(invlist) do local fuel_per_item = minetest.get_craft_result({method="fuel", width=1, items={itemstack:peek_item(1)}}).time diff --git a/util_execute_cycle.lua b/util_execute_cycle.lua index 080c0ae..7c4fcd3 100644 --- a/util_execute_cycle.lua +++ b/util_execute_cycle.lua @@ -90,7 +90,7 @@ digtron.execute_dig_cycle = function(pos, clicker) local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle local status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning) - local layout = digtron.get_all_digtron_neighbours(pos, clicker) + local layout = digtron.get_layout_image(pos, clicker) local status_text, return_code = neighbour_test(layout, status_text) if return_code ~= 0 then @@ -103,7 +103,6 @@ digtron.execute_dig_cycle = function(pos, clicker) ---------------------------------------------------------------------------------------------------------------------- - local nodes_dug = Pointset.create() local items_dropped = {} local digging_fuel_cost = 0 local particle_systems = {} @@ -113,16 +112,16 @@ digtron.execute_dig_cycle = function(pos, clicker) -- but doesn't actually dig the nodes yet. That comes later. -- If we dug them now, sand would fall and some digtron nodes would die. for k, location in pairs(layout.diggers) do - local target = minetest.get_node(location) + local target = minetest.get_node(location.pos) local targetdef = minetest.registered_nodes[target.name] if targetdef.execute_dig ~= nil then - local fuel_cost, dropped = targetdef.execute_dig(location, layout.protected, nodes_dug, controlling_coordinate) + local fuel_cost, dropped = targetdef.execute_dig(location.pos, layout.protected, layout.nodes_dug, controlling_coordinate) if dropped ~= nil then for _, itemname in pairs(dropped) do table.insert(items_dropped, itemname) end if digtron.particle_effects then - table.insert(particle_systems, dig_dust(vector.add(location, move_dir), target.param2)) + table.insert(particle_systems, dig_dust(vector.add(location.pos, move_dir), target.param2)) end end digging_fuel_cost = digging_fuel_cost + fuel_cost @@ -137,8 +136,8 @@ digtron.execute_dig_cycle = function(pos, clicker) -- as having been dug. local can_move = true for _, location in pairs(layout.all) do - local newpos = digtron.find_new_pos(location, facing) - if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then + local newpos = digtron.find_new_pos(location.pos, facing) + if not digtron.can_move_to(newpos, layout.protected, layout.nodes_dug) then can_move = false end end @@ -165,11 +164,11 @@ digtron.execute_dig_cycle = function(pos, clicker) local test_fuel_items = {} local test_build_fuel_cost = 0 for k, location in pairs(layout.builders) do - local target = minetest.get_node(location) + local target = minetest.get_node(location.pos) local targetdef = minetest.registered_nodes[target.name] - local test_location = digtron.find_new_pos(location, facing) + local test_location = digtron.find_new_pos(location.pos, facing) if targetdef.test_build ~= nil then - test_build_return_code, test_build_return_item = targetdef.test_build(location, test_location, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, layout.controller) + test_build_return_code, test_build_return_item = targetdef.test_build(location.pos, test_location, layout.inventories, layout.protected, layout.nodes_dug, controlling_coordinate, layout.controller) if test_build_return_code > 1 then can_build = false break @@ -233,32 +232,35 @@ digtron.execute_dig_cycle = function(pos, clicker) -- damage the weak flesh if digtron.diggers_damage_creatures then for k, location in pairs(layout.diggers) do - local target = minetest.get_node(location) + local target = minetest.get_node(location.pos) local targetdef = minetest.registered_nodes[target.name] if targetdef.damage_creatures ~= nil then - targetdef.damage_creatures(clicker, location, digtron.find_new_pos(location, target.param2), controlling_coordinate) + targetdef.damage_creatures(clicker, location.pos, digtron.find_new_pos(location.pos, target.param2), controlling_coordinate) end end end --move the array - digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug, clicker:get_player_name()) + digtron.move_layout_image(layout, facing, clicker:get_player_name()) + digtron.write_layout_image(layout) local oldpos = {x=pos.x, y=pos.y, z=pos.z} pos = digtron.find_new_pos(pos, facing) meta = minetest.get_meta(pos) if move_player then + local player_pos = clicker:getpos() clicker:moveto(digtron.find_new_pos(player_pos, facing), true) end + local building_fuel_cost = 0 local strange_failure = false -- execute_build on all digtron components that have one for k, location in pairs(layout.builders) do - local target = minetest.get_node(location) + local target = minetest.get_node(location.pos) local targetdef = minetest.registered_nodes[target.name] if targetdef.execute_build ~= nil then --using the old location of the controller as fallback so that any leftovers land with the rest of the digger output. Not that there should be any. - local build_return = targetdef.execute_build(location, clicker, layout.inventories, layout.protected, nodes_dug, controlling_coordinate, oldpos) + local build_return = targetdef.execute_build(location.pos, clicker, layout.inventories, layout.protected, layout.nodes_dug, controlling_coordinate, oldpos) if build_return == false then -- This happens if there's insufficient inventory, but we should have confirmed there was sufficient inventory during test phase. -- So this should never happen. However, "should never happens" happen sometimes. So @@ -299,7 +301,7 @@ digtron.execute_dig_cycle = function(pos, clicker) -- finally, dig out any nodes remaining to be dug. Some of these will have had their flag revoked because -- a builder put something there or because they're another digtron node. - local node_to_dig, whether_to_dig = nodes_dug:pop() + local node_to_dig, whether_to_dig = layout.nodes_dug:pop() while node_to_dig ~= nil do if whether_to_dig == true then minetest.log("action", string.format("%s uses Digtron to dig %s at (%d, %d, %d)", clicker:get_player_name(), minetest.get_node(node_to_dig).name, node_to_dig.x, node_to_dig.y, node_to_dig.z)) @@ -308,7 +310,7 @@ digtron.execute_dig_cycle = function(pos, clicker) -- all of the digtron's nodes wind up in nodes_dug, so this is an ideal place to stick -- a check to make sand fall after the digtron has passed. minetest.check_for_falling({x=node_to_dig.x, y=node_to_dig.y+1, z=node_to_dig.z}) - node_to_dig, whether_to_dig = nodes_dug:pop() + node_to_dig, whether_to_dig = layout.nodes_dug:pop() end return pos, status_text, 0 end @@ -317,7 +319,7 @@ end -- Simplified version of the above method that only moves, and doesn't execute diggers or builders. digtron.execute_move_cycle = function(pos, clicker) local meta = minetest.get_meta(pos) - local layout = digtron.get_all_digtron_neighbours(pos, clicker) + local layout = digtron.get_layout_image(pos, clicker) local status_text = "" local status_text, return_code = neighbour_test(layout, status_text) @@ -327,18 +329,13 @@ digtron.execute_move_cycle = function(pos, clicker) local facing = minetest.get_node(pos).param2 local controlling_coordinate = digtron.get_controlling_coordinate(pos, facing) - - local nodes_dug = Pointset.create() -- empty set, we're not digging anything - -- test if any digtrons are obstructed by non-digtron nodes that haven't been marked - -- as having been dug. - local can_move = true - for _, location in pairs(layout.all) do - local newpos = digtron.find_new_pos(location, facing) - if not digtron.can_move_to(newpos, layout.protected, nodes_dug) then - can_move = false - end - end + -- if the player is standing within the array or next to it, move him too. + local move_player = move_player_test(layout, clicker) + + -- test if any digtrons are obstructed by non-digtron nodes + digtron.move_layout_image(layout, facing, clicker:get_player_name()) + local can_move = digtron.can_write_layout_image(layout, clicker) if not can_move then -- mark this node as waiting, will clear this flag in digtron.cycle_time seconds @@ -350,15 +347,12 @@ digtron.execute_move_cycle = function(pos, clicker) end minetest.sound_play("truck", {gain=1.0, pos=pos}) - - -- if the player is standing within the array or next to it, move him too. - local move_player = move_player_test(layout, clicker) --move the array - digtron.move_digtron(facing, layout.all, layout.extents, nodes_dug) - local oldpos = {x=pos.x, y=pos.y, z=pos.z} + digtron.write_layout_image(layout) pos = digtron.find_new_pos(pos, facing) if move_player then + local player_pos = clicker:getpos() clicker:moveto(digtron.find_new_pos(player_pos, facing), true) end 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() -- cgit v1.2.3 From 8952c55914163d8141bcc3a4ba3c4e5df3153b2e Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Mon, 9 Jan 2017 01:19:35 -0700 Subject: Don't need player parameter for can_write_layout_image any more --- node_axle.lua | 2 +- util_layout.lua | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/node_axle.lua b/node_axle.lua index c30a401..f78b220 100644 --- a/node_axle.lua +++ b/node_axle.lua @@ -38,7 +38,7 @@ minetest.register_node("digtron:axle", { end local image = digtron.get_layout_image(pos, clicker) digtron.rotate_layout_image(image, node.param2) - if digtron.can_write_layout_image(image, clicker) then + if digtron.can_write_layout_image(image) then digtron.write_layout_image(image) minetest.sound_play("whirr", {gain=1.0, pos=pos}) diff --git a/util_layout.lua b/util_layout.lua index 292b23f..da5ac6d 100644 --- a/util_layout.lua +++ b/util_layout.lua @@ -172,12 +172,14 @@ digtron.move_layout_image = function(layout_image, facing, player_name) end end -digtron.can_write_layout_image = function(layout_image, player) +digtron.can_write_layout_image = function(layout_image) for k, node_image in pairs(layout_image.all) do + -- check if the target node is buildable_to or is marked as part of the digtron that's moving if not layout_image.old_pos_pointset:get(node_image.pos.x, node_image.pos.y, node_image.pos.z) and not minetest.registered_nodes[minetest.get_node(node_image.pos).name].buildable_to then return false - elseif minetest.is_protected(node_image.pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then + --check if we're moving into a protected node + elseif layout_image.protected:get(node_image.pos) then return false end end -- cgit v1.2.3 From 2912dc34b8042337421cdabba5fc1a7062f491ef Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Tue, 10 Jan 2017 00:29:09 -0700 Subject: Turn layout into a class. --- node_controllers.lua | 1 + util_execute_cycle.lua | 17 ++- util_layout.lua | 319 ++++++++++++++++++++++++++----------------------- 3 files changed, 176 insertions(+), 161 deletions(-) diff --git a/node_controllers.lua b/node_controllers.lua index caa7184..d942d38 100644 --- a/node_controllers.lua +++ b/node_controllers.lua @@ -223,6 +223,7 @@ minetest.register_node("digtron:pusher", { end local newpos, status_text, return_code = digtron.execute_move_cycle(pos, clicker) + meta = minetest.get_meta(newpos) meta:set_string("infotext", status_text) -- Start the delay before digtron can run again. diff --git a/util_execute_cycle.lua b/util_execute_cycle.lua index 7c4fcd3..e5ba7d2 100644 --- a/util_execute_cycle.lua +++ b/util_execute_cycle.lua @@ -90,7 +90,7 @@ digtron.execute_dig_cycle = function(pos, clicker) local fuel_burning = meta:get_float("fuel_burning") -- get amount of burned fuel left over from last cycle local status_text = string.format("Heat remaining in controller furnace: %d", fuel_burning) - local layout = digtron.get_layout_image(pos, clicker) + local layout = DigtronLayout.create(pos, clicker) local status_text, return_code = neighbour_test(layout, status_text) if return_code ~= 0 then @@ -241,8 +241,8 @@ digtron.execute_dig_cycle = function(pos, clicker) end --move the array - digtron.move_layout_image(layout, facing, clicker:get_player_name()) - digtron.write_layout_image(layout) + layout:move_layout_image(facing, clicker:get_player_name()) + layout:write_layout_image() local oldpos = {x=pos.x, y=pos.y, z=pos.z} pos = digtron.find_new_pos(pos, facing) meta = minetest.get_meta(pos) @@ -319,7 +319,7 @@ end -- Simplified version of the above method that only moves, and doesn't execute diggers or builders. digtron.execute_move_cycle = function(pos, clicker) local meta = minetest.get_meta(pos) - local layout = digtron.get_layout_image(pos, clicker) + local layout = DigtronLayout.create(pos, clicker) local status_text = "" local status_text, return_code = neighbour_test(layout, status_text) @@ -334,8 +334,8 @@ digtron.execute_move_cycle = function(pos, clicker) local move_player = move_player_test(layout, clicker) -- test if any digtrons are obstructed by non-digtron nodes - digtron.move_layout_image(layout, facing, clicker:get_player_name()) - local can_move = digtron.can_write_layout_image(layout, clicker) + layout:move_layout_image(facing, clicker:get_player_name()) + local can_move = layout:can_write_layout_image(clicker) if not can_move then -- mark this node as waiting, will clear this flag in digtron.cycle_time seconds @@ -349,12 +349,11 @@ digtron.execute_move_cycle = function(pos, clicker) minetest.sound_play("truck", {gain=1.0, pos=pos}) --move the array - digtron.write_layout_image(layout) + layout:write_layout_image() pos = digtron.find_new_pos(pos, facing) if move_player then local player_pos = clicker:getpos() clicker:moveto(digtron.find_new_pos(player_pos, facing), true) - end - + end return pos, "", 0 end \ No newline at end of file diff --git a/util_layout.lua b/util_layout.lua index da5ac6d..b7311c5 100644 --- a/util_layout.lua +++ b/util_layout.lua @@ -1,3 +1,147 @@ +DigtronLayout = {} +DigtronLayout.__index = DigtronLayout + +------------------------------------------------------------------------- +-- Creation + +local get_node_image = function(pos, node) + local node_image = {node=node, pos={x=pos.x, y=pos.y, z=pos.z}} + node_image.paramtype2 = minetest.registered_nodes[node.name].paramtype2 + local meta = minetest.get_meta(pos) + node_image.meta = meta:to_table() + + -- Record what kind of thing we've got in a builder node so its facing can be rotated properly + if minetest.get_item_group(node.name, "digtron") == 4 then + local build_item = node_image.meta.inventory.main[1] + if build_item ~= "" then + local build_item_def = minetest.registered_nodes[ItemStack(build_item):get_name()] + node_image.build_item_paramtype2 = build_item_def.paramtype2 + end + end + return node_image +end + +function DigtronLayout.create(pos, player) + local self = {} + setmetatable(self,DigtronLayout) + + --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.inventories = {} + self.fuelstores = {} + self.diggers = {} + self.builders = {} + self.extents = {} + 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 + + 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 if a controller node is created that contains fuel or inventory or whatever. + + 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 + + -- 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 + 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 + --buildtron array is next to unloaded nodes, too dangerous to do anything. Abort. + return nil + 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 + if digtron.lava_impassible == true then + self.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 + 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 + table.insert(self.inventories, node_image) + elseif group_number == 3 then + table.insert(self.diggers, node_image) + elseif group_number == 4 then + table.insert(self.builders, node_image) + elseif group_number == 5 then + table.insert(self.fuelstores, node_image) + elseif group_number == 6 then + table.insert(self.inventories, node_image) + table.insert(self.fuelstores, node_image) + end + + if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then + self.contains_protected_node = true + end + + -- update extents + 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) + + --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. + self.traction = self.traction + 1 + end + + testpos, _ = to_test:pop() + end + + return self +end + +------------------------------------------------------------------------ +-- Rotation + 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 @@ -56,23 +200,6 @@ local rotate_pos = function(axis, direction, pos) end end -local get_node_image = function(pos, node) - local node_image = {node=node, pos={x=pos.x, y=pos.y, z=pos.z}} - node_image.paramtype2 = minetest.registered_nodes[node.name].paramtype2 - local meta = minetest.get_meta(pos) - node_image.meta = meta:to_table() - - -- Record what kind of thing we've got in a builder node so its facing can be rotated properly - if minetest.get_item_group(node.name, "digtron") == 4 then - local build_item = node_image.meta.inventory.main[1] - if build_item ~= "" then - local build_item_def = minetest.registered_nodes[ItemStack(build_item):get_name()] - node_image.build_item_paramtype2 = build_item_def.paramtype2 - end - end - return node_image -end - local rotate_node_image = function(node_image, origin, axis, direction, old_pos_pointset) -- Facings if node_image.paramtype2 == "wallmounted" then @@ -102,7 +229,7 @@ local rotate_node_image = function(node_image, origin, axis, direction, old_pos_ 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) +function DigtronLayout.rotate_layout_image(self, facedir) -- To convert this into the direction the "top" of the axel node is pointing in: -- 0, 1, 2, 3 == (0,1,0) -- 4, 5, 6, 7 == (0,0,1) @@ -121,14 +248,17 @@ digtron.rotate_layout_image = function(layout_image, facedir) } local params = top[math.floor(facedir/4)] - 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) + for k, node_image in pairs(self.all) do + rotate_node_image(node_image, self.controller, params.axis, params.dir, self.old_pos_pointset) end - return layout_image + return self end -digtron.move_layout_image = function(layout_image, facing, player_name) - local extents = layout_image.extents +----------------------------------------------------------------------------------------------- +-- Translation + +function DigtronLayout.move_layout_image(self, facing, player_name) + local extents = self.extents local dir = digtron.facedir_to_dir_map[facing] local increment local filter @@ -164,42 +294,45 @@ digtron.move_layout_image = function(layout_image, facing, player_name) 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) + 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) 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. + 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. -- TODO: log end end -digtron.can_write_layout_image = function(layout_image) - for k, node_image in pairs(layout_image.all) do +----------------------------------------------------------------------------------------------- +-- Writing to world + +function DigtronLayout.can_write_layout_image(self) + for k, node_image in pairs(self.all) do -- check if the target node is buildable_to or is marked as part of the digtron that's moving - if not layout_image.old_pos_pointset:get(node_image.pos.x, node_image.pos.y, node_image.pos.z) + if not self.old_pos_pointset:get(node_image.pos.x, node_image.pos.y, node_image.pos.z) and not minetest.registered_nodes[minetest.get_node(node_image.pos).name].buildable_to then return false --check if we're moving into a protected node - elseif layout_image.protected:get(node_image.pos) then + elseif self.protected:get(node_image.pos) then return false end end return true end -digtron.write_layout_image = function(layout_image) +function DigtronLayout.write_layout_image(self) -- destroy the old digtron - local oldpos, _ = layout_image.old_pos_pointset:pop() + local oldpos, _ = self.old_pos_pointset:pop() while oldpos ~= nil do local old_def = minetest.registered_nodes[minetest.get_node(oldpos).name] minetest.remove_node(oldpos) if old_def.after_dig_node ~= nil then old_def.after_dig_node(oldpos) end - oldpos, _ = layout_image.old_pos_pointset:pop() + oldpos, _ = self.old_pos_pointset:pop() end -- create the new one - for k, node_image in pairs(layout_image.all) do + for k, node_image in pairs(self.all) do minetest.add_node(node_image.pos, node_image.node) minetest.get_meta(node_image.pos):from_table(node_image.meta) @@ -210,121 +343,3 @@ digtron.write_layout_image = function(layout_image) end end --- 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.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. - 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 - image.extents.max_y = pos.y - image.extents.min_y = pos.y - image.extents.max_z = pos.z - image.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 - image.protected:set(pos.x, pos.y, pos.z, true) - image.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 - --buildtron array is next to unloaded nodes, too dangerous to do anything. Abort. - 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 - 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 - - -- update extents - image.extents.max_x = math.max(image.extents.max_x, testpos.x) - image.extents.min_x = math.min(image.extents.min_x, testpos.x) - image.extents.max_y = math.max(image.extents.max_y, testpos.y) - image.extents.min_y = math.min(image.extents.min_y, testpos.y) - image.extents.max_z = math.max(image.extents.max_z, testpos.z) - image.extents.min_z = math.min(image.extents.min_z, testpos.z) - - --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. - image.traction = image.traction + 1 - end - - testpos, _ = to_test:pop() - end - - return image -end -- cgit v1.2.3 From 01fb71d2eb5d1dd41c338bef0bcc2e26a259047d Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Tue, 10 Jan 2017 00:32:08 -0700 Subject: renaming class definition files --- class_layout.lua | 345 +++++++++++++++++++++++++++++++++++++++++++++++++++++ class_pointset.lua | 73 ++++++++++++ init.lua | 3 +- pointset.lua | 73 ------------ util.lua | 1 - util_layout.lua | 345 ----------------------------------------------------- 6 files changed, 420 insertions(+), 420 deletions(-) create mode 100644 class_layout.lua create mode 100644 class_pointset.lua delete mode 100644 pointset.lua delete mode 100644 util_layout.lua diff --git a/class_layout.lua b/class_layout.lua new file mode 100644 index 0000000..b7311c5 --- /dev/null +++ b/class_layout.lua @@ -0,0 +1,345 @@ +DigtronLayout = {} +DigtronLayout.__index = DigtronLayout + +------------------------------------------------------------------------- +-- Creation + +local get_node_image = function(pos, node) + local node_image = {node=node, pos={x=pos.x, y=pos.y, z=pos.z}} + node_image.paramtype2 = minetest.registered_nodes[node.name].paramtype2 + local meta = minetest.get_meta(pos) + node_image.meta = meta:to_table() + + -- Record what kind of thing we've got in a builder node so its facing can be rotated properly + if minetest.get_item_group(node.name, "digtron") == 4 then + local build_item = node_image.meta.inventory.main[1] + if build_item ~= "" then + local build_item_def = minetest.registered_nodes[ItemStack(build_item):get_name()] + node_image.build_item_paramtype2 = build_item_def.paramtype2 + end + end + return node_image +end + +function DigtronLayout.create(pos, player) + local self = {} + setmetatable(self,DigtronLayout) + + --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.inventories = {} + self.fuelstores = {} + self.diggers = {} + self.builders = {} + self.extents = {} + 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 + + 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 if a controller node is created that contains fuel or inventory or whatever. + + 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 + + -- 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 + 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 + --buildtron array is next to unloaded nodes, too dangerous to do anything. Abort. + return nil + 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 + if digtron.lava_impassible == true then + self.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 + 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 + table.insert(self.inventories, node_image) + elseif group_number == 3 then + table.insert(self.diggers, node_image) + elseif group_number == 4 then + table.insert(self.builders, node_image) + elseif group_number == 5 then + table.insert(self.fuelstores, node_image) + elseif group_number == 6 then + table.insert(self.inventories, node_image) + table.insert(self.fuelstores, node_image) + end + + if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then + self.contains_protected_node = true + end + + -- update extents + 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) + + --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. + self.traction = self.traction + 1 + end + + testpos, _ = to_test:pop() + end + + return self +end + +------------------------------------------------------------------------ +-- Rotation + +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 + } +} + + --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) +local rotate_pos = function(axis, direction, pos) + if axis == "x" then + if direction < 0 then + return {x= pos.x, y= -pos.z, z= pos.y} + else + return {x= pos.x, y= pos.z, z= -pos.y} + end + elseif axis == "y" then + if direction < 0 then + return {x= -pos.z, y= pos.y, z= pos.x} + else + return {x= pos.z, y= pos.y, z= -pos.x} + end + else + if direction < 0 then + return {x= -pos.y, y= pos.x, z= pos.z} + else + return {x= pos.y, y= -pos.x, z= pos.z} + end + end +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 + node_image.meta.fields.build_facing = wallmounted_rotate[axis][direction][tonumber(node_image.meta.fields.build_facing)] + elseif node_image.build_item_paramtype2 == "facedir" then + node_image.meta.fields.build_facing = facedir_rotate[axis][direction][tonumber(node_image.meta.fields.build_facing)] + 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 + local pos = vector.subtract(node_image.pos, origin) + pos = rotate_pos(axis, direction, pos) + -- Move back to original reference frame + node_image.pos = vector.add(pos, origin) + + 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) +function DigtronLayout.rotate_layout_image(self, facedir) + -- To convert this into the direction the "top" of the axel node is pointing in: + -- 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 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}, + } + local params = top[math.floor(facedir/4)] + + for k, node_image in pairs(self.all) do + rotate_node_image(node_image, self.controller, params.axis, params.dir, self.old_pos_pointset) + end + return self +end + +----------------------------------------------------------------------------------------------- +-- Translation + +function DigtronLayout.move_layout_image(self, facing, player_name) + local extents = self.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(self.all) do + self.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 + 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. + -- TODO: log + end +end + +----------------------------------------------------------------------------------------------- +-- Writing to world + +function DigtronLayout.can_write_layout_image(self) + for k, node_image in pairs(self.all) do + -- 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) + and not minetest.registered_nodes[minetest.get_node(node_image.pos).name].buildable_to then + return false + --check if we're moving into a protected node + elseif self.protected:get(node_image.pos) then + return false + end + end + return true +end + +function DigtronLayout.write_layout_image(self) + -- destroy the old digtron + local oldpos, _ = self.old_pos_pointset:pop() + while oldpos ~= nil do + local old_def = minetest.registered_nodes[minetest.get_node(oldpos).name] + minetest.remove_node(oldpos) + if old_def.after_dig_node ~= nil then + old_def.after_dig_node(oldpos) + end + oldpos, _ = self.old_pos_pointset:pop() + end + + -- create the new one + for k, node_image in pairs(self.all) do + minetest.add_node(node_image.pos, node_image.node) + minetest.get_meta(node_image.pos):from_table(node_image.meta) + + local new_def = minetest.registered_nodes[node_image.node.name] + if new_def.after_place_node ~= nil then + new_def.after_place_node(node_image.pos) + end + end +end + diff --git a/class_pointset.lua b/class_pointset.lua new file mode 100644 index 0000000..5bfd22a --- /dev/null +++ b/class_pointset.lua @@ -0,0 +1,73 @@ +-- A simple special-purpose class, this is used for building up sets of three-dimensional points +-- I only added features to it as I needed them so may not be highly useful outside of this mod's context. + +Pointset = {} +Pointset.__index = Pointset + +function Pointset.create() + local set = {} + setmetatable(set,Pointset) + set.points = {} + return set +end + +function Pointset:set(x, y, z, value) + -- sets a value in the 3D array "points". + if self.points[x] == nil then + self.points[x] = {} + end + if self.points[x][y] == nil then + self.points[x][y] = {} + end + self.points[x][y][z] = value +end + +function Pointset:set_if_not_in(excluded, x, y, z, value) + -- If a value is not already set for this point in the 3D array "excluded", set it in "points" + if excluded:get(x, y, z) ~= nil then + return + end + self:set(x, y, z, value) +end + +function Pointset:get(x, y, z) + -- return a value from the 3D array "points" + if self.points[x] == nil or self.points[x][y] == nil then + return nil + end + return self.points[x][y][z] +end + +function Pointset:pop() + -- returns a point that's in the 3D array, and then removes it. + local pos = {} + local ytable + local ztable + local val + + local count = 0 + for _ in pairs(self.points) do count = count + 1 end + if count == 0 then + return nil + end + + pos.x, ytable = next(self.points) + pos.y, ztable = next(ytable) + pos.z, val = next(ztable) + + self.points[pos.x][pos.y][pos.z] = nil + + count = 0 + for _ in pairs(self.points[pos.x][pos.y]) do count = count + 1 end + if count == 0 then + self.points[pos.x][pos.y] = nil + end + + count = 0 + for _ in pairs(self.points[pos.x]) do count = count + 1 end + if count == 0 then + self.points[pos.x] = nil + end + + return pos, val +end \ No newline at end of file diff --git a/init.lua b/init.lua index af0492c..3ae536f 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,6 @@ dofile( minetest.get_modpath( "digtron" ) .. "/util.lua" ) -dofile( minetest.get_modpath( "digtron" ) .. "/pointset.lua" ) +dofile( minetest.get_modpath( "digtron" ) .. "/class_pointset.lua" ) +dofile( minetest.get_modpath( "digtron" ) .. "/class_layout.lua" ) dofile( minetest.get_modpath( "digtron" ) .. "/entities.lua" ) dofile( minetest.get_modpath( "digtron" ) .. "/node_misc.lua" ) -- contains structure and light nodes dofile( minetest.get_modpath( "digtron" ) .. "/node_storage.lua" ) -- contains inventory and fuel storage nodes diff --git a/pointset.lua b/pointset.lua deleted file mode 100644 index 5bfd22a..0000000 --- a/pointset.lua +++ /dev/null @@ -1,73 +0,0 @@ --- A simple special-purpose class, this is used for building up sets of three-dimensional points --- I only added features to it as I needed them so may not be highly useful outside of this mod's context. - -Pointset = {} -Pointset.__index = Pointset - -function Pointset.create() - local set = {} - setmetatable(set,Pointset) - set.points = {} - return set -end - -function Pointset:set(x, y, z, value) - -- sets a value in the 3D array "points". - if self.points[x] == nil then - self.points[x] = {} - end - if self.points[x][y] == nil then - self.points[x][y] = {} - end - self.points[x][y][z] = value -end - -function Pointset:set_if_not_in(excluded, x, y, z, value) - -- If a value is not already set for this point in the 3D array "excluded", set it in "points" - if excluded:get(x, y, z) ~= nil then - return - end - self:set(x, y, z, value) -end - -function Pointset:get(x, y, z) - -- return a value from the 3D array "points" - if self.points[x] == nil or self.points[x][y] == nil then - return nil - end - return self.points[x][y][z] -end - -function Pointset:pop() - -- returns a point that's in the 3D array, and then removes it. - local pos = {} - local ytable - local ztable - local val - - local count = 0 - for _ in pairs(self.points) do count = count + 1 end - if count == 0 then - return nil - end - - pos.x, ytable = next(self.points) - pos.y, ztable = next(ytable) - pos.z, val = next(ztable) - - self.points[pos.x][pos.y][pos.z] = nil - - count = 0 - for _ in pairs(self.points[pos.x][pos.y]) do count = count + 1 end - if count == 0 then - self.points[pos.x][pos.y] = nil - end - - count = 0 - for _ in pairs(self.points[pos.x]) do count = count + 1 end - if count == 0 then - self.points[pos.x] = nil - end - - return pos, val -end \ No newline at end of file diff --git a/util.lua b/util.lua index 73d6d22..acebc59 100644 --- a/util.lua +++ b/util.lua @@ -4,7 +4,6 @@ digtron = {} dofile( minetest.get_modpath( "digtron" ) .. "/util_item_place_node.lua" ) -- separated out to avoid potential for license complexity dofile( minetest.get_modpath( "digtron" ) .. "/util_movement.lua" ) -- separated out simply for tidiness, there's some big code in there -dofile( minetest.get_modpath( "digtron" ) .. "/util_layout.lua" ) -- separated out simply for tidiness, there's some big code in there dofile( minetest.get_modpath( "digtron" ) .. "/util_execute_cycle.lua" ) -- separated out simply for tidiness, there's some big code in there -- Apparently node_sound_metal_defaults is a newer thing, I ran into games using an older version of the default mod without it. diff --git a/util_layout.lua b/util_layout.lua deleted file mode 100644 index b7311c5..0000000 --- a/util_layout.lua +++ /dev/null @@ -1,345 +0,0 @@ -DigtronLayout = {} -DigtronLayout.__index = DigtronLayout - -------------------------------------------------------------------------- --- Creation - -local get_node_image = function(pos, node) - local node_image = {node=node, pos={x=pos.x, y=pos.y, z=pos.z}} - node_image.paramtype2 = minetest.registered_nodes[node.name].paramtype2 - local meta = minetest.get_meta(pos) - node_image.meta = meta:to_table() - - -- Record what kind of thing we've got in a builder node so its facing can be rotated properly - if minetest.get_item_group(node.name, "digtron") == 4 then - local build_item = node_image.meta.inventory.main[1] - if build_item ~= "" then - local build_item_def = minetest.registered_nodes[ItemStack(build_item):get_name()] - node_image.build_item_paramtype2 = build_item_def.paramtype2 - end - end - return node_image -end - -function DigtronLayout.create(pos, player) - local self = {} - setmetatable(self,DigtronLayout) - - --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.inventories = {} - self.fuelstores = {} - self.diggers = {} - self.builders = {} - self.extents = {} - 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 - - 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 if a controller node is created that contains fuel or inventory or whatever. - - 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 - - -- 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 - 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 - --buildtron array is next to unloaded nodes, too dangerous to do anything. Abort. - return nil - 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 - if digtron.lava_impassible == true then - self.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 - 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 - table.insert(self.inventories, node_image) - elseif group_number == 3 then - table.insert(self.diggers, node_image) - elseif group_number == 4 then - table.insert(self.builders, node_image) - elseif group_number == 5 then - table.insert(self.fuelstores, node_image) - elseif group_number == 6 then - table.insert(self.inventories, node_image) - table.insert(self.fuelstores, node_image) - end - - if minetest.is_protected(pos, player:get_player_name()) and not minetest.check_player_privs(player, "protection_bypass") then - self.contains_protected_node = true - end - - -- update extents - 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) - - --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. - self.traction = self.traction + 1 - end - - testpos, _ = to_test:pop() - end - - return self -end - ------------------------------------------------------------------------- --- Rotation - -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 - } -} - - --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) -local rotate_pos = function(axis, direction, pos) - if axis == "x" then - if direction < 0 then - return {x= pos.x, y= -pos.z, z= pos.y} - else - return {x= pos.x, y= pos.z, z= -pos.y} - end - elseif axis == "y" then - if direction < 0 then - return {x= -pos.z, y= pos.y, z= pos.x} - else - return {x= pos.z, y= pos.y, z= -pos.x} - end - else - if direction < 0 then - return {x= -pos.y, y= pos.x, z= pos.z} - else - return {x= pos.y, y= -pos.x, z= pos.z} - end - end -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 - node_image.meta.fields.build_facing = wallmounted_rotate[axis][direction][tonumber(node_image.meta.fields.build_facing)] - elseif node_image.build_item_paramtype2 == "facedir" then - node_image.meta.fields.build_facing = facedir_rotate[axis][direction][tonumber(node_image.meta.fields.build_facing)] - 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 - local pos = vector.subtract(node_image.pos, origin) - pos = rotate_pos(axis, direction, pos) - -- Move back to original reference frame - node_image.pos = vector.add(pos, origin) - - 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) -function DigtronLayout.rotate_layout_image(self, facedir) - -- To convert this into the direction the "top" of the axel node is pointing in: - -- 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 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}, - } - local params = top[math.floor(facedir/4)] - - for k, node_image in pairs(self.all) do - rotate_node_image(node_image, self.controller, params.axis, params.dir, self.old_pos_pointset) - end - return self -end - ------------------------------------------------------------------------------------------------ --- Translation - -function DigtronLayout.move_layout_image(self, facing, player_name) - local extents = self.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(self.all) do - self.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 - 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. - -- TODO: log - end -end - ------------------------------------------------------------------------------------------------ --- Writing to world - -function DigtronLayout.can_write_layout_image(self) - for k, node_image in pairs(self.all) do - -- 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) - and not minetest.registered_nodes[minetest.get_node(node_image.pos).name].buildable_to then - return false - --check if we're moving into a protected node - elseif self.protected:get(node_image.pos) then - return false - end - end - return true -end - -function DigtronLayout.write_layout_image(self) - -- destroy the old digtron - local oldpos, _ = self.old_pos_pointset:pop() - while oldpos ~= nil do - local old_def = minetest.registered_nodes[minetest.get_node(oldpos).name] - minetest.remove_node(oldpos) - if old_def.after_dig_node ~= nil then - old_def.after_dig_node(oldpos) - end - oldpos, _ = self.old_pos_pointset:pop() - end - - -- create the new one - for k, node_image in pairs(self.all) do - minetest.add_node(node_image.pos, node_image.node) - minetest.get_meta(node_image.pos):from_table(node_image.meta) - - local new_def = minetest.registered_nodes[node_image.node.name] - if new_def.after_place_node ~= nil then - new_def.after_place_node(node_image.pos) - end - end -end - -- cgit v1.2.3 From 435f21458ccf4d91fd5ec179bdbc5d1aa6d2945c Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Tue, 10 Jan 2017 00:36:39 -0700 Subject: the old movement code is now unneeded. --- util.lua | 1 - util_movement.lua | 98 ------------------------------------------------------- 2 files changed, 99 deletions(-) delete mode 100644 util_movement.lua diff --git a/util.lua b/util.lua index acebc59..ac7fe3d 100644 --- a/util.lua +++ b/util.lua @@ -3,7 +3,6 @@ digtron = {} dofile( minetest.get_modpath( "digtron" ) .. "/util_item_place_node.lua" ) -- separated out to avoid potential for license complexity -dofile( minetest.get_modpath( "digtron" ) .. "/util_movement.lua" ) -- separated out simply for tidiness, there's some big code in there dofile( minetest.get_modpath( "digtron" ) .. "/util_execute_cycle.lua" ) -- separated out simply for tidiness, there's some big code in there -- Apparently node_sound_metal_defaults is a newer thing, I ran into games using an older version of the default mod without it. diff --git a/util_movement.lua b/util_movement.lua deleted file mode 100644 index f7c36dd..0000000 --- a/util_movement.lua +++ /dev/null @@ -1,98 +0,0 @@ -digtron.move_node = function(pos, newpos, player_name) - -- Moves nodes, preserving digtron metadata and inventory - local node = minetest.get_node(pos) - local node_def = minetest.registered_nodes[node.name] - local oldnode = minetest.get_node(newpos) - minetest.log("action", string.format("%s moves %s from (%d, %d, %d) to (%d, %d, %d), displacing %s", player_name, node.name, pos.x, pos.y, pos.z, newpos.x, newpos.y, newpos.z, oldnode.name)) - minetest.add_node(newpos, { name=node.name, param1=node.param1, param2=node.param2 }) - -- copy the metadata - local oldmeta_table = minetest.get_meta(pos):to_table() - local meta = minetest.get_meta(newpos) - meta:from_table(oldmeta_table) - meta:set_string("waiting", nil) -- If a controller moves another controller that's in the waiting state, clear the waiting state otherwise it might get stuck like that (we've moved it away from the target of the pending 'clear the waiting state' delegate call). That means you can run a digtron as fast as you want by rapidly clicking between two different controllers, but shhh - don't tell the player that. - - -- Move the little floaty entity inside the builders - if minetest.get_item_group(node.name, "digtron") == 4 then - digtron.update_builder_item(newpos) - end - - if node_def.after_place_node then - node_def.after_place_node(newpos) - end - - -- remove node from old position - minetest.remove_node(pos) - if node_def.after_dig_node then - node_def.after_dig_node(pos) - end -end - -digtron.move_digtron = function(facing, digtrons, extents, nodes_dug, player_name) - -- move everything. Note! order is important or they'll step on each other, that's why this has complicated loops and filtering. - -- Nodes are moved in a "caterpillar" pattern - front plane first, then next plane back, then next plane back, etc. - -- positions in the digtron list will be updated when this method executes. Note that the inventories list shares - -- references to the node position tables in the digtron list, so it will reflect the updates too. - local dir = digtron.facedir_to_dir_map[facing] - local increment - local filter - local index - local target - if dir == 1 then -- z+ - filter = "z" - increment = -1 - index = extents.max_z - target = extents.min_z - extents.max_z = extents.max_z + 1 - extents.min_z = extents.min_z + 1 - elseif dir == 2 then -- x+ - filter = "x" - increment = -1 - index = extents.max_x - target = extents.min_x - extents.max_x = extents.max_x + 1 - extents.min_x = extents.min_x + 1 - elseif dir == 3 then -- z- - filter = "z" - increment = 1 - index = extents.min_z - target = extents.max_z - extents.max_z = extents.max_z - 1 - extents.min_z = extents.min_z - 1 - elseif dir == 4 then -- x- - filter = "x" - increment = 1 - index = extents.min_x - target = extents.max_x - extents.max_x = extents.max_x - 1 - extents.min_x = extents.min_x - 1 - elseif dir == 5 then -- y- - filter = "y" - increment = 1 - index = extents.min_y - target = extents.max_y - extents.max_y = extents.max_y - 1 - extents.min_y = extents.min_y - 1 - elseif dir == 6 then -- y+ - filter = "y" - increment = -1 - index = extents.max_y - target = extents.min_y - extents.max_y = extents.max_y + 1 - extents.min_y = extents.min_y + 1 - end - - while index ~= target + increment do - for k, location in pairs(digtrons) do - if location[filter] == index then - local newpos = digtron.find_new_pos(location, facing) - digtron.move_node(location, newpos, player_name) - --By updating the digtron position table in-place we also update all the special node tables as well - digtrons[k].x= newpos.x - digtrons[k].y= newpos.y - digtrons[k].z= newpos.z - nodes_dug:set(newpos.x, newpos.y, newpos.z, false) -- we've moved a digtron node into this space, mark it so that we don't dig it. - end - end - index = index + increment - end -end \ No newline at end of file -- cgit v1.2.3 From e008aed60c3e4e5875c94617a6cc6d588f283582 Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Thu, 12 Jan 2017 00:50:17 -0700 Subject: Update axle to new layout class, drop dug items in the right place --- class_layout.lua | 4 +++- node_axle.lua | 8 ++++---- textures/digtron_builder.png | Bin 783 -> 835 bytes util_execute_cycle.lua | 10 +++++----- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/class_layout.lua b/class_layout.lua index b7311c5..85beba3 100644 --- a/class_layout.lua +++ b/class_layout.lua @@ -15,7 +15,9 @@ local get_node_image = function(pos, node) local build_item = node_image.meta.inventory.main[1] if build_item ~= "" then local build_item_def = minetest.registered_nodes[ItemStack(build_item):get_name()] - node_image.build_item_paramtype2 = build_item_def.paramtype2 + if build_item_def ~= nil then + node_image.build_item_paramtype2 = build_item_def.paramtype2 + end end end return node_image diff --git a/node_axle.lua b/node_axle.lua index 9e62b07..ab36c97 100644 --- a/node_axle.lua +++ b/node_axle.lua @@ -36,10 +36,10 @@ minetest.register_node("digtron:axle", { -- Been too soon since last time the digtron rotated. return end - local image = digtron.get_layout_image(pos, clicker) - digtron.rotate_layout_image(image, node.param2) - if digtron.can_write_layout_image(image) then - digtron.write_layout_image(image) + local image = DigtronLayout.create(pos, clicker) + image:rotate_layout_image(node.param2) + if image:can_write_layout_image() then + image:write_layout_image() minetest.sound_play("whirr", {gain=1.0, pos=pos}) meta = minetest.get_meta(pos) diff --git a/textures/digtron_builder.png b/textures/digtron_builder.png index 446b76d..60fd68e 100644 Binary files a/textures/digtron_builder.png and b/textures/digtron_builder.png differ diff --git a/util_execute_cycle.lua b/util_execute_cycle.lua index 42bf410..316a003 100644 --- a/util_execute_cycle.lua +++ b/util_execute_cycle.lua @@ -221,11 +221,6 @@ digtron.execute_dig_cycle = function(pos, clicker) -- All tests passed, ready to go for real! minetest.sound_play("construction", {gain=1.0, pos=pos}) - -- store or drop the products of the digger heads - for _, itemname in pairs(items_dropped) do - digtron.place_in_inventory(itemname, layout.inventories, pos) - end - -- if the player is standing within the array or next to it, move him too. local move_player = move_player_test(layout, clicker) @@ -250,6 +245,11 @@ digtron.execute_dig_cycle = function(pos, clicker) clicker:moveto(digtron.find_new_pos(clicker:getpos(), facing), true) end + -- store or drop the products of the digger heads + for _, itemname in pairs(items_dropped) do + minetest.debug("placing in inventory", itemname) + digtron.place_in_inventory(itemname, layout.inventories, oldpos) + end local building_fuel_cost = 0 local strange_failure = false -- cgit v1.2.3