Compare commits

..

15 Commits
1.13.1 ... 1.14

9 changed files with 259 additions and 86 deletions

68
API.md
View File

@ -1,6 +1,19 @@
## API
# API :screwdriver:
### Custom tabs
### Table of Contents
1. [**Tabs**](https://github.com/minetest-mods/i3/blob/main/API.md#tabs)
2. [**Recipes**](https://github.com/minetest-mods/i3/blob/main/API.md#recipes)
3. [**Minitabs**](https://github.com/minetest-mods/i3/blob/main/API.md#minitabs)
4. [**Recipe filters**](https://github.com/minetest-mods/i3/blob/main/API.md#recipe-filters)
5. [**Search filters**](https://github.com/minetest-mods/i3/blob/main/API.md#search-filters)
6. [**Sorting methods**](https://github.com/minetest-mods/i3/blob/main/API.md#sorting-methods)
7. [**Item list compression**](https://github.com/minetest-mods/i3/blob/main/API.md#item-list-compression)
8. [**Waypoints**](https://github.com/minetest-mods/i3/blob/main/API.md#waypoints)
9. [**Miscellaneous**](https://github.com/minetest-mods/i3/blob/main/API.md#miscellaneous)
---
### Tabs
#### `i3.new_tab(name, def)`
@ -36,7 +49,7 @@ i3.new_tab("stuff", {
-- Do things
end
i3.set_fs(player) -- Update the formspec, mandatory
i3.set_fs(player) -- Update the formspec
end,
})
```
@ -72,7 +85,7 @@ A list of registered tabs.
---
### Custom recipes
### Recipes
Custom recipes are nonconventional crafts outside the main crafting grid.
They can be registered in-game dynamically and have a size beyond 3x3 items.
@ -164,6 +177,53 @@ i3.register_craft {
---
### Minitabs
Manage the tabs on the right panel of the inventory.
Allow to make a sensible list sorted by specific groups of items.
#### `i3.new_minitab(name, def)`
Add a new minitab (limited to 6).
- `name` is the tab name.
- `def` is the definition table.
Example:
```Lua
i3.new_minitab("test", {
description = "Test",
-- Whether this tab is visible or not. Optional.
access = function(player, data)
return player:get_player_name() == "singleplayer"
end,
-- Whether a specific item is shown in the list or not.
sorter = function(item, data)
return item:find"wood"
end
})
```
- `player` is an `ObjectRef` to the user.
- `data` are the user data.
- `item` is an item name string.
#### `i3.remove_minitab(name)`
Remove a minitab by name.
- `name` is the name of the tab to remove.
#### `i3.minimap`
A list of registered minitabs.
---
### Recipe filters
Recipe filters can be used to filter the recipes shown to players. Progressive

View File

@ -3,7 +3,7 @@ License of source code
The MIT License (MIT)
Copyright (c) 2020-2021 Jean-Patrick Guerrero and contributors.
Copyright (c) 2020-2023 Jean-Patrick Guerrero and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -20,7 +20,7 @@ local function lf(path)
end
i3 = {
version = 1131,
version = 114,
data = core.deserialize(storage:get_string"data") or {},
settings = {
@ -34,7 +34,7 @@ i3 = {
save_interval = 600, -- Player data save interval (in seconds)
hud_speed = 1,
hud_timer_max = 1.5,
hud_timer_max = 3,
damage_enabled = core.settings:get_bool"enable_damage",
progressive_mode = core.settings:get_bool"i3_progressive_mode",
@ -101,6 +101,7 @@ i3 = {
groups = {},
plants = {},
modules = {},
minitabs = {},
craft_types = {},
recipe_filters = {},

View File

@ -430,3 +430,55 @@ function i3.get_waypoints(player_name)
return data.waypoints
end
function i3.new_minitab(name, def)
if #i3.minitabs == 6 then
return err "i3.new_minitab: limit reached (6)"
elseif not true_str(name) then
return err "i3.new_minitab: name missing"
elseif not true_table(def) then
return err "i3.new_minitab: definition missing"
end
def.name = name
insert(i3.minitabs, def)
end
function i3.remove_minitab(name)
if not true_str(name) then
return err "i3.remove_minitab: name missing"
elseif name == "all" then
return err "i3.remove_minitab: removing the 'All' tab is not allowed"
end
for i = #i3.minitabs, 1, -1 do
local v = i3.minitabs[i]
if v and v.name == name then
remove(i3.minitabs, i)
end
end
end
i3.new_minitab("all", {
description = "All",
sorter = function()
return true
end
})
i3.new_minitab("nodes", {
description = "Nodes",
sorter = function(item)
return core.registered_nodes[item]
end
})
i3.new_minitab("items", {
description = "Items",
sorter = function(item)
return core.registered_craftitems[item] or core.registered_tools[item]
end
})

View File

@ -178,13 +178,9 @@ local function cache_recipes(item)
_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
local shift = size_rcp - size_rpl
for k, v in pairs(replacements[item]) do
k += shift

View File

@ -360,13 +360,8 @@ local function sort_by_category(data)
for i = 1, #items do
local item = items[i]
local to_add = true
if data.itab == 2 then
to_add = core.registered_nodes[item]
elseif data.itab == 3 then
to_add = core.registered_craftitems[item] or core.registered_tools[item]
end
local tab = i3.minitabs[data.itab]
local to_add = tab.sorter(item, data)
if to_add then
insert(new, item)
@ -413,6 +408,11 @@ local function get_stack(player, stack)
end
end
local function get_group_items(name)
local groups = extract_groups(name)
return i3.groups[name:sub(7)].items or groups_to_items(groups)
end
local function craft_stack(player, data, craft_rcp)
local inv = player:get_inventory()
local rcp_usg = craft_rcp and "recipe" or "usage"
@ -427,32 +427,42 @@ local function craft_stack(player, data, craft_rcp)
if is_group(name) then
items = {}
local groups = extract_groups(name)
local groupname = name:sub(7)
local item_groups = i3.groups[groupname].items or groups_to_items(groups)
local item_groups = get_group_items(name)
local remaining = count
for _, item in ipairs(item_groups) do
for _name, _count in pairs(data.crafting_counts[rcp_usg].inv) do
if item == _name and remaining > 0 then
local c = min(remaining, _count)
items[item] = c
remaining -= c
for _name, _count in pairs(data.crafting_counts[rcp_usg].inv) do
if item == _name and remaining > 0 then
local c = min(remaining, _count)
items[item] = c
remaining -= c
end
if remaining == 0 then break end
end
if remaining == 0 then break end
end
end
end
for k, v in pairs(items) do
inv:remove_item("main", fmt("%s %s", k, v * scrbar_val))
end
end
for item, v in pairs(items) do
for _ = 1, v * scrbar_val do
inv:remove_item("main", item)
if rcp_def.replacements then
for _, pair in ipairs(rcp_def.replacements) do
get_stack(player, ItemStack(pair[2]))
for _, pair in ipairs(rcp_def.replacements or {}) do
local old_name, new_name = unpack(pair)
if is_group(old_name) then
local item_groups = get_group_items(old_name)
for _, it in ipairs(item_groups) do
if item == it then
get_stack(player, ItemStack(new_name))
end
end
elseif item == old_name then
get_stack(player, ItemStack(new_name))
end
end
end
end
end

View File

@ -946,6 +946,10 @@ local function get_output_fs(fs, data, rcp, is_recipe, is_usage, shapeless, righ
local itemstr = ESC(item:to_string())
item_image_button(X + 0.11, Y, BTN_SIZE, BTN_SIZE, itemstr, _name, "")
if item:get_stack_max() < count then
label(X + 1.05, Y + 1, count)
end
local def = reg_items[name]
local unknown = not def or nil
local desc = def and def.description
@ -1044,11 +1048,9 @@ local function get_grid_fs(fs, data, rcp, is_recipe, is_usage)
local label = groups and "\nG" or ""
local replace
for j = 1, #(rcp.replacements or {}) do
local replacement = rcp.replacements[j]
for _, replacement in ipairs(rcp.replacements or {}) do
if replacement[1] == name then
replace = replace or {type = rcp.type, items = {}}
local added
for _, v in ipairs(replace.items) do
@ -1059,12 +1061,17 @@ local function get_grid_fs(fs, data, rcp, is_recipe, is_usage)
end
if not added then
label = fmt("%s%s\nR", label ~= "" and "\n" or "", label)
label = fmt("%s\nR", label)
insert(replace.items, replacement[2])
end
end
end
local _, count_sub = label:gsub("\n", "")
if count_sub == 2 then
label = label:sub(2)
end
if not large_recipe then
slot(X, Y, btn_size, btn_size)
end
@ -1087,7 +1094,12 @@ local function get_grid_fs(fs, data, rcp, is_recipe, is_usage)
item:set_name(name)
item:set_count(count)
local itemstr = ESC(item:to_string())
item_image_button(X, Y, btn_size, btn_size, itemstr, btn_name, label)
if item:get_stack_max() < count then
label(X + 0.95, Y + 0.95, count)
end
end
local def = reg_items[name]
@ -1391,7 +1403,20 @@ local function get_header_items_fs(fs, data)
end
box(X + 1, 0.2, 0.01, 0.5, "#bababa50")
local cat = {{"all", "all items"}, {"node", "nodes only"}, {"item", "items only"}}
local cat = {{"all", "all items"}}
if not recipe_filter_set() then
for _, v in ipairs(i3.minitabs) do
if v.name == "Nodes" then
insert(cat, {"node", "nodes only"})
end
if v.name == "Items" then
insert(cat, {"item", "items only"})
end
end
end
for i in ipairs(cat) do
local name, desc = unpack(cat[i])
@ -1452,22 +1477,37 @@ local function get_header_items_fs(fs, data)
end
end
local function get_minitabs(fs, data, full_height)
local _tabs = {"All", "Nodes", "Items"}
local tab_len, tab_hgh = 1.8, 0.5
local function get_minitabs(fs, data, player, full_height)
local minitabs = {}
for i, title in ipairs(_tabs) do
local selected = i == data.itab
for i, v in ipairs(i3.minitabs) do
local access = v.access
if access == nil or access(player, data) then
minitabs[i] = v.description
end
end
local tab_len, tab_hgh, i = 1.8, 0.5, 0
for id, title in pairs(minitabs) do
i++
local top = i > 3
local X = top and i - 3 or i
local selected = id == data.itab
local hover_texture = selected and PNG.tab_small_hover or PNG.tab_small
local flip = top and "^[transformFY" or ""
fs([[ style_type[image_button;bgimg=%s;bgimg_hovered=%s;
bgimg_middle=14,0,-14,-14;padding=-14,0,14,14] ]], hover_texture, PNG.tab_small_hover)
fs([[ style_type[image_button;bgimg=%s%s;bgimg_hovered=%s%s;
bgimg_middle=14,0,-14,-14;padding=-14,0,14,14] ]],
hover_texture, flip, PNG.tab_small_hover, flip)
fs([[ style_type[image_button;noclip=true;font=bold;font_size=16;
textcolor=%s;content_offset=0;sound=i3_tab] ]], selected and "#fff" or "#bbb")
fs"style_type[image_button:hovered;textcolor=#fff]"
image_button((data.inv_width - 0.65) + (i * (tab_len + 0.1)),
full_height, tab_len, tab_hgh, "", fmt("itab_%u", i), title)
image_button((data.inv_width - 0.65) + (X * (tab_len + 0.1)),
top and -tab_hgh or full_height, tab_len, tab_hgh, "", fmt("itab_%u", id), title)
end
end
@ -1515,7 +1555,7 @@ local function get_items_fs(fs, data, player, full_height)
local item_btn = fmt("item_image_button", X, Y, size, size, name, item, "")
if recipe_filter_set() then
if recipe_filter_set() and data.itab == 1 then
if data.items_progress[item] then
insert(fs, item_btn)
else
@ -1706,7 +1746,7 @@ local function make_fs(player, data)
get_items_fs(fs, data, player, full_height)
if not data.hide_tabs then
get_minitabs(fs, data, full_height)
get_minitabs(fs, data, player, full_height)
end
end

View File

@ -15,16 +15,18 @@ local function init_hud(player)
data.hud = {
bg = player:hud_add {
hud_elem_type = "image",
position = {x = 0.78, y = 1},
position = {x = 1, y = 1},
offset = {x = -320, y = 0},
alignment = {x = 1, y = 1},
scale = {x = 370, y = 112},
scale = {x = 300, y = 105},
text = "i3_bg.png",
z_index = 0xDEAD,
},
img = player:hud_add {
hud_elem_type = "image",
position = {x = 0.79, y = 1.02},
position = {x = 1, y = 1},
offset = {x = -310, y = 20},
alignment = {x = 1, y = 1},
scale = {x = 1, y = 1},
text = "",
@ -33,7 +35,8 @@ local function init_hud(player)
text = player:hud_add {
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
position = {x = 1, y = 1},
offset = {x = -235, y = 40},
alignment = {x = 1, y = 1},
number = 0xffffff,
text = "",
@ -55,13 +58,12 @@ local function init_hud(player)
end
local function show_hud(player, data)
-- It would 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.025
local dt = 0.016
local offset_y = hud_info_bg.offset.y
local speed = 5 * i3.settings.hud_speed
if hud_info_bg.position.y <= 0.9 then
if offset_y < -100 then
data.show_hud = false
data.hud_timer = (data.hud_timer or 0) + dt
end
@ -73,31 +75,34 @@ local function show_hud(player, data)
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) * i3.settings.hud_speed)
})
end
elseif data.show_hud == false then
if data.hud_timer >= i3.settings.hud_timer_max then
for _, def in pairs(data.hud) do
for name, def in pairs(data.hud) do
if name ~= "wielditem" then
local hud_info = player:hud_get(def)
player:hud_change(def, "position", {
x = hud_info.position.x,
y = hud_info.position.y + ((dt / 5) * i3.settings.hud_speed)
player:hud_change(def, "offset", {
x = hud_info.offset.x,
y = hud_info.offset.y - speed
})
end
end
elseif data.show_hud == false then
if data.hud_timer >= i3.settings.hud_timer_max then
for name, def in pairs(data.hud) do
if name ~= "wielditem" then
local hud_info = player:hud_get(def)
if hud_info_bg.position.y >= 1 then
data.show_hud = nil
player:hud_change(def, "offset", {
x = hud_info.offset.x,
y = hud_info.offset.y + speed
})
end
end
if offset_y > 0 then
data.show_hud = nil
data.hud_timer = nil
data.hud_msg = nil
data.hud_img = nil
data.hud_msg = nil
data.hud_img = nil
end
end
end

View File

@ -2,9 +2,19 @@ local set_fs = i3.set_fs
local hud_notif = i3.hud_notif
local POLL_FREQ = 0.25
IMPORT("reg_items", "reg_nodes")
IMPORT("fmt", "search", "table_merge", "array_diff")
IMPORT("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters")
IMPORT("reg_items", "reg_nodes", "fmt", "table_merge", "array_diff")
IMPORT("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters", "sort_by_category")
i3.remove_minitab"nodes"
i3.remove_minitab"items"
i3.new_minitab("unlocked", {
description = "Unlocked",
sorter = function(item, data)
return data.items_progress[item]
end
})
local function get_filtered_items(player, data)
local items, known = {}, 0
@ -145,9 +155,8 @@ local function poll_new_items(player, data, join)
end
data.items_progress = items
data.itab = 1
search(data)
sort_by_category(data)
set_fs(player)
end