OpenTTD Source 20250331-master-g3c15e0c889
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
30 if (_cur.grffile->sound_offset == 0) {
31 GrfMsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
32 return CIR_INVALID_ID;
33 }
34
35 if (last - ORIGINAL_SAMPLE_COUNT > _cur.grffile->num_sounds) {
36 GrfMsg(1, "SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", last, ORIGINAL_SAMPLE_COUNT + _cur.grffile->num_sounds);
37 return CIR_INVALID_ID;
38 }
39
40 for (uint id = first; id < last; ++id) {
41 SoundEntry *sound = GetSound(first + _cur.grffile->sound_offset - ORIGINAL_SAMPLE_COUNT);
42
43 switch (prop) {
44 case 0x08: // Relative volume
45 sound->volume = Clamp(buf.ReadByte(), 0, SOUND_EFFECT_MAX_VOLUME);
46 break;
47
48 case 0x09: // Priority
49 sound->priority = buf.ReadByte();
50 break;
51
52 case 0x0A: { // Override old sound
53 SoundID orig_sound = buf.ReadByte();
54
55 if (orig_sound >= ORIGINAL_SAMPLE_COUNT) {
56 GrfMsg(1, "SoundEffectChangeInfo: Original sound {} not defined (max {})", orig_sound, ORIGINAL_SAMPLE_COUNT);
57 } else {
58 SoundEntry *old_sound = GetSound(orig_sound);
59
60 /* Literally copy the data of the new sound over the original */
61 *old_sound = *sound;
62 }
63 break;
64 }
65
66 default:
67 ret = CIR_UNKNOWN;
68 break;
69 }
70 }
71
72 return ret;
73}
74
76template <> 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.