attempt to support d1 for mac mission
[btb/d2x.git] / main / kmatrix.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 /*
15  * $Source: /cvs/cvsroot/d2x/main/kmatrix.c,v $
16  * $Revision: 1.4 $
17  * $Author: bradleyb $
18  * $Date: 2001-10-23 21:53:18 $
19  *
20  * FIXME: put description here
21  *
22  * $Log: not supported by cvs2svn $
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <conf.h>
28 #endif
29
30 #ifdef WINDOWS
31 #include "desw.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <stdarg.h>
39
40 #include "pa_enabl.h"       //$$POLY_ACC
41 #include "error.h"
42 #include "pstypes.h"
43 #include "gr.h"
44 #include "mono.h"
45 #include "key.h"
46 #include "palette.h"
47 #include "game.h"
48 #include "gamefont.h"
49 #include "u_mem.h"
50 #include "newmenu.h"
51 #include "menu.h"
52 #include "player.h"
53 #include "screens.h"
54 #include "gamefont.h"
55 #include "cntrlcen.h"
56 #include "mouse.h"
57 #include "joy.h"
58 #include "timer.h"
59 #include "text.h"
60 #include "songs.h"
61 #include "multi.h"
62 #include "kmatrix.h"
63 #include "gauges.h"
64 #include "pcx.h"
65 #include "network.h"
66
67 #if defined(POLY_ACC)
68 #include "poly_acc.h"
69 #endif
70
71 #define CENTERING_OFFSET(x) ((300 - (70 + (x)*25 ))/2)
72 #define CENTERSCREEN (MenuHires?320:160)
73
74 int kmatrix_kills_changed = 0;
75 char ConditionLetters[]={' ','P','E','D','E','E','V','W'};
76 char WaitingForOthers=0;
77
78 int Kmatrix_nomovie_message=0;
79
80 extern char MaxPowerupsAllowed[],PowerupsInMine[];
81
82 void kmatrix_reactor (char *message);
83 void kmatrix_phallic ();
84 void kmatrix_redraw_coop();
85
86 #define LHX(x)          ((x)*(MenuHires?2:1))
87 #define LHY(y)          ((y)*(MenuHires?2.4:1))
88  
89 void kmatrix_draw_item( int  i, int *sorted )
90 {
91         int j, x, y;
92         char temp[10];
93
94         y = LHY(50+i*9);
95
96         // Print player name.
97
98         gr_printf( LHX(CENTERING_OFFSET(N_players)), y, "%s", Players[sorted[i]].callsign );
99
100    if (!((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL)))
101            gr_printf (LHX(CENTERING_OFFSET(N_players)-15),y,"%c",ConditionLetters[Players[sorted[i]].connected]);
102     
103         for (j=0; j<N_players; j++) {
104
105                 x = LHX(70 + CENTERING_OFFSET(N_players) + j*25);
106
107                 if (sorted[i]==sorted[j]) {
108                         if (kill_matrix[sorted[i]][sorted[j]] == 0) {
109                                 gr_set_fontcolor( BM_XRGB(10,10,10),-1 );
110                                 gr_printf( x, y, "%d", kill_matrix[sorted[i]][sorted[j]] );
111                         } else {
112                                 gr_set_fontcolor( BM_XRGB(25,25,25),-1 );
113                                 gr_printf( x, y, "-%d", kill_matrix[sorted[i]][sorted[j]] );
114                         }
115                 } else {
116                         if (kill_matrix[sorted[i]][sorted[j]] <= 0) {
117                                 gr_set_fontcolor( BM_XRGB(10,10,10),-1 );
118                                 gr_printf( x, y, "%d", kill_matrix[sorted[i]][sorted[j]] );
119                         } else {
120                                 gr_set_fontcolor( BM_XRGB(25,25,25),-1 );
121                                 gr_printf( x, y, "%d", kill_matrix[sorted[i]][sorted[j]] );
122                         }
123                 }
124
125         }
126
127   if (Players[sorted[i]].net_killed_total+Players[sorted[i]].net_kills_total==0)
128         sprintf (temp,"NA");
129   else
130    sprintf (temp,"%d%%",(int)((float)((float)Players[sorted[i]].net_kills_total/((float)Players[sorted[i]].net_killed_total+(float)Players[sorted[i]].net_kills_total))*100.0));                
131                 
132         x = LHX(60 + CENTERING_OFFSET(N_players) + N_players*25);
133         gr_set_fontcolor( BM_XRGB(25,25,25),-1 );
134         gr_printf( x ,y,"%4d/%s",Players[sorted[i]].net_kills_total,temp);
135 }
136
137 void kmatrix_draw_coop_item( int  i, int *sorted )
138 {
139         int  x, y;
140
141         y = LHY(50+i*9);
142
143         // Print player name.
144
145         gr_printf( LHX(CENTERING_OFFSET(N_players)), y, "%s", Players[sorted[i]].callsign );
146    gr_printf (LHX(CENTERING_OFFSET(N_players)-15),y,"%c",ConditionLetters[Players[sorted[i]].connected]);
147     
148
149         x = CENTERSCREEN;
150
151         gr_set_fontcolor( BM_XRGB(60,40,10),-1 );
152         gr_printf( x, y, "%d", Players[sorted[i]].score );
153
154         x = CENTERSCREEN+LHX(50);
155
156         gr_set_fontcolor( BM_XRGB(60,40,10),-1 );
157         gr_printf( x, y, "%d", Players[sorted[i]].net_killed_total);
158 }
159
160
161 void kmatrix_draw_names(int *sorted)
162 {
163         int j, x;
164         
165         int color;
166
167    if (Kmatrix_nomovie_message)
168     {
169                 gr_set_fontcolor( BM_XRGB(63,0,0),-1 );
170                 gr_printf( CENTERSCREEN-LHX(40), LHY(20), "(Movie not played)");
171          }
172
173         for (j=0; j<N_players; j++) {
174                 if (Game_mode & GM_TEAM)
175                         color = get_team(sorted[j]);
176                 else
177                         color = sorted[j];
178
179                 x = LHX (70 + CENTERING_OFFSET(N_players) + j*25);
180
181       if (Players[sorted[j]].connected==0)
182         gr_set_fontcolor(gr_find_closest_color(31,31,31),-1);
183       else
184         gr_set_fontcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b),-1 );
185
186                 gr_printf( x, LHY(40), "%c", Players[sorted[j]].callsign[0] );
187
188         }
189
190         x = LHX(72 + CENTERING_OFFSET(N_players) + N_players*25);
191         gr_set_fontcolor( BM_XRGB(31,31,31),-1 );
192         gr_printf( x, LHY(40), "K/E");
193                 
194 }
195 void kmatrix_draw_coop_names(int *sorted)
196 {
197         sorted=sorted;
198
199    if (Kmatrix_nomovie_message)
200     {
201                 gr_set_fontcolor( BM_XRGB(63,0,0),-1 );
202                 gr_printf( CENTERSCREEN-LHX(40), LHY(20), "(Movie not played)");
203          }
204
205         gr_set_fontcolor( BM_XRGB(63,31,31),-1 );
206         gr_printf( CENTERSCREEN, LHY(40), "SCORE");
207
208         gr_set_fontcolor( BM_XRGB(63,31,31),-1 );
209         gr_printf( CENTERSCREEN+LHX(50), LHY(40), "DEATHS");
210 }
211
212
213 void kmatrix_draw_deaths(int *sorted)
214 {
215         int y,x;
216         char reactor_message[50];
217
218    sorted=sorted;
219
220    y = LHY(55 + 72 + 35);
221         x = LHX(35);
222
223         {
224           
225                 int sw, sh, aw;
226                                         
227       gr_set_fontcolor(gr_find_closest_color(63,20,0),-1);
228       gr_get_string_size("P-Playing E-Escaped D-Died", &sw, &sh, &aw); 
229
230            if (!((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL)))
231                    gr_printf( CENTERSCREEN-(sw/2), y,"P-Playing E-Escaped D-Died");
232    
233       y+=(sh+5);
234            gr_get_string_size("V-Viewing scores W-Waiting", &sw, &sh, &aw); 
235
236            if (!((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL)))
237                    gr_printf( CENTERSCREEN-(sw/2), y,"V-Viewing scores W-Waiting");
238
239         }
240
241    y+=LHY(20);
242
243         {
244                 int sw, sh, aw;
245       gr_set_fontcolor(gr_find_closest_color(63,63,63),-1);
246
247       if (Players[Player_num].connected==7)
248        {
249         gr_get_string_size("Waiting for other players...",&sw, &sh, &aw); 
250         gr_printf( CENTERSCREEN-(sw/2), y,"Waiting for other players...");
251        }
252       else
253        {
254         gr_get_string_size(TXT_PRESS_ANY_KEY2, &sw, &sh, &aw); 
255         gr_printf( CENTERSCREEN-(sw/2), y, TXT_PRESS_ANY_KEY2);
256        }
257         }
258
259   if (Countdown_seconds_left <=0)
260    kmatrix_reactor(TXT_REACTOR_EXPLODED);
261   else
262    {
263      sprintf((char *)&reactor_message, "%s: %d %s  ", TXT_TIME_REMAINING, Countdown_seconds_left, TXT_SECONDS);
264      kmatrix_reactor ((char *)&reactor_message);
265    }
266   
267   if (Game_mode & GM_HOARD)  
268           kmatrix_phallic();
269
270 }
271 void kmatrix_draw_coop_deaths(int *sorted)
272 {
273         int j, x, y;
274         char reactor_message[50];
275         
276         y = LHY(55 + N_players * 9);
277
278 //      gr_set_fontcolor(gr_getcolor(player_rgb[j].r,player_rgb[j].g,player_rgb[j].b),-1 );
279         gr_set_fontcolor( BM_XRGB(31,31,31),-1 );
280
281         x = CENTERSCREEN+LHX(50);
282         gr_printf( x, y, TXT_DEATHS );
283
284         for (j=0; j<N_players; j++) {
285                 x = CENTERSCREEN+LHX(50);
286                 gr_printf( x, y, "%d", Players[sorted[j]].net_killed_total );
287         }
288
289    y = LHY(55 + 72 + 35);
290         x = LHX(35);
291
292         {
293           
294                 int sw, sh, aw;
295
296       gr_set_fontcolor(gr_find_closest_color(63,20,0),-1);
297       gr_get_string_size("P-Playing E-Escaped D-Died", &sw, &sh, &aw); 
298
299           if (!((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL)))
300               gr_printf( CENTERSCREEN-(sw/2), y,"P-Playing E-Escaped D-Died");
301            
302    
303       y+=(sh+5);
304            gr_get_string_size("V-Viewing scores W-Waiting", &sw, &sh, &aw); 
305
306            if (!((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL)))
307                gr_printf( CENTERSCREEN-(sw/2), y,"V-Viewing scores W-Waiting");
308
309         }
310
311    y+=LHY(20);
312
313         {
314                 int sw, sh, aw;
315       gr_set_fontcolor(gr_find_closest_color(63,63,63),-1);
316
317       if (Players[Player_num].connected==7)
318        {
319         gr_get_string_size("Waiting for other players...",&sw, &sh, &aw); 
320         gr_printf( CENTERSCREEN-(sw/2), y,"Waiting for other players...");
321        }
322       else
323        {
324         gr_get_string_size(TXT_PRESS_ANY_KEY2, &sw, &sh, &aw); 
325         gr_printf( CENTERSCREEN-(sw/2), y, TXT_PRESS_ANY_KEY2);
326        }
327         }
328
329   if (Countdown_seconds_left <=0)
330    kmatrix_reactor(TXT_REACTOR_EXPLODED);
331   else
332    {
333      sprintf((char *)&reactor_message, "%s: %d %s  ", TXT_TIME_REMAINING, Countdown_seconds_left, TXT_SECONDS);
334      kmatrix_reactor ((char *)&reactor_message);
335    }
336
337 }
338
339 void kmatrix_reactor (char *message)
340  {
341   static char oldmessage[50]={0};
342   int sw, sh, aw;
343
344   if ((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL))
345         return;
346
347   grd_curcanv->cv_font = SMALL_FONT;
348
349   if (oldmessage[0]!=0)
350    {
351     gr_set_fontcolor(gr_find_closest_color(0,0,0),-1);
352     gr_get_string_size(oldmessage, &sw, &sh, &aw); 
353    // gr_printf( CENTERSCREEN-(sw/2), LHY(55+72+12), oldmessage);
354    }
355   gr_set_fontcolor(gr_find_closest_color(0,32,63),-1);
356   gr_get_string_size(message, &sw, &sh, &aw); 
357   gr_printf( CENTERSCREEN-(sw/2), LHY(55+72+12), message);
358
359   strcpy ((char *)&oldmessage,message);
360  }
361
362 extern int PhallicLimit,PhallicMan;
363
364 void kmatrix_phallic ()
365  {
366   int sw, sh, aw;
367   char message[80];
368
369   if (!(Game_mode & GM_HOARD))
370         return;
371
372   if ((Game_mode & GM_MODEM) || (Game_mode & GM_SERIAL))
373         return;
374   
375   if (PhallicMan==-1)
376         strcpy (message,"There was no record set for this level.");
377   else
378         sprintf (message,"%s had the best record at %d points.",Players[PhallicMan].callsign,PhallicLimit);
379
380   grd_curcanv->cv_font = SMALL_FONT;
381   gr_set_fontcolor(gr_find_closest_color(63,63,63),-1);
382   gr_get_string_size(message, &sw, &sh, &aw); 
383   gr_printf( CENTERSCREEN-(sw/2), LHY(55+72+3), message);
384  }
385
386  
387 void load_stars(void);
388
389 WINDOS(dd_grs_canvas *StarBackCanvas,
390                         grs_canvas *StarBackCanvas
391 );              //,*SaveCanvas;
392
393
394 void kmatrix_redraw()
395 {
396         int i, color;
397         int sorted[MAX_NUM_NET_PLAYERS];
398 WINDOS(
399         dd_grs_canvas *tempcanvas,
400    grs_canvas *tempcanvas
401 );
402    
403    if (Game_mode & GM_MULTI_COOP)
404          {
405           kmatrix_redraw_coop();
406           return;
407          }
408          
409         multi_sort_kill_list();
410
411         WINDOS(
412            tempcanvas=dd_gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h ),
413         tempcanvas=gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h )
414         );
415
416    WINDOS(
417                 dd_gr_set_current_canvas(tempcanvas),
418                 gr_set_current_canvas (tempcanvas)
419         );
420
421         WINDOS (
422                 dd_gr_blt_notrans(StarBackCanvas, 0,0,0,0, tempcanvas,0,0,0,0),
423                 gr_bitmap (0,0,&StarBackCanvas->cv_bitmap)
424         );
425
426 WIN(DDGRLOCK(dd_grd_curcanv));
427         grd_curcanv->cv_font = MEDIUM3_FONT;
428
429         gr_string( 0x8000, LHY(10), TXT_KILL_MATRIX_TITLE       );
430
431         grd_curcanv->cv_font = SMALL_FONT;
432
433         multi_get_kill_list(sorted);
434
435         kmatrix_draw_names(sorted);
436
437         for (i=0; i<N_players; i++ )            {
438 //              mprintf((0, "Sorted kill list pos %d = %d.\n", i+1, sorted[i]));
439
440                 if (Game_mode & GM_TEAM)
441                         color = get_team(sorted[i]);
442                 else
443                         color = sorted[i];
444
445      if (Players[sorted[i]].connected==0)
446        gr_set_fontcolor(gr_find_closest_color(31,31,31),-1);
447      else
448        gr_set_fontcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b),-1 );
449
450      kmatrix_draw_item( i, sorted );
451         }
452
453         kmatrix_draw_deaths(sorted);
454 WIN(DDGRUNLOCK(dd_grd_curcanv));
455
456 WINDOS(
457         dd_gr_set_current_canvas(NULL),
458         gr_set_current_canvas(NULL)
459 );
460
461 #if defined(POLY_ACC)
462     pa_save_clut();
463     pa_update_clut(gr_palette, 0, 256, 0);
464 #endif
465
466 PA_DFX (pa_set_frontbuffer_current());
467
468 WINDOS(
469         dd_gr_blt_notrans(tempcanvas, 0,0,0,0, dd_grd_curcanv, 0,0,0,0),
470         gr_bitmap (0,0,&tempcanvas->cv_bitmap)
471 );
472
473 PA_DFX (pa_set_backbuffer_current());
474
475 #if defined(POLY_ACC)
476     pa_restore_clut();
477 #endif
478
479         gr_palette_load(gr_palette);
480 WINDOS(
481         dd_gr_free_canvas(tempcanvas),
482         gr_free_canvas (tempcanvas)
483 );
484 }
485
486 void kmatrix_redraw_coop()
487 {
488         int i, color;
489         int sorted[MAX_NUM_NET_PLAYERS];
490
491 WINDOS(
492         dd_grs_canvas *tempcanvas,
493         grs_canvas *tempcanvas
494 );
495         
496         multi_sort_kill_list();
497
498 WINDOS(
499    tempcanvas=dd_gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h ),
500    tempcanvas=gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h )
501 );
502 WINDOS(
503         dd_gr_set_current_canvas(tempcanvas),
504    gr_set_current_canvas (tempcanvas)
505 );
506
507 WINDOS (
508         dd_gr_blt_notrans(StarBackCanvas, 0,0,0,0, tempcanvas,0,0,0,0),
509         gr_bitmap (0,0,&StarBackCanvas->cv_bitmap)
510 );
511
512
513
514 WIN(DDGRLOCK(dd_grd_curcanv));
515         grd_curcanv->cv_font = MEDIUM3_FONT;
516         gr_string( 0x8000, LHY(10), "COOPERATIVE SUMMARY"       );
517
518         grd_curcanv->cv_font = SMALL_FONT;
519
520         multi_get_kill_list(sorted);
521
522         kmatrix_draw_coop_names(sorted);
523
524         for (i=0; i<N_players; i++ )            {
525
526      color = sorted[i];
527
528      if (Players[sorted[i]].connected==0)
529        gr_set_fontcolor(gr_find_closest_color(31,31,31),-1);
530      else
531        gr_set_fontcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b),-1 );
532
533      kmatrix_draw_coop_item( i, sorted );
534         }
535
536         kmatrix_draw_deaths(sorted);
537 WIN(DDGRUNLOCK(dd_grd_curcanv));
538
539 WINDOS(
540         dd_gr_set_current_canvas(NULL),
541         gr_set_current_canvas(NULL)
542 );
543
544 #if defined(POLY_ACC)
545     pa_save_clut();
546     pa_update_clut(gr_palette, 0, 256, 0);
547 #endif
548
549 PA_DFX (pa_set_frontbuffer_current());
550
551 WINDOS(
552         dd_gr_blt_notrans(tempcanvas, 0,0,0,0, dd_grd_curcanv, 0,0,0,0),
553         gr_bitmap (0,0,&tempcanvas->cv_bitmap)
554 );
555
556 PA_DFX (pa_set_backbuffer_current());
557
558 #if defined(POLY_ACC)
559     pa_restore_clut();
560 #endif
561
562         gr_palette_load(gr_palette);
563 WINDOS(
564         dd_gr_free_canvas(tempcanvas),
565         gr_free_canvas (tempcanvas)
566 );
567 }
568
569 #define MAX_VIEW_TIME   F1_0*15
570 #define ENDLEVEL_IDLE_TIME      F1_0*10
571
572 fix StartAbortMenuTime;
573
574
575 #ifdef MACINTOSH
576 extern void load_stars_palette();
577 #endif
578
579 extern void network_endlevel_poll3( int nitems, struct newmenu_item * menus, int * key, int citem );
580
581 void kmatrix_view(int network)
582 {                                                                                         
583    int i, k, done,choice;
584         fix entry_time = timer_get_approx_seconds();
585         int key;
586    int oldstates[MAX_PLAYERS];
587    int previous_seconds_left=-1;
588    int num_ready,num_escaped;
589
590         network=Game_mode & GM_NETWORK;
591  
592    for (i=0;i<MAX_NUM_NET_PLAYERS;i++)
593         digi_kill_sound_linked_to_object (Players[i].objnum);
594         
595    set_screen_mode( SCREEN_MENU );
596
597         WINDOS(
598                 StarBackCanvas=dd_gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h ),
599                 StarBackCanvas=gr_create_canvas( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h )
600         );
601         WINDOS(
602                 dd_gr_set_current_canvas(StarBackCanvas),
603            gr_set_current_canvas(StarBackCanvas)
604         );
605         #ifdef MACINTOSH
606         if (virtual_memory_on) {
607                 load_stars_palette();           // horrible hack to prevent too much paging when doing endlevel syncing
608                 gr_clear_canvas( BM_XRGB(0, 0, 0) );
609         } else
610         #endif                                                  // note link to above if/else pair
611                 load_stars();
612    
613    WaitingForOthers=0;
614    kmatrix_redraw();
615
616         //@@gr_palette_fade_in( gr_palette,32, 0);
617         game_flush_inputs();
618
619         done = 0;
620
621    for (i=0;i<N_players;i++)
622     oldstates[i]=Players[i].connected;
623
624         if (network)
625            network_endlevel(&key);
626
627         while(!done)    {
628         #ifdef WINDOWS
629                 MSG msg;
630
631                 DoMessageStuff(&msg);
632
633                 DDGRRESTORE;
634
635         #endif
636
637       kmatrix_kills_changed = 0;
638       for (i=0; i<4; i++ ) 
639           if (joy_get_button_down_cnt(i)>0) 
640                           {  
641                                   #if defined (D2_OEM)
642                                          if (Current_level_num==8)
643                                                 {
644                         Players[Player_num].connected=0;
645                                                  if (network)
646                                 network_send_endlevel_packet();
647                                                 WINDOS(
648                                                         dd_gr_free_canvas(StarBackCanvas),
649                                                    gr_free_canvas (StarBackCanvas)
650                                                 );
651                                             multi_leave_game();
652                                        Kmatrix_nomovie_message=0;
653                                        longjmp(LeaveGame, 0); 
654                         return;
655                                                 }
656                                   #endif
657                 
658             Players[Player_num].connected=7;
659                                 if (network)
660                  network_send_endlevel_packet();
661                                 break;
662                           }                     
663         for (i=0; i<3; i++ )    
664           if (mouse_button_down_count(i)>0) 
665                                 {
666                                   #if defined (D2_OEM)
667                                          if (Current_level_num==8)
668                                                 {
669                         Players[Player_num].connected=0;
670                                                  if (network)
671                                 network_send_endlevel_packet();
672                                                 WINDOS(
673                                                         dd_gr_free_canvas(StarBackCanvas),
674                                                    gr_free_canvas (StarBackCanvas)
675                                                 );
676                                             multi_leave_game();
677                                        Kmatrix_nomovie_message=0;
678                                        longjmp(LeaveGame, 0); 
679                         return;
680                                                 }
681                                   #endif
682                                         Players[Player_num].connected=7;
683                                    if (network)
684                        network_send_endlevel_packet();
685                                         break;
686                       }                 
687
688                 //see if redbook song needs to be restarted
689                 songs_check_redbook_repeat();
690
691                 k = key_inkey();
692                 switch( k )     {
693                         case KEY_ENTER:
694                         case KEY_SPACEBAR:
695                                   if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))                
696                                         {
697                                                 done=1;
698                                                 break;
699                                         }
700                                         
701                                   #if defined (D2_OEM)
702                                          if (Current_level_num==8)
703                                                 {
704                         Players[Player_num].connected=0;
705                                                  if (network)
706                                 network_send_endlevel_packet();
707                                                 WINDOS(
708                                                         dd_gr_free_canvas(StarBackCanvas),
709                                                    gr_free_canvas (StarBackCanvas)
710                                                 );
711                                             multi_leave_game();
712                                        Kmatrix_nomovie_message=0;
713                                        longjmp(LeaveGame, 0); 
714                         return;
715                                                 }
716                                   #endif
717                 
718               Players[Player_num].connected=7;
719                                   if (network)  
720                network_send_endlevel_packet();
721               break;
722                         case KEY_ESC:
723                                   if (Game_mode & GM_NETWORK)
724                                         {
725                                          StartAbortMenuTime=timer_get_approx_seconds();
726                                  choice=nm_messagebox1( NULL,network_endlevel_poll3, 2, TXT_YES, TXT_NO, TXT_ABORT_GAME );
727                                         }
728                                   else
729                           choice=nm_messagebox( NULL, 2, TXT_YES, TXT_NO, TXT_ABORT_GAME );
730               if (choice==0)
731                {
732                 Players[Player_num].connected=0;
733                                          if (network)
734                         network_send_endlevel_packet();
735                                                 WINDOS(
736                                                         dd_gr_free_canvas(StarBackCanvas),
737                                                    gr_free_canvas (StarBackCanvas)
738                                                 );
739                                     multi_leave_game();
740                                Kmatrix_nomovie_message=0;
741                                longjmp(LeaveGame, 0); 
742                 return;
743                }
744               else
745                kmatrix_kills_changed=1;
746            
747                                   break;
748
749                         case KEY_PRINT_SCREEN:
750                                 save_screen_shot(0);
751                                 break;
752                         case KEY_BACKSP:
753                                 Int3();
754                                 break;
755                         default:
756                                 break;
757                 }
758                 if (timer_get_approx_seconds() >= (entry_time+MAX_VIEW_TIME) && Players[Player_num].connected!=7)
759                         {
760                                   #if defined (D2_OEM)
761                                          if (Current_level_num==8)
762                                                 {
763                         Players[Player_num].connected=0;
764                                                  if (network)
765                                 network_send_endlevel_packet();
766                                                 WINDOS(
767                                                         dd_gr_free_canvas(StarBackCanvas),
768                                                    gr_free_canvas (StarBackCanvas)
769                                                 );
770                                             multi_leave_game();
771                                        Kmatrix_nomovie_message=0;
772                                        longjmp(LeaveGame, 0); 
773                         return;
774                                                 }
775                                   #endif
776                 
777                          if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))         
778                                 {
779                                         done=1;
780                                         break;
781                                 }
782           Players[Player_num].connected=7;
783                          if (network)
784                   network_send_endlevel_packet();
785                    }     
786
787                 if (network && (Game_mode & GM_NETWORK))
788                 {
789         network_endlevel_poll2(0, NULL, &key, 0);
790                        
791         for (num_escaped=0,num_ready=0,i=0;i<N_players;i++)
792           {
793             if (Players[i].connected && i!=Player_num)
794                {
795                 // Check timeout for idle players
796                 if (timer_get_approx_seconds() > LastPacketTime[i]+ENDLEVEL_IDLE_TIME)
797                    {
798                     mprintf((0, "idle timeout for player %d.\n", i));
799                                   Players[i].connected = 0;
800                     network_send_endlevel_sub(i);
801                    }                        
802                 }
803
804                if (Players[i].connected!=oldstates[i])
805                   {
806                    if (ConditionLetters[Players[i].connected]!=ConditionLetters[oldstates[i]])
807                      kmatrix_kills_changed=1;
808                    oldstates[i]=Players[i].connected;
809                                                  network_send_endlevel_packet();
810                   }
811                                         if (Players[i].connected==0 || Players[i].connected==7)
812                                           num_ready++;
813
814                if (Players[i].connected!=1)
815                    num_escaped++;
816           }
817
818          if (num_ready>=N_players)
819              done=1;
820          if (num_escaped>=N_players)
821              Countdown_seconds_left=-1;
822
823          if (previous_seconds_left != Countdown_seconds_left)
824             {
825              previous_seconds_left=Countdown_seconds_left;
826                                  kmatrix_kills_changed=1;
827                                 }
828                                                  
829          if ( kmatrix_kills_changed )    
830             {
831              kmatrix_redraw();
832              kmatrix_kills_changed=0;
833                         }
834
835                 }
836         }
837
838   Players[Player_num].connected=7;
839
840   if (network)  
841      network_send_endlevel_packet();  // make sure
842
843        
844 // Restore background and exit
845         gr_palette_fade_out( gr_palette, 32, 0 );
846
847         game_flush_inputs();
848
849         WINDOS(
850                 dd_gr_free_canvas(StarBackCanvas),
851                 gr_free_canvas (StarBackCanvas)
852         );
853
854    Kmatrix_nomovie_message=0;
855
856 }