Skip to content

Instantly share code, notes, and snippets.

@ShadowNinja
Created October 4, 2013 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShadowNinja/6833004 to your computer and use it in GitHub Desktop.
Save ShadowNinja/6833004 to your computer and use it in GitHub Desktop.
Particle tables
diff --git a/builtin/deprecated.lua b/builtin/deprecated.lua
index 333f64c..4251549 100644
--- a/builtin/deprecated.lua
+++ b/builtin/deprecated.lua
@@ -46,3 +46,64 @@ setmetatable(minetest.env, {
return rawget(table, key)
end
})
+
+--
+-- Particles
+--
+local add_particle_raw = minetest.add_particle
+function minetest.add_particle(pos, velocity, acceleration, expiration_time,
+ size, collision_detection, texture, player_name)
+ if not velocity then
+ add_particle_raw(pos)
+ else
+ minetest.log("info", "WARNING:"
+ .." minetest.add_particle(lots of arguments)"
+ .." is depreciated, pass a ParticleDef.")
+ add_particle_raw({
+ pos = pos,
+ velocity = velocity,
+ acceleration = acceleration,
+ expiration_time = expiration_time,
+ size = size,
+ collision_detection = collision_detection,
+ texture = texture,
+ player_name = player_name
+ })
+ end
+end
+
+local add_particlespawner_raw = minetest.add_particlespawner
+function minetest.add_particlespawner(amount, time,
+ minpos, maxpos,
+ minvel, maxvel,
+ minacc, maxacc,
+ minexptime, maxexptime,
+ minsize, maxsize,
+ collision_detection,
+ texture, player_name)
+ if not time then
+ add_particle_raw(amount)
+ else
+ minetest.log("info", "WARNING:"
+ .." minetest.add_particlespawner(lots of arguments)"
+ .." is depreciated, pass a ParticleSpawnerDef.")
+ add_particlespawner_raw({
+ amount = amount,
+ minpos = minpos,
+ maxpos = maxpos,
+ minvel = minvel,
+ maxvel = maxvel,
+ minacc = minacc,
+ maxacc = maxacc,
+ minexptime = minexptime,
+ maxexptime = maxexptime,
+ minsize = minsize,
+ maxsize = maxsize,
+ collision_detection = collision_detection,
+ texture = texture,
+ player_name = player_name
+ })
+ end
+
+
+end
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 335f8af..332d475 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -1407,30 +1407,13 @@ minetest.ban_player(name) -> ban a player
minetest.unban_player_or_ip(name) -> unban player or IP address
Particles:
-minetest.add_particle(pos, velocity, acceleration, expirationtime,
- size, collisiondetection, texture, playername)
-^ Spawn particle at pos with velocity and acceleration
-^ Disappears after expirationtime seconds
-^ collisiondetection: if true collides with physical objects
-^ Uses texture (string)
-^ Playername is optional, if specified spawns particle only on the player's client
-
-minetest.add_particlespawner(amount, time,
- minpos, maxpos,
- minvel, maxvel,
- minacc, maxacc,
- minexptime, maxexptime,
- minsize, maxsize,
- collisiondetection, texture, playername)
-^ Add a particlespawner, an object that spawns an amount of particles over time seconds
-^ The particle's properties are random values in between the boundings:
-^ minpos/maxpos, minvel/maxvel (velocity), minacc/maxacc (acceleration),
-^ minsize/maxsize, minexptime/maxexptime (expirationtime)
-^ collisiondetection: if true uses collisiondetection
-^ Uses texture (string)
-^ Playername is optional, if specified spawns particle only on the player's client
-^ If time is 0 has infinite lifespan and spawns the amount on a per-second base
-^ Returns and id
+minetest.add_particle(ParticleDef)
+^ Spawn a particle
+^ See Particle Definition
+
+minetest.add_particlespawner(ParticleSpawnerDef) -> ID
+^ Add a particlespawner, an object that spawns particles over time
+^ See Particle Spawner Definition
minetest.delete_particlespawner(id, player)
^ Delete ParticleSpawner with id (return value from add_particlespawner)
@@ -2292,3 +2275,39 @@ HUD Definition (hud_add, hud_get)
offset = {x=0, y=0},
^ See "HUD Element Types"
}
+
+Particle Definition
+{
+ pos = {x=0, y=0, z=0},
+ velocity = {x=0, y=0, z=0},
+ acceleration = {x=0, y=0, z=0},
+ expiration_time = 1,
+ ^ Time before the particle disapears, in seconds
+ size = 1,
+ collision_detection = true,
+ texture = "default_stone.png",
+ player_name = "celeron55" -- Optional, defaults to all
+}
+
+Particle Spawner Definition
+{
+ amount = 10,
+ minpos = {x=0, y=0, z=0},
+ maxpos = {x=0, y=0, z=0},
+ ^ Position range
+ minvel = {x=0, y=0, z=0},
+ maxvel = {x=0, y=0, z=0},
+ ^ Velocity range
+ minacc = {x=0, y=0, z=0},
+ maxacc = {x=0, y=0, z=0},
+ ^ Acceleration range
+ minexptime = 1,
+ maxexptime = 2,
+ ^ Range of time before the particle disapears, in seconds
+ minsize = 1,
+ maxsize = 2,
+ collision_detection = true,
+ texture = "default_wood.png",
+ player_name = "celeron55" -- Optional, defaults to all
+}
+
diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp
index 6b00914..58f705c 100644
--- a/src/script/lua_api/l_particles.cpp
+++ b/src/script/lua_api/l_particles.cpp
@@ -22,26 +22,38 @@
#include "common/c_converter.h"
#include "server.h"
-// add_particle(pos, velocity, acceleration, expirationtime,
-// size, collisiondetection, texture, player)
-// pos/velocity/acceleration = {x=num, y=num, z=num}
-// expirationtime = num (seconds)
-// size = num
-// texture = e.g."default_wood.png"
+// add_particle(ParticleDef)
int ModApiParticles::l_add_particle(lua_State *L)
{
+ if (!lua_istable(L, 1))
+ return 0;
+
// Get parameters
- v3f pos = check_v3f(L, 1);
- v3f vel = check_v3f(L, 2);
- v3f acc = check_v3f(L, 3);
- float expirationtime = luaL_checknumber(L, 4);
- float size = luaL_checknumber(L, 5);
- bool collisiondetection = lua_toboolean(L, 6);
- std::string texture = luaL_checkstring(L, 7);
-
- if (lua_gettop(L) == 8) // only spawn for a single player
+ lua_getfield(L, 1, "pos");
+ v3f pos = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "velocity");
+ v3f vel = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "acceleration");
+ v3f acc = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "expiration_time");
+ float expirationtime = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "size");
+ float size = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "collision_detection");
+ bool collisiondetection = lua_toboolean(L, -1);
+
+ lua_getfield(L, 1, "texture");
+ std::string texture = luaL_checkstring(L, -1);
+
+ lua_getfield(L, 1, "player_name");
+ if (lua_isstring(L, -1)) // only spawn for a single player
{
- const char *playername = luaL_checkstring(L, 8);
+ const char *playername = luaL_checkstring(L, -1);
getServer(L)->spawnParticle(playername,
pos, vel, acc, expirationtime,
size, collisiondetection, texture);
@@ -54,41 +66,60 @@ int ModApiParticles::l_add_particle(lua_State *L)
return 1;
}
-// add_particlespawner(amount, time,
-// minpos, maxpos,
-// minvel, maxvel,
-// minacc, maxacc,
-// minexptime, maxexptime,
-// minsize, maxsize,
-// collisiondetection,
-// texture,
-// player)
-// minpos/maxpos/minvel/maxvel/minacc/maxacc = {x=num, y=num, z=num}
-// minexptime/maxexptime = num (seconds)
-// minsize/maxsize = num
-// collisiondetection = bool
-// texture = e.g."default_wood.png"
+// add_particlespawner(ParticleSpawnerDef)
int ModApiParticles::l_add_particlespawner(lua_State *L)
{
+ if (!lua_istable(L, 1))
+ return 0;
+
// Get parameters
- u16 amount = luaL_checknumber(L, 1);
- float time = luaL_checknumber(L, 2);
- v3f minpos = check_v3f(L, 3);
- v3f maxpos = check_v3f(L, 4);
- v3f minvel = check_v3f(L, 5);
- v3f maxvel = check_v3f(L, 6);
- v3f minacc = check_v3f(L, 7);
- v3f maxacc = check_v3f(L, 8);
- float minexptime = luaL_checknumber(L, 9);
- float maxexptime = luaL_checknumber(L, 10);
- float minsize = luaL_checknumber(L, 11);
- float maxsize = luaL_checknumber(L, 12);
- bool collisiondetection = lua_toboolean(L, 13);
- std::string texture = luaL_checkstring(L, 14);
-
- if (lua_gettop(L) == 15) // only spawn for a single player
+ lua_getfield(L, 1, "amount");
+ u16 amount = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "time");
+ float time = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "minpos");
+ v3f minpos = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "maxpos");
+ v3f maxpos = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "minvel");
+ v3f minvel = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "maxvel");
+ v3f maxvel = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "minacc");
+ v3f minacc = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "maxacc");
+ v3f maxacc = check_v3f(L, -1);
+
+ lua_getfield(L, 1, "minexptime");
+ float minexptime = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "maxexptime");
+ float maxexptime = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "minsize");
+ float minsize = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "maxsize");
+ float maxsize = luaL_checknumber(L, -1);
+
+ lua_getfield(L, 1, "collision_detection");
+ bool collisiondetection = lua_toboolean(L, -1);
+
+ lua_getfield(L, 1, "texture");
+ std::string texture = luaL_checkstring(L, -1);
+
+ lua_getfield(L, 1, "player_name");
+
+ if (lua_isstring(L, -1)) // only spawn for a single player
{
- const char *playername = luaL_checkstring(L, 15);
+ const char *playername = luaL_checkstring(L, -1);
u32 id = getServer(L)->addParticleSpawner(playername,
amount, time,
minpos, maxpos,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment