]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_particles.cpp
SAPI: Mark all Lua API functions requiring envlock
[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_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 int ModApiParticles::l_add_particle(lua_State *L)
34 {
35         MAP_LOCK_REQUIRED;
36
37         // Get parameters
38         v3f pos, vel, acc;
39         pos = vel = acc = v3f(0, 0, 0);
40
41         float expirationtime, size;
42         expirationtime = size = 1;
43
44         bool collisiondetection, vertical;
45         collisiondetection = vertical = false;
46
47         std::string texture = "";
48         std::string playername = "";
49
50         if (lua_gettop(L) > 1) // deprecated
51         {
52                 log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition");
53                 pos = check_v3f(L, 1);
54                 vel = check_v3f(L, 2);
55                 acc = check_v3f(L, 3);
56                 expirationtime = luaL_checknumber(L, 4);
57                 size = luaL_checknumber(L, 5);
58                 collisiondetection = lua_toboolean(L, 6);
59                 texture = luaL_checkstring(L, 7);
60                 if (lua_gettop(L) == 8) // only spawn for a single player
61                         playername = luaL_checkstring(L, 8);
62         }
63         else if (lua_istable(L, 1))
64         {
65                 lua_getfield(L, 1, "pos");
66                 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
67                 lua_pop(L, 1);
68
69                 lua_getfield(L, 1, "vel");
70                 if (lua_istable(L, -1)) {
71                         vel = check_v3f(L, -1);
72                         log_deprecated(L, "The use of vel is deprecated. "
73                                 "Use velocity instead");
74                 }
75                 lua_pop(L, 1);
76
77                 lua_getfield(L, 1, "velocity");
78                 vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
79                 lua_pop(L, 1);
80
81                 lua_getfield(L, 1, "acc");
82                 if (lua_istable(L, -1)) {
83                         acc = check_v3f(L, -1);
84                         log_deprecated(L, "The use of acc is deprecated. "
85                                 "Use acceleration instead");
86                 }
87                 lua_pop(L, 1);
88
89                 lua_getfield(L, 1, "acceleration");
90                 acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
91                 lua_pop(L, 1);
92
93                 expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
94                 size = getfloatfield_default(L, 1, "size", 1);
95                 collisiondetection = getboolfield_default(L, 1,
96                         "collisiondetection", collisiondetection);
97                 vertical = getboolfield_default(L, 1, "vertical", vertical);
98                 texture = getstringfield_default(L, 1, "texture", "");
99                 playername = getstringfield_default(L, 1, "playername", "");
100         }
101         getServer(L)->spawnParticle(playername, pos, vel, acc,
102                         expirationtime, size, collisiondetection, vertical, texture);
103         return 1;
104 }
105
106 // add_particlespawner({amount=, time=,
107 //                              minpos=, maxpos=,
108 //                              minvel=, maxvel=,
109 //                              minacc=, maxacc=,
110 //                              minexptime=, maxexptime=,
111 //                              minsize=, maxsize=,
112 //                              collisiondetection=,
113 //                              vertical=,
114 //                              texture=,
115 //                              player=})
116 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
117 // minexptime/maxexptime = num (seconds)
118 // minsize/maxsize = num
119 // collisiondetection = bool
120 // vertical = bool
121 // texture = e.g."default_wood.png"
122 int ModApiParticles::l_add_particlespawner(lua_State *L)
123 {
124         MAP_LOCK_REQUIRED;
125
126         // Get parameters
127         u16 amount = 1;
128         v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
129             minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
130         float time, minexptime, maxexptime, minsize, maxsize;
131               time= minexptime= maxexptime= minsize= maxsize= 1;
132         bool collisiondetection, vertical;
133              collisiondetection= vertical= false;
134         std::string texture = "";
135         std::string playername = "";
136
137         if (lua_gettop(L) > 1) //deprecated
138         {
139                 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
140                 amount = luaL_checknumber(L, 1);
141                 time = luaL_checknumber(L, 2);
142                 minpos = check_v3f(L, 3);
143                 maxpos = check_v3f(L, 4);
144                 minvel = check_v3f(L, 5);
145                 maxvel = check_v3f(L, 6);
146                 minacc = check_v3f(L, 7);
147                 maxacc = check_v3f(L, 8);
148                 minexptime = luaL_checknumber(L, 9);
149                 maxexptime = luaL_checknumber(L, 10);
150                 minsize = luaL_checknumber(L, 11);
151                 maxsize = luaL_checknumber(L, 12);
152                 collisiondetection = lua_toboolean(L, 13);
153                 texture = luaL_checkstring(L, 14);
154                 if (lua_gettop(L) == 15) // only spawn for a single player
155                         playername = luaL_checkstring(L, 15);
156         }
157         else if (lua_istable(L, 1))
158         {
159                 amount = getintfield_default(L, 1, "amount", amount);
160                 time = getfloatfield_default(L, 1, "time", time);
161
162                 lua_getfield(L, 1, "minpos");
163                 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
164                 lua_pop(L, 1);
165
166                 lua_getfield(L, 1, "maxpos");
167                 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
168                 lua_pop(L, 1);
169
170                 lua_getfield(L, 1, "minvel");
171                 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
172                 lua_pop(L, 1);
173
174                 lua_getfield(L, 1, "maxvel");
175                 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
176                 lua_pop(L, 1);
177
178                 lua_getfield(L, 1, "minacc");
179                 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
180                 lua_pop(L, 1);
181
182                 lua_getfield(L, 1, "maxacc");
183                 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
184                 lua_pop(L, 1);
185
186                 minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
187                 maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
188                 minsize = getfloatfield_default(L, 1, "minsize", minsize);
189                 maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
190                 collisiondetection = getboolfield_default(L, 1,
191                         "collisiondetection", collisiondetection);
192                 vertical = getboolfield_default(L, 1, "vertical", vertical);
193                 texture = getstringfield_default(L, 1, "texture", "");
194                 playername = getstringfield_default(L, 1, "playername", "");
195         }
196
197         u32 id = getServer(L)->addParticleSpawner(amount, time,
198                         minpos, maxpos,
199                         minvel, maxvel,
200                         minacc, maxacc,
201                         minexptime, maxexptime,
202                         minsize, maxsize,
203                         collisiondetection,
204                         vertical,
205                         texture, playername);
206         lua_pushnumber(L, id);
207
208         return 1;
209 }
210
211 // delete_particlespawner(id, player)
212 // player (string) is optional
213 int ModApiParticles::l_delete_particlespawner(lua_State *L)
214 {
215         MAP_LOCK_REQUIRED;
216
217         // Get parameters
218         u32 id = luaL_checknumber(L, 1);
219         std::string playername = "";
220         if (lua_gettop(L) == 2) {
221                 playername = luaL_checkstring(L, 2);
222         }
223
224         getServer(L)->deleteParticleSpawner(playername, id);
225         return 1;
226 }
227
228 void ModApiParticles::Initialize(lua_State *L, int top)
229 {
230         API_FCT(add_particle);
231         API_FCT(add_particlespawner);
232         API_FCT(delete_particlespawner);
233 }
234