OSDN Git Service

fix #36223
[jnethack/source.git] / src / track.c
1 /* NetHack 3.6  track.c $NHDT-Date: 1432512769 2015/05/25 00:12:49 $  $NHDT-Branch: master $:$NHDT-Revision: 1.9 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed.  See license for details. */
4 /* track.c - version 1.0.2 */
5
6 #include "hack.h"
7
8 #define UTSZ 50
9
10 STATIC_VAR NEARDATA int utcnt, utpnt;
11 STATIC_VAR NEARDATA coord utrack[UTSZ];
12
13 void
14 initrack()
15 {
16     utcnt = utpnt = 0;
17 }
18
19 /* add to track */
20 void
21 settrack()
22 {
23     if (utcnt < UTSZ)
24         utcnt++;
25     if (utpnt == UTSZ)
26         utpnt = 0;
27     utrack[utpnt].x = u.ux;
28     utrack[utpnt].y = u.uy;
29     utpnt++;
30 }
31
32 coord *
33 gettrack(x, y)
34 register int x, y;
35 {
36     register int cnt, ndist;
37     register coord *tc;
38     cnt = utcnt;
39     for (tc = &utrack[utpnt]; cnt--;) {
40         if (tc == utrack)
41             tc = &utrack[UTSZ - 1];
42         else
43             tc--;
44         ndist = distmin(x, y, tc->x, tc->y);
45
46         /* if far away, skip track entries til we're closer */
47         if (ndist > 2) {
48             ndist -= 2; /* be careful due to extra decrement at top of loop */
49             cnt -= ndist;
50             if (cnt <= 0)
51                 return (coord *) 0; /* too far away, no matches possible */
52             if (tc < &utrack[ndist])
53                 tc += (UTSZ - ndist);
54             else
55                 tc -= ndist;
56         } else if (ndist <= 1)
57             return (ndist ? tc : 0);
58     }
59     return (coord *) 0;
60 }
61
62 /*track.c*/