Skip to content

Commit

Permalink
Re-order sound-related code (#12382)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmallJoker committed Jun 20, 2022
1 parent 0b41533 commit a463620
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 188 deletions.
2 changes: 1 addition & 1 deletion src/client/content_cao.cpp
Expand Up @@ -1174,7 +1174,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
// Reduce footstep gain, as non-local-player footsteps are
// somehow louder.
spec.gain *= 0.6f;
m_client->sound()->playSoundAt(spec, false, getPosition());
m_client->sound()->playSoundAt(spec, getPosition());
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/client/game.cpp
Expand Up @@ -282,15 +282,15 @@ class SoundMaker
if (m_player_step_timer <= 0 && m_player_step_sound.exists()) {
m_player_step_timer = 0.03;
if (makes_footstep_sound)
m_sound->playSound(m_player_step_sound, false);
m_sound->playSound(m_player_step_sound);
}
}

void playPlayerJump()
{
if (m_player_jump_timer <= 0.0f) {
m_player_jump_timer = 0.2f;
m_sound->playSound(SimpleSoundSpec("player_jump", 0.5f), false);
m_sound->playSound(SimpleSoundSpec("player_jump", 0.5f));
}
}

Expand All @@ -315,32 +315,32 @@ class SoundMaker
static void cameraPunchLeft(MtEvent *e, void *data)
{
SoundMaker *sm = (SoundMaker *)data;
sm->m_sound->playSound(sm->m_player_leftpunch_sound, false);
sm->m_sound->playSound(sm->m_player_leftpunch_sound);
}

static void cameraPunchRight(MtEvent *e, void *data)
{
SoundMaker *sm = (SoundMaker *)data;
sm->m_sound->playSound(sm->m_player_rightpunch_sound, false);
sm->m_sound->playSound(sm->m_player_rightpunch_sound);
}

static void nodeDug(MtEvent *e, void *data)
{
SoundMaker *sm = (SoundMaker *)data;
NodeDugEvent *nde = (NodeDugEvent *)e;
sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug, false);
sm->m_sound->playSound(sm->m_ndef->get(nde->n).sound_dug);
}

static void playerDamage(MtEvent *e, void *data)
{
SoundMaker *sm = (SoundMaker *)data;
sm->m_sound->playSound(SimpleSoundSpec("player_damage", 0.5), false);
sm->m_sound->playSound(SimpleSoundSpec("player_damage", 0.5));
}

static void playerFallingDamage(MtEvent *e, void *data)
{
SoundMaker *sm = (SoundMaker *)data;
sm->m_sound->playSound(SimpleSoundSpec("player_falling_damage", 0.5), false);
sm->m_sound->playSound(SimpleSoundSpec("player_falling_damage", 0.5));
}

void registerReceiver(MtEventManager *mgr)
Expand Down
8 changes: 4 additions & 4 deletions src/client/sound.h
Expand Up @@ -63,13 +63,13 @@ class ISoundManager
virtual void step(float dtime) = 0;
virtual void fadeSound(int sound, float step, float gain) = 0;

int playSound(const SimpleSoundSpec &spec, bool loop)
int playSound(const SimpleSoundSpec &spec)
{
return playSound(spec.name, loop, spec.gain, spec.fade, spec.pitch);
return playSound(spec.name, spec.loop, spec.gain, spec.fade, spec.pitch);
}
int playSoundAt(const SimpleSoundSpec &spec, bool loop, const v3f &pos)
int playSoundAt(const SimpleSoundSpec &spec, const v3f &pos)
{
return playSoundAt(spec.name, loop, spec.gain, pos, spec.pitch);
return playSoundAt(spec.name, spec.loop, spec.gain, pos, spec.pitch);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/gui/guiEngine.cpp
Expand Up @@ -622,9 +622,9 @@ void GUIEngine::updateTopLeftTextSize()
}

