-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
fi_main.c
3103 lines (2662 loc) · 78.7 KB
/
fi_main.c
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2010 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2010 Daniel Swanson <danij@dengine.net>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* The "In Fine" finale sequence system.
*/
// HEADER FILES ------------------------------------------------------------
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "de_defs.h"
#include "de_graphics.h"
#include "de_refresh.h"
#include "de_render.h"
#include "de_network.h"
#include "de_audio.h"
#include "de_infine.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
#define STACK_SIZE (16) // Size of the InFine state stack.
#define MAX_TOKEN_LEN (8192)
#define MAX_HANDLERS (128)
// Helper macro for defining infine command functions.
#define DEFFC(name) void FIC_##name(fi_cmd_t* cmd, fi_operand_t* ops, fi_state_t* s)
// TYPES -------------------------------------------------------------------
typedef struct {
cvartype_t type;
union {
int integer;
float flt;
const char* cstring;
} data;
} fi_operand_t;
typedef struct fi_handler_s {
int ddkey;
fi_objectname_t marker;
} fi_handler_t;
typedef struct fi_object_collection_s {
uint num;
fi_object_t** vector;
} fi_object_collection_t;
typedef struct fi_state_s {
struct fi_state_flags_s {
char can_skip:1;
char suspended:1;
char paused:1;
char eat_events:1; // Script will eat all input events.
char show_menu:1;
} flags;
finale_mode_t mode;
fi_object_collection_t objects;
fi_handler_t eventHandlers[MAX_HANDLERS];
char* script; // A copy of the script.
const char* cp; // The command cursor.
int timer;
int inTime;
boolean cmdExecuted, skipping, lastSkipped, gotoSkip, skipNext;
int doLevel; // Level of DO-skipping.
int wait;
fi_objectname_t gotoTarget;
fidata_text_t* waitingText;
fidata_pic_t* waitingPic;
material_t* bgMaterial;
animatorvector4_t bgColor;
animatorvector4_t imgColor;
animatorvector2_t imgOffset;
animatorvector4_t filter;
animatorvector3_t textColor[9];
int initialGameState; // Game state before the script began.
int overlayGameState; // Overlay scripts run only in one gameMode.
void* extraData;
} fi_state_t;
typedef struct fi_cmd_s {
char* token;
const char* operands;
void (*func) (struct fi_cmd_s* cmd, fi_operand_t* ops, fi_state_t* s);
struct fi_cmd_flags_s {
char when_skipping:1;
char when_condition_skipping; // Skipping because condition failed.
} flags;
} fi_cmd_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
fidata_text_t* P_CreateText(const char* name);
void P_DestroyText(fidata_text_t* text);
fidata_pic_t* P_CreatePic(const char* name);
void P_DestroyPic(fidata_pic_t* pic);
// Command functions.
DEFFC(Do);
DEFFC(End);
DEFFC(BGFlat);
DEFFC(BGTexture);
DEFFC(NoBGMaterial);
DEFFC(Wait);
DEFFC(WaitText);
DEFFC(WaitAnim);
DEFFC(Tic);
DEFFC(InTime);
DEFFC(Color);
DEFFC(ColorAlpha);
DEFFC(Pause);
DEFFC(CanSkip);
DEFFC(NoSkip);
DEFFC(SkipHere);
DEFFC(Events);
DEFFC(NoEvents);
DEFFC(OnKey);
DEFFC(UnsetKey);
DEFFC(If);
DEFFC(IfNot);
DEFFC(Else);
DEFFC(GoTo);
DEFFC(Marker);
DEFFC(Image);
DEFFC(ImageAt);
DEFFC(XImage);
DEFFC(Delete);
DEFFC(Patch);
DEFFC(SetPatch);
DEFFC(Anim);
DEFFC(AnimImage);
DEFFC(StateAnim);
DEFFC(Repeat);
DEFFC(ClearAnim);
DEFFC(PicSound);
DEFFC(ObjectOffX);
DEFFC(ObjectOffY);
DEFFC(ObjectOffZ);
DEFFC(ObjectScaleX);
DEFFC(ObjectScaleY);
DEFFC(ObjectScaleZ);
DEFFC(ObjectScale);
DEFFC(ObjectScaleXY);
DEFFC(ObjectScaleXYZ);
DEFFC(ObjectRGB);
DEFFC(ObjectAlpha);
DEFFC(ObjectAngle);
DEFFC(Rect);
DEFFC(FillColor);
DEFFC(EdgeColor);
DEFFC(OffsetX);
DEFFC(OffsetY);
DEFFC(Sound);
DEFFC(SoundAt);
DEFFC(SeeSound);
DEFFC(DieSound);
DEFFC(Music);
DEFFC(MusicOnce);
DEFFC(Filter);
DEFFC(Text);
DEFFC(TextFromDef);
DEFFC(TextFromLump);
DEFFC(SetText);
DEFFC(SetTextDef);
DEFFC(DeleteText);
DEFFC(Font);
DEFFC(FontA);
DEFFC(FontB);
DEFFC(TextColor);
DEFFC(TextRGB);
DEFFC(TextAlpha);
DEFFC(TextOffX);
DEFFC(TextOffY);
DEFFC(TextCenter);
DEFFC(TextNoCenter);
DEFFC(TextScroll);
DEFFC(TextPos);
DEFFC(TextRate);
DEFFC(TextLineHeight);
DEFFC(NoMusic);
DEFFC(TextScaleX);
DEFFC(TextScaleY);
DEFFC(TextScale);
DEFFC(PlayDemo);
DEFFC(Command);
DEFFC(ShowMenu);
DEFFC(NoShowMenu);
D_CMD(StartFinale);
D_CMD(StopFinale);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// Default color for Text objects.
float fiDefaultTextRGB[3] = { 1, 1, 1 };
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean active = false;
// Time is measured in seconds.
// Colors are floating point and [0,1].
static fi_cmd_t commands[] = {
// Run Control
{ "DO", "", FIC_Do, true, true },
{ "END", "", FIC_End },
{ "IF", "s", FIC_If }, // if (value-id)
{ "IFNOT", "s", FIC_IfNot }, // ifnot (value-id)
{ "ELSE", "", FIC_Else },
{ "GOTO", "s", FIC_GoTo }, // goto (marker)
{ "MARKER", "s", FIC_Marker, true },
{ "in", "f", FIC_InTime }, // in (time)
{ "pause", "", FIC_Pause },
{ "tic", "", FIC_Tic },
{ "wait", "f", FIC_Wait }, // wait (time)
{ "waittext", "s", FIC_WaitText }, // waittext (id)
{ "waitanim", "s", FIC_WaitAnim }, // waitanim (id)
{ "canskip", "", FIC_CanSkip },
{ "noskip", "", FIC_NoSkip },
{ "skiphere", "", FIC_SkipHere, true },
{ "events", "", FIC_Events },
{ "noevents", "", FIC_NoEvents },
{ "onkey", "ss", FIC_OnKey }, // onkey (keyname) (marker)
{ "unsetkey", "s", FIC_UnsetKey }, // unsetkey (keyname)
// Screen Control
{ "color", "fff", FIC_Color }, // color (red) (green) (blue)
{ "coloralpha", "ffff", FIC_ColorAlpha }, // coloralpha (r) (g) (b) (a)
{ "flat", "s", FIC_BGFlat }, // flat (flat-id)
{ "texture", "s", FIC_BGTexture }, // texture (texture-id)
{ "noflat", "", FIC_NoBGMaterial },
{ "notexture", "", FIC_NoBGMaterial },
{ "offx", "f", FIC_OffsetX }, // offx (x)
{ "offy", "f", FIC_OffsetY }, // offy (y)
{ "filter", "ffff", FIC_Filter }, // filter (r) (g) (b) (a)
// Audio
{ "sound", "s", FIC_Sound }, // sound (snd)
{ "soundat", "sf", FIC_SoundAt }, // soundat (snd) (vol:0-1)
{ "seesound", "s", FIC_SeeSound }, // seesound (mobjtype)
{ "diesound", "s", FIC_DieSound }, // diesound (mobjtype)
{ "music", "s", FIC_Music }, // music (musicname)
{ "musiconce", "s", FIC_MusicOnce }, // musiconce (musicname)
{ "nomusic", "", FIC_NoMusic },
// Objects
{ "del", "s", FIC_Delete }, // del (id)
{ "x", "sf", FIC_ObjectOffX }, // x (id) (x)
{ "y", "sf", FIC_ObjectOffY }, // y (id) (y)
{ "z", "sf", FIC_ObjectOffZ }, // z (id) (z)
{ "sx", "sf", FIC_ObjectScaleX }, // sx (id) (x)
{ "sy", "sf", FIC_ObjectScaleY }, // sy (id) (y)
{ "sz", "sf", FIC_ObjectScaleZ }, // sz (id) (z)
{ "scale", "sf", FIC_ObjectScale }, // scale (id) (factor)
{ "scalexy", "sff", FIC_ObjectScaleXY }, // scalexy (id) (x) (y)
{ "scalexyz", "sfff", FIC_ObjectScaleXYZ }, // scalexyz (id) (x) (y) (z)
{ "rgb", "sfff", FIC_ObjectRGB }, // rgb (id) (r) (g) (b)
{ "alpha", "sf", FIC_ObjectAlpha }, // alpha (id) (alpha)
{ "angle", "sf", FIC_ObjectAngle }, // angle (id) (degrees)
// Rects
{ "rect", "sffff", FIC_Rect }, // rect (hndl) (x) (y) (w) (h)
{ "fillcolor", "ssffff", FIC_FillColor }, // fillcolor (h) (top/bottom/both) (r) (g) (b) (a)
{ "edgecolor", "ssffff", FIC_EdgeColor }, // edgecolor (h) (top/bottom/both) (r) (g) (b) (a)
// Pics
{ "image", "ss", FIC_Image }, // image (id) (raw-image-lump)
{ "imageat", "sffs", FIC_ImageAt }, // imageat (id) (x) (y) (raw)
{ "ximage", "ss", FIC_XImage }, // ximage (id) (ext-gfx-filename)
{ "patch", "sffs", FIC_Patch }, // patch (id) (x) (y) (patch)
{ "set", "ss", FIC_SetPatch }, // set (id) (lump)
{ "clranim", "s", FIC_ClearAnim }, // clranim (id)
{ "anim", "ssf", FIC_Anim }, // anim (id) (patch) (time)
{ "imageanim", "ssf", FIC_AnimImage }, // imageanim (hndl) (raw-img) (time)
{ "picsound", "ss", FIC_PicSound }, // picsound (hndl) (sound)
{ "repeat", "s", FIC_Repeat }, // repeat (id)
{ "states", "ssi", FIC_StateAnim }, // states (id) (state) (count)
// Text
{ "text", "sffs", FIC_Text }, // text (hndl) (x) (y) (string)
{ "textdef", "sffs", FIC_TextFromDef }, // textdef (hndl) (x) (y) (txt-id)
{ "textlump", "sffs", FIC_TextFromLump }, // textlump (hndl) (x) (y) (lump)
{ "settext", "ss", FIC_SetText }, // settext (id) (newtext)
{ "settextdef", "ss", FIC_SetTextDef }, // settextdef (id) (txt-id)
{ "precolor", "ifff", FIC_TextColor }, // precolor (num) (r) (g) (b)
{ "center", "s", FIC_TextCenter }, // center (id)
{ "nocenter", "s", FIC_TextNoCenter }, // nocenter (id)
{ "scroll", "sf", FIC_TextScroll }, // scroll (id) (speed)
{ "pos", "si", FIC_TextPos }, // pos (id) (pos)
{ "rate", "si", FIC_TextRate }, // rate (id) (rate)
{ "font", "ss", FIC_Font }, // font (id) (font)
{ "fonta", "s", FIC_FontA }, // fonta (id)
{ "fontb", "s", FIC_FontB }, // fontb (id)
{ "linehgt", "sf", FIC_TextLineHeight }, // linehgt (hndl) (hgt)
// Game Control
{ "playdemo", "s", FIC_PlayDemo }, // playdemo (filename)
{ "cmd", "s", FIC_Command }, // cmd (console command)
{ "trigger", "", FIC_ShowMenu },
{ "notrigger", "", FIC_NoShowMenu },
// Deprecated Pic commands
{ "delpic", "s", FIC_Delete }, // delpic (id)
// Deprecated Text commands
{ "deltext", "s", FIC_DeleteText }, // deltext (hndl)
{ "textrgb", "sfff", FIC_TextRGB }, // textrgb (id) (r) (g) (b)
{ "textalpha", "sf", FIC_TextAlpha }, // textalpha (id) (alpha)
{ "tx", "sf", FIC_TextOffX }, // tx (id) (x)
{ "ty", "sf", FIC_TextOffY }, // ty (id) (y)
{ "tsx", "sf", FIC_TextScaleX }, // tsx (id) (x)
{ "tsy", "sf", FIC_TextScaleY }, // tsy (id) (y)
{ "textscale", "sf", FIC_TextScale }, // textscale (id) (x) (y)
{ NULL, 0, NULL } // Terminate.
};
static fi_state_t stateStack[STACK_SIZE];
static fi_state_t* fi; // Pointer to the current state in the stack.
static char token[MAX_TOKEN_LEN];
static void* defaultState = 0;
// Allow stretching to fill the screen at near 4:3 aspect ratios?
static byte noStretch = false;
// CODE --------------------------------------------------------------------
/**
* Called during pre-init to register cvars and ccmds for the finale system.
*/
void FI_Register(void)
{
C_VAR_BYTE("finale-nostretch", &noStretch, 0, 0, 1);
C_CMD("startfinale", "s", StartFinale);
C_CMD("startinf", "s", StartFinale);
C_CMD("stopfinale", "", StopFinale);
C_CMD("stopinf", "", StopFinale);
}
static fi_cmd_t* findCommand(const char* name)
{
int i;
for(i = 0; commands[i].token; ++i)
{
fi_cmd_t* cmd = &commands[i];
if(!stricmp(name, cmd->token))
{
return cmd;
}
}
return 0; // Not found.
}
static void useColor(animator_t *color, int components)
{
if(components == 3)
{
glColor3f(color[0].value, color[1].value, color[2].value);
}
else if(components == 4)
{
glColor4f(color[0].value, color[1].value, color[2].value, color[3].value);
}
}
static DGLuint loadGraphics(ddresourceclass_t resClass, const char* name,
gfxmode_t mode, int useMipmap, boolean clamped, int otherFlags)
{
return GL_PrepareExtTexture(resClass, name, mode, useMipmap,
GL_LINEAR, GL_LINEAR, 0 /*no anisotropy*/,
clamped? GL_CLAMP_TO_EDGE : GL_REPEAT,
clamped? GL_CLAMP_TO_EDGE : GL_REPEAT,
otherFlags);
}
static void objectSetUniqueName(fi_object_t* obj, const char* name)
{
strncpy(obj->name, name, sizeof(obj->name) - 1);
}
static fi_state_t* allocState(void)
{
fi_state_t* s;
if(!fi)
{ // Start from the bottom of the stack.
s = fi = stateStack;
}
else
{ // Get the next state from the stack.
s = fi += 1;
if(fi == stateStack + STACK_SIZE)
Con_Error("InFine state stack overflow.\n");
}
#ifdef _DEBUG // Is the stack leaking?
Con_Printf(" State index: %i.\n", fi - stateStack);
#endif
memset(s, 0, sizeof(*s));
return s;
}
static fi_state_t* newState(const char* script)
{
fi_state_t* s = allocState();
size_t size;
// Take a copy of the script.
size = strlen(script);
s->script = Z_Malloc(size + 1, PU_STATIC, 0);
memcpy(s->script, script, size);
s->script[size] = '\0';
s->cp = s->script; // Init cursor.
return s;
}
static void objectsThink(fi_object_collection_t* c)
{
uint i;
for(i = 0; i < c->num; ++i)
{
fi_object_t* obj = c->vector[i];
switch(obj->type)
{
case FI_PIC: FIData_PicThink((fidata_pic_t*)obj); break;
case FI_TEXT: FIData_TextThink((fidata_text_t*)obj); break;
default: break;
}
}
}
static void objectsDraw2(fi_object_collection_t* c, fi_obtype_e type, float xOffset, float yOffset)
{
uint i;
for(i = 0; i < c->num; ++i)
{
fi_object_t* obj = c->vector[i];
if(obj->type != type)
continue;
switch(obj->type)
{
case FI_PIC: FIData_PicDraw((fidata_pic_t*)obj, xOffset, yOffset); break;
case FI_TEXT: FIData_TextDraw((fidata_text_t*)obj, xOffset, yOffset); break;
default: break;
}
}
}
static void objectsDraw(fi_object_collection_t* c, float picXOffset, float picYOffset)
{
// Images first, then text.
objectsDraw2(c, FI_PIC, picXOffset, picYOffset);
objectsDraw2(c, FI_TEXT, 0, 0);
}
static void objectsClear(fi_object_collection_t* c)
{
// Delete external images, text strings etc.
if(c->vector)
{
uint i;
for(i = 0; i < c->num; ++i)
{
fi_object_t* obj = c->vector[i];
switch(obj->type)
{
case FI_PIC: P_DestroyPic((fidata_pic_t*)obj); break;
case FI_TEXT: P_DestroyText((fidata_text_t*)obj); break;
default:
break;
}
}
Z_Free(c->vector);
}
c->vector = NULL;
c->num = 0;
}
static fi_object_t* objectsFind2(fi_object_collection_t* c, const char* name, fi_obtype_e type)
{
assert(name && name[0]);
assert(type > FI_NONE);
{uint i;
for(i = 0; i < c->num; ++i)
{
fi_object_t* obj = c->vector[i];
if(obj->type == type && !stricmp(obj->name, name))
{
return obj;
}
}}
return NULL;
}
static fi_object_t* objectsFind(fi_object_collection_t* c, const char* name)
{
fi_object_t* obj;
// First check all pics.
if((obj = objectsFind2(c, name, FI_PIC)) != NULL)
return obj;
// Then check text objects.
if((obj = objectsFind2(c, name, FI_TEXT)) != NULL)
return obj;
return NULL;
}
static fi_object_t* objectsAdd(fi_object_collection_t* c, fi_object_t* obj)
{
// Link with this state.
c->vector = Z_Realloc(c->vector, sizeof(obj) * ++c->num, PU_STATIC);
c->vector[c->num-1] = obj;
return obj;
}
static void objectsRemove(fi_object_collection_t* c, fi_object_t* obj)
{
uint i;
for(i = 0; i < c->num; ++i)
{
fi_object_t* other = c->vector[i];
if(other == obj)
{
if(i != c->num-1)
memmove(&c->vector[i], &c->vector[i+1], sizeof(*c->vector) * (c->num-i));
if(c->num > 1)
{
c->vector = Z_Realloc(c->vector, sizeof(*c->vector) * --c->num, PU_STATIC);
}
else
{
Z_Free(c->vector);
c->vector = NULL;
c->num = 0;
}
return;
}
}
}
static void stateChangeMode(fi_state_t* s, finale_mode_t mode)
{
s->mode = mode;
}
static void stateSetExtraData(fi_state_t* s, const void* data)
{
if(!data)
return;
if(!s->extraData || !(FINALE_SCRIPT_EXTRADATA_SIZE > 0))
return;
memcpy(s->extraData, data, FINALE_SCRIPT_EXTRADATA_SIZE);
}
static void stateSetInitialGameState(fi_state_t* s, int gameState, const void* clientState)
{
s->initialGameState = gameState;
s->flags.show_menu = true; // Enabled by default.
if(FINALE_SCRIPT_EXTRADATA_SIZE > 0)
{
stateSetExtraData(s, &defaultState);
if(clientState)
stateSetExtraData(s, clientState);
}
if(s->mode == FIMODE_OVERLAY)
{ // Overlay scripts stop when the gameMode changes.
s->overlayGameState = gameState;
}
}
static void stateReleaseScript(fi_state_t* s)
{
// Free the current state.
if(s->script)
Z_Free(s->script);
s->script = 0;
s->cp = 0;
}
/**
* Clear the specified state to the default, blank state.
*/
static void stateClear(fi_state_t* s)
{
int i;
active = true;
s->flags.suspended = false;
s->flags.can_skip = true; // By default skipping is enabled.
s->flags.paused = false;
s->timer = 0;
s->cmdExecuted = false; // Nothing is drawn until a cmd has been executed.
s->skipping = false;
s->wait = 0; // Not waiting for anything.
s->inTime = 0; // Interpolation is off.
s->bgMaterial = NULL; // No background material.
s->gotoSkip = false;
s->skipNext = false;
s->waitingText = NULL;
s->waitingPic = NULL;
memset(s->gotoTarget, 0, sizeof(s->gotoTarget));
memset(s->imgOffset, 0, sizeof(s->imgOffset));
memset(s->filter, 0, sizeof(s->filter));
AnimatorVector4_Init(s->bgColor, 1, 1, 1, 1);
objectsClear(&s->objects);
for(i = 0; i < 9; ++i)
{
AnimatorVector3_Init(s->textColor[i], 1, 1, 1);
}
}
static void stateInit(fi_state_t* s, finale_mode_t mode, int gameState, const void* clientState)
{
stateClear(s);
stateChangeMode(s, mode);
stateSetInitialGameState(s, gameState, clientState);
}
static __inline fi_state_t* stackTop(void)
{
return fi;
}
static void stackPop(void)
{
fi_state_t* s = stackTop();
/*#ifdef _DEBUG
Con_Printf("InFine: s=%p (%i)\n", s, s - stateStack);
#endif*/
if(!s)
{
#ifdef _DEBUG
Con_Printf("InFine: Pop in NULL state!\n");
#endif
return;
}
stateClear(s);
stateReleaseScript(s);
// Should we go back to NULL?
if(fi == stateStack)
{
fi = NULL;
active = false;
}
else
{ // Return to previous state.
fi--;
}
}
static void scriptBegin(fi_state_t* s)
{
if(s->mode != FIMODE_LOCAL)
{
int flags = FINF_BEGIN | (s->mode == FIMODE_AFTER ? FINF_AFTER : s->mode == FIMODE_OVERLAY ? FINF_OVERLAY : 0);
ddhook_finale_script_serialize_extradata_t p;
boolean haveExtraData = false;
memset(&p, 0, sizeof(p));
if(s->extraData)
{
p.extraData = s->extraData;
p.outBuf = 0;
p.outBufSize = 0;
haveExtraData = Plug_DoHook(HOOK_FINALE_SCRIPT_SERIALIZE_EXTRADATA, 0, &p);
}
// Tell clients to start this script.
Sv_Finale(flags, s->script, (haveExtraData? p.outBuf : 0), (haveExtraData? p.outBufSize : 0));
}
// Any hooks?
Plug_DoHook(HOOK_FINALE_SCRIPT_BEGIN, (int) s->mode, s->extraData);
}
static void scriptCanSkip(fi_state_t* s, boolean yes)
{
s->flags.can_skip = yes;
}
/**
* Stop playing the script and go to next game state.
*/
static void scriptTerminate(fi_state_t* s)
{
ddhook_finale_script_stop_paramaters_t p;
int oldMode;
if(!active)
return;
if(!s->flags.can_skip)
return;
#ifdef _DEBUG
Con_Printf("Finale End: mode=%i '%.30s'\n", s->mode, s->script);
#endif
oldMode = s->mode;
memset(&p, 0, sizeof(p));
p.initialGameState = s->initialGameState;
p.extraData = s->extraData;
// This'll set fi to NULL.
stackPop();
if(oldMode != FIMODE_LOCAL)
{
// Tell clients to stop the finale.
Sv_Finale(FINF_END, 0, NULL, 0);
}
Plug_DoHook(HOOK_FINALE_SCRIPT_TERMINATE, oldMode, &p);
}
static const char* scriptNextToken(fi_state_t* s)
{
char* out;
// Skip whitespace.
while(*s->cp && isspace(*s->cp))
s->cp++;
if(!*s->cp)
return NULL; // The end has been reached.
out = token;
if(*s->cp == '"') // A string?
{
for(s->cp++; *s->cp; s->cp++)
{
if(*s->cp == '"')
{
s->cp++;
// Convert double quotes to single ones.
if(*s->cp != '"')
break;
}
*out++ = *s->cp;
}
}
else
{
while(!isspace(*s->cp) && *s->cp)
*out++ = *s->cp++;
}
*out++ = 0;
return token;
}
/**
* Parse the command operands from the script. If successfull, a ptr to a new
* vector of @c fi_operand_t objects is returned. Ownership of the vector is
* given to the caller.
*
* @return Ptr to a new vector of @c fi_operand_t or @c NULL.
*/
static fi_operand_t* scriptParseCommandOperands(fi_state_t* s, const fi_cmd_t* cmd, uint* count)
{
const char* origCursorPos;
uint numOperands;
fi_operand_t* ops = 0;
if(!cmd->operands || !cmd->operands[0])
return NULL;
origCursorPos = s->cp;
numOperands = (uint)strlen(cmd->operands);
// Operands are read sequentially. This is to potentially allow for
// command overloading at a later time.
{uint i = 0;
do
{
char typeSymbol = cmd->operands[i];
fi_operand_t* op;
cvartype_t type;
if(!scriptNextToken(s))
{
s->cp = origCursorPos;
if(ops)
free(ops);
if(count)
*count = 0;
Con_Message("scriptParseCommandOperands: Too few operands for command '%s'.\n", cmd->token);
return NULL;
}
switch(typeSymbol)
{
// Supported operand type symbols:
case 'i': type = CVT_INT; break;
case 'f': type = CVT_FLOAT; break;
case 's': type = CVT_CHARPTR; break;
default:
Con_Error("scriptParseCommandOperands: Invalid symbol '%c' in operand list for command '%s'.", typeSymbol, cmd->token);
}
if(!ops)
ops = malloc(sizeof(*ops));
else
ops = realloc(ops, sizeof(*ops) * (i+1));
op = &ops[i];
op->type = type;
switch(type)
{
case CVT_INT:
op->data.integer = strtol(token, NULL, 0);
break;
case CVT_FLOAT:
op->data.flt = strtod(token, NULL);
break;
case CVT_CHARPTR:
{
size_t len = strlen(token)+1;
char* str = malloc(len);
dd_snprintf(str, len, "%s", token);
op->data.cstring = str;
break;
}
}
} while(++i < numOperands);}
s->cp = origCursorPos;
if(count)
*count = numOperands;
return ops;
}
static boolean stateSkipRequest(fi_state_t* s)
{
s->waitingText = NULL; // Stop waiting for things.
s->waitingPic = NULL;
if(s->flags.paused)
{
s->flags.paused = false; // Un-pause.
s->wait = 0;
return true;
}
if(s->flags.can_skip)
{
s->skipping = true; // Start skipping ahead.
s->wait = 0;
return true;
}
return s->flags.eat_events;
}
static boolean scriptSkipCommand(fi_state_t* s, const fi_cmd_t* cmd)
{
if((s->skipNext && !cmd->flags.when_condition_skipping) ||
((s->skipping || s->gotoSkip) && !cmd->flags.when_skipping))
{
// While not DO-skipping, the condskip has now been done.
if(!s->doLevel)
{
if(s->skipNext)
s->lastSkipped = true;
s->skipNext = false;
}
return true;
}
return false;
}
/**
* Execute one (the next) command, advance script cursor.
*/
static boolean scriptExecuteCommand(fi_state_t* s, const char* commandString)
{
// Semicolon terminates DO-blocks.
if(!strcmp(commandString, ";"))
{
if(s->doLevel > 0)
{
if(--s->doLevel == 0)
{
// The DO-skip has been completed.
s->skipNext = false;
s->lastSkipped = true;
}
}
return true; // Success!
}
// We're now going to execute a command.
s->cmdExecuted = true;
// Is this a command we know how to execute?
{fi_cmd_t* cmd;
if((cmd = findCommand(commandString)))
{
boolean requiredOperands = (cmd->operands && cmd->operands[0]);
fi_operand_t* ops = NULL;
size_t count;
// Check that there are enough operands.
if(!requiredOperands || (ops = scriptParseCommandOperands(s, cmd, &count)))
{
// Should we skip this command?
if(scriptSkipCommand(s, cmd))
return false;
// Execute forthwith!
cmd->func(cmd, ops, s);
}
if(ops)
{ // Destroy.
{uint i;
for(i = 0; i < count; ++i)
{
fi_operand_t* op = &ops[i];
if(op->type == CVT_CHARPTR)
free((char*)op->data.cstring);
}}
free(ops);
}
// The END command may clear the current state.
if((s = stackTop()))
{ // Now we've executed the latest command.
s->lastSkipped = false;
}
}}
return true; // Success!
}
static boolean scriptExecuteNextCommand(fi_state_t* s)
{
const char* token;
if((token = scriptNextToken(s)))
{
scriptExecuteCommand(s, token);
return true;
}