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