2019-02-03 08:58:27 -05:00
|
|
|
-- LUALOCALS < ---------------------------------------------------------
|
2019-03-12 21:52:00 -04:00
|
|
|
local ItemStack, ipairs, math, minetest, nodecore, pairs, type
|
|
|
|
= ItemStack, ipairs, math, minetest, nodecore, pairs, type
|
|
|
|
local math_ceil
|
|
|
|
= math.ceil
|
2019-02-03 08:58:27 -05:00
|
|
|
-- LUALOCALS > ---------------------------------------------------------
|
|
|
|
|
2019-03-22 21:55:02 -04:00
|
|
|
local function addgroups(sum, pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local def = minetest.registered_items[node.name] or {}
|
|
|
|
if not def.groups then return end
|
|
|
|
for k, v in pairs(def.groups) do
|
|
|
|
sum[k] = (sum[k] or 0) + v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-03 08:58:27 -05:00
|
|
|
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-03-22 21:55:02 -04:00
|
|
|
if recipe.touchgroups then
|
|
|
|
local sum = {}
|
|
|
|
addgroups(sum, rel(1, 0, 0))
|
|
|
|
addgroups(sum, rel(-1, 0, 0))
|
|
|
|
addgroups(sum, rel(0, 1, 0))
|
|
|
|
addgroups(sum, rel(0, -1, 0))
|
|
|
|
addgroups(sum, rel(0, 0, 1))
|
|
|
|
addgroups(sum, rel(0, 0, -1))
|
|
|
|
for k, v in pairs(recipe.touchgroups) do
|
|
|
|
local w = sum[k] or 0
|
|
|
|
if v > 0 and w < v then return end
|
|
|
|
if v <= 0 and w > -v then return end
|
|
|
|
end
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
local mindur = recipe.duration or 0
|
2019-03-22 21:55:02 -04:00
|
|
|
if type(mindur) == "function" then mindur = mindur(recipe, pos, node, data) end
|
2019-02-09 22:44:56 -05:00
|
|
|
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-03-22 21:55:02 -04:00
|
|
|
if mindur > 0 then
|
|
|
|
if not data.duration then return end
|
|
|
|
local dur = data.duration
|
|
|
|
if type(dur) == "function" then dur = dur(data, recipe) end
|
|
|
|
if not dur or dur < mindur then
|
|
|
|
if data.inprogress then data.inprogress(data, recipe) end
|
|
|
|
return 1
|
|
|
|
end
|
2019-02-09 22:44:56 -05:00
|
|
|
end
|
2019-03-22 22:55:30 -04:00
|
|
|
if data.before then data.before(pos, rel, data, recipe) end
|
|
|
|
if recipe.before then recipe.before(pos, rel, data, recipe) 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
|
2019-03-12 21:52:00 -04:00
|
|
|
if v.match.excess then
|
|
|
|
local s = nodecore.stack_get(p)
|
|
|
|
local x = s:get_count() - (v.match.count or 1)
|
|
|
|
local n = math_ceil(x / 4)
|
|
|
|
while x > 0 do
|
|
|
|
if n > x then n = x end
|
|
|
|
x = x - n
|
|
|
|
s:set_count(n)
|
|
|
|
nodecore.item_eject(p, s, 5)
|
|
|
|
end
|
|
|
|
end
|
2019-03-14 20:08:08 -04:00
|
|
|
if r then
|
|
|
|
minetest.set_node(p, r)
|
2019-03-14 21:48:29 -04:00
|
|
|
nodecore.node_sound(p, "place")
|
2019-03-14 20:08:08 -04:00
|
|
|
end
|
2019-02-03 08:58:27 -05:00
|
|
|
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-03-22 22:55:30 -04:00
|
|
|
if recipe.after then recipe.after(pos, rel, data, recipe) end
|
|
|
|
if data.after then data.after(pos, rel, data, recipe) 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-03-09 19:40:02 -05:00
|
|
|
local function tryall(rc, pos, node, data)
|
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-03-09 19:40:02 -05:00
|
|
|
local r = go(rc, 1, 0, 0, 1)
|
|
|
|
if r then return r end
|
|
|
|
if not rc.norotate then
|
|
|
|
r = go(rc, 0, -1, 1, 0)
|
|
|
|
or go(rc, -1, 0, 0, -1)
|
|
|
|
or go(rc, 0, 1, -1, 0)
|
|
|
|
if r then return r end
|
|
|
|
if not rc.nomirror then
|
|
|
|
r = go(rc, -1, 0, 0, 1)
|
|
|
|
or go(rc, 0, 1, 1, 0)
|
|
|
|
or go(rc, 1, 0, 0, -1)
|
|
|
|
or go(rc, 0, -1, -1, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
function nodecore.craft_check(pos, node, data)
|
|
|
|
data = data or {}
|
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
|
2019-03-22 21:55:02 -04:00
|
|
|
if data.action == rc.action
|
|
|
|
and nodecore.match(node, rc.root.match) then
|
2019-03-09 19:40:02 -05:00
|
|
|
local r = tryall(rc, pos, node, data)
|
|
|
|
if r then return r == true end
|
2019-02-03 08:58:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|