Add some helpers (just in case)

This commit is contained in:
kilbith 2016-02-04 16:02:28 +01:00
parent 640c03c9bc
commit 720cdff72c
8 changed files with 44 additions and 15 deletions

View File

@ -35,7 +35,7 @@ xdecor.register("cauldron_empty", {
itemstack:replace("bucket:bucket_empty")
end
end,
collision_box = xdecor.pixelnodebox(16, cauldron_cbox)
collision_box = xdecor.pixelbox(16, cauldron_cbox)
})
xdecor.register("cauldron_idle", {
@ -44,7 +44,7 @@ xdecor.register("cauldron_idle", {
tiles = {"xdecor_cauldron_top_idle.png", "xdecor_cauldron_sides.png"},
drop = "xdecor:cauldron_empty",
infotext = "Cauldron (idle)",
collision_box = xdecor.pixelnodebox(16, cauldron_cbox),
collision_box = xdecor.pixelbox(16, cauldron_cbox),
on_rightclick = fill_water_bucket
})
@ -59,7 +59,7 @@ xdecor.register("cauldron_boiling_water", {
animation = {type="vertical_frames", length=3.0} },
"xdecor_cauldron_sides.png"
},
collision_box = xdecor.pixelnodebox(16, cauldron_cbox),
collision_box = xdecor.pixelbox(16, cauldron_cbox),
on_rightclick = fill_water_bucket
})
@ -74,7 +74,7 @@ xdecor.register("cauldron_soup", {
animation = {type="vertical_frames", length=3.0} },
"xdecor_cauldron_sides.png"
},
collision_box = xdecor.pixelnodebox(16, cauldron_cbox),
collision_box = xdecor.pixelbox(16, cauldron_cbox),
on_rightclick = function(pos, node, clicker, itemstack)
local inv = clicker:get_inventory()
local wield_item = clicker:get_wielded_item()

31
handlers/helpers.lua Normal file
View File

@ -0,0 +1,31 @@
-- Returns the greatest numeric key in a table.
function xdecor.maxn(T)
local n = 0
for k in pairs(T) do
if k > n then n = k end
end
return n
end
-- Returns the length of an hash table.
function xdecor.tablelen(T)
local n = 0
for _ in pairs(T) do n = n + 1 end
return n
end
-- Deep copy of a table. Borrowed from mesecons mod (https://github.com/Jeija/minetest-mod-mesecons).
function xdecor.tablecopy(T)
if type(T) ~= "table" then return T end -- No need to copy.
local new = {}
for k, v in pairs(T) do
if type(v) == "table" then
new[k] = xdecor.tablecopy(v)
else
new[k] = item
end
end
return new
end

View File

@ -18,10 +18,10 @@ xdecor.nodebox = {
null = { type = "fixed", fixed = { 0, 0, 0, 0, 0, 0 } }
}
xdecor.pixelnodebox = function(size, boxes)
xdecor.pixelbox = function(size, boxes)
local fixed = {}
for _, box in pairs(boxes) do
local x, y, z, w, h, l = unpack(box)
local x, y, z, w, h, l = unpack(box) -- `unpack` has been changed to `table.unpack` in newest Lua versions.
fixed[#fixed+1] = {
(x / size) - 0.5,
(y / size) - 0.5,

View File

@ -2,6 +2,7 @@
xdecor = {}
local modpath = minetest.get_modpath("xdecor")
dofile(modpath.."/handlers/helpers.lua")
dofile(modpath.."/handlers/nodeboxes.lua")
dofile(modpath.."/handlers/registration.lua")
dofile(modpath.."/chess.lua")

View File

@ -85,10 +85,7 @@ xdecor.register("frame", {
on_rotate = screwdriver.disallow,
sunlight_propagates = true,
inventory_image = "xdecor_frame.png",
node_box = {
type = "fixed",
fixed = {-0.5, -0.5, 0.4375, 0.5, 0.5, 0.5}
},
node_box = xdecor.nodebox.slab_z(0.9375),
tiles = {
"xdecor_wood.png", "xdecor_wood.png", "xdecor_wood.png",
"xdecor_wood.png", "xdecor_wood.png", "xdecor_frame.png"

View File

@ -426,7 +426,7 @@ xdecor.register("stonepath", {
on_rotate = screwdriver.rotate_simple,
sounds = default.node_sound_stone_defaults(),
sunlight_propagates = true,
node_box = xdecor.pixelnodebox(16, {
node_box = xdecor.pixelbox(16, {
{8, 0, 8, 6, .5, 6},
{1, 0, 1, 6, .5, 6},
{1, 0, 10, 5, .5, 5},
@ -453,7 +453,7 @@ xdecor.register("table", {
tiles = {"xdecor_wood.png"},
groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
sounds = default.node_sound_wood_defaults(),
node_box = xdecor.pixelnodebox(16, {
node_box = xdecor.pixelbox(16, {
{0, 14, 0, 16, 2, 16}, {5.5, 0, 5.5, 5, 14, 6}
})
})

View File

@ -64,7 +64,7 @@ xdecor.register("chair", {
sounds = default.node_sound_wood_defaults(),
groups = {choppy=3, oddly_breakable_by_hand=2, flammable=3},
on_rotate = screwdriver.rotate_simple,
node_box = xdecor.pixelnodebox(16, {
node_box = xdecor.pixelbox(16, {
{3, 0, 11, 2, 16, 2},
{11, 0, 11, 2, 16, 2},
{5, 9, 11.5, 6, 6, 1},

View File

@ -106,7 +106,7 @@ function worktable:craftguide_formspec(meta, pagenum, item, recipe_num, filter)
local items = minetest.get_all_craft_recipes(item)[recipe_num].items
local width = minetest.get_all_craft_recipes(item)[recipe_num].width
if width == 0 then width = math.min(3, #items) end
local rows = math.ceil(table.maxn(items) / width)
local rows = math.ceil(table.maxn(items) / width) -- Lua 5.3 removed `table.maxn`, use `xdecor.maxn` in case of failure.
local function is_group(item)
if item:find("^group:") then return "G" end
@ -369,7 +369,7 @@ for node in pairs(minetest.registered_nodes) do
sounds = def.sounds,
tiles = tiles,
groups = groups,
node_box = xdecor.pixelnodebox(16, {unpack(d, 3)}),
node_box = xdecor.pixelbox(16, {unpack(d, 3)}), -- `unpack` has been changed to `table.unpack` in newest Lua versions.
sunlight_propagates = true,
on_place = minetest.rotate_node,
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)