]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_particles.cpp
Update Mapgen VoxelManipulator on buffer invalidation
[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         float expirationtime, size;
39               expirationtime= size= 1;
40         bool collisiondetection, vertical;
41              collisiondetection= vertical= false;
42         std::string texture = "";
43         const char *playername = "";
44
45         if (lua_gettop(L) > 1) // deprecated
46         {
47                 log_deprecated(L,"Deprecated add_particle call with individual parameters instead of definition");
48                 pos = check_v3f(L, 1);
49                 vel = check_v3f(L, 2);
50                 acc = check_v3f(L, 3);
51                 expirationtime = luaL_checknumber(L, 4);
52                 size = luaL_checknumber(L, 5);
53                 collisiondetection = lua_toboolean(L, 6);
54                 texture = luaL_checkstring(L, 7);
55                 if (lua_gettop(L) == 8) // only spawn for a single player
56                         playername = luaL_checkstring(L, 8);
57         }
58         else if (lua_istable(L, 1))
59         {
60                 int table = lua_gettop(L);
61                 lua_pushnil(L);
62                 while (lua_next(L, table) != 0)
63                 {
64                         const char *key = lua_tostring(L, -2);
65                                   if(strcmp(key,"pos")==0){
66                                         pos=check_v3f(L, -1);
67                         }else if(strcmp(key,"vel")==0){
68                                         vel=check_v3f(L, -1);
69                         }else if(strcmp(key,"acc")==0){
70                                         acc=check_v3f(L, -1);
71                         }else if(strcmp(key,"expirationtime")==0){
72                                         expirationtime=luaL_checknumber(L, -1);
73                         }else if(strcmp(key,"size")==0){
74                                         size=luaL_checknumber(L, -1);
75                         }else if(strcmp(key,"collisiondetection")==0){
76                                         collisiondetection=lua_toboolean(L, -1);
77                         }else if(strcmp(key,"vertical")==0){
78                                         vertical=lua_toboolean(L, -1);
79                         }else if(strcmp(key,"texture")==0){
80                                         texture=luaL_checkstring(L, -1);
81                         }else if(strcmp(key,"playername")==0){
82                                         playername=luaL_checkstring(L, -1);
83                         }
84                         lua_pop(L, 1);
85                 }
86         }
87         if (strcmp(playername, "")==0) // spawn for all players
88         {
89                 getServer(L)->spawnParticleAll(pos, vel, acc,
90                         expirationtime, size, collisiondetection, vertical, texture);
91         }
92         else
93         {
94                 getServer(L)->spawnParticle(playername,
95                         pos, vel, acc, expirationtime,
96                         size, collisiondetection, vertical, texture);
97         }
98         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 int ModApiParticles::l_add_particlespawner(lua_State *L)
118 {
119         // Get parameters
120         u16 amount = 1;
121         v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
122             minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0);
123         float time, minexptime, maxexptime, minsize, maxsize;
124               time= minexptime= maxexptime= minsize= maxsize= 1;
125         bool collisiondetection, vertical;
126              collisiondetection= vertical= false;
127         std::string texture = "";
128         const char *playername = "";
129
130         if (lua_gettop(L) > 1) //deprecated
131         {
132                 log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
133                 amount = luaL_checknumber(L, 1);
134                 time = luaL_checknumber(L, 2);
135                 minpos = check_v3f(L, 3);
136                 maxpos = check_v3f(L, 4);
137                 minvel = check_v3f(L, 5);
138                 maxvel = check_v3f(L, 6);
139                 minacc = check_v3f(L, 7);
140                 maxacc = check_v3f(L, 8);
141                 minexptime = luaL_checknumber(L, 9);
142                 maxexptime = luaL_checknumber(L, 10);
143                 minsize = luaL_checknumber(L, 11);
144                 maxsize = luaL_checknumber(L, 12);
145                 collisiondetection = lua_toboolean(L, 13);
146                 texture = luaL_checkstring(L, 14);
147                 if (lua_gettop(L) == 15) // only spawn for a single player
148                         playername = luaL_checkstring(L, 15);
149         }
150         else if (lua_istable(L, 1))
151         {
152                 int table = lua_gettop(L);
153                 lua_pushnil(L);
154                 while (lua_next(L, table) != 0)
155                 {
156                         const char *key = lua_tostring(L, -2);
157                               if(strcmp(key,"amount")==0){
158                                         amount=luaL_checknumber(L, -1);
159                         }else if(strcmp(key,"time")==0){
160                                         time=luaL_checknumber(L, -1);
161                         }else if(strcmp(key,"minpos")==0){
162                                         minpos=check_v3f(L, -1);
163                         }else if(strcmp(key,"maxpos")==0){
164                                         maxpos=check_v3f(L, -1);
165                         }else if(strcmp(key,"minvel")==0){
166                                         minvel=check_v3f(L, -1);
167                         }else if(strcmp(key,"maxvel")==0){
168                                         maxvel=check_v3f(L, -1);
169                         }else if(strcmp(key,"minacc")==0){
170                                         minacc=check_v3f(L, -1);
171                         }else if(strcmp(key,"maxacc")==0){
172                                         maxacc=check_v3f(L, -1);
173                         }else if(strcmp(key,"minexptime")==0){
174                                         minexptime=luaL_checknumber(L, -1);
175                         }else if(strcmp(key,"maxexptime")==0){
176                                         maxexptime=luaL_checknumber(L, -1);
177                         }else if(strcmp(key,"minsize")==0){
178                                         minsize=luaL_checknumber(L, -1);
179                         }else if(strcmp(key,"maxsize")==0){
180                                         maxsize=luaL_checknumber(L, -1);
181                         }else if(strcmp(key,"collisiondetection")==0){
182                                         collisiondetection=lua_toboolean(L, -1);
183                         }else if(strcmp(key,"vertical")==0){
184                                         vertical=lua_toboolean(L, -1);
185                         }else if(strcmp(key,"texture")==0){
186                                         texture=luaL_checkstring(L, -1);
187                         }else if(strcmp(key,"playername")==0){
188                                         playername=luaL_checkstring(L, -1);
189                         }
190                         lua_pop(L, 1);
191                 }
192         }
193         if (strcmp(playername, "")==0) //spawn for all players
194         {
195                 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                                                         texture);
204                 lua_pushnumber(L, id);
205         }
206         else
207         {
208                 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                                                         texture);
218                 lua_pushnumber(L, id);
219         }
220         return 1;
221 }
222
223 // delete_particlespawner(id, player)
224 // player (string) is optional
225 int ModApiParticles::l_delete_particlespawner(lua_State *L)
226 {
227         // Get parameters
228         u32 id = luaL_checknumber(L, 1);
229
230         if (lua_gettop(L) == 2) // only delete for one player
231         {
232                 const char *playername = luaL_checkstring(L, 2);
233                 getServer(L)->deleteParticleSpawner(playername, id);
234         }
235         else // delete for all players
236         {
237                 getServer(L)->deleteParticleSpawnerAll(id);
238         }
239         return 1;
240 }
241
242 void ModApiParticles::Initialize(lua_State *L, int top)
243 {
244         API_FCT(add_particle);
245         API_FCT(add_particlespawner);
246         API_FCT(delete_particlespawner);
247 }
248