/******************************************************************************/
s32 GUIEngine::playSound(const SimpleSoundSpec &spec, bool looped)
s32 GUIEngine::playSound(const SimpleSoundSpec &spec)
{
s32 handle = m_sound_manager->playSound(spec, looped);
s32 handle = m_sound_manager->playSound(spec);
return handle;
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/guiEngine.h
Expand Up @@ -296,7 +296,7 @@ class GUIEngine {
clouddata m_cloud;

/** start playing a sound and return handle */
s32 playSound(const SimpleSoundSpec &spec, bool looped);
s32 playSound(const SimpleSoundSpec &spec);
/** stop playing a sound started with playSound() */
void stopSound(s32 handle);

Expand Down
21 changes: 10 additions & 11 deletions src/itemdef.cpp
Expand Up @@ -154,8 +154,8 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
os << serializeString16(node_placement_prediction);

// Version from ContentFeatures::serialize to keep in sync
sound_place.serialize(os, CONTENTFEATURES_VERSION);
sound_place_failed.serialize(os, CONTENTFEATURES_VERSION);
sound_place.serialize(os, protocol_version);
sound_place_failed.serialize(os, protocol_version);

writeF32(os, range);
os << serializeString16(palette_image);
Expand All @@ -168,7 +168,7 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
os << place_param2;
}

void ItemDefinition::deSerialize(std::istream &is)
void ItemDefinition::deSerialize(std::istream &is, u16 protocol_version)
{
// Reset everything
reset();
Expand Down Expand Up @@ -205,9 +205,8 @@ void ItemDefinition::deSerialize(std::istream &is)

node_placement_prediction = deSerializeString16(is);

// Version from ContentFeatures::serialize to keep in sync
sound_place.deSerialize(is, CONTENTFEATURES_VERSION);
sound_place_failed.deSerialize(is, CONTENTFEATURES_VERSION);
sound_place.deSerialize(is, protocol_version);
sound_place_failed.deSerialize(is, protocol_version);

range = readF32(is);
palette_image = deSerializeString16(is);
Expand Down Expand Up @@ -538,21 +537,21 @@ class CItemDefManager: public IWritableItemDefManager
os << serializeString16(it.second);
}
}
void deSerialize(std::istream &is)
void deSerialize(std::istream &is, u16 protocol_version)
{
// Clear everything
clear();
// Deserialize
int version = readU8(is);
if(version != 0)

if(readU8(is) != 0)
throw SerializationError("unsupported ItemDefManager version");

u16 count = readU16(is);
for(u16 i=0; i<count; i++)
{
// Deserialize a string and grab an ItemDefinition from it
std::istringstream tmp_is(deSerializeString16(is), std::ios::binary);
ItemDefinition def;
def.deSerialize(tmp_is);
def.deSerialize(tmp_is, protocol_version);
// Register
registerItem(def);
}
Expand Down
4 changes: 2 additions & 2 deletions src/itemdef.h
Expand Up @@ -97,7 +97,7 @@ struct ItemDefinition
~ItemDefinition();
void reset();
void serialize(std::ostream &os, u16 protocol_version) const;
void deSerialize(std::istream &is);
void deSerialize(std::istream &is, u16 protocol_version);
private:
void resetInitial();
};
Expand Down Expand Up @@ -177,7 +177,7 @@ class IWritableItemDefManager : public IItemDefManager
const std::string &convert_to)=0;

virtual void serialize(std::ostream &os, u16 protocol_version)=0;
virtual void deSerialize(std::istream &is)=0;
virtual void deSerialize(std::istream &is, u16 protocol_version)=0;

// Do stuff asked by threads that can only be done in the main thread
virtual void processQueue(IGameDef *gamedef)=0;
Expand Down
22 changes: 9 additions & 13 deletions src/network/clientpackethandler.cpp
Expand Up @@ -778,7 +778,7 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt)
decompressZlib(tmp_is, tmp_os);

// Deserialize node definitions
m_nodedef->deSerialize(tmp_os);
m_nodedef->deSerialize(tmp_os, m_proto_ver);
m_nodedef_received = true;
}

Expand All @@ -797,7 +797,7 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt)
decompressZlib(tmp_is, tmp_os);

// Deserialize node definitions
m_itemdef->deSerialize(tmp_os);
m_itemdef->deSerialize(tmp_os, m_proto_ver);
m_itemdef_received = true;
}

Expand All @@ -818,40 +818,36 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
*/

s32 server_id;
std::string name;

float gain;
SimpleSoundSpec spec;
u8 type; // 0=local, 1=positional, 2=object
v3f pos;
u16 object_id;
bool loop;
float fade = 0.0f;
float pitch = 1.0f;
bool ephemeral = false;

*pkt >> server_id >> name >> gain >> type >> pos >> object_id >> loop;
*pkt >> server_id >> spec.name >> spec.gain >> type >> pos >> object_id >> spec.loop;

try {
*pkt >> fade;
*pkt >> pitch;
*pkt >> spec.fade;
*pkt >> spec.pitch;
*pkt >> ephemeral;
} catch (PacketError &e) {};

