| 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" |
| 80 | |
| 81 | #include "hlsoundlib.h" |
| 82 | |
| 83 | #include "room.h" |
| 84 | #include "game.h" |
| 85 | #include "psrand.h" |
| 86 | |
| 87 | #define MAX_AMBIENT_SOUND_PATTERNS 100 |
| 88 | asp Ambient_sound_patterns[MAX_AMBIENT_SOUND_PATTERNS]; |
| 89 | int 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 randf() { return ((float)ps_rand()) / ((float)D3_RAND_MAX); } |
| 94 | |
| 95 | // Process an Ambient Sound Pattern |
| 96 | static void ProcessASP(asp *asp) { |
| 97 | // Check for empty ASP |
| 98 | if (!asp->num_sounds) |
| 99 | return; |
| 100 | |
| 101 | // Decrement delay |
| 102 | asp->delay -= Frametime; |
| 103 | |
| 104 | // Time to play? |
| 105 | if (asp->delay < 0.0) { |
| 106 | |
| 107 | // Figure out which sound to play |
| 108 | int roll = (ps_rand() * 100) / (D3_RAND_MAX + 1); // roll = 0..99 |
| 109 | int s; |
| 110 | |
| 111 | for (s = 0; s < asp->num_sounds; s++) |
| 112 | if ((roll -= asp->sounds[s].probability) < 0) |
| 113 | break; |
| 114 | |
| 115 | ASSERT(s < asp->num_sounds); |
| 116 | |
| 117 | // Play sound i |
| 118 | ase *sound = &asp->sounds[s]; |
| 119 | float volume = sound->min_volume + (sound->max_volume - sound->min_volume) * randf(); |
| 120 | Sound_system.Play2dSound(sound->handle, volume); |
| 121 | |
| 122 | // Compute delay until next sound |
| 123 | asp->delay = asp->min_delay + (asp->max_delay - asp->min_delay) * randf(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Does the ambient sound processing for one frame, maybe playing a sound or two |
| 128 | void DoAmbientSounds() { |
| 129 | if (OBJECT_OUTSIDE(Viewer_object)) |
| 130 | return; |
| 131 | |
| 132 | room *rp = &Rooms[Viewer_object->roomnum]; |
| 133 | |
| 134 | if (rp->ambient_sound != -1) |
| 135 | ProcessASP(&Ambient_sound_patterns[rp->ambient_sound]); |
| 136 | } |
| 137 | |
| 138 | // Initializes the ambient sounds for the current level |
| 139 | void InitAmbientSounds() { |
| 140 | // Loop through all ambient sound patterns |
| 141 | for (int p = 0; p < Num_ambient_sound_patterns; p++) { |
| 142 | asp *asp = &Ambient_sound_patterns[p]; |
| 143 | |
| 144 | // Initialize delay |
| 145 | asp->delay = asp->min_delay + (asp->max_delay - asp->min_delay) * randf(); |
| 146 | |
| 147 | // Make sure the probabilities add up |
| 148 | int prob = 0; |
| 149 | for (int s = 0; s < asp->num_sounds; s++) |
| 150 | prob += asp->sounds[s].probability; |
| 151 | if (asp->num_sounds && prob != 100) { |
| 152 | Int3(); |
| 153 | asp->sounds[0].probability += 100 - prob; // make it total 100 |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #include "ddio.h" |
| 159 | #include "soundload.h" |
| 160 | #include "descent.h" |
| 161 | #include "mem.h" |
| 162 | |
| 163 | // Close down ambient sound system and free data |
| 164 | static void FreeAmbientSoundData() { |
| 165 | for (int p = 0; p < Num_ambient_sound_patterns; p++) |
| 166 | if (Ambient_sound_patterns[p].num_sounds) |
| 167 | mem_free(Ambient_sound_patterns[p].sounds); |
| 168 | |
| 169 | Num_ambient_sound_patterns = 0; |
| 170 | } |
| 171 | |
| 172 | // Initialize the ambient sound system |
| 173 | void InitAmbientSoundSystem() { |
| 174 | atexit(FreeAmbientSoundData); |
| 175 | |
| 176 | ReadAmbientData(); |
| 177 | |
| 178 | // Get rid of deleted patterns |
| 179 | for (int p = 0; p < Num_ambient_sound_patterns; p++) { |
| 180 | |
| 181 | asp *asp = &Ambient_sound_patterns[p]; |
| 182 | |
| 183 | if (!asp->name[0]) { |
| 184 | Ambient_sound_patterns[p] = Ambient_sound_patterns[Num_ambient_sound_patterns - 1]; |
| 185 | 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 FindAmbientSoundPattern(char *aspname) { |
| 193 | if (!aspname[0]) |
| 194 | return -1; |
| 195 | |
| 196 | for (int i = 0; i < Num_ambient_sound_patterns; i++) |
| 197 | if (stricmp(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 *AmbientSoundPatternName(int n) { return Ambient_sound_patterns[n].name; } |
| 205 | |
| 206 | #define AMBIENT_FILE_ID "ASPF" |
| 207 | #define AMBIENT_FILE_VERSION 0 |
| 208 | |
| 209 | // Reads data from the ambient sound data file |
| 210 | void ReadAmbientData() { |
| 211 | CFILE *ifile; |
| 212 | char file_id[sizeof(AMBIENT_FILE_ID)]; |
| 213 | int version; |
| 214 | |
| 215 | // Get rid of any old data |
| 216 | FreeAmbientSoundData(); |
| 217 | |
| 218 | // Build filename |
| 219 | ifile = cfopen(AMBIENT_FILE_NAME, "rb"); |
| 220 | |
| 221 | if (!ifile) { |
| 222 | Int3(); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Read file ID |
| 227 | cf_ReadBytes((uint8_t *)file_id, strlen(AMBIENT_FILE_ID), ifile); |
| 228 | if (strncmp(file_id, AMBIENT_FILE_ID, strlen(AMBIENT_FILE_ID)) != 0) { |
| 229 | Int3(); |
| 230 | cfclose(ifile); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | // Read version |
| 235 | version = cf_ReadInt(ifile); |
| 236 | |
| 237 | if (version > AMBIENT_FILE_VERSION) { |
| 238 | Int3(); |
| 239 | cfclose(ifile); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Get the number of patterns |
| 244 | Num_ambient_sound_patterns = cf_ReadInt(ifile); |
| 245 | |
| 246 | // Read the patterns |
| 247 | for (int p = 0; p < Num_ambient_sound_patterns; p++) { |
| 248 | |
| 249 | asp *asp = &Ambient_sound_patterns[p]; |
| 250 | |
| 251 | cf_ReadString(asp->name, sizeof(asp->name), ifile); |
| 252 | |
| 253 | asp->min_delay = cf_ReadFloat(ifile); |
| 254 | asp->max_delay = cf_ReadFloat(ifile); |
| 255 | |
| 256 | asp->num_sounds = cf_ReadInt(ifile); |
| 257 | |
| 258 | if (asp->num_sounds > 0) |
| 259 | asp->sounds = (ase *)mem_malloc(sizeof(*asp->sounds) * asp->num_sounds); |
| 260 | else |
| 261 | asp->sounds = NULL; |
| 262 | |
| 263 | int prob = 0; |
| 264 | for (int s = 0; s < asp->num_sounds; s++) { |
| 265 | char tbuf[PAGENAME_LEN]; |
| 266 | |
| 267 | cf_ReadString(tbuf, sizeof(tbuf), ifile); |
| 268 | asp->sounds[s].handle = FindSoundName(IGNORE_TABLE(tbuf)); |
| 269 | |
| 270 | asp->sounds[s].min_volume = cf_ReadFloat(ifile); |
| 271 | asp->sounds[s].max_volume = cf_ReadFloat(ifile); |
| 272 | asp->sounds[s].probability = cf_ReadInt(ifile); |
| 273 | |
| 274 | prob += asp->sounds[s].probability; |
| 275 | } |
| 276 | |
| 277 | if (asp->num_sounds && (prob != 100)) { |
| 278 | Int3(); |
| 279 | asp->sounds[0].probability += 100 - prob; // make it total 100 |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | cfclose(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 WriteAmbientData() { |
| 292 | char filename[_MAX_PATH]; |
| 293 | CFILE *ofile; |
| 294 | |
| 295 | #ifndef NEWEDITOR |
| 296 | ddio_MakePath(filename, Base_directory_write, "data", "misc", AMBIENT_FILE_NAME, NULL); |
| 297 | #else |
| 298 | ddio_MakePath(filename, D3HogDir, "data", "misc", AMBIENT_FILE_NAME, NULL); |
| 299 | #endif |
| 300 | ofile = cfopen(filename, "wb"); |
| 301 | |
| 302 | if (!ofile) { |
| 303 | Int3(); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Write file ID & version |
| 308 | cf_WriteBytes((uint8_t *)AMBIENT_FILE_ID, strlen(AMBIENT_FILE_ID), ofile); |
| 309 | cf_WriteInt(ofile, AMBIENT_FILE_VERSION); |
| 310 | |
| 311 | // Write the number of patterns |
| 312 | cf_WriteInt(ofile, Num_ambient_sound_patterns); |
| 313 | |
| 314 | // Read the patterns |
| 315 | for (int p = 0; p < Num_ambient_sound_patterns; p++) { |
| 316 | |
| 317 | asp *asp = &Ambient_sound_patterns[p]; |
| 318 | |
| 319 | cf_WriteString(ofile, asp->name); |
| 320 | |
| 321 | cf_WriteFloat(ofile, asp->min_delay); |
| 322 | cf_WriteFloat(ofile, asp->max_delay); |
| 323 | |
| 324 | cf_WriteInt(ofile, asp->num_sounds); |
| 325 | |
| 326 | for (int s = 0; s < asp->num_sounds; s++) { |
| 327 | cf_WriteString(ofile, Sounds[asp->sounds[s].handle].name); |
| 328 | |
| 329 | cf_WriteFloat(ofile, asp->sounds[s].min_volume); |
| 330 | cf_WriteFloat(ofile, asp->sounds[s].max_volume); |
| 331 | cf_WriteInt(ofile, asp->sounds[s].probability); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | cfclose(ofile); |
| 336 | } |