Fix various bugs (Anticheat, Lua helpers) (#8013)
[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         v3f pos, vel, acc;
36         float expirationtime, size;
37         bool collisiondetection, vertical, collision_removal;
38
39         struct TileAnimationParams animation;
40         animation.type = TAT_NONE;
41
42         std::string texture;
43
44         u8 glow;
45
46         lua_getfield(L, 1, "pos");
47         pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
48         lua_pop(L, 1);
49
50         lua_getfield(L, 1, "velocity");
51         vel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
52         lua_pop(L, 1);
53
54         lua_getfield(L, 1, "acceleration");
55         acc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
56         lua_pop(L, 1);
57
58         expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
59         size = getfloatfield_default(L, 1, "size", 1);
60         collisiondetection = getboolfield_default(L, 1, "collisiondetection", false);
61         collision_removal = getboolfield_default(L, 1, "collision_removal", false);
62         vertical = getboolfield_default(L, 1, "vertical", false);
63
64         lua_getfield(L, 1, "animation");
65         animation = read_animation_definition(L, -1);
66         lua_pop(L, 1);
67
68         texture = getstringfield_default(L, 1, "texture", "");
69
70         glow = getintfield_default(L, 1, "glow", 0);
71
72         ClientEvent *event = new ClientEvent();
73         event->type                              = CE_SPAWN_PARTICLE;
74         event->spawn_particle.pos                = new v3f (pos);
75         event->spawn_particle.vel                = new v3f (vel);
76         event->spawn_particle.acc                = new v3f (acc);
77         event->spawn_particle.expirationtime     = expirationtime;
78         event->spawn_particle.size               = size;
79         event->spawn_particle.collisiondetection = collisiondetection;
80         event->spawn_particle.collision_removal  = collision_removal;
81         event->spawn_particle.vertical           = vertical;
82         event->spawn_particle.texture            = new std::string(texture);
83         event->spawn_particle.animation          = animation;
84         event->spawn_particle.glow               = glow;
85         getClient(L)->pushToEventQueue(event);
86
87         return 0;
88 }
89
90 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
91 {
92         luaL_checktype(L, 1, LUA_TTABLE);
93         // Get parameters
94         u16 amount;
95         v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
96         float time, minexptime, maxexptime, minsize, maxsize;
97         bool collisiondetection, vertical, collision_removal;
98
99         struct TileAnimationParams animation;
100         animation.type = TAT_NONE;
101         // TODO: Implement this when there is a way to get an objectref.
102         // ServerActiveObject *attached = NULL;
103         std::string texture;
104         u8 glow;
105
106         amount = getintfield_default(L, 1, "amount", 1);
107         time = getfloatfield_default(L, 1, "time", 1);
108
109         lua_getfield(L, 1, "minpos");
110         minpos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
111         lua_pop(L, 1);
112
113         lua_getfield(L, 1, "maxpos");
114         maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
115         lua_pop(L, 1);
116
117         lua_getfield(L, 1, "minvel");
118         minvel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
119         lua_pop(L, 1);
120
121         lua_getfield(L, 1, "maxvel");
122         maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
123         lua_pop(L, 1);
124
125         lua_getfield(L, 1, "minacc");
126         minacc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
127         lua_pop(L, 1);
128
129         lua_getfield(L, 1, "maxacc");
130         maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
131         lua_pop(L, 1);
132
133         minexptime = getfloatfield_default(L, 1, "minexptime", 1);
134         maxexptime = getfloatfield_default(L, 1, "maxexptime", 1);
135         minsize = getfloatfield_default(L, 1, "minsize", 1);
136         maxsize = getfloatfield_default(L, 1, "maxsize", 1);
137
138         collisiondetection = getboolfield_default(L, 1, "collisiondetection", false);
139         collision_removal = getboolfield_default(L, 1, "collision_removal", false);
140         vertical = getboolfield_default(L, 1, "vertical", false);
141
142         lua_getfield(L, 1, "animation");
143         animation = read_animation_definition(L, -1);
144         lua_pop(L, 1);
145
146         // TODO: Implement this when a way to get an objectref on the client is added
147 //      lua_getfield(L, 1, "attached");
148 //      if (!lua_isnil(L, -1)) {
149 //              ObjectRef *ref = ObjectRef::checkobject(L, -1);
150 //              lua_pop(L, 1);
151 //              attached = ObjectRef::getobject(ref);
152 //      }
153
154         texture = getstringfield_default(L, 1, "texture", "");
155         glow = getintfield_default(L, 1, "glow", 0);
156
157         u32 id = getClient(L)->getParticleManager()->getSpawnerId();
158
159         ClientEvent *event = new ClientEvent();
160         event->type                                   = CE_ADD_PARTICLESPAWNER;
161         event->add_particlespawner.amount             = amount;
162         event->add_particlespawner.spawntime          = time;
163         event->add_particlespawner.minpos             = new v3f (minpos);
164         event->add_particlespawner.maxpos             = new v3f (maxpos);
165         event->add_particlespawner.minvel             = new v3f (minvel);
166         event->add_particlespawner.maxvel             = new v3f (maxvel);
167         event->add_particlespawner.minacc             = new v3f (minacc);
168         event->add_particlespawner.maxacc             = new v3f (maxacc);
169         event->add_particlespawner.minexptime         = minexptime;
170         event->add_particlespawner.maxexptime         = maxexptime;
171         event->add_particlespawner.minsize            = minsize;
172         event->add_particlespawner.maxsize            = maxsize;
173         event->add_particlespawner.collisiondetection = collisiondetection;
174         event->add_particlespawner.collision_removal  = collision_removal;
175         event->add_particlespawner.attached_id        = 0;
176         event->add_particlespawner.vertical           = vertical;
177         event->add_particlespawner.texture            = new std::string(texture);
178         event->add_particlespawner.id                 = id;
179         event->add_particlespawner.animation          = animation;
180         event->add_particlespawner.glow               = glow;
181
182         getClient(L)->pushToEventQueue(event);
183         lua_pushnumber(L, id);
184
185         return 1;
186 }
187
188 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
189 {
190         // Get parameters
191         u32 id = luaL_checknumber(L, 1);
192
193         ClientEvent *event = new ClientEvent();
194         event->type                      = CE_DELETE_PARTICLESPAWNER;
195         event->delete_particlespawner.id = id;
196
197         getClient(L)->pushToEventQueue(event);
198         return 0;
199 }
200
201 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
202 {
203         API_FCT(add_particle);
204         API_FCT(add_particlespawner);
205         API_FCT(delete_particlespawner);
206 }