diff options
author | sfan5 <sfan5@live.de> | 2020-01-25 21:19:29 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2020-02-01 20:31:41 +0100 |
commit | ace3c76112a839aaad34f4343cd924412310bbd3 (patch) | |
tree | 6efeeb3537314a3cf8594f1a8f80eb28523ccad9 /src/server.cpp | |
parent | ea5e231959365622607c8bfd953f6d96ec54a394 (diff) | |
download | hax-minetest-server-ace3c76112a839aaad34f4343cd924412310bbd3.tar.gz hax-minetest-server-ace3c76112a839aaad34f4343cd924412310bbd3.zip |
Improve core.sound_play with ephemeral sounds and player exclusion
Diffstat (limited to 'src/server.cpp')
-rw-r--r-- | src/server.cpp | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/src/server.cpp b/src/server.cpp index b74bba258..f1613cffe 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2013,8 +2013,18 @@ void Server::SendPlayerSpeed(session_t peer_id, const v3f &added_vel) Send(&pkt); } +inline s32 Server::nextSoundId() +{ + s32 ret = m_next_sound_id; + if (m_next_sound_id == INT32_MAX) + m_next_sound_id = 0; // signed overflow is undefined + else + m_next_sound_id++; + return ret; +} + s32 Server::playSound(const SimpleSoundSpec &spec, - const ServerSoundParams ¶ms) + const ServerSoundParams ¶ms, bool ephemeral) { // Find out initial position of sound bool pos_exists = false; @@ -2025,7 +2035,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec, // Filter destination clients std::vector<session_t> dst_clients; - if(!params.to_player.empty()) { + if (!params.to_player.empty()) { RemotePlayer *player = m_env->getPlayer(params.to_player.c_str()); if(!player){ infostream<<"Server::playSound: Player \""<<params.to_player @@ -2045,6 +2055,9 @@ s32 Server::playSound(const SimpleSoundSpec &spec, RemotePlayer *player = m_env->getPlayer(client_id); if (!player) continue; + if (!params.exclude_player.empty() && + params.exclude_player == player->getName()) + continue; PlayerSAO *sao = player->getPlayerSAO(); if (!sao) @@ -2063,27 +2076,32 @@ s32 Server::playSound(const SimpleSoundSpec &spec, return -1; // Create the sound - s32 id = m_next_sound_id++; - // The sound will exist as a reference in m_playing_sounds - m_playing_sounds[id] = ServerPlayingSound(); - ServerPlayingSound &psound = m_playing_sounds[id]; - psound.params = params; - psound.spec = spec; + 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; + } float gain = params.gain * spec.gain; NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0); pkt << id << spec.name << gain << (u8) params.type << pos << params.object - << params.loop << params.fade << params.pitch; + << params.loop << params.fade << params.pitch + << ephemeral; - // Backwards compability - bool play_sound = gain > 0; + bool as_reliable = !ephemeral; for (const u16 dst_client : dst_clients) { - if (play_sound || m_clients.getProtocolVersion(dst_client) >= 32) { - psound.clients.insert(dst_client); - m_clients.send(dst_client, 0, &pkt, true); - } + if (psound) + psound->clients.insert(dst_client); + m_clients.send(dst_client, 0, &pkt, as_reliable); } return id; } |