Change the recipe code to be able to handle recipes with more than one input, and use it for the alloy furnace.

Reorganize the init.lua files.
This commit is contained in:
Novatux 2014-07-03 15:57:39 +02:00
parent 95fcc435f1
commit d55ecc39f9
14 changed files with 178 additions and 384 deletions

View File

@ -3,11 +3,16 @@ technic.register_tier("HV", "High Voltage")
local path = technic.modpath.."/machines/HV"
-- Wiring stuff
dofile(path.."/cables.lua")
dofile(path.."/quarry.lua")
dofile(path.."/forcefield.lua")
dofile(path.."/battery_box.lua")
-- Generators
dofile(path.."/solar_array.lua")
dofile(path.."/nuclear_reactor.lua")
dofile(path.."/generator.lua")
-- Machines
dofile(path.."/quarry.lua")
dofile(path.."/forcefield.lua")

View File

@ -10,5 +10,5 @@ minetest.register_craft({
}
})
technic.register_alloy_furnace({tier="LV", cook_time=6, demand={300}})
technic.register_alloy_furnace({tier = "LV", speed = 1, demand = {300}})

View File

@ -27,8 +27,7 @@ minetest.register_node("technic:coal_alloy_furnace", {
meta:set_string("infotext", S("Fuel-Fired Alloy Furnace"))
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("src2", 1)
inv:set_size("src", 2)
inv:set_size("dst", 4)
end,
can_dig = technic.machine_can_dig,
@ -61,6 +60,13 @@ minetest.register_abm({
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:get_size("src") == 1 then -- Old furnace -> convert it
inv:set_size("src", 2)
inv:set_stack("src", 2, inv:get_stack("src2", 1))
inv:set_size("src2", 0)
end
local recipe = nil
local machine_name = S("Fuel-Fired Alloy Furnace")
local formspec =
@ -68,8 +74,7 @@ minetest.register_abm({
"label[0,0;"..machine_name.."]"..
"image[2,2;1,1;default_furnace_fire_bg.png]"..
"list[current_name;fuel;2,3;1,1;]"..
"list[current_name;src;2,1;1,1;]"..
"list[current_name;src2;3,1;1,1;]"..
"list[current_name;src;2,1;2,1;]"..
"list[current_name;dst;5,1;2,2;]"..
"list[current_player;main;0,5;8,4;]"
@ -84,33 +89,22 @@ minetest.register_abm({
end
-- Get what to cook if anything
local srcstack = inv:get_stack("src", 1)
local src2stack = inv:get_stack("src2", 1)
local recipe = technic.get_alloy_recipe(srcstack, src2stack)
if srcstack:get_name() > src2stack:get_name() then
local temp = srcstack
srcstack = src2stack
src2stack = temp
end
local result = technic.get_recipe("alloy", inv:get_list("src"))
local was_active = false
if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
was_active = true
meta:set_int("fuel_time", meta:get_int("fuel_time") + 1)
if recipe then
if result then
meta:set_int("src_time", meta:get_int("src_time") + 1)
if meta:get_int("src_time") == 6 then
-- check if there's room for output in "dst" list
local dst_stack = ItemStack(recipe.output)
if inv:room_for_item("dst", dst_stack) then
srcstack:take_item(recipe.input[1].count)
inv:set_stack("src", 1, srcstack)
src2stack:take_item(recipe.input[2].count)
inv:set_stack("src2", 1, src2stack)
inv:add_item("dst", dst_stack)
end
if meta:get_int("src_time") >= result.time then
meta:set_int("src_time", 0)
local result_stack = ItemStack(result.output)
if inv:room_for_item("dst", result_stack) then
inv:set_list("src", result.new_input)
inv:add_item("dst", result_stack)
end
end
else
meta:set_int("src_time", 0)
@ -128,22 +122,17 @@ minetest.register_abm({
"image[2,2;1,1;default_furnace_fire_bg.png^[lowpart:"..
(100 - percent)..":default_furnace_fire_fg.png]"..
"list[current_name;fuel;2,3;1,1;]"..
"list[current_name;src;2,1;1,1;]"..
"list[current_name;src2;3,1;1,1;]"..
"list[current_name;src;2,1;2,1;]"..
"list[current_name;dst;5,1;2,2;]"..
"list[current_player;main;0,5;8,4;]")
return
end
-- FIXME: Make this look more like the electrical version.
-- This code refetches the recipe to see if it can be done again after the iteration
srcstack = inv:get_stack("src", 1)
srcstack = inv:get_stack("src2", 1)
local recipe = technic.get_alloy_recipe(srcstack, src2stack)
local recipe = technic.get_recipe("alloy", inv:get_list("src"))
if recipe then
if not recipe then
if was_active then
meta:set_string("infotext", "Furnace is empty")
meta:set_string("infotext", S("%s is empty"):format(machine_name))
technic.swap_node(pos, "technic:coal_alloy_furnace")
meta:set_string("formspec", formspec)
end

View File

@ -3,21 +3,31 @@ technic.register_tier("LV", "Low Voltage")
local path = technic.modpath.."/machines/LV"
-- Wiring stuff
dofile(path.."/cables.lua")
dofile(path.."/battery_box.lua")
dofile(path.."/alloy_furnace.lua")
dofile(path.."/coal_alloy_furnace.lua")
dofile(path.."/coal_furnace.lua")
-- Generators
dofile(path.."/solar_panel.lua")
dofile(path.."/solar_array.lua")
dofile(path.."/geothermal.lua")
dofile(path.."/water_mill.lua")
dofile(path.."/generator.lua")
-- Coal-powered machines (TODO -> move to somewhere else?)
dofile(path.."/coal_alloy_furnace.lua")
dofile(path.."/coal_furnace.lua")
-- Machines
dofile(path.."/alloy_furnace.lua")
dofile(path.."/electric_furnace.lua")
dofile(path.."/music_player.lua")
dofile(path.."/grinder.lua")
dofile(path.."/extractor.lua")
dofile(path.."/compressor.lua")
dofile(path.."/music_player.lua")
dofile(path.."/cnc.lua")
dofile(path.."/cnc_api.lua")
dofile(path.."/cnc_nodes.lua")
dofile(path.."/extractor.lua")
dofile(path.."/compressor.lua")

View File

@ -4,11 +4,11 @@ minetest.register_craft({
output = 'technic:mv_alloy_furnace',
recipe = {
{'technic:stainless_steel_ingot', 'technic:lv_alloy_furnace', 'technic:stainless_steel_ingot'},
{'pipeworks:tube_1', 'technic:mv_transformer', 'pipeworks:tube_1'},
{'pipeworks:tube_1', 'technic:mv_transformer', 'pipeworks:tube_1'},
{'technic:stainless_steel_ingot', 'technic:mv_cable0', 'technic:stainless_steel_ingot'},
}
})
technic.register_alloy_furnace({tier="MV", cook_time=4, upgrade=1, tube=1, demand={3000, 2000, 1000}})
technic.register_alloy_furnace({tier = "MV", speed = 1.5, upgrade = 1, tube = 1, demand = {3000, 2000, 1000}})

View File

@ -3,20 +3,26 @@ technic.register_tier("MV", "Medium Voltage")
local path = technic.modpath.."/machines/MV"
dofile(path.."/alloy_furnace.lua")
dofile(path.."/battery_box.lua")
-- Wiring stuff
dofile(path.."/cables.lua")
dofile(path.."/electric_furnace.lua")
dofile(path.."/grinder.lua")
dofile(path.."/solar_array.lua")
dofile(path.."/tool_workshop.lua")
dofile(path.."/battery_box.lua")
-- Generators
if technic.config:get_bool("enable_wind_mill") then
dofile(path.."/wind_mill.lua")
end
dofile(path.."/generator.lua")
dofile(path.."/solar_array.lua")
-- Machines
dofile(path.."/alloy_furnace.lua")
dofile(path.."/electric_furnace.lua")
dofile(path.."/grinder.lua")
dofile(path.."/extractor.lua")
dofile(path.."/compressor.lua")
dofile(path.."/tool_workshop.lua")
-- The power radiator supplies appliances with inductive coupled power:
-- Lighting and associated textures is taken directly from VanessaE's homedecor and made electric.
-- This is currently useless, slow, and mostly copied

View File

@ -1,289 +1,10 @@
local S = technic.getter
if unified_inventory and unified_inventory.register_craft_type then
unified_inventory.register_craft_type("alloy", {
description = S("Alloy cooking"),
height = 2,
width = 1,
})
end
-- Register alloy recipes
technic.alloy_recipes = {}
-- Register recipe in a table
technic.register_alloy_recipe = function(metal1, count1, metal2, count2, result, count3)
local in1 = {
name = metal1,
count = count1,
}
local in2 = {
name = metal2,
count = count2,
}
-- Sort the inputs alphebetically
if in1.name > in2.name then
local temp = in1
in1 = in2
in2 = temp
end
technic.alloy_recipes[in1.name.." "..in2.name] = {
input = {in1, in2},
output = {
name = result,
count = count3,
},
}
if unified_inventory then
unified_inventory.register_craft({
type = "alloy",
output = result.." "..count3,
items = {metal1.." "..count1, metal2.." "..count2},
width = 0,
})
end
end
minetest.after(0.01, function ()
for _, recipe in pairs(technic.alloy_recipes) do
local in1 = recipe.input[1]
local in2 = recipe.input[2]
local in1n = in1.name
local in2n = in2.name
while minetest.registered_aliases[in1n] do
in1n = minetest.registered_aliases[in1n]
end
while minetest.registered_aliases[in2n] do
in2n = minetest.registered_aliases[in2n]
end
if in1n > in2n then
local temp = in1
in1 = in2
in2 = temp
temp = in1n
in1n = in2n
in2n = temp
end
technic.alloy_recipes[in1n.." "..in2n] = {
input = {
{ name = in1n, count = in1.count },
{ name = in2n, count = in2.count },
},
output = recipe.output,
}
end
end)
-- Retrieve a recipe given the input metals.
function technic.get_alloy_recipe(stack1, stack2)
-- Sort the stacks alphebetically
if stack1:get_name() > stack2:get_name() then
local temp = stack1
stack1 = stack2
stack2 = temp
end
for _, recipe in pairs(technic.alloy_recipes) do
if recipe.input[1].name == stack1:get_name() and
recipe.input[2].name == stack2:get_name() and
stack1:get_count() >= recipe.input[1].count and
stack2:get_count() >= recipe.input[2].count then
return recipe
end
end
end
technic.register_alloy_recipe("technic:copper_dust", 3, "technic:tin_dust", 1, "technic:bronze_dust", 4)
technic.register_alloy_recipe("default:copper_ingot", 3, "moreores:tin_ingot", 1, "moreores:bronze_ingot", 4)
technic.register_alloy_recipe("technic:wrought_iron_dust", 1, "technic:coal_dust", 1, "technic:carbon_steel_dust", 1)
technic.register_alloy_recipe("technic:wrought_iron_ingot", 1, "technic:coal_dust", 1, "technic:carbon_steel_ingot", 1)
technic.register_alloy_recipe("technic:carbon_steel_dust", 1, "technic:coal_dust", 1, "technic:cast_iron_dust", 1)
technic.register_alloy_recipe("technic:carbon_steel_ingot", 1, "technic:coal_dust", 1, "technic:cast_iron_ingot", 1)
technic.register_alloy_recipe("technic:carbon_steel_dust", 3, "technic:chromium_dust", 1, "technic:stainless_steel_dust", 4)
technic.register_alloy_recipe("technic:carbon_steel_ingot", 3, "technic:chromium_ingot", 1, "technic:stainless_steel_ingot", 4)
technic.register_alloy_recipe("technic:copper_dust", 2, "technic:zinc_dust", 1, "technic:brass_dust", 3)
technic.register_alloy_recipe("default:copper_ingot", 2, "technic:zinc_ingot", 1, "technic:brass_ingot", 3)
technic.register_alloy_recipe("default:sand", 2, "technic:coal_dust", 2, "technic:silicon_wafer", 1)
technic.register_alloy_recipe("technic:silicon_wafer", 1, "technic:gold_dust", 1, "technic:doped_silicon_wafer", 1)
local tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("src", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:room_for_item("src", stack)
end,
connect_sides = {left=1, right=1, back=1, top=1, bottom=1},
}
function technic.register_alloy_furnace(data)
local tier = data.tier
local ltier = string.lower(tier)
local tube_side_texture = data.tube and "technic_"..ltier.."_alloy_furnace_side_tube.png"
or "technic_"..ltier.."_alloy_furnace_side.png"
local groups = {cracky=2}
local active_groups = {cracky=2, not_in_creative_inventory=1}
if data.tube then
groups.tubedevice = 1
groups.tubedevice_receiver = 1
active_groups.tubedevice = 1
active_groups.tubedevice_receiver = 1
end
local formspec =
"invsize[8,10;]"..
"label[0,0;"..S("%s Alloy Furnace"):format(tier).."]"..
"list[current_name;src;3,1;1,2;]"..
"list[current_name;dst;5,1;2,2;]"..
"list[current_player;main;0,6;8,4;]"
if data.upgrade then
formspec = formspec..
"list[current_name;upgrade1;1,4;1,1;]"..
"list[current_name;upgrade2;2,4;1,1;]"..
"label[1,5;"..S("Upgrade Slots").."]"
end
minetest.register_node("technic:"..ltier.."_alloy_furnace", {
description = S("%s Alloy Furnace"):format(tier),
tiles = {"technic_"..ltier.."_alloy_furnace_top.png",
"technic_"..ltier.."_alloy_furnace_bottom.png",
tube_side_texture,
tube_side_texture,
"technic_"..ltier.."_alloy_furnace_side.png",
"technic_"..ltier.."_alloy_furnace_front.png"},
paramtype2 = "facedir",
groups = groups,
tube = data.tube and tube or nil,
legacy_facedir_simple = true,
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local name = minetest.get_node(pos).name
meta:set_string("infotext", S("%s Alloy Furnace"):format(tier))
meta:set_string("formspec", formspec)
meta:set_int("tube_time", 0)
local inv = meta:get_inventory()
inv:set_size("src", 2)
inv:set_size("dst", 4)
inv:set_size("upgrade1", 1)
inv:set_size("upgrade2", 1)
end,
can_dig = technic.machine_can_dig,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
minetest.register_node("technic:"..ltier.."_alloy_furnace_active",{
description = S("%s Alloy Furnace"):format(tier),
tiles = {"technic_"..ltier.."_alloy_furnace_top.png",
"technic_"..ltier.."_alloy_furnace_bottom.png",
tube_side_texture,
tube_side_texture,
"technic_"..ltier.."_alloy_furnace_side.png",
"technic_"..ltier.."_alloy_furnace_front_active.png"},
paramtype2 = "facedir",
light_source = 8,
drop = "technic:"..ltier.."_alloy_furnace",
groups = active_groups,
tube = data.tube and tube or nil,
legacy_facedir_simple = true,
sounds = default.node_sound_stone_defaults(),
can_dig = technic.machine_can_dig,
allow_metadata_inventory_put = technic.machine_inventory_put,
allow_metadata_inventory_take = technic.machine_inventory_take,
allow_metadata_inventory_move = technic.machine_inventory_move,
})
minetest.register_abm({
nodenames = {"technic:"..ltier.."_alloy_furnace", "technic:"..ltier.."_alloy_furnace_active"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local eu_input = meta:get_int(tier.."_EU_input")
-- Machine information
local machine_name = S("%s Alloy Furnace"):format(tier)
local machine_node = "technic:"..ltier.."_alloy_furnace"
local machine_demand = data.demand
-- Setup meta data if it does not exist.
if not eu_input then
meta:set_int(tier.."_EU_demand", machine_demand[1])
meta:set_int(tier.."_EU_input", 0)
end
-- Power off automatically if no longer connected to a switching station
technic.switching_station_timeout_count(pos, tier)
local EU_upgrade, tube_upgrade = 0, 0
if data.upgrade then
EU_upgrade, tube_upgrade = technic.handle_machine_upgrades(meta)
end
if data.tube then
technic.handle_machine_pipeworks(pos, tube_upgrade)
end
-- Get what to cook if anything
local srcstack = inv:get_stack("src", 1)
local src2stack = inv:get_stack("src", 2)
local recipe = technic.get_alloy_recipe(srcstack, src2stack)
local result = recipe and ItemStack(recipe.output) or nil
-- Sort the stacks alphabetically
if srcstack:get_name() > src2stack:get_name() then
local temp = srcstack
srcstack = src2stack
src2stack = temp
end
if not result or
not inv:room_for_item("dst", result) then
technic.swap_node(pos, machine_node)
meta:set_string("infotext", S("%s Idle"):format(machine_name))
meta:set_int(tier.."_EU_demand", 0)
return
end
if eu_input < machine_demand[EU_upgrade+1] then
-- Unpowered - go idle
technic.swap_node(pos, machine_node)
meta:set_string("infotext", S("%s Unpowered"):format(machine_name))
elseif eu_input >= machine_demand[EU_upgrade+1] then
-- Powered
technic.swap_node(pos, machine_node.."_active")
meta:set_string("infotext", S("%s Active"):format(machine_name))
meta:set_int("src_time", meta:get_int("src_time") + 1)
if meta:get_int("src_time") == data.cook_time then
meta:set_int("src_time", 0)
-- check if there's room for output and that we have the materials
if inv:room_for_item("dst", result) then
srcstack:take_item(recipe.input[1].count)
inv:set_stack("src", 1, srcstack)
src2stack:take_item(recipe.input[2].count)
inv:set_stack("src", 2, src2stack)
-- Put result in "dst" list
inv:add_item("dst", result)
else
next_state = 1
end
end
end
meta:set_int(tier.."_EU_demand", machine_demand[EU_upgrade+1])
end,
})
technic.register_machine(tier, "technic:"..ltier.."_alloy_furnace", technic.receiver)
technic.register_machine(tier, "technic:"..ltier.."_alloy_furnace_active", technic.receiver)
end -- End registration
data.typename = "alloy"
data.machine_name = "alloy_furnace"
data.machine_desc = S("%s Alloy Furnace")
technic.register_base_machine(data)
end

View File

@ -0,0 +1,28 @@
local S = technic.getter
technic.register_recipe_type("alloy", S("Alloy cooking"), 2)
function technic.register_alloy_recipe(data)
data.time = data.time or 6
technic.register_recipe("alloy", data)
end
local recipes = {
{"technic:copper_dust 3", "technic:tin_dust", "technic:bronze_dust 4"},
{"default:copper_ingot 3", "moreores:tin_ingot", "default:bronze_ingot 4"},
{"technic:wrought_iron_dust", "technic:coal_dust", "technic:carbon_steel_dust"},
{"technic:wrought_iron_ingot", "technic:coal_dust", "technic:carbon_steel_ingot"},
{"technic:carbon_steel_dust", "technic:coal_dust", "technic:cast_iron_dust"},
{"technic:carbon_steel_ingot", "technic:coal_dust", "technic:cast_iron_ingot"},
{"technic:carbon_steel_dust 3", "technic:chromium_dust", "technic:stainless_steel_dust 4"},
{"technic:carbon_steel_ingot 3", "technic:chromium_ingot", "technic:stainless_steel_ingot 4"},
{"technic:copper_dust 2", "technic:zinc_dust", "technic:brass_dust 3"},
{"technic:copper_ingot 2", "technic:zinc_ingot", "technic:brass_ingot 3"},
{"default:sand 2", "technic:coal_dust 2", "technic:silicon_wafer"},
{"technic:silicon_wafer", "technic:gold_dust", "technic:doped_silicon_wafer"},
}
for _, data in pairs(recipes) do
technic.register_alloy_recipe({input = {data[1], data[2]}, output = data[3]})
end

View File

@ -19,6 +19,6 @@ local recipes = {
}
for _, data in pairs(recipes) do
technic.register_compressor_recipe({input = data[1], output = data[2]})
technic.register_compressor_recipe({input = {data[1]}, output = data[2]})
end

View File

@ -30,6 +30,6 @@ local recipes = {
}
for _, data in pairs(recipes) do
technic.register_extractor_recipe({input = data[1], output = data[2]})
technic.register_extractor_recipe({input = {data[1]}, output = data[2]})
end

View File

@ -40,7 +40,7 @@ if minetest.get_modpath("homedecor") then
end
for _, data in pairs(recipes) do
technic.register_grinder_recipe({input = data[1], output = data[2]})
technic.register_grinder_recipe({input = {data[1]}, output = data[2]})
end
local function register_dust(name, ingot)
@ -57,7 +57,7 @@ local function register_dust(name, ingot)
recipe = "technic:"..lname.."_dust",
output = ingot,
})
technic.register_grinder_recipe({ input = ingot, output = "technic:"..lname.."_dust 1" })
technic.register_grinder_recipe({ input = {ingot}, output = "technic:"..lname.."_dust 1" })
end
end

View File

@ -1,18 +1,29 @@
local path = technic.modpath.."/machines/register"
dofile(path.."/recipes.lua")
dofile(path.."/machine_base.lua")
dofile(path.."/alloy_furnace.lua")
dofile(path.."/battery_box.lua")
dofile(path.."/cables.lua")
dofile(path.."/common.lua")
dofile(path.."/electric_furnace.lua")
dofile(path.."/grinder.lua")
dofile(path.."/grinder_recipes.lua")
dofile(path.."/extractor.lua")
dofile(path.."/extractor_recipes.lua")
dofile(path.."/compressor.lua")
dofile(path.."/compressor_recipes.lua")
-- Wiring stuff
dofile(path.."/cables.lua")
dofile(path.."/battery_box.lua")
-- Generators
dofile(path.."/solar_array.lua")
dofile(path.."/generator.lua")
-- API for machines
dofile(path.."/recipes.lua")
dofile(path.."/machine_base.lua")
-- Recipes
dofile(path.."/alloy_recipes.lua")
dofile(path.."/grinder_recipes.lua")
dofile(path.."/extractor_recipes.lua")
dofile(path.."/compressor_recipes.lua")
-- Machines
dofile(path.."/alloy_furnace.lua")
dofile(path.."/electric_furnace.lua")
dofile(path.."/grinder.lua")
dofile(path.."/extractor.lua")
dofile(path.."/compressor.lua")

View File

@ -17,6 +17,7 @@ local tube = {
function technic.register_base_machine(data)
local typename = data.typename
local numitems = technic.recipes[typename].numitems
local machine_name = data.machine_name
local machine_desc = data.machine_desc
local tier = data.tier
@ -34,7 +35,7 @@ function technic.register_base_machine(data)
local formspec =
"invsize[8,9;]"..
"list[current_name;src;3,1;1,1;]"..
"list[current_name;src;"..(4-numitems)..",1;"..numitems..",1;]"..
"list[current_name;dst;5,1;2,2;]"..
"list[current_player;main;0,5;8,4;]"..
"label[0,0;"..machine_desc:format(tier).."]"
@ -65,7 +66,7 @@ function technic.register_base_machine(data)
meta:set_int("tube_time", 0)
meta:set_string("formspec", formspec)
local inv = meta:get_inventory()
inv:set_size("src", 1)
inv:set_size("src", numitems)
inv:set_size("dst", 4)
inv:set_size("upgrade1", 1)
inv:set_size("upgrade2", 1)
@ -128,7 +129,7 @@ function technic.register_base_machine(data)
technic.handle_machine_pipeworks(pos, tube_upgrade)
end
local result = technic.get_recipe(typename, inv:get_stack("src", 1))
local result = technic.get_recipe(typename, inv:get_list("src"))
if not result then
technic.swap_node(pos, machine_node)
@ -151,9 +152,7 @@ function technic.register_base_machine(data)
meta:set_int("src_time", 0)
local result_stack = ItemStack(result.output)
if inv:room_for_item("dst", result_stack) then
srcstack = inv:get_stack("src", 1)
srcstack:take_item(ItemStack(result.input):get_count())
inv:set_stack("src", 1, srcstack)
inv:set_list("src", result.new_input)
inv:add_item("dst", result_stack)
end
end

View File

@ -1,63 +1,88 @@
technic.recipes = {}
function technic.register_recipe_type(typename, desc)
technic.recipes = {cooking = {numitems = 1}}
function technic.register_recipe_type(typename, desc, numitems)
numitems = numitems or 1
if unified_inventory and unified_inventory.register_craft_type then
unified_inventory.register_craft_type(typename, {
description = desc,
height = 1,
height = numtiems,
width = 1,
})
end
technic.recipes[typename] = {}
technic.recipes[typename] = {numitems = numitems, recipes = {}}
end
function technic.register_recipe(typename, data)
local src = ItemStack(data.input):get_name()
technic.recipes[typename][src] = data
local function get_recipe_index(items)
local l = {}
for i, stack in ipairs(items) do
l[i] = ItemStack(stack):get_name()
end
table.sort(l)
return table.concat(l, "/")
end
local function register_recipe(typename, data)
-- Handle aliases
for i, stack in ipairs(data.input) do
data.input[i] = ItemStack(stack):to_string()
end
data.output = ItemStack(data.output):to_string()
local recipe = {time = data.time, input = {}, output = data.output}
local index = get_recipe_index(data.input)
for _, stack in ipairs(data.input) do
recipe.input[ItemStack(stack):get_name()] = ItemStack(stack):get_count()
end
technic.recipes[typename].recipes[index] = recipe
if unified_inventory then
unified_inventory.register_craft({
type = typename,
output = data.output,
items = {data.input},
items = data.input,
width = 0,
})
end
end
function technic.get_recipe(typename, item)
function technic.register_recipe(typename, data)
minetest.after(0.01, register_recipe, typename, data) -- Handle aliases
end
function technic.get_recipe(typename, items)
if typename == "cooking" then -- Already builtin in Minetest, so use that
local result = minetest.get_craft_result({
local result, new_input = minetest.get_craft_result({
method = "cooking",
width = 1,
items = {item}})
items = items})
-- Compatibility layer
if not result or result.time == 0 then
return nil
else
return {time = result.time,
input = item:get_name(),
output = result.item:to_string()}
new_input = new_input.items,
output = result.item}
end
end
local recipe = technic.recipes[typename][item:get_name()]
if recipe and item:get_count() >= ItemStack(recipe.input):get_count() then
return recipe
local index = get_recipe_index(items)
local recipe = technic.recipes[typename].recipes[index]
if recipe then
local new_input = {}
for i, stack in ipairs(items) do
if stack:get_count() < recipe.input[stack:get_name()] then
print(stack:get_name())
return nil
else
new_input[i] = ItemStack(stack)
new_input[i]:take_item(recipe.input[stack:get_name()])
end
end
return {time = recipe.time,
new_input = new_input,
output = recipe.output}
else
return nil
end
end
-- Handle aliases
minetest.after(0.01, function ()
for _, recipes_list in pairs(technic.recipes) do
for ingredient, recipe in pairs(recipes_list) do
ingredient = minetest.registered_aliases[ingredient]
while ingredient do
recipes_list[ingredient] = recipe
ingredient = minetest.registered_aliases[ingredient]
end
end
end
end)