| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file level.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 3rd February 2009: Renamed level.c to level.cpp |
| 11 | * - 19th July 2009: Created levelframe.cpp from parts of level.cpp |
| 12 | * - 19th July 2009: Added parts of levelload.cpp to level.cpp |
| 13 | * - 30th March 2010: Created baselevel.cpp from parts of level.cpp and |
| 14 | * levelframe.cpp |
| 15 | * - 1st August 2012: Renamed baselevel.cpp to level.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2017 AJ Thomson |
| 19 | * Copyright (c) 2015-2026 Carsten Teibes |
| 20 | * |
| 21 | * OpenJazz is distributed under the terms of |
| 22 | * the GNU General Public License, version 2.0 |
| 23 | * |
| 24 | * @par Description: |
| 25 | * Deals with functionality common to ordinary levels and bonus levels. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | #include "level.h" |
| 31 | |
| 32 | #include "game/game.h" |
| 33 | #include "io/controls.h" |
| 34 | #include "io/gfx/font.h" |
| 35 | #include "io/gfx/sprite.h" |
| 36 | #include "io/gfx/video.h" |
| 37 | #include "io/sound.h" |
| 38 | #include "player/player.h" |
| 39 | #include "jj1/scene/jj1scene.h" |
| 40 | #include "loop.h" |
| 41 | #include "setup.h" |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Create a new base level |
| 46 | */ |
| 47 | Level::Level (Game* owner) { |
| 48 | |
| 49 | game = owner; |
| 50 | |
| 51 | menuOptions[0] = "continue game"; |
| 52 | menuOptions[1] = "save game"; |
| 53 | menuOptions[2] = "load game"; |
| 54 | menuOptions[3] = "setup options"; |
| 55 | menuOptions[4] = "quit game"; |
| 56 | |
| 57 | // Arbitrary initial value |
| 58 | smoothfps = 50.0f; |
| 59 | paletteEffects = NULL; |
| 60 | paused = false; |
| 61 | |
| 62 | // Set the level stage |
| 63 | stage = LS_NORMAL; |
| 64 | sprites = tickOffset = steps = prevTicks = ticks = endTime = items = stats = 0; |
| 65 | multiplayer = false; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Destroy base level |
| 71 | */ |
| 72 | Level::~Level () { |
| 73 | |
| 74 | stopMusic(); |
| 75 | |
| 76 | if (paletteEffects) delete paletteEffects; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Set the players' initial values. |
| 83 | * |
| 84 | * @param levelType The type of level for which to create a level player |
| 85 | * @param anims New level player animations |
| 86 | * @param flippedAnims New level player flipped animations |
| 87 | * @param checkpoint Whether or not a checkpoint is in use |
| 88 | * @param x The level players' new grid x-coordinate |
| 89 | * @param y The level players' new grid y-coordinate |
| 90 | */ |
| 91 | void Level::createLevelPlayers (LevelType levelType, Anim** anims, |
| 92 | Anim** flippedAnims, bool checkpoint, unsigned char x, unsigned char y) { |
| 93 | |
| 94 | int count; |
| 95 | |
| 96 | if (!checkpoint) game->setCheckpoint(x, y); |
| 97 | |
| 98 | for (count = 0; count < nPlayers; count++) { |
| 99 | |
| 100 | players[count].createLevelPlayer(levelType, anims, flippedAnims, x, y); |
| 101 | game->resetPlayer(players + count); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Play a cutscene. |
| 110 | * |
| 111 | * @param file File name of the cutscene to be played |
| 112 | * |
| 113 | * @return Error code |
| 114 | */ |
| 115 | int Level::playScene (const char* file) { |
| 116 | |
| 117 | JJ1Scene* scene; |
| 118 | int ret; |
| 119 | |
| 120 | delete paletteEffects; |
| 121 | paletteEffects = NULL; |
| 122 | |
| 123 | try { |
| 124 | |
| 125 | scene = new JJ1Scene(file); |
| 126 | |
| 127 | } catch (int e) { |
| 128 | |
| 129 | return e; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | ret = scene->play(); |
| 134 | |
| 135 | delete scene; |
| 136 | |
| 137 | return ret; |
| 138 | |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Perform timing calculations. |
| 144 | */ |
| 145 | void Level::timeCalcs () { |
| 146 | |
| 147 | // Calculate smoothed fps |
| 148 | smoothfps = smoothfps + 1.0f - |
| 149 | (smoothfps * ((float)(ticks - prevTicks)) / 1000.0f); |
| 150 | /* This equation is a simplified version of |
| 151 | (fps * c) + (smoothfps * (1 - c)) |
| 152 | where c = (1 / fps) |
| 153 | and fps = 1000 / (ticks - prevTicks) |
| 154 | In other words, the response of smoothFPS to changes in FPS decreases as the |
| 155 | framerate increases |
| 156 | The following version is for c = (1 / smoothfps) |
| 157 | */ |
| 158 | // smoothfps = (fps / smoothfps) + smoothfps - 1; |
| 159 | |
| 160 | // Ignore outlandish values |
| 161 | if (smoothfps > 9999.0f) smoothfps = 9999.0f; |
| 162 | if (smoothfps < 1.0f) smoothfps = 1.0f; |
| 163 | |
| 164 | |
| 165 | // Track number of ticks of gameplay since the level started |
| 166 | |
| 167 | if (paused) { |
| 168 | |
| 169 | tickOffset = globalTicks - ticks; |
| 170 | |
| 171 | } else if (globalTicks - tickOffset > ticks + 100) { |
| 172 | |
| 173 | prevTicks = ticks; |
| 174 | ticks += 100; |
| 175 | |
| 176 | tickOffset = globalTicks - ticks; |
| 177 | |
| 178 | } else { |
| 179 | |
| 180 | prevTicks = ticks; |
| 181 | ticks = globalTicks - tickOffset; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Calculate the amount of time since the last completed step. |
| 190 | * |
| 191 | * @return Time since last step |
| 192 | */ |
| 193 | int Level::getTimeChange () { |
| 194 | |
| 195 | return paused? 0: ticks - ((steps * (setup.slowMotion? 100: 50)) / 3); |
| 196 | |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Display menu (if visible) and statistics. |
| 202 | * |
| 203 | * @param bg Palette index of the box(es) |
| 204 | * @param menu Whether or not the level menu should be displayed |
| 205 | * @param option Selected menu uption |
| 206 | * @param textPalIndex The first palette index for unseleceted text |
| 207 | * @param selectedTextPalIndex The first palette index for selected text |
| 208 | * @param textPalSpan The number of palette indices for text |
| 209 | */ |
| 210 | void Level::drawOverlay (unsigned char bg, bool menu, int option, |
| 211 | unsigned char textPalIndex, unsigned char selectedTextPalIndex, |
| 212 | int textPalSpan) { |
| 213 | |
| 214 | int count, width; |
| 215 | |
| 216 | // Draw graphics statistics |
| 217 | |
| 218 | if (stats & S_SCREEN) { |
| 219 | if (video.getScaleFactor() > MIN_SCALE) |
| 220 | video.drawRect(canvasW - 84, 11, 80, 37, bg); |
| 221 | else |
| 222 | video.drawRect(canvasW - 84, 11, 80, 25, bg); |
| 223 | |
| 224 | panelBigFont->showNumber(video.getWidth(), canvasW - 52, 14); |
| 225 | panelBigFont->showString("x", canvasW - 48, 14); |
| 226 | panelBigFont->showNumber(video.getHeight(), canvasW - 12, 14); |
| 227 | panelBigFont->showString("fps", canvasW - 76, 26); |
| 228 | panelBigFont->showNumber((int)smoothfps, canvasW - 12, 26); |
| 229 | |
| 230 | if (video.getScaleFactor() > MIN_SCALE) { |
| 231 | panelBigFont->showNumber(canvasW, canvasW - 52, 38); |
| 232 | panelBigFont->showString("x", canvasW - 48, 39); |
| 233 | panelBigFont->showNumber(canvasH, canvasW - 12, 38); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Draw player list |
| 238 | |
| 239 | if (stats & S_PLAYERS) { |
| 240 | |
| 241 | width = 39; |
| 242 | |
| 243 | for (count = 0; count < nPlayers; count++) |
| 244 | if (panelBigFont->getStringWidth(players[count].getName()) > width) |
| 245 | width = panelBigFont->getStringWidth(players[count].getName()); |
| 246 | |
| 247 | video.drawRect((canvasW >> 1) - 48, 11, width + 57, (nPlayers * 12) + 1, bg); |
| 248 | |
| 249 | for (count = 0; count < nPlayers; count++) { |
| 250 | |
| 251 | panelBigFont->showNumber(count + 1, |
| 252 | (canvasW >> 1) - 24, 14 + (count * 12)); |
| 253 | panelBigFont->showString(players[count].getName(), |
| 254 | (canvasW >> 1) - 16, 14 + (count * 12)); |
| 255 | panelBigFont->showNumber(players[count].teamScore, |
| 256 | (canvasW >> 1) + width + 1, 14 + (count * 12)); |
| 257 | |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | if (menu) { |
| 264 | |
| 265 | // Draw the menu |
| 266 | |
| 267 | video.drawRect((canvasW >> 1) - 72, (canvasH >> 1) - 54, 140, 92, bg); |
| 268 | video.drawRect((canvasW >> 1) - 72, (canvasH >> 1) - 54, 140, 92, textPalIndex, false); |
| 269 | |
| 270 | |
| 271 | for (count = 0; count < 5; count++) { |
| 272 | |
| 273 | // Gray out Save (unimplemented) FIXME: needs to be disabled generally in multiplayer |
| 274 | |
| 275 | if (count == 1) { |
| 276 | |
| 277 | video.drawRect((canvasW >> 1) - 68, (canvasH >> 1) + (count << 4) - 48, 132, 15, textPalIndex); |
| 278 | |
| 279 | } |
| 280 | |
| 281 | if (count == option) fontmn2->mapPalette(240, 8, selectedTextPalIndex, textPalSpan); |
| 282 | else fontmn2->mapPalette(240, 8, textPalIndex, textPalSpan); |
| 283 | |
| 284 | fontmn2->showString(menuOptions[count], (canvasW >> 1) - 64, (canvasH >> 1) + (count << 4) - 46); |
| 285 | |
| 286 | } |
| 287 | |
| 288 | fontmn2->restorePalette(); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /** |
| 296 | * Process in-game menu selection. |
| 297 | * |
| 298 | * @param menu Whether or not the level menu should be displayed |
| 299 | * @param option Chosen menu option |
| 300 | * |
| 301 | * @return Error code |
| 302 | */ |
| 303 | int Level::select (bool& menu, int option) { |
| 304 | |
| 305 | switch (option) { |
| 306 | |
| 307 | case 0: // Continue |
| 308 | |
| 309 | menu = false; |
| 310 | |
| 311 | break; |
| 312 | |
| 313 | case 1: // Save |
| 314 | |
| 315 | if (!multiplayer) { |
| 316 | |
| 317 | int ret = fileMenu.main(true, false); |
| 318 | if (ret == E_QUIT) return E_QUIT; |
| 319 | if (ret >= 0) { |
| 320 | // TODO |
| 321 | } |
| 322 | |
| 323 | // Restore level palette |
| 324 | video.setPalette(palette); |
| 325 | |
| 326 | } |
| 327 | |
| 328 | break; |
| 329 | |
| 330 | case 2: // Load |
| 331 | |
| 332 | if (!multiplayer) { |
| 333 | |
| 334 | int ret = fileMenu.main(false, false); |
| 335 | if (ret == E_QUIT) return E_QUIT; |
| 336 | if (ret >= 0) { |
| 337 | return E_LOAD0 + ret; |
| 338 | } |
| 339 | |
| 340 | // Restore level palette |
| 341 | video.setPalette(palette); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | break; |
| 346 | |
| 347 | case 3: // Setup |
| 348 | |
| 349 | if (!multiplayer) { |
| 350 | |
| 351 | bool wasSlow = setup.slowMotion; |
| 352 | |
| 353 | if (setupMenu.setupMain() == E_QUIT) return E_QUIT; |
| 354 | |
| 355 | if (wasSlow && !setup.slowMotion) steps <<= 1; |
| 356 | else if (!wasSlow && setup.slowMotion) steps >>= 1; |
| 357 | |
| 358 | // Restore level palette |
| 359 | video.setPalette(palette); |
| 360 | |
| 361 | } |
| 362 | |
| 363 | break; |
| 364 | |
| 365 | case 4: // Quit game |
| 366 | |
| 367 | return E_RETURN; |
| 368 | |
| 369 | } |
| 370 | |
| 371 | return E_NONE; |
| 372 | |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Process iteration. |
| 377 | * |
| 378 | * @param menu Whether or not the level menu should be displayed |
| 379 | * @param option Selected menu uption |
| 380 | * @param message Whether or not the "paused" message is being displayed |
| 381 | * |
| 382 | * @return Error code |
| 383 | */ |
| 384 | int Level::loop (bool& menu, int& option, bool& message) { |
| 385 | |
| 386 | int ret, x, y; |
| 387 | |
| 388 | // Networking |
| 389 | if (multiplayer) { |
| 390 | |
| 391 | ret = game->step(ticks); |
| 392 | |
| 393 | if (ret < 0) return ret; |
| 394 | |
| 395 | } |
| 396 | |
| 397 | |
| 398 | // Main loop |
| 399 | if (::loop(NORMAL_LOOP, paletteEffects, paused) == E_QUIT) return E_QUIT; |
| 400 | |
| 401 | |
| 402 | if (controls.release(C_ESCAPE)) { |
| 403 | |
| 404 | menu = !menu; |
| 405 | option = 0; |
| 406 | |
| 407 | } |
| 408 | |
| 409 | if (controls.release(C_PAUSE)) message = !message; |
| 410 | |
| 411 | if (controls.release(C_STATS)) { |
| 412 | |
| 413 | if (!multiplayer) stats ^= S_SCREEN; |
| 414 | else stats = (stats + 1) & 3; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | if (menu) { |
| 419 | |
| 420 | // Deal with menu controls |
| 421 | |
| 422 | if (controls.release(C_UP)) option = (option + 4) % 5; |
| 423 | |
| 424 | if (controls.release(C_DOWN)) option = (option + 1) % 5; |
| 425 | |
| 426 | if (controls.release(C_ENTER)) { |
| 427 | |
| 428 | ret = select(menu, option); |
| 429 | |
| 430 | if (ret < 0) return ret; |
| 431 | |
| 432 | } |
| 433 | |
| 434 | if (controls.getCursor(x, y)) { |
| 435 | |
| 436 | x -= (canvasW >> 1) - 64; |
| 437 | y -= (canvasH >> 1) - 46; |
| 438 | |
| 439 | if ((x >= 0) && (x < 120) && (y >= 0) && (y < 96)) { |
| 440 | |
| 441 | option = y >> 4; |
| 442 | |
| 443 | if (controls.wasCursorReleased()) { |
| 444 | |
| 445 | ret = select(menu, option); |
| 446 | |
| 447 | if (ret < 0) return ret; |
| 448 | |
| 449 | } |
| 450 | |
| 451 | } else if (controls.wasCursorReleased()) menu = false; |
| 452 | |
| 453 | } |
| 454 | |
| 455 | } else { |
| 456 | |
| 457 | if (controls.wasCursorReleased()) menu = true; |
| 458 | |
| 459 | } |
| 460 | |
| 461 | if (!multiplayer) paused = message || menu; |
| 462 | |
| 463 | timeCalcs(); |
| 464 | |
| 465 | return E_NONE; |
| 466 | |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /** |
| 471 | * Add extra time. |
| 472 | * |
| 473 | * @param seconds Number of seconds to add |
| 474 | */ |
| 475 | void Level::addTimer (int seconds) { |
| 476 | |
| 477 | if (stage != LS_NORMAL) return; |
| 478 | |
| 479 | endTime += seconds * 1000; |
| 480 | |
| 481 | if (endTime >= ticks + (10 * 60 * 1000)) |
| 482 | endTime = ticks + (10 * 60 * 1000) - 1; |
| 483 | |
| 484 | if (multiplayer) { |
| 485 | |
| 486 | unsigned char buffer[MTL_L_PROP]; |
| 487 | buffer[0] = MTL_L_PROP; |
| 488 | buffer[1] = MT_L_PROP; |
| 489 | buffer[2] = 2; // add timer |
| 490 | buffer[3] = seconds; |
| 491 | buffer[4] = 0; // not used |
| 492 | |
| 493 | game->send(buffer); |
| 494 | |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
| 499 | |
| 500 | /** |
| 501 | * Set the level stage. |
| 502 | * |
| 503 | * @param newStage New level stage |
| 504 | */ |
| 505 | void Level::setStage (LevelStage newStage) { |
| 506 | |
| 507 | if (stage == newStage) return; |
| 508 | |
| 509 | stage = newStage; |
| 510 | |
| 511 | if (multiplayer) { |
| 512 | |
| 513 | unsigned char buffer[MTL_L_STAGE]; |
| 514 | buffer[0] = MTL_L_STAGE; |
| 515 | buffer[1] = MT_L_STAGE; |
| 516 | buffer[2] = stage; |
| 517 | game->send(buffer); |
| 518 | |
| 519 | } |
| 520 | |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * Determine the current level stage. |
| 526 | * |
| 527 | * @return The current level stage. |
| 528 | */ |
| 529 | LevelStage Level::getStage () { |
| 530 | |
| 531 | return stage; |
| 532 | |
| 533 | } |