~fgaz/minetest-extruder

66ab9d8643fed22c861e89c94d6a81a631a71da6 — Francesco Gazzetta 1 year, 4 months ago c4d52f9
Add remove feature
1 files changed, 19 insertions(+), 12 deletions(-)

M init.lua
M init.lua => init.lua +19 -12
@@ 58,13 58,17 @@ local function visit(pos, direction)
  return visited
end

local function extrude(surface, direction)
local function extrude(surface, direction, remove)
  -- TODO use VoxelManip (and accumulate two of the bounding coordinates in visit()?)
  -- or at least bulk_set_node
  for pos_str,node in pairs(surface) do
    if node then
      local pos = vector.from_string(pos_str)
      minetest.set_node(vector.add(pos, direction), node)
      if remove then
        minetest.remove_node(pos)
      else
        minetest.set_node(vector.add(pos, direction), node)
      end
    end
  end
end


@@ 79,21 83,24 @@ local function check_privs_with_msg(player_or_name)
  return true
end

local function use(placer, pointed_thing, remove)
  if placer == nil or pointed_thing == nil or pointed_thing.type ~= "node" then return end
  if not check_privs_with_msg(placer) then return end
  local direction = vector.direction(pointed_thing.under, pointed_thing.above)
  local surface = visit(pointed_thing.under, direction)
  -- TODO ask for confirmation if surface is too big (block limit field/checkbox in formspec?)
  -- confirmation can be "click again" (store previous above and under)
  extrude(surface, direction, remove)
end

minetest.register_tool(modname .. ":extruder", {
  description = "Extruder tool\nClick to extrude a surface, right click to adjust options",
  inventory_image = "extruder.png",
  stack_max = 1,
  liquids_pointable = true,
  -- MAYBE switch to on_place since a node should be pointed?
  on_use = function(_, placer, pointed_thing)
    if placer == nil or pointed_thing == nil or pointed_thing.type ~= "node" then return end
    if not check_privs_with_msg(placer) then return end
    local direction = vector.direction(pointed_thing.under, pointed_thing.above)
    local surface = visit(pointed_thing.under, direction)
    -- TODO ask for confirmation if surface is too big (block limit field/checkbox in formspec?)
    -- confirmation can be "click again" (store previous above and under)
    extrude(surface, direction)
  end,

  on_use = function(_, placer, pointed_thing) use(placer, pointed_thing, true) end,
  on_place = function(_, placer, pointed_thing) use(placer, pointed_thing, false) end,

  -- TODO add formspec with "amount" field and "go/select through vertices" checkbox
  --on_secondary_use = function(itemstack, placer, pointed_thing)