Fandom

Wikihack

Source:NetHack 3.1.0/hacklib.c

2,034pages on
this wiki
Add New Page
Talk0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Below is the full text to hacklib.c from the source code of NetHack 3.1.0. To link to a particular line, write [[NetHack 3.1.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.1	 91/11/25	*/
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 only include 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 *		eos		(char *)
19.   	char *		s_suffix	(const char *)
20.   	char *		xcrypt		(const char *)
21.   	boolean		onlyspace	(const char *)
22.   	char *		tabexpand	(char *)
23.   	char *		visctrl		(char)
24.   	const char *	ordin		(int)
25.   	char *		sitoa		(int)
26.   	int		sgn		(int)
27.   	int		rounddiv	(long, int)
28.   	int		distmin		(int, int, int, int)
29.   	int		dist2		(int, int, int, int)
30.   	boolean		online2		(int, int)
31.   	boolean		pmatch		(const char *, const char *)
32.   	int		strncmpi	(const char *, const char *, int)
33.   	char *		strstri		(const char *, const char *)
34.   	void		setrandom	(void)
35.   	int		getyear		(void)
36.   	char *		get_date	(void)
37.   	int		phase_of_the_moon	(void)
38.   	boolean		friday_13th	(void)
39.   	int		night		(void)
40.   	int		midnight	(void)
41.   =*/
42.   #ifdef LINT
43.   # define Static		/* pacify lint */
44.   #else
45.   # define Static static
46.   #endif
47.   
48.   #ifdef OVLB
49.   boolean
50.   digit(c)		/* is 'c' a digit? */
51.       char c;
52.   {
53.       return '0' <= c && c <= '9';
54.   }
55.   
56.   boolean
57.   letter(c)		/* is 'c' a letter?  note: '@' classed as letter */
58.       char c;
59.   {
60.       return ('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
61.   }
62.   #endif /* OVLB */
63.   
64.   #ifdef OVL1
65.   char
66.   highc(c)			/* force 'c' into uppercase */
67.       char c;
68.   {
69.       return ('a' <= c && c <= 'z') ? (c & ~040) : c;
70.   }
71.   
72.   char
73.   lowc(c)			/* force 'c' into lowercase */
74.       char c;
75.   {
76.       return ('A' <= c && c <= 'Z') ? (c | 040) : c;
77.   }
78.   #endif /* OVL1 */
79.   
80.   #ifdef OVLB
81.   char *
82.   lcase(s)		/* convert a string into all lowercase */
83.       char *s;
84.   {
85.       register char *p;
86.   
87.       for (p = s; *p; p++)
88.   	if ('A' <= *p && *p <= 'Z') *p |= 040;
89.       return s;
90.   }
91.   #endif /* OVLB */
92.   
93.   #ifdef OVL0
94.   char *
95.   eos(s)			/* return the end of a string (pointing at '\0') */
96.       register char *s;
97.   {
98.       while (*s) s++;	/* s += strlen(s); */
99.       return s;
100.  }
101.  
102.  char *
103.  s_suffix(s)		/* return a name converted to possessive */
104.      const char *s;
105.  {
106.      Static char buf[BUFSZ];
107.  
108.      Strcpy(buf, s);
109.      if(!strcmpi(buf, "it"))
110.  	Strcat(buf, "s");
111.      else if(*(eos(buf)-1) == 's')
112.  	Strcat(buf, "'");
113.      else 
114.  	Strcat(buf, "'s");
115.      return buf;
116.  }
117.  
118.  char *
119.  xcrypt(str)		/* trivial text encryption routine (see makedefs) */
120.  const char *str;
121.  {
122.      Static char buf[BUFSZ];
123.      register const char *p;
124.      register char *q;
125.      register int bitmask;
126.  
127.      for (bitmask = 1, p = str, q = buf; *p; q++) {
128.  	*q = *p++;
129.  	if (*q & (32|64)) *q ^= bitmask;
130.  	if ((bitmask <<= 1) >= 32) bitmask = 1;
131.      }
132.      *q = '\0';
133.      return buf;
134.  }
135.  #endif /* OVL0 */
136.  
137.  #ifdef OVL2
138.  boolean
139.  onlyspace(s)		/* is a string entirely whitespace? */
140.      const char *s;
141.  {
142.      for (; *s; s++)
143.  	if (*s != ' ' && *s != '\t') return FALSE;
144.      return TRUE;
145.  }
146.  #endif /* OVL2 */
147.  
148.  #ifdef OVLB
149.  char *
150.  tabexpand(sbuf)		/* expand tabs into proper number of spaces */
151.      char *sbuf;
152.  {
153.      char buf[BUFSZ];
154.      register char *bp, *s = sbuf;
155.      register int idx;
156.  
157.      if (!*s) return sbuf;
158.  
159.      /* warning: no bounds checking performed */
160.      for (bp = buf, idx = 0; *s; s++)
161.  	if (*s == '\t') {
162.  	    do *bp++ = ' '; while (++idx % 8);
163.  	} else {
164.  	    *bp++ = *s;
165.  	    idx++;
166.  	}
167.      *bp = 0;
168.      return strcpy(sbuf, buf);
169.  }
170.  
171.  char *
172.  visctrl(c)		/* make a displayable string from a character */
173.      char c;
174.  {
175.      Static char ccc[3];
176.  
177.      c &= 0177;
178.  
179.      ccc[2] = '\0';
180.      if (c < 040) {
181.  	ccc[0] = '^';
182.  	ccc[1] = c | 0100;	/* letter */
183.      } else if (c == 0177) {
184.  	ccc[0] = '^';
185.  	ccc[1] = c & ~0100;	/* '?' */
186.      } else {
187.  	ccc[0] = c;		/* printable character */
188.  	ccc[1] = '\0';
189.      }
190.      return ccc;
191.  }
192.  #endif /* OVLB */
193.  
194.  #ifdef OVL2
195.  const char *
196.  ordin(n)		/* return the ordinal suffix of a number */
197.      int n;			/* note: should be non-negative */
198.  {
199.      register int dd = n % 10;
200.  
201.      return (dd == 0 || dd > 3 || (n % 100) / 10 == 1) ? "th" :
202.  	    (dd == 1) ? "st" : (dd == 2) ? "nd" : "rd";
203.  }
204.  #endif /* OVL2 */
205.  
206.  #ifdef OVL1
207.  char *
208.  sitoa(n)		/* make a signed digit string from a number */
209.      int n;
210.  {
211.      Static char buf[13];
212.  
213.      (void) sprintf(buf, (n < 0) ? "%d" : "+%d", n);
214.      return buf;
215.  }
216.  
217.  int
218.  sgn(n)			/* return the sign of a number: -1, 0, or 1 */
219.      int n;
220.  {
221.      return (n < 0) ? -1 : (n != 0);
222.  }
223.  #endif /* OVL1 */
224.  
225.  #ifdef OVLB
226.  int
227.  rounddiv(x, y)		/* calculate x/y, rounding as appropriate */
228.      long x;
229.      int  y;
230.  {
231.      int r, m;
232.      int divsgn = 1;
233.  
234.      if (y == 0)
235.  	panic("division by zero in rounddiv");
236.      else if (y < 0) {
237.  	divsgn = -divsgn;  y = -y;
238.      }
239.      if (x < 0) {
240.  	divsgn = -divsgn;  x = -x;
241.      }
242.      r = x / y;
243.      m = x % y;
244.      if (2*m >= y) r++;
245.  
246.      return divsgn * r;
247.  }
248.  #endif /* OVLB */
249.  
250.  #ifdef OVL0
251.  int
252.  distmin(x0, y0, x1, y1) /* distance between two points, in moves */
253.      int x0, y0, x1, y1;
254.  {
255.      register int dx = x0 - x1, dy = y0 - y1;
256.      if (dx < 0) dx = -dx;
257.      if (dy < 0) dy = -dy;
258.    /*  The minimum number of moves to get from (x0,y0) to (x1,y1) is the
259.     :  larger of the [absolute value of the] two deltas.
260.     */
261.      return (dx < dy) ? dy : dx;
262.  }
263.  
264.  int
265.  dist2(x0, y0, x1, y1)	/* square of euclidean distance between pair of pts */
266.      int x0, y0, x1, y1;
267.  {
268.      register int dx = x0 - x1, dy = y0 - y1;
269.      return dx * dx + dy * dy;
270.  }
271.  
272.  boolean
273.  online2(x0, y0, x1, y1) /* are two points lined up (on a straight line)? */
274.      int x0, y0, x1, y1;
275.  {
276.      register dx = x0 - x1, dy = y0 - y1;
277.    /*  If either delta is zero then they're on an orthogonal line,
278.     :  else if the deltas are equal (signs ignored) they're on a diagonal.
279.     */
280.      return !dy || !dx || (dy == dx) || (dy + dx == 0);	/* (dy == -dx) */
281.  }
282.  
283.  #endif /* OVL0 */
284.  #ifdef OVLB
285.  
286.  boolean
287.  pmatch(patrn, strng)	/* match a string against a pattern */
288.      const char *patrn, *strng;
289.  {
290.      char s, p;
291.    /*
292.     :  Simple pattern matcher:  '*' matches 0 or more characters, '?' matches
293.     :  any single character.  Returns TRUE if 'strng' matches 'patrn'.
294.     */
295.  pmatch_top:
296.      s = *strng++;  p = *patrn++;	/* get next chars and pre-advance */
297.      if (!p)			/* end of pattern */
298.  	return (s == '\0');		/* matches iff end of string too */
299.      else if (p == '*')		/* wildcard reached */
300.  	return (!*patrn || pmatch(patrn, strng-1)) ? TRUE :
301.  		s ? pmatch(patrn-1, strng) : FALSE;
302.      else if (p != s && (p != '?' || !s))  /* check single character */
303.  	return FALSE;		/* doesn't match */
304.      else				/* return pmatch(patrn, strng); */
305.  	goto pmatch_top;	/* optimize tail recursion */
306.  }
307.  #endif /* OVLB */
308.  
309.  #ifdef OVL2
310.  #ifndef STRNCMPI
311.  int
312.  strncmpi(s1, s2, n)	/* case insensitive counted string comparison */
313.      register const char *s1, *s2;
314.      register int n; /*(should probably be size_t, which is usually unsigned)*/
315.  {					/*{ aka strncasecmp }*/
316.      register char t1, t2;
317.  
318.      while (n--) {
319.  	if (!*s2) return (*s1 != 0);	/* s1 >= s2 */
320.  	else if (!*s1) return -1;	/* s1  < s2 */
321.  	t1 = lowc(*s1++);
322.  	t2 = lowc(*s2++);
323.  	if (t1 != t2) return (t1 > t2) ? 1 : -1;
324.      }
325.      return 0;				/* s1 == s2 */
326.  }
327.  #endif	/* STRNCMPI */
328.  #endif /* OVL2 */
329.  
330.  #ifdef OVLB
331.  #ifndef STRSTRI
332.  
333.  /* A note on the #ifndef GCC_WARN: "const" unfortunately has two different
334.   * meanings. One meaning for "const char *str" is that the data pointed
335.   * to by "str" happens not to be modified by this function. That is true
336.   * for strstri. Another meaning is that "str" is (or may be) a pointer
337.   * to a constant string that must never be modified. gcc takes the latter
338.   * interpretation, and warns us about possible subversion of the const
339.   * modifier. Actually, that warning can be useful (we could accidentally
340.   * turn a constant string into a non-constant one with this function)
341.   * and so we note the fact that we're returning "str" as a non-const
342.   * by declaring it as non-const if GCC_WARN is defined.
343.   */
344.  
345.  char *
346.  strstri(str, sub)	/* case insensitive substring search */
347.  #ifndef GCC_WARN
348.      const
349.  #endif
350.      char *str;
351.      const char *sub;
352.  {
353.      register const char *s1, *s2;
354.      register int i, k;
355.  # define TABSIZ 0x20	/* 0x40 would be case-sensitive */
356.      char tstr[TABSIZ], tsub[TABSIZ];	/* nibble count tables */
357.  # if 0
358.      assert( (TABSIZ & ~(TABSIZ-1)) == TABSIZ ); /* must be exact power of 2 */
359.      assert( &lowc != 0 );			/* can't be unsafe macro */
360.  # endif
361.  
362.      /* special case: empty substring */
363.      if (!*sub)	return (char *) str;
364.  
365.      /* do some useful work while determining relative lengths */
366.      for (i = 0; i < TABSIZ; i++)  tstr[i] = tsub[i] = 0;	/* init */
367.      for (k = 0, s1 = str; *s1; k++)  tstr[*s1++ & (TABSIZ-1)]++;
368.      for (	s2 = sub; *s2; --k)  tsub[*s2++ & (TABSIZ-1)]++;
369.  
370.      /* evaluate the info we've collected */
371.      if (k < 0)	return (char *) 0;  /* sub longer than str, so can't match */
372.      for (i = 0; i < TABSIZ; i++)	/* does sub have more 'x's than str? */
373.  	if (tsub[i] > tstr[i])	return (char *) 0;  /* match not possible */
374.  
375.      /* now actually compare the substring repeatedly to parts of the string */
376.      for (i = 0; i <= k; i++) {
377.  	s1 = &str[i];
378.  	s2 = sub;
379.  	while (lowc(*s1++) == lowc(*s2++))
380.  	    if (!*s2)  return (char *) &str[i];		/* full match */
381.      }
382.      return (char *) 0;	/* not found */
383.  }
384.  #endif	/* STRSTRI */
385.  #endif /* OVLB */
386.  
387.  #ifdef OVL2
388.  /*
389.   * Time routines
390.   *
391.   * The time is used for:
392.   *	- seed for rand()
393.   *	- year on tombstone and yymmdd in record file
394.   *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
395.   *	- night and midnight (the undead are dangerous at midnight)
396.   *	- determination of what files are "very old"
397.   */
398.  
399.  #if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60)
400.  extern struct tm *FDECL(localtime,(time_t *));
401.  #endif
402.  static struct tm *NDECL(getlt);
403.  
404.  void
405.  setrandom()
406.  {
407.  	/* the types are different enough here that sweeping the different
408.  	 * routine names into one via #defines is even more confusing
409.  	 */
410.  #ifdef RANDOM	/* srandom() from sys/share/random.c */
411.  	srandom((unsigned int) time((time_t *)0));
412.  #else
413.  # if defined(BSD) || defined(ULTRIX)	/* system srandom() */
414.  #  ifdef BSD
415.  #   if defined(SUNOS4)
416.  	(void)
417.  #   endif
418.  		srandom((int) time((long *)0));
419.  #  else
420.  		srandom((int) time((time_t *)0));
421.  #  endif
422.  # else
423.  #  ifdef UNIX	/* system srand48() */
424.  	srand48((long) time((time_t *)0));
425.  #  else		/* poor quality system routine */
426.  	srand((int) time((time_t *)0));
427.  #  endif
428.  # endif
429.  #endif
430.  }
431.  
432.  static struct tm *
433.  getlt()
434.  {
435.  	time_t date;
436.  
437.  #ifdef BSD
438.  	(void) time((long *)(&date));
439.  #else
440.  	(void) time(&date);
441.  #endif
442.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
443.  	return(localtime((long *)(&date)));
444.  #else
445.  	return(localtime(&date));
446.  #endif
447.  }
448.  
449.  int
450.  getyear()
451.  {
452.  	return(1900 + getlt()->tm_year);
453.  }
454.  
455.  char *
456.  get_date()
457.  {
458.  	Static char datestr[7];
459.  	register struct tm *lt = getlt();
460.  
461.  	Sprintf(datestr, "%2d%2d%2d",
462.  		lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
463.  	if(datestr[2] == ' ') datestr[2] = '0';
464.  	if(datestr[4] == ' ') datestr[4] = '0';
465.  	return(datestr);
466.  }
467.  
468.  int
469.  phase_of_the_moon()			/* 0-7, with 0: new, 4: full */
470.  /* moon period: 2551442 seconds == 29.53058 days */
471.  /* 722578680: date when there was a new moon (Tue Nov 24 04:18 1992) */
472.  /* *8/2551442: divide into 8 phases */
473.  /* *8 +2551442/2 /2551442: let the division round to nearest instead of down */
474.  {
475.  #ifdef BSD
476.  	long now = time((long *)0);
477.  #else
478.  	long now = time((time_t *)0);
479.  #endif
480.  
481.  	return (int) (((((now - 722578680L) % 2551442L) * 8L) + 2551442L/2L)
482.  		/ 2551442L);
483.  }
484.  
485.  boolean
486.  friday_13th()
487.  {
488.  	register struct tm *lt = getlt();
489.  
490.  	return (lt->tm_wday == 5 /* friday */ && lt->tm_mday == 13);
491.  }
492.  
493.  int
494.  night()
495.  {
496.  	register int hour = getlt()->tm_hour;
497.  
498.  	return(hour < 6 || hour > 21);
499.  }
500.  
501.  int
502.  midnight()
503.  {
504.  	return(getlt()->tm_hour == 0);
505.  }
506.  #endif /* OVL2 */
507.  
508.  /*hacklib.c*/

Also on Fandom

Random Wiki