Skip to content

Commit

Permalink
Fix sound and particlespawner id generation (#14059)
Browse files Browse the repository at this point in the history
* Fix server sound ids being reused to early

* Fix particlespawner id generation

It always returned 0.
Also, now the ids always grow, to make a conflict with ids in lua unlikely.
  • Loading branch information
Desour committed Nov 30, 2023
1 parent a7e5456 commit 6106e4e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/server.cpp
Expand Up @@ -2172,15 +2172,16 @@ void Server::SendPlayerSpeed(session_t peer_id, const v3f &added_vel)
inline s32 Server::nextSoundId()
{
s32 free_id = m_playing_sounds_id_last_used;
while (free_id == 0 || m_playing_sounds.find(free_id) != m_playing_sounds.end()) {
do {
if (free_id == INT32_MAX)
free_id = 0; // signed overflow is undefined
else
free_id++;

if (free_id == m_playing_sounds_id_last_used)
return 0;
}
} while (free_id == 0 || m_playing_sounds.find(free_id) != m_playing_sounds.end());

m_playing_sounds_id_last_used = free_id;
return free_id;
}
Expand Down
6 changes: 3 additions & 3 deletions src/serverenvironment.cpp
Expand Up @@ -1638,11 +1638,11 @@ u32 ServerEnvironment::addParticleSpawner(float exptime)
float time = exptime > 0.f ? exptime : PARTICLE_SPAWNER_NO_EXPIRY;

u32 free_id = m_particle_spawners_id_last_used;
while (free_id == 0 || m_particle_spawners.find(free_id) != m_particle_spawners.end()) {
do {
free_id++;
if (free_id == m_particle_spawners_id_last_used)
return 0; // full
free_id++;
}
} while (free_id == 0 || m_particle_spawners.find(free_id) != m_particle_spawners.end());

m_particle_spawners_id_last_used = free_id;
m_particle_spawners[free_id] = time;
Expand Down

0 comments on commit 6106e4e

Please sign in to comment.