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