diff options
author | frosch <frosch@openttd.org> | 2016-02-08 21:05:57 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2016-02-08 21:05:57 +0000 |
commit | f6577b6ad7eaeda4ad303d02bb1d54496829b475 (patch) | |
tree | 63f9bd5f28dc664b446c6c8b2c7d1eea98f15bbf /src/newgrf_sound.cpp | |
parent | 8f621dc5069eab6d44cf4b44e734b2c2c04d07ea (diff) | |
download | openttd-f6577b6ad7eaeda4ad303d02bb1d54496829b475.tar.xz |
(svn r27507) -Add: [NewGRF] Allow custom sound IDs in RV property 0x12, ship property 0x10 and aircraft property 0x12.
Diffstat (limited to 'src/newgrf_sound.cpp')
-rw-r--r-- | src/newgrf_sound.cpp | 33 |
1 files changed, 21 insertions, 12 deletions
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); |