collision_removal = false,
-- ^ collision_removal: if true then particle is removed when it collides,
-- ^ requires collisiondetection = true to have any effect
+ attached = ObjectRef,
+ -- ^ attached: if defined, makes particle positions relative to this object.
vertical = false,
-- ^ vertical: if true faces player using y axis only
texture = "image.png",
f32 maxsize;
bool collisiondetection;
bool collision_removal;
+ u16 attached_id;
bool vertical;
std::string *texture;
u32 id;
if(m_registered){
m_env->getScriptIface()->luaentity_Remove(m_id);
}
+
+ for (UNORDERED_SET<u32>::iterator it = m_attached_particle_spawners.begin();
+ it != m_attached_particle_spawners.end(); ++it) {
+ m_env->deleteParticleSpawner(*it, false);
+ }
}
void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
{
if(m_inventory != &m_player->inventory)
delete m_inventory;
-
}
std::string PlayerSAO::getDescription()
m_player->peer_id = 0;
m_env->savePlayer(m_player);
m_env->removePlayer(m_player);
+ for (UNORDERED_SET<u32>::iterator it = m_attached_particle_spawners.begin();
+ it != m_attached_particle_spawners.end(); ++it) {
+ m_env->deleteParticleSpawner(*it, false);
+ }
}
}
return id;
}
+u32 ServerEnvironment::addParticleSpawner(float exptime, u16 attached_id)
+{
+ u32 id = addParticleSpawner(exptime);
+ m_particle_spawner_attachments[id] = attached_id;
+ if (ServerActiveObject *obj = getActiveObject(attached_id)) {
+ obj->attachParticleSpawner(id);
+ }
+ return id;
+}
+
+void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object)
+{
+ m_particle_spawners.erase(id);
+ UNORDERED_MAP<u32, u16>::iterator it = m_particle_spawner_attachments.find(id);
+ if (it != m_particle_spawner_attachments.end()) {
+ u16 obj_id = (*it).second;
+ ServerActiveObject *sao = getActiveObject(obj_id);
+ if (sao != NULL && remove_from_object) {
+ sao->detachParticleSpawner(id);
+ }
+ m_particle_spawner_attachments.erase(id);
+ }
+}
+
ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
{
ActiveObjectMap::iterator n = m_active_objects.find(id);
void loadDefaultMeta();
u32 addParticleSpawner(float exptime);
- void deleteParticleSpawner(u32 id) { m_particle_spawners.erase(id); }
+ u32 addParticleSpawner(float exptime, u16 attached_id);
+ void deleteParticleSpawner(u32 id, bool remove_from_object = true);
/*
External ActiveObject interface
// Particles
IntervalLimiter m_particle_management_interval;
UNORDERED_MAP<u32, float> m_particle_spawners;
+ UNORDERED_MAP<u32, u16> m_particle_spawner_attachments;
};
#ifndef SERVER
bool vertical = false;
bool collision_removal = false;
+ u16 attached_id = 0;
try {
*pkt >> vertical;
*pkt >> collision_removal;
+ *pkt >> attached_id;
} catch (...) {}
event.add_particlespawner.maxsize = maxsize;
event.add_particlespawner.collisiondetection = collisiondetection;
event.add_particlespawner.collision_removal = collision_removal;
+ event.add_particlespawner.attached_id = attached_id;
event.add_particlespawner.vertical = vertical;
event.add_particlespawner.texture = new std::string(texture);
event.add_particlespawner.id = id;
u16 amount, float time,
v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc,
float minexptime, float maxexptime, float minsize, float maxsize,
- bool collisiondetection, bool collision_removal, bool vertical,
+ bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical,
video::ITexture *texture, u32 id, ParticleManager *p_manager) :
m_particlemanager(p_manager)
{
m_maxsize = maxsize;
m_collisiondetection = collisiondetection;
m_collision_removal = collision_removal;
+ m_attached_id = attached_id;
m_vertical = vertical;
m_texture = texture;
m_time = 0;
{
m_time += dtime;
+ bool unloaded = false;
+ v3f attached_offset = v3f(0,0,0);
+ if (m_attached_id != 0) {
+ if (ClientActiveObject *attached = env->getActiveObject(m_attached_id))
+ attached_offset = attached->getPosition() / BS;
+ else
+ unloaded = true;
+ }
+
if (m_spawntime != 0) // Spawner exists for a predefined timespan
{
for(std::vector<float>::iterator i = m_spawntimes.begin();
{
m_amount--;
- v3f pos = random_v3f(m_minpos, m_maxpos);
- v3f vel = random_v3f(m_minvel, m_maxvel);
- v3f acc = random_v3f(m_minacc, m_maxacc);
- float exptime = rand()/(float)RAND_MAX
- *(m_maxexptime-m_minexptime)
- +m_minexptime;
- float size = rand()/(float)RAND_MAX
- *(m_maxsize-m_minsize)
- +m_minsize;
-
- Particle* toadd = new Particle(
- m_gamedef,
- m_smgr,
- m_player,
- env,
- pos,
- vel,
- acc,
- exptime,
- size,
- m_collisiondetection,
- m_collision_removal,
- m_vertical,
- m_texture,
- v2f(0.0, 0.0),
- v2f(1.0, 1.0));
- m_particlemanager->addParticle(toadd);
+ // Pretend to, but don't actually spawn a
+ // particle if it is attached to an unloaded
+ // object.
+ if (!unloaded) {
+ v3f pos = random_v3f(m_minpos, m_maxpos)
+ + attached_offset;
+ v3f vel = random_v3f(m_minvel, m_maxvel);
+ v3f acc = random_v3f(m_minacc, m_maxacc);
+ // Make relative to offest
+ pos += attached_offset;
+ float exptime = rand()/(float)RAND_MAX
+ *(m_maxexptime-m_minexptime)
+ +m_minexptime;
+ float size = rand()/(float)RAND_MAX
+ *(m_maxsize-m_minsize)
+ +m_minsize;
+
+ Particle* toadd = new Particle(
+ m_gamedef,
+ m_smgr,
+ m_player,
+ env,
+ pos,
+ vel,
+ acc,
+ exptime,
+ size,
+ m_collisiondetection,
+ m_collision_removal,
+ m_vertical,
+ m_texture,
+ v2f(0.0, 0.0),
+ v2f(1.0, 1.0));
+ m_particlemanager->addParticle(toadd);
+ }
i = m_spawntimes.erase(i);
}
else
}
else // Spawner exists for an infinity timespan, spawn on a per-second base
{
+ // Skip this step if attached to an unloaded object
+ if (unloaded)
+ return;
for (int i = 0; i <= m_amount; i++)
{
if (rand()/(float)RAND_MAX < dtime)
{
- v3f pos = random_v3f(m_minpos, m_maxpos);
+ v3f pos = random_v3f(m_minpos, m_maxpos)
+ + attached_offset;
v3f vel = random_v3f(m_minvel, m_maxvel);
v3f acc = random_v3f(m_minacc, m_maxacc);
float exptime = rand()/(float)RAND_MAX
event->add_particlespawner.maxsize,
event->add_particlespawner.collisiondetection,
event->add_particlespawner.collision_removal,
+ event->add_particlespawner.attached_id,
event->add_particlespawner.vertical,
texture,
event->add_particlespawner.id,
float minsize, float maxsize,
bool collisiondetection,
bool collision_removal,
+ u16 attached_id,
bool vertical,
video::ITexture *texture,
u32 id,
bool m_collisiondetection;
bool m_collision_removal;
bool m_vertical;
-
+ u16 m_attached_id;
};
/**
*/
#include "lua_api/l_particles.h"
+#include "lua_api/l_object.h"
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "server.h"
time= minexptime= maxexptime= minsize= maxsize= 1;
bool collisiondetection, vertical, collision_removal;
collisiondetection = vertical = collision_removal = false;
+ ServerActiveObject *attached = NULL;
std::string texture = "";
std::string playername = "";
"collisiondetection", collisiondetection);
collision_removal = getboolfield_default(L, 1,
"collision_removal", collision_removal);
+
+ lua_getfield(L, 1, "attached");
+ if (!lua_isnil(L, -1)) {
+ ObjectRef *ref = ObjectRef::checkobject(L, -1);
+ lua_pop(L, 1);
+ attached = ObjectRef::getobject(ref);
+ }
+
vertical = getboolfield_default(L, 1, "vertical", vertical);
texture = getstringfield_default(L, 1, "texture", "");
playername = getstringfield_default(L, 1, "playername", "");
minsize, maxsize,
collisiondetection,
collision_removal,
+ attached,
vertical,
texture, playername);
lua_pushnumber(L, id);
void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos,
v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime,
float minsize, float maxsize, bool collisiondetection, bool collision_removal,
- bool vertical, const std::string &texture, u32 id)
+ u16 attached_id, bool vertical, const std::string &texture, u32 id)
{
DSTACK(FUNCTION_NAME);
pkt << id << vertical;
pkt << collision_removal;
+ pkt << attached_id;
if (peer_id != PEER_ID_INEXISTENT) {
Send(&pkt);
v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc,
float minexptime, float maxexptime, float minsize, float maxsize,
bool collisiondetection, bool collision_removal,
- bool vertical, const std::string &texture,
+ ServerActiveObject *attached, bool vertical, const std::string &texture,
const std::string &playername)
{
// m_env will be NULL if the server is initializing
peer_id = player->peer_id;
}
- u32 id = m_env->addParticleSpawner(spawntime);
+ u16 attached_id = attached ? attached->getId() : 0;
+
+ u32 id;
+ if (attached_id == 0)
+ id = m_env->addParticleSpawner(spawntime);
+ else
+ id = m_env->addParticleSpawner(spawntime, attached_id);
+
SendAddParticleSpawner(peer_id, amount, spawntime,
minpos, maxpos, minvel, maxvel, minacc, maxacc,
minexptime, maxexptime, minsize, maxsize,
- collisiondetection, collision_removal, vertical, texture, id);
+ collisiondetection, collision_removal, attached_id, vertical,
+ texture, id);
return id;
}
float minexptime, float maxexptime,
float minsize, float maxsize,
bool collisiondetection, bool collision_removal,
+ ServerActiveObject *attached,
bool vertical, const std::string &texture,
const std::string &playername);
float minexptime, float maxexptime,
float minsize, float maxsize,
bool collisiondetection, bool collision_removal,
+ u16 attached_id,
bool vertical, const std::string &texture, u32 id);
void SendDeleteParticleSpawner(u16 peer_id, u32 id);
{ return 0; }
virtual ItemStack getWieldedItem() const;
virtual bool setWieldedItem(const ItemStack &item);
+ inline void attachParticleSpawner(u32 id)
+ {
+ m_attached_particle_spawners.insert(id);
+ }
+ inline void detachParticleSpawner(u32 id)
+ {
+ m_attached_particle_spawners.erase(id);
+ }
+
/*
Number of players which know about this object. Object won't be
ServerEnvironment *m_env;
v3f m_base_position;
+ UNORDERED_SET<u32> m_attached_particle_spawners;
private:
// Used for creating objects based on type