whitespace
[btb/d2x.git] / main / automap.c
1 /* $Id: automap.c,v 1.8 2003-03-01 01:51:15 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * FIXME: put description here
18  *
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef OGL
31 #include "ogl_init.h"
32 #endif
33
34 #include "pa_enabl.h"                   //$$POLY_ACC
35 #include "error.h"
36 #include "3d.h"
37 #include "inferno.h"
38 #include "u_mem.h"
39 #include "render.h"
40 #include "object.h"
41 #include "vclip.h"
42 #include "game.h"
43 #include "mono.h"
44 #include "polyobj.h"
45 #include "sounds.h"
46 #include "player.h"
47 #include "bm.h"
48 #include "key.h"
49 #include "newmenu.h"
50 #include "menu.h"
51 #include "screens.h"
52 #include "textures.h"
53 #include "mouse.h"
54 #include "timer.h"
55 #include "segpoint.h"
56 #include "joy.h"
57 #include "iff.h"
58 #include "pcx.h"
59 #include "palette.h"
60 #include "wall.h"
61 #include "gameseq.h"
62 #include "gamefont.h"
63 #ifdef NETWORK
64 #include "network.h"
65 #endif
66 #include "kconfig.h"
67 #ifdef NETWORK
68 #include "multi.h"
69 #endif
70 #include "endlevel.h"
71 #include "text.h"
72 #include "gauges.h"
73 #include "songs.h"
74 #include "powerup.h"
75 #include "switch.h"
76 #include "automap.h"
77 #include "cntrlcen.h"
78
79 #if defined(POLY_ACC)
80 #include "poly_acc.h"
81 #endif
82
83 #ifdef OGL
84 #define AUTOMAP_DIRECT_RENDER
85 #endif
86
87 #define EF_USED                 1               // This edge is used
88 #define EF_DEFINING             2               // A structure defining edge that should always draw.
89 #define EF_FRONTIER             4               // An edge between the known and the unknown.
90 #define EF_SECRET                       8               // An edge that is part of a secret wall.
91 #define EF_GRATE                        16              // A grate... draw it all the time.
92 #define EF_NO_FADE              32              // An edge that doesn't fade with distance
93 #define EF_TOO_FAR              64              // An edge that is too far away
94
95 void modex_printf(int x,int y,char *s,grs_font *font,int color);
96
97 typedef struct Edge_info {
98         short verts[2];                         // 4 bytes
99         ubyte sides[4];                         // 4 bytes
100         short segnum[4];                        // 8 bytes      // This might not need to be stored... If you can access the normals of a side.
101         ubyte flags;                            // 1 bytes      // See the EF_??? defines above.
102         ubyte color;                            // 1 bytes
103         ubyte num_faces;                        // 1 bytes      // 19 bytes...
104 } Edge_info;
105
106 //OLD BUT GOOD -- #define MAX_EDGES_FROM_VERTS(v)   ((v*5)/2)
107 // THE following was determined by John by loading levels 1-14 and recording
108 // numbers on 10/26/94. 
109 //#define MAX_EDGES_FROM_VERTS(v)   (((v)*21)/10)
110 #define MAX_EDGES_FROM_VERTS(v)         ((v)*4)
111 //#define MAX_EDGES (MAX_EDGES_FROM_VERTS(MAX_VERTICES))
112
113 #define MAX_EDGES 6000          // Determined by loading all the levels by John & Mike, Feb 9, 1995
114
115 #define K_WALL_NORMAL_COLOR                     BM_XRGB( 29, 29, 29 )
116 #define K_WALL_DOOR_COLOR                               BM_XRGB( 5, 27, 5 )
117 #define K_WALL_DOOR_BLUE                                BM_XRGB( 0, 0, 31)
118 #define K_WALL_DOOR_GOLD                                BM_XRGB( 31, 31, 0)
119 #define K_WALL_DOOR_RED                         BM_XRGB( 31, 0, 0)
120 #define K_WALL_REVEALED_COLOR           BM_XRGB( 0, 0, 25 )     //what you see when you have the full map powerup
121 #define K_HOSTAGE_COLOR                         BM_XRGB( 0, 31, 0 )
122 #define K_FONT_COLOR_20                         BM_XRGB( 20, 20, 20 )
123 #define K_GREEN_31                                              BM_XRGB(0, 31, 0)
124
125 int     Wall_normal_color;
126 int     Wall_door_color;
127 int     Wall_door_blue;
128 int     Wall_door_gold;
129 int     Wall_door_red;
130 int     Wall_revealed_color;
131 int     Hostage_color;
132 int     Font_color_20;
133 int     Green_31;
134 int     White_63;
135 int     Blue_48;
136 int     Red_48;
137
138 void init_automap_colors(void)
139 {
140         Wall_normal_color = K_WALL_NORMAL_COLOR;
141         Wall_door_color = K_WALL_DOOR_COLOR;
142         Wall_door_blue = K_WALL_DOOR_BLUE;
143         Wall_door_gold = K_WALL_DOOR_GOLD;
144         Wall_door_red = K_WALL_DOOR_RED;
145         Wall_revealed_color = K_WALL_REVEALED_COLOR;
146         Hostage_color = K_HOSTAGE_COLOR;
147         Font_color_20 = K_FONT_COLOR_20;
148         Green_31 = K_GREEN_31;
149
150         White_63 = gr_find_closest_color_current(63,63,63);
151         Blue_48 = gr_find_closest_color_current(0,0,48);
152         Red_48 = gr_find_closest_color_current(48,0,0);
153 }
154
155 // Segment visited list
156 ubyte Automap_visited[MAX_SEGMENTS];
157
158 // Edge list variables
159 static int Num_edges=0;
160 static int Max_edges;           //set each frame
161 static int Highest_edge_index = -1;
162 static Edge_info Edges[MAX_EDGES];
163 static short DrawingListBright[MAX_EDGES];
164
165 //static short DrawingListBright[MAX_EDGES];
166 //static short Edge_used_list[MAX_EDGES];                               //which entries in edge_list have been used
167
168 // Map movement defines
169 #define PITCH_DEFAULT 9000
170 #define ZOOM_DEFAULT i2f(20*10)
171 #define ZOOM_MIN_VALUE i2f(20*5)
172 #define ZOOM_MAX_VALUE i2f(20*100)
173
174 #define SLIDE_SPEED                             (350)
175 #define ZOOM_SPEED_FACTOR               500     //(1500)
176 #define ROT_SPEED_DIVISOR               (115000)
177
178 #ifndef AUTOMAP_DIRECT_RENDER
179 // Screen anvas variables
180 static int current_page=0;
181 #ifdef WINDOWS
182 static dd_grs_canvas ddPages[2];
183 static dd_grs_canvas ddDrawingPages[2];
184
185 #define ddPage ddPages[0]
186 #define ddDrawingPage ddDrawingPages[0]
187
188 #endif
189
190 #if defined(MACINTOSH) && defined(POLY_ACC)
191         grs_canvas Pages[2];                    // non static under rave so the backbuffer callback function can get at them
192         grs_canvas DrawingPages[2];             // non static under rave so the backbuffer callback function can get at them
193 #else
194         static grs_canvas Pages[2];
195         static grs_canvas DrawingPages[2];
196 #endif
197 #endif /* AUTOMAP_DIRECT_RENDER */
198
199 #define Page Pages[0]
200 #define DrawingPage DrawingPages[0]
201
202 // Flags
203 static int Automap_cheat = 0;           // If set, show everything
204
205 // Rendering variables
206 static fix Automap_zoom = 0x9000;
207 static vms_vector view_target;
208 static fix Automap_farthest_dist = (F1_0 * 20 * 50);            // 50 segments away
209 static vms_matrix       ViewMatrix;
210 static fix ViewDist=0;
211
212 //      Function Prototypes
213 void adjust_segment_limit(int SegmentLimit);
214 void draw_all_edges(void);
215 void automap_build_edge_list(void);
216
217 #define MAX_DROP_MULTI          2
218 #define MAX_DROP_SINGLE 9
219
220 vms_vector MarkerPoint[NUM_MARKERS];            //these are only used in multi.c, and I'd get rid of them there, but when I tried to do that once, I caused some horrible bug. -MT
221 int HighlightMarker=-1;
222 char MarkerMessage[NUM_MARKERS][MARKER_MESSAGE_LEN];
223 char MarkerOwner[NUM_MARKERS][CALLSIGN_LEN+1];
224 float MarkerScale=2.0;
225 int     MarkerObject[NUM_MARKERS];
226
227 extern vms_vector Matrix_scale;         //how the matrix is currently scaled
228
229 // -------------------------------------------------------------
230
231 void DrawMarkerNumber (int num)
232  {
233   int i;
234   g3s_point BasePoint,FromPoint,ToPoint;
235
236   float ArrayX[10][20]={ {-.25, 0.0, 0.0, 0.0, -1.0, 1.0},
237                          {-1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0},
238                          {-1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 1.0},
239                          {-1.0, -1.0, -1.0, 1.0, 1.0, 1.0},
240                          {-1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0},
241                          {-1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0},
242                          {-1.0, 1.0, 1.0, 1.0},
243                          {-1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0},
244                          {-1.0, 1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0}
245
246                        };
247   float ArrayY[10][20]={ {.75, 1.0, 1.0, -1.0, -1.0, -1.0},
248                          {1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, -1.0, -1.0},
249                          {1.0, 1.0, 1.0, -1.0, -1.0, -1.0, 0.0, 0.0},
250                          {1.0, 0.0, 0.0, 0.0, 1.0, -1.0},
251                          {1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, -1.0, -1.0},
252                          {1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0},
253                          {1.0, 1.0, 1.0, -1.0},
254                          {1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 0.0, 0.0},
255                          {1.0, 1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 1.0}
256                        };
257   int NumOfPoints[]={6,10,8,6,10,10,4,10,8};
258
259   for (i=0;i<NumOfPoints[num];i++)
260    {
261     ArrayX[num][i]*=MarkerScale;
262     ArrayY[num][i]*=MarkerScale;
263    }
264
265   if (num==HighlightMarker)
266    gr_setcolor (White_63);
267   else
268    gr_setcolor (Blue_48);
269
270
271   g3_rotate_point(&BasePoint,&Objects[MarkerObject[(Player_num*2)+num]].pos);
272
273   for (i=0;i<NumOfPoints[num];i+=2)
274    {
275
276     FromPoint=BasePoint;
277     ToPoint=BasePoint;
278   
279     FromPoint.p3_x+=fixmul ((fl2f (ArrayX[num][i])),Matrix_scale.x);
280     FromPoint.p3_y+=fixmul ((fl2f (ArrayY[num][i])),Matrix_scale.y);
281     g3_code_point (&FromPoint);
282     g3_project_point (&FromPoint);
283
284     ToPoint.p3_x+=fixmul ((fl2f (ArrayX[num][i+1])),Matrix_scale.x);
285     ToPoint.p3_y+=fixmul ((fl2f (ArrayY[num][i+1])),Matrix_scale.y);
286     g3_code_point (&ToPoint);
287     g3_project_point (&ToPoint);
288     
289     #if defined(MACINTOSH) && defined(POLY_ACC)
290         {
291                 int savePAEnabledState = PAEnabled;     // icky hack.  automap draw context is no longer valid when this is called.
292                                                                                         // so we can not use the pa_draw_line function for rave
293                 
294                 PAEnabled = 0;
295             g3_draw_line( &FromPoint, &ToPoint );
296                 PAEnabled = savePAEnabledState;
297         }
298         #else
299             g3_draw_line( &FromPoint, &ToPoint );
300         #endif
301
302    }
303  }
304
305 void DropMarker (int player_marker_num)
306 {
307         int marker_num = (Player_num*2)+player_marker_num;
308         object *playerp = &Objects[Players[Player_num].objnum];
309
310         MarkerPoint[marker_num] = playerp->pos;
311
312         if (MarkerObject[marker_num] != -1)
313                 obj_delete(MarkerObject[marker_num]);
314
315         MarkerObject[marker_num] = drop_marker_object(&playerp->pos,playerp->segnum,&playerp->orient,marker_num);
316
317 #ifdef NETWORK
318         if (Game_mode & GM_MULTI)
319                 multi_send_drop_marker (Player_num,playerp->pos,player_marker_num,MarkerMessage[marker_num]);
320 #endif
321
322 }
323
324 extern char guidebot_name[];
325
326 void DropBuddyMarker(object *objp)
327 {
328         int     marker_num;
329
330         //      Find spare marker slot.  "if" code below should be an assert, but what if someone changes NUM_MARKERS or MAX_CROP_SINGLE and it never gets hit?
331         marker_num = MAX_DROP_SINGLE+1;
332         if (marker_num > NUM_MARKERS-1)
333                 marker_num = NUM_MARKERS-1;
334
335    sprintf(MarkerMessage[marker_num], "RIP: %s",guidebot_name);
336
337         MarkerPoint[marker_num] = objp->pos;
338
339         if (MarkerObject[marker_num] != -1 && MarkerObject[marker_num] !=0)
340                 obj_delete(MarkerObject[marker_num]);
341
342         MarkerObject[marker_num] = drop_marker_object(&objp->pos, objp->segnum, &objp->orient, marker_num);
343
344 }
345
346 #define MARKER_SPHERE_SIZE 0x58000
347
348 void DrawMarkers ()
349  {
350         int i,maxdrop;
351         static int cyc=10,cycdir=1;
352         g3s_point sphere_point;
353
354         if (Game_mode & GM_MULTI)
355         maxdrop=2;
356         else
357         maxdrop=9;
358   
359         for (i=0;i<maxdrop;i++)
360                 if (MarkerObject[(Player_num*2)+i] != -1) {
361
362                         g3_rotate_point(&sphere_point,&Objects[MarkerObject[(Player_num*2)+i]].pos);
363
364                         gr_setcolor (gr_find_closest_color_current(cyc,0,0));
365                         g3_draw_sphere(&sphere_point,MARKER_SPHERE_SIZE);
366                         gr_setcolor (gr_find_closest_color_current(cyc+10,0,0));
367                         g3_draw_sphere(&sphere_point,MARKER_SPHERE_SIZE/2);
368                         gr_setcolor (gr_find_closest_color_current(cyc+20,0,0));
369                         g3_draw_sphere(&sphere_point,MARKER_SPHERE_SIZE/4);
370          
371                         DrawMarkerNumber (i);
372                 }
373
374         if (cycdir)
375                 cyc+=2;
376         else
377                 cyc-=2;
378
379         if (cyc>43)
380          {
381                 cyc=43;
382                 cycdir=0;
383          }
384         else if (cyc<10)
385          {
386                 cyc=10;
387                 cycdir=1;
388          }
389
390  }
391
392 void ClearMarkers()
393  {
394         int i;
395
396         for (i=0;i<NUM_MARKERS;i++) {
397                 MarkerMessage[i][0]=0;
398                 MarkerObject[i]=-1;
399         }
400  }
401
402 void automap_clear_visited()    
403 {
404         int i;
405         for (i=0; i<MAX_SEGMENTS; i++ )
406                 Automap_visited[i] = 0;
407                           ClearMarkers();
408 }
409  
410 grs_canvas *name_canv_left,*name_canv_right;
411 char name_level[128];
412
413 void draw_player( object * obj )
414 {
415         vms_vector arrow_pos, head_pos;
416         g3s_point sphere_point, arrow_point, head_point;
417
418         // Draw Console player -- shaped like a ellipse with an arrow.
419         g3_rotate_point(&sphere_point,&obj->pos);
420         g3_draw_sphere(&sphere_point,obj->size);
421
422         // Draw shaft of arrow
423         vm_vec_scale_add( &arrow_pos, &obj->pos, &obj->orient.fvec, obj->size*3 );
424         g3_rotate_point(&arrow_point,&arrow_pos);
425         #if defined(MACINTOSH) && defined(POLY_ACC)
426         {
427                 int savePAEnabledState = PAEnabled;     // icky hack.  automap draw context is no longer valid when this is called.
428                                                                                         // so we can not use the pa_draw_line function for rave
429                 
430                 PAEnabled = 0;
431             g3_draw_line( &sphere_point, &arrow_point );
432                 PAEnabled = savePAEnabledState;
433         }
434         #else
435                 g3_draw_line( &sphere_point, &arrow_point );
436         #endif
437         
438         // Draw right head of arrow
439         vm_vec_scale_add( &head_pos, &obj->pos, &obj->orient.fvec, obj->size*2 );
440         vm_vec_scale_add2( &head_pos, &obj->orient.rvec, obj->size*1 );
441         g3_rotate_point(&head_point,&head_pos);
442         #if     defined(MACINTOSH) && defined(POLY_ACC)
443         {
444                 int savePAEnabledState = PAEnabled;                     
445                 PAEnabled = 0;
446             g3_draw_line( &arrow_point, &head_point );
447                 PAEnabled = savePAEnabledState;
448         }
449         #else
450                 g3_draw_line( &arrow_point, &head_point );
451         #endif
452
453         // Draw left head of arrow
454         vm_vec_scale_add( &head_pos, &obj->pos, &obj->orient.fvec, obj->size*2 );
455         vm_vec_scale_add2( &head_pos, &obj->orient.rvec, obj->size*(-1) );
456         g3_rotate_point(&head_point,&head_pos);
457         #if     defined(MACINTOSH) && defined(POLY_ACC)
458         {
459                 int savePAEnabledState = PAEnabled;                     
460                 PAEnabled = 0;
461             g3_draw_line( &arrow_point, &head_point );
462                 PAEnabled = savePAEnabledState;
463         }
464         #else
465                 g3_draw_line( &arrow_point, &head_point );
466         #endif
467
468         // Draw player's up vector
469         vm_vec_scale_add( &arrow_pos, &obj->pos, &obj->orient.uvec, obj->size*2 );
470         g3_rotate_point(&arrow_point,&arrow_pos);
471         #if     defined(MACINTOSH) && defined(POLY_ACC)
472         {
473                 int savePAEnabledState = PAEnabled;                     
474                 PAEnabled = 0;
475             g3_draw_line( &sphere_point, &arrow_point );
476                 PAEnabled = savePAEnabledState;
477         }
478         #else
479                 g3_draw_line( &sphere_point, &arrow_point );
480         #endif
481
482
483 }
484
485 int AutomapHires;
486 #if defined(MACINTOSH) && defined(POLY_ACC)
487
488         void pa_mac_draw_automap_extras(void)
489         {
490                 int i;
491                 int color;
492                 object * objp;
493                 g3s_point sphere_point;
494                 
495                 AutomapHires = 1;       // always on the mac
496         
497                 // Draw player...
498                 #ifdef NETWORK
499                         if (Game_mode & GM_TEAM)
500                                 color = get_team(Player_num);
501                         else
502                 #endif  
503                         color = Player_num;     // Note link to above if!
504         
505                 gr_setcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b));
506                 draw_player(&Objects[Players[Player_num].objnum]);
507         
508                 DrawMarkers();
509                 
510                 if (HighlightMarker>-1 && MarkerMessage[HighlightMarker][0]!=0)
511                  {
512                         char msg[10+MARKER_MESSAGE_LEN+1];
513         
514                         sprintf(msg,"Marker %d: %s",HighlightMarker+1,MarkerMessage[(Player_num*2)+HighlightMarker]);
515         
516                         gr_setcolor (Red_48);
517                         
518                         modex_printf(5,20,msg,SMALL_FONT,Font_color_20);
519                  } 
520                                         
521                 // Draw player(s)...
522                 #ifdef NETWORK
523                         if ( (Game_mode & (GM_TEAM | GM_MULTI_COOP)) || (Netgame.game_flags & NETGAME_FLAG_SHOW_MAP) )
524                         {
525                                 for (i=0; i<N_players; i++)     
526                                 {
527                                         if ((i != Player_num) &&
528                                                 ((Game_mode & GM_MULTI_COOP) || (get_team(Player_num) == get_team(i)) || (Netgame.game_flags & NETGAME_FLAG_SHOW_MAP)) )        
529                                         {
530                                                 if ( Objects[Players[i].objnum].type == OBJ_PLAYER )
531                                                 {
532                                                         if (Game_mode & GM_TEAM)
533                                                                 color = get_team(i);
534                                                         else
535                                                                 color = i;
536                                                         gr_setcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b));
537                                                         draw_player(&Objects[Players[i].objnum]);
538                                                 }
539                                         }
540                                 }
541                         }
542                 #endif
543         
544                 objp = &Objects[0];
545                 for (i=0;i<=Highest_object_index;i++,objp++)
546                 {
547                         switch( objp->type )
548                         {
549                                 case OBJ_HOSTAGE:
550                                         gr_setcolor(Hostage_color);
551                                         g3_rotate_point(&sphere_point,&objp->pos);
552                                         g3_draw_sphere(&sphere_point,objp->size);       
553                                         break;
554                                 case OBJ_POWERUP:
555                                         if ( Automap_visited[objp->segnum] )
556                                         {
557                                                 if ( (objp->id==POW_KEY_RED) || (objp->id==POW_KEY_BLUE) || (objp->id==POW_KEY_GOLD) )
558                                                 {
559                                                         switch (objp->id)
560                                                         {
561                                                                 case POW_KEY_RED:       gr_setcolor(gr_getcolor(63, 5, 5));     break;
562                                                                 case POW_KEY_BLUE:      gr_setcolor(gr_getcolor(5, 5, 63)); break;
563                                                                 case POW_KEY_GOLD:      gr_setcolor(gr_getcolor(63, 63, 10)); break;
564                                                                 default:
565                                                                         Error("Illegal key type: %i", objp->id);
566                                                         }
567                                                         g3_rotate_point(&sphere_point,&objp->pos);
568                                                         g3_draw_sphere(&sphere_point,objp->size*4);     
569                                                 }
570                                         }
571                                         break;
572                         }
573                 }
574         
575                 gr_bitmapm(AutomapHires?10:AutomapHires?10:5,5,&name_canv_left->cv_bitmap);
576                 gr_bitmapm(grd_curcanv->cv_bitmap.bm_w-(AutomapHires?10:5)-name_canv_right->cv_bitmap.bm_w,AutomapHires?10:5,&name_canv_right->cv_bitmap);
577         }
578
579         void pa_mac_draw_automap(void)
580         {
581                 vms_vector viewer_position;
582         
583                 g3_start_frame();
584                 render_start_frame();
585                 pa_set_context(kAutoMapDrawContextID, NULL);
586                 pa_render_start();
587                 
588                         vm_vec_scale_add(&viewer_position,&view_target,&ViewMatrix.fvec,-ViewDist );
589                         g3_set_view_matrix(&viewer_position,&ViewMatrix,Automap_zoom);
590                 
591                         draw_all_edges();
592         
593                 g3_end_frame();
594                 pa_render_end();
595         }
596 #endif
597
598 void draw_automap()
599 {
600         int i;
601         int color;
602         object * objp;
603         vms_vector viewer_position;
604         g3s_point sphere_point;
605
606         #ifdef MACINTOSH
607                 #ifdef POLY_ACC
608                         if (PAEnabled)
609                         {
610                                 pa_mac_draw_automap();
611                                 return;
612                         }
613                 #endif
614         #endif
615         
616 #ifndef AUTOMAP_DIRECT_RENDER
617         if (!AutomapHires) {
618                 WIN(mprintf((1, "Can't do lores automap in Windows!\n")));
619                 WIN(Int3());
620                 current_page ^= 1;
621                 gr_set_current_canvas(&DrawingPages[current_page]);
622         }
623         else {
624                 WINDOS(
625                         dd_gr_set_current_canvas(&ddDrawingPage),
626                         gr_set_current_canvas(&DrawingPage)
627                 );
628         }
629 #endif
630
631 #if defined(POLY_ACC)
632     pa_flush();
633 #endif
634
635         WINDOS(
636                 dd_gr_clear_canvas(BM_XRGB(0,0,0)),
637                 gr_clear_canvas(BM_XRGB(0,0,0))
638         );
639
640 WIN(DDGRLOCK(dd_grd_curcanv));
641 {
642         g3_start_frame();
643         render_start_frame();
644
645         vm_vec_scale_add(&viewer_position,&view_target,&ViewMatrix.fvec,-ViewDist );
646
647         g3_set_view_matrix(&viewer_position,&ViewMatrix,Automap_zoom);
648
649 //      mprintf((0, "dd_grd_curcanv->canvas.cv_bitmap.bm_data= %x\n", dd_grd_curcanv->canvas.cv_bitmap.bm_data));
650 //      mprintf((0, "grd_curcanv->cv_bitmap.bm_data= %x\n", grd_curcanv->cv_bitmap.bm_data));
651
652         draw_all_edges();
653
654         // Draw player...
655 #ifdef NETWORK
656         if (Game_mode & GM_TEAM)
657                 color = get_team(Player_num);
658         else
659 #endif  
660                 color = Player_num;     // Note link to above if!
661
662         gr_setcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b));
663         draw_player(&Objects[Players[Player_num].objnum]);
664
665         DrawMarkers();
666         
667         if (HighlightMarker>-1 && MarkerMessage[HighlightMarker][0]!=0)
668          {
669                 char msg[10+MARKER_MESSAGE_LEN+1];
670
671                 sprintf(msg,"Marker %d: %s",HighlightMarker+1,MarkerMessage[(Player_num*2)+HighlightMarker]);
672
673                 gr_setcolor (Red_48);
674                 
675                 modex_printf(5,20,msg,SMALL_FONT,Font_color_20);
676          } 
677                                 
678         // Draw player(s)...
679 #ifdef NETWORK
680         if ( (Game_mode & (GM_TEAM | GM_MULTI_COOP)) || (Netgame.game_flags & NETGAME_FLAG_SHOW_MAP) )  {
681                 for (i=0; i<N_players; i++)             {
682                         if ( (i != Player_num) && ((Game_mode & GM_MULTI_COOP) || (get_team(Player_num) == get_team(i)) || (Netgame.game_flags & NETGAME_FLAG_SHOW_MAP)) )      {
683                                 if ( Objects[Players[i].objnum].type == OBJ_PLAYER )    {
684                                         if (Game_mode & GM_TEAM)
685                                                 color = get_team(i);
686                                         else
687                                                 color = i;
688                                         gr_setcolor(gr_getcolor(player_rgb[color].r,player_rgb[color].g,player_rgb[color].b));
689                                         draw_player(&Objects[Players[i].objnum]);
690                                 }
691                         }
692                 }
693         }
694 #endif
695
696         objp = &Objects[0];
697         for (i=0;i<=Highest_object_index;i++,objp++) {
698                 switch( objp->type )    {
699                 case OBJ_HOSTAGE:
700                         gr_setcolor(Hostage_color);
701                         g3_rotate_point(&sphere_point,&objp->pos);
702                         g3_draw_sphere(&sphere_point,objp->size);       
703                         break;
704                 case OBJ_POWERUP:
705                         if ( Automap_visited[objp->segnum] )    {
706                                 if ( (objp->id==POW_KEY_RED) || (objp->id==POW_KEY_BLUE) || (objp->id==POW_KEY_GOLD) )  {
707                                         switch (objp->id) {
708                                         case POW_KEY_RED:               gr_setcolor(gr_getcolor(63, 5, 5));     break;
709                                         case POW_KEY_BLUE:      gr_setcolor(gr_getcolor(5, 5, 63)); break;
710                                         case POW_KEY_GOLD:      gr_setcolor(gr_getcolor(63, 63, 10)); break;
711                                         default:
712                                                 Error("Illegal key type: %i", objp->id);
713                                         }
714                                         g3_rotate_point(&sphere_point,&objp->pos);
715                                         g3_draw_sphere(&sphere_point,objp->size*4);     
716                                 }
717                         }
718                         break;
719                 }
720         }
721
722         g3_end_frame();
723
724         gr_bitmapm(AutomapHires?10:AutomapHires?10:5,5,&name_canv_left->cv_bitmap);
725         gr_bitmapm(grd_curcanv->cv_bitmap.bm_w-(AutomapHires?10:5)-name_canv_right->cv_bitmap.bm_w,AutomapHires?10:5,&name_canv_right->cv_bitmap);
726         gr_set_curfont(GAME_FONT);
727         gr_set_fontcolor(BM_XRGB(0,31,0),-1);
728         gr_uprintf(5,5,name_level);
729 }
730 WIN(DDGRUNLOCK(dd_grd_curcanv));
731
732 #ifdef OGL
733         ogl_swap_buffers();
734 #else
735 #ifndef AUTOMAP_DIRECT_RENDER
736         if (!AutomapHires)
737                 gr_show_canvas( &Pages[current_page] );
738         else {
739         #ifndef WINDOWS
740                 //gr_bm_ubitblt( Page.cv_bitmap.bm_w, Page.cv_bitmap.bm_h, Page.cv_bitmap.bm_x, Page.cv_bitmap.bm_y, 0, 0, &Page.cv_bitmap, &VR_screen_pages[0].cv_bitmap );
741                 gr_bm_ubitblt( Page.cv_bitmap.bm_w, Page.cv_bitmap.bm_h, Page.cv_bitmap.bm_x, Page.cv_bitmap.bm_y, 0, 0, &Page.cv_bitmap, &grd_curscreen->sc_canvas.cv_bitmap );
742         #else
743                 dd_gr_blt_screen(&ddPage, 0,0,0,0,0,0,0,0);
744         #endif
745         }
746         gr_update();
747 #endif
748 #endif
749 }
750
751 #ifdef WINDOWS
752 #define LEAVE_TIME 0x00010000
753 #else
754 #define LEAVE_TIME 0x4000
755 #endif
756
757 #define WINDOW_WIDTH            288
758
759
760 //print to canvas & double height
761 grs_canvas *print_to_canvas(char *s,grs_font *font, int fc, int bc, int double_flag)
762 {
763         int y;
764         ubyte *data;
765         int rs;
766         grs_canvas *temp_canv;
767         grs_font *save_font;
768         int w,h,aw;
769
770 WINDOS(
771         dd_grs_canvas *save_canv,
772         grs_canvas *save_canv
773 );
774
775 WINDOS(
776         save_canv = dd_grd_curcanv,
777         save_canv = grd_curcanv
778 );
779
780         save_font = grd_curcanv->cv_font;
781         gr_set_curfont(font);                                   //set the font we're going to use
782         gr_get_string_size(s,&w,&h,&aw);                //now get the string size
783         gr_set_curfont(save_font);                              //restore real font
784
785         //temp_canv = gr_create_canvas(font->ft_w*strlen(s),font->ft_h*2);
786         temp_canv = gr_create_canvas(w,font->ft_h*2);
787
788         gr_set_current_canvas(temp_canv);
789         gr_set_curfont(font);
790         gr_clear_canvas(TRANSPARENCY_COLOR);                                            //trans color
791         gr_set_fontcolor(fc,bc);
792         gr_printf(0,0,s);
793
794         //now double it, since we're drawing to 400-line modex screen
795
796         if (double_flag) {
797                 data = temp_canv->cv_bitmap.bm_data;
798                 rs = temp_canv->cv_bitmap.bm_rowsize;
799
800                 for (y=temp_canv->cv_bitmap.bm_h/2;y--;) {
801                         memcpy(data+(rs*y*2),data+(rs*y),temp_canv->cv_bitmap.bm_w);
802                         memcpy(data+(rs*(y*2+1)),data+(rs*y),temp_canv->cv_bitmap.bm_w);
803                 }
804         }
805
806 WINDOS(
807         dd_gr_set_current_canvas(save_canv),
808         gr_set_current_canvas(save_canv)
809 );
810
811         return temp_canv;
812 }
813
814 //print to buffer, double heights, and blit bitmap to screen
815 void modex_printf(int x,int y,char *s,grs_font *font,int color)
816 {
817         grs_canvas *temp_canv;
818
819         temp_canv = print_to_canvas(s, font, color, -1, !AutomapHires);
820
821         gr_bitmapm(x,y,&temp_canv->cv_bitmap);
822
823         gr_free_canvas(temp_canv);
824 }
825
826 //name for each group.  maybe move somewhere else
827 char *system_name[] = {
828                         "Zeta Aquilae",
829                         "Quartzon System",
830                         "Brimspark System",
831                         "Limefrost Spiral",
832                         "Baloris Prime",
833                         "Omega System"};
834
835 void create_name_canv()
836 {
837         char    name_level_left[128],name_level_right[128];
838
839         if (Current_level_num > 0)
840                 sprintf(name_level_left, "%s %i",TXT_LEVEL, Current_level_num);
841         else
842                 sprintf(name_level_left, "Secret Level %i",-Current_level_num);
843
844         if (Current_mission_num == Builtin_mission_num && Current_level_num>0)          //built-in mission
845                 sprintf(name_level_right,"%s %d: ",system_name[(Current_level_num-1)/4],((Current_level_num-1)%4)+1);
846         else
847                 strcpy(name_level_right, " ");
848
849         strcat(name_level_right, Current_level_name);
850
851         gr_set_fontcolor(Green_31,-1);
852         name_canv_left = print_to_canvas(name_level_left, SMALL_FONT, Green_31, -1, !AutomapHires);
853         name_canv_right = print_to_canvas(name_level_right,SMALL_FONT, Green_31, -1, !AutomapHires);
854
855 }
856
857 void modex_print_message(int x, int y, char *str)
858 {
859         if (!AutomapHires) {
860 #ifndef AUTOMAP_DIRECT_RENDER
861                 int     i;
862
863                 for (i=0; i<2; i++ )    {
864                         gr_set_current_canvas(&Pages[i]);
865 #endif
866
867                         modex_printf(x, y, str, MEDIUM1_FONT,Font_color_20);
868 #ifndef AUTOMAP_DIRECT_RENDER
869                 }
870                 gr_set_current_canvas(&DrawingPages[current_page]);
871 #endif
872         }
873         else {
874 #ifndef AUTOMAP_DIRECT_RENDER
875                 gr_set_current_canvas(&Page);
876 #endif
877                 modex_printf(x, y, str, MEDIUM1_FONT,Font_color_20);
878 #ifndef AUTOMAP_DIRECT_RENDER
879                 gr_set_current_canvas(&DrawingPage);
880 #endif
881         }
882 }
883
884 extern void GameLoop(int, int );
885 extern int set_segment_depths(int start_seg, ubyte *segbuf);
886
887 int Automap_active = 0;
888
889 #ifdef RELEASE
890 #define MAP_BACKGROUND_FILENAME (AutomapHires?"\x01MAPB.PCX":"\x01MAP.PCX")     //load only from hog file
891 #else
892 #define MAP_BACKGROUND_FILENAME ((AutomapHires && cfexist("mapb.pcx"))?"MAPB.PCX":"MAP.PCX")
893 #endif
894
895 int Automap_always_hires=0;
896 extern int MenuHiresAvailable;
897
898 extern int Current_display_mode;
899
900 u_int32_t automap_mode = SM(640,480);
901 int automap_width = 640;
902 int automap_height = 480;
903 int automap_use_game_res=0;
904
905 void do_automap( int key_code ) {
906         int done=0;
907         vms_matrix      tempm;
908         vms_angvec      tangles;
909         int leave_mode=0;
910         int first_time=1;
911         int pcx_error;
912         int i;
913         int c, marker_num;
914         fix entry_time;
915         int pause_game=1;               // Set to 1 if everything is paused during automap...No pause during net.
916         fix t1, t2;
917         control_info saved_control_info;
918         grs_bitmap Automap_background;
919         int Max_segments_away = 0;
920         int SegmentLimit = 1;
921         ubyte pal[256*3];
922         char maxdrop;
923         int must_free_canvas=0;
924         
925         WIN(int dd_VR_screen_mode_save);
926         WIN(int redraw_screen=0);
927
928         Automap_active = 1;
929
930         init_automap_colors();
931
932         key_code = key_code;    // disable warning...
933
934         if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
935                 pause_game = 0;
936
937         if (pause_game) {
938                 stop_time();
939                 digi_pause_digi_sounds();
940         }
941
942         Max_edges = min(MAX_EDGES_FROM_VERTS(Num_vertices),MAX_EDGES);                  //make maybe smaller than max
943
944         mprintf( (0, "Num_vertices=%d, Max_edges=%d, (MAX:%d)\n", Num_vertices, Max_edges, MAX_EDGES ));
945         mprintf( (0, "Allocated %d K for automap edge list\n", (sizeof(Edge_info)+sizeof(short))*Max_edges/1024 ));
946
947         #if !defined (WINDOWS) && !defined(MACINTOSH)
948         if ((Current_display_mode!=0 && Current_display_mode!=2) || (Automap_always_hires && MenuHiresAvailable)) {
949 #if !defined(POLY_ACC)
950                 //edit 4/23/99 Matt Mueller - don't switch res unless we need to
951                 if (grd_curscreen->sc_mode != AUTOMAP_MODE)
952                         gr_set_mode( AUTOMAP_MODE );
953                 else
954                         gr_set_current_canvas(NULL);
955                 //end edit -MM
956                 automap_width=grd_curscreen->sc_canvas.cv_bitmap.bm_w;
957                 automap_height=grd_curscreen->sc_canvas.cv_bitmap.bm_h;
958 #endif
959                 PA_DFX (pa_set_frontbuffer_current());
960                 AutomapHires = 1;
961         }
962         else {
963                 gr_set_mode( SM(320, 400));
964                 AutomapHires = 0;
965         }
966         #else
967                 AutomapHires = 1;               //Mac & Windows(?) always in hires
968         #endif
969
970         #ifdef WINDOWS
971                 dd_VR_screen_mode_save = VR_screen_mode;
972                 VR_screen_mode = SM95_640x480x8;        // HACK! Forcing reinit of 640x480
973                 set_screen_mode(SCREEN_GAME);
974         #endif
975
976         FontHires = FontHiresAvailable && AutomapHires;
977
978         create_name_canv();
979
980         gr_palette_clear();
981
982 WIN(AutomapRedraw:)
983         if (!AutomapHires) {
984 #ifndef MACINTOSH
985 #ifndef AUTOMAP_DIRECT_RENDER
986                 gr_init_sub_canvas(&Pages[0],grd_curcanv,0,0,320,400);
987                 gr_init_sub_canvas(&Pages[1],grd_curcanv,0,401,320,400);
988                 gr_init_sub_canvas(&DrawingPages[0],&Pages[0],16,69,WINDOW_WIDTH,272);
989                 gr_init_sub_canvas(&DrawingPages[1],&Pages[1],16,69,WINDOW_WIDTH,272);
990 #endif
991
992                 gr_init_bitmap_data (&Automap_background);
993 //              pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&Automap_background,BM_LINEAR,pal);
994 //              if ( pcx_error != PCX_ERROR_NONE )
995 //                      Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
996 //              gr_remap_bitmap_good( &Automap_background, pal, -1, -1 );
997
998 #ifndef AUTOMAP_DIRECT_RENDER
999                 for (i=0; i<2; i++ )    {
1000                         gr_set_current_canvas(&Pages[i]);
1001 //                      gr_bitmap( 0, 0, &Automap_background );
1002 //                      modex_printf( 40, 22,TXT_AUTOMAP,HUGE_FONT,Font_color_20);
1003 //                      modex_printf( 30,353,TXT_TURN_SHIP,SMALL_FONT,Font_color_20);
1004 //                      modex_printf( 30,369,TXT_SLIDE_UPDOWN,SMALL_FONT,Font_color_20);
1005 //                      modex_printf( 30,385,TXT_VIEWING_DISTANCE,SMALL_FONT,Font_color_20);
1006                 }
1007                 gr_free_bitmap_data(&Automap_background);       
1008                 gr_set_current_canvas(&DrawingPages[current_page]);
1009 #endif /* AUTOMAP_DIRECT_RENDER */
1010 #endif /* MACINTOSH */
1011         }
1012         else {
1013 #ifndef AUTOMAP_DIRECT_RENDER
1014                 if (VR_render_buffer[0].cv_w >= automap_width && VR_render_buffer[0].cv_h >= automap_height)
1015                 {
1016                         WIN(dd_gr_init_sub_canvas(&ddPage, &dd_VR_render_buffer[0], 0, 0, automap_width,automap_height));
1017
1018                         #if defined(MACINTOSH) && defined(POLY_ACC)
1019                                 if (PAEnabled)
1020                                 {
1021                                         // we want all the automap border stuff to be drawn straight to the screen
1022                                         gr_init_sub_canvas(&Page,&(grd_curscreen->sc_canvas),0, 0, automap_width, automap_height);
1023                                 }
1024                                 else
1025                                 {
1026                                         gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
1027                                 }
1028                         #else
1029                                 gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
1030                         #endif
1031                         
1032                 }
1033                 else {
1034                 #ifndef WINDOWS
1035                         void *raw_data;
1036                         MALLOC(raw_data,ubyte,automap_width*automap_height);
1037                         gr_init_canvas(&Page,raw_data,BM_LINEAR,automap_width,automap_height);
1038                 #else
1039                         dd_gr_init_canvas(&ddPage, BM_LINEAR, automap_width,automap_height);
1040                         gr_init_canvas(&Page,NULL,BM_LINEAR,automap_width,automap_height);
1041                 #endif
1042                         must_free_canvas = 1;
1043                 }
1044
1045                 WIN(dd_gr_init_sub_canvas(&ddDrawingPage, &ddPage, 0,0,automap_width,automap_height));
1046                 gr_init_sub_canvas(&DrawingPage,&Page, 0,0,automap_width,automap_height);
1047         
1048                 WINDOS(
1049                         dd_gr_set_current_canvas(&ddPage),
1050                         gr_set_current_canvas(&Page)
1051                 );
1052 #endif
1053
1054 #if defined(POLY_ACC)
1055
1056                 #ifndef MACINTOSH
1057                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1058         #else
1059                 if ( PAEnabled )
1060                 {
1061                         pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1062                                 if ( pcx_error != PCX_ERROR_NONE )
1063                                 {
1064                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1065                                         return;
1066                                 }
1067                     }
1068                     else 
1069                     {
1070                                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR,pal);
1071                                 if ( pcx_error != PCX_ERROR_NONE )
1072                                 {
1073                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1074                                         return;
1075                                 }
1076         
1077                                 gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1078                     }
1079             #endif
1080 #else
1081
1082                 WIN(DDGRLOCK(dd_grd_curcanv));
1083                         pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR,pal);
1084                         if ( pcx_error != PCX_ERROR_NONE )      {
1085                                 //printf("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1086                                 Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1087                                 return;
1088                         }
1089
1090                         gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1091 #endif
1092         
1093                         gr_set_curfont(HUGE_FONT);
1094                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1095                         gr_printf( 80, 36,TXT_AUTOMAP,HUGE_FONT);
1096                         gr_set_curfont(SMALL_FONT);
1097                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1098                         gr_printf( 60, 426,TXT_TURN_SHIP);
1099                         gr_printf( 60, 443,TXT_SLIDE_UPDOWN);
1100                         gr_printf( 60, 460,TXT_VIEWING_DISTANCE);
1101                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1102         
1103 #ifndef AUTOMAP_DIRECT_RENDER
1104                 WINDOS(
1105                         dd_gr_set_current_canvas(&ddDrawingPage),
1106                         gr_set_current_canvas(&DrawingPage)
1107                 );
1108 #endif
1109         }
1110
1111
1112 WIN(if (!redraw_screen) {)
1113         automap_build_edge_list();
1114
1115         if ( ViewDist==0 ) 
1116                 ViewDist = ZOOM_DEFAULT;
1117         ViewMatrix = Objects[Players[Player_num].objnum].orient;
1118
1119         tangles.p = PITCH_DEFAULT;
1120         tangles.h  = 0;
1121         tangles.b  = 0;
1122
1123         done = 0;
1124
1125         view_target = Objects[Players[Player_num].objnum].pos;
1126
1127         t1 = entry_time = timer_get_fixed_seconds();
1128         t2 = t1;
1129
1130         //Fill in Automap_visited from Objects[Players[Player_num].objnum].segnum
1131         Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1132         SegmentLimit = Max_segments_away;
1133
1134         adjust_segment_limit(SegmentLimit);
1135 WIN(})
1136
1137 WIN(if (redraw_screen) redraw_screen = 0);
1138
1139         while(!done)    {
1140                 if ( leave_mode==0 && Controls.automap_state && (timer_get_fixed_seconds()-entry_time)>LEAVE_TIME)
1141                         leave_mode = 1;
1142
1143                 if ( !Controls.automap_state && (leave_mode==1) )
1144                         done=1;
1145
1146                 if (!pause_game)        {
1147                         ushort old_wiggle;
1148                         saved_control_info = Controls;                          // Save controls so we can zero them
1149                         memset(&Controls,0,sizeof(control_info));       // Clear everything...
1150                         old_wiggle = ConsoleObject->mtype.phys_info.flags & PF_WIGGLE;  // Save old wiggle
1151                         ConsoleObject->mtype.phys_info.flags &= ~PF_WIGGLE;             // Turn off wiggle
1152                         #ifdef NETWORK
1153                         if (multi_menu_poll())
1154                                 done = 1;
1155                         #endif
1156 //                      GameLoop( 0, 0 );               // Do game loop with no rendering and no reading controls.
1157                         ConsoleObject->mtype.phys_info.flags |= old_wiggle;     // Restore wiggle
1158                         Controls = saved_control_info;
1159                 } 
1160
1161         #ifndef WINDOWS
1162                 controls_read_all();            
1163         #else
1164                 controls_read_all_win();
1165         #endif
1166
1167                 if ( Controls.automap_down_count )      {
1168                         if (leave_mode==0)
1169                                 done = 1;
1170                         c = 0;
1171                 }
1172
1173                 //see if redbook song needs to be restarted
1174                 songs_check_redbook_repeat();
1175
1176                 #ifdef WINDOWS
1177                 {
1178                         MSG msg;
1179                         DoMessageStuff(&msg);
1180                         if (_RedrawScreen) {
1181                                 _RedrawScreen = FALSE;
1182                                 redraw_screen = 1;
1183                                 goto AutomapRedraw;
1184                         }
1185                                 
1186                         if (msg.message == WM_QUIT) exit(1);
1187
1188                         DDGRRESTORE;
1189                 }
1190                 #endif
1191
1192
1193                 while( (c=key_inkey()) )        {
1194                         switch( c ) {
1195                         #ifndef NDEBUG
1196                         case KEY_BACKSP: Int3(); break;
1197                         #endif
1198         
1199                         case KEY_PRINT_SCREEN: {
1200                                 if (AutomapHires) {
1201                                 WINDOS(
1202                                         dd_gr_set_current_canvas(NULL),
1203                                                 gr_set_current_canvas(NULL)
1204                                 );
1205                                 }
1206 #ifndef AUTOMAP_DIRECT_RENDER
1207                                 else
1208                                         gr_set_current_canvas(&Pages[current_page]);
1209 #endif
1210                                 save_screen_shot(1); 
1211                                 break;
1212                         }
1213         
1214                         case KEY_ESC:
1215                                 if (leave_mode==0)
1216                                         done = 1;
1217                                  break;
1218
1219                         #ifndef NDEBUG
1220                         case KEY_DEBUGGED+KEY_F:        {
1221                                 for (i=0; i<=Highest_segment_index; i++ )
1222                                         Automap_visited[i] = 1;
1223                                 automap_build_edge_list();
1224                                 Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1225                                 SegmentLimit = Max_segments_away;
1226                                 adjust_segment_limit(SegmentLimit);
1227                                 }
1228                                 break;
1229                         #endif
1230
1231                         case KEY_MINUS:
1232                                 if (SegmentLimit > 1)           {
1233                                         SegmentLimit--;
1234                                         adjust_segment_limit(SegmentLimit);
1235                                 }
1236                                 break;
1237                         case KEY_EQUAL:
1238                                 if (SegmentLimit < Max_segments_away)   {
1239                                         SegmentLimit++;
1240                                         adjust_segment_limit(SegmentLimit);
1241                                 }
1242                                 break;
1243                         case KEY_1:
1244                         case KEY_2:
1245                         case KEY_3:
1246                         case KEY_4:
1247                         case KEY_5:
1248                         case KEY_6:
1249                         case KEY_7:
1250                         case KEY_8:
1251                         case KEY_9:
1252                         case KEY_0:
1253                                 if (Game_mode & GM_MULTI)
1254                                 maxdrop=2;
1255                                 else
1256                                 maxdrop=9;
1257
1258                         #ifndef MACINTOSH
1259                         marker_num = c-KEY_1;
1260                         #else
1261                         switch(c) {             // god this is stupid.....
1262                                 case KEY_1: marker_num = 0; break;
1263                                 case KEY_2: marker_num = 1; break;
1264                                 case KEY_3: marker_num = 2; break;
1265                                 case KEY_4: marker_num = 3; break;
1266                                 case KEY_5: marker_num = 4; break;
1267                                 case KEY_6: marker_num = 5; break;
1268                                 case KEY_7: marker_num = 6; break;
1269                                 case KEY_8: marker_num = 7; break;
1270                                 case KEY_9: marker_num = 8; break;
1271                                 case KEY_0: marker_num = 9; break;
1272                         }
1273                         #endif
1274             if (marker_num<=maxdrop)
1275                                  {
1276                                         if (MarkerObject[marker_num] != -1)
1277                                                 HighlightMarker=marker_num;
1278                                  }
1279                           break;
1280
1281                         case KEY_D+KEY_CTRLED:
1282 #ifndef AUTOMAP_DIRECT_RENDER
1283                                 if (current_page)               //menu will only work on page 0
1284                                         draw_automap(); //..so switch from 1 to 0
1285 #endif
1286
1287                                 if (HighlightMarker > -1 && MarkerObject[HighlightMarker] != -1) {
1288 #ifndef AUTOMAP_DIRECT_RENDER
1289                                         WINDOS(
1290                                                 dd_gr_set_current_canvas(&ddPages[current_page]),
1291                                                 gr_set_current_canvas(&Pages[current_page])
1292                                         );
1293 #endif
1294
1295                                         if (nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "Delete Marker?" ) == 0) {
1296                                                 obj_delete(MarkerObject[HighlightMarker]);
1297                                                 MarkerObject[HighlightMarker]=-1;
1298                                                 MarkerMessage[HighlightMarker][0]=0;
1299                                                 HighlightMarker = -1;
1300                                         }                                       
1301                                 }
1302                                 break;
1303
1304                         #ifndef RELEASE
1305                         case KEY_COMMA:
1306                                 if (MarkerScale>.5)
1307                                         MarkerScale-=.5;
1308                                 break;
1309                         case KEY_PERIOD:
1310                                 if (MarkerScale<30.0)
1311                                         MarkerScale+=.5;
1312                                 break;
1313                         #endif
1314
1315 //added 8/23/99 by Matt Mueller for hot key res/fullscreen changing, and menu access
1316 #if 0
1317                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADDIVIDE:
1318                         case KEY_ALTED+KEY_CTRLED+KEY_PADDIVIDE:
1319                         case KEY_ALTED+KEY_SHIFTED+KEY_PADDIVIDE:
1320                                 d1x_options_menu();
1321                                 break;
1322                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMULTIPLY:
1323                         case KEY_ALTED+KEY_CTRLED+KEY_PADMULTIPLY:
1324                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMULTIPLY:
1325                                 change_res();
1326                                 break;
1327                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMINUS:
1328                         case KEY_ALTED+KEY_CTRLED+KEY_PADMINUS:
1329                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMINUS:
1330                                 //lower res 
1331                                 //should we just cycle through the list that is displayed in the res change menu?
1332                                 // what if their card/X/etc can't handle that mode? hrm. 
1333                                 //well, the quick access to the menu is good enough for now.
1334                                 break;
1335                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADPLUS:
1336                         case KEY_ALTED+KEY_CTRLED+KEY_PADPLUS:
1337                         case KEY_ALTED+KEY_SHIFTED+KEY_PADPLUS:
1338                                 //increase res
1339                                 break;
1340 #endif
1341                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADENTER:
1342                         case KEY_ALTED+KEY_CTRLED+KEY_PADENTER:
1343                         case KEY_ALTED+KEY_SHIFTED+KEY_PADENTER:
1344                                 gr_toggle_fullscreen_game();
1345                                 break;
1346 //end addition -MM
1347
1348               }
1349                 }
1350
1351                 if ( Controls.fire_primary_down_count ) {
1352                         // Reset orientation
1353                         ViewDist = ZOOM_DEFAULT;
1354                         tangles.p = PITCH_DEFAULT;
1355                         tangles.h  = 0;
1356                         tangles.b  = 0;
1357                         view_target = Objects[Players[Player_num].objnum].pos;
1358                 }
1359
1360                 ViewDist -= Controls.forward_thrust_time*ZOOM_SPEED_FACTOR;
1361
1362                 tangles.p += fixdiv( Controls.pitch_time, ROT_SPEED_DIVISOR );
1363                 tangles.h  += fixdiv( Controls.heading_time, ROT_SPEED_DIVISOR );
1364                 tangles.b  += fixdiv( Controls.bank_time, ROT_SPEED_DIVISOR*2 );
1365                 
1366                 if ( Controls.vertical_thrust_time || Controls.sideways_thrust_time )   {
1367                         vms_angvec      tangles1;
1368                         vms_vector      old_vt;
1369                         old_vt = view_target;
1370                         tangles1 = tangles;
1371                         vm_angles_2_matrix(&tempm,&tangles1);
1372                         vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1373                         vm_vec_scale_add2( &view_target, &ViewMatrix.uvec, Controls.vertical_thrust_time*SLIDE_SPEED );
1374                         vm_vec_scale_add2( &view_target, &ViewMatrix.rvec, Controls.sideways_thrust_time*SLIDE_SPEED );
1375                         if ( vm_vec_dist_quick( &view_target, &Objects[Players[Player_num].objnum].pos) > i2f(1000) )   {
1376                                 view_target = old_vt;
1377                         }
1378                 } 
1379
1380                 vm_angles_2_matrix(&tempm,&tangles);
1381                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1382
1383                 if ( ViewDist < ZOOM_MIN_VALUE ) ViewDist = ZOOM_MIN_VALUE;
1384                 if ( ViewDist > ZOOM_MAX_VALUE ) ViewDist = ZOOM_MAX_VALUE;
1385
1386                 draw_automap();
1387
1388                 if ( first_time )       {
1389                         first_time = 0;
1390                         gr_palette_load( gr_palette );
1391                 }
1392
1393                 t2 = timer_get_fixed_seconds();
1394                 if (pause_game)
1395                         FrameTime=t2-t1;
1396                 t1 = t2;
1397         }
1398
1399         //d_free(Edges);
1400         //d_free(DrawingListBright);
1401
1402         gr_free_canvas(name_canv_left);  name_canv_left=NULL;
1403         gr_free_canvas(name_canv_right);  name_canv_right=NULL;
1404
1405         if (must_free_canvas)   {
1406 #ifndef AUTOMAP_DIRECT_RENDER
1407         WINDOS(
1408                 DDFreeSurface(ddPages[0].lpdds),
1409                 d_free(Page.cv_bitmap.bm_data)
1410         );
1411 #endif
1412         }
1413
1414         mprintf( (0, "Automap memory freed\n" ));
1415
1416         game_flush_inputs();
1417         
1418         #if defined(POLY_ACC) && defined(MACINTOSH)
1419                 if (PAEnabled)
1420                 {
1421                         pa_set_context(kGamePlayDrawContextID, NULL);
1422                 }
1423         #endif
1424
1425         if (pause_game)
1426         {
1427                 start_time();
1428                 digi_resume_digi_sounds();
1429         }
1430
1431 #ifdef WINDOWS
1432         VR_screen_mode = dd_VR_screen_mode_save;
1433 #endif
1434
1435         Automap_active = 0;
1436 }
1437
1438 void adjust_segment_limit(int SegmentLimit)
1439 {
1440         int i,e1;
1441         Edge_info * e;
1442
1443         mprintf(( 0, "Seglimit: %d\n", SegmentLimit ));
1444         
1445         for (i=0; i<=Highest_edge_index; i++ )  {
1446                 e = &Edges[i];
1447                 e->flags |= EF_TOO_FAR;
1448                 for (e1=0; e1<e->num_faces; e1++ )      {
1449                         if ( Automap_visited[e->segnum[e1]] <= SegmentLimit )   {
1450                                 e->flags &= (~EF_TOO_FAR);
1451                                 break;
1452                         }
1453                 }
1454         }
1455         
1456 }
1457
1458 void draw_all_edges()   
1459 {
1460         g3s_codes cc;
1461         int i,j,nbright;
1462         ubyte nfacing,nnfacing;
1463         Edge_info *e;
1464         vms_vector *tv1;
1465         fix distance;
1466         fix min_distance = 0x7fffffff;
1467         g3s_point *p1, *p2;
1468         
1469         
1470         nbright=0;
1471
1472         for (i=0; i<=Highest_edge_index; i++ )  {
1473                 //e = &Edges[Edge_used_list[i]];
1474                 e = &Edges[i];
1475                 if (!(e->flags & EF_USED)) continue;
1476
1477                 if ( e->flags & EF_TOO_FAR) continue;
1478
1479                 if (e->flags&EF_FRONTIER)       {                                               // A line that is between what we have seen and what we haven't
1480                         if ( (!(e->flags&EF_SECRET))&&(e->color==Wall_normal_color))
1481                                 continue;               // If a line isn't secret and is normal color, then don't draw it
1482                 }
1483
1484                 cc=rotate_list(2,e->verts);
1485                 distance = Segment_points[e->verts[1]].p3_z;
1486
1487                 if (min_distance>distance )
1488                         min_distance = distance;
1489
1490                 if (!cc.and)    {       //all off screen?
1491                         nfacing = nnfacing = 0;
1492                         tv1 = &Vertices[e->verts[0]];
1493                         j = 0;
1494                         while( j<e->num_faces && (nfacing==0 || nnfacing==0) )  {
1495                                 #ifdef COMPACT_SEGS
1496                                 vms_vector temp_v;
1497                                 get_side_normal(&Segments[e->segnum[j]], e->sides[j], 0, &temp_v );
1498                                 if (!g3_check_normal_facing( tv1, &temp_v ) )
1499                                 #else
1500                                 if (!g3_check_normal_facing( tv1, &Segments[e->segnum[j]].sides[e->sides[j]].normals[0] ) )
1501                                 #endif
1502                                         nfacing++;
1503                                 else
1504                                         nnfacing++;
1505                                 j++;
1506                         }
1507
1508                         if ( nfacing && nnfacing )      {
1509                                 // a contour line
1510                                 DrawingListBright[nbright++] = e-Edges;
1511                         } else if ( e->flags&(EF_DEFINING|EF_GRATE) )   {
1512                                 if ( nfacing == 0 )     {
1513                                         if ( e->flags & EF_NO_FADE )
1514                                                 gr_setcolor( e->color );
1515                                         else
1516                                                 gr_setcolor( gr_fade_table[e->color+256*8] );
1517                                         g3_draw_line( &Segment_points[e->verts[0]], &Segment_points[e->verts[1]] );
1518                                 }       else {
1519                                         DrawingListBright[nbright++] = e-Edges;
1520                                 }
1521                         }
1522                 }
1523         }
1524                 
1525 ///     mprintf( (0, "Min distance=%.2f, ViewDist=%.2f, Delta=%.2f\n", f2fl(min_distance), f2fl(ViewDist), f2fl(min_distance)- f2fl(ViewDist) ));
1526
1527         if ( min_distance < 0 ) min_distance = 0;
1528
1529         // Sort the bright ones using a shell sort
1530         {
1531                 int t;
1532                 int i, j, incr, v1, v2;
1533         
1534                 incr = nbright / 2;
1535                 while( incr > 0 )       {
1536                         for (i=incr; i<nbright; i++ )   {
1537                                 j = i - incr;
1538                                 while (j>=0 )   {
1539                                         // compare element j and j+incr
1540                                         v1 = Edges[DrawingListBright[j]].verts[0];
1541                                         v2 = Edges[DrawingListBright[j+incr]].verts[0];
1542
1543                                         if (Segment_points[v1].p3_z < Segment_points[v2].p3_z) {
1544                                                 // If not in correct order, them swap 'em
1545                                                 t=DrawingListBright[j+incr];
1546                                                 DrawingListBright[j+incr]=DrawingListBright[j];
1547                                                 DrawingListBright[j]=t;
1548                                                 j -= incr;
1549                                         }
1550                                         else
1551                                                 break;
1552                                 }
1553                         }
1554                         incr = incr / 2;
1555                 }
1556         }
1557                                         
1558         // Draw the bright ones
1559         for (i=0; i<nbright; i++ )      {
1560                 int color;
1561                 fix dist;
1562                 e = &Edges[DrawingListBright[i]];
1563                 p1 = &Segment_points[e->verts[0]];
1564                 p2 = &Segment_points[e->verts[1]];
1565                 dist = p1->p3_z - min_distance;
1566                 // Make distance be 1.0 to 0.0, where 0.0 is 10 segments away;
1567                 if ( dist < 0 ) dist=0;
1568                 if ( dist >= Automap_farthest_dist ) continue;
1569
1570                 if ( e->flags & EF_NO_FADE )    {
1571                         gr_setcolor( e->color );
1572                 } else {
1573                         dist = F1_0 - fixdiv( dist, Automap_farthest_dist );
1574                         color = f2i( dist*31 );
1575                         gr_setcolor( gr_fade_table[e->color+color*256] );       
1576                 }
1577                 g3_draw_line( p1, p2 );
1578         }
1579
1580 }
1581
1582
1583 //==================================================================
1584 //
1585 // All routines below here are used to build the Edge list
1586 //
1587 //==================================================================
1588
1589
1590 //finds edge, filling in edge_ptr. if found old edge, returns index, else return -1
1591 static int automap_find_edge(int v0,int v1,Edge_info **edge_ptr)
1592 {
1593         long vv, evv;
1594         short hash,oldhash;
1595         int ret, ev0, ev1;
1596
1597         vv = (v1<<16) + v0;
1598
1599         oldhash = hash = ((v0*5+v1) % Max_edges);
1600
1601         ret = -1;
1602
1603         while (ret==-1) {
1604                 ev0 = (int)(Edges[hash].verts[0]);
1605                 ev1 = (int)(Edges[hash].verts[1]);
1606                 evv = (ev1<<16)+ev0;
1607                 if (Edges[hash].num_faces == 0 ) ret=0;
1608                 else if (evv == vv) ret=1;
1609                 else {
1610                         if (++hash==Max_edges) hash=0;
1611                         if (hash==oldhash) Error("Edge list full!");
1612                 }
1613         }
1614
1615         *edge_ptr = &Edges[hash];
1616
1617         if (ret == 0)
1618                 return -1;
1619         else
1620                 return hash;
1621
1622 }
1623
1624
1625 void add_one_edge( short va, short vb, ubyte color, ubyte side, short segnum, int hidden, int grate, int no_fade )      {
1626         int found;
1627         Edge_info *e;
1628         short tmp;
1629
1630         if ( Num_edges >= Max_edges)    {
1631                 // GET JOHN! (And tell him that his 
1632                 // MAX_EDGES_FROM_VERTS formula is hosed.)
1633                 // If he's not around, save the mine, 
1634                 // and send him  mail so he can look 
1635                 // at the mine later. Don't modify it.
1636                 // This is important if this happens.
1637                 Int3();         // LOOK ABOVE!!!!!!
1638                 return;
1639         }
1640
1641         if ( va > vb )  {
1642                 tmp = va;
1643                 va = vb;
1644                 vb = tmp;
1645         }
1646
1647         found = automap_find_edge(va,vb,&e);
1648                 
1649         if (found == -1) {
1650                 e->verts[0] = va;
1651                 e->verts[1] = vb;
1652                 e->color = color;
1653                 e->num_faces = 1;
1654                 e->flags = EF_USED | EF_DEFINING;                       // Assume a normal line
1655                 e->sides[0] = side;
1656                 e->segnum[0] = segnum;
1657                 //Edge_used_list[Num_edges] = e-Edges;
1658                 if ( (e-Edges) > Highest_edge_index )
1659                         Highest_edge_index = e - Edges;
1660                 Num_edges++;
1661         } else {
1662                 //Assert(e->num_faces < 8 );
1663
1664                 if ( color != Wall_normal_color )
1665                         if (color != Wall_revealed_color)
1666                                 e->color = color;
1667
1668                 if ( e->num_faces < 4 ) {
1669                         e->sides[e->num_faces] = side;                                  
1670                         e->segnum[e->num_faces] = segnum;
1671                         e->num_faces++;
1672                 }
1673         }
1674
1675         if ( grate )
1676                 e->flags |= EF_GRATE;
1677
1678         if ( hidden )
1679                 e->flags|=EF_SECRET;            // Mark this as a hidden edge
1680         if ( no_fade )
1681                 e->flags |= EF_NO_FADE;
1682 }
1683
1684 void add_one_unknown_edge( short va, short vb ) {
1685         int found;
1686         Edge_info *e;
1687         short tmp;
1688
1689         if ( va > vb )  {
1690                 tmp = va;
1691                 va = vb;
1692                 vb = tmp;
1693         }
1694
1695         found = automap_find_edge(va,vb,&e);
1696         if (found != -1)        
1697                 e->flags|=EF_FRONTIER;          // Mark as a border edge
1698 }
1699
1700 #ifndef _GAMESEQ_H
1701 extern obj_position Player_init[];
1702 #endif
1703
1704 void add_segment_edges(segment *seg)
1705 {
1706         int     is_grate, no_fade;
1707         ubyte   color;
1708         int     sn;
1709         int     segnum = seg-Segments;
1710         int     hidden_flag;
1711         int ttype,trigger_num;
1712         
1713         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1714                 short   vertex_list[4];
1715
1716                 hidden_flag = 0;
1717
1718                 is_grate = 0;
1719                 no_fade = 0;
1720
1721                 color = 255;
1722                 if (seg->children[sn] == -1) {
1723                         color = Wall_normal_color;
1724                 }
1725
1726                 switch( Segment2s[segnum].special )     {
1727                 case SEGMENT_IS_FUELCEN:
1728                         color = BM_XRGB( 29, 27, 13 );
1729                         break;
1730                 case SEGMENT_IS_CONTROLCEN:
1731                         if (Control_center_present)
1732                                 color = BM_XRGB( 29, 0, 0 );
1733                         break;
1734                 case SEGMENT_IS_ROBOTMAKER:
1735                         color = BM_XRGB( 29, 0, 31 );
1736                         break;
1737                 }
1738
1739                 if (seg->sides[sn].wall_num > -1)       {
1740                 
1741                         trigger_num = Walls[seg->sides[sn].wall_num].trigger;
1742                         ttype = Triggers[trigger_num].type;
1743                         if (ttype==TT_SECRET_EXIT)
1744                                 {
1745                             color = BM_XRGB( 29, 0, 31 );
1746                                  no_fade=1;
1747                                  goto Here;
1748                                 }       
1749
1750                         switch( Walls[seg->sides[sn].wall_num].type )   {
1751                         case WALL_DOOR:
1752                                 if (Walls[seg->sides[sn].wall_num].keys == KEY_BLUE) {
1753                                         no_fade = 1;
1754                                         color = Wall_door_blue;
1755                                         //mprintf((0, "Seg %i, side %i has BLUE wall\n", segnum, sn));
1756                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_GOLD) {
1757                                         no_fade = 1;
1758                                         color = Wall_door_gold;
1759                                         //mprintf((0, "Seg %i, side %i has GOLD wall\n", segnum, sn));
1760                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_RED) {
1761                                         no_fade = 1;
1762                                         color = Wall_door_red;
1763                                         //mprintf((0, "Seg %i, side %i has RED wall\n", segnum, sn));
1764                                 } else if (!(WallAnims[Walls[seg->sides[sn].wall_num].clip_num].flags & WCF_HIDDEN)) {
1765                                         int     connected_seg = seg->children[sn];
1766                                         if (connected_seg != -1) {
1767                                                 int connected_side = find_connect_side(seg, &Segments[connected_seg]);
1768                                                 int     keytype = Walls[Segments[connected_seg].sides[connected_side].wall_num].keys;
1769                                                 if ((keytype != KEY_BLUE) && (keytype != KEY_GOLD) && (keytype != KEY_RED))
1770                                                         color = Wall_door_color;
1771                                                 else {
1772                                                         switch (Walls[Segments[connected_seg].sides[connected_side].wall_num].keys) {
1773                                                                 case KEY_BLUE:  color = Wall_door_blue; no_fade = 1; break;
1774                                                                 case KEY_GOLD:  color = Wall_door_gold; no_fade = 1; break;
1775                                                                 case KEY_RED:   color = Wall_door_red;  no_fade = 1; break;
1776                                                                 default:        Error("Inconsistent data.  Supposed to be a colored wall, but not blue, gold or red.\n");
1777                                                         }
1778                                                         //mprintf((0, "Seg %i, side %i has a colored door on the other side.\n", segnum, sn));
1779                                                 }
1780
1781                                         }
1782                                 } else {
1783                                         color = Wall_normal_color;
1784                                         hidden_flag = 1;
1785                                         //mprintf((0, "Wall at seg:side %i:%i is hidden.\n", seg-Segments, sn));
1786                                 }
1787                                 break;
1788                         case WALL_CLOSED:
1789                                 // Make grates draw properly
1790                                 if (WALL_IS_DOORWAY(seg,sn) & WID_RENDPAST_FLAG)
1791                                         is_grate = 1;
1792                                 else
1793                                         hidden_flag = 1;
1794                                 color = Wall_normal_color;
1795                                 break;
1796                         case WALL_BLASTABLE:
1797                                 // Hostage doors
1798                                 color = Wall_door_color;        
1799                                 break;
1800                         }
1801                 }
1802         
1803                 if (segnum==Player_init[Player_num].segnum)
1804                         color = BM_XRGB(31,0,31);
1805
1806                 if ( color != 255 )     {
1807                         // If they have a map powerup, draw unvisited areas in dark blue.
1808                         if (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL && (!Automap_visited[segnum]))     
1809                                 color = Wall_revealed_color;
1810
1811
1812                         Here:
1813
1814                         get_side_verts(vertex_list,segnum,sn);
1815                         add_one_edge( vertex_list[0], vertex_list[1], color, sn, segnum, hidden_flag, 0, no_fade );
1816                         add_one_edge( vertex_list[1], vertex_list[2], color, sn, segnum, hidden_flag, 0, no_fade );
1817                         add_one_edge( vertex_list[2], vertex_list[3], color, sn, segnum, hidden_flag, 0, no_fade );
1818                         add_one_edge( vertex_list[3], vertex_list[0], color, sn, segnum, hidden_flag, 0, no_fade );
1819
1820                         if ( is_grate ) {
1821                                 add_one_edge( vertex_list[0], vertex_list[2], color, sn, segnum, hidden_flag, 1, no_fade );
1822                                 add_one_edge( vertex_list[1], vertex_list[3], color, sn, segnum, hidden_flag, 1, no_fade );
1823                         }
1824                 }
1825         }
1826
1827 }
1828
1829
1830 // Adds all the edges from a segment we haven't visited yet.
1831
1832 void add_unknown_segment_edges(segment *seg)
1833 {
1834         int sn;
1835         int segnum = seg-Segments;
1836         
1837         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1838                 short   vertex_list[4];
1839
1840                 // Only add edges that have no children
1841                 if (seg->children[sn] == -1) {
1842                         get_side_verts(vertex_list,segnum,sn);
1843         
1844                         add_one_unknown_edge( vertex_list[0], vertex_list[1] );
1845                         add_one_unknown_edge( vertex_list[1], vertex_list[2] );
1846                         add_one_unknown_edge( vertex_list[2], vertex_list[3] );
1847                         add_one_unknown_edge( vertex_list[3], vertex_list[0] );
1848                 }
1849
1850
1851         }
1852
1853 }
1854
1855 void automap_build_edge_list()
1856 {       
1857         int     i,e1,e2,s;
1858         Edge_info * e;
1859
1860         Automap_cheat = 0;
1861
1862         if ( Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL_CHEAT )
1863                 Automap_cheat = 1;              // Damn cheaters...
1864
1865         // clear edge list
1866         for (i=0; i<Max_edges; i++) {
1867                 Edges[i].num_faces = 0;
1868                 Edges[i].flags = 0;
1869         }
1870         Num_edges = 0;
1871         Highest_edge_index = -1;
1872
1873         if (Automap_cheat || (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL) )       {
1874                 // Cheating, add all edges as visited
1875                 for (s=0; s<=Highest_segment_index; s++)
1876                         #ifdef EDITOR
1877                         if (Segments[s].segnum != -1)
1878                         #endif
1879                         {
1880                                 add_segment_edges(&Segments[s]);
1881                         }
1882         } else {
1883                 // Not cheating, add visited edges, and then unvisited edges
1884                 for (s=0; s<=Highest_segment_index; s++)
1885                         #ifdef EDITOR
1886                         if (Segments[s].segnum != -1)
1887                         #endif
1888                                 if (Automap_visited[s]) {
1889                                         add_segment_edges(&Segments[s]);
1890                                 }
1891         
1892                 for (s=0; s<=Highest_segment_index; s++)
1893                         #ifdef EDITOR
1894                         if (Segments[s].segnum != -1)
1895                         #endif
1896                                 if (!Automap_visited[s]) {
1897                                         add_unknown_segment_edges(&Segments[s]);
1898                                 }
1899         }
1900
1901         // Find unnecessary lines (These are lines that don't have to be drawn because they have small curvature)
1902         for (i=0; i<=Highest_edge_index; i++ )  {
1903                 e = &Edges[i];
1904                 if (!(e->flags&EF_USED)) continue;
1905
1906                 for (e1=0; e1<e->num_faces; e1++ )      {
1907                         for (e2=1; e2<e->num_faces; e2++ )      {
1908                                 if ( (e1 != e2) && (e->segnum[e1] != e->segnum[e2]) )   {
1909                                         #ifdef COMPACT_SEGS
1910                                         vms_vector v1, v2;
1911                                         get_side_normal(&Segments[e->segnum[e1]], e->sides[e1], 0, &v1 );
1912                                         get_side_normal(&Segments[e->segnum[e2]], e->sides[e2], 0, &v2 );
1913                                         if ( vm_vec_dot(&v1,&v2) > (F1_0-(F1_0/10))  )  {
1914                                         #else
1915                                         if ( vm_vec_dot( &Segments[e->segnum[e1]].sides[e->sides[e1]].normals[0], &Segments[e->segnum[e2]].sides[e->sides[e2]].normals[0] ) > (F1_0-(F1_0/10))  )       {
1916                                         #endif
1917                                                 e->flags &= (~EF_DEFINING);
1918                                                 break;
1919                                         }
1920                                 }
1921                         }
1922                         if (!(e->flags & EF_DEFINING))
1923                                 break;
1924                 }
1925         }       
1926
1927         mprintf( (0, "Automap used %d / %d edges\n", Num_edges, Max_edges  ));
1928
1929 }
1930
1931 char Marker_input [40];
1932 int Marker_index=0;
1933 ubyte DefiningMarkerMessage=0;
1934 ubyte MarkerBeingDefined;
1935 ubyte LastMarkerDropped;
1936
1937 void InitMarkerInput ()
1938  {
1939         int maxdrop,i;
1940
1941         //find free marker slot
1942
1943         if (Game_mode & GM_MULTI)
1944         maxdrop=MAX_DROP_MULTI;
1945         else
1946         maxdrop=MAX_DROP_SINGLE;
1947
1948         for (i=0;i<maxdrop;i++)
1949                 if (MarkerObject[(Player_num*2)+i] == -1)               //found free slot!
1950                         break;
1951
1952         if (i==maxdrop)         //no free slot
1953         {
1954                 if (Game_mode & GM_MULTI)
1955                         i = !LastMarkerDropped;         //in multi, replace older of two
1956                 else {
1957                         HUD_init_message("No free marker slots");
1958                         return;
1959                 }
1960         }
1961
1962         //got a free slot.  start inputting marker message
1963
1964         Marker_input[0]=0;
1965         Marker_index=0;
1966         DefiningMarkerMessage=1;
1967         MarkerBeingDefined = i;
1968  }
1969
1970 void MarkerInputMessage (int key)
1971  {
1972         switch( key )   {
1973         case KEY_F8:
1974         case KEY_ESC:
1975                 DefiningMarkerMessage = 0;
1976                 game_flush_inputs();
1977                 break;
1978         case KEY_LEFT:
1979         case KEY_BACKSP:
1980         case KEY_PAD4:
1981                 if (Marker_index > 0)
1982                         Marker_index--;
1983                 Marker_input[Marker_index] = 0;
1984                 break;
1985         case KEY_ENTER:
1986                 strcpy (MarkerMessage[(Player_num*2)+MarkerBeingDefined],Marker_input);
1987                 if (Game_mode & GM_MULTI)
1988                  strcpy (MarkerOwner[(Player_num*2)+MarkerBeingDefined],Players[Player_num].callsign);
1989                 DropMarker(MarkerBeingDefined);
1990                 LastMarkerDropped = MarkerBeingDefined;
1991                 game_flush_inputs();
1992                 DefiningMarkerMessage = 0;
1993                 break;
1994         default:
1995                 if ( key > 0 )
1996                  {
1997                   int ascii = key_to_ascii(key);
1998                   if ((ascii < 255 ))     
1999                     if (Marker_index < 38 )
2000                       {
2001                         Marker_input[Marker_index++] = ascii;
2002                         Marker_input[Marker_index] = 0;
2003                       }
2004                  }
2005                 break;
2006
2007         }
2008  }
2009   
2010
2011
2012