]> git.lizzy.rs Git - xdecor.git/commitdiff
Add some helpers (just in case)
authorkilbith <jeanpatrick.guerrero@gmail.com>
Thu, 4 Feb 2016 15:02:28 +0000 (16:02 +0100)
committerkilbith <jeanpatrick.guerrero@gmail.com>
Thu, 4 Feb 2016 15:30:49 +0000 (16:30 +0100)
cooking.lua
handlers/helpers.lua [new file with mode: 0644]
handlers/nodeboxes.lua
init.lua
itemframe.lua
nodes.lua
sitting.lua
worktable.lua

index 41624aa9ec0d9d3612e164bdffba56fd9a7528e2..c1213c7d065bd74e9d919cfaebe7a60f56167197 100644 (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()
diff --git a/handlers/helpers.lua b/handlers/helpers.lua
new file mode 100644 (file)
index 0000000..716c2bf
--- /dev/null
@@ -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
+
index 9f0953fc916769ca02dfde07533a38f414fe4179..214281444e6f250419e64f25d0cbc99c65b900bf 100644 (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,
index 1f9738645005a070b52d5848b17c62f7ca2b5678..293a02762b8188649be0a06735f43646a92347cb 100644 (file)
--- a/init.lua
+++ b/init.lua
@@ -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")
index 4da174107acb6cb302da327045fb2951a8cd696c..9b795f341c554fc2e4b36b0a4de0332abdb1d93f 100644 (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"
index 20619bf686a24be272636300e161b6e4ab9e879c..53537c4256036d0f7c76d0e8239c4668ea57de3c 100644 (file)
--- a/nodes.lua
+++ b/nodes.lua
@@ -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}
        })
 })
index d1495de9bb782205ece6d0f316f0c894f27e1725..51d2a16786febe50da7dd852f049a5bc2aa0a5cb 100644 (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},
index a596fe108231c3c3f6855bd5191fc8507fe6ab6f..cd486ee3ccfcb5de9ad0255579648077e00f4285 100644 (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)