~fgaz/minetest-extruder

476341a7d1836a4bbd5c853d3092cab43fab35f2 — Francesco Gazzetta 1 year, 11 months ago 14bae4d
Move some generic functions to lib.lua
3 files changed, 76 insertions(+), 67 deletions(-)

M .luacheckrc
M init.lua
A lib.lua
M .luacheckrc => .luacheckrc +2 -0
@@ 1,10 1,12 @@
local used_minetest_fields = {
  get_modpath = {},
  get_node = {},
  set_node = {},
  remove_node = {},
  register_tool = {},
  register_privilege = {},
  check_player_privs = {},
  privs_to_string = {},
  chat_send_player = {},
  show_formspec = {},
  register_on_player_receive_fields = {},

M init.lua => init.lua +5 -67
@@ 1,64 1,12 @@
local modname = "extruder"
extruder = {}

local lib = dofile(minetest.get_modpath(modname) .. "/lib.lua")

minetest.register_privilege("extruder")

-- TODO look into integrating with stuff like worldedit undo

-- Get the four unit vectors parallel to axes that are perpendicular to a given one (also parallel to an axis)
local function get_perpendiculars(vec)
  if vec.x ~= 0 then
    return {
      vector.new(0,  1,  0),
      vector.new(0, -1,  0),
      vector.new(0,  0,  1),
      vector.new(0,  0, -1),
    }
  end
  if vec.y ~= 0 then
    return {
      vector.new( 1, 0,  0),
      vector.new(-1, 0,  0),
      vector.new( 0, 0,  1),
      vector.new( 0, 0, -1),
    }
  end
  -- if vec.z ~= 0 then
  return {
    vector.new( 1,  0, 0),
    vector.new(-1,  0, 0),
    vector.new( 0,  1, 0),
    vector.new( 0, -1, 0),
  }
end

-- Get the four diagonal (wrt axes) "manhattan-unit" vectors perpendicular to a given one (parallel to an axis)
local function get_diagonals(vec)
  if vec.x ~= 0 then
    return {
      vector.new(0,  1,  1),
      vector.new(0,  1, -1),
      vector.new(0, -1,  1),
      vector.new(0, -1, -1),
    }
  end
  if vec.y ~= 0 then
    return {
      vector.new( 1, 0,  1),
      vector.new( 1, 0, -1),
      vector.new(-1, 0,  1),
      vector.new(-1, 0, -1),
    }
  end
  -- if vec.z ~= 0 then
  return {
    vector.new( 1,  1, 0),
    vector.new( 1, -1, 0),
    vector.new(-1,  1, 0),
    vector.new(-1, -1, 0),
  }
end

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


@@ 82,9 30,9 @@ end
local function visit(pos, direction, meta)
  local diagonal = meta:get_int("diagonal") == 1

  local neighbors = get_perpendiculars(direction)
  local neighbors = lib.get_perpendiculars(direction)
  if diagonal then
    for _,v in ipairs(get_diagonals(direction)) do
    for _,v in ipairs(lib.get_diagonals(direction)) do
      table.insert(neighbors, v)
    end
  end


@@ 119,19 67,9 @@ local function extrude(surface, direction, meta, remove)
  end
end

local function check_privs_with_msg(player_or_name)
  local success, _ = minetest.check_player_privs(player_or_name, "extruder")
  if not success then
    local player_name = type(player_or_name) == "string" and player_or_name or player_or_name:get_player_name()
    minetest.chat_send_player(player_name, modname .. ": Missing privileges: extruder")
    return false
  end
  return true
end

local function use(placer, pointed_thing, meta, 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
  if not lib.check_privs_with_msg(placer, "extruder") then return end
  local direction = vector.direction(pointed_thing.under, pointed_thing.above)
  local surface = visit(pointed_thing.under, direction, meta)
  -- TODO ask for confirmation if surface is too big (block limit field/checkbox in formspec?)

A lib.lua => lib.lua +69 -0
@@ 0,0 1,69 @@
local lib = {}

function lib.check_privs_with_msg(player_or_name, privs)
  local success, missing_privs = minetest.check_player_privs(player_or_name, privs)
  if not success then
    local player_name = type(player_or_name) == "string" and player_or_name or player_or_name:get_player_name()
    minetest.chat_send_player(player_name, "Missing privileges: " .. minetest.privs_to_string(missing_privs))
    return false
  end
  return true
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)
  if vec.x ~= 0 then
    return {
      vector.new(0,  1,  0),
      vector.new(0, -1,  0),
      vector.new(0,  0,  1),
      vector.new(0,  0, -1),
    }
  end
  if vec.y ~= 0 then
    return {
      vector.new( 1, 0,  0),
      vector.new(-1, 0,  0),
      vector.new( 0, 0,  1),
      vector.new( 0, 0, -1),
    }
  end
  -- if vec.z ~= 0 then
  return {
    vector.new( 1,  0, 0),
    vector.new(-1,  0, 0),
    vector.new( 0,  1, 0),
    vector.new( 0, -1, 0),
  }
end

-- Get the four diagonal (wrt axes) "manhattan-unit" vectors
-- perpendicular to a given one (parallel to an axis)
function lib.get_diagonals(vec)
  if vec.x ~= 0 then
    return {
      vector.new(0,  1,  1),
      vector.new(0,  1, -1),
      vector.new(0, -1,  1),
      vector.new(0, -1, -1),
    }
  end
  if vec.y ~= 0 then
    return {
      vector.new( 1, 0,  1),
      vector.new( 1, 0, -1),
      vector.new(-1, 0,  1),
      vector.new(-1, 0, -1),
    }
  end
  -- if vec.z ~= 0 then
  return {
    vector.new( 1,  1, 0),
    vector.new( 1, -1, 0),
    vector.new(-1,  1, 0),
    vector.new(-1, -1, 0),
  }
end

return lib