2023-10-11 07:17:21 +00:00
|
|
|
blockgame.crafting = blockgame.crafting or {}
|
|
|
|
local api = blockgame.crafting
|
|
|
|
|
|
|
|
local recipes = {}
|
|
|
|
api.registered_recipes = recipes
|
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
local stack_recipes = {}
|
|
|
|
|
2023-10-11 17:12:47 +00:00
|
|
|
function api.register_stack_recipe (top_node, bottom_node, result, consumes_top)
|
2023-10-11 07:17:21 +00:00
|
|
|
local consumes_top = consumes_top
|
|
|
|
if consumes_top == nil then consumes_top = true end
|
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
stack_recipes[top_node] = stack_recipes[top_node] or {}
|
2023-10-11 07:17:21 +00:00
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
local def = {
|
2023-10-11 07:17:21 +00:00
|
|
|
["type"] = "stack_two_nodes",
|
|
|
|
index_key = top_node,
|
|
|
|
top_node = top_node,
|
|
|
|
bottom_node = bottom_node,
|
|
|
|
result = result,
|
|
|
|
consumes_top = consumes_top,
|
2023-10-17 12:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(stack_recipes[top_node], def)
|
|
|
|
table.insert(api.registered_recipes, def)
|
2023-10-11 07:17:21 +00:00
|
|
|
end
|
|
|
|
|
2023-10-11 17:12:47 +00:00
|
|
|
function api.handle_placement (pos, new_node, placer, old_node, itemstack, pointed_thing)
|
2023-10-17 12:23:44 +00:00
|
|
|
if stack_recipes[new_node.name] == nil then return false end
|
2023-10-11 07:17:21 +00:00
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
for _, recipe in pairs(stack_recipes[new_node.name]) do
|
2023-10-11 07:17:21 +00:00
|
|
|
local result = api.handle_stack_recipe(recipe, pos, new_node)
|
|
|
|
if result then return not recipe.consumes_top end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
blockgame.register_on_placenode(function (...)
|
|
|
|
return blockgame.crafting.handle_placement(...)
|
|
|
|
end)
|
|
|
|
|
2023-10-11 17:12:47 +00:00
|
|
|
function api.handle_stack_recipe (recipe, pos, top_node)
|
2023-10-11 07:17:21 +00:00
|
|
|
if top_node.name ~= recipe.top_node then return false end
|
|
|
|
|
|
|
|
local down = vector.add(pos, vector.new(0, -1, 0))
|
|
|
|
local below = minetest.get_node(down)
|
|
|
|
if below.name ~= recipe.bottom_node then return false end
|
|
|
|
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
minetest.set_node(down, recipe.result)
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2023-10-11 09:15:08 +00:00
|
|
|
|
|
|
|
-- pummeling is heavily based on nodecore by Warr1024 (both the mechanic itself and the code here implementing it).
|
|
|
|
|
|
|
|
local pummel_recipes = {}
|
|
|
|
|
2023-10-11 17:12:47 +00:00
|
|
|
function api.register_pummel_recipe (def)
|
2023-10-11 09:15:08 +00:00
|
|
|
local def = def or {}
|
|
|
|
|
|
|
|
def.label = def.label or "unnamed pummel recipe"
|
|
|
|
|
|
|
|
-- TODO: throw errors when these defs are invalid instead of just returning.
|
2023-10-11 09:39:39 +00:00
|
|
|
if not def.used_item then return false end
|
2023-10-11 09:15:08 +00:00
|
|
|
if not def.target_node then return false end
|
|
|
|
if not type(def.on_success) == "function" then return false end
|
|
|
|
|
2023-10-11 09:39:39 +00:00
|
|
|
local key = def.used_item
|
|
|
|
def.index_key = key
|
2023-10-11 09:15:08 +00:00
|
|
|
|
2023-10-17 12:23:44 +00:00
|
|
|
def["type"] = "pummel"
|
|
|
|
|
2023-10-11 09:15:08 +00:00
|
|
|
pummel_recipes[key] = pummel_recipes[key] or {}
|
2023-10-17 12:23:44 +00:00
|
|
|
|
2023-10-11 09:15:08 +00:00
|
|
|
table.insert(pummel_recipes[key], def)
|
2023-10-17 12:23:44 +00:00
|
|
|
table.insert(api.registered_recipes, def)
|
2023-10-11 09:15:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: add support for pummel recipes using groups instead of just specific node names.
|
2023-10-11 17:12:47 +00:00
|
|
|
function api.pummel_check (pos, used_item, target_node)
|
2023-10-11 09:39:39 +00:00
|
|
|
local key = used_item
|
2023-10-11 09:15:08 +00:00
|
|
|
if not pummel_recipes[key] then return false end
|
|
|
|
|
|
|
|
local potential_recipes = pummel_recipes[key]
|
|
|
|
for _, def in pairs(potential_recipes) do
|
|
|
|
if def.target_node == target_node.name then
|
2023-10-11 09:39:39 +00:00
|
|
|
if type(def.check) == "function" and not def.check(pos, used_item, target_node) then
|
2023-10-11 09:15:08 +00:00
|
|
|
return false
|
|
|
|
end
|
2023-10-11 09:39:39 +00:00
|
|
|
def.on_success(pos, used_item, target_node)
|
2023-10-11 09:15:08 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local pummeling = {}
|
|
|
|
|
|
|
|
-- NOTE: might wanna change this to somehow store pummels per-node instead of per-player?
|
2023-10-13 07:31:55 +00:00
|
|
|
blockgame.register_on_dignode(function (_, _, digger)
|
2023-10-11 09:15:08 +00:00
|
|
|
if not (digger and digger:is_player()) then return end
|
|
|
|
pummeling[digger:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
2023-10-13 07:31:55 +00:00
|
|
|
blockgame.register_on_punchnode(function (pos, node, puncher, pointed)
|
2023-10-11 09:15:08 +00:00
|
|
|
if (not puncher:is_player()) or puncher:get_player_control().sneak then return end
|
|
|
|
local player_name = puncher:get_player_name()
|
|
|
|
-- if not minetest.interact(player_name) then return end
|
|
|
|
if not minetest.check_player_privs(player_name, "interact") then return end
|
|
|
|
|
|
|
|
node = node or minetest.get_node(pos)
|
|
|
|
local node_def = minetest.registered_items[node.name] or {}
|
|
|
|
if not node_def.pointable then return end
|
|
|
|
|
|
|
|
local wield = puncher:get_wielded_item()
|
|
|
|
local now = minetest.get_us_time() / 1000000
|
|
|
|
|
|
|
|
local pummel_data = {
|
|
|
|
action = "pummel",
|
|
|
|
crafter = puncher,
|
|
|
|
crafter_name = player_name,
|
|
|
|
pos = pos,
|
|
|
|
pointed = pointed,
|
|
|
|
node = node,
|
|
|
|
node_def = node_def,
|
|
|
|
start = now,
|
|
|
|
wield = wield:get_name() .. " " .. wield:get_count(),
|
|
|
|
count = 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
local old_data = pummeling[player_name]
|
|
|
|
local hash = minetest.hash_node_position
|
|
|
|
if old_data and hash(old_data.pos) == hash(pummel_data.pos)
|
|
|
|
and hash(old_data.pointed.above) == hash(pummel_data.pointed.above)
|
|
|
|
and hash(old_data.pointed.under) == hash(pummel_data.pointed.under)
|
|
|
|
and old_data.wield == pummel_data.wield
|
|
|
|
and old_data.last >= (now - 3)
|
|
|
|
then pummel_data = old_data
|
|
|
|
end
|
|
|
|
|
|
|
|
pummel_data.count = pummel_data.count + 1
|
|
|
|
pummel_data.last = now
|
|
|
|
pummel_data.duration = now - pummel_data.start - 1
|
|
|
|
pummeling[player_name] = pummel_data
|
|
|
|
|
|
|
|
if pummel_data.count < 2 then return end
|
|
|
|
|
2023-10-11 09:39:39 +00:00
|
|
|
if api.pummel_check(pos, wield:get_name(), minetest.get_node(pointed.under)) then
|
2023-10-11 09:15:08 +00:00
|
|
|
pummeling[player_name] = nil
|
|
|
|
end
|
|
|
|
end)
|