]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_particles.cpp
l_minimap: don't show minimap if configuration doesn't allow it
[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 "particles.h"
27
28 // add_particle({pos=, velocity=, acceleration=, expirationtime=,
29 //              size=, collisiondetection=, collision_removal=, vertical=,
30 //              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 // vertical = bool
37 // texture = e.g."default_wood.png"
38 // animation = TileAnimation definition
39 // glow = num
40 int ModApiParticles::l_add_particle(lua_State *L)
41 {
42         MAP_LOCK_REQUIRED;
43
44         // Get parameters
45         v3f pos, vel, acc;
46         pos = vel = acc = v3f(0, 0, 0);
47
48         float expirationtime, size;
49         expirationtime = size = 1;
50
51         bool collisiondetection, vertical, collision_removal;
52         collisiondetection = vertical = collision_removal = false;
53         struct TileAnimationParams animation;
54         animation.type = TAT_NONE;
55
56         std::string texture = "";
57         std::string playername = "";
58
59         u8 glow = 0;
60
61         if (lua_gettop(L) > 1) // deprecated
62         {
63                 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
64                 pos = check_v3f(L, 1);
65                 vel = check_v3f(L, 2);
66                 acc = check_v3f(L, 3);
67                 expirationtime = luaL_checknumber(L, 4);
68                 size = luaL_checknumber(L, 5);
69                 collisiondetection = lua_toboolean(L, 6);
70                 texture = luaL_checkstring(L, 7);
71                 if (lua_gettop(L) == 8) // only spawn for a single player
72                         playername = luaL_checkstring(L, 8);
73         }
74         else if (lua_istable(L, 1))
75         {
76                 lua_getfield(L, 1, "pos");
77                 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
78                 lua_pop(L, 1);
79
80                 lua_getfield(L, 1, "vel");
81                 if (lua_istable(L, -1)) {
82                         vel = check_v3f(L, -1);
83                         log_deprecated(L, "The use of vel is deprecated. "
84                                 "Use velocity instead");
85                 }
86                 lua_pop(L, 1);
87
88                 lua_getfield(L, 1, "velocity");
89                 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
90                 lua_pop(L, 1);
91
92                 lua_getfield(L, 1, "acc");
93                 if (lua_istable(L, -1)) {
94                         acc = check_v3f(L, -1);
95                         log_deprecated(L, "The use of acc is deprecated. "
96                                 "Use acceleration instead");
97                 }
98                 lua_pop(L, 1);
99
100                 lua_getfield(L, 1, "acceleration");
101                 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
102                 lua_pop(L, 1);
103
104                 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
105                 size = getfloatfield_default(L, 1, "size", 1);
106                 collisiondetection = getboolfield_default(L, 1,
107                         "collisiondetection", collisiondetection);
108                 collision_removal = getboolfield_default(L, 1,
109                         "collision_removal", collision_removal);
110                 vertical = getboolfield_default(L, 1, "vertical", vertical);
111
112                 lua_getfield(L, 1, "animation");
113                 animation = read_animation_definition(L, -1);
114                 lua_pop(L, 1);
115
116                 texture = getstringfield_default(L, 1, "texture", "");
117                 playername = getstringfield_default(L, 1, "playername", "");
118
119                 glow = getintfield_default(L, 1, "glow", 0);
120         }
121         getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size,
122                         collisiondetection, collision_removal, vertical, texture, animation, glow);
123         return 1;
124 }
125
126 // add_particlespawner({amount=, time=,
127 //                              minpos=, maxpos=,
128 //                              minvel=, maxvel=,
129 //                              minacc=, maxacc=,
130 //                              minexptime=, maxexptime=,
131 //                              minsize=, maxsize=,
132 //                              collisiondetection=,
133 //                              collision_removal=,
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 // vertical = bool
143 // texture = e.g."default_wood.png"
144 // animation = TileAnimation definition
145 // glow = num
146 int ModApiParticles::l_add_particlespawner(lua_State *L)
147 {
148         MAP_LOCK_REQUIRED;
149
150         // Get parameters
151         u16 amount = 1;
152         v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
153             minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
154         float time, minexptime, maxexptime, minsize, maxsize;
155               time= minexptime= maxexptime= minsize= maxsize= 1;
156         bool collisiondetection, vertical, collision_removal;
157              collisiondetection = vertical = collision_removal = 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 = lua_toboolean(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
223                 lua_getfield(L, 1, "animation");
224                 animation = read_animation_definition(L, -1);
225                 lua_pop(L, 1);
226
227                 lua_getfield(L, 1, "attached");
228                 if (!lua_isnil(L, -1)) {
229                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
230                         lua_pop(L, 1);
231                         attached = ObjectRef::getobject(ref);
232                 }
233
234                 vertical = getboolfield_default(L, 1, "vertical", vertical);
235                 texture = getstringfield_default(L, 1, "texture", "");
236                 playername = getstringfield_default(L, 1, "playername", "");
237                 glow = getintfield_default(L, 1, "glow", 0);
238         }
239
240         u32 id = getServer(L)->addParticleSpawner(amount, time,
241                         minpos, maxpos,
242                         minvel, maxvel,
243                         minacc, maxacc,
244                         minexptime, maxexptime,
245                         minsize, maxsize,
246                         collisiondetection,
247                         collision_removal,
248                         attached,
249                         vertical,
250                         texture, playername,
251                         animation, glow);
252         lua_pushnumber(L, id);
253
254         return 1;
255 }
256
257 // delete_particlespawner(id, player)
258 // player (string) is optional
259 int ModApiParticles::l_delete_particlespawner(lua_State *L)
260 {
261         MAP_LOCK_REQUIRED;
262
263         // Get parameters
264         u32 id = luaL_checknumber(L, 1);
265         std::string playername = "";
266         if (lua_gettop(L) == 2) {
267                 playername = luaL_checkstring(L, 2);
268         }
269
270         getServer(L)->deleteParticleSpawner(playername, id);
271         return 1;
272 }
273
274 void ModApiParticles::Initialize(lua_State *L, int top)
275 {
276         API_FCT(add_particle);
277         API_FCT(add_particlespawner);
278         API_FCT(delete_particlespawner);
279 }
280