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)
37 pos = vel = acc = v3f(0, 0, 0);
39 float expirationtime, size;
40 expirationtime = size = 1;
42 bool collisiondetection, vertical;
43 collisiondetection = vertical = false;
45 std::string texture = "";
46 std::string playername = "";
48 if (lua_gettop(L) > 1) // deprecated
50 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
51 pos = check_v3f(L, 1);
52 vel = check_v3f(L, 2);
53 acc = check_v3f(L, 3);
54 expirationtime = luaL_checknumber(L, 4);
55 size = luaL_checknumber(L, 5);
56 collisiondetection = lua_toboolean(L, 6);
57 texture = luaL_checkstring(L, 7);
58 if (lua_gettop(L) == 8) // only spawn for a single player
59 playername = luaL_checkstring(L, 8);
61 else if (lua_istable(L, 1))
63 lua_getfield(L, 1, "pos");
64 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
67 lua_getfield(L, 1, "vel");
68 if (lua_istable(L, -1)) {
69 vel = check_v3f(L, -1);
70 log_deprecated(L, "The use of vel is deprecated. "
71 "Use velocity instead");
75 lua_getfield(L, 1, "velocity");
76 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
79 lua_getfield(L, 1, "acc");
80 if (lua_istable(L, -1)) {
81 acc = check_v3f(L, -1);
82 log_deprecated(L, "The use of acc is deprecated. "
83 "Use acceleration instead");
87 lua_getfield(L, 1, "acceleration");
88 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
91 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
92 size = getfloatfield_default(L, 1, "size", 1);
93 collisiondetection = getboolfield_default(L, 1,
94 "collisiondetection", collisiondetection);
95 vertical = getboolfield_default(L, 1, "vertical", vertical);
96 texture = getstringfield_default(L, 1, "texture", "");
97 playername = getstringfield_default(L, 1, "playername", "");
99 getServer(L)->spawnParticle(playername, pos, vel, acc,
100 expirationtime, size, collisiondetection, vertical, texture);
104 // add_particlespawner({amount=, time=,
108 // minexptime=, maxexptime=,
109 // minsize=, maxsize=,
110 // collisiondetection=,
114 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
115 // minexptime/maxexptime = num (seconds)
116 // minsize/maxsize = num
117 // collisiondetection = bool
119 // texture = e.g."default_wood.png"
120 int ModApiParticles::l_add_particlespawner(lua_State *L)
124 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
125 minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
126 float time, minexptime, maxexptime, minsize, maxsize;
127 time= minexptime= maxexptime= minsize= maxsize= 1;
128 bool collisiondetection, vertical;
129 collisiondetection= vertical= false;
130 std::string texture = "";
131 std::string playername = "";
133 if (lua_gettop(L) > 1) //deprecated
135 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
136 amount = luaL_checknumber(L, 1);
137 time = luaL_checknumber(L, 2);
138 minpos = check_v3f(L, 3);
139 maxpos = check_v3f(L, 4);
140 minvel = check_v3f(L, 5);
141 maxvel = check_v3f(L, 6);
142 minacc = check_v3f(L, 7);
143 maxacc = check_v3f(L, 8);
144 minexptime = luaL_checknumber(L, 9);
145 maxexptime = luaL_checknumber(L, 10);
146 minsize = luaL_checknumber(L, 11);
147 maxsize = luaL_checknumber(L, 12);
148 collisiondetection = lua_toboolean(L, 13);
149 texture = luaL_checkstring(L, 14);
150 if (lua_gettop(L) == 15) // only spawn for a single player
151 playername = luaL_checkstring(L, 15);
153 else if (lua_istable(L, 1))
155 amount = getintfield_default(L, 1, "amount", amount);
156 time = getfloatfield_default(L, 1, "time", time);
158 lua_getfield(L, 1, "minpos");
159 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
162 lua_getfield(L, 1, "maxpos");
163 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
166 lua_getfield(L, 1, "minvel");
167 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
170 lua_getfield(L, 1, "maxvel");
171 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
174 lua_getfield(L, 1, "minacc");
175 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
178 lua_getfield(L, 1, "maxacc");
179 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
182 minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
183 maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
184 minsize = getfloatfield_default(L, 1, "minsize", minsize);
185 maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
186 collisiondetection = getboolfield_default(L, 1,
187 "collisiondetection", collisiondetection);
188 vertical = getboolfield_default(L, 1, "vertical", vertical);
189 texture = getstringfield_default(L, 1, "texture", "");
190 playername = getstringfield_default(L, 1, "playername", "");
193 u32 id = getServer(L)->addParticleSpawner(amount, time,
197 minexptime, maxexptime,
201 texture, playername);
202 lua_pushnumber(L, id);
207 // delete_particlespawner(id, player)
208 // player (string) is optional
209 int ModApiParticles::l_delete_particlespawner(lua_State *L)
212 u32 id = luaL_checknumber(L, 1);
213 std::string playername = "";
214 if (lua_gettop(L) == 2) {
215 playername = luaL_checkstring(L, 2);
218 getServer(L)->deleteParticleSpawner(playername, id);
222 void ModApiParticles::Initialize(lua_State *L, int top)
224 API_FCT(add_particle);
225 API_FCT(add_particlespawner);
226 API_FCT(delete_particlespawner);