1
0
mirror of git://repo.or.cz/minetest_mana.git synced 2024-11-15 23:00:20 +01:00
minetest_mana/init.lua

330 lines
8.0 KiB
Lua
Raw Normal View History

2015-02-02 03:45:57 +01:00
--[[
2015-04-01 08:47:42 +02:00
Mana 1.0.2
2015-02-02 03:45:57 +01:00
This mod adds mana to players, a special attribute
License: WTFPL
]]
--[===[
Initialization
]===]
2016-08-05 17:43:55 +02:00
local S
if (minetest.get_modpath("intllib")) then
dofile(minetest.get_modpath("intllib").."/intllib.lua")
S = intllib.Getter(minetest.get_current_modname())
else
S = function ( s ) return s end
end
2015-02-02 03:45:57 +01:00
mana = {}
mana.playerlist = {}
2015-02-14 00:59:47 +01:00
mana.settings = {}
mana.settings.default_max = 200
2015-02-14 02:16:25 +01:00
mana.settings.default_regen = 1
2015-02-14 02:37:03 +01:00
mana.settings.regen_timer = 0.2
2015-02-14 00:59:47 +01:00
do
local default_max = tonumber(minetest.setting_get("mana_default_max"))
if default_max ~= nil then
mana.settings.default_max = default_max
end
2015-02-14 02:16:25 +01:00
local default_regen = tonumber(minetest.setting_get("mana_default_regen"))
if default_regen ~= nil then
mana.settings.default_regen = default_regen
end
local regen_timer = tonumber(minetest.setting_get("mana_regen_timer"))
if regen_timer ~= nil then
mana.settings.regen_timer = regen_timer
end
2015-02-14 00:59:47 +01:00
end
2015-02-02 03:45:57 +01:00
--[===[
API functions
]===]
2015-02-02 04:20:38 +01:00
function mana.set(playername, value)
if value < 0 then
minetest.log("info", "[mana] Warning: mana.set was called with negative value!")
value = 0
end
2015-02-20 15:19:57 +01:00
value = mana.round(value)
if value > mana.playerlist[playername].maxmana then
value = mana.playerlist[playername].maxmana
end
if mana.playerlist[playername].mana ~= value then
mana.playerlist[playername].mana = value
mana.hud_update(playername)
end
2015-02-02 03:45:57 +01:00
end
2015-02-02 04:20:38 +01:00
function mana.setmax(playername, value)
if value < 0 then
value = 0
minetest.log("info", "[mana] Warning: mana.setmax was called with negative value!")
end
2015-02-20 15:19:57 +01:00
value = mana.round(value)
if mana.playerlist[playername].maxmana ~= value then
mana.playerlist[playername].maxmana = value
if(mana.playerlist[playername].mana > value) then
mana.playerlist[playername].mana = value
end
mana.hud_update(playername)
end
2015-02-02 03:45:57 +01:00
end
2015-02-14 02:26:34 +01:00
function mana.setregen(playername, value)
mana.playerlist[playername].regen = value
end
2015-02-02 03:45:57 +01:00
function mana.get(playername)
2015-02-02 04:20:38 +01:00
return mana.playerlist[playername].mana
2015-02-02 03:45:57 +01:00
end
function mana.getmax(playername)
2015-02-02 04:20:38 +01:00
return mana.playerlist[playername].maxmana
2015-02-02 03:45:57 +01:00
end
2015-02-14 02:26:34 +01:00
function mana.getregen(playername)
return mana.playerlist[playername].regen
end
function mana.add_up_to(playername, value)
2015-02-02 03:45:57 +01:00
local t = mana.playerlist[playername]
2015-02-20 15:19:57 +01:00
value = mana.round(value)
2015-02-02 04:20:38 +01:00
if(t ~= nil and value >= 0) then
2015-02-02 03:45:57 +01:00
local excess
2015-02-02 04:20:38 +01:00
if((t.mana + value) > t.maxmana) then
excess = (t.mana + value) - t.maxmana
2015-02-02 03:45:57 +01:00
t.mana = t.maxmana
else
excess = 0
2015-02-02 04:20:38 +01:00
t.mana = t.mana + value
2015-02-02 03:45:57 +01:00
end
2015-02-02 04:20:38 +01:00
mana.hud_update(playername)
2015-02-02 03:45:57 +01:00
return true, excess
else
return false
end
end
function mana.add(playername, value)
local t = mana.playerlist[playername]
2015-02-20 15:19:57 +01:00
value = mana.round(value)
if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then
t.mana = t.mana + value
mana.hud_update(playername)
return true
else
return false
end
end
2015-02-02 04:20:38 +01:00
function mana.subtract(playername, value)
2015-02-02 03:45:57 +01:00
local t = mana.playerlist[playername]
2015-02-20 15:19:57 +01:00
value = mana.round(value)
2015-02-02 04:20:38 +01:00
if(t ~= nil and t.mana >= value and value >= 0) then
t.mana = t.mana -value
mana.hud_update(playername)
2015-02-02 03:45:57 +01:00
return true
else
return false
end
end
function mana.subtract_up_to(playername, value)
local t = mana.playerlist[playername]
2015-02-20 15:19:57 +01:00
value = mana.round(value)
if(t ~= nil and value >= 0) then
local missing
if((t.mana - value) < 0) then
missing = math.abs(t.mana - value)
t.mana = 0
else
missing = 0
t.mana = t.mana - value
end
mana.hud_update(playername)
return true, missing
else
return false
end
end
2015-02-02 03:45:57 +01:00
--[===[
File handling, loading data, saving data, setting up stuff for players.
]===]
-- Load the playerlist from a previous session, if available.
do
local filepath = minetest.get_worldpath().."/mana.mt"
local file = io.open(filepath, "r")
if file then
minetest.log("action", "[mana] mana.mt opened.")
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.")
end
end
end
function mana.save_to_file()
local savetable = {}
savetable.playerlist = mana.playerlist
local savestring = minetest.serialize(savetable)
local filepath = minetest.get_worldpath().."/mana.mt"
local file = io.open(filepath, "w")
if file then
file:write(savestring)
io.close(file)
minetest.log("action", "[mana] Wrote mana data into "..filepath..".")
else
minetest.log("error", "[mana] Failed to write mana data into "..filepath..".")
end
end
minetest.register_on_respawnplayer(function(player)
2015-02-02 03:45:57 +01:00
local playername = player:get_player_name()
mana.set(playername, 0)
2015-02-02 04:20:38 +01:00
mana.hud_update(playername)
2015-02-02 03:45:57 +01:00
end)
minetest.register_on_leaveplayer(function(player)
local playername = player:get_player_name()
if not minetest.get_modpath("hudbars") ~= nil then
mana.hud_remove(playername)
end
mana.save_to_file()
2015-02-02 03:45:57 +01:00
end)
minetest.register_on_shutdown(function()
minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt")
mana.save_to_file()
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 = 0
2015-02-14 00:59:47 +01:00
mana.playerlist[playername].maxmana = mana.settings.default_max
2015-02-14 02:16:25 +01:00
mana.playerlist[playername].regen = mana.settings.default_regen
2015-05-25 22:43:41 +02:00
mana.playerlist[playername].remainder = 0
2015-02-02 03:45:57 +01:00
end
if minetest.get_modpath("hudbars") ~= nil then
2015-02-10 02:49:01 +01:00
hb.init_hudbar(player, "mana", mana.get(playername), mana.getmax(playername))
else
mana.hud_add(playername)
end
2015-02-02 03:45:57 +01:00
end)
2015-02-14 02:16:25 +01:00
--[===[
Mana regeneration
]===]
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)
2015-02-14 06:04:13 +01:00
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
2015-05-25 22:43:41 +02:00
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
end
2015-02-14 06:04:13 +01:00
end
2015-02-14 02:16:25 +01:00
end
mana.regen_timer = mana.regen_timer % mana.settings.regen_timer
2015-02-14 02:16:25 +01:00
end
end)
2015-02-02 03:45:57 +01:00
--[===[
2015-02-02 04:20:38 +01:00
HUD functions
2015-02-02 03:45:57 +01:00
]===]
if minetest.get_modpath("hudbars") ~= nil then
2016-08-05 17:43:55 +02:00
hb.register_hudbar("mana", 0xFFFFFF, S("Mana"), { bar = "mana_bar.png", icon = "mana_icon.png", bgicon = "mana_bgicon.png" }, 0, mana.settings.default_max, false)
function mana.hud_update(playername)
local player = minetest.get_player_by_name(playername)
2015-02-14 02:16:25 +01:00
if player ~= nil then
hb.change_hudbar(player, "mana", mana.get(playername), mana.getmax(playername))
end
end
function mana.hud_remove(playername)
end
else
2015-02-20 15:02:06 +01:00
function mana.manastring(playername)
2016-08-05 17:43:55 +02:00
return string.format(S("Mana: %d/%d"), mana.get(playername), mana.getmax(playername))
2015-02-20 15:02:06 +01:00
end
function mana.hud_add(playername)
local player = minetest.get_player_by_name(playername)
2015-05-30 00:53:09 +02:00
local id = player:hud_add({
2015-02-20 15:02:06 +01:00
hud_elem_type = "text",
2015-05-25 21:18:13 +02:00
position = { x = 0.5, y=1 },
2015-02-20 15:02:06 +01:00
text = mana.manastring(playername),
scale = { x = 0, y = 0 },
alignment = { x = 1, y = 0},
direction = 1,
number = 0xFFFFFF,
2015-05-25 21:18:13 +02:00
offset = { x = -262, y = -103}
2015-02-20 15:02:06 +01:00
})
mana.playerlist[playername].hudid = id
return id
end
function mana.hud_update(playername)
local player = minetest.get_player_by_name(playername)
2015-05-25 21:18:13 +02:00
player:hud_change(mana.playerlist[playername].hudid, "text", mana.manastring(playername))
2015-02-20 15:02:06 +01:00
end
function mana.hud_remove(playername)
local player = minetest.get_player_by_name(playername)
player:hud_remove(mana.playerlist[playername].hudid)
end
end
2015-02-20 15:19:57 +01:00
--[===[
Helper functions
]===]
mana.round = function(x)
return math.ceil(math.floor(x+0.5))
end