2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 int offsetX = (int)(engine.playerPosX);
29 int offsetY = (int)(engine.playerPosY);
31 map.offsetX = offsetX;
32 map.offsetY = offsetY;
34 Math::limitInt(&offsetX, 0, ((MAPWIDTH - 40) * BRICKSIZE));
35 Math::limitInt(&offsetY, 0, ((MAPHEIGHT - 30) * BRICKSIZE));
37 int mapx = offsetX >> BRICKSHIFT;
38 int mapy = offsetY >> BRICKSHIFT;
41 for (int x = 0 ; x < 21 ; x++)
43 for (int y = 0 ; y < 16 ; y++)
45 brick = map.data[mapx + x][mapy + y];
47 r.x = ((x * BRICKSIZE) - (offsetX & (BRICKSIZE - 1)));
48 r.y = ((y * BRICKSIZE) - (offsetY & (BRICKSIZE - 1)));
49 r.w = r.h = BRICKSIZE;
51 if ((brick >= MAP_BREAKABLE) && (brick < MAP_WATERANIM))
53 graphics.blit(graphics.tile[brick], r.x, r.y, graphics.screen, false);
56 if ((brick >= MAP_NORESET) && (brick < MAP_DECORATION))
58 graphics.drawRect(r.x, r.y, 32, 4, graphics.yellow, graphics.screen);
66 void drawMapTopLayer()
70 int offsetX = map.offsetX;
71 int offsetY = map.offsetY;
73 Math::limitInt(&offsetX, 0, ((MAPWIDTH - 40) * BRICKSIZE));
74 Math::limitInt(&offsetY, 0, ((MAPHEIGHT - 30) * BRICKSIZE));
76 int mapx = offsetX >> BRICKSHIFT;
77 int mapy = offsetY >> BRICKSHIFT;
81 for (int x = 0 ; x < 21 ; x++)
83 for (int y = 0 ; y < 16 ; y++)
85 r.x = ((x * BRICKSIZE) - (offsetX & (BRICKSIZE - 1)));
86 r.y = ((y * BRICKSIZE) - (offsetY & (BRICKSIZE - 1)));
87 r.w = r.h = BRICKSIZE;
89 brick = map.data[mapx + x][mapy + y];
96 if ((brick < MAP_BREAKABLE) || (brick >= MAP_WATERANIM))
98 if (brick == MAP_WATER)
100 if (map.data[mapx + x][mapy + y + 1] >= MAP_BREAKABLE)
102 addBubble((mapx + x) * BRICKSIZE, (mapy + y) * BRICKSIZE);
106 if (brick == MAP_WATERANIM)
108 brick = graphics.getWaterAnim();
110 else if (brick == MAP_SLIME)
112 brick = graphics.getSlimeAnim();
114 else if ((brick >= MAP_LAVAANIM) && (brick < MAP_TOPLAYER))
116 map.data[mapx + x][mapy + y] = graphics.getLavaAnim(brick);
117 brick = map.data[mapx + x][mapy + y];
120 graphics.blit(graphics.tile[brick], r.x, r.y, graphics.screen, false);
126 void addBlips(List *blipList, int mapX, int mapY, int type)
134 blipType = graphics.getSprite("MIAArrow", true);
135 ent = (Entity*)map.miaList.getHead();
138 blipType = graphics.getSprite("ItemArrow", true);
139 ent = (Entity*)map.itemList.getHead();
142 blipType = graphics.getSprite("EnemyArrow", true);
143 ent = (Entity*)map.enemyList.getHead();
152 while (ent->next != NULL)
154 ent = (Entity*)ent->next;
156 if (ent->health <= 0)
161 if (!requiredEnemy(ent->name))
170 if ((ent->id < ITEM_MISC) || (ent->id == ITEM_MISC_INVISIBLE))
176 x = (int)(ent->x + ent->width) >> BRICKSHIFT;
177 y = (int)(ent->y + ent->height) >> BRICKSHIFT;
185 if ((x >= 165) && (y >= 125) && (x <= 475) && (y <= 355))
187 blip = new RadarBlip();
188 blip->set(x, y, type);
189 blipList->add (blip);
194 graphics.blit(blipType->image[0], 220 + (type * 50), 100, graphics.screen, true);
196 graphics.blit(blipType->image[1], 510, 140 + (type * 50), graphics.screen, true);
198 graphics.blit(blipType->image[2], 220 + (type * 50), 380, graphics.screen, true);
200 graphics.blit(blipType->image[3], 125, 140 + (type * 50), graphics.screen, true);
205 void addMiniMapDoors(SDL_Surface *panel, int mapX, int mapY)
207 Train *train = (Train*)map.trainList.getHead();
210 int width, height, color;
212 while (train->next != NULL)
214 train = (Train*)train->next;
218 color = graphics.white;
220 x = (int)train->x >> BRICKSHIFT;
222 y = (int)train->y >> BRICKSHIFT;
224 if ((x >= mapX) && (x <= mapX + 64) && (y >= mapY) && (y <= mapY + 48))
229 if (train->type == TR_TRAIN)
233 else if ((train->type >= TR_SLIDEDOOR) && (train->type <= TR_BRONZE_SLIDEDOOR))
245 case TR_GOLD_SLIDEDOOR:
246 color = graphics.yellow;
250 case TR_SILVER_SLIDEDOOR:
251 color = graphics.grey;
255 case TR_BRONZE_SLIDEDOOR:
256 color = SDL_MapRGB(graphics.screen->format, 0xff, 0x99, 0x00);
261 case TR_LOCKED_SLIDEDOOR:
262 color = SDL_MapRGB(graphics.screen->format, 0xff, 0x00, 0xff);
266 graphics.drawRect(x * 5, y * 5, width, height, color, panel);
271 void showMap(int centerX, int centerY)
281 int minX = (map.limitLeft >> BRICKSHIFT);
282 int maxX = (map.limitRight >> BRICKSHIFT) - 44;
283 int minY = (map.limitUp >> BRICKSHIFT);
284 int maxY = (map.limitDown >> BRICKSHIFT) - 33;
286 Math::limitInt(&x1, minX, maxX);
287 Math::limitInt(&x2, minX, maxX);
288 Math::limitInt(&y1, minY, maxY);
289 Math::limitInt(&y2, minY, maxY);
291 SDL_FillRect(graphics.screen, NULL, graphics.black);
292 graphics.updateScreen();
295 SDL_Surface *panel = graphics.createSurface(320, 240);
296 SDL_Surface *background = graphics.loadImage("gfx/main/mapBackground.png");
297 SDL_SetAlpha(background, 130);
299 graphics.blit(background, 0, 0, panel, false);
301 int color = graphics.black;
303 for (int y = 0 ; y < 48 ; y++)
305 for (int x = 0 ; x < 64 ; x++)
307 if (map.data[x1 + x][y1 + y] == MAP_WATER)
309 graphics.drawRect(x * 5, y * 5, 5, 5, graphics.blue, panel);
314 addMiniMapDoors(panel, x1, y1);
316 for (int y = 0 ; y < 48 ; y++)
318 for (int x = 0 ; x < 64 ; x++)
320 color = graphics.black;
322 if (map.data[x1 + x][y1 + y] == MAP_AIR)
324 color = graphics.black;
326 else if (map.data[x1 + x][y1 + y] == MAP_WATER)
328 color = graphics.black;
330 else if (map.data[x1 + x][y1 + y] == MAP_SLIME)
332 color = graphics.green;
334 else if (map.data[x1 + x][y1 + y] == MAP_LAVA)
336 color = graphics.red;
338 else if (map.data[x1 + x][y1 + y] >= MAP_DECORATION)
340 color = graphics.black;
342 if (map.data[x1 + x][y1 + y - 1] == MAP_WATER)
344 color = graphics.blue;
346 else if (map.data[x1 + x][y1 + y + 1] == MAP_WATER)
348 color = graphics.blue;
351 else if (map.data[x1 + x][y1 + y] < MAP_WATERANIM)
353 color = graphics.darkGreen;
356 if (color != graphics.black)
358 graphics.drawRect(x * 5, y * 5, 5, 5, color, panel);
363 graphics.blit(background, 0, 0, graphics.screen, false);
364 SDL_FreeSurface(background);
368 RadarBlip *blip = new RadarBlip();
369 blip->set(160 + ((centerX - x1) * 5), 120 + ((centerY - y1) * 5), 0);
372 addBlips(&blipList, x1, y1, 1);
373 addBlips(&blipList, x1, y1, 2);
374 addBlips(&blipList, x1, y1, 3);
376 Sprite *enemySignal = graphics.getSprite("EnemySignal", true);
377 Sprite *miaSignal = graphics.getSprite("MIASignal", true);
378 Sprite *bobSignal = graphics.getSprite("BobSignal", true);
379 Sprite *itemSignal = graphics.getSprite("ItemSignal", true);
381 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
382 graphics.setFontSize(3);
383 graphics.drawString(_(game.stageName), 320, 30, TXT_CENTERED, graphics.screen);
385 graphics.setFontSize(0);
387 graphics.drawRect(160, 414, 7, 7, graphics.yellow, graphics.white, graphics.screen);
388 graphics.drawString(_("MIAs"), 175, 410, TXT_LEFT, graphics.screen);
390 graphics.drawRect(290, 414, 7, 7, graphics.blue, graphics.white, graphics.screen);
391 graphics.drawString(_("Items"), 305, 410, TXT_LEFT, graphics.screen);
393 graphics.drawRect(415, 414, 7, 7, graphics.red, graphics.white, graphics.screen);
394 graphics.drawString(_("Enemies"), 430, 410, TXT_LEFT, graphics.screen);
396 graphics.setFontSize(1);
397 snprintf(string, sizeof string, "%s - %.2d:%.2d:%.2d", _("Mission Time"), game.currentMissionHours, game.currentMissionMinutes, game.currentMissionSeconds);
398 graphics.drawString(string, 320, 60, TXT_CENTERED, graphics.screen);
399 graphics.drawString(_("Press Button to Continue..."), 320, 450, TXT_CENTERED, graphics.screen);
410 graphics.updateScreen();
411 graphics.animateSprites();
413 graphics.drawRect(160, 120, 320, 240, graphics.black, graphics.white, graphics.screen);
414 graphics.blit(panel, 160, 120, graphics.screen, false);
416 if ((config.isControl(CONTROL::MAP)) || (config.isControl(CONTROL::PAUSE)) || (engine.keyState[SDL_SCANCODE_ESCAPE]))
421 blip = (RadarBlip*)blipList.getHead();
423 while (blip->next != NULL)
425 blip = (RadarBlip*)blip->next;
430 graphics.blit(bobSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
433 graphics.blit(miaSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
436 graphics.blit(itemSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
439 graphics.blit(enemySignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
449 SDL_FillRect(graphics.screen, NULL, graphics.black);
450 graphics.updateScreen();
457 void evaluateMapAttribute(Entity *ent, int mapAttribute)
459 switch (mapAttribute)
464 case MAP_AIR_CEILING_1:
465 mapAttribute = MAP_AIR;
468 if (map.isCavesTileset)
470 mapAttribute = MAP_AIR;
473 case MAP_AIR_CEILING_2:
474 if (map.isGrasslandsTileset)
476 mapAttribute = MAP_AIR;
478 else if (map.isCavesTileset)
480 mapAttribute = MAP_WATER;
484 if (map.isCavesTileset)
486 mapAttribute = MAP_WATER;
491 switch (mapAttribute)
494 if ((ent->environment != ENV_AIR) && (!(ent->flags & ENT_INANIMATE)))
496 if (!(ent->flags & ENT_SWIMS))
500 ent->dy = PLAYER_JUMP_SPEED;
505 if ((ent == &player) && ((game.hasAquaLung) || (engine.cheatExtras)))
507 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
511 ent->environment = ENV_AIR;
515 audio.playSound(SND_WATEROUT, CH_TOUCH, ent->x);
518 ent->checkEnvironment();
528 ent->falling = false;
530 if (ent->environment == ENV_AIR)
532 audio.playSound(SND_WATERIN, CH_TOUCH, ent->x);
533 if ((mapAttribute == MAP_SLIME) || (mapAttribute == MAP_LAVA))
537 // On ice levels water is harmful (because it's very very cold!)
538 if ((map.isIceLevel) && (mapAttribute == MAP_WATER))
540 mapAttribute = MAP_LAVA;
544 if (mapAttribute == MAP_WATER)
546 ent->environment = ENV_WATER;
547 if ((ent == &player) && ((game.hasAquaLung) || (engine.cheatExtras)))
549 player.setSprites(graphics.getSprite("AquaBobRight", true), graphics.getSprite("AquaBobLeft", true), graphics.getSprite("AquaBobSpin", true));
552 else if (mapAttribute == MAP_SLIME)
554 ent->environment = ENV_SLIME;
556 else if (mapAttribute == MAP_LAVA)
558 ent->environment = ENV_LAVA;
564 void raiseWaterLevel()
566 if (map.waterLevel == map.requiredWaterLevel)
571 int y = (int)map.waterLevel;
573 if ((int)map.waterLevel != map.requiredWaterLevel)
575 for (int x = 0 ; x < MAPWIDTH ; x++)
577 if ((map.data[x][y] == MAP_AIR) || (map.isLiquid(x, y)))
579 map.data[x][y] = MAP_WATER;
582 if ((map.data[x][y] >= MAP_DECORATION) && (map.data[x][y] <= MAP_WATERANIM))
584 map.data[x][y] = MAP_WATER;
587 if (map.data[x][y] >= MAP_TOPLAYER)
589 map.data[x][y] = MAP_WATER;
592 if (map.data[x][y - 1] >= MAP_TOPLAYER)
594 map.data[x][y] = MAP_WATER;
597 if ((map.data[x][y - 1] == MAP_AIR) || (map.isLiquid(x, y - 1)))
599 map.data[x][y - 1] = MAP_WATERANIM;
602 if ((map.data[x][y - 1] >= MAP_DECORATION) && (map.data[x][y - 1] <= MAP_WATERANIM))
604 map.data[x][y - 1] = MAP_WATERANIM;
608 map.waterLevel -= 0.1;
610 int x = (int)(player.x + player.dx) >> BRICKSHIFT;
611 int y = (int)(player.y + player.height - 1) >> BRICKSHIFT;
613 int mapAttribute = map.data[x][y];
615 if ((mapAttribute == MAP_WATER) && (player.environment == MAP_AIR))
617 evaluateMapAttribute(&player, mapAttribute);
624 map.windChangeTime--;
626 if (map.windChangeTime <= 0)
630 if (Math::rrand(0, 1) == 0)
631 map.windPower = Math::rrand(-3, 3);
634 map.windChangeTime = Math::rrand(60, 600);
640 void parseMapDataLine(const char *line, int y)
647 sscanf(line, "%d", &tileIndex);
649 map.data[x][y] = tileIndex;
666 bool loadMapData(const char *filename)
670 if (!engine.loadData(filename))
671 graphics.showErrorAndExit("The requested map '%s' was not found.", filename);
673 char *token = strtok((char*)engine.dataBuffer, "\n");
674 parseMapDataLine(token, 0);
680 token = strtok(NULL, "\n");
682 parseMapDataLine(token, y);