/
d_net.cpp
717 lines (600 loc) · 18.5 KB
/
d_net.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
/** @file d_net.cpp Common code related to net games.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @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, see:
* http://www.gnu.org/licenses</small>
*/
#include "common.h"
#include "d_net.h"
#include "g_common.h"
#include "gamesession.h"
#include "player.h"
#include "hu_menu.h"
#include "p_start.h"
#include "fi_lib.h"
using namespace common;
D_CMD(SetColor);
#if __JHEXEN__
D_CMD(SetClass);
#endif
D_CMD(LocalMessage);
static void D_NetMessageEx(int player, char const *msg, dd_bool playSound);
float netJumpPower = 9;
static Writer *netWriter;
static Reader *netReader;
static void notifyAllowCheatsChange()
{
if(IS_NETGAME && IS_NETWORK_SERVER && G_GameState() != GS_STARTUP)
{
AutoStr *msg = Str_Appendf(AutoStr_NewStd(), "--- CHEATS NOW %s ON THIS SERVER ---",
netSvAllowCheats? "ENABLED" : "DISABLED");
NetSv_SendMessage(DDSP_ALL_PLAYERS, Str_Text(msg));
}
}
void D_NetConsoleRegistration()
{
C_VAR_CHARPTR("mapcycle", &mapCycle, CVF_HIDE | CVF_NO_ARCHIVE, 0, 0);
C_VAR_CHARPTR("server-game-mapcycle", &mapCycle, 0, 0, 0);
C_VAR_BYTE ("server-game-mapcycle-noexit", &mapCycleNoExit, 0, 0, 1);
C_VAR_INT2 ("server-game-cheat", &netSvAllowCheats, 0, 0, 1, notifyAllowCheatsChange);
C_CMD ("setcolor", "i", SetColor);
#if __JHEXEN__
C_CMD_FLAGS ("setclass", "i", SetClass, CMDF_NO_DEDICATED);
#endif
C_CMD ("startcycle", "", MapCycle);
C_CMD ("endcycle", "", MapCycle);
C_CMD ("message", "s", LocalMessage);
}
Writer *D_NetWrite()
{
if(netWriter)
{
Writer_Delete(netWriter);
}
netWriter = Writer_NewWithDynamicBuffer(0 /*unlimited*/);
return netWriter;
}
Reader *D_NetRead(byte const *buffer, size_t len)
{
// Get rid of the old reader.
if(netReader)
{
Reader_Delete(netReader);
}
netReader = Reader_NewWithBuffer(buffer, len);
return netReader;
}
void D_NetClearBuffer()
{
if(netReader) Reader_Delete(netReader);
if(netWriter) Writer_Delete(netWriter);
netReader = 0;
netWriter = 0;
}
int D_NetServerStarted(int before)
{
if(before) return true;
// We're the server, so...
cfg.playerColor[0] = PLR_COLOR(0, cfg.netColor);
#if __JHEXEN__
cfg.playerClass[0] = playerclass_t(cfg.netClass);
#elif __JHERETIC__
cfg.playerClass[0] = PCLASS_PLAYER;
#endif
P_ResetPlayerRespawnClasses();
#if __JDOOM64__
uint netEpisode = 0;
#else
uint netEpisode = cfg.netEpisode;
#endif
#if __JHEXEN__ // Map numbers need to be translated.
uint netMap = P_TranslateMap(cfg.netMap);
#else
uint netMap = cfg.netMap;
#endif
Uri *netMapUri = G_ComposeMapUri(netEpisode, netMap);
GameRuleset netRules(COMMON_GAMESESSION->rules()); // Make a copy of the current rules.
netRules.skill = skillmode_t(cfg.netSkill);
COMMON_GAMESESSION->end();
COMMON_GAMESESSION->begin(*netMapUri, 0/*default*/, netRules);
G_SetGameAction(GA_NONE); /// @todo Necessary?
Uri_Delete(netMapUri);
return true;
}
int D_NetServerClose(int before)
{
if(!before)
{
P_ResetPlayerRespawnClasses();
// Restore normal game state.
/// @todo fixme: "normal" is defined by the game rules config which may
/// be changed from the command line (e.g., -fast, -nomonsters).
/// In order to "restore normal" this logic is insufficient.
GameRuleset newRules(COMMON_GAMESESSION->rules());
newRules.deathmatch = false;
newRules.noMonsters = false;
#if __JHEXEN__
newRules.randomClasses = false;
#endif
COMMON_GAMESESSION->applyNewRules(newRules);
D_NetMessage(CONSOLEPLAYER, "NETGAME ENDS");
D_NetClearBuffer();
}
return true;
}
int D_NetConnect(int before)
{
if(before)
{
BusyMode_FreezeGameForBusyMode();
return true;
}
// After connecting we tell the server a bit about ourselves.
NetCl_SendPlayerInfo();
// Close the menu, the game begins!!
// advancedemo = false;
Hu_MenuCommand(MCMD_CLOSE);
return true;
}
int D_NetDisconnect(int before)
{
if(before) return true;
D_NetClearBuffer();
// Start demo.
COMMON_GAMESESSION->endAndBeginTitle();
// Restore normal game state.
GameRuleset newRules(COMMON_GAMESESSION->rules());
newRules.deathmatch = false;
newRules.noMonsters = false;
#if __JHEXEN__
newRules.randomClasses = false;
#endif
COMMON_GAMESESSION->applyNewRules(newRules);
return true;
}
long int D_NetPlayerEvent(int plrNumber, int peType, void *data)
{
// If this isn't a netgame, we won't react.
if(!IS_NETGAME)
return true;
if(peType == DDPE_ARRIVAL)
{
dd_bool showmsg = true;
if(IS_SERVER)
{
NetSv_NewPlayerEnters(plrNumber);
}
else if(plrNumber == CONSOLEPLAYER)
{
// We have arrived, the game should be begun.
App_Log(DE2_NET_NOTE, "Arrived in netgame, waiting for data...");
G_ChangeGameState(GS_WAITING);
showmsg = false;
}
else
{
// Client responds to new player?
App_Log(DE2_LOG_NOTE, "Player %i has arrived in the game", plrNumber);
P_RebornPlayerInMultiplayer(plrNumber);
//players[plrNumber].playerstate = PST_REBORN;
}
if(showmsg)
{
// Print a notification.
AutoStr *str = AutoStr_New();
Str_Appendf(str, "%s joined the game", Net_GetPlayerName(plrNumber));
D_NetMessage(CONSOLEPLAYER, Str_Text(str));
}
}
else if(peType == DDPE_EXIT)
{
AutoStr *str = AutoStr_New();
App_Log(DE2_LOG_NOTE, "Player %i has left the game", plrNumber);
players[plrNumber].playerState = playerstate_t(PST_GONE);
// Print a notification.
Str_Appendf(str, "%s left the game", Net_GetPlayerName(plrNumber));
D_NetMessage(CONSOLEPLAYER, Str_Text(str));
if(IS_SERVER)
{
P_DealPlayerStarts(0);
}
}
// DDPE_CHAT_MESSAGE occurs when a PKT_CHAT is received.
// Here we will only display the message.
else if(peType == DDPE_CHAT_MESSAGE)
{
int oldecho = cfg.echoMsg;
AutoStr *msg = AutoStr_New();
if(plrNumber > 0)
{
Str_Appendf(msg, "%s: %s", Net_GetPlayerName(plrNumber), (char const *) data);
}
else
{
Str_Appendf(msg, "[sysop] %s", (char const *) data);
}
Str_Truncate(msg, NETBUFFER_MAXMESSAGE); // not overly long, please
// The chat message is already echoed by the console.
cfg.echoMsg = false;
D_NetMessageEx(CONSOLEPLAYER, Str_Text(msg), (cfg.chatBeep? true : false));
cfg.echoMsg = oldecho;
}
return true;
}
int D_NetWorldEvent(int type, int parm, void *data)
{
switch(type)
{
//
// Server events:
//
case DDWE_HANDSHAKE: {
dd_bool newPlayer = *((dd_bool*) data);
// A new player is entering the game. We as a server should send him
// the handshake packet(s) to update his world.
// If 'data' is zero, this is a re-handshake that's used to
// begin demos.
App_Log(DE2_DEV_NET_MSG, "Sending a game state %shandshake to player %i",
newPlayer? "" : "(re)", parm);
// Mark new player for update.
players[parm].update |= PSF_REBORN;
// First, the game state.
NetSv_SendGameState(GSF_CHANGE_MAP | GSF_CAMERA_INIT | (newPlayer ? 0 : GSF_DEMO), parm);
// Send info about all players to the new one.
for(int i = 0; i < MAXPLAYERS; ++i)
{
if(players[i].plr->inGame && i != parm)
NetSv_SendPlayerInfo(i, parm);
}
// Send info about our jump power.
NetSv_SendJumpPower(parm, cfg.jumpEnabled ? cfg.jumpPower : 0);
NetSv_Paused(paused);
break; }
//
// Client events:
//
#if 0
case DDWE_SECTOR_SOUND:
// High word: sector number, low word: sound id.
if(parm & 0xffff)
S_SectorSound(P_ToPtr(DMU_SECTOR, parm >> 16), parm & 0xffff);
else
S_SectorStopSounds(P_ToPtr(DMU_SECTOR, parm >> 16));
break;
case DDWE_DEMO_END:
// Demo playback has ended. Advance demo sequence.
if(parm) // Playback was aborted.
G_DemoAborted();
else // Playback ended normally.
G_DemoEnds();
// Restore normal game state.
COMMON_GAMESESSION->rules().deathmatch = false;
COMMON_GAMESESSION->rules().noMonsters = false;
#if __JDOOM__ || __JHERETIC__ || __JDOOM64__
COMMON_GAMESESSION->rules().respawnMonsters = false;
#endif
#if __JHEXEN__
COMMON_GAMESESSION->rules().randomClasses = false;
#endif
break;
#endif
default:
return false;
}
return true;
}
void D_HandlePacket(int fromplayer, int type, void *data, size_t length)
{
Reader *reader = D_NetRead((byte *)data, length);
//
// Server events.
//
if(IS_SERVER)
{
switch (type)
{
case GPT_PLAYER_INFO:
// A player has changed color or other settings.
NetSv_ChangePlayerInfo(fromplayer, reader);
break;
case GPT_CHEAT_REQUEST:
NetSv_DoCheat(fromplayer, reader);
break;
case GPT_FLOOR_HIT_REQUEST:
NetSv_DoFloorHit(fromplayer, reader);
break;
case GPT_ACTION_REQUEST:
NetSv_DoAction(fromplayer, reader);
break;
case GPT_DAMAGE_REQUEST:
NetSv_DoDamage(fromplayer, reader);
break;
}
return;
}
//
// Client events.
//
switch(type)
{
case GPT_GAME_STATE:
App_Log(DE2_DEV_NET_MSG, "Received GTP_GAME_STATE");
NetCl_UpdateGameState(reader);
// Tell the engine we're ready to proceed. It'll start handling
// the world updates after this variable is set.
Set(DD_GAME_READY, true);
break;
case GPT_PLAYER_SPAWN_POSITION:
NetCl_PlayerSpawnPosition(reader);
break;
case GPT_TOTAL_COUNTS:
NetCl_UpdateTotalCounts(reader);
break;
case GPT_MOBJ_IMPULSE:
NetCl_MobjImpulse(reader);
break;
case GPT_LOCAL_MOBJ_STATE:
NetCl_LocalMobjState(reader);
break;
case GPT_MESSAGE:
#if __JHEXEN__
case GPT_YELLOW_MESSAGE:
#endif
{
size_t len = Reader_ReadUInt16(reader);
char *msg = (char *)Z_Malloc(len + 1, PU_GAMESTATIC, 0);
Reader_Read(reader, msg, len); msg[len] = 0;
#if __JHEXEN__
if(type == GPT_YELLOW_MESSAGE)
{
P_SetYellowMessage(&players[CONSOLEPLAYER], 0, msg);
}
else
#endif
{
P_SetMessage(&players[CONSOLEPLAYER], 0, msg);
}
Z_Free(msg);
break; }
case GPT_MAYBE_CHANGE_WEAPON: {
weapontype_t wt = (weapontype_t) Reader_ReadInt16(reader);
ammotype_t at = (ammotype_t) Reader_ReadInt16(reader);
dd_bool force = (Reader_ReadByte(reader) != 0);
P_MaybeChangeWeapon(&players[CONSOLEPLAYER], wt, at, force);
break; }
case GPT_CONSOLEPLAYER_STATE:
NetCl_UpdatePlayerState(reader, CONSOLEPLAYER);
break;
case GPT_CONSOLEPLAYER_STATE2:
NetCl_UpdatePlayerState2(reader, CONSOLEPLAYER);
break;
case GPT_PLAYER_STATE:
NetCl_UpdatePlayerState(reader, -1);
break;
case GPT_PLAYER_STATE2:
NetCl_UpdatePlayerState2(reader, -1);
break;
case GPT_PSPRITE_STATE:
NetCl_UpdatePSpriteState(reader);
break;
case GPT_INTERMISSION:
NetCl_Intermission(reader);
break;
case GPT_FINALE_STATE:
NetCl_UpdateFinaleState(reader);
break;
case GPT_PLAYER_INFO:
NetCl_UpdatePlayerInfo(reader);
break;
#if __JHERETIC__ || __JHEXEN__ || __JSTRIFE__
case GPT_CLASS: {
player_t *plr = &players[CONSOLEPLAYER];
playerclass_t newClass = playerclass_t(Reader_ReadByte(reader));
# if __JHERETIC__
playerclass_t oldClass = plr->class_;
# endif
plr->class_ = newClass;
App_Log(DE2_DEV_MAP_MSG, "Player %i class changed to %i", CONSOLEPLAYER, plr->class_);
# if __JHERETIC__
if(oldClass != newClass)
{
if(newClass == PCLASS_CHICKEN)
{
App_Log(DE2_DEV_MAP_VERBOSE, "Player %i activating morph", CONSOLEPLAYER);
P_ActivateMorphWeapon(plr);
}
else if(oldClass == PCLASS_CHICKEN)
{
App_Log(DE2_DEV_MAP_VERBOSE, "Player %i post-morph weapon %i", CONSOLEPLAYER, plr->readyWeapon);
// The morph has ended.
P_PostMorphWeapon(plr, plr->readyWeapon);
}
}
# endif
break; }
#endif
case GPT_SAVE:
NetCl_SaveGame(reader);
break;
case GPT_LOAD:
NetCl_LoadGame(reader);
break;
case GPT_PAUSE:
NetCl_Paused(reader);
break;
case GPT_JUMP_POWER:
NetCl_UpdateJumpPower(reader);
break;
default:
App_Log(DE2_NET_WARNING, "Game received unknown packet (type:%i)", type);
}
}
/**
* Plays a (local) chat sound.
*/
void D_ChatSound()
{
#if __JHEXEN__
S_LocalSound(SFX_CHAT, NULL);
#elif __JSTRIFE__
S_LocalSound(SFX_CHAT, NULL);
#elif __JHERETIC__
S_LocalSound(SFX_CHAT, NULL);
#else
# if __JDOOM__
if(gameModeBits & GM_ANY_DOOM2)
S_LocalSound(SFX_RADIO, NULL);
else
# endif
S_LocalSound(SFX_TINK, NULL);
#endif
}
/**
* Show a message on screen, optionally accompanied by Chat sound effect.
*
* @param player Player number to send the message to.
* @param playSound @c true = play the chat sound.
*/
static void D_NetMessageEx(int player, char const *msg, dd_bool playSound)
{
if(player < 0 || player > MAXPLAYERS) return;
player_t *plr = &players[player];
if(!plr->plr->inGame)
return;
// This is intended to be a local message.
// Let's make sure P_SetMessage doesn't forward it anywhere.
netSvAllowSendMsg = false;
P_SetMessage(plr, 0, msg);
if(playSound)
{
D_ChatSound();
}
netSvAllowSendMsg = true;
}
void D_NetMessage(int player, char const *msg)
{
D_NetMessageEx(player, msg, true);
}
void D_NetMessageNoSound(int player, char const *msg)
{
D_NetMessageEx(player, msg, false);
}
dd_bool D_NetDamageMobj(mobj_t *target, mobj_t *inflictor, mobj_t *source, int damage)
{
int sourcePlrNum = -1;
if(source && source->player)
{
sourcePlrNum = source->player - players;
}
if(source && !source->player)
{
// Not applicable: only damage from players.
return false;
}
if(IS_SERVER && sourcePlrNum > 0)
{
/*
* A client is trying to do damage. However, it is not guaranteed that the server is 100%
* accurately aware of the gameplay situation in which the damage is being inflicted (due
* to network latency), so instead of applying the damage now we will wait for the client
* to request it separately.
*/
return false;
}
else if(IS_CLIENT)
{
if((sourcePlrNum < 0 || sourcePlrNum == CONSOLEPLAYER) &&
target && target->player && target->player - players == CONSOLEPLAYER)
{
// Clients are allowed to damage themselves.
NetCl_DamageRequest(ClPlayer_ClMobj(CONSOLEPLAYER), inflictor, source, damage);
// No further processing of this damage is needed.
return true;
}
}
return false;
}
/**
* Console command to change the players' colors.
*/
D_CMD(SetColor)
{
DENG2_UNUSED2(src, argc);
cfg.netColor = atoi(argv[1]);
if(IS_SERVER) // A local player?
{
if(IS_DEDICATED) return false;
int player = CONSOLEPLAYER;
// Server players, must be treated as a special case because this is
// a local mobj we're dealing with. We'll change the color translation
// bits directly.
cfg.playerColor[player] = PLR_COLOR(player, cfg.netColor);
players[player].colorMap = cfg.playerColor[player];
if(players[player].plr->mo)
{
// Change the color of the mobj (translation flags).
players[player].plr->mo->flags &= ~MF_TRANSLATION;
players[player].plr->mo->flags |= (cfg.playerColor[player] << MF_TRANSSHIFT);
}
// Tell the clients about the change.
NetSv_SendPlayerInfo(player, DDSP_ALL_PLAYERS);
}
else
{
// Tell the server about the change.
NetCl_SendPlayerInfo();
}
return true;
}
/**
* Console command to change the players' class.
*/
#if __JHEXEN__
D_CMD(SetClass)
{
DENG2_UNUSED2(src, argc);
playerclass_t newClass = playerclass_t(atoi(argv[1]));
if(!(newClass < NUM_PLAYER_CLASSES))
{
return false;
}
if(!PCLASS_INFO(newClass)->userSelectable)
{
return false;
}
cfg.netClass = newClass; // Stored as a cvar.
if(IS_CLIENT)
{
// Tell the server that we want to change our class.
NetCl_SendPlayerInfo();
}
else
{
// On the server (or singleplayer) we can do an immediate change.
P_PlayerChangeClass(&players[CONSOLEPLAYER], playerclass_t(cfg.netClass));
}
return true;
}
#endif
/**
* Post a local game message.
*/
D_CMD(LocalMessage)
{
DENG2_UNUSED2(src, argc);
D_NetMessageNoSound(CONSOLEPLAYER, argv[1]);
return true;
}