minetest-i4/init.lua

295 lines
6.2 KiB
Lua
Raw Normal View History

2021-10-18 22:54:51 -05:00
local modpath = core.get_modpath "i3"
local function lf(path)
return loadfile(modpath .. path)
end
i3 = {
modules = {},
http = core.request_http_api(),
MAX_FAVS = 6,
INV_SIZE = 4*9,
HOTBAR_LEN = 9,
ITEM_BTN_SIZE = 1.1,
MIN_FORMSPEC_VERSION = 4,
SAVE_INTERVAL = 600, -- Player data save interval (in seconds)
BAG_SIZES = {
4*9 + 3,
4*9 + 6,
4*9 + 9,
},
SUBCAT = {
"bag",
"armor",
"skins",
"awards",
"waypoints",
},
META_SAVES = {
bag_item = true,
bag_size = true,
waypoints = true,
inv_items = true,
known_recipes = true,
},
-- Caches
init_items = {},
recipes_cache = {},
usages_cache = {},
fuel_cache = {},
recipe_filters = {},
search_filters = {},
craft_types = {},
tabs = {},
2021-10-18 22:54:51 -05:00
files = {
api = lf("/etc/api.lua"),
bags = lf("/etc/bags.lua"),
2021-10-18 22:54:51 -05:00
common = lf("/etc/common.lua"),
compress = lf("/etc/compress.lua"),
groups = lf("/etc/groups.lua"),
gui = lf("/etc/gui.lua"),
inventory = lf("/etc/inventory.lua"),
2021-10-18 22:54:51 -05:00
model_alias = lf("/etc/model_aliases.lua"),
progressive = lf("/etc/progressive.lua"),
recipes = lf("/etc/recipes.lua"),
2021-10-18 22:54:51 -05:00
styles = lf("/etc/styles.lua"),
},
2021-01-15 20:30:12 -06:00
progressive_mode = core.settings:get_bool "i3_progressive_mode",
item_compression = core.settings:get_bool("i3_item_compression", true),
}
2021-01-15 18:46:26 -06:00
local common = i3.files.common()
2021-01-15 21:44:25 -06:00
function i3.need(...)
local t = {}
2021-01-15 21:44:25 -06:00
for _, var in ipairs {...} do
for _, cat in pairs(common) do
for name, func in pairs(cat) do
if var == name then
t[#t + 1] = func
break
end
end
2021-01-15 21:44:25 -06:00
end
end
return unpack(t)
end
local storage = core.get_mod_storage()
local slz, dslz = core.serialize, core.deserialize
2021-01-30 16:21:20 -06:00
i3.data = dslz(storage:get_string "data") or {}
i3.compress_groups, i3.compressed = i3.files.compress()
i3.group_stereotypes, i3.group_names = i3.files.groups()
2021-01-30 16:21:20 -06:00
local set_fs = i3.files.api()
local init_backpack = i3.files.bags()
i3.files.inventory()
local cache_drops, cache_fuel, cache_recipes, cache_usages, resolve_aliases = i3.files.recipes()
2021-01-28 14:00:08 -06:00
local fmt, sort, copy = i3.need("fmt", "sort", "copy")
local show_item, reg_items = i3.need("show_item", "reg_items")
2021-01-28 14:00:08 -06:00
local function get_lang_code(info)
return info and info.lang_code
2021-01-28 14:00:08 -06:00
end
local function get_formspec_version(info)
return info and info.formspec_version or 1
end
2021-01-28 14:00:08 -06:00
local function outdated(name)
local fs = fmt("size[6.3,1.3]image[0,0;1,1;i3_book.png]label[1,0;%s]button_exit[2.6,0.8;1,1;;OK]",
"Your Minetest client is outdated.\nGet the latest version on minetest.net to play the game.")
2021-01-28 14:00:08 -06:00
core.show_formspec(name, "i3_outdated", fs)
2021-01-28 14:00:08 -06:00
end
2021-01-21 17:42:48 -06:00
local function init_data(player, info)
local name = player:get_player_name()
i3.data[name] = i3.data[name] or {}
local data = i3.data[name]
2021-01-21 17:42:48 -06:00
2021-05-31 13:48:12 -05:00
data.filter = ""
data.pagenum = 1
data.items = i3.init_items
data.items_raw = i3.init_items
2021-05-31 13:48:12 -05:00
data.favs = {}
data.export_counts = {}
data.current_tab = 1
2021-06-25 21:40:57 -05:00
data.current_itab = 1
2021-05-31 13:48:12 -05:00
data.subcat = 1
data.scrbar_inv = 0
data.lang_code = get_lang_code(info)
data.fs_version = info.formspec_version
2021-01-10 13:56:37 -06:00
core.after(0, set_fs, player)
2021-01-15 18:46:26 -06:00
end
2020-12-30 18:34:08 -06:00
if rawget(_G, "armor") then
i3.modules.armor = true
2020-12-30 22:37:04 -06:00
armor:register_on_update(set_fs)
2020-12-30 18:34:08 -06:00
end
2021-01-03 05:12:28 -06:00
if rawget(_G, "skins") then
i3.modules.skins = true
2021-01-03 05:12:28 -06:00
end
2021-01-10 13:56:37 -06:00
if rawget(_G, "awards") then
i3.modules.awards = true
2021-01-10 13:56:37 -06:00
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-28 19:24:17 -06:00
core.register_on_chat_message(function(name)
2021-01-10 14:45:57 -06:00
local player = core.get_player_by_name(name)
2021-01-28 19:24:17 -06:00
set_fs(player)
2021-01-10 13:56:37 -06:00
end)
end
2020-12-30 16:21:05 -06:00
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)
i3.init_items[#i3.init_items + 1] = name
2020-12-30 16:21:05 -06:00
_select[name] = true
end
resolve_aliases(_select)
sort(i3.init_items)
2020-12-30 16:21:05 -06:00
if i3.http and type(i3.export_url) == "string" then
2020-12-30 16:21:05 -06:00
local post_data = {
recipes = i3.recipes_cache,
usages = i3.usages_cache,
2020-12-30 16:21:05 -06:00
}
i3.http.fetch_async {
2020-12-30 16:21:05 -06:00
url = i3.export_url,
2021-06-24 20:44:38 -05:00
post_data = core.write_json(post_data),
2020-12-30 16:21:05 -06:00
}
end
end
2021-06-24 20:44:38 -05:00
core.register_on_mods_loaded(function()
2020-12-30 16:21:05 -06:00
get_init_items()
if rawget(_G, "sfinv") then
2021-05-06 20:16:11 -05:00
function sfinv.set_player_inventory_formspec() return end
2020-12-30 16:21:05 -06:00
sfinv.enabled = false
end
if rawget(_G, "unified_inventory") then
2020-12-30 16:21:05 -06:00
function unified_inventory.set_inventory_formspec() return end
end
end)
2021-04-05 19:47:37 -05:00
local function init_waypoints(player)
local name = player:get_player_name()
local data = i3.data[name]
2021-04-11 16:23:03 -05:00
data.waypoints = data.waypoints or {}
2021-04-05 19:47:37 -05:00
2021-04-11 16:23:03 -05:00
for _, v in ipairs(data.waypoints) do
2021-04-11 17:38:24 -05: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 16:23:03 -05:00
2021-04-11 17:38:24 -05:00
v.id = id
2021-04-11 16:23:03 -05:00
end
2021-04-05 19:47:37 -05:00
end
end
2021-06-24 20:44:38 -05:00
core.register_on_joinplayer(function(player)
2020-12-30 17:17:38 -06:00
local name = player:get_player_name()
2021-06-24 20:44:38 -05:00
local info = core.get_player_information and core.get_player_information(name)
2021-01-21 17:42:48 -06:00
if not info or get_formspec_version(info) < i3.MIN_FORMSPEC_VERSION then
i3.data[name] = nil
2021-01-21 17:42:48 -06:00
return outdated(name)
end
init_data(player, info)
init_backpack(player)
2021-04-05 19:47:37 -05:00
init_waypoints(player)
2021-01-24 09:08:46 -06:00
core.after(0, function()
player:hud_set_hotbar_itemcount(i3.HOTBAR_LEN)
player:hud_set_hotbar_image("i3_hotbar.png")
2021-01-25 12:28:28 -06:00
end)
2021-01-21 17:42:48 -06:00
end)
2021-05-31 15:46:02 -05:00
local function save_data(player_name)
local _data = copy(i3.data)
2021-05-31 15:46:02 -05:00
for name, v in pairs(_data) do
2021-05-31 13:48:12 -05:00
for dat in pairs(v) do
if not i3.META_SAVES[dat] then
_data[name][dat] = nil
2021-05-31 15:46:02 -05:00
if player_name and i3.data[player_name] then
i3.data[player_name][dat] = nil -- To free up some memory
2021-05-31 15:46:02 -05:00
end
2021-05-31 13:48:12 -05:00
end
2020-12-30 17:17:38 -06:00
end
2021-05-31 13:48:12 -05:00
end
storage:set_string("data", slz(_data))
2021-05-31 15:46:02 -05:00
end
2021-06-24 20:44:38 -05:00
core.register_on_leaveplayer(function(player)
2021-05-31 15:46:02 -05:00
local name = player:get_player_name()
save_data(name)
2020-12-30 16:21:05 -06:00
end)
2021-06-24 20:44:38 -05:00
core.register_on_shutdown(save_data)
2021-05-31 15:46:02 -05:00
local function routine()
save_data()
core.after(i3.SAVE_INTERVAL, routine)
2021-05-31 15:46:02 -05:00
end
core.after(i3.SAVE_INTERVAL, routine)
2020-12-30 16:21:05 -06:00
if i3.progressive_mode then
2021-10-18 22:54:51 -05:00
i3.files.progressive()
2021-01-21 17:42:48 -06:00
end
2020-12-30 16:21:05 -06:00
2021-06-25 20:10:05 -05:00
--dofile(modpath .. "/tests/test_tabs.lua")
--dofile(modpath .. "/tests/test_custom_recipes.lua")