summaryrefslogtreecommitdiff
path: root/src/hexen/sn_sonix.c
blob: 412791a30e1f71b3e7b91b1162732fc041cf3b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 1993-2008 Raven Software
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//-----------------------------------------------------------------------------


// HEADER FILES ------------------------------------------------------------

#include <string.h>
#include "h2def.h"
#include "i_system.h"
#include "i_sound.h"
#include "s_sound.h"

// MACROS ------------------------------------------------------------------

#define SS_MAX_SCRIPTS	64
#define SS_TEMPBUFFER_SIZE	1024
#define SS_SEQUENCE_NAME_LENGTH 32

#define SS_SCRIPT_NAME "SNDSEQ"
#define SS_STRING_PLAY			"play"
#define SS_STRING_PLAYUNTILDONE "playuntildone"
#define SS_STRING_PLAYTIME		"playtime"
#define SS_STRING_PLAYREPEAT	"playrepeat"
#define SS_STRING_DELAY			"delay"
#define SS_STRING_DELAYRAND		"delayrand"
#define SS_STRING_VOLUME		"volume"
#define SS_STRING_END			"end"
#define SS_STRING_STOPSOUND		"stopsound"

// TYPES -------------------------------------------------------------------

typedef enum
{
    SS_CMD_NONE,
    SS_CMD_PLAY,
    SS_CMD_WAITUNTILDONE,       // used by PLAYUNTILDONE
    SS_CMD_PLAYTIME,
    SS_CMD_PLAYREPEAT,
    SS_CMD_DELAY,
    SS_CMD_DELAYRAND,
    SS_CMD_VOLUME,
    SS_CMD_STOPSOUND,
    SS_CMD_END
} sscmds_t;

// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------

// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------

// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

static void VerifySequencePtr(int *base, int *ptr);
static int GetSoundOffset(char *name);

// EXTERNAL DATA DECLARATIONS ----------------------------------------------

extern sfxinfo_t S_sfx[];

// PUBLIC DATA DEFINITIONS -------------------------------------------------

// PRIVATE DATA DEFINITIONS ------------------------------------------------

static struct
{
    char name[SS_SEQUENCE_NAME_LENGTH];
    int scriptNum;
    int stopSound;
} SequenceTranslate[SEQ_NUMSEQ] =
{
    {
    "Platform", 0, 0},
    {
    "Platform", 0, 0},          // a 'heavy' platform is just a platform
    {
    "PlatformMetal", 0, 0},
    {
    "Platform", 0, 0},          // same with a 'creak' platform
    {
    "Silence", 0, 0},
    {
    "Lava", 0, 0},
    {
    "Water", 0, 0},
    {
    "Ice", 0, 0},
    {
    "Earth", 0, 0},
    {
    "PlatformMetal2", 0, 0},
    {
    "DoorNormal", 0, 0},
    {
    "DoorHeavy", 0, 0},
    {
    "DoorMetal", 0, 0},
    {
    "DoorCreak", 0, 0},
    {
    "Silence", 0, 0},
    {
    "Lava", 0, 0},
    {
    "Water", 0, 0},
    {
    "Ice", 0, 0},
    {
    "Earth", 0, 0},
    {
    "DoorMetal2", 0, 0},
    {
    "Wind", 0, 0}
};

static int *SequenceData[SS_MAX_SCRIPTS];

int ActiveSequences;
seqnode_t *SequenceListHead;

// CODE --------------------------------------------------------------------

//==========================================================================
//
// VerifySequencePtr
//
//   Verifies the integrity of the temporary ptr, and ensures that the ptr
//              isn't exceeding the size of the temporary buffer
//==========================================================================

static void VerifySequencePtr(int *base, int *ptr)
{
    if (ptr - base > SS_TEMPBUFFER_SIZE)
    {
        I_Error("VerifySequencePtr:  tempPtr >= %d\n", SS_TEMPBUFFER_SIZE);
    }
}

//==========================================================================
//
// GetSoundOffset
//
//==========================================================================

static int GetSoundOffset(char *name)
{
    int i;

    for (i = 0; i < NUMSFX; i++)
    {
        if (!strcasecmp(name, S_sfx[i].tagname))
        {
            return i;
        }
    }
    SC_ScriptError("GetSoundOffset:  Unknown sound name\n");
    return 0;
}

//==========================================================================
//
// SN_InitSequenceScript
//
//==========================================================================

