1 //
2 // Copyright(C) 1993-1996 Id Software, Inc.
3 // Copyright(C) 2005-2014 Simon Howard
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 //
16 // DESCRIPTION:  the automap code
17 //
18 
19 
20 #include <stdio.h>
21 
22 #include "deh_main.h"
23 
24 #include "z_zone.h"
25 #include "doomkeys.h"
26 #include "doomdef.h"
27 #include "st_stuff.h"
28 #include "p_local.h"
29 #include "w_wad.h"
30 
31 #include "m_cheat.h"
32 #include "m_controls.h"
33 #include "m_misc.h"
34 #include "i_system.h"
35 #include "i_timer.h"
36 #include "i_video.h"
37 
38 // Needs access to LFB.
39 #include "v_video.h"
40 
41 // State.
42 #include "doomstat.h"
43 #include "r_state.h"
44 
45 // Data.
46 #include "dstrings.h"
47 
48 #include "am_map.h"
49 
50 
51 // For use if I do walls with outsides/insides
52 #define REDS		(256-5*16)
53 #define REDRANGE	16
54 #define BLUES		(256-4*16+8)
55 #define BLUERANGE	8
56 #define GREENS		(7*16)
57 #define GREENRANGE	16
58 #define GRAYS		(6*16)
59 #define GRAYSRANGE	16
60 #define BROWNS		(4*16)
61 #define BROWNRANGE	16
62 #define YELLOWS		(256-32+7)
63 #define YELLOWRANGE	1
64 #define BLACK		0
65 #define WHITE		(256-47)
66 
67 // Automap colors
68 #define BACKGROUND	BLACK
69 #define YOURCOLORS	WHITE
70 #define YOURRANGE	0
71 #define WALLCOLORS	REDS
72 #define WALLRANGE	REDRANGE
73 #define TSWALLCOLORS	GRAYS
74 #define TSWALLRANGE	GRAYSRANGE
75 #define FDWALLCOLORS	BROWNS
76 #define FDWALLRANGE	BROWNRANGE
77 #define CDWALLCOLORS	YELLOWS
78 #define CDWALLRANGE	YELLOWRANGE
79 #define THINGCOLORS	GREENS
80 #define THINGRANGE	GREENRANGE
81 #define SECRETWALLCOLORS WALLCOLORS
82 #define SECRETWALLRANGE WALLRANGE
83 #define GRIDCOLORS	(GRAYS + GRAYSRANGE/2)
84 #define GRIDRANGE	0
85 #define XHAIRCOLORS	GRAYS
86 
87 // drawing stuff
88 
89 #define AM_NUMMARKPOINTS 10
90 
91 // scale on entry
92 #define INITSCALEMTOF (.2*FRACUNIT)
93 // how much the automap moves window per tic in frame-buffer coordinates
94 // moves 140 pixels in 1 second
95 #define F_PANINC	4
96 // how much zoom-in per tic
97 // goes to 2x in 1 second
98 #define M_ZOOMIN        ((int) (1.02*FRACUNIT))
99 // how much zoom-out per tic
100 // pulls out to 0.5x in 1 second
101 #define M_ZOOMOUT       ((int) (FRACUNIT/1.02))
102 
103 // translates between frame-buffer and map distances
104 #define FTOM(x) FixedMul(((x)<<FRACBITS),scale_ftom)
105 #define MTOF(x) (FixedMul((x),scale_mtof)>>FRACBITS)
106 // translates between frame-buffer and map coordinates
107 #define CXMTOF(x)  (f_x + MTOF((x)-m_x))
108 #define CYMTOF(y)  (f_y + (f_h - MTOF((y)-m_y)))
109 
110 // the following is crap
111 #define LINE_NEVERSEE ML_DONTDRAW
112 
113 typedef struct
114 {
115     int x, y;
116 } fpoint_t;
117 
118 typedef struct
119 {
120     fpoint_t a, b;
121 } fline_t;
122 
123 typedef struct
124 {
125     fixed_t		x,y;
126 } mpoint_t;
127 
128 typedef struct
129 {
130     mpoint_t a, b;
131 } mline_t;
132 
133 typedef struct
134 {
135     fixed_t slp, islp;
136 } islope_t;
137 
138 
139 
140 //
141 // The vector graphics for the automap.
142 //  A line drawing of the player pointing right,
143 //   starting from the middle.
144 //
145 #define R ((8*PLAYERRADIUS)/7)
146 mline_t player_arrow[] = {
147     { { -R+R/8, 0 }, { R, 0 } }, // -----
148     { { R, 0 }, { R-R/2, R/4 } },  // ----->
149     { { R, 0 }, { R-R/2, -R/4 } },
150     { { -R+R/8, 0 }, { -R-R/8, R/4 } }, // >---->
151     { { -R+R/8, 0 }, { -R-R/8, -R/4 } },
152     { { -R+3*R/8, 0 }, { -R+R/8, R/4 } }, // >>--->
153     { { -R+3*R/8, 0 }, { -R+R/8, -R/4 } }
154 };
155 #undef R
156 
157 #define R ((8*PLAYERRADIUS)/7)
158 mline_t cheat_player_arrow[] = {
159     { { -R+R/8, 0 }, { R, 0 } }, // -----
160     { { R, 0 }, { R-R/2, R/6 } },  // ----->
161     { { R, 0 }, { R-R/2, -R/6 } },
162     { { -R+R/8, 0 }, { -R-R/8, R/6 } }, // >----->
163     { { -R+R/8, 0 }, { -R-R/8, -R/6 } },
164     { { -R+3*R/8, 0 }, { -R+R/8, R/6 } }, // >>----->
165     { { -R+3*R/8, 0 }, { -R+R/8, -R/6 } },
166     { { -R/2, 0 }, { -R/2, -R/6 } }, // >>-d--->
167     { { -R/2, -R/6 }, { -R/2+R/6, -R/6 } },
168     { { -R/2+R/6, -R/6 }, { -R/2+R/6, R/4 } },
169     { { -R/6, 0 }, { -R/6, -R/6 } }, // >>-dd-->
170     { { -R/6, -R/6 }, { 0, -R/6 } },
171     { { 0, -R/6 }, { 0, R/4 } },
172     { { R/6, R/4 }, { R/6, -R/7 } }, // >>-ddt->
173     { { R/6, -R/7 }, { R/6+R/32, -R/7-R/32 } },
174     { { R/6+R/32, -R/7-R/32 }, { R/6+R/10, -R/7 } }
175 };
176 #undef R
177 
178 #define R (FRACUNIT)
179 mline_t triangle_guy[] = {
180     { { (fixed_t)(-.867*R), (fixed_t)(-.5*R) }, { (fixed_t)(.867*R ), (fixed_t)(-.5*R) } },
181     { { (fixed_t)(.867*R ), (fixed_t)(-.5*R) }, { (fixed_t)(0      ), (fixed_t)(R    ) } },
182     { { (fixed_t)(0      ), (fixed_t)(R    ) }, { (fixed_t)(-.867*R), (fixed_t)(-.5*R) } }
183 };
184 #undef R
185 
186 #define R (FRACUNIT)
187 mline_t thintriangle_guy[] = {
188     { { (fixed_t)(-.5*R), (fixed_t)(-.7*R) }, { (fixed_t)(R    ), (fixed_t)(0    ) } },
189     { { (fixed_t)(R    ), (fixed_t)(0    ) }, { (fixed_t)(-.5*R), (fixed_t)(.7*R ) } },
190     { { (fixed_t)(-.5*R), (fixed_t)(.7*R ) }, { (fixed_t)(-.5*R), (fixed_t)(-.7*R) } }
191 };
192 #undef R
193 
194 
195 
196 
197 static int 	cheating = 0;
198 static int 	grid = 0;
199 
200 static int 	leveljuststarted = 1; 	// kluge until AM_LevelInit() is called
201 
202 boolean    	automapactive = false;
203 static int 	finit_width = SCREENWIDTH;
204 static int 	finit_height = SCREENHEIGHT - ST_HEIGHT;
205 
206 // location of window on screen
207 static int 	f_x;
208 static int	f_y;
209 
210 // size of window on screen
211 static int 	f_w;
212 static int	f_h;
213 
214 static int 	lightlev; 		// used for funky strobing effect
215 static pixel_t*	fb; 			// pseudo-frame buffer
216 static int 	amclock;
217 
218 static mpoint_t m_paninc; // how far the window pans each tic (map coords)
219 static fixed_t 	mtof_zoommul; // how far the window zooms in each tic (map coords)
220 static fixed_t 	ftom_zoommul; // how far the window zooms in each tic (fb coords)
221 
222 static fixed_t 	m_x, m_y;   // LL x,y where the window is on the map (map coords)
223 static fixed_t 	m_x2, m_y2; // UR x,y where the window is on the map (map coords)
224 
225 //
226 // width/height of window on map (map coords)
227 //
228 static fixed_t 	m_w;
229 static fixed_t	m_h;
230 
231 // based on level size
232 static fixed_t 	min_x;
233 static fixed_t	min_y;
234 static fixed_t 	max_x;
235 static fixed_t  max_y;
236 
237 static fixed_t 	max_w; // max_x-min_x,
238 static fixed_t  max_h; // max_y-min_y
239 
240 // based on player size
241 static fixed_t 	min_w;
242 static fixed_t  min_h;
243 
244 
245 static fixed_t 	min_scale_mtof; // used to tell when to stop zooming out
246 static fixed_t 	max_scale_mtof; // used to tell when to stop zooming in
247 
248 // old stuff for recovery later
249 static fixed_t old_m_w, old_m_h;
250 static fixed_t old_m_x, old_m_y;
251 
252 // old location used by the Follower routine
253 static mpoint_t f_oldloc;
254 
255 // used by MTOF to scale from map-to-frame-buffer coords
256 static fixed_t scale_mtof = (fixed_t)INITSCALEMTOF;
257 // used by FTOM to scale from frame-buffer-to-map coords (=1/scale_mtof)
258 static fixed_t scale_ftom;
259 
260 static player_t *plr; // the player represented by an arrow
261 
262 static patch_t *marknums[10]; // numbers used for marking by the automap
263 static mpoint_t markpoints[AM_NUMMARKPOINTS]; // where the points are
264 static int markpointnum = 0; // next point to be assigned
265 
266 static int followplayer = 1; // specifies whether to follow the player around
267 
268 cheatseq_t cheat_amap = CHEAT("iddt", 0);
269 
270 static boolean stopped = true;
271 
272 // Calculates the slope and slope according to the x-axis of a line
273 // segment in map coordinates (with the upright y-axis n' all) so
274 // that it can be used with the brain-dead drawing stuff.
275 
276 void
AM_getIslope(mline_t * ml,islope_t * is)277 AM_getIslope
278 ( mline_t*	ml,
279   islope_t*	is )
280 {
281     int dx, dy;
282 
283     dy = ml->a.y - ml->b.y;
284     dx = ml->b.x - ml->a.x;
285     if (!dy) is->islp = (dx<0?-INT_MAX:INT_MAX);
286     else is->islp = FixedDiv(dx, dy);
287     if (!dx) is->slp = (dy<0?-INT_MAX:INT_MAX);
288     else is->slp = FixedDiv(dy, dx);
289 
290 }
291 
292 //
293 //
294 //
AM_activateNewScale(void)295 void AM_activateNewScale(void)
296 {
297     m_x += m_w/2;
298     m_y += m_h/2;
299     m_w = FTOM(f_w);
300     m_h = FTOM(f_h);
301     m_x -= m_w/2;
302     m_y -= m_h/2;
303     m_x2 = m_x + m_w;
304     m_y2 = m_y + m_h;
305 }
306 
307 //
308 //
309 //
AM_saveScaleAndLoc(void)310 void AM_saveScaleAndLoc(void)
311 {
312     old_m_x = m_x;
313     old_m_y = m_y;
314     old_m_w = m_w;
315     old_m_h = m_h;
316 }
317 
318 //
319 //
320 //
AM_restoreScaleAndLoc(void)321 void AM_restoreScaleAndLoc(void)
322 {
323 
324     m_w = old_m_w;
325     m_h = old_m_h;
326     if (!followplayer)
327     {
328 	m_x = old_m_x;
329 	m_y = old_m_y;
330     } else {
331 	m_x = plr->mo->x - m_w/2;
332 	m_y = plr->mo->y - m_h/2;
333     }
334     m_x2 = m_x + m_w;
335     m_y2 = m_y + m_h;
336 
337     // Change the scaling multipliers
338     scale_mtof = FixedDiv(f_w<<FRACBITS, m_w);
339     scale_ftom = FixedDiv(FRACUNIT, scale_mtof);
340 }
341 
342 //
343 // adds a marker at the current location
344 //
AM_addMark(void)345 void AM_addMark(void)
346 {
347     markpoints[markpointnum].x = m_x + m_w/2;
348     markpoints[markpointnum].y = m_y + m_h/2;
349     markpointnum = (markpointnum + 1) % AM_NUMMARKPOINTS;
350 
351 }
352 
353 //
354 // Determines bounding box of all vertices,
355 // sets global variables controlling zoom range.
356 //
AM_findMinMaxBoundaries(void)357 void AM_findMinMaxBoundaries(void)
358 {
359     int i;
360     fixed_t a;
361     fixed_t b;
362 
363     min_x = min_y =  INT_MAX;
364     max_x = max_y = -INT_MAX;
365 
366     for (i=0;i<numvertexes;i++)
367     {
368 	if (vertexes[i].x < min_x)
369 	    min_x = vertexes[i].x;
370 	else if (vertexes[i].x > max_x)
371 	    max_x = vertexes[i].x;
372 
373 	if (vertexes[i].y < min_y)
374 	    min_y = vertexes[i].y;
375 	else if (vertexes[i].y > max_y)
376 	    max_y = vertexes[i].y;
377     }
378 
379     max_w = max_x - min_x;
380     max_h = max_y - min_y;
381 
382     min_w = 2*PLAYERRADIUS; // const? never changed?
383     min_h = 2*PLAYERRADIUS;
384 
385     a = FixedDiv(f_w<<FRACBITS, max_w);
386     b = FixedDiv(f_h<<FRACBITS, max_h);
387 
388     min_scale_mtof = a < b ? a : b;
389     max_scale_mtof = FixedDiv(f_h<<FRACBITS, 2*PLAYERRADIUS);
390 
391 }
392 
393 
394 //
395 //
396 //
AM_changeWindowLoc(void)397 void AM_changeWindowLoc(void)
398 {
399     if (m_paninc.x || m_paninc.y)
400     {
401 	followplayer = 0;
402 	f_oldloc.x = INT_MAX;
403     }
404 
405     m_x += m_paninc.x;
406     m_y += m_paninc.y;
407 
408     if (m_x + m_w/2 > max_x)
409 	m_x = max_x - m_w/2;
410     else if (m_x + m_w/2 < min_x)
411 	m_x = min_x - m_w/2;
412 
413     if (m_y + m_h/2 > max_y)
414 	m_y = max_y - m_h/2;
415     else if (m_y + m_h/2 < min_y)
416 	m_y = min_y - m_h/2;
417 
418     m_x2 = m_x + m_w;
419     m_y2 = m_y + m_h;
420 }
421 
422 
423 //
424 //
425 //
AM_initVariables(void)426 void AM_initVariables(void)
427 {
428     int pnum;
429     static event_t st_notify = { ev_keyup, AM_MSGENTERED, 0, 0 };
430 
431     automapactive = true;
432     fb = I_VideoBuffer;
433 
434     f_oldloc.x = INT_MAX;
435     amclock = 0;
436     lightlev = 0;
437 
438     m_paninc.x = m_paninc.y = 0;
439     ftom_zoommul = FRACUNIT;
440     mtof_zoommul = FRACUNIT;
441 
442     m_w = FTOM(f_w);
443     m_h = FTOM(f_h);
444 
445     // find player to center on initially
446     if (playeringame[consoleplayer])
447     {
448         plr = &players[consoleplayer];
449     }
450     else
451     {
452         plr = &players[0];
453 
454 	for (pnum=0;pnum<MAXPLAYERS;pnum++)
455         {
456 	    if (playeringame[pnum])
457             {
458                 plr = &players[pnum];
459 		break;
460             }
461         }
462     }
463 
464     m_x = plr->mo->x - m_w/2;
465     m_y = plr->mo->y - m_h/2;
466     AM_changeWindowLoc();
467 
468     // for saving & restoring
469     old_m_x = m_x;
470     old_m_y = m_y;
471     old_m_w = m_w;
472     old_m_h = m_h;
473 
474     // inform the status bar of the change
475     ST_Responder(&st_notify);
476 
477 }
478 
479 //
480 //
481 //
AM_loadPics(void)482 void AM_loadPics(void)
483 {
484     int i;
485     char namebuf[9];
486 
487     for (i=0;i<10;i++)
488     {
489 	DEH_snprintf(namebuf, 9, "AMMNUM%d", i);
490 	marknums[i] = W_CacheLumpName(namebuf, PU_STATIC);
491     }
492 
493 }
494 
AM_unloadPics(void)495 void AM_unloadPics(void)
496 {
497     int i;
498     char namebuf[9];
499 
500     for (i=0;i<10;i++)
501     {
502 	DEH_snprintf(namebuf, 9, "AMMNUM%d", i);
503 	W_ReleaseLumpName(namebuf);
504     }
505 }
506 
AM_clearMarks(void)507 void AM_clearMarks(void)
508 {
509     int i;
510 
511     for (i=0;i<AM_NUMMARKPOINTS;i++)
512 	markpoints[i].x = -1; // means empty
513     markpointnum = 0;
514 }
515 
516 //
517 // should be called at the start of every level
518 // right now, i figure it out myself
519 //
AM_LevelInit(void)520 void AM_LevelInit(void)
521 {
522     leveljuststarted = 0;
523 
524     f_x = f_y = 0;
525     f_w = finit_width;
526     f_h = finit_height;
527 
528     AM_clearMarks();
529 
530     AM_findMinMaxBoundaries();
531     scale_mtof = FixedDiv(min_scale_mtof, (int) (0.7*FRACUNIT));
532     if (scale_mtof > max_scale_mtof)
533 	scale_mtof = min_scale_mtof;
534     scale_ftom = FixedDiv(FRACUNIT, scale_mtof);
535 }
536 
537 
538 
539 
540 //
541 //
542 //
AM_Stop(void)543 void AM_Stop (void)
544 {
545     static event_t st_notify = { 0, ev_keyup, AM_MSGEXITED, 0 };
546 
547     AM_unloadPics();
548     automapactive = false;
549     ST_Responder(&st_notify);
550     stopped = true;
551 }
552 
553 //
554 //
555 //
AM_Start(void)556 void AM_Start (void)
557 {
558     static int lastlevel = -1, lastepisode = -1;
559 
560     if (!stopped) AM_Stop();
561     stopped = false;
562     if (lastlevel != gamemap || lastepisode != gameepisode)
563     {
564 	AM_LevelInit();
565 	lastlevel = gamemap;
566 	lastepisode = gameepisode;
567     }
568     AM_initVariables();
569     AM_loadPics();
570 }
571 
572 //
573 // set the window scale to the maximum size
574 //
AM_minOutWindowScale(void)575 void AM_minOutWindowScale(void)
576 {
577     scale_mtof = min_scale_mtof;
578     scale_ftom = FixedDiv(FRACUNIT, scale_mtof);
579     AM_activateNewScale();
580 }
581 
582 //
583 // set the window scale to the minimum size
584 //
AM_maxOutWindowScale(void)585 void AM_maxOutWindowScale(void)
586 {
587     scale_mtof = max_scale_mtof;
588     scale_ftom = FixedDiv(FRACUNIT, scale_mtof);
589     AM_activateNewScale();
590 }
591 
592 
593 //
594 // Handle events (user inputs) in automap mode
595 //
596 boolean
AM_Responder(event_t * ev)597 AM_Responder
598 ( event_t*	ev )
599 {
600 
601     int rc;
602     static int bigstate=0;
603     static char buffer[20];
604     int key;
605 
606     rc = false;
607 
608     if (ev->type == ev_joystick && joybautomap >= 0
609         && (ev->data1 & (1 << joybautomap)) != 0)
610     {
611         joywait = I_GetTime() + 5;
612 
613         if (!automapactive)
614         {
615             AM_Start ();
616             viewactive = false;
617         }
618         else
619         {
620             bigstate = 0;
621             viewactive = true;
622             AM_Stop ();
623         }
624 
625         return true;
626     }
627 
628     if (!automapactive)
629     {
630 	if (ev->type == ev_keydown && ev->data1 == key_map_toggle)
631 	{
632 	    AM_Start ();
633 	    viewactive = false;
634 	    rc = true;
635 	}
636     }
637     else if (ev->type == ev_keydown)
638     {
639 	rc = true;
640         key = ev->data1;
641 
642         if (key == key_map_east)          // pan right
643         {
644             if (!followplayer) m_paninc.x = FTOM(F_PANINC);
645             else rc = false;
646         }
647         else if (key == key_map_west)     // pan left
648         {
649             if (!followplayer) m_paninc.x = -FTOM(F_PANINC);
650             else rc = false;
651         }
652         else if (key == key_map_north)    // pan up
653         {
654             if (!followplayer) m_paninc.y = FTOM(F_PANINC);
655             else rc = false;
656         }
657         else if (key == key_map_south)    // pan down
658         {
659             if (!followplayer) m_paninc.y = -FTOM(F_PANINC);
660             else rc = false;
661         }
662         else if (key == key_map_zoomout)  // zoom out
663         {
664             mtof_zoommul = M_ZOOMOUT;
665             ftom_zoommul = M_ZOOMIN;
666         }
667         else if (key == key_map_zoomin)   // zoom in
668         {
669             mtof_zoommul = M_ZOOMIN;
670             ftom_zoommul = M_ZOOMOUT;
671         }
672         else if (key == key_map_toggle)
673         {
674             bigstate = 0;
675             viewactive = true;
676             AM_Stop ();
677         }
678         else if (key == key_map_maxzoom)
679         {
680             bigstate = !bigstate;
681             if (bigstate)
682             {
683                 AM_saveScaleAndLoc();
684                 AM_minOutWindowScale();
685             }
686             else AM_restoreScaleAndLoc();
687         }
688         else if (key == key_map_follow)
689         {
690             followplayer = !followplayer;
691             f_oldloc.x = INT_MAX;
692             if (followplayer)
693                 plr->message = DEH_String(AMSTR_FOLLOWON);
694             else
695                 plr->message = DEH_String(AMSTR_FOLLOWOFF);
696         }
697         else if (key == key_map_grid)
698         {
699             grid = !grid;
700             if (grid)
701                 plr->message = DEH_String(AMSTR_GRIDON);
702             else
703                 plr->message = DEH_String(AMSTR_GRIDOFF);
704         }
705         else if (key == key_map_mark)
706         {
707             M_snprintf(buffer, sizeof(buffer), "%s %d",
708                        DEH_String(AMSTR_MARKEDSPOT), markpointnum);
709             plr->message = buffer;
710             AM_addMark();
711         }
712         else if (key == key_map_clearmark)
713         {
714             AM_clearMarks();
715             plr->message = DEH_String(AMSTR_MARKSCLEARED);
716         }
717         else
718         {
719             rc = false;
720         }
721 
722         if ((!deathmatch || gameversion <= exe_doom_1_8)
723          && cht_CheckCheat(&cheat_amap, ev->data2))
724         {
725             rc = false;
726             cheating = (cheating + 1) % 3;
727         }
728     }
729     else if (ev->type == ev_keyup)
730     {
731         rc = false;
732         key = ev->data1;
733 
734         if (key == key_map_east)
735         {
736             if (!followplayer) m_paninc.x = 0;
737         }
738         else if (key == key_map_west)
739         {
740             if (!followplayer) m_paninc.x = 0;
741         }
742         else if (key == key_map_north)
743         {
744             if (!followplayer) m_paninc.y = 0;
745         }
746         else if (key == key_map_south)
747         {
748             if (!followplayer) m_paninc.y = 0;
749         }
750         else if (key == key_map_zoomout || key == key_map_zoomin)
751         {
752             mtof_zoommul = FRACUNIT;
753             ftom_zoommul = FRACUNIT;
754         }
755     }
756 
757     return rc;
758 
759 }
760 
761 
762 //
763 // Zooming
764 //
AM_changeWindowScale(void)765 void AM_changeWindowScale(void)
766 {
767 
768     // Change the scaling multipliers
769     scale_mtof = FixedMul(scale_mtof, mtof_zoommul);
770     scale_ftom = FixedDiv(FRACUNIT, scale_mtof);
771 
772     if (scale_mtof < min_scale_mtof)
773 	AM_minOutWindowScale();
774     else if (scale_mtof > max_scale_mtof)
775 	AM_maxOutWindowScale();
776     else
777 	AM_activateNewScale();
778 }
779 
780 
781 //
782 //
783 //
AM_doFollowPlayer(void)784 void AM_doFollowPlayer(void)
785 {
786 
787     if (f_oldloc.x != plr->mo->x || f_oldloc.y != plr->mo->y)
788     {
789 	m_x = FTOM(MTOF(plr->mo->x)) - m_w/2;
790 	m_y = FTOM(MTOF(plr->mo->y)) - m_h/2;
791 	m_x2 = m_x + m_w;
792 	m_y2 = m_y + m_h;
793 	f_oldloc.x = plr->mo->x;
794 	f_oldloc.y = plr->mo->y;
795 
796 	//  m_x = FTOM(MTOF(plr->mo->x - m_w/2));
797 	//  m_y = FTOM(MTOF(plr->mo->y - m_h/2));
798 	//  m_x = plr->mo->x - m_w/2;
799 	//  m_y = plr->mo->y - m_h/2;
800 
801     }
802 
803 }
804 
805 //
806 //
807 //
AM_updateLightLev(void)808 void AM_updateLightLev(void)
809 {
810     static int nexttic = 0;
811     //static int litelevels[] = { 0, 3, 5, 6, 6, 7, 7, 7 };
812     static int litelevels[] = { 0, 4, 7, 10, 12, 14, 15, 15 };
813     static int litelevelscnt = 0;
814 
815     // Change light level
816     if (amclock>nexttic)
817     {
818 	lightlev = litelevels[litelevelscnt++];
819 	if (litelevelscnt == arrlen(litelevels)) litelevelscnt = 0;
820 	nexttic = amclock + 6 - (amclock % 6);
821     }
822 
823 }
824 
825 
826 //
827 // Updates on Game Tick
828 //
AM_Ticker(void)829 void AM_Ticker (void)
830 {
831 
832     if (!automapactive)
833 	return;
834 
835     amclock++;
836 
837     if (followplayer)
838 	AM_doFollowPlayer();
839 
840     // Change the zoom if necessary
841     if (ftom_zoommul != FRACUNIT)
842 	AM_changeWindowScale();
843 
844     // Change x,y location
845     if (m_paninc.x || m_paninc.y)
846 	AM_changeWindowLoc();
847 
848     // Update light level
849     // AM_updateLightLev();
850 
851 }
852 
853 
854 //
855 // Clear automap frame buffer.
856 //
AM_clearFB(int color)857 void AM_clearFB(int color)
858 {
859     memset(fb, color, f_w*f_h*sizeof(*fb));
860 }
861 
862 
863 //
864 // Automap clipping of lines.
865 //
866 // Based on Cohen-Sutherland clipping algorithm but with a slightly
867 // faster reject and precalculated slopes.  If the speed is needed,
868 // use a hash algorithm to handle  the common cases.
869 //
870 boolean
AM_clipMline(mline_t * ml,fline_t * fl)871 AM_clipMline
872 ( mline_t*	ml,
873   fline_t*	fl )
874 {
875     enum
876     {
877 	LEFT	=1,
878 	RIGHT	=2,
879 	BOTTOM	=4,
880 	TOP	=8
881     };
882 
883     register int	outcode1 = 0;
884     register int	outcode2 = 0;
885     register int	outside;
886 
887     fpoint_t	tmp;
888     int		dx;
889     int		dy;
890 
891 
892 #define DOOUTCODE(oc, mx, my) \
893     (oc) = 0; \
894     if ((my) < 0) (oc) |= TOP; \
895     else if ((my) >= f_h) (oc) |= BOTTOM; \
896     if ((mx) < 0) (oc) |= LEFT; \
897     else if ((mx) >= f_w) (oc) |= RIGHT;
898 
899 
900     // do trivial rejects and outcodes
901     if (ml->a.y > m_y2)
902 	outcode1 = TOP;
903     else if (ml->a.y < m_y)
904 	outcode1 = BOTTOM;
905 
906     if (ml->b.y > m_y2)
907 	outcode2 = TOP;
908     else if (ml->b.y < m_y)
909 	outcode2 = BOTTOM;
910 
911     if (outcode1 & outcode2)
912 	return false; // trivially outside
913 
914     if (ml->a.x < m_x)
915 	outcode1 |= LEFT;
916     else if (ml->a.x > m_x2)
917 	outcode1 |= RIGHT;
918 
919     if (ml->b.x < m_x)
920 	outcode2 |= LEFT;
921     else if (ml->b.x > m_x2)
922 	outcode2 |= RIGHT;
923 
924     if (outcode1 & outcode2)
925 	return false; // trivially outside
926 
927     // transform to frame-buffer coordinates.
928     fl->a.x = CXMTOF(ml->a.x);
929     fl->a.y = CYMTOF(ml->a.y);
930     fl->b.x = CXMTOF(ml->b.x);
931     fl->b.y = CYMTOF(ml->b.y);
932 
933     DOOUTCODE(outcode1, fl->a.x, fl->a.y);
934     DOOUTCODE(outcode2, fl->b.x, fl->b.y);
935 
936     if (outcode1 & outcode2)
937 	return false;
938 
939     while (outcode1 | outcode2)
940     {
941 	// may be partially inside box
942 	// find an outside point
943 	if (outcode1)
944 	    outside = outcode1;
945 	else
946 	    outside = outcode2;
947 
948 	// clip to each side
949 	if (outside & TOP)
950 	{
951 	    dy = fl->a.y - fl->b.y;
952 	    dx = fl->b.x - fl->a.x;
953 	    tmp.x = fl->a.x + (dx*(fl->a.y))/dy;
954 	    tmp.y = 0;
955 	}
956 	else if (outside & BOTTOM)
957 	{
958 	    dy = fl->a.y - fl->b.y;
959 	    dx = fl->b.x - fl->a.x;
960 	    tmp.x = fl->a.x + (dx*(fl->a.y-f_h))/dy;
961 	    tmp.y = f_h-1;
962 	}
963 	else if (outside & RIGHT)
964 	{
965 	    dy = fl->b.y - fl->a.y;
966 	    dx = fl->b.x - fl->a.x;
967 	    tmp.y = fl->a.y + (dy*(f_w-1 - fl->a.x))/dx;
968 	    tmp.x = f_w-1;
969 	}
970 	else if (outside & LEFT)
971 	{
972 	    dy = fl->b.y - fl->a.y;
973 	    dx = fl->b.x - fl->a.x;
974 	    tmp.y = fl->a.y + (dy*(-fl->a.x))/dx;
975 	    tmp.x = 0;
976 	}
977         else
978         {
979             tmp.x = 0;
980             tmp.y = 0;
981         }
982 
983 	if (outside == outcode1)
984 	{
985 	    fl->a = tmp;
986 	    DOOUTCODE(outcode1, fl->a.x, fl->a.y);
987 	}
988 	else
989 	{
990 	    fl->b = tmp;
991 	    DOOUTCODE(outcode2, fl->b.x, fl->b.y);
992 	}
993 
994 	if (outcode1 & outcode2)
995 	    return false; // trivially outside
996     }
997 
998     return true;
999 }
1000 #undef DOOUTCODE
1001 
1002 
1003 //
1004 // Classic Bresenham w/ whatever optimizations needed for speed
1005 //
1006 void
AM_drawFline(fline_t * fl,int color)1007 AM_drawFline
1008 ( fline_t*	fl,
1009   int		color )
1010 {
1011     register int x;
1012     register int y;
1013     register int dx;
1014     register int dy;
1015     register int sx;
1016     register int sy;
1017     register int ax;
1018     register int ay;
1019     register int d;
1020 
1021     static int fuck = 0;
1022 
1023     // For debugging only
1024     if (      fl->a.x < 0 || fl->a.x >= f_w
1025 	   || fl->a.y < 0 || fl->a.y >= f_h
1026 	   || fl->b.x < 0 || fl->b.x >= f_w
1027 	   || fl->b.y < 0 || fl->b.y >= f_h)
1028     {
1029         DEH_fprintf(stderr, "fuck %d \r", fuck++);
1030 	return;
1031     }
1032 
1033 #define PUTDOT(xx,yy,cc) fb[(yy)*f_w+(xx)]=(cc)
1034 
1035     dx = fl->b.x - fl->a.x;
1036     ax = 2 * (dx<0 ? -dx : dx);
1037     sx = dx<0 ? -1 : 1;
1038 
1039     dy = fl->b.y - fl->a.y;
1040     ay = 2 * (dy<0 ? -dy : dy);
1041     sy = dy<0 ? -1 : 1;
1042 
1043     x = fl->a.x;
1044     y = fl->a.y;
1045 
1046     if (ax > ay)
1047     {
1048 	d = ay - ax/2;
1049 	while (1)
1050 	{
1051 	    PUTDOT(x,y,color);
1052 	    if (x == fl->b.x) return;
1053 	    if (d>=0)
1054 	    {
1055 		y += sy;
1056 		d -= ax;
1057 	    }
1058 	    x += sx;
1059 	    d += ay;
1060 	}
1061     }
1062     else
1063     {
1064 	d = ax - ay/2;
1065 	while (1)
1066 	{
1067 	    PUTDOT(x, y, color);
1068 	    if (y == fl->b.y) return;
1069 	    if (d >= 0)
1070 	    {
1071 		x += sx;
1072 		d -= ay;
1073 	    }
1074 	    y += sy;
1075 	    d += ax;
1076 	}
1077     }
1078 }
1079 
1080 
1081 //
1082 // Clip lines, draw visible part sof lines.
1083 //
1084 void
AM_drawMline(mline_t * ml,int color)1085 AM_drawMline
1086 ( mline_t*	ml,
1087   int		color )
1088 {
1089     static fline_t fl;
1090 
1091     if (AM_clipMline(ml, &fl))
1092 	AM_drawFline(&fl, color); // draws it on frame buffer using fb coords
1093 }
1094 
1095 
1096 
1097 //
1098 // Draws flat (floor/ceiling tile) aligned grid lines.
1099 //
AM_drawGrid(int color)1100 void AM_drawGrid(int color)
1101 {
1102     fixed_t x, y;
1103     fixed_t start, end;
1104     mline_t ml;
1105 
1106     // Figure out start of vertical gridlines
1107     start = m_x;
1108     if ((start-bmaporgx)%(MAPBLOCKUNITS<<FRACBITS))
1109 	start += (MAPBLOCKUNITS<<FRACBITS)
1110 	    - ((start-bmaporgx)%(MAPBLOCKUNITS<<FRACBITS));
1111     end = m_x + m_w;
1112 
1113     // draw vertical gridlines
1114     ml.a.y = m_y;
1115     ml.b.y = m_y+m_h;
1116     for (x=start; x<end; x+=(MAPBLOCKUNITS<<FRACBITS))
1117     {
1118 	ml.a.x = x;
1119 	ml.b.x = x;
1120 	AM_drawMline(&ml, color);
1121     }
1122 
1123     // Figure out start of horizontal gridlines
1124     start = m_y;
1125     if ((start-bmaporgy)%(MAPBLOCKUNITS<<FRACBITS))
1126 	start += (MAPBLOCKUNITS<<FRACBITS)
1127 	    - ((start-bmaporgy)%(MAPBLOCKUNITS<<FRACBITS));
1128     end = m_y + m_h;
1129 
1130     // draw horizontal gridlines
1131     ml.a.x = m_x;
1132     ml.b.x = m_x + m_w;
1133     for (y=start; y<end; y+=(MAPBLOCKUNITS<<FRACBITS))
1134     {
1135 	ml.a.y = y;
1136 	ml.b.y = y;
1137 	AM_drawMline(&ml, color);
1138     }
1139 
1140 }
1141 
1142 //
1143 // Determines visible lines, draws them.
1144 // This is LineDef based, not LineSeg based.
1145 //
AM_drawWalls(void)1146 void AM_drawWalls(void)
1147 {
1148     int i;
1149     static mline_t l;
1150 
1151     for (i=0;i<numlines;i++)
1152     {
1153 	l.a.x = lines[i].v1->x;
1154 	l.a.y = lines[i].v1->y;
1155 	l.b.x = lines[i].v2->x;
1156 	l.b.y = lines[i].v2->y;
1157 	if (cheating || (lines[i].flags & ML_MAPPED))
1158 	{
1159 	    if ((lines[i].flags & LINE_NEVERSEE) && !cheating)
1160 		continue;
1161 	    if (!lines[i].backsector)
1162 	    {
1163 		AM_drawMline(&l, WALLCOLORS+lightlev);
1164 	    }
1165 	    else
1166 	    {
1167 		if (lines[i].special == 39)
1168 		{ // teleporters
1169 		    AM_drawMline(&l, WALLCOLORS+WALLRANGE/2);
1170 		}
1171 		else if (lines[i].flags & ML_SECRET) // secret door
1172 		{
1173 		    if (cheating) AM_drawMline(&l, SECRETWALLCOLORS + lightlev);
1174 		    else AM_drawMline(&l, WALLCOLORS+lightlev);
1175 		}
1176 		else if (lines[i].backsector->floorheight
1177 			   != lines[i].frontsector->floorheight) {
1178 		    AM_drawMline(&l, FDWALLCOLORS + lightlev); // floor level change
1179 		}
1180 		else if (lines[i].backsector->ceilingheight
1181 			   != lines[i].frontsector->ceilingheight) {
1182 		    AM_drawMline(&l, CDWALLCOLORS+lightlev); // ceiling level change
1183 		}
1184 		else if (cheating) {
1185 		    AM_drawMline(&l, TSWALLCOLORS+lightlev);
1186 		}
1187 	    }
1188 	}
1189 	else if (plr->powers[pw_allmap])
1190 	{
1191 	    if (!(lines[i].flags & LINE_NEVERSEE)) AM_drawMline(&l, GRAYS+3);
1192 	}
1193     }
1194 }
1195 
1196 
1197 //
1198 // Rotation in 2D.
1199 // Used to rotate player arrow line character.
1200 //
1201 void
AM_rotate(fixed_t * x,fixed_t * y,angle_t a)1202 AM_rotate
1203 ( fixed_t*	x,
1204   fixed_t*	y,
1205   angle_t	a )
1206 {
1207     fixed_t tmpx;
1208 
1209     tmpx =
1210 	FixedMul(*x,finecosine[a>>ANGLETOFINESHIFT])
1211 	- FixedMul(*y,finesine[a>>ANGLETOFINESHIFT]);
1212 
1213     *y   =
1214 	FixedMul(*x,finesine[a>>ANGLETOFINESHIFT])
1215 	+ FixedMul(*y,finecosine[a>>ANGLETOFINESHIFT]);
1216 
1217     *x = tmpx;
1218 }
1219 
1220 void
AM_drawLineCharacter(mline_t * lineguy,int lineguylines,fixed_t scale,angle_t angle,int color,fixed_t x,fixed_t y)1221 AM_drawLineCharacter
1222 ( mline_t*	lineguy,
1223   int		lineguylines,
1224   fixed_t	scale,
1225   angle_t	angle,
1226   int		color,
1227   fixed_t	x,
1228   fixed_t	y )
1229 {
1230     int		i;
1231     mline_t	l;
1232 
1233     for (i=0;i<lineguylines;i++)
1234     {
1235 	l.a.x = lineguy[i].a.x;
1236 	l.a.y = lineguy[i].a.y;
1237 
1238 	if (scale)
1239 	{
1240 	    l.a.x = FixedMul(scale, l.a.x);
1241 	    l.a.y = FixedMul(scale, l.a.y);
1242 	}
1243 
1244 	if (angle)
1245 	    AM_rotate(&l.a.x, &l.a.y, angle);
1246 
1247 	l.a.x += x;
1248 	l.a.y += y;
1249 
1250 	l.b.x = lineguy[i].b.x;
1251 	l.b.y = lineguy[i].b.y;
1252 
1253 	if (scale)
1254 	{
1255 	    l.b.x = FixedMul(scale, l.b.x);
1256 	    l.b.y = FixedMul(scale, l.b.y);
1257 	}
1258 
1259 	if (angle)
1260 	    AM_rotate(&l.b.x, &l.b.y, angle);
1261 
1262 	l.b.x += x;
1263 	l.b.y += y;
1264 
1265 	AM_drawMline(&l, color);
1266     }
1267 }
1268 
AM_drawPlayers(void)1269 void AM_drawPlayers(void)
1270 {
1271     int		i;
1272     player_t*	p;
1273     static int 	their_colors[] = { GREENS, GRAYS, BROWNS, REDS };
1274     int		their_color = -1;
1275     int		color;
1276 
1277     if (!netgame)
1278     {
1279 	if (cheating)
1280 	    AM_drawLineCharacter
1281 		(cheat_player_arrow, arrlen(cheat_player_arrow), 0,
1282 		 plr->mo->angle, WHITE, plr->mo->x, plr->mo->y);
1283 	else
1284 	    AM_drawLineCharacter
1285 		(player_arrow, arrlen(player_arrow), 0, plr->mo->angle,
1286 		 WHITE, plr->mo->x, plr->mo->y);
1287 	return;
1288     }
1289 
1290     for (i=0;i<MAXPLAYERS;i++)
1291     {
1292 	their_color++;
1293 	p = &players[i];
1294 
1295 	if ( (deathmatch && !singledemo) && p != plr)
1296 	    continue;
1297 
1298 	if (!playeringame[i])
1299 	    continue;
1300 
1301 	if (p->powers[pw_invisibility])
1302 	    color = 246; // *close* to black
1303 	else
1304 	    color = their_colors[their_color];
1305 
1306 	AM_drawLineCharacter
1307 	    (player_arrow, arrlen(player_arrow), 0, p->mo->angle,
1308 	     color, p->mo->x, p->mo->y);
1309     }
1310 
1311 }
1312 
1313 void
AM_drawThings(int colors,int colorrange)1314 AM_drawThings
1315 ( int	colors,
1316   int 	colorrange)
1317 {
1318     int		i;
1319     mobj_t*	t;
1320 
1321     for (i=0;i<numsectors;i++)
1322     {
1323 	t = sectors[i].thinglist;
1324 	while (t)
1325 	{
1326 	    AM_drawLineCharacter
1327 		(thintriangle_guy, arrlen(thintriangle_guy),
1328 		 16<<FRACBITS, t->angle, colors+lightlev, t->x, t->y);
1329 	    t = t->snext;
1330 	}
1331     }
1332 }
1333 
AM_drawMarks(void)1334 void AM_drawMarks(void)
1335 {
1336     int i, fx, fy, w, h;
1337 
1338     for (i=0;i<AM_NUMMARKPOINTS;i++)
1339     {
1340 	if (markpoints[i].x != -1)
1341 	{
1342 	    //      w = SHORT(marknums[i]->width);
1343 	    //      h = SHORT(marknums[i]->height);
1344 	    w = 5; // because something's wrong with the wad, i guess
1345 	    h = 6; // because something's wrong with the wad, i guess
1346 	    fx = CXMTOF(markpoints[i].x);
1347 	    fy = CYMTOF(markpoints[i].y);
1348 	    if (fx >= f_x && fx <= f_w - w && fy >= f_y && fy <= f_h - h)
1349 		V_DrawPatch(fx, fy, marknums[i]);
1350 	}
1351     }
1352 
1353 }
1354 
AM_drawCrosshair(int color)1355 void AM_drawCrosshair(int color)
1356 {
1357     fb[(f_w*(f_h+1))/2] = color; // single point for now
1358 
1359 }
1360 
AM_Drawer(void)1361 void AM_Drawer (void)
1362 {
1363     if (!automapactive) return;
1364 
1365     AM_clearFB(BACKGROUND);
1366     if (grid)
1367 	AM_drawGrid(GRIDCOLORS);
1368     AM_drawWalls();
1369     AM_drawPlayers();
1370     if (cheating==2)
1371 	AM_drawThings(THINGCOLORS, THINGRANGE);
1372     AM_drawCrosshair(XHAIRCOLORS);
1373 
1374     AM_drawMarks();
1375 
1376     V_MarkRect(f_x, f_y, f_w, f_h);
1377 
1378 }
1379