Remove attached sounds when the active object is removed (#14341)

This commit is contained in:
sfence 2024-02-25 23:10:39 +01:00 committed by GitHub
parent 39b1311a1b
commit 63a9853811
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 44 additions and 12 deletions

@ -2123,3 +2123,11 @@ const std::string &Client::getFormspecPrepend() const
{
return m_env.getLocalPlayer()->formspec_prepend;
}
void Client::removeActiveObjectSounds(u16 id)
{
for (auto it : m_sounds_to_objects) {
if (it.second == id)
m_sound->stopSound(it.first);
}
}

@ -471,6 +471,9 @@ private:
bool canSendChatMessage() const;
// remove sounds attached to object
void removeActiveObjectSounds(u16 id);
float m_packetcounter_timer = 0.0f;
float m_connection_reinit_timer = 0.1f;
float m_avg_rtt_timer = 0.0f;

@ -471,6 +471,7 @@ void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt)
for (u16 i = 0; i < removed_count; i++) {
*pkt >> id;
m_env.removeActiveObject(id);
removeActiveObjectSounds(id);
}
// Read added objects

@ -2264,6 +2264,22 @@ void Server::fadeSound(s32 handle, float step, float gain)
m_playing_sounds.erase(it);
}
void Server::stopAttachedSounds(u16 id)
{
assert(id);
for (auto it = m_playing_sounds.begin(); it != m_playing_sounds.end(); ) {
const ServerPlayingSound &sound = it->second;
if (sound.object == id) {
// Remove sound reference
it = m_playing_sounds.erase(it);
}
else
it++;
}
}
void Server::sendRemoveNode(v3s16 p, std::unordered_set<u16> *far_players,
float far_d_nodes)
{

@ -239,6 +239,7 @@ public:
s32 playSound(ServerPlayingSound &params, bool ephemeral=false);
void stopSound(s32 handle);
void fadeSound(s32 handle, float step, float gain);
void stopAttachedSounds(u16 id);
// Envlock
std::set<std::string> getPlayerEffectivePrivs(const std::string &name);

@ -1253,10 +1253,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode)
return false;
}
// Tell the object about removal
obj->removingFromEnvironment();
// Deregister in scripting api
m_script->removeObjectReference(obj);
processActiveObjectRemove(obj, id);
// Delete active object
return true;
@ -1976,10 +1973,7 @@ void ServerEnvironment::removeRemovedObjects()
}
}
// Tell the object about removal
obj->removingFromEnvironment();
// Deregister in scripting api
m_script->removeObjectReference(obj);
processActiveObjectRemove(obj, id);
// Delete
return true;
@ -2216,10 +2210,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete)
return false;
}
// Tell the object about removal
obj->removingFromEnvironment();
// Deregister in scripting api
m_script->removeObjectReference(obj);
processActiveObjectRemove(obj, id);
// Delete active object
return true;
@ -2282,6 +2273,16 @@ bool ServerEnvironment::saveStaticToBlock(
return true;
}
void ServerEnvironment::processActiveObjectRemove(ServerActiveObject *obj, u16 id)
{
// Tell the object about removal
obj->removingFromEnvironment();
// Deregister in scripting api
m_script->removeObjectReference(obj);
// stop attached sounds
m_server->stopAttachedSounds(id);
}
PlayerDatabase *ServerEnvironment::openPlayerDatabase(const std::string &name,
const std::string &savedir, const Settings &conf)
{

@ -454,6 +454,8 @@ private:
bool saveStaticToBlock(v3s16 blockpos, u16 store_id,
ServerActiveObject *obj, const StaticObject &s_obj, u32 mod_reason);
void processActiveObjectRemove(ServerActiveObject *obj, u16 id);
/*
Member variables
*/