fix 'crash when reviving shopkeeper'
[aNetHack.git] / src / track.c
blob0ba143044cc5b82f4197d86c841a7a4ddc6b723d
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 */
6 #include "hack.h"
8 #define UTSZ 50
10 STATIC_VAR NEARDATA int utcnt, utpnt;
11 STATIC_VAR NEARDATA coord utrack[UTSZ];
13 void
14 initrack()
16 utcnt = utpnt = 0;
19 /* add to track */
20 void
21 settrack()
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++;
32 coord *
33 gettrack(x, y)
34 register int x, y;
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);
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);
59 return (coord *) 0;
62 /*track.c*/