use hardcoded value instead of sizeof(tmap_info), since size depends on whether edito...
[btb/d2x.git] / main / automap.c
1 /* $Id: automap.c,v 1.20 2005-01-08 03:37:38 btb 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         temp_canv->cv_bitmap.bm_flags |= BM_FLAG_TRANSPARENT;
751         gr_clear_canvas(TRANSPARENCY_COLOR);                                            //trans color
752         gr_set_fontcolor(fc,bc);
753         gr_printf(0,0,s);
754
755         //now double it, since we're drawing to 400-line modex screen
756
757         if (double_flag) {
758                 data = temp_canv->cv_bitmap.bm_data;
759                 rs = temp_canv->cv_bitmap.bm_rowsize;
760
761                 for (y=temp_canv->cv_bitmap.bm_h/2;y--;) {
762                         memcpy(data+(rs*y*2),data+(rs*y),temp_canv->cv_bitmap.bm_w);
763                         memcpy(data+(rs*(y*2+1)),data+(rs*y),temp_canv->cv_bitmap.bm_w);
764                 }
765         }
766
767 WINDOS(
768         dd_gr_set_current_canvas(save_canv),
769         gr_set_current_canvas(save_canv)
770 );
771
772         return temp_canv;
773 }
774
775 //print to buffer, double heights, and blit bitmap to screen
776 void modex_printf(int x,int y,char *s,grs_font *font,int color)
777 {
778         grs_canvas *temp_canv;
779
780         temp_canv = print_to_canvas(s, font, color, -1, !AutomapHires);
781
782         gr_bitmapm(x,y,&temp_canv->cv_bitmap);
783
784         gr_free_canvas(temp_canv);
785 }
786
787 //name for each group.  maybe move somewhere else
788 char *system_name[] = {
789                         "Zeta Aquilae",
790                         "Quartzon System",
791                         "Brimspark System",
792                         "Limefrost Spiral",
793                         "Baloris Prime",
794                         "Omega System"};
795
796 void create_name_canv()
797 {
798         char    name_level_left[128],name_level_right[128];
799
800         if (Current_level_num > 0)
801                 sprintf(name_level_left, "%s %i",TXT_LEVEL, Current_level_num);
802         else
803                 sprintf(name_level_left, "Secret Level %i",-Current_level_num);
804
805         if (PLAYING_BUILTIN_MISSION && Current_level_num > 0)
806                 sprintf(name_level_right,"%s %d: ",system_name[(Current_level_num-1)/4],((Current_level_num-1)%4)+1);
807         else
808                 strcpy(name_level_right, " ");
809
810         strcat(name_level_right, Current_level_name);
811
812         gr_set_fontcolor(Green_31,-1);
813         name_canv_left = print_to_canvas(name_level_left, SMALL_FONT, Green_31, -1, !AutomapHires);
814         name_canv_right = print_to_canvas(name_level_right,SMALL_FONT, Green_31, -1, !AutomapHires);
815
816 }
817
818
819 extern void GameLoop(int, int );
820 extern int set_segment_depths(int start_seg, ubyte *segbuf);
821
822 int Automap_active = 0;
823
824 #ifdef RELEASE
825 #define MAP_BACKGROUND_FILENAME (AutomapHires?"\x01MAPB.PCX":"\x01MAP.PCX")     //load only from hog file
826 #else
827 #define MAP_BACKGROUND_FILENAME ((AutomapHires && cfexist("mapb.pcx"))?"MAPB.PCX":"MAP.PCX")
828 #endif
829
830 int Automap_always_hires=0;
831 extern int MenuHiresAvailable;
832
833 extern int Current_display_mode;
834
835 u_int32_t automap_mode = SM(640,480);
836 int automap_width = 640;
837 int automap_height = 480;
838 int automap_use_game_res=0;
839 int nice_automap = 0;
840
841 #define RESCALE_X(x) ((x) * automap_width / 640)
842 #define RESCALE_Y(y) ((y) * automap_height / 480)
843
844 void do_automap( int key_code ) {
845         int done=0;
846         vms_matrix      tempm;
847         vms_angvec      tangles;
848         int leave_mode=0;
849         int first_time=1;
850         int pcx_error;
851 #if !defined(AUTOMAP_DIRECT_RENDER) || !defined(NDEBUG)
852         int i;
853 #endif
854         int c, marker_num;
855         fix entry_time;
856         int pause_game=1;               // Set to 1 if everything is paused during automap...No pause during net.
857         fix t1, t2;
858         control_info saved_control_info;
859         grs_bitmap Automap_background;
860         int Max_segments_away = 0;
861         int SegmentLimit = 1;
862         ubyte pal[256*3];
863         char maxdrop;
864         int must_free_canvas=0;
865         
866         WIN(int dd_VR_screen_mode_save);
867         WIN(int redraw_screen=0);
868
869         Automap_active = 1;
870
871         init_automap_colors();
872
873         key_code = key_code;    // disable warning...
874
875         if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
876                 pause_game = 0;
877
878         if (pause_game) {
879                 stop_time();
880                 digi_pause_digi_sounds();
881         }
882
883         Max_edges = min(MAX_EDGES_FROM_VERTS(Num_vertices),MAX_EDGES);                  //make maybe smaller than max
884
885         mprintf( (0, "Num_vertices=%d, Max_edges=%d, (MAX:%d)\n", Num_vertices, Max_edges, MAX_EDGES ));
886         mprintf( (0, "Allocated %d K for automap edge list\n", (sizeof(Edge_info)+sizeof(short))*Max_edges/1024 ));
887
888         #if !defined (WINDOWS) && !defined(MACINTOSH)
889         if ((Current_display_mode!=0 && Current_display_mode!=2) || (Automap_always_hires && MenuHiresAvailable)) {
890 #if !defined(POLY_ACC)
891                 //edit 4/23/99 Matt Mueller - don't switch res unless we need to
892                 if (grd_curscreen->sc_mode != AUTOMAP_MODE)
893                         gr_set_mode( AUTOMAP_MODE );
894                 else
895                         gr_set_current_canvas(NULL);
896                 //end edit -MM
897                 automap_width=grd_curscreen->sc_canvas.cv_bitmap.bm_w;
898                 automap_height=grd_curscreen->sc_canvas.cv_bitmap.bm_h;
899 #endif
900                 PA_DFX (pa_set_frontbuffer_current());
901                 AutomapHires = 1;
902         }
903         else {
904                 gr_set_mode( SM(320, 400));
905                 AutomapHires = 0;
906         }
907         #else
908                 AutomapHires = 1;               //Mac & Windows(?) always in hires
909         #endif
910
911         #ifdef WINDOWS
912                 dd_VR_screen_mode_save = VR_screen_mode;
913                 VR_screen_mode = SM95_640x480x8;        // HACK! Forcing reinit of 640x480
914                 set_screen_mode(SCREEN_GAME);
915         #endif
916
917         FontHires = FontHiresAvailable && AutomapHires;
918
919         create_name_canv();
920
921         gr_palette_clear();
922
923 WIN(AutomapRedraw:)
924         if (!AutomapHires) {
925 #ifndef MACINTOSH
926 #ifndef AUTOMAP_DIRECT_RENDER
927                 gr_init_sub_canvas(&Pages[0],grd_curcanv,0,0,320,400);
928                 gr_init_sub_canvas(&Pages[1],grd_curcanv,0,401,320,400);
929                 gr_init_sub_canvas(&DrawingPages[0],&Pages[0],16,69,WINDOW_WIDTH,272);
930                 gr_init_sub_canvas(&DrawingPages[1],&Pages[1],16,69,WINDOW_WIDTH,272);
931 #endif
932
933                 gr_init_bitmap_data (&Automap_background);
934                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME, &Automap_background, BM_LINEAR, pal);
935                 if (pcx_error != PCX_ERROR_NONE)
936                         Error("File %s - PCX error: %s", MAP_BACKGROUND_FILENAME, pcx_errormsg(pcx_error));
937                 gr_remap_bitmap_good(&Automap_background, pal, -1, -1);
938
939 #ifndef AUTOMAP_DIRECT_RENDER
940                 for (i=0; i<2; i++)
941                 {
942                         gr_set_current_canvas(&Pages[i]);
943                         gr_bitmap(0, 0, &Automap_background );
944                         modex_printf(40,  22, TXT_AUTOMAP, HUGE_FONT, Font_color_20);
945                         modex_printf(30, 353, TXT_TURN_SHIP, SMALL_FONT, Font_color_20);
946                         modex_printf(30, 369, TXT_SLIDE_UPDOWN, SMALL_FONT, Font_color_20);
947                         modex_printf(30, 385, TXT_VIEWING_DISTANCE, SMALL_FONT, Font_color_20);
948                 }
949                 gr_free_bitmap_data(&Automap_background);       
950                 gr_set_current_canvas(&DrawingPages[current_page]);
951 #endif /* AUTOMAP_DIRECT_RENDER */
952 #endif /* MACINTOSH */
953         }
954         else {
955 #ifndef AUTOMAP_DIRECT_RENDER
956                 if (VR_render_buffer[0].cv_w >= automap_width && VR_render_buffer[0].cv_h >= automap_height)
957                 {
958                         WIN(dd_gr_init_sub_canvas(&ddPage, &dd_VR_render_buffer[0], 0, 0, automap_width,automap_height));
959
960                         #if defined(MACINTOSH) && defined(POLY_ACC)
961                                 if (PAEnabled)
962                                 {
963                                         // we want all the automap border stuff to be drawn straight to the screen
964                                         gr_init_sub_canvas(&Page,&(grd_curscreen->sc_canvas),0, 0, automap_width, automap_height);
965                                 }
966                                 else
967                                 {
968                                         gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
969                                 }
970                         #else
971                                 gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
972                         #endif
973                         
974                 }
975                 else {
976                 #ifndef WINDOWS
977                         void *raw_data;
978                         MALLOC(raw_data,ubyte,automap_width*automap_height);
979                         gr_init_canvas(&Page,raw_data,BM_LINEAR,automap_width,automap_height);
980                 #else
981                         dd_gr_init_canvas(&ddPage, BM_LINEAR, automap_width,automap_height);
982                         gr_init_canvas(&Page,NULL,BM_LINEAR,automap_width,automap_height);
983                 #endif
984                         must_free_canvas = 1;
985                 }
986
987                 WIN(dd_gr_init_sub_canvas(&ddDrawingPage, &ddPage, RESCALE_X(27), RESCALE_Y(80), RESCALE_X(582), RESCALE_Y(334)));
988                 gr_init_sub_canvas(&DrawingPage, &Page, RESCALE_X(27), RESCALE_Y(80), RESCALE_X(582), RESCALE_Y(334));
989
990                 WINDOS(
991                         dd_gr_set_current_canvas(&ddPage),
992                         gr_set_current_canvas(&Page)
993                 );
994 #endif
995
996 #if defined(POLY_ACC)
997
998                 #ifndef MACINTOSH
999                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1000         #else
1001                 if ( PAEnabled )
1002                 {
1003                         pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1004                                 if ( pcx_error != PCX_ERROR_NONE )
1005                                 {
1006                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1007                                         return;
1008                                 }
1009                     }
1010                     else
1011                     {
1012                                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR,pal);
1013                                 if ( pcx_error != PCX_ERROR_NONE )
1014                                 {
1015                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1016                                         return;
1017                                 }
1018         
1019                                 gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1020                     }
1021             #endif
1022 #else
1023
1024                 WIN(DDGRLOCK(dd_grd_curcanv));
1025                 pcx_error = pcx_read_fullscr(MAP_BACKGROUND_FILENAME, pal);
1026                         if ( pcx_error != PCX_ERROR_NONE )      {
1027                                 //printf("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1028                                 Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1029                                 return;
1030                         }
1031
1032                         gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1033 #endif
1034         
1035                         gr_set_curfont(HUGE_FONT);
1036                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1037                         gr_printf(RESCALE_X(80), RESCALE_Y(36), TXT_AUTOMAP, HUGE_FONT);
1038                         gr_set_curfont(SMALL_FONT);
1039                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1040                         gr_printf(RESCALE_X(60), RESCALE_Y(426), TXT_TURN_SHIP);
1041                         gr_printf(RESCALE_X(60), RESCALE_Y(443), TXT_SLIDE_UPDOWN);
1042                         gr_printf(RESCALE_X(60), RESCALE_Y(460), TXT_VIEWING_DISTANCE);
1043                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1044         
1045 #ifndef AUTOMAP_DIRECT_RENDER
1046                 WINDOS(
1047                         dd_gr_set_current_canvas(&ddDrawingPage),
1048                         gr_set_current_canvas(&DrawingPage)
1049                 );
1050 #endif
1051         }
1052
1053
1054 WIN(if (!redraw_screen) {)
1055         automap_build_edge_list();
1056
1057         if ( ViewDist==0 )
1058                 ViewDist = ZOOM_DEFAULT;
1059         ViewMatrix = Objects[Players[Player_num].objnum].orient;
1060
1061         tangles.p = PITCH_DEFAULT;
1062         tangles.h  = 0;
1063         tangles.b  = 0;
1064
1065         done = 0;
1066
1067         view_target = Objects[Players[Player_num].objnum].pos;
1068
1069         t1 = entry_time = timer_get_fixed_seconds();
1070         t2 = t1;
1071
1072         //Fill in Automap_visited from Objects[Players[Player_num].objnum].segnum
1073         Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1074         SegmentLimit = Max_segments_away;
1075
1076         adjust_segment_limit(SegmentLimit);
1077 WIN(})
1078
1079 WIN(if (redraw_screen) redraw_screen = 0);
1080
1081         while(!done)    {
1082                 if ( leave_mode==0 && Controls.automap_state && (timer_get_fixed_seconds()-entry_time)>LEAVE_TIME)
1083                         leave_mode = 1;
1084
1085                 if ( !Controls.automap_state && (leave_mode==1) )
1086                         done=1;
1087
1088                 if (!pause_game)        {
1089                         ushort old_wiggle;
1090                         saved_control_info = Controls;                          // Save controls so we can zero them
1091                         memset(&Controls,0,sizeof(control_info));       // Clear everything...
1092                         old_wiggle = ConsoleObject->mtype.phys_info.flags & PF_WIGGLE;  // Save old wiggle
1093                         ConsoleObject->mtype.phys_info.flags &= ~PF_WIGGLE;             // Turn off wiggle
1094                         #ifdef NETWORK
1095                         if (multi_menu_poll())
1096                                 done = 1;
1097                         #endif
1098 //                      GameLoop( 0, 0 );               // Do game loop with no rendering and no reading controls.
1099                         ConsoleObject->mtype.phys_info.flags |= old_wiggle;     // Restore wiggle
1100                         Controls = saved_control_info;
1101                 }
1102
1103         #ifndef WINDOWS
1104                 controls_read_all();            
1105         #else
1106                 controls_read_all_win();
1107         #endif
1108
1109                 if ( Controls.automap_down_count )      {
1110                         if (leave_mode==0)
1111                                 done = 1;
1112                         c = 0;
1113                 }
1114
1115                 //see if redbook song needs to be restarted
1116                 songs_check_redbook_repeat();
1117
1118                 #ifdef WINDOWS
1119                 {
1120                         MSG msg;
1121                         DoMessageStuff(&msg);
1122                         if (_RedrawScreen) {
1123                                 _RedrawScreen = FALSE;
1124                                 redraw_screen = 1;
1125                                 goto AutomapRedraw;
1126                         }
1127                                 
1128                         if (msg.message == WM_QUIT) exit(1);
1129
1130                         DDGRRESTORE;
1131                 }
1132                 #endif
1133
1134
1135                 while( (c=key_inkey()) )        {
1136                         switch( c ) {
1137                         #ifndef NDEBUG
1138                         case KEY_BACKSP: Int3(); break;
1139                         #endif
1140         
1141                         case KEY_PRINT_SCREEN: {
1142                                 if (AutomapHires) {
1143                                 WINDOS(
1144                                         dd_gr_set_current_canvas(NULL),
1145                                                 gr_set_current_canvas(NULL)
1146                                 );
1147                                 }
1148 #ifndef AUTOMAP_DIRECT_RENDER
1149                                 else
1150                                         gr_set_current_canvas(&Pages[current_page]);
1151 #endif
1152                                 save_screen_shot(1);
1153                                 break;
1154                         }
1155         
1156                         case KEY_ESC:
1157                                 if (leave_mode==0)
1158                                         done = 1;
1159                                  break;
1160
1161                         #ifndef NDEBUG
1162                         case KEY_DEBUGGED+KEY_F:        {
1163                                 for (i=0; i<=Highest_segment_index; i++ )
1164                                         Automap_visited[i] = 1;
1165                                 automap_build_edge_list();
1166                                 Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1167                                 SegmentLimit = Max_segments_away;
1168                                 adjust_segment_limit(SegmentLimit);
1169                                 }
1170                                 break;
1171                         #endif
1172
1173                         case KEY_MINUS:
1174                                 if (SegmentLimit > 1)           {
1175                                         SegmentLimit--;
1176                                         adjust_segment_limit(SegmentLimit);
1177                                 }
1178                                 break;
1179                         case KEY_EQUAL:
1180                                 if (SegmentLimit < Max_segments_away)   {
1181                                         SegmentLimit++;
1182                                         adjust_segment_limit(SegmentLimit);
1183                                 }
1184                                 break;
1185                         case KEY_1:
1186                         case KEY_2:
1187                         case KEY_3:
1188                         case KEY_4:
1189                         case KEY_5:
1190                         case KEY_6:
1191                         case KEY_7:
1192                         case KEY_8:
1193                         case KEY_9:
1194                         case KEY_0:
1195                                 if (Game_mode & GM_MULTI)
1196                                 maxdrop=2;
1197                                 else
1198                                 maxdrop=9;
1199
1200                         #ifndef MACINTOSH
1201                         marker_num = c-KEY_1;
1202                         #else
1203                         switch(c) {             // god this is stupid.....
1204                                 case KEY_1: marker_num = 0; break;
1205                                 case KEY_2: marker_num = 1; break;
1206                                 case KEY_3: marker_num = 2; break;
1207                                 case KEY_4: marker_num = 3; break;
1208                                 case KEY_5: marker_num = 4; break;
1209                                 case KEY_6: marker_num = 5; break;
1210                                 case KEY_7: marker_num = 6; break;
1211                                 case KEY_8: marker_num = 7; break;
1212                                 case KEY_9: marker_num = 8; break;
1213                                 case KEY_0: marker_num = 9; break;
1214                         }
1215                         #endif
1216             if (marker_num<=maxdrop)
1217                                  {
1218                                         if (MarkerObject[marker_num] != -1)
1219                                                 HighlightMarker=marker_num;
1220                                  }
1221                           break;
1222
1223                         case KEY_D+KEY_CTRLED:
1224 #ifndef AUTOMAP_DIRECT_RENDER
1225                                 if (current_page)               //menu will only work on page 0
1226                                         draw_automap(); //..so switch from 1 to 0
1227 #endif
1228
1229                                 if (HighlightMarker > -1 && MarkerObject[HighlightMarker] != -1) {
1230 #ifndef AUTOMAP_DIRECT_RENDER
1231                                         WINDOS(
1232                                                 dd_gr_set_current_canvas(&ddPages[current_page]),
1233                                                 gr_set_current_canvas(&Pages[current_page])
1234                                         );
1235 #endif
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                                 break;
1255                         #endif
1256
1257 //added 8/23/99 by Matt Mueller for hot key res/fullscreen changing, and menu access
1258 #if 0
1259                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADDIVIDE:
1260                         case KEY_ALTED+KEY_CTRLED+KEY_PADDIVIDE:
1261                         case KEY_ALTED+KEY_SHIFTED+KEY_PADDIVIDE:
1262                                 d1x_options_menu();
1263                                 break;
1264                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMULTIPLY:
1265                         case KEY_ALTED+KEY_CTRLED+KEY_PADMULTIPLY:
1266                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMULTIPLY:
1267                                 change_res();
1268                                 break;
1269                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMINUS:
1270                         case KEY_ALTED+KEY_CTRLED+KEY_PADMINUS:
1271                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMINUS:
1272                                 //lower res
1273                                 //should we just cycle through the list that is displayed in the res change menu?
1274                                 // what if their card/X/etc can't handle that mode? hrm.
1275                                 //well, the quick access to the menu is good enough for now.
1276                                 break;
1277                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADPLUS:
1278                         case KEY_ALTED+KEY_CTRLED+KEY_PADPLUS:
1279                         case KEY_ALTED+KEY_SHIFTED+KEY_PADPLUS:
1280                                 //increase res
1281                                 break;
1282 #endif
1283                         case KEY_ALTED+KEY_ENTER:
1284                         case KEY_ALTED+KEY_PADENTER:
1285                                 gr_toggle_fullscreen_game();
1286                                 break;
1287 //end addition -MM
1288
1289               }
1290                 }
1291
1292                 if ( Controls.fire_primary_down_count ) {
1293                         // Reset orientation
1294                         ViewDist = ZOOM_DEFAULT;
1295                         tangles.p = PITCH_DEFAULT;
1296                         tangles.h  = 0;
1297                         tangles.b  = 0;
1298                         view_target = Objects[Players[Player_num].objnum].pos;
1299                 }
1300
1301                 ViewDist -= Controls.forward_thrust_time*ZOOM_SPEED_FACTOR;
1302
1303                 tangles.p += fixdiv( Controls.pitch_time, ROT_SPEED_DIVISOR );
1304                 tangles.h  += fixdiv( Controls.heading_time, ROT_SPEED_DIVISOR );
1305                 tangles.b  += fixdiv( Controls.bank_time, ROT_SPEED_DIVISOR*2 );
1306                 
1307                 if ( Controls.vertical_thrust_time || Controls.sideways_thrust_time )   {
1308                         vms_angvec      tangles1;
1309                         vms_vector      old_vt;
1310                         old_vt = view_target;
1311                         tangles1 = tangles;
1312                         vm_angles_2_matrix(&tempm,&tangles1);
1313                         vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1314                         vm_vec_scale_add2( &view_target, &ViewMatrix.uvec, Controls.vertical_thrust_time*SLIDE_SPEED );
1315                         vm_vec_scale_add2( &view_target, &ViewMatrix.rvec, Controls.sideways_thrust_time*SLIDE_SPEED );
1316                         if ( vm_vec_dist_quick( &view_target, &Objects[Players[Player_num].objnum].pos) > i2f(1000) )   {
1317                                 view_target = old_vt;
1318                         }
1319                 }
1320
1321                 vm_angles_2_matrix(&tempm,&tangles);
1322                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1323
1324                 if ( ViewDist < ZOOM_MIN_VALUE ) ViewDist = ZOOM_MIN_VALUE;
1325                 if ( ViewDist > ZOOM_MAX_VALUE ) ViewDist = ZOOM_MAX_VALUE;
1326
1327                 draw_automap();
1328
1329                 if ( first_time )       {
1330                         first_time = 0;
1331                         gr_palette_load( gr_palette );
1332                 }
1333
1334                 t2 = timer_get_fixed_seconds();
1335                 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 :)
1336                 {
1337                         if (nice_automap)
1338                                 timer_delay(1);
1339
1340                         t2 = timer_get_fixed_seconds();
1341                 }
1342                 if (pause_game)
1343                         FrameTime=t2-t1;
1344                 t1 = t2;
1345         }
1346
1347         //d_free(Edges);
1348         //d_free(DrawingListBright);
1349
1350         gr_free_canvas(name_canv_left);  name_canv_left=NULL;
1351         gr_free_canvas(name_canv_right);  name_canv_right=NULL;
1352
1353         if (must_free_canvas)   {
1354 #ifndef AUTOMAP_DIRECT_RENDER
1355         WINDOS(
1356                 DDFreeSurface(ddPages[0].lpdds),
1357                 d_free(Page.cv_bitmap.bm_data)
1358         );
1359 #endif
1360         }
1361
1362         mprintf( (0, "Automap memory freed\n" ));
1363
1364         game_flush_inputs();
1365         
1366         #if defined(POLY_ACC) && defined(MACINTOSH)
1367                 if (PAEnabled)
1368                 {
1369                         pa_set_context(kGamePlayDrawContextID, NULL);
1370                 }
1371         #endif
1372
1373         if (pause_game)
1374         {
1375                 start_time();
1376                 digi_resume_digi_sounds();
1377         }
1378
1379 #ifdef WINDOWS
1380         VR_screen_mode = dd_VR_screen_mode_save;
1381 #endif
1382
1383         Automap_active = 0;
1384 }
1385
1386 void adjust_segment_limit(int SegmentLimit)
1387 {
1388         int i,e1;
1389         Edge_info * e;
1390
1391         mprintf(( 0, "Seglimit: %d\n", SegmentLimit ));
1392         
1393         for (i=0; i<=Highest_edge_index; i++ )  {
1394                 e = &Edges[i];
1395                 e->flags |= EF_TOO_FAR;
1396                 for (e1=0; e1<e->num_faces; e1++ )      {
1397                         if ( Automap_visited[e->segnum[e1]] <= SegmentLimit )   {
1398                                 e->flags &= (~EF_TOO_FAR);
1399                                 break;
1400                         }
1401                 }
1402         }
1403         
1404 }
1405
1406 void draw_all_edges()   
1407 {
1408         g3s_codes cc;
1409         int i,j,nbright;
1410         ubyte nfacing,nnfacing;
1411         Edge_info *e;
1412         vms_vector *tv1;
1413         fix distance;
1414         fix min_distance = 0x7fffffff;
1415         g3s_point *p1, *p2;
1416         
1417         
1418         nbright=0;
1419
1420         for (i=0; i<=Highest_edge_index; i++ )  {
1421                 //e = &Edges[Edge_used_list[i]];
1422                 e = &Edges[i];
1423                 if (!(e->flags & EF_USED)) continue;
1424
1425                 if ( e->flags & EF_TOO_FAR) continue;
1426
1427                 if (e->flags&EF_FRONTIER)       {                                               // A line that is between what we have seen and what we haven't
1428                         if ( (!(e->flags&EF_SECRET))&&(e->color==Wall_normal_color))
1429                                 continue;               // If a line isn't secret and is normal color, then don't draw it
1430                 }
1431
1432                 cc=rotate_list(2,e->verts);
1433                 distance = Segment_points[e->verts[1]].p3_z;
1434
1435                 if (min_distance>distance )
1436                         min_distance = distance;
1437
1438                 if (!cc.and)    {       //all off screen?
1439                         nfacing = nnfacing = 0;
1440                         tv1 = &Vertices[e->verts[0]];
1441                         j = 0;
1442                         while( j<e->num_faces && (nfacing==0 || nnfacing==0) )  {
1443                                 #ifdef COMPACT_SEGS
1444                                 vms_vector temp_v;
1445                                 get_side_normal(&Segments[e->segnum[j]], e->sides[j], 0, &temp_v );
1446                                 if (!g3_check_normal_facing( tv1, &temp_v ) )
1447                                 #else
1448                                 if (!g3_check_normal_facing( tv1, &Segments[e->segnum[j]].sides[e->sides[j]].normals[0] ) )
1449                                 #endif
1450                                         nfacing++;
1451                                 else
1452                                         nnfacing++;
1453                                 j++;
1454                         }
1455
1456                         if ( nfacing && nnfacing )      {
1457                                 // a contour line
1458                                 DrawingListBright[nbright++] = e-Edges;
1459                         } else if ( e->flags&(EF_DEFINING|EF_GRATE) )   {
1460                                 if ( nfacing == 0 )     {
1461                                         if ( e->flags & EF_NO_FADE )
1462                                                 gr_setcolor( e->color );
1463                                         else
1464                                                 gr_setcolor( gr_fade_table[e->color+256*8] );
1465                                         g3_draw_line( &Segment_points[e->verts[0]], &Segment_points[e->verts[1]] );
1466                                 }       else {
1467                                         DrawingListBright[nbright++] = e-Edges;
1468                                 }
1469                         }
1470                 }
1471         }
1472                 
1473 ///     mprintf( (0, "Min distance=%.2f, ViewDist=%.2f, Delta=%.2f\n", f2fl(min_distance), f2fl(ViewDist), f2fl(min_distance)- f2fl(ViewDist) ));
1474
1475         if ( min_distance < 0 ) min_distance = 0;
1476
1477         // Sort the bright ones using a shell sort
1478         {
1479                 int t;
1480                 int i, j, incr, v1, v2;
1481         
1482                 incr = nbright / 2;
1483                 while( incr > 0 )       {
1484                         for (i=incr; i<nbright; i++ )   {
1485                                 j = i - incr;
1486                                 while (j>=0 )   {
1487                                         // compare element j and j+incr
1488                                         v1 = Edges[DrawingListBright[j]].verts[0];
1489                                         v2 = Edges[DrawingListBright[j+incr]].verts[0];
1490
1491                                         if (Segment_points[v1].p3_z < Segment_points[v2].p3_z) {
1492                                                 // If not in correct order, them swap 'em
1493                                                 t=DrawingListBright[j+incr];
1494                                                 DrawingListBright[j+incr]=DrawingListBright[j];
1495                                                 DrawingListBright[j]=t;
1496                                                 j -= incr;
1497                                         }
1498                                         else
1499                                                 break;
1500                                 }
1501                         }
1502                         incr = incr / 2;
1503                 }
1504         }
1505                                         
1506         // Draw the bright ones
1507         for (i=0; i<nbright; i++ )      {
1508                 int color;
1509                 fix dist;
1510                 e = &Edges[DrawingListBright[i]];
1511                 p1 = &Segment_points[e->verts[0]];
1512                 p2 = &Segment_points[e->verts[1]];
1513                 dist = p1->p3_z - min_distance;
1514                 // Make distance be 1.0 to 0.0, where 0.0 is 10 segments away;
1515                 if ( dist < 0 ) dist=0;
1516                 if ( dist >= Automap_farthest_dist ) continue;
1517
1518                 if ( e->flags & EF_NO_FADE )    {
1519                         gr_setcolor( e->color );
1520                 } else {
1521                         dist = F1_0 - fixdiv( dist, Automap_farthest_dist );
1522                         color = f2i( dist*31 );
1523                         gr_setcolor( gr_fade_table[e->color+color*256] );       
1524                 }
1525                 g3_draw_line( p1, p2 );
1526         }
1527
1528 }
1529
1530
1531 //==================================================================
1532 //
1533 // All routines below here are used to build the Edge list
1534 //
1535 //==================================================================
1536
1537
1538 //finds edge, filling in edge_ptr. if found old edge, returns index, else return -1
1539 static int automap_find_edge(int v0,int v1,Edge_info **edge_ptr)
1540 {
1541         long vv, evv;
1542         short hash,oldhash;
1543         int ret, ev0, ev1;
1544
1545         vv = (v1<<16) + v0;
1546
1547         oldhash = hash = ((v0*5+v1) % Max_edges);
1548
1549         ret = -1;
1550
1551         while (ret==-1) {
1552                 ev0 = (int)(Edges[hash].verts[0]);
1553                 ev1 = (int)(Edges[hash].verts[1]);
1554                 evv = (ev1<<16)+ev0;
1555                 if (Edges[hash].num_faces == 0 ) ret=0;
1556                 else if (evv == vv) ret=1;
1557                 else {
1558                         if (++hash==Max_edges) hash=0;
1559                         if (hash==oldhash) Error("Edge list full!");
1560                 }
1561         }
1562
1563         *edge_ptr = &Edges[hash];
1564
1565         if (ret == 0)
1566                 return -1;
1567         else
1568                 return hash;
1569
1570 }
1571
1572
1573 void add_one_edge( short va, short vb, ubyte color, ubyte side, short segnum, int hidden, int grate, int no_fade )      {
1574         int found;
1575         Edge_info *e;
1576         short tmp;
1577
1578         if ( Num_edges >= Max_edges)    {
1579                 // GET JOHN! (And tell him that his
1580                 // MAX_EDGES_FROM_VERTS formula is hosed.)
1581                 // If he's not around, save the mine,
1582                 // and send him  mail so he can look
1583                 // at the mine later. Don't modify it.
1584                 // This is important if this happens.
1585                 Int3();         // LOOK ABOVE!!!!!!
1586                 return;
1587         }
1588
1589         if ( va > vb )  {
1590                 tmp = va;
1591                 va = vb;
1592                 vb = tmp;
1593         }
1594
1595         found = automap_find_edge(va,vb,&e);
1596                 
1597         if (found == -1) {
1598                 e->verts[0] = va;
1599                 e->verts[1] = vb;
1600                 e->color = color;
1601                 e->num_faces = 1;
1602                 e->flags = EF_USED | EF_DEFINING;                       // Assume a normal line
1603                 e->sides[0] = side;
1604                 e->segnum[0] = segnum;
1605                 //Edge_used_list[Num_edges] = e-Edges;
1606                 if ( (e-Edges) > Highest_edge_index )
1607                         Highest_edge_index = e - Edges;
1608                 Num_edges++;
1609         } else {
1610                 //Assert(e->num_faces < 8 );
1611
1612                 if ( color != Wall_normal_color )
1613                         if (color != Wall_revealed_color)
1614                                 e->color = color;
1615
1616                 if ( e->num_faces < 4 ) {
1617                         e->sides[e->num_faces] = side;                                  
1618                         e->segnum[e->num_faces] = segnum;
1619                         e->num_faces++;
1620                 }
1621         }
1622
1623         if ( grate )
1624                 e->flags |= EF_GRATE;
1625
1626         if ( hidden )
1627                 e->flags|=EF_SECRET;            // Mark this as a hidden edge
1628         if ( no_fade )
1629                 e->flags |= EF_NO_FADE;
1630 }
1631
1632 void add_one_unknown_edge( short va, short vb ) {
1633         int found;
1634         Edge_info *e;
1635         short tmp;
1636
1637         if ( va > vb )  {
1638                 tmp = va;
1639                 va = vb;
1640                 vb = tmp;
1641         }
1642
1643         found = automap_find_edge(va,vb,&e);
1644         if (found != -1)        
1645                 e->flags|=EF_FRONTIER;          // Mark as a border edge
1646 }
1647
1648 #ifndef _GAMESEQ_H
1649 extern obj_position Player_init[];
1650 #endif
1651
1652 void add_segment_edges(segment *seg)
1653 {
1654         int     is_grate, no_fade;
1655         ubyte   color;
1656         int     sn;
1657         int     segnum = seg-Segments;
1658         int     hidden_flag;
1659         int ttype,trigger_num;
1660         
1661         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1662                 short   vertex_list[4];
1663
1664                 hidden_flag = 0;
1665
1666                 is_grate = 0;
1667                 no_fade = 0;
1668
1669                 color = 255;
1670                 if (seg->children[sn] == -1) {
1671                         color = Wall_normal_color;
1672                 }
1673
1674                 switch( Segment2s[segnum].special )     {
1675                 case SEGMENT_IS_FUELCEN:
1676                         color = BM_XRGB( 29, 27, 13 );
1677                         break;
1678                 case SEGMENT_IS_CONTROLCEN:
1679                         if (Control_center_present)
1680                                 color = BM_XRGB( 29, 0, 0 );
1681                         break;
1682                 case SEGMENT_IS_ROBOTMAKER:
1683                         color = BM_XRGB( 29, 0, 31 );
1684                         break;
1685                 }
1686
1687                 if (seg->sides[sn].wall_num > -1)       {
1688                 
1689                         trigger_num = Walls[seg->sides[sn].wall_num].trigger;
1690                         ttype = Triggers[trigger_num].type;
1691                         if (ttype==TT_SECRET_EXIT)
1692                                 {
1693                             color = BM_XRGB( 29, 0, 31 );
1694                                  no_fade=1;
1695                                  goto Here;
1696                                 }       
1697
1698                         switch( Walls[seg->sides[sn].wall_num].type )   {
1699                         case WALL_DOOR:
1700                                 if (Walls[seg->sides[sn].wall_num].keys == KEY_BLUE) {
1701                                         no_fade = 1;
1702                                         color = Wall_door_blue;
1703                                         //mprintf((0, "Seg %i, side %i has BLUE wall\n", segnum, sn));
1704                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_GOLD) {
1705                                         no_fade = 1;
1706                                         color = Wall_door_gold;
1707                                         //mprintf((0, "Seg %i, side %i has GOLD wall\n", segnum, sn));
1708                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_RED) {
1709                                         no_fade = 1;
1710                                         color = Wall_door_red;
1711                                         //mprintf((0, "Seg %i, side %i has RED wall\n", segnum, sn));
1712                                 } else if (!(WallAnims[Walls[seg->sides[sn].wall_num].clip_num].flags & WCF_HIDDEN)) {
1713                                         int     connected_seg = seg->children[sn];
1714                                         if (connected_seg != -1) {
1715                                                 int connected_side = find_connect_side(seg, &Segments[connected_seg]);
1716                                                 int     keytype = Walls[Segments[connected_seg].sides[connected_side].wall_num].keys;
1717                                                 if ((keytype != KEY_BLUE) && (keytype != KEY_GOLD) && (keytype != KEY_RED))
1718                                                         color = Wall_door_color;
1719                                                 else {
1720                                                         switch (Walls[Segments[connected_seg].sides[connected_side].wall_num].keys) {
1721                                                                 case KEY_BLUE:  color = Wall_door_blue; no_fade = 1; break;
1722                                                                 case KEY_GOLD:  color = Wall_door_gold; no_fade = 1; break;
1723                                                                 case KEY_RED:   color = Wall_door_red;  no_fade = 1; break;
1724                                                                 default:        Error("Inconsistent data.  Supposed to be a colored wall, but not blue, gold or red.\n");
1725                                                         }
1726                                                         //mprintf((0, "Seg %i, side %i has a colored door on the other side.\n", segnum, sn));
1727                                                 }
1728
1729                                         }
1730                                 } else {
1731                                         color = Wall_normal_color;
1732                                         hidden_flag = 1;
1733                                         //mprintf((0, "Wall at seg:side %i:%i is hidden.\n", seg-Segments, sn));
1734                                 }
1735                                 break;
1736                         case WALL_CLOSED:
1737                                 // Make grates draw properly
1738                                 if (WALL_IS_DOORWAY(seg,sn) & WID_RENDPAST_FLAG)
1739                                         is_grate = 1;
1740                                 else
1741                                         hidden_flag = 1;
1742                                 color = Wall_normal_color;
1743                                 break;
1744                         case WALL_BLASTABLE:
1745                                 // Hostage doors
1746                                 color = Wall_door_color;        
1747                                 break;
1748                         }
1749                 }
1750         
1751                 if (segnum==Player_init[Player_num].segnum)
1752                         color = BM_XRGB(31,0,31);
1753
1754                 if ( color != 255 )     {
1755                         // If they have a map powerup, draw unvisited areas in dark blue.
1756                         if (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL && (!Automap_visited[segnum]))     
1757                                 color = Wall_revealed_color;
1758
1759
1760                         Here:
1761
1762                         get_side_verts(vertex_list,segnum,sn);
1763                         add_one_edge( vertex_list[0], vertex_list[1], color, sn, segnum, hidden_flag, 0, no_fade );
1764                         add_one_edge( vertex_list[1], vertex_list[2], color, sn, segnum, hidden_flag, 0, no_fade );
1765                         add_one_edge( vertex_list[2], vertex_list[3], color, sn, segnum, hidden_flag, 0, no_fade );
1766                         add_one_edge( vertex_list[3], vertex_list[0], color, sn, segnum, hidden_flag, 0, no_fade );
1767
1768                         if ( is_grate ) {
1769                                 add_one_edge( vertex_list[0], vertex_list[2], color, sn, segnum, hidden_flag, 1, no_fade );
1770                                 add_one_edge( vertex_list[1], vertex_list[3], color, sn, segnum, hidden_flag, 1, no_fade );
1771                         }
1772                 }
1773         }
1774
1775 }
1776
1777
1778 // Adds all the edges from a segment we haven't visited yet.
1779
1780 void add_unknown_segment_edges(segment *seg)
1781 {
1782         int sn;
1783         int segnum = seg-Segments;
1784         
1785         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1786                 short   vertex_list[4];
1787
1788                 // Only add edges that have no children
1789                 if (seg->children[sn] == -1) {
1790                         get_side_verts(vertex_list,segnum,sn);
1791         
1792                         add_one_unknown_edge( vertex_list[0], vertex_list[1] );
1793                         add_one_unknown_edge( vertex_list[1], vertex_list[2] );
1794                         add_one_unknown_edge( vertex_list[2], vertex_list[3] );
1795                         add_one_unknown_edge( vertex_list[3], vertex_list[0] );
1796                 }
1797
1798
1799         }
1800
1801 }
1802
1803 void automap_build_edge_list()
1804 {       
1805         int     i,e1,e2,s;
1806         Edge_info * e;
1807
1808         Automap_cheat = 0;
1809
1810         if ( Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL_CHEAT )
1811                 Automap_cheat = 1;              // Damn cheaters...
1812
1813         // clear edge list
1814         for (i=0; i<Max_edges; i++) {
1815                 Edges[i].num_faces = 0;
1816                 Edges[i].flags = 0;
1817         }
1818         Num_edges = 0;
1819         Highest_edge_index = -1;
1820
1821         if (Automap_cheat || (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL) )       {
1822                 // Cheating, add all edges as visited
1823                 for (s=0; s<=Highest_segment_index; s++)
1824                         #ifdef EDITOR
1825                         if (Segments[s].segnum != -1)
1826                         #endif
1827                         {
1828                                 add_segment_edges(&Segments[s]);
1829                         }
1830         } else {
1831                 // Not cheating, add visited edges, and then unvisited edges
1832                 for (s=0; s<=Highest_segment_index; s++)
1833                         #ifdef EDITOR
1834                         if (Segments[s].segnum != -1)
1835                         #endif
1836                                 if (Automap_visited[s]) {
1837                                         add_segment_edges(&Segments[s]);
1838                                 }
1839         
1840                 for (s=0; s<=Highest_segment_index; s++)
1841                         #ifdef EDITOR
1842                         if (Segments[s].segnum != -1)
1843                         #endif
1844                                 if (!Automap_visited[s]) {
1845                                         add_unknown_segment_edges(&Segments[s]);
1846                                 }
1847         }
1848
1849         // Find unnecessary lines (These are lines that don't have to be drawn because they have small curvature)
1850         for (i=0; i<=Highest_edge_index; i++ )  {
1851                 e = &Edges[i];
1852                 if (!(e->flags&EF_USED)) continue;
1853
1854                 for (e1=0; e1<e->num_faces; e1++ )      {
1855                         for (e2=1; e2<e->num_faces; e2++ )      {
1856                                 if ( (e1 != e2) && (e->segnum[e1] != e->segnum[e2]) )   {
1857                                         #ifdef COMPACT_SEGS
1858                                         vms_vector v1, v2;
1859                                         get_side_normal(&Segments[e->segnum[e1]], e->sides[e1], 0, &v1 );
1860                                         get_side_normal(&Segments[e->segnum[e2]], e->sides[e2], 0, &v2 );
1861                                         if ( vm_vec_dot(&v1,&v2) > (F1_0-(F1_0/10))  )  {
1862                                         #else
1863                                         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))  )       {
1864                                         #endif
1865                                                 e->flags &= (~EF_DEFINING);
1866                                                 break;
1867                                         }
1868                                 }
1869                         }
1870                         if (!(e->flags & EF_DEFINING))
1871                                 break;
1872                 }
1873         }       
1874
1875         mprintf( (0, "Automap used %d / %d edges\n", Num_edges, Max_edges  ));
1876
1877 }
1878
1879 char Marker_input [40];
1880 int Marker_index=0;
1881 ubyte DefiningMarkerMessage=0;
1882 ubyte MarkerBeingDefined;
1883 ubyte LastMarkerDropped;
1884
1885 void InitMarkerInput ()
1886  {
1887         int maxdrop,i;
1888
1889         //find free marker slot
1890
1891         if (Game_mode & GM_MULTI)
1892         maxdrop=MAX_DROP_MULTI;
1893         else
1894         maxdrop=MAX_DROP_SINGLE;
1895
1896         for (i=0;i<maxdrop;i++)
1897                 if (MarkerObject[(Player_num*2)+i] == -1)               //found free slot!
1898                         break;
1899
1900         if (i==maxdrop)         //no free slot
1901         {
1902                 if (Game_mode & GM_MULTI)
1903                         i = !LastMarkerDropped;         //in multi, replace older of two
1904                 else {
1905                         HUD_init_message("No free marker slots");
1906                         return;
1907                 }
1908         }
1909
1910         //got a free slot.  start inputting marker message
1911
1912         Marker_input[0]=0;
1913         Marker_index=0;
1914         DefiningMarkerMessage=1;
1915         MarkerBeingDefined = i;
1916  }
1917
1918 void MarkerInputMessage (int key)
1919  {
1920         switch( key )   {
1921         case KEY_F8:
1922         case KEY_ESC:
1923                 DefiningMarkerMessage = 0;
1924                 game_flush_inputs();
1925                 break;
1926         case KEY_LEFT:
1927         case KEY_BACKSP:
1928         case KEY_PAD4:
1929                 if (Marker_index > 0)
1930                         Marker_index--;
1931                 Marker_input[Marker_index] = 0;
1932                 break;
1933         case KEY_ENTER:
1934                 strcpy (MarkerMessage[(Player_num*2)+MarkerBeingDefined],Marker_input);
1935                 if (Game_mode & GM_MULTI)
1936                  strcpy (MarkerOwner[(Player_num*2)+MarkerBeingDefined],Players[Player_num].callsign);
1937                 DropMarker(MarkerBeingDefined);
1938                 LastMarkerDropped = MarkerBeingDefined;
1939                 game_flush_inputs();
1940                 DefiningMarkerMessage = 0;
1941                 break;
1942         default:
1943                 if ( key > 0 )
1944                  {
1945                   int ascii = key_to_ascii(key);
1946                   if ((ascii < 255 ))
1947                     if (Marker_index < 38 )
1948                       {
1949                         Marker_input[Marker_index++] = ascii;
1950                         Marker_input[Marker_index] = 0;
1951                       }
1952                  }
1953                 break;
1954
1955         }
1956  }
1957
1958
1959
1960