void SN_InitSequenceScript(void)
{
    int i, j;
    int inSequence;
    int *tempDataStart = NULL;
    int *tempDataPtr = NULL;

    inSequence = -1;
    ActiveSequences = 0;
    for (i = 0; i < SS_MAX_SCRIPTS; i++)
    {
        SequenceData[i] = NULL;
    }
    SC_Open(SS_SCRIPT_NAME);
    while (SC_GetString())
    {
        if (*sc_String == ':')
        {
            if (inSequence != -1)
            {
                SC_ScriptError("SN_InitSequenceScript:  Nested Script Error");
            }
            tempDataStart = (int *) Z_Malloc(SS_TEMPBUFFER_SIZE,
                                             PU_STATIC, NULL);
            memset(tempDataStart, 0, SS_TEMPBUFFER_SIZE);
            tempDataPtr = tempDataStart;
            for (i = 0; i < SS_MAX_SCRIPTS; i++)
            {
                if (SequenceData[i] == NULL)
                {
                    break;
                }
            }
            if (i == SS_MAX_SCRIPTS)
            {
                I_Error("Number of SS Scripts >= SS_MAX_SCRIPTS");
            }
            for (j = 0; j < SEQ_NUMSEQ; j++)
            {
                if (!strcasecmp(SequenceTranslate[j].name, sc_String + 1))
                {
                    SequenceTranslate[j].scriptNum = i;
                    inSequence = j;
                    break;
                }
            }
            continue;           // parse the next command
        }
        if (inSequence == -1)
        {
            continue;
        }
        if (SC_Compare(SS_STRING_PLAYUNTILDONE))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            SC_MustGetString();
            *tempDataPtr++ = SS_CMD_PLAY;
            *tempDataPtr++ = GetSoundOffset(sc_String);
            *tempDataPtr++ = SS_CMD_WAITUNTILDONE;
        }
        else if (SC_Compare(SS_STRING_PLAY))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            SC_MustGetString();
            *tempDataPtr++ = SS_CMD_PLAY;
            *tempDataPtr++ = GetSoundOffset(sc_String);
        }
        else if (SC_Compare(SS_STRING_PLAYTIME))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            SC_MustGetString();
            *tempDataPtr++ = SS_CMD_PLAY;
            *tempDataPtr++ = GetSoundOffset(sc_String);
            SC_MustGetNumber();
            *tempDataPtr++ = SS_CMD_DELAY;
            *tempDataPtr++ = sc_Number;
        }
        else if (SC_Compare(SS_STRING_PLAYREPEAT))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            SC_MustGetString();
            *tempDataPtr++ = SS_CMD_PLAYREPEAT;
            *tempDataPtr++ = GetSoundOffset(sc_String);
        }
        else if (SC_Compare(SS_STRING_DELAY))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            *tempDataPtr++ = SS_CMD_DELAY;
            SC_MustGetNumber();
            *tempDataPtr++ = sc_Number;
        }
        else if (SC_Compare(SS_STRING_DELAYRAND))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            *tempDataPtr++ = SS_CMD_DELAYRAND;
            SC_MustGetNumber();
            *tempDataPtr++ = sc_Number;
            SC_MustGetNumber();
            *tempDataPtr++ = sc_Number;
        }
        else if (SC_Compare(SS_STRING_VOLUME))
        {
            VerifySequencePtr(tempDataStart, tempDataPtr);
            *tempDataPtr++ = SS_CMD_VOLUME;
            SC_MustGetNumber();
            *tempDataPtr++ = sc_Number;
        }
        else if (SC_Compare(SS_STRING_END))
        {
            int dataSize;

            *tempDataPtr++ = SS_CMD_END;
            dataSize = (tempDataPtr - tempDataStart) * sizeof(int);
            SequenceData[i] = (int *) Z_Malloc(dataSize, PU_STATIC, NULL);
            memcpy(SequenceData[i], tempDataStart, dataSize);
            Z_Free(tempDataStart);
            inSequence = -1;
        }
        else if (SC_Compare(SS_STRING_STOPSOUND))
        {
            SC_MustGetString();
            SequenceTranslate[inSequence].stopSound =
                GetSoundOffset(sc_String);
            *tempDataPtr++ = SS_CMD_STOPSOUND;
        }
        else
        {
            SC_ScriptError("SN_InitSequenceScript:  Unknown commmand.\n");
        }
    }
}

//==========================================================================
//
//  SN_StartSequence
//
//==========================================================================

void SN_StartSequence(mobj_t * mobj, int sequence)
{
    seqnode_t *node;

    SN_StopSequence(mobj);      // Stop any previous sequence
    node = (seqnode_t *) Z_Malloc(sizeof(seqnode_t), PU_STATIC, NULL);
    node->sequencePtr = SequenceData[SequenceTranslate[sequence].scriptNum];
    node->sequence = sequence;
    node->mobj = mobj;
    node->delayTics = 0;
    node->stopSound = SequenceTranslate[sequence].stopSound;
    node->volume = 127;         // Start at max volume

    if (!SequenceListHead)
    {
        SequenceListHead = node;
        node->next = node->prev = NULL;
    }
    else
    {
        SequenceListHead->prev = node;
        node->next = SequenceListHead;
        node->prev = NULL;
        SequenceListHead = node;
    }
    ActiveSequences++;
    return;
}

