2021-11-03 19:36:57 +01:00
|
|
|
mcl_experience = {
|
|
|
|
on_add_xp = {},
|
2020-10-23 22:18:53 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
dofile(modpath .. "/command.lua")
|
|
|
|
dofile(modpath .. "/orb.lua")
|
|
|
|
dofile(modpath .. "/bottle.lua")
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
-- local storage
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local hud_bars = {}
|
|
|
|
local hud_levels = {}
|
|
|
|
local caches = {}
|
2021-01-04 18:06:31 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
-- helpers
|
2021-01-04 18:06:31 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function xp_to_level(xp)
|
2020-10-23 22:18:53 +02:00
|
|
|
local xp = xp or 0
|
|
|
|
local a, b, c, D
|
2021-11-03 19:36:57 +01:00
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
if xp > 1507 then
|
2021-11-03 19:36:57 +01:00
|
|
|
a, b, c = 4.5, -162.5, 2220 - xp
|
2020-10-23 22:18:53 +02:00
|
|
|
elseif xp > 352 then
|
2021-11-03 19:36:57 +01:00
|
|
|
a, b, c = 2.5, -40.5, 360 - xp
|
2020-10-23 22:18:53 +02:00
|
|
|
else
|
|
|
|
a, b, c = 1, 6, -xp
|
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
|
|
|
|
D = b * b - 4 * a * c
|
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
if D == 0 then
|
2021-11-03 19:36:57 +01:00
|
|
|
return math.floor(-b / 2 / a)
|
|
|
|
elseif D > 0 then
|
|
|
|
local v1, v2 = -b / 2 / a, math.sqrt(D) / 2 / a
|
|
|
|
return math.floor(math.max(v1 - v2, v1 + v2))
|
2020-10-23 22:18:53 +02:00
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
return 0
|
2020-10-19 14:38:21 +02:00
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function level_to_xp(level)
|
|
|
|
if level >= 1 and level <= 16 then
|
2020-10-23 22:18:53 +02:00
|
|
|
return math.floor(math.pow(level, 2) + 6 * level)
|
2021-11-03 19:36:57 +01:00
|
|
|
elseif level >= 17 and level <= 31 then
|
2020-10-23 22:18:53 +02:00
|
|
|
return math.floor(2.5 * math.pow(level, 2) - 40.5 * level + 360)
|
|
|
|
elseif level >= 32 then
|
2021-11-03 19:36:57 +01:00
|
|
|
return math.floor(4.5 * math.pow(level, 2) - 162.5 * level + 2220)
|
2020-10-23 22:18:53 +02:00
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
return 0
|
|
|
|
end
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function calculate_bounds(level)
|
|
|
|
return level_to_xp(level), level_to_xp(level + 1)
|
2020-10-24 00:46:45 +02:00
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function xp_to_bar(xp, level)
|
|
|
|
local xp_min, xp_max = calculate_bounds(level)
|
2020-10-23 22:18:53 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
return (xp - xp_min) / (xp_max - xp_min)
|
|
|
|
end
|
2021-02-12 09:20:32 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function bar_to_xp(bar, level)
|
|
|
|
local xp_min, xp_max = calculate_bounds(level)
|
2020-10-26 07:24:38 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
return xp_min + bar * (xp_max - xp_min)
|
|
|
|
end
|
2020-10-26 07:24:38 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local function get_time()
|
|
|
|
return minetest.get_us_time() / 1000000
|
|
|
|
end
|
2020-10-26 07:24:38 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
-- api
|
2020-10-23 22:18:53 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.get_level(player)
|
|
|
|
return caches[player].level
|
2020-10-19 14:38:21 +02:00
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.set_level(player, level)
|
|
|
|
local cache = caches[player]
|
|
|
|
|
|
|
|
if level ~= cache.level then
|
|
|
|
mcl_experience.set_xp(player, math.floor(bar_to_xp(xp_to_bar(mcl_experience.get_xp(player), cache.level), level)))
|
2020-10-23 22:18:53 +02:00
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
end
|
2020-10-23 22:18:53 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.get_xp(player)
|
|
|
|
return player:get_meta():get_int("xp")
|
|
|
|
end
|
2021-01-04 18:06:31 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.set_xp(player, xp)
|
|
|
|
player:get_meta():set_int("xp", xp)
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
mcl_experience.update(player)
|
|
|
|
end
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.add_xp(player, xp)
|
|
|
|
for _, cb in ipairs(mcl_experience.on_add_xp) do
|
|
|
|
xp = cb.func(player, xp) or xp
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
if xp == 0 then
|
|
|
|
break
|
2020-10-19 14:38:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local cache = caches[player]
|
|
|
|
local old_level = cache.level
|
2021-01-04 18:06:31 +01:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
mcl_experience.set_xp(player, mcl_experience.get_xp(player) + xp)
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
local current_time = get_time()
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
if current_time - cache.last_time > 0.01 then
|
|
|
|
local name = player:get_player_name()
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
if old_level == cache.level then
|
|
|
|
minetest.sound_play("mcl_experience", {
|
|
|
|
to_player = name,
|
|
|
|
gain = 0.1,
|
|
|
|
pitch = math.random(75, 99) / 100,
|
2020-10-19 14:38:21 +02:00
|
|
|
})
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
cache.last_time = current_time
|
|
|
|
else
|
|
|
|
minetest.sound_play("mcl_experience_level_up", {
|
|
|
|
to_player = name,
|
|
|
|
gain = 0.2,
|
|
|
|
})
|
2020-10-19 14:38:21 +02:00
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
cache.last_time = current_time + 0.2
|
|
|
|
end
|
2020-10-19 14:38:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.throw_xp(pos, total_xp)
|
2020-10-23 22:18:53 +02:00
|
|
|
local i, j = 0, 0
|
2022-09-08 13:16:26 +02:00
|
|
|
local obs = {}
|
2021-11-03 19:36:57 +01:00
|
|
|
while i < total_xp and j < 100 do
|
|
|
|
local xp = math.min(math.random(1, math.min(32767, total_xp - math.floor(i / 2))), total_xp - i)
|
|
|
|
local obj = minetest.add_entity(pos, "mcl_experience:orb", tostring(xp))
|
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
if not obj then
|
|
|
|
return false
|
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
|
|
|
|
obj:set_velocity(vector.new(
|
|
|
|
math.random(-2, 2) * math.random(),
|
2022-09-10 01:06:10 +02:00
|
|
|
math.random(2, 5),
|
2021-11-03 19:36:57 +01:00
|
|
|
math.random(-2, 2) * math.random()
|
|
|
|
))
|
|
|
|
|
2020-10-23 22:18:53 +02:00
|
|
|
i = i + xp
|
|
|
|
j = j + 1
|
2022-09-10 01:06:10 +02:00
|
|
|
table.insert(obs, obj)
|
2020-10-23 22:18:53 +02:00
|
|
|
end
|
2022-09-08 13:16:26 +02:00
|
|
|
return obs
|
2020-10-19 14:38:21 +02:00
|
|
|
end
|
2021-01-24 19:46:39 +01:00
|
|
|
|
2022-05-27 19:56:47 +02:00
|
|
|
function mcl_experience.remove_hud(player)
|
|
|
|
if hud_bars[player] then
|
|
|
|
player:hud_remove(hud_bars[player])
|
|
|
|
hud_bars[player] = nil
|
|
|
|
end
|
|
|
|
if hud_levels[player] then
|
|
|
|
player:hud_remove(hud_levels[player])
|
|
|
|
hud_levels[player] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_experience.setup_hud(player)
|
|
|
|
if hud_bars[player] and hud_levels[player] then return end
|
|
|
|
mcl_experience.remove_hud(player)
|
2022-05-27 15:06:25 +02:00
|
|
|
caches[player] = {
|
|
|
|
last_time = get_time(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) then
|
|
|
|
hud_bars[player] = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
2022-09-10 01:06:10 +02:00
|
|
|
position = { x = 0.5, y = 1 },
|
|
|
|
offset = { x = (-9 * 28) - 3, y = -(48 + 24 + 16 - 5) },
|
|
|
|
scale = { x = 0.35, y = 0.375 },
|
|
|
|
alignment = { x = 1, y = 1 },
|
2022-05-27 15:06:25 +02:00
|
|
|
z_index = 11,
|
|
|
|
})
|
|
|
|
|
|
|
|
hud_levels[player] = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
2022-09-10 01:06:10 +02:00
|
|
|
position = { x = 0.5, y = 1 },
|
2022-05-27 15:06:25 +02:00
|
|
|
number = 0x80FF20,
|
2022-09-10 01:06:10 +02:00
|
|
|
offset = { x = 0, y = -(48 + 24 + 24) },
|
2022-05-27 15:06:25 +02:00
|
|
|
z_index = 12,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.update(player)
|
|
|
|
local xp = mcl_experience.get_xp(player)
|
|
|
|
local cache = caches[player]
|
|
|
|
|
|
|
|
cache.level = xp_to_level(xp)
|
|
|
|
|
|
|
|
if not minetest.is_creative_enabled(player:get_player_name()) then
|
2022-05-27 15:06:25 +02:00
|
|
|
if not hud_bars[player] then
|
2022-05-27 19:56:47 +02:00
|
|
|
mcl_experience.setup_hud(player)
|
2022-05-27 15:06:25 +02:00
|
|
|
end
|
2022-10-11 20:52:30 +02:00
|
|
|
|
|
|
|
player:hud_change(hud_bars[player], "text", "(mcl_experience_bar_background.png^[lowpart:"
|
2021-11-03 19:36:57 +01:00
|
|
|
.. math.floor(math.floor(xp_to_bar(xp, cache.level) * 18) / 18 * 100)
|
2022-10-11 20:52:30 +02:00
|
|
|
.. ":mcl_experience_bar.png)^[resize:40x1456^[transformR270"
|
2021-11-03 19:36:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if cache.level == 0 then
|
|
|
|
player:hud_change(hud_levels[player], "text", "")
|
|
|
|
else
|
|
|
|
player:hud_change(hud_levels[player], "text", tostring(cache.level))
|
2021-01-24 19:46:39 +01:00
|
|
|
end
|
2021-11-03 19:36:57 +01:00
|
|
|
end
|
2021-01-24 19:46:39 +01:00
|
|
|
end
|
|
|
|
|
2021-11-03 19:36:57 +01:00
|
|
|
function mcl_experience.register_on_add_xp(func, priority)
|
2022-09-10 01:06:10 +02:00
|
|
|
table.insert(mcl_experience.on_add_xp, { func = func, priority = priority or 0 })
|
2021-11-03 19:36:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- callbacks
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2022-05-27 19:56:47 +02:00
|
|
|
mcl_experience.setup_hud(player)
|
2021-11-03 19:36:57 +01:00
|
|
|
mcl_experience.update(player)
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
2022-09-10 01:06:10 +02:00
|
|
|
hud_bars[player] = nil
|
|
|
|
hud_levels[player] = nil
|
|
|
|
caches[player] = nil
|
2021-11-03 19:36:57 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_dieplayer(function(player)
|
|
|
|
if not minetest.settings:get_bool("mcl_keepInventory", false) then
|
|
|
|
mcl_experience.throw_xp(player:get_pos(), mcl_experience.get_xp(player))
|
|
|
|
mcl_experience.set_xp(player, 0)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_on_mods_loaded(function()
|
|
|
|
table.sort(mcl_experience.on_add_xp, function(a, b) return a.priority < b.priority end)
|
|
|
|
end)
|
2022-09-10 01:06:10 +02:00
|
|
|
|
|
|
|
mcl_gamemode.register_on_gamemode_change(function(player, old_gamemode, new_gamemode)
|
|
|
|
if new_gamemode == "survival" then
|
|
|
|
mcl_experience.setup_hud(player)
|
|
|
|
mcl_experience.update(player)
|
|
|
|
elseif new_gamemode == "creative" then
|
|
|
|
mcl_experience.remove_hud(player)
|
|
|
|
end
|
|
|
|
end)
|