Add playereffects.has_effect_type
[minetest_playereffects.git] / examples.lua
blob93c498091c0116bf8769f5de0aefe6865c305e45
1 ----- EXAMPLE EFFECT TYPES -----
2 --[[ This is just a helper function to inform the user of the chat command
3 of the result and, if successful, shows the effect ID. ]]
4 local function notify(name, retcode)
5 if(retcode == false) then
6 minetest.chat_send_player(name, "Effect application failed. Effect was NOT applied.")
7 else
8 minetest.chat_send_player(name, "Effect applied. Effect ID: "..tostring(retcode))
9 end
10 end
12 --[[ Null effect. The apply function always returns false, which means applying the
13 effect will never succeed ]]
14 playereffects.register_effect_type("null", "No effect", nil, {},
15 function()
16 return false
17 end
21 -- Makes the player screen black for 5 seconds (very experimental!)
22 playereffects.register_effect_type("blind", "Blind", nil, {},
23 function(player)
24 local hudid = player:hud_add({
25 hud_elem_type = "image",
26 position = { x=0.5, y=0.5 },
27 scale = { x=-100, y=-100 },
28 text = "playereffects_example_black.png",
30 if(hudid ~= nil) then
31 return { hudid = hudid }
32 else
33 minetest.log("error", "[playereffects] [examples] The effect \"Blind\" could not be applied. The call to hud_add(...) failed.")
34 return false
35 end
36 end,
37 function(effect, player)
38 player:hud_remove(effect.metadata.hudid)
39 end
42 -- Makes the user faster
43 playereffects.register_effect_type("high_speed", "High speed", nil, {"speed"},
44 function(player)
45 player:set_physics_override(4,nil,nil)
46 end,
48 function(effect, player)
49 player:set_physics_override(1,nil,nil)
50 end
53 -- Makes the user faster (hidden effect)
54 playereffects.register_effect_type("high_speed_hidden", "High speed", nil, {"speed"},
55 function(player)
56 player:set_physics_override(4,nil,nil)
57 end,
59 function(effect, player)
60 player:set_physics_override(1,nil,nil)
61 end,
62 true
67 -- Slows the user down
68 playereffects.register_effect_type("low_speed", "Low speed", nil, {"speed"},
69 function(player)
70 player:set_physics_override(0.25,nil,nil)
71 end,
73 function(effect, player)
74 player:set_physics_override(1,nil,nil)
75 end
78 -- Increases the jump height
79 playereffects.register_effect_type("highjump", "Greater jump height", "playereffects_example_highjump.png", {"jump"},
80 function(player)
81 player:set_physics_override(nil,2,nil)
82 end,
83 function(effect, player)
84 player:set_physics_override(nil,1,nil)
85 end
88 -- Adds the “fly” privilege. Keep the privilege even if the player dies
89 playereffects.register_effect_type("fly", "Fly mode available", "playereffects_example_fly.png", {"fly"},
90 function(player)
91 local playername = player:get_player_name()
92 local privs = minetest.get_player_privs(playername)
93 privs.fly = true
94 minetest.set_player_privs(playername, privs)
95 end,
96 function(effect, player)
97 local privs = minetest.get_player_privs(effect.playername)
98 privs.fly = nil
99 minetest.set_player_privs(effect.playername, privs)
100 end,
101 false, -- not hidden
102 false -- do NOT cancel the effect on death
105 -- Repeating effect type: Adds 1 HP per second
106 playereffects.register_effect_type("regen", "Regeneration", "heart.png", {"health"},
107 function(player)
108 player:set_hp(player:get_hp()+1)
109 end,
110 nil, nil, nil, 1
113 -- Repeating effect type: Adds 1 HP per 3 seconds
114 playereffects.register_effect_type("slowregen", "Slow Regeneration", "heart.png", {"health"},
115 function(player)
116 player:set_hp(player:get_hp()+1)
117 end,
118 nil, nil, nil, 15
122 -- Dummy effect for the stree test
123 playereffects.register_effect_type("stress", "Stress Test Effect", nil, {},
124 function(player)
125 end,
126 function(effect, player)
132 ------ Chat commands for the example effects ------
133 -- Null effect (never succeeds)
134 minetest.register_chatcommand("null", {
135 params = "",
136 description = "Does nothing.",
137 privs = {},
138 func = function(name, param)
139 local ret = playereffects.apply_effect_type("null", 5, minetest.get_player_by_name(name))
140 notify(name, ret)
141 end,
144 minetest.register_chatcommand("blind", {
145 params = "",
146 description = "Makes your screen black for a short time.",
147 privs = {},
148 func = function(name, param)
149 local ret = playereffects.apply_effect_type("blind", 5, minetest.get_player_by_name(name))
150 notify(name, ret)
151 end,
153 minetest.register_chatcommand("fast", {
154 params = "",
155 description = "Makes you fast for a short time.",
156 privs = {},
157 func = function(name, param)
158 local ret = playereffects.apply_effect_type("high_speed", 10, minetest.get_player_by_name(name))
159 notify(name, ret)
160 end,
162 minetest.register_chatcommand("hfast", {
163 params = "",
164 description = "Makes you fast for a short time (hidden effect).",
165 privs = {},
166 func = function(name, param)
167 local ret = playereffects.apply_effect_type("high_speed_hidden", 10, minetest.get_player_by_name(name))
168 notify(name, ret)
169 end,
171 minetest.register_chatcommand("slow", {
172 params = "",
173 description = "Makes you slow for a long time.",
174 privs = {},
175 func = function(name, param)
176 local ret = playereffects.apply_effect_type("low_speed", 120, minetest.get_player_by_name(name))
177 notify(name, ret)
178 end,
180 minetest.register_chatcommand("highjump", {
181 params = "",
182 description = "Makes you jump higher for a short time.",
183 privs = {},
184 func = function(name, param)
185 local ret = playereffects.apply_effect_type("highjump", 20, minetest.get_player_by_name(name))
186 notify(name, ret)
187 end,
190 minetest.register_chatcommand("fly", {
191 params = "",
192 description = "Grants you the fly privilege for a minute. You keep the effect when you die.",
193 privs = {},
194 func = function(name, param)
195 local ret = playereffects.apply_effect_type("fly", 60, minetest.get_player_by_name(name))
196 notify(name, ret)
197 end,
200 minetest.register_chatcommand("regen", {
201 params = "",
202 description = "Gives you 1 half heart per second 10 times, healing you by 5 hearts in total.",
203 privs = {},
204 func = function(name, param)
205 local ret = playereffects.apply_effect_type("regen", 10, minetest.get_player_by_name(name))
206 notify(name, ret)
207 end,
210 minetest.register_chatcommand("slowregen", {
211 params = "",
212 description = "Gives you 1 half heart every 3 seconds 10 times, healing you by 5 hearts in total.",
213 privs = {},
214 func = function(name, param)
215 local ret = playereffects.apply_effect_type("slowregen", 10, minetest.get_player_by_name(name))
216 notify(name, ret)
217 end,
220 --[[
221 Cancel all active effects
223 minetest.register_chatcommand("cancelall", {
224 params = "",
225 description = "Cancels all your effects.",
226 privs = {},
227 func = function(name, param)
228 local effects = playereffects.get_player_effects(name)
229 for e=1, #effects do
230 playereffects.cancel_effect(effects[e].effect_id)
232 minetest.chat_send_player(name, "All effects cancelled.")
233 end,
236 --[[ The stress test applies a shitload of effects at once.
237 This is used to test the performance of this mod at very large effect numbers. ]]
238 minetest.register_chatcommand("stresstest", {
239 params = "[<effects>]",
240 descriptions = "Start the stress test for Player Effects with <effects> effects.",
241 privs = {server=true},
242 func = function(name, param)
243 local player = minetest.get_player_by_name(name)
244 local max = 100
245 if(type(param)=="string") then
246 if(type(tonumber(param)) == "number") then
247 max = tonumber(param)
250 minetest.debug("[playereffects] Stress test started for "..name.." with "..max.." effects.")
251 for i=1,max do
252 playereffects.apply_effect_type("stress", math.random(6,60), player)
253 if(i%100==0) then
254 minetest.debug("[playereffects] Effect "..i.." of "..max.." applied.")
255 minetest.chat_send_player(name, "[playereffects] Effect "..i.." of "..max.." applied.")