]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/game.cpp
b093fbc5eddc281a659e78c0c8210048881e171d
[quix0rs-blobwars.git] / src / game.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "game.h"
22
23 void newGame()
24 {
25         game.clear();
26         gameData.clear();
27         map.persistantList.clear();
28         engine.practice = false;
29
30         game.skill = engine.skill;
31 }
32
33 void showInGameOptions()
34 {
35         if (!engine.loadWidgets(_("data/inGameWidgets")))
36         {
37                 graphics.showErrorAndExit(ERR_FILE, _("data/inGameWidgets"));
38         }
39
40         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
41
42         int cont, options, escape, quit, escapeyes, escapeno, warnno, warnyes, quitno, quityes, train, trainno, trainyes, restart, restartno, restartyes;
43         cont = options = escape = quit = escapeyes = escapeno = warnno = warnyes = quitno = quityes = train = trainno = trainyes = restart = restartno = restartyes = 0;
44
45         engine.setWidgetVariable("continue", &cont);
46         engine.setWidgetVariable("options", &options);
47         engine.setWidgetVariable("escape", &escape);
48         engine.setWidgetVariable("restart", &restart);
49         engine.setWidgetVariable("quit", &quit);
50         engine.setWidgetVariable("train", &train);
51
52         engine.setWidgetVariable("warnno", &warnno);
53         engine.setWidgetVariable("warnyes", &warnyes);
54         
55         engine.setWidgetVariable("restartno", &restartno);
56         engine.setWidgetVariable("restartyes", &restartyes);
57
58         engine.setWidgetVariable("quitno", &quitno);
59         engine.setWidgetVariable("quityes", &quityes);
60         
61         engine.setWidgetVariable("trainno", &quitno);
62         engine.setWidgetVariable("trainyes", &quityes);
63         
64         engine.setWidgetVariable("escapeno", &escapeno);
65         engine.setWidgetVariable("escapeyes", &escapeyes);
66
67         engine.showWidgetGroup("warning", false);
68         engine.showWidgetGroup("restartconf", false);
69         engine.showWidgetGroup("escapeconf", false);
70         engine.showWidgetGroup("quitconf", false);
71         engine.showWidgetGroup("trainconf", false);
72         
73         if ((map.isBossMission) || (engine.practice) || (strcmp(map.name, "Space Station") == 0))
74         {
75                 engine.enableWidget("escape", false);
76                 engine.enableWidget("restart", false);
77         }
78                 
79         if (!engine.practice)
80         {
81                 engine.showWidget("train", false);
82         }
83         else
84         {
85                 engine.showWidget("quit", false);
86         }
87
88         engine.flushInput();
89         engine.clearInput();
90
91         drawWidgets();
92         audio.playMenuSound(2);
93         
94         int menuSound = -1;
95
96         while (true)
97         {
98                 graphics.updateScreen();                
99                 engine.getInput();
100                 config.populate();
101
102                 if ((config.isControl(CONTROL::PAUSE)) || (engine.keyState[SDLK_ESCAPE]))
103                 {
104                         engine.keyState[SDLK_ESCAPE] = 0;
105                         config.resetControl(CONTROL::PAUSE);
106                         break;
107                 }
108                 
109                 menuSound = engine.processWidgets();
110
111                 if (menuSound)
112                 {
113                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
114                         drawWidgets();
115                         audio.playMenuSound(menuSound);
116                 }
117
118                 if (cont)
119                 {
120                         break;
121                 }
122
123                 if (escape)
124                 {
125                         engine.showWidgetGroup("options", false);
126
127                         if (!gameData.stagePreviouslyCleared(game.stageName))
128                         {
129                                 engine.showWidgetGroup("warning", true);
130                                 engine.highlightWidget("warnno");
131                         }
132                         else
133                         {
134                                 engine.showWidgetGroup("escapeconf", true);
135                                 engine.highlightWidget("escapeno");
136                         }
137
138                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
139                         drawWidgets();
140                         escape = 0;
141                 }
142
143                 if ((escapeyes) || (warnyes))
144                 {
145                         audio.stopMusic();
146                         audio.stopAmbiance();
147                         addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
148                         dropCarriedItems();
149                         game.setMissionOver(MIS_PLAYERESCAPE);
150                         break;
151                 }
152
153                 if (options)
154                 {
155                         showOptions();
156                         break;
157                 }
158
159                 if ((warnno) || (quitno) || (escapeno) || (trainno) || (restartno))
160                 {
161                         engine.highlightWidget("continue");
162                         engine.showWidgetGroup("options", true);
163                         engine.showWidgetGroup("warning", false);
164                         engine.showWidgetGroup("trainconf", false);
165                         engine.showWidgetGroup("escapeconf", false);
166                         engine.showWidgetGroup("quitconf", false);
167                         engine.showWidgetGroup("restartconf", false);
168                         
169                         if (!engine.practice)
170                         {
171                                 engine.showWidget("train", false);
172                         }
173                         else
174                         {
175                                 engine.showWidget("quit", false);
176                         }
177                         
178                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
179                         drawWidgets();
180                         quitno = trainno = warnno = escapeno = restartno = 0;
181                 }
182
183                 if (quit)
184                 {
185                         engine.showWidgetGroup("options", false);
186                         engine.showWidgetGroup("quitconf", true);
187                         engine.highlightWidget("quitno");
188
189                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
190                         drawWidgets();
191                         quit = 0;
192                 }
193                 
194                 if (train)
195                 {
196                         engine.showWidgetGroup("options", false);
197                         engine.showWidgetGroup("trainconf", true);
198                         engine.highlightWidget("trainno");
199
200                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
201                         drawWidgets();
202                         train = 0;
203                 }
204                 
205                 if (restart)
206                 {
207                         engine.showWidgetGroup("options", false);
208                         engine.showWidgetGroup("restartconf", true);
209                         engine.highlightWidget("restartno");
210
211                         graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
212                         drawWidgets();
213                         restart = 0;
214                 }
215
216                 if ((quityes) || (trainyes))
217                 {
218                         game.setMissionOver(MIS_PLAYERQUIT);
219                         break;
220                 }
221                 
222                 if (restartyes)
223                 {
224                         game.setMissionOver(MIS_PLAYERRESTART);
225                         SDL_FillRect(graphics.screen, NULL, graphics.black);
226                         audio.stopMusic();
227                         audio.stopAmbiance();
228                         graphics.delay(500);
229                         break;
230                 }
231
232                 SDL_Delay(16);
233         }
234 }
235
236 void doGameStuff()
237 {
238         engine.getInput();
239         config.populate();
240         replayData.read(config.command);
241         
242         if (game.missionOverReason == MIS_INPROGRESS)
243         {
244                 config.doPause();
245         }
246
247         engine.doFrameLoop();
248
249         graphics.updateScreen();
250         graphics.animateSprites();
251         graphics.drawBackground();
252
253         doEffects();
254         doTrains();
255         doTraps();
256         drawMap();
257         doLineDefs();
258         doSwitches();
259         doItems();
260         doBullets();
261         doMIAs();
262         
263         if (map.isBossMission)
264         {
265                 doBosses();
266         }
267         
268         doEnemies();
269         doObstacles();
270         doTeleporters();
271         
272         if (map.isBlizzardLevel)
273         {
274                 doWind();
275         }
276         
277         doParticles();
278         
279         if ((SDL_GetAppState() & SDL_APPINPUTFOCUS) == 0)
280         {
281                 engine.paused = true;
282         }
283         
284         replayData.set(config.command); 
285         replayData.commit();
286 }
287
288 int gameover()
289 {
290         audio.stopMusic();
291         audio.stopAmbiance();
292         
293         if (!engine.loadWidgets(_("data/gameOverWidgets")))
294         {
295                 graphics.showErrorAndExit(ERR_FILE, _("data/gameOverWidgets"));
296         }
297
298         SDL_Surface *gameover = graphics.quickSprite("Game Over", graphics.loadImage("gfx/main/gameover.png"));
299         graphics.setTransparent(gameover);
300
301         audio.loadGameOverMusic();
302         audio.playMusic();
303
304         engine.flushInput();
305         engine.clearInput();
306
307         unsigned int frameLimit = SDL_GetTicks() + 16;
308
309         bool showGameOverOptions = false;
310         int cont, quit, menuSound;
311         cont = quit = menuSound = 0;
312
313         engine.setWidgetVariable("gameOverNo", &cont);
314         engine.setWidgetVariable("gameOverYes", &quit);
315         
316         if (game.canContinue > 1)
317         {
318                 Widget *widget = engine.getWidgetByName("gameOverNo");
319                 char postfix[100];
320                 snprintf(postfix, sizeof postfix, " (%d)", game.canContinue);
321                 strlcat(widget->label, postfix, sizeof widget->label);
322         }
323
324         while (true)
325         {
326                 if (menuSound)
327                         audio.playMenuSound(menuSound);
328
329                 doGameStuff();
330                 drawMapTopLayer();
331
332                 graphics.blit(gameover, 320, 240, graphics.screen, true);
333
334                 if (engine.userAccepts())
335                 {
336                         if (!showGameOverOptions)
337                         {
338                                 showGameOverOptions = true;
339                                 engine.showWidgetGroup("gameover", true);
340                                 engine.highlightWidget("gameOverNo");
341                                 engine.flushInput();
342                                 engine.clearInput();
343                         }
344
345                         // Can't continue on a boss mission or if no checkpoints reached!
346                         if ((map.isBossMission) || (!game.canContinue))
347                         {
348                                 engine.showWidgetGroup("gameover", false);
349                                 quit = 1;
350                         }
351                 }
352                 
353                 if (showGameOverOptions)
354                 {
355                         drawWidgets();
356                         menuSound = engine.processWidgets();
357                 }
358
359                 if ((cont) || (quit))
360                         break;
361
362                 engine.delay(frameLimit);
363                 frameLimit = SDL_GetTicks() + 16;
364         }
365
366         if (quit)
367         {
368                 audio.fadeMusic();
369                 graphics.fadeToBlack();
370                 map.clear();
371                 
372                 return SECTION_TITLE;
373         }
374
375         game.continueFromCheckPoint = true;
376         audio.stopMusic();
377         audio.reloadLevelMusic();
378
379         return SECTION_GAME;
380 }
381
382 void showMissionInformation()
383 {
384         SDL_FillRect(graphics.screen, NULL, graphics.black);
385         graphics.updateScreen();
386
387         SDL_Surface *panel = graphics.createSurface(400, 300);
388         SDL_Surface *panelBack = graphics.alphaRect(400, 300, 0x00, 0x00, 0x00);
389         SDL_SetColorKey(panel, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(panel->format, 0, 0, 0));
390
391         graphics.drawRect(1, 1, 398, 298, graphics.black, graphics.white, panelBack);
392
393         char message[256];
394         int col1 = 25;
395         int col2 = 375;
396         int y = 30;
397
398         Objective *objective;
399
400         graphics.setFontSize(0);
401
402         graphics.setFontSize(3);
403         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
404         graphics.drawString(_(game.stageName), 200, 20, TXT_CENTERED, panel);
405
406         graphics.setFontSize(0);
407
408         y += 20;
409
410         if (map.totalMIAs > 0)
411         {
412                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
413                 snprintf(message, sizeof message, _("Rescue %d MIAs"), map.requiredMIAs);
414                 graphics.drawString(message, col1, y, TXT_LEFT, panel);
415
416                 if (map.foundMIAs < map.requiredMIAs)
417                 {
418                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
419                         snprintf(message, sizeof message, "%d / %d", map.foundMIAs, map.requiredMIAs);
420                         graphics.drawString(message, col2, y, TXT_RIGHT, panel);
421                 }
422                 else
423                 {
424                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
425                         graphics.drawString(_("Completed"), col2, y, TXT_RIGHT, panel);
426                 }
427         }
428
429         objective = (Objective*)map.objectiveList.getHead();
430
431         while (objective->next != NULL)
432         {
433                 objective = (Objective*)objective->next;
434
435                 y += 20;
436
437                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
438                 
439                 if ((game.skill < 3) &&  (strstr(objective->description, "L.R.T.S.")) && (!gameData.completedWorld))
440                 {
441                         graphics.drawString(_("???? ???????? ????"), col1, y, TXT_LEFT, panel);
442                 }
443                 else
444                 {
445                         graphics.drawString(_(objective->description), col1, y, TXT_LEFT, panel);
446                 }
447                 
448                 // this is a fake objective (for the 4th Ancient Tomb)
449                 if (objective->targetValue == -1)
450                 {
451                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
452                         graphics.drawString(_("Incomplete"), col2, y, TXT_RIGHT, panel);
453                 }
454                 else if (objective->currentValue < objective->targetValue)
455                 {
456                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
457                         if (objective->targetValue == 1)
458                         {
459                                 graphics.drawString(_("Incomplete"), col2, y, TXT_RIGHT, panel);
460                         }
461                         else
462                         {
463                                 snprintf(message, sizeof message, "%d / %d", objective->currentValue, objective->targetValue);
464                                 graphics.drawString(message, col2, y, TXT_RIGHT, panel);
465                         }
466                 }
467                 else
468                 {
469                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
470                         graphics.drawString(_("Completed"), col2, y, TXT_RIGHT, panel);
471                 }
472         }
473         
474         if (game.skill == 3)
475         {
476                 graphics.setFontColor(0xff, 0xff, 0x00, 0x00, 0x00, 0x00);
477                 snprintf(message, sizeof message, _("Time Limit - %d:%.2d Minutes"), map.remainingMinutes, map.remainingSeconds);
478                 graphics.drawString(message, 200, 260, TXT_CENTERED, panel);
479         }
480         
481         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
482         graphics.drawString(_("Press Fire to Continue"), 200, 280, TXT_CENTERED, panel);
483
484         engine.flushInput();
485         engine.clearInput();
486
487         int px, py;
488
489         map.getRandomEntityPosition(&px, &py);
490         map.getRandomEntityPosition(&player.tx, &player.ty);
491
492         player.x = px;
493         player.y = py;
494
495         unsigned int frameLimit = SDL_GetTicks() + 16;
496                 
497         while (true)
498         {
499                 if ((int)player.x < player.tx) player.x += 2;
500                 if ((int)player.x > player.tx) player.x -= 2;
501                 if ((int)player.y < player.ty) player.y += 2;
502                 if ((int)player.y > player.ty) player.y -= 2;
503
504                 if (Collision::collision(player.x, player.y, 5, 5, player.tx, player.ty, 5, 5))
505                         map.getRandomEntityPosition(&player.tx, &player.ty);
506
507                 engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
508
509                 doGameStuff();
510                 drawMapTopLayer();
511                 graphics.blit(panelBack, 320, 220, graphics.screen, true);
512                 graphics.blit(panel, 320, 220, graphics.screen, true);
513
514                 engine.delay(frameLimit);
515                 frameLimit = SDL_GetTicks() + 16;
516
517                 if (engine.userAccepts())
518                         break;
519         }
520
521         SDL_FreeSurface(panel);
522         SDL_FreeSurface(panelBack);
523
524         SDL_FillRect(graphics.screen, NULL, graphics.black);
525         graphics.delay(1000);
526 }
527
528 void beamInPlayer()
529 {
530         game.getCheckPoint(&player.x, &player.y);
531         
532         int beamInTime = 180;
533         
534         unsigned int frameLimit = SDL_GetTicks() + 16;
535         
536         audio.playSound(SND_TELEPORT1, CH_ANY);
537         
538         engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
539         
540         while (beamInTime > 0)
541         {
542                 engine.delay(frameLimit);
543                 frameLimit = SDL_GetTicks() + 16;
544                 
545                 doGameStuff();
546                 drawMapTopLayer();
547                 
548                 if ((beamInTime % 10) == 0)
549                         addTeleportParticles(player.x + 10, player.y + 10, 50, -1);
550                 
551                 beamInTime--;
552         }
553         
554         game.getCheckPoint(&player.x, &player.y);
555         
556         player.dx = 0;
557         player.dy = 0;
558         player.immune = 120;
559         player.environment = ENV_AIR;
560         player.oxygen = 7;
561         player.fuel = 7;
562         Math::removeBit(&player.flags, ENT_FLIES);
563 }
564
565 int doGame()
566 {
567         graphics.setFontSize(0);
568
569         SDL_FillRect(graphics.screen, NULL, graphics.black);
570         graphics.delay(1000);
571
572         Uint32 then, frames, frameLimit, millis, frameCounter;
573         Uint32 start, cur;
574
575         #if DEBUG
576         Uint32 now;
577         char fps[10];
578         strlcpy(fps, "fps", sizeof fps);
579         #endif
580
581         engine.messageTime = -1;
582         engine.messagePriority = -1;
583
584         audio.playMusic();
585         audio.playAmbiance();
586
587         if (!game.continueFromCheckPoint)
588         {
589                 player.health = -99;
590
591                 if ((!map.isBossMission) && (replayData.replayMode == REPLAY_MODE::NONE))
592                 {
593                         showMissionInformation();
594                 }
595
596                 game.levelsStarted++;
597         }
598         else
599         {
600                 game.useObjectiveCheckPoint();
601         }
602
603         player.setVelocity(0, 1);
604         player.baseThink = 60;
605         player.health = MAX_HEALTH;
606
607         if (game.continueFromCheckPoint)
608         {
609                 player.health = (MAX_HEALTH / 2);
610                 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
611         }
612         else
613         {
614                 player.currentWeapon = &weapon[WP_PISTOL];
615         }
616
617         game.resetMissionOver();
618
619         frameLimit = SDL_GetTicks() + 16;
620         frames = millis = 0;
621         start = then = SDL_GetTicks();
622         frameCounter = SDL_GetTicks();
623
624         if ((strcmp(map.name, "Space Station") == 0) && (!game.continueFromCheckPoint))
625         {
626                 beamInPlayer();
627         }
628         else
629         {
630                 resetPlayer();
631         }
632
633         engine.flushInput();
634         engine.clearInput();
635
636         debug(("Map Clipping is %d %d %d %d\n", map.limitLeft, map.limitRight, map.limitUp, map.limitDown));
637
638         game.continueFromCheckPoint = false;
639
640         engine.paused = false;
641
642         while (true)
643         {
644                 ++frames;
645                 ++millis;
646                 cur = SDL_GetTicks();
647
648                 if (game.missionOverReason != MIS_PLAYEROUT)
649                 {
650                         engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
651                 }
652
653                 doSpawnPoints();
654                 doGameStuff();
655
656                 doPlayer();
657                 raiseWaterLevel();
658
659                 if ((player.environment == ENV_SLIME) || (player.environment == ENV_LAVA) || (player.health < 1))
660                 {
661                         if (player.health < 1)
662                         {
663                                 game.setMissionOver(MIS_PLAYERDEAD);
664                                 audio.fadeMusic();
665                                 audio.stopAmbiance();
666                         }
667                         else
668                         {
669                                 game.setMissionOver(MIS_PLAYEROUT);
670                                 player.immune = 130;
671                         }
672                 }
673
674                 if (config.isControl(CONTROL::MAP))
675                 {
676                         if (!map.isBossMission)
677                         {
678                                 showMap((int)(player.x / 32), (int)(player.y / 32));
679                         }
680                         else
681                         {
682                                 engine.setInfoMessage("Automap is not available!", 1, INFO_HINT);
683                         }
684                 }
685
686                 drawMapTopLayer();
687                 doStatusBar();
688                 doMusicInfo(cur - start);
689
690                 if ((engine.keyState[SDLK_ESCAPE]) && (game.missionOver == 0))
691                 {
692                         if (replayData.replayMode == REPLAY_MODE::NONE)
693                         {
694                                 showInGameOptions();
695                         }
696                         else
697                         {
698                                 exit(0);
699                         }
700                 }
701
702                 if (allObjectivesCompleted())
703                 {
704                         if (game.missionOver == 0)
705                         {
706                                 if (map.isBossMission)
707                                 {
708                                         map.killAllEnemies();
709                                 }
710                                 
711                                 audio.stopMusic();
712                                 audio.stopAmbiance();
713
714                                 game.setMissionOver(MIS_COMPLETE);
715                                 engine.setInfoMessage("All Required Objectives Met - Mission Complete", 10, INFO_OBJECTIVE);
716                         }
717                 }
718
719                 if (game.missionOver > 0)
720                 {
721                         game.missionOver--;
722                 
723                         if (game.missionOver == 0)
724                         {
725                                 if (game.missionOverReason == MIS_PLAYEROUT)
726                                 {
727                                         SDL_FillRect(graphics.screen, NULL, graphics.black);
728                                         graphics.updateScreen();
729                                         graphics.delay(1000);
730                                         engine.flushInput();
731                                         engine.clearInput();
732                                         resetPlayer();
733                                         game.resetMissionOver();
734                                 }
735                                 else
736                                 {
737                                         if (game.missionOverReason == MIS_COMPLETE)
738                                         {
739                                                 game.missionOver = MAX_FPS * 2;
740                                                 
741                                                 if (strcmp(map.name, "Space Station") != 0)
742                                                 {
743                                                         addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
744                                                         dropCarriedItems();
745                                                         game.missionOverReason = MIS_PLAYERESCAPE;
746                                                 }
747                                                 else
748                                                 {
749                                                         break;
750                                                 }
751                                         }
752                                         else if (game.missionOverReason == MIS_GAMECOMPLETE)
753                                         {
754                                                 addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
755                                                 game.missionOverReason = MIS_PLAYERESCAPE;
756                                                 game.missionOver = MAX_FPS * 4;
757                                         }
758                                         else
759                                         {
760                                                 if (replayData.replayMode != REPLAY_MODE::NONE)
761                                                 {
762                                                         exit(0);
763                                                 }
764                                         
765                                                 break;
766                                         }
767                                 }
768                         }
769                 }
770
771                 Math::limitInt(&(--game.lastComboTime), 0, 60);
772
773                 if (millis >= 60)
774                 {
775                         millis = 0;
776                         
777                         if ((game.missionOverReason == MIS_INPROGRESS) || (game.missionOverReason == MIS_PLAYEROUT))
778                         {
779                                 game.incrementMissionTime();
780                                 
781                                 if (game.skill == 3)
782                                 {
783                                         doTimeRemaining();
784                                 }
785                         }
786                 }
787
788                 if (engine.paused)
789                 {
790                         doPauseInfo();
791                         audio.pause();
792                 }
793
794                 while (engine.paused)
795                 {
796                         engine.getInput();
797                         config.populate();
798                         config.doPause();
799                         graphics.updateScreen();
800                         then = SDL_GetTicks();
801                         frames = 0;
802
803                         if (!engine.paused)
804                         {
805                                 audio.resume();
806                         }
807
808                         SDL_Delay(16);
809                 }
810                 
811                 if ((engine.keyState[SDLK_F3]) && (engine.cheatSkipLevel))
812                 {
813                         autoCompleteAllObjectives(true);
814                         engine.keyState[SDLK_F3] = 0;
815                         engine.setInfoMessage("Skipping Mission...", 2, INFO_OBJECTIVE);
816                 }
817                 
818                 #if DEBUG
819                 if (engine.keyState[SDLK_F1])
820                 {
821                         autoCompleteAllObjectives(false);
822                 }               
823                 #endif
824                 
825                 if (replayData.replayMode != REPLAY_MODE::PLAYBACK)
826                 {
827                         engine.delay(frameLimit);
828                 }
829                 else if (!replayData.fast)
830                 {
831                         engine.delay(frameLimit);
832                 }
833                 
834                 if (engine.keyState[SDLK_F5])
835                 {
836                         replayData.fast = !replayData.fast;
837                         engine.keyState[SDLK_F5] = 0;
838                 }
839                 
840                 frameLimit = SDL_GetTicks()  + 16;
841                 
842                 if (game.missionOverReason == MIS_GAMECOMPLETE)
843                         frameLimit = SDL_GetTicks() + 64;
844
845                 #if DEBUG
846                 static Graphics::SurfaceCache fpsCache;
847                 graphics.drawString(fps, 600, 30, true, graphics.screen, fpsCache);
848
849                 if (SDL_GetTicks() > frameCounter + 500)
850                 {
851                         now = SDL_GetTicks();
852                         snprintf(fps, sizeof fps, "%2.2f fps", ((double)frames*1000)/(now - then));
853                         then = frameCounter = SDL_GetTicks();
854                         frames = 0;
855                 }
856                 #endif
857         }
858
859         if (allObjectivesCompleted())
860         {
861                 if (strcmp(map.name, "Final Battle") == 0)
862                 {
863                         game.missionOverReason = MIS_GAMECOMPLETE;
864                 }
865                 else
866                 {
867                         game.missionOverReason = MIS_COMPLETE;
868                 }
869         }
870
871         switch (game.missionOverReason)
872         {
873                 case MIS_COMPLETE:
874                         if (strcmp(map.name, "Space Station"))
875                         {
876                                 graphics.delay(1000);
877                                 audio.loadMusic("music/grasslands");
878                                 audio.playMusic();
879                                 graphics.fadeToBlack();
880                                 
881                                 bool previouslyCompleted = gameData.stagePreviouslyCleared(game.stageName);
882                                 
883                                 showMissionClear();
884                                 
885                                 if (engine.practice)
886                                 {
887                                         return SECTION_TITLE;
888                                 }
889                                 
890                                 if (!previouslyCompleted)
891                                 {
892                                         checkEndCutscene();
893                                 }
894                                 return SECTION_HUB;
895                         }
896                         else
897                         {
898                                 graphics.fadeToBlack();
899                                 processPostMissionData();
900                                 saveGame();
901                                 game.setMapName("data/finalBattle");
902                                 game.setStageName("Final Battle");
903                                 return SECTION_GAME;
904                         }
905                         break;
906                         
907                 case MIS_GAMECOMPLETE:
908                         SDL_FillRect(graphics.screen, NULL, graphics.white);
909                         graphics.updateScreen();
910                         graphics.fadeToBlack();
911                         
912                         // we've finished the game. Extreme mode is now available! :)
913                         engine.extremeAvailable = true;
914                         map.clear();
915                         graphics.free();
916                         audio.free();
917                         checkEndCutscene();
918                         return SECTION_CREDITS;
919                         break;
920
921                 case MIS_TIMEUP:
922                         game.canContinue = 0;
923                 case MIS_PLAYERDEAD:
924                         if (player.health > -60)
925                         {
926                                 player.health = -99;
927                                 gibPlayer();
928                         }
929                         return SECTION_GAMEOVER;
930                         break;
931
932                 case MIS_PLAYERESCAPE:
933                         game.escapes++;
934                         if (gameData.stagePreviouslyCleared(game.stageName))
935                         {
936                                 processPostMissionData();
937                                 saveGame();
938                         }
939                         return SECTION_HUB;
940                         break;
941                         
942                 case MIS_PLAYERRESTART:
943                         clearAllMissionData();
944                         return SECTION_GAME;
945                         break;
946
947                 default:
948                         return SECTION_TITLE;
949                         break;
950         }
951
952         return SECTION_TITLE;
953 }