]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_particles.cpp
Move client-specific files to 'src/client' (#7902)
[dragonfireclient.git] / src / script / lua_api / l_particles.cpp
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_object.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25 #include "server.h"
26 #include "client/particles.h"
27
28 // add_particle({pos=, velocity=, acceleration=, expirationtime=,
29 //     size=, collisiondetection=, collision_removal=, object_collision=,
30 //     vertical=, texture=, player=})
31 // pos/velocity/acceleration = {x=num, y=num, z=num}
32 // expirationtime = num (seconds)
33 // size = num
34 // collisiondetection = bool
35 // collision_removal = bool
36 // object_collision = bool
37 // vertical = bool
38 // texture = e.g."default_wood.png"
39 // animation = TileAnimation definition
40 // glow = num
41 int ModApiParticles::l_add_particle(lua_State *L)
42 {
43         MAP_LOCK_REQUIRED;
44
45         // Get parameters
46         v3f pos, vel, acc;
47         float expirationtime, size;
48         expirationtime = size = 1;
49         bool collisiondetection, vertical, collision_removal, object_collision;
50         collisiondetection = vertical = collision_removal = object_collision = false;
51         struct TileAnimationParams animation;
52         animation.type = TAT_NONE;
53         std::string texture;
54         std::string playername;
55         u8 glow = 0;
56
57         if (lua_gettop(L) > 1) // deprecated
58         {
59                 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
60                 pos = check_v3f(L, 1);
61                 vel = check_v3f(L, 2);
62                 acc = check_v3f(L, 3);
63                 expirationtime = luaL_checknumber(L, 4);
64                 size = luaL_checknumber(L, 5);
65                 collisiondetection = readParam<bool>(L, 6);
66                 texture = luaL_checkstring(L, 7);
67                 if (lua_gettop(L) == 8) // only spawn for a single player
68                         playername = luaL_checkstring(L, 8);
69         }
70         else if (lua_istable(L, 1))
71         {
72                 lua_getfield(L, 1, "pos");
73                 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
74                 lua_pop(L, 1);
75
76                 lua_getfield(L, 1, "vel");
77                 if (lua_istable(L, -1)) {
78                         vel = check_v3f(L, -1);
79                         log_deprecated(L, "The use of vel is deprecated. "
80                                 "Use velocity instead");
81                 }
82                 lua_pop(L, 1);
83
84                 lua_getfield(L, 1, "velocity");
85                 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
86                 lua_pop(L, 1);
87
88                 lua_getfield(L, 1, "acc");
89                 if (lua_istable(L, -1)) {
90                         acc = check_v3f(L, -1);
91                         log_deprecated(L, "The use of acc is deprecated. "
92                                 "Use acceleration instead");
93                 }
94                 lua_pop(L, 1);
95
96                 lua_getfield(L, 1, "acceleration");
97                 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
98                 lua_pop(L, 1);
99
100                 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
101                 size = getfloatfield_default(L, 1, "size", 1);
102                 collisiondetection = getboolfield_default(L, 1,
103                         "collisiondetection", collisiondetection);
104                 collision_removal = getboolfield_default(L, 1,
105                         "collision_removal", collision_removal);
106                 object_collision = getboolfield_default(L, 1,
107                         "object_collision", object_collision);
108                 vertical = getboolfield_default(L, 1, "vertical", vertical);
109
110                 lua_getfield(L, 1, "animation");
111                 animation = read_animation_definition(L, -1);
112                 lua_pop(L, 1);
113
114                 texture = getstringfield_default(L, 1, "texture", "");
115                 playername = getstringfield_default(L, 1, "playername", "");
116
117                 glow = getintfield_default(L, 1, "glow", 0);
118         }
119         getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size,
120                         collisiondetection, collision_removal, object_collision, vertical,
121                         texture, animation, glow);
122         return 1;
123 }
124
125 // add_particlespawner({amount=, time=,
126 //                              minpos=, maxpos=,
127 //                              minvel=, maxvel=,
128 //                              minacc=, maxacc=,
129 //                              minexptime=, maxexptime=,
130 //                              minsize=, maxsize=,
131 //                              collisiondetection=,
132 //                              collision_removal=,
133 //                              object_collision=,
134 //                              vertical=,
135 //                              texture=,
136 //                              player=})
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
142 // object_collision = bool
143 // vertical = bool
144 // texture = e.g."default_wood.png"
145 // animation = TileAnimation definition
146 // glow = num
147 int ModApiParticles::l_add_particlespawner(lua_State *L)
148 {
149         MAP_LOCK_REQUIRED;
150
151         // Get parameters
152         u16 amount = 1;
153         v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
154         float time, minexptime, maxexptime, minsize, maxsize;
155         time = minexptime = maxexptime = minsize = maxsize = 1;
156         bool collisiondetection, vertical, collision_removal, object_collision;
157         collisiondetection = vertical = collision_removal = object_collision = false;
158         struct TileAnimationParams animation;
159         animation.type = TAT_NONE;
160         ServerActiveObject *attached = NULL;
161         std::string texture;
162         std::string playername;
163         u8 glow = 0;
164
165         if (lua_gettop(L) > 1) //deprecated
166         {
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);
184         }
185         else if (lua_istable(L, 1))
186         {
187                 amount = getintfield_default(L, 1, "amount", amount);
188                 time = getfloatfield_default(L, 1, "time", time);
189
190                 lua_getfield(L, 1, "minpos");
191                 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
192                 lua_pop(L, 1);
193
194                 lua_getfield(L, 1, "maxpos");
195                 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
196                 lua_pop(L, 1);
197
198                 lua_getfield(L, 1, "minvel");
199                 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
200                 lua_pop(L, 1);
201
202                 lua_getfield(L, 1, "maxvel");
203                 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
204                 lua_pop(L, 1);
205
206                 lua_getfield(L, 1, "minacc");
207                 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
208                 lua_pop(L, 1);
209
210                 lua_getfield(L, 1, "maxacc");
211                 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
212                 lua_pop(L, 1);
213
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);
222                 object_collision = getboolfield_default(L, 1,
223                         "object_collision", object_collision);
224
225                 lua_getfield(L, 1, "animation");
226                 animation = read_animation_definition(L, -1);
227                 lua_pop(L, 1);
228
229                 lua_getfield(L, 1, "attached");
230                 if (!lua_isnil(L, -1)) {
231                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
232                         lua_pop(L, 1);
233                         attached = ObjectRef::getobject(ref);
234                 }
235
236                 vertical = getboolfield_default(L, 1, "vertical", vertical);
237                 texture = getstringfield_default(L, 1, "texture", "");
238                 playername = getstringfield_default(L, 1, "playername", "");
239                 glow = getintfield_default(L, 1, "glow", 0);
240         }
241
242         u32 id = getServer(L)->addParticleSpawner(amount, time,
243                         minpos, maxpos,
244                         minvel, maxvel,
245                         minacc, maxacc,
246                         minexptime, maxexptime,
247                         minsize, maxsize,
248                         collisiondetection,
249                         collision_removal,
250                         object_collision,
251                         attached,
252                         vertical,
253                         texture, playername,
254                         animation, glow);
255         lua_pushnumber(L, id);
256
257         return 1;
258 }
259
260 // delete_particlespawner(id, player)
261 // player (string) is optional
262 int ModApiParticles::l_delete_particlespawner(lua_State *L)
263 {
264         MAP_LOCK_REQUIRED;
265
266         // Get parameters
267         u32 id = luaL_checknumber(L, 1);
268         std::string playername;
269         if (lua_gettop(L) == 2) {
270                 playername = luaL_checkstring(L, 2);
271         }
272
273         getServer(L)->deleteParticleSpawner(playername, id);
274         return 1;
275 }
276
277 void ModApiParticles::Initialize(lua_State *L, int top)
278 {
279         API_FCT(add_particle);
280         API_FCT(add_particlespawner);
281         API_FCT(delete_particlespawner);
282 }
283