2019-02-21 01:08:37 +01:00
|
|
|
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 = {}
|
2018-11-11 22:24:48 +01:00
|
|
|
|
2019-09-09 23:57:37 +02:00
|
|
|
local toolrepair
|
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
local progressive_mode = core.settings:get_bool("craftguide_progressive_mode")
|
|
|
|
local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
|
2018-11-11 22:24:48 +01:00
|
|
|
|
2019-09-06 14:48:44 +02:00
|
|
|
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
|
2019-09-09 23:57:37 +02:00
|
|
|
local reg_tools = core.registered_tools
|
2019-08-29 15:20:19 +02:00
|
|
|
local reg_items = core.registered_items
|
2019-08-30 17:05:03 +02:00
|
|
|
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
|
|
|
|
local on_joinplayer = core.register_on_joinplayer
|
|
|
|
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-08-30 18:50:55 +02:00
|
|
|
local on_mods_loaded = core.register_on_mods_loaded
|
|
|
|
local on_leaveplayer = core.register_on_leaveplayer
|
2019-08-29 15:20:19 +02:00
|
|
|
local serialize, deserialize = core.serialize, core.deserialize
|
2019-08-30 18:50:55 +02:00
|
|
|
local on_receive_fields = core.register_on_player_receive_fields
|
2018-04-08 22:31:16 +02:00
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
local ESC = core.formspec_escape
|
|
|
|
local S = core.get_translator("craftguide")
|
2018-04-08 22:31:16 +02:00
|
|
|
|
2019-09-06 14:48:44 +02:00
|
|
|
local maxn, sort, concat, copy, insert =
|
|
|
|
table.maxn, table.sort, table.concat, table.copy, table.insert
|
2019-03-01 19:02:22 +01:00
|
|
|
|
2019-06-30 16:24:18 +02:00
|
|
|
local fmt, find, gmatch, match, sub, split, upper, lower =
|
2019-03-03 16:20:19 +01:00
|
|
|
string.format, string.find, string.gmatch, string.match,
|
2019-06-30 16:24:18 +02:00
|
|
|
string.sub, string.split, string.upper, string.lower
|
2019-03-01 13:34:45 +01:00
|
|
|
|
|
|
|
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
|
2019-08-30 17:05:03 +02:00
|
|
|
local pairs, next = pairs, next
|
2019-02-21 00:26:14 +01:00
|
|
|
local vec_add, vec_mul = vector.add, vector.multiply
|
2016-12-17 13:15:02 +01:00
|
|
|
|
2019-09-05 20:31:50 +02:00
|
|
|
local ROWS = sfinv_only and 9 or 11
|
|
|
|
local LINES = 5
|
|
|
|
local IPP = ROWS * LINES
|
2019-09-07 12:07:25 +02:00
|
|
|
local WH_LIMIT = 8
|
|
|
|
|
|
|
|
local XOFFSET = sfinv_only and 3.83 or 4.66
|
|
|
|
local YOFFSET = sfinv_only and 6 or 6.6
|
|
|
|
|
|
|
|
local DEV_CORE = sub(core.get_version().string, -3) == "dev"
|
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
craftguide.background = "craftguide_bg_full.png:10"
|
2019-09-07 12:07:25 +02:00
|
|
|
|
|
|
|
local PNG = {
|
|
|
|
bg = "craftguide_bg.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",
|
|
|
|
book = "craftguide_book.png",
|
|
|
|
sign = "craftguide_sign.png",
|
|
|
|
}
|
2018-04-27 22:18:18 +02:00
|
|
|
|
2019-02-21 00:26:14 +01:00
|
|
|
local FMT = {
|
2019-08-30 17:05:03 +02:00
|
|
|
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]",
|
2019-02-21 00:26:14 +01:00
|
|
|
item_image = "item_image[%f,%f;%f,%f;%s]",
|
|
|
|
image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]",
|
|
|
|
item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
|
|
|
|
}
|
2019-01-22 01:59:54 +01:00
|
|
|
|
2019-03-21 17:30:46 +01:00
|
|
|
craftguide.group_stereotypes = {
|
2019-01-08 01:25:22 +01:00
|
|
|
wool = "wool:white",
|
|
|
|
dye = "dye:white",
|
2016-12-03 15:35:04 +01:00
|
|
|
water_bucket = "bucket:bucket_water",
|
2019-01-08 01:25:22 +01:00
|
|
|
vessel = "vessels:glass_bottle",
|
|
|
|
coal = "default:coal_lump",
|
|
|
|
flower = "flowers:dandelion_yellow",
|
2016-12-03 15:35:04 +01:00
|
|
|
mesecon_conductor_craftable = "mesecons:wire_00000000_off",
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2019-03-01 13:34:45 +01:00
|
|
|
local function table_replace(t, val, new)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if v == val then
|
|
|
|
t[k] = new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-12 14:19:54 +01:00
|
|
|
local function is_str(x)
|
|
|
|
return type(x) == "string"
|
|
|
|
end
|
|
|
|
|
2019-03-21 00:21:11 +01:00
|
|
|
local function is_table(x)
|
|
|
|
return type(x) == "table"
|
|
|
|
end
|
|
|
|
|
2019-03-12 14:19:54 +01:00
|
|
|
local function is_func(x)
|
|
|
|
return type(x) == "function"
|
|
|
|
end
|
|
|
|
|
2019-09-09 21:55:51 +02:00
|
|
|
local function is_group(item)
|
|
|
|
return sub(item, 1, 6) == "group:"
|
|
|
|
end
|
|
|
|
|
2019-09-06 14:48:44 +02:00
|
|
|
local craft_types = {}
|
2019-02-21 01:08:37 +01:00
|
|
|
|
2018-12-16 23:20:54 +01:00
|
|
|
function craftguide.register_craft_type(name, def)
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_str(name) or name == "" then
|
|
|
|
return log("error", "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
|
|
|
|
2019-02-21 01:08:37 +01:00
|
|
|
craft_types[name] = def
|
2018-12-16 23:20:54 +01:00
|
|
|
end
|
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
local function clean_name(item)
|
2019-09-16 12:33:28 +02:00
|
|
|
return match(item, "%S*")
|
2019-09-10 11:28:13 +02:00
|
|
|
end
|
|
|
|
|
2018-12-16 23:20:54 +01:00
|
|
|
function craftguide.register_craft(def)
|
2019-09-11 16:28:37 +02:00
|
|
|
def.custom = true
|
|
|
|
def.width = 0
|
|
|
|
local c = 1
|
|
|
|
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_table(def) or not next(def) then
|
|
|
|
return log("error", "craftguide.register_craft(): craft definition missing")
|
|
|
|
end
|
|
|
|
|
2019-08-29 13:48:06 +02:00
|
|
|
if def.result then
|
|
|
|
def.output = def.result -- Backward compatibility
|
|
|
|
end
|
|
|
|
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_str(def.output) or def.output == "" then
|
|
|
|
return log("error", "craftguide.register_craft(): output missing")
|
2019-08-29 13:48:06 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
if not is_table(def.items) then
|
|
|
|
def.items = {}
|
|
|
|
end
|
|
|
|
|
2019-08-29 13:48:06 +02:00
|
|
|
if def.grid then
|
|
|
|
if not is_table(def.grid) then
|
|
|
|
def.grid = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local cp = copy(def.grid)
|
2019-09-10 16:18:27 +02:00
|
|
|
sort(cp, function(a, b)
|
|
|
|
return #a > #b
|
|
|
|
end)
|
|
|
|
|
|
|
|
def.width = #cp[1]
|
2019-08-29 13:48:06 +02:00
|
|
|
|
|
|
|
for i = 1, #def.grid do
|
|
|
|
while #def.grid[i] < def.width do
|
|
|
|
def.grid[i] = def.grid[i] .. " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for symbol in gmatch(concat(def.grid), ".") do
|
|
|
|
def.items[c] = def.key[symbol]
|
|
|
|
c = c + 1
|
|
|
|
end
|
2019-09-11 16:28:37 +02:00
|
|
|
else
|
2019-09-12 12:34:03 +02:00
|
|
|
local items, len = def.items, #def.items
|
|
|
|
def.items = {}
|
2019-09-11 16:28:37 +02:00
|
|
|
|
|
|
|
for i = 1, len do
|
2019-09-12 12:34:03 +02:00
|
|
|
items[i] = items[i]:gsub(",", ", ")
|
|
|
|
local rlen = #split(items[i], ",")
|
2019-09-11 16:28:37 +02:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
if rlen > def.width then
|
|
|
|
def.width = rlen
|
2019-09-11 16:28:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, len do
|
2019-09-12 12:34:03 +02:00
|
|
|
while #split(items[i], ",") < def.width do
|
|
|
|
items[i] = items[i] .. ", "
|
2019-09-11 16:28:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-12 12:34:03 +02:00
|
|
|
for name in gmatch(concat(items, ","), "[%s%w_:]+") do
|
2019-09-11 16:28:37 +02:00
|
|
|
def.items[c] = clean_name(name)
|
|
|
|
c = c + 1
|
|
|
|
end
|
2019-08-29 13:48:06 +02:00
|
|
|
end
|
2018-12-30 21:32:36 +01:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
local output = clean_name(def.output)
|
2019-09-06 14:48:44 +02:00
|
|
|
recipes_cache[output] = recipes_cache[output] or {}
|
|
|
|
insert(recipes_cache[output], def)
|
2018-12-16 23:20:54 +01:00
|
|
|
end
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
local recipe_filters = {}
|
|
|
|
|
2019-03-03 16:20:19 +01:00
|
|
|
function craftguide.add_recipe_filter(name, f)
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_str(name) or name == "" then
|
|
|
|
return log("error", "craftguide.add_recipe_filter(): name missing")
|
|
|
|
elseif not is_func(f) then
|
|
|
|
return log("error", "craftguide.add_recipe_filter(): function missing")
|
|
|
|
end
|
2019-03-03 16:20:19 +01:00
|
|
|
|
|
|
|
recipe_filters[name] = f
|
2019-02-03 16:15:28 +01:00
|
|
|
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)
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_str(name) or name == "" then
|
|
|
|
return log("error", "craftguide.add_search_filter(): name missing")
|
|
|
|
elseif not is_func(f) then
|
|
|
|
return log("error", "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
|
|
|
|
|
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)
|
2019-02-21 00:26:14 +01:00
|
|
|
return split(sub(str, 7), ",")
|
2019-02-10 23:07:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function item_in_recipe(item, recipe)
|
|
|
|
for _, recipe_item in pairs(recipe.items) do
|
|
|
|
if recipe_item == item then
|
|
|
|
return true
|
2019-03-01 13:34:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function groups_item_in_recipe(item, recipe)
|
2019-09-09 18:27:02 +02:00
|
|
|
local def = reg_items[item]
|
|
|
|
local item_groups = def.groups
|
2019-05-08 20:57:45 +02:00
|
|
|
|
2019-03-01 13:34:45 +01:00
|
|
|
for _, recipe_item in pairs(recipe.items) do
|
2019-09-09 21:55:51 +02:00
|
|
|
if is_group(recipe_item) then
|
2019-02-10 23:07:28 +01:00
|
|
|
local groups = extract_groups(recipe_item)
|
2019-09-10 16:18:27 +02:00
|
|
|
|
2019-02-10 23:07:28 +01:00
|
|
|
if item_has_groups(item_groups, groups) then
|
2019-03-01 13:34:45 +01:00
|
|
|
local usage = copy(recipe)
|
|
|
|
table_replace(usage.items, recipe_item, item)
|
|
|
|
return usage
|
2019-02-10 23:07:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-30 17:05:03 +02:00
|
|
|
local function get_filtered_items(player, data)
|
2019-02-07 16:08:50 +01:00
|
|
|
local items, c = {}, 0
|
2019-08-30 17:05:03 +02:00
|
|
|
local known = 0
|
2019-02-03 16:15:28 +01:00
|
|
|
|
|
|
|
for i = 1, #init_items do
|
|
|
|
local item = init_items[i]
|
|
|
|
local recipes = recipes_cache[item]
|
2019-02-20 18:11:59 +01:00
|
|
|
local usages = usages_cache[item]
|
2019-02-03 16:15:28 +01:00
|
|
|
|
2019-08-30 17:05:03 +02:00
|
|
|
recipes = #apply_recipe_filters(recipes or {}, player)
|
|
|
|
usages = #apply_recipe_filters(usages or {}, player)
|
|
|
|
|
|
|
|
if recipes > 0 or usages > 0 then
|
2019-09-11 12:25:28 +02:00
|
|
|
c = c + 1
|
|
|
|
items[c] = item
|
|
|
|
|
|
|
|
if data then
|
2019-08-30 17:05:03 +02:00
|
|
|
known = known + recipes + usages
|
|
|
|
end
|
2019-02-03 16:15:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-30 17:05:03 +02:00
|
|
|
if data then
|
|
|
|
data.known_recipes = known
|
|
|
|
end
|
2019-09-11 12:25:28 +02:00
|
|
|
|
|
|
|
return items
|
2019-02-03 16:15:28 +01:00
|
|
|
end
|
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local function get_usages(item)
|
|
|
|
local usages, c = {}, 0
|
2019-02-05 14:45:57 +01:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
for _, recipes in pairs(recipes_cache) do
|
|
|
|
for i = 1, #recipes do
|
|
|
|
local recipe = recipes[i]
|
2019-09-10 16:18:27 +02:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
if item_in_recipe(item, recipe) then
|
|
|
|
c = c + 1
|
|
|
|
usages[c] = recipe
|
2019-09-06 14:48:44 +02:00
|
|
|
else
|
2019-09-09 18:27:02 +02:00
|
|
|
recipe = groups_item_in_recipe(item, recipe)
|
|
|
|
if recipe then
|
|
|
|
c = c + 1
|
|
|
|
usages[c] = recipe
|
|
|
|
end
|
2019-01-08 01:25:22 +01:00
|
|
|
end
|
|
|
|
end
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if fuel_cache[item] then
|
|
|
|
usages[#usages + 1] = {
|
|
|
|
type = "fuel",
|
|
|
|
width = 1,
|
|
|
|
items = {item},
|
|
|
|
replacements = fuel_cache[item].replacements,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return usages
|
2019-06-23 01:24:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function cache_usages(item)
|
|
|
|
local usages = get_usages(item)
|
|
|
|
if #usages > 0 then
|
2019-09-09 18:27:02 +02:00
|
|
|
usages_cache[item] = table_merge(usages, usages_cache[item] or {})
|
2019-01-22 01:44:36 +01:00
|
|
|
end
|
2019-01-08 01:25:22 +01:00
|
|
|
end
|
|
|
|
|
2019-02-11 00:01:32 +01:00
|
|
|
local function get_recipes(item, data, player)
|
|
|
|
local recipes = recipes_cache[item]
|
2019-02-20 18:11:59 +01:00
|
|
|
local usages = usages_cache[item]
|
2019-02-11 00:01:32 +01:00
|
|
|
|
|
|
|
if recipes then
|
|
|
|
recipes = apply_recipe_filters(recipes, player)
|
|
|
|
end
|
|
|
|
|
|
|
|
local no_recipes = not recipes or #recipes == 0
|
2019-09-09 21:55:51 +02:00
|
|
|
|
2019-02-20 18:11:59 +01:00
|
|
|
if no_recipes and not usages then
|
2019-02-11 00:01:32 +01:00
|
|
|
return
|
2019-02-20 18:11:59 +01:00
|
|
|
elseif usages and no_recipes then
|
2019-02-11 00:01:32 +01:00
|
|
|
data.show_usages = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.show_usages then
|
2019-02-17 15:04:01 +01:00
|
|
|
recipes = apply_recipe_filters(usages_cache[item], player)
|
2019-09-05 14:57:58 +02:00
|
|
|
if recipes and #recipes == 0 then return end
|
2019-02-11 00:01:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return recipes
|
|
|
|
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
|
2019-01-08 01:25:22 +01:00
|
|
|
local group = groups[1]
|
2019-02-17 15:04:01 +01:00
|
|
|
local def_gr = "default:" .. group
|
2019-03-21 17:30:46 +01:00
|
|
|
local stereotypes = craftguide.group_stereotypes
|
|
|
|
local stereotype = stereotypes and stereotypes[group]
|
2019-02-17 15:04:01 +01:00
|
|
|
|
2019-03-21 17:30:46 +01:00
|
|
|
if stereotype then
|
|
|
|
return stereotype
|
2019-02-17 15:04:01 +01:00
|
|
|
elseif reg_items[def_gr] then
|
|
|
|
return def_gr
|
2019-01-08 01:25:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local names = {}
|
2019-01-08 01:25:22 +01:00
|
|
|
for name, def in pairs(reg_items) do
|
|
|
|
if item_has_groups(def.groups, groups) then
|
2019-09-09 18:27:02 +02:00
|
|
|
names[#names + 1] = name
|
2016-02-21 00:42:52 +01:00
|
|
|
end
|
|
|
|
end
|
2018-03-08 17:49:07 +01:00
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
return names
|
2016-02-21 00:42:52 +01:00
|
|
|
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
|
|
|
|
|
2019-09-10 11:28:13 +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
|
2019-02-17 15:04:01 +01:00
|
|
|
local groupstr, c = {}, 0
|
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
for i = 1, #info.groups do
|
2019-02-17 15:04:01 +01:00
|
|
|
c = c + 1
|
2019-09-10 13:41:17 +02:00
|
|
|
groupstr[c] = clr("yellow", info.groups[i])
|
2016-12-03 15:35:04 +01:00
|
|
|
end
|
2018-04-17 13:30:51 +02:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
groupstr = concat(groupstr, ", ")
|
2019-02-11 00:18:10 +01:00
|
|
|
tooltip = S("Any item belonging to the group(s): @1", groupstr)
|
2019-09-10 16:18:27 +02:00
|
|
|
|
|
|
|
return fmt("tooltip[%s;%s]", item, ESC(tooltip))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_desc(def, name)
|
|
|
|
name = name or item
|
|
|
|
return def and def.description or
|
|
|
|
(def and match(name, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
|
|
|
|
S("Unknown Item (@1)", name))
|
|
|
|
end
|
|
|
|
|
|
|
|
tooltip = get_desc(reg_items[item])
|
|
|
|
|
|
|
|
local function add(str)
|
|
|
|
return tooltip .. "\n" .. str
|
2016-12-03 15:35:04 +01:00
|
|
|
end
|
2017-03-21 14:26:58 +01:00
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
if info.cooktime then
|
2019-09-10 13:41:17 +02:00
|
|
|
tooltip = add(S("Cooking time: @1", clr("yellow", info.cooktime)))
|
2016-12-03 15:35:04 +01:00
|
|
|
end
|
2017-03-21 14:26:58 +01:00
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
if info.burntime then
|
2019-09-10 13:41:17 +02:00
|
|
|
tooltip = add(S("Burning time: @1", clr("yellow", info.burntime)))
|
2016-12-10 17:17:05 +01:00
|
|
|
end
|
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
if info.replace then
|
|
|
|
local def = reg_items[info.replace]
|
2019-09-10 13:41:17 +02:00
|
|
|
local desc = clr("yellow", get_desc(def, info.replace))
|
2019-09-10 12:37:39 +02:00
|
|
|
|
|
|
|
if info.cooktime then
|
|
|
|
tooltip = add(S("Replaced by @1 on smelting", desc))
|
|
|
|
elseif info.burntime then
|
|
|
|
tooltip = add(S("Replaced by @1 on burning", desc))
|
|
|
|
else
|
|
|
|
tooltip = add(S("Replaced by @1 on crafting", desc))
|
|
|
|
end
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
if info.repair then
|
|
|
|
tooltip = add(S("Repairable by step of @1", clr("yellow", toolrepair .. "%")))
|
|
|
|
end
|
|
|
|
|
2019-03-21 17:36:00 +01:00
|
|
|
return fmt("tooltip[%s;%s]", item, ESC(tooltip))
|
2016-12-03 15:35:04 +01:00
|
|
|
end
|
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
local function get_output_fs(fs, L)
|
|
|
|
local custom_recipe = craft_types[L.recipe.type]
|
|
|
|
|
|
|
|
if custom_recipe or L.shapeless or L.recipe.type == "cooking" then
|
|
|
|
local icon = custom_recipe and custom_recipe.icon or
|
|
|
|
L.shapeless and "shapeless" or "furnace"
|
|
|
|
|
|
|
|
if not custom_recipe then
|
|
|
|
icon = fmt("craftguide_%s.png^[resize:16x16", icon)
|
|
|
|
end
|
|
|
|
|
|
|
|
local pos_x = L.rightest + L.btn_size + 0.1
|
|
|
|
local pos_y = YOFFSET + (sfinv_only and 0.25 or -0.45)
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt(FMT.image, pos_x, pos_y, 0.5, 0.5, icon)
|
|
|
|
|
|
|
|
local tooltip = custom_recipe and custom_recipe.description or
|
|
|
|
L.shapeless and S("Shapeless") or S("Cooking")
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
|
|
|
|
pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
|
|
|
|
end
|
|
|
|
|
|
|
|
local arrow_X = L.rightest + (L.s_btn_size or 1.1)
|
|
|
|
local output_X = arrow_X + 0.9
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt(FMT.image,
|
|
|
|
arrow_X, YOFFSET + (sfinv_only and 0.9 or 0.2),
|
|
|
|
0.9, 0.7, PNG.arrow)
|
|
|
|
|
|
|
|
if L.recipe.type == "fuel" then
|
|
|
|
fs[#fs + 1] = fmt(FMT.image,
|
|
|
|
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
|
|
|
|
1.1, 1.1, PNG.fire)
|
|
|
|
else
|
|
|
|
local item = L.recipe.output
|
|
|
|
local name = clean_name(item)
|
|
|
|
local burntime = fuel_cache[name] and fuel_cache[name].burntime
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt(FMT.item_image_button,
|
|
|
|
output_X, YOFFSET + (sfinv_only and 0.7 or 0),
|
|
|
|
1.1, 1.1, item, ESC(name), "")
|
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
local repair = repairable(item)
|
2019-09-10 11:28:13 +02:00
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
if burntime or repair then
|
2019-09-10 11:28:13 +02:00
|
|
|
fs[#fs + 1] = get_tooltip(name, {
|
|
|
|
burntime = burntime,
|
2019-09-10 13:41:17 +02:00
|
|
|
repair = repair,
|
2019-09-10 11:28:13 +02:00
|
|
|
})
|
2019-09-10 13:41:17 +02:00
|
|
|
end
|
2019-09-10 11:28:13 +02:00
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
if burntime then
|
|
|
|
fs[#fs + 1] = fmt(FMT.image,
|
|
|
|
output_X + 1, YOFFSET + (sfinv_only and 0.7 or 0.1),
|
|
|
|
0.6, 0.4, PNG.arrow)
|
2019-09-10 11:28:13 +02:00
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
fs[#fs + 1] = fmt(FMT.image,
|
|
|
|
output_X + 1.6, YOFFSET + (sfinv_only and 0.55 or 0),
|
|
|
|
0.6, 0.6, PNG.fire)
|
2019-09-10 11:28:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_recipe_fs(data, fs)
|
2019-01-22 01:44:36 +01:00
|
|
|
local recipe = data.recipes[data.rnum]
|
|
|
|
local width = recipe.width
|
2019-09-09 18:27:02 +02:00
|
|
|
local replacements = recipe.replacements
|
2019-01-22 01:44:36 +01:00
|
|
|
local cooktime, shapeless
|
2018-12-30 21:17:21 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
if recipe.type == "cooking" then
|
|
|
|
cooktime, width = width, 1
|
2019-09-11 16:28:37 +02:00
|
|
|
elseif width == 0 and not recipe.custom then
|
2019-01-22 01:44:36 +01:00
|
|
|
shapeless = true
|
2019-03-13 15:13:37 +01:00
|
|
|
local n = #recipe.items
|
2019-09-09 19:02:09 +02:00
|
|
|
width = (n < 5 and n > 1) and 2 or min(3, max(1, n))
|
2018-03-08 17:06:53 +01:00
|
|
|
end
|
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
local rows = ceil(maxn(recipe.items) / width)
|
2019-01-09 21:11:06 +01:00
|
|
|
local rightest, btn_size, s_btn_size = 0, 1.1
|
2016-12-10 23:15:02 +01:00
|
|
|
|
2019-02-21 16:59:07 +01:00
|
|
|
local btn_lab = data.show_usages and
|
|
|
|
ESC(S("Usage @1 of @2", data.rnum, #data.recipes)) or
|
|
|
|
ESC(S("Recipe @1 of @2", data.rnum, #data.recipes))
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt(FMT.button,
|
2019-09-07 12:07:25 +02:00
|
|
|
XOFFSET + (sfinv_only and 1.98 or 2.7),
|
|
|
|
YOFFSET + (sfinv_only and 1.9 or 1.2),
|
2019-09-06 17:23:27 +02:00
|
|
|
2.2, 1, "alternate", btn_lab)
|
2019-02-21 01:54:15 +01:00
|
|
|
|
2019-09-07 12:07:25 +02:00
|
|
|
if width > WH_LIMIT or rows > WH_LIMIT then
|
2019-02-21 00:26:14 +01:00
|
|
|
fs[#fs + 1] = fmt(FMT.label,
|
2019-09-06 17:23:27 +02:00
|
|
|
sfinv_only and 2 or 3, 7,
|
2019-09-07 12:07:25 +02:00
|
|
|
ESC(S("Recipe's too big to be displayed (@1x@2)", width, rows)))
|
2018-12-19 18:36:09 +01:00
|
|
|
|
|
|
|
return concat(fs)
|
2019-01-08 01:25:22 +01:00
|
|
|
end
|
2016-12-17 18:51:04 +01:00
|
|
|
|
2019-09-05 14:24:11 +02:00
|
|
|
for i = 1, width * rows do
|
|
|
|
local item = recipe.items[i] or ""
|
2019-09-07 12:07:25 +02:00
|
|
|
local X = ceil((i - 1) % width - width) + XOFFSET
|
|
|
|
local Y = ceil(i / width) + YOFFSET - min(2, rows)
|
2018-11-18 20:06:45 +01:00
|
|
|
|
2019-01-08 01:25:22 +01:00
|
|
|
if width > 3 or rows > 3 then
|
2019-09-05 14:24:11 +02:00
|
|
|
local xof = 1 - 4 / width
|
|
|
|
local yof = 1 - 4 / rows
|
|
|
|
local x_y = width > rows and xof or yof
|
|
|
|
|
|
|
|
btn_size = width > rows and
|
|
|
|
(3.5 + (xof * 2)) / width or (3.5 + (yof * 2)) / rows
|
2019-01-09 21:11:06 +01:00
|
|
|
s_btn_size = btn_size
|
2019-09-05 14:24:11 +02:00
|
|
|
|
2019-09-07 12:07:25 +02:00
|
|
|
X = (btn_size * ((i - 1) % width) + XOFFSET -
|
|
|
|
(sfinv_only and 2.83 or (XOFFSET - 2))) * (0.83 - (x_y / 5))
|
2019-09-05 20:31:50 +02:00
|
|
|
Y = (btn_size * floor((i - 1) / width) +
|
2019-09-07 12:07:25 +02:00
|
|
|
(sfinv_only and 5.81 or 6.5) + x_y) * (0.86 - (x_y / 5))
|
2016-12-17 18:51:04 +01:00
|
|
|
end
|
2018-11-17 17:48:47 +01:00
|
|
|
|
2019-01-08 01:25:22 +01:00
|
|
|
if X > rightest then
|
|
|
|
rightest = X
|
|
|
|
end
|
|
|
|
|
|
|
|
local groups
|
2019-05-08 20:57:45 +02:00
|
|
|
|
2019-09-09 21:55:51 +02:00
|
|
|
if is_group(item) then
|
2019-01-08 01:25:22 +01:00
|
|
|
groups = extract_groups(item)
|
2019-09-10 11:28:13 +02:00
|
|
|
local items = groups_to_items(groups)
|
|
|
|
item = items[1] or items
|
2019-01-08 01:25:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local label = groups and "\nG" or ""
|
2019-09-09 18:27:02 +02:00
|
|
|
local replace
|
|
|
|
|
|
|
|
if replacements then
|
|
|
|
for j = 1, #replacements do
|
|
|
|
local replacement = replacements[j]
|
|
|
|
if replacement[1] == item then
|
2019-09-15 22:39:08 +02:00
|
|
|
label = "\nR"
|
2019-09-09 18:27:02 +02:00
|
|
|
replace = replacement[2]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-08 01:25:22 +01:00
|
|
|
|
2019-03-11 02:49:27 +01:00
|
|
|
fs[#fs + 1] = fmt(FMT.item_image_button,
|
2019-09-06 17:23:27 +02:00
|
|
|
X, Y + (sfinv_only and 0.7 or 0),
|
2019-09-16 12:33:28 +02:00
|
|
|
btn_size, btn_size, item, clean_name(item), ESC(label))
|
2019-01-08 01:25:22 +01:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local burntime = fuel_cache[item] and fuel_cache[item].burntime
|
2019-01-22 01:44:36 +01:00
|
|
|
|
2019-09-10 16:18:27 +02:00
|
|
|
local info = {
|
2019-09-11 16:35:56 +02:00
|
|
|
unknown = not reg_items[item],
|
2019-09-10 11:28:13 +02:00
|
|
|
groups = groups,
|
|
|
|
burntime = burntime,
|
|
|
|
cooktime = cooktime,
|
|
|
|
replace = replace,
|
|
|
|
}
|
2019-02-05 14:45:57 +01:00
|
|
|
|
2019-09-10 16:18:27 +02:00
|
|
|
for _, v in pairs(info) do
|
2019-09-10 11:28:13 +02:00
|
|
|
if v then
|
2019-09-10 16:18:27 +02:00
|
|
|
fs[#fs + 1] = get_tooltip(item, info)
|
2019-09-10 11:28:13 +02:00
|
|
|
break
|
|
|
|
end
|
2019-01-22 01:44:36 +01:00
|
|
|
end
|
2018-12-30 20:25:28 +01:00
|
|
|
end
|
2018-11-17 17:48:47 +01:00
|
|
|
|
2019-09-10 11:28:13 +02:00
|
|
|
get_output_fs(fs, {
|
|
|
|
recipe = recipe,
|
|
|
|
shapeless = shapeless,
|
|
|
|
rightest = rightest,
|
|
|
|
btn_size = btn_size,
|
|
|
|
s_btn_size = s_btn_size,
|
|
|
|
})
|
2016-12-10 23:15:02 +01:00
|
|
|
end
|
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
local function make_formspec(name)
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-09-05 20:31:50 +02:00
|
|
|
data.pagemax = max(1, ceil(#data.items / IPP))
|
2016-12-17 21:16:10 +01:00
|
|
|
|
2018-12-19 18:36:09 +01:00
|
|
|
local fs = {}
|
2019-02-05 14:45:57 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
if not sfinv_only then
|
2019-09-10 11:28:13 +02:00
|
|
|
local bg, middle = match(craftguide.background, "(.-):(%d+)")
|
|
|
|
bg = bg or ""
|
|
|
|
middle = middle or "10"
|
|
|
|
|
2019-09-06 17:23:27 +02:00
|
|
|
fs[#fs + 1] = fmt([[
|
2019-09-08 12:22:24 +02:00
|
|
|
size[9.5,8.4]
|
2019-02-05 14:45:57 +01:00
|
|
|
no_prepend[]
|
2019-09-06 17:06:23 +02:00
|
|
|
bgcolor[#00000000;false]
|
2019-09-07 12:07:25 +02:00
|
|
|
background[1,1;1,1;%s;true%s]
|
2019-09-06 17:23:27 +02:00
|
|
|
]],
|
2019-09-10 11:28:13 +02:00
|
|
|
bg, DEV_CORE and ";" .. middle or "")
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
|
|
|
|
2019-09-06 17:23:27 +02:00
|
|
|
fs[#fs + 1] = fmt([[
|
|
|
|
field[0.25,0.2;%f,1;filter;;%s]
|
|
|
|
field_close_on_enter[filter;false]
|
|
|
|
]],
|
|
|
|
sfinv_only and 2.76 or 2.72, ESC(data.filter))
|
|
|
|
|
|
|
|
fs[#fs + 1] = fmt([[
|
|
|
|
image_button[%f,-0.05;0.85,0.85;%s;search;;;false;%s^\[colorize:yellow:255]
|
|
|
|
image_button[%f,-0.05;0.85,0.85;%s;clear;;;false;%s^\[colorize:red:255]
|
|
|
|
]],
|
2019-09-07 12:07:25 +02:00
|
|
|
sfinv_only and 2.6 or 2.54, PNG.search, PNG.search,
|
|
|
|
sfinv_only and 3.3 or 3.25, PNG.clear, PNG.clear)
|
2019-02-06 20:37:43 +01:00
|
|
|
|
2019-09-06 17:23:27 +02:00
|
|
|
fs[#fs + 1] = fmt([[
|
|
|
|
image_button[%f,-0.05;0.8,0.8;%s;prev;;;false;%s^\[colorize:yellow:255]
|
2019-09-16 12:56:04 +02:00
|
|
|
label[%f,%f;%s / %u]
|
2019-09-06 17:23:27 +02:00
|
|
|
image_button[%f,-0.05;0.8,0.8;%s;next;;;false;%s^\[colorize:yellow:255]
|
|
|
|
]],
|
2019-09-07 12:07:25 +02:00
|
|
|
sfinv_only and 5.45 or 6.83, PNG.prev, PNG.prev,
|
2019-09-16 12:56:04 +02:00
|
|
|
sfinv_only and 6.35 or 7.85, 0.06, clr("yellow", data.pagenum), data.pagemax,
|
|
|
|
sfinv_only and 7.2 or 8.75, PNG.next, PNG.next)
|
2019-02-17 15:04:01 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
if #data.items == 0 then
|
2019-02-11 00:30:06 +01:00
|
|
|
local no_item = S("No item to show")
|
2019-09-05 20:31:50 +02:00
|
|
|
local pos = sfinv_only and 3 or 3.8
|
2019-02-11 00:30:06 +01:00
|
|
|
|
|
|
|
if next(recipe_filters) and #init_items > 0 and data.filter == "" then
|
|
|
|
no_item = S("Collect items to reveal more recipes")
|
|
|
|
pos = pos - 1
|
|
|
|
end
|
|
|
|
|
2019-02-21 00:26:14 +01:00
|
|
|
fs[#fs + 1] = fmt(FMT.label, pos, 2, ESC(no_item))
|
2016-12-08 02:31:04 +01:00
|
|
|
end
|
|
|
|
|
2019-09-05 20:31:50 +02:00
|
|
|
local first_item = (data.pagenum - 1) * IPP
|
2019-09-07 12:07:25 +02:00
|
|
|
|
2019-09-05 20:31:50 +02:00
|
|
|
for i = first_item, first_item + IPP - 1 do
|
2019-01-23 02:15:26 +01:00
|
|
|
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
|
|
|
|
2019-09-05 20:31:50 +02:00
|
|
|
local X = i % ROWS
|
|
|
|
local Y = (i % IPP - X) / ROWS + 1
|
2016-08-05 16:57:20 +02:00
|
|
|
|
2019-03-11 02:49:27 +01:00
|
|
|
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s_inv;]",
|
2019-09-06 13:29:51 +02:00
|
|
|
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
|
|
|
|
Y - (Y * 0.1) - 0.1,
|
2019-09-06 17:23:27 +02:00
|
|
|
1, 1, item, item)
|
2016-02-21 00:42:52 +01:00
|
|
|
end
|
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
if data.recipes and #data.recipes > 0 then
|
2019-09-10 11:28:13 +02:00
|
|
|
get_recipe_fs(data, fs)
|
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
|
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
local show_fs = function(player, name)
|
2018-12-17 01:02:19 +01:00
|
|
|
if sfinv_only then
|
2019-01-22 01:44:36 +01:00
|
|
|
sfinv.set_player_inventory_formspec(player)
|
2018-12-17 01:02:19 +01:00
|
|
|
else
|
2019-02-03 16:15:28 +01:00
|
|
|
show_formspec(name, "craftguide", make_formspec(name))
|
2018-12-17 01:02:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-03 16:20:19 +01:00
|
|
|
craftguide.add_search_filter("groups", function(item, groups)
|
|
|
|
local itemdef = reg_items[item]
|
|
|
|
local has_groups = true
|
|
|
|
|
|
|
|
for i = 1, #groups do
|
|
|
|
local group = groups[i]
|
|
|
|
if not itemdef.groups[group] then
|
|
|
|
has_groups = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return has_groups
|
|
|
|
end)
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
local function search(data)
|
2016-12-17 22:24:26 +01:00
|
|
|
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]
|
2018-03-08 17:49:07 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
local opt = "^(.-)%+([%w_]+)=([%w_,]+)"
|
|
|
|
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
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
local filtered_list, c = {}, 0
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
for i = 1, #data.items_raw do
|
|
|
|
local item = data.items_raw[i]
|
2019-03-01 19:02:22 +01:00
|
|
|
local def = reg_items[item]
|
2019-06-30 19:42:32 +02:00
|
|
|
local desc = (def and def.description) and lower(def.description) or ""
|
2019-08-29 15:20:19 +02:00
|
|
|
local search_in = item .. " " .. desc
|
2019-03-03 16:20:19 +01:00
|
|
|
local to_add
|
2016-12-17 22:24:26 +01:00
|
|
|
|
2019-03-04 00:45:30 +01:00
|
|
|
if search_filter then
|
|
|
|
for filter_name, values in pairs(filters) do
|
2019-03-03 16:20:19 +01:00
|
|
|
local func = search_filters[filter_name]
|
2019-03-10 15:57:54 +01:00
|
|
|
to_add = func(item, values) and (search_filter == "" or
|
2019-03-04 00:45:30 +01:00
|
|
|
find(search_in, search_filter, 1, true))
|
2019-03-01 19:02:22 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
to_add = find(search_in, filter, 1, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
if to_add then
|
2019-01-13 01:36:38 +01:00
|
|
|
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
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
if not next(recipe_filters) then
|
2018-04-27 22:55:30 +02:00
|
|
|
-- Cache the results only if searched 2 times
|
2019-01-08 20:28:53 +01:00
|
|
|
if searches[filter] == nil then
|
|
|
|
searches[filter] = false
|
2018-04-27 22:55:30 +02:00
|
|
|
else
|
2019-01-08 20:28:53 +01:00
|
|
|
searches[filter] = filtered_list
|
2018-04-27 22:55:30 +02:00
|
|
|
end
|
2016-12-24 21:17:32 +01:00
|
|
|
end
|
2018-03-08 17:49:07 +01:00
|
|
|
|
2016-12-17 21:16:10 +01:00
|
|
|
data.items = filtered_list
|
2016-02-21 00:42:52 +01:00
|
|
|
end
|
|
|
|
|
2019-02-07 04:59:38 +01:00
|
|
|
local function init_data(name)
|
2019-06-23 01:42:12 +02:00
|
|
|
pdata[name] = {
|
2019-05-08 20:57:45 +02:00
|
|
|
filter = "",
|
|
|
|
pagenum = 1,
|
|
|
|
items = init_items,
|
2019-02-03 16:15:28 +01:00
|
|
|
items_raw = init_items,
|
2019-01-13 01:36:38 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function reset_data(data)
|
2019-01-22 01:44:36 +01:00
|
|
|
data.filter = ""
|
|
|
|
data.pagenum = 1
|
2019-02-06 01:51:09 +01:00
|
|
|
data.rnum = 1
|
2019-01-22 01:44:36 +01:00
|
|
|
data.query_item = nil
|
|
|
|
data.show_usages = nil
|
|
|
|
data.recipes = nil
|
2019-02-03 16:15:28 +01:00
|
|
|
data.items = data.items_raw
|
2019-01-13 01:36:38 +01:00
|
|
|
end
|
|
|
|
|
2019-09-09 19:06:35 +02:00
|
|
|
-- As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not return the replacements,
|
2019-09-09 18:27:02 +02:00
|
|
|
-- we have to override `core.register_craft` and `core.register_alias` and do some reverse engineering.
|
2019-09-10 16:18:27 +02:00
|
|
|
-- See engine's issues #4901 and #8920.
|
2019-09-09 18:27:02 +02:00
|
|
|
|
|
|
|
local old_register_alias = core.register_alias
|
|
|
|
local current_alias = {}
|
|
|
|
|
|
|
|
core.register_alias = function(old, new)
|
|
|
|
old_register_alias(old, new)
|
2019-09-10 16:18:27 +02:00
|
|
|
current_alias = {old, new}
|
2019-02-17 15:04:01 +01:00
|
|
|
end
|
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local old_register_craft = core.register_craft
|
2019-06-23 01:24:26 +02:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
core.register_craft = function(def)
|
|
|
|
old_register_craft(def)
|
2019-06-23 01:24:26 +02:00
|
|
|
|
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
|
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
local output = def.output or (is_str(def.recipe) and def.recipe or "")
|
2019-09-09 18:27:02 +02:00
|
|
|
if output == "" then return end
|
2019-09-10 11:28:13 +02:00
|
|
|
output = {clean_name(output)}
|
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
|
2019-09-11 17:10:46 +02:00
|
|
|
local name = output[i]
|
|
|
|
if name == current_alias[1] then
|
|
|
|
name = current_alias[2]
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
def.items = {}
|
2019-09-09 18:27:02 +02:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
if def.type == "fuel" then
|
|
|
|
fuel_cache[name] = def
|
2019-09-09 18:27:02 +02:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
elseif def.type == "cooking" then
|
|
|
|
def.width = def.cooktime
|
|
|
|
def.cooktime = nil
|
|
|
|
def.items[1] = def.recipe
|
2019-09-09 18:27:02 +02:00
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
elseif def.type == "shapeless" then
|
|
|
|
def.width = 0
|
|
|
|
for j = 1, #def.recipe do
|
|
|
|
def.items[#def.items + 1] = def.recipe[j]
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
|
|
|
else
|
2019-09-11 17:10:46 +02:00
|
|
|
def.width = #def.recipe[1]
|
2019-09-09 18:27:02 +02:00
|
|
|
local c = 1
|
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
for j = 1, #def.recipe do
|
|
|
|
if def.recipe[j] then
|
|
|
|
for h = 1, def.width do
|
|
|
|
local it = def.recipe[j][h]
|
2019-09-09 18:27:02 +02:00
|
|
|
|
|
|
|
if it and it ~= "" then
|
2019-09-11 17:10:46 +02:00
|
|
|
def.items[c] = it
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
c = c + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-11 17:10:46 +02:00
|
|
|
if def.type ~= "fuel" then
|
|
|
|
def.recipe = nil
|
|
|
|
recipes_cache[name] = recipes_cache[name] or {}
|
|
|
|
insert(recipes_cache[name], 1, def)
|
2019-06-23 01:24:26 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-09 18:27:02 +02:00
|
|
|
end
|
2019-06-23 01:24:26 +02:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local function show_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
|
2019-06-23 01:24:26 +02:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local function get_init_items()
|
|
|
|
local c = 1
|
|
|
|
for name, def in pairs(reg_items) do
|
|
|
|
if show_item(def) then
|
|
|
|
cache_usages(name)
|
2019-06-23 01:24:26 +02:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
if recipes_cache[name] or usages_cache[name] then
|
|
|
|
init_items[c] = name
|
|
|
|
c = c + 1
|
|
|
|
end
|
2019-01-13 01:36:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sort(init_items)
|
|
|
|
end
|
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
on_mods_loaded(get_init_items)
|
|
|
|
|
|
|
|
on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
init_data(name)
|
|
|
|
end)
|
|
|
|
|
2019-09-10 16:18:27 +02:00
|
|
|
local function fields(player, _f)
|
2019-01-23 02:15:26 +01:00
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2016-02-21 00:42:52 +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-01-23 02:15:26 +01:00
|
|
|
show_fs(player, name)
|
2019-09-05 14:24:11 +02:00
|
|
|
return true
|
2017-03-21 14:26:58 +01:00
|
|
|
|
2019-09-05 14:57:58 +02:00
|
|
|
elseif _f.alternate then
|
|
|
|
if #data.recipes == 1 then return end
|
2019-01-22 01:44:36 +01:00
|
|
|
local num_next = data.rnum + 1
|
|
|
|
data.rnum = data.recipes[num_next] and num_next or 1
|
2019-09-05 14:57:58 +02:00
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
show_fs(player, name)
|
2019-09-05 14:24:11 +02:00
|
|
|
return true
|
2017-03-21 14:26:58 +01:00
|
|
|
|
2019-09-05 14:57:58 +02:00
|
|
|
elseif (_f.key_enter_field == "filter" or _f.search) and _f.filter ~= "" then
|
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
|
2016-11-30 17:28:56 +01:00
|
|
|
data.pagenum = 1
|
2019-02-03 16:15:28 +01:00
|
|
|
search(data)
|
2019-09-05 14:57:58 +02:00
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
show_fs(player, name)
|
2019-09-05 14:24:11 +02:00
|
|
|
return true
|
2017-03-21 14:26:58 +01:00
|
|
|
|
2019-09-05 14:57:58 +02:00
|
|
|
elseif _f.prev or _f.next then
|
|
|
|
if data.pagemax == 1 then return end
|
|
|
|
data.pagenum = data.pagenum - (_f.prev 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
|
2016-12-13 13:10:11 +01:00
|
|
|
data.pagenum = 1
|
|
|
|
elseif data.pagenum == 0 then
|
2016-12-17 23:03:46 +01:00
|
|
|
data.pagenum = data.pagemax
|
2016-12-13 13:10:11 +01:00
|
|
|
end
|
2018-05-28 18:20:14 +02:00
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
show_fs(player, name)
|
2019-09-05 14:24:11 +02:00
|
|
|
return true
|
2019-01-22 01:44:36 +01:00
|
|
|
else
|
|
|
|
local item
|
2019-09-05 14:57:58 +02:00
|
|
|
for field in pairs(_f) do
|
2019-03-11 02:49:27 +01:00
|
|
|
if find(field, ":") then
|
|
|
|
item = field
|
2019-01-22 01:44:36 +01:00
|
|
|
break
|
2019-01-15 14:38:15 +01:00
|
|
|
end
|
2019-01-22 01:44:36 +01:00
|
|
|
end
|
2019-01-15 14:38:15 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
if not item then
|
|
|
|
return
|
2019-03-11 02:49:27 +01:00
|
|
|
elseif sub(item, -4) == "_inv" then
|
2019-09-09 21:55:51 +02:00
|
|
|
item = sub(item, 1, -5)
|
2019-01-22 01:44:36 +01:00
|
|
|
end
|
2019-01-15 14:38:15 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
if item ~= data.query_item then
|
|
|
|
data.show_usages = nil
|
|
|
|
else
|
|
|
|
data.show_usages = not data.show_usages
|
|
|
|
end
|
2019-01-02 06:35:21 +01:00
|
|
|
|
2019-02-13 14:04:13 +01:00
|
|
|
local recipes = get_recipes(item, data, player)
|
2019-09-05 14:57:58 +02:00
|
|
|
if not recipes then return end
|
2019-02-13 14:04:13 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
data.query_item = item
|
2019-02-13 14:04:13 +01:00
|
|
|
data.recipes = recipes
|
2019-02-06 01:51:09 +01:00
|
|
|
data.rnum = 1
|
2018-05-28 18:20:14 +02:00
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
show_fs(player, name)
|
2019-09-05 14:24:11 +02:00
|
|
|
return true
|
2016-02-21 00:42:52 +01:00
|
|
|
end
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
2016-02-21 00:42:52 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
if sfinv_only then
|
|
|
|
sfinv.register_page("craftguide:craftguide", {
|
2019-02-11 00:18:10 +01:00
|
|
|
title = S("Craft Guide"),
|
2019-01-02 18:57:35 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
get = function(self, player, context)
|
2019-01-23 02:15:26 +01:00
|
|
|
local name = player:get_player_name()
|
|
|
|
local formspec = make_formspec(name)
|
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
return sfinv.make_formspec(player, context, formspec)
|
2018-11-11 22:24:48 +01:00
|
|
|
end,
|
2019-01-02 18:57:35 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
on_enter = function(self, player, context)
|
2019-02-03 16:15:28 +01:00
|
|
|
if next(recipe_filters) then
|
2019-02-07 04:59:38 +01:00
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-13 00:45:06 +01:00
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
data.items_raw = get_filtered_items(player)
|
|
|
|
search(data)
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
|
|
|
end,
|
2019-01-02 18:57:35 +01:00
|
|
|
|
2019-09-10 16:18:27 +02:00
|
|
|
on_player_receive_fields = function(self, player, context, _f)
|
|
|
|
fields(player, _f)
|
2018-11-11 22:24:48 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
2019-09-10 16:18:27 +02:00
|
|
|
on_receive_fields(function(player, formname, _f)
|
2019-01-22 01:44:36 +01:00
|
|
|
if formname == "craftguide" then
|
2019-09-10 16:18:27 +02:00
|
|
|
fields(player, _f)
|
2019-01-22 01:44:36 +01:00
|
|
|
end
|
|
|
|
end)
|
2018-11-11 22:24:48 +01:00
|
|
|
|
2019-01-22 01:44:36 +01:00
|
|
|
local function on_use(user)
|
2019-01-23 02:15:26 +01:00
|
|
|
local name = user:get_player_name()
|
2017-01-30 05:24:03 +01:00
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
if next(recipe_filters) then
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-03 16:15:28 +01:00
|
|
|
data.items_raw = get_filtered_items(user)
|
|
|
|
search(data)
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
2019-01-13 01:36:38 +01:00
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
show_formspec(name, "craftguide", make_formspec(name))
|
2016-02-21 00:42:52 +01:00
|
|
|
end
|
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_craftitem("craftguide:book", {
|
2018-11-11 22:24:48 +01:00
|
|
|
description = S("Crafting Guide"),
|
2019-09-07 12:07:25 +02:00
|
|
|
inventory_image = PNG.book,
|
|
|
|
wield_image = PNG.book,
|
2018-11-11 22:24:48 +01:00
|
|
|
stack_max = 1,
|
|
|
|
groups = {book = 1},
|
|
|
|
on_use = function(itemstack, user)
|
2019-01-22 01:44:36 +01:00
|
|
|
on_use(user)
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
|
|
|
})
|
2016-02-21 00:42:52 +01:00
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_node("craftguide:sign", {
|
2018-11-11 22:24:48 +01:00
|
|
|
description = S("Crafting Guide Sign"),
|
|
|
|
drawtype = "nodebox",
|
2019-09-07 12:07:25 +02:00
|
|
|
tiles = {PNG.sign},
|
|
|
|
inventory_image = PNG.sign,
|
|
|
|
wield_image = PNG.sign,
|
2018-11-11 22:24:48 +01:00
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "wallmounted",
|
|
|
|
sunlight_propagates = true,
|
2019-01-18 15:56:52 +01:00
|
|
|
groups = {oddly_breakable_by_hand = 1, flammable = 3},
|
2018-11-11 22:24:48 +01:00
|
|
|
node_box = {
|
|
|
|
type = "wallmounted",
|
2019-09-06 18:09:54 +02:00
|
|
|
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}
|
2018-11-11 22:24:48 +01:00
|
|
|
},
|
2019-01-02 18:57:35 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
on_construct = function(pos)
|
2019-08-29 15:20:19 +02:00
|
|
|
local meta = core.get_meta(pos)
|
2019-02-21 16:59:07 +01:00
|
|
|
meta:set_string("infotext", "Crafting Guide Sign")
|
2018-11-11 22:24:48 +01:00
|
|
|
end,
|
2019-01-02 18:57:35 +01:00
|
|
|
|
2018-11-11 22:24:48 +01:00
|
|
|
on_rightclick = function(pos, node, user, itemstack)
|
2019-01-22 01:44:36 +01:00
|
|
|
on_use(user)
|
2018-11-11 22:24:48 +01:00
|
|
|
end
|
|
|
|
})
|
2016-12-13 17:30:43 +01:00
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_craft({
|
2018-11-11 22:24:48 +01:00
|
|
|
output = "craftguide:book",
|
2019-09-09 18:27:02 +02:00
|
|
|
type = "shapeless",
|
|
|
|
recipe = {"default:book"}
|
2018-11-11 22:24:48 +01:00
|
|
|
})
|
2017-01-30 05:24:03 +01:00
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_craft({
|
2018-11-11 22:24:48 +01:00
|
|
|
type = "fuel",
|
|
|
|
recipe = "craftguide:book",
|
|
|
|
burntime = 3
|
|
|
|
})
|
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_craft({
|
2018-11-11 22:24:48 +01:00
|
|
|
output = "craftguide:sign",
|
2019-09-09 18:27:02 +02:00
|
|
|
type = "shapeless",
|
|
|
|
recipe = {"default:sign_wall_wood"}
|
2018-11-11 22:24:48 +01:00
|
|
|
})
|
|
|
|
|
2019-08-29 15:20:19 +02:00
|
|
|
core.register_craft({
|
2018-11-11 22:24:48 +01:00
|
|
|
type = "fuel",
|
|
|
|
recipe = "craftguide:sign",
|
|
|
|
burntime = 10
|
|
|
|
})
|
2017-01-30 05:24:03 +01:00
|
|
|
|
2018-12-17 01:02:19 +01:00
|
|
|
if rawget(_G, "sfinv_buttons") then
|
|
|
|
sfinv_buttons.register_button("craftguide", {
|
|
|
|
title = S("Crafting Guide"),
|
|
|
|
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"),
|
2019-09-07 12:07:25 +02:00
|
|
|
image = PNG.book,
|
2018-12-17 01:02:19 +01:00
|
|
|
action = function(player)
|
2019-01-22 01:44:36 +01:00
|
|
|
on_use(player)
|
2018-12-17 01:02:19 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2017-03-19 21:12:49 +01:00
|
|
|
end
|
|
|
|
|
2019-01-23 02:15:26 +01:00
|
|
|
if progressive_mode then
|
2019-06-23 01:42:12 +02:00
|
|
|
local PLAYERS = {}
|
|
|
|
local POLL_FREQ = 0.25
|
2019-09-11 12:44:08 +02:00
|
|
|
local HUD_TIMER_MAX = 1.5
|
2019-06-23 01:42:12 +02:00
|
|
|
|
2019-09-09 18:27:02 +02:00
|
|
|
local function table_diff(t1, t2)
|
|
|
|
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-03-01 19:02:22 +01:00
|
|
|
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
|
2019-03-01 19:02:22 +01:00
|
|
|
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
|
|
|
|
local item_groups = def.groups
|
2019-03-01 19:02:22 +01:00
|
|
|
if item_has_groups(item_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
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
local function recipe_in_inv(recipe, inv_items)
|
|
|
|
for _, item in pairs(recipe.items) do
|
2019-09-05 14:57:58 +02:00
|
|
|
if not item_in_inv(item, inv_items) then return end
|
2019-02-03 16:15:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function progressive_filter(recipes, player)
|
2019-08-30 17:05:03 +02:00
|
|
|
if not recipes then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-03 16:15:28 +01:00
|
|
|
|
2019-02-13 00:45:06 +01:00
|
|
|
if #data.inv_items == 0 then
|
2019-02-03 16:15:28 +01:00
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2019-02-07 16:08:50 +01:00
|
|
|
local filtered, c = {}, 0
|
2019-02-03 16:15:28 +01:00
|
|
|
for i = 1, #recipes do
|
|
|
|
local recipe = recipes[i]
|
2019-02-13 00:45:06 +01:00
|
|
|
if recipe_in_inv(recipe, data.inv_items) then
|
2019-02-07 16:08:50 +01:00
|
|
|
c = c + 1
|
|
|
|
filtered[c] = recipe
|
2019-02-03 16:15:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
end
|
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
local item_lists = {
|
|
|
|
"main",
|
|
|
|
"craft",
|
|
|
|
"craftpreview",
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2019-09-11 12:44:08 +02:00
|
|
|
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
|
|
|
|
2019-08-30 17:05:03 +02:00
|
|
|
local hud_info_bg = player:hud_get(data.hud.bg)
|
2019-09-11 12:44:08 +02:00
|
|
|
local dt = 0.016
|
2019-08-30 17:05:03 +02:00
|
|
|
|
|
|
|
if hud_info_bg.position.y <= 0.9 then
|
|
|
|
data.show_hud = false
|
2019-09-11 12:44:08 +02:00
|
|
|
data.hud_timer = (data.hud_timer or 0) + dt
|
2019-08-30 17:05:03 +02:00
|
|
|
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,
|
2019-09-11 12:44:08 +02:00
|
|
|
y = hud_info.position.y - (dt / 5)
|
2019-08-30 17:05:03 +02:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
player:hud_change(data.hud.text, "text",
|
|
|
|
S("@1 new recipe(s) discovered!", data.discovered))
|
|
|
|
|
|
|
|
elseif data.show_hud == false then
|
2019-09-11 12:44:08 +02:00
|
|
|
if data.hud_timer >= HUD_TIMER_MAX then
|
2019-08-30 17:05:03 +02:00
|
|
|
for _, def in pairs(data.hud) do
|
|
|
|
local hud_info = player:hud_get(def)
|
|
|
|
|
|
|
|
player:hud_change(def, "position", {
|
|
|
|
x = hud_info.position.x,
|
2019-09-11 12:44:08 +02:00
|
|
|
y = hud_info.position.y + (dt / 5)
|
2019-08-30 17:05:03 +02:00
|
|
|
})
|
|
|
|
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()
|
2019-06-23 01:42:12 +02:00
|
|
|
for i = 1, #PLAYERS do
|
|
|
|
local player = PLAYERS[i]
|
2019-02-13 00:45:06 +01:00
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
|
|
|
|
2019-02-13 00:45:06 +01:00
|
|
|
local inv_items = get_inv_items(player)
|
2019-06-23 01:42:12 +02:00
|
|
|
local diff = table_diff(inv_items, data.inv_items)
|
2019-02-13 00:45:06 +01:00
|
|
|
|
|
|
|
if #diff > 0 then
|
|
|
|
data.inv_items = table_merge(diff, data.inv_items)
|
2019-08-30 17:05:03 +02:00
|
|
|
|
|
|
|
local oldknown = data.known_recipes or 0
|
2019-09-11 12:25:28 +02:00
|
|
|
local items = get_filtered_items(player, data)
|
|
|
|
|
2019-08-30 17:05:03 +02:00
|
|
|
data.discovered = data.known_recipes - oldknown
|
|
|
|
|
|
|
|
if data.show_hud == nil and data.discovered > 0 then
|
|
|
|
data.show_hud = true
|
|
|
|
end
|
2019-09-11 12:25:28 +02:00
|
|
|
|
|
|
|
if sfinv_only then
|
|
|
|
data.items_raw = items
|
|
|
|
search(data)
|
|
|
|
sfinv.set_player_inventory_formspec(player)
|
|
|
|
end
|
2019-02-13 00:45:06 +01:00
|
|
|
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()
|
2019-02-13 00:45:06 +01:00
|
|
|
|
2019-09-11 12:44:08 +02:00
|
|
|
globalstep(function()
|
2019-08-30 17:05:03 +02:00
|
|
|
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
|
2019-09-11 12:44:08 +02:00
|
|
|
show_hud_success(player, data)
|
2019-08-30 17:05:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
craftguide.add_recipe_filter("Default progressive filter", progressive_filter)
|
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
on_joinplayer(function(player)
|
2019-06-23 01:42:12 +02:00
|
|
|
PLAYERS = get_players()
|
|
|
|
|
2019-02-03 16:15:28 +01:00
|
|
|
local meta = player:get_meta()
|
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-13 00:45:06 +01:00
|
|
|
|
|
|
|
data.inv_items = deserialize(meta:get_string("inv_items")) or {}
|
2019-08-30 17:05:03 +02:00
|
|
|
data.known_recipes = deserialize(meta:get_string("known_recipes")) or 0
|
|
|
|
|
|
|
|
data.hud = {
|
|
|
|
bg = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
2019-08-30 17:39:03 +02:00
|
|
|
position = {x = 0.78, y = 1},
|
2019-08-30 17:05:03 +02:00
|
|
|
alignment = {x = 1, y = 1},
|
2019-08-30 17:39:03 +02:00
|
|
|
scale = {x = 370, y = 112},
|
2019-09-07 12:07:25 +02:00
|
|
|
text = PNG.bg,
|
2019-08-30 17:05:03 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
book = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
2019-08-30 17:39:03 +02:00
|
|
|
position = {x = 0.79, y = 1.02},
|
2019-08-30 17:05:03 +02:00
|
|
|
alignment = {x = 1, y = 1},
|
|
|
|
scale = {x = 4, y = 4},
|
2019-09-07 12:07:25 +02:00
|
|
|
text = PNG.book,
|
2019-08-30 17:05:03 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
text = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
2019-08-30 17:39:03 +02:00
|
|
|
position = {x = 0.84, y = 1.04},
|
2019-08-30 17:05:03 +02:00
|
|
|
alignment = {x = 1, y = 1},
|
|
|
|
number = 0xFFFFFF,
|
|
|
|
text = "",
|
|
|
|
}),
|
|
|
|
}
|
2019-02-03 16:15:28 +01:00
|
|
|
end)
|
|
|
|
|
2019-08-30 17:39:03 +02:00
|
|
|
local to_save = {
|
|
|
|
"inv_items",
|
|
|
|
"known_recipes",
|
|
|
|
}
|
|
|
|
|
2019-02-03 16:15:28 +01: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-02-13 00:45:06 +01:00
|
|
|
|
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, serialize(data[meta_name]))
|
|
|
|
end
|
2019-02-03 16:15:28 +01:00
|
|
|
end
|
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
on_leaveplayer(function(player)
|
2019-06-23 01:42:12 +02:00
|
|
|
PLAYERS = get_players()
|
|
|
|
save_meta(player)
|
|
|
|
end)
|
2019-02-03 16:15:28 +01:00
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
on_shutdown(function()
|
2019-06-23 01:42:12 +02:00
|
|
|
for i = 1, #PLAYERS do
|
|
|
|
local player = PLAYERS[i]
|
2019-02-13 00:45:06 +01:00
|
|
|
save_meta(player)
|
2019-01-23 02:15:26 +01:00
|
|
|
end
|
|
|
|
end)
|
2019-02-06 20:37:43 +01:00
|
|
|
end
|
2019-02-06 01:51:09 +01:00
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
on_leaveplayer(function(player)
|
2019-03-14 14:11:44 +01:00
|
|
|
local name = player:get_player_name()
|
2019-06-23 01:42:12 +02:00
|
|
|
pdata[name] = nil
|
2019-03-14 14:11:44 +01:00
|
|
|
end)
|
|
|
|
|
2019-08-30 18:50:55 +02:00
|
|
|
register_command("craft", {
|
2019-02-06 20:37:43 +01:00
|
|
|
description = S("Show recipe(s) of the pointed node"),
|
|
|
|
func = function(name)
|
2019-02-11 00:01:32 +01:00
|
|
|
local player = get_player_by_name(name)
|
2019-02-06 20:37:43 +01:00
|
|
|
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
|
|
|
|
2019-02-06 20:37:43 +01: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)
|
2019-02-06 20:37:43 +01:00
|
|
|
|
|
|
|
if node.name ~= "air" then
|
|
|
|
node_name = node.name
|
|
|
|
break
|
2019-02-06 01:51:09 +01:00
|
|
|
end
|
2019-02-06 20:37:43 +01:00
|
|
|
end
|
2019-02-06 01:51:09 +01:00
|
|
|
|
2019-09-10 13:41:17 +02:00
|
|
|
local red = clr("red", "[craftguide] ")
|
2019-02-10 20:37:24 +01:00
|
|
|
|
2019-02-06 20:37:43 +01:00
|
|
|
if not node_name then
|
2019-02-10 20:37:24 +01:00
|
|
|
return false, red .. S("No node pointed")
|
2019-02-06 20:37:43 +01:00
|
|
|
end
|
|
|
|
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-06 20:37:43 +01:00
|
|
|
reset_data(data)
|
|
|
|
|
|
|
|
local recipes = recipes_cache[node_name]
|
2019-02-20 18:11:59 +01:00
|
|
|
local usages = usages_cache[node_name]
|
2019-02-06 20:37:43 +01:00
|
|
|
|
|
|
|
if recipes then
|
|
|
|
recipes = apply_recipe_filters(recipes, player)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not recipes or #recipes == 0 then
|
2019-09-10 13:41:17 +02:00
|
|
|
local ylw = clr("yellow", node_name)
|
2019-02-10 20:37:24 +01:00
|
|
|
local msg = red .. "%s: " .. ylw
|
|
|
|
|
2019-02-20 18:11:59 +01:00
|
|
|
if usages then
|
2019-02-17 15:04:01 +01:00
|
|
|
recipes = usages_cache[node_name]
|
2019-02-06 20:37:43 +01:00
|
|
|
if #recipes > 0 then
|
|
|
|
data.show_usages = true
|
2019-02-06 01:51:09 +01:00
|
|
|
end
|
2019-02-10 20:37:24 +01:00
|
|
|
elseif recipes_cache[node_name] then
|
|
|
|
return false, fmt(msg, S("You don't know a recipe for this node"))
|
2019-02-06 20:37:43 +01:00
|
|
|
else
|
2019-02-10 23:07:28 +01:00
|
|
|
return false, fmt(msg, S("No recipe for this node"))
|
2019-02-06 01:51:09 +01:00
|
|
|
end
|
2019-02-06 20:37:43 +01:00
|
|
|
end
|
2019-02-06 01:51:09 +01:00
|
|
|
|
2019-02-06 20:37:43 +01:00
|
|
|
data.query_item = node_name
|
2019-06-23 01:42:12 +02:00
|
|
|
data.recipes = recipes
|
2019-02-06 01:51:09 +01:00
|
|
|
|
2019-02-06 20:37:43 +01:00
|
|
|
return true, show_fs(player, name)
|
|
|
|
end,
|
|
|
|
})
|
2019-01-23 02:15:26 +01:00
|
|
|
|
2019-02-11 00:01:32 +01:00
|
|
|
function craftguide.show(name, item, show_usages)
|
2019-09-06 14:48:44 +02:00
|
|
|
if not is_str(name) or name == "" then
|
|
|
|
return log("error", "craftguide.show(): player name missing")
|
|
|
|
end
|
2019-02-11 00:01:32 +01:00
|
|
|
|
2019-06-23 01:42:12 +02:00
|
|
|
local data = pdata[name]
|
2019-02-11 00:01:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
data.query_item = item
|
|
|
|
data.show_usages = show_usages
|
|
|
|
data.recipes = get_recipes(item, data, player)
|
|
|
|
|
|
|
|
show_fs(player, name)
|
|
|
|
end
|