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