Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move particle spawners to env #2602

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+55 −39
Diff settings

Always

Just for now

Copy path View file
@@ -1256,6 +1256,50 @@ void ServerEnvironment::step(float dtime)
*/
removeRemovedObjects();
}

/*
Manage particle spawner expiration
*/
if (m_particle_management_interval.step(dtime, 1.0)) {

This comment has been minimized.

Copy link
@Zeno-

Zeno- Apr 30, 2015

Contributor

Anything wrong with:

    if (m_particle_management_interval.step(dtime, 1.0)) {
        for (std::map<u32, float>::iterator it = m_particle_spawners.begin();
                it != m_particle_spawners.end(); it++) {
            if (it > 0.0f) {    // spawners that expire
                it->second -= 1.0;
                if (it->second < 0.0f)
                    m_particle_spawners.erase(it);
            }
        }
    }

This comment has been minimized.

Copy link
@kwolekr

kwolekr Apr 25, 2016

Contributor

it gets invalidated after .erase

This comment has been minimized.

Copy link
@Zeno-

Zeno- Apr 25, 2016

Contributor

Hmm, yes. Isn't that the case with the original code as well in that case (line 1275)?

If not, then:

if (m_particle_management_interval.step(dtime, 1.0)) {
    for (std::map<u32, float>::iterator it = m_particle_spawners.begin();
            it != m_particle_spawners.end(); ) {
        if (it > 0.0f) {    // spawners that expire
            it->second -= 1.0f;
            if (it->second < 0.0f)
                m_particle_spawners.erase(it++);
        } else {
            it++;
        }
    }
}

Revised after IRC discussion

if (m_particle_management_interval.step(dtime, 1.0)) {
    for (std::map<u32, float>::iterator it = m_particle_spawners.begin();
            it != m_particle_spawners.end(); ) {
        if (it >= 0.0f) {    // spawners that expire
            it->second -= 1.0f;
            if (it->second <= 0.0f)
                m_particle_spawners.erase(it++);
            else
                ++it;
        } else {
            ++it;
        }
    }

}

for(std::map<u32, float>::iterator i =
m_particle_spawners.begin();
i != m_particle_spawners.end();) {
//non expiring spawners
if (i->second <= -1.f) {
i++;
continue;
}

i->second -= 1.0;
if (i->second < 0.f)
m_particle_spawners.erase(i++);
else
i++;

This comment has been minimized.

Copy link
@obneq

obneq Apr 25, 2016

Author

so after reading up on this i wonder if this is correct, ++i is suggested in order not to skip any elements. also i think zenos 2nd version above is clearer than mine but has the same postfix increment, so im commenting here

This comment has been minimized.

Copy link
@kwolekr

kwolekr Apr 26, 2016

Contributor

D'oh... see the discussion about this in the channel...

Both your version and Zeno's version have 2 rather serious bugs relating to iterators not getting incremented and problems if floating point arithmetic doesn't result in any floating point error.

EDIT: Zeno` updated his comment with all the necessary fixes

}
}
}

u32 ServerEnvironment::addParticleSpawner(float exptime)
{
// Timers with lifetime 0 do not expire
float time = exptime > 0.f ? exptime : -1.f;

This comment has been minimized.

Copy link
@Zeno-

Zeno- Apr 30, 2015

Contributor

This would be better with the scope limited (e.g. just before line 1293)

This comment has been minimized.

Copy link
@kwolekr

kwolekr Apr 26, 2016

Contributor

Also would be nice to not shadow the function time


u32 id = 0;
for (;;) { // look for unused particlespawner id
id++;
std::map<u32, float>::iterator f;
f = m_particle_spawners.find(id);
if (f == m_particle_spawners.end()) {
m_particle_spawners[id] = time;
break;
}
}
return id;
}

void ServerEnvironment::deleteParticleSpawner(u32 id)
{
m_particle_spawners.erase(id);
}

ServerActiveObject* ServerEnvironment::getActiveObject(u16 id)
Copy path View file
@@ -231,6 +231,9 @@ class ServerEnvironment : public Environment
void saveMeta();
void loadMeta();

u32 addParticleSpawner(float exptime);
void deleteParticleSpawner(u32 id);

/*
External ActiveObject interface
-------------------------------------------
@@ -399,6 +402,10 @@ class ServerEnvironment : public Environment
// Estimate for general maximum lag as determined by server.
// Can raise to high values like 15s with eg. map generation mods.
float m_max_lag_estimate;

// Particles
IntervalLimiter m_particle_management_interval;
std::map<u32, float> m_particle_spawners;
};

#ifndef SERVER
Copy path View file
@@ -2953,19 +2953,7 @@ u32 Server::addParticleSpawner(const char *playername,
if(!player)
return -1;

u32 id = 0;
for(;;) // look for unused particlespawner id
{
id++;
if (std::find(m_particlespawner_ids.begin(),
m_particlespawner_ids.end(), id)
== m_particlespawner_ids.end())
{
m_particlespawner_ids.push_back(id);
break;
}
}

u32 id = m_env->addParticleSpawner(spawntime);
SendAddParticleSpawner(player->peer_id, amount, spawntime,
minpos, maxpos, minvel, maxvel, minacc, maxacc,
minexptime, maxexptime, minsize, maxsize,
@@ -2982,19 +2970,7 @@ u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
float minsize, float maxsize,
bool collisiondetection, bool vertical, std::string texture)
{
u32 id = 0;
for(;;) // look for unused particlespawner id
{
id++;
if (std::find(m_particlespawner_ids.begin(),
m_particlespawner_ids.end(), id)
== m_particlespawner_ids.end())
{
m_particlespawner_ids.push_back(id);
break;
}
}

u32 id = m_env->addParticleSpawner(spawntime);
SendAddParticleSpawner(PEER_ID_INEXISTENT, amount, spawntime,
minpos, maxpos, minvel, maxvel, minacc, maxacc,
minexptime, maxexptime, minsize, maxsize,
@@ -3009,19 +2985,13 @@ void Server::deleteParticleSpawner(const char *playername, u32 id)
if(!player)
return;

m_particlespawner_ids.erase(
std::remove(m_particlespawner_ids.begin(),
m_particlespawner_ids.end(), id),
m_particlespawner_ids.end());
m_env->deleteParticleSpawner(id);
SendDeleteParticleSpawner(player->peer_id, id);
}

void Server::deleteParticleSpawnerAll(u32 id)
{
m_particlespawner_ids.erase(
std::remove(m_particlespawner_ids.begin(),
m_particlespawner_ids.end(), id),
m_particlespawner_ids.end());
m_env->deleteParticleSpawner(id);
SendDeleteParticleSpawner(PEER_ID_INEXISTENT, id);
}

Copy path View file
@@ -644,11 +644,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,
*/
// key = name
std::map<std::string, Inventory*> m_detached_inventories;

/*
Particles
*/
std::vector<u32> m_particlespawner_ids;
};

/*
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.