Vanessa Ezekowitz
2015-01-12 bc0ac581aa2b79cd48e4b2083d8c7890ace580aa
commit | author | age
aa8af0 1
dd65a6 2 technic.recipes = { cooking = { input_size = 1, output_size = 1 } }
Z 3 function technic.register_recipe_type(typename, origdata)
4     local data = {}
5     for k, v in pairs(origdata) do data[k] = v end
6     data.input_size = data.input_size or 1
7     data.output_size = data.output_size or 1
8     if unified_inventory and unified_inventory.register_craft_type and data.output_size == 1 then
aa8af0 9         unified_inventory.register_craft_type(typename, {
dd65a6 10             description = data.description,
daa613 11             width = data.input_size,
Z 12             height = 1,
aa8af0 13         })
N 14     end
dd65a6 15     data.recipes = {}
Z 16     technic.recipes[typename] = data
aa8af0 17 end
N 18
d55ecc 19 local function get_recipe_index(items)
bc0ac5 20     if not items or type(items) ~= "table" then return false end
d55ecc 21     local l = {}
N 22     for i, stack in ipairs(items) do
23         l[i] = ItemStack(stack):get_name()
24     end
25     table.sort(l)
26     return table.concat(l, "/")
27 end
28
29 local function register_recipe(typename, data)
30     -- Handle aliases
31     for i, stack in ipairs(data.input) do
32         data.input[i] = ItemStack(stack):to_string()
33     end
dd65a6 34     if type(data.output) == "table" then
Z 35         for i, v in ipairs(data.output) do
36             data.output[i] = ItemStack(data.output[i]):to_string()
37         end
38     else
39         data.output = ItemStack(data.output):to_string()
40     end
d55ecc 41     
N 42     local recipe = {time = data.time, input = {}, output = data.output}
43     local index = get_recipe_index(data.input)
bc0ac5 44     if not index then
VE 45         print("[Technic] ignored registration of garbage recipe!")
46         return
47     end
d55ecc 48     for _, stack in ipairs(data.input) do
N 49         recipe.input[ItemStack(stack):get_name()] = ItemStack(stack):get_count()
50     end
51     
52     technic.recipes[typename].recipes[index] = recipe
dd65a6 53     if unified_inventory and technic.recipes[typename].output_size == 1 then
aa8af0 54         unified_inventory.register_craft({
N 55             type = typename,
56             output = data.output,
d55ecc 57             items = data.input,
aa8af0 58             width = 0,
N 59         })
60     end
61 end
62
d55ecc 63 function technic.register_recipe(typename, data)
N 64     minetest.after(0.01, register_recipe, typename, data) -- Handle aliases
65 end
66
67 function technic.get_recipe(typename, items)
aa8af0 68     if typename == "cooking" then -- Already builtin in Minetest, so use that
d55ecc 69         local result, new_input = minetest.get_craft_result({
aa8af0 70             method = "cooking",
N 71             width = 1,
d55ecc 72             items = items})
aa8af0 73         -- Compatibility layer
N 74         if not result or result.time == 0 then
75             return nil
76         else
77             return {time = result.time,
d55ecc 78                     new_input = new_input.items,
N 79                     output = result.item}
aa8af0 80         end
N 81     end
d55ecc 82     local index = get_recipe_index(items)
bc0ac5 83     if not index then
VE 84         print("[Technic] ignored registration of garbage recipe!")
85         return
86     end
d55ecc 87     local recipe = technic.recipes[typename].recipes[index]
N 88     if recipe then
89         local new_input = {}
90         for i, stack in ipairs(items) do
91             if stack:get_count() < recipe.input[stack:get_name()] then
92                 return nil
93             else
94                 new_input[i] = ItemStack(stack)
95                 new_input[i]:take_item(recipe.input[stack:get_name()])
96             end
97         end
98         return {time = recipe.time,
99                 new_input = new_input,
100                 output = recipe.output}
aa8af0 101     else
N 102         return nil
103     end
104 end
105
106