9595b2fab5a4b2f953c09f5320873e655b7691f3
[oweals/minetest.git] / src / script / lua_api / l_particles_local.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 red-001 <red-001@outlook.ie>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "lua_api/l_particles_local.h"
22 #include "common/c_content.h"
23 #include "common/c_converter.h"
24 #include "lua_api/l_internal.h"
25 #include "lua_api/l_object.h"
26 #include "client/particles.h"
27 #include "client/client.h"
28 #include "client/clientevent.h"
29
30 int ModApiParticlesLocal::l_add_particle(lua_State *L)
31 {
32         luaL_checktype(L, 1, LUA_TTABLE);
33
34         // Get parameters
35         ParticleParameters p;
36
37         lua_getfield(L, 1, "pos");
38         if (lua_istable(L, -1))
39                 p.pos = check_v3f(L, -1);
40         lua_pop(L, 1);
41
42         lua_getfield(L, 1, "velocity");
43         if (lua_istable(L, -1))
44                 p.vel = check_v3f(L, -1);
45         lua_pop(L, 1);
46
47         lua_getfield(L, 1, "acceleration");
48         if (lua_istable(L, -1))
49                 p.acc = check_v3f(L, -1);
50         lua_pop(L, 1);
51
52         p.expirationtime = getfloatfield_default(L, 1, "expirationtime",
53                 p.expirationtime);
54         p.size = getfloatfield_default(L, 1, "size", p.size);
55         p.collisiondetection = getboolfield_default(L, 1,
56                 "collisiondetection", p.collisiondetection);
57         p.collision_removal = getboolfield_default(L, 1,
58                 "collision_removal", p.collision_removal);
59         p.object_collision = getboolfield_default(L, 1,
60                 "object_collision", p.object_collision);
61         p.vertical = getboolfield_default(L, 1, "vertical", p.vertical);
62
63         lua_getfield(L, 1, "animation");
64         p.animation = read_animation_definition(L, -1);
65         lua_pop(L, 1);
66
67         p.texture = getstringfield_default(L, 1, "texture", p.texture);
68         p.glow = getintfield_default(L, 1, "glow", p.glow);
69
70         ClientEvent *event = new ClientEvent();
71         event->type           = CE_SPAWN_PARTICLE;
72         event->spawn_particle = new ParticleParameters(p);
73         getClient(L)->pushToEventQueue(event);
74
75         return 0;
76 }
77
78 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
79 {
80         luaL_checktype(L, 1, LUA_TTABLE);
81
82         // Get parameters
83         ParticleSpawnerParameters p;
84
85         p.amount = getintfield_default(L, 1, "amount", p.amount);
86         p.time = getfloatfield_default(L, 1, "time", p.time);
87
88         lua_getfield(L, 1, "minpos");
89         if (lua_istable(L, -1))
90                 p.minpos = check_v3f(L, -1);
91         lua_pop(L, 1);
92
93         lua_getfield(L, 1, "maxpos");
94         if (lua_istable(L, -1))
95                 p.maxpos = check_v3f(L, -1);
96         lua_pop(L, 1);
97
98         lua_getfield(L, 1, "minvel");
99         if (lua_istable(L, -1))
100                 p.minvel = check_v3f(L, -1);
101         lua_pop(L, 1);
102
103         lua_getfield(L, 1, "maxvel");
104         if (lua_istable(L, -1))
105                 p.maxvel = check_v3f(L, -1);
106         lua_pop(L, 1);
107
108         lua_getfield(L, 1, "minacc");
109         if (lua_istable(L, -1))
110                 p.minacc = check_v3f(L, -1);
111         lua_pop(L, 1);
112
113         lua_getfield(L, 1, "maxacc");
114         if (lua_istable(L, -1))
115                 p.maxacc = check_v3f(L, -1);
116         lua_pop(L, 1);
117
118         p.minexptime = getfloatfield_default(L, 1, "minexptime", p.minexptime);
119         p.maxexptime = getfloatfield_default(L, 1, "maxexptime", p.maxexptime);
120         p.minsize = getfloatfield_default(L, 1, "minsize", p.minsize);
121         p.maxsize = getfloatfield_default(L, 1, "maxsize", p.maxsize);
122         p.collisiondetection = getboolfield_default(L, 1,
123                 "collisiondetection", p.collisiondetection);
124         p.collision_removal = getboolfield_default(L, 1,
125                 "collision_removal", p.collision_removal);
126         p.object_collision = getboolfield_default(L, 1,
127                 "object_collision", p.object_collision);
128
129         lua_getfield(L, 1, "animation");
130         p.animation = read_animation_definition(L, -1);
131         lua_pop(L, 1);
132
133         p.vertical = getboolfield_default(L, 1, "vertical", p.vertical);
134         p.texture = getstringfield_default(L, 1, "texture", p.texture);
135         p.glow = getintfield_default(L, 1, "glow", p.glow);
136
137         u64 id = getClient(L)->getParticleManager()->generateSpawnerId();
138
139         auto event = new ClientEvent();
140         event->type                            = CE_ADD_PARTICLESPAWNER;
141         event->add_particlespawner.p           = new ParticleSpawnerParameters(p);
142         event->add_particlespawner.attached_id = 0;
143         event->add_particlespawner.id          = id;
144
145         getClient(L)->pushToEventQueue(event);
146         lua_pushnumber(L, id);
147
148         return 1;
149 }
150
151 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
152 {
153         // Get parameters
154         u32 id = luaL_checknumber(L, 1);
155
156         ClientEvent *event = new ClientEvent();
157         event->type                      = CE_DELETE_PARTICLESPAWNER;
158         event->delete_particlespawner.id = id;
159
160         getClient(L)->pushToEventQueue(event);
161         return 0;
162 }
163
164 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
165 {
166         API_FCT(add_particle);
167         API_FCT(add_particlespawner);
168         API_FCT(delete_particlespawner);
169 }