mirror of
https://github.com/minetest-mods/craftguide.git
synced 2025-06-28 14:16:16 +02:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
d324c5f1e5 | |||
dfa45789e2 | |||
80a0d67f15 | |||
22c5c9444e | |||
97676d094e | |||
ca18ae0e3a | |||
e710fcd483 | |||
a2e4f20791 |
@ -2,6 +2,7 @@ unused_args = false
|
||||
allow_defined_top = true
|
||||
|
||||
read_globals = {
|
||||
"core",
|
||||
"minetest",
|
||||
"default",
|
||||
"sfinv",
|
||||
|
65
API.md
65
API.md
@ -11,17 +11,35 @@ craftguide.register_craft_type("digging", {
|
||||
})
|
||||
```
|
||||
|
||||
#### Registering a custom crafting recipe (example)
|
||||
#### Registering a custom crafting recipe (examples)
|
||||
|
||||
```Lua
|
||||
craftguide.register_craft({
|
||||
type = "digging",
|
||||
width = 1,
|
||||
output = "default:cobble 2",
|
||||
result = "default:cobble 2",
|
||||
items = {"default:stone"},
|
||||
})
|
||||
```
|
||||
|
||||
Recipes can also be registered in a Minecraft-like way:
|
||||
|
||||
```Lua
|
||||
craftguide.register_craft({
|
||||
grid = {
|
||||
"X #",
|
||||
" ## ",
|
||||
"X#X#",
|
||||
"X X",
|
||||
},
|
||||
key = {
|
||||
['#'] = "default:wood",
|
||||
['X'] = "default:glass",
|
||||
},
|
||||
result = "default:mese 3",
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Recipe filters
|
||||
@ -119,49 +137,6 @@ Returns a map of search filters, indexed by name.
|
||||
|
||||
---
|
||||
|
||||
### Custom formspec elements
|
||||
|
||||
#### `craftguide.add_formspec_element(name, def)`
|
||||
|
||||
Adds a formspec element to the current formspec.
|
||||
Supported types: `box`, `label`, `image`, `button`, `tooltip`, `item_image`, `image_button`, `item_image_button`
|
||||
|
||||
Example:
|
||||
|
||||
```lua
|
||||
craftguide.add_formspec_element("export", {
|
||||
type = "button",
|
||||
element = function(data)
|
||||
-- Should return a table of parameters according to the formspec element type.
|
||||
-- Note: for all buttons, the 'name' parameter *must not* be specified!
|
||||
if data.recipes then
|
||||
return {
|
||||
data.iX - 3.7, -- X
|
||||
sfinv_only and 7.9 or 8, -- Y
|
||||
1.6, -- W
|
||||
1, -- H
|
||||
ESC(S("Export")) -- label
|
||||
}
|
||||
end
|
||||
end,
|
||||
-- Optional.
|
||||
action = function(player, data)
|
||||
-- When the button is pressed.
|
||||
print("Exported!")
|
||||
end
|
||||
})
|
||||
```
|
||||
|
||||
#### `craftguide.remove_formspec_element(name)`
|
||||
|
||||
Removes the formspec element with the given name.
|
||||
|
||||
#### `craftguide.get_formspec_elements()`
|
||||
|
||||
Returns a map of formspec elements, indexed by name.
|
||||
|
||||
---
|
||||
|
||||
### Miscellaneous
|
||||
|
||||
#### `craftguide.show(player_name, item, show_usages)`
|
||||
|
459
init.lua
459
init.lua
@ -1,7 +1,6 @@
|
||||
craftguide = {}
|
||||
|
||||
local M = minetest
|
||||
local player_data = {}
|
||||
local pdata = {}
|
||||
|
||||
-- Caches
|
||||
local init_items = {}
|
||||
@ -10,30 +9,31 @@ local recipes_cache = {}
|
||||
local usages_cache = {}
|
||||
local fuel_cache = {}
|
||||
|
||||
local progressive_mode = M.settings:get_bool("craftguide_progressive_mode")
|
||||
local sfinv_only = M.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
|
||||
local progressive_mode = core.settings:get_bool("craftguide_progressive_mode")
|
||||
local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
|
||||
|
||||
local after = M.after
|
||||
local colorize = M.colorize
|
||||
local reg_items = M.registered_items
|
||||
local get_result = M.get_craft_result
|
||||
local show_formspec = M.show_formspec
|
||||
local get_player_by_name = M.get_player_by_name
|
||||
local serialize, deserialize = M.serialize, M.deserialize
|
||||
local after = core.after
|
||||
local colorize = core.colorize
|
||||
local reg_items = core.registered_items
|
||||
local get_result = core.get_craft_result
|
||||
local get_players = core.get_connected_players
|
||||
local show_formspec = core.show_formspec
|
||||
local get_all_recipes = core.get_all_craft_recipes
|
||||
local get_player_by_name = core.get_player_by_name
|
||||
local serialize, deserialize = core.serialize, core.deserialize
|
||||
|
||||
local ESC = M.formspec_escape
|
||||
local S = M.get_translator("craftguide")
|
||||
local ESC = core.formspec_escape
|
||||
local S = core.get_translator("craftguide")
|
||||
|
||||
local maxn, sort, concat, insert, copy =
|
||||
table.maxn, table.sort, table.concat, table.insert,
|
||||
table.copy
|
||||
local maxn, sort, concat, copy =
|
||||
table.maxn, table.sort, table.concat, table.copy
|
||||
|
||||
local fmt, find, gmatch, match, sub, split, lower =
|
||||
local fmt, find, gmatch, match, sub, split, upper, lower =
|
||||
string.format, string.find, string.gmatch, string.match,
|
||||
string.sub, string.split, string.lower
|
||||
string.sub, string.split, string.upper, string.lower
|
||||
|
||||
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
|
||||
local pairs, next, unpack = pairs, next, unpack
|
||||
local pairs, next = pairs, next
|
||||
local vec_add, vec_mul = vector.add, vector.multiply
|
||||
|
||||
local DEFAULT_SIZE = 10
|
||||
@ -41,13 +41,12 @@ local MIN_LIMIT, MAX_LIMIT = 10, 12
|
||||
DEFAULT_SIZE = min(MAX_LIMIT, max(MIN_LIMIT, DEFAULT_SIZE))
|
||||
|
||||
local GRID_LIMIT = 5
|
||||
local POLL_FREQ = 0.25
|
||||
|
||||
local FMT = {
|
||||
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]",
|
||||
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]",
|
||||
tooltip = "tooltip[%f,%f;%f,%f;%s]",
|
||||
item_image = "item_image[%f,%f;%f,%f;%s]",
|
||||
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
|
||||
@ -148,11 +147,46 @@ function craftguide.register_craft_type(name, def)
|
||||
end
|
||||
|
||||
function craftguide.register_craft(def)
|
||||
local func = "craftguide." .. __func() .. "(): "
|
||||
assert(is_str(def.type), func .. "'type' field missing")
|
||||
assert(is_num(def.width), func .. "'width' field missing")
|
||||
assert(is_str(def.output), func .. "'output' field missing")
|
||||
assert(is_table(def.items), func .. "'items' field missing")
|
||||
if def.result then
|
||||
def.output = def.result -- Backward compatibility
|
||||
end
|
||||
|
||||
if not is_str(def.output) then
|
||||
def.output = ""
|
||||
end
|
||||
|
||||
if not is_table(def.items) then
|
||||
def.items = {}
|
||||
end
|
||||
|
||||
if not is_num(def.width) then
|
||||
def.width = 0
|
||||
end
|
||||
|
||||
if def.grid then
|
||||
if not is_table(def.grid) then
|
||||
def.grid = {}
|
||||
end
|
||||
|
||||
local cp = copy(def.grid)
|
||||
sort(cp, function(a, b)
|
||||
return #a > #b
|
||||
end)
|
||||
|
||||
def.width = #cp[1]
|
||||
|
||||
for i = 1, #def.grid do
|
||||
while #def.grid[i] < def.width do
|
||||
def.grid[i] = def.grid[i] .. " "
|
||||
end
|
||||
end
|
||||
|
||||
local c = 1
|
||||
for symbol in gmatch(concat(def.grid), ".") do
|
||||
def.items[c] = def.key[symbol]
|
||||
c = c + 1
|
||||
end
|
||||
end
|
||||
|
||||
custom_crafts[#custom_crafts + 1] = def
|
||||
end
|
||||
@ -209,34 +243,10 @@ function craftguide.get_search_filters()
|
||||
return search_filters
|
||||
end
|
||||
|
||||
local formspec_elements = {}
|
||||
|
||||
function craftguide.add_formspec_element(name, def)
|
||||
local func = "craftguide." .. __func() .. "(): "
|
||||
assert(is_str(name), func .. "formspec element name missing")
|
||||
assert(is_str(def.element), func .. "'element' field not defined")
|
||||
assert(is_str(def.type), func .. "'type' field not defined")
|
||||
assert(FMT[def.type], func .. "'" .. def.type .. "' type not supported by the API")
|
||||
|
||||
formspec_elements[name] = {
|
||||
type = def.type,
|
||||
element = def.element,
|
||||
action = def.action,
|
||||
}
|
||||
end
|
||||
|
||||
function craftguide.remove_formspec_element(name)
|
||||
formspec_elements[name] = nil
|
||||
end
|
||||
|
||||
function craftguide.get_formspec_elements()
|
||||
return formspec_elements
|
||||
end
|
||||
|
||||
local function item_has_groups(item_groups, groups)
|
||||
for i = 1, #groups do
|
||||
local group = groups[i]
|
||||
if not item_groups[group] then
|
||||
if (item_groups[group] or 0) == 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
@ -271,7 +281,7 @@ local function groups_item_in_recipe(item, recipe)
|
||||
end
|
||||
end
|
||||
|
||||
local function get_item_usages(item)
|
||||
local function get_usages(item)
|
||||
local usages, c = {}, 0
|
||||
|
||||
for _, recipes in pairs(recipes_cache) do
|
||||
@ -297,39 +307,54 @@ local function get_item_usages(item)
|
||||
return usages
|
||||
end
|
||||
|
||||
local function get_filtered_items(player)
|
||||
local function get_filtered_items(player, data)
|
||||
local items, c = {}, 0
|
||||
local known = 0
|
||||
|
||||
for i = 1, #init_items do
|
||||
local item = init_items[i]
|
||||
local recipes = recipes_cache[item]
|
||||
local usages = usages_cache[item]
|
||||
|
||||
if recipes and #apply_recipe_filters(recipes, player) > 0 or
|
||||
usages and #apply_recipe_filters(usages, player) > 0 then
|
||||
c = c + 1
|
||||
items[c] = item
|
||||
recipes = #apply_recipe_filters(recipes or {}, player)
|
||||
usages = #apply_recipe_filters(usages or {}, player)
|
||||
|
||||
if recipes > 0 or usages > 0 then
|
||||
if not data then
|
||||
c = c + 1
|
||||
items[c] = item
|
||||
else
|
||||
known = known + recipes + usages
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return items
|
||||
if data then
|
||||
data.known_recipes = known
|
||||
else
|
||||
return items
|
||||
end
|
||||
end
|
||||
|
||||
local function cache_recipes(output)
|
||||
local recipes = M.get_all_craft_recipes(output) or {}
|
||||
local c = 0
|
||||
local recipes = get_all_recipes(output) or {}
|
||||
|
||||
for i = 1, #custom_crafts do
|
||||
local custom_craft = custom_crafts[i]
|
||||
if match(custom_craft.output, "%S*") == output then
|
||||
c = c + 1
|
||||
recipes[c] = custom_craft
|
||||
recipes[#recipes + 1] = custom_craft
|
||||
end
|
||||
end
|
||||
|
||||
if #recipes > 0 then
|
||||
recipes_cache[output] = recipes
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function cache_usages(item)
|
||||
local usages = get_usages(item)
|
||||
if #usages > 0 then
|
||||
usages_cache[item] = usages
|
||||
end
|
||||
end
|
||||
|
||||
@ -350,7 +375,7 @@ local function get_recipes(item, data, player)
|
||||
|
||||
if data.show_usages then
|
||||
recipes = apply_recipe_filters(usages_cache[item], player)
|
||||
if #recipes == 0 then
|
||||
if recipes and #recipes == 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
@ -407,7 +432,11 @@ local function get_tooltip(item, groups, cooktime, burntime)
|
||||
groupstr = concat(groupstr, ", ")
|
||||
tooltip = S("Any item belonging to the group(s): @1", groupstr)
|
||||
else
|
||||
tooltip = reg_items[item].description
|
||||
local def = reg_items[item]
|
||||
|
||||
tooltip = def and def.description or
|
||||
(def and match(item, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
|
||||
S("Unknown Item (@1)", item))
|
||||
end
|
||||
|
||||
if cooktime then
|
||||
@ -470,7 +499,7 @@ local function get_recipe_fs(data, iY)
|
||||
if width > 3 or rows > 3 then
|
||||
btn_size = width > 3 and 3 / width or 3 / rows
|
||||
s_btn_size = btn_size
|
||||
X = btn_size * (i % width) + xoffset - 2.65
|
||||
X = btn_size * ((i - 1) % width) + xoffset - 2.65
|
||||
Y = btn_size * floor((i - 1) / width) + (iY + 3) - min(2, rows)
|
||||
end
|
||||
|
||||
@ -480,7 +509,7 @@ local function get_recipe_fs(data, iY)
|
||||
|
||||
local groups
|
||||
|
||||
if sub(item, 1, 6) == "group:" then
|
||||
if sub(item, 1,6) == "group:" then
|
||||
groups = extract_groups(item)
|
||||
item = groups_to_item(groups)
|
||||
end
|
||||
@ -514,7 +543,7 @@ local function get_recipe_fs(data, iY)
|
||||
end
|
||||
|
||||
fs[#fs + 1] = fmt(FMT.image,
|
||||
rightest + 1.2,
|
||||
min(3.9, rightest) + 1.2,
|
||||
sfinv_only and 6.2 or iY + 1.7,
|
||||
0.5,
|
||||
0.5,
|
||||
@ -584,7 +613,7 @@ local function get_recipe_fs(data, iY)
|
||||
end
|
||||
|
||||
local function make_formspec(name)
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
local iY = sfinv_only and 4 or data.iX - 5
|
||||
local ipp = data.iX * iY
|
||||
|
||||
@ -600,33 +629,22 @@ local function make_formspec(name)
|
||||
background[1,1;1,1;craftguide_bg.png;true]
|
||||
]]
|
||||
|
||||
fs[#fs + 1] = fmt([[ tooltip[size_inc;%s]
|
||||
tooltip[size_dec;%s] ]],
|
||||
ESC(S("Increase window size")),
|
||||
ESC(S("Decrease window size")))
|
||||
|
||||
fs[#fs + 1] = fmt([[
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;] ]],
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]
|
||||
]],
|
||||
data.iX * 0.47,
|
||||
data.iX * 0.47 + 0.6)
|
||||
end
|
||||
|
||||
fs[#fs + 1] = fmt("field[0.3,0.33;2.5,1;filter;;%s]", ESC(data.filter))
|
||||
|
||||
fs[#fs + 1] = [[
|
||||
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
|
||||
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
|
||||
field_close_on_enter[filter;false]
|
||||
]]
|
||||
|
||||
fs[#fs + 1] = fmt([[ tooltip[search;%s]
|
||||
tooltip[clear;%s]
|
||||
tooltip[prev;%s]
|
||||
tooltip[next;%s] ]],
|
||||
ESC(S("Search")),
|
||||
ESC(S("Reset")),
|
||||
ESC(S("Previous page")),
|
||||
ESC(S("Next page")))
|
||||
|
||||
fs[#fs + 1] = fmt("label[%f,%f;%s / %u]",
|
||||
sfinv_only and 6.3 or data.iX - 2.2,
|
||||
0.22,
|
||||
@ -635,12 +653,11 @@ local function make_formspec(name)
|
||||
|
||||
fs[#fs + 1] = fmt([[
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_prev_icon.png;prev;]
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_next_icon.png;next;] ]],
|
||||
image_button[%f,0.12;0.8,0.8;craftguide_next_icon.png;next;]
|
||||
]],
|
||||
sfinv_only and 5.5 or data.iX - 3.1,
|
||||
sfinv_only and 7.3 or (data.iX - 1.2) - (data.iX >= 11 and 0.08 or 0))
|
||||
|
||||
fs[#fs + 1] = fmt("field[0.3,0.32;2.5,1;filter;;%s]", ESC(data.filter))
|
||||
|
||||
if #data.items == 0 then
|
||||
local no_item = S("No item to show")
|
||||
local pos = (data.iX / 2) - 1
|
||||
@ -676,17 +693,6 @@ local function make_formspec(name)
|
||||
fs[#fs + 1] = get_recipe_fs(data, iY)
|
||||
end
|
||||
|
||||
for elem_name, def in pairs(formspec_elements) do
|
||||
local element = def.element(data)
|
||||
if element then
|
||||
if find(def.type, "button") then
|
||||
insert(element, #element, elem_name)
|
||||
end
|
||||
|
||||
fs[#fs + 1] = fmt(FMT[def.type], unpack(element))
|
||||
end
|
||||
end
|
||||
|
||||
return concat(fs)
|
||||
end
|
||||
|
||||
@ -721,13 +727,12 @@ local function search(data)
|
||||
return
|
||||
end
|
||||
|
||||
local filtered_list, c = {}, 0
|
||||
local extras = "^(.-)%+([%w_]+)=([%w_,]+)"
|
||||
local search_filter = next(search_filters) and match(filter, extras)
|
||||
local opt = "^(.-)%+([%w_]+)=([%w_,]+)"
|
||||
local search_filter = next(search_filters) and match(filter, opt)
|
||||
local filters = {}
|
||||
|
||||
if search_filter then
|
||||
for filter_name, values in gmatch(filter, sub(extras, 6, -1)) do
|
||||
for filter_name, values in gmatch(filter, sub(opt, 6)) do
|
||||
if search_filters[filter_name] then
|
||||
values = split(values, ",")
|
||||
filters[filter_name] = values
|
||||
@ -735,11 +740,13 @@ local function search(data)
|
||||
end
|
||||
end
|
||||
|
||||
local filtered_list, c = {}, 0
|
||||
|
||||
for i = 1, #data.items_raw do
|
||||
local item = data.items_raw[i]
|
||||
local def = reg_items[item]
|
||||
local desc = lower(def.description)
|
||||
local search_in = item .. desc
|
||||
local desc = (def and def.description) and lower(def.description) or ""
|
||||
local search_in = item .. " " .. desc
|
||||
local to_add
|
||||
|
||||
if search_filter then
|
||||
@ -771,7 +778,7 @@ local function search(data)
|
||||
end
|
||||
|
||||
local function init_data(name)
|
||||
player_data[name] = {
|
||||
pdata[name] = {
|
||||
filter = "",
|
||||
pagenum = 1,
|
||||
iX = sfinv_only and 8 or DEFAULT_SIZE,
|
||||
@ -790,39 +797,43 @@ local function reset_data(data)
|
||||
data.items = data.items_raw
|
||||
end
|
||||
|
||||
local function cache_usages()
|
||||
for i = 1, #init_items do
|
||||
local item = init_items[i]
|
||||
usages_cache[item] = get_item_usages(item)
|
||||
end
|
||||
local function check_item(def)
|
||||
return 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_init_items()
|
||||
local c = 0
|
||||
local items, c = {}, 0
|
||||
|
||||
for name, def in pairs(reg_items) do
|
||||
local is_fuel = cache_fuel(name)
|
||||
if not (def.groups.not_in_craft_guide == 1 or
|
||||
def.groups.not_in_creative_inventory == 1) and
|
||||
def.description and def.description ~= "" and
|
||||
(cache_recipes(name) or is_fuel) then
|
||||
if check_item(def) then
|
||||
cache_fuel(name)
|
||||
cache_recipes(name)
|
||||
|
||||
c = c + 1
|
||||
items[c] = name
|
||||
end
|
||||
end
|
||||
|
||||
c = 0
|
||||
|
||||
for i = 1, #items do
|
||||
local name = items[i]
|
||||
cache_usages(name)
|
||||
|
||||
if recipes_cache[name] or usages_cache[name] then
|
||||
c = c + 1
|
||||
init_items[c] = name
|
||||
end
|
||||
end
|
||||
|
||||
sort(init_items)
|
||||
cache_usages()
|
||||
end
|
||||
|
||||
local function on_receive_fields(player, fields)
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
|
||||
for elem_name, def in pairs(formspec_elements) do
|
||||
if fields[elem_name] and def.action then
|
||||
return def.action(player, data)
|
||||
end
|
||||
end
|
||||
local data = pdata[name]
|
||||
|
||||
if fields.clear then
|
||||
reset_data(data)
|
||||
@ -903,9 +914,9 @@ local function on_receive_fields(player, fields)
|
||||
end
|
||||
end
|
||||
|
||||
M.register_on_mods_loaded(get_init_items)
|
||||
core.register_on_mods_loaded(get_init_items)
|
||||
|
||||
M.register_on_joinplayer(function(player)
|
||||
core.register_on_joinplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
init_data(name)
|
||||
end)
|
||||
@ -924,7 +935,7 @@ if sfinv_only then
|
||||
on_enter = function(self, player, context)
|
||||
if next(recipe_filters) then
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
|
||||
data.items_raw = get_filtered_items(player)
|
||||
search(data)
|
||||
@ -936,7 +947,7 @@ if sfinv_only then
|
||||
end,
|
||||
})
|
||||
else
|
||||
M.register_on_player_receive_fields(function(player, formname, fields)
|
||||
core.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname == "craftguide" then
|
||||
on_receive_fields(player, fields)
|
||||
end
|
||||
@ -946,7 +957,7 @@ else
|
||||
local name = user:get_player_name()
|
||||
|
||||
if next(recipe_filters) then
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
data.items_raw = get_filtered_items(user)
|
||||
search(data)
|
||||
end
|
||||
@ -954,7 +965,7 @@ else
|
||||
show_formspec(name, "craftguide", make_formspec(name))
|
||||
end
|
||||
|
||||
M.register_craftitem("craftguide:book", {
|
||||
core.register_craftitem("craftguide:book", {
|
||||
description = S("Crafting Guide"),
|
||||
inventory_image = "craftguide_book.png",
|
||||
wield_image = "craftguide_book.png",
|
||||
@ -965,7 +976,7 @@ else
|
||||
end
|
||||
})
|
||||
|
||||
M.register_node("craftguide:sign", {
|
||||
core.register_node("craftguide:sign", {
|
||||
description = S("Crafting Guide Sign"),
|
||||
drawtype = "nodebox",
|
||||
tiles = {"craftguide_sign.png"},
|
||||
@ -983,7 +994,7 @@ else
|
||||
},
|
||||
|
||||
on_construct = function(pos)
|
||||
local meta = M.get_meta(pos)
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_string("infotext", "Crafting Guide Sign")
|
||||
end,
|
||||
|
||||
@ -992,27 +1003,27 @@ else
|
||||
end
|
||||
})
|
||||
|
||||
M.register_craft({
|
||||
core.register_craft({
|
||||
output = "craftguide:book",
|
||||
recipe = {
|
||||
{"default:book"}
|
||||
}
|
||||
})
|
||||
|
||||
M.register_craft({
|
||||
core.register_craft({
|
||||
type = "fuel",
|
||||
recipe = "craftguide:book",
|
||||
burntime = 3
|
||||
})
|
||||
|
||||
M.register_craft({
|
||||
core.register_craft({
|
||||
output = "craftguide:sign",
|
||||
recipe = {
|
||||
{"default:sign_wall_wood"}
|
||||
}
|
||||
})
|
||||
|
||||
M.register_craft({
|
||||
core.register_craft({
|
||||
type = "fuel",
|
||||
recipe = "craftguide:sign",
|
||||
burntime = 10
|
||||
@ -1031,6 +1042,9 @@ else
|
||||
end
|
||||
|
||||
if progressive_mode then
|
||||
local PLAYERS = {}
|
||||
local POLL_FREQ = 0.25
|
||||
|
||||
local function item_in_inv(item, inv_items)
|
||||
local inv_items_size = #inv_items
|
||||
|
||||
@ -1065,8 +1079,12 @@ if progressive_mode then
|
||||
end
|
||||
|
||||
local function progressive_filter(recipes, player)
|
||||
if not recipes then
|
||||
return {}
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
|
||||
if #data.inv_items == 0 then
|
||||
return {}
|
||||
@ -1109,19 +1127,67 @@ if progressive_mode then
|
||||
return inv_items
|
||||
end
|
||||
|
||||
local function show_hud_success(player, data, dtime)
|
||||
local hud_info_bg = player:hud_get(data.hud.bg)
|
||||
|
||||
if hud_info_bg.position.y <= 0.9 then
|
||||
data.show_hud = false
|
||||
data.hud_timer = (data.hud_timer or 0) + dtime
|
||||
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 - (dtime / 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 > 3 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 + (dtime / 5)
|
||||
})
|
||||
end
|
||||
|
||||
if hud_info_bg.position.y >= 1 then
|
||||
data.show_hud = nil
|
||||
data.hud_timer = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Workaround. Need an engine call to detect when the contents
|
||||
-- of the player inventory changed, instead.
|
||||
-- of the player inventory changed, instead
|
||||
local function poll_new_items()
|
||||
local players = M.get_connected_players()
|
||||
for i = 1, #players do
|
||||
local player = players[i]
|
||||
for i = 1, #PLAYERS do
|
||||
local player = PLAYERS[i]
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
|
||||
local inv_items = get_inv_items(player)
|
||||
local diff = table_diff(inv_items, data.inv_items)
|
||||
local diff = table_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
|
||||
|
||||
@ -1130,53 +1196,97 @@ if progressive_mode then
|
||||
|
||||
poll_new_items()
|
||||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
for i = 1, #PLAYERS do
|
||||
local player = PLAYERS[i]
|
||||
local name = player:get_player_name()
|
||||
local data = pdata[name]
|
||||
|
||||
if data.show_hud ~= nil then
|
||||
show_hud_success(player, data, dtime)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
|
||||
|
||||
M.register_on_joinplayer(function(player)
|
||||
core.register_on_joinplayer(function(player)
|
||||
PLAYERS = get_players()
|
||||
|
||||
local meta = player:get_meta()
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
|
||||
data.inv_items = deserialize(meta:get_string("inv_items")) or {}
|
||||
data.known_recipes = deserialize(meta:get_string("known_recipes")) or 0
|
||||
|
||||
data.hud = {
|
||||
bg = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
position = {x = 0.8, y = 1},
|
||||
alignment = {x = 1, y = 1},
|
||||
scale = {x = 320, y = 112},
|
||||
text = "craftguide_bg.png",
|
||||
}),
|
||||
|
||||
book = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
position = {x = 0.81, y = 1.02},
|
||||
alignment = {x = 1, y = 1},
|
||||
scale = {x = 4, y = 4},
|
||||
text = "craftguide_book.png",
|
||||
}),
|
||||
|
||||
text = player:hud_add({
|
||||
hud_elem_type = "text",
|
||||
position = {x = 0.85, y = 1.04},
|
||||
alignment = {x = 1, y = 1},
|
||||
number = 0xFFFFFF,
|
||||
text = "",
|
||||
}),
|
||||
}
|
||||
end)
|
||||
|
||||
local function save_meta(player)
|
||||
local meta = player:get_meta()
|
||||
local name = player:get_player_name()
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
|
||||
meta:set_string("inv_items", serialize(data.inv_items))
|
||||
meta:set_string("known_recipes", serialize(data.known_recipes))
|
||||
end
|
||||
|
||||
M.register_on_leaveplayer(save_meta)
|
||||
core.register_on_leaveplayer(function(player)
|
||||
PLAYERS = get_players()
|
||||
save_meta(player)
|
||||
end)
|
||||
|
||||
M.register_on_shutdown(function()
|
||||
local players = M.get_connected_players()
|
||||
for i = 1, #players do
|
||||
local player = players[i]
|
||||
core.register_on_shutdown(function()
|
||||
for i = 1, #PLAYERS do
|
||||
local player = PLAYERS[i]
|
||||
save_meta(player)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
M.register_on_leaveplayer(function(player)
|
||||
core.register_on_leaveplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
player_data[name] = nil
|
||||
pdata[name] = nil
|
||||
end)
|
||||
|
||||
M.register_chatcommand("craft", {
|
||||
core.register_chatcommand("craft", {
|
||||
description = S("Show recipe(s) of the pointed node"),
|
||||
func = function(name)
|
||||
local player = get_player_by_name(name)
|
||||
local ppos = player:get_pos()
|
||||
local dir = player:get_look_dir()
|
||||
local ppos = player:get_pos()
|
||||
ppos.y = ppos.y + 1.625
|
||||
|
||||
ppos.y = ppos.y + 1.625
|
||||
local node_name
|
||||
|
||||
for i = 1, 10 do
|
||||
local look_at = vec_add(ppos, vec_mul(dir, i))
|
||||
local node = M.get_node(look_at)
|
||||
local node = core.get_node(look_at)
|
||||
|
||||
if node.name ~= "air" then
|
||||
node_name = node.name
|
||||
@ -1190,7 +1300,7 @@ M.register_chatcommand("craft", {
|
||||
return false, red .. S("No node pointed")
|
||||
end
|
||||
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
reset_data(data)
|
||||
|
||||
local recipes = recipes_cache[node_name]
|
||||
@ -1217,7 +1327,7 @@ M.register_chatcommand("craft", {
|
||||
end
|
||||
|
||||
data.query_item = node_name
|
||||
data.recipes = recipes
|
||||
data.recipes = recipes
|
||||
|
||||
return true, show_fs(player, name)
|
||||
end,
|
||||
@ -1227,7 +1337,7 @@ function craftguide.show(name, item, show_usages)
|
||||
local func = "craftguide." .. __func() .. "(): "
|
||||
assert(is_str(name), func .. "player name missing")
|
||||
|
||||
local data = player_data[name]
|
||||
local data = pdata[name]
|
||||
local player = get_player_by_name(name)
|
||||
local query_item = data.query_item
|
||||
|
||||
@ -1241,26 +1351,3 @@ function craftguide.show(name, item, show_usages)
|
||||
|
||||
show_fs(player, name)
|
||||
end
|
||||
|
||||
--[[ Custom recipes (>3x3) test code
|
||||
|
||||
M.register_craftitem(":secretstuff:custom_recipe_test", {
|
||||
description = "Custom Recipe Test",
|
||||
})
|
||||
|
||||
local cr = {}
|
||||
for x = 1, 6 do
|
||||
cr[x] = {}
|
||||
for i = 1, 10 - x do
|
||||
cr[x][i] = {}
|
||||
for j = 1, 10 - x do
|
||||
cr[x][i][j] = "group:wood"
|
||||
end
|
||||
end
|
||||
|
||||
M.register_craft({
|
||||
output = "secretstuff:custom_recipe_test",
|
||||
recipe = cr[x]
|
||||
})
|
||||
end
|
||||
]]
|
||||
|
Reference in New Issue
Block a user