/
g_game.c
3537 lines (3069 loc) · 91.4 KB
/
g_game.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 g_game.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 1999-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*\author Copyright © 1999 Activision
*\author Copyright © 1993-1996 by id Software, Inc.
*
* 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
*/
/**
* Top-level (common) game routines.
*/
// HEADER FILES ------------------------------------------------------------
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "dmu_lib.h"
#include "fi_lib.h"
#include "hu_lib.h"
#include "p_saveg.h"
#include "g_controls.h"
#include "g_eventsequence.h"
#include "p_mapsetup.h"
#include "p_user.h"
#include "p_actor.h"
#include "p_tick.h"
#include "am_map.h"
#include "hu_stuff.h"
#include "hu_menu.h"
#include "hu_log.h"
#include "hu_chat.h"
#include "hu_msg.h"
#include "hu_pspr.h"
#include "g_common.h"
#include "g_update.h"
#include "g_eventsequence.h"
#include "d_net.h"
#include "x_hair.h"
#include "p_player.h"
#include "r_common.h"
#include "p_mapspec.h"
#include "p_start.h"
#include "p_inventory.h"
#if __JHERETIC__ || __JHEXEN__
# include "p_inventory.h"
# include "hu_inventory.h"
#endif
#include "d_net.h"
// MACROS ------------------------------------------------------------------
#define BODYQUEUESIZE (32)
#define UNNAMEDMAP "Unnamed"
#define NOTAMAPNAME "N/A"
#define READONLYCVAR CVF_READ_ONLY|CVF_NO_MAX|CVF_NO_MIN|CVF_NO_ARCHIVE
// TYPES -------------------------------------------------------------------
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
struct missileinfo_s {
mobjtype_t type;
float speed[2];
}
MonsterMissileInfo[] =
{
#if __JDOOM__ || __JDOOM64__
{MT_BRUISERSHOT, {15, 20}},
{MT_HEADSHOT, {10, 20}},
{MT_TROOPSHOT, {10, 20}},
# if __JDOOM64__
{MT_BRUISERSHOTRED, {15, 20}},
{MT_NTROSHOT, {20, 40}},
# endif
#elif __JHERETIC__
{MT_IMPBALL, {10, 20}},
{MT_MUMMYFX1, {9, 18}},
{MT_KNIGHTAXE, {9, 18}},
{MT_REDAXE, {9, 18}},
{MT_BEASTBALL, {12, 20}},
{MT_WIZFX1, {18, 24}},
{MT_SNAKEPRO_A, {14, 20}},
{MT_SNAKEPRO_B, {14, 20}},
{MT_HEADFX1, {13, 20}},
{MT_HEADFX3, {10, 18}},
{MT_MNTRFX1, {20, 26}},
{MT_MNTRFX2, {14, 20}},
{MT_SRCRFX1, {20, 28}},
{MT_SOR2FX1, {20, 28}},
#endif
{-1, {-1, -1}} // Terminator
};
#endif
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(CycleTextureGamma);
D_CMD(EndGame);
D_CMD(HelpScreen);
D_CMD(ListMaps);
D_CMD(LoadGame);
D_CMD(OpenLoadMenu);
D_CMD(QuickLoadGame);
D_CMD(QuickSaveGame);
D_CMD(SaveGame);
D_CMD(OpenSaveMenu);
void G_PlayerReborn(int player);
void G_InitNew(skillmode_t skill, uint episode, uint map);
void G_DoInitNew(void);
void G_DoReborn(int playernum);
void G_DoLoadMap(void);
void G_DoNewGame(void);
void G_DoLoadGame(void);
void G_DoPlayDemo(void);
void G_DoMapCompleted(void);
void G_DoVictory(void);
void G_DoWorldDone(void);
void G_DoSaveGame(void);
void G_DoScreenShot(void);
void G_DoQuitGame(void);
boolean G_ValidateMap(uint *episode, uint *map);
#if __JHEXEN__
void G_DoSingleReborn(void);
void H2_PageTicker(void);
void H2_AdvanceDemo(void);
#endif
void G_StopDemo(void);
void R_LoadColorPalettes(void);
void R_LoadVectorGraphics(void);
int Hook_DemoStop(int hookType, int val, void* paramaters);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
game_config_t cfg; // The global cfg.
int debugSound; // Debug flag for displaying sound info.
skillmode_t dSkill;
skillmode_t gameSkill;
uint gameEpisode;
uint gameMap;
uint nextMap;
#if __JHEXEN__
uint nextMapEntryPoint;
#endif
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
boolean secretExit;
#endif
#if __JHEXEN__
// Position indicator for cooperative net-play reborn
uint rebornPosition;
#endif
#if __JHEXEN__
uint mapHub = 0;
#endif
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
boolean respawnMonsters;
#endif
boolean monsterInfight;
boolean paused;
boolean sendPause; // Send a pause event next tic.
boolean userGame = false; // Ok to save / end game.
boolean deathmatch; // Only if started as net death.
player_t players[MAXPLAYERS];
int mapStartTic; // Game tic at map start.
int totalKills, totalItems, totalSecret; // For intermission.
boolean singledemo; // Quit after playing a demo from cmdline.
boolean briefDisabled = false;
boolean precache = true; // If @c true, load all graphics at start.
boolean customPal = false; // If @c true, a non-IWAD palette is in use.
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
wbstartstruct_t wmInfo; // Params for world map / intermission.
#endif
// Game Action Variables:
#define GA_SAVEGAME_NAME_LASTINDEX (24)
#define GA_SAVEGAME_NAME_MAXLENGTH (GA_SAVEGAME_NAME_LASTINDEX+1)
int gaSaveGameSlot = 0;
boolean gaSaveGameGenerateName = true;
char gaSaveGameName[GA_SAVEGAME_NAME_MAXLENGTH];
int gaLoadGameSlot = 0;
#if __JDOOM__ || __JDOOM64__
mobj_t *bodyQueue[BODYQUEUESIZE];
int bodyQueueSlot;
#endif
// vars used with game status cvars
int gsvInMap = 0;
int gsvCurrentMusic = 0;
int gsvMapMusic = -1;
int gsvArmor = 0;
int gsvHealth = 0;
#if !__JHEXEN__
int gsvKills = 0;
int gsvItems = 0;
int gsvSecrets = 0;
#endif
int gsvCurrentWeapon;
int gsvWeapons[NUM_WEAPON_TYPES];
int gsvKeys[NUM_KEY_TYPES];
int gsvAmmo[NUM_AMMO_TYPES];
char *gsvMapName = NOTAMAPNAME;
#if __JHERETIC__ || __JHEXEN__ || __JDOOM64__
int gsvInvItems[NUM_INVENTORYITEM_TYPES];
#endif
#if __JHEXEN__
int gsvWPieces[4];
#endif
static gamestate_t gameState = GS_STARTUP;
cvartemplate_t gamestatusCVars[] = {
{"game-state", READONLYCVAR, CVT_INT, &gameState, 0, 0},
{"game-state-map", READONLYCVAR, CVT_INT, &gsvInMap, 0, 0},
{"game-paused", READONLYCVAR, CVT_INT, &paused, 0, 0},
{"game-skill", READONLYCVAR, CVT_INT, &gameSkill, 0, 0},
{"map-id", READONLYCVAR, CVT_INT, &gameMap, 0, 0},
{"map-name", READONLYCVAR, CVT_CHARPTR, &gsvMapName, 0, 0},
{"map-episode", READONLYCVAR, CVT_INT, &gameEpisode, 0, 0},
#if __JHEXEN__
{"map-hub", READONLYCVAR, CVT_INT, &mapHub, 0, 0},
#endif
{"game-music", READONLYCVAR, CVT_INT, &gsvCurrentMusic, 0, 0},
{"map-music", READONLYCVAR, CVT_INT, &gsvMapMusic, 0, 0},
#if !__JHEXEN__
{"game-stats-kills", READONLYCVAR, CVT_INT, &gsvKills, 0, 0},
{"game-stats-items", READONLYCVAR, CVT_INT, &gsvItems, 0, 0},
{"game-stats-secrets", READONLYCVAR, CVT_INT, &gsvSecrets, 0, 0},
#endif
{"player-health", READONLYCVAR, CVT_INT, &gsvHealth, 0, 0},
{"player-armor", READONLYCVAR, CVT_INT, &gsvArmor, 0, 0},
{"player-weapon-current", READONLYCVAR, CVT_INT, &gsvCurrentWeapon, 0, 0},
#if __JDOOM__ || __JDOOM64__
// Ammo
{"player-ammo-bullets", READONLYCVAR, CVT_INT, &gsvAmmo[AT_CLIP], 0, 0},
{"player-ammo-shells", READONLYCVAR, CVT_INT, &gsvAmmo[AT_SHELL], 0, 0},
{"player-ammo-cells", READONLYCVAR, CVT_INT, &gsvAmmo[AT_CELL], 0, 0},
{"player-ammo-missiles", READONLYCVAR, CVT_INT, &gsvAmmo[AT_MISSILE], 0, 0},
// Weapons
{"player-weapon-fist", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FIRST], 0, 0},
{"player-weapon-pistol", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SECOND], 0, 0},
{"player-weapon-shotgun", READONLYCVAR, CVT_INT, &gsvWeapons[WT_THIRD], 0, 0},
{"player-weapon-chaingun", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FOURTH], 0, 0},
{"player-weapon-mlauncher", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FIFTH], 0, 0},
{"player-weapon-plasmarifle", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SIXTH], 0, 0},
{"player-weapon-bfg", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SEVENTH], 0, 0},
{"player-weapon-chainsaw", READONLYCVAR, CVT_INT, &gsvWeapons[WT_EIGHTH], 0, 0},
{"player-weapon-sshotgun", READONLYCVAR, CVT_INT, &gsvWeapons[WT_NINETH], 0, 0},
// Keys
{"player-key-blue", READONLYCVAR, CVT_INT, &gsvKeys[KT_BLUECARD], 0, 0},
{"player-key-yellow", READONLYCVAR, CVT_INT, &gsvKeys[KT_YELLOWCARD], 0, 0},
{"player-key-red", READONLYCVAR, CVT_INT, &gsvKeys[KT_REDCARD], 0, 0},
{"player-key-blueskull", READONLYCVAR, CVT_INT, &gsvKeys[KT_BLUESKULL], 0, 0},
{"player-key-yellowskull", READONLYCVAR, CVT_INT, &gsvKeys[KT_YELLOWSKULL], 0, 0},
{"player-key-redskull", READONLYCVAR, CVT_INT, &gsvKeys[KT_REDSKULL], 0, 0},
#elif __JHERETIC__
// Ammo
{"player-ammo-goldwand", READONLYCVAR, CVT_INT, &gsvAmmo[AT_CRYSTAL], 0, 0},
{"player-ammo-crossbow", READONLYCVAR, CVT_INT, &gsvAmmo[AT_ARROW], 0, 0},
{"player-ammo-dragonclaw", READONLYCVAR, CVT_INT, &gsvAmmo[AT_ORB], 0, 0},
{"player-ammo-hellstaff", READONLYCVAR, CVT_INT, &gsvAmmo[AT_RUNE], 0, 0},
{"player-ammo-phoenixrod", READONLYCVAR, CVT_INT, &gsvAmmo[AT_FIREORB], 0, 0},
{"player-ammo-mace", READONLYCVAR, CVT_INT, &gsvAmmo[AT_MSPHERE], 0, 0},
// Weapons
{"player-weapon-staff", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FIRST], 0, 0},
{"player-weapon-goldwand", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SECOND], 0, 0},
{"player-weapon-crossbow", READONLYCVAR, CVT_INT, &gsvWeapons[WT_THIRD], 0, 0},
{"player-weapon-dragonclaw", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FOURTH], 0, 0},
{"player-weapon-hellstaff", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FIFTH], 0, 0},
{"player-weapon-phoenixrod", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SIXTH], 0, 0},
{"player-weapon-mace", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SEVENTH], 0, 0},
{"player-weapon-gauntlets", READONLYCVAR, CVT_INT, &gsvWeapons[WT_EIGHTH], 0, 0},
// Keys
{"player-key-yellow", READONLYCVAR, CVT_INT, &gsvKeys[KT_YELLOW], 0, 0},
{"player-key-green", READONLYCVAR, CVT_INT, &gsvKeys[KT_GREEN], 0, 0},
{"player-key-blue", READONLYCVAR, CVT_INT, &gsvKeys[KT_BLUE], 0, 0},
// Inventory items
{"player-artifact-ring", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_INVULNERABILITY], 0, 0},
{"player-artifact-shadowsphere", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_INVISIBILITY], 0, 0},
{"player-artifact-crystalvial", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_HEALTH], 0, 0},
{"player-artifact-mysticurn", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_SUPERHEALTH], 0, 0},
{"player-artifact-tomeofpower", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TOMBOFPOWER], 0, 0},
{"player-artifact-torch", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TORCH], 0, 0},
{"player-artifact-firebomb", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_FIREBOMB], 0, 0},
{"player-artifact-egg", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_EGG], 0, 0},
{"player-artifact-wings", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_FLY], 0, 0},
{"player-artifact-chaosdevice", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TELEPORT], 0, 0},
#elif __JHEXEN__
// Mana
{"player-mana-blue", READONLYCVAR, CVT_INT, &gsvAmmo[AT_BLUEMANA], 0, 0},
{"player-mana-green", READONLYCVAR, CVT_INT, &gsvAmmo[AT_GREENMANA], 0, 0},
// Keys
{"player-key-steel", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY1], 0, 0},
{"player-key-cave", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY2], 0, 0},
{"player-key-axe", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY3], 0, 0},
{"player-key-fire", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY4], 0, 0},
{"player-key-emerald", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY5], 0, 0},
{"player-key-dungeon", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY6], 0, 0},
{"player-key-silver", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY7], 0, 0},
{"player-key-rusted", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY8], 0, 0},
{"player-key-horn", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEY9], 0, 0},
{"player-key-swamp", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEYA], 0, 0},
{"player-key-castle", READONLYCVAR, CVT_INT, &gsvKeys[KT_KEYB], 0, 0},
// Weapons
{"player-weapon-first", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FIRST], 0, 0},
{"player-weapon-second", READONLYCVAR, CVT_INT, &gsvWeapons[WT_SECOND], 0, 0},
{"player-weapon-third", READONLYCVAR, CVT_INT, &gsvWeapons[WT_THIRD], 0, 0},
{"player-weapon-fourth", READONLYCVAR, CVT_INT, &gsvWeapons[WT_FOURTH], 0, 0},
// Weapon Pieces
{"player-weapon-piece1", READONLYCVAR, CVT_INT, &gsvWPieces[0], 0, 0},
{"player-weapon-piece2", READONLYCVAR, CVT_INT, &gsvWPieces[1], 0, 0},
{"player-weapon-piece3", READONLYCVAR, CVT_INT, &gsvWPieces[2], 0, 0},
{"player-weapon-allpieces", READONLYCVAR, CVT_INT, &gsvWPieces[3], 0, 0},
// Inventory items
{"player-artifact-defender", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_INVULNERABILITY], 0, 0},
{"player-artifact-quartzflask", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_HEALTH], 0, 0},
{"player-artifact-mysticurn", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_SUPERHEALTH], 0, 0},
{"player-artifact-mysticambit", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_HEALINGRADIUS], 0, 0},
{"player-artifact-darkservant", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_SUMMON], 0, 0},
{"player-artifact-torch", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TORCH], 0, 0},
{"player-artifact-porkalator", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_EGG], 0, 0},
{"player-artifact-wings", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_FLY], 0, 0},
{"player-artifact-repulsion", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_BLASTRADIUS], 0, 0},
{"player-artifact-flechette", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_POISONBAG], 0, 0},
{"player-artifact-banishment", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TELEPORTOTHER], 0, 0},
{"player-artifact-speed", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_SPEED], 0, 0},
{"player-artifact-might", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_BOOSTMANA], 0, 0},
{"player-artifact-bracers", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_BOOSTARMOR], 0, 0},
{"player-artifact-chaosdevice", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_TELEPORT], 0, 0},
{"player-artifact-skull", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZSKULL], 0, 0},
{"player-artifact-heart", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMBIG], 0, 0},
{"player-artifact-ruby", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMRED], 0, 0},
{"player-artifact-emerald1", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMGREEN1], 0, 0},
{"player-artifact-emerald2", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMGREEN2], 0, 0},
{"player-artifact-sapphire1", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMBLUE1], 0, 0},
{"player-artifact-sapphire2", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEMBLUE2], 0, 0},
{"player-artifact-daemoncodex", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZBOOK1], 0, 0},
{"player-artifact-liberoscura", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZBOOK2], 0, 0},
{"player-artifact-flamemask", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZSKULL2], 0, 0},
{"player-artifact-glaiveseal", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZFWEAPON], 0, 0},
{"player-artifact-holyrelic", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZCWEAPON], 0, 0},
{"player-artifact-sigilmagus", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZMWEAPON], 0, 0},
{"player-artifact-gear1", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEAR1], 0, 0},
{"player-artifact-gear2", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEAR2], 0, 0},
{"player-artifact-gear3", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEAR3], 0, 0},
{"player-artifact-gear4", READONLYCVAR, CVT_INT, &gsvInvItems[IIT_PUZZGEAR4], 0, 0},
#endif
{NULL}
};
ccmdtemplate_t gameCmds[] = {
{ "endgame", "", CCmdEndGame },
{ "helpscreen", "", CCmdHelpScreen },
{ "listmaps", "", CCmdListMaps },
{ "loadgame", "ss", CCmdLoadGame },
{ "loadgame", "s", CCmdLoadGame },
{ "loadgame", "", CCmdOpenLoadMenu },
{ "quickload", "", CCmdQuickLoadGame },
{ "quicksave", "", CCmdQuickSaveGame },
{ "savegame", "sss", CCmdSaveGame },
{ "savegame", "ss", CCmdSaveGame },
{ "savegame", "s", CCmdSaveGame },
{ "savegame", "", CCmdOpenSaveMenu },
{ "togglegamma", "", CCmdCycleTextureGamma },
{ NULL }
};
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static uint dEpisode;
static uint dMap;
static gameaction_t gameAction;
static boolean quitInProgress;
// CODE --------------------------------------------------------------------
void G_Register(void)
{
int i;
for(i = 0; gamestatusCVars[i].path; ++i)
Con_AddVariable(gamestatusCVars + i);
for(i = 0; gameCmds[i].name; ++i)
Con_AddCommand(gameCmds + i);
C_VAR_BYTE("game-mobj-fracepsilon", &cfg.fracEpsilonForCollisions, 0, 0, 1);
}
boolean G_QuitInProgress(void)
{
return quitInProgress;
}
void G_SetGameAction(gameaction_t action)
{
if(G_QuitInProgress()) return;
if(gameAction != action)
gameAction = action;
}
gameaction_t G_GameAction(void)
{
return gameAction;
}
/**
* Common Pre Game Initialization routine.
* Game-specfic pre init actions should be placed in eg D_PreInit() (for jDoom)
*/
void G_CommonPreInit(void)
{
int i, j;
quitInProgress = false;
verbose = ArgExists("-verbose");
// Register hooks.
Plug_AddHook(HOOK_DEMO_STOP, Hook_DemoStop);
// Setup the players.
for(i = 0; i < MAXPLAYERS; ++i)
{
player_t* pl = players + i;
pl->plr = DD_GetPlayer(i);
pl->plr->extraData = (void*) &players[i];
/// \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(j = 0; j < NUMPSPRITES; ++j)
{
pl->pSprites[j].state = NULL;
pl->plr->pSprites[j].statePtr = NULL;
}
}
G_RegisterBindClasses();
G_RegisterPlayerControls();
P_RegisterMapObjs();
R_LoadVectorGraphics();
R_LoadColorPalettes();
P_InitPicAnims();
// Add our cvars and ccmds to the console databases.
G_ConsoleRegistration(); // Main command list.
D_NetConsoleRegistration(); // For network.
G_Register(); // Read-only game status cvars (for playsim).
G_ControlRegister(); // For controls/input.
SV_Register(); // Game-save system.
Hu_MenuRegister(); // For the menu.
GUI_Register(); // For the UI library.
Hu_MsgRegister(); // For the game messages.
ST_Register(); // For the hud/statusbar.
WI_Register(); // For the interlude/intermission.
X_Register(); // For the crosshair.
FI_StackRegister(); // For the InFine lib.
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
XG_Register();
#endif
Con_SetString2("map-name", NOTAMAPNAME, 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)
{
*tclass = 1;
if(plrColor == 0)
*tmap = 1;
else if(plrColor == 1)
*tmap = 0;
else
*tmap = plrColor;
// Fighter's colors are a bit different.
if(plrClass == PCLASS_FIGHTER && *tmap > 1)
*tclass = 0;
}
void R_SetTranslation(mobj_t* mo)
{
if(!(mo->flags & MF_TRANSLATION))
{ // No translation.
mo->tmap = mo->tclass = 0;
}
else
{
int tclass, tmap;
tmap = (mo->flags & MF_TRANSLATION) >> MF_TRANSSHIFT;
if(mo->player)
{
tclass = 1;
if(mo->player->class_ == PCLASS_FIGHTER)
{ // Fighter's colors are a bit different.
if(tmap == 0)
tmap = 2;
else if(tmap == 2)
tmap = 0;
else
tclass = 0;
}
mo->tclass = tclass;
}
else
tclass = mo->special1;
mo->tmap = tmap;
mo->tclass = tclass;
}
}
#endif
void R_LoadColorPalettes(void)
{
#define PALLUMPNAME "PLAYPAL"
#define PALENTRIES (256)
#define PALID (0)
lumpnum_t lumpNum = W_GetLumpNumForName(PALLUMPNAME);
uint8_t data[PALENTRIES*3];
// Record whether we are using a custom palette.
customPal = W_LumpIsCustom(lumpNum);
W_ReadLumpSection(lumpNum, data, 0 + PALID * (PALENTRIES * 3), PALENTRIES * 3);
R_CreateColorPalette("R8G8B8", PALLUMPNAME, data, PALENTRIES);
/**
* Create the translation tables to map the green color ramp to gray,
* brown, red.
*
* \note Assumes a given structure of the PLAYPAL. Could be read from a
* lump instead?
*/
#if __JDOOM__ || __JDOOM64__
{
byte* translationtables = (byte*) DD_GetVariable(DD_TRANSLATIONTABLES_ADDRESS);
int i;
// Translate just the 16 green colors.
for(i = 0; i < 256; ++i)
{
if(i >= 0x70 && i <= 0x7f)
{
// Map green ramp to gray, brown, red.
translationtables[i] = 0x60 + (i & 0xf);
translationtables[i + 256] = 0x40 + (i & 0xf);
translationtables[i + 512] = 0x20 + (i & 0xf);
}
else
{
// Keep all other colors as is.
translationtables[i] = translationtables[i + 256] =
translationtables[i + 512] = i;
}
}
}
#elif __JHERETIC__
{
int i;
byte* translationtables = (byte*) DD_GetVariable(DD_TRANSLATIONTABLES_ADDRESS);
// Fill out the translation tables.
for(i = 0; i < 256; ++i)
{
if(i >= 225 && i <= 240)
{
translationtables[i] = 114 + (i - 225); // yellow
translationtables[i + 256] = 145 + (i - 225); // red
translationtables[i + 512] = 190 + (i - 225); // blue
}
else
{
translationtables[i] = translationtables[i + 256] =
translationtables[i + 512] = i;
}
}
}
#else // __JHEXEN__
{
int i;
uint8_t* translationtables = (uint8_t*) DD_GetVariable(DD_TRANSLATIONTABLES_ADDRESS);
lumpnum_t lumpNum;
char name[9];
for(i = 0; i < 3 * 7; ++i)
{
dd_snprintf(name, 9, "TRANTBL%X", i);
if(-1 != (lumpNum = W_CheckLumpNumForName(name)))
{
W_ReadLumpSection(lumpNum, &translationtables[i * 256], 0, 256);
}
}
}
#endif
#undef PALID
#undef PALENTRIES
#undef PALLUMPNAME
}
/**
* @todo Read this information from a definition (ideally with more user
* friendly mnemonics...).
*/
void R_LoadVectorGraphics(void)
{
#define R (1.0f)
#define NUMITEMS(x) (sizeof(x)/sizeof((x)[0]))
const Point2Rawf keyPoints[] = {
{-3 * R / 4, 0}, {-3 * R / 4, -R / 4}, // Mid tooth.
{ 0, 0}, { -R, 0}, { -R, -R / 2}, // Shaft and end tooth.
{ 0, 0}, {R / 4, -R / 2}, // Bow.
{R / 2, -R / 2}, {R / 2, R / 2},
{R / 4, R / 2}, { 0, 0},
};
const def_svgline_t key[] = {
{ 2, &keyPoints[ 0] },
{ 3, &keyPoints[ 2] },
{ 6, &keyPoints[ 5] }
};
const Point2Rawf thintrianglePoints[] = {
{-R / 2, R - R / 2},
{ R, 0}, // `
{-R / 2, -R + R / 2}, // /
{-R / 2, R - R / 2} // |>
};
const def_svgline_t thintriangle[] = {
{ 4, thintrianglePoints },
};
#if __JDOOM__ || __JDOOM64__
const Point2Rawf arrowPoints[] = {
{ -R + R / 8, 0}, { R, 0}, // -----
{ R - R / 2, -R / 4}, { R, 0}, { R - R / 2, R / 4}, // ----->
{-R - R / 8, -R / 4}, { -R + R / 8, 0}, {-R - R / 8, R / 4}, // >---->
{-R + R / 8, -R / 4}, {-R + 3 * R / 8, 0}, {-R + R / 8, R / 4}, // >>--->
};
const def_svgline_t arrow[] = {
{ 2, &arrowPoints[ 0] },
{ 3, &arrowPoints[ 2] },
{ 3, &arrowPoints[ 5] },
{ 3, &arrowPoints[ 8] }
};
#elif __JHERETIC__ || __JHEXEN__
const Point2Rawf arrowPoints[] = {
{-R + R / 4, 0}, { 0, 0}, // center line.
{-R + R / 4, R / 8}, { R, 0}, {-R + R / 4, -R / 8}, // blade
{-R + R / 8, -R / 4}, {-R + R / 4, -R / 4}, // guard
{-R + R / 4, R / 4}, {-R + R / 8, R / 4},
{-R + R / 8, -R / 4},
{-R + R / 8, -R / 8}, {-R - R / 4, -R / 8}, // hilt
{-R - R / 4, R / 8}, {-R + R / 8, R / 8},
};
const def_svgline_t arrow[] = {
{ 2, &arrowPoints[ 0] },
{ 3, &arrowPoints[ 2] },
{ 5, &arrowPoints[ 5] },
{ 4, &arrowPoints[10] }
};
#endif
#if __JDOOM__
const Point2Rawf cheatarrowPoints[] = {
{ -R + R / 8, 0}, { R, 0}, // -----
{ R - R / 2, -R / 4}, { R, 0}, { R - R / 2, R / 4}, // ----->
{-R - R / 8, -R / 4}, { -R + R / 8, 0}, {-R - R / 8, R / 4}, // >---->
{-R + R / 8, -R / 4}, {-R + 3 * R / 8, 0}, {-R + R / 8, R / 4}, // >>--->
{ -R / 2, 0}, { -R / 2, -R / 6}, // >>-d--->
{-R / 2 + R / 6, -R / 6}, {-R / 2 + R / 6, R / 4},
{ -R / 6, 0}, { -R / 6, -R / 6}, // >>-dd-->
{ 0, -R / 6}, { 0, R / 4},
{ R / 6, R / 4}, { R / 6, -R / 7}, // >>-ddt->
{R / 6 + R / 32, -R / 7 - R / 32}, {R / 6 + R / 10, -R / 7}
};
const def_svgline_t cheatarrow[] = {
{ 2, &cheatarrowPoints[ 0] },
{ 3, &cheatarrowPoints[ 2] },
{ 3, &cheatarrowPoints[ 5] },
{ 3, &cheatarrowPoints[ 8] },
{ 4, &cheatarrowPoints[11] },
{ 4, &cheatarrowPoints[15] },
{ 4, &cheatarrowPoints[19] }
};
#endif
const Point2Rawf crossPoints[] = { // + (open center)
{-R, 0}, {-R / 5 * 2, 0},
{ 0, -R}, { 0, -R / 5 * 2},
{ R, 0}, { R / 5 * 2, 0},
{ 0, R}, { 0, R / 5 * 2}
};
const def_svgline_t cross[] = {
{ 2, &crossPoints[0] },
{ 2, &crossPoints[2] },
{ 2, &crossPoints[4] },
{ 2, &crossPoints[6] }
};
const Point2Rawf dblanglePoints[] = { // > <
{-R, -R * 10 / 14}, {-(R - (R * 10 / 14)), 0}, {-R, R * 10 / 14}, // >
{ R, -R * 10 / 14}, { R - (R * 10 / 14) , 0}, { R, R * 10 / 14}, // <
};
const def_svgline_t dblangle[] = {
{ 3, &dblanglePoints[0] },
{ 3, &dblanglePoints[3] }
};
const Point2Rawf squarePoints[] = { // square
{-R, -R}, {-R, R},
{ R, R}, { R, -R},
{-R, -R}
};
const def_svgline_t square[] = {
{ 5, squarePoints },
};
const Point2Rawf squarecornersPoints[] = { // square (open center)
{ -R, -R / 2}, {-R, -R}, {-R / 2, -R}, // topleft
{R / 2, -R}, { R, -R}, { R, -R / 2}, // topright
{ -R, R / 2}, {-R, R}, {-R / 2, R}, // bottomleft
{R / 2, R}, { R, R}, { R, R / 2}, // bottomright
};
const def_svgline_t squarecorners[] = {
{ 3, &squarecornersPoints[ 0] },
{ 3, &squarecornersPoints[ 3] },
{ 3, &squarecornersPoints[ 6] },
{ 3, &squarecornersPoints[ 9] }
};
const Point2Rawf diamondPoints[] = { // diamond
{ 0, -R}, { R, 0},
{ 0, R}, {-R, 0},
{ 0, -R}
};
const def_svgline_t diamond[] = {
{ 5, diamondPoints }
};
const Point2Rawf anglePoints[] = { // v
{-R, -R}, { 0, 0}, { R, -R}
};
const def_svgline_t angle[] = {
{ 3, anglePoints }
};
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, dblangle, NUMITEMS(dblangle));
R_NewSvg(VG_XHAIR3, square, NUMITEMS(square));
R_NewSvg(VG_XHAIR4, squarecorners, NUMITEMS(squarecorners));
R_NewSvg(VG_XHAIR5, diamond, NUMITEMS(diamond));
R_NewSvg(VG_XHAIR6, angle, NUMITEMS(angle));
#undef NUMITEMS
#undef R
}
/**
* @param name Name of the font to lookup.
* @return Unique id of the found font.
*/
fontid_t R_MustFindFontForName(const char* name)
{
Uri* 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(void)
{
if(IS_DEDICATED) return;
VERBOSE( Con_Message("R_InitRefresh: Loading data for referesh.\n") );
// Setup the view border.
cfg.screenBlocks = cfg.setBlocks;
{ Uri* paths[9];
uint i;
for(i = 0; i < 9; ++i)
paths[i] = ((borderGraphics[i] && borderGraphics[i][0])? Uri_NewWithPath2(borderGraphics[i], RC_NULL) : 0);
R_SetBorderGfx((const Uri**)paths);
for(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(void)
{
Hu_LoadData();
#if __JHERETIC__ || __JHEXEN__
VERBOSE( Con_Message("Initializing inventory...\n") )
Hu_InventoryInit();
#endif
VERBOSE2( Con_Message("Initializing statusbar...\n") )
ST_Init();
VERBOSE2( Con_Message("Initializing menu...\n") )
Hu_MenuInit();
VERBOSE2( Con_Message("Initializing status-message/question system...\n") )
Hu_MsgInit();
}
/**
* Common Post Game Initialization routine.
* Game-specific post init actions should be placed in eg D_PostInit()
* (for jDoom) and NOT here.
*/
void G_CommonPostInit(void)
{
R_InitRefresh();
FI_StackInit();
GUI_Init();
// Init the save system and create the game save directory.
SV_Init();
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
XG_ReadTypes();
#endif
VERBOSE( Con_Message("Initializing playsim...\n") )
P_Init();
VERBOSE( Con_Message("Initializing head-up displays...\n") )
R_InitHud();
G_InitEventSequences();
#if __JDOOM__ || __JHERETIC__ || __JHEXEN__
Cht_Init();
#endif
// 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");
}
/**
* Common game shutdown routine.
* \note Game-specific actions should be placed in G_Shutdown rather than here.
*/
void G_CommonShutdown(void)
{
Plug_RemoveHook(HOOK_DEMO_STOP, Hook_DemoStop);
Hu_MsgShutdown();
Hu_UnloadData();
D_NetClearBuffer();
SV_Shutdown();
P_Shutdown();
G_ShutdownEventSequences();
Hu_MenuShutdown();
ST_Shutdown();
GUI_Shutdown();
FI_StackShutdown();
}
/**
* Retrieve the current game state.
*
* @return The current game state.
*/
gamestate_t G_GameState(void)
{
return gameState;
}
#if _DEBUG
static const char* getGameStateStr(gamestate_t state)
{
struct statename_s {
gamestate_t state;
const char* 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"},
{-1, NULL}
};