Source:NetHack 5.0.0/src/track.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to track.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/track.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 5.0	track.c	$NHDT-Date: 1596498219 2020/08/03 23:43:39 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.12 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Kenneth Lorber, Kensington, Maryland, 2015. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. /* track.c - version 1.0.2 */
6. 
7. #include "hack.h"
8. 
9. #define UTSZ 100
10. 
11. static NEARDATA int utcnt, utpnt;
12. static NEARDATA coord utrack[UTSZ];
13. 
14. void
15. initrack(void)
16. {
17.     utcnt = utpnt = 0;
18.     (void) memset((genericptr_t) &utrack, 0, sizeof(utrack));
19. }
20. 
21. #ifndef SFCTOOL
22. /* add to track */
23. void
24. settrack(void)
25. {
26.     if ((uleft && uleft->otyp == RIN_STEALTH)
27.         || (uright && uright->otyp == RIN_STEALTH))
28.         return;
29. 
30.     if (utcnt < UTSZ)
31.         utcnt++;
32.     if (utpnt == UTSZ)
33.         utpnt = 0;
34.     utrack[utpnt].x = u.ux;
35.     utrack[utpnt].y = u.uy;
36.     utpnt++;
37. }
38. 
39. /* get a track coord on or next to x,y and last tracked by hero,
40.    returns null if no such track */
41. coord *
42. gettrack(coordxy x, coordxy y)
43. {
44.     int cnt, ndist;
45.     coord *tc;
46.     cnt = utcnt;
47.     for (tc = &utrack[utpnt]; cnt--;) {
48.         if (tc == utrack)
49.             tc = &utrack[UTSZ - 1];
50.         else
51.             tc--;
52.         ndist = distmin(x, y, tc->x, tc->y);
53. 
54.         if (ndist <= 1)
55.             return (ndist ? tc : 0);
56.     }
57.     return (coord *) 0;
58. }
59. #endif /* !SFCTOOL */
60. 
61. /* return TRUE if x,y has hero tracks on it */
62. boolean
63. hastrack(coordxy x, coordxy y)
64. {
65.     int i;
66. 
67.     for (i = 0; i < utcnt; i++)
68.         if (utrack[i].x == x && utrack[i].y == y)
69.             return TRUE;
70. 
71.     return FALSE;
72. }
73. 
74. /* save the hero tracking info */
75. void
76. save_track(NHFILE *nhfp)
77. {
78.     if (update_file(nhfp)) {
79.         int i;
80. 
81.         Sfo_int(nhfp, &utcnt, "track-utcnt");
82.         Sfo_int(nhfp, &utpnt, "track-utpnt");
83.         for (i = 0; i < utcnt; i++) {
84.             Sfo_nhcoord(nhfp, &utrack[i], "utrack");
85.         }
86.     }
87.     if (release_data(nhfp))
88.         initrack();
89. }
90. 
91. /* restore the hero tracking info */
92. void
93. rest_track(NHFILE *nhfp)
94. {
95.     int i;
96. 
97.     Sfi_int(nhfp, &utcnt, "track-utcnt");
98.     Sfi_int(nhfp, &utpnt, "track-utpnt");
99. 
100.     if (utcnt > UTSZ || utpnt > UTSZ)
101.         panic("rest_track: impossible pt counts");
102.     for (i = 0; i < utcnt; i++) {
103.         Sfi_nhcoord(nhfp, &utrack[i], "utrack");
104.     }
105. }
106. 
107. /*track.c*/