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