| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj2level.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 22nd July 2008: Created levelload.c from parts of level.c |
| 11 | * - 3rd February 2009: Renamed level.c to level.cpp |
| 12 | * - 19th July 2009: Added parts of levelload.cpp to level.cpp |
| 13 | * - 29th June 2010: Created jj2level.cpp from parts of level.cpp |
| 14 | * - 2nd July 2010: Created jj2event.cpp from parts of jj2level.cpp |
| 15 | * - 2nd July 2010: Created jj2eventframe.cpp from parts of jj2level.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2012 AJ Thomson |
| 19 | * |
| 20 | * OpenJazz is distributed under the terms of |
| 21 | * the GNU General Public License, version 2.0 |
| 22 | * |
| 23 | * @par Description: |
| 24 | * Deals with the creating, playing and freeing of JJ2 levels. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "event/jj2event.h" |
| 30 | #include "jj2level.h" |
| 31 | #include "jj2levelplayer.h" |
| 32 | |
| 33 | #include "game/game.h" |
| 34 | #include "game/gamemode.h" |
| 35 | #include "io/controls.h" |
| 36 | #include "io/file.h" |
| 37 | #include "io/gfx/font.h" |
| 38 | #include "io/gfx/sprite.h" |
| 39 | #include "io/gfx/video.h" |
| 40 | #include "io/sound.h" |
| 41 | #include "util.h" |
| 42 | |
| 43 | #include <string.h> |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Create a JJ2 level. |
| 48 | * |
| 49 | * @param owner The current game |
| 50 | * @param fileName Name of the file containing the level data. |
| 51 | * @param checkpoint Whether or not the player(s) will start at a checkpoint |
| 52 | * @param multi Whether or not the level will be multi-player |
| 53 | */ |
| 54 | JJ2Level::JJ2Level (Game* owner, char* fileName, bool checkpoint, bool multi) : |
| 55 | Level(owner) { |
| 56 | |
| 57 | int ret; |
| 58 | |
| 59 | // Load level data |
| 60 | |
| 61 | ret = load(fileName, checkpoint); |
| 62 | |
| 63 | if (ret < 0) throw ret; |
| 64 | |
| 65 | multiplayer = multi; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Delete the JJ2 level. |
| 72 | */ |
| 73 | JJ2Level::~JJ2Level () { |
| 74 | |
| 75 | int count; |
| 76 | |
| 77 | if (events) delete events; |
| 78 | delete[] *mods; |
| 79 | delete[] mods; |
| 80 | |
| 81 | for (count = 0; count < LAYERS; count++) delete layers[count]; |
| 82 | |
| 83 | delete[] flippedMask; |
| 84 | delete[] mask; |
| 85 | |
| 86 | delete[] musicFile; |
| 87 | delete[] nextLevel; |
| 88 | |
| 89 | for (count = 0; count < nAnimSets; count++) { |
| 90 | |
| 91 | if (animSets[count]) delete[] animSets[count]; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | delete[] animSets; |
| 96 | delete[] spriteSet; |
| 97 | |
| 98 | SDL_FreeSurface(flippedTileSet); |
| 99 | SDL_FreeSurface(tileSet); |
| 100 | |
| 101 | delete font; |
| 102 | |
| 103 | // Restore panel font palette |
| 104 | panelBigFont->restorePalette(); |
| 105 | panelSmallFont->restorePalette(); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Determine whether or not the given point is solid when travelling upwards. |
| 112 | * |
| 113 | * @param x X-coordinate |
| 114 | * @param y Y-coordinate |
| 115 | * |
| 116 | * @return Solidity |
| 117 | */ |
| 118 | bool JJ2Level::checkMaskUp (fixed x, fixed y) { |
| 119 | |
| 120 | int tX, tY; |
| 121 | |
| 122 | tX = FTOT(x); |
| 123 | tY = FTOT(y); |
| 124 | |
| 125 | // Anything off the edge of the map is solid |
| 126 | if ((x < 0) || (y < 0) || (tX >= layer->getWidth()) || (tY >= layer->getHeight())) |
| 127 | return true; |
| 128 | |
| 129 | // Event 1 is one-way |
| 130 | // Event 3 is vine |
| 131 | // Event 4 is hook |
| 132 | if ((mods[tY][tX].type == 1) || (mods[tY][tX].type == 3) || (mods[tY][tX].type == 4)) return false; |
| 133 | |
| 134 | // Check the mask in the tile in question |
| 135 | return (layer->getFlipped(tX, tY)? flippedMask: mask)[(layer->getTile(tX, tY) << 10) + ((y >> 5) & 992) + ((x >> 10) & 31)]; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Determine whether or not the given point is solid when travelling downwards. |
| 142 | * |
| 143 | * @param x X-coordinate |
| 144 | * @param y Y-coordinate |
| 145 | * @param drop Whether or not the player is dropping |
| 146 | * |
| 147 | * @return Solidity |
| 148 | */ |
| 149 | bool JJ2Level::checkMaskDown (fixed x, fixed y, bool drop) { |
| 150 | |
| 151 | int tX, tY; |
| 152 | |
| 153 | tX = FTOT(x); |
| 154 | tY = FTOT(y); |
| 155 | |
| 156 | // Anything off the edge of the map is solid |
| 157 | if ((x < 0) || (y < 0) || (tX >= layer->getWidth()) || (tY >= layer->getHeight())) |
| 158 | return true; |
| 159 | |
| 160 | // Event 3 is vine |
| 161 | // Event 4 is hook |
| 162 | if (drop && ((mods[tY][tX].type == 3) || (mods[tY][tX].type == 4))) return false; |
| 163 | |
| 164 | // Check the mask in the tile in question |
| 165 | return (layer->getFlipped(tX, tY)? flippedMask: mask)[(layer->getTile(tX, tY) << 10) + ((y >> 5) & 992) + ((x >> 10) & 31)]; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Set which level will come next. |
| 172 | * |
| 173 | * @param fileName Next level's file name |
| 174 | */ |
| 175 | void JJ2Level::setNext (char* fileName) { |
| 176 | |
| 177 | delete[] nextLevel; |
| 178 | nextLevel = createString(fileName); |
| 179 | |
| 180 | if (multiplayer) { |
| 181 | |
| 182 | unsigned char buffer[MTL_L_PROP]; |
| 183 | buffer[0] = MTL_L_PROP; |
| 184 | buffer[1] = MT_L_PROP; |
| 185 | buffer[2] = 0; // set next level |
| 186 | buffer[3] = 0; |
| 187 | buffer[4] = 0; |
| 188 | |
| 189 | game->send(buffer); |
| 190 | |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * Set the frame of the animated tile at the given location. |
| 198 | * |
| 199 | * @param gridX X-coordinate of the tile |
| 200 | * @param gridY Y-coordinate of the tile |
| 201 | * @param frame The new frame |
| 202 | */ |
| 203 | void JJ2Level::setFrame (int gridX, int gridY, unsigned char frame) { |
| 204 | |
| 205 | layer->setFrame(gridX, gridY, frame); |
| 206 | |
| 207 | if (multiplayer) { |
| 208 | |
| 209 | unsigned char buffer[MTL_L_GRID]; |
| 210 | buffer[0] = MTL_L_GRID; |
| 211 | buffer[1] = MT_L_GRID; |
| 212 | buffer[2] = gridX & 0xFF; |
| 213 | buffer[3] = gridY & 0xFF; |
| 214 | buffer[4] = 0; // tile variable |
| 215 | buffer[5] = frame; |
| 216 | buffer[6] = (gridX >> 8) & 0xFF; |
| 217 | buffer[7] = (gridY >> 8) & 0xFF; |
| 218 | |
| 219 | game->send(buffer); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Get the modifier event for the given tile. |
| 228 | * |
| 229 | * @param gridX X-coordinate of the tile |
| 230 | * @param gridY Y-coordinate of the tile |
| 231 | * |
| 232 | * @return Modifier event |
| 233 | */ |
| 234 | JJ2Modifier* JJ2Level::getModifier (int gridX, int gridY) { |
| 235 | |
| 236 | return mods[gridY] + gridX; |
| 237 | |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * Get a sprite. |
| 243 | * |
| 244 | * @param sprite Sprite number |
| 245 | * |
| 246 | * @return Sprite |
| 247 | */ |
| 248 | Sprite* JJ2Level::getSprite (unsigned char sprite) { |
| 249 | |
| 250 | return spriteSet + sprite; |
| 251 | |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * Get an animation. |
| 257 | * |
| 258 | * @param set Animation set number |
| 259 | * @param anim Animation number |
| 260 | * @param flipped Whether or not the animation should be flipped horizontally |
| 261 | * |
| 262 | * @return Animation |
| 263 | */ |
| 264 | Anim* JJ2Level::getAnim (int set, int anim, bool flipped) { |
| 265 | |
| 266 | return (flipped? flippedAnimSets: animSets)[set] + anim; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /** |
| 272 | * Get a player animation. |
| 273 | * |
| 274 | * @param character Character |
| 275 | * @param anim Animation number |
| 276 | * @param flipped Whether or not the animation should be flipped horizontally |
| 277 | * |
| 278 | * @return Animation |
| 279 | */ |
| 280 | Anim* JJ2Level::getPlayerAnim (int character, int anim, bool flipped) { |
| 281 | |
| 282 | int set; |
| 283 | |
| 284 | if (TSF) { |
| 285 | |
| 286 | if (character == 1) set = 61; |
| 287 | else if (character == 2) set = 89; |
| 288 | else set = 55; |
| 289 | |
| 290 | } else { |
| 291 | |
| 292 | if (character == 1) set = 85; |
| 293 | else set = 54; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | return getAnim(set, playerAnims[anim], flipped); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * Set the water level. |
| 304 | * |
| 305 | * @param gridY New water level y-coordinate |
| 306 | * @param instant Whether or not the change is instant |
| 307 | */ |
| 308 | void JJ2Level::setWaterLevel (int gridY, bool instant) { |
| 309 | |
| 310 | waterLevelTarget = TTOF(gridY); |
| 311 | |
| 312 | if (instant) waterLevel = waterLevelTarget - F8; |
| 313 | |
| 314 | if (multiplayer) { |
| 315 | |
| 316 | unsigned char buffer[MTL_L_PROP]; |
| 317 | buffer[0] = MTL_L_PROP; |
| 318 | buffer[1] = MT_L_PROP; |
| 319 | buffer[2] = 1; // set water level |
| 320 | buffer[3] = gridY & 0xFF; |
| 321 | buffer[4] = (gridY >> 8) & 0xFF; |
| 322 | |
| 323 | game->send(buffer); |
| 324 | |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * Determine the water level. |
| 332 | * |
| 333 | * @return The y-coordinate of the water level |
| 334 | */ |
| 335 | fixed JJ2Level::getWaterLevel () { |
| 336 | |
| 337 | return waterLevel; |
| 338 | |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /** |
| 343 | * Move a player to a warp target. |
| 344 | * |
| 345 | * @param player The player to move |
| 346 | * @param id The warp target ID |
| 347 | */ |
| 348 | void JJ2Level::warp (JJ2LevelPlayer *player, int id) { |
| 349 | |
| 350 | int x, y; |
| 351 | |
| 352 | for (y = 0; y < layer->getHeight(); y++) { |
| 353 | |
| 354 | for (x = 0; x < layer->getWidth(); x++) { |
| 355 | |
| 356 | if ((mods[y][x].type == 240) && ((mods[y][x].properties & 255) == id)) { |
| 357 | |
| 358 | player->setPosition(TTOF(x), TTOF(y)); |
| 359 | |
| 360 | return; |
| 361 | |
| 362 | } |
| 363 | |
| 364 | } |
| 365 | |
| 366 | } |
| 367 | |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /** |
| 372 | * Interpret data received from client/server |
| 373 | * |
| 374 | * @param buffer Received data |
| 375 | */ |
| 376 | void JJ2Level::receive (unsigned char* buffer) { |
| 377 | |
| 378 | switch (buffer[1]) { |
| 379 | |
| 380 | case MT_L_PROP: |
| 381 | |
| 382 | if (buffer[2] == 1) { |
| 383 | |
| 384 | waterLevelTarget = TTOF(buffer[3] + (buffer[4] << 8)); |
| 385 | |
| 386 | } else if (buffer[2] == 2) { |
| 387 | |
| 388 | if (stage == LS_NORMAL) |
| 389 | endTime += buffer[3] * 1000; |
| 390 | |
| 391 | } |
| 392 | |
| 393 | break; |
| 394 | |
| 395 | case MT_L_GRID: |
| 396 | |
| 397 | if (buffer[4] == 0) layer->setFrame(buffer[2] + (buffer[6] << 8), buffer[3] + (buffer[7] << 8), buffer[5]); |
| 398 | |
| 399 | break; |
| 400 | |
| 401 | case MT_L_STAGE: |
| 402 | |
| 403 | stage = LevelStage(buffer[2]); |
| 404 | |
| 405 | break; |
| 406 | |
| 407 | } |
| 408 | |
| 409 | } |
| 410 | |
| 411 | |
| 412 | /** |
| 413 | * Play the level. |
| 414 | * |
| 415 | * @return Error code |
| 416 | */ |
| 417 | int JJ2Level::play () { |
| 418 | |
| 419 | JJ2LevelPlayer* jj2LevelPlayer; |
| 420 | bool pmessage, pmenu; |
| 421 | int option; |
| 422 | unsigned int returnTime; |
| 423 | int count; |
| 424 | |
| 425 | jj2LevelPlayer = localPlayer->getJJ2LevelPlayer(); |
| 426 | |
| 427 | tickOffset = globalTicks; |
| 428 | ticks = T_STEP; |
| 429 | steps = 0; |
| 430 | |
| 431 | pmessage = pmenu = false; |
| 432 | option = 0; |
| 433 | |
| 434 | returnTime = 0; |
| 435 | |
| 436 | video.setPalette(palette); |
| 437 | |
| 438 | playMusic(musicFile); |
| 439 | |
| 440 | while (true) { |
| 441 | |
| 442 | int ret = loop(pmenu, option, pmessage); |
| 443 | |
| 444 | if (ret < 0) return ret; |
| 445 | |
| 446 | |
| 447 | // Check if level has been won |
| 448 | if (game && returnTime && (ticks > returnTime)) { |
| 449 | |
| 450 | ret = game->setLevel(nextLevel); |
| 451 | |
| 452 | if (ret < 0) return ret; |
| 453 | |
| 454 | return WON; |
| 455 | |
| 456 | } |
| 457 | |
| 458 | |
| 459 | // Process frame-by-frame activity |
| 460 | |
| 461 | while (getTimeChange() >= T_STEP) { |
| 462 | |
| 463 | // Apply controls to local player |
| 464 | for (count = 0; count < PCONTROLS; count++) |
| 465 | localPlayer->setControl(count, controls.getState(count)); |
| 466 | |
| 467 | ret = step(); |
| 468 | steps++; |
| 469 | |
| 470 | if (ret) return ret; |
| 471 | |
| 472 | } |
| 473 | |
| 474 | |
| 475 | // Draw the graphics |
| 476 | |
| 477 | draw(); |
| 478 | |
| 479 | |
| 480 | // If paused, draw "PAUSE" |
| 481 | if (pmessage && !pmenu) |
| 482 | font->showString("pause", (canvasW >> 1) - 44, 32); |
| 483 | |
| 484 | // If paused, silence music |
| 485 | pauseMusic(pmessage && !pmenu); |
| 486 | |
| 487 | if (stage == LS_END) { |
| 488 | |
| 489 | // The level is over, so draw gem counts |
| 490 | |
| 491 | if (!returnTime) { |
| 492 | |
| 493 | returnTime = ticks + 3000; |
| 494 | playSound(SE::UPLOOP); |
| 495 | |
| 496 | } |
| 497 | |
| 498 | // Display statistics |
| 499 | |
| 500 | font->showString("red gems", (canvasW >> 1) - 152, (canvasH >> 1) - 60); |
| 501 | font->showNumber(jj2LevelPlayer->getGems(0), (canvasW >> 1) + 124, (canvasH >> 1) - 60); |
| 502 | |
| 503 | font->showString("green gems", (canvasW >> 1) - 152, (canvasH >> 1) - 40); |
| 504 | font->showNumber(jj2LevelPlayer->getGems(1), (canvasW >> 1) + 124, (canvasH >> 1) - 40); |
| 505 | |
| 506 | font->showString("blue gems", (canvasW >> 1) - 152, (canvasH >> 1) - 20); |
| 507 | font->showNumber(jj2LevelPlayer->getGems(2), (canvasW >> 1) + 124, (canvasH >> 1) - 20); |
| 508 | |
| 509 | } |
| 510 | |
| 511 | |
| 512 | // Draw statistics, menu etc. |
| 513 | drawOverlay(JJ2_BLACK, pmenu, option, 71, 31, -8); |
| 514 | |
| 515 | } |
| 516 | |
| 517 | return E_NONE; |
| 518 | |
| 519 | } |