Wikia

Wikihack

Source:NetHack 3.4.0/hacklib.c

2,032pages on
this wiki
Talk0

Below is the full text to hacklib.c from the source code of NetHack 3.4.0. To link to a particular line, write [[NetHack 3.4.0/hacklib.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
1.    /*	SCCS Id: @(#)hacklib.c	3.4	1999/04/10	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* Copyright (c) Robert Patrick Rankin, 1991		  */
4.    /* NetHack may be freely redistributed.  See license for details. */
5.    
6.    /* We could include only config.h, except for the overlay definitions... */
7.    #include "hack.h"
8.    /*=
9.        Assorted 'small' utility routines.	They're virtually independent of
10.   NetHack, except that rounddiv may call panic().
11.   
12.         return type     routine name    argument type(s)
13.   	boolean		digit		(char)
14.   	boolean		letter		(char)
15.   	char		highc		(char)
16.   	char		lowc		(char)
17.   	char *		lcase		(char *)
18.   	char *		upstart		(char *)
19.   	char *		mungspaces	(char *)
20.   	char *		eos		(char *)
21.   	char *		s_suffix	(const char *)
22.   	char *		xcrypt		(const char *, char *)
23.   	boolean		onlyspace	(const char *)
24.   	char *		tabexpand	(char *)
25.   	char *		visctrl		(char)
26.   	const char *	ordin		(int)
27.   	char *		sitoa		(int)
28.   	int		sgn		(int)
29.   	int		rounddiv	(long, int)
30.   	int		distmin		(int, int, int, int)
31.   	int		dist2		(int, int, int, int)
32.   	boolean		online2		(int, int)
33.   	boolean		pmatch		(const char *, const char *)
34.   	int		strncmpi	(const char *, const char *, int)
35.   	char *		strstri		(const char *, const char *)
36.   	boolean		fuzzymatch	(const char *,const char *,const char *,boolean)
37.   	void		setrandom	(void)
38.   	int		getyear		(void)
39.   	char *		yymmdd		(time_t)
40.   	long		yyyymmdd	(time_t)
41.   	int		phase_of_the_moon	(void)
42.   	boolean		friday_13th	(void)
43.   	int		night		(void)
44.   	int		midnight	(void)
45.   =*/
46.   #ifdef LINT
47.   # define Static		/* pacify lint */
48.   #else
49.   # define Static static
50.   #endif
51.   
52.   #ifdef OVLB
53.   boolean
54.   digit(c)		/* is 'c' a digit? */
55.       char c;
56.   {
57.       return((boolean)('0' <= c && c <= '9'));
58.   }
59.   
60.   boolean
61.   letter(c)		/* is 'c' a letter?  note: '@' classed as letter */
62.       char c;
63.   {
64.       return((boolean)(('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z')));
65.   }
66.   #endif /* OVLB */
67.   
68.   #ifdef OVL1
69.   char
70.   highc(c)			/* force 'c' into uppercase */
71.       char c;
72.   {
73.       return((char)(('a' <= c && c <= 'z') ? (c & ~040) : c));
74.   }
75.   
76.   char
77.   lowc(c)			/* force 'c' into lowercase */
78.       char c;
79.   {
80.       return((char)(('A' <= c && c <= 'Z') ? (c | 040) : c));
81.   }
82.   #endif /* OVL1 */
83.   
84.   #ifdef OVLB
85.   char *
86.   lcase(s)		/* convert a string into all lowercase */
87.       char *s;
88.   {
89.       register char *p;
90.   
91.       for (p = s; *p; p++)
92.   	if ('A' <= *p && *p <= 'Z') *p |= 040;
93.       return s;
94.   }
95.   
96.   char *
97.   upstart(s)		/* convert first character of a string to uppercase */
98.       char *s;
99.   {
100.      if (s) *s = highc(*s);
101.      return s;
102.  }
103.  
104.  /* remove excess whitespace from a string buffer (in place) */
105.  char *
106.  mungspaces(bp)
107.  char *bp;
108.  {
109.      register char c, *p, *p2;
110.      boolean was_space = TRUE;
111.  
112.      for (p = p2 = bp; (c = *p) != '\0'; p++) {
113.  	if (c == '\t') c = ' ';
114.  	if (c != ' ' || !was_space) *p2++ = c;
115.  	was_space = (c == ' ');
116.      }
117.      if (was_space && p2 > bp) p2--;
118.      *p2 = '\0';
119.      return bp;
120.  }
121.  
122.  #endif /* OVLB */
123.  
124.  #ifdef OVL0
125.  char *
126.  eos(s)			/* return the end of a string (pointing at '\0') */
127.      register char *s;
128.  {
129.      while (*s) s++;	/* s += strlen(s); */
130.      return s;
131.  }
132.  
133.  char *
134.  s_suffix(s)		/* return a name converted to possessive */
135.      const char *s;
136.  {
137.      Static char buf[BUFSZ];
138.  
139.      Strcpy(buf, s);
140.      if(!strcmpi(buf, "it"))
141.  	Strcat(buf, "s");
142.      else if(*(eos(buf)-1) == 's')
143.  	Strcat(buf, "'");
144.      else
145.  	Strcat(buf, "'s");
146.      return buf;
147.  }
148.  
149.  char *
150.  xcrypt(str, buf)	/* trivial text encryption routine (see makedefs) */
151.  const char *str;
152.  char *buf;
153.  {
154.      register const char *p;
155.      register char *q;
156.      register int bitmask;
157.  
158.      for (bitmask = 1, p = str, q = buf; *p; q++) {
159.  	*q = *p++;
160.  	if (*q & (32|64)) *q ^= bitmask;
161.  	if ((bitmask <<= 1) >= 32) bitmask = 1;
162.      }
163.      *q = '\0';
164.      return buf;
165.  }
166.  #endif /* OVL0 */
167.  
168.  #ifdef OVL2
169.  boolean
170.  onlyspace(s)		/* is a string entirely whitespace? */
171.      const char *s;
172.  {
173.      for (; *s; s++)
174.  	if (*s != ' ' && *s != '\t') return FALSE;
175.      return TRUE;
176.  }
177.  #endif /* OVL2 */
178.  
179.  #ifdef OVLB
180.  char *
181.  tabexpand(sbuf)		/* expand tabs into proper number of spaces */
182.      char *sbuf;
183.  {
184.      char buf[BUFSZ];
185.      register char *bp, *s = sbuf;
186.      register int idx;
187.  
188.      if (!*s) return sbuf;
189.  
190.      /* warning: no bounds checking performed */
191.      for (bp = buf, idx = 0; *s; s++)
192.  	if (*s == '\t') {
193.  	    do *bp++ = ' '; while (++idx % 8);
194.  	} else {
195.  	    *bp++ = *s;
196.  	    idx++;
197.  	}
198.      *bp = 0;
199.      return strcpy(sbuf, buf);
200.  }
201.  
202.  char *
203.  visctrl(c)		/* make a displayable string from a character */
204.      char c;
205.  {
206.      Static char ccc[3];
207.  
208.      c &= 0177;
209.  
210.      ccc[2] = '\0';
211.      if (c < 040) {
212.  	ccc[0] = '^';
213.  	ccc[1] = c | 0100;	/* letter */
214.      } else if (c == 0177) {
215.  	ccc[0] = '^';
216.  	ccc[1] = c & ~0100;	/* '?' */
217.      } else {
218.  	ccc[0] = c;		/* printable character */
219.  	ccc[1] = '\0';
220.      }
221.      return ccc;
222.  }
223.  #endif /* OVLB */
224.  
225.  #ifdef OVL2
226.  const char *
227.  ordin(n)		/* return the ordinal suffix of a number */
228.      int n;			/* note: should be non-negative */
229.  {
230.      register int dd = n % 10;
231.  
232.      return (dd == 0 || dd > 3 || (n % 100) / 10 == 1) ? "th" :
233.  	    (dd == 1) ? "st" : (dd == 2) ? "nd" : "rd";
234.  }
235.  #endif /* OVL2 */
236.  
237.  #ifdef OVL1
238.  char *
239.  sitoa(n)		/* make a signed digit string from a number */
240.      int n;
241.  {
242.      Static char buf[13];
243.  
244.      Sprintf(buf, (n < 0) ? "%d" : "+%d", n);
245.      return buf;
246.  }
247.  
248.  int
249.  sgn(n)			/* return the sign of a number: -1, 0, or 1 */
250.      int n;
251.  {
252.      return (n < 0) ? -1 : (n != 0);
253.  }
254.  #endif /* OVL1 */
255.  
256.  #ifdef OVLB
257.  int
258.  rounddiv(x, y)		/* calculate x/y, rounding as appropriate */
259.      long x;
260.      int  y;
261.  {
262.      int r, m;
263.      int divsgn = 1;
264.  
265.      if (y == 0)
266.  	panic("division by zero in rounddiv");
267.      else if (y < 0) {
268.  	divsgn = -divsgn;  y = -y;
269.      }
270.      if (x < 0) {
271.  	divsgn = -divsgn;  x = -x;
272.      }
273.      r = x / y;
274.      m = x % y;
275.      if (2*m >= y) r++;
276.  
277.      return divsgn * r;
278.  }
279.  #endif /* OVLB */
280.  
281.  #ifdef OVL0
282.  int
283.  distmin(x0, y0, x1, y1) /* distance between two points, in moves */
284.      int x0, y0, x1, y1;
285.  {
286.      register int dx = x0 - x1, dy = y0 - y1;
287.      if (dx < 0) dx = -dx;
288.      if (dy < 0) dy = -dy;
289.    /*  The minimum number of moves to get from (x0,y0) to (x1,y1) is the
290.     :  larger of the [absolute value of the] two deltas.
291.     */
292.      return (dx < dy) ? dy : dx;
293.  }
294.  
295.  int
296.  dist2(x0, y0, x1, y1)	/* square of euclidean distance between pair of pts */
297.      int x0, y0, x1, y1;
298.  {
299.      register int dx = x0 - x1, dy = y0 - y1;
300.      return dx * dx + dy * dy;
301.  }
302.  
303.  boolean
304.  online2(x0, y0, x1, y1) /* are two points lined up (on a straight line)? */
305.      int x0, y0, x1, y1;
306.  {
307.      int dx = x0 - x1, dy = y0 - y1;
308.      /*  If either delta is zero then they're on an orthogonal line,
309.       *  else if the deltas are equal (signs ignored) they're on a diagonal.
310.       */
311.      return((boolean)(!dy || !dx || (dy == dx) || (dy + dx == 0)));	/* (dy == -dx) */
312.  }
313.  
314.  #endif /* OVL0 */
315.  #ifdef OVLB
316.  
317.  boolean
318.  pmatch(patrn, strng)	/* match a string against a pattern */
319.      const char *patrn, *strng;
320.  {
321.      char s, p;
322.    /*
323.     :  Simple pattern matcher:  '*' matches 0 or more characters, '?' matches
324.     :  any single character.  Returns TRUE if 'strng' matches 'patrn'.
325.     */
326.  pmatch_top:
327.      s = *strng++;  p = *patrn++;	/* get next chars and pre-advance */
328.      if (!p)			/* end of pattern */
329.  	return((boolean)(s == '\0'));		/* matches iff end of string too */
330.      else if (p == '*')		/* wildcard reached */
331.  	return((boolean)((!*patrn || pmatch(patrn, strng-1)) ? TRUE :
332.  		s ? pmatch(patrn-1, strng) : FALSE));
333.      else if (p != s && (p != '?' || !s))  /* check single character */
334.  	return FALSE;		/* doesn't match */
335.      else				/* return pmatch(patrn, strng); */
336.  	goto pmatch_top;	/* optimize tail recursion */
337.  }
338.  #endif /* OVLB */
339.  
340.  #ifdef OVL2
341.  #ifndef STRNCMPI
342.  int
343.  strncmpi(s1, s2, n)	/* case insensitive counted string comparison */
344.      register const char *s1, *s2;
345.      register int n; /*(should probably be size_t, which is usually unsigned)*/
346.  {					/*{ aka strncasecmp }*/
347.      register char t1, t2;
348.  
349.      while (n--) {
350.  	if (!*s2) return (*s1 != 0);	/* s1 >= s2 */
351.  	else if (!*s1) return -1;	/* s1  < s2 */
352.  	t1 = lowc(*s1++);
353.  	t2 = lowc(*s2++);
354.  	if (t1 != t2) return (t1 > t2) ? 1 : -1;
355.      }
356.      return 0;				/* s1 == s2 */
357.  }
358.  #endif	/* STRNCMPI */
359.  #endif /* OVL2 */
360.  
361.  #ifdef OVLB
362.  #ifndef STRSTRI
363.  
364.  char *
365.  strstri(str, sub)	/* case insensitive substring search */
366.      const char *str;
367.      const char *sub;
368.  {
369.      register const char *s1, *s2;
370.      register int i, k;
371.  # define TABSIZ 0x20	/* 0x40 would be case-sensitive */
372.      char tstr[TABSIZ], tsub[TABSIZ];	/* nibble count tables */
373.  # if 0
374.      assert( (TABSIZ & ~(TABSIZ-1)) == TABSIZ ); /* must be exact power of 2 */
375.      assert( &lowc != 0 );			/* can't be unsafe macro */
376.  # endif
377.  
378.      /* special case: empty substring */
379.      if (!*sub)	return (char *) str;
380.  
381.      /* do some useful work while determining relative lengths */
382.      for (i = 0; i < TABSIZ; i++)  tstr[i] = tsub[i] = 0;	/* init */
383.      for (k = 0, s1 = str; *s1; k++)  tstr[*s1++ & (TABSIZ-1)]++;
384.      for (	s2 = sub; *s2; --k)  tsub[*s2++ & (TABSIZ-1)]++;
385.  
386.      /* evaluate the info we've collected */
387.      if (k < 0)	return (char *) 0;  /* sub longer than str, so can't match */
388.      for (i = 0; i < TABSIZ; i++)	/* does sub have more 'x's than str? */
389.  	if (tsub[i] > tstr[i])	return (char *) 0;  /* match not possible */
390.  
391.      /* now actually compare the substring repeatedly to parts of the string */
392.      for (i = 0; i <= k; i++) {
393.  	s1 = &str[i];
394.  	s2 = sub;
395.  	while (lowc(*s1++) == lowc(*s2++))
396.  	    if (!*s2)  return (char *) &str[i];		/* full match */
397.      }
398.      return (char *) 0;	/* not found */
399.  }
400.  #endif	/* STRSTRI */
401.  
402.  /* compare two strings for equality, ignoring the presence of specified
403.     characters (typically whitespace) and possibly ignoring case */
404.  boolean
405.  fuzzymatch(s1, s2, ignore_chars, caseblind)
406.      const char *s1, *s2;
407.      const char *ignore_chars;
408.      boolean caseblind;
409.  {
410.      register char c1, c2;
411.  
412.      do {
413.  	while ((c1 = *s1++) != '\0' && index(ignore_chars, c1) != 0) continue;
414.  	while ((c2 = *s2++) != '\0' && index(ignore_chars, c2) != 0) continue;
415.  	if (!c1 || !c2) break;	/* stop when end of either string is reached */
416.  
417.  	if (caseblind) {
418.  	    c1 = lowc(c1);
419.  	    c2 = lowc(c2);
420.  	}
421.      } while (c1 == c2);
422.  
423.      /* match occurs only when the end of both strings has been reached */
424.      return (boolean)(!c1 && !c2);
425.  }
426.  
427.  #endif /* OVLB */
428.  #ifdef OVL2
429.  
430.  /*
431.   * Time routines
432.   *
433.   * The time is used for:
434.   *	- seed for rand()
435.   *	- year on tombstone and yyyymmdd in record file
436.   *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
437.   *	- night and midnight (the undead are dangerous at midnight)
438.   *	- determination of what files are "very old"
439.   */
440.  
441.  #if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60) && !defined(_DCC) && !defined(__GNUC__)
442.  extern struct tm *FDECL(localtime,(time_t *));
443.  #endif
444.  static struct tm *NDECL(getlt);
445.  
446.  void
447.  setrandom()
448.  {
449.  	/* the types are different enough here that sweeping the different
450.  	 * routine names into one via #defines is even more confusing
451.  	 */
452.  #ifdef RANDOM	/* srandom() from sys/share/random.c */
453.  	srandom((unsigned int) time((time_t *)0));
454.  #else
455.  # if defined(__APPLE__) || defined(BSD) || defined(ULTRIX) || defined(CYGWIN32) /* system srandom() */
456.  #  ifdef BSD
457.  #   if defined(SUNOS4)
458.  	(void)
459.  #   endif
460.  		srandom((int) time((long *)0));
461.  #  else
462.  		srandom((int) time((time_t *)0));
463.  #  endif
464.  # else
465.  #  ifdef UNIX	/* system srand48() */
466.  	srand48((long) time((time_t *)0));
467.  #  else		/* poor quality system routine */
468.  	srand((int) time((time_t *)0));
469.  #  endif
470.  # endif
471.  #endif
472.  }
473.  
474.  static struct tm *
475.  getlt()
476.  {
477.  	time_t date;
478.  
479.  #ifdef BSD
480.  	(void) time((long *)(&date));
481.  #else
482.  	(void) time(&date);
483.  #endif
484.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
485.  	return(localtime((long *)(&date)));
486.  #else
487.  	return(localtime(&date));
488.  #endif
489.  }
490.  
491.  int
492.  getyear()
493.  {
494.  	return(1900 + getlt()->tm_year);
495.  }
496.  
497.  #if 0
498.  /* This routine is no longer used since in 2000 it will yield "100mmdd". */
499.  char *
500.  yymmdd(date)
501.  time_t date;
502.  {
503.  	Static char datestr[10];
504.  	struct tm *lt;
505.  
506.  	if (date == 0)
507.  		lt = getlt();
508.  	else
509.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
510.  		lt = localtime((long *)(&date));
511.  #else
512.  		lt = localtime(&date);
513.  #endif
514.  
515.  	Sprintf(datestr, "%02d%02d%02d",
516.  		lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
517.  	return(datestr);
518.  }
519.  #endif
520.  
521.  long
522.  yyyymmdd(date)
523.  time_t date;
524.  {
525.  	long datenum;
526.  	struct tm *lt;
527.  
528.  	if (date == 0)
529.  		lt = getlt();
530.  	else
531.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
532.  		lt = localtime((long *)(&date));
533.  #else
534.  		lt = localtime(&date);
535.  #endif
536.  
537.  	/* just in case somebody's localtime supplies (year % 100)
538.  	   rather than the expected (year - 1900) */
539.  	if (lt->tm_year < 70)
540.  	    datenum = (long)lt->tm_year + 2000L;
541.  	else
542.  	    datenum = (long)lt->tm_year + 1900L;
543.  	/* yyyy --> yyyymm */
544.  	datenum = datenum * 100L + (long)(lt->tm_mon + 1);
545.  	/* yyyymm --> yyyymmdd */
546.  	datenum = datenum * 100L + (long)lt->tm_mday;
547.  	return datenum;
548.  }
549.  
550.  /*
551.   * moon period = 29.53058 days ~= 30, year = 365.2422 days
552.   * days moon phase advances on first day of year compared to preceding year
553.   *	= 365.2422 - 12*29.53058 ~= 11
554.   * years in Metonic cycle (time until same phases fall on the same days of
555.   *	the month) = 18.6 ~= 19
556.   * moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
557.   *	(29 as initial condition)
558.   * current phase in days = first day phase + days elapsed in year
559.   * 6 moons ~= 177 days
560.   * 177 ~= 8 reported phases * 22
561.   * + 11/22 for rounding
562.   */
563.  int
564.  phase_of_the_moon()		/* 0-7, with 0: new, 4: full */
565.  {
566.  	register struct tm *lt = getlt();
567.  	register int epact, diy, goldn;
568.  
569.  	diy = lt->tm_yday;
570.  	goldn = (lt->tm_year % 19) + 1;
571.  	epact = (11 * goldn + 18) % 30;
572.  	if ((epact == 25 && goldn > 11) || epact == 24)
573.  		epact++;
574.  
575.  	return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
576.  }
577.  
578.  boolean
579.  friday_13th()
580.  {
581.  	register struct tm *lt = getlt();
582.  
583.  	return((boolean)(lt->tm_wday == 5 /* friday */ && lt->tm_mday == 13));
584.  }
585.  
586.  int
587.  night()
588.  {
589.  	register int hour = getlt()->tm_hour;
590.  
591.  	return(hour < 6 || hour > 21);
592.  }
593.  
594.  int
595.  midnight()
596.  {
597.  	return(getlt()->tm_hour == 0);
598.  }
599.  #endif /* OVL2 */
600.  
601.  /*hacklib.c*/

Around Wikia's network

Random Wiki