1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Improve core.sound_play with ephemeral sounds and player exclusion

This commit is contained in:
sfan5 2020-01-25 21:19:29 +01:00
parent ea5e231959
commit ace3c76112
9 changed files with 78 additions and 29 deletions

View file

@ -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 &params)
const ServerSoundParams &params, 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;
}