This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.lua
executable file
·227 lines (196 loc) · 5.33 KB
/
api.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
local stats = {}
local effects = {}
local players = {}
local eiidCounter = 0
local function nvl(v, d) if v == nil then return d end return v end
-- No, I swear I didn't do any Oracle SQL ;)
local function pnm(p) if type(p) == "string" then return p elseif type(p) == "userdata" return p:get_player_name() end return nil end
--
-- STATS
--
function playerstatus.register_stat(name, spec)
if type(name) ~= "string" or type(spec) ~= "table" or stats[name] ~= nil then
return
end
stats[name] = {
basemax = spec.basemax,
baseregen = spec.baseregen or 0,
integer = nvl(spec.integer, true),
maxchangescale = nvl(spec.maxchangescale, true),
valchangecbs = {}
}
if spec.hudbar then
stats[name].hudbar = {
text_color = spec.hudbar.text_color,
label = spec.hudbar.label,
textures = spec.hudbar.textures,
default_start_hidden = nvl(spec.hudbar.default_start_hidden, false),
format_string = spec.hudbar.format_string
}
stats[name].hudbar.visible = default_start_hidden
end
end
local function stat_invalid(name)
return type(name) ~= "string" or stats[name] == nil
end
local function plr_stat_invalid(player, name)
return player == nil or players[player] == nil or players[player].stats == nil or
stat_invalid(name) or players[player].stats[name] == nil
end
function playerstatus.get_stat_basemax(name)
if stat_invalid(name) then
return nil
end
return stats[name].basemax
end
function playerstatus.get_stat_baseregen(name)
if stat_invalid(name) then
return nil
end
return stats[name].baseregen
end
function playerstatus.get_stat_effectivemax(player, name)
player = pnm(player)
if plr_stat_invalid(player, name) then
return nil
end
return players[player].stats[name].max
end
function playerstatus.get_stat_effectiveregen(player, name)
player = pnm(player)
if plr_stat_invalid(player, name) then
return nil
end
return players[player].stats[name].regen
end
function playerstatus.get_stat_value(player, name)
player = pnm(player)
if plr_stat_invalid(player, name) then
return nil
end
if stats[name].integer then
return floor(players[player].stats[name].value)
end
return players[player].stats[name].value
end
function playerstatus.set_stat_value(player, name, value)
player = pnm(player)
if plr_stat_invalid(player, name) then
return
end
players[player].stats[name].value = value
end
function playerstatus.add_stat_value(player, name, add)
player = pnm(player)
if plr_stat_invalid(player, name) then
return
end
players[player].stats[name].value = players[player].stats[name].value + add
end
function playerstatus.add_onstatvalchange_callback(name, callback)
if stat_invalid(name) or type(callback) ~= "function" then
return nil
end
table.insert(stats[name].valchangecbs, callback)
return true
end
-- function playerstatus.set_stat_hudbar_visibility(name, visible)
-- Or something like that.
--
-- EFFECTS
--
function playerstatus.register_effect(name, spec)
if type(name) ~= "string" or type(spec) ~= "table" or effects[name] ~= nil then
return nil
end
effects[name] = {
maxapplies = nvl(spec.maxapplies, 0),
statchanges = {}
}
for k, v in pairs(spec.statchanges) do
effects[name].statchanges[k] = {
baseadd = nvl(spec.statchanges.baseadd, 0),
pct = nvl(spec.statchanges.pct, 0),
add = nvl(spec.statchanges.add, 0),
regenbaseadd = nvl(spec.statchanges.regenbaseadd, 0),
regenpct = nvl(spec.statchanges.regenpct, 0),
regenadd = nvl(spec.statchanges.regenadd, 0),
disableregen = nvl(spec.statchanges.disableregen, false)
}
end
return true
end
local function effect_invalid(name)
return type(name) ~= "string" or effects[name] == nil
end
function playerstatus.apply_effect(player, effectname)
player = pnm(player)
if player == nil or effect_invalid(effectname) then
return nil
end
local applied = 0
for k, v in ipairs(players[player].effects) do
if v == effectname then
applied = applied + 1
end
end
if applied >= effects[effectname].maxapplies then
return false
end
players[player].effects[eiidCounter] = effectname
eiidCounter = eiidCounter + 1
return eiidCounter - 1
end
function playerstatus.remove_effect(eiid)
if type(eiid) ~= "number" then
return nil
end
for player, ptable in pairs(players) do
for peiid, peffectname in pairs(ptable.effects) do
if peiid == eiid then
ptable.effects[eiid] = nil
return true
end
end
end
return nil
end
function playerstatus.remove_effect_all(effectname)
if effect_invalid(effectname) then
return nil
end
local rmCount = 0
for player, ptable in pairs(players) do
for peiid, peffectname in pairs(ptable.effects) do
if peffectname == effectname then
ptable.effects[peiid] = nil
rmCount = rmCount + 1
end
end
end
return rmCount
end
--
-- INTERNALS
--
local function update_core_values(playerName)
local player = minetest.get_player_by_name(playerName)
if not player then return end
player:set_physics_override({
speed = players[playerName].stats.speed.value,
jump = players[playerName].stats.jump.value,
gravity = players[playerName].stats.gravity.value,
})
end
minetest.register_on_joinplayer(function(player)
local playerName = player:get_player_name()
players[playerName] = {
stats = {},
effects = {}
}
playerstatus.update_physics(playerName)
end)
minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name()
players[playerName] = nil
end)