aboutsummaryrefslogtreecommitdiff
path: root/src/server.cpp
diff options
context:
space:
mode:
authorSmallJoker <SmallJoker@users.noreply.github.com>2022-06-20 21:56:12 +0200
committerGitHub <noreply@github.com>2022-06-20 21:56:12 +0200
commita463620edbe57071a7101297d33226507567ca73 (patch)
tree342d433ca55f4e3d1826508c303f2e6229d503f1 /src/server.cpp
parent0b41533763bc9f104a2da2e4191f4654b8d8dab4 (diff)
downloadminetest-a463620edbe57071a7101297d33226507567ca73.tar.gz
minetest-a463620edbe57071a7101297d33226507567ca73.tar.bz2
minetest-a463620edbe57071a7101297d33226507567ca73.zip
Re-order sound-related code (#12382)
Dropped ServerSoundParams -> moved to ServerPlayingSound. This gets rid of the duplicated 'fade' and 'pitch' values on server-side where only one was used anyway. SimpleSoundSpec is the basic sound without positional information, hence 'loop' is included. Recursively added PROTOCOL_VERSION to most functions to reduce the versioning mess in the future. Per-type version numbers are kept for now as a safety rope in a special case.
Diffstat (limited to 'src/server.cpp')
-rw-r--r--src/server.cpp98
1 files changed, 32 insertions, 66 deletions
diff --git a/src/server.cpp b/src/server.cpp
index 97d2b69b4..a47062a4d 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -136,7 +136,7 @@ void *ServerThread::run()
return nullptr;
}
-v3f ServerSoundParams::getPos(ServerEnvironment *env, bool *pos_exists) const
+v3f ServerPlayingSound::getPos(ServerEnvironment *env, bool *pos_exists) const
{
if(pos_exists) *pos_exists = false;
switch(type){
@@ -2066,14 +2066,13 @@ inline s32 Server::nextSoundId()
return ret;
}
-s32 Server::playSound(const SimpleSoundSpec &spec,
- const ServerSoundParams &params, bool ephemeral)
+s32 Server::playSound(ServerPlayingSound &params, bool ephemeral)
{
// Find out initial position of sound
bool pos_exists = false;
v3f pos = params.getPos(m_env, &pos_exists);
// If position is not found while it should be, cancel sound
- if(pos_exists != (params.type != ServerSoundParams::SSP_LOCAL))
+ if(pos_exists != (params.type != ServerPlayingSound::SSP_LOCAL))
return -1;
// Filter destination clients
@@ -2118,101 +2117,68 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
if(dst_clients.empty())
return -1;
- // Create the sound
- s32 id;
- ServerPlayingSound *psound = nullptr;
- if (ephemeral) {
- id = -1; // old clients will still use this, so pick a reserved ID
- } else {
- id = nextSoundId();
- // The sound will exist as a reference in m_playing_sounds
- m_playing_sounds[id] = ServerPlayingSound();
- psound = &m_playing_sounds[id];
- psound->params = params;
- psound->spec = spec;
- }
+ // old clients will still use this, so pick a reserved ID (-1)
+ const s32 id = ephemeral ? -1 : nextSoundId();
- float gain = params.gain * spec.gain;
+ float gain = params.gain * params.spec.gain;
NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0);
- pkt << id << spec.name << gain
+ pkt << id << params.spec.name << gain
<< (u8) params.type << pos << params.object
- << params.loop << params.fade << params.pitch
+ << params.spec.loop << params.spec.fade << params.spec.pitch
<< ephemeral;
bool as_reliable = !ephemeral;
- for (const u16 dst_client : dst_clients) {
- if (psound)
- psound->clients.insert(dst_client);
- m_clients.send(dst_client, 0, &pkt, as_reliable);
+ for (const session_t peer_id : dst_clients) {
+ if (!ephemeral)
+ params.clients.insert(peer_id);
+ m_clients.send(peer_id, 0, &pkt, as_reliable);
}
+
+ if (!ephemeral)
+ m_playing_sounds[id] = std::move(params);
return id;
}
void Server::stopSound(s32 handle)
{
- // Get sound reference
- std::unordered_map<s32, ServerPlayingSound>::iterator i =
- m_playing_sounds.find(handle);
- if (i == m_playing_sounds.end())
+ auto it = m_playing_sounds.find(handle);
+ if (it == m_playing_sounds.end())
return;
- ServerPlayingSound &psound = i->second;
+
+ ServerPlayingSound &psound = it->second;
NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4);
pkt << handle;
- for (std::unordered_set<session_t>::const_iterator si = psound.clients.begin();
- si != psound.clients.end(); ++si) {
+ for (session_t peer_id : psound.clients) {
// Send as reliable
- m_clients.send(*si, 0, &pkt, true);
+ m_clients.send(peer_id, 0, &pkt, true);
}
+
// Remove sound reference
- m_playing_sounds.erase(i);
+ m_playing_sounds.erase(it);
}
void Server::fadeSound(s32 handle, float step, float gain)
{
- // Get sound reference
- std::unordered_map<s32, ServerPlayingSound>::iterator i =
- m_playing_sounds.find(handle);
- if (i == m_playing_sounds.end())
+ auto it = m_playing_sounds.find(handle);
+ if (it == m_playing_sounds.end())
return;
- ServerPlayingSound &psound = i->second;
- psound.params.gain = gain;
+ ServerPlayingSound &psound = it->second;
+ psound.gain = gain; // destination gain
NetworkPacket pkt(TOCLIENT_FADE_SOUND, 4);
pkt << handle << step << gain;
- // Backwards compability
- bool play_sound = gain > 0;
- ServerPlayingSound compat_psound = psound;
- compat_psound.clients.clear();
-
- NetworkPacket compat_pkt(TOCLIENT_STOP_SOUND, 4);
- compat_pkt << handle;
-
- for (std::unordered_set<u16>::iterator it = psound.clients.begin();
- it != psound.clients.end();) {
- if (m_clients.getProtocolVersion(*it) >= 32) {
- // Send as reliable
- m_clients.send(*it, 0, &pkt, true);
- ++it;
- } else {
- compat_psound.clients.insert(*it);
- // Stop old sound
- m_clients.send(*it, 0, &compat_pkt, true);
- psound.clients.erase(it++);
- }
+ for (session_t peer_id : psound.clients) {
+ // Send as reliable
+ m_clients.send(peer_id, 0, &pkt, true);
}
// Remove sound reference
- if (!play_sound || psound.clients.empty())
- m_playing_sounds.erase(i);
-
- if (play_sound && !compat_psound.clients.empty()) {
- // Play new sound volume on older clients
- playSound(compat_psound.spec, compat_psound.params);
- }
+ if (gain <= 0 || psound.clients.empty())
+ m_playing_sounds.erase(it);
}
void Server::sendRemoveNode(v3s16 p, std::unordered_set<u16> *far_players,