Unify some near-duplicate API's.

This commit is contained in:
Aaron Suen 2018-12-30 20:09:33 -05:00
parent f7b3cab04f
commit 9fb8b9ed8c
2 changed files with 10 additions and 21 deletions

View File

@ -44,23 +44,6 @@ function nodecore.register_craft(recipe)
table_insert(recipes, min, recipe)
end
local function match(pos, node, m)
node = node or minetest.get_node(pos)
if type(m) == "string" then
return node.name == m
end
if type(m) == "table" then
for k, v in pairs(m) do
if node[k] ~= v then return end
end
return true
end
if type(m) == "function" then
return m(pos, node)
end
error("unsupported match type " .. type(m))
end
local function craftcheck(recipe, pos, node, placer, pointed_thing, xx, xz, zx, zz)
local function rel(x, y, z)
return {
@ -78,7 +61,7 @@ local function craftcheck(recipe, pos, node, placer, pointed_thing, xx, xz, zx,
for _, v in pairs(recipe.nodes) do
if v ~= recipe.root and v.match then
local p = rel(v.x, v.y, v.z)
if not match(p, nil, v.match) then return end
if not nodecore.node_is(p, v.match) then return end
end
end
for _, v in pairs(recipe.nodes) do
@ -105,7 +88,7 @@ end
local function craftloop(pos, node, placer, pointed_thing)
for _, rc in ipairs(recipes) do
if match(rc.root, node, rc.root.match) then
if nodecore.node_is(node, rc.root.match) then
if craftcheck(rc, pos, node, placer, pointed_thing,
1, 0, 0, 1) then return true end
if not rc.norotate then

View File

@ -1,8 +1,8 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, math, minetest, nodecore, pairs, type
= ipairs, math, minetest, nodecore, pairs, type
= ipairs, math, minetest, nodecore, pairs, type
local math_random
= math.random
= math.random
-- LUALOCALS > ---------------------------------------------------------
for k, v in pairs(minetest) do
@ -23,6 +23,12 @@ end
local node_is_skip = {name = true, param2 = true, param = true, groups = true}
function nodecore.node_is(node_or_pos, match)
if not node_or_pos.name then node_or_pos = minetest.get_node(node_or_pos) end
while type(match) == "function" do
match = match(node_or_pos)
if not match then return end
if match == true then return true end
end
if type(match) == "string" then match = {name = match} end
if match.name and node_or_pos.name ~= match.name then return end
if match.param2 and node_or_pos.param2 ~= match.param2 then return end
if match.param and node_or_pos.param ~= match.param then return end