craftguide/init.lua

2322 lines
55 KiB
Lua
Raw Normal View History

craftguide = {}
2018-12-16 23:20:54 +01:00
2019-02-17 15:04:01 +01:00
-- Caches
2019-09-09 18:27:02 +02:00
local pdata = {}
2019-02-17 15:04:01 +01:00
local init_items = {}
local searches = {}
2019-01-22 01:44:36 +01:00
local recipes_cache = {}
2019-02-17 15:04:01 +01:00
local usages_cache = {}
local fuel_cache = {}
2020-07-19 23:51:51 +02:00
local replacements = {fuel = {}}
2019-09-09 23:57:37 +02:00
local toolrepair
local progressive_mode = core.settings:get_bool "craftguide_progressive_mode"
2018-11-11 22:24:48 +01:00
2019-10-09 00:16:42 +02:00
local http = core.request_http_api()
local singleplayer = core.is_singleplayer()
2019-10-09 00:16:42 +02:00
2019-10-03 22:08:51 +02:00
local reg_items = core.registered_items
local reg_tools = core.registered_tools
2020-11-24 17:12:25 +01:00
local reg_entities = core.registered_entities
2019-10-03 22:08:51 +02:00
local reg_aliases = core.registered_aliases
local log = core.log
2019-08-29 15:20:19 +02:00
local after = core.after
2019-09-10 13:41:17 +02:00
local clr = core.colorize
local parse_json = core.parse_json
local write_json = core.write_json
local chat_send = core.chat_send_player
local show_formspec = core.show_formspec
2019-08-30 18:50:55 +02:00
local globalstep = core.register_globalstep
local on_shutdown = core.register_on_shutdown
local get_players = core.get_connected_players
2020-07-17 15:31:24 +02:00
local get_craft_result = core.get_craft_result
2020-07-12 17:05:11 +02:00
local translate = minetest.get_translated_string
2019-08-30 18:50:55 +02:00
local on_joinplayer = core.register_on_joinplayer
2019-09-18 20:09:03 +02:00
local get_all_recipes = core.get_all_craft_recipes
2019-08-30 18:50:55 +02:00
local register_command = core.register_chatcommand
2019-08-29 15:20:19 +02:00
local get_player_by_name = core.get_player_by_name
2019-09-29 18:34:17 +02:00
local slz, dslz = core.serialize, core.deserialize
2019-08-30 18:50:55 +02:00
local on_mods_loaded = core.register_on_mods_loaded
local on_leaveplayer = core.register_on_leaveplayer
local get_player_info = core.get_player_information
2019-08-30 18:50:55 +02:00
local on_receive_fields = core.register_on_player_receive_fields
2019-08-29 15:20:19 +02:00
local ESC = core.formspec_escape
2020-04-16 01:55:38 +02:00
local S = core.get_translator "craftguide"
2019-12-17 02:09:40 +01:00
local ES = function(...)
return ESC(S(...))
end
2020-04-07 18:03:53 +02:00
local maxn, sort, concat, copy, insert, remove =
2019-10-03 17:02:19 +02:00
table.maxn, table.sort, table.concat, table.copy,
2020-04-07 18:03:53 +02:00
table.insert, table.remove
2020-11-18 02:17:59 +01:00
local sprintf, find, gmatch, match, sub, split, upper, lower =
2019-03-03 16:20:19 +01:00
string.format, string.find, string.gmatch, string.match,
2020-11-18 02:17:59 +01:00
string.sub, string.split, string.upper, string.lower
local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs
2020-12-08 16:05:25 +01:00
local pairs, ipairs, next, type, setmetatable, tonum =
pairs, ipairs, next, type, setmetatable, tonumber
local vec_add, vec_mul = vector.add, vector.multiply
2016-12-17 13:15:02 +01:00
2020-12-10 16:46:11 +01:00
local ROWS = 9
local LINES = 10
local IPP = ROWS * LINES
2020-06-20 21:12:53 +02:00
local MAX_FAVS = 6
2020-07-03 18:40:46 +02:00
local ITEM_BTN_SIZE = 1.1
2020-07-10 01:26:48 +02:00
-- Progressive mode
local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5
2020-12-06 02:28:00 +01:00
local MIN_FORMSPEC_VERSION = 4
local PNG = {
2020-08-02 03:10:16 +02:00
bg = "craftguide_bg.png",
bg_full = "craftguide_bg_full.png",
search = "craftguide_search_icon.png",
clear = "craftguide_clear_icon.png",
prev = "craftguide_next_icon.png^\\[transformFX",
next = "craftguide_next_icon.png",
arrow = "craftguide_arrow.png",
fire = "craftguide_fire.png",
2020-02-16 14:12:21 +01:00
fire_anim = "craftguide_fire_anim.png",
2020-08-02 03:10:16 +02:00
book = "craftguide_book.png",
sign = "craftguide_sign.png",
nothing = "craftguide_no.png",
selected = "craftguide_selected.png",
2020-02-16 14:12:21 +01:00
furnace_anim = "craftguide_furnace_anim.png",
search_hover = "craftguide_search_icon_hover.png",
2020-08-02 03:10:16 +02:00
clear_hover = "craftguide_clear_icon_hover.png",
prev_hover = "craftguide_next_icon_hover.png^\\[transformFX",
next_hover = "craftguide_next_icon_hover.png",
}
2018-04-27 22:18:18 +02:00
2020-11-15 15:30:46 +01:00
local fs_elements = {
box = "box[%f,%f;%f,%f;%s]",
label = "label[%f,%f;%s]",
image = "image[%f,%f;%f,%f;%s]",
button = "button[%f,%f;%f,%f;%s;%s]",
2019-03-21 17:36:00 +01:00
tooltip = "tooltip[%f,%f;%f,%f;%s]",
item_image = "item_image[%f,%f;%f,%f;%s]",
2020-11-18 02:17:59 +01:00
bg9 = "background9[%f,%f;%f,%f;%s;false;%u]",
2020-12-15 19:49:13 +01:00
model = "model[%f,%f;%f,%f;%s;%s;%s;0,0;true;true;%s]",
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
2020-03-19 17:32:31 +01:00
animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]",
2020-12-08 16:05:25 +01:00
scrollbar = "scrollbar[%f,%f;%f,%f;horizontal;%s;%u]",
item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
arrow = "image_button[%f,%f;0.7,0.7;%s;%s;;;false;%s]",
}
2019-01-22 01:59:54 +01:00
2020-11-15 15:30:46 +01:00
local styles = sprintf([[
2020-11-15 03:03:20 +01:00
style_type[label,field;font_size=+0]
2020-11-23 21:52:06 +01:00
style_type[image_button;border=false;sound=craftguide_click]
2020-12-10 17:50:18 +01:00
style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s;
sound=craftguide_click]
2020-12-05 22:35:12 +01:00
style[filter;border=false]
2020-08-04 16:36:09 +02:00
style[search;fgimg=%s;fgimg_hovered=%s]
style[clear;fgimg=%s;fgimg_hovered=%s]
style[prev_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_page;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[prev_recipe;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_recipe;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[prev_usage;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
style[next_usage;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]
2020-12-08 16:05:25 +01:00
style[pagenum,no_item,no_rcp;border=false;font=bold;font_size=+2;content_offset=0]
2020-12-10 17:50:18 +01:00
style[craft_rcp,craft_usg;border=false;noclip=true;font_size=+0;sound=craftguide_craft;
bgimg=craftguide_btn9.png;bgimg_hovered=craftguide_btn9_hovered.png;
bgimg_pressed=craftguide_btn9_pressed.png;bgimg_middle=4,6]
2020-08-04 16:36:09 +02:00
]],
PNG.selected, PNG.selected,
PNG.search, PNG.search_hover,
PNG.clear, PNG.clear_hover,
PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover, PNG.next_hover,
PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover, PNG.next_hover,
PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover, PNG.next_hover)
2020-12-06 02:28:00 +01:00
local function get_lang_code(info)
2020-07-07 19:15:02 +02:00
return info and info.lang_code
2020-02-26 20:51:08 +01:00
end
2020-12-06 02:28:00 +01:00
local function get_formspec_version(info)
return info and info.formspec_version or 1
end
2020-05-14 14:39:07 +02:00
local function outdated(name)
2020-11-15 15:30:46 +01:00
local fs = sprintf([[
2020-12-06 02:28:00 +01:00
size[7.1,1.3]
2020-05-14 14:39:07 +02:00
image[0,0;1,1;%s]
label[1,0;%s]
2020-12-06 02:28:00 +01:00
button_exit[3.1,0.8;1,1;;OK]
2020-05-14 14:39:07 +02:00
]],
PNG.book,
"Your Minetest client is outdated.\n" ..
"Get the latest version on minetest.net to use the Crafting Guide.")
return show_formspec(name, "craftguide", fs)
end
2019-03-21 17:30:46 +01:00
craftguide.group_stereotypes = {
dye = "dye:white",
wool = "wool:white",
2020-04-16 04:48:44 +02:00
wood = "default:wood",
tree = "default:tree",
2020-08-02 03:18:37 +02:00
sand = "default:sand",
glass = "default:glass",
stick = "default:stick",
2020-08-02 03:04:32 +02:00
stone = "default:stone",
2020-08-02 03:18:37 +02:00
leaves = "default:leaves",
coal = "default:coal_lump",
vessel = "vessels:glass_bottle",
flower = "flowers:dandelion_yellow",
water_bucket = "bucket:bucket_water",
mesecon_conductor_craftable = "mesecons:wire_00000000_off",
}
local group_names = {
2020-07-09 17:01:16 +02:00
dye = S"Any dye",
2020-08-02 03:10:16 +02:00
coal = S"Any coal",
2020-04-16 02:09:27 +02:00
sand = S"Any sand",
2020-08-02 03:10:16 +02:00
tree = S"Any tree",
wool = S"Any wool",
glass = S"Any glass",
2020-04-16 02:09:27 +02:00
stick = S"Any stick",
2020-08-02 03:05:37 +02:00
stone = S"Any stone",
2020-08-02 03:10:16 +02:00
carpet = S"Any carpet",
flower = S"Any flower",
leaves = S"Any leaves",
2020-04-16 02:09:27 +02:00
vessel = S"Any vessel",
2020-07-09 17:01:16 +02:00
wood = S"Any wood planks",
2020-08-02 03:10:16 +02:00
mushroom = S"Any mushroom",
2020-04-16 02:09:27 +02:00
["color_red,flower"] = S"Any red flower",
["color_blue,flower"] = S"Any blue flower",
["color_black,flower"] = S"Any black flower",
["color_white,flower"] = S"Any white flower",
["color_green,flower"] = S"Any green flower",
["color_orange,flower"] = S"Any orange flower",
["color_yellow,flower"] = S"Any yellow flower",
["color_violet,flower"] = S"Any violet flower",
["color_red,dye"] = S"Any red dye",
["color_blue,dye"] = S"Any blue dye",
["color_grey,dye"] = S"Any grey dye",
["color_pink,dye"] = S"Any pink dye",
["color_cyan,dye"] = S"Any cyan dye",
["color_black,dye"] = S"Any black dye",
["color_white,dye"] = S"Any white dye",
["color_brown,dye"] = S"Any brown dye",
["color_green,dye"] = S"Any green dye",
["color_orange,dye"] = S"Any orange dye",
["color_yellow,dye"] = S"Any yellow dye",
["color_violet,dye"] = S"Any violet dye",
["color_magenta,dye"] = S"Any magenta dye",
["color_dark_grey,dye"] = S"Any dark grey dye",
["color_dark_green,dye"] = S"Any dark green dye",
}
2020-11-24 17:12:25 +01:00
craftguide.model_alias = {
["boats:boat"] = {name = "boats:boat", drawtype = "entity"},
2020-12-15 19:49:13 +01:00
["carts:cart"] = {name = "carts:cart", drawtype = "entity", frames = "0,0"},
2020-11-24 17:12:25 +01:00
}
local function err(str)
return log("error", str)
end
local function msg(name, str)
2020-11-15 15:30:46 +01:00
return chat_send(name, sprintf("[craftguide] %s", str))
end
local function is_str(x)
return type(x) == "string"
end
local function true_str(str)
return is_str(str) and str ~= ""
end
local function is_table(x)
return type(x) == "table"
end
local function is_func(x)
return type(x) == "function"
end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
2020-11-18 02:17:59 +01:00
local function fmt(elem, ...)
return sprintf(fs_elements[elem], ...)
2020-11-15 03:47:43 +01:00
end
2019-12-17 01:33:40 +01:00
local function clean_name(item)
2019-10-13 18:31:46 +02:00
if sub(item, 1, 1) == ":" then
item = sub(item, 2)
end
return item
end
2019-10-03 17:02:19 +02:00
local function array_diff(t1, t2)
2019-10-01 13:05:53 +02:00
local hash = {}
for i = 1, #t1 do
local v = t1[i]
hash[v] = true
end
for i = 1, #t2 do
local v = t2[i]
hash[v] = nil
end
local diff, c = {}, 0
for i = 1, #t1 do
local v = t1[i]
if hash[v] then
c = c + 1
diff[c] = v
end
end
return diff
end
2019-10-03 17:02:19 +02:00
local function table_eq(T1, T2)
local avoid_loops = {}
local function recurse(t1, t2)
if type(t1) ~= type(t2) then return end
2020-04-16 03:57:29 +02:00
if not is_table(t1) then
2019-10-03 17:02:19 +02:00
return t1 == t2
end
2019-10-31 23:39:20 +01:00
if avoid_loops[t1] then
return avoid_loops[t1] == t2
2019-10-03 17:02:19 +02:00
end
avoid_loops[t1] = t2
2019-10-05 00:36:56 +02:00
local t2k, t2kv = {}, {}
2019-10-03 17:02:19 +02:00
for k in pairs(t2) do
2019-10-05 00:36:56 +02:00
if is_table(k) then
insert(t2kv, k)
2019-10-03 17:02:19 +02:00
end
2019-10-05 00:36:56 +02:00
t2k[k] = true
2019-10-03 17:02:19 +02:00
end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
local ok
2019-10-05 00:36:56 +02:00
for i = 1, #t2kv do
local tk = t2kv[i]
2019-10-03 17:02:19 +02:00
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
2019-10-05 00:36:56 +02:00
remove(t2kv, i)
t2k[tk] = nil
2019-10-03 17:02:19 +02:00
ok = true
break
end
end
if not ok then return end
else
if v2 == nil then return end
2019-10-05 00:36:56 +02:00
t2k[k1] = nil
2019-10-03 17:02:19 +02:00
if not recurse(v1, v2) then return end
end
end
2019-10-05 00:36:56 +02:00
if next(t2k) then return end
2019-10-03 17:02:19 +02:00
return true
end
return recurse(T1, T2)
end
2019-09-09 18:27:02 +02:00
local function table_merge(t1, t2, hash)
t1 = t1 or {}
t2 = t2 or {}
if hash then
for k, v in pairs(t2) do
t1[k] = v
end
else
local c = #t1
for i = 1, #t2 do
c = c + 1
t1[c] = t2[i]
end
end
return t1
end
local function table_replace(t, val, new)
for k, v in pairs(t) do
if v == val then
t[k] = new
end
end
end
local craft_types = {}
2018-12-16 23:20:54 +01:00
function craftguide.register_craft_type(name, def)
if not true_str(name) then
2020-04-07 18:03:53 +02:00
return err "craftguide.register_craft_type(): name missing"
end
if not is_str(def.description) then
def.description = ""
end
if not is_str(def.icon) then
def.icon = ""
end
2018-12-30 21:32:36 +01:00
craft_types[name] = def
2018-12-16 23:20:54 +01:00
end
function craftguide.register_craft(def)
2020-07-07 20:22:49 +02:00
local width, c = 0, 0
if true_str(def.url) then
if not http then
2020-11-15 15:30:46 +01:00
return err(sprintf([[craftguide.register_craft(): Unable to reach %s.
2020-04-16 01:55:38 +02:00
No HTTP support for this mod: add it to the `secure.http_mods` or
`secure.trusted_mods` setting.]], def.url))
end
http.fetch({url = def.url}, function(result)
if result.succeeded then
local t = parse_json(result.data)
if is_table(t) then
return craftguide.register_craft(t)
end
end
end)
return
end
if not is_table(def) or not next(def) then
2020-04-07 18:03:53 +02:00
return err "craftguide.register_craft(): craft definition missing"
end
if #def > 1 then
for _, v in pairs(def) do
craftguide.register_craft(v)
end
return
end
if def.result then
def.output = def.result -- Backward compatibility
def.result = nil
end
if not true_str(def.output) then
2020-04-07 18:03:53 +02:00
return err "craftguide.register_craft(): output missing"
end
2019-09-11 17:10:46 +02:00
if not is_table(def.items) then
def.items = {}
end
if def.grid then
if not is_table(def.grid) then
def.grid = {}
end
2019-09-25 12:44:49 +02:00
if not is_table(def.key) then
def.key = {}
end
local cp = copy(def.grid)
2019-09-10 16:18:27 +02:00
sort(cp, function(a, b)
return #a > #b
end)
width = #cp[1]
for i = 1, #def.grid do
while #def.grid[i] < width do
def.grid[i] = def.grid[i] .. " "
end
end
for symbol in gmatch(concat(def.grid), ".") do
2020-07-07 20:22:49 +02:00
c = c + 1
def.items[c] = def.key[symbol]
end
else
2019-09-12 12:34:03 +02:00
local items, len = def.items, #def.items
def.items = {}
for i = 1, len do
2019-09-12 12:34:03 +02:00
items[i] = items[i]:gsub(",", ", ")
local rlen = #split(items[i], ",")
if rlen > width then
width = rlen
end
end
for i = 1, len do
while #split(items[i], ",") < width do
2019-09-12 12:34:03 +02:00
items[i] = items[i] .. ", "
end
end
2019-09-12 12:34:03 +02:00
for name in gmatch(concat(items, ","), "[%s%w_:]+") do
2020-07-07 20:22:49 +02:00
c = c + 1
def.items[c] = match(name, "%S+")
end
end
2018-12-30 21:32:36 +01:00
local item = match(def.output, "%S+")
recipes_cache[item] = recipes_cache[item] or {}
def.custom = true
def.width = width
insert(recipes_cache[item], def)
2018-12-16 23:20:54 +01:00
end
local recipe_filters = {}
2019-03-03 16:20:19 +01:00
function craftguide.add_recipe_filter(name, f)
if not true_str(name) then
2020-04-07 18:03:53 +02:00
return err "craftguide.add_recipe_filter(): name missing"
elseif not is_func(f) then
2020-04-07 18:03:53 +02:00
return err "craftguide.add_recipe_filter(): function missing"
end
2019-03-03 16:20:19 +01:00
recipe_filters[name] = f
end
2019-09-29 15:24:04 +02:00
function craftguide.set_recipe_filter(name, f)
if not is_str(name) then
2020-04-07 18:03:53 +02:00
return err "craftguide.set_recipe_filter(): name missing"
2019-09-29 15:24:04 +02:00
elseif not is_func(f) then
2020-04-07 18:03:53 +02:00
return err "craftguide.set_recipe_filter(): function missing"
2019-09-29 15:24:04 +02:00
end
recipe_filters = {[name] = f}
end
function craftguide.remove_recipe_filter(name)
recipe_filters[name] = nil
end
function craftguide.get_recipe_filters()
return recipe_filters
end
local function apply_recipe_filters(recipes, player)
for _, filter in pairs(recipe_filters) do
recipes = filter(recipes, player)
end
return recipes
end
2019-03-03 16:20:19 +01:00
local search_filters = {}
function craftguide.add_search_filter(name, f)
if not true_str(name) then
2020-04-07 18:03:53 +02:00
return err "craftguide.add_search_filter(): name missing"
elseif not is_func(f) then
2020-04-07 18:03:53 +02:00
return err "craftguide.add_search_filter(): function missing"
end
2019-03-03 16:20:19 +01:00
search_filters[name] = f
end
function craftguide.remove_search_filter(name)
search_filters[name] = nil
end
function craftguide.get_search_filters()
return search_filters
end
2020-12-10 15:52:14 +01:00
local function weird_desc(str)
return not true_str(str) or find(str, "\n") or not find(str, "%u")
end
local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end
local function snip(str, limit)
return #str > limit and sprintf("%s...", sub(str, 1, limit - 3)) or str
end
local function get_desc(item, lang_code)
if sub(item, 1, 1) == "_" then
item = sub(item, 2)
end
local def = reg_items[item]
if def then
local desc = def.description
desc = lang_code and translate(lang_code, desc) or desc
if true_str(desc) then
desc = desc:trim():match("[^\n]*")
if not find(desc, "%u") then
desc = toupper(desc)
end
return desc
elseif true_str(item) then
return toupper(match(item, ":(.*)"))
end
end
return S("Unknown Item (@1)", item)
end
2019-02-10 23:07:28 +01:00
local function item_has_groups(item_groups, groups)
for i = 1, #groups do
local group = groups[i]
2019-09-05 14:57:58 +02:00
if (item_groups[group] or 0) == 0 then return end
2019-02-10 23:07:28 +01:00
end
return true
end
local function extract_groups(str)
if sub(str, 1, 6) == "group:" then
return split(sub(str, 7), ",")
2019-02-10 23:07:28 +01:00
end
end
local function get_filtered_items(player, data)
local items, known, c = {}, 0, 0
for i = 1, #init_items do
local item = init_items[i]
local recipes = recipes_cache[item]
local usages = usages_cache[item]
recipes = #apply_recipe_filters(recipes or {}, player)
2020-07-10 01:26:48 +02:00
usages = #apply_recipe_filters(usages or {}, player)
if recipes > 0 or usages > 0 then
c = c + 1
items[c] = item
if data then
known = known + recipes + usages
end
end
end
if data then
data.known_recipes = known
end
return items
end
2020-07-17 15:31:24 +02:00
local function get_burntime(item)
return get_craft_result{method = "fuel", items = {item}}.time
end
local function cache_fuel(item)
local burntime = get_burntime(item)
if burntime > 0 then
fuel_cache[item] = {
type = "fuel",
items = {item},
burntime = burntime,
2020-07-19 23:51:51 +02:00
replacements = replacements.fuel[item],
2020-07-17 15:31:24 +02:00
}
end
end
local function show_item(def)
2020-07-07 20:22:49 +02:00
return def and not (def.groups.not_in_craft_guide == 1 or
def.groups.not_in_creative_inventory == 1) and
def.description and def.description ~= ""
end
local function get_usages(recipe)
local added = {}
2020-07-30 02:29:52 +02:00
for _, item in pairs(recipe.items) do
2020-07-08 02:21:42 +02:00
item = reg_aliases[item] or item
if not added[item] then
local groups = extract_groups(item)
if groups then
for name, def in pairs(reg_items) do
if not added[name] and show_item(def) and
item_has_groups(def.groups, groups) then
local usage = copy(recipe)
table_replace(usage.items, item, name)
usages_cache[name] = usages_cache[name] or {}
2020-07-07 17:59:16 +02:00
insert(usages_cache[name], 1, usage)
added[name] = true
end
end
elseif show_item(reg_items[item]) then
usages_cache[item] = usages_cache[item] or {}
2020-07-07 17:59:16 +02:00
insert(usages_cache[item], 1, recipe)
2019-09-09 18:27:02 +02:00
end
added[item] = true
end
end
end
local function cache_usages(item)
local recipes = recipes_cache[item] or {}
for i = 1, #recipes do
get_usages(recipes[i])
2019-09-09 18:27:02 +02:00
end
if fuel_cache[item] then
2020-07-16 02:29:18 +02:00
usages_cache[item] = table_merge(usages_cache[item] or {}, {fuel_cache[item]})
end
end
local function drop_table(name, drop)
2020-08-01 17:26:16 +02:00
local count_sure = 0
local drop_items = drop.items or {}
2020-08-01 16:23:19 +02:00
local max_items = drop.max_items
for i = 1, #drop_items do
local di = drop_items[i]
2020-08-01 16:23:19 +02:00
local valid_rarity = di.rarity and di.rarity > 1
if di.rarity or not max_items or
(max_items and not di.rarity and count_sure < max_items) then
for j = 1, #di.items do
local dstack = ItemStack(di.items[j])
local dname = dstack:get_name()
local dcount = dstack:get_count()
local empty = dstack:is_empty()
if not empty and (dname ~= name or
(dname == name and dcount > 1)) then
2020-08-01 17:26:16 +02:00
local rarity = valid_rarity and di.rarity
craftguide.register_craft{
type = rarity and "digging_chance" or "digging",
items = {name},
2020-11-15 15:30:46 +01:00
output = sprintf("%s %u", dname, dcount),
2020-08-01 17:26:16 +02:00
rarity = rarity,
tools = di.tools,
}
end
end
end
2020-08-01 16:23:19 +02:00
if not di.rarity then
count_sure = count_sure + 1
end
end
end
local function cache_drops(name, drop)
if true_str(drop) then
local dstack = ItemStack(drop)
2020-08-01 17:26:16 +02:00
local dname = dstack:get_name()
local empty = dstack:is_empty()
if not empty and dname ~= name then
craftguide.register_craft{
type = "digging",
items = {name},
output = drop,
}
end
elseif is_table(drop) then
drop_table(name, drop)
2019-01-22 01:44:36 +01:00
end
end
local function cache_recipes(item)
2020-07-19 23:51:51 +02:00
local recipes = get_all_recipes(item)
if replacements[item] then
2020-07-20 15:24:39 +02:00
local _recipes = {}
2020-07-19 23:51:51 +02:00
for k, v in ipairs(recipes or {}) do
_recipes[#recipes + 1 - k] = v
end
2020-07-26 16:22:04 +02:00
local shift = 0
2020-07-27 03:39:12 +02:00
local size_rpl = maxn(replacements[item])
local size_rcp = #_recipes
2020-07-26 16:22:04 +02:00
2020-07-27 03:39:12 +02:00
if size_rpl > size_rcp then
shift = size_rcp - size_rpl
2020-07-26 16:22:04 +02:00
end
2020-07-19 23:51:51 +02:00
for k, v in pairs(replacements[item]) do
2020-07-27 04:04:51 +02:00
k = k + shift
if _recipes[k] then
_recipes[k].replacements = v
end
2020-07-19 23:51:51 +02:00
end
2020-07-20 15:24:39 +02:00
recipes = _recipes
2020-07-19 23:51:51 +02:00
end
if recipes then
recipes_cache[item] = table_merge(recipes, recipes_cache[item] or {})
end
2019-09-18 20:09:03 +02:00
end
local function get_recipes(item, data, player)
local clean_item = reg_aliases[item] or item
local recipes = recipes_cache[clean_item]
local usages = usages_cache[clean_item]
2019-12-17 01:33:40 +01:00
if recipes then
recipes = apply_recipe_filters(recipes, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then return end
usages = apply_recipe_filters(usages, player)
local no_usages = not usages or #usages == 0
return not no_recipes and recipes or nil,
2020-04-07 18:03:53 +02:00
not no_usages and usages or nil
end
2019-09-09 18:27:02 +02:00
local function groups_to_items(groups, get_all)
if not get_all and #groups == 1 then
local group = groups[1]
2020-07-18 01:39:12 +02:00
local stereotype = craftguide.group_stereotypes[group]
local def = reg_items[stereotype]
2019-02-17 15:04:01 +01:00
2020-07-18 01:39:12 +02:00
if def and show_item(def) then
2019-03-21 17:30:46 +01:00
return stereotype
end
end
2019-09-09 18:27:02 +02:00
local names = {}
for name, def in pairs(reg_items) do
2020-07-18 01:39:12 +02:00
if show_item(def) and item_has_groups(def.groups, groups) then
2019-10-12 13:16:08 +02:00
if get_all then
names[#names + 1] = name
else
return name
end
2016-02-21 00:42:52 +01:00
end
end
2019-10-12 13:16:08 +02:00
return get_all and names or ""
2016-02-21 00:42:52 +01:00
end
2020-12-08 16:05:25 +01:00
local function get_stack_max(data, is_recipe, rcp)
2020-12-11 15:19:52 +01:00
data.inv = data.player:get_inventory()
local list = data.inv:get_list("main")
local size = data.inv:get_size("main")
2020-12-08 16:05:25 +01:00
local counts_inv, counts_rcp, counts = {}, {}, {}
local rcp_usg = is_recipe and "recipe" or "usage"
for _, it in pairs(rcp.items) do
counts_rcp[it] = (counts_rcp[it] or 0) + 1
end
data.export_counts[rcp_usg] = {}
data.export_counts[rcp_usg].rcp = counts_rcp
for i = 1, size do
local stack = list[i]
if not stack:is_empty() then
local item = stack:get_name()
local count = stack:get_count()
for name in pairs(counts_rcp) do
if is_group(name) then
local def = reg_items[item]
local groups = extract_groups(name)
if item_has_groups(def.groups, groups) then
counts_inv[name] = (counts_inv[name] or 0) + count
end
end
end
counts_inv[item] = (counts_inv[item] or 0) + count
end
end
data.export_counts[rcp_usg].inv = counts_inv
for name in pairs(counts_rcp) do
counts[name] = floor((counts_inv[name] or 0) / (counts_rcp[name] or 0))
end
local max_stacks = math.huge
for _, count in pairs(counts) do
if count < max_stacks then
max_stacks = count
end
end
return max_stacks
end
2020-12-11 01:37:54 +01:00
local function craft_stack(player, pname, data, craft_rcp)
local rcp_usg = craft_rcp and "recipe" or "usage"
local output = craft_rcp and data.recipes[data.rnum].output or data.usages[data.unum].output
2020-12-08 16:05:25 +01:00
output = ItemStack(output)
local stackname, stackcount = output:get_name(), output:get_count()
2020-12-11 02:14:28 +01:00
local scrbar_val = data[sprintf("scrbar_%s", craft_rcp and "rcp" or "usg")] or 1
2020-12-08 16:05:25 +01:00
for name, count in pairs(data.export_counts[rcp_usg].rcp) do
2020-12-10 15:41:59 +01:00
local items = {[name] = count}
2020-12-08 16:05:25 +01:00
if is_group(name) then
2020-12-10 15:41:59 +01:00
items = {}
2020-12-08 16:05:25 +01:00
local groups = extract_groups(name)
2020-12-10 15:41:59 +01:00
local item_groups = groups_to_items(groups, true)
local remaining = count
2020-12-08 16:05:25 +01:00
2020-12-10 15:41:59 +01:00
for _, item in ipairs(item_groups) do
2020-12-08 16:05:25 +01:00
for _name, _count in pairs(data.export_counts[rcp_usg].inv) do
2020-12-10 15:41:59 +01:00
if item == _name and remaining > 0 then
local c = min(remaining, _count)
items[item] = c
remaining = remaining - c
2020-12-08 16:05:25 +01:00
end
2020-12-10 21:32:49 +01:00
if remaining == 0 then break end
2020-12-08 16:05:25 +01:00
end
end
end
2020-12-10 15:41:59 +01:00
for k, v in pairs(items) do
2020-12-11 15:19:52 +01:00
data.inv:remove_item("main", sprintf("%s %s", k, v * scrbar_val))
2020-12-10 15:41:59 +01:00
end
2020-12-08 16:05:25 +01:00
end
local count = stackcount * scrbar_val
local stack = ItemStack(sprintf("%s %s", stackname, count))
2020-12-10 19:05:23 +01:00
local desc = get_desc(stackname)
local message
if count > 1 then
message = clr("#ff0", sprintf("%s x %s", count, desc))
else
message = clr("#ff0", sprintf("%s", desc))
end
2020-12-08 16:05:25 +01:00
2020-12-11 15:19:52 +01:00
if data.inv:room_for_item("main", stack) then
data.inv:add_item("main", stack)
2020-12-10 15:41:59 +01:00
msg(pname, sprintf("%s added in your inventory", message))
2020-12-08 16:05:25 +01:00
else
2020-12-10 01:24:11 +01:00
local dir = player:get_look_dir()
local ppos = player:get_pos()
ppos.y = ppos.y + 1.625
2020-12-08 16:05:25 +01:00
local look_at = vec_add(ppos, vec_mul(dir, 1))
core.add_item(look_at, stack)
2020-12-10 15:41:59 +01:00
msg(pname, sprintf("%s crafted", message))
2020-12-08 16:05:25 +01:00
end
end
2020-12-10 00:48:21 +01:00
local function select_item(player, data, _f)
2020-12-08 16:05:25 +01:00
local item
2020-12-10 00:48:21 +01:00
2020-12-08 16:05:25 +01:00
for field in pairs(_f) do
if find(field, ":") then
item = field
break
end
end
if not item then
return
elseif sub(item, -4) == "_inv" then
item = sub(item, 1, -5)
elseif sub(item, 1, 1) == "_" then
item = sub(item, 2)
elseif sub(item, 1, 6) == "group|" then
item = match(item, "([%w:_]+)$")
end
item = reg_aliases[item] or item
if item == data.query_item then return end
local recipes, usages = get_recipes(item, data, player)
if not recipes and not usages then return end
if data.show_usages and not usages then return end
data.query_item = item
data.recipes = recipes
data.usages = usages
data.rnum = 1
data.unum = 1
data.scrbar_rcp = 1
data.scrbar_usg = 1
data.export_rcp = nil
data.export_usg = nil
end
2019-09-10 13:41:17 +02:00
local function repairable(tool)
2019-09-09 23:57:37 +02:00
local def = reg_tools[tool]
2019-09-10 13:41:17 +02:00
return toolrepair and def and def.groups and def.groups.disable_repair ~= 1
2019-09-09 23:57:37 +02:00
end
2020-07-08 14:36:50 +02:00
local function is_fav(favs, query_item)
2020-01-05 23:46:36 +01:00
local fav, i
2020-07-08 14:36:50 +02:00
for j = 1, #favs do
if favs[j] == query_item then
2020-01-05 23:46:36 +01:00
fav = true
i = j
break
end
end
return fav, i
end
2020-08-29 18:42:04 +02:00
local function get_tooltip(item, info)
2019-09-10 16:18:27 +02:00
local tooltip
2019-09-10 13:41:17 +02:00
2019-09-10 11:28:13 +02:00
if info.groups then
2020-04-16 02:09:27 +02:00
sort(info.groups)
tooltip = group_names[concat(info.groups, ",")]
2019-02-17 15:04:01 +01:00
2020-04-16 02:09:27 +02:00
if not tooltip then
local groupstr = {}
2018-04-17 13:30:51 +02:00
2020-04-16 02:09:27 +02:00
for i = 1, #info.groups do
insert(groupstr, clr("#ff0", info.groups[i]))
2020-04-16 02:09:27 +02:00
end
groupstr = concat(groupstr, ", ")
tooltip = S("Any item belonging to the group(s): @1", groupstr)
end
2019-10-15 15:30:52 +02:00
else
2020-08-29 18:42:04 +02:00
tooltip = get_desc(item)
2019-09-10 16:18:27 +02:00
end
local function add(str)
2020-11-15 15:30:46 +01:00
return sprintf("%s\n%s", tooltip, str)
end
2017-03-21 14:26:58 +01:00
2019-09-10 11:28:13 +02:00
if info.cooktime then
2019-12-17 01:33:40 +01:00
tooltip = add(S("Cooking time: @1", clr("#ff0", info.cooktime)))
end
2017-03-21 14:26:58 +01:00
2019-09-10 11:28:13 +02:00
if info.burntime then
2019-12-17 01:33:40 +01:00
tooltip = add(S("Burning time: @1", clr("#ff0", info.burntime)))
2016-12-10 17:17:05 +01:00
end
2019-09-10 11:28:13 +02:00
if info.replace then
2020-07-27 18:22:45 +02:00
for i = 1, #info.replace.items do
local rpl = match(info.replace.items[i], "%S+")
2020-08-29 18:42:04 +02:00
local desc = clr("#ff0", get_desc(rpl))
2020-07-09 17:15:11 +02:00
2020-07-27 18:22:45 +02:00
if info.replace.type == "cooking" then
2020-07-09 17:15:11 +02:00
tooltip = add(S("Replaced by @1 on smelting", desc))
2020-07-27 18:22:45 +02:00
elseif info.replace.type == "fuel" then
2020-07-09 17:15:11 +02:00
tooltip = add(S("Replaced by @1 on burning", desc))
else
tooltip = add(S("Replaced by @1 on crafting", desc))
end
2019-09-10 12:37:39 +02:00
end
2019-09-09 18:27:02 +02:00
end
2019-09-10 13:41:17 +02:00
if info.repair then
2019-12-17 01:33:40 +01:00
tooltip = add(S("Repairable by step of @1", clr("#ff0", toolrepair .. "%")))
2019-09-10 13:41:17 +02:00
end
if info.rarity then
2020-06-29 00:19:11 +02:00
local chance = (1 / max(1, info.rarity)) * 100
2019-12-17 01:33:40 +01:00
tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%")))
end
2020-06-29 00:19:11 +02:00
if info.tools then
local several = #info.tools > 1
local names = several and "\n" or ""
if several then
for i = 1, #info.tools do
2020-11-15 15:30:46 +01:00
names = sprintf("%s\t\t- %s\n",
2020-08-29 18:42:04 +02:00
names, clr("#ff0", get_desc(info.tools[i])))
2020-06-29 00:19:11 +02:00
end
tooltip = add(S("Only drop if using one of these tools: @1",
sub(names, 1, -2)))
else
tooltip = add(S("Only drop if using this tool: @1",
2020-08-29 18:42:04 +02:00
clr("#ff0", get_desc(info.tools[1]))))
2020-06-29 00:19:11 +02:00
end
end
2020-11-15 15:30:46 +01:00
return sprintf("tooltip[%s;%s]", item, ESC(tooltip))
end
local function get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size)
2020-06-21 00:44:27 +02:00
local custom_recipe = craft_types[rcp.type]
2019-09-10 11:28:13 +02:00
2020-06-21 00:44:27 +02:00
if custom_recipe or shapeless or rcp.type == "cooking" then
2019-09-10 11:28:13 +02:00
local icon = custom_recipe and custom_recipe.icon or
2020-06-21 00:44:27 +02:00
shapeless and "shapeless" or "furnace"
2019-09-10 11:28:13 +02:00
if not custom_recipe then
2020-11-15 15:30:46 +01:00
icon = sprintf("craftguide_%s.png^[resize:16x16", icon)
2019-09-10 11:28:13 +02:00
end
2020-12-08 01:57:54 +01:00
local pos_x = right + btn_size + 0.42
2020-12-10 16:46:11 +01:00
local pos_y = data.yoffset + 0.9
2019-09-10 11:28:13 +02:00
if sub(icon, 1, 18) == "craftguide_furnace" then
2020-11-18 02:17:59 +01:00
fs(fmt("animated_image", pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180))
2020-04-06 14:39:08 +02:00
else
2020-11-18 02:17:59 +01:00
fs(fmt("image", pos_x, pos_y, 0.5, 0.5, icon))
2020-02-16 14:12:21 +01:00
end
2019-09-10 11:28:13 +02:00
local tooltip = custom_recipe and custom_recipe.description or
2020-06-21 00:44:27 +02:00
shapeless and S"Shapeless" or S"Cooking"
2019-09-10 11:28:13 +02:00
2020-11-18 02:17:59 +01:00
fs(fmt("tooltip", pos_x, pos_y, 0.5, 0.5, ESC(tooltip)))
2019-09-10 11:28:13 +02:00
end
local arrow_X = right + 0.2 + (_btn_size or ITEM_BTN_SIZE)
2020-12-08 01:57:54 +01:00
local X = arrow_X + 1.2
2020-12-10 16:46:11 +01:00
local Y = data.yoffset + 1.4
2019-09-10 11:28:13 +02:00
2020-12-08 04:46:44 +01:00
fs(fmt("image", arrow_X, Y + 0.06, 1, 1, PNG.arrow))
2019-09-10 11:28:13 +02:00
2020-06-21 00:44:27 +02:00
if rcp.type == "fuel" then
2020-12-11 01:37:54 +01:00
fs(fmt("animated_image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180))
2019-09-10 11:28:13 +02:00
else
2020-06-21 00:44:27 +02:00
local item = rcp.output
item = ItemStack(clean_name(item))
local name = item:get_name()
local count = item:get_count()
2020-12-11 01:37:54 +01:00
local bt_s = ITEM_BTN_SIZE * 1.2
2019-09-10 11:28:13 +02:00
2020-12-11 01:37:54 +01:00
fs(fmt("image", X, Y - 0.11, bt_s, bt_s, PNG.selected))
2019-10-03 22:08:51 +02:00
local _name = sprintf("_%s", name)
2020-02-09 01:46:15 +01:00
fs(fmt("item_image_button", X + 0.1, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE,
sprintf("%s %u", name,
count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)),
_name, ""))
2019-11-10 16:11:29 +01:00
2020-05-14 16:27:30 +02:00
local def = reg_items[name]
2020-07-07 23:46:01 +02:00
local unknown = not def or nil
2020-07-15 02:05:33 +02:00
local desc = def and def.description
local weird = name ~= "" and desc and weird_desc(desc) or nil
2020-07-16 00:18:22 +02:00
local burntime = fuel_cache[name] and fuel_cache[name].burntime
2020-05-14 16:27:30 +02:00
local infos = {
2020-07-15 02:05:33 +02:00
unknown = unknown,
weird = weird,
2020-07-16 00:18:22 +02:00
burntime = burntime,
2020-07-15 02:05:33 +02:00
repair = repairable(name),
rarity = rcp.rarity,
tools = rcp.tools,
}
2019-09-10 11:28:13 +02:00
if next(infos) then
2020-11-18 02:17:59 +01:00
fs(get_tooltip(_name, infos))
2019-09-10 13:41:17 +02:00
end
2019-09-10 11:28:13 +02:00
end
end
local function get_grid_fs(fs, data, rcp, is_recipe)
local width = rcp.width or 1
2020-07-03 18:40:46 +02:00
local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE
2020-07-17 15:24:37 +02:00
local cooktime, shapeless
2020-07-17 15:24:37 +02:00
if rcp.type == "cooking" then
cooktime, width = width, 1
elseif width == 0 and not rcp.custom then
shapeless = true
local n = #rcp.items
width = (n < 5 and n > 1) and 2 or min(3, max(1, n))
end
local rows = ceil(maxn(rcp.items) / width)
local large_recipe = width > 3 or rows > 3
if large_recipe then
2020-11-18 02:17:59 +01:00
fs("style_type[item_image_button;border=true]")
end
for i = 1, width * rows do
local item = rcp.items[i] or ""
2019-12-17 01:33:40 +01:00
item = clean_name(item)
2019-10-13 18:31:46 +02:00
local name = match(item, "%S*")
local X = ceil((i - 1) % width - width)
2020-12-10 16:46:11 +01:00
X = X + (X * 0.2) + data.xoffset + 3.9
local Y = ceil(i / width) - min(2, rows)
2020-12-10 16:46:11 +01:00
Y = Y + (Y * 0.15) + data.yoffset + 1.4
if large_recipe then
btn_size = (3 / width) * (3 / rows) + 0.3
_btn_size = btn_size
2020-07-08 00:56:35 +02:00
local xi = (i - 1) % width
local yi = floor((i - 1) / width)
2020-12-10 16:46:11 +01:00
X = btn_size * xi + data.xoffset + 0.3 + (xi * 0.05)
Y = btn_size * yi + data.yoffset + 0.2 + (yi * 0.05)
end
2020-06-21 00:44:27 +02:00
if X > right then
right = X
end
local groups
2019-10-13 18:31:46 +02:00
if is_group(name) then
groups = extract_groups(name)
2019-10-12 13:16:08 +02:00
item = groups_to_items(groups)
end
local label = groups and "\nG" or ""
local replace
for j = 1, #(rcp.replacements or {}) do
local replacement = rcp.replacements[j]
if replacement[1] == name then
2020-07-27 18:22:45 +02:00
replace = replace or {type = rcp.type, items = {}}
2020-07-12 22:46:28 +02:00
local added
2020-07-27 18:22:45 +02:00
for _, v in ipairs(replace.items) do
if replacement[2] == v then
added = true
break
2020-07-12 22:46:28 +02:00
end
end
2020-07-12 22:46:28 +02:00
if not added then
2020-12-11 01:37:54 +01:00
label = sprintf("%s%s\nR", label ~= "" and "\n" or "", label)
2020-07-27 18:22:45 +02:00
replace.items[#replace.items + 1] = replacement[2]
end
end
end
if not large_recipe then
2020-11-18 02:17:59 +01:00
fs(fmt("image", X, Y, btn_size, btn_size, PNG.selected))
2019-11-10 16:11:29 +01:00
end
2020-11-15 15:30:46 +01:00
local btn_name = groups and sprintf("group|%s|%s", groups[1], item) or item
2020-07-13 00:19:10 +02:00
fs(fmt("item_image_button", X, Y, btn_size, btn_size,
sprintf("%s %u", item, is_recipe and data.scrbar_rcp or data.scrbar_usg or 1),
btn_name, label))
2020-05-14 16:27:30 +02:00
local def = reg_items[name]
2020-07-07 23:46:01 +02:00
local unknown = not def or nil
unknown = not groups and unknown or nil
2020-07-15 02:05:33 +02:00
local desc = def and def.description
local weird = name ~= "" and desc and weird_desc(desc) or nil
2020-07-16 00:18:22 +02:00
local burntime = fuel_cache[name] and fuel_cache[name].burntime
2020-05-14 16:27:30 +02:00
local infos = {
2020-07-15 02:05:33 +02:00
unknown = unknown,
weird = weird,
groups = groups,
2020-07-16 00:18:22 +02:00
burntime = burntime,
2020-07-17 15:24:37 +02:00
cooktime = cooktime,
2020-07-15 02:05:33 +02:00
replace = replace,
}
if next(infos) then
2020-11-18 02:17:59 +01:00
fs(get_tooltip(btn_name, infos))
end
end
if large_recipe then
2020-11-18 02:17:59 +01:00
fs("style_type[item_image_button;border=false]")
end
get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size)
2019-10-03 22:08:51 +02:00
end
2020-12-08 16:05:25 +01:00
local function get_rcp_lbl(fs, data, panel, rn, is_recipe)
local lbl = ES("Usage @1 of @2", data.unum, rn)
2018-12-19 18:36:09 +01:00
if is_recipe then
2020-08-29 23:08:55 +02:00
lbl = ES("Recipe @1 of @2", data.rnum, rn)
end
2016-12-17 18:51:04 +01:00
2020-08-29 23:08:55 +02:00
local _lbl = translate(data.lang_code, lbl)
2020-08-29 18:42:04 +02:00
local lbl_len = #_lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings
local shift = min(0.9, abs(12 - max(12, lbl_len)) * 0.15)
2020-06-21 00:44:27 +02:00
2020-12-10 16:46:11 +01:00
fs(fmt("label", data.xoffset + 5.65 - shift, data.yoffset + 3.37, lbl))
2020-06-21 00:44:27 +02:00
if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage"
2020-11-15 15:30:46 +01:00
local prev_name = sprintf("prev_%s", btn_suffix)
local next_name = sprintf("next_%s", btn_suffix)
2020-12-10 16:46:11 +01:00
local x_arrow = data.xoffset + 4.9
local y_arrow = data.yoffset + 3
2020-06-21 00:44:27 +02:00
2020-11-18 02:17:59 +01:00
fs(fmt("arrow", x_arrow - shift, y_arrow, PNG.prev, prev_name, ""),
fmt("arrow", x_arrow + 2.3, y_arrow, PNG.next, next_name, ""))
2020-06-21 00:44:27 +02:00
end
2020-06-20 19:10:45 +02:00
2020-08-29 23:08:55 +02:00
local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum]
get_grid_fs(fs, data, rcp, is_recipe)
2020-06-21 00:44:27 +02:00
end
2020-06-20 19:10:45 +02:00
2020-12-10 16:46:11 +01:00
local function get_model_fs(fs, data, def, model_alias)
2020-12-08 16:05:25 +01:00
if model_alias then
if model_alias.drawtype == "entity" then
def = reg_entities[model_alias.name]
local init_props = def.initial_properties
def.textures = init_props and init_props.textures or def.textures
def.mesh = init_props and init_props.mesh or def.mesh
else
def = reg_items[model_alias.name]
end
end
local tiles = def.tiles or def.textures or {}
local t = {}
for _, v in ipairs(tiles) do
local _name
if v.color then
local hex = sprintf("%02x", v.color)
while #hex < 8 do
hex = "0" .. hex
end
_name = sprintf("%s^[multiply:%s", v.name,
sprintf("#%s%s", sub(hex, 3), sub(hex, 1, 2)))
elseif v.animation then
_name = sprintf("%s^[verticalframe:%u:0", v.name, v.animation.aspect_h)
end
t[#t + 1] = _name or v.name or v
end
while #t < 6 do
t[#t + 1] = t[#t]
end
2020-12-10 16:46:11 +01:00
fs(fmt("model",
2020-12-15 19:49:13 +01:00
data.xoffset + 6.6, data.yoffset + 0.05, 1.3, 1.3, "",
def.mesh, concat(t, ","), model_alias.frames or ""))
2020-12-08 16:05:25 +01:00
end
2020-12-10 16:46:11 +01:00
local function get_title_fs(fs, data)
local fav = is_fav(data.favs, data.query_item)
local nfavs = #data.favs
local star_x, star_y, star_size = data.xoffset + 0.4, data.yoffset + 0.5, 0.4
if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then
local fav_marked = sprintf("craftguide_fav%s.png", fav and "_off" or "")
fs(sprintf("style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
2020-12-11 01:37:54 +01:00
sprintf("craftguide_fav%s.png", fav and "" or "_off"), fav_marked, fav_marked),
fmt("image_button", star_x, star_y, star_size, star_size, "", "fav", ""),
sprintf("tooltip[fav;%s]", fav and ES"Unmark this item" or ES"Mark this item"))
else
2020-12-10 16:27:58 +01:00
fs(sprintf("style[nofav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
"craftguide_fav_off.png", PNG.nothing, PNG.nothing),
2020-12-10 16:27:58 +01:00
fmt("image_button", star_x, star_y, star_size, star_size, "", "nofav", ""),
2020-12-11 01:37:54 +01:00
sprintf("tooltip[nofav;%s]", ES"Cannot mark this item. Bookmark limit reached."))
end
2020-11-18 02:17:59 +01:00
fs("style_type[label;font=bold;font_size=+6]",
2020-12-10 16:46:11 +01:00
fmt("label", data.xoffset + 1.05, data.yoffset + 0.47,
snip(ESC(get_desc(data.query_item, data.lang_code)), 32)),
2020-11-18 02:17:59 +01:00
"style_type[label;font=mono;font_size=+0]",
2020-12-10 16:46:11 +01:00
fmt("label", data.xoffset + 1.05, data.yoffset + 0.97,
clr("#7bf", snip(data.query_item, 34))),
2020-11-18 02:17:59 +01:00
"style_type[label;font=normal]")
2018-11-18 20:06:45 +01:00
2020-12-10 16:46:11 +01:00
local def = reg_items[data.query_item]
local model_alias = craftguide.model_alias[data.query_item]
2020-11-24 17:12:25 +01:00
if def.drawtype == "mesh" or model_alias then
2020-12-10 16:46:11 +01:00
get_model_fs(fs, data, def, model_alias)
2020-12-08 16:05:25 +01:00
else
2020-12-10 16:46:11 +01:00
fs(fmt("item_image",
data.xoffset + 6.8, data.yoffset + 0.17, 1.1, 1.1, data.query_item))
2020-12-08 16:05:25 +01:00
end
end
2020-11-16 02:02:02 +01:00
2020-12-08 16:05:25 +01:00
local function get_export_fs(fs, data, panel, is_recipe, is_usage, max_stacks_rcp, max_stacks_usg)
local name = is_recipe and "rcp" or "usg"
local show_export = (is_recipe and data.export_rcp) or (is_usage and data.export_usg)
fs(sprintf("style[export_%s;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
name, sprintf("craftguide_export%s.png", show_export and "" or "_off"),
"craftguide_export.png", "craftguide_export.png"),
fmt("image_button",
2020-12-10 16:46:11 +01:00
data.xoffset + 7.35, data.yoffset + 0.2, 0.45, 0.45, "",
sprintf("export_%s", name), ""),
2020-12-14 01:42:18 +01:00
sprintf("tooltip[export_%s;%s]", name, ES"Quick crafting"))
2020-12-08 16:05:25 +01:00
if not show_export then return end
local item = (is_recipe and panel.rcp[data.rnum].output) or
(is_usage and panel.rcp[data.unum].output)
item = clean_name(item)
local _name = match(item, "%S*")
local stack = ItemStack(_name)
local stack_max = stack:get_stack_max()
2020-12-10 00:48:21 +01:00
local craft_max = is_recipe and max_stacks_rcp or max_stacks_usg
2020-12-08 16:05:25 +01:00
local stack_fs = (is_recipe and data.scrbar_rcp) or (is_usage and data.scrbar_usg) or 1
2020-12-10 00:48:21 +01:00
if stack_fs > craft_max then
stack_fs = craft_max
2020-12-10 18:59:52 +01:00
if is_recipe then
data.scrbar_rcp = craft_max
elseif is_usage then
data.scrbar_usg = craft_max
end
2020-12-10 00:48:21 +01:00
end
2020-12-08 16:05:25 +01:00
fs(sprintf("style[scrbar_%s;noclip=true]", name),
2020-12-10 00:48:21 +01:00
sprintf("scrollbaroptions[min=1;max=%u;smallstep=1]", min(craft_max, stack_max)),
2020-12-10 16:46:11 +01:00
fmt("scrollbar",
data.xoffset + 8.1, data.yoffset, 3, 0.35, sprintf("scrbar_%s", name), stack_fs),
fmt("button", data.xoffset + 8.1, data.yoffset + 0.4, 3, 0.7, sprintf("craft_%s", name),
2020-12-10 19:12:50 +01:00
sprintf("%s", sprintf(stack_fs > 1 and ES"Craft %u stacks" or ES"Craft %u stack",
stack_fs))))
2020-12-08 16:05:25 +01:00
end
2020-11-16 02:02:02 +01:00
2020-12-08 16:05:25 +01:00
local function get_rcp_extra(fs, data, panel, is_recipe, is_usage)
local rn = panel.rcp and #panel.rcp
2020-11-16 02:02:02 +01:00
2020-12-08 16:05:25 +01:00
if rn then
local rcp_normal = is_recipe and panel.rcp[data.rnum].type == "normal"
local usg_normal = is_usage and panel.rcp[data.unum].type == "normal"
local max_stacks_rcp, max_stacks_usg = 0, 0
2020-11-18 01:58:24 +01:00
2020-12-08 16:05:25 +01:00
if rcp_normal then
max_stacks_rcp = get_stack_max(data, is_recipe, panel.rcp[data.rnum])
end
2020-11-16 02:02:02 +01:00
2020-12-08 16:05:25 +01:00
if usg_normal then
max_stacks_usg = get_stack_max(data, is_recipe, panel.rcp[data.unum])
2020-11-16 02:02:02 +01:00
end
2020-12-11 02:14:28 +01:00
if is_recipe and max_stacks_rcp == 0 then
data.export_rcp = nil
data.scrbar_rcp = 1
elseif is_usage and max_stacks_usg == 0 then
data.export_usg = nil
data.scrbar_usg = 1
end
2020-12-08 16:05:25 +01:00
if max_stacks_rcp > 0 or max_stacks_usg > 0 then
get_export_fs(fs, data, panel, is_recipe, is_usage, max_stacks_rcp,
max_stacks_usg)
2020-11-16 02:02:02 +01:00
end
2020-12-08 16:05:25 +01:00
get_rcp_lbl(fs, data, panel, rn, is_recipe)
2020-11-16 02:02:02 +01:00
else
2020-12-08 16:05:25 +01:00
local lbl = is_recipe and ES"No recipes" or ES"No usages"
fs(fmt("button",
2020-12-10 16:46:11 +01:00
data.xoffset + 0.1, data.yoffset + (panel.height / 2) - 0.5,
7.8, 1, "no_rcp", lbl))
2020-06-21 00:44:27 +02:00
end
end
2020-12-08 16:05:25 +01:00
local function get_favs(fs, data)
2020-12-10 16:46:11 +01:00
fs(fmt("label", data.xoffset + 0.4, data.yoffset + 0.4, ES"Bookmarks"))
2020-12-08 16:05:25 +01:00
for i = 1, #data.favs do
local item = data.favs[i]
2020-12-10 16:46:11 +01:00
local X = data.xoffset - 0.7 + (i * 1.2)
local Y = data.yoffset + 0.8
2020-12-08 16:05:25 +01:00
if data.query_item == item then
fs(fmt("image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.selected))
end
fs(fmt("item_image_button", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, ""))
end
end
local function get_panels(fs, data)
local _title = {name = "title", height = 1.4}
local _favs = {name = "favs", height = 2.2}
local _recipes = {name = "recipes", rcp = data.recipes, height = 3.9}
local _usages = {name = "usages", rcp = data.usages, height = 3.9}
2020-07-27 18:03:50 +02:00
local panels = {_title, _recipes, _usages, _favs}
2020-06-21 00:44:27 +02:00
for idx = 1, #panels do
2020-12-08 16:05:25 +01:00
local panel = panels[idx]
2020-12-10 16:46:11 +01:00
data.yoffset = 0
2020-06-21 00:44:27 +02:00
if idx > 1 then
for _idx = idx - 1, 1, -1 do
2020-12-10 16:46:11 +01:00
data.yoffset = data.yoffset + panels[_idx].height + 0.1
2020-01-05 23:46:36 +01:00
end
2019-10-03 22:08:51 +02:00
end
2019-09-05 14:24:11 +02:00
2020-12-11 01:37:54 +01:00
fs(fmt("bg9", data.xoffset + 0.1, data.yoffset, 7.9, panel.height, PNG.bg_full, 10))
2019-09-05 14:24:11 +02:00
2020-12-08 16:05:25 +01:00
local is_recipe, is_usage = panel.name == "recipes", panel.name == "usages"
2020-12-08 16:05:25 +01:00
if is_recipe or is_usage then
get_rcp_extra(fs, data, panel, is_recipe, is_usage)
2020-06-21 00:44:27 +02:00
elseif panel.name == "title" then
2020-12-10 16:46:11 +01:00
get_title_fs(fs, data)
2020-06-21 00:44:27 +02:00
elseif panel.name == "favs" then
2020-12-08 16:05:25 +01:00
get_favs(fs, data)
2020-01-05 23:46:36 +01:00
end
end
2016-12-10 23:15:02 +01:00
end
2020-02-09 02:08:24 +01:00
local function make_fs(data)
2020-11-18 02:17:59 +01:00
local fs = setmetatable({}, {
__call = function(t, ...)
t[#t + 1] = concat({...})
end
})
2019-02-05 14:45:57 +01:00
2020-12-10 16:46:11 +01:00
data.xoffset = ROWS + 1.04
2020-11-18 02:17:59 +01:00
fs(sprintf([[
formspec_version[%u]
2020-01-05 23:46:36 +01:00
size[%f,%f]
no_prepend[]
bgcolor[#0000]
2020-01-07 00:37:19 +01:00
]],
2020-12-10 16:46:11 +01:00
MIN_FORMSPEC_VERSION, data.xoffset + (data.query_item and 8 or 0), LINES + 1.7), styles)
2019-09-29 16:58:33 +02:00
2020-12-10 16:46:11 +01:00
fs(fmt("bg9", 0, 0, data.xoffset, LINES + 1.7, PNG.bg_full, 10))
2018-11-11 22:24:48 +01:00
2020-11-18 02:17:59 +01:00
fs(sprintf([[
box[0.2,0.2;3.5,0.6;#bababa25]
2020-12-08 00:49:27 +01:00
set_focus[filter]
field[0.2,0.2;3.5,0.6;filter;;%s]
2019-09-06 17:23:27 +02:00
field_close_on_enter[filter;false]
2020-11-18 02:17:59 +01:00
]], ESC(data.filter)),
fmt("image_button", 3.75, 0.15, 0.7, 0.7, "", "search", ""),
fmt("image_button", 4.43, 0.15, 0.7, 0.7, "", "clear", ""))
2020-06-20 00:00:14 +02:00
2020-12-10 16:46:11 +01:00
fs(fmt("image_button", data.xoffset - 3.2, 0.15, 0.7, 0.7, "", "prev_page", ""),
fmt("image_button", data.xoffset - 0.7, 0.15, 0.7, 0.7, "", "next_page", ""))
2019-12-17 01:33:40 +01:00
data.pagemax = max(1, ceil(#data.items / IPP))
2020-11-18 02:17:59 +01:00
fs(fmt("button",
2020-12-10 16:46:11 +01:00
data.xoffset - 2.53, 0.15, 1.88, 0.7, "pagenum",
2020-11-18 02:17:59 +01:00
sprintf("%s / %u", clr("#ff0", data.pagenum), data.pagemax)))
2020-06-20 00:00:14 +02:00
2019-01-22 01:44:36 +01:00
if #data.items == 0 then
2020-07-14 20:03:39 +02:00
local lbl = ES"No item to show"
if next(recipe_filters) and #init_items > 0 and data.filter == "" then
2020-07-14 20:03:39 +02:00
lbl = ES"Collect items to reveal more recipes"
end
2020-12-10 16:46:11 +01:00
fs(fmt("button", 0, 3, data.xoffset, 1, "no_item", lbl))
end
local first_item = (data.pagenum - 1) * IPP
for i = first_item, first_item + IPP - 1 do
local item = data.items[i + 1]
2019-09-05 14:57:58 +02:00
if not item then break end
2018-12-30 21:17:21 +01:00
local X = i % ROWS
2020-12-08 01:36:27 +01:00
X = X + (X * 0.08) + 0.2
2020-12-08 01:20:58 +01:00
local Y = (i % IPP - X) / ROWS + 1
2020-12-08 01:36:27 +01:00
Y = Y + (Y * 0.06)
if data.query_item == item then
2020-11-18 02:17:59 +01:00
fs(fmt("image", X, Y, 1, 1, PNG.selected))
2019-10-03 22:08:51 +02:00
end
2019-11-10 16:11:29 +01:00
2020-11-18 02:17:59 +01:00
fs(fmt("item_image_button", X, Y, 1, 1, item, sprintf("%s_inv", item), ""))
2016-02-21 00:42:52 +01:00
end
2019-10-03 22:08:51 +02:00
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then
2020-12-08 16:05:25 +01:00
get_panels(fs, data)
2016-02-21 00:42:52 +01:00
end
2016-03-17 01:10:34 +01:00
2019-01-22 01:44:36 +01:00
return concat(fs)
2016-02-21 00:42:52 +01:00
end
local show_fs = function(player, name)
local data = pdata[name]
show_formspec(name, "craftguide", make_fs(data))
2018-12-17 01:02:19 +01:00
end
2019-10-05 00:09:29 +02:00
craftguide.register_craft_type("digging", {
2019-12-17 02:09:40 +01:00
description = ES"Digging",
2020-04-16 18:39:39 +02:00
icon = "craftguide_steelpick.png",
2019-10-05 00:09:29 +02:00
})
craftguide.register_craft_type("digging_chance", {
2020-07-03 18:40:46 +02:00
description = ES"Digging (by chance)",
2020-04-16 18:39:39 +02:00
icon = "craftguide_mesepick.png",
2019-10-05 00:09:29 +02:00
})
local function search(data)
local filter = data.filter
2019-02-05 14:45:57 +01:00
2019-01-08 20:28:53 +01:00
if searches[filter] then
data.items = searches[filter]
return
end
2019-10-25 01:42:04 +02:00
local opt = "^(.-)%+([%w_]+)=([%w_,]+)"
2019-08-29 15:20:19 +02:00
local search_filter = next(search_filters) and match(filter, opt)
2019-03-04 00:45:30 +01:00
local filters = {}
if search_filter then
2019-08-29 15:20:19 +02:00
for filter_name, values in gmatch(filter, sub(opt, 6)) do
2019-03-04 00:45:30 +01:00
if search_filters[filter_name] then
values = split(values, ",")
filters[filter_name] = values
end
end
end
2016-12-17 17:39:22 +01:00
2020-02-16 14:48:43 +01:00
local filtered_list, c = {}, 0
2019-08-29 15:20:19 +02:00
for i = 1, #data.items_raw do
local item = data.items_raw[i]
local def = reg_items[item]
local desc = lower(translate(data.lang_code, def and def.description)) or ""
2020-11-15 15:30:46 +01:00
local search_in = sprintf("%s %s", item, desc)
2019-03-03 16:20:19 +01:00
local to_add
2019-03-04 00:45:30 +01:00
if search_filter then
for filter_name, values in pairs(filters) do
2020-02-16 14:48:43 +01:00
if values then
local func = search_filters[filter_name]
to_add = func(item, values) and (search_filter == "" or
find(search_in, search_filter, 1, true))
end
end
else
2020-02-16 14:48:43 +01:00
to_add = find(search_in, filter, 1, true)
end
if to_add then
c = c + 1
filtered_list[c] = item
2016-12-17 17:39:22 +01:00
end
end
2016-12-24 21:17:32 +01:00
if not next(recipe_filters) then
-- Cache the results only if searched 2 times
2019-01-08 20:28:53 +01:00
if searches[filter] == nil then
searches[filter] = false
else
2019-01-08 20:28:53 +01:00
searches[filter] = filtered_list
end
2016-12-24 21:17:32 +01:00
end
2016-12-17 21:16:10 +01:00
data.items = filtered_list
2016-02-21 00:42:52 +01:00
end
2019-10-22 12:53:47 +02:00
craftguide.add_search_filter("groups", function(item, groups)
local def = reg_items[item]
local has_groups = true
for i = 1, #groups do
local group = groups[i]
if not def.groups[group] then
has_groups = nil
break
end
end
return has_groups
end)
2019-09-29 15:28:53 +02:00
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
2020-07-16 02:36:04 +02:00
return the fuel, replacements and toolrepair recipes, we have to
override `core.register_craft` and do some reverse engineering.
See engine's issues #4901, #5745 and #8920. ]]
2019-09-29 15:28:53 +02:00
local old_register_craft = core.register_craft
2020-07-19 23:51:51 +02:00
local rcp_num = {}
2019-09-29 15:28:53 +02:00
2019-09-11 17:10:46 +02:00
core.register_craft = function(def)
old_register_craft(def)
2019-09-11 17:10:46 +02:00
if def.type == "toolrepair" then
toolrepair = def.additional_wear * -100
2019-09-09 23:57:37 +02:00
end
local output = def.output or (true_str(def.recipe) and def.recipe) or nil
2019-09-25 16:12:33 +02:00
if not output then return end
output = {match(output, "%S+")}
2019-09-11 17:10:46 +02:00
2019-09-09 18:27:02 +02:00
local groups
2019-09-10 11:28:13 +02:00
if is_group(output[1]) then
groups = extract_groups(output[1])
2019-09-09 18:27:02 +02:00
output = groups_to_items(groups, true)
end
for i = 1, #output do
2020-07-27 03:47:33 +02:00
local item = output[i]
rcp_num[item] = (rcp_num[item] or 0) + 1
2019-09-09 18:27:02 +02:00
2020-07-19 23:51:51 +02:00
if def.replacements then
if def.type == "fuel" then
2020-07-27 03:47:33 +02:00
replacements.fuel[item] = def.replacements
2020-07-19 23:51:51 +02:00
else
2020-07-27 03:47:33 +02:00
replacements[item] = replacements[item] or {}
replacements[item][rcp_num[item]] = def.replacements
2020-07-19 23:51:51 +02:00
end
end
end
2019-09-09 18:27:02 +02:00
end
local old_clear_craft = core.clear_craft
core.clear_craft = function(def)
old_clear_craft(def)
if true_str(def) then
2020-07-17 01:42:39 +02:00
return -- TODO
elseif is_table(def) then
return -- TODO
end
end
local function resolve_aliases(hash)
2019-10-01 13:05:53 +02:00
for oldname, newname in pairs(reg_aliases) do
2019-10-25 13:38:26 +02:00
cache_recipes(oldname)
local recipes = recipes_cache[oldname]
2019-10-01 13:05:53 +02:00
if recipes then
if not recipes_cache[newname] then
recipes_cache[newname] = {}
2019-09-30 18:04:50 +02:00
end
2019-11-10 16:11:29 +01:00
local similar
2019-10-01 13:05:53 +02:00
for i = 1, #recipes_cache[oldname] do
local rcp_old = recipes_cache[oldname][i]
for j = 1, #recipes_cache[newname] do
local rcp_new = copy(recipes_cache[newname][j])
rcp_new.output = oldname
2019-10-01 13:05:53 +02:00
2019-10-03 17:02:19 +02:00
if table_eq(rcp_old, rcp_new) then
2019-11-10 16:11:29 +01:00
similar = true
2019-10-01 13:05:53 +02:00
break
end
end
2019-11-10 16:11:29 +01:00
if not similar then
2019-10-01 13:05:53 +02:00
insert(recipes_cache[newname], rcp_old)
end
2019-09-30 18:04:50 +02:00
end
end
2019-10-01 13:05:53 +02:00
2019-10-13 18:31:46 +02:00
if newname ~= "" and recipes_cache[oldname] and not hash[newname] then
2019-10-05 00:09:29 +02:00
init_items[#init_items + 1] = newname
end
end
end
2019-10-25 13:38:26 +02:00
local function get_init_items()
local _select, _preselect = {}, {}
2019-10-25 13:38:26 +02:00
for name, def in pairs(reg_items) do
if name ~= "" and show_item(def) then
2020-07-07 20:22:49 +02:00
cache_drops(name, def.drop)
2020-12-08 01:11:40 +01:00
cache_fuel(name)
cache_recipes(name)
2020-07-17 15:24:37 +02:00
_preselect[name] = true
2019-10-01 13:05:53 +02:00
end
end
2019-09-30 18:04:50 +02:00
for name in pairs(_preselect) do
cache_usages(name)
end
for name in pairs(_preselect) do
if recipes_cache[name] or usages_cache[name] then
init_items[#init_items + 1] = name
_select[name] = true
end
end
resolve_aliases(_select)
sort(init_items)
2019-11-01 02:04:35 +01:00
if http and true_str(craftguide.export_url) then
local post_data = {
recipes = recipes_cache,
usages = usages_cache,
}
2020-01-07 00:23:15 +01:00
http.fetch_async{
2019-11-01 02:04:35 +01:00
url = craftguide.export_url,
post_data = write_json(post_data),
2020-01-07 00:23:15 +01:00
}
end
end
2019-09-29 18:34:17 +02:00
local function init_data(name)
2020-12-06 02:28:00 +01:00
local info = get_player_info(name)
2019-09-29 18:34:17 +02:00
pdata[name] = {
2020-12-08 16:05:25 +01:00
filter = "",
pagenum = 1,
items = init_items,
items_raw = init_items,
favs = {},
export_counts = {},
lang_code = get_lang_code(info),
fs_version = get_formspec_version(info),
player = get_player_by_name(name),
2019-09-29 18:34:17 +02:00
}
end
local function reset_data(data)
data.filter = ""
data.pagenum = 1
data.rnum = 1
2019-10-03 22:08:51 +02:00
data.unum = 1
2020-12-08 16:05:25 +01:00
data.scrbar_rcp = 1
data.scrbar_usg = 1
2019-09-29 18:34:17 +02:00
data.query_item = nil
data.recipes = nil
2019-10-03 22:08:51 +02:00
data.usages = nil
data.show_usages = nil
2020-12-08 16:05:25 +01:00
data.export_rcp = nil
data.export_usg = nil
2019-09-29 18:34:17 +02:00
data.items = data.items_raw
end
on_mods_loaded(get_init_items)
2019-09-09 18:27:02 +02:00
on_joinplayer(function(player)
local name = player:get_player_name()
init_data(name)
2020-12-06 02:28:00 +01:00
local data = pdata[name]
2020-05-14 14:39:07 +02:00
2020-12-06 02:28:00 +01:00
if data.fs_version < MIN_FORMSPEC_VERSION then
2020-05-14 14:39:07 +02:00
outdated(name)
end
2019-09-09 18:27:02 +02:00
end)
2019-09-10 16:18:27 +02:00
local function fields(player, _f)
2020-07-23 02:00:57 +02:00
if _f.quit then return end
local name = player:get_player_name()
2019-06-23 01:42:12 +02:00
local data = pdata[name]
2020-12-11 01:37:54 +01:00
local sb_rcp, sb_usg = _f.scrbar_rcp, _f.scrbar_usg
2020-12-08 16:05:25 +01:00
2019-09-05 14:57:58 +02:00
if _f.clear then
2019-01-08 20:28:53 +01:00
reset_data(data)
2019-10-04 23:04:35 +02:00
elseif _f.prev_recipe or _f.next_recipe then
local num = data.rnum + (_f.prev_recipe and -1 or 1)
data.rnum = data.recipes[num] and num or (_f.prev_recipe and #data.recipes or 1)
2020-12-08 16:05:25 +01:00
data.export_rcp = nil
data.scrbar_rcp = 1
2017-03-21 14:26:58 +01:00
2019-10-04 23:04:35 +02:00
elseif _f.prev_usage or _f.next_usage then
local num = data.unum + (_f.prev_usage and -1 or 1)
data.unum = data.usages[num] and num or (_f.prev_usage and #data.usages or 1)
2020-12-08 16:05:25 +01:00
data.export_usg = nil
data.scrbar_usg = 1
2019-10-03 22:08:51 +02:00
2020-02-03 01:01:00 +01:00
elseif _f.key_enter_field == "filter" or _f.search then
if _f.filter == "" then
reset_data(data)
return true, show_fs(player, name)
end
2019-09-10 16:18:27 +02:00
local str = lower(_f.filter)
if data.filter == str then return end
2019-01-11 01:54:17 +01:00
2019-09-10 16:18:27 +02:00
data.filter = str
data.pagenum = 1
2020-12-08 16:05:25 +01:00
search(data)
2019-09-05 14:57:58 +02:00
2020-11-15 03:08:41 +01:00
elseif _f.prev_page or _f.next_page then
2019-09-05 14:57:58 +02:00
if data.pagemax == 1 then return end
2019-10-04 23:04:35 +02:00
data.pagenum = data.pagenum - (_f.prev_page and 1 or -1)
2019-02-06 01:51:09 +01:00
2016-12-17 23:03:46 +01:00
if data.pagenum > data.pagemax then
data.pagenum = 1
elseif data.pagenum == 0 then
2016-12-17 23:03:46 +01:00
data.pagenum = data.pagemax
end
2020-01-05 23:46:36 +01:00
elseif _f.fav then
2020-07-08 14:36:50 +02:00
local fav, i = is_fav(data.favs, data.query_item)
2020-01-05 23:46:36 +01:00
local total = #data.favs
2020-06-20 21:12:53 +02:00
if total < MAX_FAVS and not fav then
2020-01-05 23:46:36 +01:00
data.favs[total + 1] = data.query_item
elseif fav then
remove(data.favs, i)
end
2019-01-15 14:38:15 +01:00
2020-12-08 16:05:25 +01:00
elseif _f.export_rcp or _f.export_usg then
if _f.export_rcp then
data.export_rcp = not data.export_rcp
2020-12-19 21:08:58 +01:00
if not data.export_rcp then
data.scrbar_rcp = 1
end
2020-12-08 16:05:25 +01:00
else
data.export_usg = not data.export_usg
2020-12-19 21:08:58 +01:00
if not data.export_usg then
data.scrbar_usg = 1
end
2019-01-22 01:44:36 +01:00
end
2019-01-15 14:38:15 +01:00
2020-12-11 01:37:54 +01:00
elseif (sb_rcp and sub(sb_rcp, 1, 3) == "CHG") or (sb_usg and sub(sb_usg, 1, 3) == "CHG") then
data.scrbar_rcp = sb_rcp and tonum(match(sb_rcp, "%d+"))
data.scrbar_usg = sb_usg and tonum(match(sb_usg, "%d+"))
2019-02-13 14:04:13 +01:00
2020-12-08 16:05:25 +01:00
elseif _f.craft_rcp or _f.craft_usg then
2020-12-11 01:37:54 +01:00
craft_stack(player, name, data, _f.craft_rcp)
2020-12-08 16:05:25 +01:00
else
2020-12-10 00:48:21 +01:00
select_item(player, data, _f)
2016-02-21 00:42:52 +01:00
end
2020-02-03 01:01:00 +01:00
return true, show_fs(player, name)
2018-11-11 22:24:48 +01:00
end
2016-02-21 00:42:52 +01:00
on_receive_fields(function(player, formname, _f)
if formname == "craftguide" then
fields(player, _f)
end
end)
2019-01-02 18:57:35 +01:00
local function on_use(user)
local name = user:get_player_name()
local data = pdata[name]
2018-11-11 22:24:48 +01:00
if data.fs_version < MIN_FORMSPEC_VERSION then
return outdated(name)
end
if next(recipe_filters) then
data.items_raw = get_filtered_items(user)
search(data)
end
2017-01-30 05:24:03 +01:00
show_formspec(name, "craftguide", make_fs(data))
end
core.register_craftitem("craftguide:book", {
description = S"Crafting Guide",
inventory_image = PNG.book,
wield_image = PNG.book,
stack_max = 1,
groups = {book = 1},
on_use = function(itemstack, user)
on_use(user)
2016-02-21 00:42:52 +01:00
end
})
2016-02-21 00:42:52 +01:00
core.register_node("craftguide:sign", {
description = S"Crafting Guide Sign",
drawtype = "nodebox",
tiles = {PNG.sign},
inventory_image = PNG.sign,
wield_image = PNG.sign,
paramtype = "light",
paramtype2 = "wallmounted",
sunlight_propagates = true,
groups = {
choppy = 1,
attached_node = 1,
oddly_breakable_by_hand = 1,
flammable = 3,
},
node_box = {
type = "wallmounted",
wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
wall_bottom = {-0.5, -0.5, -0.5, 0.5, -0.4375, 0.5},
wall_side = {-0.5, -0.5, -0.5, -0.4375, 0.5, 0.5}
},
on_construct = function(pos)
local meta = core.get_meta(pos)
meta:set_string("infotext", "Crafting Guide Sign")
end,
2016-12-13 17:30:43 +01:00
on_rightclick = function(pos, node, user, itemstack)
on_use(user)
end
})
2017-01-30 05:24:03 +01:00
core.register_craft{
output = "craftguide:book",
type = "shapeless",
recipe = {"default:book"}
}
2018-11-11 22:24:48 +01:00
core.register_craft{
type = "fuel",
recipe = "craftguide:book",
burntime = 3
}
2018-11-11 22:24:48 +01:00
core.register_craft{
output = "craftguide:sign",
type = "shapeless",
recipe = {"default:sign_wall_wood"}
}
2017-01-30 05:24:03 +01:00
core.register_craft{
type = "fuel",
recipe = "craftguide:sign",
burntime = 10
}
2017-03-19 21:12:49 +01:00
if progressive_mode then
local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items
2019-09-09 21:55:51 +02:00
if is_group(item) then
local groups = extract_groups(item)
for i = 1, inv_items_size do
2019-09-09 21:55:51 +02:00
local def = reg_items[inv_items[i]]
if def then
2020-12-08 16:05:25 +01:00
if item_has_groups(def.groups, groups) then
return true
end
end
end
else
for i = 1, inv_items_size do
if inv_items[i] == item then
return true
end
end
end
end
2020-12-08 16:05:25 +01:00
local function recipe_in_inv(rcp, inv_items)
for _, item in pairs(rcp.items) do
2019-09-05 14:57:58 +02:00
if not item_in_inv(item, inv_items) then return end
end
return true
end
local function progressive_filter(recipes, player)
if not recipes then
return {}
end
local name = player:get_player_name()
2019-06-23 01:42:12 +02:00
local data = pdata[name]
if #data.inv_items == 0 then
return {}
end
2019-02-07 16:08:50 +01:00
local filtered, c = {}, 0
for i = 1, #recipes do
local recipe = recipes[i]
if recipe_in_inv(recipe, data.inv_items) then
2019-02-07 16:08:50 +01:00
c = c + 1
filtered[c] = recipe
end
end
return filtered
end
2020-04-07 18:03:53 +02:00
local item_lists = {"main", "craft", "craftpreview"}
2019-08-30 18:50:55 +02:00
2019-03-13 16:56:35 +01:00
local function get_inv_items(player)
local inv = player:get_inventory()
local stacks = {}
for i = 1, #item_lists do
local list = inv:get_list(item_lists[i])
table_merge(stacks, list)
end
local inv_items, c = {}, 0
for i = 1, #stacks do
local stack = stacks[i]
if not stack:is_empty() then
local name = stack:get_name()
if reg_items[name] then
c = c + 1
inv_items[c] = name
end
end
end
return inv_items
end
2020-05-14 14:39:07 +02:00
local function init_hud(player, data)
data.hud = {
bg = player:hud_add{
hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
2020-07-10 01:40:41 +02:00
z_index = 0xDEAD,
2020-05-14 14:39:07 +02:00
},
book = player:hud_add{
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
2020-07-10 01:40:41 +02:00
z_index = 0xDEAD,
2020-05-14 14:39:07 +02:00
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
2020-07-10 01:40:41 +02:00
z_index = 0xDEAD,
2020-05-14 14:39:07 +02:00
},
}
end
local function show_hud_success(player, data)
-- It'd better to have an engine function `hud_move` to only need
-- 2 calls for the notification's back and forth.
2019-09-09 19:06:35 +02:00
local hud_info_bg = player:hud_get(data.hud.bg)
local dt = 0.016
if hud_info_bg.position.y <= 0.9 then
data.show_hud = false
data.hud_timer = (data.hud_timer or 0) + dt
end
if data.show_hud then
for _, def in pairs(data.hud) do
local hud_info = player:hud_get(def)
player:hud_change(def, "position", {
x = hud_info.position.x,
y = hud_info.position.y - (dt / 5)
})
end
player:hud_change(data.hud.text, "text",
S("@1 new recipe(s) discovered!", data.discovered))
elseif data.show_hud == false then
if data.hud_timer >= HUD_TIMER_MAX then
for _, def in pairs(data.hud) do
local hud_info = player:hud_get(def)
player:hud_change(def, "position", {
x = hud_info.position.x,
y = hud_info.position.y + (dt / 5)
})
end
if hud_info_bg.position.y >= 1 then
data.show_hud = nil
data.hud_timer = nil
end
end
end
end
2019-09-09 19:06:35 +02:00
-- Workaround. Need an engine call to detect when the contents of
-- the player inventory changed, instead.
2019-02-16 01:52:01 +01:00
local function poll_new_items()
2020-05-14 02:17:24 +02:00
local players = get_players()
for i = 1, #players do
local player = players[i]
2020-07-27 18:03:50 +02:00
local name = player:get_player_name()
local data = pdata[name]
2019-06-23 01:42:12 +02:00
local inv_items = get_inv_items(player)
2019-10-03 17:02:19 +02:00
local diff = array_diff(inv_items, data.inv_items)
if #diff > 0 then
data.inv_items = table_merge(diff, data.inv_items)
local oldknown = data.known_recipes or 0
get_filtered_items(player, data)
data.discovered = data.known_recipes - oldknown
if data.show_hud == nil and data.discovered > 0 then
data.show_hud = true
end
end
end
2019-02-16 01:52:01 +01:00
2019-03-12 14:19:54 +01:00
after(POLL_FREQ, poll_new_items)
2019-02-16 01:52:01 +01:00
end
poll_new_items()
globalstep(function()
2020-05-14 02:17:24 +02:00
local players = get_players()
for i = 1, #players do
local player = players[i]
2020-07-27 18:03:50 +02:00
local name = player:get_player_name()
local data = pdata[name]
if data.show_hud ~= nil and singleplayer then
show_hud_success(player, data)
end
end
end)
craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
2019-08-30 18:50:55 +02:00
on_joinplayer(function(player)
local name = player:get_player_name()
2019-06-23 01:42:12 +02:00
local data = pdata[name]
local meta = player:get_meta()
data.inv_items = dslz(meta:get_string "inv_items") or {}
data.known_recipes = dslz(meta:get_string "known_recipes") or 0
2020-05-14 14:39:07 +02:00
if singleplayer then
init_hud(player, data)
end
end)
2020-03-31 00:56:11 +02:00
local to_save = {"inv_items", "known_recipes"}
2019-08-30 17:39:03 +02:00
local function save_meta(player)
local meta = player:get_meta()
local name = player:get_player_name()
2019-06-23 01:42:12 +02:00
local data = pdata[name]
2019-08-30 17:39:03 +02:00
for i = 1, #to_save do
local meta_name = to_save[i]
meta:set_string(meta_name, slz(data[meta_name]))
2019-08-30 17:39:03 +02:00
end
end
2020-05-14 02:17:24 +02:00
on_leaveplayer(save_meta)
2019-08-30 18:50:55 +02:00
on_shutdown(function()
2020-05-14 02:17:24 +02:00
local players = get_players()
for i = 1, #players do
local player = players[i]
save_meta(player)
end
end)
end
2019-02-06 01:51:09 +01:00
2019-08-30 18:50:55 +02:00
on_leaveplayer(function(player)
local name = player:get_player_name()
2019-06-23 01:42:12 +02:00
pdata[name] = nil
end)
2019-10-03 22:08:51 +02:00
function craftguide.show(name, item, show_usages)
2020-07-27 18:03:50 +02:00
if not true_str(name) then
2020-04-07 18:03:53 +02:00
return err "craftguide.show(): player name missing"
2019-10-03 22:08:51 +02:00
end
local data = pdata[name]
local player = get_player_by_name(name)
local query_item = data.query_item
reset_data(data)
item = reg_items[item] and item or query_item
2019-10-04 00:52:06 +02:00
local recipes, usages = get_recipes(item, data, player)
2019-10-03 22:08:51 +02:00
2019-10-04 00:52:06 +02:00
if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then
2020-11-15 15:30:46 +01:00
return false, msg(name, sprintf("%s: %s",
2020-08-29 18:42:04 +02:00
S"No recipe or usage for this item", get_desc(item)))
end
2019-10-04 00:52:06 +02:00
2020-11-15 15:30:46 +01:00
return false, msg(name, sprintf("%s: %s",
2020-08-29 18:42:04 +02:00
S"You don't know a recipe or usage for this item", get_desc(item)))
end
2019-10-04 00:52:06 +02:00
data.query_item = item
data.recipes = recipes
data.usages = usages
2019-10-03 22:08:51 +02:00
show_fs(player, name)
end
2019-08-30 18:50:55 +02:00
register_command("craft", {
2019-11-27 13:36:06 +01:00
description = S"Show recipe(s) of the pointed node",
func = function(name)
local player = get_player_by_name(name)
local dir = player:get_look_dir()
2019-06-23 01:42:12 +02:00
local ppos = player:get_pos()
ppos.y = ppos.y + 1.625
2019-05-08 20:57:45 +02:00
local node_name
for i = 1, 10 do
2019-05-08 20:57:45 +02:00
local look_at = vec_add(ppos, vec_mul(dir, i))
2019-08-29 15:20:19 +02:00
local node = core.get_node(look_at)
if node.name ~= "air" then
node_name = node.name
break
2019-02-06 01:51:09 +01:00
end
end
2019-02-06 01:51:09 +01:00
if not node_name then
2019-11-27 13:36:06 +01:00
return false, msg(name, S"No node pointed")
end
2019-10-03 22:08:51 +02:00
return true, craftguide.show(name, node_name)
end,
})