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 "client/particles.h"
27 #include "client/client.h"
28 #include "client/clientevent.h"
30 int ModApiParticlesLocal::l_add_particle(lua_State *L)
32 luaL_checktype(L, 1, LUA_TTABLE);
37 lua_getfield(L, 1, "pos");
38 if (lua_istable(L, -1))
39 p.pos = check_v3f(L, -1);
42 lua_getfield(L, 1, "velocity");
43 if (lua_istable(L, -1))
44 p.vel = check_v3f(L, -1);
47 lua_getfield(L, 1, "acceleration");
48 if (lua_istable(L, -1))
49 p.acc = check_v3f(L, -1);
52 p.expirationtime = getfloatfield_default(L, 1, "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);
63 lua_getfield(L, 1, "animation");
64 p.animation = read_animation_definition(L, -1);
67 p.texture = getstringfield_default(L, 1, "texture", p.texture);
68 p.glow = getintfield_default(L, 1, "glow", p.glow);
70 ClientEvent *event = new ClientEvent();
71 event->type = CE_SPAWN_PARTICLE;
72 event->spawn_particle = new ParticleParameters(p);
73 getClient(L)->pushToEventQueue(event);
78 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
80 luaL_checktype(L, 1, LUA_TTABLE);
83 ParticleSpawnerParameters p;
85 p.amount = getintfield_default(L, 1, "amount", p.amount);
86 p.time = getfloatfield_default(L, 1, "time", p.time);
88 lua_getfield(L, 1, "minpos");
89 if (lua_istable(L, -1))
90 p.minpos = check_v3f(L, -1);
93 lua_getfield(L, 1, "maxpos");
94 if (lua_istable(L, -1))
95 p.maxpos = check_v3f(L, -1);
98 lua_getfield(L, 1, "minvel");
99 if (lua_istable(L, -1))
100 p.minvel = check_v3f(L, -1);
103 lua_getfield(L, 1, "maxvel");
104 if (lua_istable(L, -1))
105 p.maxvel = check_v3f(L, -1);
108 lua_getfield(L, 1, "minacc");
109 if (lua_istable(L, -1))
110 p.minacc = check_v3f(L, -1);
113 lua_getfield(L, 1, "maxacc");
114 if (lua_istable(L, -1))
115 p.maxacc = check_v3f(L, -1);
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);
129 lua_getfield(L, 1, "animation");
130 p.animation = read_animation_definition(L, -1);
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);
137 u64 id = getClient(L)->getParticleManager()->generateSpawnerId();
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;
145 getClient(L)->pushToEventQueue(event);
146 lua_pushnumber(L, id);
151 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
154 u32 id = luaL_checknumber(L, 1);
156 ClientEvent *event = new ClientEvent();
157 event->type = CE_DELETE_PARTICLESPAWNER;
158 event->delete_particlespawner.id = id;
160 getClient(L)->pushToEventQueue(event);
164 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
166 API_FCT(add_particle);
167 API_FCT(add_particlespawner);
168 API_FCT(delete_particlespawner);