void Server::SendPlayerHP(session_t peer_id)
{
PlayerSAO *playersao = getPlayerSAO(peer_id);
- // In some rare case if the player is disconnected
- // while Lua call l_punch, for example, this can be NULL
- if (!playersao)
- return;
+ assert(playersao);
SendHP(peer_id, playersao->getHP());
m_script->player_event(playersao,"health_changed");
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;
// 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
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)
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;
}
void Server::DiePlayer(session_t peer_id, const PlayerHPChangeReason &reason)
{
PlayerSAO *playersao = getPlayerSAO(peer_id);
- // In some rare cases this can be NULL -- if the player is disconnected
- // when a Lua function modifies l_punch, for example
- if (!playersao)
- return;
+ assert(playersao);
infostream << "Server::DiePlayer(): Player "
<< playersao->getPlayer()->getName()