OpenTTD Source 20250531-master-g621c031307
newgrf_act0_sounds.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_sound.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15
16#include "../safeguards.h"
17
26static ChangeInfoResult SoundEffectChangeInfo(uint first, uint last, int prop, ByteReader &buf)
27{
29 if (first == last) return ret;
30
31 if (_cur_gps.grffile->sound_offset == 0) {
32 GrfMsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
33 return CIR_INVALID_ID;
34 }
35
36 if (last - ORIGINAL_SAMPLE_COUNT > _cur_gps.grffile->num_sounds) {
37 GrfMsg(1, "SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", last, ORIGINAL_SAMPLE_COUNT + _cur_gps.grffile->num_sounds);
38 return CIR_INVALID_ID;
39 }
40
41 for (uint id = first; id < last; ++id) {
42 SoundEntry *sound = GetSound(first + _cur_gps.grffile->sound_offset - ORIGINAL_SAMPLE_COUNT);
43
44 switch (prop) {
45 case 0x08: // Relative volume
46 sound->volume = Clamp(buf.ReadByte(), 0, SOUND_EFFECT_MAX_VOLUME);
47 break;
48
49 case 0x09: // Priority
50 sound->priority = buf.ReadByte();
51 break;
52
53 case 0x0A: { // Override old sound
54 SoundID orig_sound = buf.ReadByte();
55
56 if (orig_sound >= ORIGINAL_SAMPLE_COUNT) {
57 GrfMsg(1, "SoundEffectChangeInfo: Original sound {} not defined (max {})", orig_sound, ORIGINAL_SAMPLE_COUNT);
58 } else {
59 SoundEntry *old_sound = GetSound(orig_sound);
60
61 /* Literally copy the data of the new sound over the original */
62 *old_sound = *sound;
63 }
64 break;
65 }
66
67 default:
68 ret = CIR_UNKNOWN;
69 break;
70 }
71 }
72
73 return ret;
74}
75
77template <> ChangeInfoResult GrfChangeInfoHandler<GSF_SOUNDFX>::Activation(uint first, uint last, int prop, ByteReader &buf) { return SoundEffectChangeInfo(first, last, prop, buf); }
Class to read from a NewGRF file.
uint8_t ReadByte()
Read a single byte (8 bits).
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
static ChangeInfoResult SoundEffectChangeInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for sound effects.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition sound_type.h:124
GRF feature handler.
GRFFile * grffile
Currently processed GRF file.