/
f_infine.c
2961 lines (2512 loc) · 72.2 KB
/
f_infine.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
*/
/**
* f_infine.c : The "In Fine" finale engine.
*/
// HEADER FILES ------------------------------------------------------------
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#elif __JSTRIFE__
# include "jstrife.h"
#endif
#include "hu_log.h"
#include "hu_stuff.h"
#include "f_infine.h"
#include "g_common.h"
#include "d_net.h"
#include "p_player.h"
#include "am_map.h"
#include "p_tick.h"
#include "p_start.h"
// MACROS ------------------------------------------------------------------
#define STACK_SIZE (16) // Size of the InFine state stack.
#define MAX_TOKEN_LEN (8192)
#define MAX_SEQUENCE (64)
#define MAX_PICS (128)
#define MAX_TEXT (64)
#define MAX_HANDLERS (128)
#define FI_REPEAT (-2)
// TYPES -------------------------------------------------------------------
enum {
STRETCH = 0,
PILLARBOX,
LETTERBOX,
};
typedef char handle_t[32];
typedef struct ficmd_s {
char* token;
int operands;
void (*func) (void);
boolean whenSkipping;
boolean whenCondSkipping; // Skipping because condition failed.
} ficmd_t;
typedef struct fivalue_s {
float value;
float target;
int steps;
} fivalue_t;
typedef struct fi_obj_s {
boolean used;
handle_t handle;
fivalue_t color[4];
fivalue_t scale[2];
fivalue_t x, y;
fivalue_t angle;
} fiobj_t;
typedef struct fipic_s {
fiobj_t object;
struct fipicflags_s {
char is_patch:1; // Raw image or patch.
char done:1; // Animation finished (or repeated).
char is_rect:1;
char is_ximage:1; // External graphics resource.
} flags;
int seq;
int seqWait[MAX_SEQUENCE], seqTimer;
int lump[MAX_SEQUENCE];
char flip[MAX_SEQUENCE];
short sound[MAX_SEQUENCE];
// For rectangle-objects.
fivalue_t otherColor[4];
fivalue_t edgeColor[4];
fivalue_t otherEdgeColor[4];
} fipic_t;
typedef struct fitext_s {
fiobj_t object;
struct fitextflags_s {
char centered:1;
char font_b:1;
char all_visible:1;
} flags;
int scrollWait, scrollTimer; // Automatic scrolling upwards.
int pos;
int wait, timer;
int lineheight;
char* text;
} fitext_t;
typedef struct fihandler_s {
int code;
handle_t marker;
} fihandler_t;
typedef struct fistate_s {
char* script; // A copy of the script.
char* cp; // The command cursor.
infinemode_t mode;
gamestate_t lastGameState; // Game state before the script began.
int overlayGameState; // Overlay scripts run only in one gameMode.
int timer;
boolean conditions[NUM_FICONDS];
int inTime;
boolean canSkip, skipping;
int doLevel; // Level of DO-skipping.
int wait;
boolean suspended, paused, eatEvents, showMenu;
boolean gotoSkip, skipNext, lastSkipped;
handle_t gotoTarget;
fitext_t* waitingText;
fipic_t* waitingPic;
fihandler_t keyHandlers[MAX_HANDLERS];
material_t* bgMaterial;
fivalue_t bgColor[4];
fivalue_t imgColor[4];
fivalue_t imgOffset[2];
fivalue_t filter[4];
fivalue_t textColor[9][3];
fipic_t pics[MAX_PICS];
fitext_t text[MAX_TEXT];
} fistate_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
void FI_InitValue(fivalue_t* val, float num);
int FI_TextObjectLength(fitext_t* tex);
// Command functions.
void FIC_Do(void);
void FIC_End(void);
void FIC_BGFlat(void);
void FIC_BGTexture(void);
void FIC_NoBGMaterial(void);
void FIC_Wait(void);
void FIC_WaitText(void);
void FIC_WaitAnim(void);
void FIC_Tic(void);
void FIC_InTime(void);
void FIC_Color(void);
void FIC_ColorAlpha(void);
void FIC_Pause(void);
void FIC_CanSkip(void);
void FIC_NoSkip(void);
void FIC_SkipHere(void);
void FIC_Events(void);
void FIC_NoEvents(void);
void FIC_OnKey(void);
void FIC_UnsetKey(void);
void FIC_If(void);
void FIC_IfNot(void);
void FIC_Else(void);
void FIC_GoTo(void);
void FIC_Marker(void);
void FIC_Image(void);
void FIC_ImageAt(void);
void FIC_XImage(void);
void FIC_Delete(void);
void FIC_Patch(void);
void FIC_SetPatch(void);
void FIC_Anim(void);
void FIC_AnimImage(void);
void FIC_StateAnim(void);
void FIC_Repeat(void);
void FIC_ClearAnim(void);
void FIC_PicSound(void);
void FIC_ObjectOffX(void);
void FIC_ObjectOffY(void);
void FIC_ObjectScaleX(void);
void FIC_ObjectScaleY(void);
void FIC_ObjectScale(void);
void FIC_ObjectScaleXY(void);
void FIC_ObjectRGB(void);
void FIC_ObjectAlpha(void);
void FIC_ObjectAngle(void);
void FIC_Rect(void);
void FIC_FillColor(void);
void FIC_EdgeColor(void);
void FIC_OffsetX(void);
void FIC_OffsetY(void);
void FIC_Sound(void);
void FIC_SoundAt(void);
void FIC_SeeSound(void);
void FIC_DieSound(void);
void FIC_Music(void);
void FIC_MusicOnce(void);
void FIC_Filter(void);
void FIC_Text(void);
void FIC_TextFromDef(void);
void FIC_TextFromLump(void);
void FIC_SetText(void);
void FIC_SetTextDef(void);
void FIC_DeleteText(void);
void FIC_FontA(void);
void FIC_FontB(void);
void FIC_TextColor(void);
void FIC_TextRGB(void);
void FIC_TextAlpha(void);
void FIC_TextOffX(void);
void FIC_TextOffY(void);
void FIC_TextCenter(void);
void FIC_TextNoCenter(void);
void FIC_TextScroll(void);
void FIC_TextPos(void);
void FIC_TextRate(void);
void FIC_TextLineHeight(void);
void FIC_NoMusic(void);
void FIC_TextScaleX(void);
void FIC_TextScaleY(void);
void FIC_TextScale(void);
void FIC_PlayDemo(void);
void FIC_Command(void);
void FIC_ShowMenu(void);
void FIC_NoShowMenu(void);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
boolean briefDisabled = false;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
boolean fiActive = false;
boolean fiCmdExecuted = false; // Set to true after first command.
// Time is measured in seconds.
// Colors are floating point and [0,1].
static ficmd_t fiCommands[] = {
// Run Control
{"DO", 0, FIC_Do, true, true},
{"END", 0, FIC_End},
{"IF", 1, FIC_If}, // if (value-id)
{"IFNOT", 1, FIC_IfNot}, // ifnot (value-id)
{"ELSE", 0, FIC_Else},
{"GOTO", 1, FIC_GoTo}, // goto (marker)
{"MARKER", 1, FIC_Marker, true},
{"in", 1, FIC_InTime}, // in (time)
{"pause", 0, FIC_Pause},
{"tic", 0, FIC_Tic},
{"wait", 1, FIC_Wait}, // wait (time)
{"waittext", 1, FIC_WaitText}, // waittext (handle)
{"waitanim", 1, FIC_WaitAnim}, // waitanim (handle)
{"canskip", 0, FIC_CanSkip},
{"noskip", 0, FIC_NoSkip},
{"skiphere", 0, FIC_SkipHere, true},
{"events", 0, FIC_Events},
{"noevents", 0, FIC_NoEvents},
{"onkey", 2, FIC_OnKey}, // onkey (keyname) (marker)
{"unsetkey", 1, FIC_UnsetKey}, // unsetkey (keyname)
// Screen Control
{"color", 3, FIC_Color}, // color (red) (green) (blue)
{"coloralpha", 4, FIC_ColorAlpha}, // coloralpha (r) (g) (b) (a)
{"flat", 1, FIC_BGFlat}, // flat (flat-id)
{"texture", 1, FIC_BGTexture}, // texture (texture-id)
{"noflat", 0, FIC_NoBGMaterial},
{"notexture", 0, FIC_NoBGMaterial},
{"offx", 1, FIC_OffsetX}, // offx (x)
{"offy", 1, FIC_OffsetY}, // offy (y)
{"filter", 4, FIC_Filter}, // filter (r) (g) (b) (a)
// Audio
{"sound", 1, FIC_Sound}, // sound (snd)
{"soundat", 2, FIC_SoundAt}, // soundat (snd) (vol:0-1)
{"seesound", 1, FIC_SeeSound}, // seesound (mobjtype)
{"diesound", 1, FIC_DieSound}, // diesound (mobjtype)
{"music", 1, FIC_Music}, // music (musicname)
{"musiconce", 1, FIC_MusicOnce}, // musiconce (musicname)
{"nomusic", 0, FIC_NoMusic},
// Objects
{"del", 1, FIC_Delete}, // del (handle)
{"x", 2, FIC_ObjectOffX}, // x (handle) (x)
{"y", 2, FIC_ObjectOffY}, // y (handle) (y)
{"sx", 2, FIC_ObjectScaleX}, // sx (handle) (x)
{"sy", 2, FIC_ObjectScaleY}, // sy (handle) (y)
{"scale", 2, FIC_ObjectScale}, // scale (handle) (factor)
{"scalexy", 3, FIC_ObjectScaleXY}, // scalexy (handle) (x) (y)
{"rgb", 4, FIC_ObjectRGB}, // rgb (handle) (r) (g) (b)
{"alpha", 2, FIC_ObjectAlpha}, // alpha (handle) (alpha)
{"angle", 2, FIC_ObjectAngle}, // angle (handle) (degrees)
// Rects
{"rect", 5, FIC_Rect}, // rect (hndl) (x) (y) (w) (h)
{"fillcolor", 6, FIC_FillColor}, // fillcolor (h) (top/bottom/both) (r) (g) (b) (a)
{"edgecolor", 6, FIC_EdgeColor}, // edgecolor (h) (top/bottom/both) (r) (g) (b) (a)
// Pics
{"image", 2, FIC_Image}, // image (handle) (raw-image-lump)
{"imageat", 4, FIC_ImageAt}, // imageat (handle) (x) (y) (raw)
{"ximage", 2, FIC_XImage}, // ximage (handle) (ext-gfx-filename)
{"patch", 4, FIC_Patch}, // patch (handle) (x) (y) (patch)
{"set", 2, FIC_SetPatch}, // set (handle) (lump)
{"clranim", 1, FIC_ClearAnim}, // clranim (handle)
{"anim", 3, FIC_Anim}, // anim (handle) (patch) (time)
{"imageanim", 3, FIC_AnimImage}, // imageanim (hndl) (raw-img) (time)
{"picsound", 2, FIC_PicSound}, // picsound (hndl) (sound)
{"repeat", 1, FIC_Repeat}, // repeat (handle)
{"states", 3, FIC_StateAnim}, // states (handle) (state) (count)
// Text
{"text", 4, FIC_Text}, // text (hndl) (x) (y) (string)
{"textdef", 4, FIC_TextFromDef}, // textdef (hndl) (x) (y) (txt-id)
{"textlump", 4, FIC_TextFromLump}, // textlump (hndl) (x) (y) (lump)
{"settext", 2, FIC_SetText}, // settext (handle) (newtext)
{"settextdef", 2, FIC_SetTextDef}, // settextdef (handle) (txt-id)
{"precolor", 4, FIC_TextColor}, // precolor (num) (r) (g) (b)
{"center", 1, FIC_TextCenter}, // center (handle)
{"nocenter", 1, FIC_TextNoCenter}, // nocenter (handle)
{"scroll", 2, FIC_TextScroll}, // scroll (handle) (speed)
{"pos", 2, FIC_TextPos}, // pos (handle) (pos)
{"rate", 2, FIC_TextRate}, // rate (handle) (rate)
{"fonta", 1, FIC_FontA}, // fonta (handle)
{"fontb", 1, FIC_FontB}, // fontb (handle)
{"linehgt", 2, FIC_TextLineHeight}, // linehgt (hndl) (hgt)
// Game Control
{"playdemo", 1, FIC_PlayDemo}, // playdemo (filename)
{"cmd", 1, FIC_Command}, // cmd (console command)
{"trigger", 0, FIC_ShowMenu},
{"notrigger", 0, FIC_NoShowMenu},
// Deprecated Pic commands
{"delpic", 1, FIC_Delete}, // delpic (handle)
// Deprecated Text commands
{"deltext", 1, FIC_DeleteText}, // deltext (hndl)
{"textrgb", 4, FIC_TextRGB}, // textrgb (handle) (r) (g) (b)
{"textalpha", 2, FIC_TextAlpha}, // textalpha (handle) (alpha)
{"tx", 2, FIC_TextOffX}, // tx (handle) (x)
{"ty", 2, FIC_TextOffY}, // ty (handle) (y)
{"tsx", 2, FIC_TextScaleX}, // tsx (handle) (x)
{"tsy", 2, FIC_TextScaleY}, // tsy (handle) (y)
{"textscale", 3, FIC_TextScale}, // textscale (handle) (x) (y)
{NULL, 0, NULL}
};
static fistate_t fiStateStack[STACK_SIZE];
static fistate_t* fi; // Pointer to the current state in the stack.
static char fiToken[MAX_TOKEN_LEN];
static fipic_t fiDummyPic;
static fitext_t fiDummyText;
static boolean conditionPresets[NUM_FICONDS];
// Cvars for finales:
cvar_t fiCVars[] = {
{"finale-nostretch", 0, CVT_BYTE, &cfg.fiNoStretch, 0, 1},
{NULL}
};
// CODE --------------------------------------------------------------------
/**
* Called during pre-init to register cvars and ccmds for the finale system.
*/
void FI_Register(void)
{
int i;
for(i = 0; fiCVars[i].name; ++i)
Con_AddVariable(fiCVars + i);
}
/**
* Clear the InFine state to the default, blank state.
* The 'fi' pointer must be set before calling this function.
*/
void FI_ClearState(void)
{
int i, c;
// General game state.
G_SetGameAction(GA_NONE);
if(fi->mode != FIMODE_OVERLAY)
{
G_ChangeGameState(GS_INFINE);
// Close the automap for all local players.
for(i = 0; i < MAXPLAYERS; ++i)
AM_Open(AM_MapForPlayer(i), false, true);
}
fiActive = true;
fiCmdExecuted = false; // Nothing is drawn until a cmd has been executed.
fi->suspended = false;
fi->timer = 0;
fi->canSkip = true; // By default skipping is enabled.
fi->skipping = false;
fi->wait = 0; // Not waiting for anything.
fi->inTime = 0; // Interpolation is off.
fi->bgMaterial = NULL; // No background material.
fi->paused = false;
fi->gotoSkip = false;
fi->skipNext = false;
fi->waitingText = NULL;
fi->waitingPic = NULL;
memset(fi->gotoTarget, 0, sizeof(fi->gotoTarget));
GL_SetFilter(false); // Clear the current filter.
for(i = 0; i < 4; ++i)
{
FI_InitValue(fi->bgColor + i, 1);
}
memset(fi->pics, 0, sizeof(fi->pics));
memset(fi->imgOffset, 0, sizeof(fi->imgOffset));
memset(fi->text, 0, sizeof(fi->text));
memset(fi->filter, 0, sizeof(fi->filter));
for(i = 0; i < 9; ++i)
{
for(c = 0; c < 3; ++c)
FI_InitValue(&fi->textColor[i][c], 1);
}
}
void FI_NewState(const char *script)
{
int size;
if(!fi)
{
// Start from the bottom of the stack.
fi = fiStateStack;
}
else
{
// Get the next state from the stack.
fi++;
if(fi == fiStateStack + STACK_SIZE)
{
Con_Error("FI_NewState: InFine state stack overflow.\n");
}
}
#ifdef _DEBUG
// Is the stack leaking?
Con_Printf("FI_NewState: Assigned index %i.\n", fi - fiStateStack);
#endif
memset(fi, 0, sizeof(*fi));
// Take a copy of the script.
size = strlen(script);
fi->script = Z_Malloc(size + 1, PU_STATIC, 0);
memcpy(fi->script, script, size);
fi->script[size] = '\0';
// Init the cursor, too.
fi->cp = fi->script;
}
void FI_DeleteXImage(fipic_t *pic)
{
DGL_DeleteTextures(1, (DGLuint*)&pic->lump[0]);
pic->lump[0] = 0;
pic->flags.is_ximage = false;
}
void FI_PopState(void)
{
int i;
#ifdef _DEBUG
Con_Printf("FI_PopState: fi=%p (%i)\n", fi, fi - fiStateStack);
#endif
if(!fi)
{
#ifdef _DEBUG
Con_Printf("FI_PopState: Pop in NULL state!\n");
#endif
return;
}
// Free the current state.
Z_Free(fi->script);
// Free all text strings.
for(i = 0; i < MAX_TEXT; ++i)
{
if(fi->text[i].text)
{
Z_Free(fi->text[i].text);
}
}
// Delete external images.
for(i = 0; i < MAX_PICS; ++i)
{
if(fi->pics[i].flags.is_ximage)
{
FI_DeleteXImage(fi->pics + i);
}
}
memset(fi, 0, sizeof(*fi));
// Should we go back to NULL?
if(fi == fiStateStack)
{
fi = NULL;
fiActive = false;
}
else
{
// Return to previous state.
fi--;
}
}
/**
* Reset the entire InFine state stack. This is called when a new game
* is started.
*/
void FI_Reset(void)
{
/* if(IS_CLIENT)
{
#ifdef _DEBUG
Con_Printf("FI_Reset: Called in client mode! (not reseting)\n");
#endif
return;
} */
// The state is suspended when the PlayDemo command is used.
// Being suspended means that InFine is currently not active, but
// will be restored at a later time.
if(fi && fi->suspended)
return;
// Pop all the states.
while(fi)
FI_PopState();
fiActive = false;
G_ChangeGameState(GS_WAITING);
}
/**
* Start playing the given script.
*/
void FI_Start(char *finalescript, infinemode_t mode)
{
int i;
if(mode == FIMODE_LOCAL && IS_DEDICATED)
{
// Dedicated servers don't play local scripts.
#ifdef _DEBUG
Con_Printf("FI_Start: No local scripts in dedicated mode.\n");
#endif
return;
}
#ifdef _DEBUG
Con_Printf("FI_Start: mode=%i '%.30s'\n", mode, finalescript);
#endif
// Init InFine state.
FI_NewState(finalescript);
fi->mode = mode;
fi->lastGameState = G_GetGameState();
fi->showMenu = true; // Enabled by default.
// Clear the message queue for all local players.
for(i = 0; i < MAXPLAYERS; ++i)
Hu_LogEmpty(i);
FI_ClearState();
if(!IS_CLIENT)
{
// We are able to figure out the truth values of all the
// conditions.
#if __JHEXEN__
fi->conditions[FICOND_SECRET] = false;
// Current hub has been completed?
fi->conditions[FICOND_LEAVEHUB] =
(P_GetMapCluster(gameMap) != P_GetMapCluster(nextMap));
#else
fi->conditions[FICOND_SECRET] = secretExit;
// Only Hexen has hubs.
fi->conditions[FICOND_LEAVEHUB] = false;
#endif
}
else
{
// Clients use the server-provided presets. We may not have
// enough info to figure out the real values otherwise.
for(i = 0; i < NUM_FICONDS; ++i)
{
fi->conditions[i] = conditionPresets[i];
}
}
if(mode == FIMODE_OVERLAY)
{
// Overlay scripts stop when the gameMode changes.
fi->overlayGameState = G_GetGameState();
}
if(mode != FIMODE_LOCAL)
{
// Tell clients to start this script.
NetSv_Finale(FINF_BEGIN |
(mode == FIMODE_AFTER ? FINF_AFTER : mode ==
FIMODE_OVERLAY ? FINF_OVERLAY : 0), finalescript,
fi->conditions, NUM_FICONDS);
}
memset(&fiDummyText, 0, sizeof(fiDummyText));
}
/**
* Stop playing the script and go to next game state.
*/
void FI_End(void)
{
int oldMode;
gamestate_t lastGameState;
if(!fiActive || !fi->canSkip)
return;
oldMode = fi->mode;
lastGameState = fi->lastGameState;
// This'll set fi to NULL.
FI_PopState();
#ifdef _DEBUG
Con_Printf("FI_End\n");
#endif
if(oldMode != FIMODE_LOCAL)
{
// Tell clients to stop the finale.
NetSv_Finale(FINF_END, 0, NULL, 0);
}
if(fiActive)
return;
/**
* No more scripts are left.
*/
// Return to the last game state?
if(oldMode == FIMODE_LOCAL)
{
G_ChangeGameState(lastGameState);
return;
}
// Go to the next game mode?
if(oldMode == FIMODE_AFTER) // A map has been completed.
{
if(IS_CLIENT)
{
#if __JHEXEN__ || __JSTRIFE__
Draw_TeleportIcon();
#endif
return;
}
G_SetGameAction(GA_MAPCOMPLETED);
// Don't play the debriefing again.
briefDisabled = true;
}
else if(oldMode == FIMODE_BEFORE)
{
// Enter the map, this was a briefing.
G_ChangeGameState(GS_MAP);
S_MapMusic(gameEpisode, gameMap);
mapStartTic = (int) GAMETIC;
mapTime = actualMapTime = 0;
}
}
/**
* Set the truth value of a condition. Used by clients after they've
* received a GPT_FINALE2 packet.
*/
void FI_SetCondition(int index, boolean value)
{
if(index < 0 || index >= NUM_FICONDS)
return;
conditionPresets[index] = value;
#ifdef _DEBUG
Con_Printf("FI_SetCondition: %i = %s\n", index, value ? "true" : "false");
#endif
}
DEFCC(CCmdStartInFine)
{
char *script;
if(fiActive)
return false;
if(!Def_Get(DD_DEF_FINALE, argv[1], &script))
{
Con_Printf("Script \"%s\" is not defined.\n", argv[1]);
return false;
}
// The overlay mode doesn't affect the current game mode.
FI_Start(script, (G_GetGameState() == GS_MAP? FIMODE_OVERLAY : FIMODE_LOCAL));
return true;
}
DEFCC(CCmdStopInFine)
{
if(!fiActive)
return false;
fi->canSkip = true;
FI_End();
return true;
}
/**
* Check if there is a finale before the map.
* Returns true if a finale was found.
*/
int FI_Briefing(uint episode, uint map, ddfinale_t* fin)
{
char mid[20];
// If we're already in the INFINE state, don't start a finale.
if(briefDisabled || G_GetGameState() == GS_INFINE || IS_CLIENT ||
Get(DD_PLAYBACK))
return false;
// Is there such a finale definition?
P_GetMapLumpName(episode, map, mid);
return Def_Get(DD_DEF_FINALE_BEFORE, mid, fin);
}
/**
* Check if there is a finale after the map.
* Returns true if a finale was found.
*/
int FI_Debriefing(uint episode, uint map, ddfinale_t* fin)
{
char mid[20];
// If we're already in the INFINE state, don't start a finale.
if(briefDisabled)
return false;
#if __JHEXEN__
if(cfg.overrideHubMsg && G_GetGameState() == GS_MAP &&
!(nextMap == DDMAXINT && nextMapEntryPoint == DDMAXINT) &&
P_GetMapCluster(map) != P_GetMapCluster(nextMap))
return false;
#endif
if(G_GetGameState() == GS_INFINE || IS_CLIENT || Get(DD_PLAYBACK))
return false;
// Is there such a finale definition?
P_GetMapLumpName(episode, map, mid);
return Def_Get(DD_DEF_FINALE_AFTER, mid, fin);
}
void FI_DemoEnds(void)
{
if(fi && fi->suspended)
{
int i;
// Restore the InFine state.
fi->suspended = false;
fiActive = true;
G_ChangeGameState(GS_INFINE);
G_SetGameAction(GA_NONE);
for(i = 0; i < MAXPLAYERS; ++i)
AM_Open(AM_MapForPlayer(i), false, true);
}
}
char* FI_GetToken(void)
{
char* out;
if(!fi)
return NULL;
// Skip whitespace.
while(*fi->cp && isspace(*fi->cp))
fi->cp++;
if(!*fi->cp)
return NULL; // The end has been reached.
out = fiToken;
if(*fi->cp == '"') // A string?
{
for(fi->cp++; *fi->cp; fi->cp++)
{
if(*fi->cp == '"')
{
fi->cp++;
// Convert double quotes to single ones.
if(*fi->cp != '"')
break;
}
*out++ = *fi->cp;
}
}
else
{
while(!isspace(*fi->cp) && *fi->cp)
*out++ = *fi->cp++;
}
*out++ = 0;
return fiToken;
}
int FI_GetInteger(void)
{
return strtol(FI_GetToken(), NULL, 0);
}
float FI_GetFloat(void)
{
return strtod(FI_GetToken(), NULL);
}
/**
* Reads the next token, which should be floating point number. It is
* considered seconds, and converted to tics.
*/
int FI_GetTics(void)
{
return (int) (FI_GetFloat() * 35 + 0.5);
}
/**
* Execute one (the next) command, advance script cursor.
*/
void FI_Execute(char * cmd)
{
int i, k;
char* oldcp;
// Semicolon terminates DO-blocks.
if(!strcmp(cmd, ";"))
{
if(fi->doLevel > 0)
{
if(--fi->doLevel == 0)
{
// The DO-skip has been completed.
fi->skipNext = false;
fi->lastSkipped = true;
}
}
return;
}
// We're now going to execute a command.
fiCmdExecuted = true;
// Is this a command we know how to execute?
for(i = 0; fiCommands[i].token; ++i)
{
if(!stricmp(cmd, fiCommands[i].token))
{
// Check that there are enough operands.
// k stays at zero if the number of operands is correct.
oldcp = fi->cp;
for(k = fiCommands[i].operands; k > 0; k--)
{
if(!FI_GetToken())
{
fi->cp = oldcp;
Con_Message("FI_Execute: \"%s\" has too few operands.\n",
fiCommands[i].token);
break;
}
}
// Should we skip this command?
if((fi->skipNext && !fiCommands[i].whenCondSkipping) ||
((fi->skipping || fi->gotoSkip) &&
!fiCommands[i].whenSkipping))
{
// While not DO-skipping, the condskip has now been done.
if(!fi->doLevel)
{
if(fi->skipNext)
fi->lastSkipped = true;
fi->skipNext = false;
}
return;
}
// If there were enough operands, execute the command.
fi->cp = oldcp;
if(!k)
fiCommands[i].func();
// The END command may clear the current state.
if(!fi)
return;
// Now we've executed the latest command.
fi->lastSkipped = false;
return;
}
}
// The command was not found!
Con_Message("FI_Execute: Unknown command \"%s\".\n", cmd);
}
/**
* @return @c true, if a command was found.
* @c false if there are no more commands in the script.
*/
int FI_ExecuteNextCommand(void)
{
char* cmd = FI_GetToken();
if(!cmd)
return false;
FI_Execute(cmd);
return true;
}
void FI_InitValue(fivalue_t* val, float num)
{
val->target = val->value = num;
val->steps = 0;
}
void FI_SetValue(fivalue_t* val, float num)
{
val->target = num;
val->steps = fi->inTime;
if(!val->steps)
val->value = val->target;
}
void FI_ValueThink(fivalue_t* val)