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 lua_getfield(L, 1, "node");
71 if (lua_istable(L, -1))
72 p.node = readnode(L, -1, getGameDef(L)->ndef());
75 p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
77 ClientEvent *event = new ClientEvent();
78 event->type = CE_SPAWN_PARTICLE;
79 event->spawn_particle = new ParticleParameters(p);
80 getClient(L)->pushToEventQueue(event);
85 int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
87 luaL_checktype(L, 1, LUA_TTABLE);
90 ParticleSpawnerParameters p;
92 p.amount = getintfield_default(L, 1, "amount", p.amount);
93 p.time = getfloatfield_default(L, 1, "time", p.time);
95 lua_getfield(L, 1, "minpos");
96 if (lua_istable(L, -1))
97 p.minpos = check_v3f(L, -1);
100 lua_getfield(L, 1, "maxpos");
101 if (lua_istable(L, -1))
102 p.maxpos = check_v3f(L, -1);
105 lua_getfield(L, 1, "minvel");
106 if (lua_istable(L, -1))
107 p.minvel = check_v3f(L, -1);
110 lua_getfield(L, 1, "maxvel");
111 if (lua_istable(L, -1))
112 p.maxvel = check_v3f(L, -1);
115 lua_getfield(L, 1, "minacc");
116 if (lua_istable(L, -1))
117 p.minacc = check_v3f(L, -1);
120 lua_getfield(L, 1, "maxacc");
121 if (lua_istable(L, -1))
122 p.maxacc = check_v3f(L, -1);
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);
136 lua_getfield(L, 1, "animation");
137 p.animation = read_animation_definition(L, -1);
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);
144 lua_getfield(L, 1, "node");
145 if (lua_istable(L, -1))
146 p.node = readnode(L, -1, getGameDef(L)->ndef());
149 p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
151 u64 id = getClient(L)->getParticleManager()->generateSpawnerId();
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;
159 getClient(L)->pushToEventQueue(event);
160 lua_pushnumber(L, id);
165 int ModApiParticlesLocal::l_delete_particlespawner(lua_State *L)
168 u32 id = luaL_checknumber(L, 1);
170 ClientEvent *event = new ClientEvent();
171 event->type = CE_DELETE_PARTICLESPAWNER;
172 event->delete_particlespawner.id = id;
174 getClient(L)->pushToEventQueue(event);
178 void ModApiParticlesLocal::Initialize(lua_State *L, int top)
180 API_FCT(add_particle);
181 API_FCT(add_particlespawner);
182 API_FCT(delete_particlespawner);