Wikia

Wikihack

Source:NetHack 3.2.0/hacklib.c

2,032pages on
this wiki
Talk0

Below is the full text to hacklib.c from the source code of NetHack 3.2.0. To link to a particular line, write [[NetHack 3.2.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.2	95/08/04	*/
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 *		eos		(char *)
19.   	char *		s_suffix	(const char *)
20.   	char *		xcrypt		(const char *, 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 *		yymmdd		(time_t)
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((boolean)('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((boolean)(('@' <= 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((char)(('a' <= c && c <= 'z') ? (c & ~040) : c));
70.   }
71.   
72.   char
73.   lowc(c)			/* force 'c' into lowercase */
74.       char c;
75.   {
76.       return((char)(('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, buf)	/* trivial text encryption routine (see makedefs) */
120.  const char *str;
121.  char *buf;
122.  {
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((boolean)(!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((boolean)(s == '\0'));		/* matches iff end of string too */
299.      else if (p == '*')		/* wildcard reached */
300.  	return((boolean)((!*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.  char *
334.  strstri(str, sub)	/* case insensitive substring search */
335.      const char *str;
336.      const char *sub;
337.  {
338.      register const char *s1, *s2;
339.      register int i, k;
340.  # define TABSIZ 0x20	/* 0x40 would be case-sensitive */
341.      char tstr[TABSIZ], tsub[TABSIZ];	/* nibble count tables */
342.  # if 0
343.      assert( (TABSIZ & ~(TABSIZ-1)) == TABSIZ ); /* must be exact power of 2 */
344.      assert( &lowc != 0 );			/* can't be unsafe macro */
345.  # endif
346.  
347.      /* special case: empty substring */
348.      if (!*sub)	return (char *) str;
349.  
350.      /* do some useful work while determining relative lengths */
351.      for (i = 0; i < TABSIZ; i++)  tstr[i] = tsub[i] = 0;	/* init */
352.      for (k = 0, s1 = str; *s1; k++)  tstr[*s1++ & (TABSIZ-1)]++;
353.      for (	s2 = sub; *s2; --k)  tsub[*s2++ & (TABSIZ-1)]++;
354.  
355.      /* evaluate the info we've collected */
356.      if (k < 0)	return (char *) 0;  /* sub longer than str, so can't match */
357.      for (i = 0; i < TABSIZ; i++)	/* does sub have more 'x's than str? */
358.  	if (tsub[i] > tstr[i])	return (char *) 0;  /* match not possible */
359.  
360.      /* now actually compare the substring repeatedly to parts of the string */
361.      for (i = 0; i <= k; i++) {
362.  	s1 = &str[i];
363.  	s2 = sub;
364.  	while (lowc(*s1++) == lowc(*s2++))
365.  	    if (!*s2)  return (char *) &str[i];		/* full match */
366.      }
367.      return (char *) 0;	/* not found */
368.  }
369.  #endif	/* STRSTRI */
370.  #endif /* OVLB */
371.  
372.  #ifdef OVL2
373.  /*
374.   * Time routines
375.   *
376.   * The time is used for:
377.   *	- seed for rand()
378.   *	- year on tombstone and yymmdd in record file
379.   *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
380.   *	- night and midnight (the undead are dangerous at midnight)
381.   *	- determination of what files are "very old"
382.   */
383.  
384.  #if defined(AMIGA) && !defined(AZTEC_C) && !defined(__SASC_60) && !defined(_DCC)
385.  extern struct tm *FDECL(localtime,(time_t *));
386.  #endif
387.  static struct tm *NDECL(getlt);
388.  
389.  void
390.  setrandom()
391.  {
392.  	/* the types are different enough here that sweeping the different
393.  	 * routine names into one via #defines is even more confusing
394.  	 */
395.  #ifdef RANDOM	/* srandom() from sys/share/random.c */
396.  	srandom((unsigned int) time((time_t *)0));
397.  #else
398.  # if defined(BSD) || defined(ULTRIX)	/* system srandom() */
399.  #  ifdef BSD
400.  #   if defined(SUNOS4)
401.  	(void)
402.  #   endif
403.  		srandom((int) time((long *)0));
404.  #  else
405.  		srandom((int) time((time_t *)0));
406.  #  endif
407.  # else
408.  #  ifdef UNIX	/* system srand48() */
409.  	srand48((long) time((time_t *)0));
410.  #  else		/* poor quality system routine */
411.  	srand((int) time((time_t *)0));
412.  #  endif
413.  # endif
414.  #endif
415.  }
416.  
417.  static struct tm *
418.  getlt()
419.  {
420.  	time_t date;
421.  
422.  #ifdef BSD
423.  	(void) time((long *)(&date));
424.  #else
425.  	(void) time(&date);
426.  #endif
427.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
428.  	return(localtime((long *)(&date)));
429.  #else
430.  	return(localtime(&date));
431.  #endif
432.  }
433.  
434.  int
435.  getyear()
436.  {
437.  	return(1900 + getlt()->tm_year);
438.  }
439.  
440.  char *
441.  yymmdd(date)
442.  time_t date;
443.  {
444.  	Static char datestr[7];
445.  	struct tm *lt;
446.  
447.  	if (date == 0)
448.  		lt = getlt();
449.  	else
450.  #if (defined(ULTRIX) && !(defined(ULTRIX_PROTO) || defined(NHSTDC))) || defined(BSD)
451.  		lt = localtime((long *)(&date));
452.  #else
453.  		lt = localtime(&date);
454.  #endif
455.  
456.  	Sprintf(datestr, "%02d%02d%02d",
457.  		lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
458.  	return(datestr);
459.  }
460.  
461.  /*
462.   * moon period = 29.53058 days ~= 30, year = 365.2422 days
463.   * days moon phase advances on first day of year compared to preceding year
464.   *	= 365.2422 - 12*29.53058 ~= 11
465.   * years in Metonic cycle (time until same phases fall on the same days of
466.   *	the month) = 18.6 ~= 19
467.   * moon phase on first day of year (epact) ~= (11*(year%19) + 29) % 30
468.   *	(29 as initial condition)
469.   * current phase in days = first day phase + days elapsed in year
470.   * 6 moons ~= 177 days
471.   * 177 ~= 8 reported phases * 22
472.   * + 11/22 for rounding
473.   */
474.  int
475.  phase_of_the_moon()		/* 0-7, with 0: new, 4: full */
476.  {
477.  	register struct tm *lt = getlt();
478.  	register int epact, diy, goldn;
479.  
480.  	diy = lt->tm_yday;
481.  	goldn = (lt->tm_year % 19) + 1;
482.  	epact = (11 * goldn + 18) % 30;
483.  	if ((epact == 25 && goldn > 11) || epact == 24)
484.  		epact++;
485.  
486.  	return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
487.  }
488.  
489.  boolean
490.  friday_13th()
491.  {
492.  	register struct tm *lt = getlt();
493.  
494.  	return((boolean)(lt->tm_wday == 5 /* friday */ && lt->tm_mday == 13));
495.  }
496.  
497.  int
498.  night()
499.  {
500.  	register int hour = getlt()->tm_hour;
501.  
502.  	return(hour < 6 || hour > 21);
503.  }
504.  
505.  int
506.  midnight()
507.  {
508.  	return(getlt()->tm_hour == 0);
509.  }
510.  #endif /* OVL2 */
511.  
512.  /*hacklib.c*/

Around Wikia's network

Random Wiki