/
p_start.cpp
1343 lines (1111 loc) · 37.2 KB
/
p_start.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 p_start.cpp Common player (re)spawning logic.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 1999 Activision
* @authors Copyright © 1993-1996 by 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 "p_start.h"
#include <cmath>
#include <cstdio>
#include <cstring>
#include <de/NativePath>
#include "am_map.h"
#include "d_net.h"
#include "dmu_lib.h"
#include "gamesession.h"
#include "g_common.h"
#include "g_defs.h"
#include "hu_chat.h"
#include "hu_stuff.h"
#include "mapinfo.h"
#include "p_actor.h"
#include "p_inventory.h"
#include "p_map.h"
#include "p_mapsetup.h"
#include "p_mapspec.h"
#include "p_switch.h"
#include "p_terraintype.h"
#include "p_tick.h"
#include "p_user.h"
#include "player.h"
#include "r_common.h"
using namespace de;
using namespace common;
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
# define TELEPORTSOUND SFX_TELEPT
# define MAX_START_SPOTS 4 // Maximum number of different player starts.
#else
# define TELEPORTSOUND SFX_TELEPORT
# define MAX_START_SPOTS 8
#endif
// Time interval for item respawning.
#define SPAWNQUEUE_MAX 128
uint numMapSpots;
mapspot_t *mapSpots;
#if __JHERETIC__
uint maceSpotCount;
mapspotid_t *maceSpots;
uint bossSpotCount;
mapspotid_t *bossSpots;
#endif
static int numPlayerStarts = 0;
static playerstart_t *playerStarts;
static int numPlayerDMStarts = 0;
static playerstart_t *deathmatchStarts;
/**
* New class (or -1) for each player to be applied when the player respawns.
* Actually applied on serverside, on the client only valid for the local
* player(s).
*/
static int playerRespawnAsClass[MAXPLAYERS];
static dd_bool fuzzySpawnPosition(coord_t *x, coord_t *y, coord_t *z, angle_t *angle,
int *spawnFlags)
{
#define XOFFSET (33) // Player radius = 16
#define YOFFSET (33) // Player radius = 16
DENG2_UNUSED3(z, angle, spawnFlags);
DENG_ASSERT(x != 0 && y != 0);
// Try some spots in the vicinity.
for(int i = 0; i < 9; ++i)
{
coord_t pos[2] = { *x, *y };
if(i != 0)
{
int k = (i == 4 ? 0 : i);
// Move a bit.
pos[VX] += (k % 3 - 1) * XOFFSET;
pos[VY] += (k / 3 - 1) * YOFFSET;
}
if(P_CheckSpot(pos[VX], pos[VY]))
{
*x = pos[VX];
*y = pos[VY];
return true;
}
}
return false;
#undef YOFFSET
#undef XOFFSET
}
void P_ResetPlayerRespawnClasses()
{
for(int i = 0; i < MAXPLAYERS; ++i)
{
// No change.
playerRespawnAsClass[i] = -1;
}
}
void P_SetPlayerRespawnClass(int plrNum, playerclass_t pc)
{
#ifndef __JHEXEN__
// There's only one player class.
DENG_ASSERT(pc == PCLASS_PLAYER);
#endif
playerRespawnAsClass[plrNum] = pc;
}
playerclass_t P_ClassForPlayerWhenRespawning(int plrNum, dd_bool clear)
{
#if __JHEXEN__
playerclass_t pClass = cfg.playerClass[plrNum];
#else
playerclass_t pClass = PCLASS_PLAYER;
#endif
if(playerRespawnAsClass[plrNum] != -1)
{
pClass = playerclass_t(playerRespawnAsClass[plrNum]);
if(clear)
{
// We can now clear the change request.
playerRespawnAsClass[plrNum] = -1;
}
}
return pClass;
}
mobjtype_t P_DoomEdNumToMobjType(int doomEdNum)
{
for(int i = 0; i < Get(DD_NUMMOBJTYPES); ++i)
{
if(doomEdNum == MOBJINFO[i].doomEdNum)
return mobjtype_t(i);
}
return MT_NONE;
}
void P_Init()
{
P_ResetPlayerRespawnClasses();
spechit = IterList_New();
#if __JHEXEN__
X_CreateLUTs();
#endif
#if __JHERETIC__ || __JHEXEN__
P_InitLava();
#endif
P_Update();
}
/**
* Populate the MapInfo database by parsing the MAPINFO lump.
*/
#if __JHEXEN__
static void readOneMapInfoDefinition(MapInfoParser &parser, AutoStr const &buffer, String sourceFile)
{
LOG_RES_VERBOSE("Parsing \"%s\"...") << NativePath(sourceFile).pretty();
try
{
parser.parse(buffer, sourceFile);
}
catch(MapInfoParser::ParseError const &er)
{
LOG_RES_WARNING("Failed parsing \"%s\" as MAPINFO:\n%s")
<< NativePath(sourceFile).pretty() << er.asText();
}
}
static void readMapInfoDefinitions()
{
// Clear the database.
hexDefs.clear();
// Initialize a new parser.
MapInfoParser parser(hexDefs);
// Read the primary MAPINFO (from the IWAD).
AutoStr *sourceFile = sc_FileScripts? Str_Appendf(AutoStr_New(), "%sMAPINFO.txt", sc_ScriptsDir)
: AutoStr_FromText("Lumps:MAPINFO");
AutoStr *buffer = M_ReadFileIntoString(sourceFile, 0);
if(buffer && !Str_IsEmpty(buffer))
{
readOneMapInfoDefinition(parser, *buffer, Str_Text(sourceFile));
}
else
{
LOG_RES_WARNING("MapInfoParser: Failed to open definition/script file \"%s\" for reading")
<< NativePath(Str_Text(sourceFile)).pretty();
}
#ifdef DENG_DEBUG
for(HexDefs::MapInfos::const_iterator i = hexDefs.mapInfos.begin(); i != hexDefs.mapInfos.end(); ++i)
{
MapInfo const &info = i->second;
LOG_RES_MSG("MAPINFO %s { title: \"%s\" hub: %i map: %s warp: %i }")
<< i->first.c_str() << info.gets("title")
<< info.geti("hub") << info.gets("map") << info.geti("warpTrans");
}
#endif
}
#endif
void P_Update()
{
#if __JHERETIC__ || __JHEXEN__ || __JDOOM64__
P_InitInventory();
#endif
#if __JHEXEN__
readMapInfoDefinitions();
#endif
P_InitSwitchList();
P_InitTerrainTypes();
maxHealth = 100;
GetDefInt("Player|Max Health", &maxHealth);
#if __JDOOM__ || __JDOOM64__
healthLimit = 200;
godModeHealth = 100;
megaSphereHealth = 200;
soulSphereHealth = 100;
soulSphereLimit = 200;
armorPoints[0] = 100;
armorPoints[1] = armorPoints[2] = armorPoints[3] = 200;
armorClass[0] = 1;
armorClass[1] = armorClass[2] = armorClass[3] = 2;
GetDefInt("Player|Health Limit", &healthLimit);
// Previous versions did not feature a separate value for God Health,
// so if its not found, default to the value of Max Health.
if(!GetDefInt("Player|God Health", &godModeHealth))
{
godModeHealth = maxHealth;
}
GetDefInt("Player|Green Armor", &armorPoints[0]);
GetDefInt("Player|Blue Armor", &armorPoints[1]);
GetDefInt("Player|IDFA Armor", &armorPoints[2]);
GetDefInt("Player|IDKFA Armor", &armorPoints[3]);
GetDefInt("Player|Green Armor Class", &armorClass[0]);
GetDefInt("Player|Blue Armor Class", &armorClass[1]);
GetDefInt("Player|IDFA Armor Class", &armorClass[2]);
GetDefInt("Player|IDKFA Armor Class", &armorClass[3]);
GetDefInt("MegaSphere|Give|Health", &megaSphereHealth);
GetDefInt("SoulSphere|Give|Health", &soulSphereHealth);
GetDefInt("SoulSphere|Give|Health Limit", &soulSphereLimit);
#endif
}
void P_Shutdown()
{
if(spechit)
{
IterList_Delete(spechit);
spechit = 0;
}
P_DestroyPlayerStarts();
P_DestroyAllTagLists();
P_ShutdownTerrainTypes();
P_FreeWeaponSlots();
#if __JDOOM__
delete theBossBrain; theBossBrain = 0;
#endif
}
void P_CreatePlayerStart(int defaultPlrNum, uint entryPoint, dd_bool deathmatch,
mapspotid_t spot)
{
playerstart_t *start;
if(deathmatch)
{
deathmatchStarts = (playerstart_t *)
Z_Realloc(deathmatchStarts, sizeof(playerstart_t) * ++numPlayerDMStarts, PU_MAP);
start = &deathmatchStarts[numPlayerDMStarts - 1];
App_Log(DE2_DEV_MAP_VERBOSE, "P_CreatePlayerStart: DM #%i plrNum=%i entryPoint=%i spot=%i",
numPlayerDMStarts - 1, defaultPlrNum, entryPoint, spot);
}
else
{
playerStarts = (playerstart_t *)
Z_Realloc(playerStarts, sizeof(playerstart_t) * ++numPlayerStarts, PU_MAP);
start = &playerStarts[numPlayerStarts - 1];
App_Log(DE2_DEV_MAP_VERBOSE, "P_CreatePlayerStart: Normal #%i plrNum=%i entryPoint=%i spot=%i",
numPlayerStarts - 1, defaultPlrNum, entryPoint, spot);
}
start->plrNum = defaultPlrNum;
start->entryPoint = entryPoint;
start->spot = spot;
}
void P_DestroyPlayerStarts()
{
if(playerStarts)
Z_Free(playerStarts);
playerStarts = 0;
numPlayerStarts = 0;
if(deathmatchStarts)
Z_Free(deathmatchStarts);
deathmatchStarts = 0;
numPlayerDMStarts = 0;
}
playerstart_t const *P_GetPlayerStart(uint entryPoint, int pnum, dd_bool deathmatch)
{
DENG_UNUSED(entryPoint);
if((deathmatch && !numPlayerDMStarts) || !numPlayerStarts)
return 0;
if(pnum < 0)
pnum = P_Random() % (deathmatch? numPlayerDMStarts:numPlayerStarts);
else
pnum = MINMAX_OF(0, pnum, MAXPLAYERS-1);
if(deathmatch)
{
// In deathmatch, entry point is ignored.
return &deathmatchStarts[pnum];
}
#if __JHEXEN__
// Client 1 should be treated like player 0.
if(IS_NETWORK_SERVER)
pnum--;
playerstart_t const *def = 0;
for(int i = 0; i < numPlayerStarts; ++i)
{
playerstart_t const *start = &playerStarts[i];
if(start->entryPoint == nextMapEntrance && start->plrNum - 1 == pnum)
return start;
if(!start->entryPoint && start->plrNum - 1 == pnum)
def = start;
}
// Return the default choice.
return def;
#else
return &playerStarts[players[pnum].startSpot];
#endif
}
uint P_GetNumPlayerStarts(dd_bool deathmatch)
{
if(deathmatch)
return numPlayerDMStarts;
return numPlayerStarts;
}
void P_DealPlayerStarts(uint entryPoint)
{
if(IS_CLIENT) return;
if(!numPlayerStarts)
{
App_Log(DE2_MAP_WARNING, "No player starts found, players will spawn as cameras");
return;
}
// First assign one start per player, only accepting perfect matches.
for(int i = (IS_NETWORK_SERVER? 1 : 0); i < MAXPLAYERS; ++i)
{
player_t *pl = &players[i];
if(!pl->plr->inGame)
continue;
// The number of the start spot this player will use.
int spotNumber = i % MAX_START_SPOTS;
// Player #1 should be treated like #0 on the server.
if(IS_NETWORK_SERVER) spotNumber--;
pl->startSpot = -1;
for(int k = 0; k < numPlayerStarts; ++k)
{
playerstart_t const *start = &playerStarts[k];
if(spotNumber == start->plrNum - 1 &&
start->entryPoint == entryPoint)
{
// A match!
pl->startSpot = k;
// Keep looking.
App_Log(DE2_DEV_MAP_XVERBOSE, "PlayerStart %i matches: spot=%i entryPoint=%i",
k, spotNumber, entryPoint);
}
}
// If still without a start spot, assign one randomly.
if(pl->startSpot == -1)
{
// It's likely that some players will get the same start spots.
pl->startSpot = M_Random() % numPlayerStarts;
}
}
App_Log(DE2_DEV_MAP_MSG, "Player starting spots:");
for(int i = 0; i < MAXPLAYERS; ++i)
{
player_t *pl = &players[i];
if(!pl->plr->inGame)
continue;
App_Log(DE2_DEV_MAP_MSG, "- pl%i: color %i, spot %i", i, cfg.playerColor[i], pl->startSpot);
}
}
void P_SpawnPlayer(int plrNum, playerclass_t pClass, coord_t x, coord_t y, coord_t z,
angle_t angle, int spawnFlags, dd_bool makeCamera, dd_bool pickupItems)
{
plrNum = MINMAX_OF(0, plrNum, MAXPLAYERS - 1);
// Not playing?
if(!players[plrNum].plr->inGame)
return;
pClass = playerclass_t(MINMAX_OF(0, pClass, NUM_PLAYER_CLASSES - 1));
/* $unifiedangles */
mobj_t *mo = P_SpawnMobjXYZ(PCLASS_INFO(pClass)->mobjType, x, y, z, angle, spawnFlags);
if(!mo)
Con_Error("P_SpawnPlayer: Failed spawning mobj for player %i "
"(class:%i) pos:[%g, %g, %g] angle:%i.", plrNum, pClass,
x, y, z, angle);
App_Log(DE2_DEV_MAP_MSG, "P_SpawnPlayer: Player #%i spawned pos:(%g, %g, %g) angle:%x floorz:%g mobjid:%i",
plrNum, mo->origin[VX], mo->origin[VY], mo->origin[VZ], mo->angle, mo->floorZ,
mo->thinker.id);
player_t *p = &players[plrNum];
if(p->playerState == PST_REBORN)
G_PlayerReborn(plrNum);
/// @todo Should this not occur before the reborn?
p->class_ = pClass;
// On clients, mark the remote players.
if(IS_CLIENT && plrNum != CONSOLEPLAYER)
{
mo->ddFlags = DDMF_DONTDRAW;
// The real flags are received from the server later on.
}
// Set color translations for player sprites.
if(p->colorMap > 0 && p->colorMap < NUMPLAYERCOLORS)
{
mo->flags |= p->colorMap << MF_TRANSSHIFT;
}
/*
if(cfg.playerColor[plrNum] > 0)
mo->flags |= cfg.playerColor[plrNum] << MF_TRANSSHIFT;
*/
App_Log(DE2_DEV_MAP_VERBOSE, "Player #%i spawning with color translation %i",
plrNum, (mo->flags & MF_TRANSLATION) >> MF_TRANSSHIFT);
p->plr->lookDir = 0; /* $unifiedangles */
p->plr->flags |= DDPF_FIXANGLES | DDPF_FIXORIGIN | DDPF_FIXMOM;
p->plr->flags &= ~DDPF_UNDEFINED_ORIGIN;
DENG_ASSERT(mo->angle == angle);
p->jumpTics = 0;
p->airCounter = 0;
mo->player = p;
mo->dPlayer = p->plr;
mo->health = p->health;
p->plr->mo = mo;
p->playerState = PST_LIVE;
p->refire = 0;
p->damageCount = 0;
p->bonusCount = 0;
#if __JHEXEN__
p->poisonCount = 0;
p->overridePalette = 0;
#endif
#if __JHERETIC__ || __JHEXEN__
p->morphTics = 0;
#endif
#if __JHERETIC__
p->rain1 = NULL;
p->rain2 = NULL;
#endif
p->plr->extraLight = 0;
p->plr->fixedColorMap = 0;
if(makeCamera)
p->plr->flags |= DDPF_CAMERA;
if(p->plr->flags & DDPF_CAMERA)
{
App_Log(DE2_MAP_MSG, "Player #%i spawned as a camera", plrNum);
p->plr->mo->origin[VZ] += (coord_t) cfg.plrViewHeight;
p->viewHeight = 0;
}
else
{
p->viewHeight = (coord_t) cfg.plrViewHeight;
}
p->viewHeightDelta = 0;
p->viewZ = p->plr->mo->origin[VZ] + p->viewHeight;
p->viewOffset[VX] = p->viewOffset[VY] = p->viewOffset[VZ] = 0;
// Give all cards in death match mode.
if(COMMON_GAMESESSION->rules().deathmatch)
{
#if __JHEXEN__
p->keys = 2047;
#else
for(int i = 0; i < NUM_KEY_TYPES; ++i)
p->keys[i] = true;
#endif
}
p->pendingWeapon = WT_NOCHANGE;
if(pickupItems)
{
// Check the current position so that any interactions which would
// occur as a result of collision happen immediately
// (e.g., weapon pickups at the current position will be collected).
P_CheckPosition(mo, mo->origin);
}
if(p->pendingWeapon != WT_NOCHANGE)
p->readyWeapon = p->pendingWeapon;
else
p->pendingWeapon = p->readyWeapon;
p->brain.changeWeapon = WT_NOCHANGE;
p->update |= PSF_READY_WEAPON | PSF_PENDING_WEAPON;
// Setup gun psprite.
P_SetupPsprites(p);
if(!BusyMode_Active())
{
// This is done each time the player spawns so that animations ran at
// this time are handled correctly (e.g., Hexen's health chain).
HU_WakeWidgets(p - players);
}
#if __JHEXEN__
// Update the player class in effect.
cfg.playerClass[plrNum] = pClass;
NetSv_SendPlayerInfo(plrNum, DDSP_ALL_PLAYERS);
P_ClassForPlayerWhenRespawning(plrNum, true /* now applied; clear change request */);
#endif
// Player has been spawned, so tell the engine where the camera is
// initially located. After this it will be updated after every game tick.
R_UpdateConsoleView(plrNum);
}
static void spawnPlayer(int plrNum, playerclass_t pClass, coord_t x, coord_t y,
coord_t z, angle_t angle, int spawnFlags, dd_bool makeCamera, dd_bool doTeleSpark,
dd_bool doTeleFrag)
{
#if __JDOOM__ || __JDOOM64__
dd_bool queueBody = (plrNum >= 0? true : false);
#endif
dd_bool pickupItems = true;
/* $voodoodolls */
if(plrNum < 0)
{
plrNum = -plrNum - 1;
pickupItems = false;
}
plrNum = MINMAX_OF(0, plrNum, MAXPLAYERS-1);
player_t *plr = &players[plrNum];
#if __JDOOM__ || __JDOOM64__
if(queueBody)
G_QueueBody(plr->plr->mo);
#endif
P_SpawnPlayer(plrNum, pClass, x, y, z, angle, spawnFlags, makeCamera, pickupItems);
// Spawn a teleport fog?
if(doTeleSpark && !makeCamera)
{
uint an = angle >> ANGLETOFINESHIFT;
x += 20 * FIX2FLT(finecosine[an]);
y += 20 * FIX2FLT(finesine[an]);
if(mobj_t *mo = P_SpawnTeleFog(x, y, angle + ANG180))
{
// Don't start sound on first frame.
if(mapTime > 1)
S_StartSound(TELEPORTSOUND, mo);
}
}
// Camera players do not telefrag.
if(!makeCamera && doTeleFrag)
P_Telefrag(plr->plr->mo);
}
/**
* Spawns the client's mobj on clientside.
*/
void P_SpawnClient(int plrNum)
{
App_Log(DE2_MAP_VERBOSE,
"Spawning client player mobj (for player %i; console player is %i)",
plrNum, CONSOLEPLAYER);
// The server will fix the player's position and angles soon after.
spawnPlayer(plrNum, P_ClassForPlayerWhenRespawning(plrNum, true),
-30000, -30000, 0, 0, MSF_Z_FLOOR, false, false, false);
player_t *p = &players[plrNum];
p->viewHeight = cfg.plrViewHeight;
p->viewHeightDelta = 0;
// The mobj was just spawned onto invalid coordinates. The view cannot
// be drawn until we receive the right coords.
p->plr->flags |= DDPF_UNDEFINED_ORIGIN;
// The weapon of the player is not known. The weapon cannot be raised
// until we know it.
p->plr->flags |= DDPF_UNDEFINED_WEAPON;
// Clear the view filter.
p->plr->flags &= ~DDPF_USE_VIEW_FILTER;
// The weapon should be in the down state when spawning.
p->pSprites[0].pos[VY] = WEAPONBOTTOM;
}
void P_RebornPlayerInMultiplayer(int plrNum)
{
if(plrNum < 0 || plrNum >= MAXPLAYERS)
return; // Wha?
playerclass_t pClass = P_ClassForPlayerWhenRespawning(plrNum, false);
player_t *p = &players[plrNum];
App_Log(DE2_DEV_MAP_MSG, "P_RebornPlayer: player %i (class %i)", plrNum, pClass);
if(p->plr->mo)
{
// First dissasociate the corpse.
p->plr->mo->player = 0;
p->plr->mo->dPlayer = 0;
}
if(G_GameState() != GS_MAP)
{
App_Log(DE2_DEV_MAP_ERROR,
"P_RebornPlayer: Game state is %i, won't spawn", G_GameState());
return; // Nothing else to do.
}
// Spawn at random spot if in death match.
if(COMMON_GAMESESSION->rules().deathmatch)
{
G_DeathMatchSpawnPlayer(plrNum);
return;
}
// Save player state?
#if __JHEXEN__
int oldKeys = 0, oldPieces = 0;
dd_bool oldWeaponOwned[NUM_WEAPON_TYPES];
if(!IS_CLIENT)
{
// Cooperative net-play, retain keys and weapons
oldKeys = p->keys;
oldPieces = p->pieces;
for(int i = 0; i < NUM_WEAPON_TYPES; ++i)
oldWeaponOwned[i] = p->weapons[i].owned;
}
#endif
if(IS_CLIENT)
{
P_SpawnClient(plrNum);
return;
}
/*
* Determine spawn parameters.
*/
coord_t pos[3] = { 0, 0, 0 };
angle_t angle = 0;
int spawnFlags = 0;
dd_bool makeCamera = false;
uint entryPoint = gameMapEntrance;
dd_bool foundSpot = false;
playerstart_t const *assigned = P_GetPlayerStart(entryPoint, plrNum, false);
if(assigned)
{
mapspot_t const *spot = &mapSpots[assigned->spot];
if(P_CheckSpot(spot->origin[VX], spot->origin[VY]))
{
// Appropriate player start spot is open.
App_Log(DE2_DEV_MAP_MSG, "- spawning at assigned spot");
pos[VX] = spot->origin[VX];
pos[VY] = spot->origin[VY];
pos[VZ] = spot->origin[VZ];
angle = spot->angle;
spawnFlags = spot->flags;
foundSpot = true;
}
}
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
if(!foundSpot)
{
App_Log(DE2_DEV_MAP_MSG, "- force spawning at %i", p->startSpot);
if(assigned)
{
mapspot_t const *spot = &mapSpots[assigned->spot];
pos[VX] = spot->origin[VX];
pos[VY] = spot->origin[VY];
pos[VZ] = spot->origin[VZ];
angle = spot->angle;
spawnFlags = spot->flags;
// "Fuzz" the spawn position looking for room nearby.
makeCamera = !fuzzySpawnPosition(&pos[VX], &pos[VY], &pos[VZ],
&angle, &spawnFlags);
}
else
{
pos[VX] = pos[VY] = pos[VZ] = 0;
angle = 0;
spawnFlags = MSF_Z_FLOOR;
makeCamera = true;
}
}
#else
if(!foundSpot)
{
App_Log(DE2_DEV_MAP_MSG, "P_RebornPlayer: Trying other spots for %i", plrNum);
// Try to spawn at one of the other player start spots.
for(int i = 0; i < MAXPLAYERS; ++i)
{
if(playerstart_t const *start = P_GetPlayerStart(gameMapEntrance, i, false))
{
mapspot_t const *spot = &mapSpots[start->spot];
if(P_CheckSpot(spot->origin[VX], spot->origin[VY]))
{
// Found an open start spot.
pos[VX] = spot->origin[VX];
pos[VY] = spot->origin[VY];
pos[VZ] = spot->origin[VZ];
angle = spot->angle;
spawnFlags = spot->flags;
foundSpot = true;
App_Log(DE2_DEV_MAP_MSG, "P_RebornPlayer: Spot (%g, %g) selected",
spot->origin[VX], spot->origin[VY]);
break;
}
App_Log(DE2_DEV_MAP_VERBOSE, "P_RebornPlayer: Spot (%g, %g) is unavailable",
spot->origin[VX], spot->origin[VY]);
}
}
}
if(!foundSpot)
{
// Player's going to be inside something.
if(playerstart_t const *start = P_GetPlayerStart(gameMapEntrance, plrNum, false))
{
mapspot_t const *spot = &mapSpots[start->spot];
pos[VX] = spot->origin[VX];
pos[VY] = spot->origin[VY];
pos[VZ] = spot->origin[VZ];
angle = spot->angle;
spawnFlags = spot->flags;
}
else
{
pos[VX] = pos[VY] = pos[VZ] = 0;
angle = 0;
spawnFlags = MSF_Z_FLOOR;
makeCamera = true;
}
}
#endif
App_Log(DE2_DEV_MAP_NOTE, "Multiplayer-spawning player at (%f,%f,%f) angle:%x",
pos[VX], pos[VY], pos[VZ], angle);
spawnPlayer(plrNum, pClass, pos[VX], pos[VY], pos[VZ], angle,
spawnFlags, makeCamera, true, true);
DENG_ASSERT(!IS_CLIENT);
// Restore player state?
#if __JHEXEN__
p->keys = oldKeys;
p->pieces = oldPieces;
int bestWeapon = 0;
for(int i = 0; i < NUM_WEAPON_TYPES; ++i)
{
if(oldWeaponOwned[i])
{
bestWeapon = i;
p->weapons[i].owned = true;
}
}
GetDefInt("Multiplayer|Reborn|Blue mana", &p->ammo[AT_BLUEMANA].owned);
GetDefInt("Multiplayer|Reborn|Green mana", &p->ammo[AT_GREENMANA].owned);
App_Log(DE2_MAP_VERBOSE, "Player %i reborn in multiplayer: giving mana (b:%i g:%i); "
"also old weapons, with best weapon %i", plrNum, p->ammo[AT_BLUEMANA].owned,
p->ammo[AT_GREENMANA].owned, bestWeapon);
if(bestWeapon)
{
// Bring up the best weapon.
p->readyWeapon = p->pendingWeapon = weapontype_t(bestWeapon);
}
#endif
}
dd_bool P_CheckSpot(coord_t x, coord_t y)
{
#if __JHEXEN__
#define DUMMY_TYPE MT_PLAYER_FIGHTER
#else
#define DUMMY_TYPE MT_PLAYER
#endif
// Create a dummy to test with.
coord_t const pos[3] = { x, y, 0 };
mobj_t *dummy = P_SpawnMobj(DUMMY_TYPE, pos, 0, MSF_Z_FLOOR);
if(!dummy) Con_Error("P_CheckSpot: Failed creating dummy mobj.");
dummy->flags &= ~MF_PICKUP;
//dummy->flags2 &= ~MF2_PASSMOBJ;
dd_bool result = P_CheckPosition(dummy, pos);
P_MobjRemove(dummy, true);
return result;
#undef DUMMY_TYPE
}
#if __JHERETIC__
void P_AddMaceSpot(mapspotid_t id)
{
App_Log(DE2_DEV_MAP_VERBOSE, "P_AddMaceSpot: Added mace spot %u", id);
maceSpots = (mapspotid_t *)Z_Realloc(maceSpots, sizeof(mapspotid_t) * ++maceSpotCount, PU_MAP);
maceSpots[maceSpotCount-1] = id;
}
void P_AddBossSpot(mapspotid_t id)
{
bossSpots = (mapspotid_t *)Z_Realloc(bossSpots, sizeof(mapspotid_t) * ++bossSpotCount, PU_MAP);
bossSpots[bossSpotCount-1] = id;
}
#endif
void P_SpawnPlayers()
{
if(IS_CLIENT)
{
for(int i = 0; i < MAXPLAYERS; ++i)
{
if(!players[i].plr->inGame)
continue;
// Spawn the client anywhere.
P_SpawnClient(i);
}
return;
}
// If deathmatch, randomly spawn the active players.
if(COMMON_GAMESESSION->rules().deathmatch)
{
for(int i = 0; i < MAXPLAYERS; ++i)
{
if(!players[i].plr->inGame)
continue;
players[i].plr->mo = 0;
G_DeathMatchSpawnPlayer(i);
}
}
else
{
#if __JDOOM__ || __JDOOM64__ || __JHERETIC__
if(!IS_NETGAME)
{
/* $voodoodolls */
for(int i = 0; i < numPlayerStarts; ++i)
{
if(players[0].startSpot != i && playerStarts[i].plrNum == 1)
{
mapspot_t const *spot = &mapSpots[playerStarts[i].spot];
spawnPlayer(-1, PCLASS_PLAYER, spot->origin[VX],
spot->origin[VY], spot->origin[VZ],
spot->angle, spot->flags, false,
false, false);
}
}
}
#endif
// Spawn everybody at their assigned places.
// Might get messy if there aren't enough starts.
for(int i = 0; i < MAXPLAYERS; ++i)
{
player_t *plr = &players[i];
if(!plr->plr->inGame)
continue;
playerstart_t const *start =
plr->startSpot < numPlayerStarts? &playerStarts[plr->startSpot] : 0;
coord_t pos[3];
angle_t angle;
int spawnFlags;
dd_bool spawnAsCamera;