3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 red-001 <red-001@outlook.ie>
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.
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.
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.
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 "particles.h"
28 #include "client/clientevent.h"
30 int ModApiParticlesLocal::l_add_particle(lua_State *L)
32 luaL_checktype(L, 1, LUA_TTABLE);
36 float expirationtime, size;
37 bool collisiondetection, vertical, collision_removal;
39 struct TileAnimationParams animation;
40 animation.type = TAT_NONE;
46 lua_getfield(L, 1, "pos");
47 pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
50 lua_getfield(L, 1, "velocity");
51 vel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
54 lua_getfield(L, 1, "acceleration");
55 acc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
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);
64 lua_getfield(L, 1, "animation");
65 animation = read_animation_definition(L, -1);
68 texture = getstringfield_default(L, 1, "texture", "");
70 glow = getintfield_default(L, 1, "glow", 0);
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);
90 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
92 luaL_checktype(L, 1, LUA_TTABLE);
95 v3f minpos, maxpos, minvel, maxvel, minacc, maxacc;
96 float time, minexptime, maxexptime, minsize, maxsize;
97 bool collisiondetection, vertical, collision_removal;
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;
106 amount = getintfield_default(L, 1, "amount", 1);
107 time = getfloatfield_default(L, 1, "time", 1);
109 lua_getfield(L, 1, "minpos");
110 minpos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
113 lua_getfield(L, 1, "maxpos");
114 maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
117 lua_getfield(L, 1, "minvel");
118 minvel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
121 lua_getfield(L, 1, "maxvel");
122 maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
125 lua_getfield(L, 1, "minacc");
126 minacc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
129 lua_getfield(L, 1, "maxacc");
130 maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : v3f(0, 0, 0);
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);
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);
142 lua_getfield(L, 1, "animation");
143 animation = read_animation_definition(L, -1);
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);
151 // attached = ObjectRef::getobject(ref);
154 texture = getstringfield_default(L, 1, "texture", "");
155 glow = getintfield_default(L, 1, "glow", 0);
157 u32 id = getClient(L)->getParticleManager()->getSpawnerId();
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;
182 getClient(L)->pushToEventQueue(event);
183 lua_pushnumber(L, id);
188 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
191 u32 id = luaL_checknumber(L, 1);
193 ClientEvent *event = new ClientEvent();
194 event->type = CE_DELETE_PARTICLESPAWNER;
195 event->delete_particlespawner.id = id;
197 getClient(L)->pushToEventQueue(event);
201 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
203 API_FCT(add_particle);
204 API_FCT(add_particlespawner);
205 API_FCT(delete_particlespawner);