| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1levelframe.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 | * - 30th March 2010: Created baselevel.cpp from parts of level.cpp and |
| 13 | * levelframe.cpp |
| 14 | * - 29th June 2010: Created jj2levelframe.cpp from parts of levelframe.cpp |
| 15 | * - 1st August 2012: Renamed levelframe.cpp to jj1levelframe.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2013 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 | * Provides the once-per-frame functions for levels. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "jj1bullet.h" |
| 30 | #include "event/jj1event.h" |
| 31 | #include "event/jj1guardians.h" |
| 32 | #include "jj1level.h" |
| 33 | #include "jj1levelplayer.h" |
| 34 | |
| 35 | #include "game/game.h" |
| 36 | #include "game/gamemode.h" |
| 37 | #include "io/controls.h" |
| 38 | #include "io/gfx/font.h" |
| 39 | #include "io/gfx/video.h" |
| 40 | #include "util.h" |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Level iteration. |
| 45 | * |
| 46 | * @return Error code |
| 47 | */ |
| 48 | int JJ1Level::step () { |
| 49 | |
| 50 | JJ1Event *event; |
| 51 | int viewH = canvasH; |
| 52 | int x, y; |
| 53 | |
| 54 | if(setup.hudStyle == hudType::Classic) { |
| 55 | // Can we see below the panel? |
| 56 | if (canvasW > SW) viewH = canvasH; |
| 57 | else viewH = canvasH - 33; |
| 58 | } else if(setup.hudStyle == hudType::FPS) { |
| 59 | // Leave space for panel in view height |
| 60 | viewH = canvasH - 33; |
| 61 | } |
| 62 | |
| 63 | // Search for active events |
| 64 | for (y = FTOT(viewY) - 5; y < ITOT(FTOI(viewY) + viewH) + 5; y++) { |
| 65 | |
| 66 | for (x = FTOT(viewX) - 5; x < ITOT(FTOI(viewX) + canvasW) + 5; x++) { |
| 67 | |
| 68 | if ((x >= 0) && (y >= 0) && (x < LW) && (y < LH) && |
| 69 | grid[y][x].event && (grid[y][x].event < 121) && |
| 70 | (+eventSet[grid[y][x].event].difficulty <= +getDifficulty())) { |
| 71 | |
| 72 | event = events; |
| 73 | |
| 74 | while (event) { |
| 75 | |
| 76 | // If the event has been found, stop searching |
| 77 | if (event->isFrom(x, y)) break; |
| 78 | |
| 79 | event = event->getNext(); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | // If the event wasn't found, create it |
| 84 | if (!event) { |
| 85 | |
| 86 | switch (getEvent(x, y)->movement) { |
| 87 | |
| 88 | case 28: |
| 89 | |
| 90 | events = new JJ1Bridge(x, y); |
| 91 | |
| 92 | break; |
| 93 | |
| 94 | case 41: |
| 95 | |
| 96 | events = new MedGuardian(x, y); |
| 97 | |
| 98 | break; |
| 99 | |
| 100 | case 60: |
| 101 | |
| 102 | events = new DeckGuardian(x, y); |
| 103 | |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | |
| 108 | events = new JJ1StandardEvent(eventSet + grid[y][x].event, x, y, TTOF(x), TTOF(y + 1)); |
| 109 | |
| 110 | break; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | |
| 123 | // Process bullets |
| 124 | if (bullets) bullets = bullets->step(ticks); |
| 125 | |
| 126 | // Determine the players' trajectories |
| 127 | for (x = 0; x < nPlayers; x++) players[x].getJJ1LevelPlayer()->control(ticks); |
| 128 | |
| 129 | // Process active events |
| 130 | if (events) events = events->step(ticks); |
| 131 | |
| 132 | // Apply as much of those trajectories as possible, without going into the |
| 133 | // scenery |
| 134 | for (x = 0; x < nPlayers; x++) players[x].getJJ1LevelPlayer()->move(ticks); |
| 135 | |
| 136 | |
| 137 | // Check if time has run out |
| 138 | if (ticks > endTime) { |
| 139 | |
| 140 | if (multiplayer) { |
| 141 | |
| 142 | game->getMode()->outOfTime(); |
| 143 | |
| 144 | } else { |
| 145 | |
| 146 | if ((getDifficulty() == difficultyType::Hard || |
| 147 | getDifficulty() == difficultyType::Turbo) |
| 148 | && (stage == LS_NORMAL)) |
| 149 | localPlayer->getJJ1LevelPlayer()->kill(nullptr, endTime); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | |
| 156 | // Handle change in ammo selection |
| 157 | |
| 158 | x = localPlayer->getAmmoType() + 1; |
| 159 | |
| 160 | if (x != ammoType) { |
| 161 | |
| 162 | // Change the ammo type display on the panel |
| 163 | ammoType = x; |
| 164 | ammoOffset = ITOF(26); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | if (ammoOffset > 0) { |
| 169 | |
| 170 | // Descending |
| 171 | ammoOffset -= F1; |
| 172 | |
| 173 | // Avoid an offset of 0, which prevents changes |
| 174 | if (ammoOffset == 0) ammoOffset = -1; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | // Handle change in water level |
| 180 | if (waterLevel < waterLevelTarget) waterLevelSpeed += 3200; |
| 181 | else waterLevelSpeed -= 3200; |
| 182 | if (waterLevelSpeed > 80000) waterLevelSpeed = 80000; |
| 183 | if (waterLevelSpeed < -80000) waterLevelSpeed = -80000; |
| 184 | |
| 185 | waterLevel += waterLevelSpeed >> 6; |
| 186 | |
| 187 | |
| 188 | // Handle player reactions |
| 189 | for (x = 0; x < nPlayers; x++) { |
| 190 | |
| 191 | if (players[x].getJJ1LevelPlayer()->reacted(ticks) == PR_KILLED) { |
| 192 | |
| 193 | players[x].clearAmmo(); |
| 194 | |
| 195 | if (!multiplayer) return LOST; |
| 196 | |
| 197 | game->resetPlayer(players + x); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | |
| 204 | return E_NONE; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Draw the level. |
| 212 | */ |
| 213 | void JJ1Level::draw () { |
| 214 | |
| 215 | GridElement *ge; |
| 216 | SDL_Rect src, dst; |
| 217 | int viewH = canvasH; |
| 218 | int vX, vY; |
| 219 | int x, y; |
| 220 | unsigned int change; |
| 221 | |
| 222 | |
| 223 | // Calculate change since last step |
| 224 | change = getTimeChange(); |
| 225 | |
| 226 | |
| 227 | // Calculate viewport |
| 228 | if (game && (stage == LS_END)) game->view(paused? 0: ((ticks - prevTicks) * 160)); |
| 229 | else localPlayer->getJJ1LevelPlayer()->view(ticks, paused? 0: (ticks - prevTicks), change); |
| 230 | |
| 231 | if(setup.hudStyle == hudType::Classic) { |
| 232 | // Can we see below the panel? |
| 233 | if (canvasW > SW) viewH = canvasH; |
| 234 | else viewH = canvasH - 33; |
| 235 | } else if(setup.hudStyle == hudType::FPS) { |
| 236 | // Leave space for panel in view height |
| 237 | viewH = canvasH - 33; |
| 238 | } |
| 239 | |
| 240 | // Ensure the new viewport is within the level |
| 241 | if (FTOI(viewX) + canvasW >= TTOI(LW)) viewX = ITOF(TTOI(LW) - canvasW); |
| 242 | if (viewX < 0) viewX = 0; |
| 243 | if (FTOI(viewY) + viewH >= TTOI(LH)) viewY = ITOF(TTOI(LH) - viewH); |
| 244 | if (viewY < 0) viewY = 0; |
| 245 | |
| 246 | // Use the viewport |
| 247 | dst.x = 0; |
| 248 | dst.y = 0; |
| 249 | vX = FTOI(viewX); |
| 250 | vY = FTOI(viewY); |
| 251 | dst.w = canvasW; |
| 252 | dst.h = viewH; |
| 253 | video.setClipRect(canvas, &dst); |
| 254 | |
| 255 | |
| 256 | // Set tile drawing dimensions |
| 257 | src.w = TTOI(1); |
| 258 | src.h = TTOI(1); |
| 259 | src.x = 0; |
| 260 | |
| 261 | |
| 262 | // If there is a sky, draw it |
| 263 | if (sky) { |
| 264 | |
| 265 | // Background scale |
| 266 | int bgScale; |
| 267 | if (canvasW > SW) bgScale = ((canvasH - 1) / 100) + 1; |
| 268 | else bgScale = ((canvasH - 34) / 100) + 1; |
| 269 | |
| 270 | for (y = 0; y < viewH; y += bgScale) |
| 271 | video.drawRect(0, y, canvasW, bgScale, 156 + (y / bgScale)); |
| 272 | |
| 273 | |
| 274 | // Show sun / moon / etc. |
| 275 | if (skyOrb) { |
| 276 | |
| 277 | dst.x = ((canvasW * 4) / 5) - (vX & 3); |
| 278 | dst.y = ((canvasH - 33) * 3) / 25; |
| 279 | src.y = TTOI(skyOrb + (vX & 3)); |
| 280 | SDL_BlitSurface(tileSet, &src, canvas, &dst); |
| 281 | |
| 282 | } |
| 283 | |
| 284 | } else { |
| 285 | |
| 286 | // If there is no sky, draw a blank background |
| 287 | // This is only very occasionally actually visible |
| 288 | video.clearScreen(127); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | // Show background tiles |
| 295 | |
| 296 | for (y = 0; y <= ITOT(viewH - 1) + 1; y++) { |
| 297 | |
| 298 | for (x = 0; x <= ITOT(canvasW - 1) + 1; x++) { |
| 299 | |
| 300 | if ((x + ITOT(vX) >= 256) || (y + ITOT(vY) >= 64)) { |
| 301 | |
| 302 | video.drawRect(TTOI(x) - (vX & 31), TTOI(y) - (vY & 31), 32, 32, LEVEL_BLACK); |
| 303 | |
| 304 | continue; |
| 305 | |
| 306 | } |
| 307 | |
| 308 | // Get the grid element from the given coordinates |
| 309 | ge = grid[y + ITOT(vY)] + x + ITOT(vX); |
| 310 | |
| 311 | // If this tile uses a black background, draw it |
| 312 | if (ge->bg) |
| 313 | video.drawRect(TTOI(x) - (vX & 31), TTOI(y) - (vY & 31), 32, 32, LEVEL_BLACK); |
| 314 | |
| 315 | |
| 316 | // If this is not a foreground tile, draw it |
| 317 | if ((ge->event != 124) && |
| 318 | (ge->event != 125) && |
| 319 | (eventSet[ge->event].movement != 37) && |
| 320 | (eventSet[ge->event].movement != 38)) { |
| 321 | |
| 322 | dst.x = TTOI(x) - (vX & 31); |
| 323 | dst.y = TTOI(y) - (vY & 31); |
| 324 | src.y = TTOI(ge->tile); |
| 325 | SDL_BlitSurface(tileSet, &src, canvas, &dst); |
| 326 | |
| 327 | } |
| 328 | |
| 329 | } |
| 330 | |
| 331 | } |
| 332 | |
| 333 | |
| 334 | // Show active events |
| 335 | if (events) events->draw(ticks, change); |
| 336 | |
| 337 | |
| 338 | // Show the players |
| 339 | for (x = 0; x < nPlayers; x++) players[x].getJJ1LevelPlayer()->draw(ticks, change); |
| 340 | |
| 341 | |
| 342 | // Show bullets |
| 343 | if (bullets) bullets->draw(change); |
| 344 | |
| 345 | |
| 346 | |
| 347 | // Show foreground tiles |
| 348 | |
| 349 | for (y = 0; y <= ITOT(viewH - 1) + 1; y++) { |
| 350 | |
| 351 | for (x = 0; x <= ITOT(canvasW - 1) + 1; x++) { |
| 352 | |
| 353 | if ((x + ITOT(vX) >= 256) || (y + ITOT(vY) >= 64)) continue; |
| 354 | |
| 355 | // Get the grid element from the given coordinates |
| 356 | ge = grid[y + ITOT(vY)] + x + ITOT(vX); |
| 357 | |
| 358 | // If this is an "animated" foreground tile, draw it |
| 359 | if (ge->event == 123) { |
| 360 | |
| 361 | dst.x = TTOI(x) - (vX & 31); |
| 362 | dst.y = TTOI(y) - (vY & 31); |
| 363 | if (ticks & 64) src.y = TTOI(eventSet[ge->event].multiB); |
| 364 | else src.y = TTOI(eventSet[ge->event].multiA); |
| 365 | SDL_BlitSurface(tileSet, &src, canvas, &dst); |
| 366 | |
| 367 | //video.drawRect(dst.x, dst.y, TTOI(1), TTOI(1), 44, false); |
| 368 | } |
| 369 | |
| 370 | // If this is a foreground tile, draw it |
| 371 | if ((ge->event == 124) || |
| 372 | (ge->event == 125) || |
| 373 | (eventSet[ge->event].movement == 37) || |
| 374 | (eventSet[ge->event].movement == 38)) { |
| 375 | |
| 376 | dst.x = TTOI(x) - (vX & 31); |
| 377 | dst.y = TTOI(y) - (vY & 31); |
| 378 | src.y = TTOI(ge->tile); |
| 379 | SDL_BlitSurface(tileSet, &src, canvas, &dst); |
| 380 | |
| 381 | //video.drawRect(dst.x, dst.y, TTOI(1), TTOI(1), 33, false); |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | |
| 386 | } |
| 387 | |
| 388 | // FIXME: Temporary lines showing the water level |
| 389 | video.drawRect(0, FTOI(waterLevel - viewY), canvasW, 2, 24); |
| 390 | video.drawRect(0, FTOI(waterLevel - viewY) + 3, canvasW, 1, 24); |
| 391 | video.drawRect(0, FTOI(waterLevel - viewY) + 6, canvasW, 1, 24); |
| 392 | video.drawRect(0, FTOI(waterLevel - viewY) + 10, canvasW, 1, 24); |
| 393 | |
| 394 | // Show active guardian's energy bar |
| 395 | if (events) events->drawEnergy(ticks); |
| 396 | |
| 397 | |
| 398 | // If this is a competitive game, draw the score |
| 399 | if (multiplayer) game->getMode()->drawScore(font); |
| 400 | |
| 401 | |
| 402 | // Show panel |
| 403 | |
| 404 | int offsetX = 0; |
| 405 | bool isWide = (canvasW != SW); |
| 406 | |
| 407 | if(setup.hudStyle == hudType::FPS && isWide) { |
| 408 | // center panel |
| 409 | offsetX = (canvasW >> 1) - (SW >> 1); |
| 410 | } |
| 411 | |
| 412 | video.setClipRect(canvas, nullptr); |
| 413 | |
| 414 | if (ammoOffset != 0) { |
| 415 | |
| 416 | if (ammoOffset < 0) { |
| 417 | |
| 418 | // Finished descending |
| 419 | ammoOffset = 0; |
| 420 | |
| 421 | } |
| 422 | |
| 423 | src.x = 0; |
| 424 | src.y = FTOI(ammoOffset); |
| 425 | src.w = 64; |
| 426 | src.h = 26 - src.y; |
| 427 | dst.x = 248; |
| 428 | dst.y = 3; |
| 429 | SDL_BlitSurface(panelAmmo[ammoType], &src, panel, &dst); |
| 430 | |
| 431 | } |
| 432 | |
| 433 | // always classic HUD if there is not enough space |
| 434 | if(setup.hudStyle == hudType::Classic || |
| 435 | (setup.hudStyle == hudType::FPS && !isWide)) { |
| 436 | |
| 437 | dst.x = 0; |
| 438 | dst.y = canvasH - 33; |
| 439 | SDL_BlitSurface(panel, nullptr, canvas, &dst); |
| 440 | |
| 441 | // Fill the one missing pixel row at the bottom black |
| 442 | video.drawRect(0, canvasH - 1, SW, 1, LEVEL_BLACK); |
| 443 | } else if (setup.hudStyle == hudType::FPS) { |
| 444 | // quake-style HUD |
| 445 | |
| 446 | dst.y = canvasH - 33; |
| 447 | |
| 448 | for (x = 0; x <= ITOT(offsetX); x++) { |
| 449 | // draw left border |
| 450 | dst.x = TTOI(x); |
| 451 | SDL_BlitSurface(panelBG[0], nullptr, canvas, &dst); |
| 452 | |
| 453 | // draw right border |
| 454 | dst.x = canvasW - TTOI(x + 1); |
| 455 | SDL_BlitSurface(panelBG[1], nullptr, canvas, &dst); |
| 456 | } |
| 457 | |
| 458 | // cut the panel borders and re-center |
| 459 | src.x = 1; // left: 1 white pixel |
| 460 | src.y = 0; |
| 461 | src.w = SW - 8; // right: 3 black pixels + 5 pixels around screws |
| 462 | src.h = TTOI(1); |
| 463 | dst.x = offsetX + 5; |
| 464 | SDL_BlitSurface(panel, &src, canvas, &dst); |
| 465 | |
| 466 | // Fill the one missing pixel row at the bottom black |
| 467 | video.drawRect(0, canvasH - 1, canvasW, 1, LEVEL_BLACK); |
| 468 | |
| 469 | offsetX += 4; // move everything inside right |
| 470 | } |
| 471 | |
| 472 | |
| 473 | // Show panel data |
| 474 | |
| 475 | // Show score |
| 476 | panelSmallFont->showNumber(localPlayer->getScore(), offsetX + 84, canvasH - 27); |
| 477 | |
| 478 | // Show time remaining |
| 479 | if (endTime > ticks) x = endTime - ticks; |
| 480 | else x = 0; |
| 481 | y = x / (60 * 1000); |
| 482 | panelSmallFont->showNumber(y, offsetX + 116, canvasH - 27); |
| 483 | x -= (y * 60 * 1000); |
| 484 | y = x / 1000; |
| 485 | panelSmallFont->showNumber(y, offsetX + 136, canvasH - 27); |
| 486 | x -= (y * 1000); |
| 487 | y = x / 100; |
| 488 | panelSmallFont->showNumber(y, offsetX + 148, canvasH - 27); |
| 489 | |
| 490 | // Show lives |
| 491 | panelSmallFont->showNumber(localPlayer->getLives(), offsetX + 124, canvasH - 13); |
| 492 | |
| 493 | // Show planet number |
| 494 | |
| 495 | if (worldNum <= 41) // Main game levels |
| 496 | panelSmallFont->showNumber((worldNum % 3) + 1, offsetX + 184, canvasH - 13); |
| 497 | else if ((worldNum >= 50) && (worldNum <= 52)) // Christmas levels |
| 498 | panelSmallFont->showNumber(worldNum - 49, offsetX + 184, canvasH - 13); |
| 499 | else panelSmallFont->showNumber(worldNum, offsetX + 184, canvasH - 13); |
| 500 | |
| 501 | // Show level number |
| 502 | panelSmallFont->showNumber(levelNum + 1, offsetX + 196, canvasH - 13); |
| 503 | |
| 504 | // Show ammo |
| 505 | if (localPlayer->getAmmoType() == -1) { |
| 506 | |
| 507 | // Draw "infinity" symbol |
| 508 | panelSmallFont->showString(":", offsetX + 225, canvasH - 13); |
| 509 | panelSmallFont->showString(";", offsetX + 233, canvasH - 13); |
| 510 | |
| 511 | } else { |
| 512 | |
| 513 | x = localPlayer->getAmmo(); |
| 514 | |
| 515 | // Trailing 0s |
| 516 | if (x < 100) { |
| 517 | |
| 518 | panelSmallFont->showNumber(0, offsetX + 229, canvasH - 13); |
| 519 | if (x < 10) panelSmallFont->showNumber(0, offsetX + 237, canvasH - 13); |
| 520 | |
| 521 | } |
| 522 | |
| 523 | panelSmallFont->showNumber(x > 999? 999: x, offsetX + 245, canvasH - 13); |
| 524 | |
| 525 | } |
| 526 | |
| 527 | |
| 528 | // Draw the health bar |
| 529 | |
| 530 | dst.x = 20; |
| 531 | x = localPlayer->getJJ1LevelPlayer()->getEnergy(); |
| 532 | y = (ticks - prevTicks) * 40; |
| 533 | |
| 534 | if (FTOI(energyBar) < x) { |
| 535 | // increase |
| 536 | |
| 537 | if (ITOF(x) - energyBar < y) energyBar = ITOF(x); |
| 538 | else energyBar += y; |
| 539 | |
| 540 | } else if (FTOI(energyBar) > x) { |
| 541 | // decrease |
| 542 | |
| 543 | if (energyBar - ITOF(x) < y) energyBar = ITOF(x); |
| 544 | else energyBar -= y; |
| 545 | |
| 546 | } |
| 547 | |
| 548 | if (energyBar > F1) { |
| 549 | |
| 550 | dst.w = FTOI(energyBar) - 1; |
| 551 | |
| 552 | // Choose energy bar colour |
| 553 | int color; |
| 554 | if (x <= 20) color = 32 + (((ticks / 75) * 4) & 15); // flash |
| 555 | else if (x > 51) color = 24; // blue only before first hit |
| 556 | else if (x > 38) color = 17; // green |
| 557 | else if (x > 25) color = 80; // pink |
| 558 | else if (x > 20) color = 32; // orange is only seen in easy |
| 559 | |
| 560 | // Draw energy bar |
| 561 | video.drawRect(offsetX + dst.x, canvasH - 13, dst.w, 7, color); |
| 562 | |
| 563 | dst.x += dst.w; |
| 564 | dst.w = 64 - dst.w; |
| 565 | |
| 566 | } else dst.w = 64; |
| 567 | |
| 568 | |
| 569 | // Fill in remaining energy bar space with black |
| 570 | video.drawRect(offsetX + dst.x, canvasH - 13, dst.w, 7, LEVEL_BLACK); |
| 571 | } |