/
cl_main.cpp
506 lines (413 loc) · 13.5 KB
/
cl_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
/** @file cl_main.cpp Network client.
*
* @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 "de_base.h"
#include "client/cl_def.h"
#include "api_client.h"
#include "client/cl_frame.h"
#include "client/cl_infine.h"
#include "client/cl_player.h"
#include "client/cl_sound.h"
#include "client/cl_world.h"
#include "audio/s_main.h"
#include "network/net_main.h"
#include "network/net_buf.h"
#include "network/net_demo.h"
#include "world/map.h"
#include "world/p_players.h"
#include <doomsday/console/exec.h>
#include <doomsday/filesys/fs_main.h>
#include <de/timer.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace de;
ident_t clientID;
bool handshakeReceived;
int gameReady;
int serverTime;
bool netLoggedIn; // Logged in to the server.
int clientPaused; // Set by the server.
void Cl_InitID()
{
if(int i = CommandLine_CheckWith("-id", 1))
{
clientID = strtoul(CommandLine_At(i + 1), 0, 0);
LOG_NET_NOTE("Using custom client ID: 0x%08x") << clientID;
return;
}
// Read the client ID number file.
srand(time(NULL));
if(FILE *file = fopen("client.id", "rb"))
{
if(fread(&clientID, sizeof(clientID), 1, file))
{
clientID = DD_ULONG(clientID);
fclose(file);
return;
}
fclose(file);
}
// Ah-ha, we need to generate a new ID.
clientID = (ident_t)
DD_ULONG(Timer_RealMilliseconds() * rand() + (rand() & 0xfff) +
((rand() & 0xfff) << 12) + ((rand() & 0xff) << 24));
// Write it to the file.
if(FILE *file = fopen("client.id", "wb"))
{
fwrite(&clientID, sizeof(clientID), 1, file);
fclose(file);
}
}
int Cl_GameReady()
{
return (handshakeReceived && gameReady);
}
void Cl_CleanUp()
{
LOG_NET_MSG("Cleaning up client state");
clientPaused = false;
handshakeReceived = false;
//S_Reset();
S_MapChange();
// Reset the local world state.
App_WorldSystem().reset();
// Discard the translation tables for the server we've just left.
Cl_ResetTransTables();
// Reset any view effects.
GL_ResetViewEffects();
// Forget all packets we've received but haven't yet handled.
N_ClearMessages();
}
void Cl_SendHello()
{
LOG_AS("Cl_SendHello");
Msg_Begin(PCL_HELLO2);
Writer_WriteUInt32(msgWriter, clientID);
// The game mode is included in the hello packet.
char buf[256]; zap(buf);
strncpy(buf, App_CurrentGame().identityKey().toUtf8().constData(), sizeof(buf) - 1);
LOGDEV_NET_VERBOSE("game mode = %s") << buf;
Writer_Write(msgWriter, buf, 16);
Msg_End();
Net_SendBuffer(0, 0);
}
void Cl_AnswerHandshake()
{
LOG_AS("Cl_AnswerHandshake");
byte remoteVersion = Reader_ReadByte(msgReader);
byte myConsole = Reader_ReadByte(msgReader);
uint playersInGame = Reader_ReadUInt32(msgReader);
float remoteGameTime = Reader_ReadFloat(msgReader);
// Immediately send an acknowledgement. This lets the server evaluate
// an approximate ping time.
Msg_Begin(PCL_ACK_SHAKE);
Msg_End();
Net_SendBuffer(0, 0);
// Check the version number.
if(remoteVersion != SV_VERSION)
{
LOG_NET_ERROR("Version conflict! (you:%i, server:%i)")
<< SV_VERSION << remoteVersion;
Con_Execute(CMDS_DDAY, "net disconnect", false, false);
Demo_StopPlayback();
Con_Open(true);
return;
}
// Update time and player ingame status.
gameTime = remoteGameTime;
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
/// @todo With multiple local players, must clear only the appropriate flags.
ddPlayers[i].shared.flags &= ~DDPF_LOCAL;
ddPlayers[i].shared.inGame = (playersInGame & (1 << i)) != 0;
}
consolePlayer = displayPlayer = myConsole;
clients[consolePlayer].viewConsole = consolePlayer;
// Mark us as the only local player.
ddPlayers[consolePlayer].shared.flags |= DDPF_LOCAL;
Smoother_Clear(clients[consolePlayer].smoother);
ddPlayers[consolePlayer].shared.flags &= ~DDPF_USE_VIEW_FILTER;
isClient = true;
isServer = false;
netLoggedIn = false;
clientPaused = false;
if(handshakeReceived)
return;
// This prevents redundant re-initialization.
handshakeReceived = true;
// Soon after this packet will follow the game's handshake.
gameReady = false;
Cl_InitFrame();
LOGDEV_NET_MSG("Answering handshake: myConsole:%i, remoteGameTime:%.2f")
<< myConsole << remoteGameTime;
/*
* Tell the game that we have arrived. The map will be changed when the
* game's handshake arrives (handled in the game).
*/
gx.NetPlayerEvent(consolePlayer, DDPE_ARRIVAL, 0);
// Prepare the client-side data.
Cl_InitPlayers();
Cl_InitTransTables();
// Get ready for ticking.
DD_ResetTimer();
Con_Executef(CMDS_DDAY, true, "setcon %i", consolePlayer);
}
void Cl_HandlePlayerInfo()
{
byte console = Reader_ReadByte(msgReader);
size_t len = Reader_ReadUInt16(msgReader);
len = MIN_OF(PLAYERNAMELEN - 1, len);
char name[PLAYERNAMELEN]; zap(name);
Reader_Read(msgReader, name, len);
LOG_NET_VERBOSE("Player %i named \"%s\"") << console << name;
// Is the console number valid?
if(console >= DDMAXPLAYERS)
return;
player_t *plr = &ddPlayers[console];
bool present = plr->shared.inGame;
plr->shared.inGame = true;
strcpy(clients[console].name, name);
if(!present)
{
// This is a new player! Let the game know about this.
gx.NetPlayerEvent(console, DDPE_ARRIVAL, 0);
Smoother_Clear(clients[console].smoother);
}
}
void Cl_PlayerLeaves(int plrNum)
{
LOG_NET_NOTE("Player %i has left the game") << plrNum;
ddPlayers[plrNum].shared.inGame = false;
gx.NetPlayerEvent(plrNum, DDPE_EXIT, 0);
}
void Cl_GetPackets()
{
// All messages come from the server.
while(Net_GetPacket())
{
Msg_BeginRead();
// First check for packets that are only valid when
// a game is in progress.
if(Cl_GameReady())
{
switch(netBuffer.msg.type)
{
case PSV_FIRST_FRAME2:
case PSV_FRAME2:
Cl_Frame2Received(netBuffer.msg.type);
Msg_EndRead();
continue; // Get the next packet.
case PSV_SOUND:
Cl_Sound();
Msg_EndRead();
continue; // Get the next packet.
default: break;
}
}
// How about the rest?
switch(netBuffer.msg.type)
{
case PSV_PLAYER_FIX:
ClPlayer_HandleFix();
break;
case PKT_DEMOCAM:
case PKT_DEMOCAM_RESUME:
Demo_ReadLocalCamera();
break;
case PKT_PING:
Net_PingResponse();
break;
case PSV_SYNC:
// The server updates our time. Latency has been taken into
// account, so...
gameTime = Reader_ReadFloat(msgReader);
LOGDEV_NET_VERBOSE("PSV_SYNC: gameTime=%.3f") << gameTime;
DD_ResetTimer();
break;
case PSV_HANDSHAKE:
Cl_AnswerHandshake();
break;
case PSV_MATERIAL_ARCHIVE:
Cl_ReadServerMaterials();
break;
case PSV_MOBJ_TYPE_ID_LIST:
Cl_ReadServerMobjTypeIDs();
break;
case PSV_MOBJ_STATE_ID_LIST:
Cl_ReadServerMobjStateIDs();
break;
case PKT_PLAYER_INFO:
Cl_HandlePlayerInfo();
break;
case PSV_PLAYER_EXIT:
Cl_PlayerLeaves(Reader_ReadByte(msgReader));
break;
case PKT_CHAT: {
int msgfrom = Reader_ReadByte(msgReader);
int mask = Reader_ReadUInt32(msgReader);
DENG2_UNUSED(mask);
size_t len = Reader_ReadUInt16(msgReader);
char *msg = (char *) M_Malloc(len + 1);
Reader_Read(msgReader, msg, len);
msg[len] = 0;
Net_ShowChatMessage(msgfrom, msg);
gx.NetPlayerEvent(msgfrom, DDPE_CHAT_MESSAGE, msg);
M_Free(msg);
break; }
case PSV_SERVER_CLOSE: // We should quit?
netLoggedIn = false;
Con_Execute(CMDS_DDAY, "net disconnect", true, false);
break;
case PSV_CONSOLE_TEXT: {
uint32_t conFlags = Reader_ReadUInt32(msgReader);
uint16_t textLen = Reader_ReadUInt16(msgReader);
char *text = (char *) M_Malloc(textLen + 1);
Reader_Read(msgReader, text, textLen);
text[textLen] = 0;
DENG_UNUSED(conFlags);
LOG_NOTE("%s") << text;
M_Free(text);
break; }
case PKT_LOGIN:
// Server responds to our login request. Let's see if we were successful.
netLoggedIn = CPP_BOOL(Reader_ReadByte(msgReader));
break;
case PSV_FINALE:
Cl_Finale(msgReader);
break;
case PSV_FRAME2:
case PSV_FIRST_FRAME2:
case PSV_SOUND:
LOGDEV_NET_WARNING("Packet type %i was discarded (client not ready)") << netBuffer.msg.type;
break;
default:
if(netBuffer.msg.type >= PKT_GAME_MARKER)
{
gx.HandlePacket(netBuffer.player, netBuffer.msg.type,
netBuffer.msg.data, netBuffer.length);
}
else
{
LOG_NET_WARNING("Packet was discarded (unknown type %i)") << netBuffer.msg.type;
}
}
Msg_EndRead();
}
}
#ifdef DENG_DEBUG
/**
* Check the state of the client on engineside. A debugging utility.
*/
static void assertPlayerIsValid(int plrNum)
{
LOG_AS("Client.assertPlayerIsValid");
if(!isClient || !Cl_GameReady() || clientPaused) return;
if(plrNum < 0 || plrNum >= DDMAXPLAYERS) return;
player_t *plr = &ddPlayers[plrNum];
clplayerstate_t *s = ClPlayer_State(plrNum);
// Must have a mobj!
if(!s->clMobjId || !plr->shared.mo)
return;
mobj_t *clmo = ClMobj_Find(s->clMobjId);
if(!clmo)
{
LOGDEV_NET_NOTE("Player %i does not have a clmobj yet [%i]") << plrNum << s->clMobjId;
return;
}
mobj_t *mo = plr->shared.mo;
/*
("Assert: client %i, clmo %i (flags 0x%x)", plrNum, clmo->thinker.id, clmo->ddFlags);
*/
// Make sure the flags are correctly set for a client.
if(mo->ddFlags & DDMF_REMOTE)
{
LOGDEV_NET_NOTE("Player %i's mobj should not be remote") << plrNum;
}
if(clmo->ddFlags & DDMF_SOLID)
{
LOGDEV_NET_NOTE("Player %i's clmobj should not be solid (when player is alive)") << plrNum;
}
}
#endif // DENG_DEBUG
void Cl_Ticker(timespan_t ticLength)
{
if(!isClient || !Cl_GameReady() || clientPaused)
return;
// On clientside, players are represented by two mobjs: the real mobj,
// created by the Game, is the one that is visible and modified by game
// logic. We'll need to sync the hidden client mobj (that receives all
// the changes from the server) to match the changes. The game ticker
// has already been run when Cl_Ticker() is called, so let's update the
// player's clmobj to its updated state.
for(int i = 0; i < DDMAXPLAYERS; ++i)
{
if(!ddPlayers[i].shared.inGame) continue;
if(i != consolePlayer)
{
if(ddPlayers[i].shared.mo)
{
Smoother_AddPos(clients[i].smoother, Cl_FrameGameTime(),
ddPlayers[i].shared.mo->origin[VX],
ddPlayers[i].shared.mo->origin[VY],
ddPlayers[i].shared.mo->origin[VZ],
false);
}
// Update the smoother.
Smoother_Advance(clients[i].smoother, ticLength);
}
ClPlayer_ApplyPendingFixes(i);
ClPlayer_UpdateOrigin(i);
#ifdef DENG_DEBUG
assertPlayerIsValid(i);
#endif
}
if(App_WorldSystem().hasMap())
{
App_WorldSystem().map().expireClMobjs();
}
}
/**
* Clients use this to establish a remote connection to the server.
*/
D_CMD(Login)
{
DENG2_UNUSED(src);
// Only clients can log in.
if(!isClient)
return false;
Msg_Begin(PKT_LOGIN);
// Write the password.
if(argc == 1)
{
Writer_WriteByte(msgWriter, 0); // No password given!
}
else if(strlen(argv[1]) <= 255)
{
Writer_WriteByte(msgWriter, strlen(argv[1]));
Writer_Write(msgWriter, argv[1], strlen(argv[1]));
}
else
{
Msg_End();
return false;
}
Msg_End();
Net_SendBuffer(0, 0);
return true;
}