From 65abb7c67ea2d0fd5ec1b6f0f65fe13b27cc4880 Mon Sep 17 00:00:00 2001 From: sys4-fr Date: Sat, 8 Sep 2018 00:19:16 +0200 Subject: [PATCH] Version MFF. --- API.md | 0 README.txt | 0 depends.txt | 0 description.txt | 0 init.lua | 136 ++++++++++++++++++-------------- textures/mana_bar.png | Bin textures/mana_bar_purple.png | Bin 0 -> 82 bytes textures/mana_bgicon.png | Bin 2950 -> 221 bytes textures/mana_icon.png | Bin 2994 -> 0 bytes textures/mana_icon_bg_empty.png | Bin 0 -> 635 bytes textures/mana_icon_purple.png | Bin 0 -> 600 bytes 11 files changed, 76 insertions(+), 60 deletions(-) mode change 100644 => 100755 API.md mode change 100644 => 100755 README.txt mode change 100644 => 100755 depends.txt mode change 100644 => 100755 description.txt mode change 100644 => 100755 init.lua mode change 100644 => 100755 textures/mana_bar.png create mode 100755 textures/mana_bar_purple.png mode change 100644 => 100755 textures/mana_bgicon.png delete mode 100644 textures/mana_icon.png create mode 100755 textures/mana_icon_bg_empty.png create mode 100755 textures/mana_icon_purple.png 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 0000000000000000000000000000000000000000..89e8d8ae5837289108b3d362ce52b44e478a982e GIT binary patch literal 82 zcmeAS@N?(olHy`uVBq!ia0vp^Oh7Ea$P6Ue(rms1Ddqs55Z9SZ^V#i!J^{I0o-U3d c9J0v@K(+z{;|8uvLO>RSr>mdKI;Vst03*N;6951J literal 0 HcmV?d00001 diff --git a/textures/mana_bgicon.png b/textures/mana_bgicon.png old mode 100644 new mode 100755 index ea35a9cab89cb74351bb30082d26ccc9d78f5d4d..c4f0cdda8caaf6a13200708a709c710220a47bd1 GIT binary patch delta 204 zcmZn@zsop5vYwfNfk8u;KNv_E2l#}z0%=n)P*YPgGBPqTF#+-{EG#T7Ep2RU?CtHH zoSdAUon2g9TwPt=+}zyV-FddWas+B)E(!7rX88Rt+*)}vkQ?Ud;uunKtF_mgufc$W zbv1u?826q3TZEbwO{LP!h3%elKTvPra7|7)_F(GFJxk1m^zopr0F25&WB>pF literal 2950 zcmV;13wiX3P)EX>4Tx07!|QmUmQC*A|D*y?1({%`g-xL+`x}AiX!K z(nMjH8DJ;_4l^{dA)*2iMMMM@L4qO%jD{kyB8r88V8I@cAfUux6j4!mGqP56<>kGX zm){>}eQTe+_dRFteb%}Fki7l5ymVL!fHa~vAmcQ7uoQ$&mudEnVrUCi&%W-40ak@%snFBnkD3j81WZzQ5Khz zE#g}u)=U+qaYg)A9Gk{rW&(gBiR}UoD@nwrA|~;}Lfk~W6aXA4@hgu1iUph;f%sBx z=^43vZeo&vuFKM+o7vhj=-!;{RE|Jk6vSkuF!^k{TY6dsla~v?;+;QBMqFFEsL0l4 zw$|20=Ei1U73#lk{!NK{yGXBsKlcox^?kAZm0x;20E}5tZFYRI#qR~6V>1Bq_rKUQ z4+0=5>RbE3SNEZb=OsxX$gndp$O~2}Gii1cZ;QLyD0~q#kKOx{zMvCNhFdBkxcc6a_^`8KLY^ z-l*j$7HTzW9jX*njXHvANA;j?qDE0Os847zS_y4{wnO`%BhiWIY;+O265WVyLtjGQ zMvtT4U@#aOMh9bq@y0}9k}+#ArI`JgR?K_yPPl zex4vr&>=Vw!U)NPjf5&f3*i#sA>kE~NK_}<5`&3c;s# zLeh59VbXchJ<=;OnXFBACP$M6>atgt3H=1Y2UgM2$qd#E`@bNxY<% zq>JP#$vnwQ$&-=;lG9RnDQzh?DW=pqsT!$MQ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