]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/game.cpp
Allow the game to run without sounds.
[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
300         audio.loadGameOverMusic();
301         audio.playMusic();
302
303         engine.flushInput();
304         engine.clearInput();
305
306         unsigned int frameLimit = SDL_GetTicks() + 16;
307
308         bool showGameOverOptions = false;
309         int cont, quit, menuSound;
310         cont = quit = menuSound = 0;
311
312         engine.setWidgetVariable("gameOverNo", &cont);
313         engine.setWidgetVariable("gameOverYes", &quit);
314         
315         if (game.canContinue > 1)
316         {
317                 Widget *widget = engine.getWidgetByName("gameOverNo");
318                 sprintf(widget->label, "%s (%d)", widget->label, game.canContinue);
319         }
320
321         while (true)
322         {
323                 if (menuSound)
324                         audio.playMenuSound(menuSound);
325
326                 doGameStuff();
327                 drawMapTopLayer();
328
329                 graphics.blit(gameover, 320, 240, graphics.screen, true);
330
331                 if (engine.userAccepts())
332                 {
333                         if (!showGameOverOptions)
334                         {
335                                 showGameOverOptions = true;
336                                 engine.showWidgetGroup("gameover", true);
337                                 engine.highlightWidget("gameOverNo");
338                                 engine.flushInput();
339                                 engine.clearInput();
340                         }
341
342                         // Can't continue on a boss mission or if no checkpoints reached!
343                         if ((map.isBossMission) || (!game.canContinue))
344                         {
345                                 engine.showWidgetGroup("gameover", false);
346                                 quit = 1;
347                         }
348                 }
349                 
350                 if (showGameOverOptions)
351                 {
352                         drawWidgets();
353                         menuSound = engine.processWidgets();
354                 }
355
356                 if ((cont) || (quit))
357                         break;
358
359                 engine.delay(frameLimit);
360                 frameLimit = SDL_GetTicks() + 16;
361         }
362
363         if (quit)
364         {
365                 audio.fadeMusic();
366                 graphics.fadeToBlack();
367                 map.clear();
368                 
369                 return SECTION_TITLE;
370         }
371
372         game.continueFromCheckPoint = true;
373         audio.stopMusic();
374         audio.reloadLevelMusic();
375
376         return SECTION_GAME;
377 }
378
379 void showMissionInformation()
380 {
381         SDL_FillRect(graphics.screen, NULL, graphics.black);
382         graphics.updateScreen();
383
384         SDL_Surface *panel = graphics.createSurface(400, 300);
385         SDL_Surface *panelBack = graphics.alphaRect(400, 300, 0x00, 0x00, 0x00);
386         SDL_SetColorKey(panel, (SDL_SRCCOLORKEY|SDL_RLEACCEL), SDL_MapRGB(panel->format, 0, 0, 0));
387
388         graphics.drawRect(1, 1, 398, 298, graphics.black, graphics.white, panelBack);
389
390         char message[100];
391         int col1 = 25;
392         int col2 = 375;
393         int y = 30;
394
395         Objective *objective;
396
397         graphics.setFontSize(0);
398
399         graphics.setFontSize(3);
400         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
401         graphics.drawString(_(game.stageName), 200, 20, TXT_CENTERED, panel);
402
403         graphics.setFontSize(0);
404
405         y += 20;
406
407         if (map.totalMIAs > 0)
408         {
409                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
410                 sprintf(message, _("Rescue %d MIAs"), map.requiredMIAs);
411                 graphics.drawString(message, col1, y, TXT_LEFT, panel);
412
413                 if (map.foundMIAs < map.requiredMIAs)
414                 {
415                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
416                         sprintf(message, "%d / %d", map.foundMIAs, map.requiredMIAs);
417                         graphics.drawString(message, col2, y, TXT_RIGHT, panel);
418                 }
419                 else
420                 {
421                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
422                         graphics.drawString(_("Completed"), col2, y, TXT_RIGHT, panel);
423                 }
424         }
425
426         objective = (Objective*)map.objectiveList.getHead();
427
428         while (objective->next != NULL)
429         {
430                 objective = (Objective*)objective->next;
431
432                 y += 20;
433
434                 graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
435                 
436                 if ((game.skill < 3) &&  (strstr(objective->description, "L.R.T.S.")) && (!gameData.completedWorld))
437                 {
438                         graphics.drawString(_("???? ???????? ????"), col1, y, TXT_LEFT, panel);
439                 }
440                 else
441                 {
442                         graphics.drawString(_(objective->description), col1, y, TXT_LEFT, panel);
443                 }
444                 
445                 // this is a fake objective (for the 4th Ancient Tomb)
446                 if (objective->targetValue == -1)
447                 {
448                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
449                         graphics.drawString(_("Incomplete"), col2, y, TXT_RIGHT, panel);
450                 }
451                 else if (objective->currentValue < objective->targetValue)
452                 {
453                         graphics.setFontColor(0xff, 0x00, 0x00, 0x00, 0x00, 0x00);
454                         if (objective->targetValue == 1)
455                         {
456                                 graphics.drawString(_("Incomplete"), col2, y, TXT_RIGHT, panel);
457                         }
458                         else
459                         {
460                                 sprintf(message, "%d / %d", objective->currentValue, objective->targetValue);
461                                 graphics.drawString(message, col2, y, TXT_RIGHT, panel);
462                         }
463                 }
464                 else
465                 {
466                         graphics.setFontColor(0x00, 0xff, 0x00, 0x00, 0x00, 0x00);
467                         graphics.drawString(_("Completed"), col2, y, TXT_RIGHT, panel);
468                 }
469         }
470         
471         if (game.skill == 3)
472         {
473                 graphics.setFontColor(0xff, 0xff, 0x00, 0x00, 0x00, 0x00);
474                 sprintf(message, _("Time Limit - %d:%.2d Minutes"), map.remainingMinutes, map.remainingSeconds);
475                 graphics.drawString(message, 200, 260, TXT_CENTERED, panel);
476         }
477         
478         graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
479         graphics.drawString(_("Press Fire to Continue"), 200, 280, TXT_CENTERED, panel);
480
481         engine.flushInput();
482         engine.clearInput();
483
484         int px, py;
485
486         map.getRandomEntityPosition(&px, &py);
487         map.getRandomEntityPosition(&player.tx, &player.ty);
488
489         player.x = px;
490         player.y = py;
491
492         unsigned int frameLimit = SDL_GetTicks() + 16;
493                 
494         while (true)
495         {
496                 if ((int)player.x < player.tx) player.x += 2;
497                 if ((int)player.x > player.tx) player.x -= 2;
498                 if ((int)player.y < player.ty) player.y += 2;
499                 if ((int)player.y > player.ty) player.y -= 2;
500
501                 if (Collision::collision(player.x, player.y, 5, 5, player.tx, player.ty, 5, 5))
502                         map.getRandomEntityPosition(&player.tx, &player.ty);
503
504                 engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
505
506                 doGameStuff();
507                 drawMapTopLayer();
508                 graphics.blit(panelBack, 320, 220, graphics.screen, true);
509                 graphics.blit(panel, 320, 220, graphics.screen, true);
510
511                 engine.delay(frameLimit);
512                 frameLimit = SDL_GetTicks() + 16;
513
514                 if (engine.userAccepts())
515                         break;
516         }
517
518         SDL_FreeSurface(panel);
519         SDL_FreeSurface(panelBack);
520
521         SDL_FillRect(graphics.screen, NULL, graphics.black);
522         graphics.delay(1000);
523 }
524
525 void beamInPlayer()
526 {
527         game.getCheckPoint(&player.x, &player.y);
528         
529         int beamInTime = 180;
530         
531         unsigned int frameLimit = SDL_GetTicks() + 16;
532         
533         audio.playSound(SND_TELEPORT1, CH_ANY);
534         
535         engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
536         
537         while (beamInTime > 0)
538         {
539                 engine.delay(frameLimit);
540                 frameLimit = SDL_GetTicks() + 16;
541                 
542                 doGameStuff();
543                 drawMapTopLayer();
544                 
545                 if ((beamInTime % 10) == 0)
546                         addTeleportParticles(player.x + 10, player.y + 10, 50, -1);
547                 
548                 beamInTime--;
549         }
550         
551         game.getCheckPoint(&player.x, &player.y);
552         
553         player.dx = 0;
554         player.dy = 0;
555         player.immune = 120;
556         player.environment = ENV_AIR;
557         player.oxygen = 7;
558         player.fuel = 7;
559         Math::removeBit(&player.flags, ENT_FLIES);
560 }
561
562 int doGame()
563 {
564         graphics.setFontSize(0);
565
566         SDL_FillRect(graphics.screen, NULL, graphics.black);
567         graphics.delay(1000);
568
569         Uint32 then, frames, frameLimit, millis, frameCounter;
570
571         #if !USEPAK
572         Uint32 now;
573         char fps[10];
574         strcpy(fps, "fps");
575         #endif
576
577         engine.messageTime = -1;
578         engine.messagePriority = -1;
579
580         audio.playMusic();
581         audio.playAmbiance();
582
583         if (!game.continueFromCheckPoint)
584         {
585                 player.health = -99;
586
587                 //#if USEPAK
588                 if ((!map.isBossMission) && (replayData.replayMode == REPLAY_MODE::NONE))
589                 {
590                         showMissionInformation();
591                 }
592                 //#endif
593
594                 game.levelsStarted++;
595         }
596         else
597         {
598                 game.useObjectiveCheckPoint();
599         }
600
601         player.setVelocity(0, 1);
602         player.baseThink = 60;
603         player.health = MAX_HEALTH;
604
605         if (game.continueFromCheckPoint)
606         {
607                 player.health = (MAX_HEALTH / 2);
608                 player.setSprites(graphics.getSprite("BobRight", true), graphics.getSprite("BobLeft", true), graphics.getSprite("BobSpin", true));
609         }
610         else
611         {
612                 player.currentWeapon = &weapon[WP_PISTOL];
613         }
614
615         game.resetMissionOver();
616
617         frameLimit = SDL_GetTicks() + 16;
618         frames = millis = 0;
619         then = SDL_GetTicks();
620         frameCounter = SDL_GetTicks();
621
622         if ((strcmp(map.name, "Space Station") == 0) && (!game.continueFromCheckPoint))
623         {
624                 beamInPlayer();
625         }
626         else
627         {
628                 resetPlayer();
629         }
630
631         engine.flushInput();
632         engine.clearInput();
633
634         debug(("Map Clipping is %d %d %d %d\n", map.limitLeft, map.limitRight, map.limitUp, map.limitDown));
635
636         game.continueFromCheckPoint = false;
637
638         engine.paused = false;
639
640         while (true)
641         {
642                 ++frames;
643                 ++millis;
644
645                 if (game.missionOverReason != MIS_PLAYEROUT)
646                 {
647                         engine.setPlayerPosition((int)player.x, (int)player.y, map.limitLeft, map.limitRight, map.limitUp, map.limitDown);
648                 }
649
650                 doSpawnPoints();
651                 doGameStuff();
652
653                 doPlayer();
654                 raiseWaterLevel();
655
656                 if ((player.environment == ENV_SLIME) || (player.environment == ENV_LAVA) || (player.health < 1))
657                 {
658                         if (player.health < 1)
659                         {
660                                 game.setMissionOver(MIS_PLAYERDEAD);
661                                 audio.fadeMusic();
662                                 audio.stopAmbiance();
663                         }
664                         else
665                         {
666                                 game.setMissionOver(MIS_PLAYEROUT);
667                                 player.immune = 130;
668                         }
669                 }
670
671                 if (config.isControl(CONTROL::MAP))
672                 {
673                         if (!map.isBossMission)
674                         {
675                                 showMap((int)(player.x / 32), (int)(player.y / 32));
676                         }
677                         else
678                         {
679                                 engine.setInfoMessage("Automap is not available!", 1, INFO_HINT);
680                         }
681                 }
682
683                 drawMapTopLayer();
684                 doStatusBar();
685
686                 if ((engine.keyState[SDLK_ESCAPE]) && (game.missionOver == 0))
687                 {
688                         if (replayData.replayMode == REPLAY_MODE::NONE)
689                         {
690                                 showInGameOptions();
691                         }
692                         else
693                         {
694                                 exit(0);
695                         }
696                 }
697
698                 if (allObjectivesCompleted())
699                 {
700                         if (game.missionOver == 0)
701                         {
702                                 if (map.isBossMission)
703                                 {
704                                         map.killAllEnemies();
705                                 }
706                                 
707                                 audio.stopMusic();
708                                 audio.stopAmbiance();
709
710                                 game.setMissionOver(MIS_COMPLETE);
711                                 engine.setInfoMessage("All Required Objectives Met - Mission Complete", 10, INFO_OBJECTIVE);
712                         }
713                 }
714
715                 if (game.missionOver > 0)
716                 {
717                         game.missionOver--;
718                 
719                         if (game.missionOver == 0)
720                         {
721                                 if (game.missionOverReason == MIS_PLAYEROUT)
722                                 {
723                                         SDL_FillRect(graphics.screen, NULL, graphics.black);
724                                         graphics.updateScreen();
725                                         graphics.delay(1000);
726                                         engine.flushInput();
727                                         engine.clearInput();
728                                         resetPlayer();
729                                         game.resetMissionOver();
730                                 }
731                                 else
732                                 {
733                                         if (game.missionOverReason == MIS_COMPLETE)
734                                         {
735                                                 game.missionOver = MAX_FPS * 2;
736                                                 
737                                                 if (strcmp(map.name, "Space Station") != 0)
738                                                 {
739                                                         addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
740                                                         dropCarriedItems();
741                                                         game.missionOverReason = MIS_PLAYERESCAPE;
742                                                 }
743                                                 else
744                                                 {
745                                                         break;
746                                                 }
747                                         }
748                                         else if (game.missionOverReason == MIS_GAMECOMPLETE)
749                                         {
750                                                 addTeleportParticles(player.x, player.y, 50, SND_TELEPORT3);
751                                                 game.missionOverReason = MIS_PLAYERESCAPE;
752                                                 game.missionOver = MAX_FPS * 4;
753                                         }
754                                         else
755                                         {
756                                                 if (replayData.replayMode != REPLAY_MODE::NONE)
757                                                 {
758                                                         exit(0);
759                                                 }
760                                         
761                                                 break;
762                                         }
763                                 }
764                         }
765                 }
766
767                 Math::limitInt(&(--game.lastComboTime), 0, 60);
768
769                 if (millis >= 60)
770                 {
771                         millis = 0;
772                         
773                         if ((game.missionOverReason == MIS_INPROGRESS) || (game.missionOverReason == MIS_PLAYEROUT))
774                         {
775                                 game.incrementMissionTime();
776                                 
777                                 if (game.skill == 3)
778                                 {
779                                         doTimeRemaining();
780                                 }
781                         }
782                 }
783
784                 if (engine.paused)
785                 {
786                         doPauseInfo();
787                         audio.pause();
788                 }
789
790                 while (engine.paused)
791                 {
792                         engine.getInput();
793                         config.populate();
794                         config.doPause();
795                         graphics.updateScreen();
796                         then = SDL_GetTicks();
797                         frames = 0;
798
799                         if (!engine.paused)
800                         {
801                                 audio.resume();
802                         }
803
804                         SDL_Delay(16);
805                 }
806                 
807                 if ((engine.keyState[SDLK_F3]) && (engine.cheatSkipLevel))
808                 {
809                         autoCompleteAllObjectives(true);
810                         engine.setInfoMessage("Skipping Mission...", 2, INFO_OBJECTIVE);
811                 }
812                 
813                 #if !USEPAK
814                 if (engine.keyState[SDLK_F1])
815                 {
816                         autoCompleteAllObjectives(false);
817                 }               
818                 #endif
819                 
820                 if (replayData.replayMode != REPLAY_MODE::PLAYBACK)
821                 {
822                         engine.delay(frameLimit);
823                 }
824                 else if (!replayData.fast)
825                 {
826                         engine.delay(frameLimit);
827                 }
828                 
829                 if (engine.keyState[SDLK_F5])
830                 {
831                         replayData.fast = !replayData.fast;
832                         engine.keyState[SDLK_F5] = 0;
833                 }
834                 
835                 frameLimit = SDL_GetTicks()  + 16;
836                 
837                 if (game.missionOverReason == MIS_GAMECOMPLETE)
838                         frameLimit = SDL_GetTicks() + 64;
839
840                 #if !USEPAK
841                 graphics.drawString(fps, 600, 30, true, graphics.screen);
842
843                 if (SDL_GetTicks() > frameCounter + 500)
844                 {
845                         now = SDL_GetTicks();
846                         sprintf(fps, "%2.2f fps", ((double)frames*1000)/(now - then));
847                         then = frameCounter = SDL_GetTicks();
848                         frames = 0;
849                 }
850                 #endif
851         }
852
853         if (allObjectivesCompleted())
854         {
855                 if (strcmp(map.name, "Final Battle") == 0)
856                 {
857                         game.missionOverReason = MIS_GAMECOMPLETE;
858                 }
859                 else
860                 {
861                         game.missionOverReason = MIS_COMPLETE;
862                 }
863         }
864
865         switch (game.missionOverReason)
866         {
867                 case MIS_COMPLETE:
868                         if (strcmp(map.name, "Space Station"))
869                         {
870                                 graphics.delay(1000);
871                                 audio.loadMusic("music/commando.xm");
872                                 audio.playMusic();
873                                 graphics.fadeToBlack();
874                                 
875                                 bool previouslyCompleted = gameData.stagePreviouslyCleared(game.stageName);
876                                 
877                                 showMissionClear();
878                                 
879                                 if (engine.practice)
880                                 {
881                                         return SECTION_TITLE;
882                                 }
883                                 
884                                 if (!previouslyCompleted)
885                                 {
886                                         checkEndCutscene();
887                                 }
888                                 return SECTION_HUB;
889                         }
890                         else
891                         {
892                                 graphics.fadeToBlack();
893                                 processPostMissionData();
894                                 saveGame();
895                                 game.setMapName("data/finalBattle");
896                                 game.setStageName("Final Battle");
897                                 return SECTION_GAME;
898                         }
899                         break;
900                         
901                 case MIS_GAMECOMPLETE:
902                         SDL_FillRect(graphics.screen, NULL, graphics.white);
903                         graphics.updateScreen();
904                         graphics.fadeToBlack();
905                         
906                         // we've finished the game. Extreme mode is now available! :)
907                         engine.extremeAvailable = true;
908                         map.clear();
909                         graphics.free();
910                         audio.free();
911                         checkEndCutscene();
912                         return SECTION_CREDITS;
913                         break;
914
915                 case MIS_TIMEUP:
916                         game.canContinue = 0;
917                 case MIS_PLAYERDEAD:
918                         if (player.health > -60)
919                         {
920                                 player.health = -99;
921                                 gibPlayer();
922                         }
923                         return SECTION_GAMEOVER;
924                         break;
925
926                 case MIS_PLAYERESCAPE:
927                         game.escapes++;
928                         if (gameData.stagePreviouslyCleared(game.stageName))
929                         {
930                                 processPostMissionData();
931                                 saveGame();
932                         }
933                         return SECTION_HUB;
934                         break;
935                         
936                 case MIS_PLAYERRESTART:
937                         clearAllMissionData();
938                         return SECTION_GAME;
939                         break;
940
941                 default:
942                         return SECTION_TITLE;
943                         break;
944         }
945
946         return SECTION_TITLE;
947 }