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