forked from mt-mods/xp_redo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.lua
149 lines (121 loc) · 3.11 KB
/
functions.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
xp_redo.get_rank = function(xp)
if xp < 0 then
xp = 0
end
local result = nil
for _,rank in pairs(xp_redo.ranks) do
if xp >= rank.xp then
result = rank
end
end
return result
end
local level_up = function(player, rank)
local playername = player:get_player_name()
minetest.sound_play({name="xp_redo_generic", gain=0.25}, {to_player=playername})
local one = player:hud_add({
hud_elem_type = "image",
name = "award_bg",
scale = {x = 2, y = 1},
text = "xp_levelup_bg_default.png",
position = {x = 0.5, y = 0},
offset = {x = 0, y = 138},
alignment = {x = 0, y = -1}
})
local two = player:hud_add({
hud_elem_type = "text",
name = "award_au",
number = 0xFFFFFF,
scale = {x = 100, y = 20},
text = "Level-up!",
position = {x = 0.5, y = 0},
offset = {x = 0, y = 40},
alignment = {x = 0, y = -1}
})
local three = player:hud_add({
hud_elem_type = "text",
name = "rank_title",
number = 0xFFFFFF,
scale = {x = 100, y = 20},
text = rank.name,
position = {x = 0.5, y = 0},
offset = {x = 30, y = 100},
alignment = {x = 0, y = -1}
})
local rank_offset = {x = -1.5, y = 126}
local four = player:hud_add({
hud_elem_type = "image",
name = "award_icon",
scale = {x = 2, y = 2},
text = rank.icon,
position = {x = 0.4, y = 0},
offset = rank_offset,
alignment = {x = 0, y = -1}
})
minetest.after(4, function()
player:hud_remove(one)
player:hud_remove(two)
player:hud_remove(three)
player:hud_remove(four)
end)
end
xp_redo.get_xp = function(playername)
local player = minetest.get_player_by_name(playername)
if player == nil then
return 0
end
local currentXpStr = player:get_attribute("xp")
local currentXp = 0
if currentXpStr ~= nil then
currentXp = tonumber(currentXpStr)
if currentXp == nil or currentXp < 0 then
currentXp = 0
end
end
return currentXp
end
-- http://lua-users.org/lists/lua-l/2006-01/msg00525.html
function xp_redo.format_thousand(v)
local s = string.format("%d", math.floor(v))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos)
.. string.gsub(string.sub(s, pos+1), "(...)", "'%1")
end
xp_redo.add_xp = function(playername, xp)
local player = minetest.get_player_by_name(playername)
if player == nil then
return
end
local currentXp = xp_redo.get_xp(playername)
local sumXp = currentXp + xp
if sumXp < 0 then
sumXp = 0
end
player:set_attribute("xp", sumXp)
xp_redo.run_hook("xp_change", { playername, sumXp })
local previousRank = xp_redo.get_rank(currentXp)
local currentRank = xp_redo.get_rank(sumXp)
if currentRank and currentRank.xp > previousRank.xp then
-- level up
xp_redo.run_hook("rank_change", { playername, sumXp, currentRank })
level_up(player, currentRank)
end
return sumXp
end
xp_redo.get_next_rank = function(xp, current_rank)
if xp < 0 then
xp = 0
end
if current_rank == nil then
current_rank = xp_redo.get_rank(xp)
end
local result = nil
for _,rank in pairs(xp_redo.ranks) do
if (result ~= nil and rank.xp > current_rank.xp and rank.xp < result.xp) or
(result == nil and rank.xp > current_rank.xp) then
result = rank
end
end
return result
end