3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_particles.h"
21 #include "lua_api/l_object.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
26 #include "client/particles.h"
28 // add_particle({pos=, velocity=, acceleration=, expirationtime=,
29 // size=, collisiondetection=, collision_removal=, object_collision=,
30 // vertical=, texture=, player=})
31 // pos/velocity/acceleration = {x=num, y=num, z=num}
32 // expirationtime = num (seconds)
34 // collisiondetection = bool
35 // collision_removal = bool
36 // object_collision = bool
38 // texture = e.g."default_wood.png"
39 // animation = TileAnimation definition
41 int ModApiParticles::l_add_particle(lua_State *L)
47 float expirationtime, size;
48 expirationtime = size = 1;
49 bool collisiondetection, vertical, collision_removal, object_collision;
50 collisiondetection = vertical = collision_removal = object_collision = false;
51 struct TileAnimationParams animation;
52 animation.type = TAT_NONE;
54 std::string playername;
57 if (lua_gettop(L) > 1) // deprecated
59 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
60 pos = check_v3f(L, 1);
61 vel = check_v3f(L, 2);
62 acc = check_v3f(L, 3);
63 expirationtime = luaL_checknumber(L, 4);
64 size = luaL_checknumber(L, 5);
65 collisiondetection = readParam<bool>(L, 6);
66 texture = luaL_checkstring(L, 7);
67 if (lua_gettop(L) == 8) // only spawn for a single player
68 playername = luaL_checkstring(L, 8);
70 else if (lua_istable(L, 1))
72 lua_getfield(L, 1, "pos");
73 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
76 lua_getfield(L, 1, "vel");
77 if (lua_istable(L, -1)) {
78 vel = check_v3f(L, -1);
79 log_deprecated(L, "The use of vel is deprecated. "
80 "Use velocity instead");
84 lua_getfield(L, 1, "velocity");
85 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
88 lua_getfield(L, 1, "acc");
89 if (lua_istable(L, -1)) {
90 acc = check_v3f(L, -1);
91 log_deprecated(L, "The use of acc is deprecated. "
92 "Use acceleration instead");
96 lua_getfield(L, 1, "acceleration");
97 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
100 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
101 size = getfloatfield_default(L, 1, "size", 1);
102 collisiondetection = getboolfield_default(L, 1,
103 "collisiondetection", collisiondetection);
104 collision_removal = getboolfield_default(L, 1,
105 "collision_removal", collision_removal);
106 object_collision = getboolfield_default(L, 1,
107 "object_collision", object_collision);
108 vertical = getboolfield_default(L, 1, "vertical", vertical);
110 lua_getfield(L, 1, "animation");
111 animation = read_animation_definition(L, -1);
114 texture = getstringfield_default(L, 1, "texture", "");
115 playername = getstringfield_default(L, 1, "playername", "");
117 glow = getintfield_default(L, 1, "glow", 0);
119 getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size,
120 collisiondetection, collision_removal, object_collision, vertical,
121 texture, animation, glow);
125 // add_particlespawner({amount=, time=,
129 // minexptime=, maxexptime=,
130 // minsize=, maxsize=,
131 // collisiondetection=,
132 // collision_removal=,
133 // object_collision=,
137 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
138 // minexptime/maxexptime = num (seconds)
139 // minsize/maxsize = num
140 // collisiondetection = bool
141 // collision_removal = bool
142 // object_collision = bool
144 // texture = e.g."default_wood.png"
145 // animation = TileAnimation definition
147 int ModApiParticles::l_add_particlespawner(lua_State *L)
153 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
154 float time, minexptime, maxexptime, minsize, maxsize;
155 time = minexptime = maxexptime = minsize = maxsize = 1;
156 bool collisiondetection, vertical, collision_removal, object_collision;
157 collisiondetection = vertical = collision_removal = object_collision = false;
158 struct TileAnimationParams animation;
159 animation.type = TAT_NONE;
160 ServerActiveObject *attached = NULL;
162 std::string playername;
165 if (lua_gettop(L) > 1) //deprecated
167 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
168 amount = luaL_checknumber(L, 1);
169 time = luaL_checknumber(L, 2);
170 minpos = check_v3f(L, 3);
171 maxpos = check_v3f(L, 4);
172 minvel = check_v3f(L, 5);
173 maxvel = check_v3f(L, 6);
174 minacc = check_v3f(L, 7);
175 maxacc = check_v3f(L, 8);
176 minexptime = luaL_checknumber(L, 9);
177 maxexptime = luaL_checknumber(L, 10);
178 minsize = luaL_checknumber(L, 11);
179 maxsize = luaL_checknumber(L, 12);
180 collisiondetection = readParam<bool>(L, 13);
181 texture = luaL_checkstring(L, 14);
182 if (lua_gettop(L) == 15) // only spawn for a single player
183 playername = luaL_checkstring(L, 15);
185 else if (lua_istable(L, 1))
187 amount = getintfield_default(L, 1, "amount", amount);
188 time = getfloatfield_default(L, 1, "time", time);
190 lua_getfield(L, 1, "minpos");
191 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
194 lua_getfield(L, 1, "maxpos");
195 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
198 lua_getfield(L, 1, "minvel");
199 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
202 lua_getfield(L, 1, "maxvel");
203 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
206 lua_getfield(L, 1, "minacc");
207 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
210 lua_getfield(L, 1, "maxacc");
211 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
214 minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
215 maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
216 minsize = getfloatfield_default(L, 1, "minsize", minsize);
217 maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
218 collisiondetection = getboolfield_default(L, 1,
219 "collisiondetection", collisiondetection);
220 collision_removal = getboolfield_default(L, 1,
221 "collision_removal", collision_removal);
222 object_collision = getboolfield_default(L, 1,
223 "object_collision", object_collision);
225 lua_getfield(L, 1, "animation");
226 animation = read_animation_definition(L, -1);
229 lua_getfield(L, 1, "attached");
230 if (!lua_isnil(L, -1)) {
231 ObjectRef *ref = ObjectRef::checkobject(L, -1);
233 attached = ObjectRef::getobject(ref);
236 vertical = getboolfield_default(L, 1, "vertical", vertical);
237 texture = getstringfield_default(L, 1, "texture", "");
238 playername = getstringfield_default(L, 1, "playername", "");
239 glow = getintfield_default(L, 1, "glow", 0);
242 u32 id = getServer(L)->addParticleSpawner(amount, time,
246 minexptime, maxexptime,
255 lua_pushnumber(L, id);
260 // delete_particlespawner(id, player)
261 // player (string) is optional
262 int ModApiParticles::l_delete_particlespawner(lua_State *L)
267 u32 id = luaL_checknumber(L, 1);
268 std::string playername;
269 if (lua_gettop(L) == 2) {
270 playername = luaL_checkstring(L, 2);
273 getServer(L)->deleteParticleSpawner(playername, id);
277 void ModApiParticles::Initialize(lua_State *L, int top)
279 API_FCT(add_particle);
280 API_FCT(add_particlespawner);
281 API_FCT(delete_particlespawner);