]> icculus.org git repositories - btb/d2x.git/blob - main/automap.c
hmm, g3_draw_transp_poly had the fade level backwards, decided to just fix g3_draw_po...
[btb/d2x.git] / main / automap.c
1 /* $Id: automap.c,v 1.16 2003-11-15 00:37:48 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
1014 #define RESCALE_X(x) ((x) * automap_width / 640)
1015 #define RESCALE_Y(y) ((y) * automap_height / 480)
1016
1017 void do_automap( int key_code ) {
1018         int done=0;
1019         vms_matrix      tempm;
1020         vms_angvec      tangles;
1021         int leave_mode=0;
1022         int first_time=1;
1023         int pcx_error;
1024 #if !defined(AUTOMAP_DIRECT_RENDER) || !defined(NDEBUG)
1025         int i;
1026 #endif
1027         int c, marker_num;
1028         fix entry_time;
1029         int pause_game=1;               // Set to 1 if everything is paused during automap...No pause during net.
1030         fix t1, t2;
1031         control_info saved_control_info;
1032         grs_bitmap Automap_background;
1033         int Max_segments_away = 0;
1034         int SegmentLimit = 1;
1035         ubyte pal[256*3];
1036         char maxdrop;
1037         int must_free_canvas=0;
1038         
1039         WIN(int dd_VR_screen_mode_save);
1040         WIN(int redraw_screen=0);
1041
1042         Automap_active = 1;
1043
1044         init_automap_colors();
1045
1046         key_code = key_code;    // disable warning...
1047
1048         if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1049                 pause_game = 0;
1050
1051         if (pause_game) {
1052                 stop_time();
1053                 digi_pause_digi_sounds();
1054         }
1055
1056         Max_edges = min(MAX_EDGES_FROM_VERTS(Num_vertices),MAX_EDGES);                  //make maybe smaller than max
1057
1058         mprintf( (0, "Num_vertices=%d, Max_edges=%d, (MAX:%d)\n", Num_vertices, Max_edges, MAX_EDGES ));
1059         mprintf( (0, "Allocated %d K for automap edge list\n", (sizeof(Edge_info)+sizeof(short))*Max_edges/1024 ));
1060
1061         #if !defined (WINDOWS) && !defined(MACINTOSH)
1062         if ((Current_display_mode!=0 && Current_display_mode!=2) || (Automap_always_hires && MenuHiresAvailable)) {
1063 #if !defined(POLY_ACC)
1064                 //edit 4/23/99 Matt Mueller - don't switch res unless we need to
1065                 if (grd_curscreen->sc_mode != AUTOMAP_MODE)
1066                         gr_set_mode( AUTOMAP_MODE );
1067                 else
1068                         gr_set_current_canvas(NULL);
1069                 //end edit -MM
1070                 automap_width=grd_curscreen->sc_canvas.cv_bitmap.bm_w;
1071                 automap_height=grd_curscreen->sc_canvas.cv_bitmap.bm_h;
1072 #endif
1073                 PA_DFX (pa_set_frontbuffer_current());
1074                 AutomapHires = 1;
1075         }
1076         else {
1077                 gr_set_mode( SM(320, 400));
1078                 AutomapHires = 0;
1079         }
1080         #else
1081                 AutomapHires = 1;               //Mac & Windows(?) always in hires
1082         #endif
1083
1084         #ifdef WINDOWS
1085                 dd_VR_screen_mode_save = VR_screen_mode;
1086                 VR_screen_mode = SM95_640x480x8;        // HACK! Forcing reinit of 640x480
1087                 set_screen_mode(SCREEN_GAME);
1088         #endif
1089
1090         FontHires = FontHiresAvailable && AutomapHires;
1091
1092         create_name_canv();
1093
1094         gr_palette_clear();
1095
1096 WIN(AutomapRedraw:)
1097         if (!AutomapHires) {
1098 #ifndef MACINTOSH
1099 #ifndef AUTOMAP_DIRECT_RENDER
1100                 gr_init_sub_canvas(&Pages[0],grd_curcanv,0,0,320,400);
1101                 gr_init_sub_canvas(&Pages[1],grd_curcanv,0,401,320,400);
1102                 gr_init_sub_canvas(&DrawingPages[0],&Pages[0],16,69,WINDOW_WIDTH,272);
1103                 gr_init_sub_canvas(&DrawingPages[1],&Pages[1],16,69,WINDOW_WIDTH,272);
1104 #endif
1105
1106                 gr_init_bitmap_data (&Automap_background);
1107                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME, &Automap_background, BM_LINEAR, pal);
1108                 if (pcx_error != PCX_ERROR_NONE)
1109                         Error("File %s - PCX error: %s", MAP_BACKGROUND_FILENAME, pcx_errormsg(pcx_error));
1110                 gr_remap_bitmap_good(&Automap_background, pal, -1, -1);
1111
1112 #ifndef AUTOMAP_DIRECT_RENDER
1113                 for (i=0; i<2; i++)
1114                 {
1115                         gr_set_current_canvas(&Pages[i]);
1116                         gr_bitmap(0, 0, &Automap_background );
1117                         modex_printf(40,  22, TXT_AUTOMAP, HUGE_FONT, Font_color_20);
1118                         modex_printf(30, 353, TXT_TURN_SHIP, SMALL_FONT, Font_color_20);
1119                         modex_printf(30, 369, TXT_SLIDE_UPDOWN, SMALL_FONT, Font_color_20);
1120                         modex_printf(30, 385, TXT_VIEWING_DISTANCE, SMALL_FONT, Font_color_20);
1121                 }
1122                 gr_free_bitmap_data(&Automap_background);       
1123                 gr_set_current_canvas(&DrawingPages[current_page]);
1124 #endif /* AUTOMAP_DIRECT_RENDER */
1125 #endif /* MACINTOSH */
1126         }
1127         else {
1128 #ifndef AUTOMAP_DIRECT_RENDER
1129                 if (VR_render_buffer[0].cv_w >= automap_width && VR_render_buffer[0].cv_h >= automap_height)
1130                 {
1131                         WIN(dd_gr_init_sub_canvas(&ddPage, &dd_VR_render_buffer[0], 0, 0, automap_width,automap_height));
1132
1133                         #if defined(MACINTOSH) && defined(POLY_ACC)
1134                                 if (PAEnabled)
1135                                 {
1136                                         // we want all the automap border stuff to be drawn straight to the screen
1137                                         gr_init_sub_canvas(&Page,&(grd_curscreen->sc_canvas),0, 0, automap_width, automap_height);
1138                                 }
1139                                 else
1140                                 {
1141                                         gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
1142                                 }
1143                         #else
1144                                 gr_init_sub_canvas(&Page,&VR_render_buffer[0],0, 0, automap_width, automap_height);
1145                         #endif
1146                         
1147                 }
1148                 else {
1149                 #ifndef WINDOWS
1150                         void *raw_data;
1151                         MALLOC(raw_data,ubyte,automap_width*automap_height);
1152                         gr_init_canvas(&Page,raw_data,BM_LINEAR,automap_width,automap_height);
1153                 #else
1154                         dd_gr_init_canvas(&ddPage, BM_LINEAR, automap_width,automap_height);
1155                         gr_init_canvas(&Page,NULL,BM_LINEAR,automap_width,automap_height);
1156                 #endif
1157                         must_free_canvas = 1;
1158                 }
1159
1160                 WIN(dd_gr_init_sub_canvas(&ddDrawingPage, &ddPage, RESCALE_X(27), RESCALE_Y(80), RESCALE_X(582), RESCALE_Y(334)));
1161                 gr_init_sub_canvas(&DrawingPage, &Page, RESCALE_X(27), RESCALE_Y(80), RESCALE_X(582), RESCALE_Y(334));
1162
1163                 WINDOS(
1164                         dd_gr_set_current_canvas(&ddPage),
1165                         gr_set_current_canvas(&Page)
1166                 );
1167 #endif
1168
1169 #if defined(POLY_ACC)
1170
1171                 #ifndef MACINTOSH
1172                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1173         #else
1174                 if ( PAEnabled )
1175                 {
1176                         pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR15,pal);
1177                                 if ( pcx_error != PCX_ERROR_NONE )
1178                                 {
1179                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1180                                         return;
1181                                 }
1182                     }
1183                     else
1184                     {
1185                                 pcx_error = pcx_read_bitmap(MAP_BACKGROUND_FILENAME,&(grd_curcanv->cv_bitmap),BM_LINEAR,pal);
1186                                 if ( pcx_error != PCX_ERROR_NONE )
1187                                 {
1188                                         Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1189                                         return;
1190                                 }
1191         
1192                                 gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1193                     }
1194             #endif
1195 #else
1196
1197                 WIN(DDGRLOCK(dd_grd_curcanv));
1198                 pcx_error = pcx_read_fullscr(MAP_BACKGROUND_FILENAME, pal);
1199                         if ( pcx_error != PCX_ERROR_NONE )      {
1200                                 //printf("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1201                                 Error("File %s - PCX error: %s",MAP_BACKGROUND_FILENAME,pcx_errormsg(pcx_error));
1202                                 return;
1203                         }
1204
1205                         gr_remap_bitmap_good( &(grd_curcanv->cv_bitmap), pal, -1, -1 );
1206 #endif
1207         
1208                         gr_set_curfont(HUGE_FONT);
1209                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1210                         gr_printf(RESCALE_X(80), RESCALE_Y(36), TXT_AUTOMAP, HUGE_FONT);
1211                         gr_set_curfont(SMALL_FONT);
1212                         gr_set_fontcolor(BM_XRGB(20, 20, 20), -1);
1213                         gr_printf(RESCALE_X(60), RESCALE_Y(426), TXT_TURN_SHIP);
1214                         gr_printf(RESCALE_X(60), RESCALE_Y(443), TXT_SLIDE_UPDOWN);
1215                         gr_printf(RESCALE_X(60), RESCALE_Y(460), TXT_VIEWING_DISTANCE);
1216                 WIN(DDGRUNLOCK(dd_grd_curcanv));
1217         
1218 #ifndef AUTOMAP_DIRECT_RENDER
1219                 WINDOS(
1220                         dd_gr_set_current_canvas(&ddDrawingPage),
1221                         gr_set_current_canvas(&DrawingPage)
1222                 );
1223 #endif
1224         }
1225
1226
1227 WIN(if (!redraw_screen) {)
1228         automap_build_edge_list();
1229
1230         if ( ViewDist==0 )
1231                 ViewDist = ZOOM_DEFAULT;
1232         ViewMatrix = Objects[Players[Player_num].objnum].orient;
1233
1234         tangles.p = PITCH_DEFAULT;
1235         tangles.h  = 0;
1236         tangles.b  = 0;
1237
1238         done = 0;
1239
1240         view_target = Objects[Players[Player_num].objnum].pos;
1241
1242         t1 = entry_time = timer_get_fixed_seconds();
1243         t2 = t1;
1244
1245         //Fill in Automap_visited from Objects[Players[Player_num].objnum].segnum
1246         Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1247         SegmentLimit = Max_segments_away;
1248
1249         adjust_segment_limit(SegmentLimit);
1250 WIN(})
1251
1252 WIN(if (redraw_screen) redraw_screen = 0);
1253
1254         while(!done)    {
1255                 if ( leave_mode==0 && Controls.automap_state && (timer_get_fixed_seconds()-entry_time)>LEAVE_TIME)
1256                         leave_mode = 1;
1257
1258                 if ( !Controls.automap_state && (leave_mode==1) )
1259                         done=1;
1260
1261                 if (!pause_game)        {
1262                         ushort old_wiggle;
1263                         saved_control_info = Controls;                          // Save controls so we can zero them
1264                         memset(&Controls,0,sizeof(control_info));       // Clear everything...
1265                         old_wiggle = ConsoleObject->mtype.phys_info.flags & PF_WIGGLE;  // Save old wiggle
1266                         ConsoleObject->mtype.phys_info.flags &= ~PF_WIGGLE;             // Turn off wiggle
1267                         #ifdef NETWORK
1268                         if (multi_menu_poll())
1269                                 done = 1;
1270                         #endif
1271 //                      GameLoop( 0, 0 );               // Do game loop with no rendering and no reading controls.
1272                         ConsoleObject->mtype.phys_info.flags |= old_wiggle;     // Restore wiggle
1273                         Controls = saved_control_info;
1274                 }
1275
1276         #ifndef WINDOWS
1277                 controls_read_all();            
1278         #else
1279                 controls_read_all_win();
1280         #endif
1281
1282                 if ( Controls.automap_down_count )      {
1283                         if (leave_mode==0)
1284                                 done = 1;
1285                         c = 0;
1286                 }
1287
1288                 //see if redbook song needs to be restarted
1289                 songs_check_redbook_repeat();
1290
1291                 #ifdef WINDOWS
1292                 {
1293                         MSG msg;
1294                         DoMessageStuff(&msg);
1295                         if (_RedrawScreen) {
1296                                 _RedrawScreen = FALSE;
1297                                 redraw_screen = 1;
1298                                 goto AutomapRedraw;
1299                         }
1300                                 
1301                         if (msg.message == WM_QUIT) exit(1);
1302
1303                         DDGRRESTORE;
1304                 }
1305                 #endif
1306
1307
1308                 while( (c=key_inkey()) )        {
1309                         switch( c ) {
1310                         #ifndef NDEBUG
1311                         case KEY_BACKSP: Int3(); break;
1312                         #endif
1313         
1314                         case KEY_PRINT_SCREEN: {
1315                                 if (AutomapHires) {
1316                                 WINDOS(
1317                                         dd_gr_set_current_canvas(NULL),
1318                                                 gr_set_current_canvas(NULL)
1319                                 );
1320                                 }
1321 #ifndef AUTOMAP_DIRECT_RENDER
1322                                 else
1323                                         gr_set_current_canvas(&Pages[current_page]);
1324 #endif
1325                                 save_screen_shot(1);
1326                                 break;
1327                         }
1328         
1329                         case KEY_ESC:
1330                                 if (leave_mode==0)
1331                                         done = 1;
1332                                  break;
1333
1334                         #ifndef NDEBUG
1335                         case KEY_DEBUGGED+KEY_F:        {
1336                                 for (i=0; i<=Highest_segment_index; i++ )
1337                                         Automap_visited[i] = 1;
1338                                 automap_build_edge_list();
1339                                 Max_segments_away = set_segment_depths(Objects[Players[Player_num].objnum].segnum, Automap_visited);
1340                                 SegmentLimit = Max_segments_away;
1341                                 adjust_segment_limit(SegmentLimit);
1342                                 }
1343                                 break;
1344                         #endif
1345
1346                         case KEY_MINUS:
1347                                 if (SegmentLimit > 1)           {
1348                                         SegmentLimit--;
1349                                         adjust_segment_limit(SegmentLimit);
1350                                 }
1351                                 break;
1352                         case KEY_EQUAL:
1353                                 if (SegmentLimit < Max_segments_away)   {
1354                                         SegmentLimit++;
1355                                         adjust_segment_limit(SegmentLimit);
1356                                 }
1357                                 break;
1358                         case KEY_1:
1359                         case KEY_2:
1360                         case KEY_3:
1361                         case KEY_4:
1362                         case KEY_5:
1363                         case KEY_6:
1364                         case KEY_7:
1365                         case KEY_8:
1366                         case KEY_9:
1367                         case KEY_0:
1368                                 if (Game_mode & GM_MULTI)
1369                                 maxdrop=2;
1370                                 else
1371                                 maxdrop=9;
1372
1373                         #ifndef MACINTOSH
1374                         marker_num = c-KEY_1;
1375                         #else
1376                         switch(c) {             // god this is stupid.....
1377                                 case KEY_1: marker_num = 0; break;
1378                                 case KEY_2: marker_num = 1; break;
1379                                 case KEY_3: marker_num = 2; break;
1380                                 case KEY_4: marker_num = 3; break;
1381                                 case KEY_5: marker_num = 4; break;
1382                                 case KEY_6: marker_num = 5; break;
1383                                 case KEY_7: marker_num = 6; break;
1384                                 case KEY_8: marker_num = 7; break;
1385                                 case KEY_9: marker_num = 8; break;
1386                                 case KEY_0: marker_num = 9; break;
1387                         }
1388                         #endif
1389             if (marker_num<=maxdrop)
1390                                  {
1391                                         if (MarkerObject[marker_num] != -1)
1392                                                 HighlightMarker=marker_num;
1393                                  }
1394                           break;
1395
1396                         case KEY_D+KEY_CTRLED:
1397 #ifndef AUTOMAP_DIRECT_RENDER
1398                                 if (current_page)               //menu will only work on page 0
1399                                         draw_automap(); //..so switch from 1 to 0
1400 #endif
1401
1402                                 if (HighlightMarker > -1 && MarkerObject[HighlightMarker] != -1) {
1403 #ifndef AUTOMAP_DIRECT_RENDER
1404                                         WINDOS(
1405                                                 dd_gr_set_current_canvas(&ddPages[current_page]),
1406                                                 gr_set_current_canvas(&Pages[current_page])
1407                                         );
1408 #endif
1409
1410                                         if (nm_messagebox( NULL, 2, TXT_YES, TXT_NO, "Delete Marker?" ) == 0) {
1411                                                 obj_delete(MarkerObject[HighlightMarker]);
1412                                                 MarkerObject[HighlightMarker]=-1;
1413                                                 MarkerMessage[HighlightMarker][0]=0;
1414                                                 HighlightMarker = -1;
1415                                         }                                       
1416                                 }
1417                                 break;
1418
1419                         #ifndef RELEASE
1420                         case KEY_COMMA:
1421                                 if (MarkerScale>.5)
1422                                         MarkerScale-=.5;
1423                                 break;
1424                         case KEY_PERIOD:
1425                                 if (MarkerScale<30.0)
1426                                         MarkerScale+=.5;
1427                                 break;
1428                         #endif
1429
1430 //added 8/23/99 by Matt Mueller for hot key res/fullscreen changing, and menu access
1431 #if 0
1432                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADDIVIDE:
1433                         case KEY_ALTED+KEY_CTRLED+KEY_PADDIVIDE:
1434                         case KEY_ALTED+KEY_SHIFTED+KEY_PADDIVIDE:
1435                                 d1x_options_menu();
1436                                 break;
1437                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMULTIPLY:
1438                         case KEY_ALTED+KEY_CTRLED+KEY_PADMULTIPLY:
1439                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMULTIPLY:
1440                                 change_res();
1441                                 break;
1442                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADMINUS:
1443                         case KEY_ALTED+KEY_CTRLED+KEY_PADMINUS:
1444                         case KEY_ALTED+KEY_SHIFTED+KEY_PADMINUS:
1445                                 //lower res
1446                                 //should we just cycle through the list that is displayed in the res change menu?
1447                                 // what if their card/X/etc can't handle that mode? hrm.
1448                                 //well, the quick access to the menu is good enough for now.
1449                                 break;
1450                         case KEY_CTRLED+KEY_SHIFTED+KEY_PADPLUS:
1451                         case KEY_ALTED+KEY_CTRLED+KEY_PADPLUS:
1452                         case KEY_ALTED+KEY_SHIFTED+KEY_PADPLUS:
1453                                 //increase res
1454                                 break;
1455 #endif
1456                         case KEY_ALTED+KEY_ENTER:
1457                         case KEY_ALTED+KEY_PADENTER:
1458                                 gr_toggle_fullscreen_game();
1459                                 break;
1460 //end addition -MM
1461
1462               }
1463                 }
1464
1465                 if ( Controls.fire_primary_down_count ) {
1466                         // Reset orientation
1467                         ViewDist = ZOOM_DEFAULT;
1468                         tangles.p = PITCH_DEFAULT;
1469                         tangles.h  = 0;
1470                         tangles.b  = 0;
1471                         view_target = Objects[Players[Player_num].objnum].pos;
1472                 }
1473
1474                 ViewDist -= Controls.forward_thrust_time*ZOOM_SPEED_FACTOR;
1475
1476                 tangles.p += fixdiv( Controls.pitch_time, ROT_SPEED_DIVISOR );
1477                 tangles.h  += fixdiv( Controls.heading_time, ROT_SPEED_DIVISOR );
1478                 tangles.b  += fixdiv( Controls.bank_time, ROT_SPEED_DIVISOR*2 );
1479                 
1480                 if ( Controls.vertical_thrust_time || Controls.sideways_thrust_time )   {
1481                         vms_angvec      tangles1;
1482                         vms_vector      old_vt;
1483                         old_vt = view_target;
1484                         tangles1 = tangles;
1485                         vm_angles_2_matrix(&tempm,&tangles1);
1486                         vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1487                         vm_vec_scale_add2( &view_target, &ViewMatrix.uvec, Controls.vertical_thrust_time*SLIDE_SPEED );
1488                         vm_vec_scale_add2( &view_target, &ViewMatrix.rvec, Controls.sideways_thrust_time*SLIDE_SPEED );
1489                         if ( vm_vec_dist_quick( &view_target, &Objects[Players[Player_num].objnum].pos) > i2f(1000) )   {
1490                                 view_target = old_vt;
1491                         }
1492                 }
1493
1494                 vm_angles_2_matrix(&tempm,&tangles);
1495                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
1496
1497                 if ( ViewDist < ZOOM_MIN_VALUE ) ViewDist = ZOOM_MIN_VALUE;
1498                 if ( ViewDist > ZOOM_MAX_VALUE ) ViewDist = ZOOM_MAX_VALUE;
1499
1500                 draw_automap();
1501
1502                 if ( first_time )       {
1503                         first_time = 0;
1504                         gr_palette_load( gr_palette );
1505                 }
1506
1507                 t2 = timer_get_fixed_seconds();
1508                 if (pause_game)
1509                         FrameTime=t2-t1;
1510                 t1 = t2;
1511         }
1512
1513         //d_free(Edges);
1514         //d_free(DrawingListBright);
1515
1516         gr_free_canvas(name_canv_left);  name_canv_left=NULL;
1517         gr_free_canvas(name_canv_right);  name_canv_right=NULL;
1518
1519         if (must_free_canvas)   {
1520 #ifndef AUTOMAP_DIRECT_RENDER
1521         WINDOS(
1522                 DDFreeSurface(ddPages[0].lpdds),
1523                 d_free(Page.cv_bitmap.bm_data)
1524         );
1525 #endif
1526         }
1527
1528         mprintf( (0, "Automap memory freed\n" ));
1529
1530         game_flush_inputs();
1531         
1532         #if defined(POLY_ACC) && defined(MACINTOSH)
1533                 if (PAEnabled)
1534                 {
1535                         pa_set_context(kGamePlayDrawContextID, NULL);
1536                 }
1537         #endif
1538
1539         if (pause_game)
1540         {
1541                 start_time();
1542                 digi_resume_digi_sounds();
1543         }
1544
1545 #ifdef WINDOWS
1546         VR_screen_mode = dd_VR_screen_mode_save;
1547 #endif
1548
1549         Automap_active = 0;
1550 }
1551
1552 void adjust_segment_limit(int SegmentLimit)
1553 {
1554         int i,e1;
1555         Edge_info * e;
1556
1557         mprintf(( 0, "Seglimit: %d\n", SegmentLimit ));
1558         
1559         for (i=0; i<=Highest_edge_index; i++ )  {
1560                 e = &Edges[i];
1561                 e->flags |= EF_TOO_FAR;
1562                 for (e1=0; e1<e->num_faces; e1++ )      {
1563                         if ( Automap_visited[e->segnum[e1]] <= SegmentLimit )   {
1564                                 e->flags &= (~EF_TOO_FAR);
1565                                 break;
1566                         }
1567                 }
1568         }
1569         
1570 }
1571
1572 void draw_all_edges()   
1573 {
1574         g3s_codes cc;
1575         int i,j,nbright;
1576         ubyte nfacing,nnfacing;
1577         Edge_info *e;
1578         vms_vector *tv1;
1579         fix distance;
1580         fix min_distance = 0x7fffffff;
1581         g3s_point *p1, *p2;
1582         
1583         
1584         nbright=0;
1585
1586         for (i=0; i<=Highest_edge_index; i++ )  {
1587                 //e = &Edges[Edge_used_list[i]];
1588                 e = &Edges[i];
1589                 if (!(e->flags & EF_USED)) continue;
1590
1591                 if ( e->flags & EF_TOO_FAR) continue;
1592
1593                 if (e->flags&EF_FRONTIER)       {                                               // A line that is between what we have seen and what we haven't
1594                         if ( (!(e->flags&EF_SECRET))&&(e->color==Wall_normal_color))
1595                                 continue;               // If a line isn't secret and is normal color, then don't draw it
1596                 }
1597
1598                 cc=rotate_list(2,e->verts);
1599                 distance = Segment_points[e->verts[1]].p3_z;
1600
1601                 if (min_distance>distance )
1602                         min_distance = distance;
1603
1604                 if (!cc.and)    {       //all off screen?
1605                         nfacing = nnfacing = 0;
1606                         tv1 = &Vertices[e->verts[0]];
1607                         j = 0;
1608                         while( j<e->num_faces && (nfacing==0 || nnfacing==0) )  {
1609                                 #ifdef COMPACT_SEGS
1610                                 vms_vector temp_v;
1611                                 get_side_normal(&Segments[e->segnum[j]], e->sides[j], 0, &temp_v );
1612                                 if (!g3_check_normal_facing( tv1, &temp_v ) )
1613                                 #else
1614                                 if (!g3_check_normal_facing( tv1, &Segments[e->segnum[j]].sides[e->sides[j]].normals[0] ) )
1615                                 #endif
1616                                         nfacing++;
1617                                 else
1618                                         nnfacing++;
1619                                 j++;
1620                         }
1621
1622                         if ( nfacing && nnfacing )      {
1623                                 // a contour line
1624                                 DrawingListBright[nbright++] = e-Edges;
1625                         } else if ( e->flags&(EF_DEFINING|EF_GRATE) )   {
1626                                 if ( nfacing == 0 )     {
1627                                         if ( e->flags & EF_NO_FADE )
1628                                                 gr_setcolor( e->color );
1629                                         else
1630                                                 gr_setcolor( gr_fade_table[e->color+256*8] );
1631                                         g3_draw_line( &Segment_points[e->verts[0]], &Segment_points[e->verts[1]] );
1632                                 }       else {
1633                                         DrawingListBright[nbright++] = e-Edges;
1634                                 }
1635                         }
1636                 }
1637         }
1638                 
1639 ///     mprintf( (0, "Min distance=%.2f, ViewDist=%.2f, Delta=%.2f\n", f2fl(min_distance), f2fl(ViewDist), f2fl(min_distance)- f2fl(ViewDist) ));
1640
1641         if ( min_distance < 0 ) min_distance = 0;
1642
1643         // Sort the bright ones using a shell sort
1644         {
1645                 int t;
1646                 int i, j, incr, v1, v2;
1647         
1648                 incr = nbright / 2;
1649                 while( incr > 0 )       {
1650                         for (i=incr; i<nbright; i++ )   {
1651                                 j = i - incr;
1652                                 while (j>=0 )   {
1653                                         // compare element j and j+incr
1654                                         v1 = Edges[DrawingListBright[j]].verts[0];
1655                                         v2 = Edges[DrawingListBright[j+incr]].verts[0];
1656
1657                                         if (Segment_points[v1].p3_z < Segment_points[v2].p3_z) {
1658                                                 // If not in correct order, them swap 'em
1659                                                 t=DrawingListBright[j+incr];
1660                                                 DrawingListBright[j+incr]=DrawingListBright[j];
1661                                                 DrawingListBright[j]=t;
1662                                                 j -= incr;
1663                                         }
1664                                         else
1665                                                 break;
1666                                 }
1667                         }
1668                         incr = incr / 2;
1669                 }
1670         }
1671                                         
1672         // Draw the bright ones
1673         for (i=0; i<nbright; i++ )      {
1674                 int color;
1675                 fix dist;
1676                 e = &Edges[DrawingListBright[i]];
1677                 p1 = &Segment_points[e->verts[0]];
1678                 p2 = &Segment_points[e->verts[1]];
1679                 dist = p1->p3_z - min_distance;
1680                 // Make distance be 1.0 to 0.0, where 0.0 is 10 segments away;
1681                 if ( dist < 0 ) dist=0;
1682                 if ( dist >= Automap_farthest_dist ) continue;
1683
1684                 if ( e->flags & EF_NO_FADE )    {
1685                         gr_setcolor( e->color );
1686                 } else {
1687                         dist = F1_0 - fixdiv( dist, Automap_farthest_dist );
1688                         color = f2i( dist*31 );
1689                         gr_setcolor( gr_fade_table[e->color+color*256] );       
1690                 }
1691                 g3_draw_line( p1, p2 );
1692         }
1693
1694 }
1695
1696
1697 //==================================================================
1698 //
1699 // All routines below here are used to build the Edge list
1700 //
1701 //==================================================================
1702
1703
1704 //finds edge, filling in edge_ptr. if found old edge, returns index, else return -1
1705 static int automap_find_edge(int v0,int v1,Edge_info **edge_ptr)
1706 {
1707         long vv, evv;
1708         short hash,oldhash;
1709         int ret, ev0, ev1;
1710
1711         vv = (v1<<16) + v0;
1712
1713         oldhash = hash = ((v0*5+v1) % Max_edges);
1714
1715         ret = -1;
1716
1717         while (ret==-1) {
1718                 ev0 = (int)(Edges[hash].verts[0]);
1719                 ev1 = (int)(Edges[hash].verts[1]);
1720                 evv = (ev1<<16)+ev0;
1721                 if (Edges[hash].num_faces == 0 ) ret=0;
1722                 else if (evv == vv) ret=1;
1723                 else {
1724                         if (++hash==Max_edges) hash=0;
1725                         if (hash==oldhash) Error("Edge list full!");
1726                 }
1727         }
1728
1729         *edge_ptr = &Edges[hash];
1730
1731         if (ret == 0)
1732                 return -1;
1733         else
1734                 return hash;
1735
1736 }
1737
1738
1739 void add_one_edge( short va, short vb, ubyte color, ubyte side, short segnum, int hidden, int grate, int no_fade )      {
1740         int found;
1741         Edge_info *e;
1742         short tmp;
1743
1744         if ( Num_edges >= Max_edges)    {
1745                 // GET JOHN! (And tell him that his
1746                 // MAX_EDGES_FROM_VERTS formula is hosed.)
1747                 // If he's not around, save the mine,
1748                 // and send him  mail so he can look
1749                 // at the mine later. Don't modify it.
1750                 // This is important if this happens.
1751                 Int3();         // LOOK ABOVE!!!!!!
1752                 return;
1753         }
1754
1755         if ( va > vb )  {
1756                 tmp = va;
1757                 va = vb;
1758                 vb = tmp;
1759         }
1760
1761         found = automap_find_edge(va,vb,&e);
1762                 
1763         if (found == -1) {
1764                 e->verts[0] = va;
1765                 e->verts[1] = vb;
1766                 e->color = color;
1767                 e->num_faces = 1;
1768                 e->flags = EF_USED | EF_DEFINING;                       // Assume a normal line
1769                 e->sides[0] = side;
1770                 e->segnum[0] = segnum;
1771                 //Edge_used_list[Num_edges] = e-Edges;
1772                 if ( (e-Edges) > Highest_edge_index )
1773                         Highest_edge_index = e - Edges;
1774                 Num_edges++;
1775         } else {
1776                 //Assert(e->num_faces < 8 );
1777
1778                 if ( color != Wall_normal_color )
1779                         if (color != Wall_revealed_color)
1780                                 e->color = color;
1781
1782                 if ( e->num_faces < 4 ) {
1783                         e->sides[e->num_faces] = side;                                  
1784                         e->segnum[e->num_faces] = segnum;
1785                         e->num_faces++;
1786                 }
1787         }
1788
1789         if ( grate )
1790                 e->flags |= EF_GRATE;
1791
1792         if ( hidden )
1793                 e->flags|=EF_SECRET;            // Mark this as a hidden edge
1794         if ( no_fade )
1795                 e->flags |= EF_NO_FADE;
1796 }
1797
1798 void add_one_unknown_edge( short va, short vb ) {
1799         int found;
1800         Edge_info *e;
1801         short tmp;
1802
1803         if ( va > vb )  {
1804                 tmp = va;
1805                 va = vb;
1806                 vb = tmp;
1807         }
1808
1809         found = automap_find_edge(va,vb,&e);
1810         if (found != -1)        
1811                 e->flags|=EF_FRONTIER;          // Mark as a border edge
1812 }
1813
1814 #ifndef _GAMESEQ_H
1815 extern obj_position Player_init[];
1816 #endif
1817
1818 void add_segment_edges(segment *seg)
1819 {
1820         int     is_grate, no_fade;
1821         ubyte   color;
1822         int     sn;
1823         int     segnum = seg-Segments;
1824         int     hidden_flag;
1825         int ttype,trigger_num;
1826         
1827         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1828                 short   vertex_list[4];
1829
1830                 hidden_flag = 0;
1831
1832                 is_grate = 0;
1833                 no_fade = 0;
1834
1835                 color = 255;
1836                 if (seg->children[sn] == -1) {
1837                         color = Wall_normal_color;
1838                 }
1839
1840                 switch( Segment2s[segnum].special )     {
1841                 case SEGMENT_IS_FUELCEN:
1842                         color = BM_XRGB( 29, 27, 13 );
1843                         break;
1844                 case SEGMENT_IS_CONTROLCEN:
1845                         if (Control_center_present)
1846                                 color = BM_XRGB( 29, 0, 0 );
1847                         break;
1848                 case SEGMENT_IS_ROBOTMAKER:
1849                         color = BM_XRGB( 29, 0, 31 );
1850                         break;
1851                 }
1852
1853                 if (seg->sides[sn].wall_num > -1)       {
1854                 
1855                         trigger_num = Walls[seg->sides[sn].wall_num].trigger;
1856                         ttype = Triggers[trigger_num].type;
1857                         if (ttype==TT_SECRET_EXIT)
1858                                 {
1859                             color = BM_XRGB( 29, 0, 31 );
1860                                  no_fade=1;
1861                                  goto Here;
1862                                 }       
1863
1864                         switch( Walls[seg->sides[sn].wall_num].type )   {
1865                         case WALL_DOOR:
1866                                 if (Walls[seg->sides[sn].wall_num].keys == KEY_BLUE) {
1867                                         no_fade = 1;
1868                                         color = Wall_door_blue;
1869                                         //mprintf((0, "Seg %i, side %i has BLUE wall\n", segnum, sn));
1870                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_GOLD) {
1871                                         no_fade = 1;
1872                                         color = Wall_door_gold;
1873                                         //mprintf((0, "Seg %i, side %i has GOLD wall\n", segnum, sn));
1874                                 } else if (Walls[seg->sides[sn].wall_num].keys == KEY_RED) {
1875                                         no_fade = 1;
1876                                         color = Wall_door_red;
1877                                         //mprintf((0, "Seg %i, side %i has RED wall\n", segnum, sn));
1878                                 } else if (!(WallAnims[Walls[seg->sides[sn].wall_num].clip_num].flags & WCF_HIDDEN)) {
1879                                         int     connected_seg = seg->children[sn];
1880                                         if (connected_seg != -1) {
1881                                                 int connected_side = find_connect_side(seg, &Segments[connected_seg]);
1882                                                 int     keytype = Walls[Segments[connected_seg].sides[connected_side].wall_num].keys;
1883                                                 if ((keytype != KEY_BLUE) && (keytype != KEY_GOLD) && (keytype != KEY_RED))
1884                                                         color = Wall_door_color;
1885                                                 else {
1886                                                         switch (Walls[Segments[connected_seg].sides[connected_side].wall_num].keys) {
1887                                                                 case KEY_BLUE:  color = Wall_door_blue; no_fade = 1; break;
1888                                                                 case KEY_GOLD:  color = Wall_door_gold; no_fade = 1; break;
1889                                                                 case KEY_RED:   color = Wall_door_red;  no_fade = 1; break;
1890                                                                 default:        Error("Inconsistent data.  Supposed to be a colored wall, but not blue, gold or red.\n");
1891                                                         }
1892                                                         //mprintf((0, "Seg %i, side %i has a colored door on the other side.\n", segnum, sn));
1893                                                 }
1894
1895                                         }
1896                                 } else {
1897                                         color = Wall_normal_color;
1898                                         hidden_flag = 1;
1899                                         //mprintf((0, "Wall at seg:side %i:%i is hidden.\n", seg-Segments, sn));
1900                                 }
1901                                 break;
1902                         case WALL_CLOSED:
1903                                 // Make grates draw properly
1904                                 if (WALL_IS_DOORWAY(seg,sn) & WID_RENDPAST_FLAG)
1905                                         is_grate = 1;
1906                                 else
1907                                         hidden_flag = 1;
1908                                 color = Wall_normal_color;
1909                                 break;
1910                         case WALL_BLASTABLE:
1911                                 // Hostage doors
1912                                 color = Wall_door_color;        
1913                                 break;
1914                         }
1915                 }
1916         
1917                 if (segnum==Player_init[Player_num].segnum)
1918                         color = BM_XRGB(31,0,31);
1919
1920                 if ( color != 255 )     {
1921                         // If they have a map powerup, draw unvisited areas in dark blue.
1922                         if (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL && (!Automap_visited[segnum]))     
1923                                 color = Wall_revealed_color;
1924
1925
1926                         Here:
1927
1928                         get_side_verts(vertex_list,segnum,sn);
1929                         add_one_edge( vertex_list[0], vertex_list[1], color, sn, segnum, hidden_flag, 0, no_fade );
1930                         add_one_edge( vertex_list[1], vertex_list[2], color, sn, segnum, hidden_flag, 0, no_fade );
1931                         add_one_edge( vertex_list[2], vertex_list[3], color, sn, segnum, hidden_flag, 0, no_fade );
1932                         add_one_edge( vertex_list[3], vertex_list[0], color, sn, segnum, hidden_flag, 0, no_fade );
1933
1934                         if ( is_grate ) {
1935                                 add_one_edge( vertex_list[0], vertex_list[2], color, sn, segnum, hidden_flag, 1, no_fade );
1936                                 add_one_edge( vertex_list[1], vertex_list[3], color, sn, segnum, hidden_flag, 1, no_fade );
1937                         }
1938                 }
1939         }
1940
1941 }
1942
1943
1944 // Adds all the edges from a segment we haven't visited yet.
1945
1946 void add_unknown_segment_edges(segment *seg)
1947 {
1948         int sn;
1949         int segnum = seg-Segments;
1950         
1951         for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++) {
1952                 short   vertex_list[4];
1953
1954                 // Only add edges that have no children
1955                 if (seg->children[sn] == -1) {
1956                         get_side_verts(vertex_list,segnum,sn);
1957         
1958                         add_one_unknown_edge( vertex_list[0], vertex_list[1] );
1959                         add_one_unknown_edge( vertex_list[1], vertex_list[2] );
1960                         add_one_unknown_edge( vertex_list[2], vertex_list[3] );
1961                         add_one_unknown_edge( vertex_list[3], vertex_list[0] );
1962                 }
1963
1964
1965         }
1966
1967 }
1968
1969 void automap_build_edge_list()
1970 {       
1971         int     i,e1,e2,s;
1972         Edge_info * e;
1973
1974         Automap_cheat = 0;
1975
1976         if ( Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL_CHEAT )
1977                 Automap_cheat = 1;              // Damn cheaters...
1978
1979         // clear edge list
1980         for (i=0; i<Max_edges; i++) {
1981                 Edges[i].num_faces = 0;
1982                 Edges[i].flags = 0;
1983         }
1984         Num_edges = 0;
1985         Highest_edge_index = -1;
1986
1987         if (Automap_cheat || (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL) )       {
1988                 // Cheating, add all edges as visited
1989                 for (s=0; s<=Highest_segment_index; s++)
1990                         #ifdef EDITOR
1991                         if (Segments[s].segnum != -1)
1992                         #endif
1993                         {
1994                                 add_segment_edges(&Segments[s]);
1995                         }
1996         } else {
1997                 // Not cheating, add visited edges, and then unvisited edges
1998                 for (s=0; s<=Highest_segment_index; s++)
1999                         #ifdef EDITOR
2000                         if (Segments[s].segnum != -1)
2001                         #endif
2002                                 if (Automap_visited[s]) {
2003                                         add_segment_edges(&Segments[s]);
2004                                 }
2005         
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_unknown_segment_edges(&Segments[s]);
2012                                 }
2013         }
2014
2015         // Find unnecessary lines (These are lines that don't have to be drawn because they have small curvature)
2016         for (i=0; i<=Highest_edge_index; i++ )  {
2017                 e = &Edges[i];
2018                 if (!(e->flags&EF_USED)) continue;
2019
2020                 for (e1=0; e1<e->num_faces; e1++ )      {
2021                         for (e2=1; e2<e->num_faces; e2++ )      {
2022                                 if ( (e1 != e2) && (e->segnum[e1] != e->segnum[e2]) )   {
2023                                         #ifdef COMPACT_SEGS
2024                                         vms_vector v1, v2;
2025                                         get_side_normal(&Segments[e->segnum[e1]], e->sides[e1], 0, &v1 );
2026                                         get_side_normal(&Segments[e->segnum[e2]], e->sides[e2], 0, &v2 );
2027                                         if ( vm_vec_dot(&v1,&v2) > (F1_0-(F1_0/10))  )  {
2028                                         #else
2029                                         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))  )       {
2030                                         #endif
2031                                                 e->flags &= (~EF_DEFINING);
2032                                                 break;
2033                                         }
2034                                 }
2035                         }
2036                         if (!(e->flags & EF_DEFINING))
2037                                 break;
2038                 }
2039         }       
2040
2041         mprintf( (0, "Automap used %d / %d edges\n", Num_edges, Max_edges  ));
2042
2043 }
2044
2045 char Marker_input [40];
2046 int Marker_index=0;
2047 ubyte DefiningMarkerMessage=0;
2048 ubyte MarkerBeingDefined;
2049 ubyte LastMarkerDropped;
2050
2051 void InitMarkerInput ()
2052  {
2053         int maxdrop,i;
2054
2055         //find free marker slot
2056
2057         if (Game_mode & GM_MULTI)
2058         maxdrop=MAX_DROP_MULTI;
2059         else
2060         maxdrop=MAX_DROP_SINGLE;
2061
2062         for (i=0;i<maxdrop;i++)
2063                 if (MarkerObject[(Player_num*2)+i] == -1)               //found free slot!
2064                         break;
2065
2066         if (i==maxdrop)         //no free slot
2067         {
2068                 if (Game_mode & GM_MULTI)
2069                         i = !LastMarkerDropped;         //in multi, replace older of two
2070                 else {
2071                         HUD_init_message("No free marker slots");
2072                         return;
2073                 }
2074         }
2075
2076         //got a free slot.  start inputting marker message
2077
2078         Marker_input[0]=0;
2079         Marker_index=0;
2080         DefiningMarkerMessage=1;
2081         MarkerBeingDefined = i;
2082  }
2083
2084 void MarkerInputMessage (int key)
2085  {
2086         switch( key )   {
2087         case KEY_F8:
2088         case KEY_ESC:
2089                 DefiningMarkerMessage = 0;
2090                 game_flush_inputs();
2091                 break;
2092         case KEY_LEFT:
2093         case KEY_BACKSP:
2094         case KEY_PAD4:
2095                 if (Marker_index > 0)
2096                         Marker_index--;
2097                 Marker_input[Marker_index] = 0;
2098                 break;
2099         case KEY_ENTER:
2100                 strcpy (MarkerMessage[(Player_num*2)+MarkerBeingDefined],Marker_input);
2101                 if (Game_mode & GM_MULTI)
2102                  strcpy (MarkerOwner[(Player_num*2)+MarkerBeingDefined],Players[Player_num].callsign);
2103                 DropMarker(MarkerBeingDefined);
2104                 LastMarkerDropped = MarkerBeingDefined;
2105                 game_flush_inputs();
2106                 DefiningMarkerMessage = 0;
2107                 break;
2108         default:
2109                 if ( key > 0 )
2110                  {
2111                   int ascii = key_to_ascii(key);
2112                   if ((ascii < 255 ))
2113                     if (Marker_index < 38 )
2114                       {
2115                         Marker_input[Marker_index++] = ascii;
2116                         Marker_input[Marker_index] = 0;
2117                       }
2118                  }
2119                 break;
2120
2121         }
2122  }
2123
2124
2125
2126