Debian Codegraph @

music/omflex.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/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"cxx . . $ `<file>/music/music.h`/
75 #include "musiclib.h"cxx . . $ `<file>/music/musiclib.h`/
76 #include "inffile.h"cxx . . $ `<file>/cfile/inffile.h`/
77 #include "mem.h"cxx . . $ `<file>/mem/mem.h`/
78
79 #include <string.h>
80 #include <stdlib.h>
81
82 // OMF INF FILE READ
83 #define OMFFILEERR_ADDSECTIONcxx . . $ `music/omflex.cpp:83:9`! INFFILE_CUSTOM // error adding section to list.
84 #define OMFFILEERR_INSOVERFLOWcxx . . $ `music/omflex.cpp:84:9`! (INFFILE_CUSTOM + 1) // too many instructions!
85 #define OMFFILEERR_LBLOVERFLOWcxx . . $ `music/omflex.cpp:85:9`! (INFFILE_CUSTOM + 2) // too many labels!
86 #define OMFFILEERR_GOTOcxx . . $ `music/omflex.cpp:86:9`! (INFFILE_CUSTOM + 3) // no goto label exists
87 #define OMFFILEERR_SYNTAXcxx . . $ `music/omflex.cpp:87:9`! (INFFILE_CUSTOM + 4) // syntax error.
88
89 static const char *OMFCommandscxx . . $ OMFCommands.[OMFCMD_NUMcxx . . $ `music/musiclib.h:20:9`!] = {
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 OMFLexcxx . . $ OMFLex(5c4306d8553b3696).(const char *command);
93
94 int OMFLexcxx . . $ OMFLex(5c4306d8553b3696).(const char *command) {
95 for (int i = 0; i < OMFCMD_NUMcxx . . $ `music/musiclib.h:20:9`!; i++) {
96 if (strcmp(OMFCommandscxx . . $ OMFCommands.[i], command) == 0)
97 return i;
98 }
99
100 return INFFILE_ERRORcxx . . $ INFFILE_ERROR.;
101 }
102
103 #define ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(_cmd, _num) \
104 do { \
105 if (temp_ins_idx == MAX_MUSIC_INSTRUCTIONScxx . . $ `music/music.h:252:9`!) { \
106 cmd = OMFFILEERR_INSOVERFLOWcxx . . $ `music/omflex.cpp:84:9`!; \
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_STRcxx . . $ `music/omflex.cpp:113:9`!(_cmd, _str) \
114 do { \
115 if (temp_ins_idx == MAX_MUSIC_INSTRUCTIONScxx . . $ `music/music.h:252:9`!) { \
116 cmd = OMFFILEERR_INSOVERFLOWcxx . . $ `music/omflex.cpp:84:9`!; \
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 OutrageMusicSeqcxx . . $ OutrageMusicSeq#::LoadThemecxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).(const char *file) {
125 const int MAX_FILE_LABELS = 256;
126
127 InfFilecxx . . $ InfFile# infcxx . . $ InfFile#InfFile(49f6e7a06ebc5aa8).;
128 char operand[INFFILE_LINELENcxx . . $ INFFILE_LINELEN.]; // operand
129 int theme_type;
130 music_inscxx . . $ OutrageMusicSeq#music_ins# temp_ins_buf[MAX_MUSIC_INSTRUCTIONScxx . . $ `music/music.h:252:9`!];
131 int temp_ins_idx = 0;
132 int16_t cur_region;
133 structcxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0# {
134 char *namecxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#name.;
135 int16_t indexcxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#index.;
136 int16_t regioncxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#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_tracklistcxx . . $ OutrageMusicSeq#m_tracklist..resetcxx . . $ oms_tracklist#reset(49f6e7a06ebc5aa8).();
143 m_ins_curptrcxx . . $ OutrageMusicSeq#m_ins_curptr. = m_ins_buffercxx . . $ OutrageMusicSeq#m_ins_buffer.;
144 m_str_curptrcxx . . $ OutrageMusicSeq#m_str_curptr. = m_str_buffercxx . . $ OutrageMusicSeq#m_str_buffer.;
145
146 // open file
147 if (!file)
148 return false;
149
150 if (!inf.Opencxx . . $ InfFile#Open(a6c18f6c547e5631).(file, "[theme file]", OMFLexcxx . . $ OMFLex(5c4306d8553b3696).)) {
151 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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.ReadLinecxx . . $ InfFile#ReadLine(c9857a73d68e59ba).()) {
159 int cmd;
160 bool ifi_block;
161 ifi_block = false;
162 while ((cmd = inf.ParseLinecxx . . $ InfFile#ParseLine(6abd07ddb1a912f0).(operand, INFFILE_LINELENcxx . . $ INFFILE_LINELEN.)) > INFFILE_ERRORcxx . . $ INFFILE_ERROR.) {
163 switch (cmd) {
164 case OMFCMD_LABELcxx . . $ `music/musiclib.h:26:9`!:
165 if (n_labels == MAX_FILE_LABELS) {
166 cmd = OMFFILEERR_LBLOVERFLOWcxx . . $ `music/omflex.cpp:85:9`!;
167 goto force_pre_error;
168 }
169 labels[n_labels].indexcxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#index. = temp_ins_idx;
170 labels[n_labels].regioncxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#region. = cur_region;
171 labels[n_labels++].namecxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#name. = mem_strdupcxx . . $ `mem/mem.h:81:9`!(operand);
172 break;
173 case OMFCMD_SECTIONcxx . . $ `music/musiclib.h:23:9`!:
174 case OMFCMD_REGIONcxx . . $ `music/musiclib.h:34:9`!:
175 temp_ins_idx = 0;
176 break;
177 case OMFCMD_ENDREGIONcxx . . $ `music/musiclib.h:35:9`!:
178 cur_region++;
179 break;
180 case OMFCMD_STREAMcxx . . $ `music/musiclib.h:22:9`!:
181 break;
182 case OMFCMD_BNIFcxx . . $ `music/musiclib.h:41:9`!:
183 case OMFCMD_BLTcxx . . $ `music/musiclib.h:38:9`!:
184 case OMFCMD_BGTcxx . . $ `music/musiclib.h:39:9`!:
185 case OMFCMD_BEQcxx . . $ `music/musiclib.h:40:9`!:
186 case OMFCMD_GOTOcxx . . $ `music/musiclib.h:42:9`!:
187 case OMFCMD_ENDSECTIONcxx . . $ `music/musiclib.h:25:9`!:
188 case OMFCMD_WAITcxx . . $ `music/musiclib.h:43:9`!:
189 case OMFCMD_COMPAREcxx . . $ `music/musiclib.h:27:9`!:
190 case OMFCMD_LLPTcxx . . $ `music/musiclib.h:29:9`!:
191 case OMFCMD_SETIcxx . . $ `music/musiclib.h:32:9`!:
192 case OMFCMD_LCMPcxx . . $ `music/musiclib.h:28:9`!:
193 case OMFCMD_LPLCcxx . . $ `music/musiclib.h:30:9`!:
194 case OMFCMD_INCIcxx . . $ `music/musiclib.h:31:9`!:
195 temp_ins_idx++;
196 break;
197 case OMFCMD_IFIcxx . . $ `music/musiclib.h:33:9`!:
198 temp_ins_idx++;
199 ifi_block = true;
200 break;
201 case OMFCMD_PLAYcxx . . $ `music/musiclib.h:24:9`!:
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_ERRORcxx . . $ INFFILE_ERROR.) {
213 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error in music file %s line %d.\n", file, inf.line());
214 Int3cxx . . $ `misc/pserror.h:247:9`!();
215 } else if (cmd == OMFFILEERR_LBLOVERFLOWcxx . . $ `music/omflex.cpp:85:9`!) {
216 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error in music file %s line %d (too many labels).\n", file, inf.line());
217 Int3cxx . . $ `misc/pserror.h:247:9`!();
218 }
219 }
220
221 inf.Closecxx . . $ InfFile#Close(49f6e7a06ebc5aa8).();
222
223 // reopen for SECOND PASS (actual code read)
224 if (!inf.Opencxx . . $ InfFile#Open(a6c18f6c547e5631).(file, "[theme file]", OMFLexcxx . . $ OMFLex(5c4306d8553b3696).)) {
225 mprintfcxx . . $ `ddebug/mono.h:82:9`!(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.ReadLinecxx . . $ InfFile#ReadLine(c9857a73d68e59ba).()) {
233 int cmd, i;
234 bool ifi_block;
235 ifi_block = false;
236
237 while ((cmd = inf.ParseLinecxx . . $ InfFile#ParseLine(6abd07ddb1a912f0).(operand, INFFILE_LINELENcxx . . $ INFFILE_LINELEN.)) > INFFILE_ERRORcxx . . $ INFFILE_ERROR.) {
238 switch (cmd) {
239 case OMFCMD_REGIONcxx . . $ `music/musiclib.h:34:9`!:
240 if (in_region) {
241 cmd = OMFFILEERR_SYNTAXcxx . . $ `music/omflex.cpp:87:9`!;
242 goto force_error;
243 }
244 temp_ins_idx = 0;
245 in_region = true;
246 break;
247
248 case OMFCMD_ENDREGIONcxx . . $ `music/musiclib.h:35:9`!:
249 in_region = false;
250 cur_region++;
251 break;
252
253 case OMFCMD_STREAMcxx . . $ `music/musiclib.h:22:9`!: {
254 char *sym = strchr(operand, '$'); // symbol AFTER '$'
255 if (!sym) {
256 cmd = INFFILE_ERRORcxx . . $ INFFILE_ERROR.;
257 goto force_error;
258 }
259 sym[0] = 0;
260 m_tracklistcxx . . $ OutrageMusicSeq#m_tracklist..addcxx . . $ oms_tracklist#add(b7a7aa4a824b7595).(operand, &sym[1]); // sym[0] was '$' now null terminator
261 } break;
262
263 case OMFCMD_LABELcxx . . $ `music/musiclib.h:26:9`!:
264 // already taken care of.
265 ASSERTcxx . . $ `misc/pserror.h:246:9`!(labels[n_relabels].index == temp_ins_idx);
266 n_relabels++;
267 break;
268
269 case OMFCMD_BNIFcxx . . $ `music/musiclib.h:41:9`!:
270 case OMFCMD_BLTcxx . . $ `music/musiclib.h:38:9`!:
271 case OMFCMD_BGTcxx . . $ `music/musiclib.h:39:9`!:
272 case OMFCMD_BEQcxx . . $ `music/musiclib.h:40:9`!:
273 case OMFCMD_GOTOcxx . . $ `music/musiclib.h:42:9`!:
274 for (i = 0; i < n_labels; i++) {
275 if (labels[i].regioncxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#region. == cur_region && strcmp(operand, labels[i].namecxx . . $ OutrageMusicSeq#LoadTheme(c5f7da30cbc0f4e9).$anonymous_type_0#name.) == 0) {
276 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(cmd, labels[i].index);
277 break;
278 }
279 }
280 if (i == n_labels) {
281 cmd = OMFFILEERR_GOTOcxx . . $ `music/omflex.cpp:86:9`!;
282 goto force_error;
283 }
284 break;
285
286 case OMFCMD_SECTIONcxx . . $ `music/musiclib.h:23:9`!:
287 if (!in_region) {
288 cmd = OMFFILEERR_SYNTAXcxx . . $ `music/omflex.cpp:87:9`!;
289 goto force_error;
290 }
291 if (strcmp(operand, "intro") == 0) {
292 theme_type = OMS_THEME_TYPE_INTROcxx . . $ `music/music.h:116:9`!;
293 } else if (strcmp(operand, "idle") == 0) {
294 theme_type = OMS_THEME_TYPE_IDLEcxx . . $ `music/music.h:117:9`!;
295 } else if (strcmp(operand, "combat") == 0) {
296 theme_type = OMS_THEME_TYPE_COMBATcxx . . $ `music/music.h:118:9`!;
297 } else if (strcmp(operand, "death") == 0) {
298 theme_type = OMS_THEME_TYPE_DEATHcxx . . $ `music/music.h:119:9`!;
299 } else if (strcmp(operand, "idle_combat") == 0) {
300 theme_type = OMS_THEME_TYPE_IDLE_TO_COMBATcxx . . $ `music/music.h:120:9`!;
301 } else if (strcmp(operand, "combat_idle") == 0) {
302 theme_type = OMS_THEME_TYPE_COMBAT_TO_IDLEcxx . . $ `music/music.h:121:9`!;
303 }
304
305 temp_ins_idx = 0; // reset instruction buffer
306 break;
307
308 case OMFCMD_ENDSECTIONcxx . . $ `music/musiclib.h:25:9`!:
309 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_ENDSECTIONcxx . . $ `music/musiclib.h:25:9`!, 0);
310 if (!AddToListcxx . . $ OutrageMusicSeq#AddToList(f85d6ba651fd2932).((int16_t)cur_region, (int16_t)theme_type, temp_ins_idx, temp_ins_buf)) {
311 cmd = OMFFILEERR_ADDSECTIONcxx . . $ `music/omflex.cpp:83:9`!;
312 goto force_error;
313 }
314 theme_type = -1;
315 break;
316
317 case OMFCMD_PLAYcxx . . $ `music/musiclib.h:24:9`!:
318 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_PLAYcxx . . $ `music/musiclib.h:24:9`!, 0);
319 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_MPLAYcxx . . $ `music/musiclib.h:46:9`!, 0);
320 break;
321
322 case OMFCMD_WAITcxx . . $ `music/musiclib.h:43:9`!:
323 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_WAITcxx . . $ `music/musiclib.h:43:9`!, strcmp(operand, "finished") ? 0 : 1);
324 break;
325
326 case OMFCMD_COMPAREcxx . . $ `music/musiclib.h:27:9`!: {
327 tMusicValcxx . . $ tMusicVal# val;
328 sscanf(operand, "r%hd", &val);
329 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_COMPAREcxx . . $ `music/musiclib.h:27:9`!, val);
330 break;
331 }
332
333 case OMFCMD_LLPTcxx . . $ `music/musiclib.h:29:9`!:
334 ADD_NEW_INS_STRcxx . . $ `music/omflex.cpp:113:9`!(OMFCMD_LLPTcxx . . $ `music/musiclib.h:29:9`!, operand);
335 break;
336
337 case OMFCMD_SETIcxx . . $ `music/musiclib.h:32:9`!:
338 case OMFCMD_LCMPcxx . . $ `music/musiclib.h:28:9`!:
339 case OMFCMD_LPLCcxx . . $ `music/musiclib.h:30:9`!:
340 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(cmd, (tMusicVal)atoi(operand));
341 break;
342
343 case OMFCMD_INCIcxx . . $ `music/musiclib.h:31:9`!:
344 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(cmd, 0);
345 break;
346
347 case OMFCMD_IFIcxx . . $ `music/musiclib.h:33:9`!:
348 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(cmd, (tMusicVal)atoi(operand));
349 ifi_block = true;
350 break;
351 }
352 }
353
354 if (ifi_block) {
355 ADD_NEW_INS_NUMcxx . . $ `music/omflex.cpp:103:9`!(OMFCMD_ENDIFIcxx . . $ `music/musiclib.h:47:9`!, 0);
356 }
357
358 force_error:
359 if (cmd == INFFILE_ERRORcxx . . $ INFFILE_ERROR.) {
360 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error in music file %s line %d.\n", file, inf.line());
361 Int3cxx . . $ `misc/pserror.h:247:9`!();
362 } else if (cmd == OMFFILEERR_ADDSECTIONcxx . . $ `music/omflex.cpp:83:9`!) {
363 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error in music file %s line %d (failed to add section).\n", file, inf.line());
364 Int3cxx . . $ `misc/pserror.h:247:9`!();
365 } else if (cmd == OMFFILEERR_INSOVERFLOWcxx . . $ `music/omflex.cpp:84:9`!) {
366 mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error in music file %s line %d (too many instructions).\n", file, inf.line());
367 Int3cxx . . $ `misc/pserror.h:247:9`!();
368 }
369 }
370
371 // free any memory
372 while (n_labels) {
373 mem_freecxx . . $ `mem/mem.h:80:9`!(labels[n_labels - 1].name);
374 n_labels--;
375 }
376
377 // close file
378 inf.Closecxx . . $ InfFile#Close(49f6e7a06ebc5aa8).();
379
380 return true;
381 }