Predict param2 of facedir nodes and attachment of attached_node nodes
[oweals/minetest.git] / src / scriptapi_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 "scriptapi.h"
21 #include "scriptapi_particles.h"
22 #include "server.h"
23 #include "script.h"
24 #include "scriptapi_types.h"
25 #include "scriptapi_common.h"
26
27 // add_particle(pos, velocity, acceleration, expirationtime,
28 //              size, collisiondetection, texture, player)
29 // pos/velocity/acceleration = {x=num, y=num, z=num}
30 // expirationtime = num (seconds)
31 // size = num
32 // texture = e.g."default_wood.png"
33 int l_add_particle(lua_State *L)
34 {
35         // Get server from registry
36         Server *server = get_server(L);
37         // Get parameters
38         v3f pos = check_v3f(L, 1);
39         v3f vel = check_v3f(L, 2);
40         v3f acc = check_v3f(L, 3);
41         float expirationtime = luaL_checknumber(L, 4);
42         float size = luaL_checknumber(L, 5);
43         bool collisiondetection = lua_toboolean(L, 6);
44         std::string texture = luaL_checkstring(L, 7);
45
46         if (lua_gettop(L) == 8) // only spawn for a single player
47         {
48                 const char *playername = luaL_checkstring(L, 8);
49                 server->spawnParticle(playername,
50                         pos, vel, acc, expirationtime,
51                         size, collisiondetection, texture);
52         }
53         else // spawn for all players
54         {
55                 server->spawnParticleAll(pos, vel, acc,
56                         expirationtime, size, collisiondetection, texture);
57         }
58         return 1;
59 }
60
61 // add_particlespawner(amount, time,
62 //                              minpos, maxpos,
63 //                              minvel, maxvel,
64 //                              minacc, maxacc,
65 //                              minexptime, maxexptime,
66 //                              minsize, maxsize,
67 //                              collisiondetection,
68 //                              texture,
69 //                              player)
70 // minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
71 // minexptime/maxexptime = num (seconds)
72 // minsize/maxsize = num
73 // collisiondetection = bool
74 // texture = e.g."default_wood.png"
75 int l_add_particlespawner(lua_State *L)
76 {
77         // Get server from registry
78         Server *server = get_server(L);
79         // Get parameters
80         u16 amount = luaL_checknumber(L, 1);
81         float time = luaL_checknumber(L, 2);
82         v3f minpos = check_v3f(L, 3);
83         v3f maxpos = check_v3f(L, 4);
84         v3f minvel = check_v3f(L, 5);
85         v3f maxvel = check_v3f(L, 6);
86         v3f minacc = check_v3f(L, 7);
87         v3f maxacc = check_v3f(L, 8);
88         float minexptime = luaL_checknumber(L, 9);
89         float maxexptime = luaL_checknumber(L, 10);
90         float minsize = luaL_checknumber(L, 11);
91         float maxsize = luaL_checknumber(L, 12);
92         bool collisiondetection = lua_toboolean(L, 13);
93         std::string texture = luaL_checkstring(L, 14);
94
95         if (lua_gettop(L) == 15) // only spawn for a single player
96         {
97                 const char *playername = luaL_checkstring(L, 15);
98                 u32 id = server->addParticleSpawner(playername,
99                                                         amount, time,
100                                                         minpos, maxpos,
101                                                         minvel, maxvel,
102                                                         minacc, maxacc,
103                                                         minexptime, maxexptime,
104                                                         minsize, maxsize,
105                                                         collisiondetection,
106                                                         texture);
107                 lua_pushnumber(L, id);
108         }
109         else // spawn for all players
110         {
111                 u32 id = server->addParticleSpawnerAll( amount, time,
112                                                         minpos, maxpos,
113                                                         minvel, maxvel,
114                                                         minacc, maxacc,
115                                                         minexptime, maxexptime,
116                                                         minsize, maxsize,
117                                                         collisiondetection,
118                                                         texture);
119                 lua_pushnumber(L, id);
120         }
121         return 1;
122 }
123
124 // delete_particlespawner(id, player)
125 // player (string) is optional
126 int l_delete_particlespawner(lua_State *L)
127 {
128         // Get server from registry
129         Server *server = get_server(L);
130         // Get parameters
131         u32 id = luaL_checknumber(L, 1);
132
133         if (lua_gettop(L) == 2) // only delete for one player
134         {
135                 const char *playername = luaL_checkstring(L, 2);
136                 server->deleteParticleSpawner(playername, id);
137         }
138         else // delete for all players
139         {
140                 server->deleteParticleSpawnerAll(id);
141         }
142         return 1;
143 }