]> icculus.org git repositories - btb/d2x.git/blob - main/game.c
landscape fixes, implemented gr_check_mode
[btb/d2x.git] / main / game.c
1 /* $Id: game.c,v 1.24 2003-11-18 00:29:53 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.24 2003-11-18 00:29:53 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 int Game_window_x = 0;
836 int Game_window_y = 0;
837 int Game_window_w = 0;
838 int Game_window_h = 0;
839 int max_window_w = 0;
840 int max_window_h = 0;
841
842 extern void newdemo_record_cockpit_change(int);
843
844 //initialize the various canvases on the game screen
845 //called every time the screen mode or cockpit changes
846 void init_cockpit()
847 {
848         int minx, maxx, miny, maxy;
849
850 #if defined(POLY_ACC)
851     pa_flush();                 // get rid of undrawn polys.
852     pa_clear_buffer(1, 0);
853 #endif
854
855         //Initialize the on-screen canvases
856
857         if (Newdemo_state==ND_STATE_RECORDING) {
858                 newdemo_record_cockpit_change(Cockpit_mode);
859         }
860
861         if ( VR_render_mode != VR_NONE )
862                 Cockpit_mode = CM_FULL_SCREEN;
863
864         if (!(VR_screen_flags & VRF_ALLOW_COCKPIT) && (Cockpit_mode==CM_FULL_COCKPIT || Cockpit_mode==CM_STATUS_BAR || Cockpit_mode==CM_REAR_VIEW) )
865                 Cockpit_mode = CM_FULL_SCREEN;
866
867         if ( Screen_mode == SCREEN_EDITOR )
868                 Cockpit_mode = CM_FULL_SCREEN;
869
870 #ifdef OGL
871         if (Cockpit_mode == CM_FULL_COCKPIT || Cockpit_mode == CM_REAR_VIEW) {
872                 hud_message(MSGC_GAME_FEEDBACK, "Cockpit not available in GL mode");
873                 Cockpit_mode = CM_FULL_SCREEN;
874         }
875 #endif
876
877         WINDOS(
878                 dd_gr_set_current_canvas(NULL),
879                 gr_set_current_canvas(NULL)
880                 );
881         gr_set_curfont( GAME_FONT );
882
883 #if !defined(MACINTOSH) && !defined(WINDOWS)
884         if (Game_cockpit_copy_code)
885                 d_free(Game_cockpit_copy_code);
886         Game_cockpit_copy_code  = NULL;
887 #else
888         if (Game_cockpit_copy_code)
889                 Game_cockpit_copy_code = 0;
890 #endif
891
892 //@@    #ifdef WINDOWS
893 //@@            if (_lpDDSMask) { DDFreeSurface(_lpDDSMask); _lpDDSMask = NULL; }
894 //@@    #endif
895
896 #ifdef WINDOWS
897         game_win_init_cockpit_mask(0);
898 #endif
899
900         switch( Cockpit_mode ) {
901         case CM_FULL_COCKPIT:
902         case CM_REAR_VIEW: {
903                 grs_bitmap *bm = &GameBitmaps[cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)].index];
904
905                 PIGGY_PAGE_IN(cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)]);
906
907 #ifdef WINDOWS
908                 dd_gr_set_current_canvas(NULL);
909                 game_win_init_cockpit_mask(1);
910                 dd_gr_set_current_canvas(dd_VR_offscreen_buffer);
911 #else
912                 gr_set_current_canvas(VR_offscreen_buffer);
913 #endif          
914
915                 WIN(DDGRLOCK(dd_grd_curcanv));
916                 gr_bitmap( 0, 0, bm );
917                 bm = &VR_offscreen_buffer->cv_bitmap;
918                 bm->bm_flags = BM_FLAG_TRANSPARENT;
919                 gr_ibitblt_find_hole_size ( bm, &minx, &miny, &maxx, &maxy );
920                 WIN(    win_get_span_list(bm, miny, maxy);
921                                 DDGRUNLOCK(dd_grd_curcanv)
922                         );
923
924 #ifndef WINDOWS
925 #ifndef __MSDOS__
926                 gr_ibitblt_create_mask( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize);
927 #else
928                 if ( Current_display_mode ) {
929 #if defined(POLY_ACC)
930                         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 );
931                         pa_clear_buffer(1, 0);      // clear offscreen to reduce white flash.
932 #else
933                         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 );
934 #endif
935                 } else
936                         Game_cockpit_copy_code  = gr_ibitblt_create_mask( bm, minx, miny, maxx-minx+1, maxy-miny+1, VR_offscreen_buffer->cv_bitmap.bm_rowsize );
937 #endif
938                 bm->bm_flags = 0;               // Clear all flags for offscreen canvas
939 #else
940                 Game_cockpit_copy_code  = (ubyte *)(1);
941                 bm->bm_flags = 0;               // Clear all flags for offscreen canvas
942 #endif
943                 game_init_render_sub_buffers( 0, 0, maxx-minx+1, maxy-miny+1 );
944                 break;
945         }
946
947         case CM_FULL_SCREEN:
948
949                 max_window_h = grd_curscreen->sc_h;
950
951                 if (Game_window_h > max_window_h || VR_screen_flags&VRF_ALLOW_COCKPIT)
952                         Game_window_h = max_window_h;
953
954                 if (Game_window_w > max_window_w || VR_screen_flags&VRF_ALLOW_COCKPIT)
955                         Game_window_w = max_window_w;
956
957                 Game_window_x = (max_window_w - Game_window_w)/2;
958                 Game_window_y = (max_window_h - Game_window_h)/2;
959
960                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
961                 break;
962
963         case CM_STATUS_BAR:
964
965         max_window_h = grd_curscreen->sc_h - GameBitmaps[cockpit_bitmap[CM_STATUS_BAR+(Current_display_mode?(Num_cockpits/2):0)].index].bm_h;
966
967                 if (Game_window_h > max_window_h)
968                         Game_window_h = max_window_h;
969
970                 if (Game_window_w > max_window_w)
971                         Game_window_w = max_window_w;
972
973                 Game_window_x = (max_window_w - Game_window_w)/2;
974                 Game_window_y = (max_window_h - Game_window_h)/2;
975
976                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
977                 break;
978
979         case CM_LETTERBOX:      {
980                 int x,y,w,h;
981
982                 x = 0; w = VR_render_buffer[0].cv_bitmap.bm_w;          //VR_render_width;
983                 h = (VR_render_buffer[0].cv_bitmap.bm_h * 7) / 10;
984                 y = (VR_render_buffer[0].cv_bitmap.bm_h-h)/2;
985
986                 game_init_render_sub_buffers( x, y, w, h );
987                 break;
988                 }
989
990         }
991
992 WINDOS(
993         dd_gr_set_current_canvas(NULL),
994         gr_set_current_canvas(NULL)
995 );
996 }
997
998 //selects a given cockpit (or lack of one).  See types in game.h
999 void select_cockpit(int mode)
1000 {
1001         if (mode != Cockpit_mode) {             //new mode
1002                 Cockpit_mode=mode;
1003                 init_cockpit();
1004         }
1005 }
1006
1007 extern int last_drawn_cockpit[2];
1008
1009 //force cockpit redraw next time. call this if you've trashed the screen
1010 void reset_cockpit()
1011 {
1012         force_cockpit_redraw=1;
1013         last_drawn_cockpit[0] = -1;
1014         last_drawn_cockpit[1] = -1;
1015 }
1016
1017 // void HUD_clear_messages();                           //Already declared in gauges.h
1018
1019 //NEWVR
1020 void VR_reset_params()
1021 {
1022         VR_eye_width = VR_SEPARATION;
1023         VR_eye_offset = VR_PIXEL_SHIFT;
1024         VR_eye_offset_changed = 2;
1025 }
1026
1027 void game_init_render_sub_buffers( int x, int y, int w, int h )
1028 {
1029 #ifdef WINDOWS
1030         dd_gr_init_sub_canvas( &dd_VR_render_sub_buffer[0], &dd_VR_render_buffer[0], x, y, w, h );
1031         dd_gr_init_sub_canvas( &dd_VR_render_sub_buffer[1], &dd_VR_render_buffer[1], x, y, w, h );
1032
1033         dd_VR_render_sub_buffer[0].canvas.cv_bitmap.bm_x = 0;
1034         dd_VR_render_sub_buffer[0].canvas.cv_bitmap.bm_y = 0;
1035         dd_VR_render_sub_buffer[0].xoff = x;
1036         dd_VR_render_sub_buffer[0].yoff = y;
1037         dd_VR_render_sub_buffer[1].canvas.cv_bitmap.bm_x = 0;
1038         dd_VR_render_sub_buffer[1].canvas.cv_bitmap.bm_y = 0;
1039         dd_VR_render_sub_buffer[1].xoff = x;
1040         dd_VR_render_sub_buffer[1].yoff = y;
1041
1042 #endif
1043         if (Scanline_double) {
1044                 #ifdef MACINTOSH
1045                 if ( w & 0x3 )
1046                         w &= ~0x3;
1047                 gr_init_sub_canvas( &VR_render_sub_buffer[0], &VR_render_buffer[0], x, y, w/2, (h/2)+1);
1048                 gr_init_sub_canvas( &VR_render_sub_buffer[1], &VR_render_buffer[1], x, y, w/2, (h/2)+1);
1049                 #endif
1050         } else {
1051                 gr_init_sub_canvas( &VR_render_sub_buffer[0], &VR_render_buffer[0], x, y, w, h );
1052                 gr_init_sub_canvas( &VR_render_sub_buffer[1], &VR_render_buffer[1], x, y, w, h );
1053         }
1054
1055         #ifdef MACINTOSH
1056                 #ifdef POLY_ACC
1057                         if ( PAEnabled )
1058                         {
1059                                 TQARect newBounds;
1060                                 
1061                                 newBounds.left = x;
1062                                 newBounds.right = x + w;
1063                                 newBounds.top = y;
1064                                 newBounds.bottom = y + h;
1065                                 
1066                                 pa_set_context(kGamePlayDrawContextID, &newBounds);             // must resize/create new context
1067                         }
1068                 #endif
1069         #endif
1070
1071 #ifdef WINDOWS
1072         VR_render_sub_buffer[0].cv_bitmap.bm_x = 0;
1073         VR_render_sub_buffer[0].cv_bitmap.bm_y = 0;
1074         VR_render_sub_buffer[1].cv_bitmap.bm_x = 0;
1075         VR_render_sub_buffer[1].cv_bitmap.bm_y = 0;
1076 #endif
1077 }
1078
1079
1080 #ifdef WINDOWS
1081 // Sets up the canvases we will be rendering to (WIN95)
1082 void game_init_render_buffers(int screen_mode, int render_w, int render_h, int render_method, int flags )
1083 {
1084 //      Hack for switching to higher that 640x480 modes (DDraw doesn't allow
1085 //       creating surfaces greater than the current resolution
1086
1087         if (GRMODEINFO(rw) < render_w || GRMODEINFO(rh) < render_h) {
1088                 render_w = GRMODEINFO(rw);
1089                 render_h = GRMODEINFO(rh);
1090         }
1091
1092         VR_screen_mode          = screen_mode;
1093
1094         VR_screen_flags =  flags;
1095
1096         VR_reset_params();
1097         VR_render_mode  = render_method;
1098
1099         Game_window_w           = render_w;
1100         Game_window_h           = render_h;
1101
1102         if (dd_VR_offscreen_buffer && dd_VR_offscreen_buffer != dd_grd_backcanv) {
1103                 dd_gr_free_canvas(dd_VR_offscreen_buffer);
1104         }
1105
1106         if ( (VR_render_mode==VR_AREA_DET) || (VR_render_mode==VR_INTERLACED ) )        {
1107                 if ( render_h*2 < 200 ) {
1108                         Int3();         // Not Supported yet!!!
1109 //                      VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1110                 }
1111                 else {
1112                         Int3();         // Not Supported yet!!!
1113 //                      VR_offscreen_buffer = gr_create_canvas( render_w, render_h*2 );
1114                 }
1115
1116                 Int3();                 // Not Supported yet!!!
1117 //              gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1118 //              gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, render_h, render_w, render_h );
1119         }
1120         else if (GRMODEINFO(paged) && !GRMODEINFO(dbuf)) {
1121         //      Here we will make the VR_offscreen_buffer the 2nd page and hopefully
1122         //      we can just flip it, saving a blt.
1123
1124                 dd_VR_offscreen_buffer = dd_grd_backcanv;
1125                 VR_offscreen_buffer = & dd_grd_backcanv->canvas;
1126         }
1127         else if (GRMODEINFO(dbuf)||GRMODEINFO(emul)) {
1128         //      The offscreen buffer will be created.  We will just blt this
1129         //      to the screen (which may be blted to the primary surface)
1130                 if ( render_h < 200 ) {
1131                         dd_VR_offscreen_buffer = dd_gr_create_canvas(render_w, 200);
1132                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
1133                 }
1134                 else {
1135                         dd_VR_offscreen_buffer = dd_gr_create_canvas(render_w, render_h);
1136                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
1137                 }
1138         }
1139
1140         dd_gr_init_sub_canvas( &dd_VR_render_buffer[0], dd_VR_offscreen_buffer, 0, 0, render_w, render_h );
1141         dd_gr_init_sub_canvas( &dd_VR_render_buffer[1], dd_VR_offscreen_buffer, 0, 0, render_w, render_h );
1142         gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1143         gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, render_w, render_h );
1144
1145         game_init_render_sub_buffers( 0, 0, render_w, render_h );
1146 }
1147
1148 #else
1149
1150 // Sets up the canvases we will be rendering to (NORMAL VERSION)
1151 void game_init_render_buffers(int screen_mode, int render_w, int render_h, int render_method, int flags )
1152 {
1153 //      if (vga_check_mode(screen_mode) != 0)
1154 //              Error("Cannot set requested video mode");
1155
1156         VR_screen_mode          =       screen_mode;
1157
1158         VR_screen_flags =  flags;
1159
1160 //NEWVR
1161         VR_reset_params();
1162         VR_render_mode  = render_method;
1163
1164         Game_window_w           = render_w;
1165         Game_window_h           = render_h;
1166
1167         if (VR_offscreen_buffer) {
1168                 gr_free_canvas(VR_offscreen_buffer);
1169         }
1170
1171         if ( (VR_render_mode==VR_AREA_DET) || (VR_render_mode==VR_INTERLACED ) )        {
1172                 if ( render_h*2 < 200 ) {
1173                         VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1174                 }
1175                 else {
1176                         VR_offscreen_buffer = gr_create_canvas( render_w, render_h*2 );
1177                 }
1178
1179                 gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1180                 gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, render_h, render_w, render_h );
1181         }
1182         else {
1183                 if ( render_h < 200 ) {
1184                         VR_offscreen_buffer = gr_create_canvas( render_w, 200 );
1185                 }
1186                 else {
1187 #if defined(POLY_ACC)
1188                         #ifndef MACINTOSH
1189             VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1190             d_free(VR_offscreen_buffer->cv_bitmap.bm_data);
1191             gr_init_canvas(VR_offscreen_buffer, pa_get_buffer_address(1), BM_LINEAR15, render_w, render_h);
1192                         #else
1193                                 if ( PAEnabled || gConfigInfo.mAcceleration ) {
1194                                         Cockpit_mode=CM_FULL_SCREEN;            // HACK HACK HACK HACK HACK!!!!
1195                                         VR_offscreen_buffer = gr_create_canvas2(render_w, render_h, BM_LINEAR15);
1196                                 } else
1197                     VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1198                         #endif
1199 #else
1200             VR_offscreen_buffer = gr_create_canvas( render_w, render_h );
1201 #endif
1202         }
1203
1204 #ifdef OGL
1205                 VR_offscreen_buffer->cv_bitmap.bm_type = BM_OGL;
1206 #endif
1207
1208                 gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, render_w, render_h );
1209                 gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, render_w, render_h );
1210         }
1211
1212         game_init_render_sub_buffers( 0, 0, render_w, render_h );
1213 }
1214 #endif
1215
1216 //called to get the screen in a mode compatible with popup menus.
1217 //if we can't have popups over the game screen, switch to menu mode.
1218 void set_popup_screen(void)
1219 {
1220         //WIN(LoadCursorWin(MOUSE_DEFAULT_CURSOR));
1221
1222 #ifndef OGL // always have to switch to menu mode
1223         if (! (VR_screen_flags & VRF_COMPATIBLE_MENUS))
1224 #endif
1225         {
1226                 set_screen_mode(SCREEN_MENU);           //must switch to menu mode
1227         }
1228 }
1229
1230
1231 //called to change the screen mode. Parameter sm is the new mode, one of
1232 //SMODE_GAME or SMODE_EDITOR. returns mode acutally set (could be other
1233 //mode if cannot init requested mode)
1234 int set_screen_mode(int sm)
1235 {
1236 WIN(static int force_mode_change=0);
1237 WIN(static int saved_window_w);
1238 WIN(static int saved_window_h);
1239
1240 #ifdef EDITOR
1241         if ( (sm==SCREEN_MENU) && (Screen_mode==SCREEN_EDITOR) )        {
1242                 gr_set_current_canvas( Canv_editor );
1243                 return 1;
1244         }
1245 #endif
1246
1247 #ifdef WINDOWS
1248         if ( Screen_mode == sm && W95DisplayMode == VR_screen_mode) {
1249                 dd_gr_set_current_canvas( &dd_VR_screen_pages[VR_current_page] );
1250                 return 1;
1251         }
1252 #else
1253         if ( Screen_mode == sm && VGA_current_mode == VR_screen_mode) {
1254                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1255                 return 1;
1256         }
1257 #endif
1258
1259 #ifdef OGL
1260         if ((Screen_mode == sm) && !((sm==SCREEN_GAME) && (grd_curscreen->sc_mode != VR_screen_mode) && (Screen_mode == SCREEN_GAME))) {
1261                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1262                 ogl_set_screen_mode();
1263                 return 1;
1264         }
1265 #endif
1266
1267 #ifdef EDITOR
1268         Canv_editor = NULL;
1269 #endif
1270
1271         Screen_mode = sm;
1272
1273         switch( Screen_mode )
1274         {
1275                 case SCREEN_MENU:
1276                 #ifdef WINDOWS
1277                         //mouse_set_mode(0);
1278                         //ShowCursorW();
1279                         if (!(VR_screen_flags & VRF_COMPATIBLE_MENUS)) {
1280                         // HACK!!!  Meant to save window size when switching from
1281                         // non-compat menu mode to menu mode.
1282                                 saved_window_w = Game_window_w;
1283                                 saved_window_h = Game_window_h;
1284                                 force_mode_change = 1;
1285                         }
1286                         if (W95DisplayMode != SM95_640x480x8) {
1287 //@@                            piggy_bitmap_page_out_all_w();          // 2D GFX Flush cache.
1288                                 DDSETDISPLAYMODE(SM95_640x480x8);
1289                                 dd_gr_init_screen();
1290                                 if (!gr_palette_faded_out) gr_palette_load(gr_palette);
1291                         }
1292
1293                         dd_gr_init_sub_canvas(&dd_VR_screen_pages[0], dd_grd_screencanv,
1294                                                                         0,0,
1295                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_w,
1296                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_h);
1297                         dd_gr_init_sub_canvas(&dd_VR_screen_pages[1], dd_grd_screencanv,
1298                                                                         0,0,
1299                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_w,
1300                                                                         dd_grd_screencanv->canvas.cv_bitmap.bm_h);
1301                         MenuHires = 1;
1302                         FontHires = FontHiresAvailable;
1303
1304                 #else
1305                 {
1306                         int menu_mode;
1307
1308                         MenuHires = MenuHiresAvailable;         //do highres if we can
1309
1310 #if defined(POLY_ACC)
1311                                 #ifndef MACINTOSH
1312                     menu_mode = MenuHires?SM(640,480):SM(320,200);
1313                                 #else
1314                                         menu_mode = PAEnabled?SM_640x480x15xPA:SM_640x480V;
1315                                 #endif
1316 #else
1317             menu_mode = MenuHires?SM(640,480):SM(320,200);
1318 #endif
1319
1320                         if (VGA_current_mode != menu_mode) {
1321                                 if (gr_set_mode(menu_mode))
1322                                         Error("Cannot set screen mode for menu");
1323                                 if (!gr_palette_faded_out)
1324                                         gr_palette_load(gr_palette);
1325                         }
1326
1327                         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1328                         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1329
1330                         FontHires = FontHiresAvailable && MenuHires;
1331
1332                 }
1333                 #endif
1334                 break;
1335
1336         case SCREEN_GAME:
1337         #ifdef WINDOWS
1338                 //mouse_set_mode(1);
1339                 HideCursorW();
1340                 if (force_mode_change || (W95DisplayMode != VR_screen_mode)) {
1341
1342                         DDSETDISPLAYMODE(VR_screen_mode);
1343 //@@                    piggy_bitmap_page_out_all_w();          // 2D GFX Flush cache.
1344                         dd_gr_init_screen();
1345                         mprintf((0, "Reinitializing render buffers due to display mode change.\n"));
1346                         game_init_render_buffers(W95DisplayMode,
1347                                         GRMODEINFO(rw), GRMODEINFO(rh),
1348                                         VR_render_mode, VR_screen_flags);
1349
1350                         reset_cockpit();
1351                 }
1352         #else
1353                 if (VGA_current_mode != VR_screen_mode) {
1354                         if (gr_set_mode(VR_screen_mode))        {
1355                                 Error("Cannot set desired screen mode for game!");
1356                                 //we probably should do something else here, like select a standard mode
1357                         }
1358                         #ifdef MACINTOSH
1359                         if ( (Config_control_type == 1) && (Function_mode == FMODE_GAME) )
1360                                 joydefs_calibrate();
1361                         #endif
1362                         reset_cockpit();
1363                 }
1364         #endif
1365
1366                 if ( VR_render_mode == VR_NONE )
1367                 {
1368                         max_window_w = grd_curscreen->sc_w;
1369                         max_window_h = grd_curscreen->sc_h;
1370
1371                         if (VR_screen_flags & VRF_ALLOW_COCKPIT) {
1372                                 if (Cockpit_mode == CM_STATUS_BAR)
1373                         max_window_h = grd_curscreen->sc_h - GameBitmaps[cockpit_bitmap[CM_STATUS_BAR+(Current_display_mode?(Num_cockpits/2):0)].index].bm_h;
1374                         }
1375                         else if (Cockpit_mode != CM_LETTERBOX)
1376                                 Cockpit_mode = CM_FULL_SCREEN;
1377
1378               if (Game_window_h==0 || Game_window_h > max_window_h || Game_window_w==0 || Game_window_w > max_window_w) {
1379                                 Game_window_w = max_window_w;
1380                                 Game_window_h = max_window_h;
1381               }
1382
1383                 }
1384                 else
1385                         Cockpit_mode = CM_FULL_SCREEN;
1386
1387                 #ifdef WINDOWS
1388                 //      Super hack.   If we are switching from a 320x200 game to 640x480.
1389                 //                                              and we were in a menumode when switching, we don't
1390                 //                                              restore Game_window vals
1391                         if (force_mode_change && (W95DisplayMode == SM95_320x200x8X)) {
1392                                 Game_window_w = saved_window_w;
1393                                 Game_window_h = saved_window_h;
1394                                 force_mode_change = 0;
1395                         }
1396                 #endif
1397
1398
1399         //      Define screen pages for game mode
1400         // If we designate through screen_flags to use paging, then do so.
1401                 WINDOS(
1402                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[0], dd_grd_screencanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1403                         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h )
1404                 );
1405
1406                 if ( VR_screen_flags&VRF_USE_PAGING ) {
1407                 WINDOS(
1408                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[1], dd_grd_backcanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1409                         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 )
1410                 );
1411                 }
1412                 else {
1413                 WINDOS (
1414                         dd_gr_init_sub_canvas( &dd_VR_screen_pages[1], dd_grd_screencanv, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h ),
1415                         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h )
1416                 );
1417                 }
1418
1419                 init_cockpit();
1420
1421         #ifdef WINDOWS
1422                 FontHires = FontHiresAvailable && (Current_display_mode != 0);
1423                 MenuHires = 1;
1424         #else
1425                 FontHires = FontHiresAvailable && (MenuHires = ((Current_display_mode != 0) && (Current_display_mode != 2)));
1426         #endif
1427
1428                 if ( VR_render_mode != VR_NONE )        {
1429                         // for 640x480 or higher, use hires font.
1430                         if (FontHiresAvailable && (grd_curscreen->sc_h > 400))
1431                                 FontHires = 1;
1432                         else
1433                                 FontHires = 0;
1434                 }
1435                 con_resize();
1436
1437                 break;
1438         #ifdef EDITOR
1439         case SCREEN_EDITOR:
1440                 if (grd_curscreen->sc_mode != SM(800,600))      {
1441                         int gr_error;
1442                         if ((gr_error=gr_set_mode(SM(800,600)))!=0) { //force into game scrren
1443                                 Warning("Cannot init editor screen (error=%d)",gr_error);
1444                                 return 0;
1445                         }
1446                 }
1447                 gr_palette_load( gr_palette );
1448
1449                 gr_init_sub_canvas( &VR_editor_canvas, &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
1450                 Canv_editor = &VR_editor_canvas;
1451                 gr_init_sub_canvas( &VR_screen_pages[0], Canv_editor, 0, 0, Canv_editor->cv_w, Canv_editor->cv_h );
1452                 gr_init_sub_canvas( &VR_screen_pages[1], Canv_editor, 0, 0, Canv_editor->cv_w, Canv_editor->cv_h );
1453                 gr_set_current_canvas( Canv_editor );
1454                 init_editor_screen();   //setup other editor stuff
1455                 break;
1456         #endif
1457         default:
1458                 Error("Invalid screen mode %d",sm);
1459         }
1460
1461         VR_current_page = 0;
1462
1463         WINDOS(
1464                 dd_gr_set_current_canvas(&dd_VR_screen_pages[VR_current_page]),
1465                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] )
1466         );
1467
1468         if ( VR_screen_flags&VRF_USE_PAGING )   {
1469         WINDOS(
1470                 dd_gr_flip(),
1471                 gr_show_canvas( &VR_screen_pages[VR_current_page] )
1472         );
1473         }
1474 #ifdef OGL
1475         ogl_set_screen_mode();
1476 #endif
1477
1478         return 1;
1479 }
1480
1481 int gr_toggle_fullscreen_game(void){
1482 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
1483         int i;
1484         hud_message(MSGC_GAME_FEEDBACK, "toggling fullscreen mode %s",(i=gr_toggle_fullscreen())?"on":"off" );
1485         //added 2000/06/19 Matthew Mueller - hack to fix "infinite toggle" problem
1486         //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.
1487 //      generic_key_handler(KEY_PADENTER,0);
1488         key_flush();
1489         //end addition -MM
1490         return i;
1491 #else
1492         hud_message(MSGC_GAME_FEEDBACK, "fullscreen toggle not supported by this target");
1493         return -1;
1494 #endif
1495 }
1496
1497 int arch_toggle_fullscreen_menu(void);
1498
1499 int gr_toggle_fullscreen_menu(void){
1500 #ifdef GR_SUPPORTS_FULLSCREEN_MENU_TOGGLE
1501         int i;
1502         i=arch_toggle_fullscreen_menu();
1503
1504 //      generic_key_handler(KEY_PADENTER,0);
1505         key_flush();
1506
1507         return i;
1508 #else
1509         return -1;
1510 #endif
1511 }
1512
1513 static int timer_paused=0;
1514
1515 void stop_time()
1516 {
1517         if (timer_paused==0) {
1518                 fix time;
1519                 time = timer_get_fixed_seconds();
1520                 last_timer_value = time - last_timer_value;
1521                 if (last_timer_value < 0) {
1522                         #if defined(TIMER_TEST) && !defined(NDEBUG)
1523                         Int3();         //get Matt!!!!
1524                         #endif
1525                         last_timer_value = 0;
1526                 }
1527                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1528                 time_stopped = time;
1529                 #endif
1530         }
1531         timer_paused++;
1532
1533         #if defined(TIMER_TEST) && !defined(NDEBUG)
1534         stop_count++;
1535         #endif
1536 }
1537
1538 void start_time()
1539 {
1540         timer_paused--;
1541         Assert(timer_paused >= 0);
1542         if (timer_paused==0) {
1543                 fix time;
1544                 time = timer_get_fixed_seconds();
1545                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1546                 if (last_timer_value < 0)
1547                         Int3();         //get Matt!!!!
1548                 }
1549                 #endif
1550                 last_timer_value = time - last_timer_value;
1551                 #if defined(TIMER_TEST) && !defined(NDEBUG)
1552                 time_started = time;
1553                 #endif
1554         }
1555
1556         #if defined(TIMER_TEST) && !defined(NDEBUG)
1557         start_count++;
1558         #endif
1559 }
1560
1561 MAC(extern ubyte joydefs_calibrating;)
1562
1563 void game_flush_inputs()
1564 {
1565         int dx,dy;
1566         key_flush();
1567         joy_flush();
1568         mouse_flush();
1569         #ifdef MACINTOSH
1570         if ( (Function_mode != FMODE_MENU) && !joydefs_calibrating )            // only reset mouse when not in menu or not calibrating
1571         #endif
1572                 mouse_get_delta( &dx, &dy );    // Read mouse
1573         memset(&Controls,0,sizeof(control_info));
1574 }
1575
1576 void reset_time()
1577 {
1578         last_timer_value = timer_get_fixed_seconds();
1579
1580 }
1581
1582 #ifndef RELEASE
1583 extern int Saving_movie_frames;
1584 int Movie_fixed_frametime;
1585 #else
1586 #define Saving_movie_frames     0
1587 #define Movie_fixed_frametime   0
1588 #endif
1589
1590 //added on 8/18/98 by Victor Rachels to add maximum framerate
1591 int maxfps = 80;
1592 //end this section
1593
1594 void calc_frame_time()
1595 {
1596         fix timer_value,last_frametime = FrameTime;
1597
1598         #if defined(TIMER_TEST) && !defined(NDEBUG)
1599         _last_frametime = last_frametime;
1600         #endif
1601
1602         timer_value = timer_get_fixed_seconds();
1603         FrameTime = timer_value - last_timer_value;
1604
1605         do {
1606             timer_value = timer_get_fixed_seconds();
1607             FrameTime = timer_value - last_timer_value;
1608             if (FrameTime < f1_0/maxfps);
1609                         timer_delay(1);
1610         } while (FrameTime < f1_0/maxfps);
1611
1612         #if defined(TIMER_TEST) && !defined(NDEBUG)
1613         _timer_value = timer_value;
1614         #endif
1615
1616         #ifndef NDEBUG
1617         if (!(((FrameTime > 0) && (FrameTime <= F1_0)) || (Function_mode == FMODE_EDITOR) || (Newdemo_state == ND_STATE_PLAYBACK))) {
1618                 mprintf((1,"Bad FrameTime - value = %x\n",FrameTime));
1619                 if (FrameTime == 0)
1620                         Int3(); //      Call Mike or Matt or John!  Your interrupts are probably trashed!
1621 //              if ( !dpmi_virtual_memory )
1622 //                      Int3();         //Get MATT if hit this!
1623         }
1624         #endif
1625
1626         #if defined(TIMER_TEST) && !defined(NDEBUG)
1627         actual_last_timer_value = last_timer_value;
1628         #endif
1629
1630         if ( Game_turbo_mode )
1631                 FrameTime *= 2;
1632
1633         // Limit frametime to be between 5 and 150 fps.
1634         RealFrameTime = FrameTime;
1635         if ( FrameTime < F1_0/150 ) FrameTime = F1_0/150;
1636         if ( FrameTime > F1_0/5 ) FrameTime = F1_0/5;
1637
1638         last_timer_value = timer_value;
1639
1640         if (FrameTime < 0)                                              //if bogus frametime...
1641                 FrameTime = last_frametime;             //...then use time from last frame
1642
1643         #ifndef NDEBUG
1644         if (fixed_frametime) FrameTime = fixed_frametime;
1645         #endif
1646
1647         #ifndef NDEBUG
1648         // Pause here!!!
1649         if ( Debug_pause )      {
1650                 int c;
1651                 c = 0;
1652                 while( c==0 )
1653                         c = key_peekkey();
1654
1655                 if ( c == KEY_P )       {
1656                         Debug_pause = 0;
1657                         c = key_inkey();
1658                 }
1659                 last_timer_value = timer_get_fixed_seconds();
1660         }
1661         #endif
1662
1663         #if Arcade_mode
1664                 FrameTime /= 2;
1665         #endif
1666
1667         #if defined(TIMER_TEST) && !defined(NDEBUG)
1668         stop_count = start_count = 0;
1669         #endif
1670
1671         //      Set value to determine whether homing missile can see target.
1672         //      The lower frametime is, the more likely that it can see its target.
1673         if (FrameTime <= F1_0/64)
1674                 Min_trackable_dot = MIN_TRACKABLE_DOT;  // -- 3*(F1_0 - MIN_TRACKABLE_DOT)/4 + MIN_TRACKABLE_DOT;
1675         else if (FrameTime < F1_0/32)
1676                 Min_trackable_dot = MIN_TRACKABLE_DOT + F1_0/64 - 2*FrameTime;  // -- fixmul(F1_0 - MIN_TRACKABLE_DOT, F1_0-4*FrameTime) + MIN_TRACKABLE_DOT;
1677         else if (FrameTime < F1_0/4)
1678                 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;
1679         else
1680                 Min_trackable_dot = MIN_TRACKABLE_DOT + F1_0/64 - F1_0/8;
1681
1682 }
1683
1684 //--unused-- int Auto_flythrough=0;  //if set, start flythough automatically
1685
1686 void move_player_2_segment(segment *seg,int side)
1687 {
1688         vms_vector vp;
1689
1690         compute_segment_center(&ConsoleObject->pos,seg);
1691         compute_center_point_on_side(&vp,seg,side);
1692         vm_vec_sub2(&vp,&ConsoleObject->pos);
1693         vm_vector_2_matrix(&ConsoleObject->orient,&vp,NULL,NULL);
1694
1695         obj_relink( ConsoleObject-Objects, SEG_PTR_2_NUM(seg) );
1696
1697 }
1698
1699 #ifdef NETWORK
1700 void game_draw_time_left()
1701 {
1702         char temp_string[30];
1703         fix timevar;
1704         int i;
1705
1706         gr_set_curfont( GAME_FONT );    //GAME_FONT
1707         gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
1708
1709         timevar=i2f (Netgame.PlayTimeAllowed*5*60);
1710         i=f2i(timevar-ThisLevelTime);
1711         i++;
1712
1713         sprintf( temp_string, "Time left: %d secs", i );
1714
1715         if (i>=0)
1716          gr_string(0, 32, temp_string );
1717 }
1718 #endif
1719
1720
1721 extern int Game_pause;
1722
1723 void do_photos();
1724 void level_with_floor();
1725
1726 void modex_clear_box(int x,int y,int w,int h)
1727 {
1728         grs_canvas *temp_canv,*save_canv;
1729
1730         save_canv = grd_curcanv;
1731         temp_canv = gr_create_canvas(w,h);
1732         gr_set_current_canvas(temp_canv);
1733         gr_clear_canvas(BM_XRGB(0,0,0));
1734         gr_set_current_canvas(save_canv);
1735         gr_bitmapm(x,y,&temp_canv->cv_bitmap);
1736         gr_free_canvas(temp_canv);
1737
1738 }
1739
1740 extern void modex_printf(int x,int y,char *s,grs_font *font,int color);
1741
1742 // mac routine to drop contents of screen to a pict file using copybits
1743 // save a PICT to a file
1744 #ifdef MACINTOSH
1745
1746 void SavePictScreen(int multiplayer)
1747 {
1748         OSErr err;
1749         int parid, i, count;
1750         char *pfilename, filename[50], buf[512], cwd[FILENAME_MAX];
1751         short fd;
1752         FSSpec spec;
1753         PicHandle pict_handle;
1754         static int multi_count = 0;
1755         StandardFileReply sf_reply;
1756         
1757 // dump the contents of the GameWindow into a picture using copybits
1758
1759         pict_handle = OpenPicture(&GameWindow->portRect);
1760         if (pict_handle == NULL)
1761                 return;
1762                 
1763         CopyBits(&GameWindow->portBits, &GameWindow->portBits, &GameWindow->portRect, &GameWindow->portRect, srcBic, NULL);
1764         ClosePicture();
1765
1766 // get the cwd to restore with chdir when done -- this keeps the mac world sane
1767         if (!getcwd(cwd, FILENAME_MAX))
1768                 Int3();
1769 // create the fsspec
1770
1771         sprintf(filename, "screen%d", multi_count++);
1772         pfilename = c2pstr(filename);
1773         if (!multiplayer) {
1774                 show_cursor();
1775                 StandardPutFile("\pSave PICT as:", pfilename, &sf_reply);
1776                 if (!sf_reply.sfGood)
1777                         goto end;
1778                 memcpy( &spec, &(sf_reply.sfFile), sizeof(FSSpec) );
1779                 if (sf_reply.sfReplacing)
1780                         FSpDelete(&spec);
1781                 err = FSpCreate( &spec, 'ttxt', 'PICT', smSystemScript );
1782                 if (err)
1783                         goto end;
1784         } else {
1785 //              parid = GetAppDirId();
1786                 err = FSMakeFSSpec(0, 0, pfilename, &spec);
1787                 if (err == nsvErr)
1788                         goto end;
1789                 if (err != fnfErr)
1790                         FSpDelete(&spec);
1791                 err = FSpCreate(&spec, 'ttxt', 'PICT', smSystemScript);
1792                 if (err != 0)
1793                         goto end;
1794         }
1795
1796 // write the PICT file
1797         if ( FSpOpenDF(&spec, fsRdWrPerm, &fd) )
1798                 goto end;
1799         memset(buf, 0, sizeof(buf));
1800         count = 512;
1801         if ( FSWrite(fd, &count, buf) )
1802                 goto end;
1803         count = GetHandleSize((Handle)pict_handle);
1804         HLock((Handle)pict_handle);
1805         if ( FSWrite(fd, &count, *pict_handle) ) {
1806                 FSClose(fd);
1807                 FSpDelete(&spec);
1808         }
1809
1810 end:
1811         HUnlock((Handle)pict_handle);
1812         DisposeHandle((Handle)pict_handle);
1813         FSClose(fd);
1814         hide_cursor();
1815         chdir(cwd);
1816 }
1817
1818 #endif
1819
1820 //automap_flag is now unused, since we just check if the screen we're
1821 //writing to is modex
1822 //if called from automap, current canvas is set to visible screen
1823 #ifndef OGL
1824 void save_screen_shot(int automap_flag)
1825 {
1826 #if defined(WINDOWS)
1827         mprintf((0, "Doing screen shot thing.\n"));
1828         win95_save_pcx_shot();
1829
1830 #elif !defined(MACINTOSH)
1831         fix t1;
1832         char message[100];
1833         grs_canvas *screen_canv=&grd_curscreen->sc_canvas;
1834         grs_font *save_font;
1835         static int savenum=0;
1836         static int stereo_savenum=0;
1837         grs_canvas *temp_canv,*temp_canv2,*save_canv;
1838         char savename[FILENAME_LEN],savename2[FILENAME_LEN];
1839         ubyte pal[768];
1840         int w,h,aw,x,y;
1841         int modex_flag;
1842         int stereo=0;
1843
1844         temp_canv2=NULL;
1845
1846 //      // Can't do screen shots in VR modes.
1847 //      if ( VR_render_mode != VR_NONE )
1848 //              return;
1849
1850         stop_time();
1851
1852         save_canv = grd_curcanv;
1853
1854         if ( VR_render_mode != VR_NONE && !automap_flag && Function_mode==FMODE_GAME && Screen_mode==SCREEN_GAME)
1855                 stereo = 1;
1856
1857         if ( stereo ) {
1858                 temp_canv = gr_create_canvas(VR_render_buffer[0].cv_bitmap.bm_w,VR_render_buffer[0].cv_bitmap.bm_h);
1859                 gr_set_current_canvas(temp_canv);
1860                 gr_ubitmap(0,0,&VR_render_buffer[0].cv_bitmap);
1861
1862                 temp_canv2 = gr_create_canvas(VR_render_buffer[1].cv_bitmap.bm_w,VR_render_buffer[1].cv_bitmap.bm_h);
1863                 gr_set_current_canvas(temp_canv2);
1864                 gr_ubitmap(0,0,&VR_render_buffer[1].cv_bitmap);
1865         }
1866         else {
1867                 temp_canv = gr_create_canvas(screen_canv->cv_bitmap.bm_w,screen_canv->cv_bitmap.bm_h);
1868                 gr_set_current_canvas(temp_canv);
1869                 gr_ubitmap(0,0,&screen_canv->cv_bitmap);
1870         }
1871
1872         gr_set_current_canvas(save_canv);
1873
1874         if ( savenum > 99 ) savenum = 0;
1875         if ( stereo_savenum > 99 ) stereo_savenum = 0;
1876
1877         if ( stereo ) {
1878                 sprintf(savename,"left%02d.pcx",stereo_savenum);
1879                 sprintf(savename2,"right%02d.pcx",stereo_savenum);
1880                 if (VR_eye_switch) {char t[FILENAME_LEN]; strcpy(t,savename); strcpy(savename,savename2); strcpy(savename2,t);}
1881                 stereo_savenum++;
1882                 sprintf( message, "%s '%s' & '%s'", TXT_DUMPING_SCREEN, savename, savename2 );
1883         }
1884         else {
1885                 sprintf(savename,"screen%02d.pcx",savenum++);
1886                 sprintf( message, "%s '%s'", TXT_DUMPING_SCREEN, savename );
1887         }
1888
1889         if (!automap_flag)              //if from automap, curcanv is already visible canv
1890                 gr_set_current_canvas(NULL);
1891         modex_flag = (grd_curcanv->cv_bitmap.bm_type==BM_MODEX);
1892         if (!automap_flag && modex_flag)
1893                 gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1894
1895         save_font = grd_curcanv->cv_font;
1896         gr_set_curfont(GAME_FONT);
1897         gr_set_fontcolor(gr_find_closest_color_current(0,31,0),-1);
1898         gr_get_string_size(message,&w,&h,&aw);
1899
1900         if (modex_flag)
1901                 h *= 2;
1902
1903         //I changed how these coords were calculated for the high-res automap. -MT
1904         //x = (VR_screen_pages[VR_current_page].cv_w-w)/2;
1905         //y = (VR_screen_pages[VR_current_page].cv_h-h)/2;
1906         x = (grd_curcanv->cv_w-w)/2;
1907         y = (grd_curcanv->cv_h-h)/2;
1908
1909         if (modex_flag) {
1910                 modex_clear_box(x-2,y-2,w+4,h+4);
1911                 modex_printf(x, y, message,GAME_FONT,gr_find_closest_color_current(0,31,0));
1912         } else {
1913                 gr_setcolor(gr_find_closest_color_current(0,0,0));
1914                 gr_rect(x-2,y-2,x+w+2,y+h+2);
1915                 gr_printf(x,y,message);
1916                 gr_set_curfont(save_font);
1917         }
1918         t1 = timer_get_fixed_seconds() + F1_0;
1919
1920         gr_palette_read(pal);           //get actual palette from the hardware
1921         pcx_write_bitmap(savename,&temp_canv->cv_bitmap,pal);
1922         if ( stereo )
1923                 pcx_write_bitmap(savename2,&temp_canv2->cv_bitmap,pal);
1924
1925         while ( timer_get_fixed_seconds() < t1 );               // Wait so that messag stays up at least 1 second.
1926
1927         gr_set_current_canvas(screen_canv);
1928
1929         if (grd_curcanv->cv_bitmap.bm_type!=BM_MODEX && !stereo)
1930                 gr_ubitmap(0,0,&temp_canv->cv_bitmap);
1931
1932         gr_free_canvas(temp_canv);
1933         if ( stereo )
1934                 gr_free_canvas(temp_canv2);
1935
1936         gr_set_current_canvas(save_canv);
1937         key_flush();
1938         start_time();
1939         
1940 #else
1941
1942         grs_canvas *screen_canv = &grd_curscreen->sc_canvas;
1943         grs_canvas *temp_canv, *save_canv;
1944         
1945         // Can't do screen shots in VR modes.
1946         if ( VR_render_mode != VR_NONE )
1947                 return;
1948
1949         stop_time();
1950
1951         save_canv = grd_curcanv;        
1952         temp_canv = gr_create_canvas( screen_canv->cv_bitmap.bm_w, screen_canv->cv_bitmap.bm_h );
1953         if (!temp_canv)
1954                 goto shot_done;
1955         gr_set_current_canvas( temp_canv );
1956         gr_ubitmap( 0, 0, &screen_canv->cv_bitmap );
1957         gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1958
1959         show_cursor();
1960         key_close();
1961         if (Game_mode & GM_MULTI)
1962                 SavePictScreen(1);
1963         else
1964                 SavePictScreen(0);
1965         key_init();
1966         hide_cursor();
1967
1968         gr_set_current_canvas(screen_canv);
1969         
1970 //      if (!automap_flag)
1971                 gr_ubitmap( 0, 0, &temp_canv->cv_bitmap);
1972
1973         gr_free_canvas(temp_canv);
1974 shot_done:
1975         gr_set_current_canvas(save_canv);
1976         key_flush();
1977         start_time();
1978         #endif
1979 }
1980
1981 #endif
1982
1983 //initialize flying
1984 void fly_init(object *obj)
1985 {
1986         obj->control_type = CT_FLYING;
1987         obj->movement_type = MT_PHYSICS;
1988
1989         vm_vec_zero(&obj->mtype.phys_info.velocity);
1990         vm_vec_zero(&obj->mtype.phys_info.thrust);
1991         vm_vec_zero(&obj->mtype.phys_info.rotvel);
1992         vm_vec_zero(&obj->mtype.phys_info.rotthrust);
1993 }
1994
1995 //void morph_test(), morph_step();
1996
1997
1998 //      ------------------------------------------------------------------------------------
1999
2000 void test_anim_states();
2001
2002 #include "fvi.h"
2003
2004 //put up the help message
2005 void do_show_help()
2006 {
2007         show_help();
2008 }
2009
2010
2011 extern int been_in_editor;
2012
2013 //      ------------------------------------------------------------------------------------
2014 void do_cloak_stuff(void)
2015 {
2016         int i;
2017         for (i = 0; i < N_players; i++)
2018                 if (Players[i].flags & PLAYER_FLAGS_CLOAKED) {
2019                         // mprintf(0, "Cloak time left: %7.3f\n", f2fl(CLOAK_TIME_MAX - (GameTime - Players[Player_num].cloak_time)));
2020                         if (GameTime - Players[i].cloak_time > CLOAK_TIME_MAX) {
2021                                 Players[i].flags &= ~PLAYER_FLAGS_CLOAKED;
2022                                 if (i == Player_num) {
2023                                         digi_play_sample( SOUND_CLOAK_OFF, F1_0);
2024                                         #ifdef NETWORK
2025                                         if (Game_mode & GM_MULTI)
2026                                                 multi_send_play_sound(SOUND_CLOAK_OFF, F1_0);
2027                                         maybe_drop_net_powerup(POW_CLOAK);
2028                                         multi_send_decloak(); // For demo recording
2029                                         #endif
2030 //                                      mprintf((0, " --- You have been DE-CLOAKED! ---\n"));
2031                                 }
2032                         }
2033                 }
2034 }
2035
2036 int FakingInvul=0;
2037
2038 //      ------------------------------------------------------------------------------------
2039 void do_invulnerable_stuff(void)
2040 {
2041         if (Players[Player_num].flags & PLAYER_FLAGS_INVULNERABLE) {
2042                 if (GameTime - Players[Player_num].invulnerable_time > INVULNERABLE_TIME_MAX) {
2043                         Players[Player_num].flags ^= PLAYER_FLAGS_INVULNERABLE;
2044                         if (FakingInvul==0)
2045                         {
2046                                 digi_play_sample( SOUND_INVULNERABILITY_OFF, F1_0);
2047                                 #ifdef NETWORK
2048                                 if (Game_mode & GM_MULTI)
2049                                 {
2050                                         multi_send_play_sound(SOUND_INVULNERABILITY_OFF, F1_0);
2051                                         maybe_drop_net_powerup(POW_INVULNERABILITY);
2052                                 }
2053                                 #endif
2054                                 mprintf((0, " --- You have been DE-INVULNERABLEIZED! ---\n"));
2055                         }
2056                         FakingInvul=0;
2057                 }
2058         }
2059 }
2060
2061 ubyte   Last_afterburner_state = 0;
2062 fix Last_afterburner_charge = 0;
2063
2064 #define AFTERBURNER_LOOP_START  ((digi_sample_rate==SAMPLE_RATE_22K)?32027:(32027/2))           //20098
2065 #define AFTERBURNER_LOOP_END            ((digi_sample_rate==SAMPLE_RATE_22K)?48452:(48452/2))           //25776
2066
2067 int     Ab_scale = 4;
2068
2069 //@@//  ------------------------------------------------------------------------------------
2070 //@@void afterburner_shake(void)
2071 //@@{
2072 //@@    int     rx, rz;
2073 //@@
2074 //@@    rx = (Ab_scale * fixmul(d_rand() - 16384, F1_0/8 + (((GameTime + 0x4000)*4) & 0x3fff)))/16;
2075 //@@    rz = (Ab_scale * fixmul(d_rand() - 16384, F1_0/2 + ((GameTime*4) & 0xffff)))/16;
2076 //@@
2077 //@@    // -- mprintf((0, "AB: %8x %8x\n", rx, rz));
2078 //@@    ConsoleObject->mtype.phys_info.rotvel.x += rx;
2079 //@@    ConsoleObject->mtype.phys_info.rotvel.z += rz;
2080 //@@
2081 //@@}
2082
2083 //      ------------------------------------------------------------------------------------
2084 #ifdef NETWORK
2085 extern void multi_send_sound_function (char,char);
2086 #endif
2087
2088 void do_afterburner_stuff(void)
2089 {
2090    if (!(Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER))
2091                 Afterburner_charge=0;
2092
2093         if (Endlevel_sequence || Player_is_dead)
2094                 {
2095                  digi_kill_sound_linked_to_object( Players[Player_num].objnum);
2096 #ifdef NETWORK
2097                  multi_send_sound_function (0,0);
2098 #endif
2099                 }
2100
2101         if ((Controls.afterburner_state != Last_afterburner_state && Last_afterburner_charge) || (Last_afterburner_state && Last_afterburner_charge && !Afterburner_charge)) {
2102
2103                 if (Afterburner_charge && Controls.afterburner_state && (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER)) {
2104                         digi_link_sound_to_object3( SOUND_AFTERBURNER_IGNITE, Players[Player_num].objnum, 1, F1_0, i2f(256), AFTERBURNER_LOOP_START, AFTERBURNER_LOOP_END );
2105 #ifdef NETWORK
2106                         if (Game_mode & GM_MULTI)
2107                                 multi_send_sound_function (3,SOUND_AFTERBURNER_IGNITE);
2108 #endif
2109                 } else {
2110                         digi_kill_sound_linked_to_object( Players[Player_num].objnum);
2111                         digi_link_sound_to_object2( SOUND_AFTERBURNER_PLAY, Players[Player_num].objnum, 0, F1_0, i2f(256));
2112 #ifdef NETWORK
2113                         if (Game_mode & GM_MULTI)
2114                                 multi_send_sound_function (0,0);
2115 #endif
2116                         mprintf((0,"Killing afterburner sound\n"));
2117                 }
2118         }
2119
2120         //@@if (Controls.afterburner_state && Afterburner_charge)
2121         //@@    afterburner_shake();
2122
2123         Last_afterburner_state = Controls.afterburner_state;
2124         Last_afterburner_charge = Afterburner_charge;
2125 }
2126
2127 // -- //        ------------------------------------------------------------------------------------
2128 // -- //        if energy < F1_0/2, recharge up to F1_0/2
2129 // -- void recharge_energy_frame(void)
2130 // -- {
2131 // --   if (Players[Player_num].energy < Weapon_info[0].energy_usage) {
2132 // --           Players[Player_num].energy += FrameTime/4;
2133 // --
2134 // --           if (Players[Player_num].energy > Weapon_info[0].energy_usage)
2135 // --                   Players[Player_num].energy = Weapon_info[0].energy_usage;
2136 // --   }
2137 // -- }
2138
2139 //      Amount to diminish guns towards normal, per second.
2140 #define DIMINISH_RATE   16              //      gots to be a power of 2, else change the code in diminish_palette_towards_normal
2141
2142 extern fix Flash_effect;
2143
2144  //adds to rgb values for palette flash
2145 void PALETTE_FLASH_ADD(int _dr,int _dg,int _db)
2146 {
2147         int     maxval;
2148
2149         PaletteRedAdd += _dr;
2150         PaletteGreenAdd += _dg;
2151         PaletteBlueAdd += _db;
2152
2153         // -- mprintf((0, "Palette add: %3i %3i %3i\n", PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd));
2154
2155         if (Flash_effect)
2156                 maxval = 60;
2157         else
2158                 maxval = MAX_PALETTE_ADD;
2159
2160         if (PaletteRedAdd > maxval)
2161                 PaletteRedAdd = maxval;
2162
2163         if (PaletteGreenAdd > maxval)
2164                 PaletteGreenAdd = maxval;
2165
2166         if (PaletteBlueAdd > maxval)
2167                 PaletteBlueAdd = maxval;
2168
2169         if (PaletteRedAdd < -maxval)
2170                 PaletteRedAdd = -maxval;
2171
2172         if (PaletteGreenAdd < -maxval)
2173                 PaletteGreenAdd = -maxval;
2174
2175         if (PaletteBlueAdd < -maxval)
2176                 PaletteBlueAdd = -maxval;
2177 }
2178
2179 fix     Time_flash_last_played;
2180
2181
2182 void game_palette_step_up( int r, int g, int b );
2183 //      ------------------------------------------------------------------------------------
2184 //      Diminish palette effects towards normal.
2185 void diminish_palette_towards_normal(void)
2186 {
2187         int     dec_amount = 0;
2188
2189         //      Diminish at DIMINISH_RATE units/second.
2190         //      For frame rates > DIMINISH_RATE Hz, use randomness to achieve this.
2191         if (FrameTime < F1_0/DIMINISH_RATE) {
2192                 if (d_rand() < FrameTime*DIMINISH_RATE/2)       //      Note: d_rand() is in 0..32767, and 8 Hz means decrement every frame
2193                         dec_amount = 1;
2194         } else {
2195                 dec_amount = f2i(FrameTime*DIMINISH_RATE);              // one second = DIMINISH_RATE counts
2196                 if (dec_amount == 0)
2197                         dec_amount++;                                           // make sure we decrement by something
2198         }
2199
2200         if (Flash_effect) {
2201                 int     force_do = 0;
2202
2203                 //      Part of hack system to force update of palette after exiting a menu.
2204                 if (Time_flash_last_played) {
2205                         force_do = 1;
2206                         PaletteRedAdd ^= 1;     //      Very Tricky!  In gr_palette_step_up, if all stepups same as last time, won't do anything!
2207                 }
2208
2209                 if ((Time_flash_last_played + F1_0/8 < GameTime) || (Time_flash_last_played > GameTime)) {
2210                         digi_play_sample( SOUND_CLOAK_OFF, Flash_effect/4);
2211                         Time_flash_last_played = GameTime;
2212                 }
2213
2214                 Flash_effect -= FrameTime;
2215                 if (Flash_effect < 0)
2216                         Flash_effect = 0;
2217
2218                 if (force_do || (d_rand() > 4096 )) {
2219         if ( (Newdemo_state==ND_STATE_RECORDING) && (PaletteRedAdd || PaletteGreenAdd || PaletteBlueAdd) )
2220                 newdemo_record_palette_effect(PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2221
2222                         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2223
2224                         return;
2225                 }
2226
2227         }
2228
2229         if (PaletteRedAdd > 0 ) { PaletteRedAdd -= dec_amount; if (PaletteRedAdd < 0 ) PaletteRedAdd = 0; }
2230         if (PaletteRedAdd < 0 ) { PaletteRedAdd += dec_amount; if (PaletteRedAdd > 0 ) PaletteRedAdd = 0; }
2231
2232         if (PaletteGreenAdd > 0 ) { PaletteGreenAdd -= dec_amount; if (PaletteGreenAdd < 0 ) PaletteGreenAdd = 0; }
2233         if (PaletteGreenAdd < 0 ) { PaletteGreenAdd += dec_amount; if (PaletteGreenAdd > 0 ) PaletteGreenAdd = 0; }
2234
2235         if (PaletteBlueAdd > 0 ) { PaletteBlueAdd -= dec_amount; if (PaletteBlueAdd < 0 ) PaletteBlueAdd = 0; }
2236         if (PaletteBlueAdd < 0 ) { PaletteBlueAdd += dec_amount; if (PaletteBlueAdd > 0 ) PaletteBlueAdd = 0; }
2237
2238         if ( (Newdemo_state==ND_STATE_RECORDING) && (PaletteRedAdd || PaletteGreenAdd || PaletteBlueAdd) )
2239                 newdemo_record_palette_effect(PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2240
2241         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2242
2243         //mprintf(0, "%2i %2i %2i\n", PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd);
2244 }
2245
2246 int     Redsave, Bluesave, Greensave;
2247
2248 void palette_save(void)
2249 {
2250         Redsave = PaletteRedAdd; Bluesave = PaletteBlueAdd; Greensave = PaletteGreenAdd;
2251 }
2252
2253 extern void gr_palette_step_up_vr( int r, int g, int b, int white, int black );
2254
2255 void game_palette_step_up( int r, int g, int b )
2256 {
2257         if ( VR_use_reg_code )  {
2258         #ifndef WINDOWS
2259 //              gr_palette_step_up_vr( r, g, b, VR_WHITE_INDEX, VR_BLACK_INDEX );
2260         #endif
2261         } else {
2262                 gr_palette_step_up( r, g, b );
2263         }
2264 }
2265
2266 void palette_restore(void)
2267 {
2268         PaletteRedAdd = Redsave; PaletteBlueAdd = Bluesave; PaletteGreenAdd = Greensave;
2269         game_palette_step_up( PaletteRedAdd, PaletteGreenAdd, PaletteBlueAdd );
2270
2271         //      Forces flash effect to fixup palette next frame.
2272         Time_flash_last_played = 0;
2273 }
2274
2275 extern void dead_player_frame(void);
2276
2277
2278 //      --------------------------------------------------------------------------------------------------
2279 int allowed_to_fire_laser(void)
2280 {
2281         if (Player_is_dead) {
2282                 Global_missile_firing_count = 0;
2283                 return 0;
2284         }
2285
2286         //      Make sure enough time has elapsed to fire laser, but if it looks like it will
2287         //      be a long while before laser can be fired, then there must be some mistake!
2288         if (Next_laser_fire_time > GameTime)
2289                 if (Next_laser_fire_time < GameTime + 2*F1_0)
2290                         return 0;
2291
2292         return 1;
2293 }
2294
2295 fix     Next_flare_fire_time = 0;
2296 #define FLARE_BIG_DELAY (F1_0*2)
2297
2298 int allowed_to_fire_flare(void)
2299 {
2300         if (Next_flare_fire_time > GameTime)
2301                 if (Next_flare_fire_time < GameTime + FLARE_BIG_DELAY)  //      In case time is bogus, never wait > 1 second.
2302                         return 0;
2303
2304         if (Players[Player_num].energy >= Weapon_info[FLARE_ID].energy_usage)
2305                 Next_flare_fire_time = GameTime + F1_0/4;
2306         else
2307                 Next_flare_fire_time = GameTime + FLARE_BIG_DELAY;
2308
2309         return 1;
2310 }
2311
2312 int allowed_to_fire_missile(void)
2313 {
2314 // mprintf(0, "Next fire = %7.3f, Cur time = %7.3f\n", f2fl(Next_missile_fire_time), f2fl(GameTime));
2315         //      Make sure enough time has elapsed to fire missile, but if it looks like it will
2316         //      be a long while before missile can be fired, then there must be some mistake!
2317         if (Next_missile_fire_time > GameTime)
2318                 if (Next_missile_fire_time < GameTime + 5*F1_0)
2319                         return 0;
2320
2321         return 1;
2322 }
2323
2324 void full_palette_save(void)
2325 {
2326         palette_save();
2327         apply_modified_palette();
2328         reset_palette_add();
2329         gr_palette_load( gr_palette );
2330 }
2331
2332 extern int Death_sequence_aborted;
2333 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) );
2334
2335 void show_help()
2336 {
2337         int nitems;
2338         newmenu_item m[25];
2339         #ifdef MACINTOSH
2340         char command_help[64], pixel_double_help[64], save_help[64], restore_help[64];
2341         #endif
2342
2343         m[ 0].type = NM_TYPE_TEXT; m[ 0].text = TXT_HELP_ESC;
2344         #ifndef MACINTOSH
2345         m[ 1].type = NM_TYPE_TEXT; m[ 1].text = TXT_HELP_ALT_F2;
2346         m[ 2].type = NM_TYPE_TEXT; m[ 2].text = TXT_HELP_ALT_F3;
2347         #else
2348         sprintf(save_help, "OPT-F2 (%c-s)\t Save Game", 133);
2349         sprintf(restore_help, "OPT-F3 (%c-o)\t Load Game", 133);
2350         m[ 1].type = NM_TYPE_TEXT; m[1].text = save_help;
2351         m[ 2].type = NM_TYPE_TEXT; m[2].text = restore_help;
2352         #endif
2353         m[ 3].type = NM_TYPE_TEXT; m[ 3].text = TXT_HELP_F2;
2354         m[ 4].type = NM_TYPE_TEXT; m[ 4].text = TXT_HELP_F3;
2355         m[ 5].type = NM_TYPE_TEXT; m[ 5].text = TXT_HELP_F4;
2356         m[ 6].type = NM_TYPE_TEXT; m[ 6].text = TXT_HELP_F5;
2357         #ifndef MACINTOSH
2358         m[ 7].type = NM_TYPE_TEXT; m[ 7].text = TXT_HELP_PAUSE;
2359         #else
2360         m[ 7].type = NM_TYPE_TEXT; m[ 7].text = "Pause (F15)\t  Pause";
2361         #endif
2362         m[ 8].type = NM_TYPE_TEXT; m[ 8].text = TXT_HELP_MINUSPLUS;
2363         #ifndef MACINTOSH
2364         m[ 9].type = NM_TYPE_TEXT; m[ 9].text = TXT_HELP_PRTSCN;
2365         #else
2366         m[ 9].type = NM_TYPE_TEXT; m[ 9].text = "printscrn (F13)\t  save screen shot";
2367         #endif
2368         m[10].type = NM_TYPE_TEXT; m[10].text = TXT_HELP_1TO5;
2369         m[11].type = NM_TYPE_TEXT; m[11].text = TXT_HELP_6TO10;
2370         m[12].type = NM_TYPE_TEXT; m[12].text = "Shift-F1\t  Cycle left window";
2371         m[13].type = NM_TYPE_TEXT; m[13].text = "Shift-F2\t  Cycle right window";
2372         m[14].type = NM_TYPE_TEXT; m[14].text = "Shift-F4\t  GuideBot menu";
2373         #ifndef MACINTOSH
2374         m[15].type = NM_TYPE_TEXT; m[15].text = "Alt-Shift-F4\t  Rename GuideBot";
2375         #else
2376         m[15].type = NM_TYPE_TEXT; m[15].text = "Opt-Shift-F4\t  Rename GuideBot";
2377         #endif
2378         m[16].type = NM_TYPE_TEXT; m[16].text = "Shift-F5\t  Drop primary";
2379         m[17].type = NM_TYPE_TEXT; m[17].text = "Shift-F6\t  Drop secondary";
2380         m[18].type = NM_TYPE_TEXT; m[18].text = "Shift-F7\t  Calibrate joystick";
2381         m[19].type = NM_TYPE_TEXT; m[19].text = "Shift-number\t  GuideBot commands";
2382         nitems = 20;
2383         #ifdef MACINTOSH
2384         sprintf(pixel_double_help, "%c-D\t  Toggle Pixel Double Mode", 133);
2385         m[20].type = NM_TYPE_TEXT; m[20].text = pixel_double_help;
2386         m[21].type = NM_TYPE_TEXT; m[21].text = "";
2387         sprintf(command_help, "(Use %c-# for F#. i.e. %c-1 for F1)", 133, 133);
2388         m[22].type = NM_TYPE_TEXT; m[22].text = command_help;
2389         nitems = 23;
2390         #endif
2391
2392         full_palette_save();
2393
2394         newmenu_dotiny2( NULL, TXT_KEYS, nitems, m, NULL );
2395
2396         palette_restore();
2397 }
2398
2399 //temp function until Matt cleans up game sequencing
2400 extern void temp_reset_stuff_on_level();
2401
2402 //deal with rear view - switch it on, or off, or whatever
2403 void check_rear_view()
2404 {
2405
2406         #define LEAVE_TIME 0x1000               //how long until we decide key is down  (Used to be 0x4000)
2407
2408         static int leave_mode;
2409         static fix entry_time;
2410
2411         if ( Controls.rear_view_down_count )    {               //key/button has gone down
2412
2413                 if (Rear_view) {
2414                         Rear_view = 0;
2415                         if (Cockpit_mode==CM_REAR_VIEW) {
2416                                 select_cockpit(Cockpit_mode_save);
2417                                 Cockpit_mode_save = -1;
2418                         }
2419                         if (Newdemo_state == ND_STATE_RECORDING)
2420                                 newdemo_record_restore_rearview();
2421                 }
2422                 else {
2423                         Rear_view = 1;
2424                         leave_mode = 0;         //means wait for another key
2425                         entry_time = timer_get_fixed_seconds();
2426                         if (Cockpit_mode == CM_FULL_COCKPIT) {
2427                                 Cockpit_mode_save = Cockpit_mode;
2428                                 select_cockpit(CM_REAR_VIEW);
2429                         }
2430                         if (Newdemo_state == ND_STATE_RECORDING)
2431                                 newdemo_record_rearview();
2432                 }
2433         }
2434         else
2435                 if (Controls.rear_view_down_state) {
2436
2437                         if (leave_mode==0 && (timer_get_fixed_seconds()-entry_time)>LEAVE_TIME)
2438                                 leave_mode = 1;
2439                 }
2440                 else {
2441
2442                         //@@if (leave_mode==1 && Cockpit_mode==CM_REAR_VIEW) {
2443
2444                         if (leave_mode==1 && Rear_view) {
2445                                 Rear_view = 0;
2446                                 if (Cockpit_mode==CM_REAR_VIEW) {
2447                                         select_cockpit(Cockpit_mode_save);
2448                                         Cockpit_mode_save = -1;
2449                                 }
2450                                 if (Newdemo_state == ND_STATE_RECORDING)
2451                                         newdemo_record_restore_rearview();
2452                         }
2453                 }
2454 }
2455
2456 void reset_rear_view(void)
2457 {
2458         if (Rear_view) {
2459                 if (Newdemo_state == ND_STATE_RECORDING)
2460                         newdemo_record_restore_rearview();
2461         }
2462
2463         Rear_view = 0;
2464
2465         if (!(Cockpit_mode == CM_FULL_COCKPIT || Cockpit_mode == CM_STATUS_BAR || Cockpit_mode == CM_FULL_SCREEN)) {
2466                 if (!(Cockpit_mode_save == CM_FULL_COCKPIT || Cockpit_mode_save == CM_STATUS_BAR || Cockpit_mode_save == CM_FULL_SCREEN))
2467                         Cockpit_mode_save = CM_FULL_COCKPIT;
2468                 select_cockpit(Cockpit_mode_save);
2469                 Cockpit_mode_save       = -1;
2470         }
2471
2472 }
2473
2474 int Automap_flag;
2475 int Config_menu_flag;
2476
2477 jmp_buf LeaveGame;
2478
2479 int Cheats_enabled=0;
2480
2481 extern int Laser_rapid_fire;
2482 extern void do_lunacy_on(), do_lunacy_off();
2483
2484 extern int Physics_cheat_flag,Robots_kill_robots_cheat;
2485 extern char BounceCheat,HomingCheat,OldHomingState[20];
2486 extern char AcidCheatOn,old_IntMethod, Monster_mode;
2487 extern int Buddy_dude_cheat;
2488
2489 //turns off active cheats
2490 void turn_cheats_off()
2491 {
2492         int i;
2493
2494         if (HomingCheat)
2495                 for (i=0;i<20;i++)
2496                         Weapon_info[i].homing_flag=OldHomingState[i];
2497
2498         if (AcidCheatOn)
2499         {
2500                 AcidCheatOn=0;
2501                 Interpolation_method=old_IntMethod;
2502         }
2503
2504         Buddy_dude_cheat = 0;
2505         BounceCheat=0;
2506    HomingCheat=0;
2507         do_lunacy_off();
2508         Laser_rapid_fire = 0;
2509         Physics_cheat_flag = 0;
2510         Monster_mode = 0;
2511         Robots_kill_robots_cheat=0;
2512         Robot_firing_enabled = 1;
2513 }
2514
2515 //turns off all cheats & resets cheater flag    
2516 void game_disable_cheats()
2517 {
2518         turn_cheats_off();
2519         Cheats_enabled=0;
2520 }
2521
2522
2523 //      game_setup()
2524 // ----------------------------------------------------------------------------
2525
2526 void game_setup(void)
2527 {
2528         //@@int demo_playing=0;
2529         //@@int multi_game=0;
2530
2531         do_lunacy_on();         //      Copy values for insane into copy buffer in ai.c
2532         do_lunacy_off();                //      Restore true insane mode.
2533
2534         Game_aborted = 0;
2535         last_drawn_cockpit[0] = -1;                             // Force cockpit to redraw next time a frame renders.
2536         last_drawn_cockpit[1] = -1;                             // Force cockpit to redraw next time a frame renders.
2537         Endlevel_sequence = 0;
2538
2539         //@@if ( Newdemo_state == ND_STATE_PLAYBACK )
2540         //@@    demo_playing = 1;
2541         //@@if ( Game_mode & GM_MULTI )
2542         //@@    multi_game = 1;
2543
2544         set_screen_mode(SCREEN_GAME);
2545         reset_palette_add();
2546
2547         set_warn_func(game_show_warning);
2548
2549         init_cockpit();
2550         init_gauges();
2551         //digi_init_sounds();
2552
2553         //keyd_repeat = 0;                // Don't allow repeat in game
2554         keyd_repeat = 1;                // Do allow repeat in game
2555
2556 #if !defined(WINDOWS) && !defined(MACINTOSH)
2557         //_MARK_("start of game");
2558 #endif
2559
2560         #ifdef EDITOR
2561                 if (Segments[ConsoleObject->segnum].segnum == -1)      //segment no longer exists
2562                         obj_relink( ConsoleObject-Objects, SEG_PTR_2_NUM(Cursegp) );
2563
2564                 if (!check_obj_seg(ConsoleObject))
2565                         move_player_2_segment(Cursegp,Curside);
2566         #endif
2567
2568         Viewer = ConsoleObject;
2569         fly_init(ConsoleObject);
2570
2571         Game_suspended = 0;
2572
2573         reset_time();
2574         FrameTime = 0;                  //make first frame zero
2575
2576         #ifdef EDITOR
2577         if (Current_level_num == 0) {                   //not a real level
2578                 init_player_stats_game();
2579                 init_ai_objects();
2580         }
2581         #endif
2582
2583         fix_object_segs();
2584
2585         game_flush_inputs();
2586
2587 }
2588
2589
2590 #ifdef NETWORK
2591 extern char IWasKicked;
2592 #endif
2593
2594
2595 //      ------------------------------------------------------------------------------------
2596 //this function is the game.  called when game mode selected.  runs until
2597 //editor mode or exit selected
2598 void game()
2599 {
2600         game_setup();                                                           // Replaces what was here earlier.
2601                                                                                                         // Good for Windows Sake.
2602
2603 #ifdef MWPROFILE
2604         ProfilerSetStatus(1);
2605 #endif
2606
2607         if ( setjmp(LeaveGame)==0 )     {
2608                 while (1) {
2609                         int player_shields;
2610
2611                         // GAME LOOP!
2612                         Automap_flag = 0;
2613                         Config_menu_flag = 0;
2614
2615                         if ( ConsoleObject != &Objects[Players[Player_num].objnum] )
2616                           {
2617                             mprintf ((0,"Player_num=%d objnum=%d",Player_num,Players[Player_num].objnum));
2618                             //Assert( ConsoleObject == &Objects[Players[Player_num].objnum] );
2619                           }
2620
2621                         player_shields = Players[Player_num].shields;
2622
2623                 #ifdef WINDOWS
2624                 {
2625                         MSG msg;
2626                         DoMessageStuff(&msg);           // Do Windows event handling.
2627                         if (_RedrawScreen) {
2628                                 _RedrawScreen = FALSE;
2629                                 load_palette(Current_level_palette,1,1);
2630                                 gr_palette_load(gr_palette);
2631                         }
2632                 }
2633                 #endif          
2634
2635                         ExtGameStatus=GAMESTAT_RUNNING;
2636                         GameLoop( 1, 1 );               // Do game loop with rendering and reading controls.
2637
2638                         //if the player is taking damage, give up guided missile control
2639                         if (Players[Player_num].shields != player_shields)
2640                                 release_guided_missile(Player_num);
2641
2642                         //see if redbook song needs to be restarted
2643                         songs_check_redbook_repeat();   // Handle RedBook Audio Repeating.
2644
2645                         if (Config_menu_flag)   {
2646                                 int double_save = Scanline_double;
2647
2648                                 //WIN(mouse_set_mode(0));
2649                                 if (!(Game_mode&GM_MULTI)) {palette_save(); reset_palette_add();        apply_modified_palette(); gr_palette_load( gr_palette ); }
2650                                 do_options_menu();
2651                                 if (Scanline_double != double_save)     init_cockpit();
2652                                 if (!(Game_mode&GM_MULTI)) palette_restore();
2653                                 //WIN(mouse_set_mode(1));
2654                         }
2655
2656                         if (Automap_flag) {
2657                                 int save_w=Game_window_w,save_h=Game_window_h;
2658                                 do_automap(0);
2659                                 Screen_mode=-1; set_screen_mode(SCREEN_GAME);
2660                                 Game_window_w=save_w; Game_window_h=save_h;
2661                                 init_cockpit();
2662                                 last_drawn_cockpit[0] = -1;
2663                                 last_drawn_cockpit[1] = -1;
2664                         }
2665
2666                         if ( (Function_mode != FMODE_GAME) && Auto_demo && (Newdemo_state != ND_STATE_NORMAL) ) {
2667                                 int choice, fmode;
2668                                 fmode = Function_mode;
2669                                 Function_mode = FMODE_GAME;
2670                                 palette_save();
2671                                 apply_modified_palette();
2672                                 reset_palette_add();
2673                                 gr_palette_load( gr_palette );
2674                                 choice=nm_messagebox( NULL, 2, TXT_YES, TXT_NO, TXT_ABORT_AUTODEMO );
2675                                 palette_restore();
2676                                 Function_mode = fmode;
2677                                 if (choice==0)  {
2678                                         Auto_demo = 0;
2679                                         newdemo_stop_playback();
2680                                         Function_mode = FMODE_MENU;
2681                                 } else {
2682                                         Function_mode = FMODE_GAME;
2683                                 }
2684                         }
2685
2686                         if ( (Function_mode != FMODE_GAME ) && (Newdemo_state != ND_STATE_PLAYBACK ) && (Function_mode!=FMODE_EDITOR)
2687 #ifdef NETWORK
2688                                         && !IWasKicked
2689 #endif
2690                            )            {
2691                                 int choice, fmode;
2692                                 fmode = Function_mode;
2693                                 Function_mode = FMODE_GAME;
2694                                 palette_save();
2695                                 apply_modified_palette();
2696                                 reset_palette_add();
2697                                 gr_palette_load( gr_palette );
2698                                 ExtGameStatus=GAMESTAT_ABORT_GAME;
2699                                 choice=nm_messagebox( NULL, 2, TXT_YES, TXT_NO, TXT_ABORT_GAME );
2700                                 palette_restore();
2701                                 Function_mode = fmode;
2702                                 if (choice != 0)
2703                                         Function_mode = FMODE_GAME;
2704                         }
2705
2706 #ifdef NETWORK
2707                         IWasKicked=0;
2708 #endif
2709                         if (Function_mode != FMODE_GAME)
2710                                 longjmp(LeaveGame,0);
2711
2712                         #ifdef APPLE_DEMO
2713                         if ( (keyd_time_when_last_pressed + (F1_0 * 60)) < timer_get_fixed_seconds() )          // idle in game for 1 minutes means exit
2714                                 longjmp(LeaveGame,0);
2715                         #endif
2716                 }
2717         }
2718
2719 #ifdef MWPROFILE
2720         ProfilerSetStatus(0);
2721 #endif
2722
2723         digi_stop_all();
2724
2725         if ( (Newdemo_state == ND_STATE_RECORDING) || (Newdemo_state == ND_STATE_PAUSED) )
2726                 newdemo_stop_recording();
2727
2728         #ifdef NETWORK
2729         multi_leave_game();
2730         #endif
2731
2732         if ( Newdemo_state == ND_STATE_PLAYBACK )
2733                 newdemo_stop_playback();
2734
2735    if (Cockpit_mode_save!=-1)
2736          {
2737                 Cockpit_mode=Cockpit_mode_save;
2738                 Cockpit_mode_save=-1;           
2739          }
2740
2741         if (Function_mode != FMODE_EDITOR)
2742                 gr_palette_fade_out(gr_palette,32,0);                   // Fade out before going to menu
2743
2744 //@@    if ( (!demo_playing) && (!multi_game) && (Function_mode != FMODE_EDITOR))       {
2745 //@@            scores_maybe_add_player(Game_aborted);
2746 //@@    }
2747
2748 #if !defined(WINDOWS) && !defined(MACINTOSH)
2749         //_MARK_("end of game");
2750 #endif
2751
2752         clear_warn_func(game_show_warning);     //don't use this func anymore
2753
2754         game_disable_cheats();
2755
2756         #ifdef APPLE_DEMO
2757         Function_mode = FMODE_EXIT;             // get out of game in Apple OEM version
2758         #endif
2759 }
2760
2761 //called at the end of the program
2762 void close_game()
2763 {
2764 #ifdef WINDOWS
2765         if (dd_VR_offscreen_buffer) {
2766                 if (dd_grd_backcanv != dd_VR_offscreen_buffer) {
2767                         dd_gr_free_canvas(dd_VR_offscreen_buffer);
2768                 }
2769                 dd_VR_offscreen_buffer = NULL;
2770                 VR_offscreen_buffer = NULL;
2771         }
2772 #else
2773         if (VR_offscreen_buffer)        {
2774                 gr_free_canvas(VR_offscreen_buffer);
2775                 VR_offscreen_buffer = NULL;
2776         }
2777 #endif
2778
2779         close_gauge_canvases();
2780
2781         restore_effect_bitmap_icons();
2782
2783 #if !defined(MACINTOSH) && !defined(WINDOWS)
2784         if (Game_cockpit_copy_code)     {
2785                 d_free(Game_cockpit_copy_code);
2786                 Game_cockpit_copy_code = NULL;
2787         }
2788 #else
2789         if (Game_cockpit_copy_code)
2790                 Game_cockpit_copy_code = 0;
2791 #endif
2792
2793         if (background_bitmap.bm_data)
2794                 d_free(background_bitmap.bm_data);
2795
2796         clear_warn_func(game_show_warning);     //don't use this func anymore
2797 }
2798
2799 #ifdef WINDOWS
2800 dd_grs_canvas * get_current_game_screen()
2801 {
2802         return &dd_VR_screen_pages[VR_current_page];
2803 }
2804
2805 #else
2806
2807 grs_canvas * get_current_game_screen()
2808 {
2809         return &VR_screen_pages[VR_current_page];
2810 }
2811 #endif
2812
2813
2814 extern void kconfig_center_headset();
2815
2816
2817 #ifndef NDEBUG
2818 void    speedtest_frame(void);
2819 int     Debug_slowdown=0;
2820 #endif
2821
2822 #ifdef EDITOR
2823 extern void player_follow_path(object *objp);
2824 extern void check_create_player_path(void);
2825
2826 #endif
2827
2828 extern  int     Do_appearance_effect;
2829
2830 object *Missile_viewer=NULL;
2831
2832 int Missile_view_enabled = 1;
2833
2834 int Marker_viewer_num[2]={-1,-1};
2835 int Coop_view_player[2]={-1,-1};
2836 int Cockpit_3d_view[2]={CV_NONE,CV_NONE};
2837
2838 //returns ptr to escort robot, or NULL
2839 object *find_escort()
2840 {
2841         int i;
2842
2843         for (i=0; i<=Highest_object_index; i++)
2844                 if (Objects[i].type == OBJ_ROBOT)
2845                         if (Robot_info[Objects[i].id].companion)
2846                                 return &Objects[i];
2847
2848         return NULL;
2849 }
2850
2851 extern void process_super_mines_frame(void);
2852 extern void do_seismic_stuff(void);
2853
2854 #ifndef RELEASE
2855 int Saving_movie_frames=0;
2856 int __Movie_frame_num=0;
2857
2858 #define MAX_MOVIE_BUFFER_FRAMES 250
2859 #define MOVIE_FRAME_SIZE        (320 * 200)
2860
2861 ubyte *Movie_frame_buffer;
2862 int Movie_frame_counter;
2863 ubyte Movie_pal[768];
2864 char movie_path[50] = ".\\";
2865
2866 grs_bitmap Movie_bm;
2867
2868 void flush_movie_buffer()
2869 {
2870         char savename[128];
2871         int f;
2872
2873         stop_time();
2874
2875         mprintf((0,"Flushing movie buffer..."));
2876
2877         Movie_bm.bm_data = Movie_frame_buffer;
2878
2879         for (f=0;f<Movie_frame_counter;f++) {
2880                 sprintf(savename, "%sfrm%04d.pcx",movie_path,__Movie_frame_num);
2881                 __Movie_frame_num++;
2882                 pcx_write_bitmap(savename,&Movie_bm,Movie_pal);
2883                 Movie_bm.bm_data += MOVIE_FRAME_SIZE;
2884
2885                 if (f % 5 == 0)
2886                         mprintf((0,"%3d/%3d\10\10\10\10\10\10\10",f,Movie_frame_counter));
2887         }
2888
2889         Movie_frame_counter=0;
2890
2891         mprintf((0,"done   \n"));
2892
2893         start_time();
2894 }
2895
2896 void toggle_movie_saving()
2897 {
2898         int exit;
2899
2900         Saving_movie_frames = !Saving_movie_frames;
2901
2902         if (Saving_movie_frames) {
2903                 newmenu_item m[1];
2904
2905                 m[0].type=NM_TYPE_INPUT; m[0].text_len = 50; m[0].text = movie_path;
2906                 exit = newmenu_do( NULL, "Directory for movie frames?" , 1, &(m[0]), NULL );
2907
2908                 if (exit==-1) {
2909                         Saving_movie_frames = 0;
2910                         return;
2911                 }
2912
2913                 while (isspace(movie_path[strlen(movie_path)-1]))
2914                         movie_path[strlen(movie_path)-1] = 0;
2915                 if (movie_path[strlen(movie_path)-1]!='\\' && movie_path[strlen(movie_path)-1]!=':')
2916                         strcat(movie_path,"\\");
2917
2918
2919                 if (!Movie_frame_buffer) {
2920                         Movie_frame_buffer = d_malloc(MAX_MOVIE_BUFFER_FRAMES * MOVIE_FRAME_SIZE);
2921                         if (!Movie_frame_buffer) {
2922                                 Int3();
2923                                 Saving_movie_frames=0;
2924                         }
2925
2926                         Movie_frame_counter=0;
2927
2928                         Movie_bm.bm_x = Movie_bm.bm_y = 0;
2929                         Movie_bm.bm_w = 320;
2930                         Movie_bm.bm_h = 200;
2931                         Movie_bm.bm_type = BM_LINEAR;
2932                         Movie_bm.bm_flags = 0;
2933                         Movie_bm.bm_rowsize = 320;
2934                         Movie_bm.bm_handle = 0;
2935
2936                         gr_palette_read(Movie_pal);             //get actual palette from the hardware
2937
2938                         if (Newdemo_state == ND_STATE_PLAYBACK)
2939                                 Newdemo_do_interpolate = 0;
2940                 }
2941         }
2942         else {
2943                 flush_movie_buffer();
2944
2945                 if (Newdemo_state == ND_STATE_PLAYBACK)
2946                         Newdemo_do_interpolate = 1;
2947         }
2948
2949 }
2950
2951 void save_movie_frame()
2952 {
2953         memcpy(Movie_frame_buffer+Movie_frame_counter*MOVIE_FRAME_SIZE,grd_curscreen->sc_canvas.cv_bitmap.bm_data,MOVIE_FRAME_SIZE);
2954
2955         Movie_frame_counter++;
2956
2957         if (Movie_frame_counter == MAX_MOVIE_BUFFER_FRAMES)
2958                 flush_movie_buffer();
2959
2960 }
2961
2962 #endif
2963
2964 extern int Level_shake_duration;
2965
2966 //if water or fire level, make occasional sound
2967 void do_ambient_sounds()
2968 {
2969         int has_water,has_lava;
2970         int sound;
2971
2972         has_lava = (Segment2s[ConsoleObject->segnum].s2_flags & S2F_AMBIENT_LAVA);
2973         has_water = (Segment2s[ConsoleObject->segnum].s2_flags & S2F_AMBIENT_WATER);
2974
2975         if (has_lava) {                                                 //has lava
2976                 sound = SOUND_AMBIENT_LAVA;
2977                 if (has_water && (d_rand() & 1))        //both, pick one
2978                         sound = SOUND_AMBIENT_WATER;
2979         }
2980         else if (has_water)                                             //just water
2981                 sound = SOUND_AMBIENT_WATER;
2982         else
2983                 return;
2984
2985         if (((d_rand() << 3) < FrameTime)) {                                            //play the sound
2986                 fix volume = d_rand() + f1_0/2;
2987                 digi_play_sample(sound,volume);
2988         }
2989 }
2990
2991 // -- extern void lightning_frame(void);
2992
2993 void game_render_frame();
2994 extern void omega_charge_frame(void);
2995
2996 extern time_t t_current_time, t_saved_time;
2997
2998 void flicker_lights();
2999
3000 void GameLoop(int RenderFlag, int ReadControlsFlag )
3001 {
3002         #ifndef NDEBUG
3003         //      Used to slow down frame rate for testing things.
3004         //      RenderFlag = 1; // DEBUG
3005         if (Debug_slowdown) {
3006                 int     h, i, j=0;
3007
3008                 for (h=0; h<Debug_slowdown; h++)
3009                         for (i=0; i<1000; i++)
3010                                 j += i;
3011         }
3012         #endif
3013
3014         #ifdef WINDOWS
3015         {
3016                 static int desc_dead_countdown=100;   /*  used if player shouldn't be playing */
3017
3018                 if (desc_id_exit_num) {                          // are we supposed to be checking
3019                         if (!(--desc_dead_countdown)) {// if so, at zero, then pull the plug
3020                                 char time_str[32], time_str2[32];
3021
3022                                 _ctime(&t_saved_time, time_str);
3023                                 _ctime(&t_current_time, time_str2);
3024
3025                                 Error ("EXPIRES %s.  YOUR TIME %s.\n", time_str, time_str2);
3026                                 Error ("Loading overlay -- error number: %d\n", (int)desc_id_exit_num);
3027                         }
3028                 }
3029         }
3030         #endif
3031
3032                 #ifndef RELEASE
3033                 if (FindArg("-invulnerability"))
3034                         Players[Player_num].flags |= PLAYER_FLAGS_INVULNERABLE;
3035                 #endif
3036
3037
3038                 update_player_stats();
3039                 diminish_palette_towards_normal();              //      Should leave palette effect up for as long as possible by putting right before render.
3040                 do_afterburner_stuff();
3041                 do_cloak_stuff();
3042                 do_invulnerable_stuff();
3043                 remove_obsolete_stuck_objects();
3044                 init_ai_frame();
3045                 do_final_boss_frame();
3046                 // -- lightning_frame();
3047                 // -- recharge_energy_frame();
3048
3049                 if ((Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT) && (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON)) {
3050                         static int turned_off=0;
3051                         Players[Player_num].energy -= (FrameTime*3/8);
3052                         if (Players[Player_num].energy < i2f(10)) {
3053                                 if (!turned_off) {
3054                                         Players[Player_num].flags &= ~PLAYER_FLAGS_HEADLIGHT_ON;
3055                                         turned_off = 1;
3056 #ifdef NETWORK
3057                                         if (Game_mode & GM_MULTI)
3058                                                 multi_send_flags(Player_num);           
3059 #endif
3060                                 }
3061                         }
3062                         else
3063                                 turned_off = 0;
3064
3065                         if (Players[Player_num].energy <= 0) {
3066                                 Players[Player_num].energy = 0;
3067                                 Players[Player_num].flags &= ~PLAYER_FLAGS_HEADLIGHT_ON;
3068 #ifdef NETWORK
3069                                 if (Game_mode & GM_MULTI)
3070                                         multi_send_flags(Player_num);           
3071 #endif
3072                         }
3073                 }
3074
3075
3076                 #ifdef EDITOR
3077                 check_create_player_path();
3078                 player_follow_path(ConsoleObject);
3079                 #endif
3080
3081                 #ifdef NETWORK
3082                 if (Game_mode & GM_MULTI)
3083         {
3084          multi_do_frame();
3085          if (Netgame.PlayTimeAllowed && ThisLevelTime>=i2f((Netgame.PlayTimeAllowed*5*60)))
3086              multi_check_for_killgoal_winner();
3087         }
3088
3089                 #endif
3090
3091                 if (RenderFlag) {
3092                         if (force_cockpit_redraw) {                     //screen need redrawing?
3093                                 init_cockpit();
3094                                 force_cockpit_redraw=0;
3095                         }
3096                         game_render_frame();
3097                         //show_extra_views();           //missile view, buddy bot, etc.
3098
3099                         #ifndef RELEASE
3100                         if (Saving_movie_frames)
3101                                 save_movie_frame();
3102                         #endif
3103
3104                 }
3105
3106
3107                 //mprintf(0,"Velocity %2.2f\n", f2fl(vm_vec_mag(&ConsoleObject->phys_info.velocity)));
3108
3109                 calc_frame_time();
3110
3111                 dead_player_frame();
3112                 if (Newdemo_state != ND_STATE_PLAYBACK)
3113                         do_controlcen_dead_frame();
3114
3115                 process_super_mines_frame();
3116                 do_seismic_stuff();
3117                 do_ambient_sounds();
3118
3119                 #ifndef NDEBUG
3120                 if (Speedtest_on)
3121                         speedtest_frame();
3122                 #endif
3123
3124                 if (ReadControlsFlag)
3125                         ReadControls();
3126                 else
3127                         memset(&Controls, 0, sizeof(Controls));
3128
3129                 GameTime += FrameTime;
3130
3131                 if (f2i(GameTime)/10 != f2i(GameTime-FrameTime)/10)
3132                         mprintf((0,"Gametime = %d secs\n",f2i(GameTime)));
3133
3134                 if (GameTime < 0 || GameTime > i2f(0x7fff - 600)) {
3135                         GameTime = FrameTime;   //wrap when goes negative, or gets within 10 minutes
3136                         mprintf((0,"GameTime reset to 0\n"));
3137                 }
3138
3139                 #ifndef NDEBUG
3140                 if (FindArg("-checktime") != 0)
3141                         if (GameTime >= i2f(600))               //wrap after 10 minutes
3142                                 GameTime = FrameTime;
3143                 #endif
3144
3145 #ifdef NETWORK
3146       if ((Game_mode & GM_MULTI) && Netgame.PlayTimeAllowed)
3147           ThisLevelTime +=FrameTime;
3148 #endif
3149
3150                 digi_sync_sounds();
3151
3152                 if (Endlevel_sequence) {
3153                         do_endlevel_frame();
3154                         powerup_grab_cheat_all();
3155                         do_special_effects();
3156                         return;                                 //skip everything else
3157                 }
3158
3159                 if (Newdemo_state != ND_STATE_PLAYBACK)
3160                         do_exploding_wall_frame();
3161                 if ((Newdemo_state != ND_STATE_PLAYBACK) || (Newdemo_vcr_state != ND_STATE_PAUSED)) {
3162                         do_special_effects();
3163                         wall_frame_process();
3164                         triggers_frame_process();
3165                 }
3166
3167
3168                 if (Control_center_destroyed)   {
3169                         if (Newdemo_state==ND_STATE_RECORDING )
3170                                 newdemo_record_control_center_destroyed();
3171                 }
3172
3173                 flash_frame();
3174
3175                 if ( Newdemo_state == ND_STATE_PLAYBACK )       {
3176                         newdemo_playback_one_frame();
3177                         if ( Newdemo_state != ND_STATE_PLAYBACK )               {
3178                                 longjmp( LeaveGame, 0 );                // Go back to menu
3179                         }
3180                 } else
3181                 { // Note the link to above!
3182
3183                         Players[Player_num].homing_object_dist = -1;            //      Assume not being tracked.  Laser_do_weapon_sequence modifies this.
3184
3185                         object_move_all();
3186                         powerup_grab_cheat_all();
3187
3188                         if (Endlevel_sequence)  //might have been started during move
3189                                 return;
3190
3191                         fuelcen_update_all();
3192
3193                         do_ai_frame_all();
3194
3195                         if (allowed_to_fire_laser())
3196                                 FireLaser();                            // Fire Laser!
3197
3198                         if (Auto_fire_fusion_cannon_time) {
3199                                 if (Primary_weapon != FUSION_INDEX)
3200                                         Auto_fire_fusion_cannon_time = 0;
3201                                 else if (GameTime + FrameTime/2 >= Auto_fire_fusion_cannon_time) {
3202                                         Auto_fire_fusion_cannon_time = 0;
3203                                         Global_laser_firing_count = 1;
3204                                 } else {
3205                                         vms_vector      rand_vec;
3206                                         fix                     bump_amount;
3207
3208                                         Global_laser_firing_count = 0;
3209
3210                                         ConsoleObject->mtype.phys_info.rotvel.x += (d_rand() - 16384)/8;
3211                                         ConsoleObject->mtype.phys_info.rotvel.z += (d_rand() - 16384)/8;
3212                                         make_random_vector(&rand_vec);
3213
3214                                         bump_amount = F1_0*4;
3215
3216                                         if (Fusion_charge > F1_0*2)
3217                                                 bump_amount = Fusion_charge*4;
3218
3219                                         bump_one_object(ConsoleObject, &rand_vec, bump_amount);
3220                                 }
3221                         }
3222
3223                         if (Global_laser_firing_count) {
3224                                 //      Don't cap here, gets capped in Laser_create_new and is based on whether in multiplayer mode, MK, 3/27/95
3225                                 // if (Fusion_charge > F1_0*2)
3226                                 //      Fusion_charge = F1_0*2;
3227                                 Global_laser_firing_count -= do_laser_firing_player();  //do_laser_firing(Players[Player_num].objnum, Primary_weapon);
3228                         }
3229
3230                         if (Global_laser_firing_count < 0)
3231                                 Global_laser_firing_count = 0;
3232                 }
3233
3234         if (Do_appearance_effect) {
3235                 create_player_appearance_effect(ConsoleObject);
3236                 Do_appearance_effect = 0;
3237 #ifdef NETWORK
3238                 if ((Game_mode & GM_MULTI) && Netgame.invul)
3239                 {
3240                         Players[Player_num].flags |= PLAYER_FLAGS_INVULNERABLE;
3241                         Players[Player_num].invulnerable_time = GameTime-i2f(27);
3242                         FakingInvul=1;
3243                 }
3244 #endif
3245                         
3246         }
3247
3248         omega_charge_frame();
3249         slide_textures();
3250         flicker_lights();
3251
3252         //!!hoard_light_pulse();                //do cool hoard light pulsing
3253
3254 }
3255
3256 //!!extern int Goal_blue_segnum,Goal_red_segnum;
3257 //!!extern int Hoard_goal_eclip;
3258 //!!
3259 //!!//do cool pulsing lights in hoard goals
3260 //!!hoard_light_pulse()
3261 //!!{
3262 //!!    if (Game_mode & GM_HOARD) {
3263 //!!            fix light;
3264 //!!            int frame;
3265 //!!
3266 //!!            frame = Effects[Hoard_goal_eclip].frame_count;
3267 //!!
3268 //!!            frame++;
3269 //!!
3270 //!!            if (frame >= Effects[Hoard_goal_eclip].vc.num_frames)
3271 //!!                    frame = 0;
3272 //!!
3273 //!!            light = abs(frame - 5) * f1_0 / 5;
3274 //!!
3275 //!!            Segment2s[Goal_red_segnum].static_light = Segment2s[Goal_blue_segnum].static_light = light;
3276 //!!    }
3277 //!!}
3278
3279
3280 ubyte   Slide_segs[MAX_SEGMENTS];
3281 int     Slide_segs_computed;
3282
3283 void compute_slide_segs(void)
3284 {
3285         int     segnum, sidenum;
3286
3287         for (segnum=0;segnum<=Highest_segment_index;segnum++) {
3288                 Slide_segs[segnum] = 0;
3289                 for (sidenum=0;sidenum<6;sidenum++) {
3290                         int tmn = Segments[segnum].sides[sidenum].tmap_num;
3291                         if (TmapInfo[tmn].slide_u != 0 || TmapInfo[tmn].slide_v != 0)
3292                                 Slide_segs[segnum] |= 1 << sidenum;
3293                 }
3294         }
3295
3296         Slide_segs_computed = 1;
3297 }
3298
3299 //      -----------------------------------------------------------------------------
3300 void slide_textures(void)
3301 {
3302         int segnum,sidenum,i;
3303
3304         if (!Slide_segs_computed)
3305                 compute_slide_segs();
3306
3307         for (segnum=0;segnum<=Highest_segment_index;segnum++) {
3308                 if (Slide_segs[segnum]) {
3309                         for (sidenum=0;sidenum<6;sidenum++) {
3310                                 if (Slide_segs[segnum] & (1 << sidenum)) {
3311                                         int tmn = Segments[segnum].sides[sidenum].tmap_num;
3312                                         if (TmapInfo[tmn].slide_u != 0 || TmapInfo[tmn].slide_v != 0) {
3313                                                 for (i=0;i<4;i++) {
3314                                                         Segments[segnum].sides[sidenum].uvls[i].u += fixmul(FrameTime,TmapInfo[tmn].slide_u<<8);
3315                                                         Segments[segnum].sides[sidenum].uvls[i].v += fixmul(FrameTime,TmapInfo[tmn].slide_v<<8);
3316                                                         if (Segments[segnum].sides[sidenum].uvls[i].u > f2_0) {
3317                                                                 int j;
3318                                                                 for (j=0;j<4;j++)
3319                                                                         Segments[segnum].sides[sidenum].uvls[j].u -= f1_0;
3320                                                         }
3321                                                         if (Segments[segnum].sides[sidenum].uvls[i].v > f2_0) {
3322                                                                 int j;
3323                                                                 for (j=0;j<4;j++)
3324                                                                         Segments[segnum].sides[sidenum].uvls[j].v -= f1_0;
3325                                                         }
3326                                                         if (Segments[segnum].sides[sidenum].uvls[i].u < -f2_0) {
3327                                                                 int j;
3328                                                                 for (j=0;j<4;j++)
3329                                                                         Segments[segnum].sides[sidenum].uvls[j].u += f1_0;
3330                                                         }
3331                                                         if (Segments[segnum].sides[sidenum].uvls[i].v < -f2_0) {
3332                                                                 int j;
3333                                                                 for (j=0;j<4;j++)
3334                                                                         Segments[segnum].sides[sidenum].uvls[j].v += f1_0;
3335                                                         }
3336                                                 }
3337                                         }
3338                                 }
3339                         }
3340                 }
3341         }
3342 }
3343
3344 flickering_light Flickering_lights[MAX_FLICKERING_LIGHTS];
3345
3346 int Num_flickering_lights=0;
3347
3348 void flicker_lights()
3349 {
3350         int l;
3351         flickering_light *f;
3352
3353         f = Flickering_lights;
3354
3355         for (l=0;l<Num_flickering_lights;l++,f++) {
3356                 segment *segp = &Segments[f->segnum];
3357
3358                 //make sure this is actually a light
3359                 if (! (WALL_IS_DOORWAY(segp, f->sidenum) & WID_RENDER_FLAG))
3360                         continue;
3361                 if (! (TmapInfo[segp->sides[f->sidenum].tmap_num].lighting | TmapInfo[segp->sides[f->sidenum].tmap_num2 & 0x3fff].lighting))
3362                         continue;
3363
3364                 if (f->timer == 0x80000000)             //disabled
3365                         continue;
3366
3367                 if ((f->timer -= FrameTime) < 0) {
3368
3369                         while (f->timer < 0)
3370                                 f->timer += f->delay;
3371
3372                         f->mask = ((f->mask&0x80000000)?1:0) + (f->mask<<1);
3373
3374                         if (f->mask & 1)
3375                                 add_light(f->segnum,f->sidenum);
3376                         else
3377                                 subtract_light(f->segnum,f->sidenum);
3378                 }
3379         }
3380 }
3381
3382 //returns ptr to flickering light structure, or NULL if can't find
3383 flickering_light *find_flicker(int segnum,int sidenum)
3384 {
3385         int l;
3386         flickering_light *f;
3387
3388         //see if there's already an entry for this seg/side
3389
3390         f = Flickering_lights;
3391
3392         for (l=0;l<Num_flickering_lights;l++,f++)
3393                 if (f->segnum == segnum && f->sidenum == sidenum)       //found it!
3394                         return f;
3395
3396         return NULL;
3397 }
3398
3399 //turn flickering off (because light has been turned off)
3400 void disable_flicker(int segnum,int sidenum)
3401 {
3402         flickering_light *f;
3403
3404         if ((f=find_flicker(segnum,sidenum)) != NULL)
3405                 f->timer = 0x80000000;
3406 }
3407
3408 //turn flickering off (because light has been turned on)
3409 void enable_flicker(int segnum,int sidenum)
3410 {
3411         flickering_light *f;
3412
3413         if ((f=find_flicker(segnum,sidenum)) != NULL)
3414                 f->timer = 0;
3415 }
3416
3417
3418 #ifdef EDITOR
3419
3420 //returns 1 if ok, 0 if error
3421 int add_flicker(int segnum,int sidenum,fix delay,ulong mask)
3422 {
3423         int l;
3424         flickering_light *f;
3425
3426         mprintf((0,"add_flicker: %d:%d %x %x\n",segnum,sidenum,delay,mask));
3427
3428         //see if there's already an entry for this seg/side
3429
3430         f = Flickering_lights;
3431
3432         for (l=0;l<Num_flickering_lights;l++,f++)
3433                 if (f->segnum == segnum && f->sidenum == sidenum)       //found it!
3434                         break;
3435
3436         if (mask==0) {          //clearing entry
3437                 if (l == Num_flickering_lights)
3438                         return 0;
3439                 else {
3440                         int i;
3441                         for (i=l;i<Num_flickering_lights-1;i++)
3442                                 Flickering_lights[i] = Flickering_lights[i+1];
3443                         Num_flickering_lights--;
3444                         return 1;
3445                 }
3446         }
3447
3448         if (l == Num_flickering_lights) {
3449                 if (Num_flickering_lights == MAX_FLICKERING_LIGHTS)
3450                         return 0;
3451                 else
3452                         Num_flickering_lights++;
3453         }
3454
3455         f->segnum = segnum;
3456         f->sidenum = sidenum;
3457         f->delay = f->timer = delay;
3458         f->mask = mask;
3459
3460         return 1;
3461 }
3462
3463 #endif
3464
3465 //      -----------------------------------------------------------------------------
3466 //      Fire Laser:  Registers a laser fire, and performs special stuff for the fusion
3467 //                                  cannon.
3468 void FireLaser()
3469 {
3470
3471         Global_laser_firing_count += Weapon_info[Primary_weapon_to_weapon_info[Primary_weapon]].fire_count * (Controls.fire_primary_state || Controls.fire_primary_down_count);
3472
3473         if ((Primary_weapon == FUSION_INDEX) && (Global_laser_firing_count)) {
3474                 if ((Players[Player_num].energy < F1_0*2) && (Auto_fire_fusion_cannon_time == 0)) {
3475                         Global_laser_firing_count = 0;
3476                 } else {
3477                         if (Fusion_charge == 0)
3478                                 Players[Player_num].energy -= F1_0*2;
3479
3480                         Fusion_charge += FrameTime;
3481                         Players[Player_num].energy -= FrameTime;
3482
3483                         if (Players[Player_num].energy <= 0) {
3484                                 Players[Player_num].energy = 0;
3485                                 Auto_fire_fusion_cannon_time = GameTime -1;     //      Fire now!
3486                         } else
3487                                 Auto_fire_fusion_cannon_time = GameTime + FrameTime/2 + 1;
3488                                                                                                 //      Fire the fusion cannon at this time in the future.
3489
3490                         if (Fusion_charge < F1_0*2)
3491                                 PALETTE_FLASH_ADD(Fusion_charge >> 11, 0, Fusion_charge >> 11);
3492                         else
3493                                 PALETTE_FLASH_ADD(Fusion_charge >> 11, Fusion_charge >> 11, 0);
3494
3495                         if (GameTime < Fusion_last_sound_time)          //gametime has wrapped
3496                                 Fusion_next_sound_time = Fusion_last_sound_time = GameTime;
3497
3498                         if (Fusion_next_sound_time < GameTime) {
3499                                 if (Fusion_charge > F1_0*2) {
3500                                         digi_play_sample( 11, F1_0 );
3501                                         apply_damage_to_player(ConsoleObject, ConsoleObject, d_rand() * 4);
3502                                 } else {
3503                                         create_awareness_event(ConsoleObject, PA_WEAPON_ROBOT_COLLISION);
3504                                         digi_play_sample( SOUND_FUSION_WARMUP, F1_0 );
3505                                         #ifdef NETWORK
3506                                         if (Game_mode & GM_MULTI)
3507                                                 multi_send_play_sound(SOUND_FUSION_WARMUP, F1_0);
3508                                         #endif
3509                                 }
3510                                 Fusion_last_sound_time = GameTime;
3511                                 Fusion_next_sound_time = GameTime + F1_0/8 + d_rand()/4;
3512                         }
3513                 }
3514         }
3515
3516 }
3517
3518
3519 //      -------------------------------------------------------------------------------------------------------
3520 //      If player is close enough to objnum, which ought to be a powerup, pick it up!
3521 //      This could easily be made difficulty level dependent.
3522 void powerup_grab_cheat(object *player, int objnum)
3523 {
3524         fix     powerup_size;
3525         fix     player_size;
3526         fix     dist;
3527
3528         Assert(Objects[objnum].type == OBJ_POWERUP);
3529
3530         powerup_size = Objects[objnum].size;
3531         player_size = player->size;
3532
3533         dist = vm_vec_dist_quick(&Objects[objnum].pos, &player->pos);
3534
3535         if ((dist < 2*(powerup_size + player_size)) && !(Objects[objnum].flags & OF_SHOULD_BE_DEAD)) {
3536                 vms_vector      collision_point;
3537
3538                 vm_vec_avg(&collision_point, &Objects[objnum].pos, &player->pos);
3539                 collide_player_and_powerup(player, &Objects[objnum], &collision_point);
3540         }
3541 }
3542
3543 //      -------------------------------------------------------------------------------------------------------
3544 //      Make it easier to pick up powerups.
3545 //      For all powerups in this segment, pick them up at up to twice pickuppable distance based on dot product
3546 //      from player to powerup and player's forward vector.
3547 //      This has the effect of picking them up more easily left/right and up/down, but not making them disappear
3548 //      way before the player gets there.
3549 void powerup_grab_cheat_all(void)
3550 {
3551         segment *segp;
3552         int             objnum;
3553
3554         segp = &Segments[ConsoleObject->segnum];
3555         objnum = segp->objects;
3556
3557         while (objnum != -1) {
3558                 if (Objects[objnum].type == OBJ_POWERUP)
3559                         powerup_grab_cheat(ConsoleObject, objnum);
3560                 objnum = Objects[objnum].next;
3561         }
3562
3563 }
3564
3565 int     Last_level_path_created = -1;
3566
3567 #ifdef SHOW_EXIT_PATH
3568
3569 //      ------------------------------------------------------------------------------------------------------------------
3570 //      Create path for player from current segment to goal segment.
3571 //      Return true if path created, else return false.
3572 int mark_player_path_to_segment(int segnum)
3573 {
3574         int             i;
3575         object  *objp = ConsoleObject;
3576         short           player_path_length=0;
3577         int             player_hide_index=-1;
3578
3579         if (Last_level_path_created == Current_level_num) {
3580                 return 0;
3581         }
3582
3583         Last_level_path_created = Current_level_num;
3584
3585         if (create_path_points(objp, objp->segnum, segnum, Point_segs_free_ptr, &player_path_length, 100, 0, 0, -1) == -1) {
3586                 mprintf((0, "Unable to form path of length %i for myself\n", 100));
3587                 return 0;
3588         }
3589
3590         player_hide_index = Point_segs_free_ptr - Point_segs;
3591         Point_segs_free_ptr += player_path_length;
3592
3593         if (Point_segs_free_ptr - Point_segs + MAX_PATH_LENGTH*2 > MAX_POINT_SEGS) {
3594                 mprintf((1, "Can't create path.  Not enough point_segs.\n"));
3595                 ai_reset_all_paths();
3596                 return 0;
3597         }
3598
3599         for (i=1; i<player_path_length; i++) {
3600                 int                     segnum, objnum;
3601                 vms_vector      seg_center;
3602                 object          *obj;
3603
3604                 segnum = Point_segs[player_hide_index+i].segnum;
3605                 mprintf((0, "%3i ", segnum));
3606                 seg_center = Point_segs[player_hide_index+i].point;
3607
3608                 objnum = obj_create( OBJ_POWERUP, POW_ENERGY, segnum, &seg_center, &vmd_identity_matrix, Powerup_info[POW_ENERGY].size, CT_POWERUP, MT_NONE, RT_POWERUP);
3609                 if (objnum == -1) {
3610                         Int3();         //      Unable to drop energy powerup for path
3611                         return 1;
3612                 }
3613
3614                 obj = &Objects[objnum];
3615                 obj->rtype.vclip_info.vclip_num = Powerup_info[obj->id].vclip_num;
3616                 obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].frame_time;
3617                 obj->rtype.vclip_info.framenum = 0;
3618                 obj->lifeleft = F1_0*100 + d_rand() * 4;
3619         }
3620
3621         mprintf((0, "\n"));
3622         return 1;
3623 }
3624
3625 //      Return true if it happened, else return false.
3626 int create_special_path(void)
3627 {
3628         int     i,j;
3629
3630         //      ---------- Find exit doors ----------
3631         for (i=0; i<=Highest_segment_index; i++)
3632                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
3633                         if (Segments[i].children[j] == -2) {
3634                                 mprintf((0, "Exit at segment %i\n", i));
3635                                 return mark_player_path_to_segment(i);
3636                         }
3637
3638         return 0;
3639 }
3640
3641 #endif
3642
3643
3644 #ifndef RELEASE
3645 int     Max_obj_count_mike = 0;
3646
3647 //      Shows current number of used objects.
3648 void show_free_objects(void)
3649 {
3650         if (!(FrameCount & 8)) {
3651                 int     i;
3652                 int     count=0;
3653
3654                 mprintf((0, "Highest_object_index = %3i, MAX_OBJECTS = %3i, now used = ", Highest_object_index, MAX_OBJECTS));
3655
3656                 for (i=0; i<=Highest_object_index; i++)
3657                         if (Objects[i].type != OBJ_NONE)
3658                                 count++;
3659
3660                 mprintf((0, "%3i", count));
3661
3662                 if (count > Max_obj_count_mike) {
3663                         Max_obj_count_mike = count;
3664                         mprintf((0, " ***"));
3665                 }
3666
3667                 mprintf((0, "\n"));
3668         }
3669
3670 }
3671
3672 #endif
3673
3674 #ifdef WINDOWS
3675 void game_win_init_cockpit_mask(int sram)
3676 {
3677         if (dd_VR_offscreen_buffer && dd_VR_offscreen_buffer != dd_grd_backcanv) {
3678                 dd_gr_free_canvas(dd_VR_offscreen_buffer);
3679         }
3680
3681         if (GRMODEINFO(paged) && !GRMODEINFO(dbuf)) {
3682         //      Here we will make the VR_offscreen_buffer the 2nd page and hopefully
3683         //      we can just flip it, saving a blt.
3684                 Int3();
3685         }
3686         else if (GRMODEINFO(dbuf)||GRMODEINFO(emul)) {
3687         //      The offscreen buffer will be created.  We will just blt this
3688         //      to the screen (which may be blted to the primary surface)
3689                 if ( grd_curscreen->sc_h < 200 ) {
3690                         dd_VR_offscreen_buffer = dd_gr_create_canvas(grd_curscreen->sc_w, 200);
3691                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
3692                         if (sram) {
3693                                 DDFreeSurface(dd_VR_offscreen_buffer->lpdds);
3694                                 dd_VR_offscreen_buffer->lpdds = DDCreateSysMemSurface(grd_curscreen->sc_w, 200);
3695                                 dd_VR_offscreen_buffer->sram = 1;
3696                         }
3697                 }
3698                 else {
3699                         dd_VR_offscreen_buffer = dd_gr_create_canvas(grd_curscreen->sc_w, grd_curscreen->sc_h);
3700                         VR_offscreen_buffer = &dd_VR_offscreen_buffer->canvas;
3701                         if (sram) {
3702                                 DDFreeSurface(dd_VR_offscreen_buffer->lpdds);
3703                                 dd_VR_offscreen_buffer->lpdds = DDCreateSysMemSurface(grd_curscreen->sc_w, grd_curscreen->sc_h);
3704                                 dd_VR_offscreen_buffer->sram = 1;
3705                         }
3706                 }
3707         }
3708
3709         dd_gr_init_sub_canvas( &dd_VR_render_buffer[0], dd_VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3710         dd_gr_init_sub_canvas( &dd_VR_render_buffer[1], dd_VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3711         gr_init_sub_canvas( &VR_render_buffer[0], VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3712         gr_init_sub_canvas( &VR_render_buffer[1], VR_offscreen_buffer, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3713
3714         game_init_render_sub_buffers( 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
3715 }
3716
3717 //@@void game_win_init_cockpit_mask()
3718 //@@{
3719 //@@    char title_pal[768];
3720 //@@    dd_grs_canvas ccanv;
3721 //@@    int pcx_error;
3722 //@@    LPDIRECTDRAWSURFACE dds;
3723 //@@
3724 //@@    dds = DDCreateSurface(GRMODEINFO(w), GRMODEINFO(h), 1);
3725 //@@    Assert(dds != NULL);
3726 //@@    
3727 //@@    _lpDDSMask = dds;
3728 //@@    ccanv.lpdds = dds;
3729 //@@    dd_gr_reinit_canvas(&ccanv);
3730 //@@
3731 //@@    dd_gr_set_current_canvas(&ccanv);
3732 //@@    DDGRLOCK(dd_grd_curcanv)
3733 //@@    {
3734 //@@            if (W95DisplayMode == SM95_640x480x8) {
3735 //@@                    pcx_error=pcx_read_bitmap( "MASKB.PCX", &grd_curcanv->cv_bitmap,
3736 //@@                            grd_curcanv->cv_bitmap.bm_type,
3737 //@@                            title_pal );
3738 //@@            }
3739 //@@            else {
3740 //@@                    pcx_error=pcx_read_bitmap( "MASK.PCX", &grd_curcanv->cv_bitmap,
3741 //@@                            grd_curcanv->cv_bitmap.bm_type,
3742 //@@                            title_pal );
3743 //@@            }
3744 //@@    }
3745 //@@    DDGRUNLOCK(dd_grd_curcanv);
3746 //@@
3747 //@@    Assert(pcx_error == PCX_ERROR_NONE);
3748 //@@    Game_cockpit_copy_code = (ubyte *)(0xABADC0DE);
3749 //@@}
3750
3751 #endif
3752
3753 /*
3754  * reads a flickering_light structure from a CFILE
3755  */
3756 void flickering_light_read(flickering_light *fl, CFILE *fp)
3757 {
3758         fl->segnum = cfile_read_short(fp);
3759         fl->sidenum = cfile_read_short(fp);
3760         fl->mask = cfile_read_int(fp);
3761         fl->timer = cfile_read_fix(fp);
3762         fl->delay = cfile_read_fix(fp);
3763 }