Debian Codegraph @

Descent3/ambient.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ambient.cpp $
21 * $Revision: 13 $
22 * $Date: 8/10/99 2:10p $
23 * $Author: Gwar $
24 *
25 * Ambient sound system
26 *
27 * $Log: /DescentIII/Main/ambient.cpp $
28 *
29 * 13 8/10/99 2:10p Gwar
30 * added sound support to neweditor
31 *
32 * 12 5/10/99 10:00p Ardussi
33 * changes to compile on Mac
34 *
35 * 11 4/21/99 11:05a Kevin
36 * new ps_rand and ps_srand to replace rand & srand
37 *
38 * 10 4/14/99 2:50a Jeff
39 * fixed some case mismatched #includes
40 *
41 * 9 3/24/99 7:49p Jason
42 * fixed multiplayer bugs
43 *
44 * 8 2/10/99 4:44p Jeff
45 * table file parser stuff
46 *
47 * 7 2/10/99 2:31p Matt
48 * Deleted some unused code and fixed a slightly stupid thing.
49 *
50 * 6 1/21/99 11:15p Jeff
51 * pulled out some structs and defines from header files and moved them
52 * into seperate header files so that multiplayer dlls don't require major
53 * game headers, just those new headers. Side effect is a shorter build
54 * time. Also cleaned up some header file #includes that weren't needed.
55 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
56 * manage.h and multi.h
57 *
58 * 5 10/09/98 4:13p Matt
59 * Don't specify explicit path for ambient sound pattern file.
60 *
61 * 4 10/08/98 4:23p Kevin
62 * Changed code to comply with memory library usage. Always use mem_malloc
63 * , mem_free and mem_strdup
64 *
65 * 3 8/20/98 12:35p Matt
66 * Added nice editing for ambient sound patterns
67 *
68 * 2 8/17/98 8:32p Craig
69 * Fixed Matt's bug.
70 *
71 * 1 8/17/98 7:00p Matt
72 * Added ambient sound system
73 *
74 */
75
76 #include <stdlib.h>
77 #include <string.h>
78
79 #include "ambient.h"cxx . . $ `<file>/Descent3/ambient.h`/
80
81 #include "hlsoundlib.h"cxx . . $ `<file>/sndlib/hlsoundlib.h`/
82
83 #include "room.h"cxx . . $ `<file>/Descent3/room.h`/
84 #include "game.h"cxx . . $ `<file>/Descent3/game.h`/
85 #include "psrand.h"cxx . . $ `<file>/misc/psrand.h`/
86
87 #define MAX_AMBIENT_SOUND_PATTERNScxx . . $ `Descent3/ambient.cpp:87:9`! 100
88 aspcxx . . $ asp# Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[MAX_AMBIENT_SOUND_PATTERNScxx . . $ `Descent3/ambient.cpp:87:9`!];
89 int Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns. = 0;
90
91 // Computes a floating-point pseudo-random number.
92 // Returns value in the range 0..1, with the precision 1/D3_RAND_MAX
93 static float randfcxx . . $ randf(9b8c9e39889c762c).() { return ((float)ps_randcxx . . $ ps_rand(b126dc7c1de90089).()) / ((float)D3_RAND_MAXcxx . . $ `misc/psrand.h:41:9`!); }
94
95 // Process an Ambient Sound Pattern
96 static void ProcessASPcxx . . $ ProcessASP(e783b886370c504e).(aspcxx . . $ asp# *asp) {
97 // Check for empty ASP
98 if (!asp->num_soundscxx . . $ asp#num_sounds.)
99 return;
100
101 // Decrement delay
102 asp->delaycxx . . $ asp#delay. -= Frametimecxx . . $ Frametime.;
103
104 // Time to play?
105 if (asp->delaycxx . . $ asp#delay. < 0.0) {
106
107 // Figure out which sound to play
108 int roll = (ps_randcxx . . $ ps_rand(b126dc7c1de90089).() * 100) / (D3_RAND_MAXcxx . . $ `misc/psrand.h:41:9`! + 1); // roll = 0..99
109 int s;
110
111 for (s = 0; s < asp->num_soundscxx . . $ asp#num_sounds.; s++)
112 if ((roll -= asp->soundscxx . . $ asp#sounds.[s].probabilitycxx . . $ ase#probability.) < 0)
113 break;
114
115 ASSERTcxx . . $ `misc/pserror.h:246:9`!(s < asp->num_sounds);
116
117 // Play sound i
118 asecxx . . $ ase# *sound = &asp->soundscxx . . $ asp#sounds.[s];
119 float volume = sound->min_volumecxx . . $ ase#min_volume. + (sound->max_volumecxx . . $ ase#max_volume. - sound->min_volumecxx . . $ ase#min_volume.) * randfcxx . . $ randf(9b8c9e39889c762c).();
120 Sound_systemcxx . . $ Sound_system..Play2dSoundcxx . . $ hlsSystem#Play2dSound(d251026853493025).(sound->handlecxx . . $ ase#handle., volume);
121
122 // Compute delay until next sound
123 asp->delaycxx . . $ asp#delay. = asp->min_delaycxx . . $ asp#min_delay. + (asp->max_delaycxx . . $ asp#max_delay. - asp->min_delaycxx . . $ asp#min_delay.) * randfcxx . . $ randf(9b8c9e39889c762c).();
124 }
125 }
126
127 // Does the ambient sound processing for one frame, maybe playing a sound or two
128 void DoAmbientSoundscxx . . $ DoAmbientSounds(49f6e7a06ebc5aa8).() {
129 if (OBJECT_OUTSIDEcxx . . $ `lib/object_external_struct.h:130:9`!(Viewer_object))
130 return;
131
132 roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[Viewer_objectcxx . . $ Viewer_object.->roomnumcxx . . $ object#roomnum.];
133
134 if (rp->ambient_soundcxx . . $ room#ambient_sound. != -1)
135 ProcessASPcxx . . $ ProcessASP(e783b886370c504e).(&Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[rp->ambient_soundcxx . . $ room#ambient_sound.]);
136 }
137
138 // Initializes the ambient sounds for the current level
139 void InitAmbientSoundscxx . . $ InitAmbientSounds(49f6e7a06ebc5aa8).() {
140 // Loop through all ambient sound patterns
141 for (int p = 0; p < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; p++) {
142 aspcxx . . $ asp# *asp = &Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p];
143
144 // Initialize delay
145 asp->delaycxx . . $ asp#delay. = asp->min_delaycxx . . $ asp#min_delay. + (asp->max_delaycxx . . $ asp#max_delay. - asp->min_delaycxx . . $ asp#min_delay.) * randfcxx . . $ randf(9b8c9e39889c762c).();
146
147 // Make sure the probabilities add up
148 int prob = 0;
149 for (int s = 0; s < asp->num_soundscxx . . $ asp#num_sounds.; s++)
150 prob += asp->soundscxx . . $ asp#sounds.[s].probabilitycxx . . $ ase#probability.;
151 if (asp->num_soundscxx . . $ asp#num_sounds. && prob != 100) {
152 Int3cxx . . $ `misc/pserror.h:247:9`!();
153 asp->soundscxx . . $ asp#sounds.[0].probabilitycxx . . $ ase#probability. += 100 - prob; // make it total 100
154 }
155 }
156 }
157
158 #include "ddio.h"cxx . . $ `<file>/ddio/ddio.h`/
159 #include "soundload.h"cxx . . $ `<file>/sndlib/soundload.h`/
160 #include "descent.h"cxx . . $ `<file>/Descent3/descent.h`/
161 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
162
163 // Close down ambient sound system and free data
164 static void FreeAmbientSoundDatacxx . . $ FreeAmbientSoundData(49f6e7a06ebc5aa8).() {
165 for (int p = 0; p < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; p++)
166 if (Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p].num_soundscxx . . $ asp#num_sounds.)
167 mem_freecxx . . $ `mem/mem.h:80:9`!(Ambient_sound_patterns[p].sounds);
168
169 Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns. = 0;
170 }
171
172 // Initialize the ambient sound system
173 void InitAmbientSoundSystemcxx . . $ InitAmbientSoundSystem(49f6e7a06ebc5aa8).() {
174 atexitcxx . . $ atexit(cc8c495369588262).(FreeAmbientSoundDatacxx . . $ FreeAmbientSoundData(49f6e7a06ebc5aa8).);
175
176 ReadAmbientDatacxx . . $ ReadAmbientData(49f6e7a06ebc5aa8).();
177
178 // Get rid of deleted patterns
179 for (int p = 0; p < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; p++) {
180
181 aspcxx . . $ asp# *asp = &Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p];
182
183 if (!asp->namecxx . . $ asp#name.[0]) {
184 Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p] =cxx . . $ asp#`operator=`(43bffafc1badf891). Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns. - 1];
185 Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.--;
186 }
187 }
188 }
189
190 // Return the index of a named ambient sound pattern
191 // Returns number, or -1 if can't find
192 int FindAmbientSoundPatterncxx . . $ FindAmbientSoundPattern(1ef8e6ec083aabb9).(char *aspname) {
193 if (!aspname[0])
194 return -1;
195
196 for (int i = 0; i < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; i++)
197 if (stricmpcxx . . $ `linux/linux_fix.h:43:9`!(Ambient_sound_patterns[i].name, aspname) == 0)
198 return i;
199
200 return -1;
201 }
202
203 // Returns a pointer to the name of the specified ambient sound pattern
204 char *AmbientSoundPatternNamecxx . . $ AmbientSoundPatternName(ff224f080444ee45).(int n) { return Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[n].namecxx . . $ asp#name.; }
205
206 #define AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`! "ASPF"
207 #define AMBIENT_FILE_VERSIONcxx . . $ `Descent3/ambient.cpp:207:9`! 0
208
209 // Reads data from the ambient sound data file
210 void ReadAmbientDatacxx . . $ ReadAmbientData(49f6e7a06ebc5aa8).() {
211 CFILEcxx . . $ CFILE# *ifile;
212 char file_id[sizeof(AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!)];
213 int version;
214
215 // Get rid of any old data
216 FreeAmbientSoundDatacxx . . $ FreeAmbientSoundData(49f6e7a06ebc5aa8).();
217
218 // Build filename
219 ifile = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(AMBIENT_FILE_NAMEcxx . . $ `Descent3/ambient.h:67:9`!, "rb");
220
221 if (!ifile) {
222 Int3cxx . . $ `misc/pserror.h:247:9`!();
223 return;
224 }
225
226 // Read file ID
227 cf_ReadBytescxx . . $ cf_ReadBytes(de6325ef3380a744).((uint8_tcxx . . $ uint8_t# *)file_id, strlen(AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!), ifile);
228 if (strncmp(file_id, AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!, strlen(AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!)) != 0) {
229 Int3cxx . . $ `misc/pserror.h:247:9`!();
230 cfclosecxx . . $ cfclose(3639e799710acb68).(ifile);
231 return;
232 }
233
234 // Read version
235 version = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile);
236
237 if (version > AMBIENT_FILE_VERSIONcxx . . $ `Descent3/ambient.cpp:207:9`!) {
238 Int3cxx . . $ `misc/pserror.h:247:9`!();
239 cfclosecxx . . $ cfclose(3639e799710acb68).(ifile);
240 return;
241 }
242
243 // Get the number of patterns
244 Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns. = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile);
245
246 // Read the patterns
247 for (int p = 0; p < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; p++) {
248
249 aspcxx . . $ asp# *asp = &Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p];
250
251 cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(asp->namecxx . . $ asp#name., sizeof(asp->namecxx . . $ asp#name.), ifile);
252
253 asp->min_delaycxx . . $ asp#min_delay. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(ifile);
254 asp->max_delaycxx . . $ asp#max_delay. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(ifile);
255
256 asp->num_soundscxx . . $ asp#num_sounds. = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile);
257
258 if (asp->num_soundscxx . . $ asp#num_sounds. > 0)
259 asp->soundscxx . . $ asp#sounds. = (asecxx . . $ ase# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(*asp->sounds) * asp->num_sounds);
260 else
261 asp->soundscxx . . $ asp#sounds. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
262
263 int prob = 0;
264 for (int s = 0; s < asp->num_soundscxx . . $ asp#num_sounds.; s++) {
265 char tbuf[PAGENAME_LENcxx . . $ `lib/manage_external.h:49:9`!];
266
267 cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(tbuf, sizeof(tbuf), ifile);
268 asp->soundscxx . . $ asp#sounds.[s].handlecxx . . $ ase#handle. = FindSoundNamecxx . . $ FindSoundName(5c4306d8553b3696).(IGNORE_TABLEcxx . . $ `lib/manage_external.h:54:9`!(tbuf));
269
270 asp->soundscxx . . $ asp#sounds.[s].min_volumecxx . . $ ase#min_volume. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(ifile);
271 asp->soundscxx . . $ asp#sounds.[s].max_volumecxx . . $ ase#max_volume. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(ifile);
272 asp->soundscxx . . $ asp#sounds.[s].probabilitycxx . . $ ase#probability. = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile);
273
274 prob += asp->soundscxx . . $ asp#sounds.[s].probabilitycxx . . $ ase#probability.;
275 }
276
277 if (asp->num_soundscxx . . $ asp#num_sounds. && (prob != 100)) {
278 Int3cxx . . $ `misc/pserror.h:247:9`!();
279 asp->soundscxx . . $ asp#sounds.[0].probabilitycxx . . $ ase#probability. += 100 - prob; // make it total 100
280 }
281 }
282
283 cfclosecxx . . $ cfclose(3639e799710acb68).(ifile);
284 }
285
286 #ifdef NEWEDITOR
287 extern char D3HogDir[_MAX_PATH * 2];
288 #endif
289
290 // Writes data from the ambient sound data file
291 void WriteAmbientDatacxx . . $ WriteAmbientData(49f6e7a06ebc5aa8).() {
292 char filename[_MAX_PATHcxx . . $ `linux/linux_fix.h:34:9`!];
293 CFILEcxx . . $ CFILE# *ofile;
294
295 #ifndef NEWEDITOR
296 ddio_MakePathcxx . . $ ddio_MakePath(9ad4b0815420a85).(filename, Base_directory_writecxx . . $ Base_directory_write., "data", "misc", AMBIENT_FILE_NAMEcxx . . $ `Descent3/ambient.h:67:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!);
297 #else
298 ddio_MakePath(filename, D3HogDir, "data", "misc", AMBIENT_FILE_NAME, NULL);
299 #endif
300 ofile = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(filenamecxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., "wb");
301
302 if (!ofile) {
303 Int3cxx . . $ `misc/pserror.h:247:9`!();
304 return;
305 }
306
307 // Write file ID & version
308 cf_WriteBytescxx . . $ cf_WriteBytes(b35058f74b39c3a6).((uint8_tcxx . . $ uint8_t# *)AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!, strlen(AMBIENT_FILE_IDcxx . . $ `Descent3/ambient.cpp:206:9`!), ofile);
309 cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(ofile, AMBIENT_FILE_VERSIONcxx . . $ `Descent3/ambient.cpp:207:9`!);
310
311 // Write the number of patterns
312 cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(ofile, Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.);
313
314 // Read the patterns
315 for (int p = 0; p < Num_ambient_sound_patternscxx . . $ Num_ambient_sound_patterns.; p++) {
316
317 aspcxx . . $ asp# *asp = &Ambient_sound_patternscxx . . $ Ambient_sound_patterns.[p];
318
319 cf_WriteStringcxx . . $ cf_WriteString(643e1411966bd56c).(ofile, asp->namecxx . . $ asp#name.);
320
321 cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(ofile, asp->min_delaycxx . . $ asp#min_delay.);
322 cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(ofile, asp->max_delaycxx . . $ asp#max_delay.);
323
324 cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(ofile, asp->num_soundscxx . . $ asp#num_sounds.);
325
326 for (int s = 0; s < asp->num_soundscxx . . $ asp#num_sounds.; s++) {
327 cf_WriteStringcxx . . $ cf_WriteString(643e1411966bd56c).(ofile, Soundscxx . . $ Sounds.[asp->soundscxx . . $ asp#sounds.[s].handlecxx . . $ ase#handle.].namecxx . . $ sound_info#name.);
328
329 cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(ofile, asp->soundscxx . . $ asp#sounds.[s].min_volumecxx . . $ ase#min_volume.);
330 cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(ofile, asp->soundscxx . . $ asp#sounds.[s].max_volumecxx . . $ ase#max_volume.);
331 cf_WriteIntcxx . . $ cf_WriteInt(966989f25543eb13).(ofile, asp->soundscxx . . $ asp#sounds.[s].probabilitycxx . . $ ase#probability.);
332 }
333 }
334
335 cfclosecxx . . $ cfclose(3639e799710acb68).(ofile);
336 }