diff options
-rw-r--r-- | src/newgrf.cpp | 6 | ||||
-rw-r--r-- | src/newgrf_sound.cpp | 33 | ||||
-rw-r--r-- | src/newgrf_sound.h | 1 | ||||
-rw-r--r-- | src/sound_type.h | 2 |
4 files changed, 27 insertions, 15 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp index cf45608b6..b9c1bf89e 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -1402,7 +1402,7 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop break; case 0x12: // SFX - rvi->sfx = buf->ReadByte(); + rvi->sfx = GetNewGRFSoundID(_cur.grffile, buf->ReadByte()); break; case PROP_ROADVEH_POWER: // Power in units of 10 HP. @@ -1590,7 +1590,7 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop break; case 0x10: // SFX - svi->sfx = buf->ReadByte(); + svi->sfx = GetNewGRFSoundID(_cur.grffile, buf->ReadByte()); break; case 0x11: { // Cargoes available for refitting @@ -1758,7 +1758,7 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int break; case 0x12: // SFX - avi->sfx = buf->ReadByte(); + avi->sfx = GetNewGRFSoundID(_cur.grffile, buf->ReadByte()); break; case 0x13: { // Cargoes available for refitting diff --git a/src/newgrf_sound.cpp b/src/newgrf_sound.cpp index 60ee60916..0cc113d9d 100644 --- a/src/newgrf_sound.cpp +++ b/src/newgrf_sound.cpp @@ -161,6 +161,22 @@ bool LoadNewGRFSound(SoundEntry *sound) return false; } +/** + * Resolve NewGRF sound ID. + * @param file NewGRF to get sound from. + * @param sound_id GRF-specific sound ID. (GRF-local for IDs above ORIGINAL_SAMPLE_COUNT) + * @return Translated (global) sound ID, or INVALID_SOUND. + */ +SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id) +{ + /* Global sound? */ + if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id; + + sound_id -= ORIGINAL_SAMPLE_COUNT; + if (file == NULL || sound_id >= file->num_sounds) return INVALID_SOUND; + + return file->sound_offset + sound_id; +} /** * Checks whether a NewGRF wants to play a different vehicle sound effect. @@ -185,14 +201,10 @@ bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event) /* Play default sound if callback fails */ if (callback == CALLBACK_FAILED) return false; - if (callback >= ORIGINAL_SAMPLE_COUNT) { - callback -= ORIGINAL_SAMPLE_COUNT; - - /* Play no sound if result is out of range */ - if (callback > file->num_sounds) return true; + callback = GetNewGRFSoundID(file, callback); - callback += file->sound_offset; - } + /* Play no sound, if result is invalid */ + if (callback == INVALID_SOUND) return true; assert(callback < GetNumSounds()); SndPlayVehicleFx(callback, v); @@ -207,11 +219,8 @@ bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event) */ void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile) { - if (sound_id >= ORIGINAL_SAMPLE_COUNT) { - sound_id -= ORIGINAL_SAMPLE_COUNT; - if (sound_id > file->num_sounds) return; - sound_id += file->sound_offset; - } + sound_id = GetNewGRFSoundID(file, sound_id); + if (sound_id == INVALID_SOUND) return; assert(sound_id < GetNumSounds()); SndPlayTileFx(sound_id, tile); diff --git a/src/newgrf_sound.h b/src/newgrf_sound.h index 0d3295327..efded063c 100644 --- a/src/newgrf_sound.h +++ b/src/newgrf_sound.h @@ -33,6 +33,7 @@ enum VehicleSoundEvent { SoundEntry *AllocateSound(uint num); void InitializeSoundPool(); bool LoadNewGRFSound(SoundEntry *sound); +SoundID GetNewGRFSoundID(const struct GRFFile *file, SoundID sound_id); SoundEntry *GetSound(SoundID sound_id); uint GetNumSounds(); bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event); diff --git a/src/sound_type.h b/src/sound_type.h index 72486dcac..76fe25139 100644 --- a/src/sound_type.h +++ b/src/sound_type.h @@ -119,4 +119,6 @@ static const uint ORIGINAL_SAMPLE_COUNT = 73; typedef uint16 SoundID; +static const SoundID INVALID_SOUND = 0xFFFF; + #endif /* SOUND_TYPE_H */ |