improve pummeling.

pummeling definition tables now has lists of item id:s and groups, the
same way e.g. ABM:s do, that determine what items are involved in a
recipe.

update existing (in-use) pummel recipes to make use of this.
This commit is contained in:
trans_soup 2023-10-17 14:53:08 +02:00
parent 45f59f5e03
commit c31d6cf5d0
4 changed files with 37 additions and 26 deletions

View File

@ -7,36 +7,44 @@ local pummel_recipes = {}
function api.register_pummel_recipe (def)
local def = def or {}
def.label = def.label or "unnamed pummel recipe"
-- TODO: require `def.name` to be specified and unique.
def.label = def.label or "unlabeled pummel recipe"
-- TODO: throw errors when these defs are invalid instead of just returning.
if not def.used_item then return false end
if not def.target_node then return false end
if not def.used then return false end
if not def.target then return false end
if not type(def.on_success) == "function" then return false end
local key = def.used_item
def.index_key = key
def["type"] = "pummel"
pummel_recipes[key] = pummel_recipes[key] or {}
table.insert(pummel_recipes[key], def)
table.insert(pummel_recipes, def)
table.insert(api.registered_recipes, def)
end
-- TODO: add support for pummel recipes using groups instead of just specific node names.
function api.pummel_check (pos, used_item, target_node)
local key = used_item
if not pummel_recipes[key] then return false end
-- TODO: extract these 2 functions into general api
local function starts_with (str, start)
return string.sub(str, 1, string.len(start)) == start
end
local potential_recipes = pummel_recipes[key]
for _, def in pairs(potential_recipes) do
if def.target_node == target_node.name then
if type(def.check) == "function" and not def.check(pos, used_item, target_node) then
local function item_matches (nodename, options)
return blockgame.any(options, function (option)
if nodename == option then return true end
if starts_with(option, "group:") then
local group = string.sub(option, 7)
return minetest.get_item_group(nodename, group) > 0
end
return false
end)
end
-- TODO: add support for pummel recipes using groups instead of just specific node names.
function api.pummel_check (pos, used, target_node)
for _, def in pairs(pummel_recipes) do
if item_matches(used, def.used) and item_matches(target_node.name, def.target) then
if type(def.check) == "function" and not def.check(pos, used, target_node) then
return false
end
def.on_success(pos, used_item, target_node)
def.on_success(pos, used, target_node)
return true
end
end

View File

@ -1,9 +1,10 @@
local modname = minetest.get_current_modname()
blockgame.crafting.register_pummel_recipe({
name = modname .. ":craft_tile",
label = "pummel stone into tile",
used_item = "bg_terrain:cobblestone",
target_node = "bg_terrain:cobblestone",
used = {"group:stoney"},
target = {"bg_terrain:cobblestone"},
check = function (pos, used_node, target_node)
local below_node = minetest.get_node(pos + blockgame.vector.dirs.down)
local stoney_value = minetest.get_item_group(below_node.name, "stoney")
@ -15,9 +16,10 @@ blockgame.crafting.register_pummel_recipe({
})
blockgame.crafting.register_pummel_recipe({
name = modname .. ":craft_bricks",
label = "pummel tile into bricks",
used_item = "bg_terrain:cobblestone",
target_node = modname .. ":tile",
used = {"group:stoney"},
target = {modname .. ":tile"},
check = function (pos, used_node, target_node)
local below_node = minetest.get_node(pos + blockgame.vector.dirs.down)
local stoney_value = minetest.get_item_group(below_node.name, "stoney")

View File

@ -1,2 +1,2 @@
load_file("node")
load_file("recipes")
load_file("recipe")

View File

@ -1,9 +1,10 @@
local modname = minetest.get_current_modname()
blockgame.crafting.register_pummel_recipe({
label = "pummel log into plank",
used_item = "bg_terrain:cobblestone",
target_node = "bg_tree:log",
name = modname .. ":chop_log",
label = "chop log into plank",
used = {"group:stoney"},
target = {"bg_tree:log"},
check = function (pos, used_node, target_node)
for _, side in pairs(blockgame.vector.sides) do
local output_pos = pos + side