1
0
mirror of git://repo.or.cz/minetest_mana.git synced 2025-06-29 21:50:24 +02:00

1 Commits

Author SHA1 Message Date
65abb7c67e Version MFF. 2018-09-08 00:19:16 +02:00
15 changed files with 107 additions and 95 deletions

4
API.md Normal file → Executable file
View File

@ -1,5 +1,5 @@
API documentation for Mana mod API documentation for Mana 1.1.0
============================== ================================
## Introduction ## Introduction
The API of the Mana mod allows you to set and receive The API of the Mana mod allows you to set and receive

37
README.md → README.txt Normal file → Executable file
View File

@ -1,20 +1,22 @@
# Mana [mana] Mana mod [mana]
===============
Version: 1.1.0
Note: This mod uses semantic versioning, as defined by version 2.0.0 of the SemVer standard.
See: <http://semver.org/>
* Version: 1.3.1 Description
* Note: This mod uses semantic versioning, as defined by version 2.0.0 of the SemVer standard. See: <http://semver.org/> ===========
## Description
This mod adds basic support for mana to Minetest. This mod adds basic support for mana to Minetest.
Each player will have an additional attribute: Mana. To be precise: Each player will have an additional attribute: Mana. To be precise:
* Current mana reserves: How much mana the player currently has - Current mana reserves: How much mana the player currently has
* Maximum possible mana: How much mana the player can have at maximum - Maximum possible mana: How much mana the player can have at maximum
* Mana regeneration amont: How much mana will be generated each “mana tick” (default 0.2 seconds) - Mana regeneration amont: How much mana will be generated each “mana tick” (default 0.2 seconds)
By default, each player spawns with 0/200 mana, and regenerates 1 mana per fifth of a second. All By default, each player spawns with 0/200 mana, and regenerates 1 mana per fifth of a second. All
these values can be configured with the server settings (`minetest.conf`) and it is highly advised to do these values can be configured with the server settings (`minetest.conf`) and it is highly advised to do
so if you plan to integrate this mod into a game. so if you plan to integrate this mod into a subgame.
The mana regeneration rate (the “mana tick”) is a global setting and is *not* configured on a The mana regeneration rate (the “mana tick”) is a global setting and is *not* configured on a
per-player basis. per-player basis.
@ -23,13 +25,15 @@ The mod provides a simple API to set, get, add and subtract the mana (and maximu
for setting the regeneration amount. Note that this mod itself does *not* change the gameplay in a meaningful for setting the regeneration amount. Note that this mod itself does *not* change the gameplay in a meaningful
way. You should install other mods which use the Mana mod as a dependency. way. You should install other mods which use the Mana mod as a dependency.
The API documentation is in the file `API.md`. The API documentation is in the file API.md.
If the mod “HUD bars” [`hudbars`] is installed, a blue bar will be added to the HUD showing the player's mana reserves. If the mod “HUD bars” [hudbars] is installed, a blue bar will be added to the HUD showing the player's mana reserves.
It currently only works with version 0.3.0 of this mod.
Otherwise, the mana is just shown as text. Otherwise, the mana is just shown as text.
## Configuration Configuration
=============
This mod can be configured with minetest.conf! The following settings are accepted: This mod can be configured with minetest.conf! The following settings are accepted:
@ -39,9 +43,10 @@ This mod can be configured with minetest.conf! The following settings are accept
regen value per mana tick. Default: `0.2`. This value must be positive, also try to avoid very small values regen value per mana tick. Default: `0.2`. This value must be positive, also try to avoid very small values
as those could probably stress your machine a lot. as those could probably stress your machine a lot.
License information License information
=================== ===================
* `textures/mana_icon.png`: [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/) by [Buch](http://opengameart.org/users/Buch). * textures/mana_icon.png: CC-BY by Buch <http://opengameart.org/users/Buch>.
* `textures/mana_bgicon.png`: CC BY 3.0, originally by Buch, modified by Wuzzy. * textures/mana_bgicon.png: CC-BY, originally by Buch <http://opengameart.org/users/Buch>, modified by Wuzzy.
* `textures/mana_bar.png`: [MIT License](https://opensource.org/licenses/MIT) by Wuzzy. * textures/mana_bar.png: WTFPL by Wuzzy.
* Everything else: MIT License. * Everything else: WTFPL.

1
depends.txt Executable file
View File

@ -0,0 +1 @@
hudbars?

1
description.txt Executable file
View File

@ -0,0 +1 @@
Adds a mana attribute to players, can be used as energy source for magical items, etc.

150
init.lua Normal file → Executable file
View File

@ -1,47 +1,47 @@
--[[ --[[
Mana Mana 1.0.2
This mod adds mana to players, a special attribute This mod adds mana to players, a special attribute
License: MIT License License: WTFPL
]] ]]
--[===[ --[===[
Initialization Initialization
]===] ]===]
local S = minetest.get_translator("mana")
mana = {} mana = {}
mana.playerlist = {} 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.settings:get("mana_default_max")) local default_max = tonumber(minetest.setting_get("mana_default_max"))
if default_max ~= nil then if default_max ~= nil then
mana.settings.default_max = default_max mana.settings.default_max = default_max
end end
local default_regen = tonumber(minetest.settings:get("mana_default_regen")) local default_regen = tonumber(minetest.setting_get("mana_default_regen"))
if default_regen ~= nil then if default_regen ~= nil then
mana.settings.default_regen = default_regen mana.settings.default_regen = default_regen
end end
local regen_timer = tonumber(minetest.settings:get("mana_regen_timer")) local regen_timer = tonumber(minetest.setting_get("mana_regen_timer"))
if regen_timer ~= nil then if regen_timer ~= nil then
mana.settings.regen_timer = regen_timer mana.settings.regen_timer = regen_timer
end end
end end
minetest.mkdir(minetest.get_worldpath() .. "/mana/")
--[===[ --[===[
API functions API functions
]===] ]===]
function mana.set(playername, value) function mana.set(playername, value)
if value < 0 then if value < 0 then
minetest.log("info", "[mana] Warning: mana.set was called with negative value!") minetest.log("info", "[mana] Warning: mana.set was called with negative value!")
value = 0 value = 0
@ -110,7 +110,7 @@ function mana.add(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value) value = mana.round(value)
if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then 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) mana.hud_update(playername)
return true return true
else else
@ -122,7 +122,7 @@ function mana.subtract(playername, value)
local t = mana.playerlist[playername] local t = mana.playerlist[playername]
value = mana.round(value) value = mana.round(value)
if(t ~= nil and t.mana >= value and value >= 0) then 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) mana.hud_update(playername)
return true return true
else else
@ -159,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
@ -204,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
@ -237,41 +243,38 @@ 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 local players = minetest.get_connected_players()
if mana.regen_timer >= mana.settings.regen_timer then for i=1, #players do
local factor = math.floor(mana.regen_timer / mana.settings.regen_timer) local name = players[i]:get_player_name()
local players = minetest.get_connected_players() if mana.playerlist[name] ~= nil then
for i=1, #players do if players[i]:get_hp() > 0 then
local name = players[i]:get_player_name() local plus = mana.playerlist[name].regen
if mana.playerlist[name] ~= nil then -- Compability check for version <= 1.0.2 which did not have the remainder field
if players[i]:get_hp() > 0 then if mana.playerlist[name].remainder ~= nil then
local plus = mana.playerlist[name].regen * factor plus = plus + mana.playerlist[name].remainder
-- 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
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
end end
mana.regen_timer = mana.regen_timer % mana.settings.regen_timer
end end
end) minetest.after(mana.settings.regen_timer, mana_regen_step)
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, S("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)
@ -285,30 +288,41 @@ if minetest.get_modpath("hudbars") ~= nil then
else else
function mana.manastring(playername) function mana.manastring(playername)
return S("Mana: @1/@2", mana.get(playername), mana.getmax(playername)) return string.format("Mana: %d/%d", mana.get(playername), mana.getmax(playername))
end end
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)
local player = minetest.get_player_by_name(playername) local player = minetest.get_player_by_name(playername)
player:hud_remove(mana.playerlist[playername].hudid) player:hud_remove(mana.playerlist[playername].hudid)

View File

@ -1,3 +0,0 @@
# textdomain:mana
Mana=Mana
Mana: @1/@2=Mana: @1/@2

View File

@ -1,3 +0,0 @@
# textdomain:mana
Mana=
Mana: @1/@2=

View File

@ -1,3 +0,0 @@
name = mana
description = Adds a mana attribute to players, can be used as energy source for magical items, etc.
optional_depends = hudbars

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

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