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