2022-06-13 06:10:36 +02:00
|
|
|
-- for registering recipe schemas
|
2022-06-17 02:46:09 +02:00
|
|
|
-- TODO: should register schemas w/ unified_inventory and i3 and whatever else,
|
|
|
|
-- and hide the recipes for the individual nodes (possibly a setting for such)
|
2022-06-14 01:11:20 +02:00
|
|
|
local api = stairsplus.api
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
local recipes_in_creative_inventory = stairsplus.settings.recipes_in_creative_inventory
|
|
|
|
|
|
|
|
api.registered_recipe_schemas = {}
|
|
|
|
api.registered_on_register_craft_schemas = {}
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
local function is_valid_output(item, shapes)
|
|
|
|
local item_name = item:match("^([^ ]+)")
|
|
|
|
|
2022-06-17 01:32:55 +02:00
|
|
|
return item_name and (shapes[item_name] or item_name == "node" or item_name:match(":"))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_valid_item(item, shapes)
|
|
|
|
return is_valid_output(item, shapes) or item == ""
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function verify_schema(schema)
|
|
|
|
local problems = {}
|
|
|
|
|
|
|
|
if not (schema.type == "shaped" or schema.type == "shapeless" or schema.type == nil) then
|
|
|
|
table.insert(problems, ("unimplemented schema type %q"):format(schema.type))
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
if not is_valid_output(schema.output, api.registered_shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
table.insert(problems, ("don't know how to handle output %q"):format(schema.output))
|
|
|
|
end
|
|
|
|
|
|
|
|
if schema.replacements then
|
|
|
|
for _, replacement in ipairs(schema.replacements) do
|
|
|
|
for _, item in ipairs(replacement) do
|
2022-06-16 01:18:11 +02:00
|
|
|
if not is_valid_item(item, api.registered_shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
table.insert(problems, ("don't know how to handle replacement item %q"):format(item))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if schema.type == "shapeless" then
|
2022-06-14 20:17:06 +02:00
|
|
|
for _, item in ipairs(schema.recipe) do
|
2022-06-16 01:18:11 +02:00
|
|
|
if not is_valid_item(item, api.registered_shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
table.insert(problems, ("don't know how to handle craft item %q"):format(item))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
2022-06-14 20:17:06 +02:00
|
|
|
for _, row in ipairs(schema.recipe) do
|
2022-06-14 01:11:20 +02:00
|
|
|
for _, item in ipairs(row) do
|
2022-06-16 01:18:11 +02:00
|
|
|
if not is_valid_item(item, api.registered_shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
table.insert(problems, ("don't know how to handle craft item %q"):format(item))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #problems > 0 then
|
|
|
|
return table.concat(problems, ", ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
function api.register_on_register_craft_schema(func)
|
2022-06-17 21:22:47 +02:00
|
|
|
table.insert(api.registered_on_register_craft_schemas, func)
|
2022-06-17 02:46:09 +02:00
|
|
|
end
|
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
function api.register_craft_schema(schema)
|
|
|
|
local problems = verify_schema(schema)
|
2022-06-14 20:17:06 +02:00
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
if problems then
|
|
|
|
error(problems)
|
|
|
|
end
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
stairsplus.log("info", "registering craft schema %s", minetest.serialize(schema):sub(#("return ")))
|
2022-06-16 01:18:11 +02:00
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
table.insert(api.registered_recipe_schemas, schema)
|
2022-06-17 02:46:09 +02:00
|
|
|
|
|
|
|
for _, func in ipairs(api.registered_on_register_craft_schemas) do
|
|
|
|
func(schema)
|
|
|
|
end
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function has_the_right_shapes(schema, shapes)
|
2022-06-16 01:18:11 +02:00
|
|
|
if not is_valid_output(schema.output, shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if schema.replacements then
|
|
|
|
for _, replacement in ipairs(schema.replacements) do
|
|
|
|
for _, item in ipairs(replacement) do
|
2022-06-14 20:17:06 +02:00
|
|
|
if not is_valid_item(item, shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if schema.type == "shapeless" then
|
2022-06-14 20:17:06 +02:00
|
|
|
for _, item in ipairs(schema.recipe) do
|
|
|
|
if not is_valid_item(item, shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif schema.type == "shaped" or schema.type == nil then
|
2022-06-14 20:17:06 +02:00
|
|
|
for _, row in ipairs(schema.recipe) do
|
2022-06-14 01:11:20 +02:00
|
|
|
for _, item in ipairs(row) do
|
2022-06-14 20:17:06 +02:00
|
|
|
if not is_valid_item(item, shapes) then
|
2022-06-14 01:11:20 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-06-17 01:32:55 +02:00
|
|
|
local function register_for_schema(node, schema)
|
2022-06-14 01:11:20 +02:00
|
|
|
local recipe = table.copy(schema)
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
recipe.output = api.get_schema_recipe_item(node, recipe.output)
|
2022-06-14 01:11:20 +02:00
|
|
|
|
|
|
|
if recipe.replacements then
|
|
|
|
for _, replacement in ipairs(recipe.replacements) do
|
|
|
|
for i, item in ipairs(replacement) do
|
2022-06-17 02:46:09 +02:00
|
|
|
replacement[i] = api.get_schema_recipe_item(node, item)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if recipe.type == "shapeless" then
|
2022-06-14 20:17:06 +02:00
|
|
|
for i, item in ipairs(recipe.recipe) do
|
2022-06-17 02:46:09 +02:00
|
|
|
recipe.recipe[i] = api.get_schema_recipe_item(node, item)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
elseif recipe.type == "shaped" or recipe.type == nil then
|
2022-06-17 01:32:55 +02:00
|
|
|
for _, row in ipairs(recipe.recipe) do
|
2022-06-14 01:11:20 +02:00
|
|
|
for i, item in ipairs(row) do
|
2022-06-17 02:46:09 +02:00
|
|
|
row[i] = api.get_schema_recipe_item(node, item)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-16 01:18:11 +02:00
|
|
|
stairsplus.log("info", "registering recipe %s", minetest.serialize(recipe):sub(#("return ")))
|
|
|
|
|
2022-06-17 02:46:09 +02:00
|
|
|
if not recipes_in_creative_inventory then
|
|
|
|
-- i don't think anything supports this but...
|
|
|
|
recipe.groups = {not_in_creative_inventory = 1}
|
|
|
|
end
|
|
|
|
|
2022-06-14 01:11:20 +02:00
|
|
|
minetest.register_craft(recipe)
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.register_schema_crafts_for_node(node)
|
2022-06-17 01:32:55 +02:00
|
|
|
stairsplus.log("info", "registering schema crafts for %q", node)
|
2022-06-14 01:11:20 +02:00
|
|
|
local shapes = api.get_shapes_hash(node)
|
|
|
|
for _, schema in ipairs(api.registered_recipe_schemas) do
|
|
|
|
if has_the_right_shapes(schema, shapes) then
|
2022-06-17 21:22:47 +02:00
|
|
|
stairsplus.log("verbose", "using schema %s", minetest.serialize(schema):sub(#("return ")))
|
2022-06-17 01:32:55 +02:00
|
|
|
register_for_schema(node, schema)
|
2022-06-14 01:11:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function shapes_match(a, b)
|
|
|
|
local a_shapes = api.get_shapes(a)
|
|
|
|
local b_shapes = api.get_shapes(b)
|
|
|
|
|
|
|
|
if #a_shapes ~= #b_shapes then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #a_shapes do
|
|
|
|
if a_shapes[i] ~= b_shapes[i] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function api.register_crafts_for_shapes(def)
|
|
|
|
if def.type == "cooking" then
|
|
|
|
assert(
|
|
|
|
shapes_match(def.output, def.recipe),
|
|
|
|
("error: shapes of %s and %s do not match"):format(def.output, def.recipe)
|
|
|
|
)
|
|
|
|
|
|
|
|
local shapes = api.get_shapes(def.recipe)
|
|
|
|
|
|
|
|
for _, shape in ipairs(shapes) do
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "cooking",
|
2022-06-17 02:46:09 +02:00
|
|
|
output = api.get_schema_recipe_item(def.output, shape),
|
|
|
|
recipe = api.get_schema_recipe_item(def.recipe, shape),
|
2022-06-15 01:30:41 +02:00
|
|
|
cooktime = def.cooktime(api.registered_shapes[shape].eighths),
|
2022-06-17 21:22:47 +02:00
|
|
|
groups = (not recipes_in_creative_inventory) and {not_in_creative_inventory = 1} or nil
|
2022-06-14 01:11:20 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif def.type == "fuel" then
|
|
|
|
local shapes = api.get_shapes(def.recipe)
|
|
|
|
|
|
|
|
for _, shape in ipairs(shapes) do
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "fuel",
|
2022-06-17 02:46:09 +02:00
|
|
|
recipe = api.get_schema_recipe_item(def.recipe, shape),
|
2022-06-15 01:30:41 +02:00
|
|
|
burntime = def.burntime(api.registered_shapes[shape].eighths),
|
2022-06-17 21:22:47 +02:00
|
|
|
groups = (not recipes_in_creative_inventory) and {not_in_creative_inventory = 1} or nil
|
2022-06-14 01:11:20 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
error(("unsupported recipe type %s"):format(def.type))
|
|
|
|
end
|
|
|
|
end
|