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);
38 float expirationtime, size;
39 expirationtime= size= 1;
40 bool collisiondetection, vertical;
41 collisiondetection= vertical= false;
42 std::string texture = "";
43 const char *playername = "";
45 if (lua_gettop(L) > 1) // deprecated
47 log_deprecated(L,"Deprecated add_particle call with individual parameters instead of definition");
48 pos = check_v3f(L, 1);
49 vel = check_v3f(L, 2);
50 acc = check_v3f(L, 3);
51 expirationtime = luaL_checknumber(L, 4);
52 size = luaL_checknumber(L, 5);
53 collisiondetection = lua_toboolean(L, 6);
54 texture = luaL_checkstring(L, 7);
55 if (lua_gettop(L) == 8) // only spawn for a single player
56 playername = luaL_checkstring(L, 8);
58 else if (lua_istable(L, 1))
60 int table = lua_gettop(L);
62 while (lua_next(L, table) != 0)
64 const char *key = lua_tostring(L, -2);
65 if(strcmp(key,"pos")==0){
67 }else if(strcmp(key,"vel")==0){
69 }else if(strcmp(key,"acc")==0){
71 }else if(strcmp(key,"expirationtime")==0){
72 expirationtime=luaL_checknumber(L, -1);
73 }else if(strcmp(key,"size")==0){
74 size=luaL_checknumber(L, -1);
75 }else if(strcmp(key,"collisiondetection")==0){
76 collisiondetection=lua_toboolean(L, -1);
77 }else if(strcmp(key,"vertical")==0){
78 vertical=lua_toboolean(L, -1);
79 }else if(strcmp(key,"texture")==0){
80 texture=luaL_checkstring(L, -1);
81 }else if(strcmp(key,"playername")==0){
82 playername=luaL_checkstring(L, -1);
87 if (strcmp(playername, "")==0) // spawn for all players
89 getServer(L)->spawnParticleAll(pos, vel, acc,
90 expirationtime, size, collisiondetection, vertical, texture);
94 getServer(L)->spawnParticle(playername,
95 pos, vel, acc, expirationtime,
96 size, collisiondetection, vertical, texture);
101 // add_particlespawner({amount=, time=,
105 // minexptime=, maxexptime=,
106 // minsize=, maxsize=,
107 // collisiondetection=,
111 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
112 // minexptime/maxexptime = num (seconds)
113 // minsize/maxsize = num
114 // collisiondetection = bool
116 // texture = e.g."default_wood.png"
117 int ModApiParticles::l_add_particlespawner(lua_State *L)
121 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
122 minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
123 float time, minexptime, maxexptime, minsize, maxsize;
124 time= minexptime= maxexptime= minsize= maxsize= 1;
125 bool collisiondetection, vertical;
126 collisiondetection= vertical= false;
127 std::string texture = "";
128 const char *playername = "";
130 if (lua_gettop(L) > 1) //deprecated
132 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
133 amount = luaL_checknumber(L, 1);
134 time = luaL_checknumber(L, 2);
135 minpos = check_v3f(L, 3);
136 maxpos = check_v3f(L, 4);
137 minvel = check_v3f(L, 5);
138 maxvel = check_v3f(L, 6);
139 minacc = check_v3f(L, 7);
140 maxacc = check_v3f(L, 8);
141 minexptime = luaL_checknumber(L, 9);
142 maxexptime = luaL_checknumber(L, 10);
143 minsize = luaL_checknumber(L, 11);
144 maxsize = luaL_checknumber(L, 12);
145 collisiondetection = lua_toboolean(L, 13);
146 texture = luaL_checkstring(L, 14);
147 if (lua_gettop(L) == 15) // only spawn for a single player
148 playername = luaL_checkstring(L, 15);
150 else if (lua_istable(L, 1))
152 int table = lua_gettop(L);
154 while (lua_next(L, table) != 0)
156 const char *key = lua_tostring(L, -2);
157 if(strcmp(key,"amount")==0){
158 amount=luaL_checknumber(L, -1);
159 }else if(strcmp(key,"time")==0){
160 time=luaL_checknumber(L, -1);
161 }else if(strcmp(key,"minpos")==0){
162 minpos=check_v3f(L, -1);
163 }else if(strcmp(key,"maxpos")==0){
164 maxpos=check_v3f(L, -1);
165 }else if(strcmp(key,"minvel")==0){
166 minvel=check_v3f(L, -1);
167 }else if(strcmp(key,"maxvel")==0){
168 maxvel=check_v3f(L, -1);
169 }else if(strcmp(key,"minacc")==0){
170 minacc=check_v3f(L, -1);
171 }else if(strcmp(key,"maxacc")==0){
172 maxacc=check_v3f(L, -1);
173 }else if(strcmp(key,"minexptime")==0){
174 minexptime=luaL_checknumber(L, -1);
175 }else if(strcmp(key,"maxexptime")==0){
176 maxexptime=luaL_checknumber(L, -1);
177 }else if(strcmp(key,"minsize")==0){
178 minsize=luaL_checknumber(L, -1);
179 }else if(strcmp(key,"maxsize")==0){
180 maxsize=luaL_checknumber(L, -1);
181 }else if(strcmp(key,"collisiondetection")==0){
182 collisiondetection=lua_toboolean(L, -1);
183 }else if(strcmp(key,"vertical")==0){
184 vertical=lua_toboolean(L, -1);
185 }else if(strcmp(key,"texture")==0){
186 texture=luaL_checkstring(L, -1);
187 }else if(strcmp(key,"playername")==0){
188 playername=luaL_checkstring(L, -1);
193 if (strcmp(playername, "")==0) //spawn for all players
195 u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
199 minexptime, maxexptime,
204 lua_pushnumber(L, id);
208 u32 id = getServer(L)->addParticleSpawner(playername,
213 minexptime, maxexptime,
218 lua_pushnumber(L, id);
223 // delete_particlespawner(id, player)
224 // player (string) is optional
225 int ModApiParticles::l_delete_particlespawner(lua_State *L)
228 u32 id = luaL_checknumber(L, 1);
230 if (lua_gettop(L) == 2) // only delete for one player
232 const char *playername = luaL_checkstring(L, 2);
233 getServer(L)->deleteParticleSpawner(playername, id);
235 else // delete for all players
237 getServer(L)->deleteParticleSpawnerAll(id);
242 void ModApiParticles::Initialize(lua_State *L, int top)
244 API_FCT(add_particle);
245 API_FCT(add_particlespawner);
246 API_FCT(delete_particlespawner);