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