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