Home | History | Annotate | Line # | Download | only in phantasia
main.c revision 1.12.4.2
      1 /*	$NetBSD: main.c,v 1.12.4.2 2005/04/08 21:46:41 tron Exp $	*/
      2 
      3 /*
      4  * Phantasia 3.3.2 -- Interterminal fantasy game
      5  *
      6  * Edward A. Estes
      7  * AT&T, March 12, 1986
      8  */
      9 
     10 /* DISCLAIMER:
     11  *
     12  * This game is distributed for free as is.  It is not guaranteed to work
     13  * in every conceivable environment.  It is not even guaranteed to work
     14  * in ANY environment.
     15  *
     16  * This game is distributed without notice of copyright, therefore it
     17  * may be used in any manner the recipient sees fit.  However, the
     18  * author assumes no responsibility for maintaining or revising this
     19  * game, in its original form, or any derivitives thereof.
     20  *
     21  * The author shall not be responsible for any loss, cost, or damage,
     22  * including consequential damage, caused by reliance on this material.
     23  *
     24  * The author makes no warranties, express or implied, including warranties
     25  * of merchantability or fitness for a particular purpose or use.
     26  *
     27  * AT&T is in no way connected with this game.
     28  */
     29 
     30 #include <sys/types.h>
     31 #include <pwd.h>
     32 
     33 /*
     34  * The program allocates as much file space as it needs to store characters,
     35  * so the possibility exists for the character file to grow without bound.
     36  * The file is purged upon normal entry to try to avoid that problem.
     37  * A similar problem exists for energy voids.  To alleviate the problem here,
     38  * the void file is cleared with every new king, and a limit is placed
     39  * on the size of the energy void file.
     40  */
     41 
     42 /*
     43  * Put one line of text into the file 'motd' for announcements, etc.
     44  */
     45 
     46 /*
     47  * The scoreboard file is updated when someone dies, and keeps track
     48  * of the highest character to date for that login.
     49  * Being purged from the character file does not cause the scoreboard
     50  * to be updated.
     51  */
     52 
     53 
     54 /*
     55  * main.c	Main routines for Phantasia
     56  */
     57 
     58 #include "include.h"
     59 #undef bool
     60 #include <curses.h>
     61 
     62 int	main(int, char **);
     63 
     64 int
     65 main(argc, argv)
     66 	int     argc;
     67 	char  **argv;
     68 {
     69 	bool    noheader = FALSE;	/* set if don't want header */
     70 	bool    headeronly = FALSE;	/* set if only want header */
     71 	bool    examine = FALSE;	/* set if examine a character */
     72 	time_t  seconds;		/* for time of day */
     73 	double  dtemp;			/* for temporary calculations */
     74 
     75 	initialstate();			/* init globals */
     76 
     77 	/* process arguments */
     78 	while (--argc && (*++argv)[0] == '-')
     79 		switch ((*argv)[1]) {
     80 		case 's':	/* short */
     81 			noheader = TRUE;
     82 			break;
     83 
     84 		case 'H':	/* Header */
     85 			headeronly = TRUE;
     86 			break;
     87 
     88 		case 'a':	/* all users */
     89 			activelist();
     90 			cleanup(TRUE);
     91 			/* NOTREACHED */
     92 
     93 		case 'p':	/* purge old players */
     94 			purgeoldplayers();
     95 			cleanup(TRUE);
     96 			/* NOTREACHED */
     97 
     98 		case 'S':	/* set 'Wizard' */
     99 			Wizard = !getuid();
    100 			break;
    101 
    102 		case 'x':	/* examine */
    103 			examine = TRUE;
    104 			break;
    105 
    106 		case 'm':	/* monsters */
    107 			monstlist();
    108 			cleanup(TRUE);
    109 			/* NOTREACHED */
    110 
    111 		case 'b':	/* scoreboard */
    112 			scorelist();
    113 			cleanup(TRUE);
    114 			/* NOTREACHED */
    115 		}
    116 
    117 	if (!isatty(0))		/* don't let non-tty's play */
    118 		cleanup(TRUE);
    119 	/* NOTREACHED */
    120 
    121 	playinit();		/* set up to catch signals, init curses */
    122 
    123 	if (examine) {
    124 		changestats(FALSE);
    125 		cleanup(TRUE);
    126 		/* NOTREACHED */
    127 	}
    128 	if (!noheader) {
    129 		titlelist();
    130 		purgeoldplayers();	/* clean up old characters */
    131 	}
    132 	if (headeronly)
    133 		cleanup(TRUE);
    134 	/* NOTREACHED */
    135 
    136 	do
    137 		/* get the player structure filled */
    138 	{
    139 		Fileloc = -1L;
    140 
    141 		mvaddstr(22, 17, "Do you have a character to run [Q = Quit] ? ");
    142 
    143 		switch (getanswer("NYQ", FALSE)) {
    144 		case 'Y':
    145 			Fileloc = recallplayer();
    146 			break;
    147 
    148 		case 'Q':
    149 			cleanup(TRUE);
    150 			/* NOTREACHED */
    151 
    152 		default:
    153 			Fileloc = rollnewplayer();
    154 			break;
    155 		}
    156 		clear();
    157 	}
    158 	while (Fileloc < 0L);
    159 
    160 	if (Player.p_level > 5.0)
    161 		/* low level players have long timeout */
    162 		Timeout = TRUE;
    163 
    164 	/* update some important player statistics */
    165 	strcpy(Player.p_login, Login);
    166 	time(&seconds);
    167 	Player.p_lastused = localtime(&seconds)->tm_yday;
    168 	Player.p_status = S_PLAYING;
    169 	writerecord(&Player, Fileloc);
    170 
    171 	Statptr = &Stattable[Player.p_type];	/* initialize pointer */
    172 
    173 	/* catch interrupts */
    174 #ifdef	BSD41
    175 	sigset(SIGINT, interrupt);
    176 #endif
    177 #ifdef	BSD42
    178 	signal(SIGINT, interrupt);
    179 #endif
    180 #ifdef	SYS3
    181 	signal(SIGINT, interrupt);
    182 #endif
    183 #ifdef	SYS5
    184 	signal(SIGINT, interrupt);
    185 #endif
    186 
    187 	altercoordinates(Player.p_x, Player.p_y, A_FORCED);	/* set some flags */
    188 
    189 	clear();
    190 
    191 	for (;;)
    192 		/* loop forever, processing input */
    193 	{
    194 
    195 		adjuststats();	/* cleanup stats */
    196 
    197 		if (Throne && Player.p_crowns == 0 && Player.p_specialtype != SC_KING)
    198 			/* not allowed on throne -- move */
    199 		{
    200 			mvaddstr(5, 0, "You're not allowed in the Lord's Chamber without a crown.\n");
    201 			altercoordinates(0.0, 0.0, A_NEAR);
    202 		}
    203 		checktampered();/* check for energy voids, etc. */
    204 
    205 		if (Player.p_status != S_CLOAKED
    206 		/* not cloaked */
    207 		    && (dtemp = fabs(Player.p_x)) == fabs(Player.p_y)
    208 		/* |x| = |y| */
    209 		    && !Throne)
    210 			/* not on throne */
    211 		{
    212 			dtemp = sqrt(dtemp / 100.0);
    213 			if (floor(dtemp) == dtemp)
    214 				/* |x| / 100 == n*n; at a trading post */
    215 			{
    216 				tradingpost();
    217 				clear();
    218 			}
    219 		}
    220 		checkbattle();	/* check for player to player battle */
    221 		neatstuff();	/* gurus, medics, etc. */
    222 
    223 		if (Player.p_status == S_CLOAKED) {
    224 			/* costs 3 mana per turn to be cloaked */
    225 			if (Player.p_mana > 3.0)
    226 				Player.p_mana -= 3.0;
    227 			else
    228 				/* ran out of mana, uncloak */
    229 			{
    230 				Player.p_status = S_PLAYING;
    231 				Changed = TRUE;
    232 			}
    233 		}
    234 
    235 		if (Player.p_status != S_PLAYING && Player.p_status != S_CLOAKED)
    236 			/* change status back to S_PLAYING */
    237 		{
    238 			Player.p_status = S_PLAYING;
    239 			Changed = TRUE;
    240 		}
    241 		if (Changed)
    242 			/* update file only if important stuff has changed */
    243 		{
    244 			writerecord(&Player, Fileloc);
    245 			Changed = FALSE;
    246 			continue;
    247 		}
    248 		readmessage();	/* read message, if any */
    249 
    250 		displaystats();	/* print statistics */
    251 
    252 		move(6, 0);
    253 
    254 		if (Throne)
    255 			/* maybe make king, print prompt, etc. */
    256 			throneroom();
    257 
    258 		/* print status line */
    259 		addstr("1:Move  2:Players  3:Talk  4:Stats  5:Quit  ");
    260 		if (Player.p_level >= MEL_CLOAK && Player.p_magiclvl >= ML_CLOAK)
    261 			addstr("6:Cloak  ");
    262 		if (Player.p_level >= MEL_TELEPORT && Player.p_magiclvl >= ML_TELEPORT)
    263 			addstr("7:Teleport  ");
    264 		if (Player.p_specialtype >= SC_COUNCIL || Wizard)
    265 			addstr("8:Intervene  ");
    266 
    267 		procmain();	/* process input */
    268 	}
    269 }
    270 
    271 void
    272 initialstate()
    273 {
    274 	Beyond = FALSE;
    275 	Marsh = FALSE;
    276 	Throne = FALSE;
    277 	Changed = FALSE;
    278 	Wizard = FALSE;
    279 	Timeout = FALSE;
    280 	Users = 0;
    281 	Windows = FALSE;
    282 	Echo = TRUE;
    283 
    284 	/* setup login name */
    285 	if ((Login = getlogin()) == NULL)
    286 		Login = getpwuid(getuid())->pw_name;
    287 
    288 	/* open some files */
    289 	if ((Playersfp = fopen(_PATH_PEOPLE, "r+")) == NULL)
    290 		error(_PATH_PEOPLE);
    291 	/* NOTREACHED */
    292 	if (fileno(Playersfp) < 3)
    293 		exit(1);
    294 
    295 	if ((Monstfp = fopen(_PATH_MONST, "r+")) == NULL)
    296 		error(_PATH_MONST);
    297 	/* NOTREACHED */
    298 
    299 	if ((Messagefp = fopen(_PATH_MESS, "r")) == NULL)
    300 		error(_PATH_MESS);
    301 	/* NOTREACHED */
    302 
    303 	if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL)
    304 		error(_PATH_VOID);
    305 	/* NOTREACHED */
    306 
    307 	srandom((unsigned) time(NULL));	/* prime random numbers */
    308 }
    309 
    310 long
    311 rollnewplayer()
    312 {
    313 	int     chartype;	/* character type */
    314 	int     ch;		/* input */
    315 
    316 	initplayer(&Player);	/* initialize player structure */
    317 
    318 	clear();
    319 	mvaddstr(4, 21, "Which type of character do you want:");
    320 	mvaddstr(8, 4,
    321 "1:Magic User  2:Fighter  3:Elf  4:Dwarf  5:Halfling  6:Experimento  ");
    322 	if (Wizard) {
    323 		addstr("7:Super  ? ");
    324 		chartype = getanswer("1234567", FALSE);
    325 	} else {
    326 		addstr("?  ");
    327 		chartype = getanswer("123456", FALSE);
    328 	}
    329 
    330 	do {
    331 		genchar(chartype);	/* roll up a character */
    332 
    333 		/* print out results */
    334 		mvprintw(12, 14,
    335 		    "Strength    :  %2.0f  Quickness:  %2.0f  Mana       :  %2.0f\n",
    336 		    Player.p_strength, Player.p_quickness, Player.p_mana);
    337 		mvprintw(13, 14,
    338 		    "Energy Level:  %2.0f  Brains   :  %2.0f  Magic Level:  %2.0f\n",
    339 		    Player.p_energy, Player.p_brains, Player.p_magiclvl);
    340 
    341 		if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
    342 			break;
    343 
    344 		mvaddstr(14, 14, "Type '1' to keep >");
    345 		ch = getanswer(" ", TRUE);
    346 	}
    347 	while (ch != '1');
    348 
    349 	if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
    350 		/* get coordinates for experimento */
    351 		for (;;) {
    352 			mvaddstr(16, 0, "Enter the X Y coordinates of your experimento ? ");
    353 			getstring(Databuf, SZ_DATABUF);
    354 			sscanf(Databuf, "%lf %lf", &Player.p_x, &Player.p_y);
    355 
    356 			if (fabs(Player.p_x) > D_EXPER || fabs(Player.p_y) > D_EXPER)
    357 				mvaddstr(17, 0, "Invalid coordinates.  Try again.\n");
    358 			else
    359 				break;
    360 		}
    361 
    362 	for (;;)
    363 		/* name the new character */
    364 	{
    365 		mvprintw(18, 0,
    366 		    "Give your character a name [up to %d characters] ?  ", SZ_NAME - 1);
    367 		getstring(Player.p_name, SZ_NAME);
    368 		truncstring(Player.p_name);	/* remove trailing blanks */
    369 
    370 		if (Player.p_name[0] == '\0')
    371 			/* no null names */
    372 			mvaddstr(19, 0, "Invalid name.");
    373 		else
    374 			if (findname(Player.p_name, &Other) >= 0L)
    375 				/* cannot have duplicate names */
    376 				mvaddstr(19, 0, "Name already in use.");
    377 			else
    378 				/* name is acceptable */
    379 				break;
    380 
    381 		addstr("  Pick another.\n");
    382 	}
    383 
    384 	/* get a password for character */
    385 	Echo = FALSE;
    386 
    387 	do {
    388 		mvaddstr(20, 0, "Give your character a password [up to 8 characters] ? ");
    389 		getstring(Player.p_password, SZ_PASSWORD);
    390 		mvaddstr(21, 0, "Enter again to verify: ");
    391 		getstring(Databuf, SZ_PASSWORD);
    392 	}
    393 	while (strcmp(Player.p_password, Databuf) != 0);
    394 
    395 	Echo = TRUE;
    396 
    397 	return (allocrecord());
    398 }
    399 
    400 void
    401 procmain()
    402 {
    403 	int     ch;		/* input */
    404 	double  x;		/* desired new x coordinate */
    405 	double  y;		/* desired new y coordinate */
    406 	double  temp;		/* for temporary calculations */
    407 	FILE   *fp;		/* for opening files */
    408 	int     loop;		/* a loop counter */
    409 	bool    hasmoved = FALSE;	/* set if player has moved */
    410 
    411 	ch = inputoption();
    412 	mvaddstr(4, 0, "\n\n");	/* clear status area */
    413 
    414 	move(7, 0);
    415 	clrtobot();		/* clear data on bottom area of screen */
    416 
    417 	if (Player.p_specialtype == SC_VALAR && (ch == '1' || ch == '7'))
    418 		/* valar cannot move */
    419 		ch = ' ';
    420 
    421 	switch (ch) {
    422 	case 'K':		/* move up/north */
    423 	case 'N':
    424 		x = Player.p_x;
    425 		y = Player.p_y + MAXMOVE();
    426 		hasmoved = TRUE;
    427 		break;
    428 
    429 	case 'J':		/* move down/south */
    430 	case 'S':
    431 		x = Player.p_x;
    432 		y = Player.p_y - MAXMOVE();
    433 		hasmoved = TRUE;
    434 		break;
    435 
    436 	case 'L':		/* move right/east */
    437 	case 'E':
    438 		x = Player.p_x + MAXMOVE();
    439 		y = Player.p_y;
    440 		hasmoved = TRUE;
    441 		break;
    442 
    443 	case 'H':		/* move left/west */
    444 	case 'W':
    445 		x = Player.p_x - MAXMOVE();
    446 		y = Player.p_y;
    447 		hasmoved = TRUE;
    448 		break;
    449 
    450 	default:		/* rest */
    451 		Player.p_energy += (Player.p_maxenergy + Player.p_shield) / 15.0
    452 		    + Player.p_level / 3.0 + 2.0;
    453 		Player.p_energy =
    454 		    MIN(Player.p_energy, Player.p_maxenergy + Player.p_shield);
    455 
    456 		if (Player.p_status != S_CLOAKED)
    457 			/* cannot find mana if cloaked */
    458 		{
    459 			Player.p_mana += (Circle + Player.p_level) / 4.0;
    460 
    461 			if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
    462 				/* wandering monster */
    463 				encounter(-1);
    464 		}
    465 		break;
    466 
    467 	case 'X':		/* change/examine a character */
    468 		changestats(TRUE);
    469 		break;
    470 
    471 	case '1':		/* move */
    472 		for (loop = 3; loop; --loop) {
    473 			mvaddstr(4, 0, "X Y Coordinates ? ");
    474 			getstring(Databuf, SZ_DATABUF);
    475 
    476 			if (sscanf(Databuf, "%lf %lf", &x, &y) != 2)
    477 				mvaddstr(5, 0, "Try again\n");
    478 			else
    479 				if (distance(Player.p_x, x, Player.p_y, y) > MAXMOVE())
    480 					ILLMOVE();
    481 				else {
    482 					hasmoved = TRUE;
    483 					break;
    484 				}
    485 		}
    486 		break;
    487 
    488 	case '2':		/* players */
    489 		userlist(TRUE);
    490 		break;
    491 
    492 	case '3':		/* message */
    493 		mvaddstr(4, 0, "Message ? ");
    494 		getstring(Databuf, SZ_DATABUF);
    495 		/* we open the file for writing to erase any data which is
    496 		 * already there */
    497 		fp = fopen(_PATH_MESS, "w");
    498 		if (Databuf[0] != '\0')
    499 			fprintf(fp, "%s: %s", Player.p_name, Databuf);
    500 		fclose(fp);
    501 		break;
    502 
    503 	case '4':		/* stats */
    504 		allstatslist();
    505 		break;
    506 
    507 	case '5':		/* good-bye */
    508 		leavegame();
    509 		/* NOTREACHED */
    510 
    511 	case '6':		/* cloak */
    512 		if (Player.p_level < MEL_CLOAK || Player.p_magiclvl < ML_CLOAK)
    513 			ILLCMD();
    514 		else
    515 			if (Player.p_status == S_CLOAKED)
    516 				Player.p_status = S_PLAYING;
    517 			else
    518 				if (Player.p_mana < MM_CLOAK)
    519 					mvaddstr(5, 0, "No mana left.\n");
    520 				else {
    521 					Changed = TRUE;
    522 					Player.p_mana -= MM_CLOAK;
    523 					Player.p_status = S_CLOAKED;
    524 				}
    525 		break;
    526 
    527 	case '7':		/* teleport */
    528 		/*
    529 	         * conditions for teleport
    530 	         *	- 20 per (level plus magic level)
    531 	         *	- OR council of the wise or valar or ex-valar
    532 	         *	- OR transport from throne
    533 	         * transports from throne cost no mana
    534 	         */
    535 		if (Player.p_level < MEL_TELEPORT || Player.p_magiclvl < ML_TELEPORT)
    536 			ILLCMD();
    537 		else
    538 			for (loop = 3; loop; --loop) {
    539 				mvaddstr(4, 0, "X Y Coordinates ? ");
    540 				getstring(Databuf, SZ_DATABUF);
    541 
    542 				if (sscanf(Databuf, "%lf %lf", &x, &y) == 2) {
    543 					temp = distance(Player.p_x, x, Player.p_y, y);
    544 					if (!Throne
    545 					/* can transport anywhere from throne */
    546 					    && Player.p_specialtype <= SC_COUNCIL
    547 					/* council, valar can transport
    548 					 * anywhere */
    549 					    && temp > (Player.p_level + Player.p_magiclvl) * 20.0)
    550 						/* can only move 20 per exp.
    551 						 * level + mag. level */
    552 						ILLMOVE();
    553 					else {
    554 						temp = (temp / 75.0 + 1.0) * 20.0;	/* mana used */
    555 
    556 						if (!Throne && temp > Player.p_mana)
    557 							mvaddstr(5, 0, "Not enough power for that distance.\n");
    558 						else {
    559 							if (!Throne)
    560 								Player.p_mana -= temp;
    561 							hasmoved = TRUE;
    562 							break;
    563 						}
    564 					}
    565 				}
    566 			}
    567 		break;
    568 
    569 	case 'C':
    570 	case '9':		/* monster */
    571 		if (Throne)
    572 			/* no monsters while on throne */
    573 			mvaddstr(5, 0, "No monsters in the chamber!\n");
    574 		else
    575 			if (Player.p_specialtype != SC_VALAR)
    576 				/* the valar cannot call monsters */
    577 			{
    578 				Player.p_sin += 1e-6;
    579 				encounter(-1);
    580 			}
    581 		break;
    582 
    583 	case '0':		/* decree */
    584 		if (Wizard || (Player.p_specialtype == SC_KING && Throne))
    585 			/* kings must be on throne to decree */
    586 			dotampered();
    587 		else
    588 			ILLCMD();
    589 		break;
    590 
    591 	case '8':		/* intervention */
    592 		if (Wizard || Player.p_specialtype >= SC_COUNCIL)
    593 			dotampered();
    594 		else
    595 			ILLCMD();
    596 		break;
    597 	}
    598 
    599 	if (hasmoved)
    600 		/* player has moved -- alter coordinates, and do random
    601 		 * monster */
    602 	{
    603 		altercoordinates(x, y, A_SPECIFIC);
    604 
    605 		if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
    606 			encounter(-1);
    607 	}
    608 }
    609 
    610 void
    611 titlelist()
    612 {
    613 	FILE   *fp;		/* used for opening various files */
    614 	bool    councilfound = FALSE;	/* set if we find a member of the
    615 					 * council */
    616 	bool    kingfound = FALSE;	/* set if we find a king */
    617 	double  hiexp, nxtexp;	/* used for finding the two highest players */
    618 	double  hilvl, nxtlvl;	/* used for finding the two highest players */
    619 	char    hiname[21], nxtname[21];	/* used for finding the two
    620 						 * highest players */
    621 
    622 	nxtexp = 0;
    623 	mvaddstr(0, 14,
    624 	    "W e l c o m e   t o   P h a n t a s i a (vers. 3.3.2)!");
    625 
    626 	/* print message of the day */
    627 	if ((fp = fopen(_PATH_MOTD, "r")) != NULL
    628 	    && fgets(Databuf, SZ_DATABUF, fp) != NULL) {
    629 		mvaddstr(2, 40 - strlen(Databuf) / 2, Databuf);
    630 		fclose(fp);
    631 	}
    632 	/* search for king */
    633 	fseek(Playersfp, 0L, SEEK_SET);
    634 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    635 		if (Other.p_specialtype == SC_KING &&
    636 		    Other.p_status != S_NOTUSED)
    637 			/* found the king */
    638 		{
    639 			sprintf(Databuf, "The present ruler is %s  Level:%.0f",
    640 			    Other.p_name, Other.p_level);
    641 			mvaddstr(4, 40 - strlen(Databuf) / 2, Databuf);
    642 			kingfound = TRUE;
    643 			break;
    644 		}
    645 	if (!kingfound)
    646 		mvaddstr(4, 24, "There is no ruler at this time.");
    647 
    648 	/* search for valar */
    649 	fseek(Playersfp, 0L, SEEK_SET);
    650 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    651 		if (Other.p_specialtype == SC_VALAR && Other.p_status != S_NOTUSED)
    652 			/* found the valar */
    653 		{
    654 			sprintf(Databuf, "The Valar is %s   Login:  %s", Other.p_name, Other.p_login);
    655 			mvaddstr(6, 40 - strlen(Databuf) / 2, Databuf);
    656 			break;
    657 		}
    658 	/* search for council of the wise */
    659 	fseek(Playersfp, 0L, SEEK_SET);
    660 	Lines = 10;
    661 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    662 		if (Other.p_specialtype == SC_COUNCIL && Other.p_status != S_NOTUSED)
    663 			/* found a member of the council */
    664 		{
    665 			if (!councilfound) {
    666 				mvaddstr(8, 30, "Council of the Wise:");
    667 				councilfound = TRUE;
    668 			}
    669 			/* This assumes a finite (<=5) number of C.O.W.: */
    670 			sprintf(Databuf, "%s   Login:  %s", Other.p_name, Other.p_login);
    671 			mvaddstr(Lines++, 40 - strlen(Databuf) / 2, Databuf);
    672 		}
    673 	/* search for the two highest players */
    674 	nxtname[0] = hiname[0] = '\0';
    675 	hiexp = 0.0;
    676 	nxtlvl = hilvl = 0;
    677 
    678 	fseek(Playersfp, 0L, SEEK_SET);
    679 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    680 		if (Other.p_experience > hiexp && Other.p_specialtype <= SC_KING && Other.p_status != S_NOTUSED)
    681 			/* highest found so far */
    682 		{
    683 			nxtexp = hiexp;
    684 			hiexp = Other.p_experience;
    685 			nxtlvl = hilvl;
    686 			hilvl = Other.p_level;
    687 			strcpy(nxtname, hiname);
    688 			strcpy(hiname, Other.p_name);
    689 		} else
    690 			if (Other.p_experience > nxtexp
    691 			    && Other.p_specialtype <= SC_KING
    692 			    && Other.p_status != S_NOTUSED)
    693 				/* next highest found so far */
    694 			{
    695 				nxtexp = Other.p_experience;
    696 				nxtlvl = Other.p_level;
    697 				strcpy(nxtname, Other.p_name);
    698 			}
    699 	mvaddstr(15, 28, "Highest characters are:");
    700 	sprintf(Databuf, "%s  Level:%.0f   and   %s  Level:%.0f",
    701 	    hiname, hilvl, nxtname, nxtlvl);
    702 	mvaddstr(17, 40 - strlen(Databuf) / 2, Databuf);
    703 
    704 	/* print last to die */
    705 	if ((fp = fopen(_PATH_LASTDEAD, "r")) != NULL
    706 	    && fgets(Databuf, SZ_DATABUF, fp) != NULL) {
    707 		mvaddstr(19, 25, "The last character to die was:");
    708 		mvaddstr(20, 40 - strlen(Databuf) / 2, Databuf);
    709 		fclose(fp);
    710 	}
    711 	refresh();
    712 }
    713 
    714 long
    715 recallplayer()
    716 {
    717 	long    loc = 0L;	/* location in player file */
    718 	int     loop;		/* loop counter */
    719 	int     ch;		/* input */
    720 
    721 	clear();
    722 	mvprintw(10, 0, "What was your character's name ? ");
    723 	getstring(Databuf, SZ_NAME);
    724 	truncstring(Databuf);
    725 
    726 	if ((loc = findname(Databuf, &Player)) >= 0L)
    727 		/* found character */
    728 	{
    729 		Echo = FALSE;
    730 
    731 		for (loop = 0; loop < 2; ++loop) {
    732 			/* prompt for password */
    733 			mvaddstr(11, 0, "Password ? ");
    734 			getstring(Databuf, SZ_PASSWORD);
    735 			if (strcmp(Databuf, Player.p_password) == 0)
    736 				/* password good */
    737 			{
    738 				Echo = TRUE;
    739 
    740 				if (Player.p_status != S_OFF)
    741 					/* player did not exit normally last
    742 					 * time */
    743 				{
    744 					clear();
    745 					addstr("Your character did not exit normally last time.\n");
    746 					addstr("If you think you have good cause to have your character saved,\n");
    747 					printw("you may quit and mail your reason to 'root'.\n");
    748 					addstr("Otherwise, continuing spells certain death.\n");
    749 					addstr("Do you want to quit ? ");
    750 					ch = getanswer("YN", FALSE);
    751 					if (ch == 'Y') {
    752 						Player.p_status = S_HUNGUP;
    753 						writerecord(&Player, loc);
    754 						cleanup(TRUE);
    755 						/* NOTREACHED */
    756 					}
    757 					death("Stupidity");
    758 					/* NOTREACHED */
    759 				}
    760 				return (loc);
    761 			} else
    762 				mvaddstr(12, 0, "No good.\n");
    763 		}
    764 
    765 		Echo = TRUE;
    766 	} else
    767 		mvaddstr(11, 0, "Not found.\n");
    768 
    769 	more(13);
    770 	return (-1L);
    771 }
    772 
    773 void
    774 neatstuff()
    775 {
    776 	double  temp;		/* for temporary calculations */
    777 	int     ch;		/* input */
    778 
    779 	switch ((int) ROLL(0.0, 100.0)) {
    780 	case 1:
    781 	case 2:
    782 		if (Player.p_poison > 0.0) {
    783 			mvaddstr(4, 0, "You've found a medic!  How much will you offer to be cured ? ");
    784 			temp = floor(infloat());
    785 			if (temp < 0.0 || temp > Player.p_gold)
    786 				/* negative gold, or more than available */
    787 			{
    788 				mvaddstr(6, 0, "He was not amused, and made you worse.\n");
    789 				Player.p_poison += 1.0;
    790 			} else
    791 				if (drandom() / 2.0 > (temp + 1.0) / MAX(Player.p_gold, 1))
    792 					/* medic wants 1/2 of available gold */
    793 					mvaddstr(5, 0, "Sorry, he wasn't interested.\n");
    794 				else {
    795 					mvaddstr(5, 0, "He accepted.");
    796 					Player.p_poison = MAX(0.0, Player.p_poison - 1.0);
    797 					Player.p_gold -= temp;
    798 				}
    799 		}
    800 		break;
    801 
    802 	case 3:
    803 		mvaddstr(4, 0, "You've been caught raping and pillaging!\n");
    804 		Player.p_experience += 4000.0;
    805 		Player.p_sin += 0.5;
    806 		break;
    807 
    808 	case 4:
    809 		temp = ROLL(10.0, 75.0);
    810 		mvprintw(4, 0, "You've found %.0f gold pieces, want them ? ", temp);
    811 		ch = getanswer("NY", FALSE);
    812 
    813 		if (ch == 'Y')
    814 			collecttaxes(temp, 0.0);
    815 		break;
    816 
    817 	case 5:
    818 		if (Player.p_sin > 1.0) {
    819 			mvaddstr(4, 0, "You've found a Holy Orb!\n");
    820 			Player.p_sin -= 0.25;
    821 		}
    822 		break;
    823 
    824 	case 6:
    825 		if (Player.p_poison < 1.0) {
    826 			mvaddstr(4, 0, "You've been hit with a plague!\n");
    827 			Player.p_poison += 1.0;
    828 		}
    829 		break;
    830 
    831 	case 7:
    832 		mvaddstr(4, 0, "You've found some holy water.\n");
    833 		++Player.p_holywater;
    834 		break;
    835 
    836 	case 8:
    837 		mvaddstr(4, 0, "You've met a Guru. . .");
    838 		if (drandom() * Player.p_sin > 1.0)
    839 			addstr("You disgusted him with your sins!\n");
    840 		else
    841 			if (Player.p_poison > 0.0) {
    842 				addstr("He looked kindly upon you, and cured you.\n");
    843 				Player.p_poison = 0.0;
    844 			} else {
    845 				addstr("He rewarded you for your virtue.\n");
    846 				Player.p_mana += 50.0;
    847 				Player.p_shield += 2.0;
    848 			}
    849 		break;
    850 
    851 	case 9:
    852 		mvaddstr(4, 0, "You've found an amulet.\n");
    853 		++Player.p_amulets;
    854 		break;
    855 
    856 	case 10:
    857 		if (Player.p_blindness) {
    858 			mvaddstr(4, 0, "You've regained your sight!\n");
    859 			Player.p_blindness = FALSE;
    860 		}
    861 		break;
    862 
    863 	default:		/* deal with poison */
    864 		if (Player.p_poison > 0.0) {
    865 			temp = Player.p_poison * Statptr->c_weakness
    866 			    * Player.p_maxenergy / 600.0;
    867 			if (Player.p_energy > Player.p_maxenergy / 10.0
    868 			    && temp + 5.0 < Player.p_energy)
    869 				Player.p_energy -= temp;
    870 		}
    871 		break;
    872 	}
    873 }
    874 
    875 void
    876 genchar(type)
    877 	int     type;
    878 {
    879 	int     subscript;	/* used for subscripting into Stattable */
    880 	const struct charstats *statptr; /* for pointing into Stattable */
    881 
    882 	subscript = type - '1';
    883 
    884 	if (subscript < C_MAGIC || subscript > C_EXPER)
    885 		if (subscript != C_SUPER || !Wizard)
    886 			/* fighter is default */
    887 			subscript = C_FIGHTER;
    888 
    889 	statptr = &Stattable[subscript];
    890 
    891 	Player.p_quickness =
    892 	    ROLL(statptr->c_quickness.base, statptr->c_quickness.interval);
    893 	Player.p_strength =
    894 	    ROLL(statptr->c_strength.base, statptr->c_strength.interval);
    895 	Player.p_mana =
    896 	    ROLL(statptr->c_mana.base, statptr->c_mana.interval);
    897 	Player.p_maxenergy =
    898 	    Player.p_energy =
    899 	    ROLL(statptr->c_energy.base, statptr->c_energy.interval);
    900 	Player.p_brains =
    901 	    ROLL(statptr->c_brains.base, statptr->c_brains.interval);
    902 	Player.p_magiclvl =
    903 	    ROLL(statptr->c_magiclvl.base, statptr->c_magiclvl.interval);
    904 
    905 	Player.p_type = subscript;
    906 
    907 	if (Player.p_type == C_HALFLING)
    908 		/* give halfling some experience */
    909 		Player.p_experience = ROLL(600.0, 200.0);
    910 }
    911 
    912 void
    913 playinit()
    914 {
    915 	/* catch/ingnore signals */
    916 
    917 #ifdef	BSD41
    918 	sigignore(SIGQUIT);
    919 	sigignore(SIGALRM);
    920 	sigignore(SIGTERM);
    921 	sigignore(SIGTSTP);
    922 	sigignore(SIGTTIN);
    923 	sigignore(SIGTTOU);
    924 	sighold(SIGINT);
    925 	sigset(SIGHUP, ill_sig);
    926 	sigset(SIGTRAP, ill_sig);
    927 	sigset(SIGIOT, ill_sig);
    928 	sigset(SIGEMT, ill_sig);
    929 	sigset(SIGFPE, ill_sig);
    930 	sigset(SIGBUS, ill_sig);
    931 	sigset(SIGSEGV, ill_sig);
    932 	sigset(SIGSYS, ill_sig);
    933 	sigset(SIGPIPE, ill_sig);
    934 #endif
    935 #ifdef	BSD42
    936 	signal(SIGQUIT, ill_sig);
    937 	signal(SIGALRM, SIG_IGN);
    938 	signal(SIGTERM, SIG_IGN);
    939 	signal(SIGTSTP, SIG_IGN);
    940 	signal(SIGTTIN, SIG_IGN);
    941 	signal(SIGTTOU, SIG_IGN);
    942 	signal(SIGINT, ill_sig);
    943 	signal(SIGHUP, SIG_DFL);
    944 	signal(SIGTRAP, ill_sig);
    945 	signal(SIGIOT, ill_sig);
    946 	signal(SIGEMT, ill_sig);
    947 	signal(SIGFPE, ill_sig);
    948 	signal(SIGBUS, ill_sig);
    949 	signal(SIGSEGV, ill_sig);
    950 	signal(SIGSYS, ill_sig);
    951 	signal(SIGPIPE, ill_sig);
    952 #endif
    953 #ifdef	SYS3
    954 	signal(SIGINT, SIG_IGN);
    955 	signal(SIGQUIT, SIG_IGN);
    956 	signal(SIGTERM, SIG_IGN);
    957 	signal(SIGALRM, SIG_IGN);
    958 	signal(SIGHUP, ill_sig);
    959 	signal(SIGTRAP, ill_sig);
    960 	signal(SIGIOT, ill_sig);
    961 	signal(SIGEMT, ill_sig);
    962 	signal(SIGFPE, ill_sig);
    963 	signal(SIGBUS, ill_sig);
    964 	signal(SIGSEGV, ill_sig);
    965 	signal(SIGSYS, ill_sig);
    966 	signal(SIGPIPE, ill_sig);
    967 #endif
    968 #ifdef	SYS5
    969 	signal(SIGINT, SIG_IGN);
    970 	signal(SIGQUIT, SIG_IGN);
    971 	signal(SIGTERM, SIG_IGN);
    972 	signal(SIGALRM, SIG_IGN);
    973 	signal(SIGHUP, ill_sig);
    974 	signal(SIGTRAP, ill_sig);
    975 	signal(SIGIOT, ill_sig);
    976 	signal(SIGEMT, ill_sig);
    977 	signal(SIGFPE, ill_sig);
    978 	signal(SIGBUS, ill_sig);
    979 	signal(SIGSEGV, ill_sig);
    980 	signal(SIGSYS, ill_sig);
    981 	signal(SIGPIPE, ill_sig);
    982 #endif
    983 
    984 	initscr();		/* turn on curses */
    985 	noecho();		/* do not echo input */
    986 	cbreak();		/* do not process erase, kill */
    987 	clear();
    988 	refresh();
    989 	Windows = TRUE;		/* mark the state */
    990 }
    991 
    992 void
    993 cleanup(doexit)
    994 	int    doexit;
    995 {
    996 	if (Windows) {
    997 		move(LINES - 2, 0);
    998 		refresh();
    999 		nocbreak();
   1000 		endwin();
   1001 	}
   1002 	if (Playersfp)
   1003 		fclose(Playersfp);
   1004 	if (Monstfp)
   1005 		fclose(Monstfp);
   1006 	if (Messagefp)
   1007 		fclose(Messagefp);
   1008 	if (Energyvoidfp)
   1009 		fclose(Energyvoidfp);
   1010 
   1011 	if (doexit)
   1012 		exit(0);
   1013 	/* NOTREACHED */
   1014 }
   1015