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