| 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"cxx . . $ `<file>/src/game/game.h`/ |
| 25 | |
| 26 | #include "io/file.h"cxx . . $ `<file>/src/io/file.h`/ |
| 27 | #include "io/gfx/font.h"cxx . . $ `<file>/src/io/gfx/font.h`/ |
| 28 | #include "io/gfx/video.h"cxx . . $ `<file>/src/io/gfx/video.h`/ |
| 29 | #include "io/network.h"cxx . . $ `<file>/src/io/network.h`/ |
| 30 | #include "player/player.h"cxx . . $ `<file>/src/player/player.h`/ |
| 31 | #include "setup.h"cxx . . $ `<file>/src/setup.h`/ |
| 32 | #include "util.h"cxx . . $ `<file>/src/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 | ServerGamecxx . . $ ServerGame#::ServerGamecxx . . $ ServerGame#ServerGame(b6c1c4f70c1ea4fa). (GameModeTypecxx . . $ GameModeType# modeType, char* firstLevel, difficultyTypecxx . . $ difficultyType# gameDifficulty) { |
| 45 | |
| 46 | int count; |
| 47 | |
| 48 | |
| 49 | // Create the server |
| 50 | |
| 51 | sockcxx . . $ ServerGame#sock. = netcxx . . $ net.->hostcxx . . $ Network#host(b126dc7c1de90089).(); |
| 52 | |
| 53 | if (sockcxx . . $ ServerGame#sock. < 0) throw sockcxx . . $ ServerGame#sock.; // Tee hee. Throw sock. |
| 54 | |
| 55 | |
| 56 | // Create the players |
| 57 | |
| 58 | nPlayerscxx . . $ nPlayers. = 1; |
| 59 | localPlayercxx . . $ localPlayer. = playerscxx . . $ players. = new Playercxx . . $ Player#Player(49f6e7a06ebc5aa8).[MAX_PLAYERScxx . . $ `src/game/gamemode.h:29:9`!]; |
| 60 | localPlayercxx . . $ localPlayer.->initcxx . . $ Player#init(eadcf6f4775e9cbb).(this, setupcxx . . $ setup..characterNamecxx . . $ Setup#characterName., setupcxx . . $ setup..characterColscxx . . $ Setup#characterCols., 0); |
| 61 | |
| 62 | for (count = 0; count < MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!; count++) |
| 63 | clientPlayercxx . . $ ServerGame#clientPlayer.[count] = clientStatuscxx . . $ ServerGame#clientStatus.[count] = -1; |
| 64 | |
| 65 | |
| 66 | // Copy the first level into memory |
| 67 | |
| 68 | levelFilecxx . . $ Game#levelFile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 69 | levelDatacxx . . $ ServerGame#levelData. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 70 | |
| 71 | count = setLevelcxx . . $ ServerGame#setLevel(1ef8e6ec083aabb9).(firstLevel); |
| 72 | |
| 73 | if (count < 0) { |
| 74 | |
| 75 | netcxx . . $ net.->closecxx . . $ Network#close(d4f767463ce0a6b3).(sockcxx . . $ ServerGame#sock.); |
| 76 | |
| 77 | if (levelDatacxx . . $ ServerGame#levelData.) delete[] levelDatacxx . . $ ServerGame#levelData.; |
| 78 | |
| 79 | throw count; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | difficultycxx . . $ Game#difficulty. = gameDifficulty; |
| 84 | |
| 85 | modecxx . . $ Game#mode. = createModecxx . . $ Game#createMode(14876a996d2f6cf).(modeType); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Disconnect clients and destroy server |
| 92 | */ |
| 93 | ServerGamecxx . . $ ServerGame#::~cxx . . $ ServerGame#`~ServerGame`(49f6e7a06ebc5aa8).ServerGamecxx . . $ ServerGame# () { |
| 94 | |
| 95 | int count; |
| 96 | |
| 97 | for (count = 0; count < MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!; count++) { |
| 98 | |
| 99 | if (clientStatuscxx . . $ ServerGame#clientStatus.[count] != -1) netcxx . . $ net.->closecxx . . $ Network#close(d4f767463ce0a6b3).(clientSockcxx . . $ ServerGame#clientSock.[count]); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | netcxx . . $ net.->closecxx . . $ Network#close(d4f767463ce0a6b3).(sockcxx . . $ ServerGame#sock.); |
| 104 | |
| 105 | if (levelDatacxx . . $ ServerGame#levelData.) delete[] levelDatacxx . . $ ServerGame#levelData.; |
| 106 | |
| 107 | delete modecxx . . $ Game#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 ServerGamecxx . . $ ServerGame#::setLevelcxx . . $ ServerGame#setLevel(1ef8e6ec083aabb9). (char* fileName) { |
| 120 | |
| 121 | Filecxx . . $ File#* file; |
| 122 | int count; |
| 123 | |
| 124 | if (levelFilecxx . . $ Game#levelFile.) delete[] levelFilecxx . . $ Game#levelFile.; |
| 125 | if (levelDatacxx . . $ ServerGame#levelData.) delete[] levelDatacxx . . $ ServerGame#levelData.; |
| 126 | |
| 127 | // The new level will be sent to all clients |
| 128 | for (count = 0; count < MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!; count++) { |
| 129 | |
| 130 | if (clientStatuscxx . . $ ServerGame#clientStatus.[count] != -1) clientStatuscxx . . $ ServerGame#clientStatus.[count] = 0; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | if (!fileName) { |
| 135 | |
| 136 | levelFilecxx . . $ Game#levelFile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 137 | levelDatacxx . . $ ServerGame#levelData. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 138 | |
| 139 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | try { |
| 144 | |
| 145 | file = new Filecxx . . $ File#File(e99a0a23efd851c6).(fileName, PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME.); |
| 146 | |
| 147 | } catch (int e) { |
| 148 | |
| 149 | levelFilecxx . . $ Game#levelFile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 150 | levelDatacxx . . $ ServerGame#levelData. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 151 | |
| 152 | return e; |
| 153 | |
| 154 | } |
| 155 | |
| 156 | levelFilecxx . . $ Game#levelFile. = createStringcxx . . $ createString(a8b8e32347182212).(fileName); |
| 157 | |
| 158 | // Load the entire file into memory |
| 159 | levelSizecxx . . $ ServerGame#levelSize. = file->getSizecxx . . $ File#getSize(b126dc7c1de90089).(); |
| 160 | levelDatacxx . . $ ServerGame#levelData. = file->loadBlockcxx . . $ File#loadBlock(f18dc31a6ecbac56).(levelSizecxx . . $ ServerGame#levelSize.); |
| 161 | |
| 162 | delete file; |
| 163 | |
| 164 | levelTypecxx . . $ Game#levelType. = getLevelTypecxx . . $ Game#getLevelType(b61afb7421387496).(fileName); |
| 165 | |
| 166 | if (levelTypecxx . . $ Game#levelType. != LT_JJ1cxx . . $ LT_JJ1.) return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 167 | |
| 168 | // Modify the extension section to match the actual extension |
| 169 | count = levelSizecxx . . $ ServerGame#levelSize. - 5; |
| 170 | while (levelDatacxx . . $ ServerGame#levelData.[count - 1] != 3) count--; |
| 171 | levelDatacxx . . $ ServerGame#levelData.[count] = fileName[strlen(fileName) - 3]; |
| 172 | levelDatacxx . . $ ServerGame#levelData.[count + 1] = fileName[strlen(fileName) - 2]; |
| 173 | levelDatacxx . . $ ServerGame#levelData.[count + 2] = fileName[strlen(fileName) - 1]; |
| 174 | |
| 175 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Send data to clients |
| 182 | * |
| 183 | * @param buffer Data to send. First byte indicates length. |
| 184 | */ |
| 185 | void ServerGamecxx . . $ ServerGame#::sendcxx . . $ ServerGame#send(f25546b61a629de7). (unsigned char* buffer) { |
| 186 | |
| 187 | int count; |
| 188 | |
| 189 | for (count = 0; count < MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!; 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 ((clientStatuscxx . . $ ServerGame#clientStatus.[count] != -1) && |
| 194 | (((buffer[1] & MCMASKcxx . . $ `src/game/game.h:42:9`!) != MC_PLAYERcxx . . $ `src/game/game.h:45:9`!) || |
| 195 | (buffer[2] != clientPlayercxx . . $ ServerGame#clientPlayer.[count]))) |
| 196 | netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#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 ServerGamecxx . . $ ServerGame#::stepcxx . . $ ServerGame#step(4c679297bf94b9eb). (unsigned int ticks) { |
| 211 | |
| 212 | unsigned char sendBuffer[BUFFER_LENGTHcxx . . $ `src/game/game.h:78:9`!]; |
| 213 | int count, pcount, length; |
| 214 | |
| 215 | for (count = 0; count < MAX_CLIENTScxx . . $ `src/io/network.h:44:9`!; count++) { |
| 216 | |
| 217 | if (clientStatuscxx . . $ ServerGame#clientStatus.[count] >= 0) { |
| 218 | |
| 219 | if (clientStatuscxx . . $ ServerGame#clientStatus.[count] == 0) { |
| 220 | |
| 221 | // Send level type |
| 222 | sendBuffer[0] = MTL_G_LTYPEcxx . . $ `src/game/game.h:69:9`!; |
| 223 | sendBuffer[1] = MT_G_LTYPEcxx . . $ `src/game/game.h:53:9`!; |
| 224 | sendBuffer[2] = levelTypecxx . . $ Game#levelType.; |
| 225 | netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#clientSock.[count], sendBuffer); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | // Client is connected, but not operational |
| 230 | // Send a chunk of the level |
| 231 | |
| 232 | length = levelSizecxx . . $ ServerGame#levelSize. - clientStatuscxx . . $ ServerGame#clientStatus.[count]; |
| 233 | |
| 234 | if (length > 251) length = 251; |
| 235 | |
| 236 | sendBuffer[0] = MTL_G_LEVELcxx . . $ `src/game/game.h:66:9`! + length; |
| 237 | sendBuffer[1] = MT_G_LEVELcxx . . $ `src/game/game.h:50:9`!; |
| 238 | sendBuffer[2] = clientStatuscxx . . $ ServerGame#clientStatus.[count] >> 8; |
| 239 | sendBuffer[3] = clientStatuscxx . . $ ServerGame#clientStatus.[count] & 255; |
| 240 | memcpy(sendBuffer + 4, levelDatacxx . . $ ServerGame#levelData. + clientStatuscxx . . $ ServerGame#clientStatus.[count], length); |
| 241 | length = netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#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_LEVELcxx . . $ `src/game/game.h:66:9`!) clientStatuscxx . . $ ServerGame#clientStatus.[count] = -2; |
| 246 | else if (length > 0) clientStatuscxx . . $ ServerGame#clientStatus.[count] += length - MTL_G_LEVELcxx . . $ `src/game/game.h:66:9`!; |
| 247 | |
| 248 | } |
| 249 | |
| 250 | |
| 251 | if ((clientStatuscxx . . $ ServerGame#clientStatus.[count] == -2) && (receivedcxx . . $ ServerGame#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 = netcxx . . $ net.->recvcxx . . $ Network#recv(ef9024b465536c23).(clientSockcxx . . $ ServerGame#clientSock.[count], recvBufferscxx . . $ ServerGame#recvBuffers.[count], 1); |
| 257 | |
| 258 | if (length > 0) receivedcxx . . $ ServerGame#received.[count]++; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | if ((clientStatuscxx . . $ ServerGame#clientStatus.[count] == -2) && (receivedcxx . . $ ServerGame#received.[count] > 0)) { |
| 264 | |
| 265 | // Currently receiving a message |
| 266 | // See if there is any more data |
| 267 | |
| 268 | length = netcxx . . $ net.->recvcxx . . $ Network#recv(ef9024b465536c23).(clientSockcxx . . $ ServerGame#clientSock.[count], |
| 269 | recvBufferscxx . . $ ServerGame#recvBuffers.[count] + receivedcxx . . $ ServerGame#received.[count], |
| 270 | recvBufferscxx . . $ ServerGame#recvBuffers.[count][0] - receivedcxx . . $ ServerGame#received.[count]); |
| 271 | |
| 272 | if (length > 0) receivedcxx . . $ ServerGame#received.[count] += length; |
| 273 | |
| 274 | |
| 275 | // See if the whole message has arrived |
| 276 | |
| 277 | if (receivedcxx . . $ ServerGame#received.[count] >= recvBufferscxx . . $ ServerGame#recvBuffers.[count][0]) { |
| 278 | |
| 279 | switch (recvBufferscxx . . $ ServerGame#recvBuffers.[count][1] & MCMASKcxx . . $ `src/game/game.h:42:9`!) { |
| 280 | |
| 281 | case MC_GAMEcxx . . $ `src/game/game.h:43:9`!: |
| 282 | |
| 283 | if ((recvBufferscxx . . $ ServerGame#recvBuffers.[count][1] == MT_G_PJOINcxx . . $ `src/game/game.h:48:9`!) && |
| 284 | (clientPlayercxx . . $ ServerGame#clientPlayer.[count] == -1)) { |
| 285 | |
| 286 | printf("Player %d (client %d) joined the game.\n", nPlayerscxx . . $ nPlayers., count); |
| 287 | |
| 288 | |
| 289 | // Set up the new player |
| 290 | |
| 291 | recvBufferscxx . . $ ServerGame#recvBuffers.[count][4] = modecxx . . $ Game#mode.->chooseTeamcxx . . $ GameMode#chooseTeam(7c4be072098b358f).(); |
| 292 | |
| 293 | playerscxx . . $ players.[nPlayerscxx . . $ nPlayers.].initcxx . . $ Player#init(eadcf6f4775e9cbb).(this, |
| 294 | reinterpret_cast<char*>(recvBufferscxx . . $ ServerGame#recvBuffers.[count] + 9), |
| 295 | recvBufferscxx . . $ ServerGame#recvBuffers.[count] + 5, recvBufferscxx . . $ ServerGame#recvBuffers.[count][4]); |
| 296 | addLevelPlayercxx . . $ Game#addLevelPlayer(34e2c302b1f0e5dd).(playerscxx . . $ players. + nPlayerscxx . . $ nPlayers.); |
| 297 | |
| 298 | printf("Player %d joined team %d.\n", nPlayerscxx . . $ nPlayers., recvBufferscxx . . $ ServerGame#recvBuffers.[count][4]); |
| 299 | |
| 300 | recvBufferscxx . . $ ServerGame#recvBuffers.[count][3] = clientPlayercxx . . $ ServerGame#clientPlayer.[count] = nPlayerscxx . . $ nPlayers.; |
| 301 | |
| 302 | nPlayerscxx . . $ nPlayers.++; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | if (recvBufferscxx . . $ ServerGame#recvBuffers.[count][1] == MT_G_CHECKcxx . . $ `src/game/game.h:51:9`!) { |
| 307 | |
| 308 | checkXcxx . . $ Game#checkX. = recvBufferscxx . . $ ServerGame#recvBuffers.[count][2]; |
| 309 | checkYcxx . . $ Game#checkY. = recvBufferscxx . . $ ServerGame#recvBuffers.[count][3]; |
| 310 | |
| 311 | if (recvBufferscxx . . $ ServerGame#recvBuffers.[count][0] > 4) { |
| 312 | |
| 313 | checkXcxx . . $ Game#checkX. += recvBufferscxx . . $ ServerGame#recvBuffers.[count][4] << 8; |
| 314 | checkYcxx . . $ Game#checkY. += recvBufferscxx . . $ ServerGame#recvBuffers.[count][5] << 8; |
| 315 | |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | if (recvBufferscxx . . $ ServerGame#recvBuffers.[count][1] == MT_G_SCOREcxx . . $ `src/game/game.h:52:9`!) { |
| 321 | |
| 322 | for (pcount = 0; pcount < nPlayerscxx . . $ nPlayers.; pcount++) { |
| 323 | |
| 324 | if (playerscxx . . $ players.[pcount].getTeamcxx . . $ Player#getTeam(7c4be072098b358f).() == recvBufferscxx . . $ ServerGame#recvBuffers.[count][2]) |
| 325 | playerscxx . . $ players.[pcount].teamScorecxx . . $ Player#teamScore.++; |
| 326 | |
| 327 | } |
| 328 | |
| 329 | } |
| 330 | |
| 331 | break; |
| 332 | |
| 333 | case MC_LEVELcxx . . $ `src/game/game.h:44:9`!: |
| 334 | |
| 335 | baseLevelcxx . . $ Game#baseLevel.->receivecxx . . $ Level#receive(f25546b61a629de7).(recvBufferscxx . . $ ServerGame#recvBuffers.[count]); |
| 336 | |
| 337 | break; |
| 338 | |
| 339 | case MC_PLAYERcxx . . $ `src/game/game.h:45:9`!: |
| 340 | |
| 341 | if (clientPlayercxx . . $ ServerGame#clientPlayer.[count] != -1) { |
| 342 | |
| 343 | // Assign player byte based on sender |
| 344 | recvBufferscxx . . $ ServerGame#recvBuffers.[count][2] = clientPlayercxx . . $ ServerGame#clientPlayer.[count]; |
| 345 | |
| 346 | playerscxx . . $ players.[clientPlayercxx . . $ ServerGame#clientPlayer.[count]].receivecxx . . $ Player#receive(f25546b61a629de7).(recvBufferscxx . . $ ServerGame#recvBuffers.[count]); |
| 347 | |
| 348 | } |
| 349 | |
| 350 | break; |
| 351 | |
| 352 | } |
| 353 | |
| 354 | // Update clients |
| 355 | sendcxx . . $ ServerGame#send(f25546b61a629de7).(recvBufferscxx . . $ ServerGame#recvBuffers.[count]); |
| 356 | |
| 357 | receivedcxx . . $ ServerGame#received.[count] = 0; |
| 358 | |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | |
| 363 | if (ticks >= checkTimecxx . . $ Game#checkTime.) { |
| 364 | |
| 365 | if ((clientStatuscxx . . $ ServerGame#clientStatus.[count] == -1) && levelDatacxx . . $ ServerGame#levelData.) { |
| 366 | |
| 367 | // Client is not connected |
| 368 | // Check for new connection |
| 369 | |
| 370 | clientSockcxx . . $ ServerGame#clientSock.[count] = netcxx . . $ net.->acceptcxx . . $ Network#accept(7864480464b09eea).(sockcxx . . $ ServerGame#sock.); |
| 371 | |
| 372 | if (clientSockcxx . . $ ServerGame#clientSock.[count] != -1) { |
| 373 | |
| 374 | printf("Client %d connected.\n", count); |
| 375 | |
| 376 | clientPlayercxx . . $ ServerGame#clientPlayer.[count] = -1; |
| 377 | receivedcxx . . $ ServerGame#received.[count] = 0; |
| 378 | |
| 379 | // Incorporate the new client |
| 380 | |
| 381 | // Send data |
| 382 | sendBuffer[0] = MTL_G_PROPScxx . . $ `src/game/game.h:63:9`!; |
| 383 | sendBuffer[1] = MT_G_PROPScxx . . $ `src/game/game.h:47:9`!; |
| 384 | sendBuffer[2] = 1; // Server version |
| 385 | sendBuffer[3] = modecxx . . $ Game#mode.->getModecxx . . $ GameMode#getMode(e679e067a917a25c).(); |
| 386 | sendBuffer[4] = +cxx . . $ operator+(19ccb40277a922df).difficultycxx . . $ Game#difficulty.; |
| 387 | sendBuffer[5] = MAX_PLAYERScxx . . $ `src/game/gamemode.h:29:9`!; |
| 388 | sendBuffer[6] = nPlayerscxx . . $ nPlayers.; // Number of players |
| 389 | sendBuffer[7] = count; // Client's clientID |
| 390 | netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#clientSock.[count], sendBuffer); |
| 391 | |
| 392 | // Initiate sending of level data |
| 393 | clientStatuscxx . . $ ServerGame#clientStatus.[count] = 0; |
| 394 | |
| 395 | // Inform the new client of the checkpoint |
| 396 | sendBuffer[0] = MTL_G_CHECKcxx . . $ `src/game/game.h:67:9`!; |
| 397 | sendBuffer[1] = MT_G_CHECKcxx . . $ `src/game/game.h:51:9`!; |
| 398 | sendBuffer[2] = checkXcxx . . $ Game#checkX. & 0xFF; |
| 399 | sendBuffer[3] = checkYcxx . . $ Game#checkY. & 0xFF; |
| 400 | sendBuffer[4] = (checkXcxx . . $ Game#checkX. >> 8) & 0xFF; |
| 401 | sendBuffer[5] = (checkYcxx . . $ Game#checkY. >> 8) & 0xFF; |
| 402 | netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#clientSock.[count], sendBuffer); |
| 403 | |
| 404 | // Inform the new client of the existing players |
| 405 | |
| 406 | sendBuffer[1] = MT_G_PJOINcxx . . $ `src/game/game.h:48:9`!; |
| 407 | |
| 408 | for (pcount = 0; pcount < nPlayerscxx . . $ nPlayers.; pcount++) { |
| 409 | |
| 410 | sendBuffer[0] = MTL_G_PJOINcxx . . $ `src/game/game.h:64:9`! + strlen(playerscxx . . $ players.[pcount].getNamecxx . . $ Player#getName(41208ce6ca65dd58).()); |
| 411 | sendBuffer[2] = count; |
| 412 | sendBuffer[3] = pcount; |
| 413 | sendBuffer[4] = playerscxx . . $ players.[pcount].getTeamcxx . . $ Player#getTeam(7c4be072098b358f).(); |
| 414 | memcpy(sendBuffer + 5, playerscxx . . $ players.[pcount].getColscxx . . $ Player#getCols(37d8967d10cb1e7b).(), PCOLOURScxx . . $ `src/player/player.h:48:9`!); |
| 415 | memcpy(sendBuffer + 9, playerscxx . . $ players.[pcount].getNamecxx . . $ Player#getName(41208ce6ca65dd58).(), strlen(playerscxx . . $ players.[pcount].getNamecxx . . $ Player#getName(41208ce6ca65dd58).()) + 1); |
| 416 | |
| 417 | netcxx . . $ net.->sendcxx . . $ Network#send(b2cc8657ba51080d).(clientSockcxx . . $ ServerGame#clientSock.[count], sendBuffer); |
| 418 | |
| 419 | } |
| 420 | |
| 421 | } |
| 422 | |
| 423 | } else { |
| 424 | |
| 425 | // Client is connected |
| 426 | // Check for disconnection |
| 427 | |
| 428 | if (!(netcxx . . $ net.->isConnectedcxx . . $ Network#isConnected(83c267b20bac841a).(clientSockcxx . . $ ServerGame#clientSock.[count]))) { |
| 429 | |
| 430 | printf("Client %d disconnected (code: %d).\n", count, netcxx . . $ net.->getErrorcxx . . $ Network#getError(b126dc7c1de90089).()); |
| 431 | |
| 432 | // Disconnect client |
| 433 | netcxx . . $ net.->closecxx . . $ Network#close(d4f767463ce0a6b3).(clientSockcxx . . $ ServerGame#clientSock.[count]); |
| 434 | clientStatuscxx . . $ ServerGame#clientStatus.[count] = -1; |
| 435 | |
| 436 | if (clientPlayercxx . . $ ServerGame#clientPlayer.[count] != -1) { |
| 437 | |
| 438 | // Remove the client's player |
| 439 | |
| 440 | printf("Player %d (client %d) left the game.\n", clientPlayercxx . . $ ServerGame#clientPlayer.[count], count); |
| 441 | |
| 442 | nPlayerscxx . . $ nPlayers.--; |
| 443 | |
| 444 | playerscxx . . $ players.[clientPlayercxx . . $ ServerGame#clientPlayer.[count]].deinitcxx . . $ Player#deinit(49f6e7a06ebc5aa8).(); |
| 445 | |
| 446 | // If necessary, move more recent players |
| 447 | for (pcount = clientPlayercxx . . $ ServerGame#clientPlayer.[count]; pcount < nPlayerscxx . . $ nPlayers.; pcount++) |
| 448 | memcpy(static_cast<void*>(playerscxx . . $ players. + pcount), playerscxx . . $ players. + pcount + 1, sizeof(Playercxx . . $ Player#)); |
| 449 | |
| 450 | // Clear duplicate pointers |
| 451 | memset(static_cast<void*>(playerscxx . . $ players. + nPlayerscxx . . $ nPlayers.), 0, sizeof(Playercxx . . $ Player#)); |
| 452 | |
| 453 | // Inform remaining clients that the player has left |
| 454 | sendBuffer[0] = MTL_G_PQUITcxx . . $ `src/game/game.h:65:9`!; |
| 455 | sendBuffer[1] = MT_G_PQUITcxx . . $ `src/game/game.h:49:9`!; |
| 456 | sendBuffer[2] = clientPlayercxx . . $ ServerGame#clientPlayer.[count]; |
| 457 | sendcxx . . $ ServerGame#send(f25546b61a629de7).(sendBuffer); |
| 458 | |
| 459 | clientPlayercxx . . $ ServerGame#clientPlayer.[count] = -1; |
| 460 | |
| 461 | } |
| 462 | |
| 463 | } |
| 464 | |
| 465 | } |
| 466 | |
| 467 | } |
| 468 | |
| 469 | } |
| 470 | |
| 471 | if (ticks >= checkTimecxx . . $ Game#checkTime.) checkTimecxx . . $ Game#checkTime. = ticks + T_SCHECKcxx . . $ `src/game/game.h:37:9`!; |
| 472 | |
| 473 | if (ticks >= sendTimecxx . . $ Game#sendTime.) { |
| 474 | |
| 475 | // Update clients |
| 476 | |
| 477 | sendBuffer[0] = MTL_P_TEMPcxx . . $ `src/game/game.h:76:9`!; |
| 478 | sendBuffer[1] = MT_P_TEMPcxx . . $ `src/game/game.h:60:9`!; |
| 479 | |
| 480 | for (count = 0; count < nPlayerscxx . . $ nPlayers.; count++) { |
| 481 | |
| 482 | sendBuffer[2] = count; |
| 483 | playerscxx . . $ players.[count].sendcxx . . $ Player#send(f25546b61a629de7).(sendBuffer); |
| 484 | sendcxx . . $ ServerGame#send(f25546b61a629de7).(sendBuffer); |
| 485 | |
| 486 | } |
| 487 | |
| 488 | sendTimecxx . . $ Game#sendTime. = ticks + T_SSENDcxx . . $ `src/game/game.h:36:9`!; |
| 489 | |
| 490 | } |
| 491 | |
| 492 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 493 | |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * Assign point to team and inform clients |
| 499 | * |
| 500 | * @param team Team to receive point |
| 501 | */ |
| 502 | void ServerGamecxx . . $ ServerGame#::scorecxx . . $ ServerGame#score(ac50b47256ae7414). (unsigned char team) { |
| 503 | |
| 504 | unsigned char buffer[MTL_G_SCOREcxx . . $ `src/game/game.h:68:9`!]; |
| 505 | int count; |
| 506 | |
| 507 | // Inform clients |
| 508 | buffer[0] = MTL_G_SCOREcxx . . $ `src/game/game.h:68:9`!; |
| 509 | buffer[1] = MT_G_SCOREcxx . . $ `src/game/game.h:52:9`!; |
| 510 | buffer[2] = team; |
| 511 | sendcxx . . $ ServerGame#send(f25546b61a629de7).(buffer); |
| 512 | |
| 513 | // Update self |
| 514 | for (count = 0; count < nPlayerscxx . . $ nPlayers.; count++) { |
| 515 | |
| 516 | if (playerscxx . . $ players.[count].getTeamcxx . . $ Player#getTeam(7c4be072098b358f).() == team) playerscxx . . $ players.[count].teamScorecxx . . $ Player#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 ServerGamecxx . . $ ServerGame#::setCheckpointcxx . . $ ServerGame#setCheckpoint(3e454b8ccf442868). (int gridX, int gridY) { |
| 530 | |
| 531 | unsigned char buffer[MTL_G_CHECKcxx . . $ `src/game/game.h:67:9`!]; |
| 532 | |
| 533 | buffer[0] = MTL_G_CHECKcxx . . $ `src/game/game.h:67:9`!; |
| 534 | buffer[1] = MT_G_CHECKcxx . . $ `src/game/game.h:51:9`!; |
| 535 | buffer[2] = gridX & 0xFF; |
| 536 | buffer[3] = gridY & 0xFF; |
| 537 | buffer[4] = (gridX >> 8) & 0xFF; |
| 538 | buffer[5] = (gridY >> 8) & 0xFF; |
| 539 | sendcxx . . $ ServerGame#send(f25546b61a629de7).(buffer); |
| 540 | |
| 541 | checkXcxx . . $ Game#checkX. = gridX; |
| 542 | checkYcxx . . $ Game#checkY. = gridY; |
| 543 | |
| 544 | } |