pc-hack

PC HACK 3.61 source code (archival)
git clone http://frotz.net/git/pc-hack.git
Log | Files | Refs

unix.c (1813B)


      1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
      2 /* unix.c - version 1.0.3 */
      3 
      4 /* This file collects some Unix dependencies; pager.c contains some more */
      5 
      6 /*
      7  * The time is used for:
      8  *	- seed for rand()
      9  *	- year on tombstone and yymmdd in record file
     10  *	- phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
     11  *	- night and midnight (the undead are dangerous at midnight)
     12  *	- determination of what files are "very old"
     13  */
     14 
     15 #include "hack.h"	/* mainly for index() which depends on BSD */
     16 
     17 #include	<sys/types.h>		/* for time_t */
     18 #include	<time.h>
     19 
     20 extern time_t time();
     21 
     22 setrandom()
     23 {
     24  	(void) srand((int) time ((time_t *) 0));
     25 }
     26 
     27 struct tm *
     28 getlt()
     29 {
     30 	time_t date;
     31 	struct tm *localtime();
     32 
     33 	(void) time(&date);
     34 	return(localtime(&date));
     35 }
     36 
     37 getyear()
     38 {
     39 	return(1900 + getlt()->tm_year);
     40 }
     41 
     42 char *
     43 getdate()
     44 {
     45 	static char datestr[7];
     46 	register struct tm *lt = getlt();
     47 
     48 	(void) sprintf(datestr, "%2d%2d%2d",
     49 		lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
     50 	if(datestr[2] == ' ') datestr[2] = '0';
     51 	if(datestr[4] == ' ') datestr[4] = '0';
     52 	return(datestr);
     53 }
     54 
     55 phase_of_the_moon()			/* 0-7, with 0: new, 4: full */
     56 {					/* moon period: 29.5306 days */
     57 					/* year: 365.2422 days */
     58 	register struct tm *lt = getlt();
     59 	register int epact, diy, golden;
     60 
     61 	diy = lt->tm_yday;
     62 	golden = (lt->tm_year % 19) + 1;
     63 	epact = (11 * golden + 18) % 30;
     64 	if ((epact == 25 && golden > 11) || epact == 24)
     65 		epact++;
     66 
     67 	return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
     68 }
     69 
     70 night()
     71 {
     72 	register int hour = getlt()->tm_hour;
     73 
     74 	return(hour < 6 || hour > 21);
     75 }
     76 
     77 midnight()
     78 {
     79 	return(getlt()->tm_hour == 0);
     80 }
     81 
     82 regularize(s)	/* normalize file name - we don't like ..'s or /'s */
     83 register char *s;
     84 {
     85 	register char *lp;
     86 
     87 	while((lp = index(s, '.')) || (lp = index(s, '/')))
     88 		*lp = '_';
     89 }