// Start playing
int client_id = -1;
switch(type) {
case 0: // local
client_id = m_sound->playSound(name, loop, gain, fade, pitch);
client_id = m_sound->playSound(spec);
break;
case 1: // positional
client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
client_id = m_sound->playSoundAt(spec, pos);
break;
case 2:
{ // object
ClientActiveObject *cao = m_env.getActiveObject(object_id);
if (cao)
pos = cao->getPosition();
client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
client_id = m_sound->playSoundAt(spec, pos);
break;
}
default:
Expand Down
52 changes: 23 additions & 29 deletions src/nodedef.cpp
Expand Up @@ -61,9 +61,7 @@ void NodeBox::reset()

void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
{
// Protocol >= 36
const u8 version = 6;
writeU8(os, version);
writeU8(os, 6); // version. Protocol >= 36

switch (type) {
case NODEBOX_LEVELED:
Expand Down Expand Up @@ -123,8 +121,7 @@ void NodeBox::serialize(std::ostream &os, u16 protocol_version) const

void NodeBox::deSerialize(std::istream &is)
{
int version = readU8(is);
if (version < 6)
if (readU8(is) < 6)
throw SerializationError("unsupported NodeBox version");

reset();
Expand Down Expand Up @@ -249,14 +246,13 @@ void TileDef::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, align_style);
}

void TileDef::deSerialize(std::istream &is, u8 contentfeatures_version,
NodeDrawType drawtype)
void TileDef::deSerialize(std::istream &is, NodeDrawType drawtype, u16 protocol_version)
{
int version = readU8(is);
if (version < 6)
if (readU8(is) < 6)
throw SerializationError("unsupported TileDef version");

name = deSerializeString16(is);
animation.deSerialize(is, version);
animation.deSerialize(is, protocol_version);
u16 flags = readU16(is);
backface_culling = flags & TILE_FLAG_BACKFACE_CULLING;
tileable_horizontal = flags & TILE_FLAG_TILEABLE_HORIZONTAL;
Expand Down Expand Up @@ -448,8 +444,7 @@ u8 ContentFeatures::getAlphaForLegacy() const

void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
{
const u8 version = CONTENTFEATURES_VERSION;
writeU8(os, version);
writeU8(os, CONTENTFEATURES_VERSION);

// general
os << serializeString16(name);
Expand Down Expand Up @@ -534,9 +529,9 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
collision_box.serialize(os, protocol_version);

// sound
sound_footstep.serialize(os, version);
sound_dig.serialize(os, version);
sound_dug.serialize(os, version);
sound_footstep.serialize(os, protocol_version);
sound_dig.serialize(os, protocol_version);
sound_dug.serialize(os, protocol_version);

// legacy
writeU8(os, legacy_facedir_simple);
Expand All @@ -550,11 +545,9 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, liquid_move_physics);
}

void ContentFeatures::deSerialize(std::istream &is)
void ContentFeatures::deSerialize(std::istream &is, u16 protocol_version)
{
// version detection
const u8 version = readU8(is);
if (version < CONTENTFEATURES_VERSION)
if (readU8(is) < CONTENTFEATURES_VERSION)
throw SerializationError("unsupported ContentFeatures version");

// general
Expand All @@ -576,13 +569,13 @@ void ContentFeatures::deSerialize(std::istream &is)
if (readU8(is) != 6)
throw SerializationError("unsupported tile count");
for (TileDef &td : tiledef)
td.deSerialize(is, version, drawtype);
td.deSerialize(is, drawtype, protocol_version);
for (TileDef &td : tiledef_overlay)
td.deSerialize(is, version, drawtype);
td.deSerialize(is, drawtype, protocol_version);
if (readU8(is) != CF_SPECIAL_COUNT)
throw SerializationError("unsupported CF_SPECIAL_COUNT");
for (TileDef &td : tiledef_special)
td.deSerialize(is, version, drawtype);
td.deSerialize(is, drawtype, protocol_version);
setAlphaFromLegacy(readU8(is));
color.setRed(readU8(is));
color.setGreen(readU8(is));
Expand Down Expand Up @@ -633,9 +626,9 @@ void ContentFeatures::deSerialize(std::istream &is)
collision_box.deSerialize(is);

// sounds
sound_footstep.deSerialize(is, version);
sound_dig.deSerialize(is, version);
sound_dug.deSerialize(is, version);
sound_footstep.deSerialize(is, protocol_version);
sound_dig.deSerialize(is, protocol_version);
sound_dug.deSerialize(is, protocol_version);

// read legacy properties
legacy_facedir_simple = readU8(is);
Expand Down Expand Up @@ -1546,12 +1539,13 @@ void NodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
}


void NodeDefManager::deSerialize(std::istream &is)
void NodeDefManager::deSerialize(std::istream &is, u16 protocol_version)
{
clear();
int version = readU8(is);
if (version != 1)

if (readU8(is) < 1)
throw SerializationError("unsupported NodeDefinitionManager version");

u16 count = readU16(is);
std::istringstream is2(deSerializeString32(is), std::ios::binary);
ContentFeatures f;
Expand All @@ -1561,7 +1555,7 @@ void NodeDefManager::deSerialize(std::istream &is)
// Read it from the string wrapper
std::string wrapper = deSerializeString16(is2);
std::istringstream wrapper_is(wrapper, std::ios::binary);
f.deSerialize(wrapper_is);
f.deSerialize(wrapper_is, protocol_version);

// Check error conditions
if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) {
Expand Down

0 comments on commit a463620

Please sign in to comment.