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