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