]> icculus.org git repositories - taylor/freespace2.git/blob - src/freespace2/freespace.cpp
implemented osregistry
[taylor/freespace2.git] / src / freespace2 / freespace.cpp
1 /*
2  * $Logfile: /Freespace2/code/Freespace2/FreeSpace.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Freespace main body
8  *
9  * $Log$
10  * Revision 1.12  2002/06/02 00:31:35  relnev
11  * implemented osregistry
12  *
13  * Revision 1.11  2002/06/01 09:00:34  relnev
14  * silly debug memmanager
15  *
16  * Revision 1.10  2002/06/01 07:12:32  relnev
17  * a few NDEBUG updates.
18  *
19  * removed a few warnings.
20  *
21  * Revision 1.9  2002/05/31 03:05:59  relnev
22  * sane default
23  *
24  * Revision 1.8  2002/05/29 02:52:32  theoddone33
25  * Enable OpenGL renderer
26  *
27  * Revision 1.7  2002/05/28 08:52:03  relnev
28  * implemented two assembly stubs.
29  *
30  * cleaned up a few warnings.
31  *
32  * added a little demo hackery to make it progress a little farther.
33  *
34  * Revision 1.6  2002/05/28 06:28:20  theoddone33
35  * Filesystem mods, actually reads some data files now
36  *
37  * Revision 1.5  2002/05/28 04:07:28  theoddone33
38  * New graphics stubbing arrangement
39  *
40  * Revision 1.4  2002/05/27 22:46:52  theoddone33
41  * Remove more undefined symbols
42  *
43  * Revision 1.3  2002/05/26 23:31:18  relnev
44  * added a few files that needed to be compiled
45  *
46  * freespace.cpp: now compiles
47  *
48  * Revision 1.2  2002/05/07 03:16:44  theoddone33
49  * The Great Newline Fix
50  *
51  * Revision 1.1.1.1  2002/05/03 03:28:09  root
52  * Initial import.
53  *
54  * 
55  * 201   6/16/00 3:15p Jefff
56  * sim of the year dvd version changes, a few german soty localization
57  * fixes
58  * 
59  * 200   11/03/99 11:06a Jefff
60  * 1.2 checksums
61  * 
62  * 199   10/26/99 5:07p Jamest
63  * fixed jeffs dumb debug code
64  * 
65  * 198   10/25/99 5:53p Jefff
66  * call control_config_common_init() on startup
67  * 
68  * 197   10/14/99 10:18a Daveb
69  * Fixed incorrect CD checking problem on standalone server.
70  * 
71  * 196   10/13/99 9:22a Daveb
72  * Fixed Fred jumpnode placing bug. Fixed 1024 glide tiled texture problem
73  * related to movies. Fixed launcher spawning from PXO screen.
74  * 
75  * 195   10/06/99 11:05a Jefff
76  * new oem upsell 3 hotspot coords
77  * 
78  * 194   10/06/99 10:31a Jefff
79  * OEM updates
80  * 
81  * 193   10/01/99 9:10a Daveb
82  * V 1.1 PATCH
83  * 
84  * 192   9/15/99 4:57a Dave
85  * Updated ships.tbl checksum
86  * 
87  * 191   9/15/99 3:58a Dave
88  * Removed framerate warning at all times.
89  * 
90  * 190   9/15/99 3:16a Dave
91  * Remove mt-011.fs2 from the builtin mission list.
92  * 
93  * 189   9/15/99 1:45a Dave
94  * Don't init joystick on standalone. Fixed campaign mode on standalone.
95  * Fixed no-score-report problem in TvT
96  * 
97  * 188   9/14/99 6:08a Dave
98  * Updated (final) single, multi, and campaign list.
99  * 
100  * 187   9/14/99 3:26a Dave
101  * Fixed laser fogging problem in nebula (D3D)> Fixed multiplayer
102  * respawn-too-early problem. Made a few crash points safe.
103  * 
104  * 186   9/13/99 4:52p Dave
105  * RESPAWN FIX
106  * 
107  * 185   9/12/99 8:09p Dave
108  * Fixed problem where skip-training button would cause mission messages
109  * not to get paged out for the current mission.
110  * 
111  * 184   9/10/99 11:53a Dave
112  * Shutdown graphics before sound to eliminate apparent lockups when
113  * Directsound decides to be lame. Fix TOPMOST problem with D3D windows.
114  * 
115  * 183   9/09/99 11:40p Dave
116  * Handle an Assert() in beam code. Added supernova sounds. Play the right
117  * 2 end movies properly, based upon what the player did in the mission.
118  * 
119  * 182   9/08/99 10:29p Dave
120  * Make beam sound pausing and unpausing much safer.
121  * 
122  * 181   9/08/99 10:01p Dave
123  * Make sure game won't run in a drive's root directory. Make sure
124  * standalone routes suqad war messages properly to the host.
125  * 
126  * 180   9/08/99 3:22p Dave
127  * Updated builtin mission list.
128  * 
129  * 179   9/08/99 12:01p Jefff
130  * fixed Game_builtin_mission_list typo on Training-2.fs2
131  * 
132  * 178   9/08/99 9:48a Andsager
133  * Add force feedback for engine wash.
134  * 
135  * 177   9/07/99 4:01p Dave
136  * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
137  * does everything properly (setting up address when binding). Remove
138  * black rectangle background from UI_INPUTBOX.
139  * 
140  * 176   9/13/99 2:40a Dave
141  * Comment in full 80 minute CD check for RELEASE_REAL builds.
142  * 
143  * 175   9/06/99 6:38p Dave
144  * Improved CD detection code.
145  * 
146  * 174   9/06/99 1:30a Dave
147  * Intermediate checkin. Started on enforcing CD-in-drive to play the
148  * game.
149  * 
150  * 173   9/06/99 1:16a Dave
151  * Make sure the user sees the intro movie.
152  * 
153  * 172   9/04/99 8:00p Dave
154  * Fixed up 1024 and 32 bit movie support.
155  * 
156  * 171   9/03/99 1:32a Dave
157  * CD checking by act. Added support to play 2 cutscenes in a row
158  * seamlessly. Fixed super low level cfile bug related to files in the
159  * root directory of a CD. Added cheat code to set campaign mission # in
160  * main hall.
161  * 
162  * 170   9/01/99 10:49p Dave
163  * Added nice SquadWar checkbox to the client join wait screen.
164  * 
165  * 169   9/01/99 10:14a Dave
166  * Pirate bob.
167  * 
168  * 168   8/29/99 4:51p Dave
169  * Fixed damaged checkin.
170  * 
171  * 167   8/29/99 4:18p Andsager
172  * New "burst" limit for friendly damage.  Also credit more damage done
173  * against large friendly ships.
174  * 
175  * 166   8/27/99 6:38p Alanl
176  * crush the blasted repeating messages bug
177  * 
178  * 164   8/26/99 9:09p Dave
179  * Force framerate check in everything but a RELEASE_REAL build.
180  * 
181  * 163   8/26/99 9:45a Dave
182  * First pass at easter eggs and cheats.
183  * 
184  * 162   8/24/99 8:55p Dave
185  * Make sure nondimming pixels work properly in tech menu.
186  * 
187  * 161   8/24/99 1:49a Dave
188  * Fixed client-side afterburner stuttering. Added checkbox for no version
189  * checking on PXO join. Made button info passing more friendly between
190  * client and server.
191  * 
192  * 160   8/22/99 5:53p Dave
193  * Scoring fixes. Added self destruct key. Put callsigns in the logfile
194  * instead of ship designations for multiplayer players.
195  * 
196  * 159   8/22/99 1:19p Dave
197  * Fixed up http proxy code. Cleaned up scoring code. Reverse the order in
198  * which d3d cards are detected.
199  * 
200  * 158   8/20/99 2:09p Dave
201  * PXO banner cycling.
202  * 
203  * 157   8/19/99 10:59a Dave
204  * Packet loss detection.
205  * 
206  * 156   8/19/99 10:12a Alanl
207  * preload mission-specific messages on machines greater than 48MB
208  * 
209  * 155   8/16/99 4:04p Dave
210  * Big honking checkin.
211  * 
212  * 154   8/11/99 5:54p Dave
213  * Fixed collision problem. Fixed standalone ghost problem.
214  * 
215  * 153   8/10/99 7:59p Jefff
216  * XSTR'ed some stuff
217  * 
218  * 152   8/10/99 6:54p Dave
219  * Mad optimizations. Added paging to the nebula effect.
220  * 
221  * 151   8/10/99 3:44p Jefff
222  * loads Intelligence information on startup
223  * 
224  * 150   8/09/99 3:47p Dave
225  * Fixed incorrect nebula regeneration. Default HUD to low-contrast in
226  * non-nebula missions.
227  * 
228  * 149   8/09/99 2:21p Andsager
229  * Fix patching from multiplayer direct to launcher update tab.
230  * 
231  * 148   8/09/99 10:36a Dave
232  * Version info for game.
233  * 
234  * 147   8/06/99 9:46p Dave
235  * Hopefully final changes for the demo.
236  * 
237  * 146   8/06/99 3:34p Andsager
238  * Make title version info "(D)" -> "D"  show up nicely
239  * 
240  * 145   8/06/99 2:59p Adamp
241  * Fixed NT launcher/update problem.
242  * 
243  * 144   8/06/99 1:52p Dave
244  * Bumped up MAX_BITMAPS for the demo.
245  * 
246  * 143   8/06/99 12:17p Andsager
247  * Demo: down to just 1 demo dog
248  * 
249  * 142   8/05/99 9:39p Dave
250  * Yet another new checksum.
251  * 
252  * 141   8/05/99 6:19p Dave
253  * New demo checksums.
254  * 
255  * 140   8/05/99 5:31p Andsager
256  * Up demo version 1.01
257  * 
258  * 139   8/05/99 4:22p Andsager
259  * No time limit on upsell screens.  Reverse order of display of upsell
260  * bitmaps.
261  * 
262  * 138   8/05/99 4:17p Dave
263  * Tweaks to client interpolation.
264  * 
265  * 137   8/05/99 3:52p Danw
266  * 
267  * 136   8/05/99 3:01p Danw
268  * 
269  * 135   8/05/99 2:43a Anoop
270  * removed duplicate definition.
271  * 
272  * 134   8/05/99 2:13a Dave
273  * Fixed build error.
274  * 
275  * 133   8/05/99 2:05a Dave
276  * Whee.
277  * 
278  * 132   8/05/99 1:22a Andsager
279  * fix upsell bug.
280  * 
281  * 131   8/04/99 9:51p Andsager
282  * Add title screen to demo
283  * 
284  * 130   8/04/99 6:47p Jefff
285  * fixed link error resulting from #ifdefs
286  * 
287  * 129   8/04/99 6:26p Dave
288  * Updated ship tbl checksum.
289  * 
290  * 128   8/04/99 5:40p Andsager
291  * Add multiple demo dogs
292  * 
293  * 127   8/04/99 5:36p Andsager
294  * Show upsell screens at end of demo campaign before returning to main
295  * hall.
296  * 
297  * 126   8/04/99 11:42a Danw
298  * tone down EAX reverb
299  * 
300  * 125   8/04/99 11:23a Dave
301  * Updated demo checksums.
302  * 
303  * 124   8/03/99 11:02p Dave
304  * Maybe fixed sync problems in multiplayer.
305  * 
306  * 123   8/03/99 6:21p Jefff
307  * minor text change
308  * 
309  * 122   8/03/99 3:44p Andsager
310  * Launch laucher if trying to run FS without first having configured
311  * system.
312  * 
313  * 121   8/03/99 12:45p Dave
314  * Update checksums.
315  * 
316  * 120   8/02/99 9:13p Dave
317  * Added popup tips.
318  * 
319  * 119   7/30/99 10:31p Dave
320  * Added comm menu to the configurable hud files.
321  * 
322  * 118   7/30/99 5:17p Andsager
323  * first fs2demo checksums
324  * 
325  * 117   7/29/99 3:09p Anoop
326  * 
327  * 116   7/29/99 12:05a Dave
328  * Nebula speed optimizations.
329  * 
330  * 115   7/27/99 8:59a Andsager
331  * Make major, minor version consistent for all builds.  Only show major
332  * and minor for launcher update window.
333  * 
334  * 114   7/26/99 5:50p Dave
335  * Revised ingame join. Better? We'll see....
336  * 
337  * 113   7/26/99 5:27p Andsager
338  * Add training mission as builtin to demo build
339  * 
340  * 112   7/24/99 1:54p Dave
341  * Hud text flash gauge. Reworked dead popup to use 4 buttons in red-alert
342  * missions.
343  * 
344  * 111   7/22/99 4:00p Dave
345  * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
346  * 
347  * 110   7/21/99 8:10p Dave
348  * First run of supernova effect.
349  * 
350  * 109   7/20/99 1:49p Dave
351  * Peter Drake build. Fixed some release build warnings.
352  * 
353  * 108   7/19/99 2:26p Andsager
354  * set demo multiplayer missions
355  * 
356  * 107   7/18/99 5:19p Dave
357  * Jump node icon. Fixed debris fogging. Framerate warning stuff.
358  * 
359  * 106   7/16/99 1:50p Dave
360  * 8 bit aabitmaps. yay.
361  * 
362  * 105   7/15/99 3:07p Dave
363  * 32 bit detection support. Mouse coord commandline.
364  * 
365  * 104   7/15/99 2:13p Dave
366  * Added 32 bit detection.
367  * 
368  * 103   7/15/99 9:20a Andsager
369  * FS2_DEMO initial checkin
370  * 
371  * 102   7/14/99 11:02a Dave
372  * Skill level default back to easy. Blech.
373  * 
374  * 101   7/09/99 5:54p Dave
375  * Seperated cruiser types into individual types. Added tons of new
376  * briefing icons. Campaign screen.
377  * 
378  * 100   7/08/99 4:43p Andsager
379  * New check for sparky_hi and print if not found.
380  * 
381  * 99    7/08/99 10:53a Dave
382  * New multiplayer interpolation scheme. Not 100% done yet, but still
383  * better than the old way.
384  * 
385  * 98    7/06/99 4:24p Dave
386  * Mid-level checkin. Starting on some potentially cool multiplayer
387  * smoothness crap.
388  * 
389  * 97    7/06/99 3:35p Andsager
390  * Allow movie to play before red alert mission.
391  * 
392  * 96    7/03/99 5:50p Dave
393  * Make rotated bitmaps draw properly in padlock views.
394  * 
395  * 95    7/02/99 9:55p Dave
396  * Player engine wash sound.
397  * 
398  * 94    7/02/99 4:30p Dave
399  * Much more sophisticated lightning support.
400  * 
401  * 93    6/29/99 7:52p Dave
402  * Put in exception handling in FS2.
403  * 
404  * 92    6/22/99 9:37p Dave
405  * Put in pof spewing.
406  * 
407  * 91    6/16/99 4:06p Dave
408  * New pilot info popup. Added new draw-bitmap-as-poly function.
409  * 
410  * 90    6/15/99 1:56p Andsager
411  * For release builds, allow start up in high res only with
412  * sparky_hi._fs2.vp
413  * 
414  * 89    6/15/99 9:34a Dave
415  * Fixed key checking in single threaded version of the stamp notification
416  * screen. 
417  * 
418  * 88    6/09/99 2:55p Andsager
419  * Allow multiple asteroid subtypes (of large, medium, small) and follow
420  * family.
421  * 
422  * 87    6/08/99 1:14a Dave
423  * Multi colored hud test.
424  * 
425  * 86    6/04/99 9:52a Dave
426  * Fixed some rendering problems.
427  * 
428  * 85    6/03/99 10:15p Dave
429  * Put in temporary main hall screen.
430  * 
431  * 84    6/02/99 6:18p Dave
432  * Fixed TNT lockup problems! Wheeeee!
433  * 
434  * 83    6/01/99 3:52p Dave
435  * View footage screen. Fixed xstrings to not display the & symbol. Popup,
436  * dead popup, pxo find player popup, pxo private room popup.
437  * 
438  * 82    5/26/99 1:28p Jasenw
439  * changed coords for loading ani
440  * 
441  * 81    5/26/99 11:46a Dave
442  * Added ship-blasting lighting and made the randomization of lighting
443  * much more customizable.
444  * 
445  * 80    5/24/99 5:45p Dave
446  * Added detail levels to the nebula, with a decent speedup. Split nebula
447  * lightning into its own section.
448  * 
449  * 
450  */
451
452 #ifndef PLAT_UNIX
453 #include <windows.h>
454 #include <process.h>
455 #include <direct.h>
456 #include <io.h>
457 #else
458 #endif
459
460 #include <stdio.h>
461 #include <stdlib.h>
462 #include <time.h>
463
464 #include "pstypes.h"
465 #include "systemvars.h"
466 #include "key.h"
467 #include "vecmat.h"
468 #include "2d.h"
469 #include "3d.h"
470 #include "starfield.h"
471 #include "lighting.h"
472 #include "weapon.h"
473 #include "ship.h"
474 #include "palman.h"
475 #include "osapi.h"
476 #include "fireballs.h"
477 #include "debris.h"
478 #include "timer.h"
479 #include "fix.h"
480 #include "floating.h"
481 #include "gamesequence.h"
482 #include "radar.h"
483 #include "optionsmenu.h"
484 #include "playermenu.h"
485 #include "trainingmenu.h"
486 #include "techmenu.h"
487 #include "ai.h"
488 #include "hud.h"
489 #include "hudmessage.h"
490 #include "psnet.h"
491 #include "missiongoals.h"
492 #include "missionparse.h"
493 #include "bmpman.h"
494 #include "joy.h"
495 #include "joy_ff.h"
496 #include "multi.h"
497 #include "multiutil.h"
498 #include "multimsgs.h"
499 #include "multiui.h"
500 #include "cfile.h"
501 #include "player.h"
502 #include "freespace.h"
503 #include "managepilot.h"
504 #include "sound.h"
505 #include "contexthelp.h"
506 #include "mouse.h"
507 #include "joy.h"
508 #include "missionbrief.h"
509 #include "missiondebrief.h"
510 #include "ui.h"
511 #include "missionshipchoice.h"
512 #include "model.h"
513 #include "hudconfig.h"
514 #include "controlsconfig.h"
515 #include "missionmessage.h"
516 #include "missiontraining.h"
517 #include "hudets.h"
518 #include "hudtarget.h"
519 #include "gamesnd.h"
520 #include "rbaudio.h"
521 #include "winmidi.h"
522 #include "eventmusic.h"
523 #include "animplay.h"
524 #include "missionweaponchoice.h"
525 #include "missionlog.h"
526 #include "audiostr.h"
527 #include "hudlock.h"
528 #include "missioncampaign.h"
529 #include "credits.h"
530 #include "missionhotkey.h"
531 #include "objectsnd.h"
532 #include "cmeasure.h"
533 #include "ai.h"
534 #include "linklist.h"
535 #include "shockwave.h"
536 #include "afterburner.h"
537 #include "scoring.h"
538 #include "stats.h"
539 #include "cmdline.h"
540 #include "timer.h"
541 #include "stand_gui.h"
542 #include "pcxutils.h"
543 #include "hudtargetbox.h"
544 #include "multi_xfer.h"
545 #include "hudescort.h"
546 #include "multiutil.h"
547 #include "sexp.h"
548 #include "medals.h"
549 #include "multiteamselect.h"
550 #include "ds3d.h"
551 #include "shipfx.h"
552 #include "readyroom.h"
553 #include "mainhallmenu.h"
554 #include "multilag.h"
555 #include "trails.h"
556 #include "particle.h"
557 #include "popup.h"
558 #include "multi_ingame.h"
559 #include "snazzyui.h"
560 #include "asteroid.h"
561 #include "popupdead.h"
562 #include "multi_voice.h"
563 #include "missioncmdbrief.h"
564 #include "redalert.h"
565 #include "gameplayhelp.h"
566 #include "multilag.h"
567 #include "staticrand.h"
568 #include "multi_pmsg.h"
569 #include "levelpaging.h"
570 #include "observer.h"
571 #include "multi_pause.h"
572 #include "multi_endgame.h"
573 #include "cutscenes.h"
574 #include "multi_respawn.h"
575 // #include "movie.h"
576 #include "multi_obj.h"
577 #include "multi_log.h"
578 #include "emp.h"
579 #include "localize.h"
580 #include "osregistry.h"
581 #include "barracks.h"
582 #include "missionpause.h"
583 #include "font.h"
584 #include "alphacolors.h"
585 #include "objcollide.h"
586 #include "flak.h"
587 #include "neb.h"
588 #include "neblightning.h"
589 #include "shipcontrails.h"
590 #include "awacs.h"
591 #include "beam.h"
592 #include "multi_dogfight.h"
593 #include "multi_rate.h"
594 #include "muzzleflash.h"
595 #include "encrypt.h"
596 #include "demo.h"
597 #include "version.h"
598 #include "mainhalltemp.h"
599 #include "exceptionhandler.h"
600 #ifndef PLAT_UNIX
601 #include "glide.h"
602 #endif
603 #include "supernova.h"
604 #include "hudshield.h"
605 // #include "names.h"
606 #include "shiphit.h"
607 #include "missionloopbrief.h"
608
609 #ifdef NDEBUG
610 #ifdef FRED
611 #error macro FRED is defined when trying to build release Fred.  Please undefine FRED macro in build settings
612 #endif
613 #endif
614
615 //      Revision history.
616 //      Full version:
617 //    1.00.04   5/26/98 MWA -- going final (12 pm)
618 //    1.00.03   5/26/98 MWA -- going final (3 am)
619 //    1.00.02   5/25/98 MWA -- going final
620 //    1.00.01   5/25/98 MWA -- going final
621 //              0.90            5/21/98 MWA -- getting ready for final.
622 //              0.10            4/9/98.  Set by MK.
623 //
624 //      Demo version: (obsolete since DEMO codebase split from tree)
625 //              0.03            4/10/98 AL.     Interplay rev
626 //              0.02            4/8/98  MK.     Increased when this system was modified.
627 //              0.01            4/7/98? AL.     First release to Interplay QA.
628 //
629 //      OEM version:
630 //              1.00            5/28/98 AL.     First release to Interplay QA.
631
632 void game_level_init(int seed = -1);
633 void game_post_level_init();
634 void game_do_frame();
635 void game_update_missiontime(); // called from game_do_frame() and navmap_do_frame()
636 void game_reset_time();
637 void game_show_framerate();                     // draws framerate in lower right corner
638
639 int Game_no_clear = 0;
640
641 int Pofview_running = 0;
642 int Nebedit_running = 0;
643
644 typedef struct big_expl_flash {
645         float max_flash_intensity;      // max intensity
646         float cur_flash_intensity;      // cur intensity
647         int     flash_start;            // start time
648 } big_expl_flash;
649
650 #define FRAME_FILTER 16
651
652 #define DEFAULT_SKILL_LEVEL     1
653 int     Game_skill_level = DEFAULT_SKILL_LEVEL;
654
655 #define VIEWER_ZOOM_DEFAULT 0.75f                       //      Default viewer zoom, 0.625 as per multi-lateral agreement on 3/24/97
656 float Viewer_zoom = VIEWER_ZOOM_DEFAULT;
657
658 #define EXE_FNAME                       ("fs2.exe")
659 #define LAUNCHER_FNAME  ("freespace2.exe")
660
661 // JAS: Code for warphole camera.
662 // Needs to be cleaned up.
663 vector Camera_pos = { 0.0f, 0.0f, 0.0f };
664 vector Camera_velocity = { 0.0f, 0.0f, 0.0f };
665 vector Camera_desired_velocity = { 0.0f, 0.0f, 0.0f };
666 matrix Camera_orient = IDENTITY_MATRIX;
667 float Camera_damping = 1.0f;
668 float Camera_time = 0.0f;
669 float Warpout_time = 0.0f;
670 int Warpout_forced = 0;         // Set if this is a forced warpout that cannot be cancelled.
671 int Warpout_sound = -1;
672 void camera_move();
673 int Use_joy_mouse = 0;
674 int Use_palette_flash = 1;
675 #ifndef NDEBUG
676 int Use_fullscreen_at_startup = 0;
677 #endif
678 int Show_area_effect = 0;
679 object  *Last_view_target = NULL;
680
681 int dogfight_blown = 0;
682
683 int     frame_int = -1;
684 float frametimes[FRAME_FILTER];
685 float frametotal = 0.0f;
686 float flFrametime;
687
688 #ifdef RELEASE_REAL
689         int     Show_framerate = 0;
690 #else 
691         int     Show_framerate = 1;
692 #endif
693
694 int     Framerate_cap = 120;
695 int     Show_mem = 0;
696 int     Show_cpu = 0;
697 int     Show_target_debug_info = 0;
698 int     Show_target_weapons = 0;
699 int     Game_font = -1;
700 static int Show_player_pos = 0;         // debug console command to show player world pos on HUD
701
702 int Debug_octant = -1;
703
704 fix Game_time_compression = F1_0;
705
706 // if the ships.tbl the player has is valid
707 int Game_ships_tbl_valid = 0;
708
709 // if the weapons.tbl the player has is valid
710 int Game_weapons_tbl_valid = 0;
711
712 #ifndef NDEBUG
713 int Test_begin = 0;
714 extern int      Player_attacking_enabled;
715 int Show_net_stats;
716 #endif
717
718 int Pre_player_entry;
719
720 int     Fred_running = 0;
721 char Game_current_mission_filename[MAX_FILENAME_LEN];
722 int game_single_step = 0;
723 int last_single_step=0;
724
725 extern int MSG_WINDOW_X_START;  // used to position mission_time and shields output
726 extern int MSG_WINDOW_Y_START;
727 extern int MSG_WINDOW_HEIGHT;
728
729 int game_zbuffer = 1;
730 //static int Game_music_paused;
731 static int Game_paused;
732
733 int Game_level_seed;
734
735 #define EXPIRE_BAD_CHECKSUM                     1
736 #define EXPIRE_BAD_TIME                                 2
737
738 extern void ssm_init();
739 extern void ssm_level_init();
740 extern void ssm_process();
741
742 // static variable to contain the time this version was built
743 // commented out for now until
744 // I figure out how to get the username into the file
745 //LOCAL char freespace_build_time[] = "Compiled on:"__DATE__" "__TIME__" by "__USER__;
746
747 // defines and variables used for dumping frame for making trailers.
748 #ifndef NDEBUG
749 int Debug_dump_frames = 0;                      // Set to 0 to not dump frames, else equal hz to dump. (15 or 30 probably)
750 int Debug_dump_trigger = 0;
751 int Debug_dump_frame_count;
752 int Debug_dump_frame_num = 0;
753 #define DUMP_BUFFER_NUM_FRAMES  1                       // store every 15 frames
754 #endif
755
756 // amount of time to wait after the player has died before we display the death died popup
757 #define PLAYER_DIED_POPUP_WAIT          2500
758 int Player_died_popup_wait = -1;
759 int Player_multi_died_check = -1;
760
761 // builtin mission list stuff
762 #ifdef FS2_DEMO
763         int Game_builtin_mission_count = 6;
764         fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
765                 { "SPDemo-01.fs2",                              (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     ""              },
766                 { "SPDemo-02.fs2",                              (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     ""              },
767                 { "DemoTrain.fs2",                              (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     ""              },
768                 { "Demo.fc2",                                           (FSB_FROM_VOLITION | FSB_CAMPAIGN_FILE),                                        ""              },
769                 { "MPDemo-01.fs2",                              (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
770                 { "Demo-DOG-01.fs2",                            (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
771         };
772 #elif defined(PD_BUILD)
773         int Game_builtin_mission_count = 4;
774         fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
775                 { "sm1-01.fs2",                                 (FSB_FROM_VOLITION),                                                                                            ""              },
776                 { "sm1-05.fs2",                                 (FSB_FROM_VOLITION),                                                                                            ""              },              
777                 { "sm1-01",                                                     (FSB_FROM_VOLITION),                                                                                            ""              },
778                 { "sm1-05",                                                     (FSB_FROM_VOLITION),                                                                                            ""              },              
779         };
780 #elif defined(MULTIPLAYER_BETA)
781         int Game_builtin_mission_count = 17;
782         fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
783                 // multiplayer beta
784                 { "md-01.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
785                 { "md-02.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
786                 { "md-03.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
787                 { "md-04.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
788                 { "md-05.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
789                 { "md-06.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
790                 { "md-07.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
791                 { "mt-02.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
792                 { "mt-03.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
793                 { "m-03.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
794                 { "m-04.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
795                 { "m-05.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""              },
796                 { "templar-01.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""              },
797                 { "templar-02.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""              },
798                 { "templar-03a.fs2",                            (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""              },
799                 { "templar-04a.fs2",                            (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""              },
800                 { "templar.fc2",                                        (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN_FILE),    ""              },      
801         };
802 #elif defined(OEM_BUILD)
803         int Game_builtin_mission_count = 17;
804         fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
805                 // oem version - act 1 only
806                 { "freespace2oem.fc2",                  (FSB_FROM_VOLITION | FSB_CAMPAIGN_FILE),                                        "" },
807                         
808                 // act 1
809                 { "sm1-01.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
810                 { "sm1-02.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
811                 { "sm1-03.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
812                 { "sm1-04.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
813                 { "sm1-05.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
814                 { "sm1-06.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
815                 { "sm1-07.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
816                 { "sm1-08.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
817                 { "sm1-09.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
818                 { "sm1-10.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
819                 { "training-1.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
820                 { "training-2.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
821                 { "training-3.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
822                 { "tsm-104.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
823                 { "tsm-105.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       },
824                 { "tsm-106.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_1       }
825         };
826 #else 
827         int Game_builtin_mission_count = 92;
828         fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
829                 // single player campaign
830                 { "freespace2.fc2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN_FILE),                                        "" },
831                         
832                 // act 1
833                 { "sm1-01.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
834                 { "sm1-02.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
835                 { "sm1-03.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
836                 { "sm1-04.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
837                 { "sm1-05.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
838                 { "sm1-06.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
839                 { "sm1-07.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
840                 { "sm1-08.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
841                 { "sm1-09.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
842                 { "sm1-10.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
843                 { "loop1-1.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
844                 { "loop1-2.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
845                 { "loop1-3.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
846                 { "training-1.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
847                 { "training-2.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
848                 { "training-3.fs2",                             (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
849                 { "tsm-104.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
850                 { "tsm-105.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
851                 { "tsm-106.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_2       },
852
853                 // act 2
854                 { "sm2-01.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
855                 { "sm2-02.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
856                 { "sm2-03.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
857                 { "sm2-04.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
858                 { "sm2-05.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
859                 { "sm2-06.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
860                 { "sm2-07.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
861                 { "sm2-08.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
862                 { "sm2-09.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
863                 { "sm2-10.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
864
865                 // act 3
866                 { "sm3-01.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
867                 { "sm3-02.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
868                 { "sm3-03.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
869                 { "sm3-04.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
870                 { "sm3-05.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
871                 { "sm3-06.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
872                 { "sm3-07.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
873                 { "sm3-08.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
874                 { "sm3-09.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
875                 { "sm3-10.fs2",                                 (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
876                 { "loop2-1.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },              
877                 { "loop2-2.fs2",                                        (FSB_FROM_VOLITION | FSB_CAMPAIGN),                                                     FS_CDROM_VOLUME_3       },
878
879                 // multiplayer missions
880
881                 // gauntlet
882                 { "g-shi.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
883                 { "g-ter.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
884                 { "g-vas.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
885
886                 // coop
887                 { "m-01.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
888                 { "m-02.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
889                 { "m-03.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
890                 { "m-04.fs2",                                           (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
891
892                 // dogfight
893                 { "mdh-01.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
894                 { "mdh-02.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
895                 { "mdh-03.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
896                 { "mdh-04.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
897                 { "mdh-05.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
898                 { "mdh-06.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
899                 { "mdh-07.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
900                 { "mdh-08.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
901                 { "mdh-09.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
902                 { "mdl-01.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
903                 { "mdl-02.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
904                 { "mdl-03.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
905                 { "mdl-04.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
906                 { "mdl-05.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
907                 { "mdl-06.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
908                 { "mdl-07.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
909                 { "mdl-08.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
910                 { "mdl-09.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
911                 { "mdm-01.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
912                 { "mdm-02.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
913                 { "mdm-03.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
914                 { "mdm-04.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
915                 { "mdm-05.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
916                 { "mdm-06.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
917                 { "mdm-07.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
918                 { "mdm-08.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
919                 { "mdm-09.fs2",                                 (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },              
920                 { "osdog.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
921
922                 // TvT          
923                 { "mt-01.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
924                 { "mt-02.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
925                 { "mt-03.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
926                 { "mt-04.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
927                 { "mt-05.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
928                 { "mt-06.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
929                 { "mt-07.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
930                 { "mt-08.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
931                 { "mt-09.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },
932                 { "mt-10.fs2",                                          (FSB_FROM_VOLITION | FSB_MULTI),                                                                ""                                              },                              
933
934                 // campaign
935                 { "templar.fc2",                                (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN_FILE),                                    "" },
936                 { "templar-01.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""                                              },                              
937                 { "templar-02.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""                                              },                              
938                 { "templar-03.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""                                              },                              
939                 { "templar-04.fs2",                             (FSB_FROM_VOLITION | FSB_MULTI | FSB_CAMPAIGN),                 ""                                              },                              
940         };
941 #endif
942
943
944 // Internal function prototypes
945 void game_maybe_draw_mouse(float frametime);
946 void init_animating_pointer();
947 void load_animating_pointer(char *filename, int dx, int dy);
948 void unload_animating_pointer();
949 void game_do_training_checks();
950 void game_shutdown(void);
951 void game_show_event_debug(float frametime);
952 void game_event_debug_init();
953 void game_frame();
954 void demo_upsell_show_screens();
955 void game_start_subspace_ambient_sound();
956 void game_stop_subspace_ambient_sound();
957 void verify_ships_tbl();
958 void verify_weapons_tbl();
959 void display_title_screen();
960
961 // loading background filenames
962 static char *Game_loading_bground_fname[GR_NUM_RESOLUTIONS] = {
963         "LoadingBG",            // GR_640
964         "2_LoadingBG"           // GR_1024
965 };
966
967
968 static char *Game_loading_ani_fname[GR_NUM_RESOLUTIONS] = {
969         "Loading.ani",          // GR_640
970         "2_Loading.ani"         // GR_1024
971 };
972
973 #if defined(FS2_DEMO)
974 static char *Game_demo_title_screen_fname[GR_NUM_RESOLUTIONS] = {
975         "PreLoad",
976         "2_PreLoad"
977 };
978 #elif defined(OEM_BUILD)
979 static char *Game_demo_title_screen_fname[GR_NUM_RESOLUTIONS] = {
980         "OEMPreLoad",
981         "2_OEMPreLoad"
982 };
983 #endif
984
985 // cdrom stuff
986 char Game_CDROM_dir[MAX_PATH_LEN];
987 int init_cdrom();
988
989 // How much RAM is on this machine. Set in WinMain
990 uint Freespace_total_ram = 0;
991
992 // game flash stuff
993 float Game_flash_red = 0.0f;
994 float Game_flash_green = 0.0f;
995 float Game_flash_blue = 0.0f;
996 float Sun_spot = 0.0f;
997 big_expl_flash Big_expl_flash = {0.0f, 0.0f, 0};
998
999 // game shudder stuff (in ms)
1000 int Game_shudder_time = -1;
1001 int Game_shudder_total = 0;
1002 float Game_shudder_intensity = 0.0f;                    // should be between 0.0 and 100.0
1003
1004 // EAX stuff
1005 sound_env Game_sound_env;
1006 //sound_env Game_default_sound_env = {SND_ENV_AUDITORIUM, 0.25f, 0.35f, 3.0f};
1007 sound_env Game_default_sound_env = {SND_ENV_GENERIC, 0.2F,0.2F,1.0F};
1008
1009 int Game_sound_env_update_timestamp;
1010
1011 // WARPIN CRAP BEGIN --------------------------------------------------------------------------------------------
1012
1013
1014 // WARPIN CRAP END --------------------------------------------------------------------------------------------
1015
1016 fs_builtin_mission *game_find_builtin_mission(char *filename)
1017 {
1018         int idx;
1019
1020         // look through all existing builtin missions
1021         for(idx=0; idx<Game_builtin_mission_count; idx++){
1022                 if(!stricmp(Game_builtin_mission_list[idx].filename, filename)){
1023                         return &Game_builtin_mission_list[idx];
1024                 }
1025         }
1026
1027         // didn't find it
1028         return NULL;
1029 }
1030
1031 int game_get_default_skill_level()
1032 {
1033         return DEFAULT_SKILL_LEVEL;
1034 }
1035
1036 // Resets the flash
1037 void game_flash_reset()
1038 {
1039         Game_flash_red = 0.0f;
1040         Game_flash_green = 0.0f;
1041         Game_flash_blue = 0.0f;
1042         Sun_spot = 0.0f;
1043         Big_expl_flash.max_flash_intensity = 0.0f;
1044         Big_expl_flash.cur_flash_intensity = 0.0f;
1045         Big_expl_flash.flash_start = 0;
1046 }
1047
1048 float Gf_critical = -1.0f;                                      // framerate we should be above on the average for this mission
1049 float Gf_critical_time = 0.0f;                  // how much time we've been at the critical framerate
1050
1051 void game_framerate_check_init()
1052 {
1053         // zero critical time
1054         Gf_critical_time = 0.0f;
1055                 
1056         // nebula missions
1057         if(The_mission.flags & MISSION_FLAG_FULLNEB){
1058                 // if this is a glide card
1059                 if(gr_screen.mode == GR_GLIDE){
1060 #ifndef PLAT_UNIX               
1061                         extern GrHwConfiguration hwconfig;
1062
1063                         // voodoo 2/3
1064                         if(hwconfig.SSTs[0].sstBoard.VoodooConfig.fbRam >= 4){
1065                                 Gf_critical = 15.0f;
1066                         }
1067                         // voodoo 1
1068                         else {
1069                                 Gf_critical = 10.0f;
1070                         }
1071 #else
1072                         STUB_FUNCTION;
1073                         
1074                         Gf_critical = 15.0f;
1075 #endif                                          
1076                 }
1077                 // d3d. only care about good cards here I guess (TNT)
1078                 else {
1079                         Gf_critical = 15.0f;                    
1080                 }
1081         } else {
1082                 // if this is a glide card
1083                 if(gr_screen.mode == GR_GLIDE){
1084 #ifndef PLAT_UNIX               
1085                         extern GrHwConfiguration hwconfig;
1086
1087                         // voodoo 2/3
1088                         if(hwconfig.SSTs[0].sstBoard.VoodooConfig.fbRam >= 4){
1089                                 Gf_critical = 25.0f;
1090                         }
1091                         // voodoo 1
1092                         else {
1093                                 Gf_critical = 20.0f;
1094                         }
1095 #else
1096                         STUB_FUNCTION;
1097                         
1098                         Gf_critical = 25.0f;
1099 #endif                                          
1100                 }
1101                 // d3d. only care about good cards here I guess (TNT)
1102                 else {
1103                         Gf_critical = 25.0f;
1104                 }
1105         }
1106 }
1107
1108 extern float Framerate;
1109 void game_framerate_check()
1110 {
1111         int y_start = 100;
1112         
1113         // if the current framerate is above the critical level, add frametime
1114         if(Framerate >= Gf_critical){
1115                 Gf_critical_time += flFrametime;
1116         }       
1117
1118         if(!Show_framerate){
1119                 return;
1120         }
1121
1122         // display if we're above the critical framerate
1123         if(Framerate < Gf_critical){
1124                 gr_set_color_fast(&Color_bright_red);
1125                 gr_string(200, y_start, "Framerate warning");
1126
1127                 y_start += 10;
1128         }
1129
1130         // display our current pct of good frametime
1131         if(f2fl(Missiontime) >= 0.0f){
1132                 float pct = (Gf_critical_time / f2fl(Missiontime)) * 100.0f;
1133
1134                 if(pct >= 85.0f){
1135                         gr_set_color_fast(&Color_bright_green);
1136                 } else {
1137                         gr_set_color_fast(&Color_bright_red);
1138                 }
1139
1140                 gr_printf(200, y_start, "%d%%", (int)pct);
1141
1142                 y_start += 10;
1143         }
1144 }
1145
1146
1147 // Adds a flash effect.  These can be positive or negative.
1148 // The range will get capped at around -1 to 1, so stick 
1149 // with a range like that.
1150 void game_flash( float r, float g, float b )
1151 {
1152         Game_flash_red += r;
1153         Game_flash_green += g;
1154         Game_flash_blue += b;
1155
1156         if ( Game_flash_red < -1.0f )   {
1157                 Game_flash_red = -1.0f;
1158         } else if ( Game_flash_red > 1.0f )     {
1159                 Game_flash_red = 1.0f;
1160         }
1161
1162         if ( Game_flash_green < -1.0f ) {
1163                 Game_flash_green = -1.0f;
1164         } else if ( Game_flash_green > 1.0f )   {
1165                 Game_flash_green = 1.0f;
1166         }
1167
1168         if ( Game_flash_blue < -1.0f )  {
1169                 Game_flash_blue = -1.0f;
1170         } else if ( Game_flash_blue > 1.0f )    {
1171                 Game_flash_blue = 1.0f;
1172         }
1173
1174 }
1175
1176 // Adds a flash for Big Ship explosions
1177 // cap range from 0 to 1
1178 void big_explosion_flash(float flash)
1179 {
1180         Big_expl_flash.flash_start = timestamp(1);
1181
1182         if (flash > 1.0f) {
1183                 flash = 1.0f;
1184         } else if (flash < 0.0f) {
1185                 flash = 0.0f;
1186         }
1187
1188         Big_expl_flash.max_flash_intensity = flash;
1189         Big_expl_flash.cur_flash_intensity = 0.0f;
1190 }
1191
1192 //      Amount to diminish palette towards normal, per second.
1193 #define DIMINISH_RATE   0.75f
1194 #define SUN_DIMINISH_RATE       6.00f
1195
1196 int Sun_drew = 0;
1197
1198 float sn_glare_scale = 1.7f;
1199 DCF(sn_glare, "")
1200 {
1201         dc_get_arg(ARG_FLOAT);
1202         sn_glare_scale = Dc_arg_float;
1203 }
1204
1205 float Supernova_last_glare = 0.0f;
1206 void game_sunspot_process(float frametime)
1207 {
1208         int n_lights, idx;
1209         int sn_stage;
1210         float Sun_spot_goal = 0.0f;
1211
1212         // supernova
1213         sn_stage = supernova_active();
1214         if(sn_stage){           
1215                 // sunspot differently based on supernova stage
1216                 switch(sn_stage){
1217                 // approaching. player still in control
1218                 case 1:                 
1219                         float pct;
1220                         pct = (1.0f - (supernova_time_left() / SUPERNOVA_CUT_TIME));
1221
1222                         vector light_dir;                               
1223                         light_get_global_dir(&light_dir, 0);
1224                         float dot;
1225                         dot = vm_vec_dot( &light_dir, &Eye_matrix.fvec );
1226                         
1227                         if(dot >= 0.0f){
1228                                 // scale it some more
1229                                 dot = dot * (0.5f + (pct * 0.5f));
1230                                 dot += 0.05f;                                   
1231
1232                                 Sun_spot_goal += (dot * sn_glare_scale);
1233                         }
1234
1235                         // draw the sun glow
1236                         if ( !shipfx_eye_in_shadow( &Eye_position, Viewer_obj, 0 ) )    {
1237                                 // draw the glow for this sun
1238                                 stars_draw_sun_glow(0); 
1239                         }
1240
1241                         Supernova_last_glare = Sun_spot_goal;
1242                         break;
1243
1244                 // camera cut. player not in control. note : at this point camera starts out facing the sun. so we can go nice and bright
1245                 case 2:                                         
1246                 case 3:
1247                         Sun_spot_goal = 0.9f;
1248                         Sun_spot_goal += (1.0f - (supernova_time_left() / SUPERNOVA_CUT_TIME)) * 0.1f;
1249
1250                         if(Sun_spot_goal > 1.0f){
1251                                 Sun_spot_goal = 1.0f;
1252                         }
1253
1254                         Sun_spot_goal *= sn_glare_scale;
1255                         Supernova_last_glare = Sun_spot_goal;
1256                         break;          
1257
1258                 // fade to white. display dead popup
1259                 case 4:
1260                 case 5:
1261                         Supernova_last_glare += (2.0f * flFrametime);
1262                         if(Supernova_last_glare > 2.0f){
1263                                 Supernova_last_glare = 2.0f;
1264                         }
1265
1266                         Sun_spot_goal = Supernova_last_glare;
1267                         break;
1268                 }
1269         
1270                 Sun_drew = 0;                           
1271         } else {
1272                 if ( Sun_drew ) {
1273                         // check sunspots for all suns
1274                         n_lights = light_get_global_count();
1275
1276                         // check
1277                         for(idx=0; idx<n_lights; idx++){
1278                                 //(vector *eye_pos, matrix *eye_orient)
1279                                 if ( !shipfx_eye_in_shadow( &Eye_position, Viewer_obj, idx ) )  {
1280
1281                                         vector light_dir;                               
1282                                         light_get_global_dir(&light_dir, idx);
1283
1284                                         float dot = vm_vec_dot( &light_dir, &Eye_matrix.fvec )*0.5f+0.5f;
1285
1286                                         Sun_spot_goal += (float)pow(dot,85.0f);
1287
1288                                         // draw the glow for this sun
1289                                         stars_draw_sun_glow(idx);                               
1290                                 } else {
1291                                         Sun_spot_goal = 0.0f;
1292                                 }
1293                         }
1294
1295                         Sun_drew = 0;
1296                 } else {
1297                         Sun_spot_goal = 0.0f;
1298                 }
1299         }
1300
1301         float dec_amount = frametime*SUN_DIMINISH_RATE;
1302
1303         if ( Sun_spot < Sun_spot_goal ) {
1304                 Sun_spot += dec_amount;
1305                 if ( Sun_spot > Sun_spot_goal ) {
1306                         Sun_spot = Sun_spot_goal;
1307                 }
1308         } else if ( Sun_spot > Sun_spot_goal )  {
1309                 Sun_spot -= dec_amount;
1310                 if ( Sun_spot < Sun_spot_goal ) {
1311                         Sun_spot = Sun_spot_goal;
1312                 }
1313         }
1314 }
1315
1316
1317 // Call once a frame to diminish the
1318 // flash effect to 0.
1319 void game_flash_diminish(float frametime)
1320 {
1321         float dec_amount = frametime*DIMINISH_RATE;
1322
1323         if ( Game_flash_red > 0.0f ) {
1324                 Game_flash_red -= dec_amount;           
1325                 if ( Game_flash_red < 0.0f )
1326                         Game_flash_red = 0.0f;
1327         } else {
1328                 Game_flash_red += dec_amount;           
1329                 if ( Game_flash_red > 0.0f )
1330                         Game_flash_red = 0.0f;
1331         } 
1332
1333         if ( Game_flash_green > 0.0f ) {
1334                 Game_flash_green -= dec_amount;         
1335                 if ( Game_flash_green < 0.0f )
1336                         Game_flash_green = 0.0f;
1337         } else {
1338                 Game_flash_green += dec_amount;         
1339                 if ( Game_flash_green > 0.0f )
1340                         Game_flash_green = 0.0f;
1341         } 
1342
1343         if ( Game_flash_blue > 0.0f ) {
1344                 Game_flash_blue -= dec_amount;          
1345                 if ( Game_flash_blue < 0.0f )
1346                         Game_flash_blue = 0.0f;
1347         } else {
1348                 Game_flash_blue += dec_amount;          
1349                 if ( Game_flash_blue > 0.0f )
1350                         Game_flash_blue = 0.0f;
1351         } 
1352
1353         // update big_explosion_cur_flash
1354 #define TIME_UP         1500
1355 #define TIME_DOWN       2500
1356         int duration = TIME_UP + TIME_DOWN;
1357         int time = timestamp_until(Big_expl_flash.flash_start);
1358         if (time > -duration) {
1359                 time = -time;
1360                 if (time < TIME_UP) {
1361                         Big_expl_flash.cur_flash_intensity = Big_expl_flash.max_flash_intensity * time / (float) TIME_UP;
1362                 } else {
1363                         time -= TIME_UP;
1364                         Big_expl_flash.cur_flash_intensity = Big_expl_flash.max_flash_intensity * ((float) TIME_DOWN - time) / (float) TIME_DOWN;
1365                 }
1366         }
1367         
1368         if ( Use_palette_flash )        {
1369                 int r,g,b;
1370 //              static int or=0, og=0, ob=0;
1371
1372                 // Change the 200 to change the color range of colors.
1373                 r = fl2i( Game_flash_red*128.0f );  
1374                 g = fl2i( Game_flash_green*128.0f );   
1375                 b = fl2i( Game_flash_blue*128.0f );  
1376
1377                 if ( Sun_spot > 0.0f )  {
1378                         r += fl2i(Sun_spot*128.0f);
1379                         g += fl2i(Sun_spot*128.0f);
1380                         b += fl2i(Sun_spot*128.0f);
1381                 }
1382
1383                 if ( Big_expl_flash.cur_flash_intensity  > 0.0f ) {
1384                         r += fl2i(Big_expl_flash.cur_flash_intensity*128.0f);
1385                         g += fl2i(Big_expl_flash.cur_flash_intensity*128.0f);
1386                         b += fl2i(Big_expl_flash.cur_flash_intensity*128.0f);
1387                 }
1388
1389                 if ( r < 0 ) r = 0; else if ( r > 255 ) r = 255;
1390                 if ( g < 0 ) g = 0; else if ( g > 255 ) g = 255;
1391                 if ( b < 0 ) b = 0; else if ( b > 255 ) b = 255;
1392
1393                 if ( (r!=0) || (g!=0) || (b!=0) ) {
1394                         gr_flash( r, g, b );
1395
1396                         //mprintf(( "Flash! %d,%d,%d\n", r, g, b ));
1397
1398 //                      or = r;
1399 //                      og = g;
1400 //                      ob = b;
1401                 }
1402         }
1403         
1404 }
1405
1406
1407 void game_level_close()
1408 {
1409         // De-Initialize the game subsystems
1410         message_mission_shutdown();
1411         event_music_level_close();
1412         game_stop_looped_sounds();
1413         snd_stop_all();
1414         obj_snd_level_close();                                  // uninit object-linked persistant sounds
1415         gamesnd_unload_gameplay_sounds();       // unload gameplay sounds from memory
1416         anim_level_close();                                             // stop and clean up any anim instances
1417         shockwave_level_close();
1418         fireball_level_close(); 
1419         shield_hit_close();
1420         mission_event_shutdown();
1421         asteroid_level_close();
1422         model_cache_reset();                                            // Reset/free all the model caching stuff
1423         flak_level_close();                                             // unload flak stuff
1424         neb2_level_close();                                             // shutdown gaseous nebula stuff
1425         ct_level_close();
1426         beam_level_close();
1427         mflash_level_close();
1428
1429         audiostream_unpause_all();
1430         Game_paused = 0;
1431 }
1432
1433
1434 // intializes game stuff and loads the mission.  Returns 0 on failure, 1 on success
1435 // input: seed =>       DEFAULT PARAMETER (value -1).  Only set by demo playback code.
1436 void game_level_init(int seed)
1437 {
1438         // seed the random number generator
1439         if ( seed == -1 ) {
1440                 // if no seed was passed, seed the generator either from the time value, or from the
1441                 // netgame security flags -- ensures that all players in multiplayer game will have the
1442                 // same randon number sequence (with static rand functions)
1443                 if ( Game_mode & GM_NORMAL ) {
1444                         Game_level_seed = time(NULL);
1445                 } else {
1446                         Game_level_seed = Netgame.security;
1447                 }
1448         } else {
1449                 // mwa 9/17/98 -- maybe this assert isn't needed????
1450                 Assert( !(Game_mode & GM_MULTIPLAYER) );
1451                 Game_level_seed = seed;
1452         }
1453         srand( Game_level_seed );
1454
1455         // semirand function needs to get re-initted every time in multiplayer
1456         if ( Game_mode & GM_MULTIPLAYER ){
1457                 init_semirand();
1458         }
1459
1460         Framecount = 0;
1461
1462         Key_normal_game = (Game_mode & GM_NORMAL);
1463         Cheats_enabled = 0;
1464
1465         Game_shudder_time = -1;
1466
1467         // Initialize the game subsystems
1468 //      timestamp_reset();                      // Must be inited before everything else
1469         if(!Is_standalone){
1470                 game_reset_time();                      // resets time, and resets saved time too
1471         }
1472         obj_init();                                             // Must be inited before the other systems
1473         model_free_all();                               // Free all existing models
1474         mission_brief_common_init();            // Free all existing briefing/debriefing text
1475         weapon_level_init();
1476         ai_level_init();                                //      Call this before ship_init() because it reads ai.tbl.
1477         ship_level_init();
1478         player_level_init();    
1479         shipfx_flash_init();                    // Init the ship gun flash system.
1480         game_flash_reset();                     // Reset the flash effect
1481         particle_init();                                // Reset the particle system
1482         fireball_init();
1483         debris_init();
1484         cmeasure_init();
1485         shield_hit_init();                              //      Initialize system for showing shield hits
1486         radar_mission_init();
1487         mission_init_goals();
1488         mission_log_init();
1489         messages_init();
1490         obj_snd_level_init();                                   // init object-linked persistant sounds
1491         anim_level_init();
1492         shockwave_level_init();
1493         afterburner_level_init();
1494         scoring_level_init( &Player->stats );
1495         key_level_init();
1496         asteroid_level_init();
1497         control_config_clear_used_status();
1498         collide_ship_ship_sounds_init();
1499         Missiontime = 0;
1500         Pre_player_entry = 1;                   //      Means the player has not yet entered.
1501         Entry_delay_time = 0;                   //      Could get overwritten in mission read.
1502         fireball_preload();                             //      page in warphole bitmaps
1503         observer_init();
1504         flak_level_init();                              // initialize flak - bitmaps, etc
1505         ct_level_init();                                        // initialize ships contrails, etc
1506         awacs_level_init();                             // initialize AWACS
1507         beam_level_init();                              // initialize beam weapons
1508         mflash_level_init();
1509         ssm_level_init();       
1510         supernova_level_init();
1511
1512         // multiplayer dogfight hack
1513         dogfight_blown = 0;
1514
1515         shipfx_engine_wash_level_init();
1516
1517         nebl_level_init();
1518
1519         Last_view_target = NULL;
1520         Game_paused = 0;
1521
1522         Game_no_clear = 0;
1523
1524         // campaign wasn't ended
1525         Campaign_ended_in_mission = 0;
1526 }
1527
1528 // called when a mission is over -- does server specific stuff.
1529 void freespace_stop_mission()
1530 {       
1531         game_level_close();
1532         Game_mode &= ~GM_IN_MISSION;
1533 }
1534
1535 // called at frame interval to process networking stuff
1536 void game_do_networking()
1537 {
1538         Assert( Net_player != NULL );
1539         if (!(Game_mode & GM_MULTIPLAYER)){
1540                 return;
1541         }
1542
1543         // see if this player should be reading/writing data.  Bit is set when at join
1544         // screen onward until quits back to main menu.
1545         if ( !(Net_player->flags & NETINFO_FLAG_DO_NETWORKING) ){
1546                 return;
1547         }
1548
1549         if(gameseq_get_state()!=GS_STATE_MULTI_PAUSED){
1550                 multi_do_frame();
1551         } else {
1552                 multi_pause_do_frame();
1553         }       
1554 }
1555
1556
1557 // Loads the best palette for this level, based
1558 // on nebula color and hud color.  You could just call palette_load_table with
1559 // the appropriate filename, but who wants to do that.
1560 void game_load_palette()
1561 {
1562         char palette_filename[1024];
1563
1564         // We only use 3 hud colors right now
1565         // Assert( HUD_config.color >= 0 );
1566         // Assert( HUD_config.color <= 2 );
1567
1568         Assert( Mission_palette >= 0 );
1569         Assert( Mission_palette <= 98 );
1570
1571         // if ( The_mission.flags & MISSION_FLAG_SUBSPACE )     {
1572                 strcpy( palette_filename, NOX("gamepalette-subspace") );
1573         // } else {
1574                 // sprintf( palette_filename, NOX("gamepalette%d-%02d"), HUD_config.color+1, Mission_palette+1 );
1575         // }
1576
1577         mprintf(( "Loading palette %s\n", palette_filename ));
1578
1579         // palette_load_table(palette_filename);
1580 }
1581
1582 void game_post_level_init()
1583 {
1584         // Stuff which gets called after mission is loaded.  Because player isn't created until
1585         // after mission loads, some things must get initted after the level loads
1586
1587         model_level_post_init();
1588
1589         HUD_init();
1590         hud_setup_escort_list();
1591         mission_hotkey_set_defaults();  // set up the default hotkeys (from mission file)
1592
1593         stars_level_init();     
1594         neb2_level_init();              
1595
1596 #ifndef NDEBUG
1597         game_event_debug_init();
1598 #endif
1599
1600         training_mission_init();
1601         asteroid_create_all();
1602         
1603         game_framerate_check_init();
1604 }
1605
1606
1607 // An estimate as to how high the count passed to game_loading_callback will go.
1608 // This is just a guess, it seems to always be about the same.   The count is
1609 // proportional to the code being executed, not the time, so this works good
1610 // for a bar, assuming the code does about the same thing each time you
1611 // load a level.   You can find this value by looking at the return value
1612 // of game_busy_callback(NULL), which I conveniently print out to the
1613 // debug output window with the '=== ENDING LOAD ==' stuff.   
1614 //#define COUNT_ESTIMATE 3706
1615 #define COUNT_ESTIMATE 1111
1616
1617 int Game_loading_callback_inited = 0;
1618
1619 int Game_loading_background = -1;
1620 anim * Game_loading_ani = NULL;
1621 anim_instance   *Game_loading_ani_instance;
1622 int Game_loading_frame=-1;
1623
1624 static int Game_loading_ani_coords[GR_NUM_RESOLUTIONS][2] = {
1625         {
1626                 63, 316  // GR_640
1627         },
1628         {
1629                 101, 505        // GR_1024
1630         }
1631 };
1632
1633 // This gets called 10x per second and count is the number of times 
1634 // game_busy() has been called since the current callback function
1635 // was set.
1636 void game_loading_callback(int count)
1637 {       
1638         game_do_networking();
1639
1640         Assert( Game_loading_callback_inited==1 );
1641         Assert( Game_loading_ani != NULL );
1642
1643         int framenum = ((Game_loading_ani->total_frames*count) / COUNT_ESTIMATE)+1;
1644         if ( framenum > Game_loading_ani->total_frames-1 )      {
1645                 framenum = Game_loading_ani->total_frames-1;
1646         } else if ( framenum < 0 )      {
1647                 framenum = 0;
1648         }
1649
1650         int cbitmap = -1;
1651         while ( Game_loading_frame < framenum ) {
1652                 Game_loading_frame++;
1653                 cbitmap = anim_get_next_frame(Game_loading_ani_instance);
1654         }
1655
1656
1657         if ( cbitmap > -1 )     {
1658                 if ( Game_loading_background > -1 )     {
1659                         gr_set_bitmap( Game_loading_background );
1660                         gr_bitmap(0,0);
1661                 }
1662
1663                 //mprintf(( "Showing frame %d/%d [ Bitmap=%d ]\n", Game_loading_frame ,  Game_loading_ani->total_frames, cbitmap ));
1664                 gr_set_bitmap( cbitmap );
1665                 gr_bitmap(Game_loading_ani_coords[gr_screen.res][0],Game_loading_ani_coords[gr_screen.res][1]);
1666
1667                 bm_release(cbitmap);
1668         
1669                 gr_flip();
1670         }
1671 }
1672
1673 void game_loading_callback_init()
1674 {
1675         Assert( Game_loading_callback_inited==0 );
1676
1677         Game_loading_background = bm_load(Game_loading_bground_fname[gr_screen.res]);
1678         //common_set_interface_palette("InterfacePalette");  // set the interface palette
1679
1680
1681         Game_loading_ani = anim_load( Game_loading_ani_fname[gr_screen.res]);
1682         Assert( Game_loading_ani != NULL );
1683         Game_loading_ani_instance = init_anim_instance(Game_loading_ani, 16);
1684         Assert( Game_loading_ani_instance != NULL );
1685         Game_loading_frame = -1;
1686
1687         Game_loading_callback_inited = 1;
1688         Mouse_hidden = 1;
1689         game_busy_callback( game_loading_callback, (COUNT_ESTIMATE/Game_loading_ani->total_frames)+1 ); 
1690
1691
1692 }
1693
1694 void game_loading_callback_close()
1695 {
1696         Assert( Game_loading_callback_inited==1 );
1697
1698         // Make sure bar shows all the way over.
1699         game_loading_callback(COUNT_ESTIMATE);
1700         
1701         int real_count = game_busy_callback( NULL );
1702         Mouse_hidden = 0;
1703
1704         Game_loading_callback_inited = 0;
1705         
1706 #ifndef NDEBUG
1707         mprintf(( "=================== ENDING LOAD ================\n" ));
1708         mprintf(( "Real count = %d,  Estimated count = %d\n", real_count, COUNT_ESTIMATE ));
1709         mprintf(( "================================================\n" ));
1710 #else
1711         // to remove warnings in release build
1712         real_count = 0;
1713 #endif
1714
1715         free_anim_instance(Game_loading_ani_instance);
1716         Game_loading_ani_instance = NULL;
1717         anim_free(Game_loading_ani);
1718         Game_loading_ani = NULL;
1719
1720         bm_release( Game_loading_background );
1721         common_free_interface_palette();                // restore game palette
1722         Game_loading_background = -1;
1723
1724         gr_set_font( FONT1 );
1725 }
1726
1727 // Update the sound environment (ie change EAX settings based on proximity to large ships)
1728 //
1729 void game_maybe_update_sound_environment()
1730 {
1731         // do nothing for now
1732 }
1733
1734 // Assign the sound environment for the game, based on the current mission
1735 //
1736 void game_assign_sound_environment()
1737 {
1738         /*
1739         if (The_mission.flags & MISSION_FLAG_SUBSPACE) {
1740                 Game_sound_env.id = SND_ENV_DRUGGED;
1741                 Game_sound_env.volume = 0.800f;
1742                 Game_sound_env.damping = 1.188f;
1743                 Game_sound_env.decay = 6.392f;
1744 #ifndef FS2_DEMO
1745         } else if (Num_asteroids > 30) {
1746                 Game_sound_env.id = SND_ENV_AUDITORIUM;
1747                 Game_sound_env.volume = 0.603f;
1748                 Game_sound_env.damping = 0.5f;
1749                 Game_sound_env.decay = 4.279f;
1750 #endif
1751         } else {
1752                 Game_sound_env = Game_default_sound_env;
1753         }
1754         */
1755
1756         Game_sound_env = Game_default_sound_env;
1757         Game_sound_env_update_timestamp = timestamp(1);
1758 }
1759
1760 // function which gets called before actually entering the mission.  It is broken down into a funciton
1761 // since it will get called in one place from a single player game and from another place for
1762 // a multiplayer game
1763 void freespace_mission_load_stuff()
1764 {
1765         // called if we're not on a freespace dedicated (non rendering, no pilot) server
1766         // IE : we _don't_ want to load any sounds or bitmap/texture info on this machine.
1767         if(!(Game_mode & GM_STANDALONE_SERVER)){        
1768         
1769                 mprintf(( "=================== STARTING LEVEL DATA LOAD ==================\n" ));
1770
1771                 game_loading_callback_init();
1772                 
1773                 event_music_level_init();       // preloads the first 2 seconds for each event music track
1774                 game_busy();
1775
1776                 gamesnd_unload_interface_sounds();              // unload interface sounds from memory
1777                 game_busy();
1778
1779                 gamesnd_preload_common_sounds();                        // load in sounds that are expected to play
1780                 game_busy();
1781
1782                 ship_assign_sound_all();        // assign engine sounds to ships
1783                 game_assign_sound_environment();         // assign the sound environment for this mission
1784                 game_busy();
1785
1786                 // call function in missionparse.cpp to fixup player/ai stuff.
1787                 mission_parse_fixup_players();
1788                 game_busy();
1789
1790                 // Load in all the bitmaps for this level
1791                 level_page_in();
1792
1793                 game_busy();
1794
1795                 game_loading_callback_close();  
1796         } 
1797         // the only thing we need to call on the standalone for now.
1798         else {
1799                 // call function in missionparse.cpp to fixup player/ai stuff.
1800                 mission_parse_fixup_players();
1801
1802                 // Load in all the bitmaps for this level
1803                 level_page_in();
1804         }
1805 }
1806
1807 uint load_gl_init;
1808 uint load_mission_load;
1809 uint load_post_level_init;
1810 uint load_mission_stuff;
1811
1812 // tells the server to load the mission and initialize structures
1813 int game_start_mission()
1814 {       
1815         mprintf(( "=================== STARTING LEVEL LOAD ==================\n" ));
1816         
1817         load_gl_init = time(NULL);
1818         game_level_init();
1819         load_gl_init = time(NULL) - load_gl_init;
1820         
1821         if (Game_mode & GM_MULTIPLAYER) {
1822                 Player->flags |= PLAYER_FLAGS_IS_MULTI;
1823
1824                 // clear multiplayer stats
1825                 init_multiplayer_stats();
1826         }
1827
1828         load_mission_load = time(NULL);
1829         if (mission_load()) {
1830                 if ( !(Game_mode & GM_MULTIPLAYER) ) {
1831                         popup(PF_BODY_BIG, 1, POPUP_OK, XSTR( "Attempt to load the mission failed", 169));
1832                         gameseq_post_event(GS_EVENT_MAIN_MENU);
1833                 } else {
1834                         multi_quit_game(PROMPT_NONE, MULTI_END_NOTIFY_NONE, MULTI_END_ERROR_LOAD_FAIL);
1835                 }
1836
1837                 return 0;
1838         }
1839         load_mission_load = time(NULL) - load_mission_load;
1840
1841         // If this is a red alert mission in campaign mode, bash wingman status
1842         if ( (Game_mode & GM_CAMPAIGN_MODE) && red_alert_mission() ) {
1843                 red_alert_bash_wingman_status();
1844         }
1845
1846         // the standalone server in multiplayer doesn't do any rendering, so we will not even bother loading the palette
1847         if ( !(Game_mode & GM_STANDALONE_SERVER) ) {
1848                 mprintf(( "=================== LOADING GAME PALETTE ================\n" ));
1849                 // game_load_palette();
1850         }
1851
1852         load_post_level_init = time(NULL);
1853         game_post_level_init();
1854         load_post_level_init = time(NULL) - load_post_level_init;
1855
1856         #ifndef NDEBUG
1857         {
1858                 void Do_model_timings_test();
1859                 Do_model_timings_test();        
1860         }
1861         #endif
1862
1863         load_mission_stuff = time(NULL);
1864         freespace_mission_load_stuff();
1865         load_mission_stuff = time(NULL) - load_mission_stuff;
1866
1867         return 1;
1868 }
1869
1870 int Interface_framerate = 0;
1871 #ifndef NDEBUG
1872
1873 DCF_BOOL( mouse_control, Use_mouse_to_fly )
1874 DCF_BOOL( show_framerate, Show_framerate )
1875 DCF_BOOL( show_target_debug_info, Show_target_debug_info )
1876 DCF_BOOL( show_target_weapons, Show_target_weapons )
1877 DCF_BOOL( lead_target_cheat, Players[Player_num].lead_target_cheat )
1878 DCF_BOOL( sound, Sound_enabled )
1879 DCF_BOOL( zbuffer, game_zbuffer )
1880 DCF_BOOL( shield_system, New_shield_system )
1881 DCF_BOOL( show_shield_mesh, Show_shield_mesh)
1882 DCF_BOOL( player_attacking, Player_attacking_enabled )
1883 DCF_BOOL( show_waypoints, Show_waypoints )
1884 DCF_BOOL( show_area_effect, Show_area_effect )
1885 DCF_BOOL( show_net_stats, Show_net_stats )
1886 DCF_BOOL( log, Log_debug_output_to_file )
1887 DCF_BOOL( training_msg_method, Training_msg_method )
1888 DCF_BOOL( show_player_pos, Show_player_pos )
1889 DCF_BOOL(i_framerate, Interface_framerate )
1890
1891 DCF(show_mem,"Toggles showing mem usage")
1892 {
1893         if ( Dc_command )       {       
1894                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1895                 if ( Dc_arg_type & ARG_TRUE )   Show_mem = 1;   
1896                 else if ( Dc_arg_type & ARG_FALSE ) Show_mem = 0;       
1897                 else if ( Dc_arg_type & ARG_NONE ) Show_mem ^= 1;       
1898
1899                 if ( Show_mem ) {
1900                         Show_cpu = 0;
1901                 }
1902         }       
1903         if ( Dc_help )  dc_printf( "Usage: Show_mem\nSets show_mem to true or false.  If nothing passed, then toggles it.\n" ); 
1904         if ( Dc_status )        {
1905                 dc_printf( "Show_mem is %s\n", (Show_mem?"TRUE":"FALSE") );     
1906                 dc_printf( "Show_cpu is %s\n", (Show_cpu?"TRUE":"FALSE") );     
1907         }
1908 }
1909
1910 DCF(show_cpu,"Toggles showing cpu usage")
1911 {
1912         if ( Dc_command )       {       
1913                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1914                 if ( Dc_arg_type & ARG_TRUE )   Show_cpu = 1;   
1915                 else if ( Dc_arg_type & ARG_FALSE ) Show_cpu = 0;       
1916                 else if ( Dc_arg_type & ARG_NONE ) Show_cpu ^= 1;       
1917
1918                 if ( Show_cpu ) {
1919                         Show_mem = 0;
1920                 }
1921         }       
1922         if ( Dc_help )  dc_printf( "Usage: Show_cpu\nSets show_cpu to true or false.  If nothing passed, then toggles it.\n" ); 
1923         if ( Dc_status )        {
1924                 dc_printf( "Show_mem is %s\n", (Show_mem?"TRUE":"FALSE") );     
1925                 dc_printf( "Show_cpu is %s\n", (Show_cpu?"TRUE":"FALSE") );     
1926
1927         }
1928 }
1929
1930 #else
1931
1932         // AL 4-8-98: always allow players to display their framerate
1933
1934         #ifdef FS2_DEMO
1935                 DCF_BOOL( show_framerate, Show_framerate )
1936         #endif
1937
1938 #endif  // NDEBUG
1939
1940                         int Game_init_seed;
1941
1942 DCF(use_joy_mouse,"Makes joystick move mouse cursor")
1943 {
1944         if ( Dc_command )       {       
1945                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1946                 if ( Dc_arg_type & ARG_TRUE )   Use_joy_mouse = 1;      
1947                 else if ( Dc_arg_type & ARG_FALSE ) Use_joy_mouse = 0;  
1948                 else if ( Dc_arg_type & ARG_NONE ) Use_joy_mouse ^= 1;  
1949         }       
1950         if ( Dc_help )  dc_printf( "Usage: use_joy_mouse [bool]\nSets use_joy_mouse to true or false.  If nothing passed, then toggles it.\n" );        
1951         if ( Dc_status )        dc_printf( "use_joy_mouse is %s\n", (Use_joy_mouse?"TRUE":"FALSE") );   
1952
1953         os_config_write_uint( NULL, NOX("JoystickMovesCursor"), Use_joy_mouse );
1954 }
1955
1956 DCF(palette_flash,"Toggles palette flash effect on/off")
1957 {
1958         if ( Dc_command )       {       
1959                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1960                 if ( Dc_arg_type & ARG_TRUE )   Use_palette_flash = 1;  
1961                 else if ( Dc_arg_type & ARG_FALSE ) Use_palette_flash = 0;      
1962                 else if ( Dc_arg_type & ARG_NONE ) Use_palette_flash ^= 1;      
1963         }       
1964         if ( Dc_help )  dc_printf( "Usage: palette_flash [bool]\nSets palette_flash to true or false.  If nothing passed, then toggles it.\n" );        
1965         if ( Dc_status )        dc_printf( "palette_flash is %s\n", (Use_palette_flash?"TRUE":"FALSE") );       
1966 }
1967
1968 int Use_low_mem = 0;
1969
1970 DCF(low_mem,"Uses low memory settings regardless of RAM")
1971 {
1972         if ( Dc_command )       {       
1973                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1974                 if ( Dc_arg_type & ARG_TRUE )   Use_low_mem = 1;        
1975                 else if ( Dc_arg_type & ARG_FALSE ) Use_low_mem = 0;    
1976                 else if ( Dc_arg_type & ARG_NONE ) Use_low_mem ^= 1;    
1977         }       
1978         if ( Dc_help )  dc_printf( "Usage: low_mem [bool]\nSets low_mem to true or false.  If nothing passed, then toggles it.\n" );    
1979         if ( Dc_status )        dc_printf( "low_mem is %s\n", (Use_low_mem?"TRUE":"FALSE") );   
1980
1981         os_config_write_uint( NULL, NOX("LowMem"), Use_low_mem );
1982 }
1983
1984
1985 #ifndef NDEBUG
1986
1987 DCF(force_fullscreen, "Forces game to startup in fullscreen mode")
1988 {
1989         if ( Dc_command )       {       
1990                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
1991                 if ( Dc_arg_type & ARG_TRUE )   Use_fullscreen_at_startup = 1;  
1992                 else if ( Dc_arg_type & ARG_FALSE ) Use_fullscreen_at_startup = 0;      
1993                 else if ( Dc_arg_type & ARG_NONE ) Use_fullscreen_at_startup ^= 1;      
1994         }       
1995         if ( Dc_help )  dc_printf( "Usage: force_fullscreen [bool]\nSets force_fullscreen to true or false.  If nothing passed, then toggles it.\n" );  
1996         if ( Dc_status )        dc_printf( "force_fullscreen is %s\n", (Use_fullscreen_at_startup?"TRUE":"FALSE") );    
1997         os_config_write_uint( NULL, NOX("ForceFullscreen"), Use_fullscreen_at_startup );
1998 }
1999 #endif
2000
2001 int     Framerate_delay = 0;
2002
2003 float Freespace_gamma = 1.0f;
2004
2005 DCF(gamma,"Sets Gamma factor")
2006 {
2007         if ( Dc_command )       {
2008                 dc_get_arg(ARG_FLOAT|ARG_NONE);
2009                 if ( Dc_arg_type & ARG_FLOAT )  {
2010                         Freespace_gamma = Dc_arg_float;
2011                 } else {
2012                         dc_printf( "Gamma reset to 1.0f\n" );
2013                         Freespace_gamma = 1.0f;
2014                 }
2015                 if ( Freespace_gamma < 0.1f )   {
2016                         Freespace_gamma = 0.1f;
2017                 } else if ( Freespace_gamma > 5.0f )    {
2018                         Freespace_gamma = 5.0f;
2019                 }
2020                 gr_set_gamma(Freespace_gamma);
2021
2022                 char tmp_gamma_string[32];
2023                 sprintf( tmp_gamma_string, NOX("%.2f"), Freespace_gamma );
2024                 os_config_write_string( NULL, NOX("Gamma"), tmp_gamma_string );
2025         }
2026
2027         if ( Dc_help )  {
2028                 dc_printf( "Usage: gamma <float>\n" );
2029                 dc_printf( "Sets gamma in range 1-3, no argument resets to default 1.2\n" );
2030                 Dc_status = 0;  // don't print status if help is printed.  Too messy.
2031         }
2032
2033         if ( Dc_status )        {
2034                 dc_printf( "Gamma = %.2f\n", Freespace_gamma );
2035         }
2036 }
2037
2038 void game_init()
2039 {
2040         char *ptr;
2041         int depth = 16;
2042
2043         Game_current_mission_filename[0] = 0;
2044
2045         // seed the random number generator
2046         Game_init_seed = time(NULL);
2047         srand( Game_init_seed );
2048
2049         Framerate_delay = 0;
2050
2051         #ifndef NDEBUG
2052         load_filter_info();
2053         #endif
2054
2055         extern void bm_init();
2056         bm_init();
2057
2058         // encrypt stuff
2059         encrypt_init();
2060
2061         // Initialize the timer before the os
2062         timer_init();
2063
2064         int s1, e1;
2065         // int s2, e2;
2066
2067         char whee[1024];
2068 #ifndef PLAT_UNIX       
2069         GetCurrentDirectory(1024, whee);
2070         strcat(whee, "\\");
2071 #else
2072         getcwd (whee, 1024);
2073         strcat(whee, "/");
2074 #endif
2075         strcat(whee, EXE_FNAME);
2076
2077         //Initialize the libraries
2078         s1 = timer_get_milliseconds();
2079         if(cfile_init(whee, Game_CDROM_dir)){                   // initialize before calling any cfopen stuff!!!
2080                 exit(1);
2081         }               
2082         e1 = timer_get_milliseconds();
2083
2084         // time a bunch of cfopens      
2085         /*
2086         s2 = timer_get_milliseconds();  
2087         CFILE *whee;
2088         for(int idx=0; idx<10000; idx++){
2089                 whee = cfopen("capital01.pof", "rb", CFILE_NORMAL, CF_TYPE_MODELS);
2090                 if(whee != NULL){
2091                         cfclose(whee);
2092                 }
2093                 whee = NULL;
2094                 //cf_exist("capital01.pof", CF_TYPE_MODELS);
2095         }
2096         e2 = timer_get_milliseconds();  
2097         */
2098
2099         if (Is_standalone) {
2100                 std_init_standalone();
2101         } else {                
2102                 os_init( Osreg_class_name, Osreg_app_name );
2103                 os_set_title(Osreg_title);
2104         }
2105
2106         // initialize localization module. Make sure this is down AFTER initialzing OS.
2107 //      int t1 = timer_get_milliseconds();
2108         lcl_init();     
2109         lcl_xstr_init();
2110 //      mprintf(("LCL_INIT() TOOK %d MS\n", timer_get_milliseconds()-t1));
2111
2112         // verify that he has a valid ships.tbl (will Game_ships_tbl_valid if so)
2113         verify_ships_tbl();
2114
2115         // verify that he has a valid weapons.tbl
2116         verify_weapons_tbl();
2117
2118         // Output version numbers to registry for auto patching purposes
2119         os_config_write_uint(NOX("Version"), NOX("Major"), FS_VERSION_MAJOR);
2120         os_config_write_uint(NOX("Version"), NOX("Minor"), FS_VERSION_MINOR);
2121         os_config_write_uint(NOX("Version"), NOX("Build"), FS_VERSION_BUILD);
2122
2123         Use_joy_mouse = 0;              //os_config_read_uint( NULL, NOX("JoystickMovesCursor"), 1 );
2124         //Use_palette_flash = os_config_read_uint( NULL, NOX("PaletteFlash"), 0 );
2125         Use_low_mem = os_config_read_uint( NULL, NOX("LowMem"), 0 );
2126
2127 #ifndef NDEBUG
2128         Use_fullscreen_at_startup = os_config_read_uint( NULL, NOX("ForceFullscreen"), 1 );
2129 #endif
2130
2131 #ifndef FS2_DEMO
2132         Asteroids_enabled = 1;          
2133 #endif
2134
2135 /////////////////////////////
2136 // SOUND INIT START
2137 /////////////////////////////
2138
2139         int use_a3d = 0;
2140         int use_eax = 0;
2141
2142         ptr = os_config_read_string(NULL, NOX("Soundcard"), NULL);
2143         mprintf(("soundcard = %s\n", ptr ? ptr : "<nothing>"));
2144         if (ptr) {
2145                 if (!stricmp(ptr, NOX("no sound"))) {
2146                         Cmdline_freespace_no_sound = 1;
2147
2148                 } else if (!stricmp(ptr, NOX("Aureal A3D"))) {
2149                         use_a3d = 1;
2150                 } else if (!stricmp(ptr, NOX("EAX"))) {
2151                         use_eax = 1;
2152                 }
2153         }
2154
2155         if (!Is_standalone) {
2156                 snd_init(use_a3d, use_eax);
2157         }
2158 /////////////////////////////
2159 // SOUND INIT END
2160 /////////////////////////////
2161         
2162         ptr = os_config_read_string(NULL, NOX("Videocard"), NULL);
2163         if (ptr == NULL) {
2164 #ifndef PLAT_UNIX       
2165                 MessageBox((HWND)os_get_window(), XSTR("Please configure your system in the Launcher before running FS2.\n\n The Launcher will now be started!", 1446), XSTR("Attention!", 1447), MB_OK);
2166
2167                 // fire up the UpdateLauncher executable
2168                 STARTUPINFO si;
2169                 PROCESS_INFORMATION pi;
2170
2171                 memset( &si, 0, sizeof(STARTUPINFO) );
2172                 si.cb = sizeof(si);
2173
2174                 BOOL ret = CreateProcess(       LAUNCHER_FNAME, // pointer to name of executable module 
2175                                                                         NULL,                                                   // pointer to command line string
2176                                                                         NULL,                                                   // pointer to process security attributes 
2177                                                                         NULL,                                                   // pointer to thread security attributes 
2178                                                                         FALSE,                                                  // handle inheritance flag 
2179                                                                         CREATE_DEFAULT_ERROR_MODE,              // creation flags 
2180                                                                         NULL,                                                   // pointer to new environment block 
2181                                                                         NULL,   // pointer to current directory name 
2182                                                                         &si,    // pointer to STARTUPINFO 
2183                                                                         &pi     // pointer to PROCESS_INFORMATION  
2184                                                                 );                      
2185
2186                 // If the Launcher could not be started up, let the user know
2187                 if (!ret) {
2188                         MessageBox((HWND)os_get_window(), XSTR("The Launcher could not be restarted.", 1450), XSTR("Error", 1451), MB_OK);
2189                 }
2190 #else
2191                 STUB_FUNCTION;
2192 #endif          
2193                 exit(1);
2194         }
2195
2196         if(!Is_standalone){
2197                 if(!stricmp(ptr, "Aucune accélération 3D") || !stricmp(ptr, "Keine 3D-Beschleunigerkarte") || !stricmp(ptr, "No 3D acceleration")){
2198 #ifndef PLAT_UNIX               
2199                         MessageBox((HWND)os_get_window(), XSTR("Warning, Freespace 2 requires Glide or Direct3D hardware accleration. You will not be able to run Freespace 2 without it.", 1448), XSTR("Warning", 1449), MB_OK);
2200 #else
2201                         STUB_FUNCTION;
2202 #endif                                          
2203                         exit(1);
2204                 }
2205         }
2206
2207         // check for hi res pack file 
2208         int has_sparky_hi = 0;
2209
2210         // check if sparky_hi exists -- access mode 0 means does file exist
2211         char dir[128];
2212         _getcwd(dir, 128);
2213         if ( _access("sparky_hi_fs2.vp", 0) == 0) {
2214                 has_sparky_hi = 1;
2215         } else {
2216                 mprintf(("No sparky_hi_fs2.vp in directory %s\n", dir));
2217         }
2218
2219         // see if we've got 32 bit in the string
2220         if(strstr(ptr, "32 bit")){
2221                 depth = 32;
2222         }
2223
2224         int trying_d3d = 0;
2225
2226 #ifndef PLAT_UNIX       
2227         if (!Is_standalone && ptr && (strstr(ptr, NOX("3DFX Glide")))) {
2228 #ifdef E3_BUILD
2229                 // always 640 for E3
2230                 gr_init(GR_640, GR_GLIDE);
2231 #else
2232                 // regular or hi-res ?
2233 #ifdef NDEBUG
2234                 if(has_sparky_hi && strstr(ptr, NOX("(1024x768)"))){
2235 #else
2236                 if(strstr(ptr, NOX("(1024x768)"))){
2237 #endif
2238                         gr_init(GR_1024, GR_GLIDE);
2239                 } else {                        
2240                         gr_init(GR_640, GR_GLIDE);
2241                 }
2242 #endif
2243         } else if (!Is_standalone && ptr && (strstr(ptr, NOX("Direct 3D -") ))) {
2244 #ifdef E3_BUILD         
2245                 // always 640 for E3
2246                 trying_d3d = 1;
2247                 gr_init(GR_640, GR_DIRECT3D, depth);            
2248 #else
2249                 // regular or hi-res ?
2250 #ifdef NDEBUG
2251                 if(has_sparky_hi && strstr(ptr, NOX("(1024x768)"))){
2252 #else
2253                 if(strstr(ptr, NOX("(1024x768)"))){
2254 #endif
2255                         // Direct 3D
2256                         trying_d3d = 1;
2257                         gr_init(GR_1024, GR_DIRECT3D, depth);
2258                 } else {
2259                         // Direct 3D
2260                         trying_d3d = 1;
2261                         gr_init(GR_640, GR_DIRECT3D, depth);
2262                 }
2263 #endif
2264         } else {
2265                 // Software
2266                 #ifndef NDEBUG
2267                         if ( Use_fullscreen_at_startup && !Is_standalone)       {               
2268                                 gr_init(GR_640, GR_DIRECTDRAW);
2269                         } else {
2270                                 gr_init(GR_640, GR_SOFTWARE);
2271                         }
2272                 #else
2273                         if ( !Is_standalone ) {
2274                                 gr_init(GR_640, GR_DIRECTDRAW);
2275                         } else {
2276                                 gr_init(GR_640, GR_SOFTWARE);
2277                         }
2278                 #endif
2279         }
2280 #else
2281         if (!Is_standalone /* && ptr && (strstr(ptr, NOX("OpenGL"))) */) {
2282                 if(has_sparky_hi && strstr(ptr, NOX("(1024x768)"))){
2283                         gr_init(GR_1024, GR_OPENGL);
2284                 } else {
2285                         gr_init(GR_640, GR_OPENGL);
2286                 }
2287         } else {
2288                 STUB_FUNCTION;
2289                 gr_init(GR_640, GR_SOFTWARE); 
2290         }
2291 #endif
2292
2293         // tried d3d ?
2294         extern int Gr_inited;
2295         if(trying_d3d && !Gr_inited){
2296                 extern char Device_init_error[512];
2297 #ifndef PLAT_UNIX               
2298                 MessageBox( NULL, Device_init_error, "Error intializing Direct3D", MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
2299 #lse
2300                 STUB_FUNCTION;
2301 #endif          
2302                 exit(1);
2303                 return;
2304         }
2305
2306         // Set the gamma
2307         ptr = os_config_read_string(NULL,NOX("Gamma"),NOX("1.80"));
2308         Freespace_gamma = (float)atof(ptr);
2309         if ( Freespace_gamma == 0.0f ) {
2310                 Freespace_gamma = 1.80f; 
2311         } else if ( Freespace_gamma < 0.1f ) {
2312                 Freespace_gamma = 0.1f;
2313         } else if ( Freespace_gamma > 5.0f ) {
2314                 Freespace_gamma = 5.0f;
2315         }
2316         char tmp_gamma_string[32];
2317         sprintf( tmp_gamma_string, NOX("%.2f"), Freespace_gamma );
2318         os_config_write_string( NULL, NOX("Gamma"), tmp_gamma_string );
2319
2320         gr_set_gamma(Freespace_gamma);
2321
2322 #if defined(FS2_DEMO) || defined(OEM_BUILD)
2323         // add title screen
2324         if(!Is_standalone){
2325                 display_title_screen();
2326         }
2327 #endif
2328         
2329         // attempt to load up master tracker registry info (login and password)
2330         Multi_tracker_id = -1;          
2331
2332         // pxo login and password
2333         ptr = os_config_read_string(NOX("PXO"),NOX("Login"),NULL);
2334         if(ptr == NULL){
2335                 nprintf(("Network","Error reading in PXO login data\n"));
2336                 strcpy(Multi_tracker_login,"");
2337         } else {                
2338                 strcpy(Multi_tracker_login,ptr);
2339         }
2340         ptr = os_config_read_string(NOX("PXO"),NOX("Password"),NULL);
2341         if(ptr == NULL){                
2342                 nprintf(("Network","Error reading PXO password\n"));
2343                 strcpy(Multi_tracker_passwd,"");
2344         } else {                
2345                 strcpy(Multi_tracker_passwd,ptr);
2346         }       
2347
2348         // pxo squad name and password
2349         ptr = os_config_read_string(NOX("PXO"),NOX("SquadName"),NULL);
2350         if(ptr == NULL){
2351                 nprintf(("Network","Error reading in PXO squad name\n"));
2352                 strcpy(Multi_tracker_squad_name, "");
2353         } else {                
2354                 strcpy(Multi_tracker_squad_name, ptr);
2355         }
2356
2357         // If less than 48MB of RAM, use low memory model.
2358         if ( (Freespace_total_ram < 48*1024*1024) || Use_low_mem )      {
2359                 mprintf(( "Using normal memory settings...\n" ));
2360                 bm_set_low_mem(1);              // Use every other frame of bitmaps
2361         } else {
2362                 mprintf(( "Using high memory settings...\n" ));
2363                 bm_set_low_mem(0);              // Use all frames of bitmaps
2364         }
2365
2366         // load non-darkening pixel defs
2367         palman_load_pixels();
2368
2369         // hud shield icon stuff
2370         hud_shield_game_init();
2371
2372         control_config_common_init();                           // sets up localization stuff in the control config
2373         parse_rank_tbl();
2374         parse_medal_tbl();
2375         cutscene_init();
2376         key_init();
2377         mouse_init();
2378         gamesnd_parse_soundstbl();
2379         radar_init();
2380         gameseq_init();
2381         multi_init();   
2382
2383         // standalone's don't use hte joystick and it seems to sometimes cause them to not get shutdown properly
2384         if(!Is_standalone){
2385                 joy_init();
2386         }
2387
2388         player_controls_init();
2389         model_init();   
2390
2391         //if(!Is_standalone){
2392                 event_music_init();
2393         //}     
2394
2395         obj_init();     
2396         mflash_game_init();     
2397         weapon_init();  
2398         ai_init();              
2399         ship_init();                                            // read in ships.tbl    
2400         player_init();  
2401         mission_campaign_init();                // load in the default campaign 
2402         anim_init();
2403 //      navmap_init();                                          // init the navigation map system
2404         context_help_init();                    
2405         techroom_intel_init();                  // parse species.tbl, load intel info   
2406         // initialize psnet
2407         psnet_init( Multi_options_g.protocol, Multi_options_g.port );                                           // initialize the networking code               
2408         init_animating_pointer();       
2409         asteroid_init();
2410         mission_brief_common_init();    // Mark all the briefing structures as empty.           
2411         gr_font_init();                                 // loads up all fonts           
2412
2413         neb2_init();                                            // fullneb stuff
2414         nebl_init();
2415         stars_init();
2416         ssm_init();     
2417         player_tips_init();                             // helpful tips
2418         beam_init();
2419         
2420         // load the list of pilot pic filenames (for barracks and pilot select popup quick reference)
2421         pilot_load_pic_list();  
2422         pilot_load_squad_pic_list();
2423
2424         load_animating_pointer(NOX("cursor"), 0, 0);    
2425
2426         // initialize alpha colors
2427         alpha_colors_init();    
2428
2429         Viewer_mode = 0;
2430 //      Game_music_paused = 0;
2431         Game_paused = 0;
2432
2433 #ifndef PLAT_UNIX
2434         timeBeginPeriod(1);     
2435 #endif
2436
2437         nprintf(("General", "Ships.tbl is : %s\n", Game_ships_tbl_valid ? "VALID" : "INVALID!!!!"));
2438         nprintf(("General", "Weapons.tbl is : %s\n", Game_weapons_tbl_valid ? "VALID" : "INVALID!!!!"));
2439
2440         mprintf(("cfile_init() took %d\n", e1 - s1));
2441         // mprintf(("1000 cfopens() took %d\n", e2 - s2));      
2442 }
2443
2444 char transfer_text[128];
2445
2446 float   Start_time = 0.0f;
2447
2448 float Framerate = 0.0f;
2449
2450 float Timing_total = 0.0f;
2451 float Timing_render2 = 0.0f;
2452 float Timing_render3 = 0.0f;
2453 float Timing_flip = 0.0f;
2454 float Timing_clear = 0.0f;
2455
2456 MONITOR(NumPolysDrawn);
2457 MONITOR(NumPolys);
2458 MONITOR(NumVerts);
2459 MONITOR(BmpUsed);
2460 MONITOR(BmpNew);
2461
2462 void game_get_framerate()
2463 {       
2464         char text[128] = "";
2465
2466         if ( frame_int == -1 )  {
2467                 int i;
2468                 for (i=0; i<FRAME_FILTER; i++ ) {
2469                         frametimes[i] = 0.0f;
2470                 }
2471                 frametotal = 0.0f;
2472                 frame_int = 0;
2473         }
2474         frametotal -= frametimes[frame_int];
2475         frametotal += flFrametime;
2476         frametimes[frame_int] = flFrametime;
2477         frame_int = (frame_int + 1 ) % FRAME_FILTER;
2478
2479         if ( frametotal != 0.0 )        {
2480                 if ( Framecount >= FRAME_FILTER )
2481                         Framerate = FRAME_FILTER / frametotal;
2482                 else
2483                         Framerate = Framecount / frametotal;
2484                 sprintf( text, NOX("FPS: %.1f"), Framerate );
2485         } else {
2486                 sprintf( text, NOX("FPS: ?") );
2487         }
2488         Framecount++;
2489
2490         if (Show_framerate)     {
2491                 gr_set_color_fast(&HUD_color_debug);
2492                 gr_string( 570, 2, text );
2493         }
2494 }
2495
2496 void game_show_framerate()
2497 {       
2498         float   cur_time;
2499
2500         cur_time = f2fl(timer_get_approx_seconds());
2501         if (cur_time - Start_time > 30.0f) {
2502                 mprintf(("%i frames executed in %7.3f seconds, %7.3f frames per second.\n", Framecount, cur_time - Start_time, Framecount/(cur_time - Start_time)));
2503                 Start_time += 1000.0f;
2504         }
2505
2506         //mprintf(( "%s\n", text ));
2507
2508 #ifndef NDEBUG
2509         if ( Debug_dump_frames )
2510                 return;
2511 #endif  
2512
2513         // possibly show control checking info
2514         control_check_indicate();
2515
2516 //      int bitmaps_used_this_frame, bitmaps_new_this_frame;
2517 //      bm_get_frame_usage(&bitmaps_used_this_frame,&bitmaps_new_this_frame);
2518 //      MONITOR_INC(BmpUsed, bitmaps_used_this_frame);
2519 // MONITOR_INC(BmpNew, bitmaps_new_this_frame);
2520
2521 #ifndef NDEBUG
2522         if ( Show_cpu == 1 ) {
2523                 
2524                 int sx,sy,dy;
2525                 sx = 530;
2526                 sy = 15;
2527                 dy = gr_get_font_height() + 1;
2528
2529                 gr_set_color_fast(&HUD_color_debug);
2530
2531                 {
2532 #ifndef PLAT_UNIX
2533                         extern int D3D_textures_in;
2534                         extern int D3D_textures_in_frame;
2535                         extern int Glide_textures_in;
2536                         extern int Glide_textures_in_frame;
2537                         extern int Glide_explosion_vram;
2538                         gr_printf( sx, sy, NOX("VRAM: %d KB\n"), (D3D_textures_in+Glide_textures_in)/1024 );
2539                         sy += dy;
2540                         gr_printf( sx, sy, NOX("VRAM: +%d KB\n"), (Glide_textures_in_frame+D3D_textures_in_frame)/1024 );
2541                         sy += dy;
2542                         gr_printf( sx, sy, NOX("EXP VRAM: %dKB\n"), (Glide_explosion_vram)/1024 );
2543                         sy += dy;
2544 #else
2545                         STUB_FUNCTION;
2546 #endif
2547                 }
2548 //              gr_printf( sx, sy, "BPP: %d", gr_screen.bits_per_pixel );
2549 //              sy += dy;
2550                 gr_printf( sx, sy, NOX("DMA: %s"), transfer_text );
2551                 sy += dy;
2552                 gr_printf( sx, sy, NOX("POLYP: %d"), modelstats_num_polys );
2553                 sy += dy;
2554                 gr_printf( sx, sy, NOX("POLYD: %d"), modelstats_num_polys_drawn );
2555                 sy += dy;
2556                 gr_printf( sx, sy, NOX("VERTS: %d"), modelstats_num_verts );
2557                 sy += dy;
2558
2559                 {
2560
2561                         extern int Num_pairs;           // Number of object pairs that were checked.
2562                         gr_printf( sx, sy, NOX("PAIRS: %d"), Num_pairs );
2563                         sy += dy;
2564
2565                         extern int Num_pairs_checked;   // What percent of object pairs were checked.
2566                         gr_printf( sx, sy, NOX("FVI: %d"), Num_pairs_checked );
2567                         sy += dy;
2568                         Num_pairs_checked = 0;
2569
2570                 }
2571
2572                 gr_printf( sx, sy, NOX("Snds: %d"), snd_num_playing() );
2573                 sy += dy;
2574
2575                 if ( Timing_total > 0.01f )     {
2576                         gr_printf(  sx, sy, NOX("CLEAR: %.0f%%"), Timing_clear*100.0f/Timing_total );
2577                         sy += dy;
2578                         gr_printf( sx, sy, NOX("REND2D: %.0f%%"), Timing_render2*100.0f/Timing_total );
2579                         sy += dy;
2580                         gr_printf( sx, sy, NOX("REND3D: %.0f%%"), Timing_render3*100.0f/Timing_total );
2581                         sy += dy;
2582                         gr_printf( sx, sy, NOX("FLIP: %.0f%%"), Timing_flip*100.0f/Timing_total );
2583                         sy += dy;
2584                         gr_printf( sx, sy, NOX("GAME: %.0f%%"), (Timing_total-(Timing_render2+Timing_render3+Timing_flip+Timing_clear))*100.0f/Timing_total );
2585                         sy += dy;
2586                 }
2587         }
2588                 
2589         if ( Show_mem  ) {
2590                 
2591                 int sx,sy,dy;
2592                 sx = 530;
2593                 sy = 15;
2594                 dy = gr_get_font_height() + 1;
2595
2596                 gr_set_color_fast(&HUD_color_debug);
2597
2598                 {
2599                         extern int TotalRam;
2600                         gr_printf( sx, sy, NOX("DYN: %d KB\n"), TotalRam/1024 );
2601                         sy += dy;
2602                 }       
2603
2604                 {
2605                         extern int Model_ram;
2606                         gr_printf( sx, sy, NOX("POF: %d KB\n"), Model_ram/1024 );
2607                         sy += dy;
2608                 }       
2609
2610                 gr_printf( sx, sy, NOX("BMP: %d KB\n"), bm_texture_ram/1024 );
2611                 sy += dy;
2612                 gr_printf( sx, sy, NOX("S-SRAM: %d KB\n"), Snd_sram/1024 );             // mem used to store game sound
2613                 sy += dy;
2614                 gr_printf( sx, sy, NOX("S-HRAM: %d KB\n"), Snd_hram/1024 );             // mem used to store game sound
2615                 sy += dy;
2616                 {
2617 #ifndef PLAT_UNIX
2618                         extern int D3D_textures_in;
2619                         extern int Glide_textures_in;
2620                         extern int Glide_textures_in_frame;
2621                         extern int Glide_explosion_vram;
2622                         gr_printf( sx, sy, NOX("VRAM: %d KB\n"), (D3D_textures_in+Glide_textures_in)/1024 );
2623                         sy += dy;
2624                         gr_printf( sx, sy, NOX("VRAM: +%d KB\n"), (Glide_textures_in_frame)/1024 );
2625                         sy += dy;
2626                         gr_printf( sx, sy, NOX("EXP VRAM: %dKB\n"), (Glide_explosion_vram)/1024 );
2627                         sy += dy;
2628 #else
2629                         STUB_FUNCTION;
2630 #endif
2631                 }
2632         }
2633
2634
2635         if ( Show_player_pos ) {
2636                 int sx, sy;
2637                 sx = 320;
2638                 sy = 100;
2639                 gr_printf(sx, sy, NOX("Player Pos: (%d,%d,%d)"), fl2i(Player_obj->pos.x), fl2i(Player_obj->pos.y), fl2i(Player_obj->pos.z));
2640         }
2641
2642         MONITOR_INC(NumPolys, modelstats_num_polys);
2643         MONITOR_INC(NumPolysDrawn, modelstats_num_polys_drawn );
2644         MONITOR_INC(NumVerts, modelstats_num_verts );
2645
2646         modelstats_num_polys = 0;
2647         modelstats_num_polys_drawn = 0;
2648         modelstats_num_verts = 0;
2649         modelstats_num_sortnorms = 0;
2650 #endif
2651 }
2652
2653 void game_show_standalone_framerate()
2654 {
2655         float frame_rate=30.0f;
2656         if ( frame_int == -1 )  {
2657                 int i;
2658                 for (i=0; i<FRAME_FILTER; i++ ) {
2659                         frametimes[i] = 0.0f;
2660                 }
2661                 frametotal = 0.0f;
2662                 frame_int = 0;
2663         }
2664         frametotal -= frametimes[frame_int];
2665         frametotal += flFrametime;
2666         frametimes[frame_int] = flFrametime;
2667         frame_int = (frame_int + 1 ) % FRAME_FILTER;
2668
2669         if ( frametotal != 0.0 )        {
2670                 if ( Framecount >= FRAME_FILTER ){
2671                         frame_rate = FRAME_FILTER / frametotal;
2672                 } else {
2673                         frame_rate = Framecount / frametotal;
2674                 }
2675         }
2676         std_set_standalone_fps(frame_rate);
2677         Framecount++;
2678 }
2679
2680 // function to show the time remaining in a mission.  Used only when the end-mission sexpression is used
2681 void game_show_time_left()
2682 {
2683         int diff;
2684
2685         // mission_end_time is a global from missionparse.cpp that contains the mission time at which the
2686         // mission should end (in fixed seconds).  There is code in missionparse.cpp which actually handles
2687         // checking how much time is left
2688
2689         if ( Mission_end_time == -1 ){
2690                 return;
2691         }
2692
2693         diff = f2i(Mission_end_time - Missiontime);
2694         // be sure to bash to 0.  diff could be negative on frame that we quit mission
2695         if ( diff < 0 ){
2696                 diff = 0;
2697         }
2698
2699         hud_set_default_color();
2700         gr_printf( 5, 40, XSTR( "Mission time remaining: %d seconds", 179), diff );
2701 }
2702
2703 //========================================================================================
2704 //=================== NEW DEBUG CONSOLE COMMANDS TO REPLACE OLD DEBUG PAUSE MENU =========
2705 //========================================================================================
2706
2707 #ifndef NDEBUG
2708
2709 DCF(ai_pause,"Pauses ai")
2710 {
2711         if ( Dc_command )       {       
2712                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
2713                 if ( Dc_arg_type & ARG_TRUE )   ai_paused = 1;  
2714                 else if ( Dc_arg_type & ARG_FALSE ) ai_paused = 0;      
2715                 else if ( Dc_arg_type & ARG_NONE ) ai_paused = !ai_paused;      
2716
2717                 if (ai_paused)  {       
2718                         obj_init_all_ships_physics();
2719                 }
2720         }       
2721         if ( Dc_help )  dc_printf( "Usage: ai_paused [bool]\nSets ai_paused to true or false.  If nothing passed, then toggles it.\n" );        
2722         if ( Dc_status )        dc_printf( "ai_paused is %s\n", (ai_paused?"TRUE":"FALSE") );   
2723 }
2724
2725 DCF(single_step,"Single steps the game")
2726 {
2727         if ( Dc_command )       {       
2728                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_NONE);                
2729                 if ( Dc_arg_type & ARG_TRUE )   game_single_step = 1;   
2730                 else if ( Dc_arg_type & ARG_FALSE ) game_single_step = 0;       
2731                 else if ( Dc_arg_type & ARG_NONE ) game_single_step = !game_single_step;        
2732
2733                 last_single_step = 0;   // Make so single step waits a frame before stepping
2734
2735         }       
2736         if ( Dc_help )  dc_printf( "Usage: single_step [bool]\nSets single_step to true or false.  If nothing passed, then toggles it.\n" );    
2737         if ( Dc_status )        dc_printf( "single_step is %s\n", (game_single_step?"TRUE":"FALSE") );  
2738 }
2739
2740 DCF_BOOL(physics_pause, physics_paused)
2741 DCF_BOOL(ai_rendering, Ai_render_debug_flag)
2742 DCF_BOOL(ai_firing, Ai_firing_enabled )
2743
2744 // Create some simple aliases to these commands...
2745 debug_command dc_s("s","shortcut for single_step",dcf_single_step);
2746 debug_command dc_p("p","shortcut for physics_pause", dcf_physics_pause );
2747 debug_command dc_r("r","shortcut for ai_rendering", dcf_ai_rendering );
2748 debug_command dc_f("f","shortcut for ai_firing", dcf_ai_firing);
2749 debug_command dc_a("a","shortcut for ai_pause", dcf_ai_pause);
2750 #endif
2751
2752 //========================================================================================
2753 //========================================================================================
2754
2755
2756 void game_training_pause_do()
2757 {
2758         int key;
2759
2760         key = game_check_key();
2761         if (key > 0){
2762                 gameseq_post_event(GS_EVENT_PREVIOUS_STATE);
2763         }
2764
2765         gr_flip();
2766 }
2767
2768
2769 void game_increase_skill_level()
2770 {
2771         Game_skill_level++;
2772         if (Game_skill_level >= NUM_SKILL_LEVELS){
2773                 Game_skill_level = 0;
2774         }
2775 }
2776
2777 int     Player_died_time;
2778
2779 int View_percent = 100;
2780
2781
2782 DCF(view, "Sets the percent of the 3d view to render.")
2783 {
2784         if ( Dc_command ) {
2785                 dc_get_arg(ARG_INT);
2786                 if ( (Dc_arg_int >= 5 ) || (Dc_arg_int <= 100) ) {
2787                         View_percent = Dc_arg_int;
2788                 } else {
2789                         dc_printf( "Illegal value for view. (Must be from 5-100) \n\n");
2790                         Dc_help = 1;
2791                 }
2792         }
2793
2794         if ( Dc_help ) {
2795                 dc_printf("Usage: view [n]\nwhere n is percent of view to show (5-100).\n");
2796         }
2797         
2798         if ( Dc_status ) {
2799                 dc_printf("View is set to %d%%\n", View_percent );
2800         }
2801 }
2802
2803
2804 // Set the clip region for the 3d rendering window
2805 void game_set_view_clip()
2806 {
2807         if ((Game_mode & GM_DEAD) || (supernova_active() >= 2)) {
2808                 // Set the clip region for the letterbox "dead view"
2809                 int yborder = gr_screen.max_h/4;
2810
2811                 //      Numeric constants encouraged by J "pig farmer" S, who shall remain semi-anonymous.
2812                 // J.S. I've changed my ways!! See the new "no constants" code!!!
2813                 gr_set_clip(0, yborder, gr_screen.max_w, gr_screen.max_h - yborder*2 ); 
2814         } else {
2815                 // Set the clip region for normal view
2816                 if ( View_percent >= 100 )      {
2817                         gr_reset_clip();
2818                 } else {
2819                         int xborder, yborder;
2820
2821                         if ( View_percent < 5 ) {
2822                                 View_percent = 5;
2823                         }
2824
2825                         float fp = i2fl(View_percent)/100.0f;
2826                         int fi = fl2i(fl_sqrt(fp)*100.0f);
2827                         if ( fi > 100 ) fi=100;
2828                         
2829                         xborder = ( gr_screen.max_w*(100-fi) )/200;
2830                         yborder = ( gr_screen.max_h*(100-fi) )/200;
2831
2832                         gr_set_clip(xborder, yborder, gr_screen.max_w-xborder*2,gr_screen.max_h-yborder*2 );
2833                 }
2834         }
2835 }
2836
2837
2838 void show_debug_stuff()
2839 {
2840         int     i;
2841         int     laser_count = 0, missile_count = 0;
2842
2843         for (i=0; i<MAX_OBJECTS; i++) {
2844                 if (Objects[i].type == OBJ_WEAPON){
2845                         if (Weapon_info[Weapons[Objects[i].instance].weapon_info_index].subtype == WP_LASER){
2846                                 laser_count++;
2847                         } else if (Weapon_info[Weapons[Objects[i].instance].weapon_info_index].subtype == WP_MISSILE){
2848                                 missile_count++;
2849                         }
2850                 }
2851         }
2852
2853         nprintf(("Mike", "Frame: %i Lasers: %4i, Missiles: %4i\n", Framecount, laser_count, missile_count));
2854 }
2855
2856 extern int Tool_enabled;
2857 int tst = 0;
2858 int tst_time = 0;
2859 int tst_big = 0;
2860 vector tst_pos;
2861 int tst_bitmap = -1;
2862 float tst_x, tst_y;
2863 float tst_offset, tst_offset_total;
2864 int tst_mode;
2865 int tst_stamp;
2866 void game_tst_frame_pre()
2867 {
2868         // start tst
2869         if(tst == 3){
2870                 tst = 0;
2871
2872                 // screen position
2873                 vertex v;
2874                 g3_rotate_vertex(&v, &tst_pos);
2875                 g3_project_vertex(&v);  
2876         
2877                 // offscreen
2878                 if(!((v.sx >= 0) && (v.sx <= gr_screen.max_w) && (v.sy >= 0) && (v.sy <= gr_screen.max_h))){
2879                         return;
2880                 }       
2881
2882                 // big ship? always tst
2883                 if(tst_big){
2884                         // within 3000 meters
2885                         if( vm_vec_dist_quick(&tst_pos, &Eye_position) <= 3000.0f){
2886                                 tst = 2;                                
2887                         }
2888                 } else {                        
2889                         // within 300 meters
2890                         if( (vm_vec_dist_quick(&tst_pos, &Eye_position) <= 300.0f) && ((tst_time == 0) || ((time(NULL) - tst_time) >= 10)) ){
2891                                 tst = 2;                                
2892                         } 
2893                 }                       
2894         }
2895
2896 }
2897 void game_tst_frame()
2898 {
2899         int left = 0;
2900
2901         if(!Tool_enabled){
2902                 return;
2903         }
2904         
2905         // setup tst
2906         if(tst == 2){           
2907                 tst_time = time(NULL);
2908
2909                 // load the tst bitmap          
2910                 switch((int)frand_range(0.0f, 3.0)){
2911                 case 0:                 
2912                         tst_bitmap = bm_load("ig_jim");
2913                         left = 1;
2914                         mprintf(("TST 0\n"));
2915                         break;
2916
2917                 case 1:
2918                         tst_bitmap = bm_load("ig_kan");
2919                         left = 0;
2920                         mprintf(("TST 1\n"));
2921                         break;
2922
2923                 case 2:
2924                         tst_bitmap = bm_load("ig_jim");
2925                         left = 1;
2926                         mprintf(("TST 2\n"));
2927                         break;
2928                         
2929                 default:                        
2930                         tst_bitmap = bm_load("ig_kan");
2931                         left = 0;
2932                         mprintf(("TST 3\n"));
2933                         break;
2934                 }
2935
2936                 if(tst_bitmap < 0){
2937                         tst = 0;
2938                         return;
2939                 }               
2940
2941                 // get the tst bitmap dimensions
2942                 int w, h;
2943                 bm_get_info(tst_bitmap, &w, &h);
2944
2945                 // tst y
2946                 tst_y = frand_range(0.0f, (float)gr_screen.max_h - h);
2947
2948                 snd_play(&Snds[SND_VASUDAN_BUP]);
2949
2950                 // tst x and direction
2951                 tst_mode = 0;
2952                 if(left){
2953                         tst_x = (float)-w;
2954                         tst_offset_total = (float)w;
2955                         tst_offset = (float)w;
2956                 } else {
2957                         tst_x = (float)gr_screen.max_w;
2958                         tst_offset_total = (float)-w;
2959                         tst_offset = (float)w;
2960                 }
2961
2962                 tst = 1;
2963         }
2964
2965         // run tst
2966         if(tst == 1){
2967                 float diff = (tst_offset_total / 0.5f) * flFrametime;
2968
2969                 // move the bitmap
2970                 if(tst_mode == 0){
2971                         tst_x += diff;
2972                         
2973                         tst_offset -= fl_abs(diff);
2974                 } else if(tst_mode == 2){
2975                         tst_x -= diff;
2976                         
2977                         tst_offset -= fl_abs(diff);
2978                 }
2979
2980                 // draw the bitmap
2981                 gr_set_bitmap(tst_bitmap);
2982                 gr_bitmap((int)tst_x, (int)tst_y);
2983
2984                 if(tst_mode == 1){
2985                         if(timestamp_elapsed_safe(tst_stamp, 1100)){
2986                                 tst_mode = 2;
2987                         }
2988                 } else {
2989                         // if we passed the switch point
2990                         if(tst_offset <= 0.0f){
2991                                 // switch modes
2992                                 switch(tst_mode){
2993                                 case 0:
2994                                         tst_mode = 1;
2995                                         tst_stamp = timestamp(1000);
2996                                         tst_offset = fl_abs(tst_offset_total);
2997                                         break;                          
2998
2999                                 case 2:                         
3000                                         tst = 0;
3001                                         return;
3002                                 }
3003                         }                               
3004                 }
3005         }
3006 }
3007 void game_tst_mark(object *objp, ship *shipp)
3008 {
3009         ship_info *sip; 
3010
3011         if(!Tool_enabled){
3012                 return;
3013         }
3014
3015         // bogus
3016         if((objp == NULL) || (shipp == NULL) || (shipp->ship_info_index < 0) || (shipp->ship_info_index >= Num_ship_types)){
3017                 return;
3018         }
3019         sip = &Ship_info[shipp->ship_info_index];
3020
3021         // already tst
3022         if(tst){
3023                 return;
3024         }
3025
3026         tst_pos = objp->pos;
3027         if(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
3028                 tst_big = 1;
3029         }
3030         tst = 3;
3031 }
3032
3033 extern void render_shields();
3034
3035 void player_repair_frame(float frametime)
3036 {
3037         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
3038                 int idx;
3039                 for(idx=0;idx<MAX_PLAYERS;idx++){
3040                         net_player *np;
3041
3042                         np = &Net_players[idx];
3043
3044                         if(MULTI_CONNECTED(Net_players[idx]) && (Net_player != NULL) && (Net_player->player_id != Net_players[idx].player_id) && (Net_players[idx].player != NULL) && (Net_players[idx].player->objnum >= 0) && (Net_players[idx].player->objnum < MAX_OBJECTS)){
3045
3046                                 // don't rearm/repair if the player is dead or dying/departing
3047                                 if ( !NETPLAYER_IS_DEAD(np) && !(Ships[Objects[np->player->objnum].instance].flags & (SF_DYING|SF_DEPARTING)) ) {
3048                                         ai_do_repair_frame(&Objects[Net_players[idx].player->objnum],&Ai_info[Ships[Objects[Net_players[idx].player->objnum].instance].ai_index],frametime);
3049                                 }
3050                         }
3051                 }
3052         }       
3053         if ( (Player_obj != NULL) && (Player_obj->type == OBJ_SHIP) && !(Game_mode & GM_STANDALONE_SERVER) && (Player_ship != NULL) && !(Player_ship->flags & SF_DYING) ) {
3054                 ai_do_repair_frame(Player_obj, &Ai_info[Ships[Player_obj->instance].ai_index], frametime);
3055         }
3056 }
3057
3058
3059 #ifndef NDEBUG
3060 #define NUM_FRAMES_TEST         300
3061 #define NUM_MIXED_SOUNDS        16
3062 void do_timing_test(float flFrametime)
3063 {
3064         static int framecount = 0;
3065         static int test_running = 0;
3066         static float test_time = 0.0f;
3067
3068         static int snds[NUM_MIXED_SOUNDS];
3069         int i;
3070
3071         if ( test_running ) {
3072                 framecount++;
3073                 test_time += flFrametime;
3074                 if ( framecount >= NUM_FRAMES_TEST ) {
3075                         test_running = 0;
3076                         nprintf(("General", "%d frames took %.3f seconds\n", NUM_FRAMES_TEST, test_time));
3077                         for ( i = 0; i < NUM_MIXED_SOUNDS; i++ )
3078                                 snd_stop(snds[i]);
3079                 }
3080         }
3081
3082         if ( Test_begin == 1 ) {
3083                 framecount = 0;
3084                 test_running = 1;
3085                 test_time = 0.0f;
3086                 Test_begin = 0;
3087
3088                 for ( i = 0; i < NUM_MIXED_SOUNDS; i++ )
3089                         snds[i] = -1;
3090
3091                 // start looping digital sounds
3092                 for ( i = 0; i < NUM_MIXED_SOUNDS; i++ )
3093                         snds[i] = snd_play_looping( &Snds[i], 0.0f, -1, -1);
3094         }
3095         
3096
3097 }
3098 #endif
3099
3100 DCF(dcf_fov, "Change the field of view")
3101 {
3102         if ( Dc_command )       {
3103                 dc_get_arg(ARG_FLOAT|ARG_NONE);
3104                 if ( Dc_arg_type & ARG_NONE )   {
3105                         Viewer_zoom = VIEWER_ZOOM_DEFAULT;
3106                         dc_printf( "Zoom factor reset\n" );
3107                 }
3108                 if ( Dc_arg_type & ARG_FLOAT )  {
3109                         if (Dc_arg_float < 0.25f) {
3110                                 Viewer_zoom = 0.25f;
3111                                 dc_printf("Zoom factor pinned at 0.25.\n");
3112                         } else if (Dc_arg_float > 1.25f) {
3113                                 Viewer_zoom = 1.25f;
3114                                 dc_printf("Zoom factor pinned at 1.25.\n");
3115                         } else {
3116                                 Viewer_zoom = Dc_arg_float;
3117                         }
3118                 }
3119         }
3120
3121         if ( Dc_help )  
3122                 dc_printf( "Usage: fov [factor]\nFactor is the zoom factor btwn .25 and 1.25\nNo parameter resets it to default.\n" );
3123
3124         if ( Dc_status )                                
3125                 dc_printf("Zoom factor set to %6.3f (original = 0.5, John = 0.75)", Viewer_zoom);
3126 }
3127
3128
3129 DCF(framerate_cap, "Sets the framerate cap")
3130 {
3131         if ( Dc_command ) {
3132                 dc_get_arg(ARG_INT);
3133                 if ( (Dc_arg_int >= 1 ) || (Dc_arg_int <= 120) ) {
3134                         Framerate_cap = Dc_arg_int;
3135                 } else {
3136                         dc_printf( "Illegal value for framerate cap. (Must be from 1-120) \n\n");
3137                         Dc_help = 1;
3138                 }
3139         }
3140
3141         if ( Dc_help ) {
3142                 dc_printf("Usage: framerate_cap [n]\nwhere n is the frames per second to cap framerate at.\n");
3143                 dc_printf("If n is 0 or omitted, then the framerate cap is removed\n");
3144                 dc_printf("[n] must be from 1 to 120.\n");
3145         }
3146         
3147         if ( Dc_status ) {
3148                 if ( Framerate_cap )
3149                         dc_printf("Framerate cap is set to %d fps\n", Framerate_cap );
3150                 else
3151                         dc_printf("There is no framerate cap currently active.\n");
3152         }
3153 }
3154
3155 #define MIN_DIST_TO_DEAD_CAMERA         50.0f
3156 int Show_viewing_from_self = 0;
3157
3158 void say_view_target()
3159 {
3160         object  *view_target;
3161
3162         if ((Viewer_mode & VM_OTHER_SHIP) && (Player_ai->target_objnum != -1))
3163                 view_target = &Objects[Player_ai->target_objnum];
3164         else
3165                 view_target = Player_obj;
3166
3167         if (Game_mode & GM_DEAD) {
3168                 if (Player_ai->target_objnum != -1)
3169                         view_target = &Objects[Player_ai->target_objnum];
3170         }
3171
3172         if (!(Game_mode & GM_DEAD_DIED) && ((Game_mode & (GM_DEAD_BLEW_UP)) || ((Last_view_target != NULL) && (Last_view_target != view_target)))) {
3173                 if (view_target != Player_obj){
3174
3175                         char *view_target_name = NULL;
3176                         switch(Objects[Player_ai->target_objnum].type) {
3177                         case OBJ_SHIP:
3178                                 view_target_name = Ships[Objects[Player_ai->target_objnum].instance].ship_name;
3179                                 break;
3180                         case OBJ_WEAPON:
3181                                 view_target_name = Weapon_info[Weapons[Objects[Player_ai->target_objnum].instance].weapon_info_index].name;
3182                                 Viewer_mode &= ~VM_OTHER_SHIP;
3183                                 break;
3184                         case OBJ_JUMP_NODE: {
3185                                 char    jump_node_name[128];
3186                                 strcpy(jump_node_name, XSTR( "jump node", 184));
3187                                 view_target_name = jump_node_name;
3188                                 Viewer_mode &= ~VM_OTHER_SHIP;
3189                                 break;
3190                                 }
3191
3192                         default:
3193                                 Int3();
3194                                 break;
3195                         }
3196
3197                         if ( view_target_name ) {
3198                                 HUD_fixed_printf(0.0f, XSTR( "Viewing %s%s\n", 185), (Viewer_mode & VM_OTHER_SHIP) ? XSTR( "from ", 186) : "", view_target_name);
3199                                 Show_viewing_from_self = 1;
3200                         }
3201                 } else {
3202                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER) && (Player_obj->type == OBJ_OBSERVER)){
3203                                 HUD_fixed_printf(2.0f,XSTR( "Viewing from observer\n", 187));
3204                                 Show_viewing_from_self = 1;
3205                         } else {
3206                                 if (Show_viewing_from_self)
3207                                         HUD_fixed_printf(2.0f, XSTR( "Viewing from self\n", 188));
3208                         }
3209                 }
3210         }
3211
3212         Last_view_target = view_target;
3213 }
3214
3215
3216 float Game_hit_x = 0.0f;
3217 float Game_hit_y = 0.0f;
3218
3219 // Reset at the beginning of each frame
3220 void game_whack_reset()
3221 {
3222         Game_hit_x = 0.0f;
3223         Game_hit_y = 0.0f;
3224 }
3225
3226 // Apply a 2d whack to the player
3227 void game_whack_apply( float x, float y )
3228 {
3229         // Do some force feedback
3230         joy_ff_play_dir_effect(x * 80.0f, y * 80.0f);
3231
3232         // Move the eye 
3233         Game_hit_x += x;
3234         Game_hit_y += y;
3235
3236 //      mprintf(( "WHACK = %.1f, %.1f\n", Game_hit_x, Game_hit_y ));
3237 }
3238
3239 // call to apply a "shudder"
3240 void game_shudder_apply(int time, float intensity)
3241 {
3242         Game_shudder_time = timestamp(time);
3243         Game_shudder_total = time;
3244         Game_shudder_intensity = intensity;
3245 }
3246
3247 #define FF_SCALE        10000
3248 void apply_hud_shake(matrix *eye_orient)
3249 {
3250         if (Viewer_obj == Player_obj) {
3251                 physics_info    *pi = &Player_obj->phys_info;
3252
3253                 angles  tangles;
3254
3255                 tangles.p = 0.0f;
3256                 tangles.h = 0.0f;
3257                 tangles.b = 0.0f;
3258
3259                 //      Make eye shake due to afterburner
3260                 if ( !timestamp_elapsed(pi->afterburner_decay) ) {                      
3261                         int             dtime;
3262
3263                         dtime = timestamp_until(pi->afterburner_decay);
3264                         
3265                         int r1 = myrand();
3266                         int r2 = myrand();
3267                         tangles.p += 0.07f * (float) (r1-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME));
3268                         tangles.h += 0.07f * (float) (r2-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME));
3269                 }
3270
3271                 // Make eye shake due to engine wash
3272                 extern int Wash_on;
3273                 if (Player_obj->type == OBJ_SHIP && (Ships[Player_obj->instance].wash_intensity > 0) && Wash_on ) {
3274                         int r1 = myrand();
3275                         int r2 = myrand();
3276                         tangles.p += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r1-RAND_MAX/2)/RAND_MAX;
3277                         tangles.h += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r2-RAND_MAX/2)/RAND_MAX;
3278
3279                         // get the   intensity
3280                         float intensity = FF_SCALE * Ships[Player_obj->instance].wash_intensity;
3281
3282                         // vector rand_vec
3283                         vector rand_vec;
3284                         vm_vec_rand_vec_quick(&rand_vec);
3285
3286                         // play the effect
3287                         joy_ff_play_dir_effect(intensity*rand_vec.x, intensity*rand_vec.y);
3288                 }
3289
3290         
3291                 // make hud shake due to shuddering
3292                 if(Game_shudder_time != -1){
3293                         // if the timestamp has elapsed
3294                         if(timestamp_elapsed(Game_shudder_time)){
3295                                 Game_shudder_time = -1;
3296                         } 
3297                         // otherwise apply some shudder
3298                         else {
3299                                 int dtime;
3300
3301                                 dtime = timestamp_until(Game_shudder_time);
3302                         
3303                                 int r1 = myrand();
3304                                 int r2 = myrand();
3305                                 tangles.p += (Game_shudder_intensity / 200.0f) * (float) (r1-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total));
3306                                 tangles.h += (Game_shudder_intensity / 200.0f) * (float) (r2-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total));
3307                         }
3308                 }
3309
3310                 matrix  tm, tm2;
3311                 vm_angles_2_matrix(&tm, &tangles);
3312                 Assert(vm_vec_mag(&tm.fvec) > 0.0f);
3313                 Assert(vm_vec_mag(&tm.rvec) > 0.0f);
3314                 Assert(vm_vec_mag(&tm.uvec) > 0.0f);
3315                 vm_matrix_x_matrix(&tm2, eye_orient, &tm);
3316                 *eye_orient = tm2;
3317         }
3318 }
3319
3320 extern void compute_slew_matrix(matrix *orient, angles *a);     // TODO: move code to proper place and extern in header file
3321
3322 //      Player's velocity just before he blew up.  Used to keep camera target moving.
3323 vector  Dead_player_last_vel = {1.0f, 1.0f, 1.0f};
3324
3325 //      Set eye_pos and eye_orient based on view mode.
3326 void game_render_frame_setup(vector *eye_pos, matrix *eye_orient)
3327 {
3328         vector  eye_dir;
3329
3330         static int last_Viewer_mode = 0;
3331         static int last_Game_mode = 0;
3332         static int last_Viewer_objnum = -1;
3333
3334         // This code is supposed to detect camera "cuts"... like going between
3335         // different views.
3336
3337         // determine if we need to regenerate the nebula
3338         if(     (!(last_Viewer_mode & VM_EXTERNAL) && (Viewer_mode & VM_EXTERNAL)) ||                                                   // internal to external 
3339                         ((last_Viewer_mode & VM_EXTERNAL) && !(Viewer_mode & VM_EXTERNAL)) ||                                                   // external to internal
3340                         (!(last_Viewer_mode & VM_DEAD_VIEW) && (Viewer_mode & VM_DEAD_VIEW)) ||                                                 // non dead-view to dead-view
3341                         ((last_Viewer_mode & VM_DEAD_VIEW) && !(Viewer_mode & VM_DEAD_VIEW)) ||                                                 // dead-view to non dead-view
3342                         (!(last_Viewer_mode & VM_WARP_CHASE) && (Viewer_mode & VM_WARP_CHASE)) ||                                               // non warp-chase to warp-chase
3343                         ((last_Viewer_mode & VM_WARP_CHASE) && !(Viewer_mode & VM_WARP_CHASE)) ||                                               // warp-chase to non warp-chase
3344                         (!(last_Viewer_mode & VM_OTHER_SHIP) && (Viewer_mode & VM_OTHER_SHIP)) ||                                               // non other-ship to other-ship
3345                         ((last_Viewer_mode & VM_OTHER_SHIP) && !(Viewer_mode & VM_OTHER_SHIP)) ||                                               // other-ship to non-other ship
3346                         ((Viewer_mode & VM_OTHER_SHIP) && (last_Viewer_objnum != Player_ai->target_objnum))             // other ship mode, but targets changes
3347                         ) {
3348
3349                 // regenerate the nebula
3350                 neb2_eye_changed();
3351         }               
3352
3353         if ( (last_Viewer_mode != Viewer_mode) || (last_Game_mode != Game_mode) )       {
3354                 //mprintf(( "************** Camera cut! ************\n" ));
3355                 last_Viewer_mode = Viewer_mode;
3356                 last_Game_mode = Game_mode;
3357
3358                 // Camera moved.  Tell stars & debris to not do blurring.
3359                 stars_camera_cut();             
3360         }
3361
3362         say_view_target();
3363
3364         if ( Viewer_mode & VM_PADLOCK_ANY ) {
3365                 player_display_packlock_view();
3366         }
3367         
3368         game_set_view_clip();
3369
3370         if (Game_mode & GM_DEAD) {
3371                 vector  vec_to_deader, view_pos;
3372                 float           dist;
3373
3374                 Viewer_mode |= VM_DEAD_VIEW;
3375
3376                 if (Player_ai->target_objnum != -1) {
3377                         int view_from_player = 1;
3378
3379                         if (Viewer_mode & VM_OTHER_SHIP) {
3380                                 //      View from target.
3381                                 Viewer_obj = &Objects[Player_ai->target_objnum];
3382
3383                                 last_Viewer_objnum = Player_ai->target_objnum;
3384
3385                                 if ( Viewer_obj->type == OBJ_SHIP ) {
3386                                         ship_get_eye( eye_pos, eye_orient, Viewer_obj );
3387                                         view_from_player = 0;
3388                                 }
3389                         } else {
3390                                 last_Viewer_objnum = -1;
3391                         }
3392
3393                         if ( view_from_player ) {
3394                                 //      View target from player ship.
3395                                 Viewer_obj = NULL;
3396                                 *eye_pos = Player_obj->pos;
3397                                 vm_vec_normalized_dir(&eye_dir, &Objects[Player_ai->target_objnum].pos, eye_pos);
3398                                 vm_vector_2_matrix(eye_orient, &eye_dir, NULL, NULL);
3399                         }
3400                 } else {
3401                         dist = vm_vec_normalized_dir(&vec_to_deader, &Player_obj->pos, &Dead_camera_pos);
3402                         
3403                         if (dist < MIN_DIST_TO_DEAD_CAMERA)
3404                                 dist += flFrametime * 16.0f;
3405
3406                         vm_vec_scale(&vec_to_deader, -dist);
3407                         vm_vec_add(&Dead_camera_pos, &Player_obj->pos, &vec_to_deader);
3408                         
3409                         view_pos = Player_obj->pos;
3410
3411                         if (!(Game_mode & GM_DEAD_BLEW_UP)) {
3412                                 Viewer_mode &= ~(VM_EXTERNAL | VM_CHASE);
3413                                 vm_vec_scale_add2(&Dead_camera_pos, &Original_vec_to_deader, 25.0f * flFrametime);
3414                                 Dead_player_last_vel = Player_obj->phys_info.vel;
3415                                 //nprintf(("AI", "Player death roll vel = %7.3f %7.3f %7.3f\n", Player_obj->phys_info.vel.x, Player_obj->phys_info.vel.y, Player_obj->phys_info.vel.z));
3416                         } else if (Player_ai->target_objnum != -1) {
3417                                 view_pos = Objects[Player_ai->target_objnum].pos;
3418                         } else {
3419                                 //      Make camera follow explosion, but gradually slow down.
3420                                 vm_vec_scale_add2(&Player_obj->pos, &Dead_player_last_vel, flFrametime);
3421                                 view_pos = Player_obj->pos;
3422                                 vm_vec_scale(&Dead_player_last_vel, 0.99f);
3423                                 vm_vec_scale_add2(&Dead_camera_pos, &Original_vec_to_deader, min(25.0f, vm_vec_mag_quick(&Dead_player_last_vel)) * flFrametime);
3424                         }
3425
3426                         *eye_pos = Dead_camera_pos;
3427
3428                         vm_vec_normalized_dir(&eye_dir, &Player_obj->pos, eye_pos);
3429
3430                         vm_vector_2_matrix(eye_orient, &eye_dir, NULL, NULL);
3431                         Viewer_obj = NULL;
3432                 }
3433         } 
3434
3435         // if supernova shockwave
3436         if(supernova_camera_cut()){
3437                 // no viewer obj
3438                 Viewer_obj = NULL;
3439
3440                 // call it dead view
3441                 Viewer_mode |= VM_DEAD_VIEW;
3442
3443                 // set eye pos and orient
3444                 supernova_set_view(eye_pos, eye_orient);
3445         } else {        
3446                 //      If already blown up, these other modes can override.
3447                 if (!(Game_mode & (GM_DEAD | GM_DEAD_BLEW_UP))) {
3448                         Viewer_mode &= ~VM_DEAD_VIEW;
3449
3450                         Viewer_obj = Player_obj;
3451  
3452                         if (Viewer_mode & VM_OTHER_SHIP) {
3453                                 if (Player_ai->target_objnum != -1){
3454                                         Viewer_obj = &Objects[Player_ai->target_objnum];
3455                                         last_Viewer_objnum = Player_ai->target_objnum;
3456                                 } else {
3457                                         Viewer_mode &= ~VM_OTHER_SHIP;
3458                                         last_Viewer_objnum = -1;
3459                                 }
3460                         } else {
3461                                 last_Viewer_objnum = -1;
3462                         }
3463
3464                         if (Viewer_mode & VM_EXTERNAL) {
3465                                 matrix  tm, tm2;
3466
3467                                 vm_angles_2_matrix(&tm2, &Viewer_external_info.angles);
3468                                 vm_matrix_x_matrix(&tm, &Viewer_obj->orient, &tm2);
3469
3470                                 vm_vec_scale_add(eye_pos, &Viewer_obj->pos, &tm.fvec, 2.0f * Viewer_obj->radius + Viewer_external_info.distance);
3471
3472                                 vm_vec_sub(&eye_dir, &Viewer_obj->pos, eye_pos);
3473                                 vm_vec_normalize(&eye_dir);
3474                                 vm_vector_2_matrix(eye_orient, &eye_dir, &Viewer_obj->orient.uvec, NULL);
3475                                 Viewer_obj = NULL;
3476
3477                                 //      Modify the orientation based on head orientation.
3478                                 compute_slew_matrix(eye_orient, &Viewer_slew_angles);
3479
3480                         } else if ( Viewer_mode & VM_CHASE ) {
3481                                 vector  move_dir;
3482
3483                                 if ( Viewer_obj->phys_info.speed < 0.1 )
3484                                         move_dir = Viewer_obj->orient.fvec;
3485                                 else {
3486                                         move_dir = Viewer_obj->phys_info.vel;
3487                                         vm_vec_normalize(&move_dir);
3488                                 }
3489
3490                                 vm_vec_scale_add(eye_pos, &Viewer_obj->pos, &move_dir, -3.0f * Viewer_obj->radius - Viewer_chase_info.distance);
3491                                 vm_vec_scale_add2(eye_pos, &Viewer_obj->orient.uvec, 0.75f * Viewer_obj->radius);
3492                                 vm_vec_sub(&eye_dir, &Viewer_obj->pos, eye_pos);
3493                                 vm_vec_normalize(&eye_dir);
3494
3495                                 // JAS: I added the following code because if you slew up using
3496                                 // Descent-style physics, eye_dir and Viewer_obj->orient.uvec are
3497                                 // equal, which causes a zero-length vector in the vm_vector_2_matrix
3498                                 // call because the up and the forward vector are the same.   I fixed
3499                                 // it by adding in a fraction of the right vector all the time to the
3500                                 // up vector.
3501                                 vector tmp_up = Viewer_obj->orient.uvec;
3502                                 vm_vec_scale_add2( &tmp_up, &Viewer_obj->orient.rvec, 0.00001f );
3503
3504                                 vm_vector_2_matrix(eye_orient, &eye_dir, &tmp_up, NULL);
3505                                 Viewer_obj = NULL;
3506
3507                                 //      Modify the orientation based on head orientation.
3508                                 compute_slew_matrix(eye_orient, &Viewer_slew_angles);
3509                         } else if ( Viewer_mode & VM_WARP_CHASE ) {
3510                                         *eye_pos = Camera_pos;
3511
3512                                         ship * shipp = &Ships[Player_obj->instance];
3513
3514                                         vm_vec_sub(&eye_dir, &shipp->warp_effect_pos, eye_pos);
3515                                         vm_vec_normalize(&eye_dir);
3516                                         vm_vector_2_matrix(eye_orient, &eye_dir, &Player_obj->orient.uvec, NULL);
3517                                         Viewer_obj = NULL;
3518                         } else {
3519                                 // get an eye position based upon the correct type of object
3520                                 switch(Viewer_obj->type){
3521                                 case OBJ_SHIP:
3522                                         // make a call to get the eye point for the player object
3523                                         ship_get_eye( eye_pos, eye_orient, Viewer_obj );
3524                                         break;
3525                                 case OBJ_OBSERVER:
3526                                         // make a call to get the eye point for the player object
3527                                         observer_get_eye( eye_pos, eye_orient, Viewer_obj );                            
3528                                         break;
3529                                 default :
3530                                         Int3();
3531                                 }
3532
3533                                 #ifdef JOHNS_DEBUG_CODE
3534                                 john_debug_stuff(&eye_pos, &eye_orient);
3535                                 #endif
3536                         }
3537                 }
3538         }
3539
3540         apply_hud_shake(eye_orient);
3541
3542         // setup neb2 rendering
3543         neb2_render_setup(eye_pos, eye_orient);
3544 }
3545
3546 #ifndef NDEBUG
3547 extern void ai_debug_render_stuff();
3548 #endif
3549
3550 int Game_subspace_effect = 0;
3551 DCF_BOOL( subspace, Game_subspace_effect );
3552
3553 // Does everything needed to render a frame
3554 void game_render_frame( vector * eye_pos, matrix * eye_orient )
3555 {
3556         int dont_offset;
3557
3558         g3_start_frame(game_zbuffer);
3559         g3_set_view_matrix( eye_pos, eye_orient, Viewer_zoom );
3560
3561         // maybe offset the HUD (jitter stuff)
3562         dont_offset = ((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER));
3563         HUD_set_offsets(Viewer_obj, !dont_offset);
3564         
3565         // for multiplayer clients, call code in Shield.cpp to set up the Shield_hit array.  Have to
3566         // do this becaues of the disjointed nature of this system (in terms of setup and execution).
3567         // must be done before ships are rendered
3568         if ( MULTIPLAYER_CLIENT ) {
3569                 shield_point_multi_setup();
3570         }
3571
3572         if ( Game_subspace_effect )     {
3573                 stars_draw(0,0,0,1);
3574         } else {
3575                 stars_draw(1,1,1,0);
3576         }
3577
3578         obj_render_all(obj_render);
3579         beam_render_all();                                              // render all beam weapons
3580         particle_render_all();                                  // render particles after everything else.
3581         trail_render_all();                                             // render missilie trails after everything else.        
3582         mflash_render_all();                                            // render all muzzle flashes    
3583
3584         //      Why do we not show the shield effect in these modes?  Seems ok.
3585         //if (!(Viewer_mode & (VM_EXTERNAL | VM_SLEWED | VM_CHASE | VM_DEAD_VIEW))) {
3586         render_shields();
3587         //}
3588
3589         // render nebula lightning
3590         nebl_render_all();
3591
3592         // render local player nebula
3593         neb2_render_player();   
3594
3595 #ifndef NDEBUG
3596         ai_debug_render_stuff();
3597 #endif
3598
3599 #ifndef RELEASE_REAL
3600         // game_framerate_check();
3601 #endif
3602
3603 #ifndef NDEBUG
3604         extern void snd_spew_debug_info();
3605         snd_spew_debug_info();
3606 #endif
3607
3608         //================ END OF 3D RENDERING STUFF ====================
3609
3610         hud_show_radar();
3611
3612         if( (Game_detail_flags & DETAIL_FLAG_HUD) && !(Game_mode & GM_MULTIPLAYER) || ( (Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_OBSERVER) ) ) {
3613                 hud_maybe_clear_head_area();
3614                 anim_render_all(0, flFrametime);
3615         }
3616
3617         extern int Multi_display_netinfo;
3618         if(Multi_display_netinfo){
3619                 extern void multi_display_netinfo();
3620                 multi_display_netinfo();
3621         }       
3622
3623         game_tst_frame_pre();
3624
3625 #ifndef NDEBUG
3626         do_timing_test(flFrametime);
3627 #endif
3628
3629 #ifndef NDEBUG
3630         extern int OO_update_index;     
3631         multi_rate_display(OO_update_index, 375, 0);
3632 #endif
3633
3634 #ifndef NDEBUG
3635         // test
3636         extern void oo_display();
3637         oo_display();                   
3638 #endif
3639         
3640         g3_end_frame();
3641 }
3642
3643 //#define JOHNS_DEBUG_CODE      1
3644
3645 #ifdef JOHNS_DEBUG_CODE
3646 void john_debug_stuff(vector *eye_pos, matrix *eye_orient)
3647 {
3648         //if ( keyd_pressed[KEY_LSHIFT] )               
3649         {
3650                 ship_subsys *tsys = Players[Player_num].targeted_subobject;
3651                 if ( tsys )     {
3652                         model_subsystem *turret = tsys->system_info;
3653
3654                         if (turret->type == SUBSYSTEM_TURRET )  {
3655                                 vector fvec, uvec;
3656                                 object * tobj = &Objects[Players[Player_num].targeted_subobject_parent];
3657
3658                                 ship_model_start(tobj);
3659
3660                                 model_find_world_point(eye_pos, &turret->turret_firing_point[0], turret->model_num, turret->turret_gun_sobj, &tobj->orient, &tobj->pos );
3661                                 model_find_world_dir(&fvec, &turret->turret_matrix.fvec, turret->model_num, turret->turret_gun_sobj, &tobj->orient, NULL );
3662                                 model_find_world_dir(&uvec, &turret->turret_matrix.uvec, turret->model_num, turret->turret_gun_sobj, &tobj->orient, NULL );
3663                                 
3664                                 vm_vector_2_matrix( eye_orient, &fvec, &uvec, NULL );
3665
3666                                 ship_model_stop(tobj);
3667
3668                                 Viewer_obj = NULL;
3669                         }
3670                 }
3671
3672         }
3673 }
3674 #endif
3675
3676 // following function for dumping frames for purposes of building trailers.
3677 #ifndef NDEBUG
3678
3679 // function to toggle state of dumping every frame into PCX when playing the game
3680 DCF(dump_frames, "Starts/stop frame dumping at 15 hz")
3681 {
3682         if ( Dc_command )       {
3683
3684                 if ( Debug_dump_frames == 0 )   {
3685                         // Turn it on
3686                         Debug_dump_frames = 15;
3687                         Debug_dump_trigger = 0;
3688                         gr_dump_frame_start( Debug_dump_frame_num, DUMP_BUFFER_NUM_FRAMES );
3689                         dc_printf( "Frame dumping at 15 hz is now ON\n" );
3690                 } else {
3691                         // Turn it off
3692                         Debug_dump_frames = 0;
3693                         Debug_dump_trigger = 0;
3694                         gr_dump_frame_stop();
3695                         dc_printf( "Frame dumping is now OFF\n" );
3696                 }
3697                 
3698         }
3699 }
3700
3701 DCF(dump_frames_trigger, "Starts/stop frame dumping at 15 hz")
3702 {
3703         if ( Dc_command )       {
3704
3705                 if ( Debug_dump_frames == 0 )   {
3706                         // Turn it on
3707                         Debug_dump_frames = 15;
3708                         Debug_dump_trigger = 1;
3709                         gr_dump_frame_start( Debug_dump_frame_num, DUMP_BUFFER_NUM_FRAMES );
3710                         dc_printf( "Frame dumping at 15 hz is now ON\n" );
3711                 } else {
3712                         // Turn it off
3713                         Debug_dump_frames = 0;
3714                         Debug_dump_trigger = 0;
3715                         gr_dump_frame_stop();
3716                         dc_printf( "Frame dumping is now OFF\n" );
3717                 }
3718                 
3719         }
3720 }
3721
3722 DCF(dump_frames30, "Starts/stop frame dumping at 30 hz")
3723 {
3724         if ( Dc_command )       {
3725
3726                 if ( Debug_dump_frames == 0 )   {
3727                         // Turn it on
3728                         Debug_dump_frames = 30;
3729                         Debug_dump_trigger = 0;
3730                         gr_dump_frame_start( Debug_dump_frame_num, DUMP_BUFFER_NUM_FRAMES );
3731                         dc_printf( "Frame dumping at 30 hz is now ON\n" );
3732                 } else {
3733                         // Turn it off
3734                         Debug_dump_frames = 0;
3735                         Debug_dump_trigger = 0;
3736                         gr_dump_frame_stop();
3737                         dc_printf( "Frame dumping is now OFF\n" );
3738                 }
3739                 
3740         }
3741 }
3742
3743 DCF(dump_frames30_trigger, "Starts/stop frame dumping at 30 hz")
3744 {
3745         if ( Dc_command )       {
3746
3747                 if ( Debug_dump_frames == 0 )   {
3748                         // Turn it on
3749                         Debug_dump_frames = 30;
3750                         Debug_dump_trigger = 1;
3751                         gr_dump_frame_start( Debug_dump_frame_num, DUMP_BUFFER_NUM_FRAMES );
3752                         dc_printf( "Triggered frame dumping at 30 hz is now ON\n" );
3753                 } else {
3754                         // Turn it off
3755                         Debug_dump_frames = 0;
3756                         Debug_dump_trigger = 0;
3757                         gr_dump_frame_stop();
3758                         dc_printf( "Triggered frame dumping is now OFF\n" );
3759                 }
3760                 
3761         }
3762 }
3763
3764 void game_maybe_dump_frame()
3765 {
3766         if ( !Debug_dump_frames ){
3767                 return;
3768         }
3769
3770         if( Debug_dump_trigger && !keyd_pressed[KEY_Q] ){
3771                 return;
3772         }
3773
3774         game_stop_time();
3775
3776         gr_dump_frame();
3777         Debug_dump_frame_num++;
3778
3779         game_start_time();
3780 }
3781 #endif
3782
3783 extern int Player_dead_state;
3784
3785 //      Flip the page and time how long it took.
3786 void game_flip_page_and_time_it()
3787 {       
3788         fix t1, t2,d;
3789         int t;
3790         t1 = timer_get_fixed_seconds();
3791         gr_flip();
3792         t2 = timer_get_fixed_seconds();
3793         d = t2 - t1;
3794         t = (gr_screen.max_w*gr_screen.max_h*gr_screen.bytes_per_pixel)/1024;
3795         sprintf( transfer_text, NOX("%ld MB/s"), fixmuldiv(t,65,d) );
3796 }
3797
3798 void game_simulation_frame()
3799 {
3800         // blow ships up in multiplayer dogfight
3801         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT) && (f2fl(Missiontime) >= 2.0f) && !dogfight_blown){
3802                 // blow up all non-player ships
3803                 ship_obj *moveup = GET_FIRST(&Ship_obj_list);
3804                 ship *shipp;
3805                 ship_info *sip;
3806                 while((moveup != END_OF_LIST(&Ship_obj_list)) && (moveup != NULL)){
3807                         // bogus
3808                         if((moveup->objnum < 0) || (moveup->objnum >= MAX_OBJECTS) || (Objects[moveup->objnum].type != OBJ_SHIP) || (Objects[moveup->objnum].instance < 0) || (Objects[moveup->objnum].instance >= MAX_SHIPS) || (Ships[Objects[moveup->objnum].instance].ship_info_index < 0) || (Ships[Objects[moveup->objnum].instance].ship_info_index >= Num_ship_types)){
3809                                 moveup = GET_NEXT(moveup);
3810                                 continue;
3811                         }
3812                         shipp = &Ships[Objects[moveup->objnum].instance];
3813                         sip = &Ship_info[shipp->ship_info_index];
3814
3815                         // only blow up small ships                     
3816                         if((sip->flags & SIF_SMALL_SHIP) && (multi_find_player_by_object(&Objects[moveup->objnum]) < 0) ){                                                      
3817                                 // function to simply explode a ship where it is currently at
3818                                 ship_self_destruct( &Objects[moveup->objnum] );                                 
3819                         }
3820
3821                         moveup = GET_NEXT(moveup);
3822                 }
3823
3824                 dogfight_blown = 1;
3825         }
3826
3827         // process AWACS stuff - do this first thing
3828         awacs_process();
3829
3830         // single player, set Player hits_this_frame to 0
3831         if ( !(Game_mode & GM_MULTIPLAYER) && Player ) {
3832                 Player->damage_this_burst -= (flFrametime * MAX_BURST_DAMAGE  / (0.001f * BURST_DURATION));
3833                 Player->damage_this_burst = max(Player->damage_this_burst, 0.0f);
3834         }
3835
3836         // supernova
3837         supernova_process();
3838         if(supernova_active() >= 5){
3839                 return;
3840         }
3841
3842         // fire targeting lasers now so that 
3843         // 1 - created this frame
3844         // 2 - collide this frame
3845         // 3 - render this frame
3846         // 4 - ignored and deleted next frame
3847         // the basic idea being that because it con be confusing to deal with them on a multi-frame basis, they are only valid for
3848         // frame
3849         ship_process_targeting_lasers();        
3850
3851         // do this here so that it works for multiplayer
3852         if ( Viewer_obj ) {
3853                 // get viewer direction
3854                 int viewer_direction = PHYSICS_VIEWER_REAR;
3855
3856                 if(Viewer_mode == 0){
3857                         viewer_direction = PHYSICS_VIEWER_FRONT;
3858                 }
3859                 if(Viewer_mode & VM_PADLOCK_UP){
3860                         viewer_direction = PHYSICS_VIEWER_UP;
3861                 }
3862                 else if(Viewer_mode & VM_PADLOCK_REAR){
3863                         viewer_direction = PHYSICS_VIEWER_REAR;
3864                 } 
3865                 else if(Viewer_mode & VM_PADLOCK_LEFT){
3866                         viewer_direction = PHYSICS_VIEWER_LEFT;
3867                 } 
3868                 else if(Viewer_mode & VM_PADLOCK_RIGHT){
3869                         viewer_direction = PHYSICS_VIEWER_RIGHT;
3870                 }
3871
3872                 physics_set_viewer( &Viewer_obj->phys_info, viewer_direction );
3873         } else {
3874                 physics_set_viewer( NULL, PHYSICS_VIEWER_FRONT );
3875         }
3876
3877 #define VM_PADLOCK_UP                                   (1 << 7)
3878 #define VM_PADLOCK_REAR                         (1 << 8)
3879 #define VM_PADLOCK_LEFT                         (1 << 9)
3880 #define VM_PADLOCK_RIGHT                                (1 << 10)
3881                 
3882         // evaluate mission departures and arrivals before we process all objects.
3883         if ( !(Game_mode & GM_MULTIPLAYER) || ((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && !multi_endgame_ending()) ) {
3884
3885                 // we don't want to evaluate mission stuff when any ingame joiner in multiplayer is receiving
3886                 // ships/wing packets.
3887                 if ( !((Game_mode & GM_MULTIPLAYER) && (Netgame.flags & NG_FLAG_INGAME_JOINING_CRITICAL)) && !(Game_mode & GM_DEMO_PLAYBACK)){
3888                         mission_parse_eval_stuff();
3889                 }
3890
3891                 // if we're an observer, move ourselves seperately from the standard physics
3892                 if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
3893                         obj_observer_move(flFrametime);
3894                 }
3895                 
3896                 // move all the objects now
3897                 obj_move_all(flFrametime);
3898
3899                 // check for cargo reveal (this has an internal timestamp, so only runs every N ms)
3900                 // AL: 3-15-98: It was decided to not let AI ships inspect cargo
3901                 //      ship_check_cargo_all();
3902                 if(!(Game_mode & GM_DEMO_PLAYBACK)){
3903                         mission_eval_goals();
3904                 }
3905         }
3906
3907         // always check training objectives, even in multiplayer missions. we need to do this so that the directives gauge works properly on clients
3908         if(!(Game_mode & GM_DEMO_PLAYBACK)){
3909                 training_check_objectives();
3910         }
3911         
3912         // do all interpolation now
3913         if ( (Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER) && !multi_endgame_ending() && !(Netgame.flags & NG_FLAG_SERVER_LOST)) {
3914                 // client side processing of warping in effect stages
3915                 multi_do_client_warp(flFrametime);     
3916         
3917                 // client side movement of an observer
3918                 if((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)){
3919                         obj_observer_move(flFrametime);   
3920                 }
3921
3922                 // move all objects - does interpolation now as well
3923                 obj_move_all(flFrametime);
3924         }
3925
3926         // only process the message queue when the player is "in" the game
3927         if ( !Pre_player_entry ){
3928                 message_queue_process();                                // process any messages send to the player
3929         }
3930
3931         if(!(Game_mode & GM_DEMO_PLAYBACK)){
3932                 message_maybe_distort();                                // maybe distort incoming message if comms damaged
3933                 player_repair_frame(flFrametime);       //      AI objects get repaired in ai_process, called from move code...deal with player.
3934                 player_process_pending_praise();                // maybe send off a delayed praise message to the player
3935                 player_maybe_play_all_alone_msg();      // mabye tell the player he is all alone        
3936         }
3937
3938         if(!(Game_mode & GM_STANDALONE_SERVER)){                
3939                 // process some stuff every frame (before frame is rendered)
3940                 emp_process_local();
3941
3942                 hud_update_frame();                                             // update hud systems
3943
3944                 if (!physics_paused)    {
3945                         // Move particle system
3946                         particle_move_all(flFrametime); 
3947
3948                         // Move missile trails
3949                         trail_move_all(flFrametime);            
3950
3951                         // process muzzle flashes
3952                         mflash_process_all();
3953
3954                         // Flash the gun flashes
3955                         shipfx_flash_do_frame(flFrametime);                     
3956
3957                         shockwave_move_all(flFrametime);        // update all the shockwaves
3958                 }
3959
3960                 // subspace missile strikes
3961                 ssm_process();
3962
3963                 obj_snd_do_frame();                                             // update the object-linked persistant sounds
3964                 game_maybe_update_sound_environment();
3965                 snd_update_listener(&View_position, &Player_obj->phys_info.vel, &Player_obj->orient);
3966
3967 // AL: debug code used for testing ambient subspace sound (ie when enabling subspace through debug console)
3968 #ifndef NDEBUG
3969                 if ( Game_subspace_effect ) {
3970                         game_start_subspace_ambient_sound();
3971                 }
3972 #endif
3973         }               
3974 }
3975
3976 // Maybe render and process the dead-popup
3977 void game_maybe_do_dead_popup(float frametime)
3978 {
3979         if ( popupdead_is_active() ) {
3980                 int leave_popup=1;
3981                 int choice = popupdead_do_frame(frametime);
3982
3983                 if ( Game_mode & GM_NORMAL ) {
3984                         switch(choice) {
3985                         case 0:
3986                                 // CD CHECK                             
3987                                 if(game_do_cd_mission_check(Game_current_mission_filename)){
3988                                         gameseq_post_event(GS_EVENT_ENTER_GAME);
3989                                 } else {
3990                                         gameseq_post_event(GS_EVENT_MAIN_MENU);
3991                                 }                                       
3992                                 break;
3993
3994                         case 1:
3995                                 gameseq_post_event(GS_EVENT_END_GAME);
3996                                 break;
3997
3998                         case 2:
3999                                 // CD CHECK
4000                                 if(game_do_cd_mission_check(Game_current_mission_filename)){
4001                                         gameseq_post_event(GS_EVENT_START_GAME);                                        
4002                                 } else {
4003                                         gameseq_post_event(GS_EVENT_MAIN_MENU);
4004                                 }                                       
4005                                 break;
4006
4007                         // this should only happen during a red alert mission
4008                         case 3:                         
4009                                 // bogus?
4010                                 Assert(The_mission.red_alert);
4011                                 if(!The_mission.red_alert){
4012                                         // CD CHECK
4013                                         if(game_do_cd_mission_check(Game_current_mission_filename)){
4014                                                 gameseq_post_event(GS_EVENT_START_GAME);
4015                                         } else {
4016                                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
4017                                         }
4018                                         break;
4019                                 }
4020                                 
4021                                 // choose the previous mission
4022                                 mission_campaign_previous_mission();
4023                                 // CD CHECK
4024                                 if(game_do_cd_mission_check(Game_current_mission_filename)){
4025                                         gameseq_post_event(GS_EVENT_START_GAME);
4026                                 } else {
4027                                         gameseq_post_event(GS_EVENT_MAIN_MENU);
4028                                 }                               
4029                                 break;
4030
4031                         default:
4032                                 leave_popup=0;
4033                                 break;
4034                         }
4035                 } else {
4036                         switch( choice ) {
4037
4038                         case POPUPDEAD_DO_MAIN_HALL:
4039                                 multi_quit_game(PROMPT_NONE,-1);
4040                                 break;
4041
4042                         case POPUPDEAD_DO_RESPAWN:                              
4043                                 multi_respawn_normal();
4044                                 event_music_player_respawn();
4045                                 break;
4046
4047                         case POPUPDEAD_DO_OBSERVER:
4048                                 multi_respawn_observer();
4049                                 event_music_player_respawn_as_observer();
4050                                 break;
4051
4052                         default:
4053                                 leave_popup = 0;
4054                                 break;
4055                         }
4056                 }
4057
4058                 if ( leave_popup ) {
4059                         popupdead_close();
4060                 }
4061         }
4062 }
4063
4064 // returns true if player is actually in a game_play stats
4065 int game_actually_playing()
4066 {
4067         int state;
4068
4069         state = gameseq_get_state();
4070         if ( (state != GS_STATE_GAME_PLAY) && (state != GS_STATE_DEATH_DIED) && (state != GS_STATE_DEATH_BLEW_UP) )
4071                 return 0;
4072         else
4073                 return 1;
4074 }
4075
4076 // Draw the 2D HUD gauges
4077 void game_render_hud_2d()
4078 {
4079         if ( !(Game_detail_flags & DETAIL_FLAG_HUD) ) {
4080                 return;
4081         }
4082         
4083         HUD_render_2d(flFrametime);
4084         gr_reset_clip();
4085 }
4086
4087 // Draw the 3D-dependant HUD gauges
4088 void game_render_hud_3d(vector *eye_pos, matrix *eye_orient)
4089 {
4090         g3_start_frame(0);              // 0 = turn zbuffering off
4091         g3_set_view_matrix( eye_pos, eye_orient, Viewer_zoom );
4092
4093         if ( (Game_detail_flags & DETAIL_FLAG_HUD) && (supernova_active() < 3)/* && !(Game_mode & GM_MULTIPLAYER) || ( (Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_OBSERVER) )*/ ) {
4094                 HUD_render_3d(flFrametime);
4095         }
4096
4097         // Do the sunspot
4098         game_sunspot_process(flFrametime);
4099
4100         // Diminish the palette effect
4101         game_flash_diminish(flFrametime);
4102
4103         g3_end_frame();
4104 }
4105
4106
4107 void game_frame()
4108 {
4109         int actually_playing;
4110         fix total_time1, total_time2;
4111         fix render2_time1=0, render2_time2=0;
4112         fix render3_time1=0, render3_time2=0;
4113         fix flip_time1=0, flip_time2=0;
4114         fix clear_time1=0, clear_time2=0;
4115         
4116         vector eye_pos;
4117         matrix eye_orient;
4118
4119 #ifndef NDEBUG
4120         if (Framerate_delay) {
4121                 int     start_time = timer_get_milliseconds();
4122                 while (timer_get_milliseconds() < start_time + Framerate_delay)
4123                         ;
4124         }
4125 #endif
4126
4127 #ifdef DEMO_SYSTEM
4128         demo_do_frame_start();
4129         if(Demo_error){
4130                 mprintf(("Error (%d) while processing demo!\n", Demo_error));
4131                 demo_close();
4132         }
4133 #endif
4134         
4135         // start timing frame
4136         timing_frame_start();
4137
4138         total_time1 = timer_get_fixed_seconds();
4139
4140         // var to hold which state we are in
4141         actually_playing = game_actually_playing();
4142         
4143         if ((!(Game_mode & GM_MULTIPLAYER)) || ((Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_OBSERVER))) {
4144                 if (!(Game_mode & GM_STANDALONE_SERVER)){
4145                         Assert( OBJ_INDEX(Player_obj) >= 0 );
4146                 }
4147         }
4148
4149         if (Missiontime > Entry_delay_time){
4150                 Pre_player_entry = 0;
4151         } else {
4152                 ; //nprintf(("AI", "Framecount = %i, time = %7.3f\n", Framecount, f2fl(Missiontime)));
4153         }
4154
4155         //      Note: These are done even before the player enters, else buffers can overflow.
4156         if (! (Game_mode & GM_STANDALONE_SERVER)){
4157                 radar_frame_init();
4158         }
4159
4160         shield_frame_init();
4161
4162         if ( Player->control_mode != PCM_NORMAL )
4163                 camera_move();
4164
4165         if ( !Pre_player_entry && actually_playing ) {                          
4166                 if (! (Game_mode & GM_STANDALONE_SERVER) ) {
4167
4168                         if( (!popup_running_state()) && (!popupdead_is_active()) ){
4169                                 game_process_keys();
4170
4171                                 // don't read flying controls if we're playing a demo back
4172                                 if(!(Game_mode & GM_DEMO_PLAYBACK)){
4173                                         read_player_controls( Player_obj, flFrametime);
4174                                 }
4175                         }
4176                         
4177                         // if we're not the master, we may have to send the server-critical ship status button_info bits
4178                         if ((Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER) && !(Net_player->flags & NETINFO_FLAG_OBSERVER)){
4179                                 multi_maybe_send_ship_status();
4180                         }
4181                 }
4182         }
4183
4184         // Reset the whack stuff
4185         game_whack_reset();
4186
4187         // These two lines must be outside of Pre_player_entry code,
4188         // otherwise too many lights are added.
4189         light_reset();
4190
4191         if ((Game_mode & GM_MULTIPLAYER) && (Netgame.game_state == NETGAME_STATE_SERVER_TRANSFER)){
4192                 return;
4193         }
4194         
4195         game_simulation_frame();        
4196
4197         // if not actually in a game play state, then return.  This condition could only be true in 
4198         // a multiplayer game.
4199         if ( !actually_playing ) {
4200                 Assert( Game_mode & GM_MULTIPLAYER );
4201                 return;
4202         }
4203
4204         if (!Pre_player_entry) {
4205                 if (! (Game_mode & GM_STANDALONE_SERVER)) {
4206                         clear_time1 = timer_get_fixed_seconds();
4207                         // clear the screen to black
4208                         gr_reset_clip();
4209                         if ( (Game_detail_flags & DETAIL_FLAG_CLEAR) ) {
4210                                 gr_clear();
4211                         }
4212
4213                         clear_time2 = timer_get_fixed_seconds();
4214                         render3_time1 = timer_get_fixed_seconds();
4215                         game_render_frame_setup(&eye_pos, &eye_orient);
4216                         game_render_frame( &eye_pos, &eye_orient );
4217
4218                         // save the eye position and orientation
4219                         if ( Game_mode & GM_MULTIPLAYER ) {
4220                                 Net_player->s_info.eye_pos = eye_pos;
4221                                 Net_player->s_info.eye_orient = eye_orient;
4222                         }
4223
4224                         hud_show_target_model();
4225
4226                         // check to see if we should display the death died popup
4227                         if(Game_mode & GM_DEAD_BLEW_UP){                                
4228                                 if(Game_mode & GM_MULTIPLAYER){
4229                                         // catch the situation where we're supposed to be warping out on this transition
4230                                         if(Net_player->flags & NETINFO_FLAG_WARPING_OUT){
4231                                                 gameseq_post_event(GS_EVENT_DEBRIEF);
4232                                         } else if((Player_died_popup_wait != -1) && (timestamp_elapsed(Player_died_popup_wait))){
4233                                                 Player_died_popup_wait = -1;
4234                                                 popupdead_start();
4235                                         }
4236                                 } else {
4237                                         if((Player_died_popup_wait != -1) && (timestamp_elapsed(Player_died_popup_wait))){
4238                                                 Player_died_popup_wait = -1;
4239                                                 popupdead_start();
4240                                         }
4241                                 }
4242                         }
4243
4244                         // hack - sometimes this seems to slip by in multiplayer. this should guarantee that we catch it
4245                         if((Game_mode & GM_MULTIPLAYER) && (Player_multi_died_check != -1) && (Game_mode & GM_DEAD_BLEW_UP) ){
4246                                 if(fl_abs(time(NULL) - Player_multi_died_check) > 4){
4247                                         if(!popupdead_is_active()){
4248                                                 popupdead_start();
4249                                         }
4250
4251                                         Player_multi_died_check = -1;
4252                                 }
4253                         }
4254         
4255                         render3_time2 = timer_get_fixed_seconds();
4256                         render2_time1 = timer_get_fixed_seconds();
4257
4258                         gr_reset_clip();
4259                         game_get_framerate();
4260                         game_show_framerate();          
4261
4262                         game_show_time_left();
4263
4264                         // Draw the 2D HUD gauges
4265                         if(supernova_active() < 3){
4266                                 game_render_hud_2d();
4267                         }
4268
4269                         game_set_view_clip();
4270
4271                         // Draw 3D HUD gauges                   
4272                         game_render_hud_3d(&eye_pos, &eye_orient);                                                                      
4273
4274                         game_tst_frame();
4275
4276                         render2_time2 = timer_get_fixed_seconds();
4277
4278                         // maybe render and process the dead popup
4279                         game_maybe_do_dead_popup(flFrametime);
4280
4281                         // start timing frame
4282                         timing_frame_stop();
4283                         // timing_display(30, 10);                      
4284
4285                         // If a regular popup is active, don't flip (popup code flips)
4286                         if( !popup_running_state() ){
4287                                 flip_time1 = timer_get_fixed_seconds();
4288                                 game_flip_page_and_time_it();
4289                                 flip_time2 = timer_get_fixed_seconds();
4290                         }
4291
4292 #ifndef NDEBUG
4293                         game_maybe_dump_frame();                        // used to dump pcx files for building trailers
4294 #endif          
4295                 } else {
4296                         game_show_standalone_framerate();
4297                 }
4298         }
4299
4300         game_do_training_checks();
4301         asteroid_frame();
4302
4303         // process lightning (nebula only)
4304         nebl_process();
4305
4306         total_time2 = timer_get_fixed_seconds();
4307
4308         // Got some timing numbers
4309         Timing_total = f2fl( total_time2 - total_time1 ) * 1000.0f;
4310         Timing_clear = f2fl( clear_time2 - clear_time1 ) * 1000.0f;
4311         Timing_render2 = f2fl( render2_time2- render2_time1 ) * 1000.0f;
4312         Timing_render3 = f2fl( render3_time2- render3_time1 ) * 1000.0f;
4313         Timing_flip = f2fl( flip_time2 - flip_time1 ) * 1000.0f;
4314
4315 #ifdef DEMO_SYSTEM
4316         demo_do_frame_end();
4317         if(Demo_error){
4318                 mprintf(("Error (%d) while processing demo!\n", Demo_error));
4319                 demo_close();
4320         }
4321 #endif
4322 }
4323
4324 #define MAX_FRAMETIME   (F1_0/4)                // Frametime gets saturated at this.  Changed by MK on 11/1/97.
4325                                                                                                 //      Some bug was causing Frametime to always get saturated at 2.0 seconds after the player
4326                                                                                                 //      died.  This resulted in screwed up death sequences.
4327
4328 fix Last_time = 0;                                              // The absolute time of game at end of last frame (beginning of this frame)
4329 fix Last_delta_time = 0;                                // While game is paused, this keeps track of how much elapsed in the frame before paused.
4330 static int timer_paused=0;
4331 #if defined(TIMER_TEST) && !defined(NDEBUG)
4332 static int stop_count,start_count;
4333 static int time_stopped,time_started;
4334 #endif
4335 int saved_timestamp_ticker = -1;
4336
4337 void game_reset_time()
4338 {
4339         if((Game_mode & GM_MULTIPLAYER) && (Netgame.game_state == NETGAME_STATE_SERVER_TRANSFER)){
4340                 return ;
4341         }
4342         
4343         //      Last_time = timer_get_fixed_seconds();
4344         game_start_time();
4345         timestamp_reset();
4346         game_stop_time();
4347 }
4348
4349 void game_stop_time()
4350 {
4351         if (timer_paused==0) {
4352                 fix time;
4353                 time = timer_get_fixed_seconds();
4354                 // Save how much time progressed so far in the frame so we can
4355                 // use it when we unpause.
4356                 Last_delta_time = time - Last_time;             
4357
4358                 //mprintf(("Last_time in game_stop_time = %7.3f\n", f2fl(Last_delta_time)));
4359                 if (Last_delta_time < 0) {
4360                         #if defined(TIMER_TEST) && !defined(NDEBUG)
4361                         Int3();         //get Matt!!!!
4362                         #endif
4363                         Last_delta_time = 0;
4364                 }
4365                 #if defined(TIMER_TEST) && !defined(NDEBUG)
4366                 time_stopped = time;
4367                 #endif
4368
4369                 // Stop the timer_tick stuff...
4370                 // Normally, you should never access 'timestamp_ticker', consider this a low-level routine
4371                 saved_timestamp_ticker = timestamp_ticker;
4372         }
4373         timer_paused++;
4374
4375         #if defined(TIMER_TEST) && !defined(NDEBUG)
4376         stop_count++;
4377         #endif
4378 }
4379
4380 void game_start_time()
4381 {
4382         timer_paused--;
4383         Assert(timer_paused >= 0);
4384         if (timer_paused==0) {
4385                 fix time;
4386                 time = timer_get_fixed_seconds();
4387                 #if defined(TIMER_TEST) && !defined(NDEBUG)
4388                 if (Last_time < 0)
4389                         Int3();         //get Matt!!!!
4390                 }
4391                 #endif
4392                 // Take current time, and set it backwards to account for time  
4393                 // that the frame already executed, so that timer_get_fixed_seconds() - Last_time
4394                 // will be correct when it goes to calculate the frametime next
4395                 // frame.
4396                 Last_time = time - Last_delta_time;             
4397                 #if defined(TIMER_TEST) && !defined(NDEBUG)
4398                 time_started = time;
4399                 #endif
4400
4401                 // Restore the timer_tick stuff...
4402                 // Normally, you should never access 'timestamp_ticker', consider this a low-level routine
4403                 Assert( saved_timestamp_ticker > -1 );          // Called out of order, get JAS
4404                 timestamp_ticker = saved_timestamp_ticker;
4405                 saved_timestamp_ticker = -1;
4406         }
4407
4408         #if defined(TIMER_TEST) && !defined(NDEBUG)
4409         start_count++;
4410         #endif
4411 }
4412
4413
4414 void game_set_frametime(int state)
4415 {
4416         fix thistime;
4417         float frame_cap_diff;
4418
4419         thistime = timer_get_fixed_seconds();
4420
4421         if ( Last_time == 0 )   
4422                 Frametime = F1_0 / 30;
4423         else
4424                 Frametime = thistime - Last_time;
4425
4426 //      Frametime = F1_0 / 30;
4427
4428         fix     debug_frametime = Frametime;    //      Just used to display frametime.
4429
4430         //      If player hasn't entered mission yet, make frame take 1/4 second.
4431         if ((Pre_player_entry) && (state == GS_STATE_GAME_PLAY))
4432                 Frametime = F1_0/4;
4433 #ifndef NDEBUG
4434         else if ((Debug_dump_frames) && (state == GS_STATE_GAME_PLAY)) {                                // note link to above if!!!!!
4435         
4436                 fix frame_speed = F1_0 / Debug_dump_frames;
4437
4438                 if (Frametime > frame_speed ){
4439                         nprintf(("warning","slow frame: %x\n",Frametime));
4440                 } else {                        
4441                         do {
4442                                 thistime = timer_get_fixed_seconds();
4443                                 Frametime = thistime - Last_time;
4444                         } while (Frametime < frame_speed );                     
4445                 }
4446                 Frametime = frame_speed;
4447         }
4448 #endif
4449
4450         Assert( Framerate_cap > 0 );
4451
4452         // Cap the framerate so it doesn't get too high.
4453         {
4454                 fix cap;
4455
4456                 cap = F1_0/Framerate_cap;
4457                 if (Frametime < cap) {
4458                         thistime = cap - Frametime;
4459                         //mprintf(("Sleeping for %6.3f seconds.\n", f2fl(thistime)));
4460                         Sleep( DWORD(f2fl(thistime) * 1000.0f) );
4461                         Frametime = cap;
4462                         thistime = timer_get_fixed_seconds();
4463                 }
4464         }
4465
4466         if((Game_mode & GM_STANDALONE_SERVER) && 
4467                 (f2fl(Frametime) < ((float)1.0/(float)Multi_options_g.std_framecap))){
4468
4469                 frame_cap_diff = ((float)1.0/(float)Multi_options_g.std_framecap) - f2fl(Frametime);            
4470                 Sleep((DWORD)(frame_cap_diff*1000));                            
4471                 
4472                 thistime += fl2f((frame_cap_diff));             
4473
4474                 Frametime = thistime - Last_time;
4475    }
4476
4477         // If framerate is too low, cap it.
4478         if (Frametime > MAX_FRAMETIME)  {
4479 #ifndef NDEBUG
4480                 mprintf(("Frame %2i too long!!: frametime = %.3f (%.3f)\n", Framecount, f2fl(Frametime), f2fl(debug_frametime)));
4481 #else 
4482                 // to remove warnings in release build
4483                 debug_frametime = fl2f(flFrametime);
4484 #endif
4485                 Frametime = MAX_FRAMETIME;
4486         }
4487
4488         Frametime = fixmul(Frametime, Game_time_compression);
4489
4490         Last_time = thistime;
4491         //mprintf(("Frame %i, Last_time = %7.3f\n", Framecount, f2fl(Last_time)));
4492
4493         flFrametime = f2fl(Frametime);
4494         //if(!(Game_mode & GM_PLAYING_DEMO)){
4495         timestamp_inc(flFrametime);
4496
4497 /*      if ((Framecount > 0) && (Framecount < 10)) {
4498                 mprintf(("Frame %2i: frametime = %.3f (%.3f)\n", Framecount, f2fl(Frametime), f2fl(debug_frametime)));
4499         }
4500 */
4501 }
4502
4503 // This is called from game_do_frame(), and from navmap_do_frame() 
4504 void game_update_missiontime()
4505 {
4506         // TODO JAS: Put in if and move this into game_set_frametime, 
4507         // fix navmap to call game_stop/start_time
4508         //if ( !timer_paused )  
4509                 Missiontime += Frametime;
4510 }
4511
4512 void game_do_frame()
4513 {       
4514         game_set_frametime(GS_STATE_GAME_PLAY);
4515         game_update_missiontime();
4516
4517         if (Game_mode & GM_STANDALONE_SERVER) {
4518                 std_multi_set_standalone_missiontime(f2fl(Missiontime));
4519         }
4520
4521         if ( game_single_step && (last_single_step == game_single_step) ) {
4522                 os_set_title( NOX("SINGLE STEP MODE (Pause exits, any other key steps)") );
4523                 while( key_checkch() == 0 )
4524                         os_sleep(10);
4525                 os_set_title( XSTR( "FreeSpace", 171) );
4526                 Last_time = timer_get_fixed_seconds();
4527         }
4528
4529         last_single_step = game_single_step;
4530
4531         if ((gameseq_get_state() == GS_STATE_GAME_PLAY) && Use_mouse_to_fly){
4532                 Keep_mouse_centered = 1;  // force mouse to center of our window (so we don't hit movement limits)
4533         }
4534         game_frame();
4535
4536         Keep_mouse_centered = 0;
4537         monitor_update();                       // Update monitor variables
4538 }
4539
4540 void multi_maybe_do_frame()
4541 {
4542         if ( (Game_mode & GM_MULTIPLAYER) && (Game_mode & GM_IN_MISSION) && !Multi_pause_status){
4543                 game_do_frame(); 
4544         }
4545 }
4546
4547 int Joymouse_button_status = 0;
4548
4549 // Flush all input devices
4550 void game_flush()
4551 {
4552         key_flush();
4553         mouse_flush();
4554         joy_flush();
4555         snazzy_flush();
4556
4557         Joymouse_button_status = 0;
4558
4559         //mprintf(("Game flush!\n" ));
4560 }
4561
4562 // function for multiplayer only which calls game_do_state_common() when running the
4563 // debug console
4564 void game_do_dc_networking()
4565 {
4566         Assert( Game_mode & GM_MULTIPLAYER );
4567
4568         game_do_state_common( gameseq_get_state() );
4569 }
4570
4571 // Call this whenever in a loop, or when you need to check for a keystroke.
4572 int game_check_key()
4573 {
4574         int k;
4575
4576         k = game_poll();
4577
4578         // convert keypad enter to normal enter
4579         if ((k & KEY_MASK) == KEY_PADENTER)
4580                 k = (k & ~KEY_MASK) | KEY_ENTER;
4581
4582         return k;
4583 }
4584
4585 #ifdef FS2_DEMO
4586
4587 #define DEMO_TRAILER_TIMEOUT_MS         45000                   // 45 seconds of no input, play trailer
4588 static int Demo_show_trailer_timestamp = 0;
4589
4590 void demo_reset_trailer_timer()
4591 {
4592         Demo_show_trailer_timestamp = timer_get_milliseconds();
4593 }
4594
4595 void demo_maybe_show_trailer(int k)
4596 {
4597         /*
4598         // if key pressed, reset demo trailer timer
4599         if ( k > 0 ) {
4600                 demo_reset_trailer_timer();
4601                 return;
4602         }
4603
4604         // if mouse moved, reset demo trailer timer
4605         int dx = 0, dy = 0;
4606
4607         mouse_get_delta(&dx, &dy);
4608         if ( (dx > 0) || (dy > 0) ) {
4609                 demo_reset_trailer_timer();
4610                 return;
4611         }
4612
4613         // if joystick has moved, reset demo trailer timer
4614         dx = 0;
4615         dy = 0;
4616         joy_get_delta(&dx, &dy);
4617         if ( (dx > 0) || (dy > 0) ) {
4618                 demo_reset_trailer_timer();
4619                 return;
4620         }
4621
4622         // NOTE: reseting the trailer timer on mouse/joystick presses is handled in
4623         //       the low-level code.  Ugly, I know... but was the simplest and most
4624         //       robust solution.
4625                 
4626         // if 30 seconds since last demo trailer time reset, launch movie
4627         if ( os_foreground() ) {
4628                 int now = timer_get_milliseconds();
4629                 if ( (now - Demo_show_trailer_timestamp) > DEMO_TRAILER_TIMEOUT_MS ) {
4630 //              if ( (now - Demo_show_trailer_timestamp) > 10000 ) {
4631                         // play movie here
4632                         movie_play( NOX("fstrailer2.mve") );
4633                         demo_reset_trailer_timer();
4634                 }
4635         }
4636         */
4637 }
4638
4639 #endif
4640
4641 // same as game_check_key(), except this is used while actually in the game.  Since there
4642 // generally are differences between game control keys and general UI keys, makes sense to
4643 // have seperate functions for each case.  If you are not checking a game control while in a
4644 // mission, you should probably be using game_check_key() instead.
4645 int game_poll()
4646 {
4647         int k, state;
4648
4649         if (!os_foreground()) {         
4650                 game_stop_time();
4651                 os_sleep(100);
4652                 game_start_time();
4653
4654                 // If we're in a single player game, pause it.
4655                 if (!(Game_mode & GM_MULTIPLAYER)){
4656                         if ( (gameseq_get_state() == GS_STATE_GAME_PLAY) && (!popup_active()) && (!popupdead_is_active()) )     {
4657                                 game_process_pause_key();
4658                         }
4659                 }
4660         }
4661
4662    k = key_inkey();
4663
4664 #ifdef FS2_DEMO
4665         demo_maybe_show_trailer(k);
4666 #endif
4667
4668         // Move the mouse cursor with the joystick.
4669         if (os_foreground() && (!Mouse_hidden) && (Use_joy_mouse) )     {
4670                 // Move the mouse cursor with the joystick
4671                 int mx, my, dx, dy;
4672                 int jx, jy, jz, jr;
4673
4674                 joy_get_pos( &jx, &jy, &jz, &jr );
4675
4676                 dx = fl2i(f2fl(jx)*flFrametime*500.0f);
4677                 dy = fl2i(f2fl(jy)*flFrametime*500.0f);
4678
4679                 if ( dx || dy ) {
4680                         mouse_get_real_pos( &mx, &my );
4681                         mouse_set_pos( mx+dx, my+dy );
4682                 }
4683
4684                 int j, m;
4685                 j = joy_down(0);
4686                 m = mouse_down(MOUSE_LEFT_BUTTON);
4687
4688                 if ( j != Joymouse_button_status )      {
4689                         //mprintf(( "Joy went from %d to %d, mouse is %d\n", Joymouse_button_status, j, m ));
4690                         Joymouse_button_status = j;
4691                         if ( j && (!m) )        {
4692                                 mouse_mark_button( MOUSE_LEFT_BUTTON, 1 );
4693                         } else if ( (!j) && (m) )       {
4694                                 mouse_mark_button( MOUSE_LEFT_BUTTON, 0 );
4695                         }
4696                 }
4697         }
4698
4699         // if we should be ignoring keys because of some multiplayer situations
4700         if((Game_mode & GM_MULTIPLAYER) && multi_ignore_controls(k)){
4701                 return 0;
4702         }
4703
4704         // If a popup is running, don't process all the Fn keys
4705         if( popup_active() ) {
4706                 return k;
4707         }
4708
4709         state = gameseq_get_state();
4710
4711 //      if ( k ) nprintf(( "General", "Key = %x\n", k ));
4712
4713         switch (k) {
4714                 case KEY_DEBUGGED + KEY_BACKSP:
4715                         Int3();
4716                         break;
4717
4718                 case KEY_F1:
4719                         launch_context_help();
4720                         k = 0;
4721                         break;
4722
4723                 case KEY_F2:
4724 //                      if (state != GS_STATE_INITIAL_PLAYER_SELECT) {
4725
4726                         // don't allow f2 while warping out in multiplayer      
4727                         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_WARPING_OUT)){
4728                                 break;
4729                         }
4730
4731                         switch (state) {
4732                                 case GS_STATE_INITIAL_PLAYER_SELECT:
4733                                 case GS_STATE_OPTIONS_MENU:
4734                                 case GS_STATE_HUD_CONFIG:
4735                                 case GS_STATE_CONTROL_CONFIG:
4736                                 case GS_STATE_DEATH_DIED:
4737                                 case GS_STATE_DEATH_BLEW_UP:            
4738                                 case GS_STATE_VIEW_MEDALS:
4739                                         break;
4740
4741                                 default:
4742                                         gameseq_post_event(GS_EVENT_OPTIONS_MENU);
4743                                         k = 0;
4744                                         break;
4745                         }
4746
4747                         break;
4748
4749                         // hotkey selection screen -- only valid from briefing and beyond.
4750                 case KEY_F3:    
4751                         #ifndef FS2_DEMO
4752                                 if ( (state == GS_STATE_TEAM_SELECT) || (state == GS_STATE_BRIEFING) || (state == GS_STATE_SHIP_SELECT) || (state == GS_STATE_WEAPON_SELECT) || (state == GS_STATE_GAME_PLAY) || (state == GS_STATE_GAME_PAUSED) ) {
4753                                         gameseq_post_event( GS_EVENT_HOTKEY_SCREEN );
4754                                         k = 0;
4755                                 }
4756                         #endif
4757                         break;
4758
4759                 case KEY_DEBUGGED + KEY_F3:
4760                         gameseq_post_event( GS_EVENT_TOGGLE_FULLSCREEN );
4761                         break;
4762
4763                 case KEY_DEBUGGED + KEY_F4:
4764                         gameseq_post_event( GS_EVENT_TOGGLE_GLIDE );
4765                         break;
4766                 
4767                 case KEY_F4:
4768                         if(Game_mode & GM_MULTIPLAYER){
4769                                 if((state == GS_STATE_GAME_PLAY) || (state == GS_STATE_MULTI_PAUSED)){
4770                                         gameseq_post_event( GS_EVENT_MISSION_LOG_SCROLLBACK );
4771                                         k = 0;
4772                                 } 
4773                         } else {
4774                                 if ((state == GS_STATE_GAME_PLAY) || (state == GS_STATE_DEATH_DIED) || (state == GS_STATE_DEATH_BLEW_UP) || (state == GS_STATE_GAME_PAUSED) ) {
4775                                         gameseq_post_event( GS_EVENT_MISSION_LOG_SCROLLBACK );
4776                                         k = 0;
4777                                 }
4778                         }
4779                         break;
4780
4781                 case KEY_ESC | KEY_SHIFTED:
4782                         // make sure to quit properly out of multiplayer
4783                         if(Game_mode & GM_MULTIPLAYER){
4784                                 multi_quit_game(PROMPT_NONE);
4785                         }
4786
4787                         gameseq_post_event( GS_EVENT_QUIT_GAME );
4788                         k = 0;
4789
4790                         break;
4791
4792                 case KEY_DEBUGGED + KEY_P:                      
4793                         break;                  
4794
4795                 case KEY_PRINT_SCRN: 
4796                         {
4797                                 static int counter = 0;
4798                                 char tmp_name[127];
4799
4800                                 game_stop_time();
4801
4802                                 sprintf( tmp_name, NOX("screen%02d"), counter );
4803                                 counter++;
4804                                 mprintf(( "Dumping screen to '%s'\n", tmp_name ));
4805                                 gr_print_screen(tmp_name);
4806
4807                                 game_start_time();
4808                         }
4809
4810                         k = 0;
4811                         break;
4812
4813                 case KEY_SHIFTED | KEY_ENTER: {
4814
4815 #if !defined(NDEBUG)
4816
4817                         if ( Game_mode & GM_NORMAL ){
4818                                 game_stop_time();
4819                         }
4820
4821                         // if we're in multiplayer mode, do some special networking
4822                         if(Game_mode & GM_MULTIPLAYER){
4823                                 debug_console(game_do_dc_networking);
4824                         } else {                                
4825                                 debug_console();
4826                         }
4827
4828                         game_flush();
4829
4830                         if ( Game_mode & GM_NORMAL )
4831                                 game_start_time();
4832
4833 #endif
4834
4835                         break;
4836                 }
4837         }
4838
4839         return k;
4840 }
4841
4842 void os_close()
4843 {
4844         gameseq_post_event(GS_EVENT_QUIT_GAME);
4845 }
4846
4847 void apply_physics( float damping, float desired_vel, float initial_vel, float t, float * new_vel, float * delta_pos );
4848
4849
4850 void camera_set_position( vector *pos )
4851 {
4852         Camera_pos = *pos;
4853 }
4854
4855 void camera_set_orient( matrix *orient )
4856 {
4857         Camera_orient = *orient;
4858 }
4859
4860 void camera_set_velocity( vector *vel, int instantaneous )
4861 {
4862         Camera_desired_velocity.x = 0.0f;
4863         Camera_desired_velocity.y = 0.0f;
4864         Camera_desired_velocity.z = 0.0f;
4865
4866         vm_vec_scale_add2( &Camera_desired_velocity, &Camera_orient.rvec, vel->x );
4867         vm_vec_scale_add2( &Camera_desired_velocity, &Camera_orient.uvec, vel->y );
4868         vm_vec_scale_add2( &Camera_desired_velocity, &Camera_orient.fvec, vel->z );
4869
4870         if ( instantaneous )    {
4871                 Camera_velocity = Camera_desired_velocity;
4872         }
4873
4874 }
4875
4876 //
4877 void camera_move()
4878 {
4879         vector new_vel, delta_pos;
4880
4881         apply_physics( Camera_damping, Camera_desired_velocity.x, Camera_velocity.x, flFrametime, &new_vel.x, &delta_pos.x );
4882         apply_physics( Camera_damping, Camera_desired_velocity.y, Camera_velocity.y, flFrametime, &new_vel.y, &delta_pos.y );
4883         apply_physics( Camera_damping, Camera_desired_velocity.z, Camera_velocity.z, flFrametime, &new_vel.z, &delta_pos.z );
4884
4885         Camera_velocity = new_vel;
4886
4887 //      mprintf(( "Camera velocity = %.1f,%.1f, %.1f\n", Camera_velocity.x, Camera_velocity.y, Camera_velocity.z ));
4888
4889         vm_vec_add2( &Camera_pos, &delta_pos );
4890
4891         float ot = Camera_time+0.0f;
4892
4893         Camera_time += flFrametime;
4894
4895         if ( (ot < 0.667f) && ( Camera_time >= 0.667f ) )       {
4896                 vector tmp;
4897                 
4898                 tmp.z = 4.739f;         // always go this fast forward.
4899
4900                 // pick x and y velocities so they are always on a 
4901                 // circle with a 25 m radius.
4902
4903                 float tmp_angle = frand()*PI2;
4904         
4905                 tmp.x = 22.0f * (float)sin(tmp_angle);
4906                 tmp.y = -22.0f * (float)cos(tmp_angle);
4907
4908                 //mprintf(( "Angle = %.1f, vx=%.1f, vy=%.1f\n", tmp_angle, tmp.x, tmp.y ));
4909
4910                 //mprintf(( "Changing velocity!\n" ));
4911                 camera_set_velocity( &tmp, 0 );
4912         }
4913
4914         if ( (ot < 3.0f ) && ( Camera_time >= 3.0f ) )  {
4915                 vector tmp = { 0.0f, 0.0f, 0.0f };
4916                 camera_set_velocity( &tmp, 0 );
4917         }
4918         
4919 }
4920
4921 void end_demo_campaign_do()
4922 {
4923 #if defined(FS2_DEMO)
4924         // show upsell screens
4925         demo_upsell_show_screens();
4926 #elif defined(OEM_BUILD)
4927         // show oem upsell screens
4928         oem_upsell_show_screens();
4929 #endif
4930
4931         // drop into main hall
4932         gameseq_post_event( GS_EVENT_MAIN_MENU );
4933 }
4934
4935 // All code to process events.   This is the only place
4936 // that you should change the state of the game.
4937 void game_process_event( int current_state, int event )
4938 {
4939         mprintf(("Got event %s in state %s\n", GS_event_text[event], GS_state_text[current_state]));
4940
4941         switch (event) {
4942                 case GS_EVENT_SIMULATOR_ROOM:
4943                         gameseq_set_state(GS_STATE_SIMULATOR_ROOM);
4944                         break;
4945
4946                 case GS_EVENT_MAIN_MENU:
4947                         gameseq_set_state(GS_STATE_MAIN_MENU);          
4948                         break;
4949
4950                 case GS_EVENT_OPTIONS_MENU:
4951                         gameseq_push_state( GS_STATE_OPTIONS_MENU );
4952                         break;
4953
4954                 case GS_EVENT_BARRACKS_MENU:
4955                         gameseq_set_state(GS_STATE_BARRACKS_MENU);              
4956                         break;
4957
4958                 case GS_EVENT_TECH_MENU:
4959                         gameseq_set_state(GS_STATE_TECH_MENU);          
4960                         break;
4961
4962                 case GS_EVENT_TRAINING_MENU:
4963                         gameseq_set_state(GS_STATE_TRAINING_MENU);              
4964                         break;
4965
4966                 case GS_EVENT_START_GAME:
4967                         Select_default_ship = 0;                        
4968                         Player_multi_died_check = -1;
4969                         gameseq_set_state(GS_STATE_CMD_BRIEF);
4970                         break;
4971
4972                 case GS_EVENT_START_BRIEFING:
4973                         gameseq_set_state(GS_STATE_BRIEFING);           
4974                         break;
4975
4976                 case GS_EVENT_DEBRIEF:
4977                         // did we end the campaign in the main freespace 2 single player campaign?
4978                         if(Campaign_ended_in_mission && (Game_mode & GM_CAMPAIGN_MODE) && !stricmp(Campaign.filename, "freespace2")) {
4979                                 gameseq_post_event(GS_EVENT_END_CAMPAIGN);
4980                         } else {
4981                                 gameseq_set_state(GS_STATE_DEBRIEF);            
4982                         }
4983
4984                         Player_multi_died_check = -1;
4985                         break;
4986
4987                 case GS_EVENT_SHIP_SELECTION:
4988                         gameseq_set_state( GS_STATE_SHIP_SELECT );
4989                         break;
4990
4991                 case GS_EVENT_WEAPON_SELECTION:
4992                         gameseq_set_state( GS_STATE_WEAPON_SELECT );
4993                         break;
4994
4995                 case GS_EVENT_ENTER_GAME:               
4996 #ifdef DEMO_SYSTEM
4997                         // maybe start recording a demo
4998                         if(Demo_make){
4999                                 demo_start_record("test.fsd");
5000                         }
5001 #endif
5002
5003                         if (Game_mode & GM_MULTIPLAYER) {
5004                                 // if we're respawning, make sure we change the view mode so that the hud shows up
5005                                 if (current_state == GS_STATE_DEATH_BLEW_UP) {
5006                                         Viewer_mode = 0;
5007                                 }
5008
5009                                 gameseq_set_state(GS_STATE_GAME_PLAY);
5010                         } else {
5011                                 gameseq_set_state(GS_STATE_GAME_PLAY, 1);
5012                         }
5013
5014                         Player_multi_died_check = -1;
5015
5016                         // clear multiplayer button info                        
5017                         extern button_info Multi_ship_status_bi;
5018                         memset(&Multi_ship_status_bi, 0, sizeof(button_info));
5019
5020                         Start_time = f2fl(timer_get_approx_seconds());
5021                         //Framecount = 0;
5022                         mprintf(("Entering game at time = %7.3f\n", Start_time));
5023                         break;
5024
5025
5026                 case GS_EVENT_START_GAME_QUICK:
5027                         Select_default_ship = 1;
5028                         gameseq_post_event(GS_EVENT_ENTER_GAME);
5029                         break;
5030
5031
5032                 case GS_EVENT_END_GAME:
5033                         if ( (current_state == GS_STATE_GAME_PLAY) || (current_state == GS_STATE_DEATH_DIED) ||
5034                                 (current_state == GS_STATE_DEATH_BLEW_UP) ||    (current_state == GS_STATE_DEBRIEF) || (current_state == GS_STATE_MULTI_DOGFIGHT_DEBRIEF)) {
5035                                         gameseq_set_state(GS_STATE_MAIN_MENU);
5036
5037                         } else
5038                                 Int3();
5039
5040                         Player_multi_died_check = -1;
5041                         break;
5042
5043                 case GS_EVENT_QUIT_GAME:
5044                         main_hall_stop_music();
5045                         main_hall_stop_ambient();
5046                         gameseq_set_state(GS_STATE_QUIT_GAME);
5047
5048                         Player_multi_died_check = -1;
5049                         break;
5050
5051                 case GS_EVENT_GAMEPLAY_HELP:
5052                         gameseq_push_state( GS_STATE_GAMEPLAY_HELP );
5053                         break;
5054
5055                 case GS_EVENT_PAUSE_GAME:
5056                         gameseq_push_state(GS_STATE_GAME_PAUSED);
5057                         break;
5058
5059                 case GS_EVENT_DEBUG_PAUSE_GAME:
5060                         gameseq_push_state(GS_STATE_DEBUG_PAUSED);
5061                         break;
5062
5063                 case GS_EVENT_TRAINING_PAUSE:
5064                         gameseq_push_state(GS_STATE_TRAINING_PAUSED);
5065                         break;
5066
5067                 case GS_EVENT_PREVIOUS_STATE:
5068                         gameseq_pop_state();
5069                         break;
5070
5071                 case GS_EVENT_TOGGLE_FULLSCREEN:
5072                         #ifndef HARDWARE_ONLY
5073                                 #ifndef NDEBUG
5074                                 if ( gr_screen.mode == GR_SOFTWARE )    {
5075                                         gr_init( GR_640, GR_DIRECTDRAW );
5076                                 } else if ( gr_screen.mode == GR_DIRECTDRAW )   {
5077                                         gr_init( GR_640, GR_SOFTWARE );
5078                                 }
5079                                 #endif
5080                         #endif
5081                         break;
5082
5083                 case GS_EVENT_TOGGLE_GLIDE:
5084                         #ifndef NDEBUG
5085                         if ( gr_screen.mode != GR_GLIDE )       {
5086                                 gr_init( GR_640, GR_GLIDE );
5087                         } else {
5088                                 gr_init( GR_640, GR_SOFTWARE );
5089                         }
5090                         #endif
5091                         break;                                          
5092  
5093                 case GS_EVENT_LOAD_MISSION_MENU:
5094                         gameseq_set_state(GS_STATE_LOAD_MISSION_MENU);
5095                         break;
5096
5097                 case GS_EVENT_MISSION_LOG_SCROLLBACK:
5098                         gameseq_push_state( GS_STATE_MISSION_LOG_SCROLLBACK );
5099                         break;
5100
5101                 case GS_EVENT_HUD_CONFIG:
5102                         gameseq_push_state( GS_STATE_HUD_CONFIG );
5103                         break;
5104
5105                 case GS_EVENT_CONTROL_CONFIG:
5106                         gameseq_push_state( GS_STATE_CONTROL_CONFIG );
5107                         break;  
5108
5109                 case GS_EVENT_DEATH_DIED:
5110                         gameseq_set_state( GS_STATE_DEATH_DIED );
5111                         break;
5112
5113                 case GS_EVENT_DEATH_BLEW_UP:
5114                         if (  current_state == GS_STATE_DEATH_DIED )    {
5115                                 gameseq_set_state( GS_STATE_DEATH_BLEW_UP );
5116                                 event_music_player_death();
5117
5118                                 // multiplayer clients set their extra check here
5119                                 if(Game_mode & GM_MULTIPLAYER){
5120                                         // set the multi died absolute last chance check                                        
5121                                         Player_multi_died_check = time(NULL);
5122                                 }                                       
5123                         } else {
5124                                 mprintf(( "Ignoring GS_EVENT_DEATH_BLEW_UP because we're in state %d\n", current_state ));
5125                         }
5126                         break;
5127
5128                 case GS_EVENT_NEW_CAMPAIGN:
5129                         if (!mission_load_up_campaign()){
5130                                 readyroom_continue_campaign();
5131                         }
5132
5133                         Player_multi_died_check = -1;
5134                         break;
5135
5136                 case GS_EVENT_CAMPAIGN_CHEAT:
5137                         if (!mission_load_up_campaign()){
5138                                 /*
5139                                 // bash campaign value
5140                                 extern char Main_hall_campaign_cheat[512];
5141                                 int idx;
5142                                 
5143                                 // look for the mission
5144                                 for(idx=0; idx<Campaign.num_missions; idx++){
5145                                         if(!stricmp(Campaign.missions[idx].name, Main_hall_campaign_cheat)){
5146                                                 Campaign.next_mission = idx;
5147                                                 Campaign.prev_mission = idx - 1;
5148                                                 break;
5149                                         }
5150                                 }
5151                                 */
5152
5153                                 // continue
5154                                 readyroom_continue_campaign();
5155                         }
5156
5157                         Player_multi_died_check = -1;
5158                         break;
5159
5160                 case GS_EVENT_CAMPAIGN_ROOM:
5161                         gameseq_set_state(GS_STATE_CAMPAIGN_ROOM);
5162                         break;
5163
5164                 case GS_EVENT_CMD_BRIEF:
5165                         gameseq_set_state(GS_STATE_CMD_BRIEF);
5166                         break;
5167
5168                 case GS_EVENT_RED_ALERT:
5169                         gameseq_set_state(GS_STATE_RED_ALERT);
5170                         break;
5171
5172                 case GS_EVENT_CREDITS:
5173                         gameseq_set_state( GS_STATE_CREDITS );
5174                         break;
5175
5176                 case GS_EVENT_VIEW_MEDALS:
5177                         gameseq_push_state( GS_STATE_VIEW_MEDALS );
5178                         break;
5179
5180                 case GS_EVENT_SHOW_GOALS:
5181                         gameseq_push_state( GS_STATE_SHOW_GOALS );      // use push_state() since we might get to this screen through a variety of states
5182                         break;
5183
5184                 case GS_EVENT_HOTKEY_SCREEN:
5185                         gameseq_push_state( GS_STATE_HOTKEY_SCREEN );   // use push_state() since we might get to this screen through a variety of states
5186                         break;
5187                 
5188         // multiplayer stuff follow these comments
5189
5190                 case GS_EVENT_MULTI_JOIN_GAME:
5191                         gameseq_set_state( GS_STATE_MULTI_JOIN_GAME );
5192                         break;
5193
5194                 case GS_EVENT_MULTI_HOST_SETUP:
5195                         gameseq_set_state( GS_STATE_MULTI_HOST_SETUP );
5196                         break;
5197
5198                 case GS_EVENT_MULTI_CLIENT_SETUP:
5199                         gameseq_set_state( GS_STATE_MULTI_CLIENT_SETUP );
5200                         break;
5201
5202                 case GS_EVENT_GOTO_VIEW_CUTSCENES_SCREEN:
5203                         gameseq_set_state(GS_STATE_VIEW_CUTSCENES);
5204                         break;
5205
5206                 case GS_EVENT_MULTI_STD_WAIT:
5207                         gameseq_set_state( GS_STATE_MULTI_STD_WAIT );
5208                         break;
5209
5210                 case GS_EVENT_STANDALONE_MAIN:
5211                         gameseq_set_state( GS_STATE_STANDALONE_MAIN );
5212                         break;   
5213
5214                 case GS_EVENT_MULTI_PAUSE:
5215                         gameseq_push_state( GS_STATE_MULTI_PAUSED );
5216                         break;                  
5217
5218                 case GS_EVENT_INGAME_PRE_JOIN:
5219                         gameseq_set_state( GS_STATE_INGAME_PRE_JOIN );
5220                         break;
5221                 
5222                 case GS_EVENT_EVENT_DEBUG:
5223                         gameseq_push_state(GS_STATE_EVENT_DEBUG);
5224                         break;
5225
5226                 // Start a warpout where player automatically goes 70 no matter what
5227                 // and can't cancel out of it.
5228                 case GS_EVENT_PLAYER_WARPOUT_START_FORCED:
5229                         Warpout_forced = 1;                                                     // If non-zero, bash the player to speed and go through effect
5230
5231                         // Same code as in GS_EVENT_PLAYER_WARPOUT_START only ignores current mode
5232                         Player->saved_viewer_mode = Viewer_mode;
5233                         Player->control_mode = PCM_WARPOUT_STAGE1;
5234                         Warpout_sound = snd_play(&Snds[SND_PLAYER_WARP_OUT]);
5235                         Warpout_time = 0.0f;                    // Start timer!
5236                         break;
5237
5238                 case GS_EVENT_PLAYER_WARPOUT_START:
5239                         if ( Player->control_mode != PCM_NORMAL )       {
5240                                 mprintf(( "Player isn't in normal mode; cannot warp out.\n" ));
5241                         } else {
5242                                 Player->saved_viewer_mode = Viewer_mode;
5243                                 Player->control_mode = PCM_WARPOUT_STAGE1;
5244                                 Warpout_sound = snd_play(&Snds[SND_PLAYER_WARP_OUT]);
5245                                 Warpout_time = 0.0f;                    // Start timer!
5246                                 Warpout_forced = 0;                             // If non-zero, bash the player to speed and go through effect
5247                         }
5248                         break;
5249
5250                 case GS_EVENT_PLAYER_WARPOUT_STOP:
5251                         if ( Player->control_mode != PCM_NORMAL )       {
5252                                 if ( !Warpout_forced )  {               // cannot cancel forced warpout
5253                                         Player->control_mode = PCM_NORMAL;
5254                                         Viewer_mode = Player->saved_viewer_mode;
5255                                         hud_subspace_notify_abort();
5256                                         mprintf(( "Player put back to normal mode.\n" ));
5257                                         if ( Warpout_sound > -1 )       {
5258                                                 snd_stop( Warpout_sound );
5259                                                 Warpout_sound = -1;
5260                                         }
5261                                 }
5262                         }
5263                         break;
5264
5265                 case GS_EVENT_PLAYER_WARPOUT_DONE_STAGE1:               // player ship got up to speed
5266                         if ( Player->control_mode != PCM_WARPOUT_STAGE1 )       {
5267                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_STOP );
5268                                 mprintf(( "Player put back to normal mode, because of invalid sequence in stage1.\n" ));
5269                         } else {
5270                                 mprintf(( "Hit target speed.  Starting warp effect and moving to stage 2!\n" ));
5271                                 shipfx_warpout_start( Player_obj );
5272                                 Player->control_mode = PCM_WARPOUT_STAGE2;
5273                                 Player->saved_viewer_mode = Viewer_mode;
5274                                 Viewer_mode |= VM_WARP_CHASE;
5275                                 
5276                                 vector tmp = Player_obj->pos;
5277                                 matrix tmp_m;
5278                                 ship_get_eye( &tmp, &tmp_m, Player_obj );
5279                                 vm_vec_scale_add2( &tmp, &Player_obj->orient.rvec, 0.0f );
5280                                 vm_vec_scale_add2( &tmp, &Player_obj->orient.uvec, 0.952f );
5281                                 vm_vec_scale_add2( &tmp, &Player_obj->orient.fvec, -1.782f );
5282                                 Camera_time = 0.0f;
5283                                 camera_set_position( &tmp );
5284                                 camera_set_orient( &Player_obj->orient );
5285                                 vector tmp_vel = { 0.0f, 5.1919f, 14.7f };
5286
5287                                 //mprintf(( "Rad = %.1f\n", Player_obj->radius ));
5288                                 camera_set_velocity( &tmp_vel, 1);
5289                         }
5290                         break;
5291
5292                 case GS_EVENT_PLAYER_WARPOUT_DONE_STAGE2:               // player ship got into the warp effect
5293                         if ( Player->control_mode != PCM_WARPOUT_STAGE2 )       {
5294                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_STOP );
5295                                 mprintf(( "Player put back to normal mode, because of invalid sequence in stage2.\n" ));
5296                         } else {
5297                                 mprintf(( "Hit warp effect.  Moving to stage 3!\n" ));
5298                                 Player->control_mode = PCM_WARPOUT_STAGE3;
5299                         }
5300                         break;
5301
5302                 case GS_EVENT_PLAYER_WARPOUT_DONE:      // player ship got through the warp effect
5303                         mprintf(( "Player warped out.  Going to debriefing!\n" ));
5304                         Player->control_mode = PCM_NORMAL;
5305                         Viewer_mode = Player->saved_viewer_mode;
5306                         Warpout_sound = -1;
5307
5308                         // we have a special debriefing screen for multiplayer furballs
5309                         if((Game_mode & GM_MULTIPLAYER) && (The_mission.game_type & MISSION_TYPE_MULTI_DOGFIGHT)){
5310                                 gameseq_post_event(GS_EVENT_MULTI_DOGFIGHT_DEBRIEF);
5311                         }
5312                         // do the normal debriefing for all other situations
5313                         else {
5314                                 gameseq_post_event(GS_EVENT_DEBRIEF);
5315                         }
5316                         break;
5317
5318                 case GS_EVENT_STANDALONE_POSTGAME:
5319                         gameseq_set_state(GS_STATE_STANDALONE_POSTGAME);
5320                         break;
5321
5322                 case GS_EVENT_INITIAL_PLAYER_SELECT:
5323                         gameseq_set_state(GS_STATE_INITIAL_PLAYER_SELECT);
5324                         break;
5325
5326                 case GS_EVENT_GAME_INIT:
5327         #if defined(FS2_DEMO) || defined(OEM_BUILD)
5328                         gameseq_set_state(GS_STATE_INITIAL_PLAYER_SELECT);
5329         #else                   
5330                         // see if the command line option has been set to use the last pilot, and act acoordingly
5331                         if( player_select_get_last_pilot() ) {                                                          
5332                                 // always enter the main menu -- do the automatic network startup stuff elsewhere
5333                                 // so that we still have valid checks for networking modes, etc.
5334                                 gameseq_set_state(GS_STATE_MAIN_MENU);
5335                         } else {
5336                                 gameseq_set_state(GS_STATE_INITIAL_PLAYER_SELECT);
5337                         }
5338         #endif
5339                         break;
5340
5341                 case GS_EVENT_MULTI_MISSION_SYNC:
5342                         gameseq_set_state(GS_STATE_MULTI_MISSION_SYNC);
5343                         break;          
5344
5345                 case GS_EVENT_MULTI_START_GAME:
5346                         gameseq_set_state(GS_STATE_MULTI_START_GAME);
5347                         break;
5348
5349                 case GS_EVENT_MULTI_HOST_OPTIONS:
5350                         gameseq_set_state(GS_STATE_MULTI_HOST_OPTIONS);
5351                         break;
5352
5353                 case GS_EVENT_MULTI_DOGFIGHT_DEBRIEF:
5354                         gameseq_set_state(GS_STATE_MULTI_DOGFIGHT_DEBRIEF);
5355                         break;
5356
5357                 case GS_EVENT_TEAM_SELECT:
5358                         gameseq_set_state(GS_STATE_TEAM_SELECT);
5359                         break;
5360
5361                 case GS_EVENT_END_CAMPAIGN:                     
5362                         gameseq_set_state(GS_STATE_END_OF_CAMPAIGN);
5363                         break;          
5364
5365                 case GS_EVENT_END_DEMO:
5366                         gameseq_set_state(GS_STATE_END_DEMO);
5367                         break;
5368
5369                 case GS_EVENT_LOOP_BRIEF:
5370                         gameseq_set_state(GS_STATE_LOOP_BRIEF);
5371                         break;
5372
5373                 default:
5374                         Int3();
5375                         break;
5376         }
5377 }
5378
5379 // Called when a state is being left.
5380 // The current state is still at old_state, but as soon as
5381 // this function leaves, then the current state will become
5382 // new state.     You should never try to change the state
5383 // in here... if you think you need to, you probably really
5384 // need to post an event, not change the state.
5385 void game_leave_state( int old_state, int new_state )
5386 {
5387         int end_mission = 1;
5388
5389         switch (new_state) {
5390                 case GS_STATE_GAME_PAUSED:
5391                 case GS_STATE_DEBUG_PAUSED:
5392                 case GS_STATE_OPTIONS_MENU:
5393                 case GS_STATE_CONTROL_CONFIG:           
5394                 case GS_STATE_MISSION_LOG_SCROLLBACK:
5395                 case GS_STATE_DEATH_DIED:
5396                 case GS_STATE_SHOW_GOALS:
5397                 case GS_STATE_HOTKEY_SCREEN:            
5398                 case GS_STATE_MULTI_PAUSED:
5399                 case GS_STATE_TRAINING_PAUSED:
5400                 case GS_STATE_EVENT_DEBUG:                              
5401                 case GS_STATE_GAMEPLAY_HELP:
5402                         end_mission = 0;  // these events shouldn't end a mission
5403                         break;
5404         }
5405
5406         switch (old_state) {
5407                 case GS_STATE_BRIEFING:
5408                         brief_stop_voices();
5409                         if ( (new_state != GS_STATE_OPTIONS_MENU) && (new_state != GS_STATE_WEAPON_SELECT)
5410                                   && (new_state != GS_STATE_SHIP_SELECT) && (new_state != GS_STATE_HOTKEY_SCREEN)
5411                                   && (new_state != GS_STATE_TEAM_SELECT) ){
5412                                 common_select_close();
5413                                 if ( new_state == GS_STATE_MAIN_MENU ) {
5414                                         freespace_stop_mission();       
5415                                 }
5416                         }
5417                         
5418                         // COMMAND LINE OPTION
5419                         if (Cmdline_multi_stream_chat_to_file){
5420                                 cfwrite_string(NOX("-------------------------------------------\n"),Multi_chat_stream);
5421                                 cfclose(Multi_chat_stream);
5422                         }
5423                         break;
5424
5425                 case GS_STATE_DEBRIEF:
5426                         if ( (new_state != GS_STATE_VIEW_MEDALS) && (new_state != GS_STATE_OPTIONS_MENU) ) {
5427                                 debrief_close();                                
5428                         }
5429                         break;
5430
5431                 case GS_STATE_MULTI_DOGFIGHT_DEBRIEF:
5432                         multi_df_debrief_close();
5433                         break;
5434
5435                 case GS_STATE_LOAD_MISSION_MENU:
5436                         mission_load_menu_close();
5437                         break;
5438
5439                 case GS_STATE_SIMULATOR_ROOM:
5440                         sim_room_close();
5441                         break;
5442
5443                 case GS_STATE_CAMPAIGN_ROOM:
5444                         campaign_room_close();
5445                         break;
5446
5447                 case GS_STATE_CMD_BRIEF:
5448                         if (new_state == GS_STATE_OPTIONS_MENU) {
5449                                 cmd_brief_hold();
5450
5451                         } else {
5452                                 cmd_brief_close();
5453                                 if (new_state == GS_STATE_MAIN_MENU)
5454                                         freespace_stop_mission();       
5455                         }
5456
5457                         break;
5458
5459                 case GS_STATE_RED_ALERT:
5460                         red_alert_close();
5461                         break;
5462
5463                 case GS_STATE_SHIP_SELECT:
5464                         if ( new_state != GS_STATE_OPTIONS_MENU && new_state != GS_STATE_WEAPON_SELECT &&
5465                                   new_state != GS_STATE_HOTKEY_SCREEN &&
5466                                   new_state != GS_STATE_BRIEFING && new_state != GS_STATE_TEAM_SELECT) {
5467                                 common_select_close();
5468                                 if ( new_state == GS_STATE_MAIN_MENU ) {
5469                                         freespace_stop_mission();       
5470                                 }
5471                         }
5472                         break;
5473
5474                 case GS_STATE_WEAPON_SELECT:
5475                         if ( new_state != GS_STATE_OPTIONS_MENU && new_state != GS_STATE_SHIP_SELECT &&
5476                                   new_state != GS_STATE_HOTKEY_SCREEN &&
5477                                   new_state != GS_STATE_BRIEFING && new_state != GS_STATE_TEAM_SELECT) {
5478                                 common_select_close();
5479                                 if ( new_state == GS_STATE_MAIN_MENU ) {
5480                                         freespace_stop_mission();       
5481                                 }
5482                         }
5483                         break;
5484
5485                 case GS_STATE_TEAM_SELECT:
5486                         if ( new_state != GS_STATE_OPTIONS_MENU && new_state != GS_STATE_SHIP_SELECT &&
5487                                   new_state != GS_STATE_HOTKEY_SCREEN &&
5488                                   new_state != GS_STATE_BRIEFING && new_state != GS_STATE_WEAPON_SELECT) {
5489                                 common_select_close();
5490                                 if ( new_state == GS_STATE_MAIN_MENU ) {
5491                                         freespace_stop_mission();       
5492                                 }
5493                         }                                       
5494                         break;
5495
5496                 case GS_STATE_MAIN_MENU:
5497 #if defined(PRESS_TOUR_BUILD) || defined(PD_BUILD)
5498                         mht_close();
5499 #else
5500                         main_hall_close();
5501 #endif
5502                         break;
5503
5504                 case GS_STATE_OPTIONS_MENU:
5505                         //game_start_time();
5506                         if(new_state == GS_STATE_MULTI_JOIN_GAME){
5507                                 multi_join_clear_game_list();
5508                         }
5509                         options_menu_close();
5510                         break;
5511
5512                 case GS_STATE_BARRACKS_MENU:
5513                         if(new_state != GS_STATE_VIEW_MEDALS){
5514                                 barracks_close();
5515                         }
5516                         break;
5517
5518                 case GS_STATE_MISSION_LOG_SCROLLBACK:
5519                         hud_scrollback_close();
5520                         break;
5521
5522                 case GS_STATE_TRAINING_MENU:
5523                         training_menu_close();
5524                         break;
5525
5526                 case GS_STATE_GAME_PLAY:
5527                         if ( !(Game_mode & GM_STANDALONE_SERVER) ) {
5528                                 player_save_target_and_weapon_link_prefs();
5529                                 game_stop_looped_sounds();
5530                         }
5531
5532                         sound_env_disable();
5533                         joy_ff_stop_effects();
5534
5535                         // stop game time under certain conditions
5536                         if ( end_mission || (Game_mode & GM_NORMAL) || ((Game_mode & GM_MULTIPLAYER) && (new_state == GS_STATE_MULTI_PAUSED)) ){
5537                                 game_stop_time();
5538                         }
5539
5540                         if (end_mission) {
5541                         // shut down any recording or playing demos
5542 #ifdef DEMO_SYSTEM
5543                                 demo_close();
5544 #endif
5545
5546                                 // when in multiplayer and going back to the main menu, send a leave game packet
5547                                 // right away (before calling stop mission).  stop_mission was taking to long to
5548                                 // close mission down and I want people to get notified ASAP.
5549                                 if ( (Game_mode & GM_MULTIPLAYER) && (new_state == GS_STATE_MAIN_MENU) ){
5550                                         multi_quit_game(PROMPT_NONE);
5551                                 }
5552
5553                                 freespace_stop_mission();                       
5554                                 Game_time_compression = F1_0;
5555                         }
5556                         break;
5557
5558                 case GS_STATE_TECH_MENU:
5559                         techroom_close();
5560                         break;
5561
5562                 case GS_STATE_TRAINING_PAUSED:
5563                         Training_num_lines = 0;
5564                         // fall through to GS_STATE_GAME_PAUSED
5565
5566                 case GS_STATE_GAME_PAUSED:
5567                         game_start_time();
5568                         if ( end_mission ) {
5569                                 pause_close(0);
5570                         }
5571                         break;
5572
5573                 case GS_STATE_DEBUG_PAUSED:
5574                         #ifndef NDEBUG
5575                                 game_start_time();
5576                                 pause_debug_close();
5577                         #endif
5578                         break;
5579
5580                 case GS_STATE_HUD_CONFIG:
5581                         hud_config_close();
5582                         break;
5583
5584                 // join/start a game
5585                 case GS_STATE_MULTI_JOIN_GAME:
5586                         if(new_state != GS_STATE_OPTIONS_MENU){
5587                                 multi_join_game_close();
5588                         }
5589                         break;
5590
5591                 case GS_STATE_MULTI_HOST_SETUP:
5592                 case GS_STATE_MULTI_CLIENT_SETUP:
5593                         // if this is just the host going into the options screen, don't do anything
5594                         if((new_state == GS_STATE_MULTI_HOST_OPTIONS) || (new_state == GS_STATE_OPTIONS_MENU)){
5595                                 break;
5596                         }
5597
5598                         // close down the proper state
5599                         if(old_state == GS_STATE_MULTI_HOST_SETUP){
5600                                 multi_create_game_close();
5601                         } else {
5602                                 multi_game_client_setup_close();
5603                         }
5604
5605                         // COMMAND LINE OPTION
5606                         if (Cmdline_multi_stream_chat_to_file){
5607                                 if( (new_state != GS_STATE_TEAM_SELECT) && (Multi_chat_stream!=NULL) ) {
5608                                         cfwrite_string(NOX("-------------------------------------------\n"),Multi_chat_stream);
5609                                         cfclose(Multi_chat_stream);
5610                                 }
5611                         }                       
5612                         break;
5613
5614                 case GS_STATE_CONTROL_CONFIG:
5615                         control_config_close();
5616                         break;
5617
5618                 case GS_STATE_DEATH_DIED:
5619                         Game_mode &= ~GM_DEAD_DIED;
5620                         
5621                         // early end while respawning or blowing up in a multiplayer game
5622                         if((Game_mode & GM_MULTIPLAYER) && ((new_state == GS_STATE_DEBRIEF) || (new_state == GS_STATE_MULTI_DOGFIGHT_DEBRIEF)) ){
5623                                 game_stop_time();
5624                                 freespace_stop_mission();
5625                         }
5626                         break;
5627
5628                 case GS_STATE_DEATH_BLEW_UP:
5629                         Game_mode &= ~GM_DEAD_BLEW_UP;
5630
5631                         // for single player, we might reload mission, etc.  For multiplayer, look at my new state
5632                         // to determine if I should do anything.
5633                         if ( !(Game_mode & GM_MULTIPLAYER) ) {
5634                                 if ( end_mission ){
5635                                         freespace_stop_mission();
5636                                 }
5637                         } else {
5638                                 // if we are not respawing as an observer or as a player, our new state will not
5639                                 // be gameplay state.
5640                                 if ( (new_state != GS_STATE_GAME_PLAY) && (new_state != GS_STATE_MULTI_PAUSED) ) {
5641                                         game_stop_time();                                                                       // hasn't been called yet!!
5642                                         freespace_stop_mission();
5643                                 }
5644                         }
5645                         break;
5646
5647
5648                 case GS_STATE_CREDITS:
5649                         credits_close();
5650                         break;
5651
5652                 case GS_STATE_VIEW_MEDALS:
5653                         medal_main_close();
5654                         break;
5655
5656                 case GS_STATE_SHOW_GOALS:
5657                         mission_show_goals_close();
5658                         break;
5659
5660                 case GS_STATE_HOTKEY_SCREEN:
5661                         if ( new_state != GS_STATE_OPTIONS_MENU ) {
5662                                 mission_hotkey_close();
5663                         }
5664                         break;
5665
5666                 case GS_STATE_MULTI_MISSION_SYNC:
5667                         // if we're moving into the options menu, don't do anything
5668                         if(new_state == GS_STATE_OPTIONS_MENU){
5669                                 break;
5670                         }
5671
5672                         Assert( Game_mode & GM_MULTIPLAYER );
5673                         multi_sync_close();
5674                         if ( new_state == GS_STATE_GAME_PLAY ){
5675                                 // palette_restore_palette();
5676
5677                                 // change a couple of flags to indicate our state!!!
5678                                 Net_player->state = NETPLAYER_STATE_IN_MISSION;
5679                                 send_netplayer_update_packet();
5680
5681                                 // set the game mode
5682                                 Game_mode |= GM_IN_MISSION;
5683                         }                       
5684                         break;          
5685    
5686                 case GS_STATE_VIEW_CUTSCENES:
5687                         cutscenes_screen_close();
5688                         break;
5689
5690                 case GS_STATE_MULTI_STD_WAIT:
5691                         multi_standalone_wait_close();
5692                         break;
5693
5694                 case GS_STATE_STANDALONE_MAIN:                  
5695                         standalone_main_close();
5696                         if(new_state == GS_STATE_MULTI_STD_WAIT){               
5697                                 init_multiplayer_stats();                                                                               
5698                         }                       
5699                         break;
5700
5701                 case GS_STATE_MULTI_PAUSED:
5702                         // if ( end_mission ){
5703                                 pause_close(1);
5704                         // }
5705                         break;                  
5706
5707                 case GS_STATE_INGAME_PRE_JOIN:
5708                         multi_ingame_select_close();
5709                         break;
5710
5711                 case GS_STATE_STANDALONE_POSTGAME:
5712                         multi_standalone_postgame_close();
5713                         break;
5714
5715                 case GS_STATE_INITIAL_PLAYER_SELECT:                    
5716                         player_select_close();                  
5717                         break;          
5718
5719                 case GS_STATE_MULTI_START_GAME:
5720                         multi_start_game_close();
5721                         break;
5722
5723                 case GS_STATE_MULTI_HOST_OPTIONS:
5724                         multi_host_options_close();
5725                         break;                          
5726
5727                 case GS_STATE_END_OF_CAMPAIGN:
5728                         mission_campaign_end_close();
5729                         break;
5730
5731                 case GS_STATE_LOOP_BRIEF:
5732                         loop_brief_close();
5733                         break;
5734         }
5735 }
5736
5737 // Called when a state is being entered.
5738 // The current state is set to the state we're entering at
5739 // this point, and old_state is set to the state we're coming
5740 // from.    You should never try to change the state
5741 // in here... if you think you need to, you probably really
5742 // need to post an event, not change the state.
5743
5744 void game_enter_state( int old_state, int new_state )
5745 {
5746         switch (new_state) {
5747                 case GS_STATE_MAIN_MENU:                                
5748                         // in multiplayer mode, be sure that we are not doing networking anymore.
5749                         if ( Game_mode & GM_MULTIPLAYER ) {
5750                                 Assert( Net_player != NULL );
5751                                 Net_player->flags &= ~NETINFO_FLAG_DO_NETWORKING;
5752                         }
5753
5754                         Game_time_compression = F1_0;
5755         
5756                         // determine which ship this guy is currently based on
5757 #if defined(PRESS_TOUR_BUILD) || defined(PD_BUILD)
5758                         mht_init();
5759 #else
5760                         if (Player->on_bastion) {
5761                                 main_hall_init(1);
5762                         } else {
5763                                 main_hall_init(0);
5764                         }
5765 #endif
5766                         break;
5767
5768                 case GS_STATE_BRIEFING:
5769                         main_hall_stop_music();
5770                         main_hall_stop_ambient();
5771                         
5772                         if (Game_mode & GM_NORMAL) {
5773                                 // AL: Don't call freespace_start_mission() if re-entering from ship or weapon select
5774                                 // MWA: or from options or hotkey screens
5775                                 // JH: or if the command brief state already did this
5776                                 if ( (old_state != GS_STATE_OPTIONS_MENU) && (old_state != GS_STATE_HOTKEY_SCREEN)
5777                                         && (old_state != GS_STATE_SHIP_SELECT) && (old_state != GS_STATE_WEAPON_SELECT)
5778                                         && (old_state != GS_STATE_CMD_BRIEF) ) {
5779                                         if ( !game_start_mission() )                    // this should put us into a new state on failure!
5780                                                 break;
5781                                 }
5782                         }
5783                         // maybe play a movie before the briefing.  don't play if entering briefing screen from ship or weapon select.
5784                         if ( (old_state == GS_STATE_DEBRIEF) || (old_state == GS_STATE_SIMULATOR_ROOM) || (old_state == GS_STATE_MAIN_MENU))
5785                                 mission_campaign_maybe_play_movie(CAMPAIGN_MOVIE_PRE_MISSION);
5786
5787                         Game_time_compression = F1_0;
5788
5789                         if ( red_alert_mission() ) {
5790                                 gameseq_post_event(GS_EVENT_RED_ALERT);
5791                         } else {
5792                                 brief_init();
5793                         }
5794
5795                         break;
5796
5797                 case GS_STATE_DEBRIEF:
5798                         game_stop_looped_sounds();
5799                         mission_goal_fail_incomplete();                         // fail all incomplete goals before entering debriefing
5800                         if ( (old_state != GS_STATE_VIEW_MEDALS) && (old_state != GS_STATE_OPTIONS_MENU) ){
5801                                 debrief_init();
5802                         }
5803                         break;
5804
5805                 case GS_STATE_MULTI_DOGFIGHT_DEBRIEF:
5806                         multi_df_debrief_init();
5807                         break;
5808
5809                 case GS_STATE_LOAD_MISSION_MENU:
5810                         mission_load_menu_init();
5811                         break;
5812
5813                 case GS_STATE_SIMULATOR_ROOM:
5814                         sim_room_init();
5815                         break;
5816
5817                 case GS_STATE_CAMPAIGN_ROOM:
5818                         campaign_room_init();
5819                         break;
5820
5821                 case GS_STATE_RED_ALERT:
5822                         mission_campaign_maybe_play_movie(CAMPAIGN_MOVIE_PRE_MISSION);
5823                         red_alert_init();
5824                         break;
5825
5826                 case GS_STATE_CMD_BRIEF: {
5827                         int team_num = 0;  // team number used as index for which cmd brief to use.
5828
5829                         if (old_state == GS_STATE_OPTIONS_MENU) {
5830                                 cmd_brief_unhold();
5831
5832                         } else {
5833                                 main_hall_stop_music();
5834                                 main_hall_stop_ambient();
5835
5836                                 if (Game_mode & GM_NORMAL) {
5837                                         // AL: Don't call freespace_start_mission() if re-entering from ship or weapon select
5838                                         // MWA: or from options or hotkey screens
5839                                         // JH: or if the command brief state already did this
5840                                         if ( (old_state != GS_STATE_OPTIONS_MENU) && (old_state != GS_STATE_HOTKEY_SCREEN)
5841                                                 && (old_state != GS_STATE_SHIP_SELECT) && (old_state != GS_STATE_WEAPON_SELECT) ) {
5842                                                 if ( !game_start_mission() )                    // this should put us into a new state on failure!
5843                                                         break;
5844                                         }
5845                                 }
5846
5847                                 // maybe play a movie before the briefing.  don't play if entering briefing screen from ship or weapon select.
5848                                 if ( (old_state == GS_STATE_DEBRIEF) || (old_state == GS_STATE_SIMULATOR_ROOM) || (old_state == GS_STATE_MAIN_MENU))
5849                                         mission_campaign_maybe_play_movie(CAMPAIGN_MOVIE_PRE_MISSION);
5850
5851                                 cmd_brief_init(team_num);
5852                         }
5853
5854                         break;
5855                 }
5856
5857                 case GS_STATE_SHIP_SELECT:
5858                         ship_select_init();
5859                         break;
5860
5861                 case GS_STATE_WEAPON_SELECT:
5862                         weapon_select_init();
5863                         break;
5864
5865                 case GS_STATE_TEAM_SELECT:              
5866                         multi_ts_init();
5867                         break;
5868
5869                 case GS_STATE_GAME_PAUSED:
5870                         game_stop_time();
5871                         pause_init(0);
5872                         break;
5873
5874                 case GS_STATE_DEBUG_PAUSED:
5875         //              game_stop_time();
5876         //              os_set_title("FreeSpace - PAUSED");
5877         //              break;
5878         //
5879                 case GS_STATE_TRAINING_PAUSED:
5880                         #ifndef NDEBUG
5881                                 game_stop_time();
5882                                 pause_debug_init();
5883                         #endif
5884                         break;
5885
5886                 case GS_STATE_OPTIONS_MENU:
5887                         //game_stop_time();
5888                         options_menu_init();
5889                         break;
5890  
5891                 case GS_STATE_GAME_PLAY:
5892                         // coming from the gameplay state or the main menu, we might need to load the mission
5893                         if ( (Game_mode & GM_NORMAL) && ((old_state == GS_STATE_MAIN_MENU) || (old_state == GS_STATE_GAME_PLAY) || (old_state == GS_STATE_DEATH_BLEW_UP)) ) {
5894                                 if ( !game_start_mission() )            // this should put us into a new state.
5895                                         // Failed!!!
5896                                         break;
5897                         }
5898
5899                         // if we are coming from the briefing, ship select, weapons loadout, or main menu (in the
5900                         // case of quick start), then do bitmap loads, etc  Don't do any of the loading stuff
5901                         // if we are in multiplayer -- this stuff is all handled in the multi-wait section
5902                         if ( !(Game_mode & GM_MULTIPLAYER) && (old_state == GS_STATE_BRIEFING) || (old_state == GS_STATE_SHIP_SELECT) ||
5903                                 (old_state == GS_STATE_WEAPON_SELECT) || (old_state == GS_STATE_MAIN_MENU) || (old_state == GS_STATE_MULTI_STD_WAIT)    || (old_state == GS_STATE_SIMULATOR_ROOM) ) {
5904                                         // JAS: Used to do all paging here.
5905
5906                                         #ifndef NDEBUG
5907                                         //XSTR:OFF
5908                                                 HUD_printf("Skill level is set to ** %s **", Skill_level_names(Game_skill_level));
5909                                         //XSTR:ON
5910                                         #endif
5911
5912                                         main_hall_stop_music();
5913                                         main_hall_stop_ambient();
5914                                         event_music_first_pattern();    // start the first pattern
5915                         }
5916
5917                         // special code that restores player ship selection and weapons loadout when doing a quick start
5918                         if ( !(Game_mode & GM_MULTIPLAYER) && (old_state == GS_STATE_MAIN_MENU) || (old_state == GS_STATE_DEATH_BLEW_UP)  || (old_state == GS_STATE_GAME_PLAY) ) {
5919                                 if ( !stricmp(Player_loadout.filename, Game_current_mission_filename) ) {
5920                                         wss_direct_restore_loadout();
5921                                 }
5922                         }
5923
5924                         // single-player, quick-start after just died... we need to set weapon linking and kick off the event music
5925                         if (!(Game_mode & GM_MULTIPLAYER) && (old_state == GS_STATE_DEATH_BLEW_UP) ) {
5926                                 event_music_first_pattern();    // start the first pattern
5927                         }
5928
5929                         if ( !(Game_mode & GM_STANDALONE_SERVER) && (old_state != GS_STATE_GAME_PAUSED) && (old_state != GS_STATE_MULTI_PAUSED) ) {
5930                                 event_music_first_pattern();    // start the first pattern
5931                         }                       
5932                         player_restore_target_and_weapon_link_prefs();
5933
5934                         Game_mode |= GM_IN_MISSION;
5935
5936 #ifndef NDEBUG
5937                         // required to truely make mouse deltas zeroed in debug mouse code
5938 void mouse_force_pos(int x, int y);
5939                         mouse_force_pos(gr_screen.max_w / 2, gr_screen.max_h / 2);
5940 #endif
5941
5942                         game_flush();
5943
5944                         // only start time if in single player, or coming from multi wait state
5945                         if (
5946                                         (
5947                                                 (Game_mode & GM_NORMAL) && 
5948                                                 (old_state != GS_STATE_VIEW_CUTSCENES)
5949                                         ) || (
5950                                                 (Game_mode & GM_MULTIPLAYER) && (
5951                                                         (old_state == GS_STATE_MULTI_PAUSED) ||
5952                                                         (old_state == GS_STATE_MULTI_MISSION_SYNC)
5953                                                 )
5954                                         )
5955                                 )
5956                                         game_start_time();
5957
5958                         // when coming from the multi paused state, reset the timestamps
5959                         if ( (Game_mode & GM_MULTIPLAYER) && (old_state == GS_STATE_MULTI_PAUSED) ){
5960                                 multi_reset_timestamps();
5961                         }
5962
5963                         if ((Game_mode & GM_MULTIPLAYER) && (old_state != GS_STATE_DEATH_BLEW_UP) ) {
5964                                 // initialize all object update details
5965                                 multi_oo_gameplay_init();
5966                         }
5967         
5968                         // under certain circumstances, the server should reset the object update rate limiting stuff
5969                         if( ((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)) &&
5970                                  (old_state == GS_STATE_MULTI_PAUSED) || (old_state == GS_STATE_MULTI_MISSION_SYNC) ){
5971                                 
5972                                 // reinitialize the rate limiting system for all clients
5973                                 multi_oo_rate_init_all();
5974                         }
5975
5976                         // multiplayer clients should always re-initialize their control info rate limiting system                      
5977                         if((Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER)){
5978                                 multi_oo_rate_init_all();
5979                         }
5980                         
5981                         // reset ping times
5982                         if(Game_mode & GM_MULTIPLAYER){
5983                                 multi_ping_reset_players();
5984                         }
5985
5986                         Game_subspace_effect = 0;
5987                         if (The_mission.flags & MISSION_FLAG_SUBSPACE) {
5988                                 Game_subspace_effect = 1;
5989                                 if( !(Game_mode & GM_STANDALONE_SERVER) ){      
5990                                         game_start_subspace_ambient_sound();
5991                                 }
5992                         }
5993
5994                         sound_env_set(&Game_sound_env);
5995                         joy_ff_mission_init(Ship_info[Player_ship->ship_info_index].rotation_time);
5996
5997                         // clear multiplayer button info                        i
5998                         extern button_info Multi_ship_status_bi;
5999                         memset(&Multi_ship_status_bi, 0, sizeof(button_info));
6000                         break;
6001
6002                 case GS_STATE_HUD_CONFIG:
6003                         hud_config_init();
6004                         break;
6005
6006                 case GS_STATE_MULTI_JOIN_GAME:
6007                         multi_join_clear_game_list();
6008
6009                         if (old_state != GS_STATE_OPTIONS_MENU) {
6010                                 multi_join_game_init();
6011                         }
6012
6013                         break;
6014
6015                 case GS_STATE_MULTI_HOST_SETUP:         
6016                         // don't reinitialize if we're coming back from the host options screen
6017                         if ((old_state != GS_STATE_MULTI_HOST_OPTIONS) && (old_state != GS_STATE_OPTIONS_MENU)) {
6018                                 multi_create_game_init();
6019                         }
6020
6021                         break;
6022
6023                 case GS_STATE_MULTI_CLIENT_SETUP:               
6024                         if (old_state != GS_STATE_OPTIONS_MENU) {
6025                                 multi_game_client_setup_init();
6026                         }
6027
6028                         break;
6029
6030                 case GS_STATE_CONTROL_CONFIG:
6031                         control_config_init();
6032                         break;
6033
6034                 case GS_STATE_TECH_MENU:
6035                         techroom_init();
6036                         break;
6037
6038                 case GS_STATE_BARRACKS_MENU:
6039                         if(old_state != GS_STATE_VIEW_MEDALS){
6040                                 barracks_init();
6041                         }
6042                         break;
6043
6044                 case GS_STATE_MISSION_LOG_SCROLLBACK:
6045                         hud_scrollback_init();
6046                         break;
6047
6048                 case GS_STATE_DEATH_DIED:
6049                         Player_died_time = timestamp(10);
6050
6051                         if(!(Game_mode & GM_MULTIPLAYER)){
6052                                 player_show_death_message();
6053                         }
6054                         Game_mode |= GM_DEAD_DIED;
6055                         break;
6056
6057                 case GS_STATE_DEATH_BLEW_UP:
6058                         if ( !popupdead_is_active() ) {
6059                                 Player_ai->target_objnum = -1;
6060                         }
6061
6062                         // stop any local EMP effect
6063                         emp_stop_local();
6064
6065                         Players[Player_num].flags &= ~PLAYER_FLAGS_AUTO_TARGETING;      //      Prevent immediate switch to a hostile ship.
6066                         Game_mode |= GM_DEAD_BLEW_UP;           
6067                         Show_viewing_from_self = 0;
6068
6069                         // timestamp how long we should wait before displaying the died popup
6070                         if ( !popupdead_is_active() ) {
6071                                 Player_died_popup_wait = timestamp(PLAYER_DIED_POPUP_WAIT);
6072                         }
6073                         break;
6074
6075                 case GS_STATE_GAMEPLAY_HELP:
6076                         gameplay_help_init();
6077                         break;
6078
6079                 case GS_STATE_CREDITS:
6080                         main_hall_stop_music();
6081                         main_hall_stop_ambient();
6082                         credits_init();
6083                         break;
6084
6085                 case GS_STATE_VIEW_MEDALS:
6086                         medal_main_init(Player);
6087                         break;
6088
6089                 case GS_STATE_SHOW_GOALS:
6090                         mission_show_goals_init();
6091                         break;
6092
6093                 case GS_STATE_HOTKEY_SCREEN:
6094                         mission_hotkey_init();
6095                         break;
6096
6097                 case GS_STATE_MULTI_MISSION_SYNC:
6098                         // if we're coming from the options screen, don't do any
6099                         if(old_state == GS_STATE_OPTIONS_MENU){
6100                                 break;
6101                         }
6102
6103                         switch(Multi_sync_mode){
6104                         case MULTI_SYNC_PRE_BRIEFING:
6105                                 // if moving from game forming to the team select state                                         
6106                                 multi_sync_init();                      
6107                                 break;
6108                         case MULTI_SYNC_POST_BRIEFING:
6109                                 // if moving from briefing into the mission itself                      
6110                                 multi_sync_init();
6111                         
6112                                 // tell everyone that we're now loading data
6113                                 Net_player->state = NETPLAYER_STATE_DATA_LOAD;
6114                                 send_netplayer_update_packet();
6115
6116                                 // JAS: Used to do all paging here!!!!
6117                                                                 
6118                                 Net_player->state = NETPLAYER_STATE_WAITING;                    
6119                                 send_netplayer_update_packet();                         
6120                                 Missiontime = 0;
6121                                 Game_time_compression = F1_0;
6122                                 break;
6123                         case MULTI_SYNC_INGAME:
6124                                 multi_sync_init();
6125                                 break;
6126                         }
6127                         break;          
6128    
6129                 case GS_STATE_VIEW_CUTSCENES:
6130                         cutscenes_screen_init();
6131                         break;
6132
6133                 case GS_STATE_MULTI_STD_WAIT:
6134                         multi_standalone_wait_init();
6135                         break;
6136
6137                 case GS_STATE_STANDALONE_MAIN:
6138                         // don't initialize if we're coming from one of these 2 states unless there are no 
6139                         // players left (reset situation)
6140                         if((old_state != GS_STATE_STANDALONE_POSTGAME) || multi_endgame_ending()){
6141                                 standalone_main_init();
6142                         }
6143                         break;  
6144
6145                 case GS_STATE_MULTI_PAUSED:
6146                         pause_init(1);
6147                         break;
6148                 
6149                 case GS_STATE_INGAME_PRE_JOIN:
6150                         multi_ingame_select_init();
6151                         break;
6152
6153                 case GS_STATE_STANDALONE_POSTGAME:
6154                         multi_standalone_postgame_init();
6155                         break;
6156
6157                 case GS_STATE_INITIAL_PLAYER_SELECT:
6158                         player_select_init();
6159                         break;          
6160
6161                 case GS_STATE_MULTI_START_GAME:
6162                         multi_start_game_init();
6163                         break;
6164
6165                 case GS_STATE_MULTI_HOST_OPTIONS:
6166                         multi_host_options_init();
6167                         break;          
6168
6169                 case GS_STATE_END_OF_CAMPAIGN:
6170                         mission_campaign_end_init();
6171                         break;          
6172
6173                 case GS_STATE_LOOP_BRIEF:
6174                         loop_brief_init();
6175                         break;
6176
6177         } // end switch
6178 }
6179
6180 // do stuff that may need to be done regardless of state
6181 void game_do_state_common(int state,int no_networking)
6182 {
6183         game_maybe_draw_mouse(flFrametime);             // determine if to draw the mouse this frame
6184         snd_do_frame();                                                         // update sound system
6185         event_music_do_frame();                                         // music needs to play across many states
6186
6187         multi_log_process();    
6188
6189         if (no_networking) {
6190                 return;
6191         }
6192
6193         // maybe do a multiplayer frame based on game mode and state type       
6194         if (Game_mode & GM_MULTIPLAYER) {
6195                 switch (state) {
6196                         case GS_STATE_OPTIONS_MENU:
6197                         case GS_STATE_GAMEPLAY_HELP:
6198                         case GS_STATE_HOTKEY_SCREEN:
6199                         case GS_STATE_HUD_CONFIG:
6200                         case GS_STATE_CONTROL_CONFIG:
6201                         case GS_STATE_MISSION_LOG_SCROLLBACK:
6202                         case GS_STATE_SHOW_GOALS:
6203                         case GS_STATE_VIEW_CUTSCENES:
6204                         case GS_STATE_EVENT_DEBUG:
6205                                 multi_maybe_do_frame();
6206                                 break;
6207                 }
6208                 
6209                 game_do_networking();
6210         }
6211 }
6212
6213 // Called once a frame.
6214 // You should never try to change the state
6215 // in here... if you think you need to, you probably really
6216 // need to post an event, not change the state.
6217 int Game_do_state_should_skip = 0;
6218 void game_do_state(int state)
6219 {
6220         // always lets the do_state_common() function determine if the state should be skipped
6221         Game_do_state_should_skip = 0;
6222         
6223         // legal to set the should skip state anywhere in this function
6224         game_do_state_common(state);    // do stuff that may need to be done regardless of state
6225
6226         if(Game_do_state_should_skip){
6227                 return;
6228         }
6229         
6230         switch (state) {
6231                 case GS_STATE_MAIN_MENU:
6232                         game_set_frametime(GS_STATE_MAIN_MENU);
6233 #if defined(PRESS_TOUR_BUILD) || defined(PD_BUILD)
6234                         mht_do();
6235 #else
6236                         main_hall_do(flFrametime);
6237 #endif
6238                         break;
6239
6240                 case GS_STATE_OPTIONS_MENU:
6241                         game_set_frametime(GS_STATE_OPTIONS_MENU);
6242                         options_menu_do_frame(flFrametime);
6243                         break;
6244
6245                 case GS_STATE_BARRACKS_MENU:
6246                         game_set_frametime(GS_STATE_BARRACKS_MENU);
6247                         barracks_do_frame(flFrametime);
6248                         break;
6249
6250                 case GS_STATE_TRAINING_MENU:
6251                         game_set_frametime(GS_STATE_TRAINING_MENU);
6252                         training_menu_do_frame(flFrametime);
6253                         break;
6254
6255                 case GS_STATE_TECH_MENU:
6256                         game_set_frametime(GS_STATE_TECH_MENU);
6257                         techroom_do_frame(flFrametime);
6258                         break;
6259
6260                 case GS_STATE_GAMEPLAY_HELP:
6261                         game_set_frametime(GS_STATE_GAMEPLAY_HELP);
6262                         gameplay_help_do_frame(flFrametime);
6263                         break;
6264
6265                 case GS_STATE_GAME_PLAY:        // do stuff that should be done during gameplay
6266                         game_do_frame();
6267                         break;
6268
6269                 case GS_STATE_GAME_PAUSED:
6270                         pause_do(0);
6271                         break;
6272
6273                 case GS_STATE_DEBUG_PAUSED:
6274                         #ifndef NDEBUG
6275                                 game_set_frametime(GS_STATE_DEBUG_PAUSED);
6276                                 pause_debug_do();
6277                         #endif
6278                         break;
6279
6280                 case GS_STATE_TRAINING_PAUSED:
6281                         game_training_pause_do();
6282                         break;
6283
6284                 case GS_STATE_LOAD_MISSION_MENU:
6285                         game_set_frametime(GS_STATE_LOAD_MISSION_MENU);
6286                         mission_load_menu_do();
6287                         break;
6288                 
6289                 case GS_STATE_BRIEFING:
6290                         game_set_frametime(GS_STATE_BRIEFING);
6291                         brief_do_frame(flFrametime);
6292                         break;
6293
6294                 case GS_STATE_DEBRIEF:
6295                         game_set_frametime(GS_STATE_DEBRIEF);
6296                         debrief_do_frame(flFrametime);
6297                         break;
6298
6299                 case GS_STATE_MULTI_DOGFIGHT_DEBRIEF:
6300                         game_set_frametime(GS_STATE_MULTI_DOGFIGHT_DEBRIEF);
6301                         multi_df_debrief_do();
6302                         break;
6303
6304                 case GS_STATE_SHIP_SELECT:
6305                         game_set_frametime(GS_STATE_SHIP_SELECT);
6306                         ship_select_do(flFrametime);
6307                         break;
6308
6309                 case GS_STATE_WEAPON_SELECT:
6310                         game_set_frametime(GS_STATE_WEAPON_SELECT);
6311                         weapon_select_do(flFrametime);
6312                         break;
6313
6314                 case GS_STATE_MISSION_LOG_SCROLLBACK:
6315                         game_set_frametime(GS_STATE_MISSION_LOG_SCROLLBACK);
6316                         hud_scrollback_do_frame(flFrametime);
6317                         break;
6318
6319                 case GS_STATE_HUD_CONFIG:
6320                         game_set_frametime(GS_STATE_HUD_CONFIG);
6321                         hud_config_do_frame(flFrametime);
6322                         break;
6323
6324                 case GS_STATE_MULTI_JOIN_GAME:
6325                         game_set_frametime(GS_STATE_MULTI_JOIN_GAME);
6326                         multi_join_game_do_frame();
6327                         break;
6328
6329                 case GS_STATE_MULTI_HOST_SETUP:
6330                         game_set_frametime(GS_STATE_MULTI_HOST_SETUP);
6331                         multi_create_game_do();
6332                         break;
6333
6334                 case GS_STATE_MULTI_CLIENT_SETUP:
6335                         game_set_frametime(GS_STATE_MULTI_CLIENT_SETUP);
6336                         multi_game_client_setup_do_frame();
6337                         break;
6338
6339                 case GS_STATE_CONTROL_CONFIG:
6340                         game_set_frametime(GS_STATE_CONTROL_CONFIG);
6341                         control_config_do_frame(flFrametime);
6342                         break;  
6343
6344                 case GS_STATE_DEATH_DIED:
6345                         game_do_frame();                        
6346                         break;
6347
6348                 case GS_STATE_DEATH_BLEW_UP:
6349                         game_do_frame();
6350                         break;
6351
6352                 case GS_STATE_SIMULATOR_ROOM:
6353                         game_set_frametime(GS_STATE_SIMULATOR_ROOM);
6354                         sim_room_do_frame(flFrametime);
6355                         break;
6356
6357                 case GS_STATE_CAMPAIGN_ROOM:
6358                         game_set_frametime(GS_STATE_CAMPAIGN_ROOM);
6359                         campaign_room_do_frame(flFrametime);
6360                         break;
6361
6362                 case GS_STATE_RED_ALERT:
6363                         game_set_frametime(GS_STATE_RED_ALERT);
6364                         red_alert_do_frame(flFrametime);
6365                         break;
6366
6367                 case GS_STATE_CMD_BRIEF:
6368                         game_set_frametime(GS_STATE_CMD_BRIEF);
6369                         cmd_brief_do_frame(flFrametime);
6370                         break;
6371
6372                 case GS_STATE_CREDITS:
6373                         game_set_frametime(GS_STATE_CREDITS);
6374                         credits_do_frame(flFrametime);
6375                         break;
6376
6377                 case GS_STATE_VIEW_MEDALS:
6378                         game_set_frametime(GS_STATE_VIEW_MEDALS);
6379                         medal_main_do();
6380                         break;
6381
6382                 case GS_STATE_SHOW_GOALS:
6383                         game_set_frametime(GS_STATE_SHOW_GOALS);
6384                         mission_show_goals_do_frame(flFrametime);
6385                         break;
6386
6387                 case GS_STATE_HOTKEY_SCREEN:
6388                         game_set_frametime(GS_STATE_HOTKEY_SCREEN);
6389                         mission_hotkey_do_frame(flFrametime);
6390                         break;  
6391    
6392                 case GS_STATE_VIEW_CUTSCENES:
6393                         game_set_frametime(GS_STATE_VIEW_CUTSCENES);
6394                         cutscenes_screen_do_frame();
6395                         break;
6396
6397                 case GS_STATE_MULTI_STD_WAIT:
6398                         game_set_frametime(GS_STATE_STANDALONE_MAIN);
6399                         multi_standalone_wait_do();
6400                         break;
6401
6402                 case GS_STATE_STANDALONE_MAIN:
6403                         game_set_frametime(GS_STATE_STANDALONE_MAIN);
6404                         standalone_main_do();
6405                         break;  
6406
6407                 case GS_STATE_MULTI_PAUSED:
6408                         game_set_frametime(GS_STATE_MULTI_PAUSED);
6409                         pause_do(1);
6410                         break;
6411
6412                 case GS_STATE_TEAM_SELECT:
6413                         game_set_frametime(GS_STATE_TEAM_SELECT);
6414                         multi_ts_do();
6415                         break;
6416
6417                 case GS_STATE_INGAME_PRE_JOIN:
6418                         game_set_frametime(GS_STATE_INGAME_PRE_JOIN);
6419                         multi_ingame_select_do();
6420                         break;
6421
6422                 case GS_STATE_EVENT_DEBUG:
6423         #ifndef NDEBUG
6424                         game_set_frametime(GS_STATE_EVENT_DEBUG);
6425                         game_show_event_debug(flFrametime);
6426         #endif
6427                         break;
6428
6429                 case GS_STATE_STANDALONE_POSTGAME:
6430                         game_set_frametime(GS_STATE_STANDALONE_POSTGAME);
6431                         multi_standalone_postgame_do();
6432                         break;
6433
6434                 case GS_STATE_INITIAL_PLAYER_SELECT:
6435                         game_set_frametime(GS_STATE_INITIAL_PLAYER_SELECT);
6436                         player_select_do();
6437                         break;
6438
6439                 case GS_STATE_MULTI_MISSION_SYNC:
6440                         game_set_frametime(GS_STATE_MULTI_MISSION_SYNC);
6441                         multi_sync_do();
6442                         break;          
6443
6444                 case GS_STATE_MULTI_START_GAME:
6445                         game_set_frametime(GS_STATE_MULTI_START_GAME);
6446                         multi_start_game_do();
6447                         break;
6448                 
6449                 case GS_STATE_MULTI_HOST_OPTIONS:
6450                         game_set_frametime(GS_STATE_MULTI_HOST_OPTIONS);
6451                         multi_host_options_do();
6452                         break;          
6453
6454                 case GS_STATE_END_OF_CAMPAIGN:
6455                         mission_campaign_end_do();
6456                         break;          
6457
6458                 case GS_STATE_END_DEMO:
6459                         game_set_frametime(GS_STATE_END_DEMO);
6460                         end_demo_campaign_do();
6461                         break;
6462
6463                 case GS_STATE_LOOP_BRIEF:
6464                         game_set_frametime(GS_STATE_LOOP_BRIEF);
6465                         loop_brief_do();
6466                         break;
6467
6468    } // end switch(gs_current_state)
6469 }
6470
6471
6472 // return 0 if there is enough RAM to run FreeSpace, otherwise return -1
6473 int game_do_ram_check(int ram_in_bytes)
6474 {
6475         if ( ram_in_bytes < 30*1024*1024 )      {
6476                 int allowed_to_run = 1;
6477                 if ( ram_in_bytes < 25*1024*1024 ) {
6478                         allowed_to_run = 0;
6479                 }
6480
6481                 char tmp[1024];
6482                 int Freespace_total_ram_MB;
6483                 Freespace_total_ram_MB = fl2i(ram_in_bytes/(1024*1024));
6484
6485                 if ( allowed_to_run ) {
6486
6487                         sprintf( tmp, XSTR( "FreeSpace has detected that you only have %dMB of free memory.\n\nFreeSpace requires at least 32MB of memory to run.  If you think you have more than %dMB of physical memory, ensure that you aren't running SmartDrive (SMARTDRV.EXE).  Any memory allocated to SmartDrive is not usable by applications\n\nPress 'OK' to continue running with less than the minimum required memory\n", 193), Freespace_total_ram_MB, Freespace_total_ram_MB);
6488
6489                         int msgbox_rval;
6490 #ifndef PLAT_UNIX                       
6491                         msgbox_rval = MessageBox( NULL, tmp, XSTR( "Not Enough RAM", 194), MB_OKCANCEL );
6492                         if ( msgbox_rval == IDCANCEL ) {
6493                                 return -1;
6494                         }
6495 #else
6496                         STUB_FUNCTION;
6497 #endif                  
6498                 } else {
6499                         sprintf( tmp, XSTR( "FreeSpace has detected that you only have %dMB of free memory.\n\nFreeSpace requires at least 32MB of memory to run.  If you think you have more than %dMB of physical memory, ensure that you aren't running SmartDrive (SMARTDRV.EXE).  Any memory allocated to SmartDrive is not usable by applications\n", 195), Freespace_total_ram_MB, Freespace_total_ram_MB);
6500 #ifndef PLAT_UNIX
6501                         MessageBox( NULL, tmp, XSTR( "Not Enough RAM", 194), MB_OK );
6502 #else
6503                         STUB_FUNCTION;
6504 #endif                  
6505                         return -1;
6506                 }
6507         }
6508
6509         return 0;
6510 }
6511
6512 // Check if there is a freespace.exe in the /update directory (relative to where fs.exe is installed).
6513 // If so, copy it over and remove the update directory.
6514 void game_maybe_update_launcher(char *exe_dir)
6515 {
6516 #ifndef PLAT_UNIX
6517         char src_filename[MAX_PATH];
6518         char dest_filename[MAX_PATH];
6519
6520         strcpy(src_filename, exe_dir);
6521         strcat(src_filename, NOX("\\update\\freespace.exe"));
6522
6523         strcpy(dest_filename, exe_dir);
6524         strcat(dest_filename, NOX("\\freespace.exe"));
6525
6526         // see if src_filename exists
6527         FILE *fp;
6528         fp = fopen(src_filename, "rb");
6529         if ( !fp ) {
6530                 return;
6531         }
6532         fclose(fp);
6533
6534         SetFileAttributes(dest_filename, FILE_ATTRIBUTE_NORMAL);
6535
6536         // copy updated freespace.exe to freespace exe dir
6537         if ( CopyFile(src_filename, dest_filename, 0) == 0 ) {
6538                 MessageBox( NULL, XSTR("Unable to copy freespace.exe from update directory to installed directory.  You should copy freespace.exe from the update directory (located in your FreeSpace install directory) to your install directory", 988), NULL, MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
6539                 return;
6540         }
6541
6542         // delete the file in the update directory
6543         DeleteFile(src_filename);
6544
6545         // safe to assume directory is empty, since freespace.exe should only be the file ever in the update dir
6546         char update_dir[MAX_PATH];
6547         strcpy(update_dir, exe_dir);
6548         strcat(update_dir, NOX("\\update"));
6549         RemoveDirectory(update_dir);
6550 #else
6551         STUB_FUNCTION;
6552 #endif  
6553 }
6554
6555 void game_spew_pof_info_sub(int model_num, polymodel *pm, int sm, CFILE *out, int *out_total, int *out_destroyed_total)
6556 {
6557         int i;
6558         int sub_total = 0;
6559         int sub_total_destroyed = 0;
6560         int total = 0;
6561         char str[255] = "";             
6562         
6563         // get the total for all his children
6564         for (i=pm->submodel[sm].first_child; i>-1; i = pm->submodel[i].next_sibling )   {
6565                 game_spew_pof_info_sub(model_num, pm, i, out, &sub_total, &sub_total_destroyed);
6566         }       
6567
6568         // find the # of faces for this _individual_ object     
6569         total = submodel_get_num_polys(model_num, sm);
6570         if(strstr(pm->submodel[sm].name, "-destroyed")){
6571                 sub_total_destroyed = total;
6572         }
6573         
6574         // write out total
6575         sprintf(str, "Submodel %s total : %d faces\n", pm->submodel[sm].name, total);
6576         cfputs(str, out);               
6577
6578         *out_total += total + sub_total;
6579         *out_destroyed_total += sub_total_destroyed;
6580 }
6581
6582 #define BAIL()                  do { int idx; for(idx=0; idx<num_files; idx++){ if(pof_list[idx] != NULL){free(pof_list[idx]); pof_list[idx] = NULL;}} return;} while(0);
6583 void game_spew_pof_info()
6584 {
6585         char *pof_list[1000];
6586         int num_files;  
6587         CFILE *out;
6588         int idx, model_num, i, j;
6589         polymodel *pm;
6590         int total, root_total, model_total, destroyed_total, counted;
6591         char str[255] = "";
6592
6593         // get file list
6594         num_files = cf_get_file_list(1000, pof_list, CF_TYPE_MODELS, "*.pof");
6595
6596         // spew info on all the pofs
6597         if(!num_files){
6598                 return;
6599         }
6600
6601         // go
6602         out = cfopen("pofspew.txt", "wt", CFILE_NORMAL, CF_TYPE_DATA);
6603         if(out == NULL){
6604                 BAIL();
6605         }       
6606         counted = 0;    
6607         for(idx=0; idx<num_files; idx++, counted++){
6608                 sprintf(str, "%s.pof", pof_list[idx]);
6609                 model_num = model_load(str, 0, NULL);
6610                 if(model_num >= 0){
6611                         pm = model_get(model_num);
6612
6613                         // if we have a real model
6614                         if(pm != NULL){                         
6615                                 cfputs(str, out);
6616                                 cfputs("\n", out);
6617                                 
6618                                 // go through and print all raw submodels
6619                                 cfputs("RAW\n", out);
6620                                 total = 0;
6621                                 model_total = 0;                                
6622                                 for (i=0; i<pm->n_models; i++)  {                                       
6623                                         total = submodel_get_num_polys(model_num, i);                                   
6624                                         
6625                                         model_total += total;
6626                                         sprintf(str, "Submodel %s total : %d faces\n", pm->submodel[i].name, total);
6627                                         cfputs(str, out);
6628                                 }                               
6629                                 sprintf(str, "Model total %d\n", model_total);                          
6630                                 cfputs(str, out);                               
6631
6632                                 // now go through and do it by LOD
6633                                 cfputs("BY LOD\n\n", out);                              
6634                                 for(i=0; i<pm->n_detail_levels; i++){
6635                                         sprintf(str, "LOD %d\n", i);
6636                                         cfputs(str, out);
6637
6638                                         // submodels
6639                                         root_total = submodel_get_num_polys(model_num, pm->detail[i] );
6640                                         total = 0;
6641                                         destroyed_total = 0;
6642                                         for (j=pm->submodel[pm->detail[i]].first_child; j>-1; j = pm->submodel[j].next_sibling )        {
6643                                                 game_spew_pof_info_sub(model_num, pm, j, out, &total, &destroyed_total);
6644                                         }
6645
6646                                         sprintf(str, "Submodel %s total : %d faces\n", pm->submodel[pm->detail[i]].name, root_total);
6647                                         cfputs(str, out);
6648
6649                                         sprintf(str, "TOTAL: %d\n", total + root_total);                                        
6650                                         cfputs(str, out);
6651                                         sprintf(str, "TOTAL not counting destroyed faces %d\n", (total + root_total) - destroyed_total);
6652                                         cfputs(str, out);
6653                                         sprintf(str, "TOTAL destroyed faces %d\n\n", destroyed_total);
6654                                         cfputs(str, out);
6655                                 }                               
6656                                 cfputs("------------------------------------------------------------------------\n\n", out);                            
6657                         }
6658                 }
6659
6660                 if(counted >= MAX_POLYGON_MODELS - 5){
6661                         model_free_all();
6662                         counted = 0;
6663                 }
6664         }
6665
6666         cfclose(out);
6667         model_free_all();
6668         BAIL();
6669 }
6670
6671 DCF(pofspew, "")
6672 {
6673         game_spew_pof_info();
6674 }
6675
6676 int PASCAL WinMainSub(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
6677 {
6678         int state;              
6679
6680 #ifndef PLAT_UNIX
6681         // Don't let more than one instance of Freespace run.
6682         HWND hwnd = FindWindow( NOX( "FreeSpaceClass" ), NULL );
6683         if ( hwnd )     {
6684                 SetForegroundWindow(hwnd);
6685                 return 0;
6686         }
6687 #endif
6688
6689         // Find out how much RAM is on this machine
6690 #ifndef PLAT_UNIX
6691         MEMORYSTATUS ms;
6692         ms.dwLength = sizeof(MEMORYSTATUS);
6693         GlobalMemoryStatus(&ms);
6694         Freespace_total_ram = ms.dwTotalPhys;
6695
6696         if ( game_do_ram_check(Freespace_total_ram) == -1 ) {
6697                 return 0;
6698         }
6699
6700         if ( ms.dwTotalVirtual < 1024 ) {
6701                 MessageBox( NULL, XSTR( "FreeSpace requires virtual memory to run.\r\n", 196), XSTR( "No Virtual Memory", 197), MB_OK );
6702                 return 0;
6703         }
6704
6705         if (!vm_init(24*1024*1024)) {
6706                 MessageBox( NULL, XSTR( "Not enough memory to run Freespace.\r\nTry closing down some other applications.\r\n", 198), XSTR( "Not Enough Memory", 199), MB_OK );
6707                 return 0;
6708         }
6709                 
6710         char *tmp_mem = (char *) malloc(16 * 1024 * 1024);
6711         if (!tmp_mem) {
6712                 MessageBox(NULL, XSTR( "Not enough memory to run Freespace.\r\nTry closing down some other applications.\r\n", 198), XSTR( "Not Enough Memory", 199), MB_OK);
6713                 return 0;
6714         }
6715
6716         free(tmp_mem);
6717         tmp_mem = NULL;
6718 #else
6719         STUB_FUNCTION;
6720 #endif
6721         
6722 /* this code doesn't work, and we will hit an error about being unable to load the direct draw
6723         dll before we get here anyway if it's not installed (unless we load it manually, which doesn't
6724         seem worth bothering with.
6725
6726         LONG lResult;
6727
6728         lResult = RegOpenKeyEx(
6729                 HKEY_LOCAL_MACHINE,                                     // Where it is
6730                 "Software\\Microsoft\\DirectX", // name of key
6731                 NULL,                                                                           // DWORD reserved
6732                 KEY_QUERY_VALUE,                                                // Allows all changes
6733                 &hKey                                                                           // Location to store key
6734         );
6735
6736         if (lResult == ERROR_SUCCESS) {
6737                 char version[32];
6738                 DWORD dwType, dwLen;
6739
6740                 dwLen = 32;
6741                 lResult = RegQueryValueEx(
6742                         hKey,                                                                   // Handle to key
6743                         "Version",                                                      // The values name
6744                         NULL,                                                                   // DWORD reserved
6745                         &dwType,                                                                // What kind it is
6746                         (ubyte *) version,                              // value to set
6747                         &dwLen                                                          // How many bytes to set
6748                 );
6749
6750                 if (lResult == ERROR_SUCCESS) {
6751                         dx_version = atoi(strstr(version, ".") + 1);
6752
6753                 } else {
6754                         int val;
6755                         DWORD dwType, dwLen;
6756
6757                         dwLen = 4;
6758                         lResult = RegQueryValueEx(
6759                                 hKey,                                                                   // Handle to key
6760                                 "InstalledVersion",                             // The values name
6761                                 NULL,                                                                   // DWORD reserved
6762                                 &dwType,                                                                // What kind it is
6763                                 (ubyte *) &val,                                 // value to set
6764                                 &dwLen                                                          // How many bytes to set
6765                         );
6766
6767                         if (lResult == ERROR_SUCCESS) {
6768                                 dx_version = val;
6769                         }
6770                 }
6771
6772                 RegCloseKey(hKey);
6773         }
6774
6775         if (dx_version < 3) {
6776                 MessageBox(NULL, "DirectX 3.0 or higher is required and wasn't detected.  You can get the\n"
6777                         "latest version of DirectX at:\n\n"
6778                         "http://www.microsoft.com/msdownload/directx/dxf/enduser5.0/default.htm", "DirectX required", MB_OK);
6779
6780                 MessageBox(NULL, "DirectX 3.0 or higher is required and wasn't detected.  You can install\n"
6781                         "DirectX 5.2 by pressing the 'Install DirectX' button on the FreeSpace Launcher", "DirectX required", MB_OK);
6782
6783                 return 0;
6784         }
6785 */
6786         //=====================================================
6787         // Make sure we're running in the right directory.
6788 #ifndef PLAT_UNIX
6789         char exe_dir[1024];
6790
6791         if ( GetModuleFileName( hInst, exe_dir, 1023 ) > 0 )    {
6792                 char *p = exe_dir + strlen(exe_dir);
6793
6794                 // chop off the filename
6795                 while( (p>exe_dir) && (*p!='\\') && (*p!='/') && (*p!=':') )    {
6796                         p--;
6797                 }
6798                 *p = 0;
6799
6800                 // Set directory
6801                 if ( strlen(exe_dir) > 0 )      {
6802                         SetCurrentDirectory(exe_dir);
6803                 }
6804
6805                 // check for updated freespace.exe
6806                 game_maybe_update_launcher(exe_dir);
6807         }
6808 #else
6809         STUB_FUNCTION;
6810 #endif
6811         
6812         #ifndef NDEBUG                          
6813         {
6814                 extern void windebug_memwatch_init();
6815                 windebug_memwatch_init();
6816         }
6817         #endif
6818         
6819         parse_cmdline(szCmdLine);       
6820
6821 #ifdef STANDALONE_ONLY_BUILD
6822         Is_standalone = 1;
6823         nprintf(("Network", "Standalone running"));
6824 #else
6825         if (Is_standalone){
6826                 nprintf(("Network", "Standalone running"));
6827         }
6828 #endif
6829
6830         init_cdrom();
6831         game_init();
6832         game_stop_time();
6833
6834         // maybe spew pof stuff
6835         if(Cmdline_spew_pof_info){
6836                 game_spew_pof_info();
6837                 game_shutdown();
6838                 return 1;
6839         }
6840
6841         // non-demo, non-standalone, play the intro movie
6842         if(!Is_standalone){
6843 #ifndef DEMO
6844 #ifdef RELEASE_REAL
6845                 char *plist[5];
6846                 if( (cf_get_file_list(2, plist, CF_TYPE_MULTI_PLAYERS, NOX("*.plr"))    <= 0) && (cf_get_file_list(2, plist, CF_TYPE_SINGLE_PLAYERS, NOX("*.plr"))      <= 0) ){
6847                         // prompt for cd 2
6848 #if defined(OEM_BUILD)
6849                         game_do_cd_check_specific(FS_CDROM_VOLUME_1, 1);
6850 #else
6851                         game_do_cd_check_specific(FS_CDROM_VOLUME_2, 2);
6852 #endif // defined(OEM_BUILD)
6853                 }
6854 #endif
6855         }
6856
6857         if ( !Is_standalone ) {
6858
6859                 // release -- movies always play
6860                 #if defined(NDEBUG)
6861
6862                 // in RELEASE_REAL builds make the user stick in CD2 if there are no pilots on disk so that we guarantee he plays the movie
6863                 // no soup for you!
6864                 // movie_play( NOX("intro.mve"), 0 );
6865
6866                 // debug version, movie will only play with -showmovies
6867                 #elif !defined(NDEBUG)
6868                 
6869                 // no soup for you!
6870                 // movie_play( NOX("intro.mve"), 0);
6871 /*
6872 #ifndef NDEBUG
6873                 if ( Cmdline_show_movies )
6874                         movie_play( NOX("intro.mve"), 0 );
6875 #endif
6876 */
6877                 #endif
6878         }
6879
6880 #endif  
6881
6882         if (Is_standalone){
6883                 gameseq_post_event(GS_EVENT_STANDALONE_MAIN);
6884         } else {
6885                 gameseq_post_event(GS_EVENT_GAME_INIT);         // start the game rolling -- check for default pilot, or go to the pilot select screen
6886         }
6887
6888         while (1) {
6889                 // only important for non THREADED mode
6890                 os_poll();
6891
6892                 state = gameseq_process_events();
6893                 if ( state == GS_STATE_QUIT_GAME ){
6894                         break;
6895                 }
6896         } 
6897
6898 #ifdef FS2_DEMO
6899         if(!Is_standalone){
6900                 demo_upsell_show_screens();
6901         }
6902 #elif defined(OEM_BUILD)
6903         // show upsell screens on exit
6904         oem_upsell_show_screens();
6905 #endif
6906
6907         game_shutdown();
6908         return 1;
6909 }
6910
6911 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
6912 {
6913         int result = -1;
6914 #ifndef PLAT_UNIX
6915         __try
6916         {
6917                 result = WinMainSub(hInst, hPrev, szCmdLine, nCmdShow);
6918         }
6919         __except(RecordExceptionInfo(GetExceptionInformation(), "Freespace 2 Main Thread"))
6920         {
6921                 // Do nothing here - RecordExceptionInfo() has already done
6922                 // everything that is needed. Actually this code won't even
6923                 // get called unless you return EXCEPTION_EXECUTE_HANDLER from
6924                 // the __except clause.
6925         }
6926         return result;
6927 #else
6928         STUB_FUNCTION;
6929         
6930         fprintf(stderr, "WinMain: exceptions shall fall through\n");
6931         result = WinMainSub(hInst, hPrev, szCmdLine, nCmdShow);
6932         
6933         return result;
6934 #endif  
6935 }
6936
6937 // launcher the fslauncher program on exit
6938 void game_launch_launcher_on_exit()
6939 {
6940 #ifndef PLAT_UNIX
6941         STARTUPINFO si;
6942         PROCESS_INFORMATION pi;
6943         char cmd_line[2048];
6944         char original_path[1024] = "";
6945         
6946         memset( &si, 0, sizeof(STARTUPINFO) );
6947         si.cb = sizeof(si);
6948
6949         // directory
6950         _getcwd(original_path, 1023);
6951
6952         // set up command line
6953         strcpy(cmd_line, original_path);
6954         strcat(cmd_line, "\\");
6955         strcat(cmd_line, LAUNCHER_FNAME);
6956         strcat(cmd_line, " -straight_to_update");               
6957
6958         BOOL ret = CreateProcess(       NULL,                                                                   // pointer to name of executable module 
6959                                                                                 cmd_line,                                                       // pointer to command line string
6960                                                                                 NULL,                                                                   // pointer to process security attributes 
6961                                                                                 NULL,                                                                   // pointer to thread security attributes 
6962                                                                                 FALSE,                                                          // handle inheritance flag 
6963                                                                                 CREATE_DEFAULT_ERROR_MODE,              // creation flags 
6964                                                                                 NULL,                                                                   // pointer to new environment block 
6965                                                                                 NULL,                                                                   // pointer to current directory name 
6966                                                                                 &si,                                                                    // pointer to STARTUPINFO 
6967                                                                                 &pi                                                                     // pointer to PROCESS_INFORMATION  
6968                                                                                 );                      
6969         // to eliminate build warnings
6970         ret;
6971 #else
6972         STUB_FUNCTION;
6973 #endif          
6974 }
6975
6976
6977 // game_shutdown()
6978 //
6979 // This function is called when FreeSpace terminates normally.  
6980 //
6981 void game_shutdown(void)
6982 {
6983 #ifndef PLAT_UNIX
6984         timeEndPeriod(1);
6985 #endif
6986
6987         // don't ever flip a page on the standalone!
6988         if(!(Game_mode & GM_STANDALONE_SERVER)){
6989                 gr_reset_clip();
6990                 gr_clear();
6991                 gr_flip();
6992         }
6993
6994    // if the player has left the "player select" screen and quit the game without actually choosing
6995         // a player, Player will be NULL, in which case we shouldn't write the player file out!
6996         if (!(Game_mode & GM_STANDALONE_SERVER) && (Player!=NULL) && !Is_standalone){
6997                 write_pilot_file();
6998         }
6999
7000         // load up common multiplayer icons
7001         multi_unload_common_icons();
7002         
7003         shockwave_close();                      // release any memory used by shockwave system  
7004         fireball_close();                               // free fireball system
7005         ship_close();                                   // free any memory that was allocated for the ships
7006         hud_free_scrollback_list();// free space allocated to store hud messages in hud scrollback
7007         unload_animating_pointer();// frees the frames used for the animating mouse pointer
7008         bm_unload_all();                                // free bitmaps
7009         mission_campaign_close();       // close out the campaign stuff
7010         multi_voice_close();                    // close down multiplayer voice (including freeing buffers, etc)
7011         multi_log_close();
7012 #ifdef MULTI_USE_LAG
7013         multi_lag_close();
7014 #endif
7015
7016         // the menu close functions will unload the bitmaps if they were displayed during the game
7017 #if !defined(PRESS_TOUR_BUILD) && !defined(PD_BUILD)
7018         main_hall_close();
7019 #endif
7020         training_menu_close();
7021         gr_close();
7022
7023         extern void joy_close();
7024         joy_close();
7025
7026         audiostream_close();
7027         snd_close();
7028         event_music_close();
7029         psnet_close();
7030         os_cleanup();
7031
7032         // HACKITY HACK HACK
7033         // if this flag is set, we should be firing up the launcher when exiting freespace
7034         extern int Multi_update_fireup_launcher_on_exit;
7035         if(Multi_update_fireup_launcher_on_exit){
7036                 game_launch_launcher_on_exit();
7037         }
7038 }
7039
7040 // game_stop_looped_sounds()
7041 //
7042 // This function will call the appropriate stop looped sound functions for those
7043 // modules which use looping sounds.  It is not enough just to stop a looping sound
7044 // at the DirectSound level, the game is keeping track of looping sounds, and this
7045 // function is used to inform the game that looping sounds are being halted.
7046 //
7047 void game_stop_looped_sounds()
7048 {
7049         hud_stop_looped_locking_sounds();
7050         hud_stop_looped_engine_sounds();
7051         afterburner_stop_sounds();
7052         player_stop_looped_sounds();
7053         obj_snd_stop_all();             // stop all object-linked persistant sounds
7054         game_stop_subspace_ambient_sound();
7055         snd_stop(Radar_static_looping);
7056         Radar_static_looping = -1;
7057         snd_stop(Target_static_looping);
7058         shipfx_stop_engine_wash_sound();
7059         Target_static_looping = -1;
7060 }
7061
7062 //////////////////////////////////////////////////////////////////////////
7063 //
7064 // Code for supporting an animating mouse pointer
7065 //
7066 //
7067 //////////////////////////////////////////////////////////////////////////
7068
7069 typedef struct animating_obj
7070 {
7071         int     first_frame;
7072         int     num_frames;
7073         int     current_frame;
7074         float time;
7075         float elapsed_time;
7076 } animating_obj;
7077
7078 static animating_obj Animating_mouse;
7079
7080 // ----------------------------------------------------------------------------
7081 // init_animating_pointer()
7082 //
7083 // Called by load_animating_pointer() to ensure the Animating_mouse struct
7084 // gets properly initialized
7085 //
7086 void init_animating_pointer()
7087 {
7088         Animating_mouse.first_frame     = -1;
7089         Animating_mouse.num_frames              = 0;
7090         Animating_mouse.current_frame   = -1;
7091         Animating_mouse.time                            = 0.0f;
7092         Animating_mouse.elapsed_time    = 0.0f;
7093 }
7094
7095 // ----------------------------------------------------------------------------
7096 // load_animating_pointer()
7097 //
7098 // Called at game init to load in the frames for the animating mouse pointer
7099 //
7100 // input:       filename        =>      filename of animation file that holds the animation
7101 // 
7102 void load_animating_pointer(char *filename, int dx, int dy)
7103 {
7104         int                             fps;
7105         animating_obj *am;
7106
7107         init_animating_pointer();
7108
7109         am = &Animating_mouse;
7110         am->first_frame = bm_load_animation(filename, &am->num_frames, &fps);
7111         if ( am->first_frame == -1 ) 
7112                 Error(LOCATION, "Could not load animation %s for the mouse pointer\n", filename);
7113         am->current_frame = 0;
7114         am->time = am->num_frames / i2fl(fps);
7115 }
7116
7117 // ----------------------------------------------------------------------------
7118 // unload_animating_pointer()
7119 //
7120 // Called at game shutdown to free the memory used to store the animation frames
7121 //
7122 void unload_animating_pointer()
7123 {
7124         int                             i;
7125         animating_obj   *am;
7126
7127         am = &Animating_mouse;
7128         for ( i = 0; i < am->num_frames; i++ ) {
7129                 Assert( (am->first_frame+i) >= 0 );
7130                 bm_release(am->first_frame + i);
7131         }
7132
7133         am->first_frame = -1;
7134         am->num_frames          = 0;
7135         am->current_frame = -1;
7136 }
7137
7138 // draw the correct frame of the game mouse... called from game_maybe_draw_mouse()
7139 void game_render_mouse(float frametime)
7140 {
7141         int                             mx, my;
7142         animating_obj   *am;
7143
7144         // if animating cursor exists, play the next frame
7145         am = &Animating_mouse;
7146         if ( am->first_frame != -1 ) {
7147                 mouse_get_pos(&mx, &my);
7148                 am->elapsed_time += frametime;
7149                 am->current_frame = fl2i( ( am->elapsed_time / am->time ) * (am->num_frames-1) );
7150                 if ( am->current_frame >= am->num_frames ) {
7151                         am->current_frame = 0;
7152                         am->elapsed_time = 0.0f;
7153                 }
7154                 gr_set_cursor_bitmap(am->first_frame + am->current_frame);
7155         }
7156 }
7157
7158 // ----------------------------------------------------------------------------
7159 // game_maybe_draw_mouse()
7160 //
7161 // determines whether to draw the mouse pointer at all, and what frame of
7162 // animation to use if the mouse is animating
7163 //
7164 // Sets mouse.cpp globals Mouse_hidden and Mouse_moved based on the state of the game.
7165 //
7166 // input:       frametime => elapsed frame time in seconds since last call
7167 //
7168 void game_maybe_draw_mouse(float frametime)
7169 {
7170         int game_state;
7171
7172         game_state = gameseq_get_state();
7173
7174         switch ( game_state ) {
7175                 case GS_STATE_GAME_PAUSED:
7176                 // case GS_STATE_MULTI_PAUSED:
7177                 case GS_STATE_GAME_PLAY:
7178                 case GS_STATE_DEATH_DIED:
7179                 case GS_STATE_DEATH_BLEW_UP:
7180                         if ( popup_active() || popupdead_is_active() ) {
7181                                 Mouse_hidden = 0;
7182                         } else {
7183                                 Mouse_hidden = 1;       
7184                         }
7185                         break;
7186
7187                 default:
7188                         Mouse_hidden = 0;
7189                         break;
7190         }       // end switch
7191
7192         if ( !Mouse_hidden ) 
7193                 game_render_mouse(frametime);
7194
7195 }
7196
7197 void game_do_training_checks()
7198 {
7199         int i, s;
7200         float d;
7201         waypoint_list *wplp;
7202
7203         if (Training_context & TRAINING_CONTEXT_SPEED) {
7204                 s = (int) Player_obj->phys_info.fspeed;
7205                 if ((s >= Training_context_speed_min) && (s <= Training_context_speed_max)) {
7206                         if (!Training_context_speed_set) {
7207                                 Training_context_speed_set = 1;
7208                                 Training_context_speed_timestamp = timestamp();
7209                         }
7210
7211                 } else
7212                         Training_context_speed_set = 0;
7213         }
7214
7215         if (Training_context & TRAINING_CONTEXT_FLY_PATH) {
7216                 wplp = &Waypoint_lists[Training_context_path];
7217                 if (wplp->count > Training_context_goal_waypoint) {
7218                         i = Training_context_goal_waypoint;
7219                         do {
7220                                 d = vm_vec_dist(&wplp->waypoints[i], &Player_obj->pos);
7221                                 if (d <= Training_context_distance) {
7222                                         Training_context_at_waypoint = i;
7223                                         if (Training_context_goal_waypoint == i) {
7224                                                 Training_context_goal_waypoint++;
7225                                                 snd_play(&Snds[SND_CARGO_REVEAL], 0.0f);
7226                                         }
7227
7228                                         break;
7229                                 }
7230
7231                                 i++;
7232                                 if (i == wplp->count)
7233                                         i = 0;
7234
7235                         } while (i != Training_context_goal_waypoint);
7236                 }
7237         }
7238
7239         if ((Players_target == UNINITIALIZED) || (Player_ai->target_objnum != Players_target) || (Player_ai->targeted_subsys != Players_targeted_subsys)) {
7240                 Players_target = Player_ai->target_objnum;
7241                 Players_targeted_subsys = Player_ai->targeted_subsys;
7242                 Players_target_timestamp = timestamp();
7243         }
7244 }
7245
7246 /////////// Following is for event debug view screen
7247
7248 #ifndef NDEBUG
7249
7250 #define EVENT_DEBUG_MAX 5000
7251 #define EVENT_DEBUG_EVENT 0x8000
7252
7253 int Event_debug_index[EVENT_DEBUG_MAX];
7254 int ED_count;
7255
7256 void game_add_event_debug_index(int n, int indent)
7257 {
7258         if (ED_count < EVENT_DEBUG_MAX)
7259                 Event_debug_index[ED_count++] = n | (indent << 16);
7260 }
7261
7262 void game_add_event_debug_sexp(int n, int indent)
7263 {
7264         if (n < 0)
7265                 return;
7266
7267         if (Sexp_nodes[n].first >= 0) {
7268                 game_add_event_debug_sexp(Sexp_nodes[n].first, indent);
7269                 game_add_event_debug_sexp(Sexp_nodes[n].rest, indent);
7270                 return;
7271         }
7272
7273         game_add_event_debug_index(n, indent);
7274         if (Sexp_nodes[n].subtype == SEXP_ATOM_OPERATOR)
7275                 game_add_event_debug_sexp(Sexp_nodes[n].rest, indent + 1);
7276         else
7277                 game_add_event_debug_sexp(Sexp_nodes[n].rest, indent);
7278 }
7279
7280 void game_event_debug_init()
7281 {
7282         int e;
7283
7284         ED_count = 0;
7285         for (e=0; e<Num_mission_events; e++) {
7286                 game_add_event_debug_index(e | EVENT_DEBUG_EVENT, 0);
7287                 game_add_event_debug_sexp(Mission_events[e].formula, 1);
7288         }
7289 }
7290
7291 void game_show_event_debug(float frametime) 
7292 {
7293         char buf[256];
7294         int i, k, z;
7295         int font_height, font_width;    
7296         int y_index, y_max;
7297         static int scroll_offset = 0;
7298         
7299         k = game_check_key();
7300         if (k)
7301                 switch (k) {
7302                         case KEY_UP:
7303                         case KEY_PAD8:
7304                                 scroll_offset--;
7305                                 if (scroll_offset < 0)
7306                                         scroll_offset = 0;
7307                                 break;
7308
7309                         case KEY_DOWN:
7310                         case KEY_PAD2:
7311                                 scroll_offset++;
7312                                 break;
7313
7314                         case KEY_PAGEUP:
7315                                 scroll_offset -= 20;
7316                                 if (scroll_offset < 0)
7317                                         scroll_offset = 0;
7318                                 break;
7319
7320                         case KEY_PAGEDOWN:
7321                                 scroll_offset += 20;    // not font-independent, hard-coded since I counted the lines!
7322                                 break;
7323
7324                         default:
7325                                 gameseq_post_event(GS_EVENT_PREVIOUS_STATE);
7326                                 key_flush();
7327                                 break;
7328                 } // end switch
7329
7330         gr_clear();
7331         gr_set_color_fast(&Color_bright);
7332         gr_set_font(FONT1);
7333         gr_printf(0x8000, 5, NOX("EVENT DEBUG VIEW"));
7334
7335         gr_set_color_fast(&Color_normal);
7336         gr_set_font(FONT1);
7337         gr_get_string_size(&font_width, &font_height, NOX("test"));
7338         y_max = gr_screen.max_h - font_height - 5;
7339         y_index = 45;
7340
7341         k = scroll_offset;
7342         while (k < ED_count) {
7343                 if (y_index > y_max)
7344                         break;
7345
7346                 z = Event_debug_index[k];
7347                 if (z & EVENT_DEBUG_EVENT) {
7348                         z &= 0x7fff;
7349                         sprintf(buf, NOX("%s%s (%s) %s%d %d"), (Mission_events[z].flags & MEF_CURRENT) ? NOX("* ") : "",
7350                                 Mission_events[z].name, Mission_events[z].result ? NOX("True") : NOX("False"),
7351                                 (Mission_events[z].chain_delay < 0) ? "" : NOX("x "),
7352                                 Mission_events[z].repeat_count, Mission_events[z].interval);
7353
7354                 } else {
7355                         i = (z >> 16) * 3;
7356                         buf[i] = 0;
7357                         while (i--)
7358                                 buf[i] = ' ';
7359
7360                         strcat(buf, Sexp_nodes[z & 0x7fff].text);
7361                         switch (Sexp_nodes[z & 0x7fff].value) {
7362                                 case SEXP_TRUE:
7363                                         strcat(buf, NOX(" (True)"));
7364                                         break;
7365
7366                                 case SEXP_FALSE:
7367                                         strcat(buf, NOX(" (False)"));
7368                                         break;
7369
7370                                 case SEXP_KNOWN_TRUE:
7371                                         strcat(buf, NOX(" (Always true)"));
7372                                         break;
7373
7374                                 case SEXP_KNOWN_FALSE:
7375                                         strcat(buf, NOX(" (Always false)"));
7376                                         break;
7377
7378                                 case SEXP_CANT_EVAL:
7379                                         strcat(buf, NOX(" (Can't eval)"));
7380                                         break;
7381
7382                                 case SEXP_NAN:
7383                                 case SEXP_NAN_FOREVER:
7384                                         strcat(buf, NOX(" (Not a number)"));
7385                                         break;
7386                         }
7387                 }
7388
7389                 gr_printf(10, y_index, buf);
7390                 y_index += font_height;
7391                 k++;
7392         }
7393
7394         gr_flip();
7395 }
7396
7397 #endif // NDEBUG
7398
7399 #ifndef NDEBUG
7400 FILE * Time_fp;
7401 FILE * Texture_fp;
7402
7403 extern int Tmap_npixels;
7404
7405 int Tmap_num_too_big = 0;
7406 int Num_models_needing_splitting = 0;
7407
7408 void Time_model( int modelnum )
7409 {
7410 //      mprintf(( "Timing ship '%s'\n", si->name ));
7411
7412         vector eye_pos, model_pos;
7413         matrix eye_orient, model_orient;
7414
7415         polymodel *pm = model_get( modelnum );
7416
7417         int l = strlen(pm->filename);
7418         while( (l>0) )  {
7419                 if ( (l == '/') || (l=='\\') || (l==':'))       {
7420                         l++;
7421                         break;
7422                 }
7423                 l--;
7424         }
7425         char *pof_file = &pm->filename[l];
7426
7427         int model_needs_splitting = 0;
7428
7429         //fprintf( Texture_fp, "Model: %s\n", pof_file );
7430         int i;
7431         for (i=0; i<pm->n_textures; i++ )       {
7432                 char filename[1024];
7433                 ubyte pal[768];
7434
7435                 int bmp_num = pm->original_textures[i];
7436                 if ( bmp_num > -1 )     {
7437                         bm_get_palette(pm->original_textures[i], pal, filename );               
7438                         int w,h;
7439                         bm_get_info( pm->original_textures[i],&w, &h );
7440
7441
7442                         if ( (w > 512) || (h > 512) )   {
7443                                 fprintf( Texture_fp, "%s\t%s\t%d\t%d\n", pof_file, filename, w, h );
7444                                 Tmap_num_too_big++;
7445                                 model_needs_splitting++;
7446                         }
7447                 } else {
7448                         //fprintf( Texture_fp, "\tTexture %d is bogus\n", i );
7449                 }
7450         }
7451
7452         if ( model_needs_splitting )    {
7453                 Num_models_needing_splitting++;
7454         }
7455         eye_orient = model_orient = vmd_identity_matrix;
7456         eye_pos = model_pos = vmd_zero_vector;
7457
7458         eye_pos.z = -pm->rad*2.0f;
7459
7460         vector eye_to_model;
7461
7462         vm_vec_sub( &eye_to_model, &model_pos, &eye_pos );
7463         vm_vector_2_matrix( &eye_orient, &eye_to_model, NULL, NULL );
7464
7465         fix t1 = timer_get_fixed_seconds();
7466
7467         angles ta;
7468         ta.p = ta.b = ta.h = 0.0f; 
7469         int framecount = 0;
7470
7471         Tmap_npixels = 0;
7472
7473         int bitmaps_used_this_frame, bitmaps_new_this_frame;
7474                 
7475         bm_get_frame_usage(&bitmaps_used_this_frame,&bitmaps_new_this_frame);
7476
7477         modelstats_num_polys = modelstats_num_verts = 0;
7478
7479         while( ta.h < PI2 )     {
7480
7481                 matrix m1;
7482                 vm_angles_2_matrix(&m1, &ta );
7483                 vm_matrix_x_matrix( &model_orient, &vmd_identity_matrix, &m1 );
7484
7485                 gr_reset_clip();
7486 //              gr_clear();
7487
7488                 g3_start_frame(1);
7489
7490                 g3_set_view_matrix( &eye_pos, &eye_orient, Viewer_zoom );       
7491
7492                 model_clear_instance( modelnum );
7493                 model_set_detail_level(0);              // use highest detail level
7494                 model_render( modelnum, &model_orient, &model_pos, MR_LOCK_DETAIL);     //|MR_NO_POLYS );
7495
7496                 g3_end_frame();
7497 //              gr_flip();
7498
7499                 framecount++;
7500                 ta.h += 0.1f;
7501
7502                 int k = key_inkey();
7503                 if ( k == KEY_ESC ) {
7504                         exit(1);
7505                 }
7506         }
7507
7508         fix t2 = timer_get_fixed_seconds();
7509
7510         bm_get_frame_usage(&bitmaps_used_this_frame,&bitmaps_new_this_frame);
7511         //bitmaps_used_this_frame /= framecount;
7512
7513         modelstats_num_polys /= framecount;
7514         modelstats_num_verts /= framecount;
7515
7516         Tmap_npixels /=framecount;
7517
7518
7519         mprintf(( "'%s' is %.2f FPS\n", pof_file, i2fl(framecount)/f2fl(t2-t1) ));
7520         fprintf( Time_fp, "\"%s\"\t%.0f\t%d\t%d\t%d\t%d\n", pof_file, i2fl(framecount)/f2fl(t2-t1), bitmaps_used_this_frame, modelstats_num_polys, modelstats_num_verts, Tmap_npixels );
7521 //      fprintf( Time_fp, "%.0f\t%d\t%d\t%d\t%d\n", i2fl(framecount)/f2fl(t2-t1), bitmaps_used_this_frame, modelstats_num_polys, modelstats_num_verts, Tmap_npixels );
7522
7523                 
7524 //      key_getch();
7525 }
7526
7527 int Time_models = 0;
7528 DCF_BOOL( time_models, Time_models );
7529
7530 void Do_model_timings_test()
7531 {
7532         
7533
7534         if ( !Time_models ) return;
7535
7536         mprintf(( "Timing models!\n" ));
7537
7538         int i;
7539
7540         ubyte model_used[MAX_POLYGON_MODELS];
7541         int model_id[MAX_POLYGON_MODELS];
7542         for (i=0; i<MAX_POLYGON_MODELS; i++ )   {
7543                 model_used[i] = 0;
7544         }
7545         
7546         // Load them all
7547         for (i=0; i<Num_ship_types; i++ )       {
7548                 Ship_info[i].modelnum = model_load( Ship_info[i].pof_file, 0, NULL );
7549
7550                 model_used[Ship_info[i].modelnum%MAX_POLYGON_MODELS]++;
7551                 model_id[Ship_info[i].modelnum%MAX_POLYGON_MODELS] = Ship_info[i].modelnum;
7552         }
7553
7554         Texture_fp = fopen( NOX("ShipTextures.txt"), "wt" );
7555         if ( !Texture_fp ) return;
7556
7557         Time_fp = fopen( NOX("ShipTimings.txt"), "wt" );
7558         if ( !Time_fp ) return;
7559
7560         fprintf( Time_fp, "Name\tFPS\tTRAM\tPolys\tVerts\tPixels\n" );
7561 //      fprintf( Time_fp, "FPS\tTRAM\tPolys\tVerts\tPixels\n" );
7562         
7563         for (i=0; i<MAX_POLYGON_MODELS; i++ )   {
7564                 if ( model_used[i] )    {
7565                         Time_model( model_id[i] );
7566                 }
7567         }
7568         
7569         fprintf( Texture_fp, "Number too big: %d\n", Tmap_num_too_big );
7570         fprintf( Texture_fp, "Number of models needing splitting: %d\n", Num_models_needing_splitting );
7571         
7572         fclose(Time_fp);
7573         fclose(Texture_fp);
7574
7575         exit(1);
7576 }
7577 #endif
7578
7579 // Call this function when you want to inform the player that a feature is not
7580 // enabled in the DEMO version of FreSpace
7581 void game_feature_not_in_demo_popup()
7582 {
7583         popup(PF_USE_AFFIRMATIVE_ICON|PF_BODY_BIG, 1, POPUP_OK, XSTR( "Sorry, this feature is available only in the retail version", 200));
7584 }
7585
7586 // format the specified time (fixed point) into a nice string
7587 void game_format_time(fix m_time,char *time_str)
7588 {
7589         float mtime;
7590         int hours,minutes,seconds;
7591         char tmp[10];
7592
7593         mtime = f2fl(m_time);           
7594
7595         // get the hours, minutes and seconds   
7596         hours = (int)(mtime / 3600.0f);
7597         if(hours > 0){
7598                 mtime -= (3600.0f * (float)hours);
7599         }
7600         seconds = (int)mtime%60;
7601         minutes = (int)mtime/60;                        
7602
7603         // print the hour if necessary
7604         if(hours > 0){          
7605                 sprintf(time_str,XSTR( "%d:", 201),hours);
7606                 // if there are less than 10 minutes, print a leading 0
7607                 if(minutes < 10){
7608                         strcpy(tmp,NOX("0"));
7609                         strcat(time_str,tmp);
7610                 }               
7611         }       
7612         
7613         // print the minutes
7614         if(hours){
7615                 sprintf(tmp,XSTR( "%d:", 201),minutes);
7616                 strcat(time_str,tmp);
7617         } else {
7618                 sprintf(time_str,XSTR( "%d:", 201),minutes);
7619         }
7620
7621         // print the seconds
7622         if(seconds < 10){
7623                 strcpy(tmp,NOX("0"));
7624                 strcat(time_str,tmp);
7625         } 
7626         sprintf(tmp,"%d",seconds);
7627         strcat(time_str,tmp);
7628 }
7629
7630 //      Stuff version string in *str.
7631 void get_version_string(char *str)
7632 {
7633 //XSTR:OFF
7634 if ( FS_VERSION_BUILD == 0 ) {
7635         sprintf(str,"v%d.%02d",FS_VERSION_MAJOR, FS_VERSION_MINOR);
7636 } else {
7637         sprintf(str,"v%d.%02d.%02d",FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD );
7638 }
7639
7640 #if defined (FS2_DEMO)
7641         strcat(str, " D");
7642 #elif defined (OEM_BUILD)
7643         strcat(str, " (OEM)");
7644 #endif
7645 //XSTR:ON
7646         /*
7647         HMODULE hMod;
7648         DWORD bogus_handle;
7649         char myname[_MAX_PATH];
7650         int namelen, major, minor, build, waste;
7651         unsigned int buf_size;
7652         DWORD version_size;
7653         char *infop;
7654         VOID *bufp;
7655         BOOL result;
7656
7657         // Find my EXE file name
7658         hMod = GetModuleHandle(NULL);
7659         namelen = GetModuleFileName( hMod, myname, _MAX_PATH );
7660
7661         version_size = GetFileVersionInfoSize(myname, &bogus_handle );
7662         infop = (char *)malloc(version_size);
7663         result = GetFileVersionInfo( myname, 0, version_size, (LPVOID)infop );
7664
7665         // get the product version
7666         result = VerQueryValue((LPVOID)infop, TEXT("\\StringFileInfo\\040904b0\\ProductVersion"), &bufp, &buf_size );
7667         sscanf( (char *)bufp, "%d, %d, %d, %d", &major, &minor, &build, &waste );
7668 #ifdef DEMO
7669         sprintf(str,"Dv%d.%02d",major, minor);
7670 #else
7671         sprintf(str,"v%d.%02d",major, minor);
7672 #endif
7673         */
7674 }
7675
7676 void get_version_string_short(char *str)
7677 {
7678         sprintf(str,"v%d.%02d",FS_VERSION_MAJOR, FS_VERSION_MINOR);
7679 }
7680
7681 // ----------------------------------------------------------------
7682 //
7683 // OEM UPSELL SCREENS BEGIN
7684 //
7685 // ----------------------------------------------------------------
7686 #if defined(OEM_BUILD)
7687
7688 #define NUM_OEM_UPSELL_SCREENS                          3
7689 #define OEM_UPSELL_SCREEN_DELAY                         10000
7690
7691 static int Oem_upsell_bitmaps_loaded = 0;
7692 static int Oem_upsell_bitmaps[GR_NUM_RESOLUTIONS][NUM_OEM_UPSELL_SCREENS];
7693 static int Oem_upsell_screen_number = 0;
7694 static int Oem_upsell_show_next_bitmap_time;
7695
7696 //XSTR:OFF
7697 static char *Oem_upsell_bitmap_filenames[GR_NUM_RESOLUTIONS][NUM_OEM_UPSELL_SCREENS] = 
7698 {
7699         {       "OEMUpSell02",
7700                 "OEMUpSell01",
7701                 "OEMUpSell03",
7702         },
7703         {       "2_OEMUpSell02",
7704                 "2_OEMUpSell01",
7705                 "2_OEMUpSell03",
7706         },
7707 };
7708 //XSTR:ON
7709
7710 static int Oem_normal_cursor = -1;
7711 static int Oem_web_cursor = -1;
7712 //#define OEM_UPSELL_URL                "http://www.interplay-store.com/"
7713 #define OEM_UPSELL_URL          "http://www.interplay.com/cgi-bin/oemlinks.pl/pid=483421&cid=18384"
7714
7715 void oem_upsell_next_screen()
7716 {
7717         Oem_upsell_screen_number++;
7718         if ( Oem_upsell_screen_number == (NUM_OEM_UPSELL_SCREENS-1) ) {
7719                 // extra long delay, mouse shown on last upsell
7720                 Oem_upsell_show_next_bitmap_time = timer_get_milliseconds() + OEM_UPSELL_SCREEN_DELAY*2;
7721                 Mouse_hidden = 0;
7722
7723         } else {
7724                 Oem_upsell_show_next_bitmap_time = timer_get_milliseconds() + OEM_UPSELL_SCREEN_DELAY;
7725         }
7726 }
7727
7728 void oem_upsell_load_bitmaps()
7729 {
7730         int i;
7731
7732         for ( i = 0; i < NUM_OEM_UPSELL_SCREENS; i++ ) {
7733                 Oem_upsell_bitmaps[gr_screen.res][i] = bm_load(Oem_upsell_bitmap_filenames[gr_screen.res][i]);
7734         }
7735 }
7736
7737 void oem_upsell_unload_bitmaps()
7738 {
7739         int i;
7740
7741         for ( i = 0; i < NUM_OEM_UPSELL_SCREENS; i++ ) {
7742                 if(Oem_upsell_bitmaps[gr_screen.res][i] >= 0){
7743                         bm_unload(Oem_upsell_bitmaps[gr_screen.res][i]);
7744                 }
7745         }
7746
7747         // unloaded
7748         Oem_upsell_bitmaps_loaded = 0;
7749 }
7750
7751 // clickable hotspot on 3rd OEM upsell screen
7752 static int Oem_upsell3_button_coords[GR_NUM_RESOLUTIONS][4] = {
7753         {       // GR_640
7754                 28, 350, 287, 96                                        // x, y, w, h
7755         },
7756         {       // GR_1024
7757                 45, 561, 460, 152                                       // x, y, w, h
7758         }
7759 };
7760
7761 void oem_upsell_show_screens()
7762 {
7763         int current_time, k;
7764         int done = 0;
7765
7766         if ( !Oem_upsell_bitmaps_loaded ) {
7767                 oem_upsell_load_bitmaps();
7768                 Oem_upsell_bitmaps_loaded = 1;
7769         }
7770
7771         // may use upsell screens more than once
7772         Oem_upsell_show_next_bitmap_time = timer_get_milliseconds() + OEM_UPSELL_SCREEN_DELAY;
7773         Oem_upsell_screen_number = 0;
7774         
7775         key_flush();
7776         Mouse_hidden = 1;
7777
7778         // set up cursors
7779         int nframes;                                            // used to pass, not really needed (should be 1)
7780         Oem_normal_cursor = gr_get_cursor_bitmap();
7781         Oem_web_cursor = bm_load_animation("cursorweb", &nframes);
7782         Assert(Oem_web_cursor >= 0);
7783         if (Oem_web_cursor < 0) {
7784                 Oem_web_cursor = Oem_normal_cursor;
7785         }
7786
7787         while(!done) {
7788
7789                 //oem_reset_trailer_timer();
7790
7791                 current_time = timer_get_milliseconds();
7792
7793                 os_poll();
7794                 k = key_inkey();
7795
7796                 // advance screen on keypress or timeout
7797                 if (( k > 0 ) || (mouse_up_count(MOUSE_LEFT_BUTTON) > 0) || (current_time > Oem_upsell_show_next_bitmap_time)) {
7798                         oem_upsell_next_screen();
7799                 }
7800
7801                 // check if we are done
7802                 if ( Oem_upsell_screen_number >= NUM_OEM_UPSELL_SCREENS ) {
7803                         Oem_upsell_screen_number--;
7804                         done = 1;
7805                 } else {
7806                         if ( Oem_upsell_bitmaps[gr_screen.res][Oem_upsell_screen_number] < 0 ) {
7807                                 done = 1;
7808                         }
7809                 }
7810
7811                 // show me the upsell
7812                 if ( Oem_upsell_bitmaps[gr_screen.res][Oem_upsell_screen_number] >= 0 ) {               
7813                         gr_set_bitmap(Oem_upsell_bitmaps[gr_screen.res][Oem_upsell_screen_number]);
7814                         gr_bitmap(0,0);
7815                 }
7816
7817                 // if this is the 3rd upsell, make it clickable, d00d
7818                 if ( Oem_upsell_screen_number == NUM_OEM_UPSELL_SCREENS-1 ) {
7819                         int mx, my;
7820                         int button_state = mouse_get_pos(&mx, &my);
7821                         if ( (mx >= Oem_upsell3_button_coords[gr_screen.res][0]) && (mx <= Oem_upsell3_button_coords[gr_screen.res][0] + Oem_upsell3_button_coords[gr_screen.res][2])
7822                                 && (my >= Oem_upsell3_button_coords[gr_screen.res][1]) && (my <= Oem_upsell3_button_coords[gr_screen.res][1] + Oem_upsell3_button_coords[gr_screen.res][3]) )
7823                         {
7824                                 // switch cursors
7825                                 gr_set_cursor_bitmap(Oem_web_cursor); //, GR_CURSOR_LOCK);
7826
7827                                 // check for clicks
7828                                 if (button_state & MOUSE_LEFT_BUTTON) {
7829                                         // launch URL
7830                                         multi_pxo_url(OEM_UPSELL_URL);
7831                                         done = 1;
7832                                 } 
7833                         } else {
7834                                 // switch cursor back to normal one
7835                                 gr_set_cursor_bitmap(Oem_normal_cursor); //, GR_CURSOR_UNLOCK);
7836                         }
7837                 }
7838
7839                 if ( done ) {
7840                         if (gameseq_get_state() != GS_STATE_END_DEMO) {
7841                                 gr_fade_out(0);
7842                                 Sleep(300);
7843                         }
7844                 }
7845
7846                 gr_flip();
7847         }
7848
7849         // unload bitmap
7850         oem_upsell_unload_bitmaps();
7851
7852         // switch cursor back to normal one
7853         gr_set_cursor_bitmap(Oem_normal_cursor); //, GR_CURSOR_UNLOCK);
7854
7855 }
7856
7857 #endif // defined(OEM_BUILD)
7858 // ----------------------------------------------------------------
7859 //
7860 // OEM UPSELL SCREENS END
7861 //
7862 // ----------------------------------------------------------------
7863
7864
7865
7866 // ----------------------------------------------------------------
7867 //
7868 // DEMO UPSELL SCREENS BEGIN
7869 //
7870 // ----------------------------------------------------------------
7871
7872 #ifdef FS2_DEMO
7873
7874 //#define NUM_DEMO_UPSELL_SCREENS                               4
7875
7876 #define NUM_DEMO_UPSELL_SCREENS                         2
7877 #define DEMO_UPSELL_SCREEN_DELAY                                3000
7878
7879 static int Demo_upsell_bitmaps_loaded = 0;
7880 static int Demo_upsell_bitmaps[GR_NUM_RESOLUTIONS][NUM_DEMO_UPSELL_SCREENS];
7881 static int Demo_upsell_screen_number = 0;
7882 static int Demo_upsell_show_next_bitmap_time;
7883
7884 //XSTR:OFF
7885 static char *Demo_upsell_bitmap_filenames[GR_NUM_RESOLUTIONS][NUM_DEMO_UPSELL_SCREENS] = 
7886 {
7887         {       "UpSell02",
7888                 "UpSell01",
7889         },
7890         {       "2_UpSell02",
7891                 "2_UpSell01",
7892         },
7893         // "DemoUpsell3",
7894         // "DemoUpsell4",
7895 };
7896 //XSTR:ON
7897
7898 void demo_upsell_next_screen()
7899 {
7900         Demo_upsell_screen_number++;
7901         if ( Demo_upsell_screen_number == (NUM_DEMO_UPSELL_SCREENS-1) ) {
7902                 Demo_upsell_show_next_bitmap_time = timer_get_milliseconds() + DEMO_UPSELL_SCREEN_DELAY*4;
7903         } else {
7904                 Demo_upsell_show_next_bitmap_time = timer_get_milliseconds() + DEMO_UPSELL_SCREEN_DELAY;
7905         }
7906
7907         /*
7908         if ( Demo_upsell_screen_number < NUM_DEMO_UPSELL_SCREENS ) {
7909                 if ( Demo_upsell_bitmap_filenames[gr_screen.res][Demo_upsell_screen_number] >= 0 ) {
7910 #ifndef HARDWARE_ONLY
7911                         palette_use_bm_palette(Demo_upsell_bitmaps[gr_screen.res][Demo_upsell_screen_number]);
7912 #endif
7913                 }
7914         }
7915         */
7916 }
7917
7918 void demo_upsell_load_bitmaps()
7919 {
7920         int i;
7921
7922         for ( i = 0; i < NUM_DEMO_UPSELL_SCREENS; i++ ) {
7923                 Demo_upsell_bitmaps[gr_screen.res][i] = bm_load(Demo_upsell_bitmap_filenames[gr_screen.res][i]);
7924         }
7925 }
7926
7927 void demo_upsell_unload_bitmaps()
7928 {
7929         int i;
7930
7931         for ( i = 0; i < NUM_DEMO_UPSELL_SCREENS; i++ ) {
7932                 if(Demo_upsell_bitmaps[gr_screen.res][i] >= 0){
7933                         bm_unload(Demo_upsell_bitmaps[gr_screen.res][i]);
7934                 }
7935         }
7936
7937         // unloaded
7938         Demo_upsell_bitmaps_loaded = 0;
7939 }
7940
7941 void demo_upsell_show_screens()
7942 {
7943         int current_time, k;
7944         int done = 0;
7945
7946         if ( !Demo_upsell_bitmaps_loaded ) {
7947                 demo_upsell_load_bitmaps();
7948                 Demo_upsell_bitmaps_loaded = 1;
7949         }
7950
7951         // may use upsell screens more than once
7952         Demo_upsell_show_next_bitmap_time = timer_get_milliseconds() + DEMO_UPSELL_SCREEN_DELAY;
7953         Demo_upsell_screen_number = 0;
7954         
7955         key_flush();
7956         Mouse_hidden = 1;
7957
7958         while(!done) {
7959
7960                 demo_reset_trailer_timer();
7961
7962                 current_time = timer_get_milliseconds();
7963
7964 // #ifndef THREADED
7965                 os_poll();
7966 // #endif
7967                 k = key_inkey();
7968
7969                 // don't time out, wait for keypress
7970                 /*
7971                 if ( current_time > Demo_upsell_show_next_bitmap_time ) {
7972                         demo_upsell_next_screen();
7973                         k = 0;
7974                 }*/
7975
7976                 if ( k > 0 ) {
7977                         demo_upsell_next_screen();
7978                 }
7979
7980                 if ( Demo_upsell_screen_number >= NUM_DEMO_UPSELL_SCREENS ) {
7981                         Demo_upsell_screen_number--;
7982                         done = 1;
7983                 } else {
7984                         if ( Demo_upsell_bitmaps[gr_screen.res][Demo_upsell_screen_number] < 0 ) {
7985                                 done = 1;
7986                         }
7987                 }
7988
7989                 if ( Demo_upsell_bitmaps[gr_screen.res][Demo_upsell_screen_number] >= 0 ) {             
7990                         gr_set_bitmap(Demo_upsell_bitmaps[gr_screen.res][Demo_upsell_screen_number]);
7991                         gr_bitmap(0,0);
7992                 }
7993
7994                 if ( done ) {
7995                         if (gameseq_get_state() != GS_STATE_END_DEMO) {
7996                                 gr_fade_out(0);
7997                                 Sleep(300);
7998                         }
7999                 }
8000
8001                 gr_flip();
8002         }
8003
8004         // unload bitmap
8005         demo_upsell_unload_bitmaps();
8006 }
8007
8008 #endif // DEMO
8009
8010 // ----------------------------------------------------------------
8011 //
8012 // DEMO UPSELL SCREENS END
8013 //
8014 // ----------------------------------------------------------------
8015
8016
8017 // ----------------------------------------------------------------
8018 //
8019 // Subspace Ambient Sound START
8020 //
8021 // ----------------------------------------------------------------
8022
8023 static int Subspace_ambient_left_channel = -1;
8024 static int Subspace_ambient_right_channel = -1;
8025
8026 // 
8027 void game_start_subspace_ambient_sound()
8028 {
8029         if ( Subspace_ambient_left_channel < 0 ) {
8030                 Subspace_ambient_left_channel = snd_play_looping(&Snds[SND_SUBSPACE_LEFT_CHANNEL], -1.0f);
8031         }
8032
8033         if ( Subspace_ambient_right_channel < 0 ) {
8034                 Subspace_ambient_right_channel = snd_play_looping(&Snds[SND_SUBSPACE_RIGHT_CHANNEL], 1.0f);
8035         }
8036 }
8037
8038 void game_stop_subspace_ambient_sound()
8039 {
8040         if ( Subspace_ambient_left_channel >= 0 ) {
8041                 snd_stop(Subspace_ambient_left_channel);
8042                 Subspace_ambient_left_channel = -1;
8043         }
8044
8045         if ( Subspace_ambient_right_channel >= 0 ) {
8046                 snd_stop(Subspace_ambient_right_channel);
8047                 Subspace_ambient_right_channel = -1;
8048         }
8049 }
8050
8051 // ----------------------------------------------------------------
8052 //
8053 // Subspace Ambient Sound END
8054 //
8055 // ----------------------------------------------------------------
8056
8057 // ----------------------------------------------------------------
8058 //
8059 // CDROM detection code START
8060 //
8061 // ----------------------------------------------------------------
8062
8063 #define CD_SIZE_72_MINUTE_MAX                   (697000000)
8064
8065 uint game_get_cd_used_space(char *path)
8066 {
8067 #ifndef PLAT_UNIX
8068         uint total = 0;
8069         char use_path[512] = "";
8070         char sub_path[512] = "";
8071         WIN32_FIND_DATA find;
8072         HANDLE find_handle;
8073
8074         // recurse through all files and directories
8075         strcpy(use_path, path);
8076         strcat(use_path, "*.*");
8077         find_handle = FindFirstFile(use_path, &find);
8078
8079         // bogus
8080         if(find_handle == INVALID_HANDLE_VALUE){
8081                 return 0;
8082         }       
8083
8084         // whee
8085         do {
8086                 // subdirectory. make sure to ignore . and ..
8087                 if((find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && stricmp(find.cFileName, ".") && stricmp(find.cFileName, "..")){
8088                         // subsearch
8089                         strcpy(sub_path, path);
8090                         strcat(sub_path, find.cFileName);
8091                         strcat(sub_path, "\\");
8092                         total += game_get_cd_used_space(sub_path);      
8093                 } else {
8094                         total += (uint)find.nFileSizeLow;
8095                 }                               
8096         } while(FindNextFile(find_handle, &find));      
8097
8098         // close
8099         FindClose(find_handle);
8100
8101         // total
8102         return total;
8103 #else
8104         STUB_FUNCTION;
8105         
8106         return 0;
8107 #endif  
8108 }
8109
8110
8111 // if volume_name is non-null, the CD name must match that
8112 int find_freespace_cd(char *volume_name)
8113 {
8114 #ifndef PLAT_UNIX
8115         char oldpath[MAX_PATH];
8116         char volume[256];
8117         int i;
8118         int cdrom_drive=-1;
8119         int volume_match = 0;
8120         _finddata_t find;
8121         int find_handle;
8122
8123         GetCurrentDirectory(MAX_PATH, oldpath);
8124
8125         for (i = 0; i < 26; i++) 
8126         {
8127 //XSTR:OFF
8128                 char path[]="d:\\";
8129 //XSTR:ON
8130
8131                 path[0] = (char)('A'+i);
8132                 if (GetDriveType(path) == DRIVE_CDROM) {
8133                         cdrom_drive = -3;
8134                         if ( GetVolumeInformation(path, volume, 256, NULL, NULL, NULL, NULL, 0) == TRUE ) {
8135                                 nprintf(("CD", "CD volume: %s\n", volume));
8136                         
8137                                 // check for any CD volume
8138                                 int volume1_present = 0;
8139                                 int volume2_present = 0;
8140                                 int volume3_present = 0;                
8141
8142                                 char full_check[512] = "";
8143
8144                                 // look for setup.exe
8145                                 strcpy(full_check, path);
8146                                 strcat(full_check, "setup.exe");                                
8147                                 find_handle = _findfirst(full_check, &find);
8148                                 if(find_handle != -1){
8149                                         volume1_present = 1;                            
8150                                         _findclose(find_handle);                                
8151                                 }
8152
8153                                 // look for intro.mve
8154                                 strcpy(full_check, path);
8155                                 strcat(full_check, "intro.mve");                                
8156                                 find_handle = _findfirst(full_check, &find);
8157                                 if(find_handle != -1){
8158                                         volume2_present = 1;
8159                                         _findclose(find_handle);                                                
8160                                 }                               
8161
8162                                 // look for endpart1.mve
8163                                 strcpy(full_check, path);
8164                                 strcat(full_check, "endpart1.mve");                             
8165                                 find_handle = _findfirst(full_check, &find);
8166                                 if(find_handle != -1){
8167                                         volume3_present = 1;
8168                                         _findclose(find_handle);                                
8169                                 }                               
8170                         
8171                                 // see if we have the specific CD we're looking for
8172                                 if ( volume_name ) {
8173                                         // volume 1
8174                                         if ( !stricmp(volume_name, FS_CDROM_VOLUME_1) && volume1_present) {
8175                                                 volume_match = 1;
8176                                         }
8177                                         // volume 2
8178                                         if ( !stricmp(volume_name, FS_CDROM_VOLUME_2) && volume2_present) {
8179                                                 volume_match = 1;
8180                                         }
8181                                         // volume 3
8182                                         if ( !stricmp(volume_name, FS_CDROM_VOLUME_3) && volume3_present) {
8183                                                 volume_match = 1;
8184                                         }
8185                                 } else {                                                                                
8186                                         if ( volume1_present || volume2_present || volume3_present ) {
8187                                                 volume_match = 1;
8188                                         }
8189                                 }
8190                                 
8191                                 // here's where we make sure that CD's 2 and 3 are not just ripped - check to make sure its capacity is > 697,000,000 bytes                             
8192                                 if ( volume_match ){
8193 #ifdef RELEASE_REAL                                     
8194                                         // we don't care about CD1 though. let it be whatever size it wants, since the game will demand CD's 2 and 3 at the proper time
8195                                         if(volume2_present || volume3_present) {
8196                                                 // first step - check to make sure its a cdrom
8197                                                 if(GetDriveType(path) != DRIVE_CDROM){                                                  
8198                                                         break;
8199                                                 }
8200
8201 #if !defined(OEM_BUILD)
8202                                                 // oem not on 80 min cds, so dont check tha size
8203                                                 // check its size
8204                                                 uint used_space = game_get_cd_used_space(path);                                                                                 
8205                                                 if(used_space < CD_SIZE_72_MINUTE_MAX){                                                 
8206                                                         break;
8207                                                 }
8208 #endif // !defined(OEM_BUILD)
8209                                         }                                       
8210
8211                                         cdrom_drive = i;
8212                                         break;
8213 #else
8214                                         cdrom_drive = i;
8215                                         break;
8216 #endif // RELEASE_REAL
8217                                 }
8218                         }
8219                 }
8220         }       
8221
8222         SetCurrentDirectory(oldpath);
8223         return cdrom_drive;
8224 #else
8225         STUB_FUNCTION;
8226         
8227         return 0;
8228 #endif  
8229 }
8230
8231 int set_cdrom_path(int drive_num)
8232 {
8233         int rval;
8234
8235         if (drive_num < 0) {                    //no CD
8236 //              #ifndef NDEBUG
8237 //              strcpy(CDROM_dir,"j:\\FreeSpaceCD\\");                          //set directory
8238 //              rval = 1;
8239 //              #else
8240                 strcpy(Game_CDROM_dir,"");                              //set directory
8241                 rval = 0;
8242 //              #endif
8243         } else {
8244                 sprintf(Game_CDROM_dir,NOX("%c:\\"), 'a' + drive_num );                 //set directory
8245                 rval = 1;
8246         }
8247
8248         return rval;
8249 }
8250
8251 int init_cdrom()
8252 {
8253         int i, rval;
8254
8255         //scan for CD, etc.
8256
8257         rval = 1;
8258
8259         #ifndef DEMO
8260         i = find_freespace_cd();
8261
8262         rval = set_cdrom_path(i);
8263
8264         /*
8265         if ( rval ) {
8266                 nprintf(("CD", "Using %s for FreeSpace CD\n", CDROM_dir));
8267         } else {
8268                 nprintf(("CD", "FreeSpace CD not found\n"));
8269         }
8270         */
8271         #endif
8272
8273         return rval;
8274 }
8275
8276 int Last_cd_label_found = 0;
8277 char Last_cd_label[256];
8278
8279 int game_cd_changed()
8280 {
8281 #ifndef PLAT_UNIX
8282         char label[256];
8283         int found;
8284         int changed = 0;
8285         
8286         if ( strlen(Game_CDROM_dir) == 0 ) {
8287                 init_cdrom();
8288         }
8289
8290         found = GetVolumeInformation(Game_CDROM_dir, label, 256, NULL, NULL, NULL, NULL, 0);
8291
8292         if ( found != Last_cd_label_found )     {
8293                 Last_cd_label_found = found;
8294                 if ( found )    {
8295                         mprintf(( "CD '%s' was inserted\n", label ));
8296                         changed = 1;
8297                 } else {
8298                         mprintf(( "CD '%s' was removed\n", Last_cd_label ));
8299                         changed = 1;
8300                 }
8301         } else {
8302                 if ( Last_cd_label_found )      {
8303                         if ( !stricmp( Last_cd_label, label ))  {
8304                                 //mprintf(( "CD didn't change\n" ));
8305                         } else {
8306                                 mprintf(( "CD was changed from '%s' to '%s'\n", Last_cd_label, label ));
8307                                 changed = 1;
8308                         }
8309                 } else {
8310                         // none found before, none found now.
8311                         //mprintf(( "still no CD...\n" ));
8312                 }
8313         }
8314         
8315         Last_cd_label_found = found;
8316         if ( found )    {
8317                 strcpy( Last_cd_label, label );
8318         } else {
8319                 strcpy( Last_cd_label, "" );
8320         }
8321
8322         return changed;
8323 #else
8324         STUB_FUNCTION;
8325         
8326         return 0;
8327 #endif          
8328 }
8329
8330 // check if _any_ FreeSpace2 CDs are in the drive
8331 // return: 1    => CD now in drive
8332 //                        0     =>      Could not find CD, they refuse to put it in the drive
8333 int game_do_cd_check(char *volume_name)
8334 {       
8335 #if !defined(GAME_CD_CHECK)
8336         return 1;
8337 #else
8338         int cd_present = 0;
8339         int cd_drive_num;
8340
8341         int num_attempts = 0;
8342         int refresh_files = 0;
8343         while(1) {
8344                 int path_set_ok, popup_rval;
8345
8346                 cd_drive_num = find_freespace_cd(volume_name);
8347                 path_set_ok = set_cdrom_path(cd_drive_num);
8348                 if ( path_set_ok ) {
8349                         cd_present = 1;
8350                         if ( refresh_files ) {
8351                                 cfile_refresh();
8352                                 refresh_files = 0;
8353                         }
8354                         break;
8355                 }
8356
8357                 // standalone mode
8358                 if(Is_standalone){
8359                         cd_present = 0;
8360                         break;
8361                 } else {
8362                         // no CD found, so prompt user
8363                         popup_rval = popup(PF_BODY_BIG, 1, POPUP_OK, XSTR( "FreeSpace 2 CD not found\n\nInsert a FreeSpace 2 CD to continue", 202));
8364                         refresh_files = 1;
8365                         if ( popup_rval != 1 ) {
8366                                 cd_present = 0;
8367                                 break;
8368                         }
8369
8370                         if ( num_attempts++ > 5 ) {
8371                                 cd_present = 0;
8372                                 break;
8373                         }
8374                 }
8375         }
8376
8377         return cd_present;
8378 #endif
8379 }
8380
8381 // check if _any_ FreeSpace2 CDs are in the drive
8382 // return: 1    => CD now in drive
8383 //                        0     =>      Could not find CD, they refuse to put it in the drive
8384 int game_do_cd_check_specific(char *volume_name, int cdnum)
8385 {       
8386         int cd_present = 0;
8387         int cd_drive_num;
8388
8389         int num_attempts = 0;
8390         int refresh_files = 0;
8391         while(1) {
8392                 int path_set_ok, popup_rval;
8393
8394                 cd_drive_num = find_freespace_cd(volume_name);
8395                 path_set_ok = set_cdrom_path(cd_drive_num);
8396                 if ( path_set_ok ) {
8397                         cd_present = 1;
8398                         if ( refresh_files ) {
8399                                 cfile_refresh();
8400                                 refresh_files = 0;
8401                         }
8402                         break;
8403                 }
8404
8405                 if(Is_standalone){
8406                         cd_present = 0;
8407                         break;
8408                 } else {
8409                         // no CD found, so prompt user
8410 #if defined(DVD_MESSAGE_HACK)
8411                         popup_rval = popup(PF_BODY_BIG, 1, POPUP_OK, XSTR("Please insert DVD", 1468));
8412 #else
8413                         popup_rval = popup(PF_BODY_BIG, 1, POPUP_OK, XSTR("Please insert CD %d", 1468), cdnum);
8414 #endif
8415                         refresh_files = 1;
8416                         if ( popup_rval != 1 ) {
8417                                 cd_present = 0;
8418                                 break;
8419                         }
8420
8421                         if ( num_attempts++ > 5 ) {
8422                                 cd_present = 0;
8423                                 break;
8424                         }
8425                 }
8426         }
8427
8428         return cd_present;
8429 }
8430
8431 // only need to do this in RELEASE_REAL
8432 int game_do_cd_mission_check(char *filename)
8433 {       
8434 #ifdef RELEASE_REAL
8435         int cd_num;
8436         int cd_present = 0;
8437         int cd_drive_num;
8438         fs_builtin_mission *m = game_find_builtin_mission(filename);
8439
8440         // check for changed CD
8441         if(game_cd_changed()){
8442                 cfile_refresh();
8443         }
8444
8445         // multiplayer
8446         if((Game_mode & GM_MULTIPLAYER) || Is_standalone){
8447                 return 1;
8448         }
8449
8450         // not builtin, so do a general check (any FS2 CD will do)
8451         if(m == NULL){
8452                 return game_do_cd_check();
8453         }
8454
8455         // does not have any CD requirement, do a general check
8456         if(strlen(m->cd_volume) <= 0){
8457                 return game_do_cd_check();
8458         }
8459
8460         // get the volume
8461         if(!stricmp(m->cd_volume, FS_CDROM_VOLUME_1)){
8462                 cd_num = 1;
8463         } else if(!stricmp(m->cd_volume, FS_CDROM_VOLUME_2)){
8464                 cd_num = 2;
8465         } else if(!stricmp(m->cd_volume, FS_CDROM_VOLUME_3)){
8466                 cd_num = 3; 
8467         } else {
8468                 return game_do_cd_check();
8469         }
8470
8471         // did we find the cd?
8472         if(find_freespace_cd(m->cd_volume) >= 0){
8473                 return 1;
8474         }
8475
8476         // make sure the volume exists
8477         int num_attempts = 0;
8478         int refresh_files = 0;
8479         while(1){
8480                 int path_set_ok, popup_rval;
8481
8482                 cd_drive_num = find_freespace_cd(m->cd_volume);
8483                 path_set_ok = set_cdrom_path(cd_drive_num);
8484                 if ( path_set_ok ) {
8485                         cd_present = 1;
8486                         if ( refresh_files ) {
8487                                 cfile_refresh();
8488                                 refresh_files = 0;
8489                         }
8490                         break;
8491                 }
8492
8493                 // no CD found, so prompt user
8494 #if defined(DVD_MESSAGE_HACK)
8495                 popup_rval = popup(PF_BODY_BIG, 1, POPUP_OK, XSTR("Please insert DVD", 1468));
8496 #else
8497                 popup_rval = popup(PF_BODY_BIG, 1, POPUP_OK, XSTR("Please insert CD %d", 1468), cd_num);
8498 #endif
8499
8500                 refresh_files = 1;
8501                 if ( popup_rval != 1 ) {
8502                         cd_present = 0;
8503                         break;
8504                 }
8505
8506                 if ( num_attempts++ > 5 ) {
8507                         cd_present = 0;
8508                         break;
8509                 }
8510         }       
8511
8512         return cd_present;
8513 #else
8514         return 1;
8515 #endif
8516 }
8517
8518 // ----------------------------------------------------------------
8519 //
8520 // CDROM detection code END
8521 //
8522 // ----------------------------------------------------------------
8523
8524 // ----------------------------------------------------------------
8525 // SHIPS TBL VERIFICATION STUFF
8526 //
8527
8528 // checksums, just keep a list of all valid ones, if it matches any of them, keep it
8529 #define NUM_SHIPS_TBL_CHECKSUMS         1
8530 /*
8531 int Game_ships_tbl_checksums[NUM_SHIPS_TBL_CHECKSUMS] = {
8532         -463907578,                                             // US - beta 1
8533         1696074201,                                             // FS2 demo
8534 };
8535 */
8536 int Game_ships_tbl_checksums[NUM_SHIPS_TBL_CHECKSUMS] = {
8537 //      -1022810006,                                    // 1.0 FULL
8538         -1254285366                                             // 1.2 FULL (German)
8539 };
8540
8541 void verify_ships_tbl()
8542 {       
8543         /*
8544 #ifdef NDEBUG
8545         Game_ships_tbl_valid = 1;
8546 #else
8547         */
8548         uint file_checksum;             
8549         int idx;
8550
8551         // detect if the packfile exists
8552         CFILE *detect = cfopen("ships.tbl", "rb");
8553         Game_ships_tbl_valid = 0;        
8554         
8555         // not mission-disk
8556         if(!detect){
8557                 Game_ships_tbl_valid = 0;
8558                 return;
8559         }       
8560
8561         // get the long checksum of the file
8562         file_checksum = 0;
8563         cfseek(detect, 0, SEEK_SET);    
8564         cf_chksum_long(detect, &file_checksum);
8565         cfclose(detect);
8566         detect = NULL;  
8567
8568         // now compare the checksum/filesize against known #'s
8569         for(idx=0; idx<NUM_SHIPS_TBL_CHECKSUMS; idx++){
8570                 if(Game_ships_tbl_checksums[idx] == (int)file_checksum){
8571                         Game_ships_tbl_valid = 1;
8572                         return;
8573                 }
8574         }
8575 // #endif
8576 }
8577
8578 DCF(shipspew, "display the checksum for the current ships.tbl")
8579 {
8580         uint file_checksum;
8581         CFILE *detect = cfopen("ships.tbl", "rb");
8582         // get the long checksum of the file
8583         file_checksum = 0;
8584         cfseek(detect, 0, SEEK_SET);    
8585         cf_chksum_long(detect, &file_checksum);
8586         cfclose(detect);
8587
8588         dc_printf("%d", file_checksum);
8589 }
8590
8591 // ----------------------------------------------------------------
8592 // WEAPONS TBL VERIFICATION STUFF
8593 //
8594
8595 // checksums, just keep a list of all valid ones, if it matches any of them, keep it
8596 #define NUM_WEAPONS_TBL_CHECKSUMS               1
8597 /*
8598 int Game_weapons_tbl_checksums[NUM_WEAPONS_TBL_CHECKSUMS] = {
8599         141718090,                              // US - beta 1
8600         -266420030,                             // demo 1
8601 };
8602 */
8603 int Game_weapons_tbl_checksums[NUM_WEAPONS_TBL_CHECKSUMS] = {
8604 //      399297860,                              // 1.0 FULL     
8605         -553984927                              // 1.2 FULL (german)
8606 };
8607
8608 void verify_weapons_tbl()
8609 {       
8610         /*
8611 #ifdef NDEBUG
8612         Game_weapons_tbl_valid = 1;
8613 #else
8614         */
8615         uint file_checksum;             
8616         int idx;
8617
8618         // detect if the packfile exists
8619         CFILE *detect = cfopen("weapons.tbl", "rb");
8620         Game_weapons_tbl_valid = 0;      
8621         
8622         // not mission-disk
8623         if(!detect){
8624                 Game_weapons_tbl_valid = 0;
8625                 return;
8626         }       
8627
8628         // get the long checksum of the file
8629         file_checksum = 0;
8630         cfseek(detect, 0, SEEK_SET);    
8631         cf_chksum_long(detect, &file_checksum);
8632         cfclose(detect);
8633         detect = NULL;  
8634
8635         // now compare the checksum/filesize against known #'s
8636         for(idx=0; idx<NUM_WEAPONS_TBL_CHECKSUMS; idx++){
8637                 if(Game_weapons_tbl_checksums[idx] == (int)file_checksum){
8638                         Game_weapons_tbl_valid = 1;
8639                         return;
8640                 }
8641         }
8642 // #endif
8643 }
8644
8645 DCF(wepspew, "display the checksum for the current weapons.tbl")
8646 {
8647         uint file_checksum;
8648         CFILE *detect = cfopen("weapons.tbl", "rb");
8649         // get the long checksum of the file
8650         file_checksum = 0;
8651         cfseek(detect, 0, SEEK_SET);    
8652         cf_chksum_long(detect, &file_checksum);
8653         cfclose(detect);
8654
8655         dc_printf("%d", file_checksum);
8656 }
8657
8658 // if the game is running using hacked data
8659 int game_hacked_data()
8660 {
8661         // hacked!
8662         if(!Game_weapons_tbl_valid || !Game_ships_tbl_valid){
8663                 return 1;
8664         }
8665
8666         // not hacked
8667         return 0;
8668 }
8669
8670 void display_title_screen()
8671 {
8672 #if defined(FS2_DEMO) || defined(OEM_BUILD)
8673         ///int title_bitmap;
8674
8675         // load bitmap
8676         int title_bitmap = bm_load(Game_demo_title_screen_fname[gr_screen.res]);
8677         if (title_bitmap == -1) {
8678                 return;
8679         }
8680
8681         // d3d  
8682         if((gr_screen.mode == GR_DIRECT3D) && (Gr_bitmap_poly)){
8683                 extern void d3d_start_frame();
8684                 d3d_start_frame();
8685         }
8686
8687         // set
8688         gr_set_bitmap(title_bitmap);
8689
8690         // draw
8691         gr_bitmap(0, 0);
8692
8693         // d3d  
8694         if((gr_screen.mode == GR_DIRECT3D) && (Gr_bitmap_poly)){
8695                 extern void d3d_stop_frame();
8696                 d3d_stop_frame();
8697         }
8698
8699         // flip
8700         gr_flip();
8701
8702         bm_unload(title_bitmap);
8703 #endif  // FS2_DEMO || OEM_BUILD
8704 }
8705
8706 // return true if the game is running with "low memory", which is less than 48MB
8707 bool game_using_low_mem()
8708 {
8709         if (Use_low_mem == 0) {
8710                 return false;
8711         } else {
8712                 return true;
8713         }
8714 }
8715