| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj2levelload.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 levelload.c to levelload.cpp |
| 12 | * - 28th June 2010: Created levelloadjj2.cpp from parts of levelload.cpp |
| 13 | * - 29th June 2010: Renamed levelloadjj2.cpp to jj2levelload.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2017 AJ Thomson |
| 17 | * |
| 18 | * OpenJazz is distributed under the terms of |
| 19 | * the GNU General Public License, version 2.0 |
| 20 | * |
| 21 | * @par Description: |
| 22 | * Deals with the loading of JJ2 level data. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include "event/jj2event.h" |
| 28 | #include "jj2level.h" |
| 29 | #include "jj2levelplayer.h" |
| 30 | |
| 31 | #include "game/game.h" |
| 32 | #include "io/file.h" |
| 33 | #include "io/gfx/font.h" |
| 34 | #include "io/gfx/sprite.h" |
| 35 | #include "io/gfx/video.h" |
| 36 | #include "io/sound.h" |
| 37 | #include "loop.h" |
| 38 | #include "util.h" |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | |
| 43 | #define SKEY 254 /* Sprite colour key */ |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Load a sprite. |
| 48 | * |
| 49 | * @param parameters Sprite parameters |
| 50 | * @param compressedPixels Compressed data from which to obtain the sprite data |
| 51 | * @param sprite Sprite that will receive the loaded data |
| 52 | * @param flippedSprite Sprite that will receive the flipped loaded data |
| 53 | */ |
| 54 | void JJ2Level::loadSprite (unsigned char* parameters, unsigned char* compressedPixels, Sprite* sprite, Sprite* flippedSprite) { |
| 55 | |
| 56 | unsigned char* pixels; |
| 57 | int width, height; |
| 58 | int srcPos, dstPos, rle; |
| 59 | int x, y; |
| 60 | |
| 61 | // Load dimensions |
| 62 | width = createShort(parameters); |
| 63 | height = createShort(parameters + 2); |
| 64 | |
| 65 | if ((width == 0) || (height == 0)) { |
| 66 | |
| 67 | sprite->clearPixels(); |
| 68 | |
| 69 | return; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | // Decompress pixels |
| 75 | |
| 76 | pixels = new unsigned char[width * height]; |
| 77 | memset(pixels, 0, width * height); |
| 78 | |
| 79 | srcPos = createInt(parameters + 16); |
| 80 | dstPos = 0; |
| 81 | |
| 82 | while (dstPos < width * height) { |
| 83 | |
| 84 | rle = compressedPixels[srcPos]; |
| 85 | srcPos++; |
| 86 | |
| 87 | if (rle > 128) { |
| 88 | |
| 89 | rle &= 127; |
| 90 | |
| 91 | if (dstPos + rle < width * height) |
| 92 | memcpy(pixels + dstPos, compressedPixels + srcPos, rle); |
| 93 | |
| 94 | srcPos += rle; |
| 95 | dstPos += rle; |
| 96 | |
| 97 | } else if (rle == 128) { |
| 98 | |
| 99 | rle = dstPos % width; |
| 100 | |
| 101 | if (rle) dstPos = dstPos + width - rle; |
| 102 | |
| 103 | } else { |
| 104 | |
| 105 | dstPos += rle; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | // Set sprite data |
| 113 | sprite->setOffset(createShort(parameters + 8), |
| 114 | createShort(parameters + 10)); |
| 115 | sprite->setPixels(pixels, width, height, 0); |
| 116 | |
| 117 | // Flip sprite |
| 118 | for (y = 0; y < height; y++) { |
| 119 | |
| 120 | for (x = 0; x < (width >> 1); x++) { |
| 121 | |
| 122 | rle = pixels[(y * width) + x]; |
| 123 | pixels[(y * width) + x] = pixels[(y * width) + (width - 1) - x]; |
| 124 | pixels[(y * width) + (width - 1) - x] = rle; |
| 125 | |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | // Set flipped sprite data |
| 131 | flippedSprite->setOffset(-createShort(parameters + 8) - width, |
| 132 | createShort(parameters + 10)); |
| 133 | flippedSprite->setPixels(pixels, width, height, 0); |
| 134 | |
| 135 | delete[] pixels; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Load sprites. |
| 142 | * |
| 143 | * @return Error code |
| 144 | */ |
| 145 | int JJ2Level::loadSprites () { |
| 146 | |
| 147 | File* file; |
| 148 | int* setOffsets; |
| 149 | int set; |
| 150 | |
| 151 | // Thanks to Neobeo for working out the .j2a format |
| 152 | |
| 153 | |
| 154 | try { |
| 155 | |
| 156 | file = new File("anims.j2a", PATH_TYPE_GAME); |
| 157 | |
| 158 | } catch (int e) { |
| 159 | |
| 160 | return e; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | file->seek(24, true); |
| 165 | |
| 166 | nAnimSets = file->loadInt(); |
| 167 | |
| 168 | setOffsets = new int[nAnimSets]; |
| 169 | |
| 170 | for (set = 0; set < nAnimSets; set++) setOffsets[set] = file->loadInt(); |
| 171 | |
| 172 | |
| 173 | // Count number of sprites |
| 174 | |
| 175 | int nSprites = 0; |
| 176 | |
| 177 | for (set = 0; set < nAnimSets; set++) { |
| 178 | |
| 179 | file->seek(setOffsets[set] + 6, true); |
| 180 | |
| 181 | nSprites += file->loadShort(); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | spriteSet = new Sprite[nSprites]; |
| 186 | flippedSpriteSet = new Sprite[nSprites]; |
| 187 | animSets = new Anim *[nAnimSets]; |
| 188 | flippedAnimSets = new Anim *[nAnimSets]; |
| 189 | |
| 190 | |
| 191 | // Load animations and sprites |
| 192 | |
| 193 | nSprites = 0; |
| 194 | |
| 195 | for (set = 0; set < nAnimSets; set++) { |
| 196 | |
| 197 | file->seek(setOffsets[set] + 4, true); |
| 198 | |
| 199 | int setAnims = file->loadChar(); |
| 200 | |
| 201 | if (setAnims) { |
| 202 | |
| 203 | animSets[set] = new Anim[setAnims]; |
| 204 | flippedAnimSets[set] = new Anim[setAnims]; |
| 205 | |
| 206 | } else { |
| 207 | |
| 208 | animSets[set] = NULL; |
| 209 | flippedAnimSets[set] = NULL; |
| 210 | |
| 211 | } |
| 212 | |
| 213 | file->seek(7, false); |
| 214 | |
| 215 | int aCLength = file->loadInt(); |
| 216 | int aLength = file->loadInt(); |
| 217 | int bCLength = file->loadInt(); |
| 218 | int bLength = file->loadInt(); |
| 219 | int cCLength = file->loadInt(); |
| 220 | int cLength = file->loadInt(); |
| 221 | file->loadInt(); // Don't need this compressed block length |
| 222 | file->loadInt(); // Don't need this block length |
| 223 | |
| 224 | unsigned char* aBuffer = file->loadLZ(aCLength, aLength); |
| 225 | unsigned char* bBuffer = file->loadLZ(bCLength, bLength); |
| 226 | unsigned char* cBuffer = file->loadLZ(cCLength, cLength); |
| 227 | |
| 228 | int setSprite = 0; |
| 229 | |
| 230 | for (int anim = 0; anim < setAnims; anim++) { |
| 231 | |
| 232 | int animSprites = createShort(aBuffer + (anim * 8)); |
| 233 | |
| 234 | // Fonts are loaded separately |
| 235 | if (animSprites == 224) animSprites = 1; |
| 236 | |
| 237 | animSets[set][anim].setData(animSprites, 0, 0, 0, 0, 0, 0); |
| 238 | flippedAnimSets[set][anim].setData(animSprites, 0, 0, 0, 0, 0, 0); |
| 239 | |
| 240 | for (int sprite = 0; sprite < animSprites; sprite++) { |
| 241 | |
| 242 | loadSprite(bBuffer + (setSprite * 24), cBuffer, spriteSet + nSprites, flippedSpriteSet + nSprites); |
| 243 | |
| 244 | animSets[set][anim].setFrame(sprite, false); |
| 245 | animSets[set][anim].setFrameData(spriteSet + nSprites, 0, 0); |
| 246 | flippedAnimSets[set][anim].setFrame(sprite, false); |
| 247 | flippedAnimSets[set][anim].setFrameData(flippedSpriteSet + nSprites, 0, 0); |
| 248 | |
| 249 | setSprite++; |
| 250 | nSprites++; |
| 251 | |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | delete[] cBuffer; |
| 257 | delete[] bBuffer; |
| 258 | delete[] aBuffer; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | delete[] setOffsets; |
| 264 | |
| 265 | delete file; |
| 266 | |
| 267 | |
| 268 | return E_NONE; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * Load the tileset. |
| 275 | * |
| 276 | * @param fileName Name of the file containing the tileset |
| 277 | * |
| 278 | * @return The number of tiles loaded and the maximum possible number of tiles |
| 279 | */ |
| 280 | int JJ2Level::loadTiles (char* fileName) { |
| 281 | |
| 282 | File* file; |
| 283 | unsigned char* aBuffer; |
| 284 | unsigned char* bBuffer; |
| 285 | unsigned char* dBuffer; |
| 286 | unsigned char* tileBuffer; |
| 287 | int aCLength, bCLength, cCLength, dCLength; |
| 288 | int aLength, bLength, dLength; |
| 289 | int count, x, y; |
| 290 | int maxTiles; |
| 291 | int tiles; |
| 292 | |
| 293 | // Thanks to Neobeo for working out the most of the .j2t format |
| 294 | |
| 295 | |
| 296 | try { |
| 297 | |
| 298 | file = new File(fileName, PATH_TYPE_GAME); |
| 299 | |
| 300 | } catch (int e) { |
| 301 | |
| 302 | return e; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | // Skip to version indicator |
| 307 | file->seek(220, true); |
| 308 | |
| 309 | maxTiles = file->loadShort(); |
| 310 | |
| 311 | if (maxTiles == 0x201) maxTiles = 4096; |
| 312 | else maxTiles = 1024; |
| 313 | |
| 314 | |
| 315 | // Skip to compressed block lengths |
| 316 | file->seek(8, false); |
| 317 | aCLength = file->loadInt(); |
| 318 | aLength = file->loadInt(); |
| 319 | bCLength = file->loadInt(); |
| 320 | bLength = file->loadInt(); |
| 321 | cCLength = file->loadInt(); |
| 322 | file->loadInt(); // Don't need this block length |
| 323 | dCLength = file->loadInt(); |
| 324 | dLength = file->loadInt(); |
| 325 | |
| 326 | aBuffer = file->loadLZ(aCLength, aLength); |
| 327 | bBuffer = file->loadLZ(bCLength, bLength); |
| 328 | file->seek(cCLength, false); // Don't need this block |
| 329 | dBuffer = file->loadLZ(dCLength, dLength); |
| 330 | |
| 331 | delete file; |
| 332 | |
| 333 | |
| 334 | // Load the palette |
| 335 | for (count = 0; count < MAX_PALETTE_COLORS; count++) { |
| 336 | |
| 337 | palette[count].r = aBuffer[count << 2]; |
| 338 | palette[count].g = aBuffer[(count << 2) + 1]; |
| 339 | palette[count].b = aBuffer[(count << 2) + 2]; |
| 340 | |
| 341 | } |
| 342 | |
| 343 | |
| 344 | // Load tiles |
| 345 | |
| 346 | tiles = createShort(aBuffer + 1024); |
| 347 | tileBuffer = new unsigned char[tiles << 10]; |
| 348 | |
| 349 | for (count = 0; count < tiles; count++) { |
| 350 | |
| 351 | memcpy(tileBuffer + (count << 10), bBuffer + createInt(aBuffer + 1028 + (maxTiles << 1) + (count << 2)), 1024); |
| 352 | |
| 353 | } |
| 354 | |
| 355 | tileSet = createSurface(tileBuffer, TTOI(1), TTOI(tiles)); |
| 356 | enableColorKey(tileSet, 0); |
| 357 | |
| 358 | // Flip tiles |
| 359 | for (count = 0; count < TTOI(tiles); count++) { |
| 360 | |
| 361 | for (x = 0; x < 16; x++) { |
| 362 | |
| 363 | y = tileBuffer[(count * 32) + x]; |
| 364 | tileBuffer[(count * 32) + x] = tileBuffer[(count * 32) + 31 - x]; |
| 365 | tileBuffer[(count * 32) + 31 - x] = y; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | } |
| 370 | |
| 371 | flippedTileSet = createSurface(tileBuffer, TTOI(1), TTOI(tiles)); |
| 372 | enableColorKey(flippedTileSet, 0); |
| 373 | |
| 374 | delete[] tileBuffer; |
| 375 | |
| 376 | |
| 377 | // Load mask |
| 378 | |
| 379 | mask = new char[tiles << 10]; |
| 380 | |
| 381 | // Unpack bits |
| 382 | for (count = 0; count < tiles; count++) { |
| 383 | |
| 384 | for (y = 0; y < 32; y++) { |
| 385 | |
| 386 | for (x = 0; x < 32; x++) |
| 387 | mask[(count << 10) + (y << 5) + x] = (dBuffer[createInt(aBuffer + 1028 + (maxTiles * 18) + (count << 2)) + (y << 2) + (x >> 3)] >> (x & 7)) & 1; |
| 388 | |
| 389 | } |
| 390 | |
| 391 | } |
| 392 | |
| 393 | flippedMask = new char[tiles << 10]; |
| 394 | |
| 395 | // Unpack bits |
| 396 | for (count = 0; count < tiles; count++) { |
| 397 | |
| 398 | for (y = 0; y < 32; y++) { |
| 399 | |
| 400 | for (x = 0; x < 32; x++) |
| 401 | flippedMask[(count << 10) + (y << 5) + x] = (dBuffer[createInt(aBuffer + 1028 + (maxTiles * 22) + (count << 2)) + (y << 2) + (x >> 3)] >> (x & 7)) & 1; |
| 402 | |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
| 407 | delete[] dBuffer; |
| 408 | delete[] bBuffer; |
| 409 | delete[] aBuffer; |
| 410 | |
| 411 | |
| 412 | /* Uncomment the code below if you want to see the mask instead of the tile |
| 413 | graphics during gameplay */ |
| 414 | |
| 415 | /*if (SDL_MUSTLOCK(tileSet)) SDL_LockSurface(tileSet); |
| 416 | if (SDL_MUSTLOCK(flippedTileSet)) SDL_LockSurface(flippedTileSet); |
| 417 | |
| 418 | for (count = 0; count < tiles; count++) { |
| 419 | |
| 420 | for (y = 0; y < 32; y++) { |
| 421 | |
| 422 | for (x = 0; x < 32; x++) { |
| 423 | |
| 424 | if (mask[(count << 10) + (y << 5) + x] == 1) |
| 425 | ((char *)(tileSet->pixels))[(count << 10) + (y << 5) + x] = 43; |
| 426 | |
| 427 | if (flippedMask[(count << 10) + (y << 5) + x] == 1) |
| 428 | ((char *)(flippedTileSet->pixels))[(count << 10) + (y << 5) + x] = 88; |
| 429 | |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | } |
| 435 | |
| 436 | if (SDL_MUSTLOCK(tileSet)) SDL_UnlockSurface(tileSet); |
| 437 | if (SDL_MUSTLOCK(flippedTileSet)) SDL_UnlockSurface(flippedTileSet);*/ |
| 438 | |
| 439 | |
| 440 | return tiles | (maxTiles << 16); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * Create an event. |
| 447 | * |
| 448 | * @param x X-coordinate of the new event |
| 449 | * @param y Y-coordinate of the new event |
| 450 | * @param data Event parameters |
| 451 | */ |
| 452 | void JJ2Level::createEvent (int x, int y, const unsigned char* data) { |
| 453 | |
| 454 | unsigned char type; |
| 455 | int properties; |
| 456 | |
| 457 | type = *data; |
| 458 | properties = (data[1] >> 4) + (data[2] << 4) + (data[3] << 12); |
| 459 | |
| 460 | if ((type < 33) || ((type >= 206) && (type <= 208)) || (type == 230) || (type == 240) || (type == 245)) { |
| 461 | |
| 462 | mods[y][x].type = type; |
| 463 | mods[y][x].properties = properties; |
| 464 | |
| 465 | return; |
| 466 | |
| 467 | } |
| 468 | |
| 469 | mods[y][x].type = 0; |
| 470 | |
| 471 | if (type <= 40) { |
| 472 | |
| 473 | events = new AmmoJJ2Event(events, x, y, type, TSF); |
| 474 | |
| 475 | } else if ((type >= 44) && (type <= 45)) { |
| 476 | |
| 477 | events = new CoinGemJJ2Event(events, x, y, type, TSF); |
| 478 | |
| 479 | } else if (type == 60) { |
| 480 | |
| 481 | events = new SpringJJ2Event(events, x, y, type, TSF, properties); |
| 482 | |
| 483 | } else if (type == 62) { |
| 484 | |
| 485 | events = new SpringJJ2Event(events, x, y, type, TSF, properties); |
| 486 | |
| 487 | } else if ((type >= 63) && (type <= 66)) { |
| 488 | |
| 489 | events = new CoinGemJJ2Event(events, x, y, type, TSF); |
| 490 | |
| 491 | } else if ((type >= 72) && (type <= 73)) { |
| 492 | |
| 493 | events = new FoodJJ2Event(events, x, y, type, TSF); |
| 494 | |
| 495 | } else if (type == 80) { |
| 496 | |
| 497 | events = new FoodJJ2Event(events, x, y, type, TSF); |
| 498 | |
| 499 | } else if ((type >= 85) && (type <= 87)) { |
| 500 | |
| 501 | events = new SpringJJ2Event(events, x, y, type, TSF, properties); |
| 502 | |
| 503 | } else if ((type >= 141) && (type <= 147)) { |
| 504 | |
| 505 | events = new FoodJJ2Event(events, x, y, type, TSF); |
| 506 | |
| 507 | } else if ((type >= 154) && (type <= 182)) { |
| 508 | |
| 509 | events = new FoodJJ2Event(events, x, y, type, TSF); |
| 510 | |
| 511 | } else { |
| 512 | |
| 513 | events = new OtherJJ2Event(events, x, y, type, TSF, properties); |
| 514 | |
| 515 | } |
| 516 | |
| 517 | } |
| 518 | |
| 519 | |
| 520 | /** |
| 521 | * Load the level. |
| 522 | * |
| 523 | * @param fileName Name of the file containing the level data |
| 524 | * @param checkpoint Whether or not the player(s) will start at a checkpoint |
| 525 | * |
| 526 | * @return Error code |
| 527 | */ |
| 528 | int JJ2Level::load (char *fileName, bool checkpoint) { |
| 529 | |
| 530 | Anim* pAnims[JJ2PANIMS]; |
| 531 | Anim* pFlippedAnims[JJ2PANIMS]; |
| 532 | File *file; |
| 533 | char *string; |
| 534 | unsigned char* aBuffer; |
| 535 | unsigned char* bBuffer; |
| 536 | unsigned char* cBuffer; |
| 537 | unsigned char* dBuffer; |
| 538 | int aCLength, bCLength, cCLength, dCLength; |
| 539 | int aLength, bLength, cLength, dLength; |
| 540 | int tiles; |
| 541 | int count, x, y, ret; |
| 542 | unsigned char tileQuad[8]; |
| 543 | short int* quadRefs; |
| 544 | int width, height; |
| 545 | unsigned char startX, startY; |
| 546 | |
| 547 | // Thanks to Neobeo for working out the most of the .j2l format |
| 548 | |
| 549 | |
| 550 | try { |
| 551 | |
| 552 | font = new Font(false); |
| 553 | |
| 554 | } catch (int e) { |
| 555 | |
| 556 | throw; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | |
| 561 | // Open JJ2 level file |
| 562 | |
| 563 | try { |
| 564 | |
| 565 | file = new File(fileName, PATH_TYPE_GAME); |
| 566 | |
| 567 | } catch (int e) { |
| 568 | |
| 569 | delete font; |
| 570 | return e; |
| 571 | |
| 572 | } |
| 573 | |
| 574 | // Load level name |
| 575 | file->seek(188, true); |
| 576 | |
| 577 | string = reinterpret_cast<char *>(file->loadBlock(32)); |
| 578 | |
| 579 | |
| 580 | // Show loading screen |
| 581 | |
| 582 | video.setPalette(menuPalette); |
| 583 | |
| 584 | video.clearScreen(0); |
| 585 | |
| 586 | x = (canvasW >> 1) - ((strlen(string) + 6) << 2); |
| 587 | x = fontmn2->showString("LOADING ", x - 60, (canvasH >> 1) - 16); |
| 588 | fontmn2->showString(string, x, (canvasH >> 1) - 16); |
| 589 | |
| 590 | delete[] string; |
| 591 | |
| 592 | if (::loop(NORMAL_LOOP) == E_QUIT) return E_QUIT; |
| 593 | |
| 594 | |
| 595 | // Skip to compressed block lengths |
| 596 | file->seek(230, true); |
| 597 | aCLength = file->loadInt(); |
| 598 | aLength = file->loadInt(); |
| 599 | bCLength = file->loadInt(); |
| 600 | bLength = file->loadInt(); |
| 601 | cCLength = file->loadInt(); |
| 602 | cLength = file->loadInt(); |
| 603 | dCLength = file->loadInt(); |
| 604 | dLength = file->loadInt(); |
| 605 | |
| 606 | aBuffer = file->loadLZ(aCLength, aLength); |
| 607 | bBuffer = file->loadLZ(bCLength, bLength); |
| 608 | cBuffer = file->loadLZ(cCLength, cLength); |
| 609 | dBuffer = file->loadLZ(dCLength, dLength); |
| 610 | |
| 611 | delete file; |
| 612 | |
| 613 | // Load tile set from given file |
| 614 | |
| 615 | ret = loadTiles(reinterpret_cast<char *>(aBuffer + 51)); |
| 616 | |
| 617 | if (ret < 0) { |
| 618 | |
| 619 | delete[] dBuffer; |
| 620 | delete[] cBuffer; |
| 621 | delete[] bBuffer; |
| 622 | delete[] aBuffer; |
| 623 | |
| 624 | delete font; |
| 625 | |
| 626 | return ret; |
| 627 | |
| 628 | } |
| 629 | |
| 630 | TSF = ret >> 28; |
| 631 | tiles = ret & 0xFFFF; |
| 632 | |
| 633 | |
| 634 | // Next level |
| 635 | string = reinterpret_cast<char *>(aBuffer + 115); |
| 636 | |
| 637 | if (fileExists(string, PATH_TYPE_GAME)) nextLevel = createString(string); |
| 638 | else nextLevel = createString(string, ".j2l"); |
| 639 | |
| 640 | |
| 641 | // Music file |
| 642 | string = reinterpret_cast<char *>(aBuffer + 179); |
| 643 | |
| 644 | if (fileExists(string, PATH_TYPE_GAME)) musicFile = createString(string); |
| 645 | else musicFile = createString(string, ".j2b"); |
| 646 | |
| 647 | |
| 648 | // Create layers |
| 649 | |
| 650 | quadRefs = reinterpret_cast<short int *>(dBuffer); |
| 651 | |
| 652 | for (count = 0; count < LAYERS; count++) { |
| 653 | |
| 654 | int flags = aBuffer[8403 + (count << 2)]; |
| 655 | width = createInt(aBuffer + 8403 + 48 + (count << 2)); |
| 656 | int pitch = createInt(aBuffer + 8403 + 80 + (count << 2)); |
| 657 | height = createInt(aBuffer + 8403 + 112 + (count << 2)); |
| 658 | fixed xSpeed = createInt(aBuffer + 8403 + 248 + (count << 2)) >> 6; |
| 659 | fixed ySpeed = createInt(aBuffer + 8403 + 280 + (count << 2)) >> 6; |
| 660 | |
| 661 | pitch = (pitch + 3) >> 2; |
| 662 | |
| 663 | if (aBuffer[8403 + 40 + count]) { |
| 664 | |
| 665 | layers[count] = new JJ2Layer(flags, width, height, xSpeed, ySpeed); |
| 666 | |
| 667 | for (y = 0; y < height; y++) { |
| 668 | |
| 669 | for (x = 0; x < width; x++) { |
| 670 | |
| 671 | if ((x & 3) == 0) memcpy(tileQuad, cBuffer + (quadRefs[x >> 2] << 3), 8); |
| 672 | |
| 673 | layers[count]->setTile(x, y, createShort(tileQuad + ((x & 3) << 1)), TSF, tiles); |
| 674 | |
| 675 | } |
| 676 | |
| 677 | quadRefs += pitch; |
| 678 | |
| 679 | } |
| 680 | |
| 681 | } else { |
| 682 | |
| 683 | // No tile data |
| 684 | |
| 685 | layers[count] = new JJ2Layer(); |
| 686 | |
| 687 | } |
| 688 | |
| 689 | } |
| 690 | |
| 691 | layer = layers[3]; |
| 692 | width = layer->getWidth(); |
| 693 | height = layer->getHeight(); |
| 694 | |
| 695 | |
| 696 | // Load events |
| 697 | startX = 1; |
| 698 | startY = 1; |
| 699 | |
| 700 | mods = new JJ2Modifier *[height]; |
| 701 | *mods = new JJ2Modifier[width * height]; |
| 702 | |
| 703 | events = NULL; |
| 704 | |
| 705 | for (y = 0; y < height; y++) { |
| 706 | |
| 707 | mods[y] = *mods + (y * width); |
| 708 | |
| 709 | for (x = 0; x < width; x++) { |
| 710 | |
| 711 | // Create event or assign modifier |
| 712 | createEvent(x, y, bBuffer + (((y * width) + x) << 2)); |
| 713 | |
| 714 | if (mods[y][x].type == 29) { |
| 715 | |
| 716 | // Jazz start pos |
| 717 | |
| 718 | startX = x; |
| 719 | startY = y; |
| 720 | |
| 721 | } |
| 722 | |
| 723 | } |
| 724 | |
| 725 | } |
| 726 | |
| 727 | delete[] dBuffer; |
| 728 | delete[] cBuffer; |
| 729 | delete[] bBuffer; |
| 730 | delete[] aBuffer; |
| 731 | |
| 732 | |
| 733 | // Load anims from anims.j2a |
| 734 | |
| 735 | ret = loadSprites(); |
| 736 | |
| 737 | if (ret < 0) { |
| 738 | |
| 739 | if (events) delete events; |
| 740 | delete[] *mods; |
| 741 | delete[] mods; |
| 742 | |
| 743 | for (x = 0; x < LAYERS; x++) delete layers[x]; |
| 744 | |
| 745 | delete[] flippedMask; |
| 746 | delete[] mask; |
| 747 | |
| 748 | delete[] musicFile; |
| 749 | delete[] nextLevel; |
| 750 | |
| 751 | SDL_FreeSurface(flippedTileSet); |
| 752 | SDL_FreeSurface(tileSet); |
| 753 | |
| 754 | delete font; |
| 755 | |
| 756 | return ret; |
| 757 | |
| 758 | } |
| 759 | |
| 760 | |
| 761 | // Set initial water level |
| 762 | waterLevelTarget = TTOF(layer->getHeight() + 1); |
| 763 | waterLevel = waterLevelTarget - F8; |
| 764 | waterLevelSpeed = 0; |
| 765 | |
| 766 | |
| 767 | // Generate player's animation set references |
| 768 | |
| 769 | if (multiplayer) { |
| 770 | |
| 771 | string = new char[MTL_P_ANIMS + 1]; |
| 772 | |
| 773 | string[0] = MTL_P_ANIMS + 1; |
| 774 | string[1] = MT_P_ANIMS; |
| 775 | string[2] = 0; |
| 776 | string[3] = 0; |
| 777 | |
| 778 | game->send(reinterpret_cast<unsigned char *>(string)); |
| 779 | |
| 780 | delete[] string; |
| 781 | |
| 782 | } |
| 783 | |
| 784 | // Set the players' initial values |
| 785 | |
| 786 | if (TSF) { |
| 787 | |
| 788 | for (count = 0; count < JJ2PANIMS; count++) { |
| 789 | |
| 790 | playerAnims[count] = count; |
| 791 | pAnims[count] = animSets[55] + count; |
| 792 | pFlippedAnims[count] = flippedAnimSets[55] + count; |
| 793 | |
| 794 | } |
| 795 | |
| 796 | } else { |
| 797 | |
| 798 | playerAnims[JJ2PA_BOARD] = 1; |
| 799 | playerAnims[JJ2PA_BOARDSW] = 2; |
| 800 | playerAnims[JJ2PA_STOMP] = 3; |
| 801 | playerAnims[JJ2PA_DEAD] = 4; |
| 802 | playerAnims[JJ2PA_DIE] = 5; |
| 803 | playerAnims[JJ2PA_CROUCH1] = 6; |
| 804 | playerAnims[JJ2PA_CROUCHED] = 7; |
| 805 | playerAnims[JJ2PA_CROUCHSHOOT] = 8; |
| 806 | playerAnims[JJ2PA_CROUCH2] = 9; |
| 807 | playerAnims[JJ2PA_VINE] = 11; |
| 808 | playerAnims[JJ2PA_EXIT1] = 12; |
| 809 | playerAnims[JJ2PA_FALL] = 13; |
| 810 | playerAnims[JJ2PA_STOMPING] = 14; |
| 811 | playerAnims[JJ2PA_LAND] = 15; |
| 812 | playerAnims[JJ2PA_STANDSHOOT] = 16; |
| 813 | playerAnims[JJ2PA_STANDSHOOTUP] = 17; |
| 814 | playerAnims[JJ2PA_WHIP1] = 18; |
| 815 | playerAnims[JJ2PA_UNFROG] = 19; |
| 816 | playerAnims[JJ2PA_HOOKWHIP] = 21; |
| 817 | playerAnims[JJ2PA_HOOKDIAG] = 22; |
| 818 | playerAnims[JJ2PA_HOOKSHOOTUP] = 23; |
| 819 | playerAnims[JJ2PA_HOOK1] = 24; |
| 820 | playerAnims[JJ2PA_HOOK2] = 25; |
| 821 | playerAnims[JJ2PA_HOOKWHIPUP] = 26; |
| 822 | playerAnims[JJ2PA_HOOKSHOOT] = 27; |
| 823 | playerAnims[JJ2PA_HELI] = 28; |
| 824 | playerAnims[JJ2PA_HELIWHIP] = 29; |
| 825 | playerAnims[JJ2PA_HELISHOOT] = 30; |
| 826 | playerAnims[JJ2PA_HPOLE] = 31; |
| 827 | playerAnims[JJ2PA_HURT] = 32; |
| 828 | playerAnims[JJ2PA_WAIT1] = 33; |
| 829 | playerAnims[JJ2PA_WAIT2] = 34; |
| 830 | playerAnims[JJ2PA_WAIT3] = 35; |
| 831 | playerAnims[JJ2PA_WAIT4] = 36; |
| 832 | playerAnims[JJ2PA_WAIT5] = 37; |
| 833 | playerAnims[JJ2PA_FALLWHIP] = 38; |
| 834 | playerAnims[JJ2PA_FALLSHOOT] = 39; |
| 835 | playerAnims[JJ2PA_FLOAT1] = 40; |
| 836 | playerAnims[JJ2PA_FLOAT2] = 41; |
| 837 | playerAnims[JJ2PA_UP1] = 42; |
| 838 | playerAnims[JJ2PA_EDGE] = 43; |
| 839 | playerAnims[JJ2PA_CARRY] = 44; |
| 840 | playerAnims[JJ2PA_UNLOAD] = 45; |
| 841 | playerAnims[JJ2PA_LOAD] = 46; |
| 842 | playerAnims[JJ2PA_LOOKUP] = 47; |
| 843 | playerAnims[JJ2PA_WOOZYWALK] = 55; |
| 844 | playerAnims[JJ2PA_PUSH] = 56; |
| 845 | playerAnims[JJ2PA_WHIP2] = 57; |
| 846 | playerAnims[JJ2PA_EXIT2] = 58; |
| 847 | playerAnims[JJ2PA_SPEED1] = 59; |
| 848 | playerAnims[JJ2PA_SPEED2] = 60; |
| 849 | playerAnims[JJ2PA_FALLMOVE] = 61; |
| 850 | playerAnims[JJ2PA_JUMP] = 63; |
| 851 | playerAnims[JJ2PA_BALL] = 67; |
| 852 | playerAnims[JJ2PA_WALKSHOOT] = 68; |
| 853 | playerAnims[JJ2PA_RUN] = 70; |
| 854 | playerAnims[JJ2PA_SPEEDRUN] = 71; |
| 855 | playerAnims[JJ2PA_STOP1] = 72; |
| 856 | playerAnims[JJ2PA_MYSTERY] = 73; |
| 857 | playerAnims[JJ2PA_STOP2] = 74; |
| 858 | playerAnims[JJ2PA_UP2] = 75; |
| 859 | playerAnims[JJ2PA_STAND] = 76; |
| 860 | playerAnims[JJ2PA_POWER] = 77; |
| 861 | playerAnims[JJ2PA_POWEREND] = 78; |
| 862 | playerAnims[JJ2PA_POWERSTART] = 79; |
| 863 | playerAnims[JJ2PA_WOOZYSTAND] = 80; |
| 864 | playerAnims[JJ2PA_SWIMDOWN] = 81; |
| 865 | playerAnims[JJ2PA_SWIM] = 82; |
| 866 | playerAnims[JJ2PA_SWIMDIAGDOWN] = 83; |
| 867 | playerAnims[JJ2PA_SWIMDIAGUP] = 84; |
| 868 | playerAnims[JJ2PA_SWIMUP] = 85; |
| 869 | playerAnims[JJ2PA_VINESDIAG] = 86; |
| 870 | playerAnims[JJ2PA_WARPOUT] = 87; |
| 871 | playerAnims[JJ2PA_WARPFALLIN] = 88; |
| 872 | playerAnims[JJ2PA_WARPFALL] = 89; |
| 873 | playerAnims[JJ2PA_WARPFALLOUT] = 90; |
| 874 | playerAnims[JJ2PA_WARPIN] = 91; |
| 875 | playerAnims[JJ2PA_VPOLE] = 92; |
| 876 | |
| 877 | for (count = 0; count < JJ2PANIMS; count++) { |
| 878 | |
| 879 | pAnims[count] = animSets[54] + playerAnims[count]; |
| 880 | pFlippedAnims[count] = flippedAnimSets[54] + playerAnims[count]; |
| 881 | |
| 882 | } |
| 883 | |
| 884 | } |
| 885 | |
| 886 | |
| 887 | createLevelPlayers(LT_JJ2, pAnims, pFlippedAnims, checkpoint, startX, startY); |
| 888 | |
| 889 | |
| 890 | // And that's us done! |
| 891 | |
| 892 | |
| 893 | // Set the tick at which the level will end, though this is not used |
| 894 | endTime = (5 - game->getDifficulty()) * 2 * 60 * 1000; |
| 895 | |
| 896 | |
| 897 | // Adjust panel fonts to use bonus level palette |
| 898 | panelBigFont->mapPalette(0, 32, 64, 8); |
| 899 | panelSmallFont->mapPalette(75, 5, 64, 8); |
| 900 | |
| 901 | |
| 902 | return E_NONE; |
| 903 | |
| 904 | } |