techage_modpack_ru_upd/techage/basis/recipe_lib.lua

293 lines
7.1 KiB
Lua
Raw Permalink Normal View History

2020-05-31 23:31:18 +03:00
--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
2020-10-25 23:32:47 +03:00
AGPL v3
2020-05-31 23:31:18 +03:00
See LICENSE.txt for more information
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
Recipe lib for formspecs
]]--
local S = techage.S
local M = minetest.get_meta
2021-02-07 16:37:07 +03:00
local Recipes = {} -- {rtype = {ouput = {....},...}}
local NormalizedRecipes = {} -- {output = "", items = {...}}
2020-05-31 23:31:18 +03:00
local range = techage.in_range
techage.recipes = {}
2022-01-03 13:59:31 +03:00
local GROUP_ITEMS = {
stone = "default:cobble",
wood = "default:wood",
book = "default:book",
sand = "default:sand",
leaves = "default:leaves",
stick = "default:stick",
tree = "default:tree",
vessel = "vessels:glass_bottle",
wool = "wool:white",
}
2020-05-31 23:31:18 +03:00
local RECIPE = {
output = {name = "", num = 0},
waste = {name = "", num = 0},
2022-01-04 21:43:30 +03:00
input = {
2020-05-31 23:31:18 +03:00
{name = "", num =0},
{name = "", num =0},
{name = "", num =0},
{name = "", num =0},
},
}
2022-01-03 13:59:31 +03:00
local function filter_recipes_based_on_points(recipes, owner)
local ex_points = 0
if owner then
local player = minetest.get_player_by_name(owner)
ex_points = techage.get_expoints(player) or 0
end
2022-01-04 21:43:30 +03:00
2022-01-03 13:59:31 +03:00
local tbl = {}
for _,item in ipairs(recipes) do
if ex_points >= (item.ex_points or 0) then
tbl[#tbl + 1] = item
end
end
return tbl
end
2020-05-31 23:31:18 +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
2022-07-11 21:24:44 +03:00
function techage.recipes.get(nvm, rtype, owner)
2020-05-31 23:31:18 +03:00
local recipes = Recipes[rtype] or {}
2022-07-11 21:24:44 +03:00
if owner then
recipes = filter_recipes_based_on_points(recipes, owner)
end
return recipes[nvm.recipe_idx or 1] or recipes[1]
2020-05-31 23:31:18 +03:00
end
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
-- 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
2022-01-04 21:43:30 +03:00
2021-02-07 16:37:07 +03:00
local name, num, output
2020-05-31 23:31:18 +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
2022-01-04 21:43:30 +03:00
if recipe.waste then
2020-05-31 23:31:18 +03:00
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-21 18:45:15 +03:00
item.catalyst = recipe.catalyst
2022-01-03 13:59:31 +03:00
item.ex_points = recipe.ex_points or 0
2020-07-21 18:45:15 +03:00
Recipes[rtype][#Recipes[rtype]+1] = item
2021-02-07 16:37:07 +03:00
output = name
2020-05-31 23:31:18 +03:00
2022-01-03 13:59:31 +03:00
techage.recipes.register_craft({
2022-01-04 21:43:30 +03:00
output = recipe.output,
2022-01-03 13:59:31 +03:00
items = recipe.input,
type = rtype,
})
2021-02-07 16:37:07 +03:00
NormalizedRecipes[output] = {
2022-01-04 21:43:30 +03:00
output = recipe.output,
2021-02-07 16:37:07 +03:00
items = recipe.input,
}
2020-05-31 23:31:18 +03:00
end
2022-01-03 13:59:31 +03:00
function techage.recipes.formspec(x, y, rtype, nvm, owner)
2020-05-31 23:31:18 +03:00
local recipes = Recipes[rtype] or {}
2022-01-03 13:59:31 +03:00
recipes = filter_recipes_based_on_points(recipes, owner)
2020-07-21 18:45:15 +03:00
nvm.recipe_idx = range(nvm.recipe_idx or 1, 1, #recipes)
2020-05-31 23:31:18 +03:00
local idx = nvm.recipe_idx
2020-07-21 18:45:15 +03:00
local recipe = recipes[idx] or RECIPE
2020-05-31 23:31:18 +03:00
local output = recipe.output.name.." "..recipe.output.num
local waste = recipe.waste.name.." "..recipe.waste.num
2020-07-21 18:45:15 +03:00
local catalyst = recipe.catalyst and techage.item_image_small(2.05, 0, recipe.catalyst, S("Catalyst")) or ""
2020-05-31 23:31:18 +03:00
return "container["..x..","..y.."]"..
"background[0,0;4,3;techage_form_grey.png]"..
input_string(recipe)..
2020-07-21 18:45:15 +03:00
"image[2,0.7;1,1;techage_form_arrow.png]"..
catalyst..
2020-05-31 23:31:18 +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-21 18:45:15 +03:00
"label[1.9,2.2;"..S("Recipe")..": "..idx.."/"..#recipes.."]"..
2020-05-31 23:31:18 +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
local nvm = techage.get_nvm(pos)
2022-01-04 21:43:30 +03:00
2020-05-31 23:31:18 +03:00
nvm.recipe_idx = nvm.recipe_idx or 1
2022-01-04 21:43:30 +03:00
if not nvm.running then
2020-05-31 23:31:18 +03:00
if fields.next == ">>" then
nvm.recipe_idx = nvm.recipe_idx + 1
2023-03-05 13:54:37 +03:00
return true
2020-05-31 23:31:18 +03:00
elseif fields.priv == "<<" then
nvm.recipe_idx = nvm.recipe_idx - 1
2023-03-05 13:54:37 +03:00
return true
2020-05-31 23:31:18 +03:00
end
end
end
2021-02-07 16:37:07 +03:00
function techage.recipes.get_recipe(name)
return NormalizedRecipes[name]
end
2022-01-04 21:43:30 +03:00
2022-09-24 12:01:30 +03:00
function techage.recipes.set_recipe(pos, rtype, idx)
local nvm = techage.get_nvm(pos)
if not nvm.running then
local recipes = Recipes[rtype] or {}
idx = tonumber(idx) or 1
nvm.recipe_idx = range(idx, 1, #recipes)
end
end
2022-01-04 21:43:30 +03:00
2022-01-03 13:59:31 +03:00
function techage.recipes.get_default_group_item_name(item_name)
if item_name and item_name:sub(1, 6) == "group:" then
local default_name = GROUP_ITEMS[item_name:sub(7)]
if default_name then
return default_name
end
end
return item_name
end
function techage.recipes.add_group_item(group, default_item_name)
GROUP_ITEMS[group] = default_item_name
end
-------------------------------------------------------------------------------
2022-01-04 21:43:30 +03:00
-- Borrowed from ghaydn
2022-01-03 13:59:31 +03:00
-------------------------------------------------------------------------------
local has_i3 = minetest.get_modpath("i3")
local has_ui = minetest.get_modpath("unified_inventory")
local has_cg = minetest.get_modpath("craftguide")
local function format_i3(input)
local output = {}
for _, entry in ipairs(input) do
local secondput = ""
if type(entry) == "table" then
for _, secondtry in ipairs(entry) do
secondput = secondput..secondtry..","
end
table.insert(output, secondput)
else
table.insert(output, entry)
end
end
return output
end
techage.recipes.register_craft_type = function(name, def)
if has_cg then
local cg_def = {
description = def.description,
icon = def.icon,
}
craftguide.register_craft_type(name, cg_def)
end
if has_i3 then
local i3_def = {
description = def.description,
icon = def.icon,
width = def.width or 3,
height = def.height or 3,
dynamic_display_size = def.dynamic_display_size or nil,
uses_crafting_grid = def.uses_crafting_grid,
}
i3.register_craft_type(name, i3_def)
end
if has_ui then
local ui_def = {
description = def.description,
icon = def.icon,
width = def.width or 3,
height = def.height or 3,
dynamic_display_size = def.dynamic_display_size or nil,
uses_crafting_grid = def.uses_crafting_grid,
}
unified_inventory.register_craft_type(name, ui_def)
end
end
techage.recipes.register_craft = function(def)
if not def.items then
if def.input then
def.items = table.copy(def.input)
elseif def.recipe then
def.items = table.copy(def.recipe)
end
end
if not def.result then
if def.output then def.result = def.output end
end
if has_cg then
local cg_def = {
result = def.result,
type = def.type,
items = def.items,
}
craftguide.register_craft(cg_def)
end
if has_i3 then
local i3_def = {
result = def.result,
type = def.type,
items = format_i3(def.items),
width = def.width or 3,
}
i3.register_craft(i3_def)
end
if has_ui then
local ui_def = {
output = def.result,
type = def.type,
items = def.items,
width = def.width or 3,
height = def.height or 3,
}
unified_inventory.register_craft(ui_def)
end
end