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 "particles.h"
28 // add_particle({pos=, velocity=, acceleration=, expirationtime=,
29 // size=, collisiondetection=, collision_removal=, vertical=,
30 // texture=, player=})
31 // pos/velocity/acceleration = {x=num, y=num, z=num}
32 // expirationtime = num (seconds)
34 // collisiondetection = bool
35 // collision_removal = bool
37 // texture = e.g."default_wood.png"
38 // animation = TileAnimation definition
40 int ModApiParticles::l_add_particle(lua_State *L)
46 pos = vel = acc = v3f(0, 0, 0);
48 float expirationtime, size;
49 expirationtime = size = 1;
51 bool collisiondetection, vertical, collision_removal;
52 collisiondetection = vertical = collision_removal = false;
53 struct TileAnimationParams animation;
54 animation.type = TAT_NONE;
57 std::string playername;
61 if (lua_gettop(L) > 1) // deprecated
63 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
64 pos = check_v3f(L, 1);
65 vel = check_v3f(L, 2);
66 acc = check_v3f(L, 3);
67 expirationtime = luaL_checknumber(L, 4);
68 size = luaL_checknumber(L, 5);
69 collisiondetection = readParam<bool>(L, 6);
70 texture = luaL_checkstring(L, 7);
71 if (lua_gettop(L) == 8) // only spawn for a single player
72 playername = luaL_checkstring(L, 8);
74 else if (lua_istable(L, 1))
76 lua_getfield(L, 1, "pos");
77 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
80 lua_getfield(L, 1, "vel");
81 if (lua_istable(L, -1)) {
82 vel = check_v3f(L, -1);
83 log_deprecated(L, "The use of vel is deprecated. "
84 "Use velocity instead");
88 lua_getfield(L, 1, "velocity");
89 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
92 lua_getfield(L, 1, "acc");
93 if (lua_istable(L, -1)) {
94 acc = check_v3f(L, -1);
95 log_deprecated(L, "The use of acc is deprecated. "
96 "Use acceleration instead");
100 lua_getfield(L, 1, "acceleration");
101 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
104 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
105 size = getfloatfield_default(L, 1, "size", 1);
106 collisiondetection = getboolfield_default(L, 1,
107 "collisiondetection", collisiondetection);
108 collision_removal = getboolfield_default(L, 1,
109 "collision_removal", collision_removal);
110 vertical = getboolfield_default(L, 1, "vertical", vertical);
112 lua_getfield(L, 1, "animation");
113 animation = read_animation_definition(L, -1);
116 texture = getstringfield_default(L, 1, "texture", "");
117 playername = getstringfield_default(L, 1, "playername", "");
119 glow = getintfield_default(L, 1, "glow", 0);
121 getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size,
122 collisiondetection, collision_removal, vertical, texture, animation, glow);
126 // add_particlespawner({amount=, time=,
130 // minexptime=, maxexptime=,
131 // minsize=, maxsize=,
132 // collisiondetection=,
133 // collision_removal=,
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
143 // texture = e.g."default_wood.png"
144 // animation = TileAnimation definition
146 int ModApiParticles::l_add_particlespawner(lua_State *L)
152 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
153 minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
154 float time, minexptime, maxexptime, minsize, maxsize;
155 time= minexptime= maxexptime= minsize= maxsize= 1;
156 bool collisiondetection, vertical, collision_removal;
157 collisiondetection = vertical = collision_removal = 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);
223 lua_getfield(L, 1, "animation");
224 animation = read_animation_definition(L, -1);
227 lua_getfield(L, 1, "attached");
228 if (!lua_isnil(L, -1)) {
229 ObjectRef *ref = ObjectRef::checkobject(L, -1);
231 attached = ObjectRef::getobject(ref);
234 vertical = getboolfield_default(L, 1, "vertical", vertical);
235 texture = getstringfield_default(L, 1, "texture", "");
236 playername = getstringfield_default(L, 1, "playername", "");
237 glow = getintfield_default(L, 1, "glow", 0);
240 u32 id = getServer(L)->addParticleSpawner(amount, time,
244 minexptime, maxexptime,
252 lua_pushnumber(L, id);
257 // delete_particlespawner(id, player)
258 // player (string) is optional
259 int ModApiParticles::l_delete_particlespawner(lua_State *L)
264 u32 id = luaL_checknumber(L, 1);
265 std::string playername;
266 if (lua_gettop(L) == 2) {
267 playername = luaL_checkstring(L, 2);
270 getServer(L)->deleteParticleSpawner(playername, id);
274 void ModApiParticles::Initialize(lua_State *L, int top)
276 API_FCT(add_particle);
277 API_FCT(add_particlespawner);
278 API_FCT(delete_particlespawner);