From 30d9e54803b5d71ae7b5dad72644b911e12593a9 Mon Sep 17 00:00:00 2001 From: kay27 Date: Sat, 24 Oct 2020 02:46:45 +0400 Subject: [PATCH] Implement leftovers of https://git.minetest.land/Wuzzy/MineClone2/issues/841#issuecomment-11440 --- mods/ENTITIES/mcl_item_entity/init.lua | 2 +- mods/HUD/mcl_experience/init.lua | 79 ++++++++++--------- .../locale/mcl_experience.ru.tr | 1 + mods/HUD/mcl_experience/locale/template.txt | 1 + mods/ITEMS/mcl_core/nodes_base.lua | 12 +-- mods/ITEMS/mcl_nether/init.lua | 2 +- 6 files changed, 53 insertions(+), 44 deletions(-) diff --git a/mods/ENTITIES/mcl_item_entity/init.lua b/mods/ENTITIES/mcl_item_entity/init.lua index f971d0f3..0481b534 100644 --- a/mods/ENTITIES/mcl_item_entity/init.lua +++ b/mods/ENTITIES/mcl_item_entity/init.lua @@ -227,7 +227,7 @@ function minetest.handle_node_drops(pos, drops, digger) local toolcaps if digger ~= nil then if mcl_experience.throw_experience then - local experience_amount = minetest.get_item_group(dug_node.name,"experience") + local experience_amount = minetest.get_item_group(dug_node.name,"xp") if experience_amount > 0 then mcl_experience.throw_experience(pos, experience_amount) end diff --git a/mods/HUD/mcl_experience/init.lua b/mods/HUD/mcl_experience/init.lua index bb62acbe..2eec016b 100644 --- a/mods/HUD/mcl_experience/init.lua +++ b/mods/HUD/mcl_experience/init.lua @@ -39,7 +39,7 @@ local load_data = function(player) local meta = player:get_meta() temp_pool.xp = meta:get_int("xp") or 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) + temp_pool.bar, temp_pool.bar_step, temp_pool.xp_next_level = mcl_experience.xp_to_bar(temp_pool.xp, temp_pool.level) temp_pool.last_time= minetest.get_us_time()/1000000 end @@ -132,31 +132,21 @@ minetest.register_on_shutdown(function() end) -local name function mcl_experience.get_player_xp_level(player) - name = player:get_player_name() + local name = player:get_player_name() return(pool[name].level) end -local name -local temp_pool function mcl_experience.set_player_xp_level(player,level) - name = player:get_player_name() + local name = player:get_player_name() + if level == pool[name].level then + return + end pool[name].level = level - pool[name].xp = mcl_experience.level_to_xp(level) ---todo: update bar - hud_manager.change_hud({ - player = player, - hud_name = "xp_level_fg", - element = "text", - data = tostring(level) - }) - hud_manager.change_hud({ - player = player, - hud_name = "xp_level_bg", - element = "text", - data = tostring(level) - }) + pool[name].xp, pool[name].next_level = mcl_experience.bar_to_xp(pool[name].bar, level) + hud_manager.change_hud({player = player, hud_name = "xp_level_fg", element = "text", data = tostring(level)}) + hud_manager.change_hud({player = player, hud_name = "xp_level_bg", element = "text", data = tostring(level)}) + -- we may don't update the bar end local name @@ -240,9 +230,18 @@ function mcl_experience.xp_to_bar(xp, level) local level = level or mcl_experience.xp_to_level(xp) local xp_this_level = mcl_experience.level_to_xp(level) local xp_next_level = mcl_experience.level_to_xp(level+1) - local bar = math.floor((xp-xp_this_level)/(xp_next_level-xp_this_level)*35) - return bar, xp_next_level -end + local bar_step = 36 / (xp_next_level-xp_this_level) + local bar = (xp-xp_this_level) * bar_step + return bar, bar_step, xp_next_level +end + +function mcl_experience.bar_to_xp(bar, level) + local xp_this_level = mcl_experience.level_to_xp(level) + local xp_next_level = mcl_experience.level_to_xp(level+1) + local bar_step = 36 / (xp_next_level-xp_this_level) + local xp = xp_this_level + math.floor(bar/36*(xp_next_level-xp_this_level)) + return xp, bar_step, xp_next_level +end function mcl_experience.add_experience(player, experience) local name = player:get_player_name() @@ -250,22 +249,27 @@ function mcl_experience.add_experience(player, experience) 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 - minetest.sound_play("level_up",{gain=0.2,to_player = name}) - temp_pool.last_time = minetest.get_us_time()/1000000 + if (temp_pool.xp >= temp_pool.xp_next_level) or (experience < 1) then + temp_pool.level = mcl_experience.xp_to_level(temp_pool.xp) + temp_pool.bar, temp_pool.bar_step, 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 + minetest.sound_play("level_up",{gain=0.2,to_player = name}) + temp_pool.last_time = minetest.get_us_time()/1000000 + end + 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)}) end - 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)}) - elseif minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then - 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}) + else + if minetest.get_us_time()/1000000 - temp_pool.last_time > 0.01 then + 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}) + end + temp_pool.bar = temp_pool.bar + temp_pool.bar_step * experience end if old_bar ~= temp_pool.bar then - hud_manager.change_hud({player = player, hud_name = "experience_bar", element = "number", data = temp_pool.bar}) + hud_manager.change_hud({player = player, hud_name = "experience_bar", element = "number", data = math.floor(temp_pool.bar)}) end end @@ -288,7 +292,7 @@ minetest.register_on_dieplayer(function(player) 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({player = player, hud_name = "experience_bar", element = "number", data = math.floor(temp_pool.bar)}) mcl_experience.throw_experience(player:get_pos(), xp_amount) end) @@ -516,6 +520,9 @@ minetest.register_chatcommand("xp", { return false, S("Error: Player not found") end mcl_experience.add_experience(player, xp) + local playername = player:get_player_name() +-- minetest.chat_send_player(name, "Added " .. tostring(xp) .. " XP to " .. playername .. ", they've got " .. tostring(pool[playername].xp) .. " XP, level " .. tostring(pool[playername].level)) + minetest.chat_send_player(name, S("Added @1 XP to @2, total: @3, experience level: @4", tostring(xp), playername, tostring(pool[playername].xp), tostring(pool[playername].level))) end, }) diff --git a/mods/HUD/mcl_experience/locale/mcl_experience.ru.tr b/mods/HUD/mcl_experience/locale/mcl_experience.ru.tr index 1d2195e8..2d0f579a 100644 --- a/mods/HUD/mcl_experience/locale/mcl_experience.ru.tr +++ b/mods/HUD/mcl_experience/locale/mcl_experience.ru.tr @@ -3,3 +3,4 @@ Gives [[player ] ] XP=Даёт [[игроку <игрок> []] Error: Too many parameters!=Ошибка: слишком много параметров! Error: Incorrect value of XP=Ошибка: Недопустимое значение XP Error: Player not found=Ошибка: Игрок не найден +Added @1 XP to @2, total: @3, experience level: @4=Добавляем @1 XP игроку @2, итого: @3, уровень опыта: @4 diff --git a/mods/HUD/mcl_experience/locale/template.txt b/mods/HUD/mcl_experience/locale/template.txt index 963d9dff..98b00a46 100644 --- a/mods/HUD/mcl_experience/locale/template.txt +++ b/mods/HUD/mcl_experience/locale/template.txt @@ -3,3 +3,4 @@ Gives [[player ] ] XP= Error: Too many parameters!= Error: Incorrect value of XP= Error: Player not found= +Added @1 XP to @2, total: @3, experience level: @4= diff --git a/mods/ITEMS/mcl_core/nodes_base.lua b/mods/ITEMS/mcl_core/nodes_base.lua index 0728cb39..978dde64 100644 --- a/mods/ITEMS/mcl_core/nodes_base.lua +++ b/mods/ITEMS/mcl_core/nodes_base.lua @@ -31,7 +31,7 @@ minetest.register_node("mcl_core:stone_with_coal", { tiles = {"mcl_core_coal_ore.png"}, is_ground_content = true, stack_max = 64, - groups = {pickaxey=1, building_block=1, material_stone=1, experience=1}, + groups = {pickaxey=1, building_block=1, material_stone=1, xp=1}, drop = 'mcl_core:coal_lump', sounds = mcl_sounds.node_sound_stone_defaults(), _mcl_blast_resistance = 3, @@ -77,7 +77,7 @@ minetest.register_node("mcl_core:stone_with_redstone", { tiles = {"mcl_core_redstone_ore.png"}, is_ground_content = true, stack_max = 64, - groups = {pickaxey=4, building_block=1, material_stone=1, experience=7}, + groups = {pickaxey=4, building_block=1, material_stone=1, xp=7}, drop = { items = { max_items = 1, @@ -110,7 +110,7 @@ minetest.register_node("mcl_core:stone_with_redstone_lit", { light_source = 9, is_ground_content = true, stack_max = 64, - groups = {pickaxey=4, not_in_creative_inventory=1, material_stone=1, experience=7}, + groups = {pickaxey=4, not_in_creative_inventory=1, material_stone=1, xp=7}, drop = { items = { max_items = 1, @@ -141,7 +141,7 @@ minetest.register_node("mcl_core:stone_with_lapis", { tiles = {"mcl_core_lapis_ore.png"}, is_ground_content = true, stack_max = 64, - groups = {pickaxey=3, building_block=1, material_stone=1, experience=6}, + groups = {pickaxey=3, building_block=1, material_stone=1, xp=6}, drop = { max_items = 1, items = { @@ -163,7 +163,7 @@ minetest.register_node("mcl_core:stone_with_emerald", { tiles = {"mcl_core_emerald_ore.png"}, is_ground_content = true, stack_max = 64, - groups = {pickaxey=4, building_block=1, material_stone=1, experience=6}, + groups = {pickaxey=4, building_block=1, material_stone=1, xp=6}, drop = "mcl_core:emerald", sounds = mcl_sounds.node_sound_stone_defaults(), _mcl_blast_resistance = 3, @@ -176,7 +176,7 @@ minetest.register_node("mcl_core:stone_with_diamond", { tiles = {"mcl_core_diamond_ore.png"}, is_ground_content = true, stack_max = 64, - groups = {pickaxey=4, building_block=1, material_stone=1, experience=4}, + groups = {pickaxey=4, building_block=1, material_stone=1, xp=4}, drop = "mcl_core:diamond", sounds = mcl_sounds.node_sound_stone_defaults(), _mcl_blast_resistance = 3, diff --git a/mods/ITEMS/mcl_nether/init.lua b/mods/ITEMS/mcl_nether/init.lua index ee92de02..aac92839 100644 --- a/mods/ITEMS/mcl_nether/init.lua +++ b/mods/ITEMS/mcl_nether/init.lua @@ -35,7 +35,7 @@ minetest.register_node("mcl_nether:quartz_ore", { stack_max = 64, tiles = {"mcl_nether_quartz_ore.png"}, is_ground_content = true, - groups = {pickaxey=1, building_block=1, material_stone=1, experience=3}, + groups = {pickaxey=1, building_block=1, material_stone=1, xp=3}, drop = 'mcl_nether:quartz', sounds = mcl_sounds.node_sound_stone_defaults(), _mcl_blast_resistance = 3,