Debian Codegraph @

prboom2/src/hexen/sn_sonix.c

raw
1 //
2 // Copyright(C) 1993-1996 Id Software, Inc.
3 // Copyright(C) 1993-2008 Raven Software
4 // Copyright(C) 2005-2014 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16
17 #include <string.h>
18
19 #include "doomdef.h"
20 #include "m_random.h"
21 #include "s_sound.h"
22 #include "sounds.h"
23 #include "p_mobj.h"
24 #include "lprintf.h"
25 #include "sc_man.h"
26
27 #include "dsda/pause.h"
28
29 #include "sn_sonix.h"
30
31 #define SS_MAX_SCRIPTS 64
32 #define SS_TEMPBUFFER_SIZE 1024
33 #define SS_SEQUENCE_NAME_LENGTH 32
34
35 #define SS_SCRIPT_NAME "SNDSEQ"
36 #define SS_STRING_PLAY "play"
37 #define SS_STRING_PLAYUNTILDONE "playuntildone"
38 #define SS_STRING_PLAYTIME "playtime"
39 #define SS_STRING_PLAYREPEAT "playrepeat"
40 #define SS_STRING_DELAY "delay"
41 #define SS_STRING_DELAYRAND "delayrand"
42 #define SS_STRING_VOLUME "volume"
43 #define SS_STRING_END "end"
44 #define SS_STRING_STOPSOUND "stopsound"
45
46 typedef enum
47 {
48 SS_CMD_NONE,
49 SS_CMD_PLAY,
50 SS_CMD_WAITUNTILDONE, // used by PLAYUNTILDONE
51 SS_CMD_PLAYTIME,
52 SS_CMD_PLAYREPEAT,
53 SS_CMD_DELAY,
54 SS_CMD_DELAYRAND,
55 SS_CMD_VOLUME,
56 SS_CMD_STOPSOUND,
57 SS_CMD_END
58 } sscmds_t;
59
60 static void VerifySequencePtr(int *base, int *ptr);
61 static int GetSoundOffset(char *name);
62
63 static struct
64 {
65 char name[SS_SEQUENCE_NAME_LENGTH];
66 int scriptNum;
67 int stopSound;
68 } SequenceTranslate[SEQ_NUMSEQ] =
69 {
70 {
71 "Platform", 0, 0},
72 {
73 "Platform", 0, 0}, // a 'heavy' platform is just a platform
74 {
75 "PlatformMetal", 0, 0},
76 {
77 "Platform", 0, 0}, // same with a 'creak' platform
78 {
79 "Silence", 0, 0},
80 {
81 "Lava", 0, 0},
82 {
83 "Water", 0, 0},
84 {
85 "Ice", 0, 0},
86 {
87 "Earth", 0, 0},
88 {
89 "PlatformMetal2", 0, 0},
90 {
91 "DoorNormal", 0, 0},
92 {
93 "DoorHeavy", 0, 0},
94 {
95 "DoorMetal", 0, 0},
96 {
97 "DoorCreak", 0, 0},
98 {
99 "Silence", 0, 0},
100 {
101 "Lava", 0, 0},
102 {
103 "Water", 0, 0},
104 {
105 "Ice", 0, 0},
106 {
107 "Earth", 0, 0},
108 {
109 "DoorMetal2", 0, 0},
110 {
111 "Wind", 0, 0}
112 };
113
114 static int *SequenceData[SS_MAX_SCRIPTS];
115
116 int ActiveSequences;
117 seqnode_t *SequenceListHead;
118
119 static void VerifySequencePtr(int *base, int *ptr)
120 {
121 if (ptr - base > SS_TEMPBUFFER_SIZE)
122 {
123 I_Error("VerifySequencePtr: tempPtr >= %d\n", SS_TEMPBUFFER_SIZE);
124 }
125 }
126
127 static int GetSoundOffset(char *name)
128 {
129 int i;
130
131 for (i = 0; i < num_sfx; i++)
132 {
133 if (!strcasecmp(name, S_sfx[i].tagname))
134 {
135 return i;
136 }
137 }
138 SC_ScriptError("GetSoundOffset: Unknown sound name\n");
139 return 0;
140 }
141
142 void SN_InitSequenceScript(void)
143 {
144 int i, j;
145 int inSequence;
146 int *tempDataStart = NULL;
147 int *tempDataPtr = NULL;
148
149 inSequence = -1;
150 ActiveSequences = 0;
151 for (i = 0; i < SS_MAX_SCRIPTS; i++)
152 {
153 SequenceData[i] = NULL;
154 }
155 SC_OpenLump(SS_SCRIPT_NAME);
156 while (SC_GetString())
157 {
158 if (*sc_String == ':')
159 {
160 if (inSequence != -1)
161 {
162 SC_ScriptError("SN_InitSequenceScript: Nested Script Error");
163 }
164 tempDataStart = (int *) Z_Malloc(SS_TEMPBUFFER_SIZE);
165 memset(tempDataStart, 0, SS_TEMPBUFFER_SIZE);
166 tempDataPtr = tempDataStart;
167 for (i = 0; i < SS_MAX_SCRIPTS; i++)
168 {
169 if (SequenceData[i] == NULL)
170 {
171 break;
172 }
173 }
174 if (i == SS_MAX_SCRIPTS)
175 {
176 I_Error("Number of SS Scripts >= SS_MAX_SCRIPTS");
177 }
178 for (j = 0; j < SEQ_NUMSEQ; j++)
179 {
180 if (!strcasecmp(SequenceTranslate[j].name, sc_String + 1))
181 {
182 SequenceTranslate[j].scriptNum = i;
183 inSequence = j;
184 break;
185 }
186 }
187 continue; // parse the next command
188 }
189 if (inSequence == -1)
190 {
191 continue;
192 }
193 if (SC_Compare(SS_STRING_PLAYUNTILDONE))
194 {
195 VerifySequencePtr(tempDataStart, tempDataPtr);
196 SC_MustGetString();
197 *tempDataPtr++ = SS_CMD_PLAY;
198 *tempDataPtr++ = GetSoundOffset(sc_String);
199 *tempDataPtr++ = SS_CMD_WAITUNTILDONE;
200 }
201 else if (SC_Compare(SS_STRING_PLAY))
202 {
203 VerifySequencePtr(tempDataStart, tempDataPtr);
204 SC_MustGetString();
205 *tempDataPtr++ = SS_CMD_PLAY;
206 *tempDataPtr++ = GetSoundOffset(sc_String);
207 }
208 else if (SC_Compare(SS_STRING_PLAYTIME))
209 {
210 VerifySequencePtr(tempDataStart, tempDataPtr);
211 SC_MustGetString();
212 *tempDataPtr++ = SS_CMD_PLAY;
213 *tempDataPtr++ = GetSoundOffset(sc_String);
214 SC_MustGetNumber();
215 *tempDataPtr++ = SS_CMD_DELAY;
216 *tempDataPtr++ = sc_Number;
217 }
218 else if (SC_Compare(SS_STRING_PLAYREPEAT))
219 {
220 VerifySequencePtr(tempDataStart, tempDataPtr);
221 SC_MustGetString();
222 *tempDataPtr++ = SS_CMD_PLAYREPEAT;
223 *tempDataPtr++ = GetSoundOffset(sc_String);
224 }
225 else if (SC_Compare(SS_STRING_DELAY))
226 {
227 VerifySequencePtr(tempDataStart, tempDataPtr);
228 *tempDataPtr++ = SS_CMD_DELAY;
229 SC_MustGetNumber();
230 *tempDataPtr++ = sc_Number;
231 }
232 else if (SC_Compare(SS_STRING_DELAYRAND))
233 {
234 VerifySequencePtr(tempDataStart, tempDataPtr);
235 *tempDataPtr++ = SS_CMD_DELAYRAND;
236 SC_MustGetNumber();
237 *tempDataPtr++ = sc_Number;
238 SC_MustGetNumber();
239 *tempDataPtr++ = sc_Number;
240 }
241 else if (SC_Compare(SS_STRING_VOLUME))
242 {
243 VerifySequencePtr(tempDataStart, tempDataPtr);
244 *tempDataPtr++ = SS_CMD_VOLUME;
245 SC_MustGetNumber();
246 *tempDataPtr++ = sc_Number;
247 }
248 else if (SC_Compare(SS_STRING_END))
249 {
250 int dataSize;
251
252 *tempDataPtr++ = SS_CMD_END;
253 dataSize = (tempDataPtr - tempDataStart) * sizeof(int);
254 SequenceData[i] = (int *) Z_Malloc(dataSize);
255 memcpy(SequenceData[i], tempDataStart, dataSize);
256 Z_Free(tempDataStart);
257 inSequence = -1;
258 }
259 else if (SC_Compare(SS_STRING_STOPSOUND))
260 {
261 SC_MustGetString();
262 SequenceTranslate[inSequence].stopSound =
263 GetSoundOffset(sc_String);
264 *tempDataPtr++ = SS_CMD_STOPSOUND;
265 }
266 else
267 {
268 SC_ScriptError("SN_InitSequenceScript: Unknown command.\n");
269 }
270 }
271 }
272
273 void SN_StartSequence(mobj_t * mobj, int sequence)
274 {
275 seqnode_t *node;
276
277 SN_StopSequence(mobj); // Stop any previous sequence
278 node = (seqnode_t *) Z_Malloc(sizeof(seqnode_t));
279 node->sequencePtr = SequenceData[SequenceTranslate[sequence].scriptNum];
280 node->sequence = sequence;
281 node->mobj = mobj;
282 node->delayTics = 0;
283 node->stopSound = SequenceTranslate[sequence].stopSound;
284 node->volume = 127; // Start at max volume
285
286 if (!SequenceListHead)
287 {
288 SequenceListHead = node;
289 node->next = node->prev = NULL;
290 }
291 else
292 {
293 SequenceListHead->prev = node;
294 node->next = SequenceListHead;
295 node->prev = NULL;
296 SequenceListHead = node;
297 }
298 ActiveSequences++;
299 return;
300 }
301
302 void SN_StartSequenceName(mobj_t * mobj, const char *name)
303 {
304 int i;
305
306 for (i = 0; i < SEQ_NUMSEQ; i++)
307 {
308 if (!strcmp(name, SequenceTranslate[i].name))
309 {
310 SN_StartSequence(mobj, i);
311 return;
312 }
313 }
314 }
315
316 void SN_StopSequence(mobj_t * mobj)
317 {
318 seqnode_t *node;
319 seqnode_t *next_node;
320
321 for (node = SequenceListHead; node; node = next_node)
322 {
323 next_node = node->next;
324 if (node->mobj == mobj)
325 {
326 S_StopSound(mobj);
327 if (node->stopSound)
328 {
329 S_StartSoundAtVolume(mobj, node->stopSound, node->volume, 0);
330 }
331 if (SequenceListHead == node)
332 {
333 SequenceListHead = node->next;
334 }
335 if (node->prev)
336 {
337 node->prev->next = node->next;
338 }
339 if (node->next)
340 {
341 node->next->prev = node->prev;
342 }
343 Z_Free(node);
344 ActiveSequences--;
345 }
346 }
347 }
348
349 void SN_UpdateActiveSequences(void)
350 {
351 seqnode_t *node;
352 seqnode_t *next_node;
353 dboolean sndPlaying;
354
355 if (!ActiveSequences || dsda_Paused())
356 { // No sequences currently playing/game is paused
357 return;
358 }
359 for (node = SequenceListHead; node; node = next_node)
360 {
361 next_node = node->next;
362 if (node->delayTics)
363 {
364 node->delayTics--;
365 continue;
366 }
367 sndPlaying = S_GetSoundPlayingInfo(node->mobj, node->currentSoundID);
368 switch (*node->sequencePtr)
369 {
370 case SS_CMD_PLAY:
371 if (!sndPlaying)
372 {
373 node->currentSoundID = *(node->sequencePtr + 1);
374 S_StartSoundAtVolume(node->mobj, node->currentSoundID,
375 node->volume, 0);
376 }
377 node->sequencePtr += 2;
378 break;
379 case SS_CMD_WAITUNTILDONE:
380 if (!sndPlaying)
381 {
382 node->sequencePtr++;
383 node->currentSoundID = 0;
384 }
385 break;
386 case SS_CMD_PLAYREPEAT:
387 if (!sndPlaying)
388 {
389 node->currentSoundID = *(node->sequencePtr + 1);
390 S_StartSoundAtVolume(node->mobj, node->currentSoundID,
391 node->volume, 0);
392 }
393 break;
394 case SS_CMD_DELAY:
395 node->delayTics = *(node->sequencePtr + 1);
396 node->sequencePtr += 2;
397 node->currentSoundID = 0;
398 break;
399 case SS_CMD_DELAYRAND:
400 node->delayTics = *(node->sequencePtr + 1) +
401 M_Random() % (*(node->sequencePtr + 2) -
402 *(node->sequencePtr + 1));
403 node->sequencePtr += 2;
404 node->currentSoundID = 0;
405 break;
406 case SS_CMD_VOLUME:
407 node->volume = (127 * (*(node->sequencePtr + 1))) / 100;
408 node->sequencePtr += 2;
409 break;
410 case SS_CMD_STOPSOUND:
411 // Wait until something else stops the sequence
412 break;
413 case SS_CMD_END:
414 SN_StopSequence(node->mobj);
415 break;
416 default:
417 break;
418 }
419 }
420 }
421
422 void SN_StopAllSequences(void)
423 {
424 seqnode_t *node;
425 seqnode_t *next_node;
426
427 for (node = SequenceListHead; node; node = next_node)
428 {
429 next_node = node->next;
430 node->stopSound = 0; // don't play any stop sounds
431 SN_StopSequence(node->mobj);
432 }
433 }
434
435 int SN_GetSequenceOffset(int sequence, int *sequencePtr)
436 {
437 return (sequencePtr -
438 SequenceData[SequenceTranslate[sequence].scriptNum]);
439 }
440
441 void SN_ChangeNodeData(int nodeNum, int seqOffset, int delayTics, int volume,
442 int currentSoundID)
443 {
444 int i;
445 seqnode_t *node;
446
447 i = 0;
448 node = SequenceListHead;
449 while (node && i < nodeNum)
450 {
451 node = node->next;
452 i++;
453 }
454 if (!node)
455 { // reach the end of the list before finding the nodeNum-th node
456 return;
457 }
458 node->delayTics = delayTics;
459 node->volume = volume;
460 node->sequencePtr += seqOffset;
461 node->currentSoundID = currentSoundID;
462 }