/
fi_lib.c
655 lines (568 loc) · 17.7 KB
/
fi_lib.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
/** @file fi_lib.c InFine Helper routines and LIFO "script stack".
*
* @authors Copyright © 2006-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include <string.h>
#include <assert.h>
#include "common.h"
#include "p_sound.h"
#include "p_tick.h"
#include "hu_log.h"
#include "hu_stuff.h"
#include "am_map.h"
#include "g_common.h"
#include "r_common.h"
#include "d_net.h"
#include "fi_lib.h"
typedef struct {
byte secret:1;
byte leave_hub:1;
} fi_state_conditions_t;
typedef struct {
finaleid_t finaleId;
finale_mode_t mode;
fi_state_conditions_t conditions;
/// Gamestate before the finale began.
gamestate_t initialGamestate;
/**
* Optionally the ID of the source script definition. A new script is
* not started if its definition ID matches one already on the stack.
* @note Maximum ID length defined in the DED Reader implementation.
*/
char defId[64];
} fi_state_t;
D_CMD(StartFinale);
D_CMD(StopFinale);
int Hook_FinaleScriptStop(int hookType, int finaleId, void* paramaters);
int Hook_FinaleScriptTicker(int hookType, int finalId, void* paramaters);
int Hook_FinaleScriptEvalIf(int hookType, int finaleId, void* paramaters);
/// Script state stack.
static dd_bool finaleStackInited = false;
static uint finaleStackSize = 0;
static fi_state_t* finaleStack = 0;
static fi_state_t remoteFinaleState; // For the client.
// Console commands for this library:
static ccmdtemplate_t ccmds[] = {
{ "startfinale", "s", CCmdStartFinale },
{ "startinf", "s", CCmdStartFinale },
{ "stopfinale", "", CCmdStopFinale },
{ "stopinf", "", CCmdStopFinale },
{ NULL }
};
void FI_StackRegister(void)
{
int i;
for(i = 0; ccmds[i].name; ++i)
Con_AddCommand(ccmds + i);
}
static void initStateConditions(fi_state_t *s)
{
// Set the presets.
s->conditions.secret = false;
#if !__JHEXEN__
s->conditions.leave_hub = false;
#endif
// Only the server is able to figure out the truth values of all the conditions.
if(IS_CLIENT) return;
#if __JHEXEN__
s->conditions.secret = false;
#else
s->conditions.secret = secretExit;
#endif
#if __JHEXEN__
// Leaving the current cluster?
{
Uri *nextMapUri = G_ComposeMapUri(gameEpisode, nextMap);
mapinfo_t *curMapInfo = P_CurrentMapInfo();
mapinfo_t *nextMapInfo = P_MapInfo(nextMapUri);
if(curMapInfo && nextMapInfo)
{
s->conditions.leave_hub = (curMapInfo->cluster != nextMapInfo->cluster);
}
Uri_Delete(nextMapUri);
}
App_Log(DE2_DEV_SCR_VERBOSE, "Infine state condition: leave_hub=%i", s->conditions.leave_hub);
#endif
}
static fi_state_t *stateForFinaleId(finaleid_t id)
{
if(finaleStackInited)
{
uint i;
for(i = 0; i < finaleStackSize; ++i)
{
fi_state_t *s = &finaleStack[i];
if(s->finaleId == id)
return s;
}
}
if(IS_CLIENT)
{
if(remoteFinaleState.finaleId)
{
App_Log(DE2_DEV_SCR_XVERBOSE,
"stateForFinaleId: Finale %i is remote, using server's state (id %i)",
id, remoteFinaleState.finaleId);
return &remoteFinaleState;
}
}
return 0;
}
static dd_bool stackHasDefId(const char* defId)
{
uint i;
for(i = 0; i < finaleStackSize; ++i)
{
fi_state_t* s = &finaleStack[i];
if(!stricmp(s->defId, defId))
return true;
}
return false;
}
static __inline fi_state_t* stackTop(void)
{
return (finaleStackSize == 0? 0 : &finaleStack[finaleStackSize-1]);
}
static fi_state_t* stackPush(finaleid_t finaleId, finale_mode_t mode, gamestate_t prevGamestate,
const char* defId)
{
fi_state_t* s;
finaleStack = Z_Realloc(finaleStack, sizeof(*finaleStack) * ++finaleStackSize, PU_GAMESTATIC);
s = &finaleStack[finaleStackSize-1];
s->finaleId = finaleId;
s->mode = mode;
s->initialGamestate = prevGamestate;
if(defId)
{
(void) strncpy(s->defId, defId, sizeof(s->defId) - 1);
s->defId[sizeof(s->defId) - 1] = 0; // terminate
}
else
{
// Source ID not provided.
memset(s->defId, 0, sizeof(s->defId));
}
initStateConditions(s);
return s;
}
static void NetSv_SendFinaleState(fi_state_t* s)
{
Writer* writer = D_NetWrite();
// First the flags.
Writer_WriteByte(writer, s->mode);
Writer_WriteUInt32(writer, s->finaleId);
// Then the conditions.
Writer_WriteByte(writer, 2); // Number of conditions.
Writer_WriteByte(writer, s->conditions.secret);
Writer_WriteByte(writer, s->conditions.leave_hub);
Net_SendPacket(DDSP_ALL_PLAYERS, GPT_FINALE_STATE, Writer_Data(writer), Writer_Size(writer));
}
void NetCl_UpdateFinaleState(Reader* msg)
{
int i, numConds;
fi_state_t* s = &remoteFinaleState;
// Flags.
s->mode = Reader_ReadByte(msg);
s->finaleId = Reader_ReadUInt32(msg); // serverside id (local is different)
// Conditions.
numConds = Reader_ReadByte(msg);
for(i = 0; i < numConds; ++i)
{
byte cond = Reader_ReadByte(msg);
if(i == 0) s->conditions.secret = cond;
if(i == 1) s->conditions.leave_hub = cond;
}
App_Log(DE2_DEV_SCR_MSG,
"NetCl_FinaleState: Updated finale %i: mode %i, secret=%i, leave_hud=%i",
s->finaleId, s->mode, s->conditions.secret, s->conditions.leave_hub);
}
void FI_StackInit(void)
{
if(finaleStackInited) return;
finaleStack = 0; finaleStackSize = 0;
Plug_AddHook(HOOK_FINALE_SCRIPT_STOP, Hook_FinaleScriptStop);
Plug_AddHook(HOOK_FINALE_SCRIPT_TICKER, Hook_FinaleScriptTicker);
Plug_AddHook(HOOK_FINALE_EVAL_IF, Hook_FinaleScriptEvalIf);
finaleStackInited = true;
}
void FI_StackShutdown(void)
{
if(!finaleStackInited) return;
// Terminate all scripts on the stack.
FI_StackClearAll();
if(finaleStack)
Z_Free(finaleStack);
finaleStack = 0; finaleStackSize = 0;
Plug_RemoveHook(HOOK_FINALE_SCRIPT_STOP, Hook_FinaleScriptStop);
Plug_RemoveHook(HOOK_FINALE_SCRIPT_TICKER, Hook_FinaleScriptTicker);
Plug_RemoveHook(HOOK_FINALE_EVAL_IF, Hook_FinaleScriptEvalIf);
finaleStackInited = false;
}
void FI_StackExecute(const char* scriptSrc, int flags, finale_mode_t mode)
{
FI_StackExecuteWithId(scriptSrc, flags, mode, NULL);
}
void FI_StackExecuteWithId(const char* scriptSrc, int flags, finale_mode_t mode, const char* defId)
{
fi_state_t* s, *prevTopScript;
gamestate_t prevGamestate;
ddstring_t setupCmds;
finaleid_t finaleId;
int i, fontIdx;
DENG_ASSERT(finaleStackInited);
// Should we ignore this?
if(defId && stackHasDefId(defId))
{
App_Log(DE2_SCR_NOTE, "Finale ID \"%s\" is already running, won't execute again", defId);
return;
}
prevGamestate = G_GameState();
prevTopScript = stackTop();
// Configure the predefined fonts.
Str_Init(&setupCmds);
fontIdx = 1;
Str_Appendf(&setupCmds, "prefont %i %s", fontIdx++, "a");
Str_Appendf(&setupCmds, "\nprefont %i %s", fontIdx++, "b");
Str_Appendf(&setupCmds, "\nprefont %i %s", fontIdx++, "status");
#if __JDOOM__
Str_Appendf(&setupCmds, "\nprefont %i %s", fontIdx++, "index");
#endif
#if __JDOOM__ || __JDOOM64__
Str_Appendf(&setupCmds, "\nprefont %i %s", fontIdx++, "small");
#endif
#if __JHERETIC__ || __JHEXEN__
Str_Appendf(&setupCmds, "\nprefont %i %s", fontIdx++, "smallin");
#endif
// Configure the predefined colors.
#if __JDOOM__
Str_Appendf(&setupCmds, "\nprecolor 2 %f %f %f\n", defFontRGB[CR], defFontRGB[CG], defFontRGB[CB]);
Str_Appendf(&setupCmds, "\nprecolor 1 %f %f %f\n", defFontRGB2[CR], defFontRGB2[CG], defFontRGB2[CB]);
Str_Appendf(&setupCmds, "\nprecolor 3 %f %f %f\n", defFontRGB3[CR], defFontRGB3[CG], defFontRGB3[CB]);
i = 4;
#elif __JHERETIC__
Str_Appendf(&setupCmds, "\nprecolor 3 %f %f %f\n", defFontRGB[CR], defFontRGB[CG], defFontRGB[CB]);
Str_Appendf(&setupCmds, "\nprecolor 2 %f %f %f\n", defFontRGB2[CR], defFontRGB2[CG], defFontRGB2[CB]);
Str_Appendf(&setupCmds, "\nprecolor 1 %f %f %f\n", defFontRGB3[CR], defFontRGB3[CG], defFontRGB3[CB]);
i = 4;
#elif __JHEXEN__
Str_Appendf(&setupCmds, "\nprecolor 3 %f %f %f\n", defFontRGB[CR], defFontRGB[CG], defFontRGB[CB]);
Str_Appendf(&setupCmds, "\nprecolor 2 %f %f %f\n", defFontRGB2[CR], defFontRGB2[CG], defFontRGB2[CB]);
Str_Appendf(&setupCmds, "\nprecolor 1 %f %f %f\n", defFontRGB3[CR], defFontRGB3[CG], defFontRGB3[CB]);
i = 4;
#elif __JDOOM64__
Str_Appendf(&setupCmds, "\nprecolor 2 %f %f %f\n", defFontRGB[CR], defFontRGB[CG], defFontRGB[CB]);
Str_Appendf(&setupCmds, "\nprecolor 1 %f %f %f\n", defFontRGB2[CR], defFontRGB2[CG], defFontRGB2[CB]);
i = 3;
#else
i = 0;
#endif
// Set the rest to white.
for(; i <= FIPAGE_NUM_PREDEFINED_COLORS; ++i)
{
Str_Appendf(&setupCmds, "\nprecolor %i 1 1 1\n", i);
}
finaleId = FI_Execute2(scriptSrc, flags, Str_Text(&setupCmds));
Str_Free(&setupCmds);
if(finaleId == 0)
return;
if(mode != FIMODE_OVERLAY)
{
G_ChangeGameState(GS_INFINE);
}
// Only the top-most script can be "active".
if(prevTopScript)
{
FI_ScriptSuspend(prevTopScript->finaleId);
}
s = stackPush(finaleId, mode, prevGamestate, defId);
// Do we need to transmit the state conditions to clients?
if(IS_SERVER && !(flags & FF_LOCAL))
{
NetSv_SendFinaleState(s);
}
}
dd_bool FI_StackActive(void)
{
fi_state_t* s;
if(!finaleStackInited) Con_Error("FI_StackActive: Not initialized yet!");
if((s = stackTop()))
{
return FI_ScriptActive(s->finaleId);
}
return false;
}
static void stackClear(dd_bool ignoreSuspendedScripts)
{
fi_state_t* s;
assert(finaleStackInited);
if((s = stackTop()) && FI_ScriptActive(s->finaleId))
{
// 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(ignoreSuspendedScripts && FI_ScriptSuspended(s->finaleId))
return;
// Pop all the states.
while((s = stackTop()))
{
FI_ScriptTerminate(s->finaleId);
}
}
}
void FI_StackClear(void)
{
if(!finaleStackInited) Con_Error("FI_StackClear: Not initialized yet!");
stackClear(true);
}
void FI_StackClearAll(void)
{
if(!finaleStackInited) Con_Error("FI_StackClearAll: Not initialized yet!");
stackClear(false);
}
int Hook_FinaleScriptStop(int hookType, int finaleId, void* parameters)
{
gamestate_t initialGamestate;
finale_mode_t mode;
fi_state_t* s = stateForFinaleId(finaleId);
DENG_UNUSED(hookType);
DENG_UNUSED(parameters);
if(IS_CLIENT && s == &remoteFinaleState)
{
App_Log(DE2_DEV_SCR_MSG, "Hook_FinaleScriptStop: Clientside script stopped, clearing remote state");
memset(&remoteFinaleState, 0, sizeof(remoteFinaleState));
return true;
}
if(!s)
{
// Finale was not initiated by us...
return true;
}
initialGamestate = s->initialGamestate;
mode = s->mode;
// Should we go back to NULL?
if(finaleStackSize > 1)
{ // Resume the next script on the stack.
finaleStack = Z_Realloc(finaleStack, sizeof(*finaleStack) * --finaleStackSize, PU_GAMESTATIC);
FI_ScriptResume(stackTop()->finaleId);
return true;
}
/**
* No more scripts are left.
*/
Z_Free(finaleStack); finaleStack = 0;
finaleStackSize = 0;
// Return to the previous game state?
if(FI_ScriptFlags(finaleId) & FF_LOCAL)
{
G_ChangeGameState(initialGamestate);
return true;
}
// Go to the next game mode?
if(mode == FIMODE_AFTER) // A map has been completed.
{
if(IS_CLIENT) return true;
G_SetGameAction(GA_ENDDEBRIEFING);
}
else if(mode == FIMODE_BEFORE) // A briefing has ended.
{
// Its time to start the map; que music and begin!
S_MapMusic(0/*current map*/);
HU_WakeWidgets(-1 /* all players */);
G_BeginMap();
Pause_End(); // skip forced period
}
return true;
}
int Hook_FinaleScriptTicker(int hookType, int finaleId, void* paramaters)
{
ddhook_finale_script_ticker_paramaters_t* p = (ddhook_finale_script_ticker_paramaters_t*) paramaters;
gamestate_t gamestate = G_GameState();
fi_state_t* s = stateForFinaleId(finaleId);
if(!s || IS_CLIENT)
{
// Finale was not initiated by us, leave it alone.
return true;
}
/**
* Once the game state changes we suspend ticking of InFine scripts.
* Additionally, in overlay mode we stop the script if its skippable.
*
* Is this really the best place to handle this?
*/
if(gamestate != GS_INFINE && s->initialGamestate != gamestate)
{
// Overlay scripts don't survive this...
if(s->mode == FIMODE_OVERLAY && p->canSkip)
{
FI_ScriptTerminate(s->finaleId);
}
p->runTick = false;
}
return true;
}
#if __JHEXEN__
static int playerClassForName(const char* name)
{
if(name && name[0])
{
if(!stricmp(name, "fighter"))
return PCLASS_FIGHTER;
if(!stricmp(name, "cleric"))
return PCLASS_CLERIC;
if(!stricmp(name, "mage"))
return PCLASS_MAGE;
}
return PCLASS_NONE;
}
#endif
int Hook_FinaleScriptEvalIf(int hookType, int finaleId, void* paramaters)
{
ddhook_finale_script_evalif_paramaters_t* p = (ddhook_finale_script_evalif_paramaters_t*) paramaters;
fi_state_t* s = stateForFinaleId(finaleId);
#if __JHEXEN__
// Player class names.
int pclass;
#endif
if(!s)
{ // Finale was not initiated by us, therefore we have no say in this.
return false;
}
if(!stricmp(p->token, "secret"))
{ // Secret exit was used?
p->returnVal = s->conditions.secret;
return true;
}
if(!stricmp(p->token, "deathmatch"))
{
p->returnVal = (gameRules.deathmatch != false);
return true;
}
if(!stricmp(p->token, "leavehub"))
{ // Current hub has been completed?
p->returnVal = s->conditions.leave_hub;
return true;
}
#if __JHEXEN__
// Player class names.
if((pclass = playerClassForName(p->token)) != PCLASS_NONE)
{
if(IS_DEDICATED)
{
// Always false; no local players on the server.
p->returnVal = false;
}
else
{
p->returnVal = (cfg.playerClass[CONSOLEPLAYER] == pclass);
}
return true;
}
#endif
/**
* Game modes.
* \todo dj: the following conditions should be moved into the engine.
*/
if(!stricmp(p->token, "shareware"))
{
#if __JDOOM__
p->returnVal = ((gameMode == doom_shareware) != false);
#elif __JHERETIC__
p->returnVal = ((gameMode == heretic_shareware) != false);
/*#elif __JHEXEN__
p->returnVal = ((gameMode == hexen_demo) != false);*/
#else
p->returnVal = false;
#endif
return true;
}
#if __JDOOM__
if(!stricmp(p->token, "ultimate"))
{
p->returnVal = (gameMode == doom_ultimate);
return true;
}
if(!stricmp(p->token, "commercial"))
{
p->returnVal = (gameModeBits & GM_ANY_DOOM2) != 0;
return true;
}
#endif
return false;
}
int FI_PrivilegedResponder(const void* ev)
{
fi_state_t* s;
if(!finaleStackInited) return false;
if(IS_CLIENT && DD_GetInteger(DD_CURRENT_CLIENT_FINALE_ID))
{
return FI_ScriptResponder(DD_GetInteger(DD_CURRENT_CLIENT_FINALE_ID), ev);
}
if((s = stackTop()))
{
return FI_ScriptResponder(s->finaleId, ev);
}
return false;
}
dd_bool FI_IsMenuTrigger(void)
{
fi_state_t* s;
if(!finaleStackInited) Con_Error("FI_IsMenuTrigger: Not initialized yet!");
if((s = stackTop()))
{
return FI_ScriptIsMenuTrigger(s->finaleId);
}
return false;
}
dd_bool FI_RequestSkip(void)
{
fi_state_t* s;
if(!finaleStackInited) Con_Error("FI_RequestSkip: Not initialized yet!");
if((s = stackTop()))
{
return FI_ScriptRequestSkip(s->finaleId);
}
return false;
}
D_CMD(StartFinale)
{
ddfinale_t fin;
// Only one active overlay allowed.
if(FI_StackActive())
return false;
if(!Def_Get(DD_DEF_FINALE, argv[1], &fin))
{
App_Log(DE2_SCR_ERROR, "Script '%s' is not defined.\n", argv[1]);
return false;
}
G_SetGameAction(GA_NONE);
FI_StackExecute(fin.script, FF_LOCAL, FIMODE_OVERLAY);
return true;
}
D_CMD(StopFinale)
{
fi_state_t* s;
if(!FI_StackActive())
return false;
// Only 'overlays' can be explictly stopped this way.
if((s = stackTop()) && s->mode == FIMODE_OVERLAY)
{
FI_ScriptTerminate(s->finaleId);
return true;
}
return false;
}