]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/map.cpp
Fix minimap rendering.
[quix0rs-blobwars.git] / src / map.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
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.
9
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.
13
14 See the GNU General Public License for more details.
15
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.
19
20 */
21
22 #include "map.h"
23
24 void drawMap()
25 {
26         SDL_Rect r;
27
28         int offsetX = (int)(engine.playerPosX);
29         int offsetY = (int)(engine.playerPosY);
30
31         map.offsetX = offsetX;
32         map.offsetY = offsetY;
33
34         Math::limitInt(&offsetX, 0, ((MAPWIDTH - 40) * BRICKSIZE));
35         Math::limitInt(&offsetY, 0, ((MAPHEIGHT - 30) * BRICKSIZE));
36
37         int mapx = offsetX >> BRICKSHIFT;
38         int mapy = offsetY >> BRICKSHIFT;
39         int brick = 0;
40
41         for (int x = 0 ; x < 21 ; x++)
42         {
43                 for (int y = 0 ; y < 16 ; y++)
44                 {
45                         brick = map.data[mapx + x][mapy + y];
46
47                         r.x = ((x * BRICKSIZE) - (offsetX & (BRICKSIZE - 1)));
48                         r.y = ((y * BRICKSIZE) - (offsetY & (BRICKSIZE - 1)));
49                         r.w = r.h = BRICKSIZE;
50
51                         if ((brick >= MAP_BREAKABLE) && (brick < MAP_WATERANIM))
52                         {
53                                 graphics.blit(graphics.tile[brick], r.x, r.y, graphics.screen, false);
54                                 
55                                 #if DEBUG
56                                 if ((brick >= MAP_NORESET) && (brick < MAP_DECORATION))
57                                 {
58                                         graphics.drawRect(r.x, r.y, 32, 4, graphics.yellow, graphics.screen);
59                                 }
60                                 #endif
61                         }
62                 }
63         }
64 }
65
66 void drawMapTopLayer()
67 {
68         SDL_Rect r;
69
70         int offsetX = map.offsetX;
71         int offsetY = map.offsetY;
72
73         Math::limitInt(&offsetX, 0, ((MAPWIDTH - 40) * BRICKSIZE));
74         Math::limitInt(&offsetY, 0, ((MAPHEIGHT - 30) * BRICKSIZE));
75
76         int mapx = offsetX >> BRICKSHIFT;
77         int mapy = offsetY >> BRICKSHIFT;
78
79         int brick;
80
81         for (int x = 0 ; x < 21 ; x++)
82         {
83                 for (int y = 0 ; y < 16 ; y++)
84                 {
85                         r.x = ((x * BRICKSIZE) - (offsetX & (BRICKSIZE - 1)));
86                         r.y = ((y * BRICKSIZE) - (offsetY & (BRICKSIZE - 1)));
87                         r.w = r.h = BRICKSIZE;
88
89                         brick = map.data[mapx + x][mapy + y];
90
91                         if (brick == 0)
92                         {
93                                 continue;
94                         }
95
96                         if ((brick < MAP_BREAKABLE) || (brick >= MAP_WATERANIM))
97                         {
98                                 if (brick == MAP_WATER)
99                                 {
100                                         if (map.data[mapx + x][mapy + y + 1] >= MAP_BREAKABLE)
101                                         {
102                                                 addBubble((mapx + x) * BRICKSIZE, (mapy + y) * BRICKSIZE);
103                                         }
104                                 }
105
106                                 if (brick == MAP_WATERANIM)
107                                 {
108                                         brick = graphics.getWaterAnim();
109                                 }
110                                 else if (brick == MAP_SLIME)
111                                 {
112                                         brick = graphics.getSlimeAnim();
113                                 }
114                                 else if ((brick >= MAP_LAVAANIM) && (brick < MAP_TOPLAYER))
115                                 {
116                                         map.data[mapx + x][mapy + y] = graphics.getLavaAnim(brick);
117                                         brick = map.data[mapx + x][mapy + y];
118                                 }
119
120                                 graphics.blit(graphics.tile[brick], r.x, r.y, graphics.screen, false);
121                         }
122                 }
123         }
124 }
125
126 void addBlips(List *blipList, int mapX, int mapY, int type)
127 {
128         Sprite *blipType;
129         Entity *ent;
130
131         switch (type)
132         {
133                 case 1:
134                         blipType = graphics.getSprite("MIAArrow", true);
135                         ent = (Entity*)map.miaList.getHead();
136                         break;
137                 case 2:
138                         blipType = graphics.getSprite("ItemArrow", true);
139                         ent = (Entity*)map.itemList.getHead();
140                         break;
141                 case 3:
142                         blipType = graphics.getSprite("EnemyArrow", true);
143                         ent = (Entity*)map.enemyList.getHead();
144                         break;
145                 default:
146                         return;
147         }
148
149         RadarBlip *blip;
150         int x, y;
151
152         while (ent->next != NULL)
153         {
154                 ent = (Entity*)ent->next;
155
156                 if (ent->health <= 0)
157                         continue;
158
159                 if (type == 3)
160                 {
161                         if (!requiredEnemy(ent->name))
162                         {
163                                 continue;
164                         }
165                 }
166
167                 // Items
168                 if (type == 2)
169                 {
170                         if ((ent->id < ITEM_MISC) || (ent->id == ITEM_MISC_INVISIBLE))
171                         {
172                                 continue;
173                         }
174                 }
175
176                 x = (int)(ent->x + ent->width) >> BRICKSHIFT;
177                 y = (int)(ent->y + ent->height) >> BRICKSHIFT;
178
179                 x -= mapX;
180                 y -= mapY;
181
182                 x = (160) + (x * 5);
183                 y = (120) + (y * 5);
184
185                 if ((x >= 165) && (y >= 125) && (x <= 475) && (y <= 355))
186                 {
187                         blip = new RadarBlip();
188                         blip->set(x, y, type);
189                         blipList->add (blip);
190                 }
191                 else
192                 {
193                         if (y < 125)
194                                 graphics.blit(blipType->image[0], 220 + (type * 50), 100, graphics.screen, true);
195                         if (x > 475)
196                                 graphics.blit(blipType->image[1], 510, 140 + (type * 50), graphics.screen, true);
197                         if (y > 355)
198                                 graphics.blit(blipType->image[2], 220 + (type * 50), 380, graphics.screen, true);
199                         if (x < 165)
200                                 graphics.blit(blipType->image[3], 125, 140 + (type * 50), graphics.screen, true);
201                 }
202         }
203 }
204
205 void addMiniMapDoors(SDL_Surface *panel, int mapX, int mapY)
206 {
207         Train *train = (Train*)map.trainList.getHead();
208
209         int x, y;
210         int width, height, color;
211
212         while (train->next != NULL)
213         {
214                 train = (Train*)train->next;
215                 
216                 width = 5;
217                 height = 5;
218                 color = graphics.white;
219                 
220                 x = (int)train->x >> BRICKSHIFT;
221                 
222                 y = (int)train->y >> BRICKSHIFT;
223                 
224                 if ((x >= mapX) && (x <= mapX + 64) && (y >= mapY) && (y <= mapY + 48))
225                 {
226                         x -= mapX;
227                         y -= mapY;
228                         
229                         if (train->type == TR_TRAIN)
230                         {
231                                 width = 10;
232                         }
233                         else if ((train->type >= TR_SLIDEDOOR) && (train->type <= TR_BRONZE_SLIDEDOOR))
234                         {
235                                 width = 10;
236                         }
237                         else
238                         {
239                                 height = 10;
240                         }
241                         
242                         switch (train->type)
243                         {
244                                 case TR_GOLD_DOOR:
245                                 case TR_GOLD_SLIDEDOOR:
246                                         color = graphics.yellow;
247                                         break;
248                                         
249                                 case TR_SILVER_DOOR:
250                                 case TR_SILVER_SLIDEDOOR:
251                                         color = graphics.grey;
252                                         break;
253                                         
254                                 case TR_BRONZE_DOOR:
255                                 case TR_BRONZE_SLIDEDOOR:
256                                         color = SDL_MapRGB(graphics.screen->format, 0xff, 0x99, 0x00);
257                                         break;
258                                         
259                                 case TR_DOOR:
260                                 case TR_LOCKED_DOOR:
261                                 case TR_LOCKED_SLIDEDOOR:
262                                         color = SDL_MapRGB(graphics.screen->format, 0xff, 0x00, 0xff);
263                                         break;
264                         }
265                         
266                         graphics.drawRect(x * 5, y * 5, width, height, color, panel);
267                 }
268         }
269 }
270
271 void showMap(int centerX, int centerY)
272 {
273         char string[100];
274         int x1, y1, x2, y2;
275
276         x1 = centerX - 32;
277         x2 = centerX + 32;
278         y1 = centerY - 24;
279         y2 = centerY + 24;
280         
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;
285
286         Math::limitInt(&x1, minX, maxX);
287         Math::limitInt(&x2, minX, maxX);
288         Math::limitInt(&y1, minY, maxY);
289         Math::limitInt(&y2, minY, maxY);
290
291         SDL_FillRect(graphics.screen, NULL, graphics.black);
292         graphics.updateScreen();
293         SDL_Delay(500);
294         
295         SDL_Surface *panel = graphics.createSurface(320, 240);
296         SDL_Surface *background = graphics.loadImage("gfx/main/mapBackground.png");
297         SDL_SetAlpha(background, 130);
298         
299         graphics.blit(background, 0, 0, panel, false);
300         
301         int color = graphics.black;
302         
303         for (int y = 0 ; y < 48 ; y++)
304         {
305                 for (int x = 0 ; x < 64 ; x++)
306                 {
307                         if (map.data[x1 + x][y1 + y] == MAP_WATER)
308                         {
309                                 graphics.drawRect(x * 5, y * 5, 5, 5, graphics.blue, panel);
310                         }
311                 }
312         }
313         
314         addMiniMapDoors(panel, x1, y1);
315
316         for (int y = 0 ; y < 48 ; y++)
317         {
318                 for (int x = 0 ; x < 64 ; x++)
319                 {
320                         color = graphics.black;
321
322                         if (map.data[x1 + x][y1 + y] == MAP_AIR)
323                         {
324                                 color = graphics.black;
325                         }
326                         else if (map.data[x1 + x][y1 + y] == MAP_WATER)
327                         {
328                                 color = graphics.black;
329                         }
330                         else if (map.data[x1 + x][y1 + y] == MAP_SLIME)
331                         {
332                                 color = graphics.green;
333                         }
334                         else if (map.data[x1 + x][y1 + y] == MAP_LAVA)
335                         {
336                                 color = graphics.red;
337                         }
338                         else if (map.data[x1 + x][y1 + y] >= MAP_DECORATION)
339                         {
340                                 color = graphics.black;
341                                 
342                                 if (map.data[x1 + x][y1 + y - 1] == MAP_WATER)
343                                 {
344                                         color = graphics.blue;
345                                 }
346                                 else if (map.data[x1 + x][y1 + y + 1] == MAP_WATER)
347                                 {
348                                         color = graphics.blue;
349                                 }
350                         }
351                         else if (map.data[x1 + x][y1 + y] < MAP_WATERANIM)
352                         {
353                                 color = graphics.darkGreen;
354                         }
355
356                         if (color != graphics.black)
357                         {
358                                 graphics.drawRect(x * 5, y * 5, 5, 5, color, panel);
359                         }
360                 }
361         }
362
363         graphics.blit(background, 0, 0, graphics.screen, false);
364         SDL_FreeSurface(background);
365
366         List blipList;
367
368         RadarBlip *blip = new RadarBlip();
369         blip->set(160 + ((centerX - x1) * 5), 120 + ((centerY - y1) * 5), 0);
370         blipList.add(blip);
371
372         addBlips(&blipList, x1, y1, 1);
373         addBlips(&blipList, x1, y1, 2);
374         addBlips(&blipList, x1, y1, 3);
375         
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);
380
381         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
382         graphics.setFontSize(3);
383         graphics.drawString(_(game.stageName), 320, 30, TXT_CENTERED, graphics.screen);
384
385         graphics.setFontSize(0);
386         
387         graphics.drawRect(160, 414, 7, 7, graphics.yellow, graphics.white, graphics.screen);
388         graphics.drawString(_("MIAs"), 175, 410, TXT_LEFT, graphics.screen);
389
390         graphics.drawRect(290, 414, 7, 7, graphics.blue, graphics.white, graphics.screen);
391         graphics.drawString(_("Items"), 305, 410, TXT_LEFT, graphics.screen);
392
393         graphics.drawRect(415, 414, 7, 7, graphics.red, graphics.white, graphics.screen);
394         graphics.drawString(_("Enemies"), 430, 410, TXT_LEFT, graphics.screen);
395
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);
400
401         engine.flushInput();
402         engine.clearInput();
403
404         doMusicInfo(-1);
405
406         while (true)
407         {
408                 engine.getInput();
409                 config.populate();
410                 graphics.updateScreen();
411                 graphics.animateSprites();
412
413                 graphics.drawRect(160, 120, 320, 240, graphics.black, graphics.white, graphics.screen);
414                 graphics.blit(panel, 160, 120, graphics.screen, false);
415
416                 if ((config.isControl(CONTROL::MAP)) || (config.isControl(CONTROL::PAUSE)) || (engine.keyState[SDL_SCANCODE_ESCAPE]))
417                 {
418                         break;
419                 }
420
421                 blip = (RadarBlip*)blipList.getHead();
422
423                 while (blip->next != NULL)
424                 {
425                         blip = (RadarBlip*)blip->next;
426
427                         switch (blip->type)
428                         {
429                                 case 0:
430                                         graphics.blit(bobSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
431                                         break;
432                                 case 1:
433                                         graphics.blit(miaSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
434                                         break;
435                                 case 2:
436                                         graphics.blit(itemSignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
437                                         break;
438                                 case 3:
439                                         graphics.blit(enemySignal->getCurrentFrame(), blip->x, blip->y, graphics.screen, true);
440                                         break;
441                         }
442                 }
443
444                 SDL_Delay(16);
445         }
446
447         blipList.clear();
448
449         SDL_FillRect(graphics.screen, NULL, graphics.black);
450         graphics.updateScreen();
451         SDL_Delay(500);
452
453         engine.flushInput();
454         engine.clearInput();
455 }
456
457 void evaluateMapAttribute(Entity *ent, int mapAttribute)
458 {
459         switch (mapAttribute)
460         {
461                 case MAP_AIR_WALL_1:
462                 case MAP_AIR_WALL_2:
463                 case MAP_AIR_WALL_3:
464                 case MAP_AIR_CEILING_1:
465                         mapAttribute = MAP_AIR;
466                         break;
467                 case MAP_AIR_WALL_4:
468                         if (map.isCavesTileset)
469                         {
470                                 mapAttribute = MAP_AIR;
471                         }
472                         break;
473                 case MAP_AIR_CEILING_2:
474                         if (map.isGrasslandsTileset)
475                         {
476                                 mapAttribute = MAP_AIR;
477                         }
478                         else if (map.isCavesTileset)
479                         {
480                                 mapAttribute = MAP_WATER;
481                         }
482                         break;
483                 case MAP_WATER_WALL:
484                         if (map.isCavesTileset)
485                         {
486                                 mapAttribute = MAP_WATER;
487                         }
488                         break;
489         }
490
491         switch (mapAttribute)
492         {
493                 case MAP_AIR:
494                         if ((ent->environment != ENV_AIR) && (!(ent->flags & ENT_INANIMATE)))
495                         {
496                                 if (!(ent->flags & ENT_SWIMS))
497                                 {
498                                         if (ent->dy < 0)
499                                         {
500                                                 ent->dy = PLAYER_JUMP_SPEED;
501                                         }
502
503                                         if (ent == &player)
504                                         {
505                                                 if ((ent == &player) && ((game.hasAquaLung) || (engine.cheatExtras)))
506                                                 {
507                                                         player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
508                                                 }
509                                         }
510
511                                         ent->environment = ENV_AIR;
512                                         
513                                         if (ent->dy < 0)
514                                         {
515                                                 audio.playSound(SND_WATEROUT, CH_TOUCH, ent->x);
516                                         }
517
518                                         ent->checkEnvironment();
519                                 }
520                         }
521                         ent->falling = true;
522                 case MAP_SOLID:
523                         break;
524                 case MAP_WATER:
525                 case MAP_SLIME:
526                 case MAP_LAVA:
527
528                         ent->falling = false;
529
530                         if (ent->environment == ENV_AIR)
531                         {
532                                 audio.playSound(SND_WATERIN, CH_TOUCH, ent->x);
533                                 if ((mapAttribute == MAP_SLIME) || (mapAttribute == MAP_LAVA))
534                                         ent->thinktime = 1;
535                         }
536                         
537                         // On ice levels water is harmful (because it's very very cold!)
538                         if ((map.isIceLevel) && (mapAttribute == MAP_WATER))
539                         {
540                                 mapAttribute = MAP_LAVA;
541                                 ent->thinktime = 1;
542                         }
543
544                         if (mapAttribute == MAP_WATER)
545                         {
546                                 ent->environment = ENV_WATER;
547                                 if ((ent == &player) && ((game.hasAquaLung) || (engine.cheatExtras)))
548                                 {
549                                         player.setSprites(graphics.getSprite("AquaBobRight", true), graphics.getSprite("AquaBobLeft", true), graphics.getSprite("AquaBobSpin", true));
550                                 }
551                         }
552                         else if (mapAttribute == MAP_SLIME)
553                         {
554                                 ent->environment = ENV_SLIME;
555                         }
556                         else if (mapAttribute == MAP_LAVA)
557                         {
558                                 ent->environment = ENV_LAVA;
559                         }
560                         break;
561         }
562 }
563
564 void raiseWaterLevel()
565 {
566         if (map.waterLevel == map.requiredWaterLevel)
567         {
568                 return;
569         }
570
571         int y = (int)map.waterLevel;
572
573         if ((int)map.waterLevel != map.requiredWaterLevel)
574         {
575                 for (int x = 0 ; x < MAPWIDTH ; x++)
576                 {
577                         if ((map.data[x][y] == MAP_AIR) || (map.isLiquid(x, y)))
578                         {
579                                 map.data[x][y] = MAP_WATER;
580                         }
581                         
582                         if ((map.data[x][y] >= MAP_DECORATION) && (map.data[x][y] <= MAP_WATERANIM))
583                         {
584                                 map.data[x][y] = MAP_WATER;
585                         }
586                         
587                         if (map.data[x][y] >= MAP_TOPLAYER)
588                         {
589                                 map.data[x][y] = MAP_WATER;
590                         }
591                         
592                         if (map.data[x][y - 1] >= MAP_TOPLAYER)
593                         {
594                                 map.data[x][y] = MAP_WATER;
595                         }
596
597                         if ((map.data[x][y - 1] == MAP_AIR) || (map.isLiquid(x, y - 1)))
598                         {
599                                 map.data[x][y - 1] = MAP_WATERANIM;
600                         }
601                         
602                         if ((map.data[x][y - 1] >= MAP_DECORATION) && (map.data[x][y - 1] <= MAP_WATERANIM))
603                         {
604                                 map.data[x][y - 1] = MAP_WATERANIM;
605                         }
606                 }
607
608                 map.waterLevel -= 0.1;
609
610                 int x = (int)(player.x + player.dx) >> BRICKSHIFT;
611                 int y = (int)(player.y + player.height - 1) >> BRICKSHIFT;
612
613                 int mapAttribute = map.data[x][y];
614
615                 if ((mapAttribute == MAP_WATER) && (player.environment == MAP_AIR))
616                 {
617                         evaluateMapAttribute(&player, mapAttribute);
618                 }
619         }
620 }
621
622 void doWind()
623 {
624         map.windChangeTime--;
625         
626         if (map.windChangeTime <= 0)
627         {
628                 map.windPower = 0;
629                 
630                 if (Math::rrand(0, 1) == 0)
631                         map.windPower = Math::rrand(-3, 3);
632                         
633                 // change wind time
634                 map.windChangeTime = Math::rrand(60, 600);
635         }
636         
637         addWindParticles();
638 }
639
640 void parseMapDataLine(const char *line, int y)
641 {
642         int tileIndex = 0;
643         int x = 0;
644
645         while (true)
646         {
647                 sscanf(line, "%d", &tileIndex);
648
649                 map.data[x][y] = tileIndex;
650
651                 while (true)
652                 {
653                         line++;
654
655                         if (*line == ' ')
656                                 break;
657                 }
658
659                 x++;
660
661                 if (x == MAPWIDTH)
662                         break;
663         }
664 }
665
666 bool loadMapData(const char *filename)
667 {
668         map.clear();
669
670         if (!engine.loadData(filename))
671                 graphics.showErrorAndExit("The requested map '%s' was not found.", filename);
672
673         char *token = strtok((char*)engine.dataBuffer, "\n");
674         parseMapDataLine(token, 0);
675
676         int y = 1;
677
678         while (true)
679         {
680                 token = strtok(NULL, "\n");
681
682                 parseMapDataLine(token, y);
683
684                 y++;
685                 if (y == MAPHEIGHT)
686                         break;
687         }
688
689         getMapTokens();
690
691         adjustObjectives();
692         initMIAPhrases();
693
694         return true;
695 }