-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
122 lines (99 loc) · 4.3 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--[[
Solar Mana mod [solarmana]
==========================
A mana regeneration controller: only regenerate mana in sunlight.
Copyright (C) 2015 Ben Deutsch <ben@bendeutsch.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
]]
local time_total_regen_check = 0.5
local time_next_regen_check = time_total_regen_check
-- TODO: make this globally accessible
local mana_from_node = {
['default:goldblock'] = 0.5,
['group:wood'] = 0.2,
['default:stone'] = -0.2,
}
-- just for debugging: see below
local time_total_mana_reset = 10.0
local time_next_mana_reset = time_total_mana_reset
minetest.register_globalstep(function(dtime)
-- We do not care if this does not run as often as possible,
-- as all it does is change the regeneration to a fixed number
time_next_regen_check = time_next_regen_check - dtime
if time_next_regen_check < 0.0 then
time_next_regen_check = time_total_regen_check
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local pos = player:getpos()
local pos_y = pos.y
-- the middle of the block with the player's head
pos.y = math.floor(pos_y) + 1.5
local node = minetest.get_node(pos)
-- Currently uses 'get_node_light' to determine whether
-- a node is "in sunlight".
local light_day = minetest.get_node_light(pos, 0.5)
local light_night = minetest.get_node_light(pos, 0.0)
local light_now = minetest.get_node_light(pos) or 0
local regen_to = 0
-- simplest version checks for "full sunlight now"
if light_now >= 15 then
regen_to = 1
end
-- we can get a bit more lenience by testing whether
-- * a node is "affected by sunlight" (day > night)
-- * the node is "bright enough now"
-- However: you could deny yourself mana regeneration
-- with torches :-/
--[[
if light_day > light_night and light_now > 12 then
regen_to = 1
end
--]]
-- next, check the block we're standing on.
pos.y = math.floor(pos_y) - 0.5
node = minetest.get_node(pos)
local nodemana = mana_from_node[node.name]
for key, value in pairs(mana_from_node) do
if key:split(":")[1] == "group" then
local groupname = key:split(":")[2]
if minetest.get_node_group(node.name, groupname) > 0 then
if nodemana then
nodemana = math.max(nodemana, value) -- We get the greater one (if the node is part of 2 or more groups)
else
nodemana = value
end
end
end
end
if nodemana then
if nodemana > 0 then
regen_to = math.max(regen_to, nodemana)
else
regen_to = regen_to + nodemana -- negative, remember?
end
--print("Regen to "..regen_to.." : "..node.name)
end
mana.setregen(name, regen_to)
--print("Regen to "..regen_to.." : "..light_day.."/"..light_now.."/"..light_night)
end
end
--[[ Comment this in for testing if you have no mana sink
time_next_mana_reset = time_next_mana_reset - dtime
if time_next_mana_reset < 0.0 then
time_next_mana_reset = time_total_mana_reset
mana.set('singleplayer', 100)
print("Resetting mana")
end
--]]
end)