| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file game.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 | * - 3rd June 2009: Created network.cpp from parts of game.cpp |
| 13 | * - 18th July 2009: Created servergame.cpp from parts of game.cpp |
| 14 | * - 18th July 2009: Created clientgame.cpp from parts of game.cpp |
| 15 | * - 3rd October 2010: Created localgame.cpp from parts of game.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2017 AJ Thomson |
| 19 | * |
| 20 | * OpenJazz is distributed under the terms of |
| 21 | * the GNU General Public License, version 2.0 |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "game.h"cxx . . $ `<file>/src/game/game.h`/ |
| 27 | #include "gamemode.h"cxx . . $ `<file>/src/game/gamemode.h`/ |
| 28 | |
| 29 | #include "io/gfx/video.h"cxx . . $ `<file>/src/io/gfx/video.h`/ |
| 30 | #include "io/sound.h"cxx . . $ `<file>/src/io/sound.h`/ |
| 31 | #include "jj1/bonuslevel/jj1bonuslevel.h"cxx . . $ `<file>/src/jj1/bonuslevel/jj1bonuslevel.h`/ |
| 32 | #include "jj1/level/jj1level.h"cxx . . $ `<file>/src/jj1/level/jj1level.h`/ |
| 33 | #include "jj1/planet/jj1planet.h"cxx . . $ `<file>/src/jj1/planet/jj1planet.h`/ |
| 34 | #ifdef ENABLE_JJ2 |
| 35 | #include "jj2/level/jj2level.h" |
| 36 | #endif |
| 37 | #include "player/player.h"cxx . . $ `<file>/src/player/player.h`/ |
| 38 | #include "util.h"cxx . . $ `<file>/src/util.h`/ |
| 39 | #include "io/log.h"cxx . . $ `<file>/src/io/log.h`/ |
| 40 | |
| 41 | #include <string.h> |
| 42 | #include <cassert> |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Create base game |
| 47 | */ |
| 48 | Gamecxx . . $ Game#::Gamecxx . . $ Game#Game(49f6e7a06ebc5aa8). () { |
| 49 | |
| 50 | levelFilecxx . . $ Game#levelFile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 51 | levelTypecxx . . $ Game#levelType. = LT_JJ1cxx . . $ LT_JJ1.; |
| 52 | |
| 53 | playerscxx . . $ players. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 54 | baseLevelcxx . . $ Game#baseLevel. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 55 | |
| 56 | planetIdcxx . . $ Game#planetId. = -1; |
| 57 | modecxx . . $ Game#mode. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 58 | // Default difficulty setting |
| 59 | difficultycxx . . $ Game#difficulty. = difficultyTypecxx . . $ difficultyType#::Normalcxx . . $ difficultyType#Normal.; |
| 60 | sendTimecxx . . $ Game#sendTime. = checkTimecxx . . $ Game#checkTime. = 0; |
| 61 | checkXcxx . . $ Game#checkX. = checkYcxx . . $ Game#checkY. = 0; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Destroy game |
| 68 | */ |
| 69 | Gamecxx . . $ Game#::~cxx . . $ Game#`~Game`(49f6e7a06ebc5aa8).Gamecxx . . $ Game# () { |
| 70 | |
| 71 | if (levelFilecxx . . $ Game#levelFile.) delete[] levelFilecxx . . $ Game#levelFile.; |
| 72 | |
| 73 | if (playerscxx . . $ players.) delete[] playerscxx . . $ players.; |
| 74 | localPlayercxx . . $ localPlayer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Check the if a file name corresponds to a type |
| 81 | * |
| 82 | * @param fileName The name of the file |
| 83 | * @param type The type identifier in lower case characters |
| 84 | * @param typeLength The length of the type identifier string |
| 85 | * |
| 86 | * @return Whether or not the file name corresponds to the type |
| 87 | */ |
| 88 | bool Gamecxx . . $ Game#::isFileTypecxx . . $ Game#isFileType(f827cee075336303). (const char *fileName, const char *type, int typeLength) { |
| 89 | |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < typeLength; i++) { |
| 93 | |
| 94 | if ((fileName[i] != type[i]) && (fileName[i] != type[i] - 32)) return false; |
| 95 | |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Create a new game mode |
| 104 | * |
| 105 | * @param modeType The mode to create |
| 106 | * |
| 107 | * @return The new game mode (NULL on failure) |
| 108 | */ |
| 109 | GameModecxx . . $ GameMode#* Gamecxx . . $ Game#::createModecxx . . $ Game#createMode(14876a996d2f6cf). (GameModeTypecxx . . $ GameModeType# modeType) { |
| 110 | |
| 111 | switch (modeType) { |
| 112 | |
| 113 | case M_SINGLEcxx . . $ M_SINGLE.: |
| 114 | |
| 115 | return new SingleGameModecxx . . $ SingleGameMode#(); |
| 116 | |
| 117 | case M_COOPcxx . . $ M_COOP.: |
| 118 | |
| 119 | return new CoopGameModecxx . . $ CoopGameMode#(); |
| 120 | |
| 121 | case M_BATTLEcxx . . $ M_BATTLE.: |
| 122 | |
| 123 | return new BattleGameModecxx . . $ BattleGameMode#(); |
| 124 | |
| 125 | case M_TEAMBATTLEcxx . . $ M_TEAMBATTLE.: |
| 126 | |
| 127 | return new TeamBattleGameModecxx . . $ TeamBattleGameMode#(); |
| 128 | |
| 129 | case M_RACEcxx . . $ M_RACE.: |
| 130 | |
| 131 | return new RaceGameModecxx . . $ RaceGameMode#(); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Get the game's mode |
| 142 | * |
| 143 | * @return The game's mode |
| 144 | */ |
| 145 | GameModecxx . . $ GameMode#* Gamecxx . . $ Game#::getModecxx . . $ Game#getMode(b2ac8a9286443acb). () { |
| 146 | |
| 147 | return modecxx . . $ Game#mode.; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Get the game's difficulty |
| 154 | * |
| 155 | * @return The game's difficulty |
| 156 | */ |
| 157 | difficultyTypecxx . . $ difficultyType# Gamecxx . . $ Game#::getDifficultycxx . . $ Game#getDifficulty(601dbc13202031c0). () { |
| 158 | |
| 159 | return difficultycxx . . $ Game#difficulty.; |
| 160 | |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Set the game's difficulty |
| 166 | */ |
| 167 | void Gamecxx . . $ Game#::setDifficultycxx . . $ Game#setDifficulty(7b65cd849eaf05fb). (difficultyTypecxx . . $ difficultyType# diff) { |
| 168 | |
| 169 | difficultycxx . . $ Game#difficulty. = diff; |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |
| 174 | /** |
| 175 | * Play a level. |
| 176 | * |
| 177 | * @return Error code |
| 178 | */ |
| 179 | int Gamecxx . . $ Game#::playLevelcxx . . $ Game#playLevel(cbb24b081db01e54). (char* fileName, bool intro, bool checkpoint) { |
| 180 | |
| 181 | bool multiplayer; |
| 182 | int ret; |
| 183 | |
| 184 | multiplayer = (modecxx . . $ Game#mode.->getModecxx . . $ GameMode#getMode(e679e067a917a25c).() != M_SINGLEcxx . . $ M_SINGLE.); |
| 185 | |
| 186 | if (isFileTypecxx . . $ Game#isFileType(f827cee075336303).(fileName, "macro", 5)) { |
| 187 | |
| 188 | // Load and play the level |
| 189 | |
| 190 | try { |
| 191 | |
| 192 | baseLevelcxx . . $ Game#baseLevel. = levelcxx . . $ level. = new JJ1DemoLevelcxx . . $ JJ1DemoLevel#(this, fileName); |
| 193 | |
| 194 | } catch (int e) { |
| 195 | |
| 196 | return e; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | ret = levelcxx . . $ level.->playcxx . . $ JJ1Level#play(b126dc7c1de90089).(); |
| 201 | |
| 202 | delete levelcxx . . $ level.; |
| 203 | baseLevelcxx . . $ Game#baseLevel. = levelcxx . . $ level. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 204 | |
| 205 | } else if (levelTypecxx . . $ Game#levelType. == LT_JJ1BONUScxx . . $ LT_JJ1BONUS.) { |
| 206 | |
| 207 | JJ1BonusLevelcxx . . $ JJ1BonusLevel# *bonus; |
| 208 | |
| 209 | try { |
| 210 | |
| 211 | baseLevelcxx . . $ Game#baseLevel. = bonus = new JJ1BonusLevelcxx . . $ JJ1BonusLevel#(this, fileName, multiplayer); |
| 212 | |
| 213 | } catch (int e) { |
| 214 | |
| 215 | return e; |
| 216 | |
| 217 | } |
| 218 | |
| 219 | ret = bonus->playcxx . . $ JJ1BonusLevel#play(b126dc7c1de90089).(); |
| 220 | |
| 221 | delete bonus; |
| 222 | baseLevelcxx . . $ Game#baseLevel. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 223 | |
| 224 | #ifdef ENABLE_JJ2 |
| 225 | |
| 226 | } else if (levelType == LT_JJ2) { |
| 227 | |
| 228 | try { |
| 229 | |
| 230 | baseLevel = jj2Level = new JJ2Level(this, fileName, checkpoint, multiplayer); |
| 231 | |
| 232 | } catch (int e) { |
| 233 | |
| 234 | return e; |
| 235 | |
| 236 | } |
| 237 | |
| 238 | ret = jj2Level->play(); |
| 239 | |
| 240 | delete jj2Level; |
| 241 | baseLevel = jj2Level = NULL; |
| 242 | |
| 243 | #endif |
| 244 | |
| 245 | } else { |
| 246 | |
| 247 | try { |
| 248 | |
| 249 | baseLevelcxx . . $ Game#baseLevel. = levelcxx . . $ level. = new JJ1Levelcxx . . $ JJ1Level#(this, fileName, checkpoint, multiplayer); |
| 250 | |
| 251 | } catch (int e) { |
| 252 | |
| 253 | return e; |
| 254 | |
| 255 | } |
| 256 | |
| 257 | if (intro) { |
| 258 | |
| 259 | JJ1Planetcxx . . $ JJ1Planet# *planet; |
| 260 | char *planetFileName = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 261 | |
| 262 | planetFileName = createFileNamecxx . . $ createFileName(2050848944e00d54).("PLANET", levelcxx . . $ level.->getWorldcxx . . $ JJ1Level#getWorld(b126dc7c1de90089).()); |
| 263 | |
| 264 | try { |
| 265 | |
| 266 | planet = new JJ1Planetcxx . . $ JJ1Planet#(planetFileName, planetIdcxx . . $ Game#planetId.); |
| 267 | |
| 268 | } catch (int e) { |
| 269 | |
| 270 | planet = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 271 | |
| 272 | } |
| 273 | |
| 274 | delete[] planetFileName; |
| 275 | |
| 276 | if (planet) { |
| 277 | |
| 278 | if (planet->playcxx . . $ JJ1Planet#play(b126dc7c1de90089).() == E_QUITcxx . . $ `src/OpenJazz.h:67:9`!) { |
| 279 | |
| 280 | delete planet; |
| 281 | delete levelcxx . . $ level.; |
| 282 | |
| 283 | return E_QUITcxx . . $ `src/OpenJazz.h:67:9`!; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | planetIdcxx . . $ Game#planetId. = planet->getIdcxx . . $ JJ1Planet#getId(b126dc7c1de90089).(); |
| 288 | |
| 289 | delete planet; |
| 290 | |
| 291 | } |
| 292 | |
| 293 | } |
| 294 | |
| 295 | ret = levelcxx . . $ level.->playcxx . . $ JJ1Level#play(b126dc7c1de90089).(); |
| 296 | |
| 297 | delete levelcxx . . $ level.; |
| 298 | baseLevelcxx . . $ Game#baseLevel. = levelcxx . . $ level. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | return ret; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Determine the type of the specified level file. |
| 309 | * |
| 310 | * @return Level type |
| 311 | */ |
| 312 | LevelTypecxx . . $ LevelType# Gamecxx . . $ Game#::getLevelTypecxx . . $ Game#getLevelType(b61afb7421387496). (const char* fileName) { |
| 313 | |
| 314 | #ifdef ENABLE_JJ2 |
| 315 | int length = strlen(fileName); |
| 316 | if ((length > 4) && isFileType(fileName + length - 4, ".j2l", 4)) return LT_JJ2; |
| 317 | #endif |
| 318 | if (isFileTypecxx . . $ Game#isFileType(f827cee075336303).(fileName, "bonusmap", 8)) return LT_JJ1BONUScxx . . $ LT_JJ1BONUS.; |
| 319 | return LT_JJ1cxx . . $ LT_JJ1.; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /** |
| 325 | * Play a level. |
| 326 | * |
| 327 | * @return Error code |
| 328 | */ |
| 329 | int Gamecxx . . $ Game#::playLevelcxx . . $ Game#playLevel(1ef8e6ec083aabb9). (char* fileName) { |
| 330 | |
| 331 | levelTypecxx . . $ Game#levelType. = getLevelTypecxx . . $ Game#getLevelType(b61afb7421387496).(fileName); |
| 332 | |
| 333 | return playLevelcxx . . $ Game#playLevel(cbb24b081db01e54).(fileName, false, false); |
| 334 | |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |
| 339 | * Play the game |
| 340 | * |
| 341 | * @return Error code |
| 342 | */ |
| 343 | int Gamecxx . . $ Game#::playcxx . . $ Game#play(b126dc7c1de90089). () { |
| 344 | |
| 345 | bool multiplayer = (modecxx . . $ Game#mode.->getModecxx . . $ GameMode#getMode(e679e067a917a25c).() != M_SINGLEcxx . . $ M_SINGLE.); |
| 346 | bool checkpoint = false; |
| 347 | |
| 348 | // Play the level(s) |
| 349 | while (true) { |
| 350 | |
| 351 | if (!levelFilecxx . . $ Game#levelFile.) return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 352 | |
| 353 | sendTimecxx . . $ Game#sendTime. = checkTimecxx . . $ Game#checkTime. = 0; |
| 354 | |
| 355 | // Load and play the level |
| 356 | |
| 357 | int ret = playLevelcxx . . $ Game#playLevel(cbb24b081db01e54).(levelFilecxx . . $ Game#levelFile., !multiplayer, checkpoint); |
| 358 | |
| 359 | if (ret <= 0) return ret; |
| 360 | |
| 361 | if (levelFilecxx . . $ Game#levelFile. && isFileTypecxx . . $ Game#isFileType(f827cee075336303).(levelFilecxx . . $ Game#levelFile., "bonusmap", 8)) { |
| 362 | |
| 363 | if (ret == WONcxx . . $ `src/level/level.h:46:9`!) { |
| 364 | |
| 365 | char *fileName; |
| 366 | |
| 367 | // Go to next level |
| 368 | fileName = createFileNamecxx . . $ createFileName(2050848944e00d54).("BONUSMAP", (levelFilecxx . . $ Game#levelFile.[10] * 10) + levelFilecxx . . $ Game#levelFile.[11] - 527); |
| 369 | setLevelcxx . . $ Game#setLevel(1ef8e6ec083aabb9).(fileName); |
| 370 | delete[] fileName; |
| 371 | |
| 372 | } |
| 373 | |
| 374 | } else { |
| 375 | |
| 376 | if (ret == WONcxx . . $ `src/level/level.h:46:9`!) { |
| 377 | |
| 378 | // Won the level |
| 379 | |
| 380 | // Do not use old level's checkpoint coordinates |
| 381 | checkpoint = false; |
| 382 | |
| 383 | } else { |
| 384 | |
| 385 | // Lost the level |
| 386 | |
| 387 | if (!localPlayercxx . . $ localPlayer.->getLivescxx . . $ Player#getLives(b126dc7c1de90089).()) return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 388 | |
| 389 | // Use checkpoint coordinates |
| 390 | checkpoint = true; |
| 391 | |
| 392 | } |
| 393 | |
| 394 | } |
| 395 | |
| 396 | } |
| 397 | |
| 398 | return E_NONEcxx . . $ `src/OpenJazz.h:69:9`!; |
| 399 | |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Move the viewport towards the exit sign |
| 405 | * |
| 406 | * @param change Distance to move |
| 407 | */ |
| 408 | void Gamecxx . . $ Game#::viewcxx . . $ Game#view(d4f767463ce0a6b3). (int change) { |
| 409 | |
| 410 | if (TTOFcxx . . $ `src/level/level.h:38:9`!(checkX) > viewXcxx . . $ viewX. + (canvasWcxx . . $ canvasW. << 9) + change) viewXcxx . . $ viewX. += change; |
| 411 | else if (TTOFcxx . . $ `src/level/level.h:38:9`!(checkX) < viewXcxx . . $ viewX. + (canvasWcxx . . $ canvasW. << 9) - change) viewXcxx . . $ viewX. -= change; |
| 412 | |
| 413 | if (TTOFcxx . . $ `src/level/level.h:38:9`!(checkY) > viewYcxx . . $ viewY. + (canvasHcxx . . $ canvasH. << 9) + change) viewYcxx . . $ viewY. += change; |
| 414 | else if (TTOFcxx . . $ `src/level/level.h:38:9`!(checkY) < viewYcxx . . $ viewY. + (canvasHcxx . . $ canvasH. << 9) - change) viewYcxx . . $ viewY. -= change; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Make a player restart the level from the beginning/last checkpoint |
| 421 | * |
| 422 | * @param player Player to reset |
| 423 | */ |
| 424 | void Gamecxx . . $ Game#::resetPlayercxx . . $ Game#resetPlayer(34e2c302b1f0e5dd). (Playercxx . . $ Player# *player) { |
| 425 | |
| 426 | player->resetcxx . . $ Player#reset(3e454b8ccf442868).(checkXcxx . . $ Game#checkX., checkYcxx . . $ Game#checkY.); |
| 427 | |
| 428 | } |
| 429 | |
| 430 | |
| 431 | /** |
| 432 | * Create a level player |
| 433 | * |
| 434 | * @param player Player for which to create the level player |
| 435 | */ |
| 436 | void Gamecxx . . $ Game#::addLevelPlayercxx . . $ Game#addLevelPlayer(34e2c302b1f0e5dd). (Playercxx . . $ Player# *player) { |
| 437 | |
| 438 | int count; |
| 439 | |
| 440 | if (levelcxx . . $ level.) { |
| 441 | |
| 442 | Animcxx . . $ Anim#* pAnims[JJ1PANIMScxx . . $ `src/jj1/level/jj1level.h:99:9`!]; |
| 443 | |
| 444 | for (count = 0; count < JJ1PANIMScxx . . $ `src/jj1/level/jj1level.h:99:9`!; count++) pAnims[count] = levelcxx . . $ level.->getPlayerAnimcxx . . $ JJ1Level#getPlayerAnim(e50c847d642a1093).(count); |
| 445 | |
| 446 | player->createLevelPlayercxx . . $ Player#createLevelPlayer(27ab3ccb3efab72).(levelTypecxx . . $ Game#levelType., pAnims, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, checkXcxx . . $ Game#checkX., checkYcxx . . $ Game#checkY.); |
| 447 | |
| 448 | #ifdef ENABLE_JJ2 |
| 449 | |
| 450 | } else if (jj2Level) { |
| 451 | |
| 452 | Anim* pAnims[JJ2PANIMS]; |
| 453 | Anim* pFlippedAnims[JJ2PANIMS]; |
| 454 | |
| 455 | for (count = 0; count < JJ2PANIMS; count++) { |
| 456 | |
| 457 | pAnims[count] = jj2Level->getPlayerAnim(0, count, false); |
| 458 | pFlippedAnims[count] = jj2Level->getPlayerAnim(0, count, true); |
| 459 | |
| 460 | } |
| 461 | |
| 462 | player->createLevelPlayer(levelType, pAnims, pFlippedAnims, checkX, checkY); |
| 463 | |
| 464 | #endif |
| 465 | |
| 466 | } else { |
| 467 | |
| 468 | LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Cannot create level player!"); |
| 469 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(false); |
| 470 | |
| 471 | } |
| 472 | |
| 473 | } |