Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
9 :
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.
14 :
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.
18 : */
19 :
20 : #include "lua_api/l_particles.h"
21 : #include "lua_api/l_internal.h"
22 : #include "common/c_converter.h"
23 : #include "server.h"
24 :
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)
29 : // size = num
30 : // collisiondetection = bool
31 : // vertical = bool
32 : // texture = e.g."default_wood.png"
33 0 : int ModApiParticles::l_add_particle(lua_State *L)
34 : {
35 : // Get parameters
36 0 : v3f pos, vel, acc;
37 0 : pos= vel= acc= v3f(0, 0, 0);
38 : float expirationtime, size;
39 0 : expirationtime= size= 1;
40 : bool collisiondetection, vertical;
41 0 : collisiondetection= vertical= false;
42 0 : std::string texture = "";
43 0 : const char *playername = "";
44 :
45 0 : if (lua_gettop(L) > 1) // deprecated
46 : {
47 0 : log_deprecated(L,"Deprecated add_particle call with individual parameters instead of definition");
48 0 : pos = check_v3f(L, 1);
49 0 : vel = check_v3f(L, 2);
50 0 : acc = check_v3f(L, 3);
51 0 : expirationtime = luaL_checknumber(L, 4);
52 0 : size = luaL_checknumber(L, 5);
53 0 : collisiondetection = lua_toboolean(L, 6);
54 0 : texture = luaL_checkstring(L, 7);
55 0 : if (lua_gettop(L) == 8) // only spawn for a single player
56 0 : playername = luaL_checkstring(L, 8);
57 : }
58 0 : else if (lua_istable(L, 1))
59 : {
60 0 : int table = lua_gettop(L);
61 0 : lua_pushnil(L);
62 0 : while (lua_next(L, table) != 0)
63 : {
64 0 : const char *key = lua_tostring(L, -2);
65 0 : if(strcmp(key,"pos")==0){
66 0 : pos=check_v3f(L, -1);
67 0 : }else if(strcmp(key,"vel")==0){
68 0 : vel=check_v3f(L, -1);
69 0 : }else if(strcmp(key,"acc")==0){
70 0 : acc=check_v3f(L, -1);
71 0 : }else if(strcmp(key,"expirationtime")==0){
72 0 : expirationtime=luaL_checknumber(L, -1);
73 0 : }else if(strcmp(key,"size")==0){
74 0 : size=luaL_checknumber(L, -1);
75 0 : }else if(strcmp(key,"collisiondetection")==0){
76 0 : collisiondetection=lua_toboolean(L, -1);
77 0 : }else if(strcmp(key,"vertical")==0){
78 0 : vertical=lua_toboolean(L, -1);
79 0 : }else if(strcmp(key,"texture")==0){
80 0 : texture=luaL_checkstring(L, -1);
81 0 : }else if(strcmp(key,"playername")==0){
82 0 : playername=luaL_checkstring(L, -1);
83 : }
84 0 : lua_pop(L, 1);
85 : }
86 : }
87 0 : if (strcmp(playername, "")==0) // spawn for all players
88 : {
89 0 : getServer(L)->spawnParticleAll(pos, vel, acc,
90 0 : expirationtime, size, collisiondetection, vertical, texture);
91 : }
92 : else
93 : {
94 0 : getServer(L)->spawnParticle(playername,
95 : pos, vel, acc, expirationtime,
96 0 : size, collisiondetection, vertical, texture);
97 : }
98 0 : return 1;
99 : }
100 :
101 : // add_particlespawner({amount=, time=,
102 : // minpos=, maxpos=,
103 : // minvel=, maxvel=,
104 : // minacc=, maxacc=,
105 : // minexptime=, maxexptime=,
106 : // minsize=, maxsize=,
107 : // collisiondetection=,
108 : // vertical=,
109 : // texture=,
110 : // player=})
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
115 : // vertical = bool
116 : // texture = e.g."default_wood.png"
117 0 : int ModApiParticles::l_add_particlespawner(lua_State *L)
118 : {
119 : // Get parameters
120 0 : u16 amount = 1;
121 0 : v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
122 0 : minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
123 : float time, minexptime, maxexptime, minsize, maxsize;
124 0 : time= minexptime= maxexptime= minsize= maxsize= 1;
125 : bool collisiondetection, vertical;
126 0 : collisiondetection= vertical= false;
127 0 : std::string texture = "";
128 0 : const char *playername = "";
129 :
130 0 : if (lua_gettop(L) > 1) //deprecated
131 : {
132 0 : log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
133 0 : amount = luaL_checknumber(L, 1);
134 0 : time = luaL_checknumber(L, 2);
135 0 : minpos = check_v3f(L, 3);
136 0 : maxpos = check_v3f(L, 4);
137 0 : minvel = check_v3f(L, 5);
138 0 : maxvel = check_v3f(L, 6);
139 0 : minacc = check_v3f(L, 7);
140 0 : maxacc = check_v3f(L, 8);
141 0 : minexptime = luaL_checknumber(L, 9);
142 0 : maxexptime = luaL_checknumber(L, 10);
143 0 : minsize = luaL_checknumber(L, 11);
144 0 : maxsize = luaL_checknumber(L, 12);
145 0 : collisiondetection = lua_toboolean(L, 13);
146 0 : texture = luaL_checkstring(L, 14);
147 0 : if (lua_gettop(L) == 15) // only spawn for a single player
148 0 : playername = luaL_checkstring(L, 15);
149 : }
150 0 : else if (lua_istable(L, 1))
151 : {
152 0 : int table = lua_gettop(L);
153 0 : lua_pushnil(L);
154 0 : while (lua_next(L, table) != 0)
155 : {
156 0 : const char *key = lua_tostring(L, -2);
157 0 : if(strcmp(key,"amount")==0){
158 0 : amount=luaL_checknumber(L, -1);
159 0 : }else if(strcmp(key,"time")==0){
160 0 : time=luaL_checknumber(L, -1);
161 0 : }else if(strcmp(key,"minpos")==0){
162 0 : minpos=check_v3f(L, -1);
163 0 : }else if(strcmp(key,"maxpos")==0){
164 0 : maxpos=check_v3f(L, -1);
165 0 : }else if(strcmp(key,"minvel")==0){
166 0 : minvel=check_v3f(L, -1);
167 0 : }else if(strcmp(key,"maxvel")==0){
168 0 : maxvel=check_v3f(L, -1);
169 0 : }else if(strcmp(key,"minacc")==0){
170 0 : minacc=check_v3f(L, -1);
171 0 : }else if(strcmp(key,"maxacc")==0){
172 0 : maxacc=check_v3f(L, -1);
173 0 : }else if(strcmp(key,"minexptime")==0){
174 0 : minexptime=luaL_checknumber(L, -1);
175 0 : }else if(strcmp(key,"maxexptime")==0){
176 0 : maxexptime=luaL_checknumber(L, -1);
177 0 : }else if(strcmp(key,"minsize")==0){
178 0 : minsize=luaL_checknumber(L, -1);
179 0 : }else if(strcmp(key,"maxsize")==0){
180 0 : maxsize=luaL_checknumber(L, -1);
181 0 : }else if(strcmp(key,"collisiondetection")==0){
182 0 : collisiondetection=lua_toboolean(L, -1);
183 0 : }else if(strcmp(key,"vertical")==0){
184 0 : vertical=lua_toboolean(L, -1);
185 0 : }else if(strcmp(key,"texture")==0){
186 0 : texture=luaL_checkstring(L, -1);
187 0 : }else if(strcmp(key,"playername")==0){
188 0 : playername=luaL_checkstring(L, -1);
189 : }
190 0 : lua_pop(L, 1);
191 : }
192 : }
193 0 : if (strcmp(playername, "")==0) //spawn for all players
194 : {
195 0 : u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
196 : minpos, maxpos,
197 : minvel, maxvel,
198 : minacc, maxacc,
199 : minexptime, maxexptime,
200 : minsize, maxsize,
201 : collisiondetection,
202 : vertical,
203 0 : texture);
204 0 : lua_pushnumber(L, id);
205 : }
206 : else
207 : {
208 0 : u32 id = getServer(L)->addParticleSpawner(playername,
209 : amount, time,
210 : minpos, maxpos,
211 : minvel, maxvel,
212 : minacc, maxacc,
213 : minexptime, maxexptime,
214 : minsize, maxsize,
215 : collisiondetection,
216 : vertical,
217 0 : texture);
218 0 : lua_pushnumber(L, id);
219 : }
220 0 : return 1;
221 : }
222 :
223 : // delete_particlespawner(id, player)
224 : // player (string) is optional
225 0 : int ModApiParticles::l_delete_particlespawner(lua_State *L)
226 : {
227 : // Get parameters
228 0 : u32 id = luaL_checknumber(L, 1);
229 :
230 0 : if (lua_gettop(L) == 2) // only delete for one player
231 : {
232 0 : const char *playername = luaL_checkstring(L, 2);
233 0 : getServer(L)->deleteParticleSpawner(playername, id);
234 : }
235 : else // delete for all players
236 : {
237 0 : getServer(L)->deleteParticleSpawnerAll(id);
238 : }
239 0 : return 1;
240 : }
241 :
242 0 : void ModApiParticles::Initialize(lua_State *L, int top)
243 : {
244 0 : API_FCT(add_particle);
245 0 : API_FCT(add_particlespawner);
246 0 : API_FCT(delete_particlespawner);
247 3 : }
248 :
|