hack.main.c revision 1.6 1 1.6 jsm /* $NetBSD: hack.main.c,v 1.6 2001/03/25 20:44:01 jsm Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.2 mycroft */
6 1.2 mycroft
7 1.4 christos #include <sys/cdefs.h>
8 1.2 mycroft #ifndef lint
9 1.6 jsm __RCSID("$NetBSD: hack.main.c,v 1.6 2001/03/25 20:44:01 jsm Exp $");
10 1.4 christos #endif /* not lint */
11 1.1 cgd
12 1.1 cgd #include <signal.h>
13 1.4 christos #include <stdlib.h>
14 1.4 christos #include <unistd.h>
15 1.4 christos #include <fcntl.h>
16 1.1 cgd #include "hack.h"
17 1.4 christos #include "extern.h"
18 1.1 cgd
19 1.1 cgd #ifdef QUEST
20 1.1 cgd #define gamename "quest"
21 1.1 cgd #else
22 1.1 cgd #define gamename "hack"
23 1.1 cgd #endif
24 1.1 cgd
25 1.4 christos int (*afternmv) __P((void));
26 1.4 christos int (*occupation) __P((void));
27 1.6 jsm const char *occtxt; /* defined when occupation != NULL */
28 1.1 cgd
29 1.4 christos int hackpid; /* current pid */
30 1.4 christos int locknum; /* max num of players */
31 1.1 cgd #ifdef DEF_PAGER
32 1.6 jsm const char *catmore; /* default pager */
33 1.1 cgd #endif
34 1.4 christos char SAVEF[PL_NSIZ + 11] = "save/"; /* save/99999player */
35 1.4 christos char *hname; /* name of the game (argv[0] of call) */
36 1.4 christos char obuf[BUFSIZ]; /* BUFSIZ is defined in stdio.h */
37 1.4 christos
38 1.4 christos int main __P((int, char *[]));
39 1.6 jsm static void chdirx __P((const char *, boolean));
40 1.4 christos
41 1.4 christos int
42 1.4 christos main(argc, argv)
43 1.4 christos int argc;
44 1.4 christos char *argv[];
45 1.1 cgd {
46 1.4 christos int fd;
47 1.1 cgd #ifdef CHDIR
48 1.4 christos char *dir;
49 1.1 cgd #endif
50 1.1 cgd
51 1.6 jsm /* Check for dirty tricks with closed fds 0, 1, 2 */
52 1.6 jsm fd = open("/dev/null", O_RDONLY);
53 1.6 jsm if (fd < 3)
54 1.6 jsm exit(1);
55 1.6 jsm close(fd);
56 1.6 jsm
57 1.1 cgd hname = argv[0];
58 1.1 cgd hackpid = getpid();
59 1.1 cgd
60 1.1 cgd #ifdef CHDIR /* otherwise no chdir() */
61 1.1 cgd /*
62 1.1 cgd * See if we must change directory to the playground.
63 1.1 cgd * (Perhaps hack runs suid and playground is inaccessible
64 1.1 cgd * for the player.)
65 1.1 cgd * The environment variable HACKDIR is overridden by a
66 1.1 cgd * -d command line option (must be the first option given)
67 1.1 cgd */
68 1.1 cgd
69 1.1 cgd dir = getenv("HACKDIR");
70 1.4 christos if (argc > 1 && !strncmp(argv[1], "-d", 2)) {
71 1.1 cgd argc--;
72 1.1 cgd argv++;
73 1.4 christos dir = argv[0] + 2;
74 1.4 christos if (*dir == '=' || *dir == ':')
75 1.4 christos dir++;
76 1.4 christos if (!*dir && argc > 1) {
77 1.1 cgd argc--;
78 1.1 cgd argv++;
79 1.1 cgd dir = argv[0];
80 1.1 cgd }
81 1.4 christos if (!*dir)
82 1.4 christos error("Flag -d must be followed by a directory name.");
83 1.1 cgd }
84 1.1 cgd #endif
85 1.1 cgd
86 1.1 cgd /*
87 1.1 cgd * Who am i? Algorithm: 1. Use name as specified in HACKOPTIONS
88 1.1 cgd * 2. Use $USER or $LOGNAME (if 1. fails)
89 1.1 cgd * 3. Use getlogin() (if 2. fails)
90 1.1 cgd * The resulting name is overridden by command line options.
91 1.1 cgd * If everything fails, or if the resulting name is some generic
92 1.1 cgd * account like "games", "play", "player", "hack" then eventually
93 1.1 cgd * we'll ask him.
94 1.1 cgd * Note that we trust him here; it is possible to play under
95 1.1 cgd * somebody else's name.
96 1.1 cgd */
97 1.4 christos {
98 1.4 christos char *s;
99 1.1 cgd
100 1.4 christos initoptions();
101 1.4 christos if (!*plname && (s = getenv("USER")))
102 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
103 1.4 christos if (!*plname && (s = getenv("LOGNAME")))
104 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
105 1.4 christos if (!*plname && (s = getlogin()))
106 1.4 christos (void) strncpy(plname, s, sizeof(plname) - 1);
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * Now we know the directory containing 'record' and
111 1.1 cgd * may do a prscore().
112 1.1 cgd */
113 1.4 christos if (argc > 1 && !strncmp(argv[1], "-s", 2)) {
114 1.1 cgd #ifdef CHDIR
115 1.4 christos chdirx(dir, 0);
116 1.1 cgd #endif
117 1.1 cgd prscore(argc, argv);
118 1.1 cgd exit(0);
119 1.1 cgd }
120 1.1 cgd /*
121 1.1 cgd * It seems he really wants to play.
122 1.1 cgd * Remember tty modes, to be restored on exit.
123 1.1 cgd */
124 1.1 cgd gettty();
125 1.4 christos setbuf(stdout, obuf);
126 1.1 cgd setrandom();
127 1.1 cgd startup();
128 1.1 cgd cls();
129 1.4 christos u.uhp = 1; /* prevent RIP on early quits */
130 1.4 christos u.ux = FAR; /* prevent nscr() */
131 1.1 cgd (void) signal(SIGHUP, hangup);
132 1.1 cgd
133 1.1 cgd /*
134 1.1 cgd * Find the creation date of this game,
135 1.1 cgd * so as to avoid restoring outdated savefiles.
136 1.1 cgd */
137 1.1 cgd gethdate(hname);
138 1.1 cgd
139 1.1 cgd /*
140 1.1 cgd * We cannot do chdir earlier, otherwise gethdate will fail.
141 1.1 cgd */
142 1.1 cgd #ifdef CHDIR
143 1.4 christos chdirx(dir, 1);
144 1.1 cgd #endif
145 1.1 cgd
146 1.1 cgd /*
147 1.1 cgd * Process options.
148 1.1 cgd */
149 1.4 christos while (argc > 1 && argv[1][0] == '-') {
150 1.1 cgd argv++;
151 1.1 cgd argc--;
152 1.4 christos switch (argv[0][1]) {
153 1.1 cgd #ifdef WIZARD
154 1.1 cgd case 'D':
155 1.4 christos /* if(!strcmp(getlogin(), WIZARD)) */
156 1.4 christos wizard = TRUE;
157 1.4 christos /*
158 1.4 christos * else printf("Sorry.\n");
159 1.4 christos */
160 1.1 cgd break;
161 1.1 cgd #endif
162 1.1 cgd #ifdef NEWS
163 1.1 cgd case 'n':
164 1.1 cgd flags.nonews = TRUE;
165 1.1 cgd break;
166 1.1 cgd #endif
167 1.1 cgd case 'u':
168 1.4 christos if (argv[0][2])
169 1.4 christos (void) strncpy(plname, argv[0] + 2, sizeof(plname) - 1);
170 1.4 christos else if (argc > 1) {
171 1.4 christos argc--;
172 1.4 christos argv++;
173 1.4 christos (void) strncpy(plname, argv[0], sizeof(plname) - 1);
174 1.1 cgd } else
175 1.1 cgd printf("Player name expected after -u\n");
176 1.1 cgd break;
177 1.1 cgd default:
178 1.1 cgd /* allow -T for Tourist, etc. */
179 1.4 christos (void) strncpy(pl_character, argv[0] + 1,
180 1.4 christos sizeof(pl_character) - 1);
181 1.1 cgd
182 1.1 cgd /* printf("Unknown option: %s\n", *argv); */
183 1.1 cgd }
184 1.1 cgd }
185 1.1 cgd
186 1.4 christos if (argc > 1)
187 1.1 cgd locknum = atoi(argv[1]);
188 1.1 cgd #ifdef MAX_NR_OF_PLAYERS
189 1.4 christos if (!locknum || locknum > MAX_NR_OF_PLAYERS)
190 1.1 cgd locknum = MAX_NR_OF_PLAYERS;
191 1.1 cgd #endif
192 1.1 cgd #ifdef DEF_PAGER
193 1.5 kleink if (((catmore = getenv("HACKPAGER")) == NULL &&
194 1.5 kleink (catmore = getenv("PAGER")) == NULL) ||
195 1.5 kleink catmore[0] == '\0')
196 1.1 cgd catmore = DEF_PAGER;
197 1.1 cgd #endif
198 1.1 cgd #ifdef MAIL
199 1.1 cgd getmailstatus();
200 1.1 cgd #endif
201 1.1 cgd #ifdef WIZARD
202 1.4 christos if (wizard)
203 1.4 christos (void) strcpy(plname, "wizard");
204 1.4 christos else
205 1.1 cgd #endif
206 1.4 christos if (!*plname || !strncmp(plname, "player", 4)
207 1.1 cgd || !strncmp(plname, "games", 4))
208 1.1 cgd askname();
209 1.1 cgd plnamesuffix(); /* strip suffix from name; calls askname() */
210 1.4 christos /* again if suffix was whole name */
211 1.4 christos /* accepts any suffix */
212 1.1 cgd #ifdef WIZARD
213 1.4 christos if (!wizard) {
214 1.1 cgd #endif
215 1.1 cgd /*
216 1.1 cgd * check for multiple games under the same name
217 1.1 cgd * (if !locknum) or check max nr of players (otherwise)
218 1.1 cgd */
219 1.4 christos (void) signal(SIGQUIT, SIG_IGN);
220 1.4 christos (void) signal(SIGINT, SIG_IGN);
221 1.4 christos if (!locknum)
222 1.4 christos (void) strcpy(lock, plname);
223 1.1 cgd getlock(); /* sets lock if locknum != 0 */
224 1.1 cgd #ifdef WIZARD
225 1.1 cgd } else {
226 1.4 christos char *sfoo;
227 1.4 christos (void) strcpy(lock, plname);
228 1.4 christos if ((sfoo = getenv("MAGIC")) != NULL)
229 1.4 christos while (*sfoo) {
230 1.4 christos switch (*sfoo++) {
231 1.4 christos case 'n':
232 1.4 christos (void) srandom(*sfoo++);
233 1.1 cgd break;
234 1.1 cgd }
235 1.1 cgd }
236 1.4 christos if ((sfoo = getenv("GENOCIDED")) != NULL) {
237 1.4 christos if (*sfoo == '!') {
238 1.6 jsm const struct permonst *pm = mons;
239 1.4 christos char *gp = genocided;
240 1.1 cgd
241 1.4 christos while (pm < mons + CMNUM + 2) {
242 1.4 christos if (!strchr(sfoo, pm->mlet))
243 1.1 cgd *gp++ = pm->mlet;
244 1.1 cgd pm++;
245 1.1 cgd }
246 1.1 cgd *gp = 0;
247 1.1 cgd } else
248 1.1 cgd (void) strcpy(genocided, sfoo);
249 1.1 cgd (void) strcpy(fut_geno, genocided);
250 1.1 cgd }
251 1.1 cgd }
252 1.1 cgd #endif
253 1.1 cgd setftty();
254 1.1 cgd (void) sprintf(SAVEF, "save/%d%s", getuid(), plname);
255 1.4 christos regularize(SAVEF + 5); /* avoid . or / in name */
256 1.6 jsm if ((fd = open(SAVEF, O_RDONLY)) >= 0 &&
257 1.4 christos (uptodate(fd) || unlink(SAVEF) == 666)) {
258 1.4 christos (void) signal(SIGINT, done1);
259 1.1 cgd pline("Restoring old save file...");
260 1.1 cgd (void) fflush(stdout);
261 1.4 christos if (!dorecover(fd))
262 1.1 cgd goto not_recovered;
263 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
264 1.1 cgd flags.move = 0;
265 1.1 cgd } else {
266 1.1 cgd not_recovered:
267 1.1 cgd fobj = fcobj = invent = 0;
268 1.1 cgd fmon = fallen_down = 0;
269 1.1 cgd ftrap = 0;
270 1.1 cgd fgold = 0;
271 1.1 cgd flags.ident = 1;
272 1.1 cgd init_objects();
273 1.1 cgd u_init();
274 1.1 cgd
275 1.4 christos (void) signal(SIGINT, done1);
276 1.1 cgd mklev();
277 1.1 cgd u.ux = xupstair;
278 1.1 cgd u.uy = yupstair;
279 1.1 cgd (void) inshop();
280 1.1 cgd setsee();
281 1.1 cgd flags.botlx = 1;
282 1.1 cgd makedog();
283 1.4 christos {
284 1.4 christos struct monst *mtmp;
285 1.4 christos if ((mtmp = m_at(u.ux, u.uy)) != NULL)
286 1.4 christos mnexto(mtmp); /* riv05!a3 */
287 1.1 cgd }
288 1.1 cgd seemons();
289 1.1 cgd #ifdef NEWS
290 1.4 christos if (flags.nonews || !readnews())
291 1.1 cgd /* after reading news we did docrt() already */
292 1.1 cgd #endif
293 1.1 cgd docrt();
294 1.1 cgd
295 1.1 cgd /* give welcome message before pickup messages */
296 1.1 cgd pline("Hello %s, welcome to %s!", plname, gamename);
297 1.1 cgd
298 1.1 cgd pickup(1);
299 1.4 christos read_engr_at(u.ux, u.uy);
300 1.1 cgd flags.move = 1;
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd flags.moonphase = phase_of_the_moon();
304 1.4 christos if (flags.moonphase == FULL_MOON) {
305 1.1 cgd pline("You are lucky! Full moon tonight.");
306 1.1 cgd u.uluck++;
307 1.4 christos } else if (flags.moonphase == NEW_MOON) {
308 1.1 cgd pline("Be careful! New moon tonight.");
309 1.1 cgd }
310 1.1 cgd initrack();
311 1.1 cgd
312 1.4 christos for (;;) {
313 1.4 christos if (flags.move) { /* actual time passed */
314 1.1 cgd
315 1.1 cgd settrack();
316 1.1 cgd
317 1.4 christos if (moves % 2 == 0 ||
318 1.4 christos (!(Fast & ~INTRINSIC) && (!Fast || rn2(3)))) {
319 1.1 cgd movemon();
320 1.4 christos if (!rn2(70))
321 1.4 christos (void) makemon((struct permonst *) 0, 0, 0);
322 1.1 cgd }
323 1.4 christos if (Glib)
324 1.4 christos glibr();
325 1.1 cgd timeout();
326 1.1 cgd ++moves;
327 1.4 christos if (flags.time)
328 1.4 christos flags.botl = 1;
329 1.4 christos if (u.uhp < 1) {
330 1.1 cgd pline("You die...");
331 1.1 cgd done("died");
332 1.1 cgd }
333 1.4 christos if (u.uhp * 10 < u.uhpmax && moves - wailmsg > 50) {
334 1.4 christos wailmsg = moves;
335 1.4 christos if (u.uhp == 1)
336 1.4 christos pline("You hear the wailing of the Banshee...");
337 1.4 christos else
338 1.4 christos pline("You hear the howling of the CwnAnnwn...");
339 1.1 cgd }
340 1.4 christos if (u.uhp < u.uhpmax) {
341 1.4 christos if (u.ulevel > 9) {
342 1.4 christos if (Regeneration || !(moves % 3)) {
343 1.4 christos flags.botl = 1;
344 1.4 christos u.uhp += rnd((int) u.ulevel - 9);
345 1.4 christos if (u.uhp > u.uhpmax)
346 1.4 christos u.uhp = u.uhpmax;
347 1.1 cgd }
348 1.4 christos } else if (Regeneration ||
349 1.4 christos (!(moves % (22 - u.ulevel * 2)))) {
350 1.1 cgd flags.botl = 1;
351 1.1 cgd u.uhp++;
352 1.1 cgd }
353 1.1 cgd }
354 1.4 christos if (Teleportation && !rn2(85))
355 1.4 christos tele();
356 1.4 christos if (Searching && multi >= 0)
357 1.4 christos (void) dosearch();
358 1.1 cgd gethungry();
359 1.1 cgd invault();
360 1.1 cgd amulet();
361 1.1 cgd }
362 1.4 christos if (multi < 0) {
363 1.4 christos if (!++multi) {
364 1.1 cgd pline(nomovemsg ? nomovemsg :
365 1.4 christos "You can move again.");
366 1.1 cgd nomovemsg = 0;
367 1.4 christos if (afternmv)
368 1.4 christos (*afternmv) ();
369 1.1 cgd afternmv = 0;
370 1.1 cgd }
371 1.1 cgd }
372 1.1 cgd find_ac();
373 1.1 cgd #ifndef QUEST
374 1.4 christos if (!flags.mv || Blind)
375 1.1 cgd #endif
376 1.1 cgd {
377 1.1 cgd seeobjs();
378 1.1 cgd seemons();
379 1.1 cgd nscr();
380 1.1 cgd }
381 1.4 christos if (flags.botl || flags.botlx)
382 1.4 christos bot();
383 1.1 cgd
384 1.1 cgd flags.move = 1;
385 1.1 cgd
386 1.4 christos if (multi >= 0 && occupation) {
387 1.4 christos if (monster_nearby())
388 1.1 cgd stop_occupation();
389 1.4 christos else if ((*occupation) () == 0)
390 1.1 cgd occupation = 0;
391 1.1 cgd continue;
392 1.1 cgd }
393 1.4 christos if (multi > 0) {
394 1.1 cgd #ifdef QUEST
395 1.4 christos if (flags.run >= 4)
396 1.4 christos finddir();
397 1.1 cgd #endif
398 1.1 cgd lookaround();
399 1.4 christos if (!multi) { /* lookaround may clear multi */
400 1.1 cgd flags.move = 0;
401 1.1 cgd continue;
402 1.1 cgd }
403 1.4 christos if (flags.mv) {
404 1.4 christos if (multi < COLNO && !--multi)
405 1.1 cgd flags.mv = flags.run = 0;
406 1.1 cgd domove();
407 1.1 cgd } else {
408 1.1 cgd --multi;
409 1.1 cgd rhack(save_cm);
410 1.1 cgd }
411 1.4 christos } else if (multi == 0) {
412 1.1 cgd #ifdef MAIL
413 1.1 cgd ckmailstatus();
414 1.1 cgd #endif
415 1.1 cgd rhack((char *) 0);
416 1.1 cgd }
417 1.4 christos if (multi && multi % 7 == 0)
418 1.1 cgd (void) fflush(stdout);
419 1.1 cgd }
420 1.1 cgd }
421 1.1 cgd
422 1.4 christos void
423 1.1 cgd glo(foo)
424 1.4 christos int foo;
425 1.1 cgd {
426 1.1 cgd /* construct the string xlock.n */
427 1.4 christos char *tf;
428 1.1 cgd
429 1.1 cgd tf = lock;
430 1.4 christos while (*tf && *tf != '.')
431 1.4 christos tf++;
432 1.1 cgd (void) sprintf(tf, ".%d", foo);
433 1.1 cgd }
434 1.1 cgd
435 1.1 cgd /*
436 1.1 cgd * plname is filled either by an option (-u Player or -uPlayer) or
437 1.1 cgd * explicitly (-w implies wizard) or by askname.
438 1.1 cgd * It may still contain a suffix denoting pl_character.
439 1.1 cgd */
440 1.4 christos void
441 1.4 christos askname()
442 1.4 christos {
443 1.4 christos int c, ct;
444 1.1 cgd printf("\nWho are you? ");
445 1.1 cgd (void) fflush(stdout);
446 1.1 cgd ct = 0;
447 1.4 christos while ((c = getchar()) != '\n') {
448 1.4 christos if (c == EOF)
449 1.4 christos error("End of input\n");
450 1.1 cgd /* some people get confused when their erase char is not ^H */
451 1.4 christos if (c == '\010') {
452 1.4 christos if (ct)
453 1.4 christos ct--;
454 1.1 cgd continue;
455 1.1 cgd }
456 1.4 christos if (c != '-')
457 1.4 christos if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
458 1.4 christos c = '_';
459 1.4 christos if (ct < sizeof(plname) - 1)
460 1.4 christos plname[ct++] = c;
461 1.1 cgd }
462 1.1 cgd plname[ct] = 0;
463 1.4 christos if (ct == 0)
464 1.4 christos askname();
465 1.1 cgd }
466 1.1 cgd
467 1.4 christos /* VARARGS1 */
468 1.4 christos void
469 1.4 christos #ifdef __STDC__
470 1.4 christos impossible(const char *s, ...)
471 1.4 christos #else
472 1.4 christos impossible(va_alist)
473 1.4 christos va_dcl
474 1.4 christos #endif
475 1.1 cgd {
476 1.4 christos va_list ap;
477 1.4 christos #ifndef __STDC__
478 1.4 christos const char *s;
479 1.4 christos
480 1.4 christos va_start(ap);
481 1.4 christos s = va_arg(ap, const char *);
482 1.4 christos #else
483 1.4 christos va_start(ap, s);
484 1.4 christos #endif
485 1.4 christos vpline(s, ap);
486 1.4 christos va_end(ap);
487 1.1 cgd pline("Program in disorder - perhaps you'd better Quit.");
488 1.1 cgd }
489 1.1 cgd
490 1.1 cgd #ifdef CHDIR
491 1.1 cgd static void
492 1.1 cgd chdirx(dir, wr)
493 1.6 jsm const char *dir;
494 1.4 christos boolean wr;
495 1.1 cgd {
496 1.1 cgd
497 1.1 cgd #ifdef SECURE
498 1.4 christos if (dir /* User specified directory? */
499 1.1 cgd #ifdef HACKDIR
500 1.4 christos && strcmp(dir, HACKDIR) /* and not the default? */
501 1.1 cgd #endif
502 1.1 cgd ) {
503 1.4 christos (void) setuid(getuid()); /* Ron Wessels */
504 1.1 cgd (void) setgid(getgid());
505 1.1 cgd }
506 1.1 cgd #endif
507 1.1 cgd
508 1.1 cgd #ifdef HACKDIR
509 1.4 christos if (dir == NULL)
510 1.1 cgd dir = HACKDIR;
511 1.1 cgd #endif
512 1.1 cgd
513 1.4 christos if (dir && chdir(dir) < 0) {
514 1.1 cgd perror(dir);
515 1.1 cgd error("Cannot chdir to %s.", dir);
516 1.1 cgd }
517 1.1 cgd /* warn the player if he cannot write the record file */
518 1.1 cgd /* perhaps we should also test whether . is writable */
519 1.1 cgd /* unfortunately the access systemcall is worthless */
520 1.4 christos if (wr) {
521 1.4 christos int fd;
522 1.1 cgd
523 1.4 christos if (dir == NULL)
524 1.4 christos dir = ".";
525 1.6 jsm if ((fd = open(RECORD, O_RDWR)) < 0) {
526 1.4 christos printf("Warning: cannot write %s/%s", dir, RECORD);
527 1.4 christos getret();
528 1.4 christos } else
529 1.4 christos (void) close(fd);
530 1.1 cgd }
531 1.1 cgd }
532 1.1 cgd #endif
533 1.1 cgd
534 1.4 christos void
535 1.1 cgd stop_occupation()
536 1.1 cgd {
537 1.4 christos if (occupation) {
538 1.1 cgd pline("You stop %s.", occtxt);
539 1.1 cgd occupation = 0;
540 1.1 cgd }
541 1.1 cgd }
542