This commit is contained in:
kay27 2020-10-24 00:18:53 +04:00
parent a127136c60
commit 290893355b
4 changed files with 177 additions and 185 deletions

View File

@ -1,6 +1,6 @@
-- eXPerience mod -- eXPerience mod
-- This mod has adopted from oil_boi's Crafter-minetest -- This mod has adopted from oil_boi's Crafter-minetest
-- ( https://www.patreon.com/oil_boi ) by kay27@bk.ru -- ( https://www.patreon.com/oil_boi ) by kay27@bk.ru
-- for MineClone 2 under GNU GENERAL PUBLIC LICENSE. -- for MineClone 2 under GNU General Public License v3.0.
-- Copyright (c) Oil_boi, Wuzzy, kay27, -- Copyright (c) Oil_boi, Wuzzy, kay27,
-- experience_orb texture by github.com/Gerold55 -- experience_orb texture by github.com/Gerold55

View File

@ -1,57 +1,57 @@
local S = minetest.get_translator("mcl_experience") local S = minetest.get_translator("mcl_experience")
mcl_experience = {} mcl_experience = {}
local pool = {}
local
minetest,math,vector,os,pairs,type
=
minetest,math,vector,os,pairs,type
local storage = minetest.get_mod_storage()
local registered_nodes local registered_nodes
local gravity = {x = 0, y = -((tonumber(minetest.settings:get("movement_gravity"))) or 9.81), z = 0}
local size_min, size_max = 20, 59 -- percents
local delta_size = size_max - size_min
local size_to_xp = {
{-32768, 2}, -- 1
{ 3, 6}, -- 2
{ 7, 16}, -- 3
{ 17, 36}, -- 4
{ 37, 72}, -- 5
{ 73, 148}, -- 6
{ 149, 306}, -- 7
{ 307, 616}, -- 8
{ 617, 1236}, -- 9
{ 1237, 2476}, --10
{ 2477, 32767} --11
}
local function xp_to_size(xp)
local i, l = 1, #size_to_xp
while (xp > size_to_xp[i][1]) and (i < l) do
i = i + 1
end
return ((i-1) / (l-1) * delta_size + size_min)/100
end
minetest.register_on_mods_loaded(function() minetest.register_on_mods_loaded(function()
registered_nodes = minetest.registered_nodes registered_nodes = minetest.registered_nodes
end) end)
local pool = {}
-- loads data from mod storage
local name
local temp_pool
local load_data = function(player) local load_data = function(player)
name = player:get_player_name() local name = player:get_player_name()
pool[name] = {} pool[name] = {}
temp_pool = pool[name] local temp_pool = pool[name]
if storage:get_int(name.."xp_save") > 0 then local meta = player:get_meta()
temp_pool.xp_level = storage:get_int(name.."xp_level") temp_pool.xp = meta:get_int("xp") or 0
temp_pool.xp_bar = storage:get_int(name.."xp_bar" ) temp_pool.level = mcl_experience.xp_to_level(temp_pool.xp)
temp_pool.last_time= minetest.get_us_time()/1000000 temp_pool.bar, temp_pool.xp_next_level = mcl_experience.xp_to_bar(temp_pool.xp, temp_pool.level)
else temp_pool.last_time= minetest.get_us_time()/1000000
temp_pool.xp_level = 0
temp_pool.xp_bar = 0
temp_pool.last_time= minetest.get_us_time()/1000000
end
end end
-- saves data to be utilized on next login -- saves data to be utilized on next login
local name local save_data = function(player)
local temp_pool name = player:get_player_name()
local save_data = function(name) local temp_pool = pool[name]
if type(name) ~= "string" and name:is_player() then local meta = player:get_meta()
name = name:get_player_name() meta:set_int("xp", temp_pool.xp)
end
temp_pool = pool[name]
storage:set_int(name.."xp_level",temp_pool.xp_level)
storage:set_int(name.."xp_bar", temp_pool.xp_bar )
storage:set_int(name.."xp_save",1)
pool[name] = nil pool[name] = nil
end end
-------hud manager
local minetest = minetest
local player_huds = {} -- the list of players hud lists (3d array) local player_huds = {} -- the list of players hud lists (3d array)
hud_manager = {} -- hud manager class hud_manager = {} -- hud manager class
@ -119,7 +119,10 @@ end)
-- is used for shutdowns to save all data -- is used for shutdowns to save all data
local save_all = function() local save_all = function()
for name,_ in pairs(pool) do for name,_ in pairs(pool) do
save_data(name) local player = minetest.get_player_by_name(name)
if player then
save_data(player)
end
end end
end end
@ -132,14 +135,16 @@ end)
local name local name
function mcl_experience.get_player_xp_level(player) function mcl_experience.get_player_xp_level(player)
name = player:get_player_name() name = player:get_player_name()
return(pool[name].xp_level) return(pool[name].level)
end end
local name local name
local temp_pool local temp_pool
function mcl_experience.set_player_xp_level(player,level) function mcl_experience.set_player_xp_level(player,level)
name = player:get_player_name() name = player:get_player_name()
pool[name].xp_level = level pool[name].level = level
pool[name].xp = mcl_experience.level_to_xp(level)
--todo: update bar
hud_manager.change_hud({ hud_manager.change_hud({
player = player, player = player,
hud_name = "xp_level_fg", hud_name = "xp_level_fg",
@ -154,16 +159,6 @@ function mcl_experience.set_player_xp_level(player,level)
}) })
end end
minetest.hud_replace_builtin("health",{
hud_elem_type = "statbar",
position = {x = 0.5, y = 1},
text = "heart.png",
number = core.PLAYER_MAX_HP_DEFAULT,
direction = 0,
size = {x = 24, y = 24},
offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 38)},
})
local name local name
local temp_pool local temp_pool
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
@ -173,101 +168,105 @@ minetest.register_on_joinplayer(function(player)
name = player:get_player_name() name = player:get_player_name()
temp_pool = pool[name] temp_pool = pool[name]
hud_manager.add_hud(player,"experience_bar_background",{ hud_manager.add_hud(player, "experience_bar_background",
hud_elem_type = "statbar", {
position = {x=0.5, y=1}, hud_elem_type = "statbar", position = {x=0.5, y=1},
name = "experience bar background", name = "experience bar background", text = "experience_bar_background.png",
text = "experience_bar_background.png", number = 36, direction = 0,
number = 36, offset = {x = (-8 * 28) - 29, y = -(48 + 24 + 16)},
direction = 0, size = { x=28, y=28 }, z_index = 3,
offset = {x = (-8 * 28) - 29, y = -(48 + 24 + 16)},
size = { x=28, y=28 },
z_index = 0,
}) })
hud_manager.add_hud(player,"experience_bar",{ hud_manager.add_hud(player,"experience_bar",
hud_elem_type = "statbar", {
position = {x=0.5, y=1}, hud_elem_type = "statbar", position = {x=0.5, y=1},
name = "experience bar", name = "experience bar", text = "experience_bar.png",
text = "experience_bar.png", number = temp_pool.bar, direction = 0,
number = temp_pool.xp_bar, offset = {x = (-8 * 28) - 29, y = -(48 + 24 + 16)},
direction = 0, size = { x=28, y=28 }, z_index = 4,
offset = {x = (-8 * 28) - 29, y = -(48 + 24 + 16)}, })
size = { x=28, y=28 },
z_index = 0,
})
hud_manager.add_hud(player,"xp_level_bg",{ hud_manager.add_hud(player,"xp_level_bg",
hud_elem_type = "text", {
position = {x=0.5, y=1}, hud_elem_type = "text", position = {x=0.5, y=1},
name = "xp_level_bg", name = "xp_level_bg", text = tostring(temp_pool.level),
text = tostring(temp_pool.xp_level), number = 0x000000,
number = 0x000000, offset = {x = 0, y = -(48 + 24 + 24)},
offset = {x = 0, y = -(48 + 24 + 24)}, z_index = 5,
z_index = 0, })
})
hud_manager.add_hud(player,"xp_level_fg",{ hud_manager.add_hud(player,"xp_level_fg",
hud_elem_type = "text", {
position = {x=0.5, y=1}, hud_elem_type = "text", position = {x=0.5, y=1},
name = "xp_level_fg", name = "xp_level_fg", text = tostring(temp_pool.level),
text = tostring(temp_pool.xp_level), number = 0xFFFFFF,
number = 0xFFFFFF, offset = {x = -1, y = -(48 + 24 + 25)},
offset = {x = -1, y = -(48 + 24 + 25)}, z_index = 6,
z_index = 0,
}) })
end) end)
function mcl_experience.xp_to_level(xp)
local name local xp = xp or 0
local temp_pool local a, b, c, D
local function level_up_experience(player) if xp > 1507 then
name = player:get_player_name() a, b, c = 4.5, -162.5, 2220-xp
temp_pool = pool[name] elseif xp > 352 then
a, b, c = 2.5, -40.5, 360-xp
temp_pool.xp_level = temp_pool.xp_level + 1 else
a, b, c = 1, 6, -xp
hud_manager.change_hud({ end
player = player, D = b*b-4*a*c
hud_name = "xp_level_fg", if D == 0 then
element = "text", return math.floor(-b/2/a)
data = tostring(temp_pool.xp_level) elseif D > 0 then
}) local v1, v2 = -b/2/a, math.sqrt(D)/2/a
hud_manager.change_hud({ return math.floor((math.max(v1-v2, v1+v2)))
player = player, end
hud_name = "xp_level_bg", return 0
element = "text",
data = tostring(temp_pool.xp_level)
})
end end
function mcl_experience.level_to_xp(level)
if (level >= 1 and level <= 16) then
return math.floor(math.pow(level, 2) + 6 * level)
elseif (level >= 17 and level <= 31) then
return math.floor(2.5 * math.pow(level, 2) - 40.5 * level + 360)
elseif level >= 32 then
return math.floor(4.5 * math.pow(level, 2) - 162.5 * level + 2220);
end
return 0
end
local name function mcl_experience.xp_to_bar(xp, level)
local temp_pool local level = level or mcl_experience.xp_to_level(xp)
function mcl_experience.add_experience(player,experience) local xp_this_level = mcl_experience.level_to_xp(level)
name = player:get_player_name() local xp_next_level = mcl_experience.level_to_xp(level+1)
temp_pool = pool[name] local bar = math.floor((xp-xp_this_level)/(xp_next_level-xp_this_level)*35)
return bar, xp_next_level
temp_pool.xp_bar = temp_pool.xp_bar + experience end
if temp_pool.xp_bar > 36 then function mcl_experience.add_experience(player, experience)
local name = player:get_player_name()
local temp_pool = pool[name]
local old_bar, old_xp, old_level = temp_pool.bar, temp_pool.xp, temp_pool.level
temp_pool.xp = math.max(temp_pool.xp + experience, 0)
temp_pool.level = mcl_experience.xp_to_level(temp_pool.xp)
temp_pool.bar, temp_pool.xp_next_level = mcl_experience.xp_to_bar(temp_pool.xp, temp_pool.level)
if old_level ~= temp_pool.level then
if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.04 then if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.04 then
minetest.sound_play("level_up",{gain=0.2,to_player = name}) minetest.sound_play("level_up",{gain=0.2,to_player = name})
temp_pool.last_time = minetest.get_us_time()/1000000 temp_pool.last_time = minetest.get_us_time()/1000000
end end
temp_pool.xp_bar = temp_pool.xp_bar - 36 hud_manager.change_hud({player = player, hud_name = "xp_level_fg", element = "text", data = tostring(temp_pool.level)})
level_up_experience(player) hud_manager.change_hud({player = player, hud_name = "xp_level_bg", element = "text", data = tostring(temp_pool.level)})
else elseif minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then
if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then temp_pool.last_time = minetest.get_us_time()/1000000
temp_pool.last_time = minetest.get_us_time()/1000000 minetest.sound_play("experience",{gain=0.1,to_player = name,pitch=math.random(75,99)/100})
minetest.sound_play("experience",{gain=0.1,to_player = name,pitch=math.random(75,99)/100}) end
end
if old_bar ~= temp_pool.bar then
hud_manager.change_hud({player = player, hud_name = "experience_bar", element = "number", data = temp_pool.bar})
end end
hud_manager.change_hud({
player = player,
hud_name = "experience_bar",
element = "number",
data = temp_pool.xp_bar
})
end end
--reset player level --reset player level
@ -275,35 +274,23 @@ local name
local temp_pool local temp_pool
local xp_amount local xp_amount
minetest.register_on_dieplayer(function(player) minetest.register_on_dieplayer(function(player)
if minetest.settings:get_bool("mcl_keepInventory", false) then
return
end
name = player:get_player_name() name = player:get_player_name()
temp_pool = pool[name] temp_pool = pool[name]
xp_amount = temp_pool.xp_level xp_amount = temp_pool.xp
temp_pool.xp_bar = 0 temp_pool.bar = 0
temp_pool.xp_level = 0 temp_pool.level = 0
temp_pool.xp = 0
hud_manager.change_hud({player = player, hud_name = "xp_level_fg", element = "text", data = tostring(temp_pool.level)})
hud_manager.change_hud({player = player, hud_name = "xp_level_bg", element = "text", data = tostring(temp_pool.level)})
hud_manager.change_hud({player = player, hud_name = "experience_bar", element = "number", data = temp_pool.bar})
hud_manager.change_hud({ mcl_experience.throw_experience(player:get_pos(), xp_amount)
player = player,
hud_name = "xp_level_fg",
element = "text",
data = tostring(temp_pool.xp_level)
})
hud_manager.change_hud({
player = player,
hud_name = "xp_level_bg",
element = "text",
data = tostring(temp_pool.xp_level)
})
hud_manager.change_hud({
player = player,
hud_name = "experience_bar",
element = "number",
data = temp_pool.xp_bar
})
mcl_experience.throw_experience(player:get_pos(), xp_amount)
end) end)
@ -349,7 +336,7 @@ local function xp_step(self, dtime)
acceleration = vector.new(goal.x-currentvel.x,goal.y-currentvel.y,goal.z-currentvel.z) acceleration = vector.new(goal.x-currentvel.x,goal.y-currentvel.y,goal.z-currentvel.z)
self.object:add_velocity(vector.add(acceleration,player_velocity)) self.object:add_velocity(vector.add(acceleration,player_velocity))
elseif distance < 0.4 then elseif distance < 0.4 then
mcl_experience.add_experience(collector,2) mcl_experience.add_experience(collector, self._xp)
self.object:remove() self.object:remove()
end end
return return
@ -420,7 +407,7 @@ local function xp_step(self, dtime)
self.slippery_state = is_slippery self.slippery_state = is_slippery
if is_moving then if is_moving then
self.object:set_acceleration({x = 0, y = -9.81, z = 0}) self.object:set_acceleration(gravity)
else else
self.object:set_acceleration({x = 0, y = 0, z = 0}) self.object:set_acceleration({x = 0, y = 0, z = 0})
self.object:set_velocity({x = 0, y = 0, z = 0}) self.object:set_velocity({x = 0, y = 0, z = 0})
@ -466,9 +453,11 @@ minetest.register_entity("mcl_experience:orb", {
)) ))
self.object:set_armor_groups({immortal = 1}) self.object:set_armor_groups({immortal = 1})
self.object:set_velocity({x = 0, y = 2, z = 0}) self.object:set_velocity({x = 0, y = 2, z = 0})
self.object:set_acceleration({x = 0, y = -9.81, z = 0}) self.object:set_acceleration(gravity)
size = math.random(20,36)/100 local xp = tonumber(staticdata)
self.object:set_properties({ self._xp = xp
size = xp_to_size(xp)
self.object:set_properties({
visual_size = {x = size, y = size}, visual_size = {x = size, y = size},
glow = 14, glow = 14,
}) })
@ -480,7 +469,7 @@ minetest.register_entity("mcl_experience:orb", {
self.physical_state = true self.physical_state = true
self.object:set_properties({physical = true}) self.object:set_properties({physical = true})
self.object:set_velocity({x=0, y=0, z=0}) self.object:set_velocity({x=0, y=0, z=0})
self.object:set_acceleration({x=0, y=-9.81, z=0}) self.object:set_acceleration(gravity)
end end
end, end,
@ -497,10 +486,9 @@ minetest.register_entity("mcl_experience:orb", {
end, end,
}) })
minetest.register_chatcommand("xp", { minetest.register_chatcommand("xp", {
params = S("[<player>] [<xp>]"), params = S("[<player>] [<xp>]"),
description = S("Gives [player <player>] [<xp>] XP"), description = S("Gives [[player <player>] <xp>] XP"),
privs = {server=true}, privs = {server=true},
func = function(name, params) func = function(name, params)
local player, xp = nil, 1000 local player, xp = nil, 1000
@ -521,27 +509,31 @@ minetest.register_chatcommand("xp", {
if i == 2 then if i == 2 then
player = minetest.get_player_by_name(P[1]) player = minetest.get_player_by_name(P[1])
end end
if (not xp) or (xp < 1) then if not xp then
return false, S("Error: Incorrect number of XP") return false, S("Error: Incorrect value of XP")
end end
if not player then if not player then
return false, S("Error: Player not found") return false, S("Error: Player not found")
end end
local pos = player:get_pos() mcl_experience.add_experience(player, xp)
pos.y = pos.y + 1.2
mcl_experience.throw_experience(pos, xp)
end, end,
}) })
function mcl_experience.throw_experience(pos, amount) function mcl_experience.throw_experience(pos, amount)
for i = 1,amount do local i, j = 0, 0
object = minetest.add_entity(pos, "mcl_experience:orb") local obj, xp
if object then while i < amount and j < 100 do
object:set_velocity({ xp = math.min(math.random(1, math.min(32767, amount-math.floor(i/2))), amount-i)
x=math.random(-2,2)*math.random(), obj = minetest.add_entity(pos, "mcl_experience:orb", tostring(xp))
y=math.random(2,5), if not obj then
z=math.random(-2,2)*math.random() return false
}) end
end obj:set_velocity({
end x=math.random(-2,2)*math.random(),
y=math.random(2,5),
z=math.random(-2,2)*math.random()
})
i = i + xp
j = j + 1
end
end end

View File

@ -1,5 +1,5 @@
[<player>] [<xp>]=[<игрок>] [<xp>] [<player>] [<xp>]=[<игрок>] [<xp>]
Gives [player <player>] [<xp>] XP=Даёт [игроку <игрок>] [<xp>] Gives [[player <player>] <xp>] XP=Даёт [[игроку <игрок> [<xp>]] единиц опыта XP
Error: Too many parameters!=Ошибка: слишком много параметров! Error: Too many parameters!=Ошибка: слишком много параметров!
Error: Incorrect number of XP=Ошибка: Неправильное число XP Error: Incorrect value of XP=Ошибка: Недопустимое значение XP
Error: Player not found=Ошибка: Игрок не найден Error: Player not found=Ошибка: Игрок не найден

View File

@ -1,5 +1,5 @@
[<player>] [<xp>]= [<player>] [<xp>]=
Gives [player <player>] [<xp>] XP= Gives [[player <player>] <xp>] XP=
Error: Too many parameters!= Error: Too many parameters!=
Error: Incorrect number of XP= Error: Incorrect value of XP=
Error: Player not found= Error: Player not found=