Loading

Paste #ppj9fc2wl

  1. Index: src/newgrf.cpp
  2. ===================================================================
  3. --- src/newgrf.cpp  (revision 27506)
  4. +++ src/newgrf.cpp  (working copy)
  5. @@ -1402,7 +1402,7 @@
  6.                 break;
  7.  
  8.             case 0x12: // SFX
  9. -               rvi->sfx = buf->ReadByte();
  10. +               rvi->sfx = GetNewGRFSoundId(_cur.grffile, buf->ReadByte());
  11.                 break;
  12.  
  13.             case PROP_ROADVEH_POWER: // Power in units of 10 HP.
  14. @@ -1590,7 +1590,7 @@
  15.                 break;
  16.  
  17.             case 0x10: // SFX
  18. -               svi->sfx = buf->ReadByte();
  19. +               svi->sfx = GetNewGRFSoundId(_cur.grffile, buf->ReadByte());
  20.                 break;
  21.  
  22.             case 0x11: { // Cargoes available for refitting
  23. @@ -1758,7 +1758,7 @@
  24.                 break;
  25.  
  26.             case 0x12: // SFX
  27. -               avi->sfx = buf->ReadByte();
  28. +               avi->sfx = GetNewGRFSoundId(_cur.grffile, buf->ReadByte());
  29.                 break;
  30.  
  31.             case 0x13: { // Cargoes available for refitting
  32. Index: src/newgrf_sound.cpp
  33. ===================================================================
  34. --- src/newgrf_sound.cpp    (revision 27506)
  35. +++ src/newgrf_sound.cpp    (working copy)
  36. @@ -161,7 +161,23 @@
  37.     return false;
  38.  }
  39.  
  40. +/**
  41. + * Resolve NewGRF sounds IDs.
  42. + * @param file NewGRF to get sound from.
  43. + * @param sound_id GRF-specific sound ID. (GRF-local for IDs above ORIGINAL_SAMPLE_COUNT)
  44. + * @return Translated (global) sound ID, or INVALID_SOUND.
  45. + */
  46. +SoundID GetNewGRFSoundId(const GRFFile *file, SoundID sound_id)
  47. +{
  48. +   /* Global sound? */
  49. +   if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
  50.  
  51. +   sound_id -= ORIGINAL_SAMPLE_COUNT;
  52. +   if (file == NULL || sound_id >= file->num_sounds) return INVALID_SOUND;
  53. +
  54. +   return file->sound_offset  + sound_id;
  55. +}
  56. +
  57.  /**
  58.   * Checks whether a NewGRF wants to play a different vehicle sound effect.
  59.   * @param v Vehicle to play sound effect for.
  60. @@ -185,15 +201,11 @@
  61.     /* Play default sound if callback fails */
  62.     if (callback == CALLBACK_FAILED) return false;
  63.  
  64. -   if (callback >= ORIGINAL_SAMPLE_COUNT) {
  65. -       callback -= ORIGINAL_SAMPLE_COUNT;
  66. +   callback = GetNewGRFSoundId(file, callback);
  67.  
  68. -       /* Play no sound if result is out of range */
  69. -       if (callback > file->num_sounds) return true;
  70. +   /* Play no sound, if result is invalid */
  71. +   if (callback == INVALID_SOUND) return true;
  72.  
  73. -       callback += file->sound_offset;
  74. -   }
  75. -
  76.     assert(callback < GetNumSounds());
  77.     SndPlayVehicleFx(callback, v);
  78.     return true;
  79. @@ -207,11 +219,8 @@
  80.   */
  81.  void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
  82.  {
  83. -   if (sound_id >= ORIGINAL_SAMPLE_COUNT) {
  84. -       sound_id -= ORIGINAL_SAMPLE_COUNT;
  85. -       if (sound_id > file->num_sounds) return;
  86. -       sound_id += file->sound_offset;
  87. -   }
  88. +   sound_id = GetNewGRFSoundId(file, sound_id);
  89. +   if (sound_id == INVALID_SOUND) return;
  90.  
  91.     assert(sound_id < GetNumSounds());
  92.     SndPlayTileFx(sound_id, tile);
  93. Index: src/newgrf_sound.h
  94. ===================================================================
  95. --- src/newgrf_sound.h  (revision 27506)
  96. +++ src/newgrf_sound.h  (working copy)
  97. @@ -33,6 +33,7 @@
  98.  SoundEntry *AllocateSound(uint num);
  99.  void InitializeSoundPool();
  100.  bool LoadNewGRFSound(SoundEntry *sound);
  101. +SoundID GetNewGRFSoundId(const struct GRFFile *file, SoundID sound_id);
  102.  SoundEntry *GetSound(SoundID sound_id);
  103.  uint GetNumSounds();
  104.  bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event);
  105. Index: src/sound_type.h
  106. ===================================================================
  107. --- src/sound_type.h    (revision 27506)
  108. +++ src/sound_type.h    (working copy)
  109. @@ -119,4 +119,6 @@
  110.  
  111.  typedef uint16 SoundID;
  112.  
  113. +static const SoundID INVALID_SOUND = 0xFFFF;
  114. +
  115.  #endif /* SOUND_TYPE_H */
  116.  

Comments