~fgaz/minetest-extruder

919738fb927053493e22285e2fb9bc950f42a42b — Francesco Gazzetta 2 years ago c143c8d
Add overwrite setting
1 files changed, 34 insertions(+), 13 deletions(-)

M init.lua
M init.lua => init.lua +34 -13
@@ 60,6 60,7 @@ end

local function extrude(surface, direction, meta, remove)
  local amount = math.max(1, meta:get_int("amount"))
  local overwrite = meta:get_int("overwrite") == 1

  -- TODO use VoxelManip (and accumulate two of the bounding coordinates in visit()?)
  -- or at least bulk_set_node


@@ 72,9 73,9 @@ local function extrude(surface, direction, meta, remove)
          minetest.remove_node(pos)
          pos = vector.subtract(pos, direction)
        else
          -- MAYBE check that it's air, do not overwrite other nodes
          -- or add a setting
          minetest.set_node(pos, node)
          if overwrite or minetest.get_node(pos).name == "air" then
            minetest.set_node(pos, node)
          end
          pos = vector.add(pos, direction)
        end
      end


@@ 104,9 105,10 @@ end

local settings_formspec = [[
  formspec_version[3]
  size[4,3]
  button_exit[0.5,1.8;3,0.8;confirm;Confirm]
  size[4,3.5]
  button_exit[0.5,2.4;3,0.8;confirm;Confirm]
  field[0.5,0.6;3,0.8;amount;Extrusion amount;%d]
  checkbox[0.5,1.9;overwrite;Allow overwriting;%s]
]]

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


@@ 128,9 130,14 @@ minetest.register_tool(modname .. ":extruder", {
  -- TODO add "go/select through vertices" checkbox
  -- TODO add "only extrude/select nodes of the same type"
  on_secondary_use = function(itemstack, placer, _)
    local amount = math.max(1, itemstack:get_meta():get_int("amount"))
    local meta = itemstack:get_meta()
    local amount = math.max(1, meta:get_int("amount"))
    local overwrite = meta:get_int("overwrite") == 1
    minetest.show_formspec(placer:get_player_name(), modname .. ":settings",
      string.format(settings_formspec, amount)
      string.format(settings_formspec,
        amount,
        overwrite and "true" or "false"
      )
    )
  end,
})


@@ 142,14 149,28 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
  local wield_index = player:get_wield_index()
  local itemstack = inventory:get_stack("main", wield_index)
  if (not itemstack) or (itemstack:get_name() ~= (modname .. ":extruder")) then return end
  local meta = itemstack:get_meta()

  local amount
  if fields.amount then
    amount = tonumber(fields.amount)
    if (not amount) or (amount < 1) then amount = 1 end
    meta:set_int("amount", amount)
  else
    amount = math.max(1, meta:get_int("amount"))
  end

  local amount = tonumber(fields.amount)
  if (not amount) or (amount < 1) then amount = 1 end
  local amount_str = amount == 1 and "" or tostring(amount)
  itemstack:get_meta():set_int("amount", amount)
  local overwrite
  if fields.overwrite then
    overwrite = fields.overwrite == "true"
    meta:set_int("overwrite", overwrite and 1 or 0)
  else
    overwrite = meta:get_int("overwrite") == 1
  end

  itemstack:get_meta():set_string("count_meta", table.concat({
    amount_str,
  meta:set_string("count_meta", table.concat({
    amount == 1 and "" or tostring(amount),
    overwrite and "!" or "",
  }, ""))

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