diff --git a/mods/quests/depends.txt b/mods/quests/depends.txt index 355f0a1..7866b19 100644 --- a/mods/quests/depends.txt +++ b/mods/quests/depends.txt @@ -1,3 +1,4 @@ default central_message story +xp diff --git a/mods/quests/depends.txt~ b/mods/quests/depends.txt~ index 355f0a1..7866b19 100644 --- a/mods/quests/depends.txt~ +++ b/mods/quests/depends.txt~ @@ -1,3 +1,4 @@ default central_message story +xp diff --git a/mods/quests/init.lua b/mods/quests/init.lua index b40b62f..631db56 100644 --- a/mods/quests/init.lua +++ b/mods/quests/init.lua @@ -27,7 +27,7 @@ minetest.register_node("quests:quest_block", { end if itemstack:to_table().name == meta:get_string("item") then meta:set_string("infotext", "Thank you!") - player:get_inventory():add_item("main", {name = "default:xp",count = math.random(10, 30)}) + xp.add_xp(player, math.random(3, 30)) minetest.add_particlespawner({ amount = 500, time = 5, @@ -46,7 +46,6 @@ minetest.register_node("quests:quest_block", { texture = "default_xp.png", }) minetest.remove_node(pos) - cmsg.push_message_player(player, "You got some XP!") else meta:set_string("infotext", "That isnt the item I am searching for..") end @@ -58,7 +57,7 @@ minetest.register_node("quests:map", { tiles = {"quests_map_top.png", "quests_map_top.png", "quests_map.png", "quests_map.png", "quests_map.png", "quests_map.png"}, groups = {quest = 1, cracky = 3}, on_punch = function(pos, node, player, pointed_thing) - player:get_inventory():add_item("main", {name = "default:xp", count = math.random(3, 20)}) + xp.add_xp(player, math.random(3, 30)) minetest.add_particlespawner({ amount = 500, time = 5, @@ -77,7 +76,6 @@ minetest.register_node("quests:map", { texture = "default_xp.png", }) minetest.remove_node(pos) - cmsg.push_message_player(player, "You got some XP!") end, }) diff --git a/mods/quests/init.lua~ b/mods/quests/init.lua~ index b40b62f..631db56 100644 --- a/mods/quests/init.lua~ +++ b/mods/quests/init.lua~ @@ -27,7 +27,7 @@ minetest.register_node("quests:quest_block", { end if itemstack:to_table().name == meta:get_string("item") then meta:set_string("infotext", "Thank you!") - player:get_inventory():add_item("main", {name = "default:xp",count = math.random(10, 30)}) + xp.add_xp(player, math.random(3, 30)) minetest.add_particlespawner({ amount = 500, time = 5, @@ -46,7 +46,6 @@ minetest.register_node("quests:quest_block", { texture = "default_xp.png", }) minetest.remove_node(pos) - cmsg.push_message_player(player, "You got some XP!") else meta:set_string("infotext", "That isnt the item I am searching for..") end @@ -58,7 +57,7 @@ minetest.register_node("quests:map", { tiles = {"quests_map_top.png", "quests_map_top.png", "quests_map.png", "quests_map.png", "quests_map.png", "quests_map.png"}, groups = {quest = 1, cracky = 3}, on_punch = function(pos, node, player, pointed_thing) - player:get_inventory():add_item("main", {name = "default:xp", count = math.random(3, 20)}) + xp.add_xp(player, math.random(3, 30)) minetest.add_particlespawner({ amount = 500, time = 5, @@ -77,7 +76,6 @@ minetest.register_node("quests:map", { texture = "default_xp.png", }) minetest.remove_node(pos) - cmsg.push_message_player(player, "You got some XP!") end, }) diff --git a/mods/xp/LICENSE.txt b/mods/xp/LICENSE.txt new file mode 100644 index 0000000..6f6a256 --- /dev/null +++ b/mods/xp/LICENSE.txt @@ -0,0 +1,16 @@ +License for Code +---------------- + +Copyright (C) 2016 cd2 (cdqwertz) + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +http://www.gnu.org/licenses/lgpl-2.1.html + +License for Media +----------------- + +CC-BY-SA 3.0 UNPORTED. Created by cd2 (cdqwertz) diff --git a/mods/xp/depends.txt b/mods/xp/depends.txt new file mode 100644 index 0000000..0b809ad --- /dev/null +++ b/mods/xp/depends.txt @@ -0,0 +1 @@ +central_message diff --git a/mods/xp/depends.txt~ b/mods/xp/depends.txt~ new file mode 100644 index 0000000..0b809ad --- /dev/null +++ b/mods/xp/depends.txt~ @@ -0,0 +1 @@ +central_message diff --git a/mods/xp/init.lua b/mods/xp/init.lua new file mode 100644 index 0000000..6b1255c --- /dev/null +++ b/mods/xp/init.lua @@ -0,0 +1,116 @@ +xp = {} +xp.lvl = 20 +xp.player_xp = {} +xp.player_levels = {} +xp.xp_hud = {} +xp.level_hud = {} + +xp.xp_file = minetest.get_worldpath() .. "/xp" +xp.lvl_file = minetest.get_worldpath() .. "/levels" + +function xp.add_xp(player, num) + if xp.player_xp[player:get_player_name()] then + xp.player_xp[player:get_player_name()] = xp.player_xp[player:get_player_name()] + num + else + xp.player_xp[player:get_player_name()] = num + end + + cmsg.push_message_player(player, "You got "..tostring(num).. " xp!") + + if xp.player_levels[player:get_player_name()] then + if xp.player_xp[player:get_player_name()] > xp.lvl*xp.player_levels[player:get_player_name()] then + xp.player_xp[player:get_player_name()] = 0 + xp.add_lvl(player) + end + else + xp.player_levels[player:get_player_name()] = 1 + end + print("[info] xp for player ".. player:get_player_name() .. " " .. xp.player_xp[player:get_player_name()].."/"..xp.lvl*xp.player_levels[player:get_player_name()].." = " .. (xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()])) + player:hud_change(xp.xp_hud[player:get_player_name()], "number", 20*((xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()]))) + xp.save_xp() +end + +function xp.add_lvl(player) + if xp.player_levels[player:get_player_name()] then + xp.player_levels[player:get_player_name()] = xp.player_levels[player:get_player_name()] + 1 + else + xp.player_levels[player:get_player_name()] = 1 + end + xp.save_levels() + player:hud_change(xp.level_hud[player:get_player_name()], "text", xp.player_levels[player:get_player_name()]) + cmsg.push_message_player(player, "Level up! You are now Level " .. tostring(xp.player_levels[player:get_player_name()])) +end + +minetest.register_on_joinplayer(function(player) + xp.xp_hud[player:get_player_name()] = player:hud_add({ + hud_elem_type = "statbar", + position = {x=0.5,y=1.0}, + size = {x=16, y=16}, + offset = {x=-(32*8+16), y=-(48*2+16)}, + text = "xp_xp.png", + number = 20*((xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()])), + }) + xp.level_hud[player:get_player_name()] = player:hud_add({ + hud_elem_type = "text", + position = {x=0.5,y=1}, + text = xp.player_levels[player:get_player_name()], + number = 0xFFFFFF, + alignment = {x=0.5,y=1}, + offset = {x=0, y=-(48*2+16)}, + }) +end) + +function xp.load_xp() + local input = io.open(xp.xp_file, "r") + if input then + local str = input:read() + if str then + for k, v in str.gmatch(str,"(%w+)=(%w+)") do + xp.player_xp[k] = tonumber(v) + end + end + io.close(input) + end +end + +function xp.save_xp() + if xp.player_xp then + local output = io.open(xp.xp_file, "w") + local str = "" + for k, v in pairs(xp.player_xp) do + str = str .. k .. "=" .. v .. "," + end + str = str:sub(1, #str - 1) + output:write(str) + io.close(output) + end +end + +function xp.load_levels() + local input = io.open(xp.lvl_file, "r") + if input then + local str = input:read() + if str then + for k, v in str.gmatch(str,"(%w+)=(%w+)") do + xp.player_levels[k] = tonumber(v) + end + end + io.close(input) + end +end + +function xp.save_levels() + if xp.player_xp then + local output = io.open(xp.lvl_file, "w") + local str = "" + for k, v in pairs(xp.player_levels) do + str = str .. k .. "=" .. v .. "," + end + str = str:sub(1, #str - 1) + output:write(str) + io.close(output) + end +end + +xp.load_xp() +xp.load_levels() diff --git a/mods/xp/init.lua~ b/mods/xp/init.lua~ new file mode 100644 index 0000000..6b1255c --- /dev/null +++ b/mods/xp/init.lua~ @@ -0,0 +1,116 @@ +xp = {} +xp.lvl = 20 +xp.player_xp = {} +xp.player_levels = {} +xp.xp_hud = {} +xp.level_hud = {} + +xp.xp_file = minetest.get_worldpath() .. "/xp" +xp.lvl_file = minetest.get_worldpath() .. "/levels" + +function xp.add_xp(player, num) + if xp.player_xp[player:get_player_name()] then + xp.player_xp[player:get_player_name()] = xp.player_xp[player:get_player_name()] + num + else + xp.player_xp[player:get_player_name()] = num + end + + cmsg.push_message_player(player, "You got "..tostring(num).. " xp!") + + if xp.player_levels[player:get_player_name()] then + if xp.player_xp[player:get_player_name()] > xp.lvl*xp.player_levels[player:get_player_name()] then + xp.player_xp[player:get_player_name()] = 0 + xp.add_lvl(player) + end + else + xp.player_levels[player:get_player_name()] = 1 + end + print("[info] xp for player ".. player:get_player_name() .. " " .. xp.player_xp[player:get_player_name()].."/"..xp.lvl*xp.player_levels[player:get_player_name()].." = " .. (xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()])) + player:hud_change(xp.xp_hud[player:get_player_name()], "number", 20*((xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()]))) + xp.save_xp() +end + +function xp.add_lvl(player) + if xp.player_levels[player:get_player_name()] then + xp.player_levels[player:get_player_name()] = xp.player_levels[player:get_player_name()] + 1 + else + xp.player_levels[player:get_player_name()] = 1 + end + xp.save_levels() + player:hud_change(xp.level_hud[player:get_player_name()], "text", xp.player_levels[player:get_player_name()]) + cmsg.push_message_player(player, "Level up! You are now Level " .. tostring(xp.player_levels[player:get_player_name()])) +end + +minetest.register_on_joinplayer(function(player) + xp.xp_hud[player:get_player_name()] = player:hud_add({ + hud_elem_type = "statbar", + position = {x=0.5,y=1.0}, + size = {x=16, y=16}, + offset = {x=-(32*8+16), y=-(48*2+16)}, + text = "xp_xp.png", + number = 20*((xp.player_xp[player:get_player_name()])/(xp.lvl*xp.player_levels[player:get_player_name()])), + }) + xp.level_hud[player:get_player_name()] = player:hud_add({ + hud_elem_type = "text", + position = {x=0.5,y=1}, + text = xp.player_levels[player:get_player_name()], + number = 0xFFFFFF, + alignment = {x=0.5,y=1}, + offset = {x=0, y=-(48*2+16)}, + }) +end) + +function xp.load_xp() + local input = io.open(xp.xp_file, "r") + if input then + local str = input:read() + if str then + for k, v in str.gmatch(str,"(%w+)=(%w+)") do + xp.player_xp[k] = tonumber(v) + end + end + io.close(input) + end +end + +function xp.save_xp() + if xp.player_xp then + local output = io.open(xp.xp_file, "w") + local str = "" + for k, v in pairs(xp.player_xp) do + str = str .. k .. "=" .. v .. "," + end + str = str:sub(1, #str - 1) + output:write(str) + io.close(output) + end +end + +function xp.load_levels() + local input = io.open(xp.lvl_file, "r") + if input then + local str = input:read() + if str then + for k, v in str.gmatch(str,"(%w+)=(%w+)") do + xp.player_levels[k] = tonumber(v) + end + end + io.close(input) + end +end + +function xp.save_levels() + if xp.player_xp then + local output = io.open(xp.lvl_file, "w") + local str = "" + for k, v in pairs(xp.player_levels) do + str = str .. k .. "=" .. v .. "," + end + str = str:sub(1, #str - 1) + output:write(str) + io.close(output) + end +end + +xp.load_xp() +xp.load_levels() diff --git a/mods/xp/textures/xp_xp.png b/mods/xp/textures/xp_xp.png new file mode 100644 index 0000000..4fb9dc0 Binary files /dev/null and b/mods/xp/textures/xp_xp.png differ