2019-02-03 08:58:27 -05:00
|
|
|
-- LUALOCALS < ---------------------------------------------------------
|
2019-02-10 00:20:55 -05:00
|
|
|
local ItemStack, ipairs, minetest, nodecore, pairs, type
|
|
|
|
= ItemStack, ipairs, minetest, nodecore, pairs, type
|
2019-02-03 08:58:27 -05:00
|
|
|
-- LUALOCALS > ---------------------------------------------------------
|
|
|
|
|
|
|
|
local function craftcheck(recipe, pos, node, data, xx, xz, zx, zz)
|
|
|
|
local function rel(x, y, z)
|
|
|
|
return {
|
|
|
|
x = pos.x + xx * x + zx * z,
|
|
|
|
y = pos.y + y,
|
|
|
|
z = pos.z + xz * x + zz * z
|
|
|
|
}
|
|
|
|
end
|
2019-02-10 00:20:55 -05:00
|
|
|
data.wield = ItemStack(data.wield or data.crafter and data.crafter:get_wielded_item())
|
2019-02-03 08:58:27 -05:00
|
|
|
if recipe.check and not recipe.check(pos, data, rel) then return end
|
2019-02-10 00:20:55 -05:00
|
|
|
if recipe.wield and (not data.wield or not nodecore.match(
|
|
|
|
{stack = data.wield}, recipe.wield)) then return end
|
2019-02-03 08:58:27 -05:00
|
|
|
if recipe.normal then
|
|
|
|
if data.pointed.type ~= "node" or
|
|
|
|
recipe.normal.y ~= data.pointed.above.y - data.pointed.under.y then return end
|
|
|
|
local rx = recipe.normal.x * xx + recipe.normal.z * zx
|
|
|
|
if rx ~= data.pointed.above.x - data.pointed.under.x then return end
|
|
|
|
local rz = recipe.normal.x * xz + recipe.normal.z * zz
|
|
|
|
if rz ~= data.pointed.above.z - data.pointed.under.z then return end
|
|
|
|
end
|
2019-03-07 16:47:12 -05:00
|
|
|
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 nodecore.match(p, v.match) then return end
|
|
|
|
end
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
local mindur = recipe.duration or 0
|
|
|
|
if recipe.toolgroups then
|
2019-02-10 00:20:55 -05:00
|
|
|
if not data.wield then return end
|
|
|
|
local dg = data.wield:get_tool_capabilities().groupcaps
|
2019-02-09 22:44:56 -05:00
|
|
|
local t
|
|
|
|
for gn, lv in pairs(recipe.toolgroups) do
|
|
|
|
local gt = dg[gn]
|
|
|
|
gt = gt and gt.times
|
|
|
|
gt = gt and gt[lv]
|
|
|
|
if gt and (not t or t > gt) then t = gt end
|
|
|
|
end
|
|
|
|
if not t then return end
|
|
|
|
mindur = mindur + t
|
|
|
|
end
|
2019-02-09 23:36:39 -05:00
|
|
|
if mindur > 0 and (not data.duration or data.duration < mindur) then
|
2019-02-09 22:44:56 -05:00
|
|
|
if data.inprogress then return data.inprogress(data, recipe) end
|
|
|
|
return
|
|
|
|
end
|
2019-02-10 00:20:55 -05:00
|
|
|
if recipe.before then recipe.before(pos, rel, data) end
|
2019-02-03 08:58:27 -05:00
|
|
|
for _, v in pairs(recipe.nodes) do
|
|
|
|
if v.replace then
|
|
|
|
local p = rel(v.x, v.y, v.z)
|
|
|
|
local r = v.replace
|
|
|
|
while type(r) == "function" do
|
|
|
|
r = r(p, v)
|
|
|
|
end
|
|
|
|
if r and type(r) == "string" then
|
|
|
|
r = {name = r}
|
|
|
|
end
|
|
|
|
if r then minetest.set_node(p, r) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if recipe.items then
|
|
|
|
for _, v in pairs(recipe.items) do
|
2019-02-10 00:20:55 -05:00
|
|
|
nodecore.item_eject(rel(v.x or 0, v.y or 0, v.z or 0),
|
|
|
|
v.name, v.scatter, v.count, v.velocity)
|
2019-02-03 08:58:27 -05:00
|
|
|
end
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
if recipe.consumewield then
|
|
|
|
nodecore.consume_wield(data.crafter, recipe.consumewield)
|
|
|
|
elseif recipe.toolgroups and recipe.toolwear and data.crafter then
|
|
|
|
nodecore.wear_wield(data.crafter, recipe.toolgroups, recipe.toolwear)
|
|
|
|
end
|
2019-02-03 08:58:27 -05:00
|
|
|
if recipe.after then recipe.after(pos, rel, data) end
|
2019-02-23 17:00:42 -05:00
|
|
|
if nodecore.player_stat_add then
|
|
|
|
nodecore.player_stat_add(1, data.crafter, "craft", recipe.label)
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
minetest.log((data.crafter and data.crafter:get_player_name() or "unknown")
|
|
|
|
.. " completed recipe \"" .. recipe.label .. "\" at " ..
|
|
|
|
minetest.pos_to_string(pos) .. " upon " .. node.name)
|
2019-02-03 08:58:27 -05:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2019-02-03 12:10:27 -05:00
|
|
|
function nodecore.craft_check(pos, node, data)
|
2019-02-03 08:58:27 -05:00
|
|
|
data = data or {}
|
2019-02-03 12:10:27 -05:00
|
|
|
local function go(rc, xx, xz, zx, zz)
|
2019-02-03 08:58:27 -05:00
|
|
|
return craftcheck(rc, pos, node, data, xx, xz, zx, zz)
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
node.x = pos.x
|
|
|
|
node.y = pos.y
|
|
|
|
node.z = pos.z
|
2019-02-10 00:20:55 -05:00
|
|
|
data.node = node
|
2019-02-03 11:24:06 -05:00
|
|
|
for _, rc in ipairs(nodecore.craft_recipes) do
|
|
|
|
if nodecore.match(node, rc.root.match) and data.action == rc.action then
|
2019-02-03 12:10:27 -05:00
|
|
|
if go(rc, 1, 0, 0, 1) then return true end
|
2019-02-03 08:58:27 -05:00
|
|
|
if not rc.norotate then
|
2019-02-03 12:10:27 -05:00
|
|
|
if go(rc, 0, -1, 1, 0) then return true end
|
|
|
|
if go(rc, -1, 0, 0, -1) then return true end
|
|
|
|
if go(rc, 0, 1, -1, 0) then return true end
|
2019-02-03 08:58:27 -05:00
|
|
|
if not rc.nomirror then
|
2019-02-03 12:10:27 -05:00
|
|
|
if go(rc, -1, 0, 0, 1) then return true end
|
|
|
|
if go(rc, 0, 1, 1, 0) then return true end
|
|
|
|
if go(rc, 1, 0, 0, -1) then return true end
|
|
|
|
if go(rc, 0, -1, -1, 0) then return true end
|
2019-02-03 08:58:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|