| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1level.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 1st January 2006: Created events.c from parts of level.c |
| 11 | * - 22nd July 2008: Created levelload.c from parts of level.c |
| 12 | * - 3rd February 2009: Renamed level.c to level.cpp |
| 13 | * - 5th February 2009: Added parts of events.cpp and level.cpp to player.cpp |
| 14 | * - 9th March 2009: Created game.cpp from parts of menu.cpp and level.cpp |
| 15 | * - 18th July 2009: Created demolevel.cpp from parts of level.cpp and |
| 16 | * levelload.cpp |
| 17 | * - 19th July 2009: Created levelframe.cpp from parts of level.cpp |
| 18 | * - 19th July 2009: Added parts of levelload.cpp to level.cpp |
| 19 | * - 30th March 2010: Created baselevel.cpp from parts of level.cpp and |
| 20 | * levelframe.cpp |
| 21 | * - 29th June 2010: Created jj2level.cpp from parts of level.cpp |
| 22 | * - 1st August 2012: Renamed level.cpp to jj1level.cpp |
| 23 | * |
| 24 | * @par Licence: |
| 25 | * Copyright (c) 2005-2017 AJ Thomson |
| 26 | * Copyright (c) 2015-2024 Carsten Teibes |
| 27 | * |
| 28 | * OpenJazz is distributed under the terms of |
| 29 | * the GNU General Public License, version 2.0 |
| 30 | * |
| 31 | * @par Description: |
| 32 | * Deals with the creating, playing and freeing of levels. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include "jj1bullet.h" |
| 38 | #include "event/jj1event.h" |
| 39 | #include "jj1level.h" |
| 40 | #include "jj1levelplayer.h" |
| 41 | #include "jj1/jj1episodeutils.h" |
| 42 | |
| 43 | #include "game/game.h" |
| 44 | #include "game/gamemode.h" |
| 45 | #include "io/controls.h" |
| 46 | #include "io/file.h" |
| 47 | #include "io/gfx/font.h" |
| 48 | #include "io/gfx/paletteeffects.h" |
| 49 | #include "io/gfx/sprite.h" |
| 50 | #include "io/gfx/video.h" |
| 51 | #include "io/sound.h" |
| 52 | #include "io/log.h" |
| 53 | #include "util.h" |
| 54 | |
| 55 | #include <string.h> |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Base constructor for JJ1DemoLevel sub-class. |
| 60 | * |
| 61 | * @param owner The current game |
| 62 | */ |
| 63 | JJ1Level::JJ1Level (Game* owner) : Level(owner) { |
| 64 | tileSet = panel = nullptr; |
| 65 | for (int i = 0; i < 6; i++) |
| 66 | panelAmmo[i] = nullptr; |
| 67 | for (int i = 0; i < 2; i++) |
| 68 | panelBG[i] = nullptr; |
| 69 | events = nullptr; |
| 70 | bullets = nullptr; |
| 71 | sceneFile = nullptr; |
| 72 | spriteSet = nullptr; |
| 73 | sky = false; |
| 74 | skyOrb = 0; |
| 75 | levelNum = worldNum = nextLevelNum = nextWorldNum = 0; |
| 76 | enemies = 0; |
| 77 | waterLevel = waterLevelTarget = waterLevelSpeed = 0; |
| 78 | energyBar = ammoType = ammoOffset = 0; |
| 79 | font = nullptr; |
| 80 | musicFile = nullptr; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Create a JJ1 level. |
| 86 | * |
| 87 | * @param owner The current game |
| 88 | * @param fileName Name of the file containing the level data. |
| 89 | * @param checkpoint Whether or not the player(s) will start at a checkpoint |
| 90 | * @param multi Whether or not the level will be multi-player |
| 91 | */ |
| 92 | JJ1Level::JJ1Level (Game* owner, char* fileName, bool checkpoint, bool multi) : |
| 93 | Level (owner) { |
| 94 | |
| 95 | // Load level data |
| 96 | int ret = load(fileName, checkpoint); |
| 97 | if (ret < 0) throw ret; |
| 98 | |
| 99 | multiplayer = multi; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Delete HUD graphical data. |
| 106 | */ |
| 107 | void JJ1Level::deletePanel () { |
| 108 | |
| 109 | video.destroySurface(panel); |
| 110 | for (int i = 0; i < 6; i++) |
| 111 | video.destroySurface(panelAmmo[i]); |
| 112 | for (int i = 0; i < 2; i++) |
| 113 | video.destroySurface(panelBG[i]); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Delete the JJ1 level. |
| 120 | */ |
| 121 | JJ1Level::~JJ1Level () { |
| 122 | |
| 123 | // Free events |
| 124 | if (events) delete events; |
| 125 | |
| 126 | // Free bullets |
| 127 | if (bullets) delete bullets; |
| 128 | |
| 129 | for (int i = 0; i < PATHS; i++) { |
| 130 | |
| 131 | delete[] path[i].x; |
| 132 | delete[] path[i].y; |
| 133 | |
| 134 | } |
| 135 | |
| 136 | delete[] sceneFile; |
| 137 | delete[] musicFile; |
| 138 | |
| 139 | delete[] spriteSet; |
| 140 | |
| 141 | video.destroySurface(tileSet); |
| 142 | |
| 143 | deletePanel(); |
| 144 | |
| 145 | delete font; |
| 146 | |
| 147 | resampleSounds(); |
| 148 | |
| 149 | video.setTitle(nullptr); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * Determine whether or not the given point is solid when travelling upwards. |
| 156 | * |
| 157 | * @param x X-coordinate |
| 158 | * @param y Y-coordinate |
| 159 | * |
| 160 | * @return Solidity |
| 161 | */ |
| 162 | bool JJ1Level::checkMaskUp (fixed x, fixed y) { |
| 163 | |
| 164 | GridElement *ge; |
| 165 | |
| 166 | // Anything off the edge of the map is solid |
| 167 | if ((x < 0) || (y < 0) || (x >= TTOF(LW)) || (y >= TTOF(LH))) |
| 168 | return true; |
| 169 | |
| 170 | ge = grid[FTOT(y)] + FTOT(x); |
| 171 | |
| 172 | // JJ1Event 122 is one-way |
| 173 | if (ge->event == 122) return false; |
| 174 | |
| 175 | // Check the mask in the tile in question |
| 176 | return mask[ge->tile][((y >> 9) & 56) + ((x >> 12) & 7)]; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Determine whether or not the given point is solid when travelling downwards. |
| 183 | * |
| 184 | * @param x X-coordinate |
| 185 | * @param y Y-coordinate |
| 186 | * |
| 187 | * @return Solidity |
| 188 | */ |
| 189 | bool JJ1Level::checkMaskDown (fixed x, fixed y) { |
| 190 | |
| 191 | // Anything off the edge of the map is solid |
| 192 | if ((x < 0) || (y < 0) || (x >= TTOF(LW)) || (y >= TTOF(LH))) |
| 193 | return true; |
| 194 | |
| 195 | // Check the mask in the tile in question |
| 196 | return mask[grid[FTOT(y)][FTOT(x)].tile][((y >> 9) & 56) + ((x >> 12) & 7)]; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Determine whether or not the given point should cause damage to the player. |
| 203 | * |
| 204 | * @param x X-coordinate |
| 205 | * @param y Y-coordinate |
| 206 | * |
| 207 | * @return Painful solidity |
| 208 | */ |
| 209 | bool JJ1Level::checkSpikes (fixed x, fixed y) { |
| 210 | |
| 211 | GridElement *ge; |
| 212 | |
| 213 | // Anything off the edge of the map is not spikes |
| 214 | // Ignore the bottom, as it is deadly anyway |
| 215 | if ((x < 0) || (y < 0) || (x > TTOF(LW))) return false; |
| 216 | |
| 217 | ge = grid[FTOT(y)] + FTOT(x); |
| 218 | |
| 219 | // JJ1Event 126 is spikes |
| 220 | if (ge->event != 126) return false; |
| 221 | |
| 222 | // Check the mask in the tile in question |
| 223 | return mask[ge->tile][((y >> 9) & 56) + ((x >> 12) & 7)]; |
| 224 | |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * Determine the level's world number. |
| 230 | * |
| 231 | * @return World number |
| 232 | */ |
| 233 | int JJ1Level::getWorld() { |
| 234 | |
| 235 | return worldNum; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * Set which level will come next. |
| 242 | * |
| 243 | * @param nextLevel Next level's number |
| 244 | * @param nextWorld Next level's world number |
| 245 | */ |
| 246 | void JJ1Level::setNext (int nextLevel, int nextWorld) { |
| 247 | |
| 248 | nextLevelNum = nextLevel; |
| 249 | nextWorldNum = nextWorld; |
| 250 | |
| 251 | if(nextLevelNum == 99 || nextWorldNum == 99) { |
| 252 | LOG_MAX("No next level available, episode/game will end"); |
| 253 | } else { |
| 254 | LOG_MAX("Next level is LEVEL%d.%03d", nextLevelNum, nextWorldNum); |
| 255 | } |
| 256 | |
| 257 | if (multiplayer) { |
| 258 | |
| 259 | unsigned char buffer[MTL_L_PROP]; |
| 260 | buffer[0] = MTL_L_PROP; |
| 261 | buffer[1] = MT_L_PROP; |
| 262 | buffer[2] = 0; // set next level |
| 263 | buffer[3] = nextLevel; |
| 264 | buffer[4] = nextWorld; |
| 265 | |
| 266 | game->send(buffer); |
| 267 | |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Set the tile at the given location. |
| 275 | * |
| 276 | * @param gridX X-coordinate of the tile |
| 277 | * @param gridY Y-coordinate of the tile |
| 278 | * @param tile The new tile |
| 279 | */ |
| 280 | void JJ1Level::setTile (unsigned char gridX, unsigned char gridY, unsigned char tile) { |
| 281 | |
| 282 | grid[gridY][gridX].tile = tile; |
| 283 | |
| 284 | if (multiplayer) { |
| 285 | |
| 286 | unsigned char buffer[MTL_L_GRID]; |
| 287 | buffer[0] = MTL_L_GRID; |
| 288 | buffer[1] = MT_L_GRID; |
| 289 | buffer[2] = gridX; |
| 290 | buffer[3] = gridY; |
| 291 | buffer[4] = 0; // tile variable |
| 292 | buffer[5] = tile; |
| 293 | |
| 294 | game->send(buffer); |
| 295 | |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | |
| 301 | difficultyType JJ1Level::getDifficulty () { |
| 302 | return game->getDifficulty(); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Get the active events. |
| 308 | * |
| 309 | * @return The first active event |
| 310 | */ |
| 311 | JJ1Event* JJ1Level::getEvents () { |
| 312 | |
| 313 | return events; |
| 314 | |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /** |
| 319 | * Get the event data for the event from the given tile. |
| 320 | * |
| 321 | * @param gridX X-coordinate of the tile |
| 322 | * @param gridY Y-coordinate of the tile |
| 323 | * |
| 324 | * @return JJ1Event data |
| 325 | */ |
| 326 | JJ1EventType* JJ1Level::getEvent (unsigned char gridX, unsigned char gridY) { |
| 327 | |
| 328 | int event = grid[gridY][gridX].event; |
| 329 | |
| 330 | if (event) return eventSet + event; |
| 331 | |
| 332 | return nullptr; |
| 333 | |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * Get the hits incurred by the event from the given tile. |
| 339 | * |
| 340 | * @param gridX X-coordinate of the tile |
| 341 | * @param gridY Y-coordinate of the tile |
| 342 | * |
| 343 | * @return Number of hits |
| 344 | */ |
| 345 | unsigned char JJ1Level::getEventHits (unsigned char gridX, unsigned char gridY) { |
| 346 | |
| 347 | return grid[gridY][gridX].hits; |
| 348 | |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /** |
| 353 | * Get the set time for the event from the given tile. |
| 354 | * |
| 355 | * @param gridX X-coordinate of the tile |
| 356 | * @param gridY Y-coordinate of the tile |
| 357 | * |
| 358 | * @return Time |
| 359 | */ |
| 360 | unsigned int JJ1Level::getEventTime (unsigned char gridX, unsigned char gridY) { |
| 361 | |
| 362 | return grid[gridY][gridX].time; |
| 363 | |
| 364 | } |
| 365 | |
| 366 | |
| 367 | /** |
| 368 | * Remove the event from the given tile. |
| 369 | * |
| 370 | * @param gridX X-coordinate of the tile |
| 371 | * @param gridY Y-coordinate of the tile |
| 372 | */ |
| 373 | void JJ1Level::clearEvent (unsigned char gridX, unsigned char gridY) { |
| 374 | |
| 375 | // Ignore if the event has been un-destroyed |
| 376 | if (!grid[gridY][gridX].hits && |
| 377 | eventSet[grid[gridY][gridX].event].strength) return; |
| 378 | |
| 379 | grid[gridY][gridX].event = 0; |
| 380 | |
| 381 | if (multiplayer) { |
| 382 | |
| 383 | unsigned char buffer[MTL_L_GRID]; |
| 384 | buffer[0] = MTL_L_GRID; |
| 385 | buffer[1] = MT_L_GRID; |
| 386 | buffer[2] = gridX; |
| 387 | buffer[3] = gridY; |
| 388 | buffer[4] = 2; // event variable |
| 389 | buffer[5] = 0; |
| 390 | |
| 391 | game->send(buffer); |
| 392 | |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Register hit(s) on the event for the given tile. |
| 400 | * |
| 401 | * @param gridX X-coordinate of the tile |
| 402 | * @param gridY Y-coordinate of the tile |
| 403 | * @param hits The number of hits to attempt to inflict on the event |
| 404 | * @param source The player that inflicted the hit(s) |
| 405 | * @param time Time |
| 406 | * |
| 407 | * @return The remaining number of hits until the event is destroyed |
| 408 | */ |
| 409 | int JJ1Level::hitEvent (unsigned char gridX, unsigned char gridY, int hits, JJ1LevelPlayer* source, unsigned int time) { |
| 410 | |
| 411 | GridElement *ge = grid[gridY] + gridX; |
| 412 | int hitsToKill = eventSet[ge->event].strength; |
| 413 | |
| 414 | // If the event cannot be hit, return negative |
| 415 | if (!hitsToKill || (ge->hits == 255)) return -1; |
| 416 | |
| 417 | // In turbo difficulty, enemy events generally take one more hit |
| 418 | if (getDifficulty() == difficultyType::Turbo && eventSet[ge->event].modifier == 0) |
| 419 | hitsToKill++; |
| 420 | |
| 421 | // If the event has already been destroyed, do nothing |
| 422 | if (ge->hits >= hitsToKill) return 0; |
| 423 | |
| 424 | // Check if the event has been killed |
| 425 | if (ge->hits + hits >= hitsToKill) { |
| 426 | |
| 427 | // Notify the player that shot the bullet |
| 428 | // If this returns false, ignore the hit |
| 429 | if (!source->takeEvent(eventSet + ge->event, gridX, gridY, ticks)) { |
| 430 | |
| 431 | return hitsToKill - ge->hits; |
| 432 | |
| 433 | } |
| 434 | |
| 435 | ge->hits = (hits == 255)? 255: hitsToKill; |
| 436 | ge->time = time; |
| 437 | |
| 438 | } else { |
| 439 | |
| 440 | ge->hits += hits; |
| 441 | |
| 442 | } |
| 443 | |
| 444 | if (multiplayer) { |
| 445 | |
| 446 | unsigned char buffer[MTL_L_GRID]; |
| 447 | buffer[0] = MTL_L_GRID; |
| 448 | buffer[1] = MT_L_GRID; |
| 449 | buffer[2] = gridX; |
| 450 | buffer[3] = gridY; |
| 451 | buffer[4] = 3; // hits variable |
| 452 | buffer[5] = ge->hits; |
| 453 | |
| 454 | game->send(buffer); |
| 455 | |
| 456 | } |
| 457 | |
| 458 | return hitsToKill - ge->hits; |
| 459 | |
| 460 | } |
| 461 | |
| 462 | |
| 463 | /** |
| 464 | * Set the time of the event from the given tile. |
| 465 | * |
| 466 | * @param gridX X-coordinate of the tile |
| 467 | * @param gridY Y-coordinate of the tile |
| 468 | * @param time Time |
| 469 | */ |
| 470 | void JJ1Level::setEventTime (unsigned char gridX, unsigned char gridY, unsigned int time) { |
| 471 | |
| 472 | grid[gridY][gridX].time = time; |
| 473 | |
| 474 | } |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * Get a sprite. |
| 479 | * |
| 480 | * @param sprite Sprite number |
| 481 | * |
| 482 | * @return Sprite |
| 483 | */ |
| 484 | Sprite* JJ1Level::getSprite (unsigned char sprite) { |
| 485 | |
| 486 | return spriteSet + sprite; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | |
| 491 | /** |
| 492 | * Get an animation. |
| 493 | * |
| 494 | * @param anim Animation number |
| 495 | * |
| 496 | * @return Animation |
| 497 | */ |
| 498 | Anim* JJ1Level::getAnim (unsigned char anim) { |
| 499 | |
| 500 | return animSet + anim; |
| 501 | |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /** |
| 506 | * Get a "miscellaneous" animation. |
| 507 | * |
| 508 | * @param anim Animation number |
| 509 | * |
| 510 | * @return Animation |
| 511 | */ |
| 512 | Anim* JJ1Level::getMiscAnim (unsigned char anim) { |
| 513 | |
| 514 | return animSet + miscAnims[anim]; |
| 515 | |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /** |
| 520 | * Get a player animation. |
| 521 | * |
| 522 | * @param anim Animation number |
| 523 | * |
| 524 | * @return Animation |
| 525 | */ |
| 526 | Anim* JJ1Level::getPlayerAnim (unsigned char anim) { |
| 527 | |
| 528 | return animSet + playerAnims[anim]; |
| 529 | |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Get a level animation. |
| 534 | * |
| 535 | * @param anim Animation number |
| 536 | * |
| 537 | * @return Animation |
| 538 | */ |
| 539 | Anim* JJ1Level::getLevelAnim (unsigned char anim) { |
| 540 | |
| 541 | return animSet + levelAnims[anim]; |
| 542 | |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Get a level sound. |
| 547 | * |
| 548 | * @param sound Sound number |
| 549 | * |
| 550 | * @return Sound |
| 551 | */ |
| 552 | SE::Type JJ1Level::getLevelSound (unsigned char sound) { |
| 553 | |
| 554 | return static_cast<SE::Type>(levelSounds[sound]); |
| 555 | |
| 556 | } |
| 557 | |
| 558 | |
| 559 | /** |
| 560 | * Set the water level. |
| 561 | * |
| 562 | * @param gridY New water level y-coordinate |
| 563 | */ |
| 564 | void JJ1Level::setWaterLevel (unsigned char gridY) { |
| 565 | |
| 566 | waterLevelTarget = TTOF(gridY) + F2; |
| 567 | |
| 568 | if (multiplayer) { |
| 569 | |
| 570 | unsigned char buffer[MTL_L_PROP]; |
| 571 | buffer[0] = MTL_L_PROP; |
| 572 | buffer[1] = MT_L_PROP; |
| 573 | buffer[2] = 1; // set water level |
| 574 | buffer[3] = gridY; |
| 575 | buffer[4] = 0; // Doesn't really matter |
| 576 | |
| 577 | game->send(buffer); |
| 578 | |
| 579 | } |
| 580 | |
| 581 | } |
| 582 | |
| 583 | |
| 584 | /** |
| 585 | * Determine the water level. |
| 586 | * |
| 587 | * @return The y-coordinate of the water level |
| 588 | */ |
| 589 | fixed JJ1Level::getWaterLevel () { |
| 590 | |
| 591 | return waterLevel; |
| 592 | |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /** |
| 597 | * Create new bullet(s) (or event(s), if applicable) |
| 598 | * |
| 599 | * @param sourcePlayer The player that fired the bullet (if any) |
| 600 | * @param gridX The grid X-coordinate of the origin of the event that fired the bullet (if any) |
| 601 | * @param gridY The grid Y-coordinate of the origin of the event that fired the bullet (if any) |
| 602 | * @param startX The initial X-coordinate of the bullet |
| 603 | * @param startY The initial Y-coordinate of the bullet |
| 604 | * @param bullet Type |
| 605 | * @param facing The direction of the bullet |
| 606 | * @param time Time |
| 607 | */ |
| 608 | void JJ1Level::createBullet (JJ1LevelPlayer* sourcePlayer, unsigned char gridX, unsigned char gridY, fixed startX, fixed startY, unsigned char bullet, bool facing, unsigned int time) { |
| 609 | |
| 610 | signed char* set; |
| 611 | int direction; |
| 612 | |
| 613 | set = bulletSet[bullet]; |
| 614 | |
| 615 | direction = facing? 1: 0; |
| 616 | |
| 617 | if (set[B_GRAVITY | direction] == 4) { |
| 618 | |
| 619 | events = new JJ1StandardEvent(eventSet + set[B_SPRITE | direction], gridX, gridY, startX, startY + F32); |
| 620 | |
| 621 | } else if (set[B_SPRITE | direction] != 0) { |
| 622 | |
| 623 | // Create new bullet |
| 624 | level->bullets = new JJ1Bullet(level->bullets, |
| 625 | sourcePlayer, |
| 626 | startX, |
| 627 | startY, |
| 628 | set, |
| 629 | direction, |
| 630 | time); |
| 631 | |
| 632 | if (set[B_XSPEED | direction | 2] != 0) { |
| 633 | |
| 634 | // Create the other bullet |
| 635 | level->bullets = new JJ1Bullet(level->bullets, |
| 636 | sourcePlayer, |
| 637 | startX, |
| 638 | startY, |
| 639 | set, |
| 640 | direction | 2, |
| 641 | time); |
| 642 | |
| 643 | } |
| 644 | |
| 645 | // FIXME: rewrite "set" |
| 646 | auto se = static_cast<SE::Type>(set[B_STARTSOUND]); |
| 647 | playSound(se); |
| 648 | |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /** |
| 655 | * Start a flash palette effect. |
| 656 | * |
| 657 | * @param red Red component of flash colour |
| 658 | * @param green Green component of flash colour |
| 659 | * @param blue Blue component of flash colour |
| 660 | * @param duration Duration of the flash effect |
| 661 | */ |
| 662 | void JJ1Level::flash (unsigned char red, unsigned char green, unsigned char blue, int duration) { |
| 663 | |
| 664 | paletteEffects = new FlashPaletteEffect(red, green, blue, duration, paletteEffects); |
| 665 | |
| 666 | } |
| 667 | |
| 668 | |
| 669 | /** |
| 670 | * Play the bonus level. |
| 671 | * |
| 672 | * @return Error code |
| 673 | */ |
| 674 | int JJ1Level::playBonus () { |
| 675 | |
| 676 | char *bonusFile; |
| 677 | int ret; |
| 678 | |
| 679 | if (!localPlayer->getJJ1LevelPlayer()->hasGem()) return E_NONE; |
| 680 | |
| 681 | delete paletteEffects; |
| 682 | paletteEffects = nullptr; |
| 683 | |
| 684 | bonusFile = createFileName("BONUSMAP", worldToEpisode(worldNum)); |
| 685 | |
| 686 | // If the gem has been collected, play the bonus level |
| 687 | ret = game->playLevel(bonusFile); |
| 688 | |
| 689 | delete[] bonusFile; |
| 690 | |
| 691 | return ret; |
| 692 | |
| 693 | } |
| 694 | |
| 695 | |
| 696 | /** |
| 697 | * Interpret data received from client/server |
| 698 | * |
| 699 | * @param buffer Received data |
| 700 | */ |
| 701 | void JJ1Level::receive (unsigned char* buffer) { |
| 702 | |
| 703 | switch (buffer[1]) { |
| 704 | |
| 705 | case MT_L_PROP: |
| 706 | |
| 707 | if (buffer[2] == 0) { |
| 708 | |
| 709 | nextLevelNum = buffer[3]; |
| 710 | nextWorldNum = buffer[4]; |
| 711 | |
| 712 | } else if (buffer[2] == 1) { |
| 713 | |
| 714 | waterLevelTarget = TTOF(buffer[3]); |
| 715 | |
| 716 | } else if (buffer[2] == 2) { |
| 717 | |
| 718 | if (stage == LS_NORMAL) |
| 719 | endTime += buffer[3] * 1000; |
| 720 | |
| 721 | } |
| 722 | |
| 723 | break; |
| 724 | |
| 725 | case MT_L_GRID: |
| 726 | |
| 727 | if (buffer[4] == 0) grid[buffer[3]][buffer[2]].tile = buffer[5]; |
| 728 | else if (buffer[4] == 2) |
| 729 | grid[buffer[3]][buffer[2]].event = buffer[5]; |
| 730 | else if (buffer[4] == 3) |
| 731 | grid[buffer[3]][buffer[2]].hits = buffer[5]; |
| 732 | |
| 733 | break; |
| 734 | |
| 735 | case MT_L_STAGE: |
| 736 | |
| 737 | stage = LevelStage(buffer[2]); |
| 738 | |
| 739 | break; |
| 740 | |
| 741 | } |
| 742 | |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /** |
| 747 | * Play the level. |
| 748 | * |
| 749 | * @return Error code |
| 750 | */ |
| 751 | int JJ1Level::play () { |
| 752 | JJ1LevelPlayer* levelPlayer = localPlayer->getJJ1LevelPlayer(); |
| 753 | tickOffset = globalTicks; |
| 754 | ticks = T_STEP; |
| 755 | steps = 0; |
| 756 | bool pmessage = false, pmenu = false; |
| 757 | int option = 0; |
| 758 | |
| 759 | unsigned int returnTime = 0; |
| 760 | int timeBonus = -1; |
| 761 | int perfect = 0, enemyPercent = 0, itemPercent = 0; |
| 762 | |
| 763 | video.setPalette(palette); |
| 764 | |
| 765 | playMusic(musicFile); |
| 766 | |
| 767 | while (true) { |
| 768 | |
| 769 | int ret = loop(pmenu, option, pmessage); |
| 770 | if (ret < 0) return ret; |
| 771 | |
| 772 | // Check if level has been won |
| 773 | if (game && returnTime && (ticks > returnTime)) { |
| 774 | |
| 775 | // Play "Level end" cutscene |
| 776 | // if available and secret level 2 was not found (FIXME: check JJ1, if true) |
| 777 | if (strlen(sceneFile) && nextLevelNum != 2) { |
| 778 | if (playScene(sceneFile) == E_QUIT) return E_QUIT; |
| 779 | } |
| 780 | |
| 781 | if (!multiplayer) { |
| 782 | // If the gem has been collected, play the bonus level |
| 783 | ret = playBonus(); |
| 784 | |
| 785 | if (ret < 0) return ret; |
| 786 | } |
| 787 | |
| 788 | // No next level, end game |
| 789 | if (nextLevelNum == 99) { |
| 790 | ret = game->setLevel(nullptr); |
| 791 | } else { |
| 792 | // Advance to next level |
| 793 | char *string = createFileName("LEVEL", nextLevelNum, nextWorldNum); |
| 794 | ret = game->setLevel(string); |
| 795 | delete[] string; |
| 796 | } |
| 797 | |
| 798 | if (ret < 0) return ret; |
| 799 | |
| 800 | return WON; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | // Process frame-by-frame activity |
| 805 | |
| 806 | while (getTimeChange() >= T_STEP) { |
| 807 | |
| 808 | bool playerWasAlive = (localPlayer->getJJ1LevelPlayer()->getEnergy() != 0); |
| 809 | |
| 810 | // Apply controls to local player |
| 811 | for (int i = 0; i < PCONTROLS; i++) |
| 812 | localPlayer->setControl(i, controls.getState(i)); |
| 813 | |
| 814 | ret = step(); |
| 815 | steps++; |
| 816 | |
| 817 | if (ret) return ret; |
| 818 | |
| 819 | if (!multiplayer && playerWasAlive && (localPlayer->getJJ1LevelPlayer()->getEnergy() == 0)) |
| 820 | flash(0, 0, 0, T_END << 1); |
| 821 | |
| 822 | } |
| 823 | |
| 824 | |
| 825 | // Draw the graphics |
| 826 | |
| 827 | draw(); |
| 828 | |
| 829 | |
| 830 | // If paused, draw "PAUSE" |
| 831 | if (pmessage && !pmenu) |
| 832 | font->showString("pause", (canvasW >> 1) - 44, 32); |
| 833 | |
| 834 | // If paused, silence music |
| 835 | pauseMusic(pmessage && !pmenu); |
| 836 | |
| 837 | if (stage == LS_END) { |
| 838 | |
| 839 | // The level is over, so draw play statistics & bonuses |
| 840 | |
| 841 | // Apply time bonus |
| 842 | |
| 843 | if (timeBonus) { |
| 844 | |
| 845 | int bonusCount = (ticks - prevTicks) / 100; |
| 846 | if (!bonusCount) bonusCount = 1; |
| 847 | |
| 848 | if (timeBonus == -1) { |
| 849 | |
| 850 | if (ticks < endTime) timeBonus = ((endTime - ticks) / 60000) * 100; |
| 851 | else timeBonus = 0; |
| 852 | |
| 853 | LOG_DEBUG("Killed %d of %d enemies, needed %d", |
| 854 | levelPlayer->getEnemies(), enemies, nEnemies[+getDifficulty()]); |
| 855 | LOG_DEBUG("Collected %d of %d items, needed %d", |
| 856 | levelPlayer->getItems(), items, nItems); |
| 857 | |
| 858 | if (nEnemies[+getDifficulty()]) { |
| 859 | enemyPercent = (levelPlayer->getEnemies() * 100) / (float) nEnemies[+getDifficulty()]; |
| 860 | if (enemyPercent > 100) enemyPercent = 100; |
| 861 | } |
| 862 | if (nItems) { |
| 863 | itemPercent = (levelPlayer->getItems() * 100) / (float) nItems; |
| 864 | if (itemPercent > 100) itemPercent = 100; |
| 865 | } |
| 866 | |
| 867 | // FIXME: check if JJ1 shows perfect score when there are no enemies or items |
| 868 | if (enemyPercent == 100 && itemPercent == 100) { |
| 869 | perfect = 100; |
| 870 | LOG_DEBUG("Reached a perfect (100%%) score"); |
| 871 | } |
| 872 | |
| 873 | } else if (timeBonus - bonusCount >= 0) { |
| 874 | |
| 875 | localPlayer->addScore(bonusCount * 10); |
| 876 | timeBonus -= bonusCount; |
| 877 | |
| 878 | auto se = getLevelSound(LSND_ENDCOUNT); |
| 879 | if(!isSoundPlaying(se)) |
| 880 | playSound(se); |
| 881 | |
| 882 | } else { |
| 883 | |
| 884 | localPlayer->addScore(timeBonus * 10); |
| 885 | timeBonus = 0; |
| 886 | |
| 887 | } |
| 888 | |
| 889 | if (timeBonus == 0) { |
| 890 | |
| 891 | returnTime = ticks + T_END; |
| 892 | paletteEffects = new WhiteOutPaletteEffect(T_END, paletteEffects); |
| 893 | playSound(getLevelSound(LSND_DONE)); |
| 894 | |
| 895 | } |
| 896 | |
| 897 | } |
| 898 | |
| 899 | // Display statistics & bonuses |
| 900 | |
| 901 | font->showString("time", (canvasW >> 1) - 152, (canvasH >> 1) - 60); |
| 902 | font->showNumber(timeBonus, (canvasW >> 1) + 124, (canvasH >> 1) - 60); |
| 903 | |
| 904 | font->showString("enemies", (canvasW >> 1) - 152, (canvasH >> 1) - 40); |
| 905 | font->showNumber(enemyPercent, (canvasW >> 1) + 124, (canvasH >> 1) - 40); |
| 906 | font->showString("%", (canvasW >> 1) + 124, (canvasH >> 1) - 40); |
| 907 | |
| 908 | font->showString("items", (canvasW >> 1) - 152, (canvasH >> 1) - 20); |
| 909 | font->showNumber(itemPercent, (canvasW >> 1) + 124, (canvasH >> 1) - 20); |
| 910 | font->showString("%", (canvasW >> 1) + 124, (canvasH >> 1) - 20); |
| 911 | |
| 912 | font->showString("perfect", (canvasW >> 1) - 152, canvasH >> 1); |
| 913 | font->showNumber(perfect, (canvasW >> 1) + 124, canvasH >> 1); |
| 914 | |
| 915 | font->showString("score", (canvasW >> 1) - 152, (canvasH >> 1) + 40); |
| 916 | font->showNumber(localPlayer->getScore(), (canvasW >> 1) + 124, (canvasH >> 1) + 40); |
| 917 | |
| 918 | } |
| 919 | |
| 920 | |
| 921 | // Draw statistics, menu etc. |
| 922 | drawOverlay(LEVEL_BLACK, pmenu, option, 15, 47, -16); |
| 923 | |
| 924 | } |
| 925 | |
| 926 | return E_NONE; |
| 927 | |
| 928 | } |