~fgaz/minetest-extruder

bd49155d2c0cc2c2dd2c6976649bd4e8d15120d4 — Francesco Gazzetta 1 year, 2 months ago d2e85b3
Use metadata helpers
2 files changed, 33 insertions(+), 16 deletions(-)

M init.lua
M lib.lua
M init.lua => init.lua +25 -16
@@ 7,6 7,17 @@ minetest.register_privilege("extruder")

-- TODO look into integrating with stuff like worldedit undo

local function meta_get_amount(meta)
  return math.max(1, meta:get_int("amount"))
end

local function meta_set_amount(meta, amount)
  if (not amount) or (amount < 1) then amount = 1 end
  meta:set_int("amount", amount)
  return amount
end


local function visit_wrapped(visited, to_visit, direction, neighbors)
  local pos = table.remove(to_visit)
  while pos do


@@ 28,7 39,7 @@ local function visit_wrapped(visited, to_visit, direction, neighbors)
end

local function visit(pos, direction, meta)
  local diagonal = meta:get_int("diagonal") == 1
  local diagonal = lib.meta_get_bool(meta, "diagonal")

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


@@ 43,8 54,8 @@ local function visit(pos, direction, meta)
end

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

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


@@ 105,14 116,11 @@ minetest.register_tool(modname .. ":extruder", {
  -- TODO add "only extrude/select nodes of the same type"
  on_secondary_use = function(itemstack, placer, _)
    local meta = itemstack:get_meta()
    local amount = math.max(1, meta:get_int("amount"))
    local overwrite = meta:get_int("overwrite") == 1
    local diagonal = meta:get_int("diagonal") == 1
    minetest.show_formspec(placer:get_player_name(), modname .. ":settings",
      string.format(settings_formspec,
        amount,
        overwrite and "true" or "false",
        diagonal and "true" or "false"
        meta_get_amount(meta),
        tostring(lib.meta_get_bool(meta, "overwrite")),
        tostring(lib.meta_get_bool(meta, "diagonal"))
      )
    )
  end,


@@ 132,26 140,27 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
  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)
    -- We assign it back because meta_set_amount sanitizes the value too
    amount = meta_set_amount(meta, amount)
  else
    amount = math.max(1, meta:get_int("amount"))
    amount = meta_get_amount(meta)
  end

  -- TODO do all the checkboxes in a loop
  local overwrite
  if fields.overwrite then
    overwrite = fields.overwrite == "true"
    meta:set_int("overwrite", overwrite and 1 or 0)
    lib.meta_set_bool(meta, "overwrite", overwrite)
  else
    overwrite = meta:get_int("overwrite") == 1
    overwrite = lib.meta_get_bool(meta, "overwrite")
  end

  local diagonal
  if fields.diagonal then
    diagonal = fields.diagonal == "true"
    meta:set_int("diagonal", diagonal and 1 or 0)
    lib.meta_set_bool(meta, "diagonal", diagonal)
  else
    diagonal = meta:get_int("diagonal") == 1
    diagonal = lib.meta_get_bool(meta, "diagonal")
  end

  meta:set_string("count_meta", table.concat({

M lib.lua => lib.lua +8 -0
@@ 10,6 10,14 @@ function lib.check_privs_with_msg(player_or_name, privs)
  return true
end

-- https://github.com/minetest/minetest/pull/12894
function lib.meta_get_bool(meta, name)
  return minetest.is_yes(meta:get(name) or "true")
end
function lib.meta_set_bool(meta, name, value)
  meta:set_string(name, value and "true" or "false")
end

-- Get the four unit vectors parallel to axes
-- that are perpendicular to a given one (also parallel to an axis)
function lib.get_perpendiculars(vec)