]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/mission.cpp
Fix all issues found by the Clang Static Analyzer.
[quix0rs-blobwars.git] / src / mission.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 "mission.h"
23
24 // This is for easy mode
25 void skipBossMission()
26 {
27         if (game.stagesCleared == 5)
28         {
29                 gameData.addCompletedObjective("BioMech Supply Depot", "Easy Mode Skip", 1, 1);
30                 game.stagesCleared = 6;
31         }
32         
33         if (game.stagesCleared == 10)
34         {
35                 gameData.addCompletedObjective("BioMech Communications", "Easy Mode Skip", 1, 1);
36                 game.hasAquaLung = true;
37                 game.stagesCleared = 11;
38                 SDL_FillRect(graphics.screen, NULL, graphics.black);
39                 graphics.delay(250);
40                 graphics.drawString(_("You got the Aqua Lung!"), 320, 420, TXT_CENTERED, graphics.screen);
41                 graphics.delay(2000);
42                 SDL_FillRect(graphics.screen, NULL, graphics.black);
43         }
44         
45         if (game.stagesCleared == 15)
46         {
47                 gameData.addCompletedObjective("BioMech Assimilator", "Easy Mode Skip", 1, 1);
48                 game.hasJetPack = true;
49                 game.stagesCleared = 16;
50                 SDL_FillRect(graphics.screen, NULL, graphics.black);
51                 graphics.delay(250);
52                 graphics.drawString(_("You got the Jet Pack!"), 320, 420, TXT_CENTERED, graphics.screen);
53                 graphics.delay(2000);
54                 SDL_FillRect(graphics.screen, NULL, graphics.black);
55         }
56         
57         if (game.stagesCleared == 21)
58         {
59                 gameData.addCompletedObjective("BioMech HQ", "Easy Mode Skip", 1, 1);
60                 game.stagesCleared = 22;
61         }
62 }
63
64 void processPostMissionData()
65 {
66         char string[1024];
67         
68         if (!gameData.stagePreviouslyCleared(game.stageName))
69         {
70                 presentPlayerMedal(game.stageName);
71                 game.stagesCleared++;
72         }
73         
74         if (game.skill == 0)
75         {
76                 skipBossMission();
77         }
78         
79         gameData.setMIARescueCount(map.name, map.foundMIAs, map.totalMIAs);
80
81         Objective *objective = (Objective*)map.objectiveList.getHead();
82         Entity *mia = (Entity*)map.miaList.getHead();
83
84         while (objective->next != NULL)
85         {
86                 objective = (Objective*)objective->next;
87                 
88                 gameData.addCompletedObjective(map.name, objective->description, objective->currentValue, objective->targetValue);
89         }
90
91         bool miaFound;
92
93         while (mia->next != NULL)
94         {
95                 mia = (Entity*)mia->next;
96
97                 miaFound = true;
98
99                 if (mia->health > 0)
100                         miaFound = false;
101
102                 snprintf(string, sizeof string, "MIA_%s", mia->name);
103                 
104                 if (miaFound)
105                 {
106                         gameData.addCompletedObjective(map.name, string, 1, 1);
107                 }
108                 else
109                 {
110                         gameData.addCompletedObjective(map.name, string, 0, 1);
111                 }
112         }
113
114         game.totalUpStats();
115         
116         if (game.totalEnemiesDefeated >= 1000)
117         {
118                 presentPlayerMedal("1000_Enemies");
119         }
120         
121         if (game.totalEnemiesDefeated >= 2500)
122         {
123                 presentPlayerMedal("2500_Enemies");
124         }
125 }
126
127 void clearAllMissionData()
128 {
129         char levelMIAKey[100];
130         
131         snprintf(levelMIAKey, sizeof levelMIAKey, "%s MIAs", game.stageName);
132         
133         Data *data = (Data*)gameData.dataList.getHead();
134         Data *previous = data;
135         
136         while (data->next != NULL)
137         {
138                 data = (Data*)data->next;
139                 
140                 if ((strcmp(data->key, game.stageName) == 0) || (strstr(data->key, levelMIAKey)))
141                 {
142                         gameData.dataList.remove(previous, data);
143                         data = previous;
144                 }
145                 else
146                 {
147                         previous = data;
148                 }
149         }
150         
151         map.destroyPersistant(map.name);
152 }
153
154 void showMissionClear()
155 {
156         int colCount = 7;
157         bool perfect = perfectlyCompleted();
158
159         SDL_Surface *clear;
160
161         (perfect) ? clear = graphics.loadImage("gfx/main/areaPerfect.png") : clear = graphics.loadImage("gfx/main/areaClear.png");
162
163         graphics.loadBackground("gfx/main/areaClearBackGround.jpg");
164
165         SDL_Surface *panel = graphics.alphaRect(550, 420, 0x00, 0x00, 0x00);
166         SDL_SetAlpha(panel, 180);
167         graphics.drawRect(1, 1, panel->w - 2, panel->h - 2 , graphics.black, graphics.white, panel);
168
169         graphics.blit(panel, (640 - panel->w) / 2, (480 - panel->h) / 2, graphics.background, false);
170
171         audio.loadSound(0, "sound/pop1");
172         audio.loadSound(1, "sound/cheer");
173
174         SDL_FillRect(graphics.screen, NULL, graphics.black);
175         graphics.updateScreen();
176
177         int y = 520;
178         int miaY = 335;
179         int clearY = 520;
180         Entity *mia = (Entity*)map.miaList.getHead();
181         Sprite *teleportStar = graphics.getSprite("TeleportStar", true);
182         char message[256];
183         int col1 = 360;
184         int col2 = 380;
185         int count = 0;
186         int place = 0;
187         int nextStarBurst = 10 * (Math::prand() % 6);
188
189         float px, py, dx, dy;
190
191
192         while (mia->next != NULL)
193         {
194                 mia = (Entity*)mia->next;
195
196                 if (mia->health > 0)
197                         continue;
198
199                 mia->x = 700;
200                 
201                 mia->y = miaY;
202                 
203                 count++;
204
205                 if (count > colCount)
206                 {
207                         miaY += 25;
208                         count = 0;
209                 }
210
211                 place += 25;
212         }
213
214         if (place > (colCount * 25))
215                 place = colCount * 25;
216
217         place = (640 - place) / 2;
218
219         graphics.setFontSize(1);
220
221         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
222
223         engine.flushInput();
224         engine.clearInput();
225
226         y = 130;
227         
228         if (map.totalMIAs > 0)
229         {
230                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
231                 snprintf(message, sizeof message, _("Rescue %d MIAs"), map.requiredMIAs);
232                 graphics.drawString(message, col1, y, TXT_RIGHT, graphics.background);
233
234                 if (map.foundMIAs < map.requiredMIAs)
235                 {
236                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
237                         snprintf(message, sizeof message, "%d / %d", map.foundMIAs, map.requiredMIAs);
238                         graphics.drawString(message, col2, y, TXT_LEFT, graphics.background);
239                 }
240                 else
241                 {
242                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
243                         graphics.drawString(_("Completed"), col2, y, TXT_LEFT, graphics.background);
244                 }
245         }
246
247         Objective *objective = (Objective*)map.objectiveList.getHead();
248
249         engine.setPlayerPosition(0, 0, -1, -1, -1, -1);
250
251         while (objective->next != NULL)
252         {
253                 objective = (Objective*)objective->next;
254
255                 y += 20;
256
257                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
258                 
259                 if ((game.skill < 3) &&  (strstr(objective->description, "L.R.T.S.")) && (!gameData.completedWorld))
260                 {
261                         graphics.drawString(_("???? ???????? ????"), col1, y, TXT_RIGHT, graphics.background);
262                 }
263                 else
264                 {
265                         graphics.drawString(_(objective->description), col1, y, TXT_RIGHT, graphics.background);
266                 }
267
268                 if (objective->currentValue < objective->targetValue)
269                 {
270                         if (objective->targetValue == 1)
271                         {
272                                 graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
273                                 graphics.drawString(_("Incomplete"), col2, y, TXT_LEFT, graphics.background);
274                         }
275                         else
276                         {
277                                 graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
278                                 snprintf(message, sizeof message, "%d / %d", objective->currentValue, objective->targetValue);
279                                 graphics.drawString(message, col2, y, TXT_LEFT, graphics.background);
280                         }
281                 }
282                 else
283                 {
284                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
285                         graphics.drawString(_("Completed"), col2, y, TXT_LEFT, graphics.background);
286                 }
287         }
288         
289         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
290
291         unsigned int frameLimit = SDL_GetTicks() + 16;
292
293         if (perfect)
294                 audio.playSound(1, 1);
295
296         while (true)
297         {
298                 engine.getInput();
299                 config.populate();
300                 graphics.updateScreen();
301                 graphics.animateSprites();
302
303                 if (engine.userAccepts())
304                         break;
305
306                 graphics.drawBackground();
307
308                 graphics.blit(clear, 320, clearY, graphics.screen, true);
309
310                 Math::limitInt(&(clearY -= 5), 70, 520);
311
312                 count = 0;
313
314                 mia = (Entity*)map.miaList.getHead();
315
316                 if (clearY == 70)
317                 {
318                         while (mia->next != NULL)
319                         {
320                                 mia = (Entity*)mia->next;
321
322                                 if (mia->health > 0)
323                                         continue;
324
325                                 if (count > colCount)
326                                         count = 0;
327
328                                 graphics.blit(mia->getFaceImage(), (int)mia->x, (int)mia->y, graphics.screen, false);
329
330                                 if (mia->x == 700)
331                                         audio.playSound(0, 0);
332
333                                 if (mia->x > (place + (count * 25)))
334                                         Math::limitFloat(&(mia->x -= 35), place + (count * 25), 640);
335
336                                 if (mia->x > place + (count * 25))
337                                         break;
338
339                                 count++;
340                         }
341                         
342                         if (perfect)
343                         {
344                                 Math::limitInt(&(--nextStarBurst), 0, 999);
345
346                                 if (nextStarBurst == 0)
347                                 {
348                                         nextStarBurst = 10 * (Math::prand() % 6);
349
350                                         px = Math::rrand(50, 585);
351                                         py = Math::rrand(55, 85);
352
353                                         for (int i = 0 ; i < 50 ; i++)
354                                         {
355                                                 dx = Math::rrand(-30, 30); dx /= 20;
356                                                 dy = Math::rrand(-30, 30); dy /= 20;
357                                                 map.addParticle(px, py, dx, dy, Math::rrand(30, 60), graphics.red, teleportStar, PAR_WEIGHTLESS);
358                                         }
359                                 }
360
361                                 doParticles();
362                         }
363                 }
364
365                 static Graphics::SurfaceCache cache;
366                 snprintf(message, sizeof message, "%s - %.2d:%.2d:%.2d", _("Mission Time"), game.currentMissionHours, game.currentMissionMinutes, game.currentMissionSeconds);
367                 graphics.drawString(message, 320, 420, true, graphics.screen, cache);
368
369                 engine.delay(frameLimit);
370                 frameLimit = SDL_GetTicks() + 16;
371         }
372
373         audio.stopMusic();
374
375         processPostMissionData();
376
377         if (!engine.practice)
378         {
379                 saveGame();
380         }
381 }
382