Version MFF.

This commit is contained in:
sys4-fr 2018-09-08 00:19:16 +02:00
parent f2e6e6e8ad
commit 65abb7c67e
11 changed files with 76 additions and 60 deletions

0
API.md Normal file → Executable file
View File

0
README.txt Normal file → Executable file
View File

0
depends.txt Normal file → Executable file
View File

0
description.txt Normal file → Executable file
View File

88
init.lua Normal file → Executable file
View File

@ -14,8 +14,8 @@ mana.playerlist = {}
mana.settings = {} mana.settings = {}
mana.settings.default_max = 200 mana.settings.default_max = 200
mana.settings.default_regen = 1 mana.settings.default_regen = 10
mana.settings.regen_timer = 0.2 mana.settings.regen_timer = 10
do do
local default_max = tonumber(minetest.setting_get("mana_default_max")) local default_max = tonumber(minetest.setting_get("mana_default_max"))
@ -35,6 +35,8 @@ do
end end
minetest.mkdir(minetest.get_worldpath() .. "/mana/")
--[===[ --[===[
API functions API functions
]===] ]===]
@ -157,35 +159,39 @@ end
-- Load the playerlist from a previous session, if available. -- Load the playerlist from a previous session, if available.
do function mana.load_file(playername)
local filepath = minetest.get_worldpath().."/mana.mt" local filepath = minetest.get_worldpath().."/mana/" .. playername
local file = io.open(filepath, "r") local file = io.open(filepath, "r")
if file then if file then
minetest.log("action", "[mana] mana.mt opened.") minetest.log("action", "[mana] File opened for player " .. playername .. ".")
local string = file:read() local string = file:read()
io.close(file) io.close(file)
if(string ~= nil) then if(string ~= nil) then
local savetable = minetest.deserialize(string) local savetable = minetest.deserialize(string)
mana.playerlist = savetable.playerlist if savetable and type(savetable) == "table" then
minetest.debug("[mana] mana.mt successfully read.") minetest.log("action", "[mana] Data successfully read.")
return savetable
end end
end end
end
return {}
end end
function mana.save_to_file() mana.playerlist = {}
local savetable = {}
savetable.playerlist = mana.playerlist function mana.save_to_file(playername)
local savetable = mana.playerlist[playername]
local savestring = minetest.serialize(savetable) 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") local file = io.open(filepath, "w")
if file then if file then
file:write(savestring) file:write(savestring)
io.close(file) io.close(file)
minetest.log("action", "[mana] Wrote mana data into "..filepath..".") minetest.log("action", "[mana] Wrote mana data for "..playername..".")
else 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
end end
@ -202,19 +208,21 @@ minetest.register_on_leaveplayer(function(player)
if not minetest.get_modpath("hudbars") ~= nil then if not minetest.get_modpath("hudbars") ~= nil then
mana.hud_remove(playername) mana.hud_remove(playername)
end end
mana.save_to_file() mana.save_to_file(playername)
end) end)
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()
minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt") minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt")
mana.save_to_file() for _, pref in pairs(minetest.get_connected_players()) do
mana.save_to_file(pref:get_player_name())
end
end) end)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name() local playername = player:get_player_name()
if mana.playerlist[playername] == nil then mana.playerlist[playername] = mana.load_file(playername)
mana.playerlist[playername] = {} if not mana.playerlist[playername].mana then
mana.playerlist[playername].mana = 0 mana.playerlist[playername].mana = 0
mana.playerlist[playername].maxmana = mana.settings.default_max mana.playerlist[playername].maxmana = mana.settings.default_max
mana.playerlist[playername].regen = mana.settings.default_regen mana.playerlist[playername].regen = mana.settings.default_regen
@ -235,16 +243,13 @@ end)
mana.regen_timer = 0 mana.regen_timer = 0
minetest.register_globalstep(function(dtime) function mana_regen_step()
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() local players = minetest.get_connected_players()
for i=1, #players do for i=1, #players do
local name = players[i]:get_player_name() local name = players[i]:get_player_name()
if mana.playerlist[name] ~= nil then if mana.playerlist[name] ~= nil then
if players[i]:get_hp() > 0 then if players[i]:get_hp() > 0 then
local plus = mana.playerlist[name].regen * factor local plus = mana.playerlist[name].regen
-- Compability check for version <= 1.0.2 which did not have the remainder field -- Compability check for version <= 1.0.2 which did not have the remainder field
if mana.playerlist[name].remainder ~= nil then if mana.playerlist[name].remainder ~= nil then
plus = plus + mana.playerlist[name].remainder plus = plus + mana.playerlist[name].remainder
@ -260,16 +265,16 @@ minetest.register_globalstep(function(dtime)
end end
end end
end end
mana.regen_timer = mana.regen_timer % mana.settings.regen_timer minetest.after(mana.settings.regen_timer, mana_regen_step)
end end
end) minetest.after(0, mana_regen_step)
--[===[ --[===[
HUD functions HUD functions
]===] ]===]
if minetest.get_modpath("hudbars") ~= nil then 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) function mana.hud_update(playername)
local player = minetest.get_player_by_name(playername) local player = minetest.get_player_by_name(playername)
@ -288,23 +293,34 @@ else
function mana.hud_add(playername) function mana.hud_add(playername)
local player = minetest.get_player_by_name(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({ local id = player:hud_add({
hud_elem_type = "text", hud_elem_type = "statbar",
position = { x = 0.5, y=1 }, position = {x=0.5,y=1},
text = mana.manastring(playername), size = {x=24, y=24},
scale = { x = 0, y = 0 }, text = "mana_icon_purple.png",
alignment = { x = 1, y = 0}, number = mana.get(playername)/10,
direction = 1, alignment = {x=-1,y=-1},
number = 0xFFFFFF, offset = {x=80, y=-186},
offset = { x = -262, y = -103} }
}) )
mana.playerlist[playername].hudid = id mana.playerlist[playername].hudid = id
return id return id
end end
function mana.hud_update(playername) function mana.hud_update(playername)
local player = minetest.get_player_by_name(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 end
function mana.hud_remove(playername) function mana.hud_remove(playername)

0
textures/mana_bar.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 80 B

After

Width:  |  Height:  |  Size: 80 B

BIN
textures/mana_bar_purple.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 B

BIN
textures/mana_bgicon.png Normal file → Executable file

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 221 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

BIN
textures/mana_icon_bg_empty.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 635 B

BIN
textures/mana_icon_purple.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B