Source:NetHack 3.6.0/src/track.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to track.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/track.c#line123]], 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.

Top of file

  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.  

initrack

  1.  void
  2.  initrack()
  3.  {
  4.      utcnt = utpnt = 0;
  5.  }
  6.  

settrack

  1.  /* add to track */
  2.  void
  3.  settrack()
  4.  {
  5.      if (utcnt < UTSZ)
  6.          utcnt++;
  7.      if (utpnt == UTSZ)
  8.          utpnt = 0;
  9.      utrack[utpnt].x = u.ux;
  10.      utrack[utpnt].y = u.uy;
  11.      utpnt++;
  12.  }
  13.  

gettrack

  1.  coord *
  2.  gettrack(x, y)
  3.  register int x, y;
  4.  {
  5.      register int cnt, ndist;
  6.      register coord *tc;
  7.      cnt = utcnt;
  8.      for (tc = &utrack[utpnt]; cnt--;) {
  9.          if (tc == utrack)
  10.              tc = &utrack[UTSZ - 1];
  11.          else
  12.              tc--;
  13.          ndist = distmin(x, y, tc->x, tc->y);
  14.  
  15.          /* if far away, skip track entries til we're closer */
  16.          if (ndist > 2) {
  17.              ndist -= 2; /* be careful due to extra decrement at top of loop */
  18.              cnt -= ndist;
  19.              if (cnt <= 0)
  20.                  return (coord *) 0; /* too far away, no matches possible */
  21.              if (tc < &utrack[ndist])
  22.                  tc += (UTSZ - ndist);
  23.              else
  24.                  tc -= ndist;
  25.          } else if (ndist <= 1)
  26.              return (ndist ? tc : 0);
  27.      }
  28.      return (coord *) 0;
  29.  }
  30.  
  31.  /*track.c*/