| 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/music/omflex.cpp $ |
| 21 | * $Revision: 14 $ |
| 22 | * $Date: 4/01/99 4:50p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Read OMF file lexical analyzer. |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/music/omflex.cpp $ |
| 28 | * |
| 29 | * 14 4/01/99 4:50p Matt |
| 30 | * Took out Warning() function, chaning those calls to mprintf() |
| 31 | * |
| 32 | * 13 2/27/99 6:52p Samir |
| 33 | * fixed label-region scope problem. |
| 34 | * |
| 35 | * 12 1/28/99 2:22p Samir |
| 36 | * simplified music system for D3. |
| 37 | * |
| 38 | * 11 12/15/98 10:22a Samir |
| 39 | * upped max labels. |
| 40 | * |
| 41 | * 10 12/11/98 3:28p Samir |
| 42 | * added start of regions |
| 43 | * |
| 44 | * 9 12/08/98 11:41a Samir |
| 45 | * added new region commands. |
| 46 | * |
| 47 | * 8 12/07/98 2:57p Samir |
| 48 | * added transitions for combat. |
| 49 | * |
| 50 | * 7 12/03/98 12:48p Samir |
| 51 | * speed up sequencer so that gap between samples isn't too big. |
| 52 | * |
| 53 | * 6 11/20/98 5:20p Samir |
| 54 | * added a bunch of high level scripting commands. |
| 55 | * |
| 56 | * 5 11/16/98 4:15p Samir |
| 57 | * added death. |
| 58 | * |
| 59 | * 4 11/13/98 2:27p Samir |
| 60 | * new music system. |
| 61 | * |
| 62 | * 3 8/10/98 5:56p Samir |
| 63 | * new command for end of combat music added. |
| 64 | * |
| 65 | * 2 7/28/98 5:43p Samir |
| 66 | * reorg of music system. |
| 67 | * |
| 68 | * 1 7/28/98 11:48a Samir |
| 69 | * moved from sequencer.cpp |
| 70 | * |
| 71 | * $NoKeywords: $ |
| 72 | */ |
| 73 | |
| 74 | #include "music.h" |
| 75 | #include "musiclib.h" |
| 76 | #include "inffile.h" |
| 77 | #include "mem.h" |
| 78 | |
| 79 | #include <string.h> |
| 80 | #include <stdlib.h> |
| 81 | |
| 82 | // OMF INF FILE READ |
| 83 | #define OMFFILEERR_ADDSECTION INFFILE_CUSTOM // error adding section to list. |
| 84 | #define OMFFILEERR_INSOVERFLOW (INFFILE_CUSTOM + 1) // too many instructions! |
| 85 | #define OMFFILEERR_LBLOVERFLOW (INFFILE_CUSTOM + 2) // too many labels! |
| 86 | #define OMFFILEERR_GOTO (INFFILE_CUSTOM + 3) // no goto label exists |
| 87 | #define OMFFILEERR_SYNTAX (INFFILE_CUSTOM + 4) // syntax error. |
| 88 | |
| 89 | static const char *OMFCommands[OMFCMD_NUM] = { |
| 90 | "stream", "section", "play", "endsection", "label", "compare", "lcmp", "llpt", "lplc", "inci", "seti", |
| 91 | "ifi", "region", "endregion", "q2", "blt", "bgt", "beq", "bnif", "goto", "wait"}; |
| 92 | static int OMFLex(const char *command); |
| 93 | |
| 94 | int OMFLex(const char *command) { |
| 95 | for (int i = 0; i < OMFCMD_NUM; i++) { |
| 96 | if (strcmp(OMFCommands[i], command) == 0) |
| 97 | return i; |
| 98 | } |
| 99 | |
| 100 | return INFFILE_ERROR; |
| 101 | } |
| 102 | |
| 103 | #define ADD_NEW_INS_NUM(_cmd, _num) \ |
| 104 | do { \ |
| 105 | if (temp_ins_idx == MAX_MUSIC_INSTRUCTIONS) { \ |
| 106 | cmd = OMFFILEERR_INSOVERFLOW; \ |
| 107 | goto force_error; \ |
| 108 | } \ |
| 109 | temp_ins_buf[temp_ins_idx].cmd = (_cmd); \ |
| 110 | temp_ins_buf[temp_ins_idx++].opr.num = (_num); \ |
| 111 | } while (0) |
| 112 | |
| 113 | #define ADD_NEW_INS_STR(_cmd, _str) \ |
| 114 | do { \ |
| 115 | if (temp_ins_idx == MAX_MUSIC_INSTRUCTIONS) { \ |
| 116 | cmd = OMFFILEERR_INSOVERFLOW; \ |
| 117 | goto force_error; \ |
| 118 | } \ |
| 119 | temp_ins_buf[temp_ins_idx].cmd = (_cmd); \ |
| 120 | temp_ins_buf[temp_ins_idx++].opr.str = alloc_and_copy_str((_str)); \ |
| 121 | } while (0) |
| 122 | |
| 123 | // takes a filename containing oms data. |
| 124 | bool OutrageMusicSeq::LoadTheme(const char *file) { |
| 125 | const int MAX_FILE_LABELS = 256; |
| 126 | |
| 127 | InfFile inf; |
| 128 | char operand[INFFILE_LINELEN]; // operand |
| 129 | int theme_type; |
| 130 | music_ins temp_ins_buf[MAX_MUSIC_INSTRUCTIONS]; |
| 131 | int temp_ins_idx = 0; |
| 132 | int16_t cur_region; |
| 133 | struct { |
| 134 | char *name; |
| 135 | int16_t index; |
| 136 | int16_t region; |
| 137 | } labels[MAX_FILE_LABELS]; |
| 138 | int n_labels = 0, n_relabels = 0; |
| 139 | bool in_region = false; |
| 140 | |
| 141 | // reset list management for new theme. |
| 142 | m_tracklist.reset(); |
| 143 | m_ins_curptr = m_ins_buffer; |
| 144 | m_str_curptr = m_str_buffer; |
| 145 | |
| 146 | // open file |
| 147 | if (!file) |
| 148 | return false; |
| 149 | |
| 150 | if (!inf.Open(file, "[theme file]", OMFLex)) { |
| 151 | mprintf(0, "Unable to find requested theme %s or bad file.\n", file); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | cur_region = 0; |
| 156 | |
| 157 | // perform FIRST PASS for LABELS |
| 158 | while (inf.ReadLine()) { |
| 159 | int cmd; |
| 160 | bool ifi_block; |
| 161 | ifi_block = false; |
| 162 | while ((cmd = inf.ParseLine(operand, INFFILE_LINELEN)) > INFFILE_ERROR) { |
| 163 | switch (cmd) { |
| 164 | case OMFCMD_LABEL: |
| 165 | if (n_labels == MAX_FILE_LABELS) { |
| 166 | cmd = OMFFILEERR_LBLOVERFLOW; |
| 167 | goto force_pre_error; |
| 168 | } |
| 169 | labels[n_labels].index = temp_ins_idx; |
| 170 | labels[n_labels].region = cur_region; |
| 171 | labels[n_labels++].name = mem_strdup(operand); |
| 172 | break; |
| 173 | case OMFCMD_SECTION: |
| 174 | case OMFCMD_REGION: |
| 175 | temp_ins_idx = 0; |
| 176 | break; |
| 177 | case OMFCMD_ENDREGION: |
| 178 | cur_region++; |
| 179 | break; |
| 180 | case OMFCMD_STREAM: |
| 181 | break; |
| 182 | case OMFCMD_BNIF: |
| 183 | case OMFCMD_BLT: |
| 184 | case OMFCMD_BGT: |
| 185 | case OMFCMD_BEQ: |
| 186 | case OMFCMD_GOTO: |
| 187 | case OMFCMD_ENDSECTION: |
| 188 | case OMFCMD_WAIT: |
| 189 | case OMFCMD_COMPARE: |
| 190 | case OMFCMD_LLPT: |
| 191 | case OMFCMD_SETI: |
| 192 | case OMFCMD_LCMP: |
| 193 | case OMFCMD_LPLC: |
| 194 | case OMFCMD_INCI: |
| 195 | temp_ins_idx++; |
| 196 | break; |
| 197 | case OMFCMD_IFI: |
| 198 | temp_ins_idx++; |
| 199 | ifi_block = true; |
| 200 | break; |
| 201 | case OMFCMD_PLAY: |
| 202 | temp_ins_idx++; |
| 203 | temp_ins_idx++; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (ifi_block) { |
| 209 | temp_ins_idx++; |
| 210 | } |
| 211 | force_pre_error: |
| 212 | if (cmd == INFFILE_ERROR) { |
| 213 | mprintf(0, "Error in music file %s line %d.\n", file, inf.line()); |
| 214 | Int3(); |
| 215 | } else if (cmd == OMFFILEERR_LBLOVERFLOW) { |
| 216 | mprintf(0, "Error in music file %s line %d (too many labels).\n", file, inf.line()); |
| 217 | Int3(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | inf.Close(); |
| 222 | |
| 223 | // reopen for SECOND PASS (actual code read) |
| 224 | if (!inf.Open(file, "[theme file]", OMFLex)) { |
| 225 | mprintf(0, "Unable to find requested theme %s or bad file.\n", file); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | theme_type = -1; |
| 230 | cur_region = 0; |
| 231 | |
| 232 | while (inf.ReadLine()) { |
| 233 | int cmd, i; |
| 234 | bool ifi_block; |
| 235 | ifi_block = false; |
| 236 | |
| 237 | while ((cmd = inf.ParseLine(operand, INFFILE_LINELEN)) > INFFILE_ERROR) { |
| 238 | switch (cmd) { |
| 239 | case OMFCMD_REGION: |
| 240 | if (in_region) { |
| 241 | cmd = OMFFILEERR_SYNTAX; |
| 242 | goto force_error; |
| 243 | } |
| 244 | temp_ins_idx = 0; |
| 245 | in_region = true; |
| 246 | break; |
| 247 | |
| 248 | case OMFCMD_ENDREGION: |
| 249 | in_region = false; |
| 250 | cur_region++; |
| 251 | break; |
| 252 | |
| 253 | case OMFCMD_STREAM: { |
| 254 | char *sym = strchr(operand, '$'); // symbol AFTER '$' |
| 255 | if (!sym) { |
| 256 | cmd = INFFILE_ERROR; |
| 257 | goto force_error; |
| 258 | } |
| 259 | sym[0] = 0; |
| 260 | m_tracklist.add(operand, &sym[1]); // sym[0] was '$' now null terminator |
| 261 | } break; |
| 262 | |
| 263 | case OMFCMD_LABEL: |
| 264 | // already taken care of. |
| 265 | ASSERT(labels[n_relabels].index == temp_ins_idx); |
| 266 | n_relabels++; |
| 267 | break; |
| 268 | |
| 269 | case OMFCMD_BNIF: |
| 270 | case OMFCMD_BLT: |
| 271 | case OMFCMD_BGT: |
| 272 | case OMFCMD_BEQ: |
| 273 | case OMFCMD_GOTO: |
| 274 | for (i = 0; i < n_labels; i++) { |
| 275 | if (labels[i].region == cur_region && strcmp(operand, labels[i].name) == 0) { |
| 276 | ADD_NEW_INS_NUM(cmd, labels[i].index); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | if (i == n_labels) { |
| 281 | cmd = OMFFILEERR_GOTO; |
| 282 | goto force_error; |
| 283 | } |
| 284 | break; |
| 285 | |
| 286 | case OMFCMD_SECTION: |
| 287 | if (!in_region) { |
| 288 | cmd = OMFFILEERR_SYNTAX; |
| 289 | goto force_error; |
| 290 | } |
| 291 | if (strcmp(operand, "intro") == 0) { |
| 292 | theme_type = OMS_THEME_TYPE_INTRO; |
| 293 | } else if (strcmp(operand, "idle") == 0) { |
| 294 | theme_type = OMS_THEME_TYPE_IDLE; |
| 295 | } else if (strcmp(operand, "combat") == 0) { |
| 296 | theme_type = OMS_THEME_TYPE_COMBAT; |
| 297 | } else if (strcmp(operand, "death") == 0) { |
| 298 | theme_type = OMS_THEME_TYPE_DEATH; |
| 299 | } else if (strcmp(operand, "idle_combat") == 0) { |
| 300 | theme_type = OMS_THEME_TYPE_IDLE_TO_COMBAT; |
| 301 | } else if (strcmp(operand, "combat_idle") == 0) { |
| 302 | theme_type = OMS_THEME_TYPE_COMBAT_TO_IDLE; |
| 303 | } |
| 304 | |
| 305 | temp_ins_idx = 0; // reset instruction buffer |
| 306 | break; |
| 307 | |
| 308 | case OMFCMD_ENDSECTION: |
| 309 | ADD_NEW_INS_NUM(OMFCMD_ENDSECTION, 0); |
| 310 | if (!AddToList((int16_t)cur_region, (int16_t)theme_type, temp_ins_idx, temp_ins_buf)) { |
| 311 | cmd = OMFFILEERR_ADDSECTION; |
| 312 | goto force_error; |
| 313 | } |
| 314 | theme_type = -1; |
| 315 | break; |
| 316 | |
| 317 | case OMFCMD_PLAY: |
| 318 | ADD_NEW_INS_NUM(OMFCMD_PLAY, 0); |
| 319 | ADD_NEW_INS_NUM(OMFCMD_MPLAY, 0); |
| 320 | break; |
| 321 | |
| 322 | case OMFCMD_WAIT: |
| 323 | ADD_NEW_INS_NUM(OMFCMD_WAIT, strcmp(operand, "finished") ? 0 : 1); |
| 324 | break; |
| 325 | |
| 326 | case OMFCMD_COMPARE: { |
| 327 | tMusicVal val; |
| 328 | sscanf(operand, "r%hd", &val); |
| 329 | ADD_NEW_INS_NUM(OMFCMD_COMPARE, val); |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | case OMFCMD_LLPT: |
| 334 | ADD_NEW_INS_STR(OMFCMD_LLPT, operand); |
| 335 | break; |
| 336 | |
| 337 | case OMFCMD_SETI: |
| 338 | case OMFCMD_LCMP: |
| 339 | case OMFCMD_LPLC: |
| 340 | ADD_NEW_INS_NUM(cmd, (tMusicVal)atoi(operand)); |
| 341 | break; |
| 342 | |
| 343 | case OMFCMD_INCI: |
| 344 | ADD_NEW_INS_NUM(cmd, 0); |
| 345 | break; |
| 346 | |
| 347 | case OMFCMD_IFI: |
| 348 | ADD_NEW_INS_NUM(cmd, (tMusicVal)atoi(operand)); |
| 349 | ifi_block = true; |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (ifi_block) { |
| 355 | ADD_NEW_INS_NUM(OMFCMD_ENDIFI, 0); |
| 356 | } |
| 357 | |
| 358 | force_error: |
| 359 | if (cmd == INFFILE_ERROR) { |
| 360 | mprintf(0, "Error in music file %s line %d.\n", file, inf.line()); |
| 361 | Int3(); |
| 362 | } else if (cmd == OMFFILEERR_ADDSECTION) { |
| 363 | mprintf(0, "Error in music file %s line %d (failed to add section).\n", file, inf.line()); |
| 364 | Int3(); |
| 365 | } else if (cmd == OMFFILEERR_INSOVERFLOW) { |
| 366 | mprintf(0, "Error in music file %s line %d (too many instructions).\n", file, inf.line()); |
| 367 | Int3(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // free any memory |
| 372 | while (n_labels) { |
| 373 | mem_free(labels[n_labels - 1].name); |
| 374 | n_labels--; |
| 375 | } |
| 376 | |
| 377 | // close file |
| 378 | inf.Close(); |
| 379 | |
| 380 | return true; |
| 381 | } |