2018-12-13 10:19:08 +00:00
|
|
|
local have_ui = minetest.get_modpath("unified_inventory")
|
2018-12-17 10:46:13 +00:00
|
|
|
local have_cg = minetest.get_modpath("craftguide") and craftguide and craftguide.register_craft
|
2018-06-18 07:09:43 +00:00
|
|
|
|
|
|
|
elepm.craft = {}
|
|
|
|
elepm.craft.types = {}
|
|
|
|
|
2018-12-17 17:48:45 +00:00
|
|
|
local function item_string( ... )
|
|
|
|
-- body
|
|
|
|
end
|
|
|
|
|
2018-06-18 07:09:43 +00:00
|
|
|
function elepm.register_craft_type(name, def)
|
|
|
|
elepm.craft.types[name] = {
|
|
|
|
inputs = def.inputs or 2,
|
|
|
|
description = def.description or name,
|
2021-06-18 21:49:57 +00:00
|
|
|
overview = def.overview or name,
|
2018-07-11 19:01:31 +00:00
|
|
|
time = def.time or 0,
|
|
|
|
gui_name = def.gui_name,
|
2018-06-18 07:09:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 10:46:13 +00:00
|
|
|
elepm.craft[name] = {}
|
|
|
|
|
|
|
|
-- Don't register cooking or fuel types externally.
|
|
|
|
if name == "cooking" or name == "fuel" then return end
|
|
|
|
|
2018-12-13 10:19:08 +00:00
|
|
|
if have_ui and unified_inventory.register_craft_type then
|
|
|
|
unified_inventory.register_craft_type(name, {
|
|
|
|
description = def.description or name,
|
2018-12-17 12:25:41 +00:00
|
|
|
icon = def.icon or "elepower_machine_side.png",
|
2018-12-13 10:19:08 +00:00
|
|
|
width = def.inputs or 2,
|
|
|
|
height = 1,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-12-17 10:46:13 +00:00
|
|
|
if have_cg then
|
|
|
|
craftguide.register_craft_type(name, {
|
|
|
|
description = def.description,
|
2018-12-17 12:25:41 +00:00
|
|
|
icon = def.icon or "elepower_machine_side.png",
|
2018-12-17 10:46:13 +00:00
|
|
|
})
|
|
|
|
end
|
2018-06-18 07:09:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function elepm.register_craft(craftdef)
|
|
|
|
if not craftdef.type or not elepm.craft.types[craftdef.type] then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2018-08-12 12:42:32 +00:00
|
|
|
if craftdef.type == "cooking" or craftdef.type == "fuel" then
|
2018-06-18 11:43:40 +00:00
|
|
|
minetest.register_craft(craftdef)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-25 16:33:18 +00:00
|
|
|
local inputs = craftdef.recipe
|
|
|
|
local outputs = craftdef.output
|
|
|
|
local ctype = craftdef.type
|
|
|
|
local ctypedef = elepm.craft.types[ctype]
|
|
|
|
local time = (craftdef.time or craftdef.cooktime or 5) + (ctypedef.time or 0)
|
2018-06-18 07:09:43 +00:00
|
|
|
|
|
|
|
local craftrecipe = {}
|
|
|
|
for _,input in ipairs(inputs) do
|
|
|
|
local stack = ItemStack(input)
|
|
|
|
if stack and not stack:is_empty() then
|
|
|
|
craftrecipe[stack:get_name()] = stack:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local craftresult = {}
|
|
|
|
if type(outputs) == "table" then
|
2018-06-18 11:43:40 +00:00
|
|
|
for _,output in ipairs(outputs) do
|
2018-06-18 07:09:43 +00:00
|
|
|
local stack = ItemStack(output)
|
|
|
|
if stack and not stack:is_empty() then
|
2018-06-18 11:43:40 +00:00
|
|
|
craftresult[#craftresult + 1] = stack:to_string()
|
2018-06-18 07:09:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
craftresult = ItemStack(outputs)
|
|
|
|
end
|
|
|
|
|
|
|
|
local recipe = {
|
|
|
|
recipe = craftrecipe,
|
|
|
|
output = craftresult,
|
|
|
|
time = time
|
|
|
|
}
|
2019-01-22 21:14:38 +00:00
|
|
|
|
|
|
|
table.insert(elepm.craft[ctype], recipe)
|
2018-06-18 07:09:43 +00:00
|
|
|
|
2018-12-17 10:46:13 +00:00
|
|
|
if have_ui or have_cg then
|
2018-12-13 10:19:08 +00:00
|
|
|
local spec = {}
|
2018-12-17 17:48:45 +00:00
|
|
|
local items = {}
|
2018-12-13 10:19:08 +00:00
|
|
|
|
|
|
|
for item, count in pairs(recipe.recipe) do
|
2018-12-17 17:48:45 +00:00
|
|
|
local stack = ItemStack(item)
|
|
|
|
stack:set_count(count)
|
|
|
|
spec[#spec+1] = stack
|
|
|
|
items[#items+1] = stack:to_string()
|
2018-12-17 10:46:13 +00:00
|
|
|
end
|
|
|
|
|
2018-12-13 10:19:08 +00:00
|
|
|
if type(recipe.output) == "table" then
|
|
|
|
for _,itm in pairs(recipe.output) do
|
|
|
|
local itmst = ItemStack(itm)
|
2018-12-17 10:46:13 +00:00
|
|
|
if have_ui then
|
|
|
|
unified_inventory.register_craft({
|
|
|
|
type = craftdef.type,
|
|
|
|
output = itmst,
|
|
|
|
items = spec,
|
2018-12-25 16:33:18 +00:00
|
|
|
width = ctypedef.inputs,
|
2018-12-17 10:46:13 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-12-17 17:48:45 +00:00
|
|
|
if have_cg then
|
2018-12-17 10:46:13 +00:00
|
|
|
craftguide.register_craft({
|
|
|
|
type = craftdef.type,
|
2018-12-17 17:48:45 +00:00
|
|
|
output = itmst:to_string(),
|
2018-12-17 10:46:13 +00:00
|
|
|
items = items,
|
2018-12-25 16:33:18 +00:00
|
|
|
width = ctypedef.inputs,
|
2018-12-17 10:46:13 +00:00
|
|
|
})
|
|
|
|
end
|
2018-12-13 10:19:08 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-17 10:46:13 +00:00
|
|
|
if have_ui then
|
|
|
|
unified_inventory.register_craft({
|
|
|
|
type = craftdef.type,
|
|
|
|
output = recipe.output,
|
|
|
|
items = spec,
|
2018-12-25 16:33:18 +00:00
|
|
|
width = ctypedef.inputs,
|
2018-12-17 10:46:13 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
if have_cg then
|
|
|
|
craftguide.register_craft({
|
|
|
|
type = craftdef.type,
|
2018-12-17 17:48:45 +00:00
|
|
|
output = recipe.output:to_string(),
|
2018-12-17 10:46:13 +00:00
|
|
|
items = items,
|
2018-12-25 16:33:18 +00:00
|
|
|
width = ctypedef.inputs,
|
2018-12-17 10:46:13 +00:00
|
|
|
})
|
|
|
|
end
|
2018-12-13 10:19:08 +00:00
|
|
|
end
|
2018-06-18 07:09:43 +00:00
|
|
|
end
|
|
|
|
|
2018-06-22 18:03:41 +00:00
|
|
|
local no_recipe = {
|
|
|
|
time = 0,
|
|
|
|
new_input = {},
|
|
|
|
output = {}
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:09:43 +00:00
|
|
|
function elepm.get_recipe(type, inputs)
|
2018-06-21 13:08:32 +00:00
|
|
|
if not elepm.craft[type] or not inputs then
|
2018-06-22 18:03:41 +00:00
|
|
|
return no_recipe
|
2018-06-18 07:09:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Minetest's cooking builtin type
|
|
|
|
if type == "cooking" then
|
|
|
|
local result, new_input = minetest.get_craft_result({
|
|
|
|
method = "cooking",
|
|
|
|
width = 1,
|
|
|
|
items = inputs
|
|
|
|
})
|
|
|
|
|
|
|
|
if not result or result.time == 0 then
|
2018-06-22 18:03:41 +00:00
|
|
|
return no_recipe
|
2018-06-18 07:09:43 +00:00
|
|
|
else
|
|
|
|
return {
|
|
|
|
time = result.time,
|
|
|
|
new_input = new_input.items,
|
|
|
|
output = result.item
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Custom types
|
2018-06-22 18:03:41 +00:00
|
|
|
local result = no_recipe
|
2018-06-18 07:09:43 +00:00
|
|
|
for _,recipe in ipairs(elepm.craft[type]) do
|
|
|
|
local recip_match = true
|
|
|
|
local inputs_full = {}
|
|
|
|
local new_input = {}
|
|
|
|
|
|
|
|
for _,input in ipairs(inputs) do
|
|
|
|
local in_name = input:get_name()
|
|
|
|
|
|
|
|
if not recipe.recipe[in_name] then
|
|
|
|
recip_match = false
|
|
|
|
elseif recipe.recipe[in_name] > input:get_count() then
|
|
|
|
recip_match = false
|
|
|
|
end
|
|
|
|
|
|
|
|
if not recip_match then break end
|
|
|
|
|
|
|
|
table.insert(inputs_full, in_name)
|
|
|
|
|
|
|
|
local istack = ItemStack(in_name)
|
|
|
|
istack:set_count(input:get_count() - recipe.recipe[in_name])
|
|
|
|
new_input[#new_input + 1] = istack
|
|
|
|
end
|
|
|
|
|
|
|
|
if recip_match then
|
|
|
|
for _,ingredient in ipairs(recipe.recipe) do
|
|
|
|
local its = ItemStack(ingredient)
|
|
|
|
if not inputs_full[its:get_name()] then
|
|
|
|
recip_match = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if recip_match then
|
|
|
|
result = recipe
|
|
|
|
result.new_input = new_input
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Cooking craft type built-in.
|
|
|
|
elepm.register_craft_type("cooking", {
|
|
|
|
description = "Cooking",
|
2021-06-18 21:49:57 +00:00
|
|
|
overview = "Cooking is more than just the prepartion of food "..
|
|
|
|
"it provides access to the prepartion of ores to metals "..
|
|
|
|
"and the creation of glass and glass objects.",
|
|
|
|
inputs = 1,
|
2018-06-18 07:09:43 +00:00
|
|
|
})
|