]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/hack/hack.unix.c
Add /etc/defaults.
[FreeBSD/FreeBSD.git] / games / hack / hack.unix.c
1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.unix.c - version 1.0.3 */
3
4 /* This file collects some Unix dependencies; hack.pager.c contains some more */
5
6 /*
7  * The time is used for:
8  *      - seed for random()
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 <stdio.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include "hack.h"       /* mainly for index() which depends on BSD */
19
20 #include        <sys/types.h>           /* for time_t and stat */
21 #include        <sys/stat.h>
22 #include        <time.h>
23
24 setrandom()
25 {
26         (void) srandomdev();
27 }
28
29 struct tm *
30 getlt()
31 {
32         time_t date;
33         struct tm *localtime();
34
35         (void) time(&date);
36         return(localtime(&date));
37 }
38
39 getyear()
40 {
41         return(1900 + getlt()->tm_year);
42 }
43
44 char *
45 getdate()
46 {
47         static char datestr[7];
48         register struct tm *lt = getlt();
49
50         (void) snprintf(datestr, sizeof(datestr), "%02d%02d%02d",
51                 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday);
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 struct stat buf, hbuf;
83
84 gethdate(name) char *name; {
85 /* old version - for people short of space */
86 register char *np;
87
88         name = "/usr/games/hide/hack";
89         if(stat(name, &hbuf))
90                 error("Cannot get status of %s.",
91                         (np = rindex(name, '/')) ? np+1 : name);
92 #if 0
93 /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
94
95
96 /*
97  * The problem with   #include  <sys/param.h>   is that this include file
98  * does not exist on all systems, and moreover, that it sometimes includes
99  * <sys/types.h> again, so that the compiler sees these typedefs twice.
100  */
101 #define         MAXPATHLEN      1024
102
103 register char *np, *path;
104 char filename[MAXPATHLEN+1];
105         if (index(name, '/') != NULL || (path = getenv("PATH")) == NULL)
106                 path = "";
107
108         for (;;) {
109                 if ((np = index(path, ':')) == NULL)
110                         np = path + strlen(path);       /* point to end str */
111                 if (np - path <= 1)                     /* %% */
112                         (void) strcpy(filename, name);
113                 else {
114                         (void) strncpy(filename, path, np - path);
115                         filename[np - path] = '/';
116                         (void) strcpy(filename + (np - path) + 1, name);
117                 }
118                 if (stat(filename, &hbuf) == 0)
119                         return;
120                 if (*np == '\0')
121                         break;
122                 path = np + 1;
123         }
124         error("Cannot get status of %s.",
125                 (np = rindex(name, '/')) ? np+1 : name);
126 #endif
127 }
128
129 uptodate(fd) {
130         if(fstat(fd, &buf)) {
131                 pline("Cannot get status of saved level? ");
132                 return(0);
133         }
134         if(buf.st_mtime < hbuf.st_mtime) {
135                 pline("Saved level is out of date. ");
136                 return(0);
137         }
138         return(1);
139 }
140
141 /* see whether we should throw away this xlock file */
142 veryold(fd) {
143         register int i;
144         time_t date;
145
146         if(fstat(fd, &buf)) return(0);                  /* cannot get status */
147         if(buf.st_size != sizeof(int)) return(0);       /* not an xlock file */
148         (void) time(&date);
149         if(date - buf.st_mtime < 3L*24L*60L*60L) {      /* recent */
150                 extern int errno;
151                 int lockedpid;  /* should be the same size as hackpid */
152
153                 if(read(fd, (char *)&lockedpid, sizeof(lockedpid)) !=
154                         sizeof(lockedpid))
155                         /* strange ... */
156                         return(0);
157
158                 /* From: Rick Adams <seismo!rick> */
159                 /* This will work on 4.1cbsd, 4.2bsd and system 3? & 5. */
160                 /* It will do nothing on V7 or 4.1bsd. */
161                 if(!(kill(lockedpid, 0) == -1 && errno == ESRCH))
162                         return(0);
163         }
164         (void) close(fd);
165         for(i = 1; i <= MAXLEVEL; i++) {                /* try to remove all */
166                 glo(i);
167                 (void) unlink(lock);
168         }
169         glo(0);
170         if(unlink(lock)) return(0);                     /* cannot remove it */
171         return(1);                                      /* success! */
172 }
173
174 getlock()
175 {
176         extern int errno, hackpid, locknum;
177         register int i = 0, fd;
178
179         (void) fflush(stdout);
180
181         /* we ignore QUIT and INT at this point */
182         if (link(HLOCK, LLOCK) == -1) {
183                 register int errnosv = errno;
184
185                 perror(HLOCK);
186                 printf("Cannot link %s to %s\n", LLOCK, HLOCK);
187                 switch(errnosv) {
188                 case ENOENT:
189                     printf("Perhaps there is no (empty) file %s ?\n", HLOCK);
190                     break;
191                 case EACCES:
192                     printf("It seems you don't have write permission here.\n");
193                     break;
194                 case EEXIST:
195                     printf("(Try again or rm %s.)\n", LLOCK);
196                     break;
197                 default:
198                     printf("I don't know what is wrong.");
199                 }
200                 getret();
201                 error("");
202                 /*NOTREACHED*/
203         }
204
205         regularize(lock);
206         glo(0);
207         if(locknum > 25) locknum = 25;
208
209         do {
210                 if(locknum) lock[0] = 'a' + i++;
211
212                 if((fd = open(lock, 0)) == -1) {
213                         if(errno == ENOENT) goto gotlock;    /* no such file */
214                         perror(lock);
215                         (void) unlink(LLOCK);
216                         error("Cannot open %s", lock);
217                 }
218
219                 if(veryold(fd)) /* if true, this closes fd and unlinks lock */
220                         goto gotlock;
221                 (void) close(fd);
222         } while(i < locknum);
223
224         (void) unlink(LLOCK);
225         error(locknum ? "Too many hacks running now."
226                       : "There is a game in progress under your name.");
227 gotlock:
228         fd = creat(lock, FMASK);
229         if(unlink(LLOCK) == -1)
230                 error("Cannot unlink %s.", LLOCK);
231         if(fd == -1) {
232                 error("cannot creat lock file.");
233         } else {
234                 if(write(fd, (char *) &hackpid, sizeof(hackpid))
235                     != sizeof(hackpid)){
236                         error("cannot write lock");
237                 }
238                 if(close(fd) == -1) {
239                         error("cannot close lock");
240                 }
241         }
242 }
243
244 #ifdef MAIL
245
246 /*
247  * Notify user when new mail has arrived. [Idea from Merlyn Leroy, but
248  * I don't know the details of his implementation.]
249  * { Later note: he disliked my calling a general mailreader and felt that
250  *   hack should do the paging itself. But when I get mail, I want to put it
251  *   in some folder, reply, etc. - it would be unreasonable to put all these
252  *   functions in hack. }
253  * The mail daemon '2' is at present not a real monster, but only a visual
254  * effect. Thus, makemon() is superfluous. This might become otherwise,
255  * however. The motion of '2' is less restrained than usual: diagonal moves
256  * from a DOOR are possible. He might also use SDOOR's. Also, '2' is visible
257  * in a ROOM, even when you are Blind.
258  * Its path should be longer when you are Telepat-hic and Blind.
259  *
260  * Interesting side effects:
261  *      - You can get rich by sending yourself a lot of mail and selling
262  *        it to the shopkeeper. Unfortunately mail isn't very valuable.
263  *      - You might die in case '2' comes along at a critical moment during
264  *        a fight and delivers a scroll the weight of which causes you to
265  *        collapse.
266  *
267  * Possible extensions:
268  *      - Open the file MAIL and do fstat instead of stat for efficiency.
269  *        (But sh uses stat, so this cannot be too bad.)
270  *      - Examine the mail and produce a scroll of mail called "From somebody".
271  *      - Invoke MAILREADER in such a way that only this single letter is read.
272  *
273  *      - Make him lose his mail when a Nymph steals the letter.
274  *      - Do something to the text when the scroll is enchanted or cancelled.
275  */
276 #include        "def.mkroom.h"
277 static struct stat omstat,nmstat;
278 static char *mailbox;
279 static long laststattime;
280
281 getmailstatus() {
282         if(!(mailbox = getenv("MAIL")))
283                 return;
284         if(stat(mailbox, &omstat)){
285 #ifdef PERMANENT_MAILBOX
286                 pline("Cannot get status of MAIL=%s .", mailbox);
287                 mailbox = 0;
288 #else
289                 omstat.st_mtime = 0;
290 #endif PERMANENT_MAILBOX
291         }
292 }
293
294 ckmailstatus() {
295         if(!mailbox
296 #ifdef MAILCKFREQ
297                     || moves < laststattime + MAILCKFREQ
298 #endif MAILCKFREQ
299                                                         )
300                 return;
301         laststattime = moves;
302         if(stat(mailbox, &nmstat)){
303 #ifdef PERMANENT_MAILBOX
304                 pline("Cannot get status of MAIL=%s anymore.", mailbox);
305                 mailbox = 0;
306 #else
307                 nmstat.st_mtime = 0;
308 #endif PERMANENT_MAILBOX
309         } else if(nmstat.st_mtime > omstat.st_mtime) {
310                 if(nmstat.st_size)
311                         newmail();
312                 getmailstatus();        /* might be too late ... */
313         }
314 }
315
316 newmail() {
317         /* produce a scroll of mail */
318         register struct obj *obj;
319         register struct monst *md;
320         extern char plname[];
321         extern struct obj *mksobj(), *addinv();
322         extern struct monst *makemon();
323         extern struct permonst pm_mail_daemon;
324
325         obj = mksobj(SCR_MAIL);
326         if(md = makemon(&pm_mail_daemon, u.ux, u.uy)) /* always succeeds */
327                 mdrush(md,0);
328
329         pline("\"Hello, %s! I have some mail for you.\"", plname);
330         if(md) {
331                 if(dist(md->mx,md->my) > 2)
332                         pline("\"Catch!\"");
333                 more();
334
335                 /* let him disappear again */
336                 mdrush(md,1);
337                 mondead(md);
338         }
339
340         obj = addinv(obj);
341         (void) identify(obj);           /* set known and do prinv() */
342 }
343
344 /* make md run through the cave */
345 mdrush(md,away)
346 register struct monst *md;
347 boolean away;
348 {
349         register int uroom = inroom(u.ux, u.uy);
350         if(uroom >= 0) {
351                 register int tmp = rooms[uroom].fdoor;
352                 register int cnt = rooms[uroom].doorct;
353                 register int fx = u.ux, fy = u.uy;
354                 while(cnt--) {
355                         if(dist(fx,fy) < dist(doors[tmp].x, doors[tmp].y)){
356                                 fx = doors[tmp].x;
357                                 fy = doors[tmp].y;
358                         }
359                         tmp++;
360                 }
361                 tmp_at(-1, md->data->mlet);     /* open call */
362                 if(away) {      /* interchange origin and destination */
363                         unpmon(md);
364                         tmp = fx; fx = md->mx; md->mx = tmp;
365                         tmp = fy; fy = md->my; md->my = tmp;
366                 }
367                 while(fx != md->mx || fy != md->my) {
368                         register int dx,dy,nfx = fx,nfy = fy,d1,d2;
369
370                         tmp_at(fx,fy);
371                         d1 = DIST(fx,fy,md->mx,md->my);
372                         for(dx = -1; dx <= 1; dx++) for(dy = -1; dy <= 1; dy++)
373                             if(dx || dy) {
374                                 d2 = DIST(fx+dx,fy+dy,md->mx,md->my);
375                                 if(d2 < d1) {
376                                     d1 = d2;
377                                     nfx = fx+dx;
378                                     nfy = fy+dy;
379                                 }
380                             }
381                         if(nfx != fx || nfy != fy) {
382                             fx = nfx;
383                             fy = nfy;
384                         } else {
385                             if(!away) {
386                                 md->mx = fx;
387                                 md->my = fy;
388                             }
389                             break;
390                         }
391                 }
392                 tmp_at(-1,-1);                  /* close call */
393         }
394         if(!away)
395                 pmon(md);
396 }
397
398 readmail() {
399 #ifdef DEF_MAILREADER                   /* This implies that UNIX is defined */
400         register char *mr = 0;
401         more();
402         if(!(mr = getenv("MAILREADER")))
403                 mr = DEF_MAILREADER;
404         if(child(1)){
405                 execl(mr, mr, (char *) 0);
406                 exit(1);
407         }
408 #else DEF_MAILREADER
409         (void) page_file(mailbox, FALSE);
410 #endif DEF_MAILREADER
411         /* get new stat; not entirely correct: there is a small time
412            window where we do not see new mail */
413         getmailstatus();
414 }
415 #endif MAIL
416
417 regularize(s)   /* normalize file name - we don't like ..'s or /'s */
418 register char *s;
419 {
420         register char *lp;
421
422         while((lp = index(s, '.')) || (lp = index(s, '/')))
423                 *lp = '_';
424 }