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