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