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