diff --git a/API.md b/API.md old mode 100644 new mode 100755 diff --git a/README.txt b/README.txt old mode 100644 new mode 100755 diff --git a/depends.txt b/depends.txt old mode 100644 new mode 100755 diff --git a/description.txt b/description.txt old mode 100644 new mode 100755 diff --git a/init.lua b/init.lua old mode 100644 new mode 100755 index 0267720..66e88b0 --- a/init.lua +++ b/init.lua @@ -14,8 +14,8 @@ mana.playerlist = {} mana.settings = {} mana.settings.default_max = 200 -mana.settings.default_regen = 1 -mana.settings.regen_timer = 0.2 +mana.settings.default_regen = 10 +mana.settings.regen_timer = 10 do local default_max = tonumber(minetest.setting_get("mana_default_max")) @@ -35,11 +35,13 @@ do end +minetest.mkdir(minetest.get_worldpath() .. "/mana/") + --[===[ API functions ]===] -function mana.set(playername, value) +function mana.set(playername, value) if value < 0 then minetest.log("info", "[mana] Warning: mana.set was called with negative value!") value = 0 @@ -108,7 +110,7 @@ function mana.add(playername, value) local t = mana.playerlist[playername] value = mana.round(value) if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then - t.mana = t.mana + value + t.mana = t.mana + value mana.hud_update(playername) return true else @@ -120,7 +122,7 @@ function mana.subtract(playername, value) local t = mana.playerlist[playername] value = mana.round(value) if(t ~= nil and t.mana >= value and value >= 0) then - t.mana = t.mana -value + t.mana = t.mana -value mana.hud_update(playername) return true else @@ -157,35 +159,39 @@ end -- Load the playerlist from a previous session, if available. -do - local filepath = minetest.get_worldpath().."/mana.mt" +function mana.load_file(playername) + local filepath = minetest.get_worldpath().."/mana/" .. playername local file = io.open(filepath, "r") if file then - minetest.log("action", "[mana] mana.mt opened.") + minetest.log("action", "[mana] File opened for player " .. playername .. ".") local string = file:read() io.close(file) if(string ~= nil) then local savetable = minetest.deserialize(string) - mana.playerlist = savetable.playerlist - minetest.debug("[mana] mana.mt successfully read.") + if savetable and type(savetable) == "table" then + minetest.log("action", "[mana] Data successfully read.") + return savetable + end end end + return {} end -function mana.save_to_file() - local savetable = {} - savetable.playerlist = mana.playerlist +mana.playerlist = {} + +function mana.save_to_file(playername) + local savetable = mana.playerlist[playername] local savestring = minetest.serialize(savetable) - local filepath = minetest.get_worldpath().."/mana.mt" + local filepath = minetest.get_worldpath().."/mana/" .. playername local file = io.open(filepath, "w") if file then file:write(savestring) io.close(file) - minetest.log("action", "[mana] Wrote mana data into "..filepath..".") + minetest.log("action", "[mana] Wrote mana data for "..playername..".") else - minetest.log("error", "[mana] Failed to write mana data into "..filepath..".") + minetest.log("error", "[mana] Failed to write mana data for "..playername..".") end end @@ -202,19 +208,21 @@ minetest.register_on_leaveplayer(function(player) if not minetest.get_modpath("hudbars") ~= nil then mana.hud_remove(playername) end - mana.save_to_file() + mana.save_to_file(playername) end) minetest.register_on_shutdown(function() - minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt") - mana.save_to_file() + minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt") + for _, pref in pairs(minetest.get_connected_players()) do + mana.save_to_file(pref:get_player_name()) + end end) minetest.register_on_joinplayer(function(player) local playername = player:get_player_name() - - if mana.playerlist[playername] == nil then - mana.playerlist[playername] = {} + + mana.playerlist[playername] = mana.load_file(playername) + if not mana.playerlist[playername].mana then mana.playerlist[playername].mana = 0 mana.playerlist[playername].maxmana = mana.settings.default_max mana.playerlist[playername].regen = mana.settings.default_regen @@ -235,41 +243,38 @@ end) mana.regen_timer = 0 -minetest.register_globalstep(function(dtime) - mana.regen_timer = mana.regen_timer + dtime - if mana.regen_timer >= mana.settings.regen_timer then - local factor = math.floor(mana.regen_timer / mana.settings.regen_timer) - local players = minetest.get_connected_players() - for i=1, #players do - local name = players[i]:get_player_name() - if mana.playerlist[name] ~= nil then - if players[i]:get_hp() > 0 then - local plus = mana.playerlist[name].regen * factor - -- Compability check for version <= 1.0.2 which did not have the remainder field - if mana.playerlist[name].remainder ~= nil then - plus = plus + mana.playerlist[name].remainder - end - local plus_now = math.floor(plus) - local floor = plus - plus_now - if plus_now > 0 then - mana.add_up_to(name, plus_now) - else - mana.subtract_up_to(name, math.abs(plus_now)) - end - mana.playerlist[name].remainder = floor +function mana_regen_step() + local players = minetest.get_connected_players() + for i=1, #players do + local name = players[i]:get_player_name() + if mana.playerlist[name] ~= nil then + if players[i]:get_hp() > 0 then + local plus = mana.playerlist[name].regen + -- Compability check for version <= 1.0.2 which did not have the remainder field + if mana.playerlist[name].remainder ~= nil then + plus = plus + mana.playerlist[name].remainder end + local plus_now = math.floor(plus) + local floor = plus - plus_now + if plus_now > 0 then + mana.add_up_to(name, plus_now) + else + mana.subtract_up_to(name, math.abs(plus_now)) + end + mana.playerlist[name].remainder = floor end end - mana.regen_timer = mana.regen_timer % mana.settings.regen_timer end -end) + minetest.after(mana.settings.regen_timer, mana_regen_step) +end +minetest.after(0, mana_regen_step) --[===[ HUD functions ]===] if minetest.get_modpath("hudbars") ~= nil then - hb.register_hudbar("mana", 0xFFFFFF, "Mana", { bar = "mana_bar.png", icon = "mana_icon.png", bgicon = "mana_bgicon.png" }, 0, mana.settings.default_max, false) + hb.register_hudbar("mana", 0xFFFFFF, "Mana", { bar = "mana_bar_purple.png", icon = "mana_icon_purple.png", bgicon = "mana_bgicon.png" }, 0, mana.settings.default_max, false) function mana.hud_update(playername) local player = minetest.get_player_by_name(playername) @@ -285,28 +290,39 @@ else function mana.manastring(playername) return string.format("Mana: %d/%d", mana.get(playername), mana.getmax(playername)) end - + function mana.hud_add(playername) local player = minetest.get_player_by_name(playername) + player:hud_add({ + hud_elem_type = "statbar", + position = {x=0.5,y=1}, + size = {x=24, y=24}, + text = "mana_icon_bg_empty.png", + number = 20, + alignment = {x=-1,y=-1}, + offset = {x=80, y=-186}, + } + ) local id = player:hud_add({ - hud_elem_type = "text", - position = { x = 0.5, y=1 }, - text = mana.manastring(playername), - scale = { x = 0, y = 0 }, - alignment = { x = 1, y = 0}, - direction = 1, - number = 0xFFFFFF, - offset = { x = -262, y = -103} - }) + hud_elem_type = "statbar", + position = {x=0.5,y=1}, + size = {x=24, y=24}, + text = "mana_icon_purple.png", + number = mana.get(playername)/10, + alignment = {x=-1,y=-1}, + offset = {x=80, y=-186}, + } + ) + mana.playerlist[playername].hudid = id return id end - + function mana.hud_update(playername) local player = minetest.get_player_by_name(playername) - player:hud_change(mana.playerlist[playername].hudid, "text", mana.manastring(playername)) + player:hud_change(mana.playerlist[playername].hudid, "number", mana.get(playername)/100) end - + function mana.hud_remove(playername) local player = minetest.get_player_by_name(playername) player:hud_remove(mana.playerlist[playername].hudid) diff --git a/textures/mana_bar.png b/textures/mana_bar.png old mode 100644 new mode 100755 diff --git a/textures/mana_bar_purple.png b/textures/mana_bar_purple.png new file mode 100755 index 0000000..89e8d8a Binary files /dev/null and b/textures/mana_bar_purple.png differ diff --git a/textures/mana_bgicon.png b/textures/mana_bgicon.png old mode 100644 new mode 100755 index ea35a9c..c4f0cdd Binary files a/textures/mana_bgicon.png and b/textures/mana_bgicon.png differ diff --git a/textures/mana_icon.png b/textures/mana_icon.png deleted file mode 100644 index d25ddff..0000000 Binary files a/textures/mana_icon.png and /dev/null differ diff --git a/textures/mana_icon_bg_empty.png b/textures/mana_icon_bg_empty.png new file mode 100755 index 0000000..45d5e4f Binary files /dev/null and b/textures/mana_icon_bg_empty.png differ diff --git a/textures/mana_icon_purple.png b/textures/mana_icon_purple.png new file mode 100755 index 0000000..f8400c7 Binary files /dev/null and b/textures/mana_icon_purple.png differ