]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_particles_local.cpp
Joystick: Remap joystick-specific KeyTypes to generic ones
[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         lua_getfield(L, 1, "node");
71         if (lua_istable(L, -1))
72                 p.node = readnode(L, -1, getGameDef(L)->ndef());
73         lua_pop(L, 1);
74
75         p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
76
77         ClientEvent *event = new ClientEvent();
78         event->type           = CE_SPAWN_PARTICLE;
79         event->spawn_particle = new ParticleParameters(p);
80         getClient(L)->pushToEventQueue(event);
81
82         return 0;
83 }
84
85 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
86 {
87         luaL_checktype(L, 1, LUA_TTABLE);
88
89         // Get parameters
90         ParticleSpawnerParameters p;
91
92         p.amount = getintfield_default(L, 1, "amount", p.amount);
93         p.time = getfloatfield_default(L, 1, "time", p.time);
94
95         lua_getfield(L, 1, "minpos");
96         if (lua_istable(L, -1))
97                 p.minpos = check_v3f(L, -1);
98         lua_pop(L, 1);
99
100         lua_getfield(L, 1, "maxpos");
101         if (lua_istable(L, -1))
102                 p.maxpos = check_v3f(L, -1);
103         lua_pop(L, 1);
104
105         lua_getfield(L, 1, "minvel");
106         if (lua_istable(L, -1))
107                 p.minvel = check_v3f(L, -1);
108         lua_pop(L, 1);
109
110         lua_getfield(L, 1, "maxvel");
111         if (lua_istable(L, -1))
112                 p.maxvel = check_v3f(L, -1);
113         lua_pop(L, 1);
114
115         lua_getfield(L, 1, "minacc");
116         if (lua_istable(L, -1))
117                 p.minacc = check_v3f(L, -1);
118         lua_pop(L, 1);
119
120         lua_getfield(L, 1, "maxacc");
121         if (lua_istable(L, -1))
122                 p.maxacc = check_v3f(L, -1);
123         lua_pop(L, 1);
124
125         p.minexptime = getfloatfield_default(L, 1, "minexptime", p.minexptime);
126         p.maxexptime = getfloatfield_default(L, 1, "maxexptime", p.maxexptime);
127         p.minsize = getfloatfield_default(L, 1, "minsize", p.minsize);
128         p.maxsize = getfloatfield_default(L, 1, "maxsize", p.maxsize);
129         p.collisiondetection = getboolfield_default(L, 1,
130                 "collisiondetection", p.collisiondetection);
131         p.collision_removal = getboolfield_default(L, 1,
132                 "collision_removal", p.collision_removal);
133         p.object_collision = getboolfield_default(L, 1,
134                 "object_collision", p.object_collision);
135
136         lua_getfield(L, 1, "animation");
137         p.animation = read_animation_definition(L, -1);
138         lua_pop(L, 1);
139
140         p.vertical = getboolfield_default(L, 1, "vertical", p.vertical);
141         p.texture = getstringfield_default(L, 1, "texture", p.texture);
142         p.glow = getintfield_default(L, 1, "glow", p.glow);
143
144         lua_getfield(L, 1, "node");
145         if (lua_istable(L, -1))
146                 p.node = readnode(L, -1, getGameDef(L)->ndef());
147         lua_pop(L, 1);
148
149         p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
150
151         u64 id = getClient(L)->getParticleManager()->generateSpawnerId();
152
153         auto event = new ClientEvent();
154         event->type                            = CE_ADD_PARTICLESPAWNER;
155         event->add_particlespawner.p           = new ParticleSpawnerParameters(p);
156         event->add_particlespawner.attached_id = 0;
157         event->add_particlespawner.id          = id;
158
159         getClient(L)->pushToEventQueue(event);
160         lua_pushnumber(L, id);
161
162         return 1;
163 }
164
165 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
166 {
167         // Get parameters
168         u32 id = luaL_checknumber(L, 1);
169
170         ClientEvent *event = new ClientEvent();
171         event->type                      = CE_DELETE_PARTICLESPAWNER;
172         event->delete_particlespawner.id = id;
173
174         getClient(L)->pushToEventQueue(event);
175         return 0;
176 }
177
178 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
179 {
180         API_FCT(add_particle);
181         API_FCT(add_particlespawner);
182         API_FCT(delete_particlespawner);
183 }