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