| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file clientgame.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 clientgame.cpp from parts of game.cpp |
| 13 | * |
| 14 | * @par Licence: |
| 15 | * Copyright (c) 2005-2017 AJ Thomson |
| 16 | * |
| 17 | * OpenJazz is distributed under the terms of |
| 18 | * the GNU General Public License, version 2.0 |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #include "game.h" |
| 24 | #include "gamemode.h" |
| 25 | |
| 26 | #include "io/controls.h" |
| 27 | #include "io/file.h" |
| 28 | #include "io/gfx/font.h" |
| 29 | #include "io/gfx/video.h" |
| 30 | #include "io/network.h" |
| 31 | #include "player/player.h" |
| 32 | #include "loop.h" |
| 33 | #include "setup.h" |
| 34 | #include "util.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Create game client |
| 41 | * |
| 42 | * @param address Address of the server to which to connect |
| 43 | */ |
| 44 | ClientGame::ClientGame (char* address) { |
| 45 | |
| 46 | unsigned char buffer[BUFFER_LENGTH]; |
| 47 | unsigned int timeout; |
| 48 | int count, ret; |
| 49 | GameModeType modeType; |
| 50 | |
| 51 | sock = net->join(address); |
| 52 | |
| 53 | if (sock < 0) throw sock; // Tee hee hee hee hee. |
| 54 | |
| 55 | |
| 56 | // Receive initialisation message |
| 57 | |
| 58 | count = 0; |
| 59 | timeout = globalTicks + T_SCHECK + T_TIMEOUT; |
| 60 | |
| 61 | // Wait for whole message to arrive |
| 62 | while (count < MTL_G_PROPS) { |
| 63 | |
| 64 | if (loop(NORMAL_LOOP) == E_QUIT) { |
| 65 | |
| 66 | net->close(sock); |
| 67 | |
| 68 | throw E_QUIT; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | if (controls.release(C_ESCAPE)) { |
| 73 | |
| 74 | net->close(sock); |
| 75 | |
| 76 | throw E_RETURN; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | SDL_Delay(T_MENU_FRAME); |
| 81 | |
| 82 | video.clearScreen(0); |
| 83 | fontmn2->showStringCentered("WAITING FOR REPLY"); |
| 84 | |
| 85 | ret = net->recv(sock, buffer + count, MTL_G_PROPS - count); |
| 86 | |
| 87 | if (ret > 0) count += ret; |
| 88 | |
| 89 | if (globalTicks > timeout) { |
| 90 | |
| 91 | net->close(sock); |
| 92 | |
| 93 | throw E_TIMEOUT; |
| 94 | |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | // Make sure message is valid |
| 100 | if (buffer[1] != MT_G_PROPS) { |
| 101 | |
| 102 | net->close(sock); |
| 103 | |
| 104 | throw E_DATA; |
| 105 | |
| 106 | } else if (buffer[2] != 1) { |
| 107 | |
| 108 | net->close(sock); |
| 109 | |
| 110 | throw E_VERSION; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | printf("Connected to server (version %d).\n", buffer[2]); |
| 115 | |
| 116 | // Copy game parameters |
| 117 | modeType = GameModeType(buffer[3]); |
| 118 | difficulty = static_cast<difficultyType>(buffer[4]); |
| 119 | maxPlayers = buffer[5]; |
| 120 | nPlayers = buffer[6]; |
| 121 | clientID = buffer[7]; |
| 122 | |
| 123 | printf("Game mode %d, difficulty %d, %d of %d players.\n", modeType, +difficulty, nPlayers, maxPlayers); |
| 124 | |
| 125 | if (nPlayers > maxPlayers) { |
| 126 | |
| 127 | net->close(sock); |
| 128 | |
| 129 | throw E_DATA; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | |
| 134 | mode = createMode(modeType); |
| 135 | |
| 136 | if (!mode) { |
| 137 | |
| 138 | net->close(sock); |
| 139 | |
| 140 | throw E_DATA; |
| 141 | |
| 142 | } |
| 143 | |
| 144 | |
| 145 | // Create players |
| 146 | nPlayers = 0; |
| 147 | players = new Player[maxPlayers]; |
| 148 | |
| 149 | |
| 150 | // Download the level from the server |
| 151 | |
| 152 | levelFile = createString(LEVEL_FILE); |
| 153 | file = NULL; |
| 154 | |
| 155 | ret = setLevel(NULL); |
| 156 | |
| 157 | if (ret < 0) { |
| 158 | |
| 159 | net->close(sock); |
| 160 | |
| 161 | if (file) delete file; |
| 162 | |
| 163 | delete mode; |
| 164 | |
| 165 | throw ret; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | // Add a new player to the game |
| 170 | |
| 171 | buffer[0] = MTL_G_PJOIN + strlen(setup.characterName); |
| 172 | buffer[1] = MT_G_PJOIN; |
| 173 | buffer[2] = clientID; |
| 174 | buffer[3] = 0; // Player's number, assigned by the server |
| 175 | buffer[4] = 0; // Player's team, assigned by the server |
| 176 | memcpy(buffer + 5, setup.characterCols, 4); |
| 177 | memcpy(buffer + 9, setup.characterName, strlen(setup.characterName) + 1); |
| 178 | send(buffer); |
| 179 | |
| 180 | |
| 181 | // Wait for acknowledgement |
| 182 | |
| 183 | localPlayer = NULL; |
| 184 | |
| 185 | while (!localPlayer) { |
| 186 | |
| 187 | if (loop(NORMAL_LOOP) == E_QUIT) { |
| 188 | |
| 189 | net->close(sock); |
| 190 | |
| 191 | if (file) delete file; |
| 192 | |
| 193 | delete mode; |
| 194 | |
| 195 | throw E_QUIT; |
| 196 | |
| 197 | } |
| 198 | |
| 199 | if (controls.release(C_ESCAPE)) { |
| 200 | |
| 201 | net->close(sock); |
| 202 | |
| 203 | if (file) delete file; |
| 204 | |
| 205 | delete mode; |
| 206 | |
| 207 | throw E_RETURN; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | video.clearScreen(0); |
| 212 | fontmn2->showStringCentered("JOINING GAME"); |
| 213 | |
| 214 | ret = step(0); |
| 215 | |
| 216 | if (ret < 0) { |
| 217 | |
| 218 | net->close(sock); |
| 219 | |
| 220 | if (file) delete file; |
| 221 | |
| 222 | delete mode; |
| 223 | |
| 224 | throw ret; |
| 225 | |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Disconnect and destroy client |
| 235 | */ |
| 236 | ClientGame::~ClientGame () { |
| 237 | |
| 238 | net->close(sock); |
| 239 | |
| 240 | if (file) delete file; |
| 241 | |
| 242 | delete mode; |
| 243 | |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Set the next level and receive level data from server |
| 249 | * |
| 250 | * @param fileName The file name of the next level |
| 251 | * |
| 252 | * @return Error code |
| 253 | */ |
| 254 | int ClientGame::setLevel (char* fileName) { |
| 255 | |
| 256 | (void)fileName; |
| 257 | |
| 258 | int ret; |
| 259 | |
| 260 | video.setPalette(menuPalette); |
| 261 | |
| 262 | // Wait for level data to start arriving |
| 263 | while (!file && levelFile) { |
| 264 | |
| 265 | if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT; |
| 266 | |
| 267 | if (controls.release(C_ESCAPE)) return E_RETURN; |
| 268 | |
| 269 | SDL_Delay(T_MENU_FRAME); |
| 270 | |
| 271 | video.clearScreen(0); |
| 272 | fontmn2->showStringCentered("WAITING FOR SERVER"); |
| 273 | |
| 274 | ret = step(0); |
| 275 | |
| 276 | if (ret < 0) return ret; |
| 277 | |
| 278 | } |
| 279 | |
| 280 | // Wait for level data to finish arriving |
| 281 | while (file && levelFile) { |
| 282 | |
| 283 | if (loop(NORMAL_LOOP) == E_QUIT) return E_QUIT; |
| 284 | |
| 285 | if (controls.release(C_ESCAPE)) return E_RETURN; |
| 286 | |
| 287 | SDL_Delay(T_MENU_FRAME); |
| 288 | |
| 289 | video.clearScreen(0); |
| 290 | |
| 291 | // TODO: center / align stuff better |
| 292 | fontmn2->showString("downloaded", canvasW >> 2, (canvasH >> 1) - 16); |
| 293 | fontmn2->showNumber(file->tell(), (canvasW >> 2) + 56, canvasH >> 1); |
| 294 | fontmn2->showString("bytes", (canvasW >> 2) + 64, canvasH >> 1); |
| 295 | |
| 296 | ret = step(0); |
| 297 | |
| 298 | if (ret < 0) return ret; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | return E_NONE; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Send data to server |
| 309 | * |
| 310 | * @param buffer Data to send. First byte indicates length. |
| 311 | */ |
| 312 | void ClientGame::send (unsigned char* buffer) { |
| 313 | |
| 314 | net->send(sock, buffer); |
| 315 | |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /** |
| 320 | * Game iteration |
| 321 | * |
| 322 | * @param ticks Current time |
| 323 | * |
| 324 | * @return Error code |
| 325 | */ |
| 326 | int ClientGame::step (unsigned int ticks) { |
| 327 | |
| 328 | int length; |
| 329 | |
| 330 | // Receive data from server |
| 331 | |
| 332 | if (received == 0) { |
| 333 | |
| 334 | // Not currently receiving a message |
| 335 | // See if there is a new message to receive |
| 336 | |
| 337 | length = net->recv(sock, recvBuffer, 1); |
| 338 | |
| 339 | if (length > 0) received++; |
| 340 | |
| 341 | } |
| 342 | |
| 343 | if (received > 0) { |
| 344 | |
| 345 | // Currently receiving a message |
| 346 | // See if there is any more data |
| 347 | |
| 348 | length = net->recv(sock, recvBuffer + received, |
| 349 | recvBuffer[0] - received); |
| 350 | |
| 351 | if (length > 0) received += length; |
| 352 | |
| 353 | |
| 354 | // See if the whole message has arrived |
| 355 | |
| 356 | if (received >= recvBuffer[0]) { |
| 357 | |
| 358 | switch (recvBuffer[1] & MCMASK) { |
| 359 | |
| 360 | case MC_GAME: |
| 361 | |
| 362 | if (recvBuffer[1] == MT_G_LEVEL) { |
| 363 | |
| 364 | bool firstMessage; |
| 365 | |
| 366 | if (!file) { |
| 367 | |
| 368 | // Not already storing level data, so open the file |
| 369 | |
| 370 | try { |
| 371 | |
| 372 | file = new File(levelFile, PATH_TYPE_TEMP, true); |
| 373 | |
| 374 | } catch (int e) { |
| 375 | |
| 376 | return e; |
| 377 | |
| 378 | } |
| 379 | |
| 380 | firstMessage = true; |
| 381 | |
| 382 | } else |
| 383 | firstMessage = false; |
| 384 | |
| 385 | if (file) { |
| 386 | |
| 387 | file->seek((recvBuffer[2] << 8) + recvBuffer[3], true); |
| 388 | |
| 389 | for (int i = 4; i < recvBuffer[0]; i++) |
| 390 | file->storeChar(recvBuffer[i]); |
| 391 | |
| 392 | } |
| 393 | |
| 394 | // If a zero-length block has been sent, it is the last |
| 395 | if (recvBuffer[0] == MTL_G_LEVEL) { |
| 396 | |
| 397 | if (firstMessage) { |
| 398 | |
| 399 | // If the last message was also the first, |
| 400 | // then the run of levels has ended |
| 401 | |
| 402 | delete[] levelFile; |
| 403 | levelFile = NULL; |
| 404 | |
| 405 | } |
| 406 | |
| 407 | delete file; |
| 408 | file = NULL; |
| 409 | |
| 410 | } |
| 411 | |
| 412 | break; |
| 413 | |
| 414 | } |
| 415 | |
| 416 | if ((recvBuffer[1] == MT_G_PJOIN) && |
| 417 | (recvBuffer[3] < maxPlayers)) { |
| 418 | |
| 419 | printf("Player %d joined the game.\n", recvBuffer[3]); |
| 420 | |
| 421 | // Add the new player, and any that have been missed |
| 422 | int player_num; |
| 423 | for (player_num = nPlayers; player_num <= recvBuffer[3]; player_num++) { |
| 424 | |
| 425 | players[player_num].init(this, reinterpret_cast<char*>(recvBuffer + 9), |
| 426 | recvBuffer + 5, recvBuffer[4]); |
| 427 | addLevelPlayer(players + player_num); |
| 428 | |
| 429 | printf("Player %d joined team %d.\n", player_num, recvBuffer[4]); |
| 430 | |
| 431 | } |
| 432 | |
| 433 | nPlayers = player_num; |
| 434 | |
| 435 | if (recvBuffer[2] == clientID) |
| 436 | localPlayer = players + recvBuffer[3]; |
| 437 | |
| 438 | } |
| 439 | |
| 440 | if ((recvBuffer[1] == MT_G_PQUIT) && |
| 441 | (recvBuffer[2] < nPlayers)) { |
| 442 | |
| 443 | printf("Player %d left the game.\n", recvBuffer[2]); |
| 444 | |
| 445 | // Remove the player |
| 446 | |
| 447 | players[recvBuffer[2]].deinit(); |
| 448 | |
| 449 | // If necessary, move more recent players |
| 450 | for (int i = recvBuffer[2]; i < nPlayers; i++) |
| 451 | memcpy(static_cast<void*>(players + i), players + i + 1, |
| 452 | sizeof(Player)); |
| 453 | |
| 454 | // Clear duplicate pointers |
| 455 | memset(static_cast<void*>(players + nPlayers), 0, sizeof(Player)); |
| 456 | |
| 457 | } |
| 458 | |
| 459 | if (recvBuffer[1] == MT_G_CHECK) { |
| 460 | |
| 461 | checkX = recvBuffer[2]; |
| 462 | checkY = recvBuffer[3]; |
| 463 | |
| 464 | if (recvBuffer[0] > 4) { |
| 465 | |
| 466 | checkX += recvBuffer[4] << 8; |
| 467 | checkY += recvBuffer[5] << 8; |
| 468 | |
| 469 | } |
| 470 | |
| 471 | } |
| 472 | |
| 473 | if (recvBuffer[1] == MT_G_SCORE) { |
| 474 | |
| 475 | for (int i = 0; i < nPlayers; i++) { |
| 476 | |
| 477 | if (players[i].getTeam() == recvBuffer[2]) |
| 478 | players[i].teamScore++; |
| 479 | |
| 480 | } |
| 481 | |
| 482 | } |
| 483 | |
| 484 | if (recvBuffer[1] == MT_G_LTYPE) { |
| 485 | |
| 486 | levelType = (LevelType)recvBuffer[2]; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | break; |
| 491 | |
| 492 | case MC_LEVEL: |
| 493 | |
| 494 | if (baseLevel) baseLevel->receive(recvBuffer); |
| 495 | |
| 496 | break; |
| 497 | |
| 498 | case MC_PLAYER: |
| 499 | |
| 500 | if (recvBuffer[2] < maxPlayers) |
| 501 | players[recvBuffer[2]].receive(recvBuffer); |
| 502 | |
| 503 | break; |
| 504 | |
| 505 | } |
| 506 | |
| 507 | received = 0; |
| 508 | |
| 509 | } |
| 510 | |
| 511 | } |
| 512 | |
| 513 | if (ticks >= checkTime) { |
| 514 | |
| 515 | // Check for disconnection |
| 516 | |
| 517 | if (!(net->isConnected(sock))) { |
| 518 | |
| 519 | if (file) delete file; |
| 520 | file = NULL; |
| 521 | |
| 522 | return E_N_DISCONNECT; |
| 523 | |
| 524 | } |
| 525 | |
| 526 | checkTime = ticks + T_CCHECK; |
| 527 | |
| 528 | } |
| 529 | |
| 530 | if (localPlayer && (ticks >= sendTime)) { |
| 531 | |
| 532 | // Update server |
| 533 | unsigned char sendBuffer[BUFFER_LENGTH]; |
| 534 | sendBuffer[0] = MTL_P_TEMP; |
| 535 | sendBuffer[1] = MT_P_TEMP; |
| 536 | sendBuffer[2] = 0; |
| 537 | localPlayer->send(sendBuffer); |
| 538 | send(sendBuffer); |
| 539 | |
| 540 | sendTime = ticks + T_CSEND; |
| 541 | |
| 542 | } |
| 543 | |
| 544 | return E_NONE; |
| 545 | |
| 546 | } |
| 547 | |
| 548 | |
| 549 | /** |
| 550 | * Ask server to award team a point |
| 551 | * |
| 552 | * @param team Team to receive point |
| 553 | */ |
| 554 | void ClientGame::score (unsigned char team) { |
| 555 | |
| 556 | unsigned char buffer[MTL_G_SCORE]; |
| 557 | |
| 558 | // Inform server |
| 559 | buffer[0] = MTL_G_SCORE; |
| 560 | buffer[1] = MT_G_SCORE; |
| 561 | buffer[2] = team; |
| 562 | send(buffer); |
| 563 | |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /** |
| 568 | * Ask server to approve new checkpoint |
| 569 | * |
| 570 | * @param gridX X-coordinate (in tiles) of the checkpoint |
| 571 | * @param gridY Y-coordinate (in tiles) of the checkpoint |
| 572 | */ |
| 573 | void ClientGame::setCheckpoint (int gridX, int gridY) { |
| 574 | |
| 575 | unsigned char buffer[MTL_G_CHECK]; |
| 576 | |
| 577 | buffer[0] = MTL_G_CHECK; |
| 578 | buffer[1] = MT_G_CHECK; |
| 579 | buffer[2] = gridX & 0xFF; |
| 580 | buffer[3] = gridY & 0xFF; |
| 581 | buffer[4] = (gridX >> 8) & 0xFF; |
| 582 | buffer[5] = (gridY >> 8) & 0xFF; |
| 583 | send(buffer); |
| 584 | |
| 585 | } |