/
sv_main.cpp
1192 lines (1010 loc) · 33.7 KB
/
sv_main.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 sv_main.cpp Network server.
* @ingroup server
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include <math.h>
#define DENG_NO_API_MACROS_SERVER
#include "api_server.h"
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_filesys.h"
#include "de_network.h"
#include "de_play.h"
#include "de_misc.h"
#include "de_defs.h"
#include "api_materialarchive.h"
#include "map/r_world.h"
#include <de/ArrayValue>
#include <de/NumberValue>
#include <de/Log>
// This is absolute maximum bandwidth rating. Frame size is practically
// unlimited with this score.
#define MAX_BANDWIDTH_RATING 100
// When the difference between clientside and serverside positions is this
// much, server will update its position to match the clientside position,
// which is assumed to be correct.
#define WARP_LIMIT 300
void Sv_ClientCoords(int playerNum);
int netRemoteUser = 0; // The client who is currently logged in.
char *netPassword = (char *) ""; // Remote login password.
// This is the limit when accepting new clients.
int svMaxPlayers = DDMAXPLAYERS;
static MaterialArchive* materialDict;
/**
* Fills the provided struct with information about the local server.
*/
void Sv_GetInfo(serverinfo_t* info)
{
AutoStr* mapPath;
int i;
assert(theMap);
memset(info, 0, sizeof(*info));
// Let's figure out what we want to tell about ourselves.
info->version = DOOMSDAY_VERSION;
dd_snprintf(info->plugin, sizeof(info->plugin) - 1, "%s %s", (char*) gx.GetVariable(DD_PLUGIN_NAME), (char*) gx.GetVariable(DD_PLUGIN_VERSION_SHORT));
strncpy(info->gameIdentityKey, Str_Text(App_CurrentGame().identityKey()), sizeof(info->gameIdentityKey) - 1);
strncpy(info->gameConfig, (char const *) gx.GetVariable(DD_GAME_CONFIG), sizeof(info->gameConfig) - 1);
strncpy(info->name, serverName, sizeof(info->name) - 1);
strncpy(info->description, serverInfo, sizeof(info->description) - 1);
info->numPlayers = Sv_GetNumPlayers();
// The server player is there, it's just hidden.
info->maxPlayers = DDMAXPLAYERS - (isDedicated ? 1 : 0);
// Don't go over the limit.
if(info->maxPlayers > svMaxPlayers)
info->maxPlayers = svMaxPlayers;
info->canJoin = (isServer != 0 && Sv_GetNumPlayers() < svMaxPlayers);
// Identifier of the current map.
mapPath = Uri_Resolved(GameMap_Uri(theMap));
strncpy(info->map, Str_Text(mapPath), sizeof(info->map) - 1);
// These are largely unused at the moment... Mainly intended for
// the game's custom values.
memcpy(info->data, serverData, sizeof(info->data));
// Also include the port we're using.
info->port = nptIPPort;
// Let's compile a list of client names.
for(i = 0; i < DDMAXPLAYERS; ++i)
{
if(clients[i].connected)
M_LimitedStrCat(info->clientNames, clients[i].name, 15, ';', sizeof(info->clientNames));
}
// Some WAD names.
F_ComposePWADFileList(info->pwads, sizeof(info->pwads), ";");
// This should be a CRC number that describes all the loaded data.
info->loadedFilesCRC = F_LoadedFilesCRC();
}
de::Record *Sv_InfoToRecord(serverinfo_t *info)
{
de::Record *rec = new de::Record;
rec->addNumber ("port", info->port);
rec->addText ("name", info->name);
rec->addText ("info", info->description);
rec->addNumber ("ver", info->version);
rec->addText ("game", info->plugin);
rec->addText ("mode", info->gameIdentityKey);
rec->addText ("setup", info->gameConfig);
rec->addText ("iwad", info->iwad);
rec->addNumber ("wcrc", info->loadedFilesCRC);
rec->addText ("pwads", info->pwads);
rec->addText ("map", info->map);
rec->addNumber ("nump", info->numPlayers);
rec->addNumber ("maxp", info->maxPlayers);
rec->addBoolean("open", info->canJoin);
rec->addText ("plrn", info->clientNames);
de::ArrayValue &data = rec->addArray("data").value<de::ArrayValue>();
for(uint i = 0; i < sizeof(info->data) / sizeof(info->data[0]); ++i)
{
data << de::NumberValue(info->data[i]);
}
return rec;
}
/**
* @return Length of the string.
*/
size_t Sv_InfoToString(serverinfo_t* info, ddstring_t* msg)
{
unsigned int i;
Str_Appendf(msg, "port:%i\n", info->port);
Str_Appendf(msg, "name:%s\n", info->name);
Str_Appendf(msg, "info:%s\n", info->description);
Str_Appendf(msg, "ver:%i\n", info->version);
Str_Appendf(msg, "game:%s\n", info->plugin);
Str_Appendf(msg, "mode:%s\n", info->gameIdentityKey);
Str_Appendf(msg, "setup:%s\n", info->gameConfig);
Str_Appendf(msg, "iwad:%s\n", info->iwad);
Str_Appendf(msg, "wcrc:%i\n", info->loadedFilesCRC);
Str_Appendf(msg, "pwads:%s\n", info->pwads);
Str_Appendf(msg, "map:%s\n", info->map);
Str_Appendf(msg, "nump:%i\n", info->numPlayers);
Str_Appendf(msg, "maxp:%i\n", info->maxPlayers);
Str_Appendf(msg, "open:%i\n", info->canJoin);
Str_Appendf(msg, "plrn:%s\n", info->clientNames);
for(i = 0; i < sizeof(info->data) / sizeof(info->data[0]); ++i)
{
Str_Appendf(msg, "data%i:%x\n", i, info->data[i]);
}
return Str_Length(msg);
}
/**
* @return gametic - cmdtime.
*/
int Sv_Latency(byte cmdtime)
{
return Net_TimeDelta(SECONDS_TO_TICKS(gameTime), cmdtime);
}
/**
* For local players.
*/
/* $unifiedangles */
/*
void Sv_FixLocalAngles(boolean clearFixAnglesFlag)
{
ddplayer_t *pl;
int i;
for(i = 0; i < DDMAXPLAYERS; ++i)
{
pl = players + i;
if(!pl->inGame || !(pl->flags & DDPF_LOCAL))
continue;
// This is not for clients.
if(isDedicated && i == 0)
continue;
if(pl->flags & DDPF_FIXANGLES)
{
if(clearFixAnglesFlag)
{
pl->flags &= ~DDPF_FIXANGLES;
}
else
{
pl->clAngle = pl->mo->angle;
pl->clLookDir = pl->lookDir;
}
}
}
}
*/
void Sv_HandlePlayerInfoFromClient(client_t* sender)
{
int console = Reader_ReadByte(msgReader); // ignored
char oldName[PLAYERNAMELEN];
size_t len;
assert(netBuffer.player == (sender - clients));
#ifdef _DEBUG
Con_Message("Sv_HandlePlayerInfoFromClient: from=%i, console=%i", netBuffer.player, console);
#endif
console = netBuffer.player;
strcpy(oldName, sender->name);
len = Reader_ReadUInt16(msgReader);
len = MIN_OF(PLAYERNAMELEN - 1, len); // there is a maximum size
Reader_Read(msgReader, sender->name, len);
sender->name[len] = 0;
Con_FPrintf(CPF_TRANSMIT | SV_CONSOLE_PRINT_FLAGS, "%s renamed to %s.\n", oldName, sender->name);
// Relay to others.
Net_SendPlayerInfo(console, DDSP_ALL_PLAYERS);
}
/**
* Handles a server-specific network message. Assumes that Msg_BeginRead()
* has already been called to begin reading the message.
*/
void Sv_HandlePacket(void)
{
ident_t id;
int i, mask, from = netBuffer.player;
player_t *plr = &ddPlayers[from];
ddplayer_t *ddpl = &plr->shared;
client_t *sender = &clients[from];
int msgfrom;
char *msg;
char buf[17];
size_t len;
switch(netBuffer.msg.type)
{
case PCL_HELLO:
case PCL_HELLO2:
// Get the ID of the client.
id = Reader_ReadUInt32(msgReader);
Con_Printf("Sv_HandlePacket: Hello from client %i (%08X).\n",
from, id);
// Check for duplicate IDs.
if(!ddpl->inGame && !sender->handshake)
{
// Console 0 is always reserved for the server itself (not a player).
for(i = 1; i < DDMAXPLAYERS; ++i)
{
if(clients[i].connected && clients[i].id == id)
{
// Send a message to everybody.
Con_FPrintf(CPF_TRANSMIT | SV_CONSOLE_PRINT_FLAGS,
"New client connection refused: Duplicate ID "
"(%08x). From=%i, i=%i\n", id, from, i);
N_TerminateClient(from);
break;
}
}
if(i < DDMAXPLAYERS)
break; // Can't continue, refused!
}
// This is OK.
sender->id = id;
if(netBuffer.msg.type == PCL_HELLO2)
{
// Check the game mode (max 16 chars).
Reader_Read(msgReader, buf, 16);
if(strnicmp(buf, Str_Text(App_CurrentGame().identityKey()), 16))
{
Con_Printf(" Bad Game ID: %-.16s\n", buf);
N_TerminateClient(from);
break;
}
}
// The client requests a handshake.
if(!ddpl->inGame && !sender->handshake)
{
// This'll be true until the client says it's ready.
sender->handshake = true;
// The player is now in the game.
ddPlayers[from].shared.inGame = true;
// Tell the game about this.
gx.NetPlayerEvent(from, DDPE_ARRIVAL, 0);
// Send the handshake packets.
Sv_Handshake(from, true);
// Note the time when the player entered.
sender->enterTime = Timer_RealSeconds();
}
else if(ddpl->inGame)
{
// The player is already in the game but requests a new
// handshake. Perhaps it's starting to record a demo.
Sv_Handshake(from, false);
}
break;
case PKT_OK:
// The client says it's ready to receive frames.
sender->ready = true;
#ifdef _DEBUG
Con_Printf("Sv_HandlePacket: OK (\"ready!\") from client %i "
"(%08X).\n", from, sender->id);
#endif
if(sender->handshake)
{
// The handshake is complete. The client has acknowledged it
// and sends its regards.
sender->handshake = false;
// Send a clock sync message.
Msg_Begin(PSV_SYNC);
Writer_WriteFloat(msgWriter, gameTime);
Msg_End();
Net_SendBuffer(from, 0);
// Send welcome string.
Sv_SendText(from, SV_CONSOLE_PRINT_FLAGS, SV_WELCOME_STRING "\n");
}
break;
case PKT_CHAT:
// The first byte contains the sender.
msgfrom = Reader_ReadByte(msgReader);
// Is the message for us?
mask = Reader_ReadUInt32(msgReader);
// Copy the message into a buffer.
len = Reader_ReadUInt16(msgReader);
msg = (char *) M_Malloc(len + 1);
Reader_Read(msgReader, msg, len);
msg[len] = 0;
// Message for us? Show it locally.
if(mask & 1)
{
Net_ShowChatMessage(msgfrom, msg);
gx.NetPlayerEvent(msgfrom, DDPE_CHAT_MESSAGE, msg);
}
// Servers relay chat messages to all the recipients.
Net_WriteChatMessage(msgfrom, mask, msg);
for(i = 1; i < DDMAXPLAYERS; ++i)
if(ddPlayers[i].shared.inGame && (mask & (1 << i)) && i != from)
{
Net_SendBuffer(i, 0);
}
M_Free(msg);
break;
case PCL_FINALE_REQUEST: {
finaleid_t fid = Reader_ReadUInt32(msgReader);
uint16_t params = Reader_ReadUInt16(msgReader);
#ifdef _DEBUG
Con_Message("PCL_FINALE_REQUEST: fid=%i params=%i", fid, params);
#endif
if(params == 1)
{
// Skip.
FI_ScriptRequestSkip(fid);
}
break; }
case PKT_PLAYER_INFO:
Sv_HandlePlayerInfoFromClient(sender);
break;
default:
Con_Error("Sv_HandlePacket: Invalid value, netBuffer.msg.type = %i.",
(int) netBuffer.msg.type);
break;
}
}
/**
* Handles a login packet. If the password is OK and no other client
* is current logged in, a response is sent.
*/
void Sv_Login(void)
{
char password[300];
byte passLen = 0;
if(netRemoteUser)
{
Sv_SendText(netBuffer.player, SV_CONSOLE_PRINT_FLAGS,
"Sv_Login: A client is already logged in.\n");
return;
}
// Check the password.
passLen = Reader_ReadByte(msgReader);
memset(password, 0, sizeof(password));
Reader_Read(msgReader, password, passLen);
if(strcmp(password, netPassword))
{
Sv_SendText(netBuffer.player, SV_CONSOLE_PRINT_FLAGS,
"Sv_Login: Invalid password.\n");
return;
}
// OK!
netRemoteUser = netBuffer.player;
Con_Message("Sv_Login: %s (client %i) logged in.",
clients[netRemoteUser].name, netRemoteUser);
// Send a confirmation packet to the client.
Msg_Begin(PKT_LOGIN);
Writer_WriteByte(msgWriter, true); // Yes, you're logged in.
Msg_End();
Net_SendBuffer(netRemoteUser, 0);
}
/**
* Executes the command in the message buffer.
* Usually sent by Con_Send.
*/
void Sv_ExecuteCommand(void)
{
int flags;
byte cmdSource;
unsigned short len;
boolean silent;
char *cmd = 0;
if(!netRemoteUser)
{
Con_Message("Sv_ExecuteCommand: Cmd received but no one's logged in!");
return;
}
// The command packet is very simple.
len = Reader_ReadUInt16(msgReader);
silent = (len & 0x8000) != 0;
len &= 0x7fff;
switch(netBuffer.msg.type)
{
/*case PKT_COMMAND:
cmdSource = CMDS_UNKNOWN; // unknown command source.
break;*/
case PKT_COMMAND2:
// New format includes flags and command source.
// Flags are currently unused but added for future expansion.
flags = Reader_ReadUInt16(msgReader);
cmdSource = Reader_ReadByte(msgReader);
break;
default:
Con_Error("Sv_ExecuteCommand: Not a command packet!\n");
return;
}
// Make a copy of the command.
cmd = (char *) M_Malloc(len + 1);
Reader_Read(msgReader, cmd, len);
cmd[len] = 0;
Con_Execute(cmdSource, cmd, silent, true);
M_Free(cmd);
}
/**
* Server's packet handler.
*/
void Sv_GetPackets(void)
{
int netconsole;
client_t *sender;
while(Net_GetPacket())
{
Msg_BeginRead();
switch(netBuffer.msg.type)
{
case PCL_GOODBYE:
// The client is leaving.
N_TerminateClient(netBuffer.player);
break;
case PKT_COORDS:
Sv_ClientCoords(netBuffer.player);
break;
case PCL_ACK_SHAKE:
// The client has acknowledged our handshake.
// Note the time (this isn't perfectly accurate, though).
netconsole = netBuffer.player;
if(netconsole >= 0 && netconsole < DDMAXPLAYERS)
{
sender = &clients[netconsole];
sender->shakePing = Timer_RealMilliseconds() - sender->shakePing;
Con_Printf("Cl%i handshake ping: %i ms\n", netconsole,
sender->shakePing);
}
break;
case PCL_ACK_PLAYER_FIX: {
player_t* plr = &ddPlayers[netBuffer.player];
ddplayer_t* ddpl = &plr->shared;
fixcounters_t* acked = &ddpl->fixAcked;
acked->angles = Reader_ReadInt32(msgReader);
acked->origin = Reader_ReadInt32(msgReader);
acked->mom = Reader_ReadInt32(msgReader);
#ifdef _DEBUG
Con_Message("PCL_ACK_PLAYER_FIX: (%i) Angles %i (%i), pos %i (%i), mom %i (%i).",
netBuffer.player,
acked->angles,
ddpl->fixCounter.angles,
acked->origin,
ddpl->fixCounter.origin,
acked->mom,
ddpl->fixCounter.mom);
#endif
break; }
case PKT_PING:
Net_PingResponse();
break;
case PCL_HELLO:
case PCL_HELLO2:
case PKT_OK:
case PKT_CHAT:
case PKT_PLAYER_INFO:
case PCL_FINALE_REQUEST:
Sv_HandlePacket();
break;
case PKT_LOGIN:
Sv_Login();
break;
//case PKT_COMMAND:
case PKT_COMMAND2:
Sv_ExecuteCommand();
break;
default:
if(netBuffer.msg.type >= PKT_GAME_MARKER)
{
// A client has sent a game specific packet.
gx.HandlePacket(netBuffer.player, netBuffer.msg.type,
netBuffer.msg.data, netBuffer.length);
}
break;
}
Msg_EndRead();
}
}
/**
* Assign a new console to the player. Returns true if successful.
* Called by N_Update().
*/
boolean Sv_PlayerArrives(unsigned int nodeID, char const *name)
{
LOG_AS("Sv_PlayerArrives");
LOG_INFO("'%s' has arrived.") << name;
// We need to find the new player a client entry.
for(int i = 1; i < DDMAXPLAYERS; ++i)
{
client_t *cl = &clients[i];
if(!cl->connected)
{
player_t *plr = &ddPlayers[i];
ddplayer_t *ddpl = &plr->shared;
// This'll do.
cl->connected = true;
cl->ready = false;
cl->nodeID = nodeID;
cl->viewConsole = i;
cl->lastTransmit = -1;
strncpy(cl->name, name, PLAYERNAMELEN);
ddpl->fixAcked.angles =
ddpl->fixAcked.origin =
ddpl->fixAcked.mom = -1;
// Clear the view filter.
memset(ddpl->filterColor, 0, sizeof(ddpl->filterColor));
ddpl->flags &= ~DDPF_VIEW_FILTER;
Sv_InitPoolForClient(i);
Smoother_Clear(cl->smoother);
LOG_VERBOSE("'%s' assigned to console %i (node:%u)") << cl->name << i << nodeID;
// In order to get in the game, the client must first
// shake hands. It'll request this by sending a Hello packet.
// We'll be waiting...
cl->handshake = false;
return true;
}
}
return false;
}
/**
* Remove the specified player from the game. Called by N_Update().
*/
void Sv_PlayerLeaves(unsigned int nodeID)
{
int plrNum = N_IdentifyPlayer(nodeID);
boolean wasInGame;
player_t *plr;
client_t *cl;
if(plrNum == -1) return; // Bogus?
// Log off automatically.
if(netRemoteUser == plrNum)
netRemoteUser = 0;
cl = &clients[plrNum];
plr = &ddPlayers[plrNum];
// Print a little something in the console.
Con_Message("Sv_PlayerLeaves: '%s' (console %i) has left, was connected for %f seconds.",
cl->name, plrNum, Timer_RealSeconds() - cl->enterTime);
wasInGame = plr->shared.inGame;
plr->shared.inGame = false;
cl->connected = false;
cl->ready = false;
//cl->updateCount = 0;
cl->handshake = false;
cl->nodeID = 0;
cl->bandwidthRating = BWR_DEFAULT;
// Remove the player's data from the register.
Sv_PlayerRemoved(plrNum);
if(wasInGame)
{
// Inform the DLL about this.
gx.NetPlayerEvent(plrNum, DDPE_EXIT, NULL);
// Inform other clients about this.
Msg_Begin(PSV_PLAYER_EXIT);
Writer_WriteByte(msgWriter, plrNum);
Msg_End();
Net_SendBuffer(NSP_BROADCAST, 0);
}
// This client no longer has an ID number.
cl->id = 0;
}
/**
* The player will be sent the introductory handshake packets.
*/
void Sv_Handshake(int plrNum, boolean newPlayer)
{
StringArray* ar;
int i;
uint playersInGame = 0;
#ifdef _DEBUG
Con_Message("Sv_Handshake: Shaking hands with player %i.", plrNum);
#endif
for(i = 0; i < DDMAXPLAYERS; ++i)
if(clients[i].connected)
playersInGame |= 1 << i;
Msg_Begin(PSV_HANDSHAKE);
Writer_WriteByte(msgWriter, SV_VERSION);
Writer_WriteByte(msgWriter, plrNum);
Writer_WriteUInt32(msgWriter, playersInGame);
Writer_WriteFloat(msgWriter, gameTime);
Msg_End();
Net_SendBuffer(plrNum, 0);
// Include the list of material Ids.
Msg_Begin(PSV_MATERIAL_ARCHIVE);
MaterialArchive_Write(materialDict, msgWriter);
Msg_End();
Net_SendBuffer(plrNum, 0);
// Include the list of thing Ids.
ar = Def_ListMobjTypeIDs();
Msg_Begin(PSV_MOBJ_TYPE_ID_LIST);
StringArray_Write(ar, msgWriter);
Msg_End();
Net_SendBuffer(plrNum, 0);
StringArray_Delete(ar);
// Include the list of state Ids.
ar = Def_ListStateIDs();
Msg_Begin(PSV_MOBJ_STATE_ID_LIST);
StringArray_Write(ar, msgWriter);
Msg_End();
Net_SendBuffer(plrNum, 0);
StringArray_Delete(ar);
if(newPlayer)
{
// Note the time when the handshake was sent.
clients[plrNum].shakePing = Timer_RealMilliseconds();
}
// The game DLL wants to shake hands as well?
gx.NetWorldEvent(DDWE_HANDSHAKE, plrNum, (void *) &newPlayer);
// Propagate client information.
for(i = 0; i < DDMAXPLAYERS; ++i)
{
if(clients[i].connected)
{
Net_SendPlayerInfo(i, plrNum);
}
// Send the new player's info to other players.
if(newPlayer && i != 0 && i != plrNum && clients[i].connected)
{
Net_SendPlayerInfo(plrNum, i);
}
}
if(!newPlayer)
{
// This is not a new player (just a re-handshake) but we'll
// nevertheless re-init the client's state register. For new
// players this is done in Sv_PlayerArrives.
Sv_InitPoolForClient(plrNum);
}
ddPlayers[plrNum].shared.flags |= DDPF_FIXANGLES | DDPF_FIXORIGIN | DDPF_FIXMOM;
}
void Sv_StartNetGame(void)
{
int i;
// Reset all the counters and other data.
for(i = 0; i < DDMAXPLAYERS; ++i)
{
client_t *client = &clients[i];
player_t *plr = &ddPlayers[i];
ddplayer_t *ddpl = &plr->shared;
ddpl->inGame = false;
ddpl->flags &= ~DDPF_CAMERA;
client->connected = false;
client->ready = false;
client->nodeID = 0;
client->enterTime = 0;
client->lastTransmit = -1;
client->fov = 90;
client->viewConsole = -1;
memset(client->name, 0, sizeof(client->name));
client->bandwidthRating = BWR_DEFAULT;
Smoother_Clear(client->smoother);
}
gameTime = 0;
firstNetUpdate = true;
netRemoteUser = 0;
// The server is always player number zero.
consolePlayer = displayPlayer = 0;
netGame = true;
isServer = true;
allowSending = true;
// Prepare the material dictionary we'll be using with clients.
materialDict = MaterialArchive_New(false);
#ifdef _DEBUG
Con_Message("Sv_StartNetGame: Prepared material dictionary with %i materials.",
MaterialArchive_Count(materialDict));
#endif
if(!isDedicated)
{
player_t *plr = &ddPlayers[consolePlayer];
ddplayer_t *ddpl = &plr->shared;
client_t *cl = &clients[consolePlayer];
ddpl->inGame = true;
cl->connected = true;
cl->ready = true;
cl->viewConsole = 0;
strcpy(cl->name, playerName);
}
}
void Sv_StopNetGame(void)
{
if(materialDict)
{
MaterialArchive_Delete(materialDict);
materialDict = 0;
}
}
unsigned int Sv_IdForMaterial(Material* mat)
{
assert(materialDict);
return MaterialArchive_FindUniqueSerialId(materialDict, mat);
}
void Sv_SendText(int to, int con_flags, const char* text)
{
uint32_t len = MIN_OF(0xffff, strlen(text));
Msg_Begin(PSV_CONSOLE_TEXT);
Writer_WriteUInt32(msgWriter, con_flags & ~CPF_TRANSMIT);
Writer_WriteUInt16(msgWriter, len);
Writer_Write(msgWriter, text, len);
Msg_End();
Net_SendBuffer(to, 0);
}
/**
* Asks a client to disconnect. Clients will immediately disconnect
* after receiving the PSV_SERVER_CLOSE message.
*/
void Sv_Kick(int who)
{
if(!clients[who].connected)
return;
Sv_SendText(who, SV_CONSOLE_PRINT_FLAGS, "You were kicked out!\n");
Msg_Begin(PSV_SERVER_CLOSE);
Msg_End();
Net_SendBuffer(who, 0);
}
/**
* Sends player @a plrNum's position, momentum and/or angles override to all
* clients.
*/
void Sv_SendPlayerFixes(int plrNum)
{
int fixes = 0;
player_t *plr = &ddPlayers[plrNum];
ddplayer_t *ddpl = &plr->shared;
if(!(ddpl->flags & (DDPF_FIXANGLES | DDPF_FIXORIGIN | DDPF_FIXMOM)))
{
// Nothing to fix.
return;
}
// Start writing a player fix message.
Msg_Begin(PSV_PLAYER_FIX);
// Which player is being fixed?
Writer_WriteByte(msgWriter, plrNum);
// Indicate what is included in the message.
if(ddpl->flags & DDPF_FIXANGLES) fixes |= 1;
if(ddpl->flags & DDPF_FIXORIGIN) fixes |= 2;
if(ddpl->flags & DDPF_FIXMOM) fixes |= 4;
Writer_WriteUInt32(msgWriter, fixes);
Writer_WriteUInt16(msgWriter, ddpl->mo->thinker.id);
#ifdef _DEBUG
Con_Message("Sv_SendPlayerFixes: Fixing mobj %i...", ddpl->mo->thinker.id);
#endif
// Increment counters.
if(ddpl->flags & DDPF_FIXANGLES)
{
Writer_WriteInt32(msgWriter, ++ddpl->fixCounter.angles);
Writer_WriteUInt32(msgWriter, ddpl->mo->angle);
Writer_WriteFloat(msgWriter, ddpl->lookDir);
#ifdef _DEBUG
Con_Message("Sv_SendPlayerFixes: Sent angles (%i): angle=%x lookdir=%f",
ddpl->fixCounter.angles, ddpl->mo->angle, ddpl->lookDir);
#endif
}
if(ddpl->flags & DDPF_FIXORIGIN)
{
Writer_WriteInt32(msgWriter, ++ddpl->fixCounter.origin);
Writer_WriteFloat(msgWriter, ddpl->mo->origin[VX]);
Writer_WriteFloat(msgWriter, ddpl->mo->origin[VY]);
Writer_WriteFloat(msgWriter, ddpl->mo->origin[VZ]);
#ifdef _DEBUG
Con_Message("Sv_SendPlayerFixes: Sent position (%i): %f, %f, %f",
ddpl->fixCounter.origin,
ddpl->mo->origin[VX], ddpl->mo->origin[VY], ddpl->mo->origin[VZ]);
#endif
}
if(ddpl->flags & DDPF_FIXMOM)
{
Writer_WriteInt32(msgWriter, ++ddpl->fixCounter.mom);
Writer_WriteFloat(msgWriter, ddpl->mo->mom[MX]);
Writer_WriteFloat(msgWriter, ddpl->mo->mom[MY]);
Writer_WriteFloat(msgWriter, ddpl->mo->mom[MZ]);
#ifdef _DEBUG
Con_Message("Sv_SendPlayerFixes: Sent momentum (%i): %f, %f, %f",
ddpl->fixCounter.mom,
ddpl->mo->mom[MX], ddpl->mo->mom[MY], ddpl->mo->mom[MZ]);
#endif
}
Msg_End();
// Send the fix message to everyone.
Net_SendBuffer(DDSP_ALL_PLAYERS, 0);
ddpl->flags &= ~(DDPF_FIXANGLES | DDPF_FIXORIGIN | DDPF_FIXMOM);
#ifdef _DEBUG
Con_Message("Sv_SendPlayerFixes: Cleared FIX flags of player %i.", plrNum);
#endif
// Clear the smoother for this client.
if(clients[plrNum].smoother)
{
Smoother_Clear(clients[plrNum].smoother);
}
}
void Sv_Ticker(timespan_t ticLength)
{
int i;
if(!isDedicated) return;
// Note last angles for all players.
for(i = 0; i < DDMAXPLAYERS; ++i)
{
player_t *plr = &ddPlayers[i];
if(!plr->shared.inGame || !plr->shared.mo)
continue;
// Update the smoother?
if(clients[i].smoother)
{
Smoother_Advance(clients[i].smoother, ticLength);
}
if(DD_IsSharpTick())
{
plr->shared.lastAngle = plr->shared.mo->angle;
}
/*
if(clients[i].bwrAdjustTime > 0)
{
// BWR adjust time tics away.
clients[i].bwrAdjustTime--;
}*/
// Increment counter, send new data.
Sv_SendPlayerFixes(i);
}
}
/**
* @return The number of players in the game.
*/
int Sv_GetNumPlayers(void)
{
int i, count;
// Clients can't count.
if(isClient)
return 1;