~fgaz/minetest-extruder

8ee8b4d0423154cef58500ea287545e2a5b95792 — Francesco Gazzetta 1 year, 2 months ago 191d3ca
Add option to select only nodes of the same type
1 files changed, 23 insertions(+), 7 deletions(-)

M init.lua
M init.lua => init.lua +23 -7
@@ 18,14 18,17 @@ local function meta_set_amount(meta, amount)
end


local function visit_wrapped(visited, to_visit, direction, neighbors)
local function visit_wrapped(visited, to_visit, direction, neighbors, node_name)
  local pos = table.remove(to_visit)
  while pos do
    if visited[vector.to_string(pos)] == nil then
      local above = vector.add(pos, direction)
      local node = minetest.get_node(pos)
      local node_above = minetest.get_node(above)
      if node.name == "air" or node.name == "ignore" or node_above.name ~= "air" then
      if node.name == "air" or
         node.name == "ignore" or
         node_above.name ~= "air" or
         (node_name and node_name ~= node.name) then
        visited[vector.to_string(pos)] = false
      else
        visited[vector.to_string(pos)] = node


@@ 40,6 43,7 @@ end

local function visit(pos, direction, meta)
  local diagonal = lib.meta_get_bool(meta, "diagonal")
  local same_node = lib.meta_get_bool(meta, "same_node")

  local neighbors = lib.get_perpendiculars(direction)
  if diagonal then


@@ 48,8 52,10 @@ local function visit(pos, direction, meta)
    end
  end

  local node_name = same_node and minetest.get_node(pos).name or nil

  local visited = {}
  visit_wrapped(visited, {pos}, direction, neighbors)
  visit_wrapped(visited, {pos}, direction, neighbors, node_name)
  return visited
end



@@ 90,11 96,12 @@ end

local settings_formspec = [[
  formspec_version[3]
  size[8,4.5]
  button_exit[0.5,3.2;7,0.8;confirm;Confirm]
  size[8,5.2]
  button_exit[0.5,3.9;7,0.8;confirm;Confirm]
  field[0.5,0.7;4,0.8;amount;Extrusion amount;%d]
  checkbox[0.5,2;overwrite;Allow overwriting;%s]
  checkbox[0.5,2.7;diagonal;Select through vertices (diagonally);%s]
  checkbox[0.5,3.4;same_node;Only select nodes of the same type;%s]
]]

minetest.register_tool(modname .. ":extruder", {


@@ 113,14 120,14 @@ minetest.register_tool(modname .. ":extruder", {
    use(placer, pointed_thing, itemstack:get_meta(), false)
  end,

  -- TODO add "only extrude/select nodes of the same type"
  on_secondary_use = function(itemstack, placer, _)
    local meta = itemstack:get_meta()
    minetest.show_formspec(placer:get_player_name(), modname .. ":settings",
      string.format(settings_formspec,
        meta_get_amount(meta),
        tostring(lib.meta_get_bool(meta, "overwrite")),
        tostring(lib.meta_get_bool(meta, "diagonal"))
        tostring(lib.meta_get_bool(meta, "diagonal")),
        tostring(lib.meta_get_bool(meta, "same_node"))
      )
    )
  end,


@@ 163,10 170,19 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
    diagonal = lib.meta_get_bool(meta, "diagonal")
  end

  local same_node
  if fields.same_node then
    same_node = fields.same_node == "true"
    lib.meta_set_bool(meta, "same_node", same_node)
  else
    same_node = lib.meta_get_bool(meta, "same_node")
  end

  meta:set_string("count_meta", table.concat({
    amount == 1 and "" or tostring(amount),
    overwrite and "!" or "",
    diagonal and "✳" or "",
    same_node and "=" or "",
  }, ""))

  inventory:set_stack("main", wield_index, itemstack)