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