2019-11-10 23:08:37 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
TechAge
|
|
|
|
=======
|
|
|
|
|
|
|
|
Copyright (C) 2019 Joachim Stolberg
|
|
|
|
|
2020-10-19 20:09:17 +03:00
|
|
|
AGPL v3
|
2019-11-10 23:08:37 +03:00
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
Recipe lib for formspecs
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local S = techage.S
|
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2021-02-06 19:20:45 +03:00
|
|
|
local Recipes = {} -- {rtype = {ouput = {....},...}}
|
|
|
|
local NormalizedRecipes = {} -- {output = "", items = {...}}
|
2019-11-10 23:08:37 +03:00
|
|
|
|
2020-01-26 01:15:44 +03:00
|
|
|
local range = techage.in_range
|
2019-11-10 23:08:37 +03:00
|
|
|
|
|
|
|
techage.recipes = {}
|
|
|
|
|
2019-11-17 02:08:10 +03:00
|
|
|
local RECIPE = {
|
|
|
|
output = {name = "", num = 0},
|
|
|
|
waste = {name = "", num = 0},
|
|
|
|
input = {
|
|
|
|
{name = "", num =0},
|
|
|
|
{name = "", num =0},
|
|
|
|
{name = "", num =0},
|
|
|
|
{name = "", num =0},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-10 23:08:37 +03:00
|
|
|
-- Formspec
|
|
|
|
local function input_string(recipe)
|
|
|
|
local tbl = {}
|
|
|
|
for idx, item in ipairs(recipe.input) do
|
|
|
|
local x = ((idx-1) % 2)
|
|
|
|
local y = math.floor((idx-1) / 2)
|
|
|
|
tbl[idx] = techage.item_image(x, y, item.name.." "..item.num)
|
|
|
|
end
|
|
|
|
return table.concat(tbl, "")
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.recipes.get(nvm, rtype)
|
2019-11-10 23:08:37 +03:00
|
|
|
local recipes = Recipes[rtype] or {}
|
2020-07-16 19:34:19 +03:00
|
|
|
return recipes[nvm.recipe_idx or 1]
|
2019-11-10 23:08:37 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Add 4 input/output/waste recipe
|
|
|
|
-- {
|
|
|
|
-- output = "<item-name> <units>", -- units = 1..n
|
|
|
|
-- waste = "<item-name> <units>", -- units = 1..n
|
|
|
|
-- input = { -- up to 4 items
|
|
|
|
-- "<item-name> <units>",
|
|
|
|
-- "<item-name> <units>",
|
|
|
|
-- },
|
|
|
|
-- }
|
|
|
|
function techage.recipes.add(rtype, recipe)
|
|
|
|
if not Recipes[rtype] then
|
|
|
|
Recipes[rtype] = {}
|
|
|
|
end
|
|
|
|
|
2021-02-06 19:20:45 +03:00
|
|
|
local name, num, output
|
2019-11-10 23:08:37 +03:00
|
|
|
local item = {input = {}}
|
|
|
|
for idx = 1,4 do
|
|
|
|
local inp = recipe.input[idx] or ""
|
|
|
|
name, num = unpack(string.split(inp, " "))
|
|
|
|
item.input[idx] = {name = name or "", num = tonumber(num) or 0}
|
|
|
|
end
|
|
|
|
if recipe.waste then
|
|
|
|
name, num = unpack(string.split(recipe.waste, " "))
|
|
|
|
else
|
|
|
|
name, num = "", "0"
|
|
|
|
end
|
|
|
|
item.waste = {name = name or "", num = tonumber(num) or 0}
|
|
|
|
name, num = unpack(string.split(recipe.output, " "))
|
|
|
|
item.output = {name = name or "", num = tonumber(num) or 0}
|
2020-07-06 20:13:37 +03:00
|
|
|
item.catalyst = recipe.catalyst
|
2020-07-16 19:34:19 +03:00
|
|
|
Recipes[rtype][#Recipes[rtype]+1] = item
|
2021-02-06 19:20:45 +03:00
|
|
|
output = name
|
2019-11-10 23:08:37 +03:00
|
|
|
|
|
|
|
if minetest.global_exists("unified_inventory") then
|
|
|
|
unified_inventory.register_craft({
|
|
|
|
output = recipe.output,
|
|
|
|
items = recipe.input,
|
|
|
|
type = rtype,
|
|
|
|
})
|
|
|
|
end
|
2021-02-06 19:20:45 +03:00
|
|
|
NormalizedRecipes[output] = {
|
|
|
|
output = recipe.output,
|
|
|
|
items = recipe.input,
|
|
|
|
}
|
2019-11-10 23:08:37 +03:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
function techage.recipes.formspec(x, y, rtype, nvm)
|
2019-11-10 23:08:37 +03:00
|
|
|
local recipes = Recipes[rtype] or {}
|
2020-07-16 19:34:19 +03:00
|
|
|
nvm.recipe_idx = range(nvm.recipe_idx or 1, 1, #recipes)
|
2020-01-31 21:55:10 +03:00
|
|
|
local idx = nvm.recipe_idx
|
2020-07-16 19:34:19 +03:00
|
|
|
local recipe = recipes[idx] or RECIPE
|
2019-11-10 23:08:37 +03:00
|
|
|
local output = recipe.output.name.." "..recipe.output.num
|
|
|
|
local waste = recipe.waste.name.." "..recipe.waste.num
|
2020-07-06 20:13:37 +03:00
|
|
|
local catalyst = recipe.catalyst and techage.item_image_small(2.05, 0, recipe.catalyst, S("Catalyst")) or ""
|
2019-11-10 23:08:37 +03:00
|
|
|
return "container["..x..","..y.."]"..
|
|
|
|
"background[0,0;4,3;techage_form_grey.png]"..
|
|
|
|
input_string(recipe)..
|
2020-07-06 20:13:37 +03:00
|
|
|
"image[2,0.7;1,1;techage_form_arrow.png]"..
|
|
|
|
catalyst..
|
2019-11-10 23:08:37 +03:00
|
|
|
techage.item_image(2.95, 0, output)..
|
|
|
|
techage.item_image(2.95, 1, waste)..
|
|
|
|
"button[0,2;1,1;priv;<<]"..
|
|
|
|
"button[1,2;1,1;next;>>]"..
|
2020-07-16 19:34:19 +03:00
|
|
|
"label[1.9,2.2;"..S("Recipe")..": "..idx.."/"..#recipes.."]"..
|
2019-11-10 23:08:37 +03:00
|
|
|
"container_end[]"
|
|
|
|
end
|
|
|
|
|
|
|
|
function techage.recipes.on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
2020-01-31 21:55:10 +03:00
|
|
|
local nvm = techage.get_nvm(pos)
|
2019-11-10 23:08:37 +03:00
|
|
|
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.recipe_idx = nvm.recipe_idx or 1
|
|
|
|
if not nvm.running then
|
2019-11-10 23:08:37 +03:00
|
|
|
if fields.next == ">>" then
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.recipe_idx = nvm.recipe_idx + 1
|
2019-11-10 23:08:37 +03:00
|
|
|
elseif fields.priv == "<<" then
|
2020-01-31 21:55:10 +03:00
|
|
|
nvm.recipe_idx = nvm.recipe_idx - 1
|
2019-11-10 23:08:37 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-06 19:20:45 +03:00
|
|
|
|
|
|
|
function techage.recipes.get_recipe(name)
|
|
|
|
return NormalizedRecipes[name]
|
|
|
|
end
|
2021-10-22 20:03:02 +03:00
|
|
|
|