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