i3/init.lua

3351 lines
80 KiB
Lua
Raw Normal View History

2020-12-30 23:21:05 +01:00
i3 = {}
2021-06-25 04:21:23 +02:00
local modpath = core.get_modpath "i3"
2021-05-31 20:48:12 +02:00
local storage = core.get_mod_storage()
local slz, dslz = core.serialize, core.deserialize
local pdata = dslz(storage:get_string "pdata") or {}
2020-12-30 23:21:05 +01:00
-- Caches
local init_items = {}
local recipes_cache = {}
local usages_cache = {}
local fuel_cache = {}
local replacements = {fuel = {}}
local toolrepair
2021-01-16 01:46:26 +01:00
local tabs = {}
2021-06-25 04:21:23 +02:00
local model_aliases = loadfile(modpath .. "/etc/model_aliases.lua")()
local PNG, styles, fs_elements = loadfile(modpath .. "/etc/styles.lua")()
local compress_groups, compressed = loadfile(modpath .. "/etc/compress.lua")()
local group_stereotypes, group_names = loadfile(modpath .. "/etc/groups.lua")()
2021-01-16 01:46:26 +01:00
2020-12-30 23:21:05 +01:00
local progressive_mode = core.settings:get_bool "i3_progressive_mode"
2021-06-25 03:44:38 +02:00
local item_compression = core.settings:get_bool "i3_item_compression"
2021-01-16 04:01:30 +01:00
local damage_enabled = core.settings:get_bool "enable_damage"
2021-01-25 19:28:28 +01:00
2021-01-10 20:56:37 +01:00
local __3darmor, __skinsdb, __awards
2021-05-07 03:16:11 +02:00
local __sfinv, old_sfinv_fn
local __unified_inventory, old_unified_inventory_fn
2020-12-30 23:21:05 +01:00
local http = core.request_http_api()
local singleplayer = core.is_singleplayer()
2021-06-25 03:44:38 +02:00
local log = core.log
local after = core.after
local clr = core.colorize
2020-12-30 23:21:05 +01:00
local reg_items = core.registered_items
local reg_nodes = core.registered_nodes
local reg_craftitems = core.registered_craftitems
2020-12-30 23:21:05 +01:00
local reg_tools = core.registered_tools
local reg_entities = core.registered_entities
local reg_aliases = core.registered_aliases
2021-01-29 01:07:59 +01:00
local check_privs = core.check_player_privs
2021-06-25 04:21:23 +02:00
local translate = core.get_translated_string
2021-01-22 00:42:48 +01:00
local create_inventory = core.create_detached_inventory
2020-12-30 23:21:05 +01:00
local ESC = core.formspec_escape
local S = core.get_translator "i3"
local ES = function(...)
return ESC(S(...))
end
2021-04-05 23:21:10 +02:00
local maxn, sort, concat, copy, insert, remove, indexof =
2020-12-30 23:21:05 +01:00
table.maxn, table.sort, table.concat, table.copy,
2021-04-05 23:21:10 +02:00
table.insert, table.remove, table.indexof
2020-12-30 23:21:05 +01:00
local sprintf, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match,
string.sub, string.split, string.upper, string.lower
2021-04-07 01:17:38 +02:00
local min, max, floor, ceil, random =
math.min, math.max, math.floor, math.ceil, math.random
2021-01-23 01:25:54 +01:00
2021-03-29 03:08:48 +02:00
local pairs, ipairs, next, type, setmetatable, tonum, unpack, select =
pairs, ipairs, next, type, setmetatable, tonumber, unpack, select
2020-12-30 23:21:05 +01:00
2021-05-31 21:35:09 +02:00
local vec_new, vec_add, vec_mul, vec_eq, vec_round =
vector.new, vector.add, vector.multiply, vector.equals, vector.round
2020-12-30 23:21:05 +01:00
local MAX_FAVS = 6
local ITEM_BTN_SIZE = 1.1
2021-01-24 16:08:46 +01:00
local INV_SIZE = 36
local HOTBAR_COUNT = 9
2021-01-22 00:42:48 +01:00
2021-05-31 22:46:02 +02:00
-- Players data interval
local SAVE_INTERVAL = 600
2020-12-30 23:21:05 +01:00
-- Progressive mode
local POLL_FREQ = 0.25
local HUD_TIMER_MAX = 1.5
local MIN_FORMSPEC_VERSION = 4
2021-01-22 00:42:48 +01:00
local BAG_SIZES = {
2021-01-24 02:09:15 +01:00
small = INV_SIZE + 3,
2021-01-30 23:21:20 +01:00
medium = INV_SIZE + 6,
large = INV_SIZE + 9,
2021-01-22 00:42:48 +01:00
}
2021-04-05 23:21:10 +02:00
local SUBCAT = {
2021-04-06 03:49:07 +02:00
"bag",
"armor",
"skins",
"awards",
"waypoints",
2021-04-05 23:21:10 +02:00
}
2020-12-30 23:21:05 +01:00
local function get_lang_code(info)
return info and info.lang_code
end
local function get_formspec_version(info)
return info and info.formspec_version or 1
end
local function outdated(name)
2021-01-22 20:15:41 +01:00
local fs = sprintf("size[5.8,1.3]image[0,0;1,1;%s]label[1,0;%s]button_exit[2.4,0.8;1,1;;OK]",
PNG.book,
"Your Minetest client is outdated.\nGet the latest version on minetest.net to use i3")
2020-12-30 23:21:05 +01:00
2021-06-25 03:44:38 +02:00
core.show_formspec(name, "i3", fs)
2020-12-30 23:21:05 +01:00
end
2021-01-22 19:55:17 +01:00
local old_is_creative_enabled = core.is_creative_enabled
function core.is_creative_enabled(name)
2021-01-29 01:07:59 +01:00
return check_privs(name, {creative = true}) or old_is_creative_enabled(name)
2021-01-22 19:55:17 +01:00
end
2020-12-30 23:21:05 +01:00
local function err(str)
return log("error", str)
end
local function msg(name, str)
2021-06-25 03:44:38 +02:00
return core.chat_send_player(name, sprintf("[i3] %s", str))
2020-12-30 23:21:05 +01:00
end
2021-01-02 12:19:45 +01:00
local function is_num(x)
return type(x) == "number"
end
2020-12-30 23:21:05 +01:00
local function is_str(x)
return type(x) == "string"
end
local function true_str(str)
return is_str(str) and str ~= ""
end
local function is_table(x)
return type(x) == "table"
end
local function is_func(x)
return type(x) == "function"
end
local function is_group(item)
return sub(item, 1, 6) == "group:"
end
local function fmt(elem, ...)
2021-01-13 22:16:46 +01:00
if not fs_elements[elem] then
return sprintf(elem, ...)
end
2020-12-30 23:21:05 +01:00
return sprintf(fs_elements[elem], ...)
end
local function clean_name(item)
2021-06-25 04:46:12 +02:00
if sub(item, 1, 1) == ":" or sub(item, 1, 1) == " " or sub(item, 1, 1) == "_" then
2020-12-30 23:21:05 +01:00
item = sub(item, 2)
end
return item
end
2021-04-30 23:14:32 +02:00
local function round(num, decimal)
local mul = 10 ^ decimal
return floor(num * mul + 0.5) / mul
end
2020-12-30 23:21:05 +01:00
local function array_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
local function table_eq(T1, T2)
local avoid_loops = {}
local function recurse(t1, t2)
if type(t1) ~= type(t2) then return end
if not is_table(t1) then
return t1 == t2
end
if avoid_loops[t1] then
return avoid_loops[t1] == t2
end
avoid_loops[t1] = t2
local t2k, t2kv = {}, {}
for k in pairs(t2) do
if is_table(k) then
insert(t2kv, k)
end
t2k[k] = true
end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
local ok
for i = 1, #t2kv do
local tk = t2kv[i]
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
remove(t2kv, i)
t2k[tk] = nil
ok = true
break
end
end
if not ok then return end
else
if v2 == nil then return end
t2k[k1] = nil
if not recurse(v1, v2) then return end
end
end
if next(t2k) then return end
return true
end
return recurse(T1, T2)
end
local function table_merge(t1, t2, hash)
t1 = t1 or {}
t2 = t2 or {}
if hash then
for k, v in pairs(t2) do
t1[k] = v
end
else
local c = #t1
for i = 1, #t2 do
c = c + 1
t1[c] = t2[i]
end
end
return t1
end
local function table_replace(t, val, new)
for k, v in pairs(t) do
if v == val then
t[k] = new
end
end
end
local craft_types = {}
function i3.register_craft_type(name, def)
if not true_str(name) then
2021-01-16 03:30:12 +01:00
return err "i3.register_craft_type: name missing"
2020-12-30 23:21:05 +01:00
end
if not is_str(def.description) then
def.description = ""
end
craft_types[name] = def
end
function i3.register_craft(def)
local width, c = 0, 0
if true_str(def.url) then
if not http then
2021-01-13 22:16:46 +01:00
return err(fmt([[i3.register_craft(): Unable to reach %s.
2020-12-30 23:21:05 +01:00
No HTTP support for this mod: add it to the `secure.http_mods` or
`secure.trusted_mods` setting.]], def.url))
end
http.fetch({url = def.url}, function(result)
if result.succeeded then
2021-06-25 03:44:38 +02:00
local t = core.parse_json(result.data)
2020-12-30 23:21:05 +01:00
if is_table(t) then
return i3.register_craft(t)
end
end
end)
return
end
if not is_table(def) or not next(def) then
2021-01-16 03:30:12 +01:00
return err "i3.register_craft: craft definition missing"
2020-12-30 23:21:05 +01:00
end
if #def > 1 then
for _, v in pairs(def) do
i3.register_craft(v)
end
return
end
if def.result then
def.output = def.result -- Backward compatibility
def.result = nil
end
if not true_str(def.output) then
2021-01-16 03:30:12 +01:00
return err "i3.register_craft: output missing"
2020-12-30 23:21:05 +01:00
end
if not is_table(def.items) then
def.items = {}
end
if def.grid then
if not is_table(def.grid) then
def.grid = {}
end
if not is_table(def.key) then
def.key = {}
end
local cp = copy(def.grid)
sort(cp, function(a, b)
return #a > #b
end)
width = #cp[1]
for i = 1, #def.grid do
while #def.grid[i] < width do
def.grid[i] = def.grid[i] .. " "
end
end
for symbol in gmatch(concat(def.grid), ".") do
c = c + 1
def.items[c] = def.key[symbol]
end
else
local items, len = def.items, #def.items
def.items = {}
for i = 1, len do
local rlen = #split(items[i], ",")
if rlen > width then
width = rlen
end
end
for i = 1, len do
while #split(items[i], ",") < width do
2021-03-26 23:34:29 +01:00
items[i] = fmt("%s,", items[i])
2020-12-30 23:21:05 +01:00
end
end
for name in gmatch(concat(items, ","), "[%s%w_:]+") do
c = c + 1
2021-03-26 23:34:29 +01:00
def.items[c] = clean_name(name)
2020-12-30 23:21:05 +01:00
end
end
local item = match(def.output, "%S+")
recipes_cache[item] = recipes_cache[item] or {}
def.custom = true
def.width = width
insert(recipes_cache[item], def)
end
local recipe_filters = {}
function i3.add_recipe_filter(name, f)
if not true_str(name) then
2021-01-16 03:30:12 +01:00
return err "i3.add_recipe_filter: name missing"
2020-12-30 23:21:05 +01:00
elseif not is_func(f) then
2021-01-16 03:30:12 +01:00
return err "i3.add_recipe_filter: function missing"
2020-12-30 23:21:05 +01:00
end
recipe_filters[name] = f
end
function i3.set_recipe_filter(name, f)
if not is_str(name) then
2021-01-16 03:30:12 +01:00
return err "i3.set_recipe_filter: name missing"
2020-12-30 23:21:05 +01:00
elseif not is_func(f) then
2021-01-16 03:30:12 +01:00
return err "i3.set_recipe_filter: function missing"
2020-12-30 23:21:05 +01:00
end
recipe_filters = {[name] = f}
end
2021-01-29 02:56:28 +01:00
function i3.delete_recipe_filter(name)
2020-12-30 23:21:05 +01:00
recipe_filters[name] = nil
end
function i3.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
local search_filters = {}
function i3.add_search_filter(name, f)
if not true_str(name) then
2021-01-16 03:30:12 +01:00
return err "i3.add_search_filter: name missing"
2020-12-30 23:21:05 +01:00
elseif not is_func(f) then
2021-01-16 03:30:12 +01:00
return err "i3.add_search_filter: function missing"
2020-12-30 23:21:05 +01:00
end
search_filters[name] = f
end
function i3.remove_search_filter(name)
search_filters[name] = nil
end
function i3.get_search_filters()
return search_filters
end
2021-06-25 17:20:15 +02:00
local function compression_active(data)
return item_compression and not next(recipe_filters) and data.filter == ""
2021-06-25 03:44:38 +02:00
end
2021-06-25 17:20:15 +02:00
local function compressible(item, data)
return compression_active(data) and compress_groups[item]
2021-06-25 03:44:38 +02:00
end
2020-12-30 23:21:05 +01:00
local function weird_desc(str)
return not true_str(str) or find(str, "\n") or not find(str, "%u")
end
local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end
local function snip(str, limit)
2021-01-13 22:16:46 +01:00
return #str > limit and fmt("%s...", sub(str, 1, limit - 3)) or str
2020-12-30 23:21:05 +01:00
end
2021-02-07 01:37:09 +01:00
local function get_desc(item)
2020-12-30 23:21:05 +01:00
if sub(item, 1, 1) == "_" then
item = sub(item, 2)
end
local def = reg_items[item]
2021-04-06 17:30:25 +02:00
if not def then
return S("Unknown Item (@1)", item)
end
2020-12-30 23:21:05 +01:00
2021-04-06 17:30:25 +02:00
local desc = def.description
2020-12-30 23:21:05 +01:00
2021-04-06 17:30:25 +02:00
if true_str(desc) then
2021-04-06 18:09:24 +02:00
desc = desc:trim():match("[^\n]*"):gsub("_", " ")
2020-12-30 23:21:05 +01:00
2021-04-06 17:30:25 +02:00
if not find(desc, "%u") then
desc = toupper(desc)
2020-12-30 23:21:05 +01:00
end
2021-04-06 17:30:25 +02:00
return desc
elseif true_str(item) then
return toupper(match(item, ":(.*)"))
end
2020-12-30 23:21:05 +01:00
end
local function item_has_groups(item_groups, groups)
for i = 1, #groups do
local group = groups[i]
if (item_groups[group] or 0) == 0 then return end
end
return true
end
local function extract_groups(str)
if sub(str, 1, 6) == "group:" then
return split(sub(str, 7), ",")
end
end
local function get_filtered_items(player, data)
local items, known, c = {}, 0, 0
for i = 1, #init_items do
local item = init_items[i]
local recipes = recipes_cache[item]
local usages = usages_cache[item]
recipes = #apply_recipe_filters(recipes or {}, player)
usages = #apply_recipe_filters(usages or {}, player)
if recipes > 0 or usages > 0 then
c = c + 1
items[c] = item
if data then
known = known + recipes + usages
end
end
end
if data then
data.known_recipes = known
end
return items
end
local function get_burntime(item)
2021-06-25 03:44:38 +02:00
return core.get_craft_result{method = "fuel", items = {item}}.time
2020-12-30 23:21:05 +01:00
end
local function cache_fuel(item)
local burntime = get_burntime(item)
if burntime > 0 then
fuel_cache[item] = {
type = "fuel",
items = {item},
burntime = burntime,
replacements = replacements.fuel[item],
}
end
end
local function show_item(def)
return def and def.groups.not_in_creative_inventory ~= 1 and
def.description and def.description ~= ""
end
2021-06-26 04:40:57 +02:00
local function reset_compression(data)
2021-06-25 03:44:38 +02:00
data.alt_items = nil
data.expand = ""
2021-06-26 04:40:57 +02:00
end
2021-06-25 03:44:38 +02:00
2021-06-26 04:40:57 +02:00
local function search(data)
reset_compression(data)
2021-01-16 01:46:26 +01:00
2021-06-26 04:40:57 +02:00
local filter = data.filter
2021-01-16 01:46:26 +01:00
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(opt, 6)) do
if search_filters[filter_name] then
values = split(values, ",")
filters[filter_name] = values
end
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(translate(data.lang_code, def and def.description)) or ""
local search_in = fmt("%s %s", item, desc)
local temp, j, to_add = {}, 1
2021-01-16 01:46:26 +01:00
if search_filter then
for filter_name, values in pairs(filters) do
if values then
local func = search_filters[filter_name]
to_add = (j > 1 and temp[item] or j == 1) and
func(item, values) and (search_filter == "" or
2021-01-16 01:46:26 +01:00
find(search_in, search_filter, 1, true))
if to_add then
temp[item] = true
end
j = j + 1
2021-01-16 01:46:26 +01:00
end
end
else
to_add = find(search_in, filter, 1, true)
end
if to_add then
c = c + 1
filtered_list[c] = item
end
end
2021-06-26 04:40:57 +02:00
data.items = filtered_list
end
local function sort_by_category(data)
reset_compression(data)
2021-06-26 05:29:49 +02:00
local items = data.items_raw
2021-06-26 04:40:57 +02:00
if data.filter ~= "" then
search(data)
2021-06-26 05:29:49 +02:00
items = data.items
2021-06-26 04:40:57 +02:00
end
local new = {}
for i = 1, #items do
local item = items[i]
local to_add = true
if data.current_itab == 2 then
to_add = reg_nodes[item]
elseif data.current_itab == 3 then
to_add = reg_craftitems[item] or reg_tools[item]
end
if to_add then
new[#new + 1] = item
2021-01-16 01:46:26 +01:00
end
end
2021-06-26 04:40:57 +02:00
data.items = new
2021-01-16 01:46:26 +01:00
end
2021-01-27 01:05:07 +01:00
local function get_item_usages(item, recipe, added)
local groups = extract_groups(item)
if groups then
for name, def in pairs(reg_items) do
if not added[name] and show_item(def) and item_has_groups(def.groups, groups) then
local usage = copy(recipe)
table_replace(usage.items, item, name)
usages_cache[name] = usages_cache[name] or {}
insert(usages_cache[name], 1, usage)
added[name] = true
end
end
elseif show_item(reg_items[item]) then
usages_cache[item] = usages_cache[item] or {}
insert(usages_cache[item], 1, recipe)
end
end
2020-12-30 23:21:05 +01:00
local function get_usages(recipe)
local added = {}
for _, item in pairs(recipe.items) do
item = reg_aliases[item] or item
2021-01-27 01:05:07 +01:00
if not added[item] then
get_item_usages(item, recipe, added)
2020-12-30 23:21:05 +01:00
added[item] = true
end
end
end
local function cache_usages(item)
local recipes = recipes_cache[item] or {}
for i = 1, #recipes do
get_usages(recipes[i])
end
if fuel_cache[item] then
usages_cache[item] = table_merge(usages_cache[item] or {}, {fuel_cache[item]})
end
end
local function drop_table(name, drop)
local count_sure = 0
local drop_items = drop.items or {}
local max_items = drop.max_items
for i = 1, #drop_items do
local di = drop_items[i]
local valid_rarity = di.rarity and di.rarity > 1
if di.rarity or not max_items or
(max_items and not di.rarity and count_sure < max_items) then
for j = 1, #di.items do
local dstack = ItemStack(di.items[j])
local dname = dstack:get_name()
local dcount = dstack:get_count()
local empty = dstack:is_empty()
2021-01-22 23:23:43 +01:00
if not empty and (dname ~= name or (dname == name and dcount > 1)) then
2020-12-30 23:21:05 +01:00
local rarity = valid_rarity and di.rarity
i3.register_craft{
type = rarity and "digging_chance" or "digging",
items = {name},
2021-01-13 22:16:46 +01:00
output = fmt("%s %u", dname, dcount),
2020-12-30 23:21:05 +01:00
rarity = rarity,
tools = di.tools,
}
end
end
end
if not di.rarity then
count_sure = count_sure + 1
end
end
end
local function cache_drops(name, drop)
if true_str(drop) then
local dstack = ItemStack(drop)
local dname = dstack:get_name()
local empty = dstack:is_empty()
if not empty and dname ~= name then
i3.register_craft{
type = "digging",
items = {name},
output = drop,
}
end
elseif is_table(drop) then
drop_table(name, drop)
end
end
local function cache_recipes(item)
2021-06-25 03:44:38 +02:00
local recipes = core.get_all_craft_recipes(item)
2020-12-30 23:21:05 +01:00
if replacements[item] then
local _recipes = {}
for k, v in ipairs(recipes or {}) do
_recipes[#recipes + 1 - k] = v
end
local shift = 0
local size_rpl = maxn(replacements[item])
local size_rcp = #_recipes
if size_rpl > size_rcp then
shift = size_rcp - size_rpl
end
for k, v in pairs(replacements[item]) do
k = k + shift
if _recipes[k] then
_recipes[k].replacements = v
end
end
recipes = _recipes
end
if recipes then
recipes_cache[item] = table_merge(recipes, recipes_cache[item] or {})
end
end
2021-01-23 20:54:49 +01:00
local function get_recipes(player, item)
2020-12-30 23:21:05 +01:00
local clean_item = reg_aliases[item] or item
local recipes = recipes_cache[clean_item]
local usages = usages_cache[clean_item]
if recipes then
recipes = apply_recipe_filters(recipes, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then return end
usages = apply_recipe_filters(usages, player)
local no_usages = not usages or #usages == 0
return not no_recipes and recipes or nil,
not no_usages and usages or nil
end
local function groups_to_items(groups, get_all)
if not get_all and #groups == 1 then
local group = groups[1]
2021-06-25 04:21:23 +02:00
local stereotype = group_stereotypes[group]
2020-12-30 23:21:05 +01:00
local def = reg_items[stereotype]
2021-06-25 04:46:12 +02:00
if show_item(def) then
2020-12-30 23:21:05 +01:00
return stereotype
end
end
local names = {}
2021-06-25 04:46:12 +02:00
2020-12-30 23:21:05 +01:00
for name, def in pairs(reg_items) do
if show_item(def) and item_has_groups(def.groups, groups) then
if get_all then
names[#names + 1] = name
else
return name
end
end
end
return get_all and names or ""
end
2021-01-02 12:09:54 +01:00
local function __sort(inv, reverse)
sort(inv, function(a, b)
2021-01-02 12:46:34 +01:00
if not is_str(a) then
2021-01-02 12:09:54 +01:00
a = a:get_name()
end
2021-01-02 12:46:34 +01:00
if not is_str(b) then
2021-01-02 12:09:54 +01:00
b = b:get_name()
end
if reverse then
return a > b
end
return a < b
end)
end
2020-12-30 23:21:05 +01:00
local function sort_itemlist(player, az)
local inv = player:get_inventory()
local list = inv:get_list("main")
local size = inv:get_size("main")
local new_inv, stack_meta = {}, {}
for i = 1, size do
local stack = list[i]
local name = stack:get_name()
local count = stack:get_count()
local empty = stack:is_empty()
local meta = stack:get_meta():to_table()
2021-01-01 20:23:42 +01:00
local wear = stack:get_wear() > 0
2020-12-30 23:21:05 +01:00
if not empty then
2021-01-01 20:23:42 +01:00
if next(meta.fields) or wear then
2020-12-30 23:21:05 +01:00
stack_meta[#stack_meta + 1] = stack
else
2021-01-13 22:16:46 +01:00
new_inv[#new_inv + 1] = fmt("%s %u", name, count)
2020-12-30 23:21:05 +01:00
end
end
end
2021-01-02 12:09:54 +01:00
for i = 1, #stack_meta do
new_inv[#new_inv + 1] = stack_meta[i]
end
2020-12-30 23:21:05 +01:00
if az then
2021-01-02 12:09:54 +01:00
__sort(new_inv)
2020-12-30 23:21:05 +01:00
else
2021-01-02 12:09:54 +01:00
__sort(new_inv, true)
2020-12-30 23:21:05 +01:00
end
inv:set_list("main", new_inv)
end
local function compress_items(player)
local inv = player:get_inventory()
local list = inv:get_list("main")
local size = inv:get_size("main")
2021-01-13 22:24:44 +01:00
local new_inv, _new_inv, special = {}, {}, {}
2020-12-30 23:21:05 +01:00
for i = 1, size do
local stack = list[i]
local name = stack:get_name()
local count = stack:get_count()
2021-01-13 22:24:44 +01:00
local stackmax = stack:get_stack_max()
2020-12-30 23:21:05 +01:00
local empty = stack:is_empty()
local meta = stack:get_meta():to_table()
2021-01-01 20:23:42 +01:00
local wear = stack:get_wear() > 0
2020-12-30 23:21:05 +01:00
if not empty then
2021-01-13 22:24:44 +01:00
if next(meta.fields) or wear or count >= stackmax then
special[#special + 1] = stack
2020-12-30 23:21:05 +01:00
else
new_inv[name] = new_inv[name] or 0
new_inv[name] = new_inv[name] + count
end
end
end
for name, count in pairs(new_inv) do
local stackmax = ItemStack(name):get_stack_max()
local iter = ceil(count / stackmax)
local leftover = count
for _ = 1, iter do
2021-01-13 22:16:46 +01:00
_new_inv[#_new_inv + 1] = fmt("%s %u", name, min(stackmax, leftover))
2020-12-30 23:21:05 +01:00
leftover = leftover - stackmax
end
end
2021-01-13 22:24:44 +01:00
for i = 1, #special do
_new_inv[#_new_inv + 1] = special[i]
2020-12-30 23:21:05 +01:00
end
2021-01-02 12:09:54 +01:00
__sort(_new_inv)
inv:set_list("main", _new_inv)
2020-12-30 23:21:05 +01:00
end
local function get_stack_max(inv, data, is_recipe, rcp)
local list = inv:get_list("main")
local size = inv:get_size("main")
local counts_inv, counts_rcp, counts = {}, {}, {}
local rcp_usg = is_recipe and "recipe" or "usage"
for _, it in pairs(rcp.items) do
counts_rcp[it] = (counts_rcp[it] or 0) + 1
end
data.export_counts[rcp_usg] = {}
data.export_counts[rcp_usg].rcp = counts_rcp
for i = 1, size do
local stack = list[i]
if not stack:is_empty() then
local item = stack:get_name()
local count = stack:get_count()
for name in pairs(counts_rcp) do
if is_group(name) then
local def = reg_items[item]
2020-12-30 23:48:06 +01:00
if def then
local groups = extract_groups(name)
if item_has_groups(def.groups, groups) then
counts_inv[name] = (counts_inv[name] or 0) + count
end
2020-12-30 23:21:05 +01:00
end
end
end
counts_inv[item] = (counts_inv[item] or 0) + count
end
end
data.export_counts[rcp_usg].inv = counts_inv
for name in pairs(counts_rcp) do
counts[name] = floor((counts_inv[name] or 0) / (counts_rcp[name] or 0))
end
local max_stacks = math.huge
for _, count in pairs(counts) do
if count < max_stacks then
max_stacks = count
end
end
return max_stacks
end
2021-01-22 00:42:48 +01:00
local function spawn_item(player, stack)
local dir = player:get_look_dir()
local ppos = player:get_pos()
ppos.y = ppos.y + 1.625
local look_at = vec_add(ppos, vec_mul(dir, 1))
core.add_item(look_at, stack)
end
2021-04-23 05:17:45 +02:00
local function get_stack(player, stack, str)
2020-12-30 23:21:05 +01:00
local inv = player:get_inventory()
2021-04-23 05:17:45 +02:00
local name = player:get_player_name()
2020-12-30 23:21:05 +01:00
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
2021-04-23 05:17:45 +02:00
msg(name, S("@1 added in your inventory", str))
2020-12-30 23:21:05 +01:00
else
2021-01-22 00:42:48 +01:00
spawn_item(player, stack)
2021-04-23 05:17:45 +02:00
msg(name, S("@1 spawned", str))
2020-12-30 23:21:05 +01:00
end
end
2021-04-23 05:17:45 +02:00
local function craft_stack(player, data, craft_rcp)
2020-12-30 23:21:05 +01:00
local inv = player:get_inventory()
local rcp_usg = craft_rcp and "recipe" or "usage"
local output = craft_rcp and data.recipes[data.rnum].output or data.usages[data.unum].output
2021-04-05 23:21:10 +02:00
output = ItemStack(output)
2020-12-30 23:21:05 +01:00
local stackname, stackcount, stackmax = output:get_name(), output:get_count(), output:get_stack_max()
2021-01-13 22:16:46 +01:00
local scrbar_val = data[fmt("scrbar_%s", craft_rcp and "rcp" or "usg")] or 1
2020-12-30 23:21:05 +01:00
for name, count in pairs(data.export_counts[rcp_usg].rcp) do
local items = {[name] = count}
if is_group(name) then
items = {}
local groups = extract_groups(name)
local item_groups = groups_to_items(groups, true)
local remaining = count
for _, item in ipairs(item_groups) do
for _name, _count in pairs(data.export_counts[rcp_usg].inv) do
if item == _name and remaining > 0 then
local c = min(remaining, _count)
items[item] = c
remaining = remaining - c
end
if remaining == 0 then break end
end
end
end
for k, v in pairs(items) do
2021-01-13 22:16:46 +01:00
inv:remove_item("main", fmt("%s %s", k, v * scrbar_val))
2020-12-30 23:21:05 +01:00
end
end
local count = stackcount * scrbar_val
local desc = get_desc(stackname)
local iter = ceil(count / stackmax)
local leftover = count
for _ = 1, iter do
local c = min(stackmax, leftover)
2021-04-23 05:17:45 +02:00
local str
2020-12-30 23:21:05 +01:00
if c > 1 then
2021-04-23 05:17:45 +02:00
str = clr("#ff0", fmt("%s x %s", c, desc))
2020-12-30 23:21:05 +01:00
else
2021-04-23 05:17:45 +02:00
str = clr("#ff0", fmt("%s", desc))
2020-12-30 23:21:05 +01:00
end
2021-01-13 22:16:46 +01:00
local stack = ItemStack(fmt("%s %s", stackname, c))
2021-04-23 05:17:45 +02:00
get_stack(player, stack, str)
2020-12-30 23:21:05 +01:00
leftover = leftover - stackmax
end
end
local function select_item(player, name, data, _f)
local item
for field in pairs(_f) do
if find(field, ":") then
item = field
break
end
end
2021-06-25 03:44:38 +02:00
if not item then return end
2020-12-30 23:21:05 +01:00
2021-06-25 17:20:15 +02:00
if compressible(item, data) then
2021-06-25 03:44:38 +02:00
local idx
2020-12-30 23:21:05 +01:00
2021-06-25 03:44:38 +02:00
for i = 1, #data.items do
local it = data.items[i]
if it == item then
idx = i
break
end
end
if data.expand ~= "" then
data.alt_items = nil
if item == data.expand then
data.expand = nil
return
end
end
if idx and item ~= data.expand then
data.alt_items = copy(data.items)
data.expand = item
if compress_groups[item] then
local items = copy(compress_groups[item])
insert(items, fmt("_%s", item))
sort(items, function(a, b)
if a:sub(1, 1) == "_" then
a = a:sub(2)
end
return a < b
end)
local i = 1
for _, v in ipairs(items) do
2021-06-25 04:46:12 +02:00
if show_item(reg_items[clean_name(v)]) then
insert(data.alt_items, idx + i, v)
i = i + 1
end
2021-06-25 03:44:38 +02:00
end
end
end
else
if sub(item, 1, 1) == "_" then
item = sub(item, 2)
elseif sub(item, 1, 6) == "group|" then
item = match(item, "([%w:_]+)$")
end
item = reg_aliases[item] or item
if not reg_items[item] then return end
if core.is_creative_enabled(name) then
local stack = ItemStack(item)
local stackmax = stack:get_stack_max()
stack = fmt("%s %s", item, stackmax)
return get_stack(player, stack, clr("#ff0", fmt("%u x %s", stackmax, get_desc(item))))
end
2020-12-30 23:21:05 +01:00
2021-06-25 03:44:38 +02:00
if item == data.query_item then return end
local recipes, usages = get_recipes(player, item)
2020-12-30 23:21:05 +01:00
2021-06-25 03:44:38 +02:00
data.query_item = item
data.recipes = recipes
data.usages = usages
data.rnum = 1
data.unum = 1
data.scrbar_rcp = 1
data.scrbar_usg = 1
data.export_rcp = nil
data.export_usg = nil
end
2020-12-30 23:21:05 +01:00
end
local function repairable(tool)
local def = reg_tools[tool]
return toolrepair and def and def.groups and def.groups.disable_repair ~= 1
end
local function is_fav(favs, query_item)
local fav, i
for j = 1, #favs do
if favs[j] == query_item then
fav = true
i = j
break
end
end
return fav, i
end
2021-04-11 02:43:51 +02:00
local function get_tooltip(item, info, pos)
2020-12-30 23:21:05 +01:00
local tooltip
if info.groups then
sort(info.groups)
tooltip = group_names[concat(info.groups, ",")]
if not tooltip then
local groupstr = {}
for i = 1, #info.groups do
insert(groupstr, clr("#ff0", info.groups[i]))
end
groupstr = concat(groupstr, ", ")
2021-01-24 19:07:15 +01:00
tooltip = S("Any item belonging to the groups: @1", groupstr)
2020-12-30 23:21:05 +01:00
end
else
tooltip = info.meta_desc or get_desc(item)
2020-12-30 23:21:05 +01:00
end
local function add(str)
2021-01-13 22:16:46 +01:00
return fmt("%s\n%s", tooltip, str)
2020-12-30 23:21:05 +01:00
end
if info.cooktime then
tooltip = add(S("Cooking time: @1", clr("#ff0", info.cooktime)))
end
if info.burntime then
tooltip = add(S("Burning time: @1", clr("#ff0", info.burntime)))
end
if info.replace then
for i = 1, #info.replace.items do
local rpl = match(info.replace.items[i], "%S+")
local desc = clr("#ff0", get_desc(rpl))
if info.replace.type == "cooking" then
tooltip = add(S("Replaced by @1 on smelting", desc))
elseif info.replace.type == "fuel" then
tooltip = add(S("Replaced by @1 on burning", desc))
else
tooltip = add(S("Replaced by @1 on crafting", desc))
end
end
end
if info.repair then
tooltip = add(S("Repairable by step of @1", clr("#ff0", toolrepair .. "%")))
end
if info.rarity then
local chance = (1 / max(1, info.rarity)) * 100
tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%")))
end
if info.tools then
local several = #info.tools > 1
local names = several and "\n" or ""
if several then
for i = 1, #info.tools do
2021-01-13 22:16:46 +01:00
names = fmt("%s\t\t- %s\n", names, clr("#ff0", get_desc(info.tools[i])))
2020-12-30 23:21:05 +01:00
end
2021-01-22 23:23:43 +01:00
tooltip = add(S("Only drop if using one of these tools: @1", sub(names, 1, -2)))
2020-12-30 23:21:05 +01:00
else
tooltip = add(S("Only drop if using this tool: @1",
clr("#ff0", get_desc(info.tools[1]))))
end
end
2021-04-11 02:43:51 +02:00
if pos then
return fmt("tooltip", pos.x, pos.y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, ESC(tooltip))
end
2021-01-13 22:16:46 +01:00
return fmt("tooltip[%s;%s]", item, ESC(tooltip))
2020-12-30 23:21:05 +01:00
end
local function get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size)
local custom_recipe = craft_types[rcp.type]
if custom_recipe or shapeless or rcp.type == "cooking" then
2021-01-22 23:23:43 +01:00
local icon = custom_recipe and custom_recipe.icon or shapeless and "shapeless" or "furnace"
2020-12-30 23:21:05 +01:00
if not custom_recipe then
2021-01-16 01:46:26 +01:00
icon = fmt("i3_%s.png^\\[resize:16x16", icon)
2020-12-30 23:21:05 +01:00
end
local pos_x = right + btn_size + 0.42
local pos_y = data.yoffset + 0.9
if sub(icon, 1, 10) == "i3_furnace" then
2021-03-29 03:08:48 +02:00
fs("animated_image", pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180)
2020-12-30 23:21:05 +01:00
else
2021-03-29 03:08:48 +02:00
fs("image", pos_x, pos_y, 0.5, 0.5, icon)
2020-12-30 23:21:05 +01:00
end
local tooltip = custom_recipe and custom_recipe.description or
shapeless and S"Shapeless" or S"Cooking"
2021-03-29 03:08:48 +02:00
fs("tooltip", pos_x, pos_y, 0.5, 0.5, ESC(tooltip))
2020-12-30 23:21:05 +01:00
end
local arrow_X = right + 0.2 + (_btn_size or ITEM_BTN_SIZE)
local X = arrow_X + 1.2
local Y = data.yoffset + 1.4
2021-03-29 03:08:48 +02:00
fs("image", arrow_X, Y + 0.06, 1, 1, PNG.arrow)
2020-12-30 23:21:05 +01:00
if rcp.type == "fuel" then
2021-03-29 03:08:48 +02:00
fs("animated_image", X + 0.05, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180)
2020-12-30 23:21:05 +01:00
else
local item = ItemStack(rcp.output)
local meta = item:get_meta()
2020-12-30 23:21:05 +01:00
local name = item:get_name()
local count = item:get_count()
local bt_s = ITEM_BTN_SIZE * 1.2
local _name = fmt("_%s", name)
2021-04-11 02:43:51 +02:00
local pos
2020-12-30 23:21:05 +01:00
if meta:get_string("color") ~= "" or meta:get_string("palette_index") ~= "" then
local rcp_usg = is_recipe and "rcp" or "usg"
fs(fmt("style_type[list;size=%f]", ITEM_BTN_SIZE))
2021-04-10 04:10:27 +02:00
fs("listcolors[#bababa50;#bababa99]")
fs(fmt("list[detached:i3_output_%s;main;%f,%f;1,1;]", rcp_usg, X + 0.11, Y))
fs("button", X + 0.11, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, _name, "")
2020-12-30 23:21:05 +01:00
2021-06-25 03:44:38 +02:00
local inv = core.get_inventory {
type = "detached",
name = fmt("i3_output_%s", rcp_usg)
}
inv:set_stack("main", 1, item)
2021-04-11 02:43:51 +02:00
pos = {x = X + 0.11, y = Y}
else
fs("image", X, Y - 0.11, bt_s, bt_s, PNG.slot)
2020-12-30 23:21:05 +01:00
fs("item_image_button",
X + 0.11, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE,
fmt("%s %u", name, count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)),
_name, "")
end
2020-12-30 23:21:05 +01:00
local def = reg_items[name]
local unknown = not def or nil
local desc = def and def.description
local weird = name ~= "" and desc and weird_desc(desc) or nil
local burntime = fuel_cache[name] and fuel_cache[name].burntime
local short_desc = meta:get_string("short_description")
local long_desc = meta:get_string("description")
local meta_desc = (short_desc ~= "" and short_desc) or (long_desc ~= "" and long_desc)
2020-12-30 23:21:05 +01:00
local infos = {
unknown = unknown,
weird = weird,
burntime = burntime,
repair = repairable(name),
rarity = rcp.rarity,
tools = rcp.tools,
meta_desc = meta_desc,
2020-12-30 23:21:05 +01:00
}
if next(infos) then
2021-04-11 02:43:51 +02:00
fs(get_tooltip(_name, infos, pos))
2020-12-30 23:21:05 +01:00
end
end
end
local function get_grid_fs(fs, data, rcp, is_recipe)
local width = rcp.width or 1
local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE
local cooktime, shapeless
if rcp.type == "cooking" then
cooktime, width = width, 1
elseif width == 0 and not rcp.custom then
shapeless = true
local n = #rcp.items
width = (n < 5 and n > 1) and 2 or min(3, max(1, n))
end
local rows = ceil(maxn(rcp.items) / width)
local large_recipe = width > 3 or rows > 3
if large_recipe then
fs("style_type[item_image_button;border=true]")
end
for i = 1, width * rows do
local item = rcp.items[i] or ""
item = ItemStack(item)
local meta = item:get_meta()
local name = item:get_name()
local count = item:get_count()
2020-12-30 23:21:05 +01:00
local X = ceil((i - 1) % width - width)
2021-05-01 16:02:35 +02:00
X = X + (X * 0.2) + data.inv_width + 3.9
2020-12-30 23:21:05 +01:00
local Y = ceil(i / width) - min(2, rows)
Y = Y + (Y * 0.15) + data.yoffset + 1.4
if large_recipe then
btn_size = (3 / width) * (3 / rows) + 0.3
_btn_size = btn_size
local xi = (i - 1) % width
local yi = floor((i - 1) / width)
2021-05-01 16:02:35 +02:00
X = btn_size * xi + data.inv_width + 0.3 + (xi * 0.05)
2020-12-30 23:21:05 +01:00
Y = btn_size * yi + data.yoffset + 0.2 + (yi * 0.05)
end
if X > right then
right = X
end
local groups
if is_group(name) then
groups = extract_groups(name)
name = groups_to_items(groups)
2020-12-30 23:21:05 +01:00
end
local label = groups and "\nG" or ""
local replace
for j = 1, #(rcp.replacements or {}) do
local replacement = rcp.replacements[j]
if replacement[1] == name then
replace = replace or {type = rcp.type, items = {}}
local added
for _, v in ipairs(replace.items) do
if replacement[2] == v then
added = true
break
end
end
if not added then
2021-01-13 22:16:46 +01:00
label = fmt("%s%s\nR", label ~= "" and "\n" or "", label)
2020-12-30 23:21:05 +01:00
replace.items[#replace.items + 1] = replacement[2]
end
end
end
if not large_recipe then
2021-03-29 03:08:48 +02:00
fs("image", X, Y, btn_size, btn_size, PNG.slot)
2020-12-30 23:21:05 +01:00
end
local btn_name = groups and fmt("group|%s|%s", groups[1], name) or name
2020-12-30 23:21:05 +01:00
2021-03-29 03:08:48 +02:00
fs("item_image_button", X, Y, btn_size, btn_size,
fmt("%s %u", name, count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)),
2021-03-29 03:08:48 +02:00
btn_name, label)
2020-12-30 23:21:05 +01:00
local def = reg_items[name]
local unknown = not def or nil
unknown = not groups and unknown or nil
2020-12-30 23:21:05 +01:00
local desc = def and def.description
local weird = name ~= "" and desc and weird_desc(desc) or nil
local burntime = fuel_cache[name] and fuel_cache[name].burntime
local short_desc = meta:get_string("short_description")
local long_desc = meta:get_string("description")
2021-04-10 03:45:40 +02:00
local meta_desc = (short_desc ~= "" and short_desc) or (long_desc ~= "" and long_desc) or nil
2020-12-30 23:21:05 +01:00
local infos = {
unknown = unknown,
weird = weird,
groups = groups,
burntime = burntime,
cooktime = cooktime,
replace = replace,
meta_desc = meta_desc,
2020-12-30 23:21:05 +01:00
}
if next(infos) then
fs(get_tooltip(btn_name, infos))
end
end
if large_recipe then
fs("style_type[item_image_button;border=false]")
end
get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size)
end
local function get_rcp_lbl(fs, data, panel, rn, is_recipe)
local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum]
if rcp.custom then
2021-05-01 16:02:35 +02:00
fs("hypertext", data.inv_width + 4.8, data.yoffset + 0.12, 3, 0.6, "custom_rcp",
fmt("<global size=16><right><i>%s</i></right>", ES"Custom recipe"))
end
2020-12-30 23:21:05 +01:00
local lbl = ES("Usage @1 of @2", data.unum, rn)
if is_recipe then
lbl = ES("Recipe @1 of @2", data.rnum, rn)
end
2021-04-07 01:17:38 +02:00
local one = rn == 1
local y = data.yoffset + 3.3
2020-12-30 23:21:05 +01:00
2021-05-01 16:02:35 +02:00
fs("hypertext", data.inv_width + (one and 4.7 or 3.95), y, 3, 0.6, "rcp_num",
2021-04-07 01:17:38 +02:00
fmt("<global size=16><right>%s</right>", lbl))
2020-12-30 23:21:05 +01:00
2021-04-07 01:17:38 +02:00
if not one then
2020-12-30 23:21:05 +01:00
local btn_suffix = is_recipe and "recipe" or "usage"
2021-01-13 22:16:46 +01:00
local prev_name = fmt("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix)
2021-04-07 01:17:38 +02:00
local size = 0.3
2020-12-30 23:21:05 +01:00
2021-05-01 16:02:35 +02:00
fs("image_button", data.inv_width + 7.05, y, size, size, "", prev_name, "")
fs("image_button", data.inv_width + 7.5, y, size, size, "", next_name, "")
2020-12-30 23:21:05 +01:00
end
get_grid_fs(fs, data, rcp, is_recipe)
end
local function get_model_fs(fs, data, def, model_alias)
if model_alias then
if model_alias.drawtype == "entity" then
def = reg_entities[model_alias.name]
local init_props = def.initial_properties
def.textures = init_props and init_props.textures or def.textures
def.mesh = init_props and init_props.mesh or def.mesh
else
def = reg_items[model_alias.name]
end
end
local tiles = def.tiles or def.textures or {}
local t = {}
for _, v in ipairs(tiles) do
local _name
if v.color then
2021-01-02 12:19:45 +01:00
if is_num(v.color) then
2021-01-13 22:16:46 +01:00
local hex = fmt("%02x", v.color)
2020-12-30 23:21:05 +01:00
2021-01-02 12:19:45 +01:00
while #hex < 8 do
2021-04-12 00:34:11 +02:00
hex = "0" .. hex
2021-01-02 12:19:45 +01:00
end
2020-12-30 23:21:05 +01:00
2021-04-12 00:34:11 +02:00
_name = fmt("%s^[multiply:%s", v.name,
fmt("#%s%s", sub(hex, 3), sub(hex, 1, 2)))
2021-01-02 12:19:45 +01:00
else
2021-01-13 22:16:46 +01:00
_name = fmt("%s^[multiply:%s", v.name, v.color)
2021-01-02 12:19:45 +01:00
end
2020-12-30 23:21:05 +01:00
elseif v.animation then
2021-01-13 22:16:46 +01:00
_name = fmt("%s^[verticalframe:%u:0", v.name, v.animation.aspect_h)
2020-12-30 23:21:05 +01:00
end
t[#t + 1] = _name or v.name or v
end
while #t < 6 do
t[#t + 1] = t[#t]
end
2021-05-01 16:02:35 +02:00
fs("model", data.inv_width + 6.6, data.yoffset + 0.05, 1.3, 1.3, "preview",
2021-01-03 21:28:42 +01:00
def.mesh, concat(t, ","), "0,0", "true", "true",
2021-03-29 03:08:48 +02:00
model_alias and model_alias.frames or "")
2020-12-30 23:21:05 +01:00
end
local function get_header(fs, data)
local fav = is_fav(data.favs, data.query_item)
local nfavs = #data.favs
2021-05-01 16:28:18 +02:00
local star_x, star_y, size = data.inv_width + 0.3, data.yoffset + 0.2, 0.4
2020-12-30 23:21:05 +01:00
if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then
2021-01-13 22:16:46 +01:00
local fav_marked = fmt("i3_fav%s.png", fav and "_off" or "")
2020-12-30 23:21:05 +01:00
2021-01-13 22:16:46 +01:00
fs(fmt("style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
2021-03-29 03:08:48 +02:00
fmt("i3_fav%s.png", fav and "" or "_off"), fav_marked, fav_marked))
2021-05-01 16:28:18 +02:00
fs("image_button", star_x, star_y, size, size, "", "fav", "")
2021-03-29 03:08:48 +02:00
fs(fmt("tooltip[fav;%s]", fav and ES"Unmark this item" or ES"Mark this item"))
2020-12-30 23:21:05 +01:00
else
2021-01-13 22:16:46 +01:00
fs(fmt("style[nofav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]",
2021-03-29 03:08:48 +02:00
"i3_fav_off.png", PNG.cancel, PNG.cancel))
2021-05-01 16:28:18 +02:00
fs("image_button", star_x, star_y, size, size, "", "nofav", "")
2021-03-29 03:08:48 +02:00
fs(fmt("tooltip[nofav;%s]", ES"Cannot mark this item. Bookmark limit reached."))
2020-12-30 23:21:05 +01:00
end
2021-05-01 16:28:18 +02:00
fs("image_button", star_x + 0.05, star_y + 0.6, size, size, "", "exit", "")
2021-05-01 00:28:11 +02:00
fs(fmt("tooltip[exit;%s]", ES"Back to item list"))
2021-05-01 00:39:47 +02:00
local desc_lim, name_lim = 34, 35
2021-03-16 18:33:01 +01:00
local desc = translate(data.lang_code, get_desc(data.query_item))
desc = ESC(desc)
2020-12-30 23:21:05 +01:00
local tech_name = data.query_item
2021-05-01 16:02:35 +02:00
local X = data.inv_width + 0.95
2020-12-30 23:21:05 +01:00
local Y1 = data.yoffset + 0.47
local Y2 = Y1 + 0.5
if #desc > desc_lim then
2021-03-29 03:08:48 +02:00
fs("tooltip", X, Y1 - 0.1, 5.7, 0.24, desc)
2020-12-30 23:21:05 +01:00
desc = snip(desc, desc_lim)
end
if #tech_name > name_lim then
2021-03-29 03:08:48 +02:00
fs("tooltip", X, Y2 - 0.1, 5.7, 0.24, tech_name)
2020-12-30 23:21:05 +01:00
tech_name = snip(tech_name, name_lim)
end
2021-04-06 17:30:25 +02:00
fs("style_type[label;font=bold;font_size=20]")
2021-03-29 03:08:48 +02:00
fs("label", X, Y1, desc)
fs("style_type[label;font=mono;font_size=16]")
fs("label", X, Y2, clr("#7bf", tech_name))
fs("style_type[label;font=normal;font_size=16]")
2020-12-30 23:21:05 +01:00
local def = reg_items[data.query_item]
2021-06-25 04:21:23 +02:00
local model_alias = model_aliases[data.query_item]
2020-12-30 23:21:05 +01:00
if def.drawtype == "mesh" or model_alias then
get_model_fs(fs, data, def, model_alias)
else
2021-05-01 16:02:35 +02:00
fs("item_image", data.inv_width + 6.8, data.yoffset + 0.17, 1.1, 1.1, data.query_item)
2020-12-30 23:21:05 +01:00
end
end
2021-01-16 21:56:52 +01:00
local function get_export_fs(fs, data, is_recipe, is_usage, max_stacks_rcp, max_stacks_usg)
2020-12-30 23:21:05 +01:00
local name = is_recipe and "rcp" or "usg"
local show_export = (is_recipe and data.export_rcp) or (is_usage and data.export_usg)
2021-01-13 22:16:46 +01:00
fs(fmt("style[export_%s;fgimg=%s;fgimg_hovered=%s]",
2021-03-29 03:08:48 +02:00
name, fmt("%s", show_export and PNG.export_hover or PNG.export), PNG.export_hover))
2021-05-01 16:02:35 +02:00
fs("image_button", data.inv_width + 7.35, data.yoffset + 0.2, 0.45, 0.45, "", fmt("export_%s", name), "")
2021-03-29 03:08:48 +02:00
fs(fmt("tooltip[export_%s;%s]", name, ES"Quick crafting"))
2020-12-30 23:21:05 +01:00
if not show_export then return end
local craft_max = is_recipe and max_stacks_rcp or max_stacks_usg
local stack_fs = (is_recipe and data.scrbar_rcp) or (is_usage and data.scrbar_usg) or 1
if stack_fs > craft_max then
stack_fs = craft_max
if is_recipe then
data.scrbar_rcp = craft_max
elseif is_usage then
data.scrbar_usg = craft_max
end
end
2021-01-13 22:16:46 +01:00
fs(fmt("style[scrbar_%s;noclip=true]", name),
2021-03-29 03:08:48 +02:00
fmt("scrollbaroptions[min=1;max=%u;smallstep=1]", craft_max))
2021-05-01 16:02:35 +02:00
fs("scrollbar", data.inv_width + 8.1, data.yoffset, 3, 0.35, "horizontal", fmt("scrbar_%s", name), stack_fs)
fs("button", data.inv_width + 8.1, data.yoffset + 0.4, 3, 0.7, fmt("craft_%s", name),
2021-03-29 03:08:48 +02:00
ES("Craft (x@1)", stack_fs))
2020-12-30 23:21:05 +01:00
end
2021-05-01 00:28:11 +02:00
local function get_rcp_extra(player, fs, data, panel, is_recipe, is_usage)
2021-05-01 01:48:51 +02:00
fs("container[0,0.075]")
2020-12-30 23:21:05 +01:00
local rn = panel.rcp and #panel.rcp
if rn then
local rcp_ok = is_recipe and panel.rcp[data.rnum].type == "normal"
local usg_ok = is_usage and panel.rcp[data.unum].type == "normal"
2020-12-30 23:21:05 +01:00
local max_stacks_rcp, max_stacks_usg = 0, 0
local inv = player:get_inventory()
if rcp_ok then
2020-12-30 23:21:05 +01:00
max_stacks_rcp = get_stack_max(inv, data, is_recipe, panel.rcp[data.rnum])
end
if usg_ok then
2020-12-30 23:21:05 +01:00
max_stacks_usg = get_stack_max(inv, data, is_recipe, panel.rcp[data.unum])
end
if is_recipe and max_stacks_rcp == 0 then
data.export_rcp = nil
data.scrbar_rcp = 1
elseif is_usage and max_stacks_usg == 0 then
data.export_usg = nil
data.scrbar_usg = 1
end
if max_stacks_rcp > 0 or max_stacks_usg > 0 then
2021-01-16 21:56:52 +01:00
get_export_fs(fs, data, is_recipe, is_usage, max_stacks_rcp, max_stacks_usg)
2020-12-30 23:21:05 +01:00
end
get_rcp_lbl(fs, data, panel, rn, is_recipe)
else
local lbl = is_recipe and ES"No recipes" or ES"No usages"
2021-05-01 16:02:35 +02:00
fs("button", data.inv_width + 0.1, data.yoffset + (panel.height / 2) - 0.5,
2021-03-29 03:08:48 +02:00
7.8, 1, "no_rcp", lbl)
2020-12-30 23:21:05 +01:00
end
2021-05-01 01:48:51 +02:00
fs("container_end[]")
2020-12-30 23:21:05 +01:00
end
2021-06-26 04:40:57 +02:00
local function get_items_fs(fs, data, full_height)
2021-06-25 17:20:15 +02:00
if compression_active(data) then
2021-06-25 03:44:38 +02:00
local new = {}
for i = 1, #data.items do
local item = data.items[i]
if not compressed[item] then
new[#new + 1] = item
end
end
data.items = new
end
local items = data.alt_items or data.items
2021-06-26 04:40:57 +02:00
local rows, lines = 8, 12
2021-05-01 00:28:11 +02:00
local ipp = rows * lines
2021-05-01 00:39:47 +02:00
local size = 0.85
2021-05-01 00:28:11 +02:00
2021-05-01 16:02:35 +02:00
fs(fmt("box[%f,0.2;4.05,0.6;#bababa25]", data.inv_width + 0.3),
2021-05-01 00:28:11 +02:00
"set_focus[filter]",
2021-05-01 16:02:35 +02:00
fmt("field[%f,0.2;2.95,0.6;filter;;%s]", data.inv_width + 0.35, ESC(data.filter)),
2021-05-01 00:28:11 +02:00
"field_close_on_enter[filter;false]")
2021-06-25 03:44:38 +02:00
fs("image_button", data.inv_width + 3.35, 0.35, 0.3, 0.3, "", "cancel", "")
2021-05-01 16:02:35 +02:00
fs("image_button", data.inv_width + 3.85, 0.32, 0.35, 0.35, "", "search", "")
2021-06-25 03:44:38 +02:00
fs("image_button", data.inv_width + 5.27, 0.3, 0.35, 0.35, "", "prev_page", "")
fs("image_button", data.inv_width + 7.45, 0.3, 0.35, 0.35, "", "next_page", "")
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
data.pagemax = max(1, ceil(#items / ipp))
2021-05-01 00:28:11 +02:00
2021-05-01 16:02:35 +02:00
fs("button", data.inv_width + 5.6, 0.14, 1.88, 0.7, "pagenum",
2021-05-01 00:28:11 +02:00
fmt("%s / %u", clr("#ff0", data.pagenum), data.pagemax))
2021-06-25 03:44:38 +02:00
if #items == 0 then
2021-05-01 00:28:11 +02:00
local lbl = ES"No item to show"
if next(recipe_filters) and #init_items > 0 and data.filter == "" then
lbl = ES"Collect items to reveal more recipes"
end
2021-05-01 16:02:35 +02:00
fs("button", data.inv_width + 0.1, 3, 8, 1, "no_item", lbl)
2021-06-25 03:44:38 +02:00
else
local first_item = (data.pagenum - 1) * ipp
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
for i = first_item, first_item + ipp - 1 do
local item = items[i + 1]
if not item then break end
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
local _compressed = item:sub(1, 1) == "_"
local name = _compressed and item:sub(2) or item
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
local X = i % rows
X = X - (X * 0.045) + data.inv_width + 0.28
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
local Y = round((i % ipp - X) / rows + 1, 0)
2021-06-26 04:40:57 +02:00
Y = Y - (Y * 0.085) + 0.95
2021-05-01 00:28:11 +02:00
2021-06-25 03:44:38 +02:00
fs[#fs + 1] = fmt("item_image_button", X, Y, size, size, name, item, "")
2021-06-25 17:20:15 +02:00
if compressible(item, data) then
2021-06-25 03:44:38 +02:00
local expand = data.expand == name
fs(fmt("tooltip[%s;%s]", item, expand and ES"Click to hide" or ES"Click to expand"))
fs("style_type[label;font=bold;font_size=20]")
fs("label", X + 0.65, Y + 0.7, expand and "-" or "+")
fs("style_type[label;font=normal;font_size=16]")
end
end
2021-05-01 00:28:11 +02:00
end
2021-06-26 04:40:57 +02:00
local _tabs = {"All", "Nodes", "Items"}
local tab_len, tab_hgh = 1.8, 0.5
for i, title in ipairs(_tabs) do
local selected = i == data.current_itab
fs(fmt([[style_type[image_button;fgimg=%s;fgimg_hovered=%s;noclip=true;
font_size=16;textcolor=%s;content_offset=0;sound=i3_tab] ]],
selected and PNG.tab_small_hover or PNG.tab_small,
PNG.tab_small_hover, selected and "#fff" or "#ddd"))
fs("style_type[image_button:hovered;textcolor=#fff]")
fs("image_button", (data.inv_width - 0.65) + (i * (tab_len + 0.1)),
full_height, tab_len, tab_hgh, "", fmt("itab_%u", i), title)
end
2021-05-01 00:28:11 +02:00
end
2020-12-30 23:21:05 +01:00
local function get_favs(fs, data)
2021-05-01 16:02:35 +02:00
fs("label", data.inv_width + 0.4, data.yoffset + 0.4, ES"Bookmarks")
2020-12-30 23:21:05 +01:00
for i = 1, #data.favs do
local item = data.favs[i]
2021-05-01 16:02:35 +02:00
local X = data.inv_width - 0.7 + (i * 1.2)
2020-12-30 23:21:05 +01:00
local Y = data.yoffset + 0.8
if data.query_item == item then
2021-03-29 03:08:48 +02:00
fs("image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.slot)
2020-12-30 23:21:05 +01:00
end
2021-03-29 03:08:48 +02:00
fs("item_image_button", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, "")
2020-12-30 23:21:05 +01:00
end
end
2021-05-01 00:28:11 +02:00
local function get_panels(player, data, fs, full_height)
2020-12-30 23:21:05 +01:00
local _title = {name = "title", height = 1.4}
local _favs = {name = "favs", height = 2.23}
2021-06-26 04:40:57 +02:00
local _items = {name = "items", height = full_height}
2021-05-01 01:48:51 +02:00
local _recipes = {name = "recipes", rcp = data.recipes, height = 4.045}
local _usages = {name = "usages", rcp = data.usages, height = 4.045}
2021-06-26 04:40:57 +02:00
local panels
2021-05-01 00:28:11 +02:00
if data.query_item then
panels = {_title, _recipes, _usages, _favs}
else
2021-06-26 04:40:57 +02:00
panels = {_items}
2021-05-01 00:28:11 +02:00
end
2020-12-30 23:21:05 +01:00
for idx = 1, #panels do
local panel = panels[idx]
data.yoffset = 0
if idx > 1 then
for _idx = idx - 1, 1, -1 do
data.yoffset = data.yoffset + panels[_idx].height + 0.1
end
end
2021-05-01 16:02:35 +02:00
fs("bg9", data.inv_width + 0.1, data.yoffset, 7.9, panel.height, PNG.bg_full, 10)
2020-12-30 23:21:05 +01:00
local is_recipe, is_usage = panel.name == "recipes", panel.name == "usages"
if is_recipe or is_usage then
2021-05-01 00:28:11 +02:00
get_rcp_extra(player, fs, data, panel, is_recipe, is_usage)
elseif panel.name == "items" then
2021-06-26 04:40:57 +02:00
get_items_fs(fs, data, full_height)
2020-12-30 23:21:05 +01:00
elseif panel.name == "title" then
get_header(fs, data)
elseif panel.name == "favs" then
get_favs(fs, data)
end
end
end
2021-04-19 21:14:09 +02:00
local function add_subtitle(fs, name, y, ctn_len, font_size, sep, label)
2021-04-10 04:26:58 +02:00
fs(fmt("style[%s;font=bold;font_size=%u]", name, font_size))
2021-03-29 03:08:48 +02:00
fs("button", 0, y, ctn_len, 0.5, name, ESC(label))
2021-04-19 21:14:09 +02:00
if sep then
fs("image", 0, y + 0.55, ctn_len, 0.035, PNG.bar)
end
2021-01-10 20:56:37 +01:00
end
2021-03-12 23:50:17 +01:00
local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlocked, award_list_nb)
2021-01-13 22:16:46 +01:00
local percent = fmt("%.1f%%", (awards_unlocked * 100) / award_list_nb):gsub(".0", "")
2021-01-10 01:06:40 +01:00
2021-04-19 21:14:09 +02:00
add_subtitle(fs, "awards", yextra, ctn_len, 18, false,
2021-03-22 21:57:02 +01:00
ES("Achievements: @1 of @2 (@3)", awards_unlocked, award_list_nb, percent))
2020-12-30 23:21:05 +01:00
2021-01-12 02:17:15 +01:00
for i = 1, award_list_nb do
local award = award_list[i]
2021-04-19 21:14:09 +02:00
local y = yextra - 0.6 + i + (i * 0.3)
2021-01-11 23:03:53 +01:00
2021-01-11 22:28:00 +01:00
local def, progress = award.def, award.progress
2021-03-12 23:52:11 +01:00
local title, desc = def.title, def.description
title = translate(data.lang_code, title)
desc = translate(data.lang_code, desc):gsub("%.$", "")
2021-01-11 23:03:53 +01:00
2021-01-25 20:17:40 +01:00
local title_lim, _title = 27
2021-03-12 23:52:11 +01:00
local desc_lim, _desc = 39
2021-01-25 20:17:40 +01:00
local icon_size = 1.1
local box_len = ctn_len - icon_size + 0.1
2021-01-11 23:03:53 +01:00
if #title > title_lim then
_title = snip(title, title_lim)
end
if #desc > desc_lim then
_desc = snip(desc, desc_lim)
end
2021-01-11 21:59:11 +01:00
2021-01-11 22:28:00 +01:00
if not award.unlocked and def.secret then
title = ES"Secret award"
desc = ES"Unlock this award to find out what it is"
end
2021-01-10 22:07:36 +01:00
2021-01-11 22:28:00 +01:00
local icon = def.icon or "awards_unknown.png"
2020-12-30 23:21:05 +01:00
2021-01-11 22:28:00 +01:00
if not award.unlocked then
2021-01-13 22:16:46 +01:00
icon = fmt("%s^\\[colorize:#000:180", icon)
2021-01-10 18:03:17 +01:00
end
2021-02-08 19:34:18 +01:00
fs[#fs + 1] = fmt("image", 0, y + 0.01, icon_size, icon_size, icon)
fs[#fs + 1] = "style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]"
fs[#fs + 1] = fmt("box", icon_size + 0.1, y, box_len, icon_size, "")
2021-01-10 22:07:36 +01:00
2021-01-11 22:28:00 +01:00
if progress then
local current, target = progress.current, progress.target
local curr_bar = (current * box_len) / target
2021-01-10 22:07:36 +01:00
2021-02-08 19:34:18 +01:00
fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, box_len, 0.3, "#101010")
fs[#fs + 1] = "style_type[box;colors=#9dc34c80,#9dc34c,#9dc34c,#9dc34c80]"
fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, "")
fs[#fs + 1] = "style_type[label;font_size=14]"
fs[#fs + 1] = fmt("label", icon_size + 0.5, y + 0.97, fmt("%u / %u", current, target))
2021-01-10 20:56:37 +01:00
2021-01-11 22:28:00 +01:00
y = y - 0.14
end
2021-01-03 21:13:28 +01:00
2021-03-12 23:50:17 +01:00
title = _title or title
desc = _desc or desc
2021-02-08 19:34:18 +01:00
fs[#fs + 1] = "style_type[label;font=bold;font_size=17]"
2021-03-12 23:50:17 +01:00
fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.4, title)
2021-02-08 19:34:18 +01:00
fs[#fs + 1] = "style_type[label;font=normal;font_size=15]"
2021-03-12 23:50:17 +01:00
fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.75, clr("#bbbbbb", desc))
2021-02-08 19:34:18 +01:00
fs[#fs + 1] = "style_type[label;font_size=16]"
2020-12-30 23:21:05 +01:00
end
2021-01-11 22:28:00 +01:00
end
2020-12-30 23:21:05 +01:00
2021-06-12 16:44:57 +02:00
local function get_waypoint_fs(fs, data, player, yextra, ctn_len)
2021-04-12 20:28:59 +02:00
fs(fmt("box[0,%f;4.9,0.6;#bababa25]", yextra + 1.1))
fs("label", 0, yextra + 0.85, ES"Waypoint name:")
fs(fmt("field[0.1,%f;4.8,0.6;waypoint_name;;]", yextra + 1.1))
fs("image_button", 5.1, yextra + 1.15, 0.5, 0.5, "", "waypoint_add", "")
2021-04-11 23:23:03 +02:00
fs(fmt("tooltip[waypoint_add;%s]", ES"Add waypoint"))
if #data.waypoints == 0 then return end
for i, v in ipairs(data.waypoints) do
2021-04-19 21:14:09 +02:00
local y = yextra + 1.35 + (i - (i * 0.3))
2021-04-12 05:46:26 +02:00
local icon_size, yi = 0.35, y + 0.12
2021-04-11 23:23:03 +02:00
2021-04-12 00:24:55 +02:00
fs("style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]")
fs("box", 0, y, ctn_len, 0.6, "")
2021-04-11 23:23:03 +02:00
local waypoint_name, lim = v.name, 18
if #v.name > lim then
waypoint_name = snip(waypoint_name, lim)
end
fs("style_type[label;font_size=17]")
2021-04-12 00:34:11 +02:00
local hex = fmt("%02x", v.color)
while #hex < 6 do
hex = "0" .. hex
end
fs("label", 0.15, y + 0.33, clr(fmt("#%s", hex), waypoint_name))
2021-04-11 23:23:03 +02:00
2021-06-12 16:44:57 +02:00
fs("tooltip", 0, y, ctn_len - 2.5, 0.65,
fmt("Name: %s\nPosition:%s", clr("#ff0", v.name),
2021-06-25 03:44:38 +02:00
core.pos_to_string(v.pos, 0):sub(2,-2):gsub("(%-*%d+)", clr("#ff0", " %1"))))
2021-06-12 16:44:57 +02:00
2021-04-19 21:32:29 +02:00
local del = fmt("waypoint_%u_delete", i)
2021-04-11 23:23:03 +02:00
fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", del, PNG.trash, PNG.trash_hover))
2021-04-12 05:46:26 +02:00
fs("image_button", ctn_len - 0.5, yi, icon_size, icon_size, "", del, "")
fs(fmt("tooltip[%s;%s]", del, ES"Remove waypoint"))
2021-04-11 23:23:03 +02:00
2021-04-19 21:32:29 +02:00
local rfs = fmt("waypoint_%u_refresh", i)
2021-04-11 23:23:03 +02:00
fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", rfs, PNG.refresh, PNG.refresh_hover))
2021-04-12 05:46:26 +02:00
fs("image_button", ctn_len - 1, yi, icon_size, icon_size, "", rfs, "")
2021-04-11 23:23:03 +02:00
fs(fmt("tooltip[%s;%s]", rfs, ES"Change color"))
2021-04-19 21:32:29 +02:00
local vsb = fmt("waypoint_%u_hide", i)
2021-04-11 23:23:03 +02:00
fs(fmt("style[%s;fgimg=%s;content_offset=0]", vsb, v.hide and PNG.nonvisible or PNG.visible))
2021-04-12 05:46:26 +02:00
fs("image_button", ctn_len - 1.5, yi, icon_size, icon_size, "", vsb, "")
2021-04-11 23:23:03 +02:00
fs(fmt("tooltip[%s;%s]", vsb, v.hide and ES"Show waypoint" or ES"Hide waypoint"))
2021-06-12 16:44:57 +02:00
if check_privs(player, {teleport = true}) then
2021-04-19 21:32:29 +02:00
local tp = fmt("waypoint_%u_teleport", i)
2021-04-11 23:23:03 +02:00
fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]",
tp, PNG.teleport, PNG.teleport_hover))
2021-04-12 05:46:26 +02:00
fs("image_button", ctn_len - 2, yi, icon_size, icon_size, "", tp, "")
2021-04-11 23:23:03 +02:00
fs(fmt("tooltip[%s;%s]", tp, ES"Teleport to waypoint"))
end
end
fs("style_type[label;font_size=16]")
end
2021-03-22 21:40:05 +01:00
local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
2021-01-11 22:28:00 +01:00
local name = player:get_player_name()
2021-04-19 21:14:09 +02:00
add_subtitle(fs, "player_name", 0, ctn_len, 22, true, ESC(name))
2020-12-30 23:21:05 +01:00
2021-05-09 16:54:09 +02:00
if damage_enabled then
local hp = data.hp or player:get_hp() or 20
local half = ceil((hp / 2) % 1)
local hearts = (hp / 2) + half
local heart_size = 0.35
local heart_x, heart_h = 0.65, yoffset + 0.75
2020-12-30 23:21:05 +01:00
2021-05-09 16:54:09 +02:00
for i = 1, 10 do
fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h,
heart_size, heart_size, PNG.heart_grey)
end
2021-01-16 04:09:45 +01:00
for i = 1, hearts do
2021-03-29 03:08:48 +02:00
fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h,
2021-01-24 03:51:42 +01:00
heart_size, heart_size,
2021-03-29 03:08:48 +02:00
(half == 1 and i == floor(hearts)) and PNG.heart_half or PNG.heart)
2021-01-16 04:09:45 +01:00
end
2021-05-09 16:54:09 +02:00
else
yoffset = yoffset - 0.5
2020-12-30 23:21:05 +01:00
end
2021-03-29 03:08:48 +02:00
fs(fmt("list[current_player;craft;%f,%f;3,3;]", 0, yoffset + 1.45))
fs("image", 3.47, yoffset + 2.69, 0.85, 0.85, PNG.arrow)
fs(fmt("list[current_player;craftpreview;%f,%f;1,1;]", 4.45, yoffset + 2.6),
fmt("list[detached:i3_trash;main;%f,%f;1,1;]", 4.45, yoffset + 3.75))
fs("image", 4.45, yoffset + 3.75, 1, 1, PNG.trash)
2020-12-30 23:21:05 +01:00
2021-05-09 16:54:09 +02:00
local yextra = damage_enabled and 5.5 or 5
2021-01-21 20:50:13 +01:00
2021-04-06 03:49:07 +02:00
for i, title in ipairs(SUBCAT) do
2021-04-05 23:21:10 +02:00
local btn_name = fmt("btn_%s", title)
fs(fmt("style[btn_%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]", title,
2021-04-06 04:14:40 +02:00
data.subcat == i and PNG[fmt("%s_hover", title)] or PNG[title],
2021-04-05 23:21:10 +02:00
PNG[fmt("%s_hover", title)]))
2021-04-06 03:49:07 +02:00
fs("image_button", 0.25 + ((i - 1) * 1.18), yextra - 0.2, 0.5, 0.5, "", btn_name, "")
2021-04-05 23:21:10 +02:00
fs(fmt("tooltip[%s;%s]", btn_name, title:gsub("^%l", upper)))
end
2021-01-10 20:56:37 +01:00
2021-04-06 03:10:01 +02:00
fs("box", 0, yextra + 0.45, ctn_len, 0.045, "#bababa50")
2021-04-06 04:14:40 +02:00
fs("box", (data.subcat - 1) * 1.18, yextra + 0.45, 1, 0.045, "#f9826c")
2021-04-06 03:49:07 +02:00
local function not_installed(modname)
fs("hypertext", 0, yextra + 0.9, ctn_len, 0.6, "not_installed",
2021-04-06 03:49:07 +02:00
fmt("<center><style color=#7bf font=mono>%s</style> not installed</center>", modname))
end
2020-12-30 23:21:05 +01:00
2021-04-06 04:14:40 +02:00
if data.subcat == 1 then
2021-01-22 19:11:11 +01:00
fs(fmt("list[detached:%s_backpack;main;0,%f;1,1;]", ESC(name), yextra + 0.7))
2020-12-30 23:21:05 +01:00
2021-01-23 00:55:37 +01:00
if not data.bag:get_stack("main", 1):is_empty() then
fs("hypertext", 1.2, yextra + 0.89, ctn_len - 1.9, 0.8, "bpk",
2021-03-29 03:08:48 +02:00
ES("The inventory is extended by @1 slots", BAG_SIZES[data.bag_size] - INV_SIZE))
2021-01-23 00:55:37 +01:00
end
2021-04-06 04:14:40 +02:00
elseif data.subcat == 2 then
2021-01-22 19:11:11 +01:00
if __3darmor then
fs(fmt("list[detached:%s_armor;armor;0,%f;3,2;]", ESC(name), yextra + 0.7))
local armor_def = armor.def[name]
2021-01-02 21:15:18 +01:00
2021-03-29 03:08:48 +02:00
fs("label", 3.65, yextra + 1.55, fmt("%s: %s", ES"Level", armor_def.level))
fs("label", 3.65, yextra + 2.05, fmt("%s: %s", ES"Heal", armor_def.heal))
2021-01-22 19:11:11 +01:00
else
2021-04-06 03:49:07 +02:00
not_installed("3d_armor")
2021-01-02 21:15:18 +01:00
end
2021-04-06 04:14:40 +02:00
elseif data.subcat == 3 then
2021-01-22 19:11:11 +01:00
if __skinsdb then
local _skins = skins.get_skinlist_for_player(name)
2021-02-27 19:05:07 +01:00
local sks = {}
2021-01-02 21:15:18 +01:00
2021-01-22 19:11:11 +01:00
for _, skin in ipairs(_skins) do
2021-02-27 19:05:07 +01:00
sks[#sks + 1] = skin.name
2021-01-22 19:11:11 +01:00
end
2021-01-02 21:15:18 +01:00
2021-02-27 19:05:07 +01:00
sks = concat(sks, ","):gsub(";", "")
2021-04-15 17:46:17 +02:00
fs("label", 0, yextra + 0.85, fmt("%s:", ES"Select a skin"))
fs(fmt("dropdown[0,%f;4,0.6;skins;%s;%u;true]", yextra + 1.1, sks, data.skin_id or 1))
2021-01-22 19:11:11 +01:00
else
2021-04-06 03:49:07 +02:00
not_installed("skinsdb")
2021-01-22 19:11:11 +01:00
end
2020-12-30 23:21:05 +01:00
2021-04-06 04:14:40 +02:00
elseif data.subcat == 4 then
2021-04-05 23:21:10 +02:00
if __awards then
2021-04-19 21:14:09 +02:00
yextra = yextra + 0.7
2021-04-05 23:21:10 +02:00
get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlocked, award_list_nb)
else
2021-04-06 03:49:07 +02:00
not_installed("awards")
2021-01-22 19:11:11 +01:00
end
2021-04-06 02:47:37 +02:00
2021-04-06 04:14:40 +02:00
elseif data.subcat == 5 then
2021-06-12 16:44:57 +02:00
get_waypoint_fs(fs, data, player, yextra, ctn_len)
2021-01-11 22:28:00 +01:00
end
end
2021-01-10 20:56:37 +01:00
2021-01-21 19:44:13 +01:00
local function get_tabs_fs(player, data, fs, full_height)
2021-01-16 15:13:07 +01:00
local tab_len, tab_hgh, c, over = 3, 0.5, 0
2021-01-29 02:24:17 +01:00
local _tabs = copy(tabs)
2021-01-16 01:46:26 +01:00
for i, def in ipairs(tabs) do
if def.access and not def.access(player, data) then
2021-01-29 02:24:17 +01:00
remove(_tabs, i)
2021-01-16 01:46:26 +01:00
end
end
2021-01-29 02:24:17 +01:00
local shift = min(3, #_tabs)
for i, def in ipairs(_tabs) do
2021-01-16 01:46:26 +01:00
if not over and c > 2 then
over = true
c = 0
end
local btm = i <= 3
if not btm then
2021-01-29 02:24:17 +01:00
shift = #_tabs - 3
2021-01-16 01:46:26 +01:00
end
2021-01-21 19:28:54 +01:00
local selected = i == data.current_tab
2021-01-28 21:13:29 +01:00
fs(fmt([[style_type[image_button;fgimg=%s;fgimg_hovered=%s;noclip=true;
2021-01-25 19:28:28 +01:00
font_size=16;textcolor=%s;content_offset=0;sound=i3_tab] ]],
2021-01-21 19:28:54 +01:00
selected and (btm and PNG.tab_hover or PNG.tab_hover_top) or (btm and PNG.tab or PNG.tab_top),
2021-01-22 23:23:43 +01:00
btm and PNG.tab_hover or PNG.tab_hover_top, selected and "#fff" or "#ddd"))
2021-01-16 01:46:26 +01:00
2021-05-01 16:02:35 +02:00
local X = (data.inv_width / 2) + (c * (tab_len + 0.1)) - ((tab_len + 0.05) * (shift / 2))
2021-01-16 15:13:07 +01:00
local Y = btm and full_height or -tab_hgh
2021-01-16 01:46:26 +01:00
2021-01-21 19:28:54 +01:00
fs("style_type[image_button:hovered;textcolor=#fff]")
2021-04-06 03:49:07 +02:00
fs("image_button", X, Y, tab_len, tab_hgh, "", fmt("tab_%s", def.name), ESC(def.description))
2021-01-15 01:33:24 +01:00
2021-01-16 01:46:26 +01:00
if def.image and def.image ~= "" then
local desc = translate(data.lang_code, def.description)
2021-03-29 03:08:48 +02:00
fs("style_type[image;noclip=true]")
fs("image", X + (tab_len / 2) - ((#desc * 0.1) / 2) - 0.55,
Y + 0.05, 0.35, 0.35, fmt("%s^\\[resize:16x16", def.image))
2021-01-16 01:46:26 +01:00
end
c = c + 1
2021-01-11 22:28:00 +01:00
end
2021-01-21 19:44:13 +01:00
end
2021-01-10 20:56:37 +01:00
2021-01-21 19:44:13 +01:00
local function get_debug_grid(data, fs, full_height)
2021-01-16 01:46:26 +01:00
local spacing = 0.2
2021-01-21 19:44:13 +01:00
2021-05-01 16:02:35 +02:00
for x = 0, data.inv_width + 8, spacing do
2021-03-29 03:08:48 +02:00
fs("box", x, 0, 0.01, full_height, "#ff0")
2021-01-16 01:46:26 +01:00
end
2021-01-21 19:44:13 +01:00
2021-01-16 01:46:26 +01:00
for y = 0, full_height, spacing do
2021-05-01 16:02:35 +02:00
fs("box", 0, y, data.inv_width + 8, 0.01, "#ff0")
2021-01-16 01:46:26 +01:00
end
2021-01-10 20:56:37 +01:00
2021-05-01 16:02:35 +02:00
fs("box", data.inv_width / 2, 0, 0.01, full_height, "#f00")
fs("box", 0, full_height / 2, data.inv_width, 0.01, "#f00")
2021-01-21 19:44:13 +01:00
end
2021-01-16 01:46:26 +01:00
2021-01-21 19:44:13 +01:00
local function make_fs(player, data)
--local start = os.clock()
2021-01-21 19:55:23 +01:00
2021-01-21 19:44:13 +01:00
local fs = setmetatable({}, {
__call = function(t, ...)
2021-03-29 03:08:48 +02:00
local args = {...}
local elem = fs_elements[args[1]]
if elem then
t[#t + 1] = fmt(elem, select(2, ...))
else
t[#t + 1] = concat(args)
end
2021-01-21 19:44:13 +01:00
end
})
2021-05-01 16:02:35 +02:00
data.inv_width = 10.23
2021-05-01 01:48:51 +02:00
local full_height = 12
2021-01-21 19:44:13 +01:00
2021-01-29 01:14:21 +01:00
local tab = tabs[data.current_tab]
2021-01-21 20:50:13 +01:00
fs(fmt("formspec_version[%u]size[%f,%f]no_prepend[]bgcolor[#0000]",
2021-05-01 16:02:35 +02:00
MIN_FORMSPEC_VERSION, data.inv_width + 8, full_height), styles)
2021-01-21 19:44:13 +01:00
2021-05-01 16:02:35 +02:00
fs("bg9", 0, 0, data.inv_width, full_height, PNG.bg_full, 10)
2021-01-21 19:44:13 +01:00
2021-01-28 21:00:08 +01:00
if tab then
tab.formspec(player, data, fs)
end
2021-01-21 19:44:13 +01:00
2021-05-01 00:28:11 +02:00
get_panels(player, data, fs, full_height)
2021-01-21 19:44:13 +01:00
2021-05-01 00:28:11 +02:00
if #tabs > 1 then
get_tabs_fs(player, data, fs, full_height)
end
2021-01-21 19:44:13 +01:00
--get_debug_grid(data, fs, full_height)
2021-01-16 01:46:26 +01:00
--print("make_fs()", fmt("%.2f ms", (os.clock() - start) * 1000))
2021-01-21 19:44:13 +01:00
2021-01-16 01:46:26 +01:00
return concat(fs)
end
2021-01-30 23:21:20 +01:00
function i3.set_fs(player, _fs)
2021-05-11 00:20:46 +02:00
if not player or player.is_fake_player then return end
2021-01-11 22:28:00 +01:00
local name = player:get_player_name()
2021-01-16 01:46:26 +01:00
local data = pdata[name]
2021-01-25 00:38:57 +01:00
if not data then return end
2021-01-16 01:46:26 +01:00
2021-01-30 23:21:20 +01:00
local fs = fmt("%s%s", make_fs(player, data), _fs or "")
2021-01-16 01:46:26 +01:00
player:set_inventory_formspec(fs)
end
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
local set_fs = i3.set_fs
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
function i3.new_tab(def)
2021-01-16 03:25:03 +01:00
if not is_table(def) or not next(def) then
2021-01-16 03:30:12 +01:00
return err "i3.new_tab: tab definition missing"
2021-01-16 03:25:03 +01:00
end
if not true_str(def.name) then
2021-01-28 21:00:08 +01:00
return err "i3.new_tab: tab name missing"
2021-01-16 03:25:03 +01:00
end
if not true_str(def.description) then
2021-01-16 03:30:12 +01:00
return err "i3.new_tab: description missing"
2021-01-16 03:25:03 +01:00
end
2021-01-16 03:30:12 +01:00
if #tabs == 6 then
return err(fmt("i3.new_tab: cannot add '%s' tab. Limit reached (6).", def.name))
2021-01-16 01:46:26 +01:00
end
2021-01-16 03:30:12 +01:00
tabs[#tabs + 1] = def
2021-01-16 01:46:26 +01:00
end
2021-01-16 04:44:25 +01:00
function i3.get_tabs()
return tabs
end
2021-01-28 21:00:08 +01:00
function i3.delete_tab(tabname)
if not true_str(tabname) then
return err "i3.delete_tab: tab name missing"
2021-01-16 04:44:25 +01:00
end
for i, def in ipairs(tabs) do
2021-01-28 21:00:08 +01:00
if tabname == def.name then
2021-01-16 04:44:25 +01:00
remove(tabs, i)
end
end
end
2021-01-28 21:00:08 +01:00
function i3.set_tab(player, tabname)
local name = player:get_player_name()
local data = pdata[name]
2021-01-30 23:21:20 +01:00
if not tabname or tabname == "" then
data.current_tab = 0
return
end
2021-01-28 21:00:08 +01:00
local found
for i, def in ipairs(tabs) do
if not found and def.name == tabname then
data.current_tab = i
found = true
end
end
if not found then
return err(fmt("i3.set_tab: tab name '%s' does not exist", tabname))
end
end
local set_tab = i3.set_tab
function i3.override_tab(tabname, newdef)
if not is_table(newdef) or not next(newdef) then
return err "i3.override_tab: tab definition missing"
end
if not true_str(newdef.name) then
return err "i3.override_tab: tab name missing"
end
if not true_str(newdef.description) then
return err "i3.override_tab: description missing"
end
for i, def in ipairs(tabs) do
if def.name == tabname then
tabs[i] = newdef
end
end
end
2021-01-22 00:42:48 +01:00
local function init_data(player, info)
local name = player:get_player_name()
2021-05-31 20:48:12 +02:00
pdata[name] = pdata[name] or {}
2021-01-16 01:46:26 +01:00
local data = pdata[name]
2021-01-22 00:42:48 +01:00
2021-05-31 20:48:12 +02:00
data.filter = ""
data.pagenum = 1
data.items = init_items
data.items_raw = init_items
data.favs = {}
data.export_counts = {}
data.current_tab = 1
2021-06-26 04:40:57 +02:00
data.current_itab = 1
2021-05-31 20:48:12 +02:00
data.subcat = 1
data.scrbar_inv = 0
data.lang_code = get_lang_code(info)
data.fs_version = info.formspec_version
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
after(0, set_fs, player)
end
local function reset_data(data)
data.filter = ""
2021-06-25 03:44:38 +02:00
data.expand = ""
2021-01-16 01:46:26 +01:00
data.pagenum = 1
data.rnum = 1
data.unum = 1
data.scrbar_rcp = 1
data.scrbar_usg = 1
data.query_item = nil
data.recipes = nil
data.usages = nil
data.export_rcp = nil
data.export_usg = nil
2021-06-25 03:44:38 +02:00
data.alt_items = nil
2021-01-16 01:46:26 +01:00
data.items = data.items_raw
2021-06-26 05:29:49 +02:00
if data.current_itab > 1 then
sort_by_category(data)
end
2021-01-16 01:46:26 +01:00
end
2021-05-01 00:28:11 +02:00
local function rcp_fields(player, data, fields)
2021-01-16 01:46:26 +01:00
local name = player:get_player_name()
local sb_rcp, sb_usg = fields.scrbar_rcp, fields.scrbar_usg
2021-01-11 21:59:11 +01:00
2021-05-01 02:02:07 +02:00
if fields.cancel then
reset_data(data)
elseif fields.exit then
data.query_item = nil
elseif fields.key_enter_field == "filter" or fields.search then
if fields.filter == "" then
reset_data(data)
return set_fs(player)
end
local str = lower(fields.filter)
if data.filter == str then return end
data.filter = str
data.pagenum = 1
search(data)
2021-06-26 05:29:49 +02:00
if data.current_itab > 1 then
sort_by_category(data)
end
2021-05-01 02:02:07 +02:00
elseif fields.prev_page or fields.next_page then
if data.pagemax == 1 then return end
data.pagenum = data.pagenum - (fields.prev_page and 1 or -1)
if data.pagenum > data.pagemax then
data.pagenum = 1
elseif data.pagenum == 0 then
data.pagenum = data.pagemax
end
elseif fields.prev_recipe or fields.next_recipe then
2021-01-16 03:36:49 +01:00
local num = data.rnum + (fields.prev_recipe and -1 or 1)
data.rnum = data.recipes[num] and num or (fields.prev_recipe and #data.recipes or 1)
data.export_rcp = nil
data.scrbar_rcp = 1
elseif fields.prev_usage or fields.next_usage then
local num = data.unum + (fields.prev_usage and -1 or 1)
data.unum = data.usages[num] and num or (fields.prev_usage and #data.usages or 1)
data.export_usg = nil
data.scrbar_usg = 1
elseif fields.fav then
2021-01-16 01:46:26 +01:00
local fav, i = is_fav(data.favs, data.query_item)
local total = #data.favs
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
if total < MAX_FAVS and not fav then
data.favs[total + 1] = data.query_item
elseif fav then
remove(data.favs, i)
end
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
elseif fields.export_rcp or fields.export_usg then
if fields.export_rcp then
data.export_rcp = not data.export_rcp
2021-01-11 22:28:00 +01:00
2021-01-16 01:46:26 +01:00
if not data.export_rcp then
data.scrbar_rcp = 1
end
else
data.export_usg = not data.export_usg
2021-01-10 20:56:37 +01:00
2021-01-16 01:46:26 +01:00
if not data.export_usg then
data.scrbar_usg = 1
2021-01-14 15:18:55 +01:00
end
2021-01-11 22:28:00 +01:00
end
2021-01-16 01:46:26 +01:00
elseif (sb_rcp and sub(sb_rcp, 1, 3) == "CHG") or (sb_usg and sub(sb_usg, 1, 3) == "CHG") then
data.scrbar_rcp = sb_rcp and tonum(match(sb_rcp, "%d+"))
data.scrbar_usg = sb_usg and tonum(match(sb_usg, "%d+"))
elseif fields.craft_rcp or fields.craft_usg then
2021-04-23 05:17:45 +02:00
craft_stack(player, data, fields.craft_rcp)
2021-01-16 01:46:26 +01:00
else
select_item(player, name, data, fields)
end
end
2021-01-22 00:42:48 +01:00
local function get_inv_slots(data, fs)
2021-05-01 01:48:51 +02:00
local inv_x, inv_y = 0.22, 6.9
2021-01-30 23:21:20 +01:00
local width, size, spacing = HOTBAR_COUNT, 1, 0.1
2021-01-22 00:42:48 +01:00
local bag = data.bag_size
2021-01-26 02:16:10 +01:00
2021-01-29 22:54:40 +01:00
fs("style_type[box;colors=#77777710,#77777710,#777,#777]")
2021-01-22 00:42:48 +01:00
2021-01-24 16:08:46 +01:00
for i = 0, HOTBAR_COUNT - 1 do
2021-03-29 03:08:48 +02:00
fs("box", i * size + inv_x + (i * spacing), inv_y, size, size, "")
end
2021-01-22 00:42:48 +01:00
2021-01-24 16:08:46 +01:00
fs(fmt("style_type[list;size=%f;spacing=%f]", size, spacing),
fmt("list[current_player;main;%f,%f;%u,1;]", inv_x, inv_y, HOTBAR_COUNT))
2021-01-22 00:42:48 +01:00
if bag then
if bag == "small" then
2021-01-30 23:21:20 +01:00
width, size = 10, 0.892
elseif bag == "medium" then
2021-01-30 23:21:20 +01:00
width, size = 11, 0.8
elseif bag == "large" then
2021-01-30 23:21:20 +01:00
width, size = 12, 0.726
end
2021-01-22 00:42:48 +01:00
end
2021-01-16 01:46:26 +01:00
2021-01-24 04:45:24 +01:00
fs(fmt("style_type[list;size=%f;spacing=%f]", size, spacing),
2021-01-30 23:21:20 +01:00
fmt("list[current_player;main;%f,%f;%u,%u;%u]", inv_x, inv_y + 1.15,
2021-01-24 16:08:46 +01:00
width, (bag and BAG_SIZES[data.bag_size] or INV_SIZE) / width, HOTBAR_COUNT),
2021-01-24 04:45:24 +01:00
"style_type[list;size=1;spacing=0.15]")
2021-03-16 19:34:25 +01:00
fs("listring[current_player;craft]listring[current_player;main]")
2021-01-22 00:42:48 +01:00
end
local function get_inventory_fs(player, data, fs)
2021-03-16 19:34:25 +01:00
fs("listcolors[#bababa50;#bababa99]")
2021-01-22 00:42:48 +01:00
get_inv_slots(data, fs)
2021-01-16 01:46:26 +01:00
2021-01-16 03:41:45 +01:00
local props = player:get_properties()
local name = player:get_player_name()
2021-01-16 01:46:26 +01:00
2021-05-01 01:48:51 +02:00
local ctn_len, ctn_hgt = 5.7, 6.3
2021-03-22 21:40:05 +01:00
local yoffset = 0
2021-01-24 16:38:21 +01:00
2021-01-16 03:41:45 +01:00
if props.mesh ~= "" then
local anim = player:get_local_animation()
2021-01-25 20:29:38 +01:00
local armor_skin = __3darmor or __skinsdb
2021-04-19 03:43:02 +02:00
local t = {}
for _, v in ipairs(props.textures) do
t[#t + 1] = ESC(v):gsub(",", "!")
end
2021-01-16 03:41:45 +01:00
2021-04-19 03:43:02 +02:00
local textures = concat(t, ","):gsub("!", ",")
--fs("style[player_model;bgcolor=black]")
2021-06-20 02:06:55 +02:00
fs("model", 0.2, 0.2, armor_skin and 4 or 3.4, ctn_hgt,
2021-04-19 03:43:02 +02:00
"player_model", props.mesh, textures, "0,-150", "false", "false",
fmt("%u,%u%s", anim.x, anim.y, data.fs_version >= 5 and ";30" or ""))
2021-01-16 03:41:45 +01:00
else
local size = 2.5
2021-03-29 03:08:48 +02:00
fs("image", 0.7, 0.2, size, size * props.visual_size.y, props.textures[1])
2021-01-16 03:41:45 +01:00
end
2021-01-16 01:46:26 +01:00
2021-01-16 03:41:45 +01:00
local award_list, award_list_nb
local awards_unlocked = 0
2021-05-09 16:54:09 +02:00
local max_val = damage_enabled and 12 or 7
2021-01-16 01:46:26 +01:00
2021-04-06 04:14:40 +02:00
if __3darmor and data.subcat == 2 then
2021-04-11 23:23:03 +02:00
if data.scrbar_inv >= max_val then
2021-04-07 00:37:29 +02:00
data.scrbar_inv = data.scrbar_inv + 10
2021-01-16 03:41:45 +01:00
end
2021-01-16 01:46:26 +01:00
2021-04-07 00:37:29 +02:00
max_val = max_val + 10
2021-01-16 01:46:26 +01:00
2021-04-06 04:14:40 +02:00
elseif __awards and data.subcat == 4 then
2021-01-22 00:42:48 +01:00
award_list = awards.get_award_states(name)
award_list_nb = #award_list
2021-01-16 03:41:45 +01:00
2021-01-22 00:42:48 +01:00
for i = 1, award_list_nb do
local award = award_list[i]
2021-01-16 01:46:26 +01:00
2021-01-22 00:42:48 +01:00
if award.unlocked then
awards_unlocked = awards_unlocked + 1
end
2021-01-16 03:41:45 +01:00
end
2021-01-16 01:46:26 +01:00
2021-04-05 23:21:10 +02:00
max_val = max_val + (award_list_nb * 13)
2021-04-06 02:47:37 +02:00
2021-04-06 04:14:40 +02:00
elseif data.subcat == 5 then
2021-04-12 05:28:26 +02:00
local wp_nb = #data.waypoints
2021-04-11 23:23:03 +02:00
2021-04-12 05:28:26 +02:00
if wp_nb > 0 then
2021-04-12 20:28:59 +02:00
local mul = (wp_nb > 8 and 7) or (wp_nb > 4 and 6) or 5
2021-04-12 05:28:26 +02:00
max_val = max_val + 11 + (wp_nb * mul)
2021-04-11 23:23:03 +02:00
end
2021-01-16 03:41:45 +01:00
end
2021-01-16 01:46:26 +01:00
2021-01-22 00:42:48 +01:00
fs(fmt([[
scrollbaroptions[arrows=hide;thumbsize=%u;max=%u]
2021-01-25 20:17:40 +01:00
scrollbar[%f,0.2;0.2,%f;vertical;scrbar_inv;%u]
2021-01-22 00:42:48 +01:00
scrollbaroptions[arrows=default;thumbsize=0;max=1000]
]],
2021-05-09 16:54:09 +02:00
(max_val * 4) / 12, max_val, 9.8, ctn_hgt, data.scrbar_inv))
2021-01-22 00:42:48 +01:00
2021-01-24 02:09:15 +01:00
fs(fmt("scroll_container[3.9,0.2;%f,%f;scrbar_inv;vertical]", ctn_len, ctn_hgt))
2021-06-20 00:04:33 +02:00
get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
2021-01-22 00:42:48 +01:00
fs("scroll_container_end[]")
2021-01-16 01:46:26 +01:00
2021-01-16 03:41:45 +01:00
local btn = {
{"trash", ES"Trash all items"},
{"sort_az", ES"Sort items (A-Z)"},
{"sort_za", ES"Sort items (Z-A)"},
{"compress", ES"Compress items"},
}
for i, v in ipairs(btn) do
local btn_name, tooltip = unpack(v)
2021-01-21 20:50:13 +01:00
fs(fmt("style[%s;fgimg=%s;fgimg_hovered=%s;content_offset=0]",
btn_name, PNG[btn_name], PNG[fmt("%s_hover", btn_name)]))
2021-05-01 01:48:51 +02:00
fs("image_button", i + 3.447 - (i * 0.4), 11.43, 0.35, 0.35, "", btn_name, "")
2021-01-16 03:41:45 +01:00
fs(fmt("tooltip[%s;%s]", btn_name, tooltip))
end
end
i3.new_tab {
name = "inventory",
description = S"Inventory",
formspec = get_inventory_fs,
2020-12-30 23:21:05 +01:00
2021-01-16 01:46:26 +01:00
fields = function(player, data, fields)
local name = player:get_player_name()
local sb_inv = fields.scrbar_inv
2020-12-30 23:21:05 +01:00
2021-01-16 01:46:26 +01:00
if fields.skins and data.skin_id ~= tonum(fields.skins) then
data.skin_id = tonum(fields.skins)
local _skins = skins.get_skinlist_for_player(name)
skins.set_player_skin(player, _skins[data.skin_id])
end
2021-01-03 23:22:17 +01:00
2021-04-05 23:21:10 +02:00
for field in pairs(fields) do
if sub(field, 1, 4) == "btn_" then
2021-04-06 04:14:40 +02:00
data.subcat = indexof(SUBCAT, sub(field, 5))
2021-04-05 23:21:10 +02:00
break
2021-04-11 23:23:03 +02:00
elseif find(field, "waypoint_%d+") then
local id, action = match(field, "_(%d+)_(%w+)$")
id = tonum(id)
2021-04-19 21:32:29 +02:00
local waypoint = data.waypoints[id]
if not waypoint then return end
2021-04-11 23:23:03 +02:00
if action == "delete" then
player:hud_remove(waypoint.id)
2021-04-19 21:32:29 +02:00
remove(data.waypoints, id)
2021-04-11 23:23:03 +02:00
elseif action == "teleport" then
2021-05-31 21:35:09 +02:00
local pos = vec_new(waypoint.pos)
2021-04-11 23:23:03 +02:00
pos.y = pos.y + 0.5
2021-05-31 21:50:43 +02:00
local vel = player:get_velocity()
player:add_velocity(vec_mul(vel, -1))
2021-04-11 23:23:03 +02:00
player:set_pos(pos)
2021-05-31 21:50:43 +02:00
2021-04-11 23:23:03 +02:00
msg(name, fmt("Teleported to %s", clr("#ff0", waypoint.name)))
elseif action == "refresh" then
local color = random(0xffffff)
waypoint.color = color
2021-04-20 03:27:37 +02:00
player:hud_change(waypoint.id, "number", color)
2021-04-11 23:23:03 +02:00
elseif action == "hide" then
if waypoint.hide then
local new_id = player:hud_add {
hud_elem_type = "waypoint",
name = waypoint.name,
text = " m",
world_pos = waypoint.pos,
number = waypoint.color,
z_index = -300,
}
waypoint.id = new_id
2021-04-12 05:46:26 +02:00
waypoint.hide = nil
2021-04-11 23:23:03 +02:00
else
player:hud_remove(waypoint.id)
2021-04-12 05:46:26 +02:00
waypoint.hide = true
2021-04-11 23:23:03 +02:00
end
end
2021-04-12 05:46:26 +02:00
break
2021-01-16 01:46:26 +01:00
end
2021-04-05 23:21:10 +02:00
end
2020-12-30 23:21:05 +01:00
2021-05-01 02:02:07 +02:00
if fields.trash then
2021-04-05 23:21:10 +02:00
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
2021-03-29 02:36:28 +02:00
2021-01-16 01:46:26 +01:00
elseif fields.compress then
compress_items(player)
elseif fields.sort_az or fields.sort_za then
sort_itemlist(player, fields.sort_az)
elseif sb_inv and sub(sb_inv, 1, 3) == "CHG" then
data.scrbar_inv = tonum(match(sb_inv, "%d+"))
return
2021-04-06 02:47:37 +02:00
elseif fields.waypoint_add then
2021-05-31 20:11:57 +02:00
local pos = player:get_pos()
for _, v in ipairs(data.waypoints) do
if vec_eq(vec_round(pos), vec_round(v.pos)) then
return msg(name, "You already set a waypoint at this position")
end
end
2021-04-06 02:47:37 +02:00
local waypoint = fields.waypoint_name
if fields.waypoint_name == "" then
2021-04-11 23:23:03 +02:00
waypoint = "Waypoint"
2021-04-06 02:47:37 +02:00
end
local color = random(0xffffff)
local id = player:hud_add {
hud_elem_type = "waypoint",
name = waypoint,
2021-04-06 03:13:44 +02:00
text = " m",
2021-04-06 02:47:37 +02:00
world_pos = pos,
number = color,
z_index = -300,
}
insert(data.waypoints, {name = waypoint, pos = pos, color = color, id = id})
2021-04-12 05:28:26 +02:00
data.scrbar_inv = data.scrbar_inv + 1000
2020-12-30 23:21:05 +01:00
end
2021-01-16 01:46:26 +01:00
return set_fs(player)
end,
}
2020-12-30 23:21:05 +01:00
2021-01-22 00:42:48 +01:00
local trash = create_inventory("i3_trash", {
2021-01-23 20:54:49 +01:00
allow_put = function(_, _, _, stack)
2020-12-30 23:21:05 +01:00
return stack:get_count()
end,
2021-01-23 20:54:49 +01:00
on_put = function(inv, listname, _, _, player)
2020-12-30 23:21:05 +01:00
inv:set_list(listname, {})
2021-01-16 23:40:30 +01:00
local name = player:get_player_name()
2021-01-17 00:27:51 +01:00
2021-01-22 19:45:02 +01:00
if not core.is_creative_enabled(name) then
2021-01-16 23:40:30 +01:00
set_fs(player)
end
2020-12-30 23:21:05 +01:00
end,
})
trash:set_size("main", 1)
local output_rcp = create_inventory("i3_output_rcp", {})
output_rcp:set_size("main", 1)
local output_usg = create_inventory("i3_output_usg", {})
output_usg:set_size("main", 1)
2021-01-17 00:27:51 +01:00
core.register_on_player_inventory_action(function(player, _, _, info)
local name = player:get_player_name()
2021-01-22 19:45:02 +01:00
if not core.is_creative_enabled(name) and
2021-01-17 00:27:51 +01:00
((info.from_list == "main" and info.to_list == "craft") or
(info.from_list == "craft" and info.to_list == "main") or
(info.from_list == "craftresult" and info.to_list == "main")) then
2020-12-31 05:37:04 +01:00
set_fs(player)
2020-12-31 01:34:08 +01:00
end
2020-12-30 23:21:05 +01:00
end)
2020-12-31 01:34:08 +01:00
if rawget(_G, "armor") then
2021-01-03 12:12:28 +01:00
__3darmor = true
2020-12-31 05:37:04 +01:00
armor:register_on_update(set_fs)
2020-12-31 01:34:08 +01:00
end
2021-01-03 12:12:28 +01:00
if rawget(_G, "skins") then
__skinsdb = true
end
2021-01-10 20:56:37 +01:00
if rawget(_G, "awards") then
__awards = true
core.register_on_craft(function(_, player)
set_fs(player)
end)
core.register_on_dignode(function(_, _, player)
set_fs(player)
end)
core.register_on_placenode(function(_, _, player)
set_fs(player)
end)
2021-01-29 02:24:17 +01:00
core.register_on_chat_message(function(name)
2021-01-10 21:45:57 +01:00
local player = core.get_player_by_name(name)
2021-01-29 02:24:17 +01:00
set_fs(player)
2021-01-10 20:56:37 +01:00
end)
core.register_on_dieplayer(set_fs)
end
2021-05-15 00:18:47 +02:00
core.register_on_chatcommand(function(name)
2021-01-29 02:24:17 +01:00
local player = core.get_player_by_name(name)
after(0, set_fs, player)
end)
2021-05-15 00:18:47 +02:00
core.register_on_priv_grant(function(name, _, priv)
if priv == "creative" or priv == "all" then
local data = pdata[name]
reset_data(data)
data.favs = {}
local player = core.get_player_by_name(name)
after(0, set_fs, player)
end
end)
2020-12-30 23:21:05 +01:00
i3.register_craft_type("digging", {
description = ES"Digging",
icon = "i3_steelpick.png",
})
i3.register_craft_type("digging_chance", {
description = ES"Digging (by chance)",
icon = "i3_mesepick.png",
})
i3.add_search_filter("groups", function(item, groups)
local def = reg_items[item]
local has_groups = true
for _, group in ipairs(groups) do
2020-12-30 23:21:05 +01:00
if not def.groups[group] then
has_groups = nil
break
end
end
return has_groups
end)
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
return the fuel, replacements and toolrepair recipes, we have to
override `core.register_craft` and do some reverse engineering.
See engine's issues #4901, #5745 and #8920. ]]
local old_register_craft = core.register_craft
local rcp_num = {}
core.register_craft = function(def)
old_register_craft(def)
if def.type == "toolrepair" then
toolrepair = def.additional_wear * -100
end
local output = def.output or (true_str(def.recipe) and def.recipe) or nil
if not output then return end
output = {match(output, "%S+")}
local groups
if is_group(output[1]) then
groups = extract_groups(output[1])
output = groups_to_items(groups, true)
end
for i = 1, #output do
local item = output[i]
rcp_num[item] = (rcp_num[item] or 0) + 1
if def.replacements then
if def.type == "fuel" then
replacements.fuel[item] = def.replacements
else
replacements[item] = replacements[item] or {}
replacements[item][rcp_num[item]] = def.replacements
end
end
end
end
local old_clear_craft = core.clear_craft
core.clear_craft = function(def)
old_clear_craft(def)
if true_str(def) then
return -- TODO
elseif is_table(def) then
return -- TODO
end
end
local function resolve_aliases(hash)
for oldname, newname in pairs(reg_aliases) do
cache_recipes(oldname)
local recipes = recipes_cache[oldname]
if recipes then
if not recipes_cache[newname] then
recipes_cache[newname] = {}
end
local similar
for i = 1, #recipes_cache[oldname] do
local rcp_old = recipes_cache[oldname][i]
for j = 1, #recipes_cache[newname] do
local rcp_new = copy(recipes_cache[newname][j])
rcp_new.output = oldname
if table_eq(rcp_old, rcp_new) then
similar = true
break
end
end
if not similar then
insert(recipes_cache[newname], rcp_old)
end
end
end
if newname ~= "" and recipes_cache[oldname] and not hash[newname] then
init_items[#init_items + 1] = newname
end
end
end
local function get_init_items()
local _select, _preselect = {}, {}
for name, def in pairs(reg_items) do
if name ~= "" and show_item(def) then
cache_drops(name, def.drop)
cache_fuel(name)
cache_recipes(name)
_preselect[name] = true
end
end
for name in pairs(_preselect) do
cache_usages(name)
init_items[#init_items + 1] = name
_select[name] = true
end
resolve_aliases(_select)
sort(init_items)
if http and true_str(i3.export_url) then
local post_data = {
recipes = recipes_cache,
usages = usages_cache,
}
2021-06-25 03:44:38 +02:00
http.fetch_async {
2020-12-30 23:21:05 +01:00
url = i3.export_url,
2021-06-25 03:44:38 +02:00
post_data = core.write_json(post_data),
2020-12-30 23:21:05 +01:00
}
end
end
2021-06-25 03:44:38 +02:00
core.register_on_mods_loaded(function()
2020-12-30 23:21:05 +01:00
get_init_items()
2021-05-07 03:16:11 +02:00
__sfinv = rawget(_G, "sfinv")
2020-12-30 23:21:05 +01:00
2021-05-07 03:16:11 +02:00
if __sfinv then
old_sfinv_fn = sfinv.set_player_inventory_formspec
function sfinv.set_player_inventory_formspec() return end
2020-12-30 23:21:05 +01:00
sfinv.enabled = false
end
2021-05-07 03:16:11 +02:00
__unified_inventory = rawget(_G, "unified_inventory")
2021-01-25 19:28:28 +01:00
2021-05-07 03:16:11 +02:00
if __unified_inventory then
2021-01-25 19:28:28 +01:00
old_unified_inventory_fn = unified_inventory.set_inventory_formspec
2020-12-30 23:21:05 +01:00
function unified_inventory.set_inventory_formspec() return end
end
end)
2021-01-22 00:42:48 +01:00
local function init_backpack(player)
local name = player:get_player_name()
local data = pdata[name]
2021-04-15 17:46:17 +02:00
local inv = player:get_inventory()
2021-01-22 00:42:48 +01:00
2021-06-20 00:04:33 +02:00
inv:set_size("main", data.bag_size and BAG_SIZES[data.bag_size] or INV_SIZE)
2021-01-22 00:42:48 +01:00
data.bag = create_inventory(fmt("%s_backpack", name), {
2021-04-15 17:46:17 +02:00
allow_put = function(_inv, listname, _, stack)
local empty = _inv:get_stack(listname, 1):is_empty()
2021-01-22 00:42:48 +01:00
if empty and sub(stack:get_name(), 1, 7) == "i3:bag_" then
return 1
end
2021-04-15 17:46:17 +02:00
msg(name, ES"This is not a backpack")
2021-01-22 00:42:48 +01:00
return 0
end,
on_put = function(_, _, _, stack)
data.bag_size = match(stack:get_name(), "_(%w+)$")
2021-04-15 17:46:17 +02:00
inv:set_size("main", BAG_SIZES[data.bag_size])
2021-01-22 00:42:48 +01:00
set_fs(player)
end,
on_take = function()
for i = INV_SIZE + 1, BAG_SIZES[data.bag_size] do
2021-04-15 17:46:17 +02:00
local stack = inv:get_stack("main", i)
2021-01-22 00:42:48 +01:00
if not stack:is_empty() then
spawn_item(player, stack)
end
end
data.bag_size = nil
2021-04-15 17:46:17 +02:00
inv:set_size("main", INV_SIZE)
2021-01-22 00:42:48 +01:00
set_fs(player)
end,
})
data.bag:set_size("main", 1)
if data.bag_size then
data.bag:set_stack("main", 1, fmt("i3:bag_%s", data.bag_size))
end
end
2021-04-06 02:47:37 +02:00
local function init_waypoints(player)
local name = player:get_player_name()
local data = pdata[name]
2021-04-11 23:23:03 +02:00
data.waypoints = data.waypoints or {}
2021-04-06 02:47:37 +02:00
2021-04-11 23:23:03 +02:00
for _, v in ipairs(data.waypoints) do
2021-04-12 00:38:24 +02:00
if not v.hide then
local id = player:hud_add {
hud_elem_type = "waypoint",
name = v.name,
text = " m",
world_pos = v.pos,
number = v.color,
z_index = -300,
}
2021-04-11 23:23:03 +02:00
2021-04-12 00:38:24 +02:00
v.id = id
2021-04-11 23:23:03 +02:00
end
2021-04-06 02:47:37 +02:00
end
end
2021-06-25 03:44:38 +02:00
core.register_on_joinplayer(function(player)
2020-12-31 00:17:38 +01:00
local name = player:get_player_name()
2021-06-25 03:44:38 +02:00
local info = core.get_player_information and core.get_player_information(name)
2021-01-22 00:42:48 +01:00
2021-06-20 02:06:55 +02:00
if not info or get_formspec_version(info) < MIN_FORMSPEC_VERSION then
2021-05-07 03:16:11 +02:00
if __sfinv then
sfinv.set_player_inventory_formspec = old_sfinv_fn
2021-01-26 23:36:24 +01:00
sfinv.enabled = true
end
2021-01-25 19:28:28 +01:00
2021-05-07 03:16:11 +02:00
if __unified_inventory then
2021-01-25 19:28:28 +01:00
unified_inventory.set_inventory_formspec = old_unified_inventory_fn
2021-05-07 03:16:11 +02:00
if __sfinv then
2021-01-25 19:28:28 +01:00
sfinv.enabled = false
end
end
2021-06-20 02:06:55 +02:00
pdata[name] = nil
2021-01-22 00:42:48 +01:00
return outdated(name)
end
init_data(player, info)
init_backpack(player)
2021-04-06 02:47:37 +02:00
init_waypoints(player)
2021-01-24 16:08:46 +01:00
2021-01-25 19:28:28 +01:00
after(0, function()
player:hud_set_hotbar_itemcount(HOTBAR_COUNT)
player:hud_set_hotbar_image(PNG.hotbar)
end)
2021-01-22 00:42:48 +01:00
end)
core.register_on_dieplayer(function(player)
local name = player:get_player_name()
2020-12-31 00:17:38 +01:00
local data = pdata[name]
2021-03-24 19:28:45 +01:00
if not data then return end
2020-12-30 23:21:05 +01:00
2021-01-22 00:42:48 +01:00
data.bag_size = nil
data.bag:set_list("main", {})
local inv = player:get_inventory()
inv:set_size("main", INV_SIZE)
set_fs(player)
end)
2021-05-31 20:48:12 +02:00
local META_SAVES = {
bag_size = true,
waypoints = true,
skin_id = true,
inv_items = true,
known_recipes = true,
}
2021-01-22 00:42:48 +01:00
2021-05-31 22:46:02 +02:00
local function save_data(player_name)
local _pdata = copy(pdata)
for name, v in pairs(_pdata) do
2021-05-31 20:48:12 +02:00
for dat in pairs(v) do
if not META_SAVES[dat] then
2021-05-31 22:46:02 +02:00
_pdata[name][dat] = nil
if player_name then
pdata[player_name][dat] = nil -- To free up some memory
end
2021-05-31 20:48:12 +02:00
end
2020-12-31 00:17:38 +01:00
end
2021-05-31 20:48:12 +02:00
end
2021-05-31 22:46:02 +02:00
storage:set_string("pdata", slz(_pdata))
end
2021-06-25 03:44:38 +02:00
core.register_on_leaveplayer(function(player)
2021-05-31 22:46:02 +02:00
local name = player:get_player_name()
save_data(name)
2020-12-30 23:21:05 +01:00
end)
2021-06-25 03:44:38 +02:00
core.register_on_shutdown(save_data)
2021-05-31 22:46:02 +02:00
local function routine()
save_data()
after(SAVE_INTERVAL, routine)
end
after(SAVE_INTERVAL, routine)
2021-06-25 03:44:38 +02:00
core.register_on_player_receive_fields(function(player, formname, fields)
2021-01-31 00:01:44 +01:00
if formname ~= "" then
2021-02-08 19:39:13 +01:00
return false
2021-01-31 00:01:44 +01:00
end
2020-12-30 23:21:05 +01:00
local name = player:get_player_name()
local data = pdata[name]
2021-01-22 00:42:48 +01:00
if not data then return end
2020-12-30 23:21:05 +01:00
2021-01-16 01:46:26 +01:00
for f in pairs(fields) do
if sub(f, 1, 4) == "tab_" then
local tabname = sub(f, 5)
2021-01-28 21:00:08 +01:00
set_tab(player, tabname)
2021-01-16 01:46:26 +01:00
break
2021-06-26 04:40:57 +02:00
elseif sub(f, 1, 5) == "itab_" then
2021-06-26 07:08:14 +02:00
data.pagenum = 1
2021-06-26 04:40:57 +02:00
data.current_itab = tonum(f:sub(-1))
sort_by_category(data)
2020-12-30 23:21:05 +01:00
end
end
2021-05-01 02:02:07 +02:00
rcp_fields(player, data, fields)
2021-01-29 01:07:59 +01:00
local tab = tabs[data.current_tab]
2021-01-31 00:01:44 +01:00
if tab and tab.fields then
return true, tab.fields(player, data, fields)
end
return true, set_fs(player)
2020-12-30 23:21:05 +01:00
end)
core.register_on_player_hpchange(function(player, hpchange)
local name = player:get_player_name()
local data = pdata[name]
2021-01-10 01:39:51 +01:00
if not data then return end
2020-12-30 23:21:05 +01:00
2021-01-10 01:39:51 +01:00
local hp_max = player:get_properties().hp_max
2020-12-30 23:21:05 +01:00
data.hp = min(hp_max, player:get_hp() + hpchange)
2020-12-31 05:37:04 +01:00
set_fs(player)
2020-12-30 23:21:05 +01:00
end)
if progressive_mode then
local function item_in_inv(item, inv_items)
local inv_items_size = #inv_items
if is_group(item) then
local groups = extract_groups(item)
2021-06-25 03:44:38 +02:00
2020-12-30 23:21:05 +01:00
for i = 1, inv_items_size do
local def = reg_items[inv_items[i]]
if def then
if item_has_groups(def.groups, groups) then
return true
end
end
end
else
for i = 1, inv_items_size do
if inv_items[i] == item then
return true
end
end
end
end
local function recipe_in_inv(rcp, inv_items)
for _, item in pairs(rcp.items) do
if not item_in_inv(item, inv_items) then return end
end
return true
end
local function progressive_filter(recipes, player)
if not recipes then
return {}
end
local name = player:get_player_name()
local data = pdata[name]
if #data.inv_items == 0 then
return {}
end
local filtered, c = {}, 0
2021-01-22 23:23:43 +01:00
2020-12-30 23:21:05 +01:00
for i = 1, #recipes do
local recipe = recipes[i]
if recipe_in_inv(recipe, data.inv_items) then
c = c + 1
filtered[c] = recipe
end
end
return filtered
end
local item_lists = {"main", "craft", "craftpreview"}
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]
2021-06-25 03:44:38 +02:00
2020-12-30 23:21:05 +01:00
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
local function init_hud(player, data)
data.hud = {
2021-04-06 02:47:37 +02:00
bg = player:hud_add {
2020-12-30 23:21:05 +01:00
hud_elem_type = "image",
position = {x = 0.78, y = 1},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
text = PNG.bg,
z_index = 0xDEAD,
},
2021-04-06 02:47:37 +02:00
book = player:hud_add {
2020-12-30 23:21:05 +01:00
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
alignment = {x = 1, y = 1},
scale = {x = 4, y = 4},
text = PNG.book,
z_index = 0xDEAD,
},
2021-04-06 02:47:37 +02:00
text = player:hud_add {
2020-12-30 23:21:05 +01:00
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
z_index = 0xDEAD,
},
}
end
local function show_hud_success(player, data)
-- It'd better to have an engine function `hud_move` to only need
-- 2 calls for the notification's back and forth.
local hud_info_bg = player:hud_get(data.hud.bg)
local dt = 0.016
if hud_info_bg.position.y <= 0.9 then
data.show_hud = false
data.hud_timer = (data.hud_timer or 0) + dt
end
if data.show_hud then
for _, def in pairs(data.hud) do
local hud_info = player:hud_get(def)
player:hud_change(def, "position", {
x = hud_info.position.x,
y = hud_info.position.y - (dt / 5)
})
end
player:hud_change(data.hud.text, "text",
S("@1 new recipe(s) discovered!", data.discovered))
elseif data.show_hud == false then
if data.hud_timer >= HUD_TIMER_MAX then
for _, def in pairs(data.hud) do
local hud_info = player:hud_get(def)
player:hud_change(def, "position", {
x = hud_info.position.x,
y = hud_info.position.y + (dt / 5)
})
end
if hud_info_bg.position.y >= 1 then
data.show_hud = nil
data.hud_timer = nil
end
end
end
end
-- Workaround. Need an engine call to detect when the contents of
-- the player inventory changed, instead.
local function poll_new_items()
2021-06-25 03:44:38 +02:00
local players = core.get_connected_players()
2020-12-30 23:21:05 +01:00
for i = 1, #players do
local player = players[i]
local name = player:get_player_name()
local data = pdata[name]
local inv_items = get_inv_items(player)
2020-12-31 00:43:12 +01:00
local diff = array_diff(inv_items, data.inv_items)
2020-12-30 23:21:05 +01:00
if #diff > 0 then
data.inv_items = table_merge(diff, data.inv_items)
local oldknown = data.known_recipes or 0
local items = 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
data.items_raw = items
2021-06-26 05:29:49 +02:00
data.current_itab = 1
2020-12-30 23:21:05 +01:00
search(data)
2020-12-31 05:37:04 +01:00
set_fs(player)
2020-12-30 23:21:05 +01:00
end
end
after(POLL_FREQ, poll_new_items)
end
poll_new_items()
2021-06-25 03:44:38 +02:00
core.register_globalstep(function()
local players = core.get_connected_players()
2020-12-30 23:21:05 +01: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 and singleplayer then
show_hud_success(player, data)
end
end
end)
i3.add_recipe_filter("Default progressive filter", progressive_filter)
2021-06-25 03:44:38 +02:00
core.register_on_joinplayer(function(player)
2020-12-30 23:21:05 +01:00
local name = player:get_player_name()
2021-01-14 14:53:32 +01:00
local data = pdata[name]
2020-12-31 00:17:38 +01:00
2021-05-31 20:48:12 +02:00
data.inv_items = data.inv_items or {}
data.known_recipes = data.known_recipes or 0
2020-12-30 23:21:05 +01:00
2021-01-14 14:53:32 +01:00
local items = get_filtered_items(player, data)
data.items_raw = items
search(data)
2020-12-30 23:21:05 +01:00
if singleplayer then
init_hud(player, data)
end
end)
2021-01-22 00:42:48 +01:00
end
2020-12-30 23:21:05 +01:00
2021-01-23 01:25:54 +01:00
local bag_recipes = {
small = {
{"", "farming:string", ""},
{"group:wool", "group:wool", "group:wool"},
{"group:wool", "group:wool", "group:wool"},
},
medium = {
{"farming:string", "i3:bag_small", "farming:string"},
{"farming:string", "i3:bag_small", "farming:string"},
},
large = {
{"farming:string", "i3:bag_medium", "farming:string"},
{"farming:string", "i3:bag_medium", "farming:string"},
},
}
for size, rcp in pairs(bag_recipes) do
2021-01-22 00:42:48 +01:00
local bagname = fmt("i3:bag_%s", size)
2020-12-30 23:21:05 +01:00
2021-01-22 00:42:48 +01:00
core.register_craftitem(bagname, {
description = fmt("%s Backpack", size:gsub("^%l", upper)),
inventory_image = fmt("i3_bag_%s.png", size),
stack_max = 1,
})
2021-01-23 01:25:54 +01:00
core.register_craft {output = bagname, recipe = rcp}
core.register_craft {type = "fuel", recipe = bagname, burntime = 3}
2020-12-30 23:21:05 +01:00
end
2021-06-26 03:10:05 +02:00
--dofile(modpath .. "/tests/test_tabs.lua")
--dofile(modpath .. "/tests/test_custom_recipes.lua")