1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.unix.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.unix.c,v 1.8 1999/11/16 02:57:13 billf Exp $ */
4 /* $DragonFly: src/games/hack/hack.unix.c,v 1.7 2006/08/21 19:45:32 pavalos Exp $ */
6 /* This file collects some Unix dependencies; hack.pager.c contains some more */
9 * The time is used for:
11 * - year on tombstone and yymmdd in record file
12 * - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
13 * - night and midnight (the undead are dangerous at midnight)
14 * - determination of what files are "very old"
18 #include "hack.h" /* mainly for index() which depends on BSD */
20 #include <sys/types.h> /* for time_t and stat */
22 #include <sys/param.h>
26 static struct tm *getlt(void);
27 static bool veryold(int);
29 static void newmail(void);
30 static void mdrush(struct monst *, bool);
45 return(localtime(&date));
51 return(1900 + getlt()->tm_year);
57 static char datestr[7];
58 struct tm *lt = getlt();
60 snprintf(datestr, sizeof(datestr), "%02d%02d%02d",
61 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
66 phase_of_the_moon(void) /* 0-7, with 0: new, 4: full */
67 { /* moon period: 29.5306 days */
68 /* year: 365.2422 days */
69 struct tm *lt = getlt();
70 int epact, diy, golden;
73 golden = (lt->tm_year % 19) + 1;
74 epact = (11 * golden + 18) % 30;
75 if ((epact == 25 && golden > 11) || epact == 24)
78 return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
84 int hour = getlt()->tm_hour;
86 return(hour < 6 || hour > 21);
92 return(getlt()->tm_hour == 0);
95 struct stat buf, hbuf;
98 gethdate(const char *name)
101 char filename[MAXPATHLEN+1];
103 if (strchr(name, '/') != NULL || (p = getenv("PATH")) == NULL)
105 np = path = strdup(p); /* Make a copy for strsep. */
110 if ((p = strsep(&np, ":")) == NULL)
112 if (*p == '\0') /* :: */
113 (void) strlcpy(filename, name, sizeof(filename));
115 (void) snprintf(filename, sizeof(filename),
118 if (stat(filename, &hbuf) == 0) {
123 error("Cannot get status of %s.",
124 (p = strrchr(name, '/')) ? p+1 : name);
131 if(fstat(fd, &buf)) {
132 pline("Cannot get status of saved level? ");
135 if(buf.st_mtime < hbuf.st_mtime) {
136 pline("Saved level is out of date. ");
142 /* see whether we should throw away this xlock file */
149 if(fstat(fd, &buf)) return(0); /* cannot get status */
150 if(buf.st_size != sizeof(int)) return(0); /* not an xlock file */
152 if(date - buf.st_mtime < 3L*24L*60L*60L) { /* recent */
153 int lockedpid; /* should be the same size as hackpid */
155 if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
160 /* From: Rick Adams <seismo!rick> */
161 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5. */
162 /* It will do nothing on V7 or 4.1bsd. */
163 if(!(kill(lockedpid, 0) == -1 && errno == ESRCH))
167 for(i = 1; i <= MAXLEVEL; i++) { /* try to remove all */
172 if(unlink(lock)) return(0); /* cannot remove it */
173 return(1); /* success! */
183 /* we ignore QUIT and INT at this point */
184 if (link(HLOCK, LLOCK) == -1) {
188 printf("Cannot link %s to %s\n", LLOCK, HLOCK);
191 printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
194 printf("It seems you don't have write permission here.\n");
197 printf("(Try again or rm %s.)\n", LLOCK);
200 printf("I don't know what is wrong.");
209 if(locknum > 25) locknum = 25;
212 if(locknum) lock[0] = 'a' + i++;
214 if((fd = open(lock, 0)) == -1) {
215 if(errno == ENOENT) goto gotlock; /* no such file */
218 error("Cannot open %s", lock);
221 if(veryold(fd)) /* if true, this closes fd and unlinks lock */
224 } while(i < locknum);
227 error(locknum ? "Too many hacks running now."
228 : "There is a game in progress under your name.");
230 fd = creat(lock, FMASK);
231 if(unlink(LLOCK) == -1)
232 error("Cannot unlink %s.", LLOCK);
234 error("cannot creat lock file.");
236 if(write(fd, (char *) &hackpid, sizeof(hackpid))
238 error("cannot write lock");
240 if(close(fd) == -1) {
241 error("cannot close lock");
249 * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
250 * I don't know the details of his implementation.]
251 * { Later note: he disliked my calling a general mailreader and felt that
252 * hack should do the paging itself. But when I get mail, I want to put it
253 * in some folder, reply, etc. - it would be unreasonable to put all these
254 * functions in hack. }
255 * The mail daemon '2' is at present not a real monster, but only a visual
256 * effect. Thus, makemon() is superfluous. This might become otherwise,
257 * however. The motion of '2' is less restrained than usual: diagonal moves
258 * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
259 * in a ROOM, even when you are Blind.
260 * Its path should be longer when you are Telepat-hic and Blind.
262 * Interesting side effects:
263 * - You can get rich by sending yourself a lot of mail and selling
264 * it to the shopkeeper. Unfortunately mail isn't very valuable.
265 * - You might die in case '2' comes along at a critical moment during
266 * a fight and delivers a scroll the weight of which causes you to
269 * Possible extensions:
270 * - Open the file MAIL and do fstat instead of stat for efficiency.
271 * (But sh uses stat, so this cannot be too bad.)
272 * - Examine the mail and produce a scroll of mail called "From somebody".
273 * - Invoke MAILREADER in such a way that only this single letter is read.
275 * - Make him lose his mail when a Nymph steals the letter.
276 * - Do something to the text when the scroll is enchanted or cancelled.
278 #include "def.mkroom.h"
279 static struct stat omstat,nmstat;
280 static char *mailbox;
281 static long laststattime;
286 if(!(mailbox = getenv("MAIL")))
288 if(stat(mailbox, &omstat)){
289 #ifdef PERMANENT_MAILBOX
290 pline("Cannot get status of MAIL=%s .", mailbox);
294 #endif /* PERMANENT_MAILBOX */
303 || moves < laststattime + MAILCKFREQ
304 #endif /* MAILCKFREQ */
307 laststattime = moves;
308 if(stat(mailbox, &nmstat)){
309 #ifdef PERMANENT_MAILBOX
310 pline("Cannot get status of MAIL=%s anymore.", mailbox);
314 #endif /* PERMANENT_MAILBOX */
315 } else if(nmstat.st_mtime > omstat.st_mtime) {
318 getmailstatus(); /* might be too late ... */
325 /* produce a scroll of mail */
328 extern struct permonst pm_mail_daemon;
330 obj = mksobj(SCR_MAIL);
331 if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
334 pline("\"Hello, %s! I have some mail for you.\"", plname);
336 if(dist(md->mx,md->my) > 2)
340 /* let him disappear again */
346 identify(obj); /* set known and do prinv() */
349 /* make md run through the cave */
351 mdrush(struct monst *md, bool away)
353 int uroom = inroom(u.ux, u.uy);
355 int tmp = rooms[uroom].fdoor;
356 int cnt = rooms[uroom].doorct;
357 int fx = u.ux, fy = u.uy;
359 if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){
365 tmp_at(-1, md->data->mlet); /* open call */
366 if(away) { /* interchange origin and destination */
368 tmp = fx; fx = md->mx; md->mx = tmp;
369 tmp = fy; fy = md->my; md->my = tmp;
371 while(fx != md->mx || fy != md->my) {
372 int dx,dy,nfx = fx,nfy = fy,d1,d2;
375 d1 = DIST(fx,fy,md->mx,md->my);
376 for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
378 d2 = DIST(fx+dx,fy+dy,md->mx,md->my);
385 if(nfx != fx || nfy != fy) {
396 tmp_at(-1,-1); /* close call */
405 #ifdef DEF_MAILREADER /* This implies that UNIX is defined */
408 if(!(mr = getenv("MAILREADER")))
414 #else /* DEF_MAILREADER */
415 page_file(mailbox, FALSE);
416 #endif /* DEF_MAILREADER */
417 /* get new stat; not entirely correct: there is a small time
418 window where we do not see new mail */
424 regularize(char *s) /* normalize file name - we don't like ..'s or /'s */
428 while((lp = index(s, '.')) || (lp = index(s, '/')))