Index: src/newgrf.cpp =================================================================== --- src/newgrf.cpp (revision 27506) +++ src/newgrf.cpp (working copy) @@ -1402,7 +1402,7 @@ 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 @@ 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 @@ break; case 0x12: // SFX - avi->sfx = buf->ReadByte(); + avi->sfx = GetNewGRFSoundId(_cur.grffile, buf->ReadByte()); break; case 0x13: { // Cargoes available for refitting Index: src/newgrf_sound.cpp =================================================================== --- src/newgrf_sound.cpp (revision 27506) +++ src/newgrf_sound.cpp (working copy) @@ -161,7 +161,23 @@ return false; } +/** + * Resolve NewGRF sounds IDs. + * @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. * @param v Vehicle to play sound effect for. @@ -185,15 +201,11 @@ /* Play default sound if callback fails */ if (callback == CALLBACK_FAILED) return false; - if (callback >= ORIGINAL_SAMPLE_COUNT) { - callback -= ORIGINAL_SAMPLE_COUNT; + callback = GetNewGRFSoundId(file, callback); - /* Play no sound if result is out of range */ - if (callback > file->num_sounds) return true; + /* Play no sound, if result is invalid */ + if (callback == INVALID_SOUND) return true; - callback += file->sound_offset; - } - assert(callback < GetNumSounds()); SndPlayVehicleFx(callback, v); return true; @@ -207,11 +219,8 @@ */ 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); Index: src/newgrf_sound.h =================================================================== --- src/newgrf_sound.h (revision 27506) +++ src/newgrf_sound.h (working copy) @@ -33,6 +33,7 @@ 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); Index: src/sound_type.h =================================================================== --- src/sound_type.h (revision 27506) +++ src/sound_type.h (working copy) @@ -119,4 +119,6 @@ typedef uint16 SoundID; +static const SoundID INVALID_SOUND = 0xFFFF; + #endif /* SOUND_TYPE_H */