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_internal.h"
22 #include "common/c_converter.h"
25 // add_particle({pos=, velocity=, acceleration=, expirationtime=,
26 // size=, collisiondetection=, vertical=, texture=, player=})
27 // pos/velocity/acceleration = {x=num, y=num, z=num}
28 // expirationtime = num (seconds)
30 // collisiondetection = bool
32 // texture = e.g."default_wood.png"
33 int ModApiParticles::l_add_particle(lua_State *L)
39 pos = vel = acc = v3f(0, 0, 0);
41 float expirationtime, size;
42 expirationtime = size = 1;
44 bool collisiondetection, vertical;
45 collisiondetection = vertical = false;
47 std::string texture = "";
48 std::string playername = "";
50 if (lua_gettop(L) > 1) // deprecated
52 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
53 pos = check_v3f(L, 1);
54 vel = check_v3f(L, 2);
55 acc = check_v3f(L, 3);
56 expirationtime = luaL_checknumber(L, 4);
57 size = luaL_checknumber(L, 5);
58 collisiondetection = lua_toboolean(L, 6);
59 texture = luaL_checkstring(L, 7);
60 if (lua_gettop(L) == 8) // only spawn for a single player
61 playername = luaL_checkstring(L, 8);
63 else if (lua_istable(L, 1))
65 lua_getfield(L, 1, "pos");
66 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
69 lua_getfield(L, 1, "vel");
70 if (lua_istable(L, -1)) {
71 vel = check_v3f(L, -1);
72 log_deprecated(L, "The use of vel is deprecated. "
73 "Use velocity instead");
77 lua_getfield(L, 1, "velocity");
78 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
81 lua_getfield(L, 1, "acc");
82 if (lua_istable(L, -1)) {
83 acc = check_v3f(L, -1);
84 log_deprecated(L, "The use of acc is deprecated. "
85 "Use acceleration instead");
89 lua_getfield(L, 1, "acceleration");
90 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
93 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
94 size = getfloatfield_default(L, 1, "size", 1);
95 collisiondetection = getboolfield_default(L, 1,
96 "collisiondetection", collisiondetection);
97 vertical = getboolfield_default(L, 1, "vertical", vertical);
98 texture = getstringfield_default(L, 1, "texture", "");
99 playername = getstringfield_default(L, 1, "playername", "");
101 getServer(L)->spawnParticle(playername, pos, vel, acc,
102 expirationtime, size, collisiondetection, vertical, texture);
106 // add_particlespawner({amount=, time=,
110 // minexptime=, maxexptime=,
111 // minsize=, maxsize=,
112 // collisiondetection=,
116 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
117 // minexptime/maxexptime = num (seconds)
118 // minsize/maxsize = num
119 // collisiondetection = bool
121 // texture = e.g."default_wood.png"
122 int ModApiParticles::l_add_particlespawner(lua_State *L)
128 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
129 minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
130 float time, minexptime, maxexptime, minsize, maxsize;
131 time= minexptime= maxexptime= minsize= maxsize= 1;
132 bool collisiondetection, vertical;
133 collisiondetection= vertical= false;
134 std::string texture = "";
135 std::string playername = "";
137 if (lua_gettop(L) > 1) //deprecated
139 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
140 amount = luaL_checknumber(L, 1);
141 time = luaL_checknumber(L, 2);
142 minpos = check_v3f(L, 3);
143 maxpos = check_v3f(L, 4);
144 minvel = check_v3f(L, 5);
145 maxvel = check_v3f(L, 6);
146 minacc = check_v3f(L, 7);
147 maxacc = check_v3f(L, 8);
148 minexptime = luaL_checknumber(L, 9);
149 maxexptime = luaL_checknumber(L, 10);
150 minsize = luaL_checknumber(L, 11);
151 maxsize = luaL_checknumber(L, 12);
152 collisiondetection = lua_toboolean(L, 13);
153 texture = luaL_checkstring(L, 14);
154 if (lua_gettop(L) == 15) // only spawn for a single player
155 playername = luaL_checkstring(L, 15);
157 else if (lua_istable(L, 1))
159 amount = getintfield_default(L, 1, "amount", amount);
160 time = getfloatfield_default(L, 1, "time", time);
162 lua_getfield(L, 1, "minpos");
163 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
166 lua_getfield(L, 1, "maxpos");
167 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
170 lua_getfield(L, 1, "minvel");
171 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
174 lua_getfield(L, 1, "maxvel");
175 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
178 lua_getfield(L, 1, "minacc");
179 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
182 lua_getfield(L, 1, "maxacc");
183 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
186 minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
187 maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
188 minsize = getfloatfield_default(L, 1, "minsize", minsize);
189 maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
190 collisiondetection = getboolfield_default(L, 1,
191 "collisiondetection", collisiondetection);
192 vertical = getboolfield_default(L, 1, "vertical", vertical);
193 texture = getstringfield_default(L, 1, "texture", "");
194 playername = getstringfield_default(L, 1, "playername", "");
197 u32 id = getServer(L)->addParticleSpawner(amount, time,
201 minexptime, maxexptime,
205 texture, playername);
206 lua_pushnumber(L, id);
211 // delete_particlespawner(id, player)
212 // player (string) is optional
213 int ModApiParticles::l_delete_particlespawner(lua_State *L)
218 u32 id = luaL_checknumber(L, 1);
219 std::string playername = "";
220 if (lua_gettop(L) == 2) {
221 playername = luaL_checkstring(L, 2);
224 getServer(L)->deleteParticleSpawner(playername, id);
228 void ModApiParticles::Initialize(lua_State *L, int top)
230 API_FCT(add_particle);
231 API_FCT(add_particlespawner);
232 API_FCT(delete_particlespawner);