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