]> icculus.org git repositories - btb/d2x.git/blob - main/game.c
add caching of weapon inner models, screen shatter effects, robot explosions and...
[btb/d2x.git] / main / game.c
1 /* $Id: game.c,v 1.27 2004-05-20 07:42:38 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Game loop for Inferno
18  *
19  * Old Log:
20  * Revision 1.1  1995/12/05  16:01:09  allender
21  * Initial revision
22  *
23  * Revision 1.38  1995/11/13  13:02:35  allender
24  * put up HUD message when player starts tournament
25  *
26  * Revision 1.37  1995/11/13  09:21:05  allender
27  * ved and shorted tournament mode messages
28  *
29  * Revision 1.36  1995/11/09  17:27:00  allender
30  * took out cheats during demo playback
31  *
32  * Revision 1.35  1995/11/07  17:05:41  allender
33  * move registered cheats
34  *
35  * Revision 1.34  1995/11/03  12:55:45  allender
36  * shareware changes
37  *
38  * Revision 1.33  1995/10/29  20:15:00  allender
39  * took out frame rate cheat.  Pause for at least a second because
40  * of cmd-P problem
41  *
42  * Revision 1.32  1995/10/26  14:11:26  allender
43  * fix up message box stuff to align correctly
44  *
45  * Revision 1.31  1995/10/24  18:09:41  allender
46  * ixed cockpit weirdness -- don't update cockpit when do_appl_quit
47  * is called -- screen saved in mevent.c
48  *
49  * Revision 1.30  1995/10/21  23:39:10  allender
50  * ruise marking indicator
51  *
52  * Revision 1.29  1995/10/21  22:52:27  allender
53  * bald guy cheat -- print screen stuff
54  *
55  * Revision 1.28  1995/10/20  00:54:28  allender
56  * new help menus and redbook checking in outer game loop
57  *
58  * Revision 1.27  1995/10/17  15:34:19  allender
59  * pixel double is now default mode
60  *
61  * Revision 1.26  1995/10/12  17:34:44  allender
62  * bigger message box -- command key equivs for function keys
63  *
64  * Revision 1.25  1995/10/11  12:17:14  allender
65  * removed event loop processing
66  *
67  * Revision 1.24  1995/10/11  00:58:47  allender
68  * removed debugging code
69  *
70  * Revision 1.23  1995/10/10  11:50:32  allender
71  * fixed boxed message to align on 8 byte boundry,
72  * and some debug code
73  *
74  * Revision 1.22  1995/09/24  10:51:26  allender
75  * cannot go to finder in network..added cmd-q for quit
76  *
77  * Revision 1.21  1995/09/22  15:05:18  allender
78  * *more* hud and font type stuff (messages)
79  *
80  * Revision 1.20  1995/09/22  14:39:57  allender
81  * ved framerate counter up
82  *
83  * Revision 1.19  1995/09/18  17:01:28  allender
84  * start of compatibility stuff
85  *
86  * Revision 1.18  1995/09/15  15:53:13  allender
87  * better handling of PICT screen shots
88  *
89  * Revision 1.17  1995/09/14  15:27:41  allender
90  * fixed function type on message_box routiens
91  *
92  * Revision 1.16  1995/09/13  11:37:47  allender
93  * put in call to dump PICT file instead of PCX
94  *
95  * Revision 1.15  1995/09/08  17:13:28  allender
96  * put back in ibitblt.h and start of PICT picture dump
97  *
98  * Revision 1.14  1995/09/07  10:20:58  allender
99  * make cockpit mode default
100  *
101  * Revision 1.13  1995/09/07  10:17:34  allender
102  * added command key equivalents for function keys
103  *
104  * Revision 1.12  1995/09/04  11:36:47  allender
105  * fixed pixel double mode to have correct number of rendered
106  * lines
107  *
108  * Revision 1.11  1995/09/01  15:47:07  allender
109  * cap frame rate at 60 fps
110  *
111  * Revision 1.10  1995/08/26  16:25:59  allender
112  * whole buncha' stuff!!!!
113  *
114  * Revision 1.9  1995/08/01  16:04:47  allender
115  * put in ctrl_esc sequence to go to menubar
116  *
117  * Revision 1.8  1995/07/28  14:15:11  allender
118  * added FRAME cheat to display frame rate
119  *
120  * Revision 1.7  1995/07/17  08:54:19  allender
121  * *** empty log message ***
122  *
123  * Revision 1.6  1995/07/12  12:54:06  allender
124  * removed some debug keys
125  *
126  * Revision 1.5  1995/07/05  16:44:35  allender
127  * changed some debug keys
128  *
129  * Revision 1.4  1995/06/23  10:24:57  allender
130  * added scanline doubling routine
131  *
132  * Revision 1.3  1995/06/13  13:08:26  allender
133  * added special debug key to move window into upper left corner.
134  * also added debug key to put game in 640x480 mode
135  *
136  * Revision 1.2  1995/06/12  11:10:31  allender
137  * added DEL_SHIFT_M to move window to corner of screen
138  *
139  * Revision 1.1  1995/05/16  15:25:08  allender
140  * Initial revision
141  *
142  * Revision 2.36  1996/01/05  16:52:05  john
143  * Improved 3d stuff.
144  *
145  * Revision 2.35  1995/10/09  22:17:10  john
146  * Took out the page flipping in set_screen_mode, which shouldn't
147  * be there.  This was hosing the modex stuff.
148  *
149  * Revision 2.34  1995/10/09  19:46:34  john
150  * Fixed bug with modex paging with lcdbios.
151  *
152  * Revision 2.33  1995/10/08  11:46:09  john
153  * Fixed bug with 2d offset in interlaced mode in low res.
154  * Made LCDBIOS with pageflipping using VESA set start
155  * Address function.  X=CRTC offset, Y=0.
156  *
157  * Revision 2.32  1995/10/07  13:20:51  john
158  * Added new modes for LCDBIOS, also added support for -JoyNice,
159  * and added Shift+F1-F4 to controls various stereoscopic params.
160  *
161  * Revision 2.31  1995/05/31  14:34:43  unknown
162  * fixed warnings.
163  *
164  * Revision 2.30  1995/05/08  11:23:45  john
165  * Made 3dmax work like Kasan wants it to.
166  *
167  * Revision 2.29  1995/04/06  13:47:39  yuan
168  * Restored rear view to original.
169  *
170  * Revision 2.28  1995/04/06  12:13:07  john
171  * Fixed some bugs with 3dmax.
172  *
173  * Revision 2.27  1995/04/05  13:18:18  mike
174  * decrease energy usage on fusion cannon
175  *
176  * Revision 2.26  1995/03/30  16:36:32  mike
177  * text localization.
178  *
179  * Revision 2.25  1995/03/27  16:45:26  john
180  * Fixed some cheat bugs.  Added astral cheat.
181  *
182  * Revision 2.24  1995/03/27  15:37:11  mike
183  * boost fusion cannon for non-multiplayer modes.
184  *
185  * Revision 2.23  1995/03/24  17:48:04  john
186  * Fixed bug with menus and 320x100.
187  *
188  * Revision 2.22  1995/03/24  15:34:02  mike
189  * cheats.
190  *
191  * Revision 2.21  1995/03/24  13:11:39  john
192  * Added save game during briefing screens.
193  *
194  * Revision 2.20  1995/03/21  14:40:50  john
195  * Ifdef'd out the NETWORK code.
196  *
197  * Revision 2.19  1995/03/16  22:07:16  john
198  * Made so only for screen can be used for anything other
199  * than mode 13.
200  *
201  * Revision 2.18  1995/03/16  21:45:35  john
202  * Made all paged modes have incompatible menus!
203  *
204  * Revision 2.17  1995/03/16  18:30:35  john
205  * Made wider than 320 screens not have
206  * a status bar mode.
207  *
208  * Revision 2.16  1995/03/16  10:53:34  john
209  * Move VFX center to Shift+Z instead of Enter because
210  * it conflicted with toggling HUD on/off.
211  *
212  * Revision 2.15  1995/03/16  10:18:33  john
213  * Fixed bug with VFX mode not working. also made warning
214  * when it can't set VESA mode.
215  *
216  * Revision 2.14  1995/03/14  16:22:39  john
217  * Added cdrom alternate directory stuff.
218  *
219  * Revision 2.13  1995/03/14  12:14:17  john
220  * Made VR helmets have 4 resolutions to choose from.
221  *
222  * Revision 2.12  1995/03/10  13:47:33  john
223  * Added head tracking sensitivity.
224  *
225  * Revision 2.11  1995/03/10  13:13:47  john
226  * Added code to show T-xx on iglasses.
227  *
228  * Revision 2.10  1995/03/09  18:07:29  john
229  * Fixed bug with iglasses tracking not "centering" right.
230  * Made VFX have bright headlight lighting.
231  *
232  * Revision 2.9  1995/03/09  11:48:02  john
233  * Added HUD for VR helmets.
234  *
235  * Revision 2.8  1995/03/07  15:12:53  john
236  * Fixed VFX,3dmax support.
237  *
238  * Revision 2.7  1995/03/07  11:35:03  john
239  * Fixed bug with cockpit in rear view.
240  *
241  * Revision 2.6  1995/03/06  18:40:17  john
242  * Added some ifdef EDITOR stuff.
243  *
244  * Revision 2.5  1995/03/06  18:31:21  john
245  * Fixed bug with nmenu popping up on editor screen.
246  *
247  * Revision 2.4  1995/03/06  17:28:33  john
248  * Fixed but with cockpit toggling wrong.
249  *
250  * Revision 2.3  1995/03/06  16:08:10  mike
251  * Fix compile errors if building without editor.
252  *
253  * Revision 2.2  1995/03/06  15:24:10  john
254  * New screen techniques.
255  *
256  * Revision 2.1  1995/02/27  13:41:03  john
257  * Removed floating point from frame rate calculations.
258  *
259  * Revision 2.0  1995/02/27  11:31:54  john
260  * New version 2.0, which has no anonymous unions, builds with
261  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
262  *
263  * Revision 1.770  1995/02/22  12:45:15  allender
264  * remove anonymous unions from object structure
265  *
266  * Revision 1.769  1995/02/15  10:06:25  allender
267  * make pause pause game during demo playback
268  *
269  * Revision 1.768  1995/02/13  20:35:11  john
270  * Lintized
271  *
272  * Revision 1.767  1995/02/13  19:40:29  allender
273  * added place to demo record restoration from rear view in place that
274  * I forgot before
275  *
276  * Revision 1.766  1995/02/13  10:29:27  john
277  * Fixed bug with cheats not restoreing across save games.
278  *
279  * Revision 1.765  1995/02/11  22:54:33  john
280  * Made loading for pig not show up for demos.
281  *
282  * Revision 1.764  1995/02/11  17:30:08  allender
283  * ifndef NDEBUG around strip frame stuff
284  *
285  * Revision 1.763  1995/02/11  17:13:01  rob
286  * Took out modem.c code fille stuff.
287  *
288  * Revision 1.762  1995/02/11  16:36:47  allender
289  * debug key to strip frames from end of demo
290  *
291  * Revision 1.761  1995/02/11  14:29:16  john
292  * Turned off cheats when going into game.
293  *
294  * Revision 1.760  1995/02/11  13:46:54  mike
295  * fix cheats.
296  *
297  * Revision 1.759  1995/02/11  12:36:09  matt
298  * Cleaned up cheats
299  *
300  * Revision 1.758  1995/02/11  12:27:04  mike
301  * fix path-to-exit cheat.
302  *
303  * Revision 1.757  1995/02/11  01:56:24  mike
304  * robots don't fire cheat.
305  *
306  * Revision 1.756  1995/02/10  16:38:40  mike
307  * illuminate path to exit cheat.
308  *
309  * Revision 1.755  1995/02/10  16:19:40  mike
310  * new show-path-to-exit system, still buggy, compiled out.
311  *
312  * Revision 1.754  1995/02/10  15:54:46  matt
313  * Added new cheats
314  *
315  * Revision 1.753  1995/02/09  12:25:42  matt
316  * Made mem_fill() test routines not be called if RELEASE
317  *
318  * Revision 1.752  1995/02/09  08:49:32  mike
319  * change fill opcode value to 0xcc, int 3 value.
320  *
321  *
322  * Revision 1.751  1995/02/09  02:59:26  mike
323  * check code for 00066xxx bugs.
324  *
325  * Revision 1.750  1995/02/08  17:10:02  mike
326  * add, but don't call, debug code.
327  *
328  * Revision 1.749  1995/02/07  11:07:27  john
329  * Added hooks for confirm on game state restore.
330  *
331  * Revision 1.748  1995/02/06  15:52:45  mike
332  * add mini megawow powerup for giving reasonable weapons.
333  *
334  * Revision 1.747  1995/02/06  12:53:35  allender
335  * force endlevel_sequence to 0 to fix weird bug
336  *
337  * Revision 1.746  1995/02/04  10:03:30  mike
338  * Fly to exit cheat.
339  *
340  * Revision 1.745  1995/02/02  15:57:52  john
341  * Added turbo mode cheat.
342  *
343  * Revision 1.744  1995/02/02  14:43:39  john
344  * Uppped frametime limit to 150 Hz.
345  *
346  * Revision 1.743  1995/02/02  13:37:16  mike
347  * move T-?? message down in certain modes.
348  *
349  * Revision 1.742  1995/02/02  01:26:59  john
350  * Took out no key repeating.
351  *
352  * Revision 1.741  1995/01/29  21:36:44  mike
353  * make fusion cannon not make pitching slow.
354  *
355  * Revision 1.740  1995/01/28  15:57:57  john
356  * Made joystick calibration be only when wrong detected in
357  * menu or joystick axis changed.
358  *
359  * Revision 1.739  1995/01/28  15:21:03  yuan
360  * Added X-tra life cheat.
361  *
362  * Revision 1.738  1995/01/27  14:08:31  rob
363  * Fixed a bug.
364  *
365  * Revision 1.737  1995/01/27  14:04:59  rob
366  * Its not my fault, Mark told me to do it!
367  *
368  * Revision 1.736  1995/01/27  13:12:18  rob
369  * Added charging noises to play across net.
370  *
371  * Revision 1.735  1995/01/27  11:48:28  allender
372  * check for newdemo_state to be paused and stop recording.  We might be
373  * in between levels
374  *
375  * Revision 1.734  1995/01/26  22:11:41  mike
376  * Purple chromo-blaster (ie, fusion cannon) spruce up (chromification)
377  *
378  * Revision 1.733  1995/01/26  17:03:04  mike
379  * make fusion cannon have more chrome, make fusion, mega rock you!
380  *
381  * Revision 1.732  1995/01/25  14:37:25  john
382  * Made joystick only prompt for calibration once...
383  *
384  * Revision 1.731  1995/01/24  15:49:14  john
385  * Made typeing in long net messages wrap on
386  * small screen sizes.
387  *
388  * Revision 1.730  1995/01/24  15:23:42  mike
389  * network message tweaking.
390  *
391  * Revision 1.729  1995/01/24  12:00:47  john
392  * Fixed bug with defing macro passing keys to controls.
393  *
394  * Revision 1.728  1995/01/24  11:53:35  john
395  * Added better macro defining code.
396  *
397  * Revision 1.727  1995/01/23  22:17:15  john
398  * Fixed bug with not clearing key buffer when leaving f8.
399  *
400  * Revision 1.726  1995/01/23  22:07:09  john
401  * Added flush to game inputs during F8.
402  *
403  */
404
405 #ifdef HAVE_CONFIG_H
406 #include <conf.h>
407 #endif
408
409 #ifdef RCS
410 char game_rcsid[] = "$Id: game.c,v 1.27 2004-05-20 07:42:38 btb Exp $";
411 #endif
412
413 #ifdef WINDOWS
414 #include "desw.h"
415 #endif
416
417 #include <stdio.h>
418 #include <stdlib.h>
419 #include <string.h>
420 #include <stdarg.h>
421 #include <ctype.h>
422 #include <time.h>
423
424 #ifdef MACINTOSH
425 #include <Files.h>
426 #include <StandardFile.h>
427 #include <Quickdraw.h>
428 #include <Script.h>
429 #include <Strings.h>
430 #endif
431
432 #ifdef OGL
433 #include "ogl_init.h"
434 #endif
435
436 #include "pstypes.h"
437 #include "console.h"
438 #include "pa_enabl.h"       //$$POLY_ACC
439 #include "gr.h"
440 #include "inferno.h"
441 #include "game.h"
442 #include "key.h"
443 #include "object.h"
444 #include "physics.h"
445 #include "error.h"
446 #include "joy.h"
447 #include "mono.h"
448 #include "iff.h"
449 #include "pcx.h"
450 #include "timer.h"
451 #include "render.h"
452 #include "laser.h"
453 #include "screens.h"
454 #include "textures.h"
455 #include "slew.h"
456 #include "gauges.h"
457 #include "texmap.h"
458 #include "3d.h"
459 #include "effects.h"
460 #include "menu.h"
461 #include "gameseg.h"
462 #include "wall.h"
463 #include "ai.h"
464 #include "fuelcen.h"
465 #include "digi.h"
466 #include "ibitblt.h"
467 #include "u_mem.h"
468 #include "palette.h"
469 #include "morph.h"
470 #include "lighting.h"
471 #include "newdemo.h"
472 #include "collide.h"
473 #include "weapon.h"
474 #include "sounds.h"
475 #include "args.h"
476 #include "gameseq.h"
477 #include "automap.h"
478 #include "text.h"
479 #include "powerup.h"
480 #include "fireball.h"
481 #include "newmenu.h"
482 #ifdef NETWORK
483 #include "network.h"
484 #endif
485 #include "gamefont.h"
486 #include "endlevel.h"
487 #include "joydefs.h"
488 #include "kconfig.h"
489 #include "mouse.h"
490 #include "switch.h"
491 #include "controls.h"
492 #include "songs.h"
493 #include "gamepal.h"
494
495 #if defined(POLY_ACC)
496 #include "poly_acc.h"
497 #endif
498
499 #include "multi.h"
500 #include "desc_id.h"
501 #include "cntrlcen.h"
502 #include "pcx.h"
503 #include "state.h"
504 #include "piggy.h"
505 #include "multibot.h"
506 #include "ai.h"
507 #include "robot.h"
508 #include "playsave.h"
509 #include "fix.h"
510 #include "hudmsg.h"
511
512 int VGA_current_mode;
513
514 #ifdef MWPROFILER
515 #include <profiler.h>
516 #endif
517
518 //#define TEST_TIMER    1               //if this is set, do checking on timer
519
520 #define SHOW_EXIT_PATH  1
521
522 #ifdef EDITOR
523 #include "editor/editor.h"
524 #endif
525
526 //#define _MARK_ON 1
527 #ifdef __WATCOMC__
528 #if __WATCOMC__ < 1000
529 #include <wsample.h>            //should come after inferno.h to get mark setting
530 #endif
531 #endif
532
533
534 extern void ReadControls(void);         // located in gamecntl.c
535 extern int Current_display_mode;
536 extern void do_final_boss_frame(void);
537
538 int     Speedtest_on = 0;
539
540 #ifndef NDEBUG
541 int     Mark_count = 0;                 // number of debugging marks set
542 int     Speedtest_start_time;
543 int     Speedtest_segnum;
544 int     Speedtest_sidenum;
545 int     Speedtest_frame_start;
546 int     Speedtest_count=0;                              //      number of times to do the debug test.
547 #endif
548
549 static fix last_timer_value=0;
550 fix ThisLevelTime=0;
551
552 #if defined(TIMER_TEST) && !defined(NDEBUG)
553 fix _timer_value,actual_last_timer_value,_last_frametime;
554 int stop_count,start_count;
555 int time_stopped,time_started;
556 #endif
557
558 #ifndef MACINTOSH
559 ubyte * Game_cockpit_copy_code = NULL;
560 #else
561 ubyte Game_cockpit_copy_code = 0;
562 ubyte Scanline_double = 1;
563 #endif
564
565 int                     VR_screen_mode                  = 0;
566
567 ubyte                   VR_screen_flags = 0;            //see values in screens.h
568 ubyte                   VR_current_page = 0;
569 fix                     VR_eye_width            = F1_0;
570 int                     VR_render_mode          = VR_NONE;
571 int                     VR_low_res                      = 3;                            // Default to low res
572 int                     VR_show_hud = 1;
573 int                     VR_sensitivity     = 1;         // 0 - 2
574
575 //NEWVR
576 int                     VR_eye_offset            = 0;
577 int                     VR_eye_switch            = 0;
578 int                     VR_eye_offset_changed = 0;
579 int                     VR_use_reg_code         = 0;
580
581 grs_canvas  *VR_offscreen_buffer        = NULL;         // The offscreen data buffer
582 grs_canvas      VR_render_buffer[2];                                    //  Two offscreen buffers for left/right eyes.
583 grs_canvas      VR_render_sub_buffer[2];                        //  Two sub buffers for left/right eyes.
584 grs_canvas      VR_screen_pages[2];                                     //  Two pages of VRAM if paging is available
585 grs_canvas      VR_editor_canvas;                                               //  The canvas that the editor writes to.
586
587 #ifdef WINDOWS
588 //@@ LPDIRECTDRAWSURFACE _lpDDSMask = NULL;
589 dd_grs_canvas *dd_VR_offscreen_buffer = NULL;
590 dd_grs_canvas dd_VR_screen_pages[2];
591 dd_grs_canvas dd_VR_render_buffer[2];
592 dd_grs_canvas dd_VR_render_sub_buffer[2];
593
594 void game_win_init_cockpit_mask(int sram);
595 #endif
596
597 //do menus work in 640x480 or 320x200?
598 //PC version sets this in main().  Mac versios is always high-res, so set to 1 here
599 int MenuHiresAvailable = 1;             //can we do highres menus?
600 int MenuHires = 1;                              //are we currently in highres menus?
601
602 int Debug_pause=0;                              //John's debugging pause system
603
604 int Cockpit_mode=CM_FULL_COCKPIT;               //set game.h for values
605
606 int Cockpit_mode_save=-1;                                       //set while in letterbox or rear view, or -1
607 int force_cockpit_redraw=0;
608
609 cvar_t r_framerate = {"r_framerate","0"};
610
611 int PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd;
612
613 //      Toggle_var points at a variable which gets !ed on ctrl-alt-T press.
614 int     Dummy_var;
615 int     *Toggle_var = &Dummy_var;
616
617 #ifdef EDITOR
618 //flag for whether initial fade-in has been done
619 char faded_in;
620 #endif
621
622 #ifndef NDEBUG                          //these only exist if debugging
623
624 int Game_double_buffer = 1;     //double buffer by default
625 fix fixed_frametime=0;          //if non-zero, set frametime to this
626
627 #endif
628
629 int Game_suspended=0;           //if non-zero, nothing moves but player
630
631 fix     RealFrameTime;
632 fix     Auto_fire_fusion_cannon_time = 0;
633 fix     Fusion_charge = 0;
634 fix     Fusion_next_sound_time = 0;
635 fix     Fusion_last_sound_time = 0;
636
637 int Debug_spew = 1;
638 int Game_turbo_mode = 0;
639
640 int Game_mode = GM_GAME_OVER;
641
642 int     Global_laser_firing_count = 0;
643 int     Global_missile_firing_count = 0;
644
645 grs_bitmap background_bitmap;
646
647 int Game_aborted;
648
649 #define BACKGROUND_NAME "statback.pcx"
650
651 //      Function prototypes for GAME.C exclusively.
652
653 void GameLoop(int RenderFlag, int ReadControlsFlag);
654 void FireLaser(void);
655 void slide_textures(void);
656 void powerup_grab_cheat_all(void);
657
658 //      Other functions
659 extern void multi_check_for_killgoal_winner();
660 extern void RestoreGameSurfaces();
661
662 // window functions
663
664 void grow_window(void);
665 void shrink_window(void);
666
667 // text functions
668
669 void fill_background();
670
671 #ifndef RELEASE
672 void show_framerate(void);
673 void ftoa(char *string, fix f);
674 #endif
675
676 extern ubyte DefiningMarkerMessage;
677 extern char Marker_input[];
678
679 //      ==============================================================================================
680
681 extern char john_head_on;
682
683 void load_background_bitmap()
684 {
685         ubyte pal[256*3];
686         int pcx_error;
687
688         if (background_bitmap.bm_data)
689                 d_free(background_bitmap.bm_data);
690
691         background_bitmap.bm_data=NULL;
692         pcx_error = pcx_read_bitmap(john_head_on?"johnhead.pcx":BACKGROUND_NAME,&background_bitmap,BM_LINEAR,pal);
693         if (pcx_error != PCX_ERROR_NONE)
694                 Error("File %s - PCX error: %s",BACKGROUND_NAME,pcx_errormsg(pcx_error));
695         gr_remap_bitmap_good( &background_bitmap, pal, -1, -1 );
696 }
697
698
699 //this is called once per game
700 void init_game()
701 {
702         atexit(close_game);             //for cleanup
703
704         init_objects();
705
706         init_special_effects();
707
708         init_ai_system();
709
710         init_gauge_canvases();
711
712         init_exploding_walls();
713
714         load_background_bitmap();
715
716         Clear_window = 2;               //      do portal only window clear.
717
718         set_detail_level_parameters(Detail_level);
719
720         build_mission_list(0);
721
722         /* Register cvars */
723         cvar_registervariable(&r_framerate);
724
725 }
726
727
728 void reset_palette_add()
729 {
730         PaletteRedAdd           = 0;
731         PaletteGreenAdd = 0;
732         PaletteBlueAdd          = 0;
733         //gr_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
734 }
735
736
737 #ifdef WINDOWS
738 void win_get_span_list(grs_bitmap *bm, int miny, int maxy)
739 {
740         int x,y;
741         int mode = 0;
742         ubyte *data;
743         int offset;
744         int lspan=0, rspan=0, span=0;
745 //@@FILE *fp;
746
747         data = bm->bm_data;
748 //@@    fp = fopen("cockspan.dat", "w");
749
750         for (y = 0; y < miny; y++)
751                 win_cockpit_mask[y].num = 0;
752
753         for (y = miny; y <= maxy; y++)
754         {
755                 span = 0;
756                 //@@ fprintf(fp, "line %d: ", y);               
757                 for (x = 0; x < bm->bm_w; x++)
758                 {
759                         offset = y*bm->bm_rowsize + x;
760
761                         if (data[offset] == 255) {
762                                 switch (mode)
763                                 {
764                                         case 0:                         // Start Mode
765                                                 lspan   = x;
766                                                 win_cockpit_mask[y].span[span].xmin = x;
767                                                 mode = 1;
768                                         //@@    fprintf(fp, "<%d,", lspan);
769                                                 break;
770
771                                         case 1:                         // Transparency mode
772                                                 rspan = x;
773                                                 win_cockpit_mask[y].span[span].xmax = x;
774                                                 break;
775
776                                         case 2:                         // Switch from Draw mode to transparent
777                                                 lspan = x;
778                                                 win_cockpit_mask[y].span[span].xmin = x;
779                                         //@@    fprintf(fp, "<%d,", lspan);
780                                                 mode = 1;
781                                                 break;
782                                 }
783                         }
784                         else {
785                                 switch(mode)
786                                 {
787                                         case 0:                         // Start mode
788                                                 mode = 2;
789                                                 break;
790                                         
791                                         case 1:                         // Switching from transparent to Draw
792                                                 rspan = x;
793                                                 mode = 2;
794                                                 win_cockpit_mask[y].span[span].xmax = x;
795                                                 span++;
796                                         //@@    fprintf(fp, "%d> ", rspan);
797                                                 break;
798
799                                         case 2:
800                                                 break;
801                                 }
802                         }
803                 }
804                 if (mode == 1) {
805                 //@@    fprintf(fp, "%d> ", rspan);
806                         win_cockpit_mask[y].span[span].xmax = rspan;
807                         span++;
808                 }
809                 win_cockpit_mask[y].num = span;
810         //@@    fprintf(fp, "\n");
811                 mode = 0;
812         }
813         win_cockpit_mask[y].num = 255;
814 }
815 #endif //WINDOWS
816         
817
818 void game_show_warning(char *s)
819 {
820
821         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME)))
822                 stop_time();
823
824         nm_messagebox( TXT_WARNING, 1, TXT_OK, s );
825
826         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME)))
827                 start_time();
828 }
829
830
831 //these should be in gr.h
832 #define cv_w  cv_bitmap.bm_w
833 #define cv_h  cv_bitmap.bm_h
834
835 //added 3/24/99 by Owen Evans for screen res changing
836 u_int32_t Game_screen_mode = 0;
837 //end added - OE
838 int Game_window_x = 0;
839 int Game_window_y = 0;
840 int Game_window_w = 0;
841 int Game_window_h = 0;
842 int max_window_w = 0;
843 int max_window_h = 0;
844
845 extern void newdemo_record_cockpit_change(int);
846
847 //initialize the various canvases on the game screen
848 //called every time the screen mode or cockpit changes
849 void init_cockpit()
850 {
851         int minx, maxx, miny, maxy;
852
853 #if defined(POLY_ACC)
854     pa_flush();                 // get rid of undrawn polys.
855     pa_clear_buffer(1, 0);
856 #endif
857
858         //Initialize the on-screen canvases
859
860         if (Newdemo_state==ND_STATE_RECORDING) {
861                 newdemo_record_cockpit_change(Cockpit_mode);
862         }
863
864         if ( VR_render_mode != VR_NONE )
865                 Cockpit_mode = CM_FULL_SCREEN;
866
867         if (!(VR_screen_flags & VRF_ALLOW_COCKPIT) && (Cockpit_mode==CM_FULL_COCKPIT || Cockpit_mode==CM_STATUS_BAR || Cockpit_mode==CM_REAR_VIEW) )
868                 Cockpit_mode = CM_FULL_SCREEN;
869
870         if ( Screen_mode == SCREEN_EDITOR )
871                 Cockpit_mode = CM_FULL_SCREEN;
872
873 #ifdef OGL
874         if (Cockpit_mode == CM_FULL_COCKPIT || Cockpit_mode == CM_REAR_VIEW) {
875                 hud_message(MSGC_GAME_FEEDBACK, "Cockpit not available in GL mode");
876                 Cockpit_mode = CM_FULL_SCREEN;
877         }
878 #endif
879
880         WINDOS(
881                 dd_gr_set_current_canvas(NULL),
882                 gr_set_current_canvas(NULL)
883                 );
884         gr_set_curfont( GAME_FONT );
885
886 #if !defined(MACINTOSH) && !defined(WINDOWS)
887         if (Game_cockpit_copy_code)
888                 d_free(Game_cockpit_copy_code);
889         Game_cockpit_copy_code  = NULL;
890 #else
891         if (Game_cockpit_copy_code)
892                 Game_cockpit_copy_code = 0;
893 #endif
894
895 //@@    #ifdef WINDOWS
896 //@@            if (_lpDDSMask) { DDFreeSurface(_lpDDSMask); _lpDDSMask = NULL; }
897 //@@    #endif
898
899 #ifdef WINDOWS
900         game_win_init_cockpit_mask(0);
901 #endif
902
903         switch( Cockpit_mode ) {
904         case CM_FULL_COCKPIT:
905         case CM_REAR_VIEW: {
906                 grs_bitmap *bm = &GameBitmaps[cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)].index];
907
908                 PIGGY_PAGE_IN(cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)]);
909
910 #ifdef WINDOWS
911                 dd_gr_set_current_canvas(NULL);
912                 game_win_init_cockpit_mask(1);
913                 dd_gr_set_current_canvas(dd_VR_offscreen_buffer);
914 #else
915                 gr_set_current_canvas(VR_offscreen_buffer);
916 #endif          
917
918                 WIN(DDGRLOCK(dd_grd_curcanv));
919                 gr_bitmap( 0, 0, bm );
920                 bm = &VR_offscreen_buffer->cv_bitmap;
921                 bm->bm_flags = BM_FLAG_TRANSPARENT;
922                 gr_ibitblt_find_hole_size ( bm, &minx, &miny, &maxx, &maxy );
923                 WIN(    win_get_span_list(bm, miny, maxy);
924                                 DDGRUNLOCK(dd_grd_curcanv)
925                         );
926
927 #ifndef WINDOWS
928 #ifndef __MSDOS__
929                 gr_ibitblt_create_mask( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize);
930 #else
931                 if ( Current_display_mode ) {
932 #if defined(POLY_ACC)
933                         Game_cockpit_copy_code  = gr_ibitblt_create_mask_pa( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize );
934                         pa_clear_buffer(1, 0);      // clear offscreen to reduce white flash.
935 #else
936                         Game_cockpit_copy_code  = gr_ibitblt_create_mask_svga( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize );
937 #endif
938                 } else
939                         Game_cockpit_copy_code  = gr_ibitblt_create_mask( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize );
940 #endif
941                 bm->bm_flags = 0;               // Clear all flags for offscreen canvas
942 #else
943                 Game_cockpit_copy_code  = (ubyte *)(1);
944                 bm->bm_flags = 0;               // Clear all flags for offscreen canvas
945 #endif
946                 game_init_render_sub_buffers( 0, 0, maxx-minx+1, maxy-miny+1 );
947                 break;
948         }
949
950         case CM_FULL_SCREEN:
951
952                 max_window_h = grd_curscreen->sc_h;
953
954                 if (Game_window_h > max_window_h || VR_screen_flags&VRF_ALLOW_COCKPIT)
955                         Game_window_h = max_window_h;
956
957                 if (Game_window_w > max_window_w || VR_screen_flags&VRF_ALLOW_COCKPIT)
958                         Game_window_w = max_window_w;
959
960                 Game_window_x = (max_window_w - Game_window_w)/2;
961                 Game_window_y = (max_window_h - Game_window_h)/2;
962
963                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
964                 break;
965
966         case CM_STATUS_BAR:
967
968         max_window_h = grd_curscreen->sc_h - GameBitmaps[cockpit_bitmap[CM_STATUS_BAR+(Current_display_mode?(Num_cockpits/2):0)].index].bm_h;
969
970                 if (Game_window_h > max_window_h)
971                         Game_window_h = max_window_h;
972
973                 if (Game_window_w > max_window_w)
974                         Game_window_w = max_window_w;
975
976                 Game_window_x = (max_window_w - Game_window_w)/2;
977                 Game_window_y = (max_window_h - Game_window_h)/2;
978
979                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
980                 break;
981
982         case CM_LETTERBOX:      {
983                 int x,y,w,h;
984
985                 x = 0; w = VR_render_buffer[0].cv_bitmap.bm_w;          //VR_render_width;
986                 h = (VR_render_buffer[0].cv_bitmap.bm_h * 7) / 10;
987                 y = (VR_render_buffer[0].cv_bitmap.bm_h-h)/2;
988
989                 game_init_render_sub_buffers( x, y, w, h );
990                 break;
991                 }
992
993         }
994
995 WINDOS(
996         dd_gr_set_current_canvas(NULL),
997         gr_set_current_canvas(NULL)
998 );
999 }
1000
1001 //selects a given cockpit (or lack of one).  See types in game.h
1002 void select_cockpit(int mode)
1003 {
1004         if (mode != Cockpit_mode) {             //new mode
1005                 Cockpit_mode=mode;
1006                 init_cockpit();
1007         }
1008 }
1009
1010 extern int last_drawn_cockpit[2];
1011
1012 //force cockpit redraw next time. call this if you've trashed the screen
1013 void reset_cockpit()
1014 {
1015         force_cockpit_redraw=1;
1016         last_drawn_cockpit[0] = -1;
1017         last_drawn_cockpit[1] = -1;
1018 }
1019
1020 // void HUD_clear_messages();                           //Already declared in gauges.h
1021
1022 //NEWVR
1023 void VR_reset_params()
1024 {
1025         VR_eye_width = VR_SEPARATION;
1026         VR_eye_offset = VR_PIXEL_SHIFT;
1027         VR_eye_offset_changed = 2;
1028 }
1029
1030 void game_init_render_sub_buffers( int x, int y, int w, int h )
1031 {
1032 #ifdef WINDOWS
1033         dd_gr_init_sub_canvas( &dd_VR_render_sub_buffer[0], &dd_VR_render_buffer[0], x, y, w, h );
1034         dd_gr_init_sub_canvas( &dd_VR_render_sub_buffer[1], &dd_VR_render_buffer[1], x, y, w, h );
1035
1036         dd_VR_render_sub_buffer[0].canvas.cv_bitmap.bm_x = 0;
1037         dd_VR_render_sub_buffer[0].canvas.cv_bitmap.bm_y = 0;
1038         dd_VR_render_sub_buffer[0].xoff = x;
1039         dd_VR_render_sub_buffer[0].yoff = y;
1040         dd_VR_render_sub_buffer[1].canvas.cv_bitmap.bm_x = 0;
1041         dd_VR_render_sub_buffer[1].canvas.cv_bitmap.bm_y = 0;
1042         dd_VR_render_sub_buffer[1].xoff = x;
1043         dd_VR_render_sub_buffer[1].yoff = y;
1044
1045 #endif
1046         if (Scanline_double) {
1047                 #ifdef MACINTOSH
1048                 if ( w & 0x3 )
1049                         w &= ~0x3;
1050                 gr_init_sub_canvas( &VR_render_sub_buffer[0], &VR_render_buffer[0], x, y, w/2, (h/2)+1);
1051                 gr_init_sub_canvas( &VR_render_sub_buffer[1], &VR_render_buffer[1], x, y, w/2, (h/2)+1);
1052                 #endif
1053         } else {
1054                 gr_init_sub_canvas( &VR_render_sub_buffer[0], &VR_render_buffer[0], x, y, w, h );
1055                 gr_init_sub_canvas( &VR_render_sub_buffer[1], &VR_render_buffer[1], x, y, w, h );
1056         }
1057
1058         #ifdef MACINTOSH
1059                 #ifdef POLY_ACC
1060                         if ( PAEnabled )
1061                         {
1062                                 TQARect newBounds;
1063                                 
1064                                 newBounds.left = x;
1065                                 newBounds.right = x + w;
1066                                 newBounds.top = y;
1067                                 newBounds.bottom = y + h;
1068                                 
1069                                 pa_set_context(kGamePlayDrawContextID, &newBounds);             // must resize/create new context
1070                         }
1071                 #endif
1072         #endif
1073
1074 #ifdef WINDOWS
1075         VR_render_sub_buffer[0].cv_bitmap.bm_x = 0;
1076         VR_render_sub_buffer[0].cv_bitmap.bm_y = 0;
1077         VR_render_sub_buffer[1].cv_bitmap.bm_x = 0;
1078         VR_render_sub_buffer[1].cv_bitmap.bm_y = 0;
1079 #endif
1080 }
1081
1082
1083 #ifdef WINDOWS
1084 // Sets up the canvases we will be rendering to (WIN95)
1085 void game_init_render_buffers(int screen_mode, int render_w, int render_h, int render_method, int flags )
1086 {
1087 //      Hack for switching to higher that 640x480 modes (DDraw doesn't allow
1088 //       creating surfaces greater than the current resolution
1089
1090         if (GRMODEINFO(rw) < render_w || GRMODEINFO(rh) < render_h) {
1091                 render_w = GRMODEINFO(rw);
1092                 render_h = GRMODEINFO(rh);
1093         }
1094
1095         VR_screen_mode          = screen_mode;
1096
1097         VR_screen_flags =  flags;
1098
1099         VR_reset_params();
1100         VR_render_mode  = render_method;
1101
1102         Game_window_w           = render_w;
1103         Game_window_h           = render_h;
1104
1105         if (dd_VR_offscreen_buffer && dd_VR_offscreen_buffer != dd_grd_backcanv) {
1106                 dd_gr_free_canvas(dd_VR_offscreen_buffer);
1107         }
1108
1109         if ( (VR_render_mode==VR_AREA_DET) || (VR_render_mode==VR_INTERLACED ) )        {
1110                 if ( render_h*2 < 200 ) {
1111                         Int3();         // Not Supported yet!!!
1112 //                      VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1113                 }
1114                 else {
1115                         Int3();         // Not Supported yet!!!
1116 //                      VR_offscreen_buffer = gr_create_canvas( render_w, render_h*2 );
1117                 }
1118
1119                 Int3();                 // Not Supported yet!!!
1120 //              gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1121 //              gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, render_h, render_w, render_h );
1122         }
1123         else if (GRMODEINFO(paged) && !GRMODEINFO(dbuf)) {
1124         //      Here we will make the VR_offscreen_buffer the 2nd page and hopefully
1125         //      we can just flip it, saving a blt.
1126
1127                 dd_VR_offscreen_buffer = dd_grd_backcanv;
1128                 VR_offscreen_buffer = & dd_grd_backcanv->canvas;
1129         }
1130         else if (GRMODEINFO(dbuf)||GRMODEINFO(emul)) {
1131         //      The offscreen buffer will be created.  We will just blt this
1132         //      to the screen (which may be blted to the primary surface)
1133                 if ( render_h < 200 ) {
1134                         dd_VR_offscreen_buffer = dd_gr_create_canvas(render_w, 200);
1135                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
1136                 }
1137                 else {
1138                         dd_VR_offscreen_buffer = dd_gr_create_canvas(render_w, render_h);
1139                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
1140                 }
1141         }
1142
1143         dd_gr_init_sub_canvas( &dd_VR_render_buffer[0], dd_VR_offscreen_buffer, 0, 0, render_w, render_h );
1144         dd_gr_init_sub_canvas( &dd_VR_render_buffer[1], dd_VR_offscreen_buffer, 0, 0, render_w, render_h );
1145         gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1146         gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, render_w, render_h );
1147
1148         game_init_render_sub_buffers( 0, 0, render_w, render_h );
1149 }
1150
1151 #else
1152
1153 // Sets up the canvases we will be rendering to (NORMAL VERSION)
1154 void game_init_render_buffers(int screen_mode, int render_w, int render_h, int render_method, int flags )
1155 {
1156 //      if (vga_check_mode(screen_mode) != 0)
1157 //              Error("Cannot set requested video mode");
1158
1159         VR_screen_mode          =       screen_mode;
1160
1161         VR_screen_flags =  flags;
1162
1163 //NEWVR
1164         VR_reset_params();
1165         VR_render_mode  = render_method;
1166
1167         Game_window_w           = render_w;
1168         Game_window_h           = render_h;
1169
1170         if (VR_offscreen_buffer) {
1171                 gr_free_canvas(VR_offscreen_buffer);
1172         }
1173
1174         if ( (VR_render_mode==VR_AREA_DET) || (VR_render_mode==VR_INTERLACED ) )        {
1175                 if ( render_h*2 < 200 ) {
1176                         VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1177                 }
1178                 else {
1179                         VR_offscreen_buffer = gr_create_canvas( render_w, render_h*2 );
1180                 }
1181
1182                 gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1183                 gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, render_h, render_w, render_h );
1184         }
1185         else {
1186                 if ( render_h < 200 ) {
1187                         VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1188                 }
1189                 else {
1190 #if defined(POLY_ACC)
1191                         #ifndef MACINTOSH
1192             VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1193             d_free(VR_offscreen_buffer->cv_bitmap.bm_data);
1194             gr_init_canvas(VR_offscreen_buffer, pa_get_buffer_address(1), BM_LINEAR15, render_w, render_h);
1195                         #else
1196                                 if ( PAEnabled || gConfigInfo.mAcceleration ) {
1197                                         Cockpit_mode=CM_FULL_SCREEN;            // HACK HACK HACK HACK HACK!!!!
1198                                         VR_offscreen_buffer = gr_create_canvas2(render_w, render_h, BM_LINEAR15);
1199                                 } else
1200                     VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1201                         #endif
1202 #else
1203             VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1204 #endif
1205         }
1206
1207 #ifdef OGL
1208                 VR_offscreen_buffer->cv_bitmap.bm_type = BM_OGL;
1209 #endif
1210
1211                 gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1212                 gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, render_w, render_h );
1213         }
1214
1215         game_init_render_sub_buffers( 0, 0, render_w, render_h );
1216 }
1217 #endif
1218
1219 //called to get the screen in a mode compatible with popup menus.
1220 //if we can't have popups over the game screen, switch to menu mode.
1221 void set_popup_screen(void)
1222 {
1223         //WIN(LoadCursorWin(MOUSE_DEFAULT_CURSOR));
1224
1225 #ifndef OGL // always have to switch to menu mode
1226         if (! (VR_screen_flags & VRF_COMPATIBLE_MENUS))
1227 #endif
1228         {
1229                 set_screen_mode(SCREEN_MENU);           //must switch to menu mode
1230         }
1231 }
1232
1233
1234 //called to change the screen mode. Parameter sm is the new mode, one of
1235 //SMODE_GAME or SMODE_EDITOR. returns mode acutally set (could be other
1236 //mode if cannot init requested mode)
1237 int set_screen_mode(int sm)
1238 {
1239 WIN(static int force_mode_change=0);
1240 WIN(static int saved_window_w);
1241 WIN(static int saved_window_h);
1242
1243 #ifdef EDITOR
1244         if ( (sm==SCREEN_MENU) && (Screen_mode==SCREEN_EDITOR) )        {
1245                 gr_set_current_canvas( Canv_editor );
1246                 return 1;
1247         }
1248 #endif
1249
1250 #ifdef WINDOWS
1251         if ( Screen_mode == sm && W95DisplayMode == VR_screen_mode) {
1252                 dd_gr_set_current_canvas( &dd_VR_screen_pages[VR_current_page] );
1253                 return 1;
1254         }
1255 #else
1256         if ( Screen_mode == sm && VGA_current_mode == VR_screen_mode) {
1257                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1258                 return 1;
1259         }
1260 #endif
1261
1262 #ifdef OGL
1263         if ((Screen_mode == sm) && !((sm==SCREEN_GAME) && (grd_curscreen->sc_mode != VR_screen_mode) && (Screen_mode == SCREEN_GAME))) {
1264                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1265                 ogl_set_screen_mode();
1266                 return 1;
1267         }
1268 #endif
1269
1270 #ifdef EDITOR
1271         Canv_editor = NULL;
1272 #endif
1273
1274         Screen_mode = sm;
1275
1276         switch( Screen_mode )
1277         {
1278                 case SCREEN_MENU:
1279                 #ifdef WINDOWS
1280                         //mouse_set_mode(0);
1281                         //ShowCursorW();
1282                         if (!(VR_screen_flags & VRF_COMPATIBLE_MENUS)) {
1283                         // HACK!!!  Meant to save window size when switching from
1284                         // non-compat menu mode to menu mode.
1285                                 saved_window_w = Game_window_w;
1286                                 saved_window_h = Game_window_h;
1287                                 force_mode_change = 1;
1288                         }
1289                         if (W95DisplayMode != SM95_640x480x8) {
1290 //@@                            piggy_bitmap_page_out_all_w();          // 2D GFX Flush cache.
1291                                 DDSETDISPLAYMODE(SM95_640x480x8);
1292                                 dd_gr_init_screen();
1293                                 if (!gr_palette_faded_out) gr_palette_load(gr_palette);
1294                         }
1295
1296                         dd_gr_init_sub_canvas(&dd_VR_screen_pages[0], dd_grd_screencanv,
1297                                                                         0,0,
1298                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_w,
1299                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_h);
1300                         dd_gr_init_sub_canvas(&dd_VR_screen_pages[1], dd_grd_screencanv,
1301                                                                         0,0,
1302                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_w,
1303                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_h);
1304                         MenuHires = 1;
1305                         FontHires = FontHiresAvailable;
1306
1307                 #else
1308                 {
1309                         int menu_mode;
1310
1311                         MenuHires = MenuHiresAvailable;         //do highres if we can
1312
1313 #if defined(POLY_ACC)
1314                                 #ifndef MACINTOSH
1315                     menu_mode = MenuHires?SM(640,480):SM(320,200);
1316                                 #else
1317                                         menu_mode = PAEnabled?SM_640x480x15xPA:SM_640x480V;
1318                                 #endif
1319 #else
1320             menu_mode = MenuHires?SM(640,480):SM(320,200);
1321 #endif
1322
1323                         if (VGA_current_mode != menu_mode) {
1324                                 if (gr_set_mode(menu_mode))
1325                                         Error("Cannot set screen mode for menu");
1326                                 if (!gr_palette_faded_out)
1327                                         gr_palette_load(gr_palette);
1328                         }
1329
1330                         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1331                         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1332
1333                         FontHires = FontHiresAvailable && MenuHires;
1334
1335                 }
1336                 #endif
1337                 break;
1338
1339         case SCREEN_GAME:
1340         #ifdef WINDOWS
1341                 //mouse_set_mode(1);
1342                 HideCursorW();
1343                 if (force_mode_change || (W95DisplayMode != VR_screen_mode)) {
1344
1345                         DDSETDISPLAYMODE(VR_screen_mode);
1346 //@@                    piggy_bitmap_page_out_all_w();          // 2D GFX Flush cache.
1347                         dd_gr_init_screen();
1348                         mprintf((0, "Reinitializing render buffers due to display mode change.\n"));
1349                         game_init_render_buffers(W95DisplayMode,
1350                                         GRMODEINFO(rw), GRMODEINFO(rh),
1351                                         VR_render_mode, VR_screen_flags);
1352
1353                         reset_cockpit();
1354                 }
1355         #else
1356                 if (VGA_current_mode != VR_screen_mode) {
1357                         if (gr_set_mode(VR_screen_mode))        {
1358                                 Error("Cannot set desired screen mode for game!");
1359                                 //we probably should do something else here, like select a standard mode
1360                         }
1361                         #ifdef MACINTOSH
1362                         if ( (Config_control_type == 1) && (Function_mode == FMODE_GAME) )
1363                                 joydefs_calibrate();
1364                         #endif
1365                         reset_cockpit();
1366                 }
1367         #endif
1368
1369                 if ( VR_render_mode == VR_NONE )
1370                 {
1371                         max_window_w = grd_curscreen->sc_w;
1372                         max_window_h = grd_curscreen->sc_h;
1373
1374                         if (VR_screen_flags & VRF_ALLOW_COCKPIT) {
1375                                 if (Cockpit_mode == CM_STATUS_BAR)
1376                         max_window_h = grd_curscreen->sc_h - GameBitmaps[cockpit_bitmap[CM_STATUS_BAR+(Current_display_mode?(Num_cockpits/2):0)].index].bm_h;
1377                         }
1378                         else if (Cockpit_mode != CM_LETTERBOX)
1379                                 Cockpit_mode = CM_FULL_SCREEN;
1380
1381               if (Game_window_h==0 || Game_window_h > max_window_h || Game_window_w==0 || Game_window_w > max_window_w) {
1382                                 Game_window_w = max_window_w;
1383                                 Game_window_h = max_window_h;
1384               }
1385
1386                 }
1387                 else
1388                         Cockpit_mode = CM_FULL_SCREEN;
1389
1390                 #ifdef WINDOWS
1391                 //      Super hack.   If we are switching from a 320x200 game to 640x480.
1392                 //                                              and we were in a menumode when switching, we don't
1393                 //                                              restore Game_window vals
1394                         if (force_mode_change && (W95DisplayMode == SM95_320x200x8X)) {
1395                                 Game_window_w = saved_window_w;
1396                                 Game_window_h = saved_window_h;
1397                                 force_mode_change = 0;
1398                         }
1399                 #endif
1400
1401
1402         //      Define screen pages for game mode
1403         // If we designate through screen_flags to use paging, then do so.
1404                 WINDOS(
1405                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[0], dd_grd_screencanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1406                         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h )
1407                 );
1408
1409                 if ( VR_screen_flags&VRF_USE_PAGING ) {
1410                 WINDOS(
1411                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[1], dd_grd_backcanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1412                         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, grd_curscreen->sc_h, grd_curscreen->sc_w, grd_curscreen->sc_h )
1413                 );
1414                 }
1415                 else {
1416                 WINDOS (
1417                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[1], dd_grd_screencanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1418                         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h )
1419                 );
1420                 }
1421
1422                 init_cockpit();
1423
1424         #ifdef WINDOWS
1425                 FontHires = FontHiresAvailable && (Current_display_mode != 0);
1426                 MenuHires = 1;
1427         #else
1428                 FontHires = FontHiresAvailable && (MenuHires = ((Current_display_mode != 0) && (Current_display_mode != 2)));
1429         #endif
1430
1431                 if ( VR_render_mode != VR_NONE )        {
1432                         // for 640x480 or higher, use hires font.
1433                         if (FontHiresAvailable && (grd_curscreen->sc_h > 400))
1434                                 FontHires = 1;
1435                         else
1436                                 FontHires = 0;
1437                 }
1438                 con_resize();
1439
1440                 break;
1441         #ifdef EDITOR
1442         case SCREEN_EDITOR:
1443                 if (grd_curscreen->sc_mode != SM(800,600))      {
1444                         int gr_error;
1445                         if ((gr_error=gr_set_mode(SM(800,600)))!=0) { //force into game scrren
1446                                 Warning("Cannot init editor screen (error=%d)",gr_error);
1447                                 return 0;
1448                         }
1449                 }
1450                 gr_palette_load( gr_palette );
1451
1452                 gr_init_sub_canvas( &VR_editor_canvas, &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1453                 Canv_editor = &VR_editor_canvas;
1454                 gr_init_sub_canvas( &VR_screen_pages[0], Canv_editor, 0, 0, Canv_editor->cv_w, Canv_editor->cv_h );
1455                 gr_init_sub_canvas( &VR_screen_pages[1], Canv_editor, 0, 0, Canv_editor->cv_w, Canv_editor->cv_h );
1456                 gr_set_current_canvas( Canv_editor );
1457                 init_editor_screen();   //setup other editor stuff
1458                 break;
1459         #endif
1460         default:
1461                 Error("Invalid screen mode %d",sm);
1462         }
1463
1464         VR_current_page = 0;
1465
1466         WINDOS(
1467                 dd_gr_set_current_canvas(&dd_VR_screen_pages[VR_current_page]),
1468                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] )
1469         );
1470
1471         if ( VR_screen_flags&VRF_USE_PAGING )   {
1472         WINDOS(
1473                 dd_gr_flip(),
1474                 gr_show_canvas( &VR_screen_pages[VR_current_page] )
1475         );
1476         }
1477 #ifdef OGL
1478         ogl_set_screen_mode();
1479 #endif
1480
1481         return 1;
1482 }
1483
1484 int gr_toggle_fullscreen_game(void){
1485 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
1486         int i;
1487         hud_message(MSGC_GAME_FEEDBACK, "toggling fullscreen mode %s",(i=gr_toggle_fullscreen())?"on":"off" );
1488         //added 2000/06/19 Matthew Mueller - hack to fix "infinite toggle" problem
1489         //it seems to be that the screen mode change takes long enough that the key has already sent repeat codes, or that its unpress event gets dropped, etc.  This is a somewhat ugly fix, but it works.
1490 //      generic_key_handler(KEY_PADENTER,0);
1491         key_flush();
1492         //end addition -MM
1493         return i;
1494 #else
1495         hud_message(MSGC_GAME_FEEDBACK, "fullscreen toggle not supported by this target");
1496         return -1;
1497 #endif
1498 }
1499
1500 int arch_toggle_fullscreen_menu(void);
1501
1502 int gr_toggle_fullscreen_menu(void){
1503 #ifdef GR_SUPPORTS_FULLSCREEN_MENU_TOGGLE
1504         int i;
1505         i=arch_toggle_fullscreen_menu();
1506
1507 //      generic_key_handler(KEY_PADENTER,0);
1508         key_flush();
1509
1510         return i;
1511 #else
1512         return -1;
1513 #endif
1514 }
1515
1516 static int timer_paused=0;
1517
1518 void stop_time()
1519 {
1520         if (timer_paused==0) {
1521                 fix time;
1522                 time = timer_get_fixed_seconds();
1523                 last_timer_value = time - last_timer_value;
1524                 if (last_timer_value < 0) {
1525                         #if defined(TIMER_TEST) && !defined(NDEBUG)
1526                         Int3();         //get Matt!!!!
1527                         #endif
1528                         last_timer_value = 0;
1529                 }
1530                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1531                 time_stopped = time;
1532                 #endif
1533         }
1534         timer_paused++;
1535
1536         #if defined(TIMER_TEST) && !defined(NDEBUG)
1537         stop_count++;
1538         #endif
1539 }
1540
1541 void start_time()
1542 {
1543         timer_paused--;
1544         Assert(timer_paused >= 0);
1545         if (timer_paused==0) {
1546                 fix time;
1547                 time = timer_get_fixed_seconds();
1548                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1549                 if (last_timer_value < 0)
1550                         Int3();         //get Matt!!!!
1551                 }
1552                 #endif
1553                 last_timer_value = time - last_timer_value;
1554                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1555                 time_started = time;
1556                 #endif
1557         }
1558
1559         #if defined(TIMER_TEST) && !defined(NDEBUG)
1560         start_count++;
1561         #endif
1562 }
1563
1564 MAC(extern ubyte joydefs_calibrating;)
1565
1566 void game_flush_inputs()
1567 {
1568         int dx,dy;
1569         key_flush();
1570         joy_flush();
1571         mouse_flush();
1572         #ifdef MACINTOSH
1573         if ( (Function_mode != FMODE_MENU) && !joydefs_calibrating )            // only reset mouse when not in menu or not calibrating
1574         #endif
1575                 mouse_get_delta( &dx, &dy );    // Read mouse
1576         memset(&Controls,0,sizeof(control_info));
1577 }
1578
1579 void reset_time()
1580 {
1581         last_timer_value = timer_get_fixed_seconds();
1582
1583 }
1584
1585 #ifndef RELEASE
1586 extern int Saving_movie_frames;
1587 int Movie_fixed_frametime;
1588 #else
1589 #define Saving_movie_frames     0
1590 #define Movie_fixed_frametime   0
1591 #endif
1592
1593 //added on 8/18/98 by Victor Rachels to add maximum framerate
1594 int maxfps = 80;
1595 //end this section
1596
1597 void calc_frame_time()
1598 {
1599         fix timer_value,last_frametime = FrameTime;
1600
1601         #if defined(TIMER_TEST) && !defined(NDEBUG)
1602         _last_frametime = last_frametime;
1603         #endif
1604
1605         timer_value = timer_get_fixed_seconds();
1606         FrameTime = timer_value - last_timer_value;
1607
1608         do {
1609             timer_value = timer_get_fixed_seconds();
1610             FrameTime = timer_value - last_timer_value;
1611             if (FrameTime < f1_0/maxfps);
1612                         timer_delay(1);
1613         } while (FrameTime < f1_0/maxfps);
1614
1615         #if defined(TIMER_TEST) && !defined(NDEBUG)
1616         _timer_value = timer_value;
1617         #endif
1618
1619         #ifndef NDEBUG
1620         if (!(((FrameTime > 0) && (FrameTime <= F1_0)) || (Function_mode == FMODE_EDITOR) || (Newdemo_state == ND_STATE_PLAYBACK))) {
1621                 mprintf((1,"Bad FrameTime - value = %x\n",FrameTime));
1622                 if (FrameTime == 0)
1623                         Int3(); //      Call Mike or Matt or John!  Your interrupts are probably trashed!
1624 //              if ( !dpmi_virtual_memory )
1625 //                      Int3();         //Get MATT if hit this!
1626         }
1627         #endif
1628
1629         #if defined(TIMER_TEST) && !defined(NDEBUG)
1630         actual_last_timer_value = last_timer_value;
1631         #endif
1632
1633         if ( Game_turbo_mode )
1634                 FrameTime *= 2;
1635
1636         // Limit frametime to be between 5 and 150 fps.
1637         RealFrameTime = FrameTime;
1638         if ( FrameTime < F1_0/150 ) FrameTime = F1_0/150;
1639         if ( FrameTime > F1_0/5 ) FrameTime = F1_0/5;
1640
1641         last_timer_value = timer_value;
1642
1643         if (FrameTime < 0)                                              //if bogus frametime...
1644                 FrameTime = last_frametime;             //...then use time from last frame
1645
1646         #ifndef NDEBUG
1647         if (fixed_frametime) FrameTime = fixed_frametime;
1648         #endif
1649
1650         #ifndef NDEBUG
1651         // Pause here!!!
1652         if ( Debug_pause )      {
1653                 int c;
1654                 c = 0;
1655                 while( c==0 )
1656                         c = key_peekkey();
1657
1658                 if ( c == KEY_P )       {
1659                         Debug_pause = 0;
1660                         c = key_inkey();
1661                 }
1662                 last_timer_value = timer_get_fixed_seconds();
1663         }
1664         #endif
1665
1666         #if Arcade_mode
1667                 FrameTime /= 2;
1668         #endif
1669
1670         #if defined(TIMER_TEST) && !defined(NDEBUG)
1671         stop_count = start_count = 0;
1672         #endif
1673
1674         //      Set value to determine whether homing missile can see target.
1675         //      The lower frametime is, the more likely that it can see its target.
1676         if (FrameTime <= F1_0/64)
1677                 Min_trackable_dot = MIN_TRACKABLE_DOT;  // -- 3*(F1_0 - MIN_TRACKABLE_DOT)/4 + MIN_TRACKABLE_DOT;
1678         else if (FrameTime < F1_0/32)
1679                 Min_trackable_dot = MIN_TRACKABLE_DOT + F1_0/64 - 2*FrameTime;  // -- fixmul(F1_0 - MIN_TRACKABLE_DOT, F1_0-4*FrameTime) + MIN_TRACKABLE_DOT;
1680         else if (FrameTime < F1_0/4)
1681                 Min_trackable_dot = MIN_TRACKABLE_DOT + F1_0/64 - F1_0/16 - FrameTime;  // -- fixmul(F1_0 - MIN_TRACKABLE_DOT, F1_0-4*FrameTime) + MIN_TRACKABLE_DOT;
1682         else
1683                 Min_trackable_dot = MIN_TRACKABLE_DOT + F1_0/64 - F1_0/8;
1684
1685 }
1686
1687 //--unused-- int Auto_flythrough=0;  //if set, start flythough automatically
1688
1689 void move_player_2_segment(segment *seg,int side)
1690 {
1691         vms_vector vp;
1692
1693         compute_segment_center(&ConsoleObject->pos,seg);
1694         compute_center_point_on_side(&vp,seg,side);
1695         vm_vec_sub2(&vp,&ConsoleObject->pos);
1696         vm_vector_2_matrix(&ConsoleObject->orient,&vp,NULL,NULL);
1697
1698         obj_relink( ConsoleObject-Objects, SEG_PTR_2_NUM(seg) );
1699
1700 }
1701
1702 #ifdef NETWORK
1703 void game_draw_time_left()
1704 {
1705         char temp_string[30];
1706         fix timevar;
1707         int i;
1708
1709         gr_set_curfont( GAME_FONT );    //GAME_FONT
1710         gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
1711
1712         timevar=i2f (Netgame.PlayTimeAllowed*5*60);
1713         i=f2i(timevar-ThisLevelTime);
1714         i++;
1715
1716         sprintf( temp_string, "Time left: %d secs", i );
1717
1718         if (i>=0)
1719          gr_string(0, 32, temp_string );
1720 }
1721 #endif
1722
1723
1724 extern int Game_pause;
1725
1726 void do_photos();
1727 void level_with_floor();
1728
1729 void modex_clear_box(int x,int y,int w,int h)
1730 {
1731         grs_canvas *temp_canv,*save_canv;
1732
1733         save_canv = grd_curcanv;
1734         temp_canv = gr_create_canvas(w,h);
1735         gr_set_current_canvas(temp_canv);
1736         gr_clear_canvas(BM_XRGB(0,0,0));
1737         gr_set_current_canvas(save_canv);
1738         gr_bitmapm(x,y,&temp_canv->cv_bitmap);
1739         gr_free_canvas(temp_canv);
1740
1741 }
1742
1743 extern void modex_printf(int x,int y,char *s,grs_font *font,int color);
1744
1745 // mac routine to drop contents of screen to a pict file using copybits
1746 // save a PICT to a file
1747 #ifdef MACINTOSH
1748
1749 void SavePictScreen(int multiplayer)
1750 {
1751         OSErr err;
1752         int parid, i, count;
1753         char *pfilename, filename[50], buf[512], cwd[FILENAME_MAX];
1754         short fd;
1755         FSSpec spec;
1756         PicHandle pict_handle;
1757         static int multi_count = 0;
1758         StandardFileReply sf_reply;
1759         
1760 // dump the contents of the GameWindow into a picture using copybits
1761
1762         pict_handle = OpenPicture(&GameWindow->portRect);
1763         if (pict_handle == NULL)
1764                 return;
1765                 
1766         CopyBits(&GameWindow->portBits, &GameWindow->portBits, &GameWindow->portRect, &GameWindow->portRect, srcBic, NULL);
1767         ClosePicture();
1768
1769 // get the cwd to restore with chdir when done -- this keeps the mac world sane
1770         if (!getcwd(cwd, FILENAME_MAX))
1771                 Int3();
1772 // create the fsspec
1773
1774         sprintf(filename, "screen%d", multi_count++);
1775         pfilename = c2pstr(filename);
1776         if (!multiplayer) {
1777                 show_cursor();
1778                 StandardPutFile("\pSave PICT as:", pfilename, &sf_reply);
1779                 if (!sf_reply.sfGood)
1780                         goto end;
1781                 memcpy( &spec, &(sf_reply.sfFile), sizeof(FSSpec) );
1782                 if (sf_reply.sfReplacing)
1783                         FSpDelete(&spec);
1784                 err = FSpCreate( &spec, 'ttxt', 'PICT', smSystemScript );
1785                 if (err)
1786                         goto end;
1787         } else {
1788 //              parid = GetAppDirId();
1789                 err = FSMakeFSSpec(0, 0, pfilename, &spec);
1790                 if (err == nsvErr)
1791                         goto end;
1792                 if (err != fnfErr)
1793                         FSpDelete(&spec);
1794                 err = FSpCreate(&spec, 'ttxt', 'PICT', smSystemScript);
1795                 if (err != 0)
1796                         goto end;
1797         }
1798
1799 // write the PICT file
1800         if ( FSpOpenDF(&spec, fsRdWrPerm, &fd) )
1801                 goto end;
1802         memset(buf, 0, sizeof(buf));
1803         count = 512;
1804         if ( FSWrite(fd, &count, buf) )
1805                 goto end;
1806         count = GetHandleSize((Handle)pict_handle);
1807         HLock((Handle)pict_handle);
1808         if ( FSWrite(fd, &count, *pict_handle) ) {
1809                 FSClose(fd);
1810                 FSpDelete(&spec);
1811         }
1812
1813 end:
1814         HUnlock((Handle)pict_handle);
1815         DisposeHandle((Handle)pict_handle);
1816         FSClose(fd);
1817         hide_cursor();
1818         chdir(cwd);
1819 }
1820
1821 #endif
1822
1823 //automap_flag is now unused, since we just check if the screen we're
1824 //writing to is modex
1825 //if called from automap, current canvas is set to visible screen
1826 #ifndef OGL
1827 void save_screen_shot(int automap_flag)
1828 {
1829 #if defined(WINDOWS)
1830         mprintf((0, "Doing screen shot thing.\n"));
1831         win95_save_pcx_shot();
1832
1833 #elif !defined(MACINTOSH)
1834         fix t1;
1835         char message[100];
1836         grs_canvas *screen_canv=&grd_curscreen->sc_canvas;
1837         grs_font *save_font;
1838         static int savenum=0;
1839         static int stereo_savenum=0;
1840         grs_canvas *temp_canv,*temp_canv2,*save_canv;
1841         char savename[FILENAME_LEN],savename2[FILENAME_LEN];
1842         ubyte pal[768];
1843         int w,h,aw,x,y;
1844         int modex_flag;
1845         int stereo=0;
1846
1847         temp_canv2=NULL;
1848
1849 //      // Can't do screen shots in VR modes.
1850 //      if ( VR_render_mode != VR_NONE )
1851 //              return;
1852
1853         stop_time();
1854
1855         save_canv = grd_curcanv;
1856
1857         if ( VR_render_mode != VR_NONE && !automap_flag && Function_mode==FMODE_GAME && Screen_mode==SCREEN_GAME)
1858                 stereo = 1;
1859
1860         if ( stereo ) {
1861                 temp_canv = gr_create_canvas(VR_render_buffer[0].cv_bitmap.bm_w,VR_render_buffer[0].cv_bitmap.bm_h);
1862                 gr_set_current_canvas(temp_canv);
1863                 gr_ubitmap(0,0,&VR_render_buffer[0].cv_bitmap);
1864
1865                 temp_canv2 = gr_create_canvas(VR_render_buffer[1].cv_bitmap.bm_w,VR_render_buffer[1].cv_bitmap.bm_h);
1866                 gr_set_current_canvas(temp_canv2);
1867                 gr_ubitmap(0,0,&VR_render_buffer[1].cv_bitmap);
1868         }
1869         else {
1870                 temp_canv = gr_create_canvas(screen_canv->cv_bitmap.bm_w,screen_canv->cv_bitmap.bm_h);
1871                 gr_set_current_canvas(temp_canv);
1872                 gr_ubitmap(0,0,&screen_canv->cv_bitmap);
1873         }
1874
1875         gr_set_current_canvas(save_canv);
1876
1877         if ( savenum > 99 ) savenum = 0;
1878         if ( stereo_savenum > 99 ) stereo_savenum = 0;
1879
1880         if ( stereo ) {
1881                 sprintf(savename,"left%02d.pcx",stereo_savenum);
1882                 sprintf(savename2,"right%02d.pcx",stereo_savenum);
1883                 if (VR_eye_switch) {char t[FILENAME_LEN]; strcpy(t,savename); strcpy(savename,savename2); strcpy(savename2,t);}
1884                 stereo_savenum++;
1885                 sprintf( message, "%s '%s' & '%s'", TXT_DUMPING_SCREEN, savename, savename2 );
1886         }
1887         else {
1888                 sprintf(savename,"screen%02d.pcx",savenum++);
1889                 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
1890         }
1891
1892         if (!automap_flag)              //if from automap, curcanv is already visible canv
1893                 gr_set_current_canvas(NULL);
1894         modex_flag = (grd_curcanv->cv_bitmap.bm_type==BM_MODEX);
1895         if (!automap_flag && modex_flag)
1896                 gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1897
1898         save_font = grd_curcanv->cv_font;
1899         gr_set_curfont(GAME_FONT);
1900         gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
1901         gr_get_string_size(message,&w,&h,&aw);
1902
1903         if (modex_flag)
1904                 h *= 2;
1905
1906         //I changed how these coords were calculated for the high-res automap. -MT
1907         //x = (VR_screen_pages[VR_current_page].cv_w-w)/2;
1908         //y = (VR_screen_pages[VR_current_page].cv_h-h)/2;
1909         x = (grd_curcanv->cv_w-w)/2;
1910         y = (grd_curcanv->cv_h-h)/2;
1911
1912         if (modex_flag) {
1913                 modex_clear_box(x-2,y-2,w+4,h+4);
1914                 modex_printf(x, y, message,GAME_FONT,gr_find_closest_color_current(0,31,0));
1915         } else {
1916                 gr_setcolor(gr_find_closest_color_current(0,0,0));
1917                 gr_rect(x-2,y-2,x+w+2,y+h+2);
1918                 gr_printf(x,y,message);
1919                 gr_set_curfont(save_font);
1920         }
1921         t1 = timer_get_fixed_seconds() + F1_0;
1922
1923         gr_palette_read(pal);           //get actual palette from the hardware
1924         pcx_write_bitmap(savename,&temp_canv->cv_bitmap,pal);
1925         if ( stereo )
1926                 pcx_write_bitmap(savename2,&temp_canv2->cv_bitmap,pal);
1927
1928         while ( timer_get_fixed_seconds() < t1 );               // Wait so that messag stays up at least 1 second.
1929
1930         gr_set_current_canvas(screen_canv);
1931
1932         if (grd_curcanv->cv_bitmap.bm_type!=BM_MODEX && !stereo)
1933                 gr_ubitmap(0,0,&temp_canv->cv_bitmap);
1934
1935         gr_free_canvas(temp_canv);
1936         if ( stereo )
1937                 gr_free_canvas(temp_canv2);
1938
1939         gr_set_current_canvas(save_canv);
1940         key_flush();
1941         start_time();
1942         
1943 #else
1944
1945         grs_canvas *screen_canv = &grd_curscreen->sc_canvas;
1946         grs_canvas *temp_canv, *save_canv;
1947         
1948         // Can't do screen shots in VR modes.
1949         if ( VR_render_mode != VR_NONE )
1950                 return;
1951
1952         stop_time();
1953
1954         save_canv = grd_curcanv;        
1955         temp_canv = gr_create_canvas( screen_canv->cv_bitmap.bm_w, screen_canv->cv_bitmap.bm_h );
1956         if (!temp_canv)
1957                 goto shot_done;
1958         gr_set_current_canvas( temp_canv );
1959         gr_ubitmap( 0, 0, &screen_canv->cv_bitmap );
1960         gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1961
1962         show_cursor();
1963         key_close();
1964         if (Game_mode & GM_MULTI)
1965                 SavePictScreen(1);
1966         else
1967                 SavePictScreen(0);
1968         key_init();
1969         hide_cursor();
1970
1971         gr_set_current_canvas(screen_canv);
1972         
1973 //      if (!automap_flag)
1974                 gr_ubitmap( 0, 0, &temp_canv->cv_bitmap);
1975
1976         gr_free_canvas(temp_canv);
1977 shot_done:
1978         gr_set_current_canvas(save_canv);
1979         key_flush();
1980         start_time();
1981         #endif
1982 }
1983
1984 #endif
1985
1986 //initialize flying
1987 void fly_init(object *obj)
1988 {
1989         obj->control_type = CT_FLYING;
1990         obj->movement_type = MT_PHYSICS;
1991
1992         vm_vec_zero(&obj->mtype.phys_info.velocity);
1993         vm_vec_zero(&obj->mtype.phys_info.thrust);
1994         vm_vec_zero(&obj->mtype.phys_info.rotvel);
1995         vm_vec_zero(&obj->mtype.phys_info.rotthrust);
1996 }
1997
1998 //void morph_test(), morph_step();
1999
2000
2001 //      ------------------------------------------------------------------------------------
2002
2003 void test_anim_states();
2004
2005 #include "fvi.h"
2006
2007 //put up the help message
2008 void do_show_help()
2009 {
2010         show_help();
2011 }
2012
2013
2014 extern int been_in_editor;
2015
2016 //      ------------------------------------------------------------------------------------
2017 void do_cloak_stuff(void)
2018 {
2019         int i;
2020         for (i = 0; i < N_players; i++)
2021                 if (Players[i].flags & PLAYER_FLAGS_CLOAKED) {
2022                         // mprintf(0, "Cloak time left: %7.3f\n", f2fl(CLOAK_TIME_MAX - (GameTime - Players[Player_num].cloak_time)));
2023                         if (GameTime - Players[i].cloak_time > CLOAK_TIME_MAX) {
2024                                 Players[i].flags &= ~PLAYER_FLAGS_CLOAKED;
2025                                 if (i == Player_num) {
2026                                         digi_play_sample( SOUND_CLOAK_OFF, F1_0);
2027                                         #ifdef NETWORK
2028                                         if (Game_mode & GM_MULTI)
2029                                                 multi_send_play_sound(SOUND_CLOAK_OFF, F1_0);
2030                                         maybe_drop_net_powerup(POW_CLOAK);
2031                                         multi_send_decloak(); // For demo recording
2032                                         #endif
2033 //                                      mprintf((0, " --- You have been DE-CLOAKED! ---\n"));
2034                                 }
2035                         }
2036                 }
2037 }
2038
2039 int FakingInvul=0;
2040
2041 //      ------------------------------------------------------------------------------------
2042 void do_invulnerable_stuff(void)
2043 {
2044         if (Players[Player_num].flags & PLAYER_FLAGS_INVULNERABLE) {
2045                 if (GameTime - Players[Player_num].invulnerable_time > INVULNERABLE_TIME_MAX) {
2046                         Players[Player_num].flags ^= PLAYER_FLAGS_INVULNERABLE;
2047                         if (FakingInvul==0)
2048                         {
2049                                 digi_play_sample( SOUND_INVULNERABILITY_OFF, F1_0);
2050                                 #ifdef NETWORK
2051                                 if (Game_mode & GM_MULTI)
2052                                 {
2053                                         multi_send_play_sound(SOUND_INVULNERABILITY_OFF, F1_0);
2054                                         maybe_drop_net_powerup(POW_INVULNERABILITY);
2055                                 }
2056                                 #endif
2057                                 mprintf((0, " --- You have been DE-INVULNERABLEIZED! ---\n"));
2058                         }
2059                         FakingInvul=0;
2060                 }
2061         }
2062 }
2063
2064 ubyte   Last_afterburner_state = 0;
2065 fix Last_afterburner_charge = 0;
2066
2067 #define AFTERBURNER_LOOP_START  ((digi_sample_rate==SAMPLE_RATE_22K)?32027:(32027/2))           //20098
2068 #define AFTERBURNER_LOOP_END            ((digi_sample_rate==SAMPLE_RATE_22K)?48452:(48452/2))           //25776
2069
2070 int     Ab_scale = 4;
2071
2072 //@@//  ------------------------------------------------------------------------------------
2073 //@@void afterburner_shake(void)
2074 //@@{
2075 //@@    int     rx, rz;
2076 //@@
2077 //@@    rx = (Ab_scale * fixmul(d_rand() - 16384, F1_0/8 + (((GameTime + 0x4000)*4) & 0x3fff)))/16;
2078 //@@    rz = (Ab_scale * fixmul(d_rand() - 16384, F1_0/2 + ((GameTime*4) & 0xffff)))/16;
2079 //@@
2080 //@@    // -- mprintf((0, "AB: %8x %8x\n", rx, rz));
2081 //@@    ConsoleObject->mtype.phys_info.rotvel.x += rx;
2082 //@@    ConsoleObject->mtype.phys_info.rotvel.z += rz;
2083 //@@
2084 //@@}
2085
2086 //      ------------------------------------------------------------------------------------
2087 #ifdef NETWORK
2088 extern void multi_send_sound_function (char,char);
2089 #endif
2090
2091 void do_afterburner_stuff(void)
2092 {
2093    if (!(Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER))
2094                 Afterburner_charge=0;
2095
2096         if (Endlevel_sequence || Player_is_dead)
2097                 {
2098                  digi_kill_sound_linked_to_object( Players[Player_num].objnum);
2099 #ifdef NETWORK
2100                  multi_send_sound_function (0,0);
2101 #endif
2102                 }
2103
2104         if ((Controls.afterburner_state != Last_afterburner_state && Last_afterburner_charge) || (Last_afterburner_state && Last_afterburner_charge && !Afterburner_charge)) {
2105
2106                 if (Afterburner_charge && Controls.afterburner_state && (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER)) {
2107                         digi_link_sound_to_object3( SOUND_AFTERBURNER_IGNITE, Players[Player_num].objnum, 1, F1_0, i2f(256), AFTERBURNER_LOOP_START, AFTERBURNER_LOOP_END );
2108 #ifdef NETWORK
2109                         if (Game_mode & GM_MULTI)
2110                                 multi_send_sound_function (3,SOUND_AFTERBURNER_IGNITE);
2111 #endif
2112                 } else {
2113                         digi_kill_sound_linked_to_object( Players[Player_num].objnum);
2114                         digi_link_sound_to_object2( SOUND_AFTERBURNER_PLAY, Players[Player_num].objnum, 0, F1_0, i2f(256));
2115 #ifdef NETWORK
2116                         if (Game_mode & GM_MULTI)
2117                                 multi_send_sound_function (0,0);
2118 #endif
2119                         mprintf((0,"Killing afterburner sound\n"));
2120                 }
2121         }
2122
2123         //@@if (Controls.afterburner_state && Afterburner_charge)
2124         //@@    afterburner_shake();
2125
2126         Last_afterburner_state = Controls.afterburner_state;
2127         Last_afterburner_charge = Afterburner_charge;
2128 }
2129
2130 // -- //        ------------------------------------------------------------------------------------
2131 // -- //        if energy < F1_0/2, recharge up to F1_0/2
2132 // -- void recharge_energy_frame(void)
2133 // -- {
2134 // --   if (Players[Player_num].energy < Weapon_info[0].energy_usage) {
2135 // --           Players[Player_num].energy += FrameTime/4;
2136 // --
2137 // --           if (Players[Player_num].energy > Weapon_info[0].energy_usage)
2138 // --                   Players[Player_num].energy = Weapon_info[0].energy_usage;
2139 // --   }
2140 // -- }
2141
2142 //      Amount to diminish guns towards normal, per second.
2143 #define DIMINISH_RATE   16              //      gots to be a power of 2, else change the code in diminish_palette_towards_normal
2144
2145 extern fix Flash_effect;
2146
2147  //adds to rgb values for palette flash
2148 void PALETTE_FLASH_ADD(int _dr,int _dg,int _db)
2149 {
2150         int     maxval;
2151
2152         PaletteRedAdd += _dr;
2153         PaletteGreenAdd += _dg;
2154         PaletteBlueAdd += _db;
2155
2156         // -- mprintf((0, "Palette add: %3i %3i %3i\n", PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd));
2157
2158         if (Flash_effect)
2159                 maxval = 60;
2160         else
2161                 maxval = MAX_PALETTE_ADD;
2162
2163         if (PaletteRedAdd > maxval)
2164                 PaletteRedAdd = maxval;
2165
2166         if (PaletteGreenAdd > maxval)
2167                 PaletteGreenAdd = maxval;
2168
2169         if (PaletteBlueAdd > maxval)
2170                 PaletteBlueAdd = maxval;
2171
2172         if (PaletteRedAdd < -maxval)
2173                 PaletteRedAdd = -maxval;
2174
2175         if (PaletteGreenAdd < -maxval)
2176                 PaletteGreenAdd = -maxval;
2177
2178         if (PaletteBlueAdd < -maxval)
2179                 PaletteBlueAdd = -maxval;
2180 }
2181
2182 fix     Time_flash_last_played;
2183
2184
2185 void game_palette_step_up( int r, int g, int b );
2186 //      ------------------------------------------------------------------------------------
2187 //      Diminish palette effects towards normal.
2188 void diminish_palette_towards_normal(void)
2189 {
2190         int     dec_amount = 0;
2191
2192         //      Diminish at DIMINISH_RATE units/second.
2193         //      For frame rates > DIMINISH_RATE Hz, use randomness to achieve this.
2194         if (FrameTime < F1_0/DIMINISH_RATE) {
2195                 if (d_rand() < FrameTime*DIMINISH_RATE/2)       //      Note: d_rand() is in 0..32767, and 8 Hz means decrement every frame
2196                         dec_amount = 1;
2197         } else {
2198                 dec_amount = f2i(FrameTime*DIMINISH_RATE);              // one second = DIMINISH_RATE counts
2199                 if (dec_amount == 0)
2200                         dec_amount++;                                           // make sure we decrement by something
2201         }
2202
2203         if (Flash_effect) {
2204                 int     force_do = 0;
2205
2206                 //      Part of hack system to force update of palette after exiting a menu.
2207                 if (Time_flash_last_played) {
2208                         force_do = 1;
2209                         PaletteRedAdd ^= 1;     //      Very Tricky!  In gr_palette_step_up, if all stepups same as last time, won't do anything!
2210                 }
2211
2212                 if ((Time_flash_last_played + F1_0/8 < GameTime) || (Time_flash_last_played > GameTime)) {
2213                         digi_play_sample( SOUND_CLOAK_OFF, Flash_effect/4);
2214                         Time_flash_last_played = GameTime;
2215                 }
2216
2217                 Flash_effect -= FrameTime;
2218                 if (Flash_effect < 0)
2219                         Flash_effect = 0;
2220
2221                 if (force_do || (d_rand() > 4096 )) {
2222         if ( (Newdemo_state==ND_STATE_RECORDING) && (PaletteRedAdd || PaletteGreenAdd || PaletteBlueAdd) )
2223                 newdemo_record_palette_effect(PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2224
2225                         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2226
2227                         return;
2228                 }
2229
2230         }
2231
2232         if (PaletteRedAdd > 0 ) { PaletteRedAdd -= dec_amount; if (PaletteRedAdd < 0 ) PaletteRedAdd = 0; }
2233         if (PaletteRedAdd < 0 ) { PaletteRedAdd += dec_amount; if (PaletteRedAdd > 0 ) PaletteRedAdd = 0; }
2234
2235         if (PaletteGreenAdd > 0 ) { PaletteGreenAdd -= dec_amount; if (PaletteGreenAdd < 0 ) PaletteGreenAdd = 0; }
2236         if (PaletteGreenAdd < 0 ) { PaletteGreenAdd += dec_amount; if (PaletteGreenAdd > 0 ) PaletteGreenAdd = 0; }
2237
2238         if (PaletteBlueAdd > 0 ) { PaletteBlueAdd -= dec_amount; if (PaletteBlueAdd < 0 ) PaletteBlueAdd = 0; }
2239         if (PaletteBlueAdd < 0 ) { PaletteBlueAdd += dec_amount; if (PaletteBlueAdd > 0 ) PaletteBlueAdd = 0; }
2240
2241         if ( (Newdemo_state==ND_STATE_RECORDING) && (PaletteRedAdd || PaletteGreenAdd || PaletteBlueAdd) )
2242                 newdemo_record_palette_effect(PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2243
2244         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2245
2246         //mprintf(0, "%2i %2i %2i\n", PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2247 }
2248
2249 int     Redsave, Bluesave, Greensave;
2250
2251 void palette_save(void)
2252 {
2253         Redsave = PaletteRedAdd; Bluesave = PaletteBlueAdd; Greensave = PaletteGreenAdd;
2254 }
2255
2256 extern void gr_palette_step_up_vr( int r, int g, int b, int white, int black );
2257
2258 void game_palette_step_up( int r, int g, int b )
2259 {
2260         if ( VR_use_reg_code )  {
2261         #ifndef WINDOWS
2262 //              gr_palette_step_up_vr( r, g, b, VR_WHITE_INDEX, VR_BLACK_INDEX );
2263         #endif
2264         } else {
2265                 gr_palette_step_up( r, g, b );
2266         }
2267 }
2268
2269 void palette_restore(void)
2270 {
2271         PaletteRedAdd = Redsave; PaletteBlueAdd = Bluesave; PaletteGreenAdd = Greensave;
2272         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2273
2274         //      Forces flash effect to fixup palette next frame.
2275         Time_flash_last_played = 0;
2276 }
2277
2278 extern void dead_player_frame(void);
2279
2280
2281 //      --------------------------------------------------------------------------------------------------
2282 int allowed_to_fire_laser(void)
2283 {
2284         if (Player_is_dead) {
2285                 Global_missile_firing_count = 0;
2286                 return 0;
2287         }
2288
2289         //      Make sure enough time has elapsed to fire laser, but if it looks like it will
2290         //      be a long while before laser can be fired, then there must be some mistake!
2291         if (Next_laser_fire_time > GameTime)
2292                 if (Next_laser_fire_time < GameTime + 2*F1_0)
2293                         return 0;
2294
2295         return 1;
2296 }
2297
2298 fix     Next_flare_fire_time = 0;
2299 #define FLARE_BIG_DELAY (F1_0*2)
2300
2301 int allowed_to_fire_flare(void)
2302 {
2303         if (Next_flare_fire_time > GameTime)
2304                 if (Next_flare_fire_time < GameTime + FLARE_BIG_DELAY)  //      In case time is bogus, never wait > 1 second.
2305                         return 0;
2306
2307         if (Players[Player_num].energy >= Weapon_info[FLARE_ID].energy_usage)
2308                 Next_flare_fire_time = GameTime + F1_0/4;
2309         else
2310                 Next_flare_fire_time = GameTime + FLARE_BIG_DELAY;
2311
2312         return 1;
2313 }
2314
2315 int allowed_to_fire_missile(void)
2316 {
2317 // mprintf(0, "Next fire = %7.3f, Cur time = %7.3f\n", f2fl(Next_missile_fire_time), f2fl(GameTime));
2318         //      Make sure enough time has elapsed to fire missile, but if it looks like it will
2319         //      be a long while before missile can be fired, then there must be some mistake!
2320         if (Next_missile_fire_time > GameTime)
2321                 if (Next_missile_fire_time < GameTime + 5*F1_0)
2322                         return 0;
2323
2324         return 1;
2325 }
2326
2327 void full_palette_save(void)
2328 {
2329         palette_save();
2330         apply_modified_palette();
2331         reset_palette_add();
2332         gr_palette_load( gr_palette );
2333 }
2334
2335 extern int Death_sequence_aborted;
2336 extern int newmenu_dotiny2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) );
2337
2338 void show_help()
2339 {
2340         int nitems;
2341         newmenu_item m[25];
2342         #ifdef MACINTOSH
2343         char command_help[64], pixel_double_help[64], save_help[64], restore_help[64];
2344         #endif
2345
2346         m[ 0].type = NM_TYPE_TEXT; m[ 0].text = TXT_HELP_ESC;
2347         #ifndef MACINTOSH
2348         m[ 1].type = NM_TYPE_TEXT; m[ 1].text = TXT_HELP_ALT_F2;
2349         m[ 2].type = NM_TYPE_TEXT; m[ 2].text = TXT_HELP_ALT_F3;
2350         #else
2351         sprintf(save_help, "OPT-F2 (%c-s)\t Save Game", 133);
2352         sprintf(restore_help, "OPT-F3 (%c-o)\t Load Game", 133);
2353         m[ 1].type = NM_TYPE_TEXT; m[1].text = save_help;
2354         m[ 2].type = NM_TYPE_TEXT; m[2].text = restore_help;
2355         #endif
2356         m[ 3].type = NM_TYPE_TEXT; m[ 3].text = TXT_HELP_F2;
2357         m[ 4].type = NM_TYPE_TEXT; m[ 4].text = TXT_HELP_F3;
2358         m[ 5].type = NM_TYPE_TEXT; m[ 5].text = TXT_HELP_F4;
2359         m[ 6].type = NM_TYPE_TEXT; m[ 6].text = TXT_HELP_F5;
2360         #ifndef MACINTOSH
2361         m[ 7].type = NM_TYPE_TEXT; m[ 7].text = TXT_HELP_PAUSE;
2362         #else
2363         m[ 7].type = NM_TYPE_TEXT; m[ 7].text = "Pause (F15)\t  Pause";
2364         #endif
2365         m[ 8].type = NM_TYPE_TEXT; m[ 8].text = TXT_HELP_MINUSPLUS;
2366         #ifndef MACINTOSH
2367         m[ 9].type = NM_TYPE_TEXT; m[ 9].text = TXT_HELP_PRTSCN;
2368         #else
2369         m[ 9].type = NM_TYPE_TEXT; m[ 9].text = "printscrn (F13)\t  save screen shot";
2370         #endif
2371         m[10].type = NM_TYPE_TEXT; m[10].text = TXT_HELP_1TO5;
2372         m[11].type = NM_TYPE_TEXT; m[11].text = TXT_HELP_6TO10;
2373         m[12].type = NM_TYPE_TEXT; m[12].text = "Shift-F1\t  Cycle left window";
2374         m[13].type = NM_TYPE_TEXT; m[13].text = "Shift-F2\t  Cycle right window";
2375         m[14].type = NM_TYPE_TEXT; m[14].text = "Shift-F4\t  GuideBot menu";
2376         #ifndef MACINTOSH
2377         m[15].type = NM_TYPE_TEXT; m[15].text = "Alt-Shift-F4\t  Rename GuideBot";
2378         #else
2379         m[15].type = NM_TYPE_TEXT; m[15].text = "Opt-Shift-F4\t  Rename GuideBot";
2380         #endif
2381         m[16].type = NM_TYPE_TEXT; m[16].text = "Shift-F5\t  Drop primary";
2382         m[17].type = NM_TYPE_TEXT; m[17].text = "Shift-F6\t  Drop secondary";
2383         m[18].type = NM_TYPE_TEXT; m[18].text = "Shift-F7\t  Calibrate joystick";
2384         m[19].type = NM_TYPE_TEXT; m[19].text = "Shift-number\t  GuideBot commands";
2385         nitems = 20;
2386         #ifdef MACINTOSH
2387         sprintf(pixel_double_help, "%c-D\t  Toggle Pixel Double Mode", 133);
2388         m[20].type = NM_TYPE_TEXT; m[20].text = pixel_double_help;
2389         m[21].type = NM_TYPE_TEXT; m[21].text = "";
2390         sprintf(command_help, "(Use %c-# for F#. i.e. %c-1 for F1)", 133, 133);
2391         m[22].type = NM_TYPE_TEXT; m[22].text = command_help;
2392         nitems = 23;
2393         #endif
2394
2395         full_palette_save();
2396
2397         newmenu_dotiny2( NULL, TXT_KEYS, nitems, m, NULL );
2398
2399         palette_restore();
2400 }
2401
2402 //temp function until Matt cleans up game sequencing
2403 extern void temp_reset_stuff_on_level();
2404
2405 //deal with rear view - switch it on, or off, or whatever
2406 void check_rear_view()
2407 {
2408
2409         #define LEAVE_TIME 0x1000               //how long until we decide key is down  (Used to be 0x4000)
2410
2411         static int leave_mode;
2412         static fix entry_time;
2413
2414         if ( Controls.rear_view_down_count )    {               //key/button has gone down
2415
2416                 if (Rear_view) {
2417                         Rear_view = 0;
2418                         if (Cockpit_mode==CM_REAR_VIEW) {
2419                                 select_cockpit(Cockpit_mode_save);
2420                                 Cockpit_mode_save = -1;
2421                         }
2422                         if (Newdemo_state == ND_STATE_RECORDING)
2423                                 newdemo_record_restore_rearview();
2424                 }
2425                 else {
2426                         Rear_view = 1;
2427                         leave_mode = 0;         //means wait for another key
2428                         entry_time = timer_get_fixed_seconds();
2429                         if (Cockpit_mode == CM_FULL_COCKPIT) {
2430                                 Cockpit_mode_save = Cockpit_mode;
2431                                 select_cockpit(CM_REAR_VIEW);
2432                         }
2433                         if (Newdemo_state == ND_STATE_RECORDING)
2434                                 newdemo_record_rearview();
2435                 }
2436         }
2437         else
2438                 if (Controls.rear_view_down_state) {
2439
2440                         if (leave_mode==0 && (timer_get_fixed_seconds()-entry_time)>LEAVE_TIME)
2441                                 leave_mode = 1;
2442                 }
2443                 else {
2444
2445                         //@@if (leave_mode==1 && Cockpit_mode==CM_REAR_VIEW) {
2446
2447                         if (leave_mode==1 && Rear_view) {
2448                                 Rear_view = 0;
2449                                 if (Cockpit_mode==CM_REAR_VIEW) {
2450                                         select_cockpit(Cockpit_mode_save);
2451                                         Cockpit_mode_save = -1;
2452                                 }
2453                                 if (Newdemo_state == ND_STATE_RECORDING)
2454                                         newdemo_record_restore_rearview();
2455                         }
2456                 }
2457 }
2458
2459 void reset_rear_view(void)
2460 {
2461         if (Rear_view) {
2462                 if (Newdemo_state == ND_STATE_RECORDING)
2463                         newdemo_record_restore_rearview();
2464         }
2465
2466         Rear_view = 0;
2467
2468         if (!(Cockpit_mode == CM_FULL_COCKPIT || Cockpit_mode == CM_STATUS_BAR || Cockpit_mode == CM_FULL_SCREEN)) {
2469                 if (!(Cockpit_mode_save == CM_FULL_COCKPIT || Cockpit_mode_save == CM_STATUS_BAR || Cockpit_mode_save == CM_FULL_SCREEN))
2470                         Cockpit_mode_save = CM_FULL_COCKPIT;
2471                 select_cockpit(Cockpit_mode_save);
2472                 Cockpit_mode_save       = -1;
2473         }
2474
2475 }
2476
2477 int Automap_flag;
2478 int Config_menu_flag;
2479
2480 jmp_buf LeaveGame;
2481
2482 int gr_renderstats = 0;
2483 // need to define "cheat" for renderstats
2484 int gr_badtexture = 0;
2485 // need to define "cheat" for badtexture
2486
2487 int Cheats_enabled=0;
2488
2489 extern int Laser_rapid_fire;
2490 extern void do_lunacy_on(), do_lunacy_off();
2491
2492 extern int Physics_cheat_flag,Robots_kill_robots_cheat;
2493 extern char BounceCheat,HomingCheat,OldHomingState[20];
2494 extern char AcidCheatOn,old_IntMethod, Monster_mode;
2495 extern int Buddy_dude_cheat;
2496
2497 //turns off active cheats
2498 void turn_cheats_off()
2499 {
2500         int i;
2501
2502         if (HomingCheat)
2503                 for (i=0;i<20;i++)
2504                         Weapon_info[i].homing_flag=OldHomingState[i];
2505
2506         if (AcidCheatOn)
2507         {
2508                 AcidCheatOn=0;
2509                 Interpolation_method=old_IntMethod;
2510         }
2511
2512         Buddy_dude_cheat = 0;
2513         BounceCheat=0;
2514    HomingCheat=0;
2515         do_lunacy_off();
2516         Laser_rapid_fire = 0;
2517         Physics_cheat_flag = 0;
2518         Monster_mode = 0;
2519         Robots_kill_robots_cheat=0;
2520         Robot_firing_enabled = 1;
2521 }
2522
2523 //turns off all cheats & resets cheater flag    
2524 void game_disable_cheats()
2525 {
2526         turn_cheats_off();
2527         Cheats_enabled=0;
2528 }
2529
2530
2531 //      game_setup()
2532 // ----------------------------------------------------------------------------
2533
2534 void game_setup(void)
2535 {
2536         //@@int demo_playing=0;
2537         //@@int multi_game=0;
2538
2539         do_lunacy_on();         //      Copy values for insane into copy buffer in ai.c
2540         do_lunacy_off();                //      Restore true insane mode.
2541
2542         Game_aborted = 0;
2543         last_drawn_cockpit[0] = -1;                             // Force cockpit to redraw next time a frame renders.
2544         last_drawn_cockpit[1] = -1;                             // Force cockpit to redraw next time a frame renders.
2545         Endlevel_sequence = 0;
2546
2547         //@@if ( Newdemo_state == ND_STATE_PLAYBACK )
2548         //@@    demo_playing = 1;
2549         //@@if ( Game_mode & GM_MULTI )
2550         //@@    multi_game = 1;
2551
2552         set_screen_mode(SCREEN_GAME);
2553         reset_palette_add();
2554
2555         set_warn_func(game_show_warning);
2556
2557         init_cockpit();
2558         init_gauges();
2559         //digi_init_sounds();
2560
2561         //keyd_repeat = 0;                // Don't allow repeat in game
2562         keyd_repeat = 1;                // Do allow repeat in game
2563
2564 #if !defined(WINDOWS) && !defined(MACINTOSH)
2565         //_MARK_("start of game");
2566 #endif
2567
2568         #ifdef EDITOR
2569                 if (Segments[ConsoleObject->segnum].segnum == -1)      //segment no longer exists
2570                         obj_relink( ConsoleObject-Objects, SEG_PTR_2_NUM(Cursegp) );
2571
2572                 if (!check_obj_seg(ConsoleObject))
2573                         move_player_2_segment(Cursegp,Curside);
2574         #endif
2575
2576         Viewer = ConsoleObject;
2577         fly_init(ConsoleObject);
2578
2579         Game_suspended = 0;
2580
2581         reset_time();
2582         FrameTime = 0;                  //make first frame zero
2583
2584         #ifdef EDITOR
2585         if (Current_level_num == 0) {                   //not a real level
2586                 init_player_stats_game();
2587                 init_ai_objects();
2588         }
2589         #endif
2590
2591         fix_object_segs();
2592
2593         game_flush_inputs();
2594
2595 }
2596
2597
2598 #ifdef NETWORK
2599 extern char IWasKicked;
2600 #endif
2601
2602
2603 //      ------------------------------------------------------------------------------------
2604 //this function is the game.  called when game mode selected.  runs until
2605 //editor mode or exit selected
2606 void game()
2607 {
2608         game_setup();                                                           // Replaces what was here earlier.
2609                                                                                                         // Good for Windows Sake.
2610
2611 #ifdef MWPROFILE
2612         ProfilerSetStatus(1);
2613 #endif
2614
2615         if ( setjmp(LeaveGame)==0 )     {
2616                 while (1) {
2617                         int player_shields;
2618
2619                         // GAME LOOP!
2620                         Automap_flag = 0;
2621                         Config_menu_flag = 0;
2622
2623                         if ( ConsoleObject != &Objects[Players[Player_num].objnum] )
2624                           {
2625                             mprintf ((0,"Player_num=%d objnum=%d",Player_num,Players[Player_num].objnum));
2626                             //Assert( ConsoleObject == &Objects[Players[Player_num].objnum] );
2627                           }
2628
2629                         player_shields = Players[Player_num].shields;
2630
2631                 #ifdef WINDOWS
2632                 {
2633                         MSG msg;
2634                         DoMessageStuff(&msg);           // Do Windows event handling.
2635                         if (_RedrawScreen) {
2636                                 _RedrawScreen = FALSE;
2637                                 load_palette(Current_level_palette,1,1);
2638                                 gr_palette_load(gr_palette);
2639                         }
2640                 }
2641                 #endif          
2642
2643                         ExtGameStatus=GAMESTAT_RUNNING;
2644                         GameLoop( 1, 1 );               // Do game loop with rendering and reading controls.
2645
2646                         //if the player is taking damage, give up guided missile control
2647                         if (Players[Player_num].shields != player_shields)
2648                                 release_guided_missile(Player_num);
2649
2650                         //see if redbook song needs to be restarted
2651                         songs_check_redbook_repeat();   // Handle RedBook Audio Repeating.
2652
2653                         if (Config_menu_flag)   {
2654                                 int double_save = Scanline_double;
2655
2656                                 //WIN(mouse_set_mode(0));
2657                                 if (!(Game_mode&GM_MULTI)) {palette_save(); reset_palette_add();        apply_modified_palette(); gr_palette_load( gr_palette ); }
2658                                 do_options_menu();
2659                                 if (Scanline_double != double_save)     init_cockpit();
2660                                 if (!(Game_mode&GM_MULTI)) palette_restore();
2661                                 //WIN(mouse_set_mode(1));
2662                         }
2663
2664                         if (Automap_flag) {
2665                                 int save_w=Game_window_w,save_h=Game_window_h;
2666                                 do_automap(0);
2667                                 Screen_mode=-1; set_screen_mode(SCREEN_GAME);
2668                                 Game_window_w=save_w; Game_window_h=save_h;
2669                                 init_cockpit();
2670                                 last_drawn_cockpit[0] = -1;
2671                                 last_drawn_cockpit[1] = -1;
2672                         }
2673
2674                         if ( (Function_mode != FMODE_GAME) && Auto_demo && (Newdemo_state != ND_STATE_NORMAL) ) {
2675                                 int choice, fmode;
2676                                 fmode = Function_mode;
2677                                 Function_mode = FMODE_GAME;
2678                                 palette_save();
2679                                 apply_modified_palette();
2680                                 reset_palette_add();
2681                                 gr_palette_load( gr_palette );
2682                                 choice=nm_messagebox( NULL, 2, TXT_YES, TXT_NO, TXT_ABORT_AUTODEMO );
2683                                 palette_restore();
2684                                 Function_mode = fmode;
2685                                 if (choice==0)  {
2686                                         Auto_demo = 0;
2687                                         newdemo_stop_playback();
2688                                         Function_mode = FMODE_MENU;
2689                                 } else {
2690                                         Function_mode = FMODE_GAME;
2691                                 }
2692                         }
2693
2694                         if ( (Function_mode != FMODE_GAME ) && (Newdemo_state != ND_STATE_PLAYBACK ) && (Function_mode!=FMODE_EDITOR)
2695 #ifdef NETWORK
2696                                         && !IWasKicked
2697 #endif
2698                            )            {
2699                                 int choice, fmode;
2700                                 fmode = Function_mode;
2701                                 Function_mode = FMODE_GAME;
2702                                 palette_save();
2703                                 apply_modified_palette();
2704                                 reset_palette_add();
2705                                 gr_palette_load( gr_palette );
2706                                 ExtGameStatus=GAMESTAT_ABORT_GAME;
2707                                 choice=nm_messagebox( NULL, 2, TXT_YES, TXT_NO, TXT_ABORT_GAME );
2708                                 palette_restore();
2709                                 Function_mode = fmode;
2710                                 if (choice != 0)
2711                                         Function_mode = FMODE_GAME;
2712                         }
2713
2714 #ifdef NETWORK
2715                         IWasKicked=0;
2716 #endif
2717                         if (Function_mode != FMODE_GAME)
2718                                 longjmp(LeaveGame,0);
2719
2720                         #ifdef APPLE_DEMO
2721                         if ( (keyd_time_when_last_pressed + (F1_0 * 60)) < timer_get_fixed_seconds() )          // idle in game for 1 minutes means exit
2722                                 longjmp(LeaveGame,0);
2723                         #endif
2724                 }
2725         }
2726
2727 #ifdef MWPROFILE
2728         ProfilerSetStatus(0);
2729 #endif
2730
2731         digi_stop_all();
2732
2733         if ( (Newdemo_state == ND_STATE_RECORDING) || (Newdemo_state == ND_STATE_PAUSED) )
2734                 newdemo_stop_recording();
2735
2736         #ifdef NETWORK
2737         multi_leave_game();
2738         #endif
2739
2740         if ( Newdemo_state == ND_STATE_PLAYBACK )
2741                 newdemo_stop_playback();
2742
2743    if (Cockpit_mode_save!=-1)
2744          {
2745                 Cockpit_mode=Cockpit_mode_save;
2746                 Cockpit_mode_save=-1;           
2747          }
2748
2749         if (Function_mode != FMODE_EDITOR)
2750                 gr_palette_fade_out(gr_palette,32,0);                   // Fade out before going to menu
2751
2752 //@@    if ( (!demo_playing) && (!multi_game) && (Function_mode != FMODE_EDITOR))       {
2753 //@@            scores_maybe_add_player(Game_aborted);
2754 //@@    }
2755
2756 #if !defined(WINDOWS) && !defined(MACINTOSH)
2757         //_MARK_("end of game");
2758 #endif
2759
2760         clear_warn_func(game_show_warning);     //don't use this func anymore
2761
2762         game_disable_cheats();
2763
2764         #ifdef APPLE_DEMO
2765         Function_mode = FMODE_EXIT;             // get out of game in Apple OEM version
2766         #endif
2767 }
2768
2769 //called at the end of the program
2770 void close_game()
2771 {
2772 #ifdef WINDOWS
2773         if (dd_VR_offscreen_buffer) {
2774                 if (dd_grd_backcanv != dd_VR_offscreen_buffer) {
2775                         dd_gr_free_canvas(dd_VR_offscreen_buffer);
2776                 }
2777                 dd_VR_offscreen_buffer = NULL;
2778                 VR_offscreen_buffer = NULL;
2779         }
2780 #else
2781         if (VR_offscreen_buffer)        {
2782                 gr_free_canvas(VR_offscreen_buffer);
2783                 VR_offscreen_buffer = NULL;
2784         }
2785 #endif
2786
2787         close_gauge_canvases();
2788
2789         restore_effect_bitmap_icons();
2790
2791 #if !defined(MACINTOSH) && !defined(WINDOWS)
2792         if (Game_cockpit_copy_code)     {
2793                 d_free(Game_cockpit_copy_code);
2794                 Game_cockpit_copy_code = NULL;
2795         }
2796 #else
2797         if (Game_cockpit_copy_code)
2798                 Game_cockpit_copy_code = 0;
2799 #endif
2800
2801         if (background_bitmap.bm_data)
2802                 d_free(background_bitmap.bm_data);
2803
2804         clear_warn_func(game_show_warning);     //don't use this func anymore
2805 }
2806
2807 #ifdef WINDOWS
2808 dd_grs_canvas * get_current_game_screen()
2809 {
2810         return &dd_VR_screen_pages[VR_current_page];
2811 }
2812
2813 #else
2814
2815 grs_canvas * get_current_game_screen()
2816 {
2817         return &VR_screen_pages[VR_current_page];
2818 }
2819 #endif
2820
2821
2822 extern void kconfig_center_headset();
2823
2824
2825 #ifndef NDEBUG
2826 void    speedtest_frame(void);
2827 int     Debug_slowdown=0;
2828 #endif
2829
2830 #ifdef EDITOR
2831 extern void player_follow_path(object *objp);
2832 extern void check_create_player_path(void);
2833
2834 #endif
2835
2836 extern  int     Do_appearance_effect;
2837
2838 object *Missile_viewer=NULL;
2839
2840 int Missile_view_enabled = 1;
2841
2842 int Marker_viewer_num[2]={-1,-1};
2843 int Coop_view_player[2]={-1,-1};
2844 int Cockpit_3d_view[2]={CV_NONE,CV_NONE};
2845
2846 //returns ptr to escort robot, or NULL
2847 object *find_escort()
2848 {
2849         int i;
2850
2851         for (i=0; i<=Highest_object_index; i++)
2852                 if (Objects[i].type == OBJ_ROBOT)
2853                         if (Robot_info[Objects[i].id].companion)
2854                                 return &Objects[i];
2855
2856         return NULL;
2857 }
2858
2859 extern void process_super_mines_frame(void);
2860 extern void do_seismic_stuff(void);
2861
2862 #ifndef RELEASE
2863 int Saving_movie_frames=0;
2864 int __Movie_frame_num=0;
2865
2866 #define MAX_MOVIE_BUFFER_FRAMES 250
2867 #define MOVIE_FRAME_SIZE        (320 * 200)
2868
2869 ubyte *Movie_frame_buffer;
2870 int Movie_frame_counter;
2871 ubyte Movie_pal[768];
2872 char movie_path[50] = ".\\";
2873
2874 grs_bitmap Movie_bm;
2875
2876 void flush_movie_buffer()
2877 {
2878         char savename[128];
2879         int f;
2880
2881         stop_time();
2882
2883         mprintf((0,"Flushing movie buffer..."));
2884
2885         Movie_bm.bm_data = Movie_frame_buffer;
2886
2887         for (f=0;f<Movie_frame_counter;f++) {
2888                 sprintf(savename, "%sfrm%04d.pcx",movie_path,__Movie_frame_num);
2889                 __Movie_frame_num++;
2890                 pcx_write_bitmap(savename,&Movie_bm,Movie_pal);
2891                 Movie_bm.bm_data += MOVIE_FRAME_SIZE;
2892
2893                 if (f % 5 == 0)
2894                         mprintf((0,"%3d/%3d\10\10\10\10\10\10\10",f,Movie_frame_counter));
2895         }
2896
2897         Movie_frame_counter=0;
2898
2899         mprintf((0,"done   \n"));
2900
2901         start_time();
2902 }
2903
2904 void toggle_movie_saving()
2905 {
2906         int exit;
2907
2908         Saving_movie_frames = !Saving_movie_frames;
2909
2910         if (Saving_movie_frames) {
2911                 newmenu_item m[1];
2912
2913                 m[0].type=NM_TYPE_INPUT; m[0].text_len = 50; m[0].text = movie_path;
2914                 exit = newmenu_do( NULL, "Directory for movie frames?" , 1, &(m[0]), NULL );
2915
2916                 if (exit==-1) {
2917                         Saving_movie_frames = 0;
2918                         return;
2919                 }
2920
2921                 while (isspace(movie_path[strlen(movie_path)-1]))
2922                         movie_path[strlen(movie_path)-1] = 0;
2923                 if (movie_path[strlen(movie_path)-1]!='\\' && movie_path[strlen(movie_path)-1]!=':')
2924                         strcat(movie_path,"\\");
2925
2926
2927                 if (!Movie_frame_buffer) {
2928                         Movie_frame_buffer = d_malloc(MAX_MOVIE_BUFFER_FRAMES * MOVIE_FRAME_SIZE);
2929                         if (!Movie_frame_buffer) {
2930                                 Int3();
2931                                 Saving_movie_frames=0;
2932                         }
2933
2934                         Movie_frame_counter=0;
2935
2936                         Movie_bm.bm_x = Movie_bm.bm_y = 0;
2937                         Movie_bm.bm_w = 320;
2938                         Movie_bm.bm_h = 200;
2939                         Movie_bm.bm_type = BM_LINEAR;
2940                         Movie_bm.bm_flags = 0;
2941                         Movie_bm.bm_rowsize = 320;
2942                         Movie_bm.bm_handle = 0;
2943
2944                         gr_palette_read(Movie_pal);             //get actual palette from the hardware
2945
2946                         if (Newdemo_state == ND_STATE_PLAYBACK)
2947                                 Newdemo_do_interpolate = 0;
2948                 }
2949         }
2950         else {
2951                 flush_movie_buffer();
2952
2953                 if (Newdemo_state == ND_STATE_PLAYBACK)
2954                         Newdemo_do_interpolate = 1;
2955         }
2956
2957 }
2958
2959 void save_movie_frame()
2960 {
2961         memcpy(Movie_frame_buffer+Movie_frame_counter*MOVIE_FRAME_SIZE,grd_curscreen->sc_canvas.cv_bitmap.bm_data,MOVIE_FRAME_SIZE);
2962
2963         Movie_frame_counter++;
2964
2965         if (Movie_frame_counter == MAX_MOVIE_BUFFER_FRAMES)
2966                 flush_movie_buffer();
2967
2968 }
2969
2970 #endif
2971
2972 extern int Level_shake_duration;
2973
2974 //if water or fire level, make occasional sound
2975 void do_ambient_sounds()
2976 {
2977         int has_water,has_lava;
2978         int sound;
2979
2980         has_lava = (Segment2s[ConsoleObject->segnum].s2_flags & S2F_AMBIENT_LAVA);
2981         has_water = (Segment2s[ConsoleObject->segnum].s2_flags & S2F_AMBIENT_WATER);
2982
2983         if (has_lava) {                                                 //has lava
2984                 sound = SOUND_AMBIENT_LAVA;
2985                 if (has_water && (d_rand() & 1))        //both, pick one
2986                         sound = SOUND_AMBIENT_WATER;
2987         }
2988         else if (has_water)                                             //just water
2989                 sound = SOUND_AMBIENT_WATER;
2990         else
2991                 return;
2992
2993         if (((d_rand() << 3) < FrameTime)) {                                            //play the sound
2994                 fix volume = d_rand() + f1_0/2;
2995                 digi_play_sample(sound,volume);
2996         }
2997 }
2998
2999 // -- extern void lightning_frame(void);
3000
3001 void game_render_frame();
3002 extern void omega_charge_frame(void);
3003
3004 extern time_t t_current_time, t_saved_time;
3005
3006 void flicker_lights();
3007
3008 void GameLoop(int RenderFlag, int ReadControlsFlag )
3009 {
3010         #ifndef NDEBUG
3011         //      Used to slow down frame rate for testing things.
3012         //      RenderFlag = 1; // DEBUG
3013         if (Debug_slowdown) {
3014                 int     h, i, j=0;
3015
3016                 for (h=0; h<Debug_slowdown; h++)
3017                         for (i=0; i<1000; i++)
3018                                 j += i;
3019         }
3020         #endif
3021
3022         #ifdef WINDOWS
3023         {
3024                 static int desc_dead_countdown=100;   /*  used if player shouldn't be playing */
3025
3026                 if (desc_id_exit_num) {                          // are we supposed to be checking
3027                         if (!(--desc_dead_countdown)) {// if so, at zero, then pull the plug
3028                                 char time_str[32], time_str2[32];
3029
3030                                 _ctime(&t_saved_time, time_str);
3031                                 _ctime(&t_current_time, time_str2);
3032
3033                                 Error ("EXPIRES %s.  YOUR TIME %s.\n", time_str, time_str2);
3034                                 Error ("Loading overlay -- error number: %d\n", (int)desc_id_exit_num);
3035                         }
3036                 }
3037         }
3038         #endif
3039
3040                 #ifndef RELEASE
3041                 if (FindArg("-invulnerability"))
3042                         Players[Player_num].flags |= PLAYER_FLAGS_INVULNERABLE;
3043                 #endif
3044
3045
3046                 update_player_stats();
3047                 diminish_palette_towards_normal();              //      Should leave palette effect up for as long as possible by putting right before render.
3048                 do_afterburner_stuff();
3049                 do_cloak_stuff();
3050                 do_invulnerable_stuff();
3051                 remove_obsolete_stuck_objects();
3052                 init_ai_frame();
3053                 do_final_boss_frame();
3054                 // -- lightning_frame();
3055                 // -- recharge_energy_frame();
3056
3057                 if ((Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) && (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON)) {
3058                         static int turned_off=0;
3059                         Players[Player_num].energy -= (FrameTime*3/8);
3060                         if (Players[Player_num].energy < i2f(10)) {
3061                                 if (!turned_off) {
3062                                         Players[Player_num].flags &= ~PLAYER_FLAGS_HEADLIGHT_ON;
3063                                         turned_off = 1;
3064 #ifdef NETWORK
3065                                         if (Game_mode & GM_MULTI)
3066                                                 multi_send_flags(Player_num);           
3067 #endif
3068                                 }
3069                         }
3070                         else
3071                                 turned_off = 0;
3072
3073                         if (Players[Player_num].energy <= 0) {
3074                                 Players[Player_num].energy = 0;
3075                                 Players[Player_num].flags &= ~PLAYER_FLAGS_HEADLIGHT_ON;
3076 #ifdef NETWORK
3077                                 if (Game_mode & GM_MULTI)
3078                                         multi_send_flags(Player_num);           
3079 #endif
3080                         }
3081                 }
3082
3083
3084                 #ifdef EDITOR
3085                 check_create_player_path();
3086                 player_follow_path(ConsoleObject);
3087                 #endif
3088
3089                 #ifdef NETWORK
3090                 if (Game_mode & GM_MULTI)
3091         {
3092          multi_do_frame();
3093          if (Netgame.PlayTimeAllowed && ThisLevelTime>=i2f((Netgame.PlayTimeAllowed*5*60)))
3094              multi_check_for_killgoal_winner();
3095         }
3096
3097                 #endif
3098
3099                 if (RenderFlag) {
3100                         if (force_cockpit_redraw) {                     //screen need redrawing?
3101                                 init_cockpit();
3102                                 force_cockpit_redraw=0;
3103                         }
3104                         game_render_frame();
3105                         //show_extra_views();           //missile view, buddy bot, etc.
3106
3107                         #ifndef RELEASE
3108                         if (Saving_movie_frames)
3109                                 save_movie_frame();
3110                         #endif
3111
3112                 }
3113
3114
3115                 //mprintf(0,"Velocity %2.2f\n", f2fl(vm_vec_mag(&ConsoleObject->phys_info.velocity)));
3116
3117                 calc_frame_time();
3118
3119                 dead_player_frame();
3120                 if (Newdemo_state != ND_STATE_PLAYBACK)
3121                         do_controlcen_dead_frame();
3122
3123                 process_super_mines_frame();
3124                 do_seismic_stuff();
3125                 do_ambient_sounds();
3126
3127                 #ifndef NDEBUG
3128                 if (Speedtest_on)
3129                         speedtest_frame();
3130                 #endif
3131
3132                 if (ReadControlsFlag)
3133                         ReadControls();
3134                 else
3135                         memset(&Controls, 0, sizeof(Controls));
3136
3137                 GameTime += FrameTime;
3138
3139                 if (f2i(GameTime)/10 != f2i(GameTime-FrameTime)/10)
3140                         mprintf((0,"Gametime = %d secs\n",f2i(GameTime)));
3141
3142                 if (GameTime < 0 || GameTime > i2f(0x7fff - 600)) {
3143                         GameTime = FrameTime;   //wrap when goes negative, or gets within 10 minutes
3144                         mprintf((0,"GameTime reset to 0\n"));
3145                 }
3146
3147                 #ifndef NDEBUG
3148                 if (FindArg("-checktime") != 0)
3149                         if (GameTime >= i2f(600))               //wrap after 10 minutes
3150                                 GameTime = FrameTime;
3151                 #endif
3152
3153 #ifdef NETWORK
3154       if ((Game_mode & GM_MULTI) && Netgame.PlayTimeAllowed)
3155           ThisLevelTime +=FrameTime;
3156 #endif
3157
3158                 digi_sync_sounds();
3159
3160                 if (Endlevel_sequence) {
3161                         do_endlevel_frame();
3162                         powerup_grab_cheat_all();
3163                         do_special_effects();
3164                         return;                                 //skip everything else
3165                 }
3166
3167                 if (Newdemo_state != ND_STATE_PLAYBACK)
3168                         do_exploding_wall_frame();
3169                 if ((Newdemo_state != ND_STATE_PLAYBACK) || (Newdemo_vcr_state != ND_STATE_PAUSED)) {
3170                         do_special_effects();
3171                         wall_frame_process();
3172                         triggers_frame_process();
3173                 }
3174
3175
3176                 if (Control_center_destroyed)   {
3177                         if (Newdemo_state==ND_STATE_RECORDING )
3178                                 newdemo_record_control_center_destroyed();
3179                 }
3180
3181                 flash_frame();
3182
3183                 if ( Newdemo_state == ND_STATE_PLAYBACK )       {
3184                         newdemo_playback_one_frame();
3185                         if ( Newdemo_state != ND_STATE_PLAYBACK )               {
3186                                 longjmp( LeaveGame, 0 );                // Go back to menu
3187                         }
3188                 } else
3189                 { // Note the link to above!
3190
3191                         Players[Player_num].homing_object_dist = -1;            //      Assume not being tracked.  Laser_do_weapon_sequence modifies this.
3192
3193                         object_move_all();
3194                         powerup_grab_cheat_all();
3195
3196                         if (Endlevel_sequence)  //might have been started during move
3197                                 return;
3198
3199                         fuelcen_update_all();
3200
3201                         do_ai_frame_all();
3202
3203                         if (allowed_to_fire_laser())
3204                                 FireLaser();                            // Fire Laser!
3205
3206                         if (Auto_fire_fusion_cannon_time) {
3207                                 if (Primary_weapon != FUSION_INDEX)
3208                                         Auto_fire_fusion_cannon_time = 0;
3209                                 else if (GameTime + FrameTime/2 >= Auto_fire_fusion_cannon_time) {
3210                                         Auto_fire_fusion_cannon_time = 0;
3211                                         Global_laser_firing_count = 1;
3212                                 } else {
3213                                         vms_vector      rand_vec;
3214                                         fix                     bump_amount;
3215
3216                                         Global_laser_firing_count = 0;
3217
3218                                         ConsoleObject->mtype.phys_info.rotvel.x += (d_rand() - 16384)/8;
3219                                         ConsoleObject->mtype.phys_info.rotvel.z += (d_rand() - 16384)/8;
3220                                         make_random_vector(&rand_vec);
3221
3222                                         bump_amount = F1_0*4;
3223
3224                                         if (Fusion_charge > F1_0*2)
3225                                                 bump_amount = Fusion_charge*4;
3226
3227                                         bump_one_object(ConsoleObject, &rand_vec, bump_amount);
3228                                 }
3229                         }
3230
3231                         if (Global_laser_firing_count) {
3232                                 //      Don't cap here, gets capped in Laser_create_new and is based on whether in multiplayer mode, MK, 3/27/95
3233                                 // if (Fusion_charge > F1_0*2)
3234                                 //      Fusion_charge = F1_0*2;
3235                                 Global_laser_firing_count -= do_laser_firing_player();  //do_laser_firing(Players[Player_num].objnum, Primary_weapon);
3236                         }
3237
3238                         if (Global_laser_firing_count < 0)
3239                                 Global_laser_firing_count = 0;
3240                 }
3241
3242         if (Do_appearance_effect) {
3243                 create_player_appearance_effect(ConsoleObject);
3244                 Do_appearance_effect = 0;
3245 #ifdef NETWORK
3246                 if ((Game_mode & GM_MULTI) && Netgame.invul)
3247                 {
3248                         Players[Player_num].flags |= PLAYER_FLAGS_INVULNERABLE;
3249                         Players[Player_num].invulnerable_time = GameTime-i2f(27);
3250                         FakingInvul=1;
3251                 }
3252 #endif
3253                         
3254         }
3255
3256         omega_charge_frame();
3257         slide_textures();
3258         flicker_lights();
3259
3260         //!!hoard_light_pulse();                //do cool hoard light pulsing
3261
3262 }
3263
3264 //!!extern int Goal_blue_segnum,Goal_red_segnum;
3265 //!!extern int Hoard_goal_eclip;
3266 //!!
3267 //!!//do cool pulsing lights in hoard goals
3268 //!!hoard_light_pulse()
3269 //!!{
3270 //!!    if (Game_mode & GM_HOARD) {
3271 //!!            fix light;
3272 //!!            int frame;
3273 //!!
3274 //!!            frame = Effects[Hoard_goal_eclip].frame_count;
3275 //!!
3276 //!!            frame++;
3277 //!!
3278 //!!            if (frame >= Effects[Hoard_goal_eclip].vc.num_frames)
3279 //!!                    frame = 0;
3280 //!!
3281 //!!            light = abs(frame - 5) * f1_0 / 5;
3282 //!!
3283 //!!            Segment2s[Goal_red_segnum].static_light = Segment2s[Goal_blue_segnum].static_light = light;
3284 //!!    }
3285 //!!}
3286
3287
3288 ubyte   Slide_segs[MAX_SEGMENTS];
3289 int     Slide_segs_computed;
3290
3291 void compute_slide_segs(void)
3292 {
3293         int     segnum, sidenum;
3294
3295         for (segnum=0;segnum<=Highest_segment_index;segnum++) {
3296                 Slide_segs[segnum] = 0;
3297                 for (sidenum=0;sidenum<6;sidenum++) {
3298                         int tmn = Segments[segnum].sides[sidenum].tmap_num;
3299                         if (TmapInfo[tmn].slide_u != 0 || TmapInfo[tmn].slide_v != 0)
3300                                 Slide_segs[segnum] |= 1 << sidenum;
3301                 }
3302         }
3303
3304         Slide_segs_computed = 1;
3305 }
3306
3307 //      -----------------------------------------------------------------------------
3308 void slide_textures(void)
3309 {
3310         int segnum,sidenum,i;
3311
3312         if (!Slide_segs_computed)
3313                 compute_slide_segs();
3314
3315         for (segnum=0;segnum<=Highest_segment_index;segnum++) {
3316                 if (Slide_segs[segnum]) {
3317                         for (sidenum=0;sidenum<6;sidenum++) {
3318                                 if (Slide_segs[segnum] & (1 << sidenum)) {
3319                                         int tmn = Segments[segnum].sides[sidenum].tmap_num;
3320                                         if (TmapInfo[tmn].slide_u != 0 || TmapInfo[tmn].slide_v != 0) {
3321                                                 for (i=0;i<4;i++) {
3322                                                         Segments[segnum].sides[sidenum].uvls[i].u += fixmul(FrameTime,TmapInfo[tmn].slide_u<<8);
3323                                                         Segments[segnum].sides[sidenum].uvls[i].v += fixmul(FrameTime,TmapInfo[tmn].slide_v<<8);
3324                                                         if (Segments[segnum].sides[sidenum].uvls[i].u > f2_0) {
3325                                                                 int j;
3326                                                                 for (j=0;j<4;j++)
3327                                                                         Segments[segnum].sides[sidenum].uvls[j].u -= f1_0;
3328                                                         }
3329                                                         if (Segments[segnum].sides[sidenum].uvls[i].v > f2_0) {
3330                                                                 int j;
3331                                                                 for (j=0;j<4;j++)
3332                                                                         Segments[segnum].sides[sidenum].uvls[j].v -= f1_0;
3333                                                         }
3334                                                         if (Segments[segnum].sides[sidenum].uvls[i].u < -f2_0) {
3335                                                                 int j;
3336                                                                 for (j=0;j<4;j++)
3337                                                                         Segments[segnum].sides[sidenum].uvls[j].u += f1_0;
3338                                                         }
3339                                                         if (Segments[segnum].sides[sidenum].uvls[i].v < -f2_0) {
3340                                                                 int j;
3341                                                                 for (j=0;j<4;j++)
3342                                                                         Segments[segnum].sides[sidenum].uvls[j].v += f1_0;
3343                                                         }
3344                                                 }
3345                                         }
3346                                 }
3347                         }
3348                 }
3349         }
3350 }
3351
3352 flickering_light Flickering_lights[MAX_FLICKERING_LIGHTS];
3353
3354 int Num_flickering_lights=0;
3355
3356 void flicker_lights()
3357 {
3358         int l;
3359         flickering_light *f;
3360
3361         f = Flickering_lights;
3362
3363         for (l=0;l<Num_flickering_lights;l++,f++) {
3364                 segment *segp = &Segments[f->segnum];
3365
3366                 //make sure this is actually a light
3367                 if (! (WALL_IS_DOORWAY(segp, f->sidenum) & WID_RENDER_FLAG))
3368                         continue;
3369                 if (! (TmapInfo[segp->sides[f->sidenum].tmap_num].lighting | TmapInfo[segp->sides[f->sidenum].tmap_num2 & 0x3fff].lighting))
3370                         continue;
3371
3372                 if (f->timer == 0x80000000)             //disabled
3373                         continue;
3374
3375                 if ((f->timer -= FrameTime) < 0) {
3376
3377                         while (f->timer < 0)
3378                                 f->timer += f->delay;
3379
3380                         f->mask = ((f->mask&0x80000000)?1:0) + (f->mask<<1);
3381
3382                         if (f->mask & 1)
3383                                 add_light(f->segnum,f->sidenum);
3384                         else
3385                                 subtract_light(f->segnum,f->sidenum);
3386                 }
3387         }
3388 }
3389
3390 //returns ptr to flickering light structure, or NULL if can't find
3391 flickering_light *find_flicker(int segnum,int sidenum)
3392 {
3393         int l;
3394         flickering_light *f;
3395
3396         //see if there's already an entry for this seg/side
3397
3398         f = Flickering_lights;
3399
3400         for (l=0;l<Num_flickering_lights;l++,f++)
3401                 if (f->segnum == segnum && f->sidenum == sidenum)       //found it!
3402                         return f;
3403
3404         return NULL;
3405 }
3406
3407 //turn flickering off (because light has been turned off)
3408 void disable_flicker(int segnum,int sidenum)
3409 {
3410         flickering_light *f;
3411
3412         if ((f=find_flicker(segnum,sidenum)) != NULL)
3413                 f->timer = 0x80000000;
3414 }
3415
3416 //turn flickering off (because light has been turned on)
3417 void enable_flicker(int segnum,int sidenum)
3418 {
3419         flickering_light *f;
3420
3421         if ((f=find_flicker(segnum,sidenum)) != NULL)
3422                 f->timer = 0;
3423 }
3424
3425
3426 #ifdef EDITOR
3427
3428 //returns 1 if ok, 0 if error
3429 int add_flicker(int segnum, int sidenum, fix delay, unsigned long mask)
3430 {
3431         int l;
3432         flickering_light *f;
3433
3434         mprintf((0,"add_flicker: %d:%d %x %x\n",segnum,sidenum,delay,mask));
3435
3436         //see if there's already an entry for this seg/side
3437
3438         f = Flickering_lights;
3439
3440         for (l=0;l<Num_flickering_lights;l++,f++)
3441                 if (f->segnum == segnum && f->sidenum == sidenum)       //found it!
3442                         break;
3443
3444         if (mask==0) {          //clearing entry
3445                 if (l == Num_flickering_lights)
3446                         return 0;
3447                 else {
3448                         int i;
3449                         for (i=l;i<Num_flickering_lights-1;i++)
3450                                 Flickering_lights[i] = Flickering_lights[i+1];
3451                         Num_flickering_lights--;
3452                         return 1;
3453                 }
3454         }
3455
3456         if (l == Num_flickering_lights) {
3457                 if (Num_flickering_lights == MAX_FLICKERING_LIGHTS)
3458                         return 0;
3459                 else
3460                         Num_flickering_lights++;
3461         }
3462
3463         f->segnum = segnum;
3464         f->sidenum = sidenum;
3465         f->delay = f->timer = delay;
3466         f->mask = mask;
3467
3468         return 1;
3469 }
3470
3471 #endif
3472
3473 //      -----------------------------------------------------------------------------
3474 //      Fire Laser:  Registers a laser fire, and performs special stuff for the fusion
3475 //                                  cannon.
3476 void FireLaser()
3477 {
3478
3479         Global_laser_firing_count += Weapon_info[Primary_weapon_to_weapon_info[Primary_weapon]].fire_count * (Controls.fire_primary_state || Controls.fire_primary_down_count);
3480
3481         if ((Primary_weapon == FUSION_INDEX) && (Global_laser_firing_count)) {
3482                 if ((Players[Player_num].energy < F1_0*2) && (Auto_fire_fusion_cannon_time == 0)) {
3483                         Global_laser_firing_count = 0;
3484                 } else {
3485                         if (Fusion_charge == 0)
3486                                 Players[Player_num].energy -= F1_0*2;
3487
3488                         Fusion_charge += FrameTime;
3489                         Players[Player_num].energy -= FrameTime;
3490
3491                         if (Players[Player_num].energy <= 0) {
3492                                 Players[Player_num].energy = 0;
3493                                 Auto_fire_fusion_cannon_time = GameTime -1;     //      Fire now!
3494                         } else
3495                                 Auto_fire_fusion_cannon_time = GameTime + FrameTime/2 + 1;
3496                                                                                                 //      Fire the fusion cannon at this time in the future.
3497
3498                         if (Fusion_charge < F1_0*2)
3499                                 PALETTE_FLASH_ADD(Fusion_charge >> 11, 0, Fusion_charge >> 11);
3500                         else
3501                                 PALETTE_FLASH_ADD(Fusion_charge >> 11, Fusion_charge >> 11, 0);
3502
3503                         if (GameTime < Fusion_last_sound_time)          //gametime has wrapped
3504                                 Fusion_next_sound_time = Fusion_last_sound_time = GameTime;
3505
3506                         if (Fusion_next_sound_time < GameTime) {
3507                                 if (Fusion_charge > F1_0*2) {
3508                                         digi_play_sample( 11, F1_0 );
3509                                         apply_damage_to_player(ConsoleObject, ConsoleObject, d_rand() * 4);
3510                                 } else {
3511                                         create_awareness_event(ConsoleObject, PA_WEAPON_ROBOT_COLLISION);
3512                                         digi_play_sample( SOUND_FUSION_WARMUP, F1_0 );
3513                                         #ifdef NETWORK
3514                                         if (Game_mode & GM_MULTI)
3515                                                 multi_send_play_sound(SOUND_FUSION_WARMUP, F1_0);
3516                                         #endif
3517                                 }
3518                                 Fusion_last_sound_time = GameTime;
3519                                 Fusion_next_sound_time = GameTime + F1_0/8 + d_rand()/4;
3520                         }
3521                 }
3522         }
3523
3524 }
3525
3526
3527 //      -------------------------------------------------------------------------------------------------------
3528 //      If player is close enough to objnum, which ought to be a powerup, pick it up!
3529 //      This could easily be made difficulty level dependent.
3530 void powerup_grab_cheat(object *player, int objnum)
3531 {
3532         fix     powerup_size;
3533         fix     player_size;
3534         fix     dist;
3535
3536         Assert(Objects[objnum].type == OBJ_POWERUP);
3537
3538         powerup_size = Objects[objnum].size;
3539         player_size = player->size;
3540
3541         dist = vm_vec_dist_quick(&Objects[objnum].pos, &player->pos);
3542
3543         if ((dist < 2*(powerup_size + player_size)) && !(Objects[objnum].flags & OF_SHOULD_BE_DEAD)) {
3544                 vms_vector      collision_point;
3545
3546                 vm_vec_avg(&collision_point, &Objects[objnum].pos, &player->pos);
3547                 collide_player_and_powerup(player, &Objects[objnum], &collision_point);
3548         }
3549 }
3550
3551 //      -------------------------------------------------------------------------------------------------------
3552 //      Make it easier to pick up powerups.
3553 //      For all powerups in this segment, pick them up at up to twice pickuppable distance based on dot product
3554 //      from player to powerup and player's forward vector.
3555 //      This has the effect of picking them up more easily left/right and up/down, but not making them disappear
3556 //      way before the player gets there.
3557 void powerup_grab_cheat_all(void)
3558 {
3559         segment *segp;
3560         int             objnum;
3561
3562         segp = &Segments[ConsoleObject->segnum];
3563         objnum = segp->objects;
3564
3565         while (objnum != -1) {
3566                 if (Objects[objnum].type == OBJ_POWERUP)
3567                         powerup_grab_cheat(ConsoleObject, objnum);
3568                 objnum = Objects[objnum].next;
3569         }
3570
3571 }
3572
3573 int     Last_level_path_created = -1;
3574
3575 #ifdef SHOW_EXIT_PATH
3576
3577 //      ------------------------------------------------------------------------------------------------------------------
3578 //      Create path for player from current segment to goal segment.
3579 //      Return true if path created, else return false.
3580 int mark_player_path_to_segment(int segnum)
3581 {
3582         int             i;
3583         object  *objp = ConsoleObject;
3584         short           player_path_length=0;
3585         int             player_hide_index=-1;
3586
3587         if (Last_level_path_created == Current_level_num) {
3588                 return 0;
3589         }
3590
3591         Last_level_path_created = Current_level_num;
3592
3593         if (create_path_points(objp, objp->segnum, segnum, Point_segs_free_ptr, &player_path_length, 100, 0, 0, -1) == -1) {
3594                 mprintf((0, "Unable to form path of length %i for myself\n", 100));
3595                 return 0;
3596         }
3597
3598         player_hide_index = Point_segs_free_ptr - Point_segs;
3599         Point_segs_free_ptr += player_path_length;
3600
3601         if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) {
3602                 mprintf((1, "Can't create path.  Not enough point_segs.\n"));
3603                 ai_reset_all_paths();
3604                 return 0;
3605         }
3606
3607         for (i=1; i<player_path_length; i++) {
3608                 int                     segnum, objnum;
3609                 vms_vector      seg_center;
3610                 object          *obj;
3611
3612                 segnum = Point_segs[player_hide_index+i].segnum;
3613                 mprintf((0, "%3i ", segnum));
3614                 seg_center = Point_segs[player_hide_index+i].point;
3615
3616                 objnum = obj_create( OBJ_POWERUP, POW_ENERGY, segnum, &seg_center, &vmd_identity_matrix, Powerup_info[POW_ENERGY].size, CT_POWERUP, MT_NONE, RT_POWERUP);
3617                 if (objnum == -1) {
3618                         Int3();         //      Unable to drop energy powerup for path
3619                         return 1;
3620                 }
3621
3622                 obj = &Objects[objnum];
3623                 obj->rtype.vclip_info.vclip_num = Powerup_info[obj->id].vclip_num;
3624                 obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].frame_time;
3625                 obj->rtype.vclip_info.framenum = 0;
3626                 obj->lifeleft = F1_0*100 + d_rand() * 4;
3627         }
3628
3629         mprintf((0, "\n"));
3630         return 1;
3631 }
3632
3633 //      Return true if it happened, else return false.
3634 int create_special_path(void)
3635 {
3636         int     i,j;
3637
3638         //      ---------- Find exit doors ----------
3639         for (i=0; i<=Highest_segment_index; i++)
3640                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
3641                         if (Segments[i].children[j] == -2) {
3642                                 mprintf((0, "Exit at segment %i\n", i));
3643                                 return mark_player_path_to_segment(i);
3644                         }
3645
3646         return 0;
3647 }
3648
3649 #endif
3650
3651
3652 #ifndef RELEASE
3653 int     Max_obj_count_mike = 0;
3654
3655 //      Shows current number of used objects.
3656 void show_free_objects(void)
3657 {
3658         if (!(FrameCount & 8)) {
3659                 int     i;
3660                 int     count=0;
3661
3662                 mprintf((0, "Highest_object_index = %3i, MAX_OBJECTS = %3i, now used = ", Highest_object_index, MAX_OBJECTS));
3663
3664                 for (i=0; i<=Highest_object_index; i++)
3665                         if (Objects[i].type != OBJ_NONE)
3666                                 count++;
3667
3668                 mprintf((0, "%3i", count));
3669
3670                 if (count > Max_obj_count_mike) {
3671                         Max_obj_count_mike = count;
3672                         mprintf((0, " ***"));
3673                 }
3674
3675                 mprintf((0, "\n"));
3676         }
3677
3678 }
3679
3680 #endif
3681
3682 #ifdef WINDOWS
3683 void game_win_init_cockpit_mask(int sram)
3684 {
3685         if (dd_VR_offscreen_buffer && dd_VR_offscreen_buffer != dd_grd_backcanv) {
3686                 dd_gr_free_canvas(dd_VR_offscreen_buffer);
3687         }
3688
3689         if (GRMODEINFO(paged) && !GRMODEINFO(dbuf)) {
3690         //      Here we will make the VR_offscreen_buffer the 2nd page and hopefully
3691         //      we can just flip it, saving a blt.
3692                 Int3();
3693         }
3694         else if (GRMODEINFO(dbuf)||GRMODEINFO(emul)) {
3695         //      The offscreen buffer will be created.  We will just blt this
3696         //      to the screen (which may be blted to the primary surface)
3697                 if ( grd_curscreen->sc_h < 200 ) {
3698                         dd_VR_offscreen_buffer = dd_gr_create_canvas(grd_curscreen->sc_w, 200);
3699                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
3700                         if (sram) {
3701                                 DDFreeSurface(dd_VR_offscreen_buffer->lpdds);
3702                                 dd_VR_offscreen_buffer->lpdds = DDCreateSysMemSurface(grd_curscreen->sc_w, 200);
3703                                 dd_VR_offscreen_buffer->sram = 1;
3704                         }
3705                 }
3706                 else {
3707                         dd_VR_offscreen_buffer = dd_gr_create_canvas(grd_curscreen->sc_w, grd_curscreen->sc_h);
3708                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
3709                         if (sram) {
3710                                 DDFreeSurface(dd_VR_offscreen_buffer->lpdds);
3711                                 dd_VR_offscreen_buffer->lpdds = DDCreateSysMemSurface(grd_curscreen->sc_w, grd_curscreen->sc_h);
3712                                 dd_VR_offscreen_buffer->sram = 1;
3713                         }
3714                 }
3715         }
3716
3717         dd_gr_init_sub_canvas( &dd_VR_render_buffer[0], dd_VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3718         dd_gr_init_sub_canvas( &dd_VR_render_buffer[1], dd_VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3719         gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3720         gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3721
3722         game_init_render_sub_buffers( 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3723 }
3724
3725 //@@void game_win_init_cockpit_mask()
3726 //@@{
3727 //@@    char title_pal[768];
3728 //@@    dd_grs_canvas ccanv;
3729 //@@    int pcx_error;
3730 //@@    LPDIRECTDRAWSURFACE dds;
3731 //@@
3732 //@@    dds = DDCreateSurface(GRMODEINFO(w), GRMODEINFO(h), 1);
3733 //@@    Assert(dds != NULL);
3734 //@@    
3735 //@@    _lpDDSMask = dds;
3736 //@@    ccanv.lpdds = dds;
3737 //@@    dd_gr_reinit_canvas(&ccanv);
3738 //@@
3739 //@@    dd_gr_set_current_canvas(&ccanv);
3740 //@@    DDGRLOCK(dd_grd_curcanv)
3741 //@@    {
3742 //@@            if (W95DisplayMode == SM95_640x480x8) {
3743 //@@                    pcx_error=pcx_read_bitmap( "MASKB.PCX", &grd_curcanv->cv_bitmap,
3744 //@@                            grd_curcanv->cv_bitmap.bm_type,
3745 //@@                            title_pal );
3746 //@@            }
3747 //@@            else {
3748 //@@                    pcx_error=pcx_read_bitmap( "MASK.PCX", &grd_curcanv->cv_bitmap,
3749 //@@                            grd_curcanv->cv_bitmap.bm_type,
3750 //@@                            title_pal );
3751 //@@            }
3752 //@@    }
3753 //@@    DDGRUNLOCK(dd_grd_curcanv);
3754 //@@
3755 //@@    Assert(pcx_error == PCX_ERROR_NONE);
3756 //@@    Game_cockpit_copy_code = (ubyte *)(0xABADC0DE);
3757 //@@}
3758
3759 #endif
3760
3761 /*
3762  * reads a flickering_light structure from a CFILE
3763  */
3764 void flickering_light_read(flickering_light *fl, CFILE *fp)
3765 {
3766         fl->segnum = cfile_read_short(fp);
3767         fl->sidenum = cfile_read_short(fp);
3768         fl->mask = cfile_read_int(fp);
3769         fl->timer = cfile_read_fix(fp);
3770         fl->delay = cfile_read_fix(fp);
3771 }