2019-06-10 22:31:58 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
Copyright (C) 2019-2020 Joachim Stolberg
|
2019-06-10 22:31:58 +03:00
|
|
|
|
2019-08-22 21:49:47 +03:00
|
|
|
GPL v3
|
2019-06-10 22:31:58 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
Cooking routines for furnace
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
|
|
|
local P = minetest.string_to_pos
|
|
|
|
local M = minetest.get_meta
|
2019-07-02 22:33:12 +03:00
|
|
|
local S = techage.S
|
2019-06-10 22:31:58 +03:00
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local range = techage.in_range
|
2019-06-21 18:45:15 +03:00
|
|
|
|
2019-11-17 02:08:10 +03:00
|
|
|
local Recipes = {} -- registered recipes {output = {recipe, ...},}
|
|
|
|
local Ingredients = {} -- {{input = output},
|
2019-06-10 22:31:58 +03:00
|
|
|
local KeyList = {} -- index to Recipes key translation
|
|
|
|
|
|
|
|
techage.furnace = {}
|
|
|
|
|
2019-06-29 00:46:44 +03:00
|
|
|
|
|
|
|
local function all_ingredients_available(output, ingr)
|
2019-08-19 00:16:13 +03:00
|
|
|
if Recipes[output] then
|
|
|
|
for idx,recipe in ipairs(Recipes[output]) do
|
|
|
|
local not_in_list = false
|
|
|
|
for _,item in ipairs(recipe.input) do
|
|
|
|
if not techage.in_list(ingr, item) then
|
|
|
|
not_in_list = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not_in_list == false then
|
|
|
|
return idx -- list number of the recipe
|
|
|
|
end
|
2019-06-29 00:46:44 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-21 18:45:15 +03:00
|
|
|
-- Return a list with all outputs of the given list of ingredients
|
|
|
|
local function get_recipes(ingr)
|
|
|
|
if #ingr > 0 then
|
|
|
|
local tbl = {}
|
|
|
|
for _,item in ipairs(ingr) do
|
|
|
|
if Ingredients[item] then
|
|
|
|
for _,output in ipairs(Ingredients[item]) do
|
2019-06-29 00:46:44 +03:00
|
|
|
if all_ingredients_available(output, ingr) then
|
|
|
|
techage.add_to_set(tbl, output)
|
|
|
|
end
|
2019-06-21 18:45:15 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tbl
|
|
|
|
else
|
|
|
|
return KeyList
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function techage.furnace.get_ingredients(pos)
|
|
|
|
local inv = M(pos):get_inventory()
|
|
|
|
local tbl = {}
|
|
|
|
for _,stack in ipairs(inv:get_list('src')) do
|
|
|
|
if stack:get_name() ~= "" then
|
|
|
|
tbl[#tbl+1] = stack:get_name()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tbl
|
|
|
|
end
|
|
|
|
|
2019-11-17 02:08:10 +03:00
|
|
|
local function remove_item_from_list(list, item)
|
|
|
|
for _,stack in ipairs(list) do
|
|
|
|
if stack:get_name() == item then
|
|
|
|
stack:set_count(stack:get_count() - 1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2019-06-10 22:31:58 +03:00
|
|
|
-- move recipe src items to output inventory
|
2019-06-21 19:53:30 +03:00
|
|
|
local function process(inv, recipe, output)
|
2019-11-17 02:08:10 +03:00
|
|
|
-- check dst inv
|
|
|
|
local stack = ItemStack(output)
|
|
|
|
stack:set_count(recipe.number)
|
|
|
|
if not inv:room_for_item("dst", stack) then
|
|
|
|
return techage.BLOCKED
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
|
|
|
-- remove items
|
2019-11-17 02:08:10 +03:00
|
|
|
local list = inv:get_list("src")
|
2019-06-10 22:31:58 +03:00
|
|
|
for _,item in ipairs(recipe.input) do
|
2019-11-17 02:08:10 +03:00
|
|
|
if not remove_item_from_list(list, item) then
|
|
|
|
return techage.STANDBY
|
|
|
|
end
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
2019-11-17 02:08:10 +03:00
|
|
|
-- store changes on scr
|
|
|
|
inv:set_list("src", list)
|
|
|
|
-- add output to dst
|
2019-06-10 22:31:58 +03:00
|
|
|
inv:add_item("dst", stack)
|
2019-11-17 02:08:10 +03:00
|
|
|
return techage.RUNNING
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.furnace.check_if_worth_to_wakeup(pos, nvm)
|
2019-11-17 02:08:10 +03:00
|
|
|
local inv = M(pos):get_inventory()
|
2020-01-31 21:55:10 +03:00
|
|
|
if not nvm.output or not nvm.num_recipe then
|
2019-11-17 02:08:10 +03:00
|
|
|
return false
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
local recipe = Recipes[nvm.output] and Recipes[nvm.output][nvm.num_recipe]
|
2019-11-17 02:08:10 +03:00
|
|
|
if not recipe then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- check dst inv
|
2020-01-31 21:55:10 +03:00
|
|
|
local stack = ItemStack(nvm.output)
|
2019-11-17 02:08:10 +03:00
|
|
|
stack:set_count(recipe.number)
|
|
|
|
if not inv:room_for_item("dst", stack) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- check src inv
|
|
|
|
local list = inv:get_list("src")
|
|
|
|
for _,item in ipairs(recipe.input) do
|
|
|
|
if not remove_item_from_list(list, item) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2019-06-10 22:31:58 +03:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.furnace.smelting(pos, nvm, elapsed)
|
2019-06-10 22:31:58 +03:00
|
|
|
local inv = M(pos):get_inventory()
|
2019-06-21 19:53:30 +03:00
|
|
|
local state = techage.RUNNING
|
2019-06-10 22:31:58 +03:00
|
|
|
if inv and not inv:is_empty("src") then
|
2020-01-31 21:55:10 +03:00
|
|
|
if not nvm.output or not nvm.num_recipe then
|
2019-11-03 23:21:03 +03:00
|
|
|
return techage.FAULT, "recipe error"
|
2019-06-21 19:53:30 +03:00
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
local recipe = Recipes[nvm.output] and Recipes[nvm.output][nvm.num_recipe]
|
2019-06-21 19:53:30 +03:00
|
|
|
if not recipe then
|
2019-11-03 23:21:03 +03:00
|
|
|
return techage.FAULT, "recipe error"
|
2019-06-21 19:53:30 +03:00
|
|
|
end
|
2019-06-10 22:31:58 +03:00
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
elapsed = elapsed + (nvm.leftover or 0)
|
2019-06-10 22:31:58 +03:00
|
|
|
while elapsed >= recipe.time do
|
2020-01-31 21:55:10 +03:00
|
|
|
state = process(inv, recipe, nvm.output)
|
2019-11-17 02:08:10 +03:00
|
|
|
if state ~= techage.RUNNING then
|
|
|
|
return state
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
|
|
|
elapsed = elapsed - recipe.time
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.leftover = elapsed
|
2019-08-22 21:22:52 +03:00
|
|
|
if recipe.time >= 10 then
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.item_percent = math.min(math.floor((nvm.leftover * 100.0) / recipe.time), 100)
|
2019-08-22 21:22:52 +03:00
|
|
|
else
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.item_percent = 100
|
2019-08-22 21:22:52 +03:00
|
|
|
end
|
2019-06-10 22:31:58 +03:00
|
|
|
return state
|
|
|
|
end
|
|
|
|
return techage.STANDBY
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.furnace.get_output(nvm, ingr, idx)
|
2019-06-21 18:45:15 +03:00
|
|
|
local tbl = get_recipes(ingr)
|
|
|
|
idx = range(idx, 1, #tbl)
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.output = tbl[idx] or tbl[1]
|
|
|
|
nvm.num_recipe = all_ingredients_available(nvm.output, ingr)
|
|
|
|
return nvm.output
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
|
|
|
|
2019-06-21 18:45:15 +03:00
|
|
|
function techage.furnace.get_num_recipes(ingr)
|
|
|
|
return #get_recipes(ingr)
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.furnace.reset_cooking(nvm)
|
|
|
|
nvm.leftover = 0
|
|
|
|
nvm.item_percent = 0
|
2019-06-10 22:31:58 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if minetest.global_exists("unified_inventory") then
|
|
|
|
unified_inventory.register_craft_type("ta3_melting", {
|
2019-07-02 22:33:12 +03:00
|
|
|
description = S("TA3 Melting"),
|
2019-06-10 22:31:58 +03:00
|
|
|
icon = "techage_concrete.png^techage_appl_furnace.png^techage_frame_ta3.png",
|
|
|
|
width = 2,
|
|
|
|
height = 2,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function techage.furnace.register_recipe(recipe)
|
2019-06-21 19:53:30 +03:00
|
|
|
local words = string.split(recipe.output, " ")
|
|
|
|
local output = words[1]
|
|
|
|
local number = tonumber(words[2] or 1)
|
2019-06-10 22:31:58 +03:00
|
|
|
table.insert(KeyList, output)
|
2019-08-19 00:16:13 +03:00
|
|
|
--print(recipe.output, dump(recipe.recipe))
|
|
|
|
if not Recipes[output] then
|
|
|
|
Recipes[output] = {}
|
|
|
|
end
|
|
|
|
table.insert(Recipes[output], {
|
2019-06-10 22:31:58 +03:00
|
|
|
input = recipe.recipe,
|
|
|
|
number = number,
|
|
|
|
time = math.max((recipe.time or 3) * number, 2),
|
2019-08-19 00:16:13 +03:00
|
|
|
})
|
2019-06-21 18:45:15 +03:00
|
|
|
for _,item in ipairs(recipe.recipe) do
|
|
|
|
if Ingredients[item] then
|
|
|
|
techage.add_to_set(Ingredients[item], output)
|
|
|
|
else
|
|
|
|
Ingredients[item] = {output}
|
|
|
|
end
|
|
|
|
end
|
2019-06-10 22:31:58 +03:00
|
|
|
|
|
|
|
if minetest.global_exists("unified_inventory") then
|
|
|
|
recipe.items = recipe.recipe
|
|
|
|
recipe.type = "ta3_melting"
|
|
|
|
unified_inventory.register_craft(recipe)
|
|
|
|
end
|
|
|
|
end
|