Compare commits

..

7 Commits

3 changed files with 126 additions and 85 deletions

211
init.lua
View File

@ -7,7 +7,6 @@ local searches = {}
local recipes_cache = {} local recipes_cache = {}
local usages_cache = {} local usages_cache = {}
local fuel_cache = {} local fuel_cache = {}
local toolrepair local toolrepair
local progressive_mode = core.settings:get_bool "craftguide_progressive_mode" local progressive_mode = core.settings:get_bool "craftguide_progressive_mode"
@ -16,6 +15,7 @@ local autocache = core.settings:get_bool "craftguide_autocache"
local http = core.request_http_api() local http = core.request_http_api()
local storage = core.get_mod_storage() local storage = core.get_mod_storage()
local singleplayer = core.is_singleplayer()
local reg_items = core.registered_items local reg_items = core.registered_items
local reg_tools = core.registered_tools local reg_tools = core.registered_tools
@ -65,6 +65,7 @@ local FORMSPEC_MINIMAL_VERSION = 3
local ROWS = 9 local ROWS = 9
local LINES = sfinv_only and 5 or 9 local LINES = sfinv_only and 5 or 9
local IPP = ROWS * LINES
local WH_LIMIT = 8 local WH_LIMIT = 8
local XOFFSET = sfinv_only and 3.83 or 11.2 local XOFFSET = sfinv_only and 3.83 or 11.2
@ -110,6 +111,20 @@ local function get_fs_version(name)
return info and info.formspec_version or 1 return info and info.formspec_version or 1
end end
local function outdated(name)
local fs = fmt([[
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
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
local function mul_elem(elem, n) local function mul_elem(elem, n)
local fstr, elems = "", {} local fstr, elems = "", {}
@ -696,6 +711,10 @@ local function is_fav(data)
return fav, i return fav, i
end end
local function check_newline(def)
return def and def.description and find(def.description, "\n")
end
local function get_desc(name) local function get_desc(name)
if sub(name, 1, 1) == "_" then if sub(name, 1, 1) == "_" then
name = sub(name, 2) name = sub(name, 2)
@ -813,11 +832,15 @@ local function get_output_fs(data, fs, L)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]", fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
output_X, Y, 1.1, 1.1, item, _name, "") output_X, Y, 1.1, 1.1, item, _name, "")
local def = reg_items[name]
local infos = { local infos = {
unknown = not reg_items[name] or nil, unknown = not def or nil,
burntime = fuel_cache[name], burntime = fuel_cache[name],
repair = repairable(name), repair = repairable(name),
rarity = L.rarity, rarity = L.rarity,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -924,12 +947,15 @@ local function get_grid_fs(data, fs, rcp, spacing)
fs[#fs + 1] = fmt(FMT.item_image_button, fs[#fs + 1] = fmt(FMT.item_image_button,
X, Y, btn_size, btn_size, item, item, label) X, Y, btn_size, btn_size, item, item, label)
local def = reg_items[name]
local infos = { local infos = {
unknown = not reg_items[name] or nil, unknown = not def or nil,
groups = groups, groups = groups,
burntime = fuel_cache[name], burntime = fuel_cache[name],
cooktime = cooktime, cooktime = cooktime,
replace = replace, replace = replace,
newline = check_newline(def),
} }
if next(infos) then if next(infos) then
@ -1084,26 +1110,38 @@ local function make_fs(data)
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
style[filter;border=false] style[filter;border=false]
field[0.4,0.2;6.45,1;filter;;%s] field[0.4,0.2;2.5,1;filter;;%s]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
box[0,0;6.35,0.6;#bababa25] box[0,0;2.4,0.6;#bababa25]
]], ]],
ESC(data.filter)) ESC(data.filter))
fs[#fs + 1] = fmt([[ fs[#fs + 1] = fmt([[
style_type[image;noclip=false]
style_type[image_button;border=false] style_type[image_button;border=false]
style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s] style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s]
style[search;fgimg=%s;fgimg_hovered=%s] style[search;fgimg=%s;fgimg_hovered=%s]
style[clear;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[pagenum;border=false]
]], ]],
PNG.selected, PNG.selected, PNG.selected, PNG.selected,
PNG.search, PNG.search_hover, PNG.search, PNG.search_hover,
PNG.clear, PNG.clear_hover) PNG.clear, PNG.clear_hover,
PNG.prev, PNG.prev_hover, PNG.prev_hover,
PNG.next, PNG.next_hover, PNG.next_hover)
fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 2), fs[#fs + 1] = fmt(mul_elem(FMT.image_button, 4),
sfinv_only and 2.6 or 6.45, -0.06, 0.85, 0.85, "", "search", "", sfinv_only and 2.6 or 2.54, -0.06, 0.85, 0.85, "", "search", "",
sfinv_only and 3.3 or 7.15, -0.06, 0.85, 0.85, "", "clear", "") sfinv_only and 3.3 or 3.25, -0.06, 0.85, 0.85, "", "clear", "",
sfinv_only and 5.45 or (9 * 6.83) / 11, -0.06, 0.85, 0.85, "", "prev_page", "",
sfinv_only and 7.2 or (9 * 8.75) / 11, -0.06, 0.85, 0.85, "", "next_page", "")
data.pagemax = max(1, ceil(#data.items / IPP))
fs[#fs + 1] = fmt(FMT.button,
sfinv_only and 5.85 or 6, -0.1, sfinv_only and 1.82 or 1.62, 1, "pagenum",
fmt("%u / %u", data.pagenum, data.pagemax))
if #data.items == 0 then if #data.items == 0 then
local no_item = ES"No item to show" local no_item = ES"No item to show"
@ -1117,29 +1155,24 @@ local function make_fs(data)
fs[#fs + 1] = fmt(FMT.label, pos, 2, no_item) fs[#fs + 1] = fmt(FMT.label, pos, 2, no_item)
end end
fs[#fs + 1] = "scroll_container[0,1.3;9,9.3;scrbar;vertical]" local first_item = (data.pagenum - 1) * IPP
for i = 0, #data.items do for i = first_item, first_item + IPP - 1 do
local item = data.items[i + 1] local item = data.items[i + 1]
if not item then break end if not item then break end
local X = i % ROWS local X = i % ROWS
local Y = (i - X) / ROWS local Y = (i % IPP - X) / ROWS + 1
X = X - (X * 0.12) X = X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05
Y = Y - (Y * 0.02) Y = Y - (Y * 0.1) - 0.1
if data.query_item == item then if data.query_item == item then
fs[#fs + 1] = fmt(FMT.image, X, Y, 1, 1, PNG.selected) fs[#fs + 1] = fmt(FMT.image, X, Y, 1, 1, PNG.selected)
end end
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]", fs[#fs + 1] = fmt("item_image_button[%f,%f;1,1;%s;%s_inv;]", X, Y, item, item)
X, Y, 1, 1, item, item)
end end
fs[#fs + 1] = "scroll_container_end[]"
fs[#fs + 1] = fmt("scrollbaroptions[arrows=hide;max=%u]", (#data.items / 8) * 10 - 80)
fs[#fs + 1] = fmt("scrollbar[7.2,0.8;0.45,8.1;vertical;scrbar;%u]", data.scrbar)
if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then if (data.recipes and #data.recipes > 0) or (data.usages and #data.usages > 0) then
get_panels(data, fs) get_panels(data, fs)
end end
@ -1158,12 +1191,12 @@ end
craftguide.register_craft_type("digging", { craftguide.register_craft_type("digging", {
description = ES"Digging", description = ES"Digging",
icon = "default_tool_steelpick.png", icon = "craftguide_steelpick.png",
}) })
craftguide.register_craft_type("digging_chance", { craftguide.register_craft_type("digging_chance", {
description = ES"Digging Chance", description = ES"Digging Chance",
icon = "default_tool_mesepick.png", icon = "craftguide_mesepick.png",
}) })
local function search(data) local function search(data)
@ -1516,7 +1549,7 @@ end
local function init_data(name) local function init_data(name)
pdata[name] = { pdata[name] = {
filter = "", filter = "",
scrbar = 0, pagenum = 1,
items = init_items, items = init_items,
items_raw = init_items, items_raw = init_items,
favs = {}, favs = {},
@ -1526,7 +1559,7 @@ end
local function reset_data(data) local function reset_data(data)
data.filter = "" data.filter = ""
data.scrbar = 0 data.pagenum = 1
data.rnum = 1 data.rnum = 1
data.unum = 1 data.unum = 1
data.query_item = nil data.query_item = nil
@ -1541,6 +1574,10 @@ on_mods_loaded(get_init_items)
on_joinplayer(function(player) on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
init_data(name) init_data(name)
if pdata[name].fs_version < FORMSPEC_MINIMAL_VERSION then
outdated(name)
end
end) end)
local function fields(player, _f) local function fields(player, _f)
@ -1568,9 +1605,19 @@ local function fields(player, _f)
if data.filter == str then return end if data.filter == str then return end
data.filter = str data.filter = str
data.scrbar = 0 data.pagenum = 1
search(data) search(data)
elseif _f.prev_page or _f.next_page then
if data.pagemax == 1 then return end
data.pagenum = data.pagenum - (_f.prev_page and 1 or -1)
if data.pagenum > data.pagemax then
data.pagenum = 1
elseif data.pagenum == 0 then
data.pagenum = data.pagemax
end
elseif _f.fav then elseif _f.fav then
local fav, i = is_fav(data) local fav, i = is_fav(data)
local total = #data.favs local total = #data.favs
@ -1618,7 +1665,6 @@ local function fields(player, _f)
data.usages = usages data.usages = usages
data.rnum = 1 data.rnum = 1
data.unum = 1 data.unum = 1
data.scrbar = tonumber(match(_f.scrbar, "%d+"))
end end
return true, show_fs(player, name) return true, show_fs(player, name)
@ -1636,9 +1682,8 @@ if sfinv_only then
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
local formspec = make_fs(data)
return sfinv.make_formspec(player, context, formspec) return sfinv.make_formspec(player, context, make_fs(data))
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
@ -1667,17 +1712,7 @@ else
local data = pdata[name] local data = pdata[name]
if data.fs_version < FORMSPEC_MINIMAL_VERSION then if data.fs_version < FORMSPEC_MINIMAL_VERSION then
local fs = fmt([[ return outdated(name)
size[6.6,1.3]
image[0,0;1,1;%s]
label[1,0;%s]
button_exit[2.8,0.8;1,1;;OK]
]],
PNG.nothing,
"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 end
if next(recipe_filters) then if next(recipe_filters) then
@ -1708,7 +1743,12 @@ else
paramtype = "light", paramtype = "light",
paramtype2 = "wallmounted", paramtype2 = "wallmounted",
sunlight_propagates = true, sunlight_propagates = true,
groups = {choppy = 1, attached_node = 1, oddly_breakable_by_hand = 1, flammable = 3}, groups = {
choppy = 1,
attached_node = 1,
oddly_breakable_by_hand = 1,
flammable = 3,
},
node_box = { node_box = {
type = "wallmounted", type = "wallmounted",
wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5}, wall_top = {-0.5, 0.4375, -0.5, 0.5, 0.5, 0.5},
@ -1753,7 +1793,7 @@ else
if rawget(_G, "sfinv_buttons") then if rawget(_G, "sfinv_buttons") then
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S"Crafting Guide", title = S"Crafting Guide",
tooltip = S"Shows a list of available crafting recipes, cooking recipes and fuels", tooltip = S"Shows a list of available crafting recipes",
image = PNG.book, image = PNG.book,
action = function(player) action = function(player)
on_use(player) on_use(player)
@ -1763,7 +1803,6 @@ else
end end
if progressive_mode then if progressive_mode then
local PLAYERS = {}
local POLL_FREQ = 0.25 local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5 local HUD_TIMER_MAX = 1.5
@ -1850,6 +1889,34 @@ if progressive_mode then
return inv_items return inv_items
end end
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,
},
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,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end
local function show_hud_success(player, data) local function show_hud_success(player, data)
-- It'd better to have an engine function `hud_move` to only need -- It'd better to have an engine function `hud_move` to only need
-- 2 calls for the notification's back and forth. -- 2 calls for the notification's back and forth.
@ -1897,8 +1964,9 @@ if progressive_mode then
-- Workaround. Need an engine call to detect when the contents of -- Workaround. Need an engine call to detect when the contents of
-- the player inventory changed, instead. -- the player inventory changed, instead.
local function poll_new_items() local function poll_new_items()
for i = 1, #PLAYERS do local players = get_players()
local player = PLAYERS[i] for i = 1, #players do
local player = players[i]
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
@ -1931,12 +1999,13 @@ if progressive_mode then
poll_new_items() poll_new_items()
globalstep(function() globalstep(function()
for i = 1, #PLAYERS do local players = get_players()
local player = PLAYERS[i] for i = 1, #players do
local player = players[i]
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
if data.show_hud ~= nil then if data.show_hud ~= nil and singleplayer then
show_hud_success(player, data) show_hud_success(player, data)
end end
end end
@ -1945,8 +2014,6 @@ if progressive_mode then
craftguide.add_recipe_filter("Default progressive filter", progressive_filter) craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
on_joinplayer(function(player) on_joinplayer(function(player)
PLAYERS = get_players()
local name = player:get_player_name() local name = player:get_player_name()
local data = pdata[name] local data = pdata[name]
@ -1954,31 +2021,9 @@ if progressive_mode then
data.inv_items = dslz(meta:get_string "inv_items") or {} data.inv_items = dslz(meta:get_string "inv_items") or {}
data.known_recipes = dslz(meta:get_string "known_recipes") or 0 data.known_recipes = dslz(meta:get_string "known_recipes") or 0
data.hud = { if singleplayer then
bg = player:hud_add{ init_hud(player, data)
hud_elem_type = "image", end
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
},
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,
},
text = player:hud_add{
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
},
}
end) end)
local to_save = {"inv_items", "known_recipes"} local to_save = {"inv_items", "known_recipes"}
@ -1994,14 +2039,12 @@ if progressive_mode then
end end
end end
on_leaveplayer(function(player) on_leaveplayer(save_meta)
PLAYERS = get_players()
save_meta(player)
end)
on_shutdown(function() on_shutdown(function()
for i = 1, #PLAYERS do local players = get_players()
local player = PLAYERS[i] for i = 1, #players do
local player = players[i]
save_meta(player) save_meta(player)
end end
end) end)
@ -2029,13 +2072,11 @@ function craftguide.show(name, item, show_usages)
if not recipes and not usages then if not recipes and not usages then
if not recipes_cache[item] and not usages_cache[item] then if not recipes_cache[item] and not usages_cache[item] then
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"No recipe or usage for this item", S"No recipe or usage for this item", get_desc(item)))
get_desc(item)))
end end
return false, msg(name, fmt("%s: %s", return false, msg(name, fmt("%s: %s",
S"You don't know a recipe or usage for this item", S"You don't know a recipe or usage for this item", get_desc(item)))
get_desc(item)))
end end
data.query_item = item data.query_item = item

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB