]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/game.cpp
8257e20164fddcdab2ba29797573ab73a0b2c22d
[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                 strncat(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[100];
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
574         #if !USEPAK
575         Uint32 now;
576         char fps[10];
577         strncpy(fps, "fps", sizeof fps);
578         #endif
579
580         engine.messageTime = -1;
581         engine.messagePriority = -1;
582
583         audio.playMusic();
584         audio.playAmbiance();
585
586         if (!game.continueFromCheckPoint)
587         {
588                 player.health = -99;
589
590                 //#if USEPAK
591                 if ((!map.isBossMission) && (replayData.replayMode == REPLAY_MODE::NONE))
592                 {
593                         showMissionInformation();
594                 }
595                 //#endif
596
597                 game.levelsStarted++;
598         }
599         else
600         {
601                 game.useObjectiveCheckPoint();
602         }
603
604         player.setVelocity(0, 1);
605         player.baseThink = 60;
606         player.health = MAX_HEALTH;
607
608         if (game.continueFromCheckPoint)
609         {
610                 player.health = (MAX_HEALTH / 2);
611                 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
612         }
613         else
614         {
615                 player.currentWeapon = &weapon[WP_PISTOL];
616         }
617
618         game.resetMissionOver();
619
620         frameLimit = SDL_GetTicks() + 16;
621         frames = millis = 0;
622         then = SDL_GetTicks();
623         frameCounter = SDL_GetTicks();
624
625         if ((strcmp(map.name, "Space Station") == 0) && (!game.continueFromCheckPoint))
626         {
627                 beamInPlayer();
628         }
629         else
630         {
631                 resetPlayer();
632         }
633
634         engine.flushInput();
635         engine.clearInput();
636
637         debug(("Map Clipping is %d %d %d %d\n", map.limitLeft, map.limitRight, map.limitUp, map.limitDown));
638
639         game.continueFromCheckPoint = false;
640
641         engine.paused = false;
642
643         while (true)
644         {
645                 ++frames;
646                 ++millis;
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
689                 if ((engine.keyState[SDLK_ESCAPE]) && (game.missionOver == 0))
690                 {
691                         if (replayData.replayMode == REPLAY_MODE::NONE)
692                         {
693                                 showInGameOptions();
694                         }
695                         else
696                         {
697                                 exit(0);
698                         }
699                 }
700
701                 if (allObjectivesCompleted())
702                 {
703                         if (game.missionOver == 0)
704                         {
705                                 if (map.isBossMission)
706                                 {
707                                         map.killAllEnemies();
708                                 }
709                                 
710                                 audio.stopMusic();
711                                 audio.stopAmbiance();
712
713                                 game.setMissionOver(MIS_COMPLETE);
714                                 engine.setInfoMessage("All Required Objectives Met - Mission Complete", 10, INFO_OBJECTIVE);
715                         }
716                 }
717
718                 if (game.missionOver > 0)
719                 {
720                         game.missionOver--;
721                 
722                         if (game.missionOver == 0)
723                         {
724                                 if (game.missionOverReason == MIS_PLAYEROUT)
725                                 {
726                                         SDL_FillRect(graphics.screen, NULL, graphics.black);
727                                         graphics.updateScreen();
728                                         graphics.delay(1000);
729                                         engine.flushInput();
730                                         engine.clearInput();
731                                         resetPlayer();
732                                         game.resetMissionOver();
733                                 }
734                                 else
735                                 {
736                                         if (game.missionOverReason == MIS_COMPLETE)
737                                         {
738                                                 game.missionOver = MAX_FPS * 2;
739                                                 
740                                                 if (strcmp(map.name, "Space Station") != 0)
741                                                 {
742                                                         addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
743                                                         dropCarriedItems();
744                                                         game.missionOverReason = MIS_PLAYERESCAPE;
745                                                 }
746                                                 else
747                                                 {
748                                                         break;
749                                                 }
750                                         }
751                                         else if (game.missionOverReason == MIS_GAMECOMPLETE)
752                                         {
753                                                 addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
754                                                 game.missionOverReason = MIS_PLAYERESCAPE;
755                                                 game.missionOver = MAX_FPS * 4;
756                                         }
757                                         else
758                                         {
759                                                 if (replayData.replayMode != REPLAY_MODE::NONE)
760                                                 {
761                                                         exit(0);
762                                                 }
763                                         
764                                                 break;
765                                         }
766                                 }
767                         }
768                 }
769
770                 Math::limitInt(&(--game.lastComboTime), 0, 60);
771
772                 if (millis >= 60)
773                 {
774                         millis = 0;
775                         
776                         if ((game.missionOverReason == MIS_INPROGRESS) || (game.missionOverReason == MIS_PLAYEROUT))
777                         {
778                                 game.incrementMissionTime();
779                                 
780                                 if (game.skill == 3)
781                                 {
782                                         doTimeRemaining();
783                                 }
784                         }
785                 }
786
787                 if (engine.paused)
788                 {
789                         doPauseInfo();
790                         audio.pause();
791                 }
792
793                 while (engine.paused)
794                 {
795                         engine.getInput();
796                         config.populate();
797                         config.doPause();
798                         graphics.updateScreen();
799                         then = SDL_GetTicks();
800                         frames = 0;
801
802                         if (!engine.paused)
803                         {
804                                 audio.resume();
805                         }
806
807                         SDL_Delay(16);
808                 }
809                 
810                 if ((engine.keyState[SDLK_F3]) && (engine.cheatSkipLevel))
811                 {
812                         autoCompleteAllObjectives(true);
813                         engine.keyState[SDLK_F3] = 0;
814                         engine.setInfoMessage("Skipping Mission...", 2, INFO_OBJECTIVE);
815                 }
816                 
817                 #if !USEPAK
818                 if (engine.keyState[SDLK_F1])
819                 {
820                         autoCompleteAllObjectives(false);
821                 }               
822                 #endif
823                 
824                 if (replayData.replayMode != REPLAY_MODE::PLAYBACK)
825                 {
826                         engine.delay(frameLimit);
827                 }
828                 else if (!replayData.fast)
829                 {
830                         engine.delay(frameLimit);
831                 }
832                 
833                 if (engine.keyState[SDLK_F5])
834                 {
835                         replayData.fast = !replayData.fast;
836                         engine.keyState[SDLK_F5] = 0;
837                 }
838                 
839                 frameLimit = SDL_GetTicks()  + 16;
840                 
841                 if (game.missionOverReason == MIS_GAMECOMPLETE)
842                         frameLimit = SDL_GetTicks() + 64;
843
844                 #if !USEPAK
845                 graphics.drawString(fps, 600, 30, true, graphics.screen);
846
847                 if (SDL_GetTicks() > frameCounter + 500)
848                 {
849                         now = SDL_GetTicks();
850                         snprintf(fps, sizeof fps, "%2.2f fps", ((double)frames*1000)/(now - then));
851                         then = frameCounter = SDL_GetTicks();
852                         frames = 0;
853                 }
854                 #endif
855         }
856
857         if (allObjectivesCompleted())
858         {
859                 if (strcmp(map.name, "Final Battle") == 0)
860                 {
861                         game.missionOverReason = MIS_GAMECOMPLETE;
862                 }
863                 else
864                 {
865                         game.missionOverReason = MIS_COMPLETE;
866                 }
867         }
868
869         switch (game.missionOverReason)
870         {
871                 case MIS_COMPLETE:
872                         if (strcmp(map.name, "Space Station"))
873                         {
874                                 graphics.delay(1000);
875                                 audio.loadMusic("music/commando.xm");
876                                 audio.playMusic();
877                                 graphics.fadeToBlack();
878                                 
879                                 bool previouslyCompleted = gameData.stagePreviouslyCleared(game.stageName);
880                                 
881                                 showMissionClear();
882                                 
883                                 if (engine.practice)
884                                 {
885                                         return SECTION_TITLE;
886                                 }
887                                 
888                                 if (!previouslyCompleted)
889                                 {
890                                         checkEndCutscene();
891                                 }
892                                 return SECTION_HUB;
893                         }
894                         else
895                         {
896                                 graphics.fadeToBlack();
897                                 processPostMissionData();
898                                 saveGame();
899                                 game.setMapName("data/finalBattle");
900                                 game.setStageName("Final Battle");
901                                 return SECTION_GAME;
902                         }
903                         break;
904                         
905                 case MIS_GAMECOMPLETE:
906                         SDL_FillRect(graphics.screen, NULL, graphics.white);
907                         graphics.updateScreen();
908                         graphics.fadeToBlack();
909                         
910                         // we've finished the game. Extreme mode is now available! :)
911                         engine.extremeAvailable = true;
912                         map.clear();
913                         graphics.free();
914                         audio.free();
915                         checkEndCutscene();
916                         return SECTION_CREDITS;
917                         break;
918
919                 case MIS_TIMEUP:
920                         game.canContinue = 0;
921                 case MIS_PLAYERDEAD:
922                         if (player.health > -60)
923                         {
924                                 player.health = -99;
925                                 gibPlayer();
926                         }
927                         return SECTION_GAMEOVER;
928                         break;
929
930                 case MIS_PLAYERESCAPE:
931                         game.escapes++;
932                         if (gameData.stagePreviouslyCleared(game.stageName))
933                         {
934                                 processPostMissionData();
935                                 saveGame();
936                         }
937                         return SECTION_HUB;
938                         break;
939                         
940                 case MIS_PLAYERRESTART:
941                         clearAllMissionData();
942                         return SECTION_GAME;
943                         break;
944
945                 default:
946                         return SECTION_TITLE;
947                         break;
948         }
949
950         return SECTION_TITLE;
951 }