Home | History | Annotate | Line # | Download | only in phantasia
main.c revision 1.19
      1 /*	$NetBSD: main.c,v 1.19 2009/05/25 23:14:33 dholland 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/stat.h>
     31 #include <sys/types.h>
     32 #include <pwd.h>
     33 
     34 /*
     35  * The program allocates as much file space as it needs to store characters,
     36  * so the possibility exists for the character file to grow without bound.
     37  * The file is purged upon normal entry to try to avoid that problem.
     38  * A similar problem exists for energy voids.  To alleviate the problem here,
     39  * the void file is cleared with every new king, and a limit is placed
     40  * on the size of the energy void file.
     41  */
     42 
     43 /*
     44  * Put one line of text into the file 'motd' for announcements, etc.
     45  */
     46 
     47 /*
     48  * The scoreboard file is updated when someone dies, and keeps track
     49  * of the highest character to date for that login.
     50  * Being purged from the character file does not cause the scoreboard
     51  * to be updated.
     52  */
     53 
     54 
     55 /*
     56  * main.c	Main routines for Phantasia
     57  */
     58 
     59 #include "include.h"
     60 #undef bool
     61 #include <curses.h>
     62 
     63 int	main(int, char **);
     64 
     65 int
     66 main(int argc, 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(void)
    272 {
    273 	struct stat sb;
    274 
    275 	Beyond = FALSE;
    276 	Marsh = FALSE;
    277 	Throne = FALSE;
    278 	Changed = FALSE;
    279 	Wizard = FALSE;
    280 	Timeout = FALSE;
    281 	Users = 0;
    282 	Windows = FALSE;
    283 	Echo = TRUE;
    284 
    285 	/* setup login name */
    286 	if ((Login = getlogin()) == NULL)
    287 		Login = getpwuid(getuid())->pw_name;
    288 
    289 	/* open some files */
    290 	if ((Playersfp = fopen(_PATH_PEOPLE, "r+")) == NULL)
    291 		error(_PATH_PEOPLE);
    292 	/* NOTREACHED */
    293 	if (fileno(Playersfp) < 3)
    294 		exit(1);
    295 
    296 	if ((Monstfp = fopen(_PATH_MONST, "r+")) == NULL)
    297 		error(_PATH_MONST);
    298 	/* NOTREACHED */
    299 
    300 	if ((Messagefp = fopen(_PATH_MESS, "r")) == NULL)
    301 		error(_PATH_MESS);
    302 	/* NOTREACHED */
    303 
    304 	if ((Energyvoidfp = fopen(_PATH_VOID, "r+")) == NULL)
    305 		error(_PATH_VOID);
    306 	if (fstat(fileno(Energyvoidfp), &sb) == -1)
    307 		error("stat");
    308 	if (sb.st_size == 0) {
    309 		/* initialize grail to new location */
    310 		Enrgyvoid.ev_active = TRUE;
    311 		Enrgyvoid.ev_x = ROLL(-1.0e6, 2.0e6);
    312 		Enrgyvoid.ev_y = ROLL(-1.0e6, 2.0e6);
    313 		writevoid(&Enrgyvoid, 0L);
    314 	}
    315 
    316 	/* NOTREACHED */
    317 
    318 	srandom((unsigned) time(NULL));	/* prime random numbers */
    319 }
    320 
    321 long
    322 rollnewplayer(void)
    323 {
    324 	int     chartype;	/* character type */
    325 	int     ch;		/* input */
    326 
    327 	initplayer(&Player);	/* initialize player structure */
    328 
    329 	clear();
    330 	mvaddstr(4, 21, "Which type of character do you want:");
    331 	mvaddstr(8, 4,
    332 "1:Magic User  2:Fighter  3:Elf  4:Dwarf  5:Halfling  6:Experimento  ");
    333 	if (Wizard) {
    334 		addstr("7:Super  ? ");
    335 		chartype = getanswer("1234567", FALSE);
    336 	} else {
    337 		addstr("?  ");
    338 		chartype = getanswer("123456", FALSE);
    339 	}
    340 
    341 	do {
    342 		genchar(chartype);	/* roll up a character */
    343 
    344 		/* print out results */
    345 		mvprintw(12, 14,
    346 		    "Strength    :  %2.0f  Quickness:  %2.0f  Mana       :  %2.0f\n",
    347 		    Player.p_strength, Player.p_quickness, Player.p_mana);
    348 		mvprintw(13, 14,
    349 		    "Energy Level:  %2.0f  Brains   :  %2.0f  Magic Level:  %2.0f\n",
    350 		    Player.p_energy, Player.p_brains, Player.p_magiclvl);
    351 
    352 		if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
    353 			break;
    354 
    355 		mvaddstr(14, 14, "Type '1' to keep >");
    356 		ch = getanswer(" ", TRUE);
    357 	}
    358 	while (ch != '1');
    359 
    360 	if (Player.p_type == C_EXPER || Player.p_type == C_SUPER)
    361 		/* get coordinates for experimento */
    362 		for (;;) {
    363 			mvaddstr(16, 0, "Enter the X Y coordinates of your experimento ? ");
    364 			getstring(Databuf, SZ_DATABUF);
    365 			sscanf(Databuf, "%lf %lf", &Player.p_x, &Player.p_y);
    366 
    367 			if (fabs(Player.p_x) > D_EXPER || fabs(Player.p_y) > D_EXPER)
    368 				mvaddstr(17, 0, "Invalid coordinates.  Try again.\n");
    369 			else
    370 				break;
    371 		}
    372 
    373 	for (;;)
    374 		/* name the new character */
    375 	{
    376 		mvprintw(18, 0,
    377 		    "Give your character a name [up to %d characters] ?  ", SZ_NAME - 1);
    378 		getstring(Player.p_name, SZ_NAME);
    379 		truncstring(Player.p_name);	/* remove trailing blanks */
    380 
    381 		if (Player.p_name[0] == '\0')
    382 			/* no null names */
    383 			mvaddstr(19, 0, "Invalid name.");
    384 		else
    385 			if (findname(Player.p_name, &Other) >= 0L)
    386 				/* cannot have duplicate names */
    387 				mvaddstr(19, 0, "Name already in use.");
    388 			else
    389 				/* name is acceptable */
    390 				break;
    391 
    392 		addstr("  Pick another.\n");
    393 	}
    394 
    395 	/* get a password for character */
    396 	Echo = FALSE;
    397 
    398 	do {
    399 		mvaddstr(20, 0, "Give your character a password [up to 8 characters] ? ");
    400 		getstring(Player.p_password, SZ_PASSWORD);
    401 		mvaddstr(21, 0, "Enter again to verify: ");
    402 		getstring(Databuf, SZ_PASSWORD);
    403 	}
    404 	while (strcmp(Player.p_password, Databuf) != 0);
    405 
    406 	Echo = TRUE;
    407 
    408 	return (allocrecord());
    409 }
    410 
    411 void
    412 procmain(void)
    413 {
    414 	int     ch;		/* input */
    415 	double  x;		/* desired new x coordinate */
    416 	double  y;		/* desired new y coordinate */
    417 	double  temp;		/* for temporary calculations */
    418 	FILE   *fp;		/* for opening files */
    419 	int     loop;		/* a loop counter */
    420 	bool    hasmoved = FALSE;	/* set if player has moved */
    421 
    422 	ch = inputoption();
    423 	mvaddstr(4, 0, "\n\n");	/* clear status area */
    424 
    425 	move(7, 0);
    426 	clrtobot();		/* clear data on bottom area of screen */
    427 
    428 	if (Player.p_specialtype == SC_VALAR && (ch == '1' || ch == '7'))
    429 		/* valar cannot move */
    430 		ch = ' ';
    431 
    432 	switch (ch) {
    433 	case 'K':		/* move up/north */
    434 	case 'N':
    435 		x = Player.p_x;
    436 		y = Player.p_y + MAXMOVE();
    437 		hasmoved = TRUE;
    438 		break;
    439 
    440 	case 'J':		/* move down/south */
    441 	case 'S':
    442 		x = Player.p_x;
    443 		y = Player.p_y - MAXMOVE();
    444 		hasmoved = TRUE;
    445 		break;
    446 
    447 	case 'L':		/* move right/east */
    448 	case 'E':
    449 		x = Player.p_x + MAXMOVE();
    450 		y = Player.p_y;
    451 		hasmoved = TRUE;
    452 		break;
    453 
    454 	case 'H':		/* move left/west */
    455 	case 'W':
    456 		x = Player.p_x - MAXMOVE();
    457 		y = Player.p_y;
    458 		hasmoved = TRUE;
    459 		break;
    460 
    461 	default:		/* rest */
    462 		Player.p_energy += (Player.p_maxenergy + Player.p_shield) / 15.0
    463 		    + Player.p_level / 3.0 + 2.0;
    464 		Player.p_energy =
    465 		    MIN(Player.p_energy, Player.p_maxenergy + Player.p_shield);
    466 
    467 		if (Player.p_status != S_CLOAKED)
    468 			/* cannot find mana if cloaked */
    469 		{
    470 			Player.p_mana += (Circle + Player.p_level) / 4.0;
    471 
    472 			if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
    473 				/* wandering monster */
    474 				encounter(-1);
    475 		}
    476 		break;
    477 
    478 	case 'X':		/* change/examine a character */
    479 		changestats(TRUE);
    480 		break;
    481 
    482 	case '1':		/* move */
    483 		for (loop = 3; loop; --loop) {
    484 			mvaddstr(4, 0, "X Y Coordinates ? ");
    485 			getstring(Databuf, SZ_DATABUF);
    486 
    487 			if (sscanf(Databuf, "%lf %lf", &x, &y) != 2)
    488 				mvaddstr(5, 0, "Try again\n");
    489 			else
    490 				if (distance(Player.p_x, x, Player.p_y, y) > MAXMOVE())
    491 					ILLMOVE();
    492 				else {
    493 					hasmoved = TRUE;
    494 					break;
    495 				}
    496 		}
    497 		break;
    498 
    499 	case '2':		/* players */
    500 		userlist(TRUE);
    501 		break;
    502 
    503 	case '3':		/* message */
    504 		mvaddstr(4, 0, "Message ? ");
    505 		getstring(Databuf, SZ_DATABUF);
    506 		/* we open the file for writing to erase any data which is
    507 		 * already there */
    508 		fp = fopen(_PATH_MESS, "w");
    509 		if (Databuf[0] != '\0')
    510 			fprintf(fp, "%s: %s", Player.p_name, Databuf);
    511 		fclose(fp);
    512 		break;
    513 
    514 	case '4':		/* stats */
    515 		allstatslist();
    516 		break;
    517 
    518 	case '5':		/* good-bye */
    519 		leavegame();
    520 		/* NOTREACHED */
    521 
    522 	case '6':		/* cloak */
    523 		if (Player.p_level < MEL_CLOAK || Player.p_magiclvl < ML_CLOAK)
    524 			ILLCMD();
    525 		else
    526 			if (Player.p_status == S_CLOAKED)
    527 				Player.p_status = S_PLAYING;
    528 			else
    529 				if (Player.p_mana < MM_CLOAK)
    530 					mvaddstr(5, 0, "No mana left.\n");
    531 				else {
    532 					Changed = TRUE;
    533 					Player.p_mana -= MM_CLOAK;
    534 					Player.p_status = S_CLOAKED;
    535 				}
    536 		break;
    537 
    538 	case '7':		/* teleport */
    539 		/*
    540 	         * conditions for teleport
    541 	         *	- 20 per (level plus magic level)
    542 	         *	- OR council of the wise or valar or ex-valar
    543 	         *	- OR transport from throne
    544 	         * transports from throne cost no mana
    545 	         */
    546 		if (Player.p_level < MEL_TELEPORT || Player.p_magiclvl < ML_TELEPORT)
    547 			ILLCMD();
    548 		else
    549 			for (loop = 3; loop; --loop) {
    550 				mvaddstr(4, 0, "X Y Coordinates ? ");
    551 				getstring(Databuf, SZ_DATABUF);
    552 
    553 				if (sscanf(Databuf, "%lf %lf", &x, &y) == 2) {
    554 					temp = distance(Player.p_x, x, Player.p_y, y);
    555 					if (!Throne
    556 					/* can transport anywhere from throne */
    557 					    && Player.p_specialtype <= SC_COUNCIL
    558 					/* council, valar can transport
    559 					 * anywhere */
    560 					    && temp > (Player.p_level + Player.p_magiclvl) * 20.0)
    561 						/* can only move 20 per exp.
    562 						 * level + mag. level */
    563 						ILLMOVE();
    564 					else {
    565 						temp = (temp / 75.0 + 1.0) * 20.0;	/* mana used */
    566 
    567 						if (!Throne && temp > Player.p_mana)
    568 							mvaddstr(5, 0, "Not enough power for that distance.\n");
    569 						else {
    570 							if (!Throne)
    571 								Player.p_mana -= temp;
    572 							hasmoved = TRUE;
    573 							break;
    574 						}
    575 					}
    576 				}
    577 			}
    578 		break;
    579 
    580 	case 'C':
    581 	case '9':		/* monster */
    582 		if (Throne)
    583 			/* no monsters while on throne */
    584 			mvaddstr(5, 0, "No monsters in the chamber!\n");
    585 		else
    586 			if (Player.p_specialtype != SC_VALAR)
    587 				/* the valar cannot call monsters */
    588 			{
    589 				Player.p_sin += 1e-6;
    590 				encounter(-1);
    591 			}
    592 		break;
    593 
    594 	case '0':		/* decree */
    595 		if (Wizard || (Player.p_specialtype == SC_KING && Throne))
    596 			/* kings must be on throne to decree */
    597 			dotampered();
    598 		else
    599 			ILLCMD();
    600 		break;
    601 
    602 	case '8':		/* intervention */
    603 		if (Wizard || Player.p_specialtype >= SC_COUNCIL)
    604 			dotampered();
    605 		else
    606 			ILLCMD();
    607 		break;
    608 	}
    609 
    610 	if (hasmoved)
    611 		/* player has moved -- alter coordinates, and do random
    612 		 * monster */
    613 	{
    614 		altercoordinates(x, y, A_SPECIFIC);
    615 
    616 		if (drandom() < 0.2 && Player.p_status == S_PLAYING && !Throne)
    617 			encounter(-1);
    618 	}
    619 }
    620 
    621 void
    622 titlelist(void)
    623 {
    624 	FILE   *fp;		/* used for opening various files */
    625 	bool    councilfound = FALSE;	/* set if we find a member of the
    626 					 * council */
    627 	bool    kingfound = FALSE;	/* set if we find a king */
    628 	double  hiexp, nxtexp;	/* used for finding the two highest players */
    629 	double  hilvl, nxtlvl;	/* used for finding the two highest players */
    630 	char    hiname[21], nxtname[21];	/* used for finding the two
    631 						 * highest players */
    632 
    633 	nxtexp = 0;
    634 	mvaddstr(0, 14,
    635 	    "W e l c o m e   t o   P h a n t a s i a (vers. 3.3.2)!");
    636 
    637 	/* print message of the day */
    638 	if ((fp = fopen(_PATH_MOTD, "r")) != NULL
    639 	    && fgets(Databuf, SZ_DATABUF, fp) != NULL) {
    640 		mvaddstr(2, 40 - strlen(Databuf) / 2, Databuf);
    641 		fclose(fp);
    642 	}
    643 	/* search for king */
    644 	fseek(Playersfp, 0L, SEEK_SET);
    645 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    646 		if (Other.p_specialtype == SC_KING &&
    647 		    Other.p_status != S_NOTUSED)
    648 			/* found the king */
    649 		{
    650 			snprintf(Databuf, SZ_DATABUF,
    651 			    "The present ruler is %s  Level:%.0f",
    652 			    Other.p_name, Other.p_level);
    653 			mvaddstr(4, 40 - strlen(Databuf) / 2, Databuf);
    654 			kingfound = TRUE;
    655 			break;
    656 		}
    657 	if (!kingfound)
    658 		mvaddstr(4, 24, "There is no ruler at this time.");
    659 
    660 	/* search for valar */
    661 	fseek(Playersfp, 0L, SEEK_SET);
    662 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    663 		if (Other.p_specialtype == SC_VALAR && Other.p_status != S_NOTUSED)
    664 			/* found the valar */
    665 		{
    666 			snprintf(Databuf, SZ_DATABUF,
    667 				"The Valar is %s   Login:  %s",
    668 				Other.p_name, Other.p_login);
    669 			mvaddstr(6, 40 - strlen(Databuf) / 2, Databuf);
    670 			break;
    671 		}
    672 	/* search for council of the wise */
    673 	fseek(Playersfp, 0L, SEEK_SET);
    674 	Lines = 10;
    675 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    676 		if (Other.p_specialtype == SC_COUNCIL && Other.p_status != S_NOTUSED)
    677 			/* found a member of the council */
    678 		{
    679 			if (!councilfound) {
    680 				mvaddstr(8, 30, "Council of the Wise:");
    681 				councilfound = TRUE;
    682 			}
    683 			/* This assumes a finite (<=5) number of C.O.W.: */
    684 			snprintf(Databuf, SZ_DATABUF,
    685 				"%s   Login:  %s", Other.p_name, Other.p_login);
    686 			mvaddstr(Lines++, 40 - strlen(Databuf) / 2, Databuf);
    687 		}
    688 	/* search for the two highest players */
    689 	nxtname[0] = hiname[0] = '\0';
    690 	hiexp = 0.0;
    691 	nxtlvl = hilvl = 0;
    692 
    693 	fseek(Playersfp, 0L, SEEK_SET);
    694 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
    695 		if (Other.p_experience > hiexp && Other.p_specialtype <= SC_KING && Other.p_status != S_NOTUSED)
    696 			/* highest found so far */
    697 		{
    698 			nxtexp = hiexp;
    699 			hiexp = Other.p_experience;
    700 			nxtlvl = hilvl;
    701 			hilvl = Other.p_level;
    702 			strcpy(nxtname, hiname);
    703 			strcpy(hiname, Other.p_name);
    704 		} else
    705 			if (Other.p_experience > nxtexp
    706 			    && Other.p_specialtype <= SC_KING
    707 			    && Other.p_status != S_NOTUSED)
    708 				/* next highest found so far */
    709 			{
    710 				nxtexp = Other.p_experience;
    711 				nxtlvl = Other.p_level;
    712 				strcpy(nxtname, Other.p_name);
    713 			}
    714 	mvaddstr(15, 28, "Highest characters are:");
    715 	snprintf(Databuf, SZ_DATABUF,
    716 	    "%s  Level:%.0f   and   %s  Level:%.0f",
    717 	    hiname, hilvl, nxtname, nxtlvl);
    718 	mvaddstr(17, 40 - strlen(Databuf) / 2, Databuf);
    719 
    720 	/* print last to die */
    721 	if ((fp = fopen(_PATH_LASTDEAD, "r")) != NULL
    722 	    && fgets(Databuf, SZ_DATABUF, fp) != NULL) {
    723 		mvaddstr(19, 25, "The last character to die was:");
    724 		mvaddstr(20, 40 - strlen(Databuf) / 2, Databuf);
    725 	}
    726 	if (fp)
    727 		fclose(fp);
    728 	refresh();
    729 }
    730 
    731 long
    732 recallplayer(void)
    733 {
    734 	long    loc = 0L;	/* location in player file */
    735 	int     loop;		/* loop counter */
    736 	int     ch;		/* input */
    737 
    738 	clear();
    739 	mvprintw(10, 0, "What was your character's name ? ");
    740 	getstring(Databuf, SZ_NAME);
    741 	truncstring(Databuf);
    742 
    743 	if ((loc = findname(Databuf, &Player)) >= 0L)
    744 		/* found character */
    745 	{
    746 		Echo = FALSE;
    747 
    748 		for (loop = 0; loop < 2; ++loop) {
    749 			/* prompt for password */
    750 			mvaddstr(11, 0, "Password ? ");
    751 			getstring(Databuf, SZ_PASSWORD);
    752 			if (strcmp(Databuf, Player.p_password) == 0)
    753 				/* password good */
    754 			{
    755 				Echo = TRUE;
    756 
    757 				if (Player.p_status != S_OFF)
    758 					/* player did not exit normally last
    759 					 * time */
    760 				{
    761 					clear();
    762 					addstr("Your character did not exit normally last time.\n");
    763 					addstr("If you think you have good cause to have your character saved,\n");
    764 					printw("you may quit and mail your reason to 'root'.\n");
    765 					addstr("Otherwise, continuing spells certain death.\n");
    766 					addstr("Do you want to quit ? ");
    767 					ch = getanswer("YN", FALSE);
    768 					if (ch == 'Y') {
    769 						Player.p_status = S_HUNGUP;
    770 						writerecord(&Player, loc);
    771 						cleanup(TRUE);
    772 						/* NOTREACHED */
    773 					}
    774 					death("Stupidity");
    775 					/* NOTREACHED */
    776 				}
    777 				return (loc);
    778 			} else
    779 				mvaddstr(12, 0, "No good.\n");
    780 		}
    781 
    782 		Echo = TRUE;
    783 	} else
    784 		mvaddstr(11, 0, "Not found.\n");
    785 
    786 	more(13);
    787 	return (-1L);
    788 }
    789 
    790 void
    791 neatstuff(void)
    792 {
    793 	double  temp;		/* for temporary calculations */
    794 	int     ch;		/* input */
    795 
    796 	switch ((int) ROLL(0.0, 100.0)) {
    797 	case 1:
    798 	case 2:
    799 		if (Player.p_poison > 0.0) {
    800 			mvaddstr(4, 0, "You've found a medic!  How much will you offer to be cured ? ");
    801 			temp = floor(infloat());
    802 			if (temp < 0.0 || temp > Player.p_gold)
    803 				/* negative gold, or more than available */
    804 			{
    805 				mvaddstr(6, 0, "He was not amused, and made you worse.\n");
    806 				Player.p_poison += 1.0;
    807 			} else
    808 				if (drandom() / 2.0 > (temp + 1.0) / MAX(Player.p_gold, 1))
    809 					/* medic wants 1/2 of available gold */
    810 					mvaddstr(5, 0, "Sorry, he wasn't interested.\n");
    811 				else {
    812 					mvaddstr(5, 0, "He accepted.");
    813 					Player.p_poison = MAX(0.0, Player.p_poison - 1.0);
    814 					Player.p_gold -= temp;
    815 				}
    816 		}
    817 		break;
    818 
    819 	case 3:
    820 		mvaddstr(4, 0, "You've been caught raping and pillaging!\n");
    821 		Player.p_experience += 4000.0;
    822 		Player.p_sin += 0.5;
    823 		break;
    824 
    825 	case 4:
    826 		temp = ROLL(10.0, 75.0);
    827 		mvprintw(4, 0, "You've found %.0f gold pieces, want them ? ", temp);
    828 		ch = getanswer("NY", FALSE);
    829 
    830 		if (ch == 'Y')
    831 			collecttaxes(temp, 0.0);
    832 		break;
    833 
    834 	case 5:
    835 		if (Player.p_sin > 1.0) {
    836 			mvaddstr(4, 0, "You've found a Holy Orb!\n");
    837 			Player.p_sin -= 0.25;
    838 		}
    839 		break;
    840 
    841 	case 6:
    842 		if (Player.p_poison < 1.0) {
    843 			mvaddstr(4, 0, "You've been hit with a plague!\n");
    844 			Player.p_poison += 1.0;
    845 		}
    846 		break;
    847 
    848 	case 7:
    849 		mvaddstr(4, 0, "You've found some holy water.\n");
    850 		++Player.p_holywater;
    851 		break;
    852 
    853 	case 8:
    854 		mvaddstr(4, 0, "You've met a Guru. . .");
    855 		if (drandom() * Player.p_sin > 1.0)
    856 			addstr("You disgusted him with your sins!\n");
    857 		else
    858 			if (Player.p_poison > 0.0) {
    859 				addstr("He looked kindly upon you, and cured you.\n");
    860 				Player.p_poison = 0.0;
    861 			} else {
    862 				addstr("He rewarded you for your virtue.\n");
    863 				Player.p_mana += 50.0;
    864 				Player.p_shield += 2.0;
    865 			}
    866 		break;
    867 
    868 	case 9:
    869 		mvaddstr(4, 0, "You've found an amulet.\n");
    870 		++Player.p_amulets;
    871 		break;
    872 
    873 	case 10:
    874 		if (Player.p_blindness) {
    875 			mvaddstr(4, 0, "You've regained your sight!\n");
    876 			Player.p_blindness = FALSE;
    877 		}
    878 		break;
    879 
    880 	default:		/* deal with poison */
    881 		if (Player.p_poison > 0.0) {
    882 			temp = Player.p_poison * Statptr->c_weakness
    883 			    * Player.p_maxenergy / 600.0;
    884 			if (Player.p_energy > Player.p_maxenergy / 10.0
    885 			    && temp + 5.0 < Player.p_energy)
    886 				Player.p_energy -= temp;
    887 		}
    888 		break;
    889 	}
    890 }
    891 
    892 void
    893 genchar(int type)
    894 {
    895 	int     subscript;	/* used for subscripting into Stattable */
    896 	const struct charstats *statptr; /* for pointing into Stattable */
    897 
    898 	subscript = type - '1';
    899 
    900 	if (subscript < C_MAGIC || subscript > C_EXPER)
    901 		if (subscript != C_SUPER || !Wizard)
    902 			/* fighter is default */
    903 			subscript = C_FIGHTER;
    904 
    905 	statptr = &Stattable[subscript];
    906 
    907 	Player.p_quickness =
    908 	    ROLL(statptr->c_quickness.base, statptr->c_quickness.interval);
    909 	Player.p_strength =
    910 	    ROLL(statptr->c_strength.base, statptr->c_strength.interval);
    911 	Player.p_mana =
    912 	    ROLL(statptr->c_mana.base, statptr->c_mana.interval);
    913 	Player.p_maxenergy =
    914 	    Player.p_energy =
    915 	    ROLL(statptr->c_energy.base, statptr->c_energy.interval);
    916 	Player.p_brains =
    917 	    ROLL(statptr->c_brains.base, statptr->c_brains.interval);
    918 	Player.p_magiclvl =
    919 	    ROLL(statptr->c_magiclvl.base, statptr->c_magiclvl.interval);
    920 
    921 	Player.p_type = subscript;
    922 
    923 	if (Player.p_type == C_HALFLING)
    924 		/* give halfling some experience */
    925 		Player.p_experience = ROLL(600.0, 200.0);
    926 }
    927 
    928 void
    929 playinit(void)
    930 {
    931 	/* catch/ingnore signals */
    932 
    933 #ifdef	BSD41
    934 	sigignore(SIGQUIT);
    935 	sigignore(SIGALRM);
    936 	sigignore(SIGTERM);
    937 	sigignore(SIGTSTP);
    938 	sigignore(SIGTTIN);
    939 	sigignore(SIGTTOU);
    940 	sighold(SIGINT);
    941 	sigset(SIGHUP, ill_sig);
    942 	sigset(SIGTRAP, ill_sig);
    943 	sigset(SIGIOT, ill_sig);
    944 	sigset(SIGEMT, ill_sig);
    945 	sigset(SIGFPE, ill_sig);
    946 	sigset(SIGBUS, ill_sig);
    947 	sigset(SIGSEGV, ill_sig);
    948 	sigset(SIGSYS, ill_sig);
    949 	sigset(SIGPIPE, ill_sig);
    950 #endif
    951 #ifdef	BSD42
    952 	signal(SIGQUIT, ill_sig);
    953 	signal(SIGALRM, SIG_IGN);
    954 	signal(SIGTERM, SIG_IGN);
    955 	signal(SIGTSTP, SIG_IGN);
    956 	signal(SIGTTIN, SIG_IGN);
    957 	signal(SIGTTOU, SIG_IGN);
    958 	signal(SIGINT, ill_sig);
    959 	signal(SIGHUP, SIG_DFL);
    960 	signal(SIGTRAP, ill_sig);
    961 	signal(SIGIOT, ill_sig);
    962 	signal(SIGEMT, ill_sig);
    963 	signal(SIGFPE, ill_sig);
    964 	signal(SIGBUS, ill_sig);
    965 	signal(SIGSEGV, ill_sig);
    966 	signal(SIGSYS, ill_sig);
    967 	signal(SIGPIPE, ill_sig);
    968 #endif
    969 #ifdef	SYS3
    970 	signal(SIGINT, SIG_IGN);
    971 	signal(SIGQUIT, SIG_IGN);
    972 	signal(SIGTERM, SIG_IGN);
    973 	signal(SIGALRM, SIG_IGN);
    974 	signal(SIGHUP, ill_sig);
    975 	signal(SIGTRAP, ill_sig);
    976 	signal(SIGIOT, ill_sig);
    977 	signal(SIGEMT, ill_sig);
    978 	signal(SIGFPE, ill_sig);
    979 	signal(SIGBUS, ill_sig);
    980 	signal(SIGSEGV, ill_sig);
    981 	signal(SIGSYS, ill_sig);
    982 	signal(SIGPIPE, ill_sig);
    983 #endif
    984 #ifdef	SYS5
    985 	signal(SIGINT, SIG_IGN);
    986 	signal(SIGQUIT, SIG_IGN);
    987 	signal(SIGTERM, SIG_IGN);
    988 	signal(SIGALRM, SIG_IGN);
    989 	signal(SIGHUP, ill_sig);
    990 	signal(SIGTRAP, ill_sig);
    991 	signal(SIGIOT, ill_sig);
    992 	signal(SIGEMT, ill_sig);
    993 	signal(SIGFPE, ill_sig);
    994 	signal(SIGBUS, ill_sig);
    995 	signal(SIGSEGV, ill_sig);
    996 	signal(SIGSYS, ill_sig);
    997 	signal(SIGPIPE, ill_sig);
    998 #endif
    999 
   1000 	if (!initscr()) {	/* turn on curses */
   1001 		fprintf(stderr, "couldn't initialize screen\n");
   1002 		exit (0);
   1003 	}
   1004 	noecho();		/* do not echo input */
   1005 	cbreak();		/* do not process erase, kill */
   1006 	clear();
   1007 	refresh();
   1008 	Windows = TRUE;		/* mark the state */
   1009 }
   1010 
   1011 void
   1012 cleanup(int doexit)
   1013 {
   1014 	if (Windows) {
   1015 		move(LINES - 2, 0);
   1016 		refresh();
   1017 		nocbreak();
   1018 		endwin();
   1019 	}
   1020 	if (Playersfp)
   1021 		fclose(Playersfp);
   1022 	if (Monstfp)
   1023 		fclose(Monstfp);
   1024 	if (Messagefp)
   1025 		fclose(Messagefp);
   1026 	if (Energyvoidfp)
   1027 		fclose(Energyvoidfp);
   1028 
   1029 	if (doexit)
   1030 		exit(0);
   1031 	/* NOTREACHED */
   1032 }
   1033