Skip to content

Commit 6278da2

Browse files
obneqZeno-
obneq
authored andcommittedApr 27, 2016
Handle particle spawners in env and delete expired ids
Rebased by Zeno (2016-04-2016)
1 parent 26a9a85 commit 6278da2

File tree

4 files changed

+62
-22
lines changed

4 files changed

+62
-22
lines changed
 

‎src/environment.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
5050

5151
#define LBM_NAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_:"
5252

53+
// A number that is much smaller than the timeout for particle spawners should/could ever be
54+
#define PARTICLE_SPAWNER_NO_EXPIRY -1024.f
55+
5356
Environment::Environment():
5457
m_time_of_day_speed(0),
5558
m_time_of_day(9000),
@@ -1544,6 +1547,49 @@ void ServerEnvironment::step(float dtime)
15441547
*/
15451548
removeRemovedObjects();
15461549
}
1550+
1551+
/*
1552+
Manage particle spawner expiration
1553+
*/
1554+
if (m_particle_management_interval.step(dtime, 1.0)) {
1555+
for (std::map<u32, float>::iterator i = m_particle_spawners.begin();
1556+
i != m_particle_spawners.end(); ) {
1557+
//non expiring spawners
1558+
if (i->second == PARTICLE_SPAWNER_NO_EXPIRY) {
1559+
++i;
1560+
continue;
1561+
}
1562+
1563+
i->second -= 1.0f;
1564+
if (i->second <= 0.f)
1565+
m_particle_spawners.erase(i++);
1566+
else
1567+
++i;
1568+
}
1569+
}
1570+
}
1571+
1572+
u32 ServerEnvironment::addParticleSpawner(float exptime)
1573+
{
1574+
// Timers with lifetime 0 do not expire
1575+
float time = exptime > 0.f ? exptime : PARTICLE_SPAWNER_NO_EXPIRY;
1576+
1577+
u32 id = 0;
1578+
for (;;) { // look for unused particlespawner id
1579+
id++;
1580+
std::map<u32, float>::iterator f;
1581+
f = m_particle_spawners.find(id);
1582+
if (f == m_particle_spawners.end()) {
1583+
m_particle_spawners[id] = time;
1584+
break;
1585+
}
1586+
}
1587+
return id;
1588+
}
1589+
1590+
void ServerEnvironment::deleteParticleSpawner(u32 id)
1591+
{
1592+
m_particle_spawners.erase(id);
15471593
}
15481594

15491595
ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)

‎src/environment.h

+7
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ class ServerEnvironment : public Environment
337337
// env_meta.txt doesn't exist (e.g. new world)
338338
void loadDefaultMeta();
339339

340+
u32 addParticleSpawner(float exptime);
341+
void deleteParticleSpawner(u32 id);
342+
340343
/*
341344
External ActiveObject interface
342345
-------------------------------------------
@@ -516,6 +519,10 @@ class ServerEnvironment : public Environment
516519
// Estimate for general maximum lag as determined by server.
517520
// Can raise to high values like 15s with eg. map generation mods.
518521
float m_max_lag_estimate;
522+
523+
// Particles
524+
IntervalLimiter m_particle_management_interval;
525+
std::map<u32, float> m_particle_spawners;
519526
};
520527

521528
#ifndef SERVER

‎src/server.cpp

+8-17
Original file line numberDiff line numberDiff line change
@@ -3194,19 +3194,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime,
31943194
peer_id = player->peer_id;
31953195
}
31963196

3197-
u32 id = 0;
3198-
for(;;) // look for unused particlespawner id
3199-
{
3200-
id++;
3201-
if (std::find(m_particlespawner_ids.begin(),
3202-
m_particlespawner_ids.end(), id)
3203-
== m_particlespawner_ids.end())
3204-
{
3205-
m_particlespawner_ids.push_back(id);
3206-
break;
3207-
}
3208-
}
3209-
3197+
u32 id = m_env->addParticleSpawner(spawntime);
32103198
SendAddParticleSpawner(peer_id, amount, spawntime,
32113199
minpos, maxpos, minvel, maxvel, minacc, maxacc,
32123200
minexptime, maxexptime, minsize, maxsize,
@@ -3229,13 +3217,16 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id)
32293217
peer_id = player->peer_id;
32303218
}
32313219

3232-
m_particlespawner_ids.erase(
3233-
std::remove(m_particlespawner_ids.begin(),
3234-
m_particlespawner_ids.end(), id),
3235-
m_particlespawner_ids.end());
3220+
m_env->deleteParticleSpawner(id);
32363221
SendDeleteParticleSpawner(peer_id, id);
32373222
}
32383223

3224+
void Server::deleteParticleSpawnerAll(u32 id)
3225+
{
3226+
m_env->deleteParticleSpawner(id);
3227+
SendDeleteParticleSpawner(PEER_ID_INEXISTENT, id);
3228+
}
3229+
32393230
Inventory* Server::createDetachedInventory(const std::string &name)
32403231
{
32413232
if(m_detached_inventories.count(name) > 0){

‎src/server.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
287287
const std::string &playername);
288288

289289
void deleteParticleSpawner(const std::string &playername, u32 id);
290+
void deleteParticleSpawnerAll(u32 id);
290291

291292
// Creates or resets inventory
292293
Inventory* createDetachedInventory(const std::string &name);
@@ -662,11 +663,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,
662663
// key = name
663664
std::map<std::string, Inventory*> m_detached_inventories;
664665

665-
/*
666-
Particles
667-
*/
668-
std::vector<u32> m_particlespawner_ids;
669-
670666
DISABLE_CLASS_COPY(Server);
671667
};
672668

0 commit comments

Comments
 (0)
Please sign in to comment.