//==========================================================================
//
//  SN_StartSequenceName
//
//==========================================================================

void SN_StartSequenceName(mobj_t * mobj, char *name)
{
    int i;

    for (i = 0; i < SEQ_NUMSEQ; i++)
    {
        if (!strcmp(name, SequenceTranslate[i].name))
        {
            SN_StartSequence(mobj, i);
            return;
        }
    }
}

//==========================================================================
//
//  SN_StopSequence
//
//==========================================================================

void SN_StopSequence(mobj_t * mobj)
{
    seqnode_t *node;

    for (node = SequenceListHead; node; node = node->next)
    {
        if (node->mobj == mobj)
        {
            S_StopSound(mobj);
            if (node->stopSound)
            {
                S_StartSoundAtVolume(mobj, node->stopSound, node->volume);
            }
            if (SequenceListHead == node)
            {
                SequenceListHead = node->next;
            }
            if (node->prev)
            {
                node->prev->next = node->next;
            }
            if (node->next)
            {
                node->next->prev = node->prev;
            }
            Z_Free(node);
            ActiveSequences--;
        }
    }
}

//==========================================================================
//
//  SN_UpdateActiveSequences
//
//==========================================================================

void SN_UpdateActiveSequences(void)
{
    seqnode_t *node;
    boolean sndPlaying;

    if (!ActiveSequences || paused)
    {                           // No sequences currently playing/game is paused
        return;
    }
    for (node = SequenceListHead; node; node = node->next)
    {
        if (node->delayTics)
        {
            node->delayTics--;
            continue;
        }
        sndPlaying = S_GetSoundPlayingInfo(node->mobj, node->currentSoundID);
        switch (*node->sequencePtr)
        {
            case SS_CMD_PLAY:
                if (!sndPlaying)
                {
                    node->currentSoundID = *(node->sequencePtr + 1);
                    S_StartSoundAtVolume(node->mobj, node->currentSoundID,
                                         node->volume);
                }
                node->sequencePtr += 2;
                break;
            case SS_CMD_WAITUNTILDONE:
                if (!sndPlaying)
                {
                    node->sequencePtr++;
                    node->currentSoundID = 0;
                }
                break;
            case SS_CMD_PLAYREPEAT:
                if (!sndPlaying)
                {
                    node->currentSoundID = *(node->sequencePtr + 1);
                    S_StartSoundAtVolume(node->mobj, node->currentSoundID,
                                         node->volume);
                }
                break;
            case SS_CMD_DELAY:
                node->delayTics = *(node->sequencePtr + 1);
                node->sequencePtr += 2;
                node->currentSoundID = 0;
                break;
            case SS_CMD_DELAYRAND:
                node->delayTics = *(node->sequencePtr + 1) +
                    M_Random() % (*(node->sequencePtr + 2) -
                                  *(node->sequencePtr + 1));
                node->sequencePtr += 2;
                node->currentSoundID = 0;
                break;
            case SS_CMD_VOLUME:
                node->volume = (127 * (*(node->sequencePtr + 1))) / 100;
                node->sequencePtr += 2;
                break;
            case SS_CMD_STOPSOUND:
                // Wait until something else stops the sequence
                break;
            case SS_CMD_END:
                SN_StopSequence(node->mobj);
                break;
            default:
                break;
        }
    }
}

//==========================================================================
//
//  SN_StopAllSequences
//
//==========================================================================

void SN_StopAllSequences(void)
{
    seqnode_t *node;

    for (node = SequenceListHead; node; node = node->next)
    {
        node->stopSound = 0;    // don't play any stop sounds
        SN_StopSequence(node->mobj);
    }
}

//==========================================================================
//
//  SN_GetSequenceOffset
//
//==========================================================================

int SN_GetSequenceOffset(int sequence, int *sequencePtr)
{
    return (sequencePtr -
            SequenceData[SequenceTranslate[sequence].scriptNum]);
}

//==========================================================================
//
//  SN_ChangeNodeData
//
//      nodeNum zero is the first node
//==========================================================================

void SN_ChangeNodeData(int nodeNum, int seqOffset, int delayTics, int volume,
                       int currentSoundID)
{
    int i;
    seqnode_t *node;

    i = 0;
    node = SequenceListHead;
    while (node && i < nodeNum)
    {
        node = node->next;
        i++;
    }
    if (!node)
    {                           // reach the end of the list before finding the nodeNum-th node
        return;
    }
    node->delayTics = delayTics;
    node->volume = volume;
    node->sequencePtr += seqOffset;
    node->currentSoundID = currentSoundID;
}