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