/
g_game.cpp
3095 lines (2667 loc) · 93.6 KB
/
g_game.cpp
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 g_game.c Top-level (common) game routines.
*
* @authors Copyright © 1999-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Activision
* @authors Copyright © 1993-1996 id Software, Inc.
*
* @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 "common.h"
#include "g_common.h"
#include <cctype>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <de/App>
#include <de/CommandLine>
#include <de/LogBuffer>
#include <de/NativePath>
#include <de/RecordValue>
#include <doomsday/DoomsdayApp>
#include <doomsday/defs/episode.h>
#include <doomsday/defs/mapinfo.h>
#include <doomsday/busymode.h>
#include <doomsday/uri.h>
#include "acs/system.h"
#include "animdefs.h"
#include "d_net.h"
#include "d_netcl.h"
#include "d_netsv.h"
#include "dmu_lib.h"
#include "fi_lib.h"
#include "g_controls.h"
#include "g_defs.h"
#include "g_eventsequence.h"
#include "g_update.h"
#include "gamesession.h"
#include "hu_lib.h"
#include "hu_inventory.h"
#include "hu_menu.h"
#include "hu_msg.h"
#include "hu_pspr.h"
#include "hu_stuff.h"
#include "p_actor.h"
#include "p_inventory.h"
#include "p_map.h"
#include "p_mapsetup.h"
#include "p_mapspec.h"
#include "p_savedef.h"
#include "p_saveio.h"
#include "p_saveg.h"
#include "p_sound.h"
#include "p_start.h"
#include "p_tick.h"
#include "p_user.h"
#include "player.h"
#include "r_common.h"
#include "r_special.h"
#include "saveslots.h"
#include "x_hair.h"
#include "menu/widgets/widget.h"
using namespace de;
using namespace common;
void R_LoadVectorGraphics();
int Hook_DemoStop(int hookType, int val, void *parm);
game_config_t cfg; // The global cfg.
#if __JDOOM__ || __JDOOM64__
#define BODYQUEUESIZE (32)
mobj_t *bodyQueue[BODYQUEUESIZE];
int bodyQueueSlot;
#endif
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
bool secretExit;
#endif
dd_bool monsterInfight;
player_t players[MAXPLAYERS];
int totalKills, totalItems, totalSecret; // For intermission.
dd_bool singledemo; // Quit after playing a demo from cmdline.
dd_bool briefDisabled;
dd_bool precache = true; // If @c true, load all graphics at start.
dd_bool customPal; // If @c true, a non-IWAD palette is in use.
wbstartstruct_t wmInfo; // Intermission parameters.
de::Uri nextMapUri;
uint nextMapEntryPoint;
static bool quitInProgress;
static gamestate_t gameState = GS_STARTUP;
static SaveSlots *sslots;
// Game actions.
static gameaction_t gameAction;
// Game action parameters:
static GameRules &G_NewSessionRules()
{
static GameRules gaNewSessionRules;
return gaNewSessionRules;
}
static String gaNewSessionEpisodeId;
static de::Uri gaNewSessionMapUri;
static uint gaNewSessionMapEntrance;
static String gaSaveSessionSlot;
static bool gaSaveSessionGenerateDescription = true;
static String gaSaveSessionUserDescription;
static String gaLoadSessionSlot;
dd_bool G_QuitInProgress()
{
return ::quitInProgress;
}
void G_SetGameAction(gameaction_t newAction)
{
if (G_QuitInProgress()) return;
if (::gameAction != newAction)
{
::gameAction = newAction;
}
}
void G_SetGameActionNewSession(GameRules const &rules, String episodeId, de::Uri const &mapUri,
uint mapEntrance)
{
G_NewSessionRules() = rules;
::gaNewSessionEpisodeId = episodeId;
::gaNewSessionMapUri = mapUri;
::gaNewSessionMapEntrance = mapEntrance;
G_SetGameAction(GA_NEWSESSION);
}
bool G_SetGameActionSaveSession(String slotId, String *userDescription)
{
if (!gfw_Session()->isSavingPossible()) return false;
if (!G_SaveSlots().has(slotId)) return false;
::gaSaveSessionSlot = slotId;
if (userDescription && !userDescription->isEmpty())
{
// A new description.
::gaSaveSessionGenerateDescription = false;
::gaSaveSessionUserDescription = *userDescription;
}
else
{
// Reusing the current name or generating a new one.
::gaSaveSessionGenerateDescription = (userDescription && userDescription->isEmpty());
::gaSaveSessionUserDescription.clear();
}
G_SetGameAction(GA_SAVESESSION);
return true;
}
bool G_SetGameActionLoadSession(String slotId)
{
if (!gfw_Session()->isLoadingPossible()) return false;
// Check whether this slot is in use. We do this here also because we need to provide our
// caller with instant feedback. Naturally this is no guarantee that the game-save will
// be accessible come load time.
auto scheduleLoad = [slotId] ()
{
if (G_SaveSlots()[slotId].isLoadable())
{
// Everything appears to be in order - schedule the game-save load!
gaLoadSessionSlot = slotId;
G_SetGameAction(GA_LOADSESSION);
}
else
{
LOG_RES_ERROR("Cannot load from save slot '%s': not in use") << slotId;
}
};
try
{
auto const &slot = G_SaveSlots()[slotId];
GameStateFolder const &save = App::rootFolder().locate<GameStateFolder const>(slot.savePath());
Record const &meta = save.metadata();
if (meta.has("packages"))
{
DoomsdayApp::app().checkPackageCompatibility(
meta.getStringList("packages"),
String::format("The savegame " _E(b) "%s" _E(.) " was created with "
"mods that are different than the ones currently in use.",
meta.gets("userDescription").toUtf8().constData()),
scheduleLoad);
}
else
{
scheduleLoad();
}
}
catch (SaveSlots::MissingSlotError const &er)
{
LOG_RES_WARNING("Save slot '%s' not found: %s") << slotId << er.asText();
return false;
}
return true;
}
void G_SetGameActionMapCompleted(de::Uri const &nextMapUri, uint nextMapEntryPoint, bool secretExit)
{
#if __JHEXEN__
DENG2_UNUSED(secretExit);
#else
DENG2_UNUSED(nextMapEntryPoint);
#endif
if (IS_CLIENT) return;
if (::cyclingMaps && ::mapCycleNoExit) return;
#if __JHEXEN__
if ((::gameMode == hexen_betademo || ::gameMode == hexen_demo) &&
!(nextMapUri.path() == "MAP01" ||
nextMapUri.path() == "MAP02" ||
nextMapUri.path() == "MAP03" ||
nextMapUri.path() == "MAP04"))
{
// Not possible in the 4-map demo.
P_SetMessage(&players[CONSOLEPLAYER], "PORTAL INACTIVE -- DEMO");
return;
}
#endif
::nextMapUri = nextMapUri;
#if __JHEXEN__
::nextMapEntryPoint = nextMapEntryPoint;
#else
::secretExit = secretExit;
# if __JDOOM__
// If no Wolf3D maps, no secret exit!
if (::secretExit && (::gameModeBits & GM_ANY_DOOM2))
{
if (!P_MapExists(de::makeUri("Maps:MAP31").compose().toUtf8().constData()))
{
::secretExit = false;
}
}
# endif
#endif
G_SetGameAction(GA_MAPCOMPLETED);
}
void G_SetGameActionMapCompletedAndSetNextMap()
{
G_SetGameActionMapCompleted(gfw_Session()->mapUriForNamedExit("next"));
}
static void initSaveSlots()
{
delete sslots;
sslots = new SaveSlots;
// Setup the logical save slot bindings.
menu::Widget::Flag const gameMenuSaveSlotWidgetIds[NUMSAVESLOTS] = {
menu::Widget::Id0, menu::Widget::Id1, menu::Widget::Id2,
menu::Widget::Id3, menu::Widget::Id4, menu::Widget::Id5,
#if !__JHEXEN__
menu::Widget::Id6, menu::Widget::Id7
#endif
};
for (int i = 0; i < NUMSAVESLOTS; ++i)
{
sslots->add(String::number(i), true, String(SAVEGAMENAME"%1").arg(i),
int(gameMenuSaveSlotWidgetIds[i]));
}
}
/**
* Common Pre Game Initialization routine.
* Game-specfic pre init actions should be placed in eg D_PreInit() (for jDoom)
*/
void G_CommonPreInit()
{
::quitInProgress = false;
// Apply the default game rules.
gfw_Session()->applyNewRules(gfw_DefaultGameRules() = GameRules());
// Register hooks.
Plug_AddHook(HOOK_DEMO_STOP, Hook_DemoStop);
// Setup the players.
for (int i = 0; i < MAXPLAYERS; ++i)
{
player_t *pl = &players[i];
pl->plr = DD_GetPlayer(i);
pl->plr->extraData = (void *) pl;
/// @todo Only necessary because the engine does not yet unload game plugins when they
/// are not in use; thus a game change may leave these pointers dangling.
for (int k = 0; k < NUMPSPRITES; ++k)
{
pl->pSprites[k].state = nullptr;
pl->plr->pSprites[k].statePtr = nullptr;
}
}
G_RegisterBindClasses();
P_RegisterMapObjs();
R_LoadVectorGraphics();
R_LoadColorPalettes();
P_InitPicAnims();
// Add our cvars and ccmds to the console databases.
G_ConsoleRegistration(); // Main command list.
acs::System::consoleRegister();
D_NetConsoleRegister();
G_ConsoleRegister();
Pause_Register();
G_ControlRegister();
SaveSlots::consoleRegister();
Hu_MenuConsoleRegister();
GUI_Register();
Hu_MsgRegister();
ST_Register(); // For the hud/statusbar.
IN_ConsoleRegister(); // For the interlude/intermission.
X_Register(); // For the crosshair.
FI_StackRegister(); // For the InFine lib.
R_SpecialFilterRegister();
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
XG_Register();
#endif
Con_SetString2("map-author", "Unknown", SVF_WRITE_OVERRIDE);
Con_SetString2("map-name", "Unknown", SVF_WRITE_OVERRIDE);
}
#if __JHEXEN__
/**
* @todo all this swapping colors around is rather silly, why not simply
* reorder the translation tables at load time?
*/
void R_GetTranslation(int plrClass, int plrColor, int *tclass, int *tmap)
{
int mapped;
if (plrClass == PCLASS_PIG)
{
// A pig is never translated.
*tclass = *tmap = 0;
return;
}
if (gameMode == hexen_v10)
{
int const mapping[3][4] = {
/* Fighter */ { 1, 2, 0, 3 },
/* Cleric */ { 1, 0, 2, 3 },
/* Mage */ { 1, 0, 2, 3 }
};
DENG2_ASSERT(plrClass >= 0 && plrClass < 3);
DENG2_ASSERT(plrColor >= 0 && plrColor < 4);
mapped = mapping[plrClass][plrColor];
}
else
{
int const mapping[3][8] = {
/* Fighter */ { 1, 2, 0, 3, 4, 5, 6, 7 },
/* Cleric */ { 1, 0, 2, 3, 4, 5, 6, 7 },
/* Mage */ { 1, 0, 2, 3, 4, 5, 6, 7 }
};
DENG2_ASSERT(plrClass >= 0 && plrClass < 3);
DENG2_ASSERT(plrColor >= 0 && plrColor < 8);
mapped = mapping[plrClass][plrColor];
}
*tclass = (mapped? plrClass : 0);
*tmap = mapped;
}
void Mobj_UpdateTranslationClassAndMap(mobj_t *mo)
{
DENG2_ASSERT(mo);
if (mo->player)
{
int plrColor = (mo->flags & MF_TRANSLATION) >> MF_TRANSSHIFT;
R_GetTranslation(mo->player->class_, plrColor, &mo->tclass, &mo->tmap);
}
else if (mo->flags & MF_TRANSLATION)
{
mo->tclass = mo->special1;
mo->tmap = (mo->flags & MF_TRANSLATION) >> MF_TRANSSHIFT;
}
else
{
// No translation.
mo->tmap = mo->tclass = 0;
}
}
#endif // __JHEXEN__
void R_LoadColorPalettes()
{
#define PALLUMPNAME "PLAYPAL"
#define PALENTRIES (256)
#define PALID (0)
File1 &playpal = CentralLumpIndex()[CentralLumpIndex().findLast(String(PALLUMPNAME) + ".lmp")];
customPal = playpal.hasCustom(); // Remember whether we are using a custom palette.
uint8_t colors[PALENTRIES * 3];
playpal.read(colors, 0 + PALID * (PALENTRIES * 3), PALENTRIES * 3);
colorpaletteid_t palId = R_CreateColorPalette("R8G8B8", PALLUMPNAME, colors, PALENTRIES);
ddstring_s xlatId; Str_InitStd(&xlatId);
#if __JHEXEN__
// Load the translation tables.
{
int const numPerClass = (gameMode == hexen_v10? 3 : 7);
// In v1.0, the color translations are a bit different. There are only
// three translation maps per class, whereas Doomsday assumes seven maps
// per class. Thus we'll need to account for the difference.
int xlatNum = 0;
for (int cl = 0; cl < 3; ++cl)
for (int i = 0; i < 7; ++i)
{
if (i == numPerClass) break; // Not present.
String lumpName;
if (xlatNum < 10)
{
lumpName = String("TRANTBL%1").arg(xlatNum);
}
else
{
lumpName = String("TRANTBL%1").arg('A' + (xlatNum - 10));
}
xlatNum++;
LOG_AS("R_LoadColorPalettes")
LOG_RES_XVERBOSE("Reading translation table '%s' as tclass=%i tmap=%i",
lumpName << cl << i);
lumpName += ".lmp";
if (CentralLumpIndex().contains(lumpName))
{
File1 &lump = CentralLumpIndex()[CentralLumpIndex().findLast(lumpName)];
uint8_t const *mappings = lump.cache();
Str_Appendf(Str_Clear(&xlatId), "%i", 7 * cl + i);
R_CreateColorPaletteTranslation(palId, &xlatId, mappings);
lump.unlock();
}
}
}
#else
// Create the translation tables to map the green color ramp to gray,
// brown, red. Could be read from a lump instead?
{
uint8_t xlat[PALENTRIES];
for (int xlatNum = 0; xlatNum < 3; ++xlatNum)
{
// Translate just the 16 green colors.
for (int palIdx = 0; palIdx < 256; ++palIdx)
{
# if __JHERETIC__
if (palIdx >= 225 && palIdx <= 240)
{
xlat[palIdx] = xlatNum == 0? 114 + (palIdx - 225) /*yellow*/ :
xlatNum == 1? 145 + (palIdx - 225) /*red*/ :
190 + (palIdx - 225) /*blue*/;
}
# else
if (palIdx >= 0x70 && palIdx <= 0x7f)
{
// Map green ramp to gray, brown, red.
xlat[palIdx] = xlatNum == 0? 0x60 + (palIdx & 0xf) :
xlatNum == 1? 0x40 + (palIdx & 0xf) :
0x20 + (palIdx & 0xf);
}
# endif
else
{
// Keep all other colors as is.
xlat[palIdx] = palIdx;
}
}
Str_Appendf(Str_Clear(&xlatId), "%i", xlatNum);
R_CreateColorPaletteTranslation(palId, &xlatId, xlat);
}
}
#endif
Str_Free(&xlatId);
#undef PALID
#undef PALENTRIES
#undef PALLUMPNAME
}
/**
* @todo Read this information from a definition (ideally with more user
* friendly mnemonics...).
*/
void R_LoadVectorGraphics()
{
#define R (1.0f)
#define NUMITEMS(x) (sizeof(x)/sizeof((x)[0]))
#define Pt Point2Rawf
Point2Rawf const keyPoints[] = {
Pt(-3 * R / 4, 0), Pt(-3 * R / 4, -R / 4), // Mid tooth.
Pt( 0, 0), Pt( -R, 0), Pt( -R, -R / 2), // Shaft and end tooth.
Pt( 0, 0), Pt(R / 4, -R / 2), // Bow.
Pt(R / 2, -R / 2), Pt(R / 2, R / 2),
Pt(R / 4, R / 2), Pt( 0, 0),
};
def_svgline_t const key[] = {
{ 2, &keyPoints[ 0] },
{ 3, &keyPoints[ 2] },
{ 6, &keyPoints[ 5] }
};
Point2Rawf const thintrianglePoints[] = {
Pt(-R / 2, R - R / 2),
Pt( R, 0), // `
Pt(-R / 2, -R + R / 2), // /
Pt(-R / 2, R - R / 2) // |>
};
def_svgline_t const thintriangle[] = {
{ 4, thintrianglePoints },
};
#if __JDOOM__ || __JDOOM64__
Point2Rawf const arrowPoints[] = {
Pt( -R + R / 8, 0 ), Pt( R, 0), // -----
Pt( R - R / 2, -R / 4), Pt( R, 0), Pt( R - R / 2, R / 4), // ----->
Pt(-R - R / 8, -R / 4), Pt( -R + R / 8, 0), Pt(-R - R / 8, R / 4), // >---->
Pt(-R + R / 8, -R / 4), Pt(-R + 3 * R / 8, 0), Pt(-R + R / 8, R / 4), // >>--->
};
def_svgline_t const arrow[] = {
{ 2, &arrowPoints[ 0] },
{ 3, &arrowPoints[ 2] },
{ 3, &arrowPoints[ 5] },
{ 3, &arrowPoints[ 8] }
};
#elif __JHERETIC__ || __JHEXEN__
Point2Rawf const arrowPoints[] = {
Pt(-R + R / 4, 0), Pt( 0, 0), // center line.
Pt(-R + R / 4, R / 8), Pt( R, 0), Pt(-R + R / 4, -R / 8), // blade
Pt(-R + R / 8, -R / 4), Pt(-R + R / 4, -R / 4), // guard
Pt(-R + R / 4, R / 4), Pt(-R + R / 8, R / 4),
Pt(-R + R / 8, -R / 4),
Pt(-R + R / 8, -R / 8), Pt(-R - R / 4, -R / 8), // hilt
Pt(-R - R / 4, R / 8), Pt(-R + R / 8, R / 8),
};
def_svgline_t const arrow[] = {
{ 2, &arrowPoints[ 0] },
{ 3, &arrowPoints[ 2] },
{ 5, &arrowPoints[ 5] },
{ 4, &arrowPoints[10] }
};
#endif
#if __JDOOM__
Point2Rawf const cheatarrowPoints[] = {
Pt( -R + R / 8, 0 ), Pt( R, 0), // -----
Pt( R - R / 2, -R / 4), Pt( R, 0), Pt( R - R / 2, R / 4), // ----->
Pt(-R - R / 8, -R / 4), Pt( -R + R / 8, 0), Pt(-R - R / 8, R / 4), // >---->
Pt(-R + R / 8, -R / 4), Pt(-R + 3 * R / 8, 0), Pt(-R + R / 8, R / 4), // >>--->
Pt( -R / 2, 0), Pt( -R / 2, -R / 6), // >>-d--->
Pt(-R / 2 + R / 6, -R / 6), Pt(-R / 2 + R / 6, R / 4),
Pt( -R / 6, 0), Pt( -R / 6, -R / 6), // >>-dd-->
Pt( 0, -R / 6), Pt( 0, R / 4),
Pt( R / 6, R / 4), Pt( R / 6, -R / 7), // >>-ddt->
Pt(R / 6 + R / 32, -R / 7 - R / 32), Pt(R / 6 + R / 10, -R / 7)
};
def_svgline_t const cheatarrow[] = {
{ 2, &cheatarrowPoints[ 0] },
{ 3, &cheatarrowPoints[ 2] },
{ 3, &cheatarrowPoints[ 5] },
{ 3, &cheatarrowPoints[ 8] },
{ 4, &cheatarrowPoints[11] },
{ 4, &cheatarrowPoints[15] },
{ 4, &cheatarrowPoints[19] }
};
#endif
Point2Rawf const crossPoints[] = { // + (open center)
Pt(-R, 0), Pt(-R / 5 * 2, 0),
Pt( 0, -R), Pt( 0, -R / 5 * 2),
Pt( R, 0), Pt( R / 5 * 2, 0),
Pt( 0, R), Pt( 0, R / 5 * 2)
};
def_svgline_t const cross[] = {
{ 2, &crossPoints[0] },
{ 2, &crossPoints[2] },
{ 2, &crossPoints[4] },
{ 2, &crossPoints[6] }
};
Point2Rawf const twinanglesPoints[] = { // > <
Pt(-R, -R * 10 / 14), Pt(-(R - (R * 10 / 14)), 0), Pt(-R, R * 10 / 14), // >
Pt( R, -R * 10 / 14), Pt( R - (R * 10 / 14) , 0), Pt( R, R * 10 / 14), // <
};
def_svgline_t const twinangles[] = {
{ 3, &twinanglesPoints[0] },
{ 3, &twinanglesPoints[3] }
};
Point2Rawf const squarePoints[] = { // square
Pt(-R, -R), Pt(-R, R),
Pt( R, R), Pt( R, -R),
Pt(-R, -R)
};
def_svgline_t const square[] = {
{ 5, squarePoints },
};
Point2Rawf const squarecornersPoints[] = { // square (open center)
Pt( -R, -R / 2), Pt(-R, -R), Pt(-R / 2, -R), // topleft
Pt(R / 2, -R), Pt( R, -R), Pt( R, -R / 2), // topright
Pt( -R, R / 2), Pt(-R, R), Pt(-R / 2, R), // bottomleft
Pt(R / 2, R), Pt( R, R), Pt( R, R / 2), // bottomright
};
def_svgline_t const squarecorners[] = {
{ 3, &squarecornersPoints[ 0] },
{ 3, &squarecornersPoints[ 3] },
{ 3, &squarecornersPoints[ 6] },
{ 3, &squarecornersPoints[ 9] }
};
Point2Rawf const anglePoints[] = { // v
Pt(-R, -R), Pt(0, 0), Pt(R, -R)
};
def_svgline_t const angle[] = {
{ 3, anglePoints }
};
if (IS_DEDICATED) return;
R_NewSvg(VG_KEY, key, NUMITEMS(key));
R_NewSvg(VG_TRIANGLE, thintriangle, NUMITEMS(thintriangle));
R_NewSvg(VG_ARROW, arrow, NUMITEMS(arrow));
#if __JDOOM__
R_NewSvg(VG_CHEATARROW, cheatarrow, NUMITEMS(cheatarrow));
#endif
R_NewSvg(VG_XHAIR1, cross, NUMITEMS(cross));
R_NewSvg(VG_XHAIR2, twinangles, NUMITEMS(twinangles));
R_NewSvg(VG_XHAIR3, square, NUMITEMS(square));
R_NewSvg(VG_XHAIR4, squarecorners, NUMITEMS(squarecorners));
R_NewSvg(VG_XHAIR5, angle, NUMITEMS(angle));
#undef P
#undef R
#undef NUMITEMS
}
/**
* @param name Name of the font to lookup.
* @return Unique id of the found font.
*/
fontid_t R_MustFindFontForName(char const *name)
{
uri_s *uri = Uri_NewWithPath2(name, RC_NULL);
fontid_t fontId = Fonts_ResolveUri(uri);
Uri_Delete(uri);
if (fontId) return fontId;
Con_Error("Failed loading font \"%s\".", name);
exit(1); // Unreachable.
}
void R_InitRefresh()
{
if (IS_DEDICATED) return;
LOG_RES_VERBOSE("Loading data for refresh...");
// Setup the view border.
cfg.common.screenBlocks = cfg.common.setBlocks;
{
uri_s *paths[9];
for (int i = 0; i < 9; ++i)
{
paths[i] = ((borderGraphics[i] && borderGraphics[i][0])? Uri_NewWithPath2(borderGraphics[i], RC_NULL) : 0);
}
R_SetBorderGfx((uri_s const **)paths);
for (int i = 0; i < 9; ++i)
{
if (paths[i])
{
Uri_Delete(paths[i]);
}
}
}
R_ResizeViewWindow(RWF_FORCE|RWF_NO_LERP);
// Locate our fonts.
fonts[GF_FONTA] = R_MustFindFontForName("a");
fonts[GF_FONTB] = R_MustFindFontForName("b");
fonts[GF_STATUS] = R_MustFindFontForName("status");
#if __JDOOM__
fonts[GF_INDEX] = R_MustFindFontForName("index");
#endif
#if __JDOOM__ || __JDOOM64__
fonts[GF_SMALL] = R_MustFindFontForName("small");
#endif
#if __JHERETIC__ || __JHEXEN__
fonts[GF_SMALLIN] = R_MustFindFontForName("smallin");
#endif
fonts[GF_MAPPOINT] = R_MustFindFontForName("mappoint");
float mul = 1.4f;
DD_SetVariable(DD_PSPRITE_LIGHTLEVEL_MULTIPLIER, &mul);
}
void R_InitHud()
{
Hu_LoadData();
#if __JHERETIC__ || __JHEXEN__
LOG_VERBOSE("Initializing inventory...");
Hu_InventoryInit();
#endif
LOG_VERBOSE("Initializing statusbar...");
ST_Init();
LOG_VERBOSE("Initializing menu...");
Hu_MenuInit();
LOG_VERBOSE("Initializing status-message/question system...");
Hu_MsgInit();
}
SaveSlots &G_SaveSlots()
{
DENG2_ASSERT(sslots != 0);
return *sslots;
}
/**
* @attention Game-specific post init actions should be placed in the game-appropriate
* post init routine (e.g., D_PostInit() for libdoom) and NOT here.
*/
void G_CommonPostInit()
{
R_InitRefresh();
FI_StackInit();
GUI_Init();
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
XG_ReadTypes();
#endif
LOG_VERBOSE("Initializing playsim...");
P_Init();
LOG_VERBOSE("Initializing head-up displays...");
R_InitHud();
initSaveSlots();
G_InitEventSequences();
#if __JDOOM__ || __JHERETIC__ || __JHEXEN__
G_RegisterCheats();
#endif
// Change the turbo multiplier.
{
auto &cmdLine = CommandLine::get();
turboMul = float(gfw_GameProfile()->optionValue("turbo").asNumber());
if (int arg = cmdLine.check("-turbo"))
{
int scale = 200; // Default to 2x without a numeric value.
if (arg + 1 < cmdLine.count() && !cmdLine.isOption(arg + 1))
{
scale = cmdLine.at(arg + 1).toInt();
}
scale = de::clamp(10, scale, 400);
turboMul = scale / 100.f;
LOG_NOTE("Turbo speed: %i%%") << scale;
}
}
// From this point on, the shortcuts are always active.
DD_Execute(true, "activatebcontext shortcut");
// Display a breakdown of the available maps.
DD_Execute(true, "listmaps");
}
void G_AutoStartOrBeginTitleLoop()
{
CommandLine &cmdLine = DENG2_APP->commandLine();
String startEpisodeId;
de::Uri startMapUri;
// A specific episode?
if (int arg = cmdLine.check("-episode", 1))
{
String episodeId = cmdLine.at(arg + 1);
if (Record const *episodeDef = Defs().episodes.tryFind("id", episodeId))
{
// Ensure this is a playable episode.
de::Uri startMap(episodeDef->gets("startMap"), RC_NULL);
if (P_MapExists(startMap.compose().toUtf8().constData()))
{
startEpisodeId = episodeId;
}
}
}
// A specific map?
if (int arg = cmdLine.check("-warp", 1))
{
bool haveEpisode = (arg + 2 < cmdLine.count() && !cmdLine.isOption(arg + 2));
if (haveEpisode)
{
if (Record const *episodeDef = Defs().episodes.tryFind("id", cmdLine.at(arg + 1)))
{
// Ensure this is a playable episode.
de::Uri startMap(episodeDef->gets("startMap"), RC_NULL);
if (P_MapExists(startMap.compose().toUtf8().constData()))
{
startEpisodeId = episodeDef->gets("id");
}
}
}
// The map.
bool isNumber;
int mapWarpNumber = cmdLine.at(arg + (haveEpisode? 2 : 1)).toInt(&isNumber);
if (!isNumber)
{
// It must be a URI, then.
Block rawMapUri = cmdLine.at(arg + (haveEpisode? 2 : 1)).toUtf8();
char *args[1] = { const_cast<char *>(rawMapUri.constData()) };
startMapUri = de::Uri::fromUserInput(args, 1);
if (startMapUri.scheme().isEmpty()) startMapUri.setScheme("Maps");
}
else
{
if (startEpisodeId.isEmpty())
{
// Pick the first playable episode.
startEpisodeId = FirstPlayableEpisodeId();
}
// Map warp numbers must be translated in the context of an Episode.
startMapUri = TranslateMapWarpNumber(startEpisodeId, mapWarpNumber);
}
}
// Are we attempting an auto-start?
bool autoStart = (IS_NETGAME || !startEpisodeId.isEmpty() || !startMapUri.isEmpty());
if (autoStart)
{
if (startEpisodeId.isEmpty())
{
// Pick the first playable episode.
startEpisodeId = FirstPlayableEpisodeId();
}
// Ensure that the map exists.
if (!P_MapExists(startMapUri.compose().toUtf8().constData()))
{
startMapUri.clear();
// Pick the start map from the episode, if specified and playable.
if (Record const *episodeDef = Defs().episodes.tryFind("id", startEpisodeId))
{
de::Uri startMap(episodeDef->gets("startMap"), RC_NULL);
if (P_MapExists(startMap.compose().toUtf8().constData()))
{
startMapUri = startMap;
}
}
}
}
// Are we auto-starting?
if (!startEpisodeId.isEmpty() && !startMapUri.isEmpty())
{
LOG_NOTE("Auto-starting episode '%s', map \"%s\", skill %i")
<< startEpisodeId
<< startMapUri
<< gfw_DefaultRule(skill);
// Don't brief when autostarting.
::briefDisabled = true;
G_SetGameActionNewSession(gfw_DefaultGameRules(), startEpisodeId, startMapUri);
}
else
{
gfw_Session()->endAndBeginTitle(); // Start up intro loop.
}
}
/**
* Common game shutdown routine.
* @note Game-specific actions should be placed in G_Shutdown rather than here.
*/
void G_CommonShutdown()
{
gfw_Session()->end();
Plug_RemoveHook(HOOK_DEMO_STOP, Hook_DemoStop);
Hu_MsgShutdown();
Hu_UnloadData();
D_NetClearBuffer();
P_Shutdown();
G_ShutdownEventSequences();
FI_StackShutdown();
Hu_MenuShutdown();
ST_Shutdown();
GUI_Shutdown();
delete sslots; sslots = 0;
}
gamestate_t G_GameState()
{
return gameState;
}
static char const *getGameStateStr(gamestate_t state)
{
struct statename_s {
gamestate_t state;
char const *name;
} stateNames[] =
{
{ GS_MAP, "GS_MAP" },
{ GS_INTERMISSION, "GS_INTERMISSION" },
{ GS_FINALE, "GS_FINALE" },
{ GS_STARTUP, "GS_STARTUP" },
{ GS_WAITING, "GS_WAITING" },
{ GS_INFINE, "GS_INFINE" },
{ gamestate_t(-1), 0 }
};
for (uint i = 0; stateNames[i].name; ++i)
{
if (stateNames[i].state == state)
return stateNames[i].name;
}
return 0;
}
/**
* Called when the gameui binding context is active. Triggers the menu.
*/
int G_UIResponder(event_t *ev)
{
// Handle "Press any key to continue" messages.
if (Hu_MsgResponder(ev))
return true;
if (ev->state != EVS_DOWN)