nodecore-cd2025/mods/nc_api/util_node_is.lua
Aaron Suen 739ad29acc Be a little more forgiving on tool wear for stonifying wood tools.
Allow up to 2% wear.  Sometimes you're diggng with a stone tool and
the tip breaks off and you don't notice (there's no sound yet),
so you accidentally dig a little with the wood tool and get it a
little dirty.  Let's be a little forgiving, and allow the stone
tip applicaton to also apply a little "repair" to the underlying
wood tool.
2019-01-26 11:39:55 -05:00

77 lines
2.1 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, pairs, type
= minetest, nodecore, pairs, type
-- LUALOCALS > ---------------------------------------------------------
local node_is_skip = {
name = true,
param2 = true,
param = true,
groups = true,
visinv = true,
count = true,
wear = true,
metadata = true,
}
function nodecore.node_is(thing, match)
if not thing.name then
local p = { }
for k, v in pairs(minetest.get_node(thing)) do
p[k] = v
end
for k, v in pairs(thing) do
p[k] = v
end
thing = p
end
while type(match) == "function" do
match = match(thing)
if not match then return end
if match == true then return thing end
end
if type(match) == "string" then match = {name = match} end
if match.name and thing.name ~= match.name then return end
if match.param2 and thing.param2 ~= match.param2 then return end
if match.param and thing.param ~= match.param then return end
if match.count and thing.count ~= match.count then return end
if match.wear then
if match.wear < 1 then match.wear = match.wear * 65535 end
if thing.wear > match.wear then return end
end
if match.metadata and thing.metadata ~= match.metadata then return end
local def = minetest.registered_items[thing.name]
if match.groups then
if not def.groups then return end
for k, v in pairs(match.groups) do
if v == true then
if not def.groups[k] then return end
elseif v == false then
if def.groups[k] then return end
else
if def.groups[k] ~= v then return end
end
end
end
for k, v in pairs(match) do
if not node_is_skip[k] then
if def[k] ~= v then return end
end
end
if match.visinv then
local stack = minetest.get_meta(thing)
:get_inventory():get_stack("solo", 1)
if not stack or stack:is_empty() then return end
local p = { }
p.name = stack:get_name()
p.count = stack:get_count()
p.wear = stack:get_wear()
p.metadata = stack:get_metadata()
return nodecore.node_is(p, match.visinv)
end
return thing
end
function nodecore.buildable_to(node_or_pos)
return nodecore.node_is(node_or_pos, {buildable_to = true})
end