Update for 0.4.16 settings syntax
[minetest_mana.git] / init.lua
blob7aebc0aa0dd8addc21fd46df32426a706643fa50
1 --[[
2 Mana
3 This mod adds mana to players, a special attribute
5 License: WTFPL
6 ]]
8 --[===[
9 Initialization
10 ]===]
12 local S
13 if (minetest.get_modpath("intllib")) then
14 S = intllib.Getter()
15 else
16 S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
17 end
19 mana = {}
20 mana.playerlist = {}
22 mana.settings = {}
23 mana.settings.default_max = 200
24 mana.settings.default_regen = 1
25 mana.settings.regen_timer = 0.2
28 local default_max = tonumber(minetest.settings:get("mana_default_max"))
29 if default_max ~= nil then
30 mana.settings.default_max = default_max
31 end
33 local default_regen = tonumber(minetest.settings:get("mana_default_regen"))
34 if default_regen ~= nil then
35 mana.settings.default_regen = default_regen
36 end
38 local regen_timer = tonumber(minetest.settings:get("mana_regen_timer"))
39 if regen_timer ~= nil then
40 mana.settings.regen_timer = regen_timer
41 end
42 end
45 --[===[
46 API functions
47 ]===]
49 function mana.set(playername, value)
50 if value < 0 then
51 minetest.log("info", "[mana] Warning: mana.set was called with negative value!")
52 value = 0
53 end
54 value = mana.round(value)
55 if value > mana.playerlist[playername].maxmana then
56 value = mana.playerlist[playername].maxmana
57 end
58 if mana.playerlist[playername].mana ~= value then
59 mana.playerlist[playername].mana = value
60 mana.hud_update(playername)
61 end
62 end
64 function mana.setmax(playername, value)
65 if value < 0 then
66 value = 0
67 minetest.log("info", "[mana] Warning: mana.setmax was called with negative value!")
68 end
69 value = mana.round(value)
70 if mana.playerlist[playername].maxmana ~= value then
71 mana.playerlist[playername].maxmana = value
72 if(mana.playerlist[playername].mana > value) then
73 mana.playerlist[playername].mana = value
74 end
75 mana.hud_update(playername)
76 end
77 end
79 function mana.setregen(playername, value)
80 mana.playerlist[playername].regen = value
81 end
83 function mana.get(playername)
84 return mana.playerlist[playername].mana
85 end
87 function mana.getmax(playername)
88 return mana.playerlist[playername].maxmana
89 end
91 function mana.getregen(playername)
92 return mana.playerlist[playername].regen
93 end
95 function mana.add_up_to(playername, value)
96 local t = mana.playerlist[playername]
97 value = mana.round(value)
98 if(t ~= nil and value >= 0) then
99 local excess
100 if((t.mana + value) > t.maxmana) then
101 excess = (t.mana + value) - t.maxmana
102 t.mana = t.maxmana
103 else
104 excess = 0
105 t.mana = t.mana + value
107 mana.hud_update(playername)
108 return true, excess
109 else
110 return false
114 function mana.add(playername, value)
115 local t = mana.playerlist[playername]
116 value = mana.round(value)
117 if(t ~= nil and ((t.mana + value) <= t.maxmana) and value >= 0) then
118 t.mana = t.mana + value
119 mana.hud_update(playername)
120 return true
121 else
122 return false
126 function mana.subtract(playername, value)
127 local t = mana.playerlist[playername]
128 value = mana.round(value)
129 if(t ~= nil and t.mana >= value and value >= 0) then
130 t.mana = t.mana -value
131 mana.hud_update(playername)
132 return true
133 else
134 return false
138 function mana.subtract_up_to(playername, value)
139 local t = mana.playerlist[playername]
140 value = mana.round(value)
141 if(t ~= nil and value >= 0) then
142 local missing
143 if((t.mana - value) < 0) then
144 missing = math.abs(t.mana - value)
145 t.mana = 0
146 else
147 missing = 0
148 t.mana = t.mana - value
150 mana.hud_update(playername)
151 return true, missing
152 else
153 return false
161 --[===[
162 File handling, loading data, saving data, setting up stuff for players.
163 ]===]
166 -- Load the playerlist from a previous session, if available.
168 local filepath = minetest.get_worldpath().."/mana.mt"
169 local file = io.open(filepath, "r")
170 if file then
171 minetest.log("action", "[mana] mana.mt opened.")
172 local string = file:read()
173 io.close(file)
174 if(string ~= nil) then
175 local savetable = minetest.deserialize(string)
176 mana.playerlist = savetable.playerlist
177 minetest.debug("[mana] mana.mt successfully read.")
182 function mana.save_to_file()
183 local savetable = {}
184 savetable.playerlist = mana.playerlist
186 local savestring = minetest.serialize(savetable)
188 local filepath = minetest.get_worldpath().."/mana.mt"
189 local file = io.open(filepath, "w")
190 if file then
191 file:write(savestring)
192 io.close(file)
193 minetest.log("action", "[mana] Wrote mana data into "..filepath..".")
194 else
195 minetest.log("error", "[mana] Failed to write mana data into "..filepath..".")
200 minetest.register_on_respawnplayer(function(player)
201 local playername = player:get_player_name()
202 mana.set(playername, 0)
203 mana.hud_update(playername)
204 end)
207 minetest.register_on_leaveplayer(function(player)
208 local playername = player:get_player_name()
209 if not minetest.get_modpath("hudbars") ~= nil then
210 mana.hud_remove(playername)
212 mana.save_to_file()
213 end)
215 minetest.register_on_shutdown(function()
216 minetest.log("action", "[mana] Server shuts down. Rescuing data into mana.mt")
217 mana.save_to_file()
218 end)
220 minetest.register_on_joinplayer(function(player)
221 local playername = player:get_player_name()
223 if mana.playerlist[playername] == nil then
224 mana.playerlist[playername] = {}
225 mana.playerlist[playername].mana = 0
226 mana.playerlist[playername].maxmana = mana.settings.default_max
227 mana.playerlist[playername].regen = mana.settings.default_regen
228 mana.playerlist[playername].remainder = 0
231 if minetest.get_modpath("hudbars") ~= nil then
232 hb.init_hudbar(player, "mana", mana.get(playername), mana.getmax(playername))
233 else
234 mana.hud_add(playername)
236 end)
239 --[===[
240 Mana regeneration
241 ]===]
243 mana.regen_timer = 0
245 minetest.register_globalstep(function(dtime)
246 mana.regen_timer = mana.regen_timer + dtime
247 if mana.regen_timer >= mana.settings.regen_timer then
248 local factor = math.floor(mana.regen_timer / mana.settings.regen_timer)
249 local players = minetest.get_connected_players()
250 for i=1, #players do
251 local name = players[i]:get_player_name()
252 if mana.playerlist[name] ~= nil then
253 if players[i]:get_hp() > 0 then
254 local plus = mana.playerlist[name].regen * factor
255 -- Compability check for version <= 1.0.2 which did not have the remainder field
256 if mana.playerlist[name].remainder ~= nil then
257 plus = plus + mana.playerlist[name].remainder
259 local plus_now = math.floor(plus)
260 local floor = plus - plus_now
261 if plus_now > 0 then
262 mana.add_up_to(name, plus_now)
263 else
264 mana.subtract_up_to(name, math.abs(plus_now))
266 mana.playerlist[name].remainder = floor
270 mana.regen_timer = mana.regen_timer % mana.settings.regen_timer
272 end)
274 --[===[
275 HUD functions
276 ]===]
278 if minetest.get_modpath("hudbars") ~= nil then
279 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)
281 function mana.hud_update(playername)
282 local player = minetest.get_player_by_name(playername)
283 if player ~= nil then
284 hb.change_hudbar(player, "mana", mana.get(playername), mana.getmax(playername))
288 function mana.hud_remove(playername)
291 else
292 function mana.manastring(playername)
293 return S("Mana: @1/@2", mana.get(playername), mana.getmax(playername))
296 function mana.hud_add(playername)
297 local player = minetest.get_player_by_name(playername)
298 local id = player:hud_add({
299 hud_elem_type = "text",
300 position = { x = 0.5, y=1 },
301 text = mana.manastring(playername),
302 scale = { x = 0, y = 0 },
303 alignment = { x = 1, y = 0},
304 direction = 1,
305 number = 0xFFFFFF,
306 offset = { x = -262, y = -103}
308 mana.playerlist[playername].hudid = id
309 return id
312 function mana.hud_update(playername)
313 local player = minetest.get_player_by_name(playername)
314 player:hud_change(mana.playerlist[playername].hudid, "text", mana.manastring(playername))
317 function mana.hud_remove(playername)
318 local player = minetest.get_player_by_name(playername)
319 player:hud_remove(mana.playerlist[playername].hudid)
323 --[===[
324 Helper functions
325 ]===]
326 mana.round = function(x)
327 return math.ceil(math.floor(x+0.5))