| 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 | |
| 19 | #include "d3music.h" |
| 20 | #include "music.h" |
| 21 | |
| 22 | // generic constants |
| 23 | #define MUSIC_IDLE_TIME 240.0f // how many seconds before background music silences. |
| 24 | |
| 25 | // flags for musicAI |
| 26 | #define MUSICAIF_PLAYERDEAD 0x1 |
| 27 | #define MUSICAIF_PLAYERINVUL 0x2 |
| 28 | #define MUSICAIF_STARTLEVEL 0x4 // game started a new level |
| 29 | #define MUSICAIF_HOSTILES 0x8 // there are hostiles around |
| 30 | #define MUSICAIF_NEWREGION 0x40 |
| 31 | |
| 32 | // names of themes. (match to music.h THEME_TYPES) |
| 33 | const char *const Music_type_names[] = {"No song", "Intro song", "Idle song", "Combat song", |
| 34 | "Death song", "Idle-Combat song", "Combat-Idle song"}; |
| 35 | |
| 36 | // global data |
| 37 | #ifdef _DEBUG |
| 38 | bool Music_debug_verbose = false; // debug flag for music system |
| 39 | #endif |
| 40 | |
| 41 | // in library data. |
| 42 | static OutrageMusicSeq Music_seq; |
| 43 | static bool Music_on = false; |
| 44 | static bool Allow_music = false; |
| 45 | static float Music_volume = 1.0f; |
| 46 | |
| 47 | // music ai structure. |
| 48 | static struct { |
| 49 | float peace_timer; // how long has player been out of combat. |
| 50 | int flags; // current state of music AI. |
| 51 | int cur_song; // current song |
| 52 | int n_hostiles; // number of hostiles ai thinks there are. |
| 53 | tMusicVal trigger; |
| 54 | int16_t pending_region; // pending region change. |
| 55 | bool was_toggled_on; // music was turned on by user. |
| 56 | bool immediate_switch; // force switch immediately for regions |
| 57 | bool restart_sequencer; // restarts sequencer next frame. |
| 58 | } MusicAI; |
| 59 | |
| 60 | // handles operations on current events. |
| 61 | static void D3MusicSongSelector(); |
| 62 | |
| 63 | // creates music ai struction from music info passed in. |
| 64 | static void D3MusicAI(tMusicSeqInfo *music_info); |
| 65 | |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | void InitD3Music(bool allow_music) { |
| 69 | Music_on = false; |
| 70 | Allow_music = allow_music; |
| 71 | } |
| 72 | |
| 73 | void CloseD3Music() { Music_on = false; } |
| 74 | |
| 75 | // is music system on? |
| 76 | bool IsD3MusicOn() { return Music_on; } |
| 77 | |
| 78 | // starts up the music sequencer |
| 79 | void D3MusicStart(const char *theme_file) { |
| 80 | if (theme_file && Music_seq.Init(theme_file)) { |
| 81 | Music_on = true; |
| 82 | Music_seq.SetVolume(Music_volume); |
| 83 | } else { |
| 84 | Music_on = false; |
| 85 | } |
| 86 | |
| 87 | if (!Allow_music) { |
| 88 | Music_on = false; |
| 89 | } |
| 90 | |
| 91 | if (Music_on) { |
| 92 | if (Music_volume > 0.0f) { |
| 93 | Music_seq.Start(); |
| 94 | } else { |
| 95 | Music_on = false; // volume was 0.0f, so don't mark music as on since we don't start |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // reset AI state. |
| 100 | MusicAI.cur_song = OMS_THEME_TYPE_NONE; |
| 101 | //@@ MusicAI.mood_default = MUSIC_MOOD_DEFAULT; |
| 102 | //@@ MusicAI.mood = MUSIC_MOOD_DEFAULT; |
| 103 | MusicAI.flags = 0; |
| 104 | MusicAI.n_hostiles = 0; |
| 105 | MusicAI.was_toggled_on = false; |
| 106 | MusicAI.pending_region = -1; |
| 107 | MusicAI.restart_sequencer = false; |
| 108 | MusicAI.immediate_switch = false; |
| 109 | D3MusicSetRegion(0); |
| 110 | |
| 111 | // reset AI timers |
| 112 | MusicAI.peace_timer = 0.0f; |
| 113 | } |
| 114 | |
| 115 | // stops the music sequencer |
| 116 | void D3MusicStop() { |
| 117 | Music_seq.Stop(); |
| 118 | Music_seq.Shutdown(); |
| 119 | Music_on = false; |
| 120 | } |
| 121 | |
| 122 | // execute music sequencer. |
| 123 | void D3MusicDoFrame(tMusicSeqInfo *music_info) { |
| 124 | // update music ai with new music info |
| 125 | if (!Music_on) |
| 126 | return; |
| 127 | |
| 128 | D3MusicAI(music_info); |
| 129 | |
| 130 | // handle current events. |
| 131 | D3MusicSongSelector(); |
| 132 | |
| 133 | // run frame. |
| 134 | Music_seq.Frame(music_info->frametime); |
| 135 | |
| 136 | // end |
| 137 | music_info->cur_loop_name = Music_seq.GetCurrentLoopName(&music_info->cur_loop_count); |
| 138 | music_info->cur_song = MusicAI.cur_song; |
| 139 | } |
| 140 | |
| 141 | // toggle music system. |
| 142 | void D3MusicToggle() { |
| 143 | Music_on = (!Music_on && Allow_music && Music_volume > 0.0f); |
| 144 | if (Music_on) { |
| 145 | Music_seq.Start(); |
| 146 | MusicAI.was_toggled_on = true; |
| 147 | } else { |
| 148 | Music_seq.Stop(); |
| 149 | MusicAI.was_toggled_on = false; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // toggle music system. |
| 154 | void D3MusicToggle(bool state) { |
| 155 | if (state && !Music_on && Allow_music && Music_volume > 0.0f) { |
| 156 | Music_seq.Start(); |
| 157 | MusicAI.was_toggled_on = true; |
| 158 | Music_on = true; |
| 159 | } else if (!state && Music_on) { |
| 160 | Music_seq.Stop(); |
| 161 | MusicAI.was_toggled_on = false; |
| 162 | Music_on = false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // pauses and or resumes music |
| 167 | void D3MusicPause() { Music_seq.Pause(); } |
| 168 | |
| 169 | void D3MusicResume() { Music_seq.Resume(); } |
| 170 | |
| 171 | // volume stuff |
| 172 | float D3MusicGetVolume() { return Music_volume; } |
| 173 | |
| 174 | void D3MusicSetVolume(float vol) { |
| 175 | const float kEpsilon = std::numeric_limits<float>::min(); |
| 176 | if (vol <= kEpsilon) { |
| 177 | Music_seq.Stop(); |
| 178 | MusicAI.was_toggled_on = false; |
| 179 | Music_on = false; |
| 180 | } else if (Music_volume <= kEpsilon) { |
| 181 | Music_on = true; |
| 182 | MusicAI.was_toggled_on = true; |
| 183 | if (MusicAI.pending_region == -1) { |
| 184 | MusicAI.pending_region = D3MusicGetRegion(); |
| 185 | } |
| 186 | Music_seq.Start(); |
| 187 | } |
| 188 | Music_seq.SetVolume(vol); |
| 189 | Music_volume = vol; |
| 190 | } |
| 191 | |
| 192 | // New Interface |
| 193 | // Sequencing within level. |
| 194 | // play intro track. |
| 195 | // play main background music. |
| 196 | // occasionally switch to other ambient music options. |
| 197 | // when hostility towards player is upped, play 'hostile' music. |
| 198 | // when player enters combat (player takes damage from hostile), switch to combat music. |
| 199 | // fade out background music after 2-3 minutes if no change in state. |
| 200 | // |
| 201 | |
| 202 | // creates music ai struction from music info passed in. |
| 203 | void D3MusicAI(tMusicSeqInfo *music_info) { |
| 204 | // determine flags state. |
| 205 | if (music_info->player_dead) { |
| 206 | MusicAI.flags |= MUSICAIF_PLAYERDEAD; |
| 207 | } else { |
| 208 | MusicAI.flags &= (~MUSICAIF_PLAYERDEAD); |
| 209 | } |
| 210 | |
| 211 | if (music_info->started_level) { |
| 212 | MusicAI.flags |= MUSICAIF_STARTLEVEL; |
| 213 | MusicAI.peace_timer = 0.0f; |
| 214 | } else { |
| 215 | MusicAI.flags &= (~MUSICAIF_STARTLEVEL); |
| 216 | } |
| 217 | |
| 218 | if (music_info->n_hostiles && !(MusicAI.flags & MUSICAIF_HOSTILES)) { |
| 219 | MusicAI.flags |= MUSICAIF_HOSTILES; |
| 220 | MusicAI.peace_timer = 0.0f; |
| 221 | } else if (!music_info->n_hostiles && (MusicAI.flags & MUSICAIF_HOSTILES)) { |
| 222 | // make sure hostiles flags is cleared once. when no hostiles, but hostiles flag is on. |
| 223 | MusicAI.flags &= (~MUSICAIF_HOSTILES); |
| 224 | MusicAI.peace_timer = 0.0f; |
| 225 | //@@ MusicAI.mood_timer = 0.0f; |
| 226 | } |
| 227 | |
| 228 | //@@// set mood timers |
| 229 | //@@ if (!CHECK_FLAG(MusicAI.flags, MUSICAIF_NEGMOODTIMER) && MusicAI.mood < 0) { |
| 230 | //@@ MusicAI.flags |= MUSICAIF_NEGMOODTIMER; |
| 231 | //@@ MusicAI.negmood_timer = 0.0f; |
| 232 | //@@ } |
| 233 | //@@ if (CHECK_FLAG(MusicAI.flags, MUSICAIF_NEGMOODTIMER) && MusicAI.mood >=0) { |
| 234 | //@@ MusicAI.flags &= (~MUSICAIF_NEGMOODTIMER); |
| 235 | //@@ MusicAI.negmood_timer = 0.0f; |
| 236 | //@@ } |
| 237 | //@@ if (!CHECK_FLAG(MusicAI.flags, MUSICAIF_POSMOODTIMER) && MusicAI.mood > 0) { |
| 238 | //@@ MusicAI.flags |= MUSICAIF_POSMOODTIMER; |
| 239 | //@@ MusicAI.posmood_timer = 0.0f; |
| 240 | //@@ } |
| 241 | //@@ if (CHECK_FLAG(MusicAI.flags, MUSICAIF_POSMOODTIMER) && MusicAI.mood <=0) { |
| 242 | //@@ MusicAI.flags &= (~MUSICAIF_POSMOODTIMER); |
| 243 | //@@ MusicAI.posmood_timer = 0.0f; |
| 244 | //@@ } |
| 245 | |
| 246 | //@@// determine mood of music |
| 247 | //@@ float shield_scalar = (float)music_info->player_shield_level/MUSIC_PLR_SHIELD_LVLS; |
| 248 | //@@ if (shield_scalar > 1.0f) shield_scalar = 1.0f; |
| 249 | |
| 250 | //@@// being damaged saddens mood |
| 251 | //@@ if (music_info->player_damaged) { |
| 252 | //@@ MusicAI.mood -= ((1.0f-shield_scalar)*MUSIC_MOOD_DAMAGE_MOD); |
| 253 | //@@ } |
| 254 | |
| 255 | //@@// killing someone HOSTILE raises one's mood. |
| 256 | //@@ MusicAI.mood += (music_info->n_hostiles_player_killed*MUSIC_MOOD_KILLS_MOD); |
| 257 | |
| 258 | //@@// a reduction in hostiles will also modify mood positively, and more hostiles has the opposite effect. |
| 259 | //@@ if ((music_info->n_hostiles-MusicAI.n_hostiles)) { |
| 260 | //@@ MusicAI.mood -= |
| 261 | //((1.5f-shield_scalar)*((music_info->n_hostiles-MusicAI.n_hostiles)*MUSIC_MOOD_HOSTILES_MOD)); |
| 262 | //@@ } |
| 263 | |
| 264 | //@@// if no hostiles and mood != mood_default, then modify mood until it equals 0 again. do it every second. |
| 265 | //@@ if (MusicAI.mood != MusicAI.mood_default) { |
| 266 | //@@ int16_t mod = (MusicAI.flags & MUSICAIF_HOSTILES) ? 1 : 2; |
| 267 | //@@ float time_mod = (MusicAI.flags & MUSICAIF_HOSTILES) ? 0.2f : 0.2f; |
| 268 | //@@ if (MusicAI.mood_timer >= time_mod) { |
| 269 | //@@ if (MusicAI.mood > MusicAI.mood_default) { |
| 270 | //@@ MusicAI.mood -= mod; |
| 271 | //@@ if (MusicAI.mood < MusicAI.mood_default) MusicAI.mood = MusicAI.mood_default; |
| 272 | //@@ } |
| 273 | //@@ if (MusicAI.mood < MusicAI.mood_default) { |
| 274 | //@@ MusicAI.mood += mod; |
| 275 | //@@ if (MusicAI.mood > MusicAI.mood_default) MusicAI.mood = MusicAI.mood_default; |
| 276 | //@@ } |
| 277 | //@@ MusicAI.mood_timer = 0.0f; |
| 278 | //@@ } |
| 279 | //@@ MusicAI.mood_timer += music_info->frametime; |
| 280 | //@@ } |
| 281 | //@@ |
| 282 | //@@ if (CHECK_FLAG(MusicAI.flags, MUSICAIF_NEGMOODTIMER)) { |
| 283 | //@@ MusicAI.negmood_timer += music_info->frametime; |
| 284 | //@@ } |
| 285 | //@@ if (CHECK_FLAG(MusicAI.flags, MUSICAIF_POSMOODTIMER)) { |
| 286 | //@@ MusicAI.posmood_timer += music_info->frametime; |
| 287 | //@@ } |
| 288 | //@@ |
| 289 | // other stuff |
| 290 | MusicAI.n_hostiles = music_info->n_hostiles; |
| 291 | |
| 292 | if (!MusicAI.n_hostiles) { // how long have we been without hostiles |
| 293 | MusicAI.peace_timer += music_info->frametime; |
| 294 | } |
| 295 | |
| 296 | //@@ if (MusicAI.flags & MUSICAIF_PLAYERDEAD) { // if dead, mood is always set to default. |
| 297 | //@@ MusicAI.mood = MusicAI.mood_default; |
| 298 | //@@ } |
| 299 | |
| 300 | // output |
| 301 | //@@ Music_seq.SetRegister(MUSICREG_MOOD_VALUE, MusicAI.mood); |
| 302 | //@@ Music_seq.SetRegister(MUSICREG_POSMOOD_TIMER, MusicAI.posmood_timer); |
| 303 | //@@ Music_seq.SetRegister(MUSICREG_NEGMOOD_TIMER, MusicAI.negmood_timer); |
| 304 | Music_seq.SetRegister(MUSICREG_PEACE_TIMER, MusicAI.peace_timer); |
| 305 | Music_seq.SetRegister(MUSICREG_TRIGGER_VALUE, MusicAI.trigger); |
| 306 | |
| 307 | music_info->peace_timer = MusicAI.peace_timer; |
| 308 | //@@ music_info->mood = MusicAI.mood; |
| 309 | //@@ music_info->nmood_timer = MusicAI.negmood_timer; |
| 310 | //@@ music_info->pmood_timer = MusicAI.posmood_timer; |
| 311 | |
| 312 | if (MusicAI.trigger > 0) { |
| 313 | MusicAI.trigger = MUSICTRIGGER_NONE; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // handles operations on current events. |
| 318 | void D3MusicSongSelector() { |
| 319 | oms_q_evt evt; |
| 320 | |
| 321 | if ((MusicAI.cur_song != OMS_THEME_TYPE_DEATH) && (MusicAI.flags & MUSICAIF_PLAYERDEAD)) { |
| 322 | MusicAI.cur_song = OMS_THEME_TYPE_DEATH; |
| 323 | Music_seq.StartSong(MusicAI.cur_song, false); |
| 324 | } |
| 325 | |
| 326 | if (MusicAI.was_toggled_on) { |
| 327 | if (MusicAI.pending_region > -1) { |
| 328 | Music_seq.SetCurrentRegion(MusicAI.pending_region); |
| 329 | MusicAI.pending_region = -1; |
| 330 | } |
| 331 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 332 | MusicAI.peace_timer = 0.0f; |
| 333 | Music_seq.StartSong(MusicAI.cur_song, false); |
| 334 | MusicAI.was_toggled_on = false; |
| 335 | } |
| 336 | |
| 337 | // intro music will always play at start of level. |
| 338 | if (MusicAI.flags & MUSICAIF_STARTLEVEL) { |
| 339 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 340 | Music_seq.SetCurrentRegion(0); |
| 341 | Music_seq.StartSong(MusicAI.cur_song, false); |
| 342 | } |
| 343 | if (MusicAI.pending_region > -1 && MusicAI.immediate_switch) { |
| 344 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 345 | Music_seq.SetCurrentRegion(MusicAI.pending_region); |
| 346 | Music_seq.StartSong(MusicAI.cur_song, false); |
| 347 | MusicAI.pending_region = -1; |
| 348 | MusicAI.immediate_switch = false; |
| 349 | } |
| 350 | |
| 351 | // process events and songs. |
| 352 | do { |
| 353 | if (!Music_seq.m_output_q.recv(&evt)) { |
| 354 | evt.cmd = OMS_EVT_NONE; |
| 355 | } |
| 356 | |
| 357 | switch (MusicAI.cur_song) { |
| 358 | case OMS_THEME_TYPE_NONE: |
| 359 | if (MusicAI.pending_region > -1) { |
| 360 | mprintf(0, "D3MUSIC: new region request processed.\n"); |
| 361 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 362 | Music_seq.SetCurrentRegion(MusicAI.pending_region); |
| 363 | Music_seq.StartSong(MusicAI.cur_song, true); |
| 364 | MusicAI.pending_region = -1; |
| 365 | } |
| 366 | break; |
| 367 | |
| 368 | case OMS_THEME_TYPE_IDLE: // IDLE MUSIC CURRENTLY PLAYING |
| 369 | if (evt.cmd == OMS_EVT_SONGENDED) { |
| 370 | MusicAI.cur_song = OMS_THEME_TYPE_NONE; |
| 371 | // if (Music_seq.GetCurrentRegion() == 0) { |
| 372 | // mprintf(0, "Ending Region 0.\n"); |
| 373 | // MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 374 | // Music_seq.SetCurrentRegion(1); |
| 375 | // Music_seq.StartSong(MusicAI.cur_song, true); |
| 376 | // } |
| 377 | // else { |
| 378 | // mprintf(0, "D3MUSIC: Song ended normally?\n"); |
| 379 | // } |
| 380 | } else if (evt.cmd == OMS_EVT_LOOPENDING) { |
| 381 | // shall we switch regions? |
| 382 | if (MusicAI.pending_region > -1) { |
| 383 | mprintf(0, "D3MUSIC: new region request processed.\n"); |
| 384 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 385 | Music_seq.SetCurrentRegion(MusicAI.pending_region); |
| 386 | Music_seq.StartSong(MusicAI.cur_song, true); |
| 387 | MusicAI.pending_region = -1; |
| 388 | } |
| 389 | } |
| 390 | break; |
| 391 | |
| 392 | case OMS_THEME_TYPE_DEATH: |
| 393 | if (!(MusicAI.flags & MUSICAIF_PLAYERDEAD)) { |
| 394 | MusicAI.cur_song = OMS_THEME_TYPE_IDLE; |
| 395 | MusicAI.peace_timer = 0.0f; |
| 396 | Music_seq.StartSong(MusicAI.cur_song, false); |
| 397 | } |
| 398 | break; |
| 399 | |
| 400 | default: |
| 401 | Int3(); |
| 402 | } |
| 403 | } while (evt.cmd != OMS_EVT_NONE); |
| 404 | } |
| 405 | |
| 406 | // set music region |
| 407 | void D3MusicSetRegion(int16_t region, bool immediate) { |
| 408 | if (Music_seq.GetCurrentRegion() != region) { |
| 409 | MusicAI.immediate_switch = immediate; |
| 410 | MusicAI.pending_region = region; |
| 411 | //@@ if (MusicAI.immediate_switch) { |
| 412 | //@@ // at next frame, start again! |
| 413 | //@@ Music_seq.Pause(); |
| 414 | //@@ MusicAI.restart_sequencer = true; |
| 415 | //@@ } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // retreives current region |
| 420 | int16_t D3MusicGetRegion() { return Music_seq.GetPlayingRegion(); } |
| 421 | |
| 422 | // retreives current PLAYING region. |
| 423 | int16_t D3MusicGetPendingRegion() { return MusicAI.pending_region; } |
| 424 | |
| 425 | // starts special in-game cinematic music |
| 426 | void D3MusicStartCinematic() {} |
| 427 | |
| 428 | // stops special in-game cinematic music |
| 429 | void D3MusicStopCinematic() {} |