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