Progressive reveal based on doc
parent
a84b5d34cd
commit
e293581bce
|
@ -3,16 +3,15 @@
|
|||
sfcraftguide
|
||||
============
|
||||
|
||||
A simple craftguide without any hard dependencies. Supports a progressive reveal system that can follow doc items help.
|
||||
An augmented mtg_craftguide. Supports a progressive reveal system that follows doc items help.
|
||||
|
||||
Based on [mtg_craftguide](https://github.com/minetest/minetest_game), which itself is based on [craftguide](https://github.com/minetest-mods/craftguide).
|
||||
|
||||
Optional dependencies
|
||||
Dependencies
|
||||
---------------------
|
||||
If sfinv is used, adds a "Recipes" tab to the inventory. Click an item to see it's recipes.
|
||||
Click again to show usages.
|
||||
Adds a "Recipes" tab to the sfinv inventory. Click an item to see it's recipes. Click again to show usages.
|
||||
|
||||
If [doc/help](https://forum.minetest.net/viewtopic.php?f=11&t=15912) is used, recipes reveal will be synchronized with the doc items discovered.
|
||||
Recipes reveal will be synchronized with the doc items discovered ([doc/help](https://forum.minetest.net/viewtopic.php?f=11&t=15912))
|
||||
|
||||
Authors of media
|
||||
----------------
|
||||
|
|
|
@ -61,7 +61,7 @@ local function imatch(str, filter)
|
|||
end
|
||||
|
||||
|
||||
local function execute_search(data)
|
||||
function sfcg.execute_search(data)
|
||||
local init_items = sfcg.init_items
|
||||
local filter = data.filter
|
||||
if filter == "" then
|
||||
|
@ -70,12 +70,23 @@ local function execute_search(data)
|
|||
end
|
||||
data.items = {}
|
||||
|
||||
local reg_items = minetest.registered_items
|
||||
for _, item in ipairs(init_items) do
|
||||
local def = minetest.registered_items[item]
|
||||
local desc = def and minetest.get_translated_string(data.lang_code, def.description)
|
||||
|
||||
if imatch(item, filter) or desc and imatch(desc, filter) then
|
||||
table.insert(data.items, item)
|
||||
if imatch(item, filter) then
|
||||
table.insert(data.items, item)
|
||||
else
|
||||
local def = reg_items[item]
|
||||
local desc = def and def.description
|
||||
if desc then
|
||||
if imatch(desc, filter) then
|
||||
table.insert(data.items, item)
|
||||
else
|
||||
local tr_desc = minetest.get_translated_string(data.lang_code, desc)
|
||||
if tr_desc and imatch(tr_desc, filter) then
|
||||
table.insert(data.items, item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -90,7 +101,7 @@ local function table_replace(t, val, new)
|
|||
end
|
||||
|
||||
|
||||
local function item_has_groups(item_groups, groups)
|
||||
function sfcg.item_has_groups(item_groups, groups)
|
||||
for _, group in ipairs(groups) do
|
||||
if not item_groups[group] then
|
||||
return false
|
||||
|
@ -98,9 +109,10 @@ local function item_has_groups(item_groups, groups)
|
|||
end
|
||||
return true
|
||||
end
|
||||
local item_has_groups = sfcg.item_has_groups
|
||||
|
||||
|
||||
local function groups_to_item(groups)
|
||||
function sfcg.groups_to_item(groups)
|
||||
if #groups == 1 then
|
||||
local group = groups[1]
|
||||
if group_stereotypes[group] then
|
||||
|
@ -126,13 +138,15 @@ local function get_craftable_recipes(output)
|
|||
return nil
|
||||
end
|
||||
|
||||
local groups_to_item = sfcg.groups_to_item
|
||||
local reg_items = minetest.registered_items
|
||||
for i = #recipes, 1, -1 do
|
||||
for _, item in pairs(recipes[i].items) do
|
||||
local groups = extract_groups(item)
|
||||
if groups then
|
||||
item = groups_to_item(groups)
|
||||
end
|
||||
if not minetest.registered_items[item] then
|
||||
if not reg_items[item] then
|
||||
table.remove(recipes, i)
|
||||
break
|
||||
end
|
||||
|
@ -146,7 +160,7 @@ end
|
|||
|
||||
|
||||
local function show_item(def)
|
||||
return def.groups.not_in_craft_guide ~= 1 and def.description ~= ""
|
||||
return def and def.groups and def.groups.not_in_craft_guide ~= 1 and def.description ~= ""
|
||||
end
|
||||
|
||||
|
||||
|
@ -154,13 +168,14 @@ local function cache_usages(recipe)
|
|||
|
||||
local added = {}
|
||||
local usages_cache = sfcg.usages_cache
|
||||
local reg_items = minetest.registered_items
|
||||
|
||||
for _, item in pairs(recipe.items) do
|
||||
if not added[item] then
|
||||
local groups = extract_groups(item)
|
||||
if groups then
|
||||
|
||||
for name, def in pairs(minetest.registered_items) do
|
||||
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 = table.copy(recipe)
|
||||
|
@ -171,7 +186,7 @@ local function cache_usages(recipe)
|
|||
end
|
||||
end
|
||||
|
||||
elseif show_item(minetest.registered_items[item]) then
|
||||
elseif show_item(reg_items[item]) then
|
||||
usages_cache[item] = usages_cache[item] or {}
|
||||
table.insert(usages_cache[item], recipe)
|
||||
end
|
||||
|
@ -186,6 +201,7 @@ local function is_fuel(item)
|
|||
return minetest.get_craft_result({method="fuel", items={item}}).time > 0
|
||||
end
|
||||
|
||||
|
||||
function sfcg.item_button_fs(fs, x, y, item, element_name, groups)
|
||||
table.insert(fs, ("item_image_button[%s,%s;1.05,1.05;%s;%s;%s]")
|
||||
:format(x, y, item, element_name, groups and "\n"..esc(S("G")) or ""))
|
||||
|
@ -212,6 +228,7 @@ function sfcg.item_button_fs(fs, x, y, item, element_name, groups)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
function sfcg.on_receive_fields(player, fields)
|
||||
local name = player:get_player_name()
|
||||
local data = sfcg.player_data[name]
|
||||
|
@ -231,7 +248,7 @@ function sfcg.on_receive_fields(player, fields)
|
|||
end
|
||||
data.filter = new
|
||||
data.pagenum = 1
|
||||
execute_search(data)
|
||||
sfcg.execute_search(data)
|
||||
return true
|
||||
|
||||
elseif fields.prev or fields.next then
|
||||
|
@ -284,6 +301,15 @@ function sfcg.on_receive_fields(player, fields)
|
|||
end
|
||||
|
||||
|
||||
function sfcg.update_for_player(playername)
|
||||
local player = minetest.get_player_by_name(playername)
|
||||
if player then
|
||||
sfcg.execute_search(sfcg.player_data[playername])
|
||||
end
|
||||
return player
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_mods_loaded(function()
|
||||
local recipes_cache = sfcg.recipes_cache
|
||||
local usages_cache = sfcg.usages_cache
|
||||
|
@ -312,12 +338,15 @@ minetest.register_on_joinplayer(function(player)
|
|||
local name = player:get_player_name()
|
||||
local info = minetest.get_player_information(name)
|
||||
|
||||
sfcg.player_data[name] = {
|
||||
local data = {
|
||||
playername = name,
|
||||
filter = "",
|
||||
pagenum = 1,
|
||||
items = sfcg.init_items,
|
||||
lang_code = info.lang_code
|
||||
}
|
||||
sfcg.player_data[name] = data
|
||||
sfcg.execute_search(data)
|
||||
end)
|
||||
|
||||
|
||||
|
|
9
init.lua
9
init.lua
|
@ -10,7 +10,16 @@ sfcg = {
|
|||
usages_cache = {},
|
||||
}
|
||||
|
||||
function sfcg.get_usages(data, item)
|
||||
return sfcg.usages_cache[item]
|
||||
end
|
||||
|
||||
function sfcg.get_recipes(data, item)
|
||||
return sfcg.recipes_cache[item]
|
||||
end
|
||||
|
||||
dofile(modpath.."/craftguide.lua")
|
||||
dofile(modpath.."/reveal.lua")
|
||||
if (minetest.get_modpath("sfinv") and minetest.global_exists("sfinv")) then
|
||||
dofile(modpath.."/sfinv.lua")
|
||||
end
|
||||
|
|
6
mod.conf
6
mod.conf
|
@ -1,5 +1,5 @@
|
|||
name = sfcraftguide
|
||||
description = A simple craftguide without any hard dependencies. Supports a progressive reveal system that can follow docs item help.
|
||||
depends =
|
||||
optional_depends = sfinv, doc
|
||||
description = An augmented mtg_craftguide. Supports a progressive reveal system that can follow docs item help.
|
||||
depends = doc, doc_items, sfinv
|
||||
optional_depends =
|
||||
min_minetest_version = 5.0
|
||||
|
|
10
sfinv.lua
10
sfinv.lua
|
@ -6,6 +6,7 @@ local function coords(i, cols)
|
|||
return i % cols, math.floor(i / cols)
|
||||
end
|
||||
|
||||
|
||||
local function recipe_fs(fs, data)
|
||||
local recipe = data.recipes[data.rnum]
|
||||
local width = recipe.width
|
||||
|
@ -127,6 +128,15 @@ local function get_formspec(player)
|
|||
return table.concat(fs)
|
||||
end
|
||||
|
||||
local orig_update_for_player = sfcg.update_for_player
|
||||
function sfcg.update_for_player(playername)
|
||||
local player = orig_update_for_player(playername)
|
||||
if player then
|
||||
sfinv.set_player_inventory_formspec(player)
|
||||
end
|
||||
return player
|
||||
end
|
||||
|
||||
|
||||
sfinv.register_page("sfcraftguide:craftguide", {
|
||||
title = esc(S("Recipes")),
|
||||
|
|
Loading…
Reference in New Issue