Debian Codegraph @

src/game/servergame.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file servergame.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created level.c and menu.c
10 * - 3rd of February 2009: Renamed level.c to level.cpp and menu.c to menu.cpp
11 * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp
12 * - 18th July 2009: Created servergame.cpp from parts of game.cpp
13 * - 2nd August 2009: Created gamemode.cpp from parts of servergame.cpp
14 *
15 * @par Licence:
16 * Copyright (c) 2005-2013 AJ Thomson
17 *
18 * OpenJazz is distributed under the terms of
19 * the GNU General Public License, version 2.0
20 *
21 */
22
23
24 #include "game.h"
25
26 #include "io/file.h"
27 #include "io/gfx/font.h"
28 #include "io/gfx/video.h"
29 #include "io/network.h"
30 #include "player/player.h"
31 #include "setup.h"
32 #include "util.h"
33
34 #include <string.h>
35
36
37 /**
38 * Create game server
39 *
40 * @param modeType Game mode
41 * @param firstLevel File name of the first level to play
42 * @param gameDifficulty Difficulty setting
43 */
44 ServerGame::ServerGame (GameModeType modeType, char* firstLevel, difficultyType gameDifficulty) {
45
46 int count;
47
48
49 // Create the server
50
51 sock = net->host();
52
53 if (sock < 0) throw sock; // Tee hee. Throw sock.
54
55
56 // Create the players
57
58 nPlayers = 1;
59 localPlayer = players = new Player[MAX_PLAYERS];
60 localPlayer->init(this, setup.characterName, setup.characterCols, 0);
61
62 for (count = 0; count < MAX_CLIENTS; count++)
63 clientPlayer[count] = clientStatus[count] = -1;
64
65
66 // Copy the first level into memory
67
68 levelFile = NULL;
69 levelData = NULL;
70
71 count = setLevel(firstLevel);
72
73 if (count < 0) {
74
75 net->close(sock);
76
77 if (levelData) delete[] levelData;
78
79 throw count;
80
81 }
82
83 difficulty = gameDifficulty;
84
85 mode = createMode(modeType);
86
87 }
88
89
90 /**
91 * Disconnect clients and destroy server
92 */
93 ServerGame::~ServerGame () {
94
95 int count;
96
97 for (count = 0; count < MAX_CLIENTS; count++) {
98
99 if (clientStatus[count] != -1) net->close(clientSock[count]);
100
101 }
102
103 net->close(sock);
104
105 if (levelData) delete[] levelData;
106
107 delete mode;
108
109 }
110
111
112 /**
113 * Set the next level and load it into memory
114 *
115 * @param fileName The file name of the next level
116 *
117 * @return Error code
118 */
119 int ServerGame::setLevel (char* fileName) {
120
121 File* file;
122 int count;
123
124 if (levelFile) delete[] levelFile;
125 if (levelData) delete[] levelData;
126
127 // The new level will be sent to all clients
128 for (count = 0; count < MAX_CLIENTS; count++) {
129
130 if (clientStatus[count] != -1) clientStatus[count] = 0;
131
132 }
133
134 if (!fileName) {
135
136 levelFile = NULL;
137 levelData = NULL;
138
139 return E_NONE;
140
141 }
142
143 try {
144
145 file = new File(fileName, PATH_TYPE_GAME);
146
147 } catch (int e) {
148
149 levelFile = NULL;
150 levelData = NULL;
151
152 return e;
153
154 }
155
156 levelFile = createString(fileName);
157
158 // Load the entire file into memory
159 levelSize = file->getSize();
160 levelData = file->loadBlock(levelSize);
161
162 delete file;
163
164 levelType = getLevelType(fileName);
165
166 if (levelType != LT_JJ1) return E_NONE;
167
168 // Modify the extension section to match the actual extension
169 count = levelSize - 5;
170 while (levelData[count - 1] != 3) count--;
171 levelData[count] = fileName[strlen(fileName) - 3];
172 levelData[count + 1] = fileName[strlen(fileName) - 2];
173 levelData[count + 2] = fileName[strlen(fileName) - 1];
174
175 return E_NONE;
176
177 }
178
179
180 /**
181 * Send data to clients
182 *
183 * @param buffer Data to send. First byte indicates length.
184 */
185 void ServerGame::send (unsigned char* buffer) {
186
187 int count;
188
189 for (count = 0; count < MAX_CLIENTS; count++) {
190
191 // Send data to client, unless the data concerns the client's player
192 // Each client is solely responsible for its player's state
193 if ((clientStatus[count] != -1) &&
194 (((buffer[1] & MCMASK) != MC_PLAYER) ||
195 (buffer[2] != clientPlayer[count])))
196 net->send(clientSock[count], buffer);
197
198 }
199
200 }
201
202
203 /**
204 * Game iteration
205 *
206 * @param ticks Current time
207 *
208 * @return Error code
209 */
210 int ServerGame::step (unsigned int ticks) {
211
212 unsigned char sendBuffer[BUFFER_LENGTH];
213 int count, pcount, length;
214
215 for (count = 0; count < MAX_CLIENTS; count++) {
216
217 if (clientStatus[count] >= 0) {
218
219 if (clientStatus[count] == 0) {
220
221 // Send level type
222 sendBuffer[0] = MTL_G_LTYPE;
223 sendBuffer[1] = MT_G_LTYPE;
224 sendBuffer[2] = levelType;
225 net->send(clientSock[count], sendBuffer);
226
227 }
228
229 // Client is connected, but not operational
230 // Send a chunk of the level
231
232 length = levelSize - clientStatus[count];
233
234 if (length > 251) length = 251;
235
236 sendBuffer[0] = MTL_G_LEVEL + length;
237 sendBuffer[1] = MT_G_LEVEL;
238 sendBuffer[2] = clientStatus[count] >> 8;
239 sendBuffer[3] = clientStatus[count] & 255;
240 memcpy(sendBuffer + 4, levelData + clientStatus[count], length);
241 length = net->send(clientSock[count], sendBuffer);
242
243 // Client is operational if the whole level has been sent
244 // Otherwise, keep sending data
245 if (length == MTL_G_LEVEL) clientStatus[count] = -2;
246 else if (length > 0) clientStatus[count] += length - MTL_G_LEVEL;
247
248 }
249
250
251 if ((clientStatus[count] == -2) && (received[count] == 0)) {
252
253 // Client is operational, but not currently receiving a message
254 // See if there is a new message to receive
255
256 length = net->recv(clientSock[count], recvBuffers[count], 1);
257
258 if (length > 0) received[count]++;
259
260 }
261
262
263 if ((clientStatus[count] == -2) && (received[count] > 0)) {
264
265 // Currently receiving a message
266 // See if there is any more data
267
268 length = net->recv(clientSock[count],
269 recvBuffers[count] + received[count],
270 recvBuffers[count][0] - received[count]);
271
272 if (length > 0) received[count] += length;
273
274
275 // See if the whole message has arrived
276
277 if (received[count] >= recvBuffers[count][0]) {
278
279 switch (recvBuffers[count][1] & MCMASK) {
280
281 case MC_GAME:
282
283 if ((recvBuffers[count][1] == MT_G_PJOIN) &&
284 (clientPlayer[count] == -1)) {
285
286 printf("Player %d (client %d) joined the game.\n", nPlayers, count);
287
288
289 // Set up the new player
290
291 recvBuffers[count][4] = mode->chooseTeam();
292
293 players[nPlayers].init(this,
294 reinterpret_cast<char*>(recvBuffers[count] + 9),
295 recvBuffers[count] + 5, recvBuffers[count][4]);
296 addLevelPlayer(players + nPlayers);
297
298 printf("Player %d joined team %d.\n", nPlayers, recvBuffers[count][4]);
299
300 recvBuffers[count][3] = clientPlayer[count] = nPlayers;
301
302 nPlayers++;
303
304 }
305
306 if (recvBuffers[count][1] == MT_G_CHECK) {
307
308 checkX = recvBuffers[count][2];
309 checkY = recvBuffers[count][3];
310
311 if (recvBuffers[count][0] > 4) {
312
313 checkX += recvBuffers[count][4] << 8;
314 checkY += recvBuffers[count][5] << 8;
315
316 }
317
318 }
319
320 if (recvBuffers[count][1] == MT_G_SCORE) {
321
322 for (pcount = 0; pcount < nPlayers; pcount++) {
323
324 if (players[pcount].getTeam() == recvBuffers[count][2])
325 players[pcount].teamScore++;
326
327 }
328
329 }
330
331 break;
332
333 case MC_LEVEL:
334
335 baseLevel->receive(recvBuffers[count]);
336
337 break;
338
339 case MC_PLAYER:
340
341 if (clientPlayer[count] != -1) {
342
343 // Assign player byte based on sender
344 recvBuffers[count][2] = clientPlayer[count];
345
346 players[clientPlayer[count]].receive(recvBuffers[count]);
347
348 }
349
350 break;
351
352 }
353
354 // Update clients
355 send(recvBuffers[count]);
356
357 received[count] = 0;
358
359 }
360
361 }
362
363 if (ticks >= checkTime) {
364
365 if ((clientStatus[count] == -1) && levelData) {
366
367 // Client is not connected
368 // Check for new connection
369
370 clientSock[count] = net->accept(sock);
371
372 if (clientSock[count] != -1) {
373
374 printf("Client %d connected.\n", count);
375
376 clientPlayer[count] = -1;
377 received[count] = 0;
378
379 // Incorporate the new client
380
381 // Send data
382 sendBuffer[0] = MTL_G_PROPS;
383 sendBuffer[1] = MT_G_PROPS;
384 sendBuffer[2] = 1; // Server version
385 sendBuffer[3] = mode->getMode();
386 sendBuffer[4] = +difficulty;
387 sendBuffer[5] = MAX_PLAYERS;
388 sendBuffer[6] = nPlayers; // Number of players
389 sendBuffer[7] = count; // Client's clientID
390 net->send(clientSock[count], sendBuffer);
391
392 // Initiate sending of level data
393 clientStatus[count] = 0;
394
395 // Inform the new client of the checkpoint
396 sendBuffer[0] = MTL_G_CHECK;
397 sendBuffer[1] = MT_G_CHECK;
398 sendBuffer[2] = checkX & 0xFF;
399 sendBuffer[3] = checkY & 0xFF;
400 sendBuffer[4] = (checkX >> 8) & 0xFF;
401 sendBuffer[5] = (checkY >> 8) & 0xFF;
402 net->send(clientSock[count], sendBuffer);
403
404 // Inform the new client of the existing players
405
406 sendBuffer[1] = MT_G_PJOIN;
407
408 for (pcount = 0; pcount < nPlayers; pcount++) {
409
410 sendBuffer[0] = MTL_G_PJOIN + strlen(players[pcount].getName());
411 sendBuffer[2] = count;
412 sendBuffer[3] = pcount;
413 sendBuffer[4] = players[pcount].getTeam();
414 memcpy(sendBuffer + 5, players[pcount].getCols(), PCOLOURS);
415 memcpy(sendBuffer + 9, players[pcount].getName(), strlen(players[pcount].getName()) + 1);
416
417 net->send(clientSock[count], sendBuffer);
418
419 }
420
421 }
422
423 } else {
424
425 // Client is connected
426 // Check for disconnection
427
428 if (!(net->isConnected(clientSock[count]))) {
429
430 printf("Client %d disconnected (code: %d).\n", count, net->getError());
431
432 // Disconnect client
433 net->close(clientSock[count]);
434 clientStatus[count] = -1;
435
436 if (clientPlayer[count] != -1) {
437
438 // Remove the client's player
439
440 printf("Player %d (client %d) left the game.\n", clientPlayer[count], count);
441
442 nPlayers--;
443
444 players[clientPlayer[count]].deinit();
445
446 // If necessary, move more recent players
447 for (pcount = clientPlayer[count]; pcount < nPlayers; pcount++)
448 memcpy(static_cast<void*>(players + pcount), players + pcount + 1, sizeof(Player));
449
450 // Clear duplicate pointers
451 memset(static_cast<void*>(players + nPlayers), 0, sizeof(Player));
452
453 // Inform remaining clients that the player has left
454 sendBuffer[0] = MTL_G_PQUIT;
455 sendBuffer[1] = MT_G_PQUIT;
456 sendBuffer[2] = clientPlayer[count];
457 send(sendBuffer);
458
459 clientPlayer[count] = -1;
460
461 }
462
463 }
464
465 }
466
467 }
468
469 }
470
471 if (ticks >= checkTime) checkTime = ticks + T_SCHECK;
472
473 if (ticks >= sendTime) {
474
475 // Update clients
476
477 sendBuffer[0] = MTL_P_TEMP;
478 sendBuffer[1] = MT_P_TEMP;
479
480 for (count = 0; count < nPlayers; count++) {
481
482 sendBuffer[2] = count;
483 players[count].send(sendBuffer);
484 send(sendBuffer);
485
486 }
487
488 sendTime = ticks + T_SSEND;
489
490 }
491
492 return E_NONE;
493
494 }
495
496
497 /**
498 * Assign point to team and inform clients
499 *
500 * @param team Team to receive point
501 */
502 void ServerGame::score (unsigned char team) {
503
504 unsigned char buffer[MTL_G_SCORE];
505 int count;
506
507 // Inform clients
508 buffer[0] = MTL_G_SCORE;
509 buffer[1] = MT_G_SCORE;
510 buffer[2] = team;
511 send(buffer);
512
513 // Update self
514 for (count = 0; count < nPlayers; count++) {
515
516 if (players[count].getTeam() == team) players[count].teamScore++;
517
518 }
519
520 }
521
522
523 /**
524 * Set the checkpoint and inform clients
525 *
526 * @param gridX X-coordinate (in tiles) of the checkpoint
527 * @param gridY Y-coordinate (in tiles) of the checkpoint
528 */
529 void ServerGame::setCheckpoint (int gridX, int gridY) {
530
531 unsigned char buffer[MTL_G_CHECK];
532
533 buffer[0] = MTL_G_CHECK;
534 buffer[1] = MT_G_CHECK;
535 buffer[2] = gridX & 0xFF;
536 buffer[3] = gridY & 0xFF;
537 buffer[4] = (gridX >> 8) & 0xFF;
538 buffer[5] = (gridY >> 8) & 0xFF;
539 send(buffer);
540
541 checkX = gridX;
542 checkY = gridY;
543
544 }