xref: /netbsd-src/games/phantasia/misc.c (revision 9f4a9600be3013fd256265533fbb085e3c80d678)
1*9f4a9600Sandvar /*	$NetBSD: misc.c,v 1.24 2022/05/24 06:27:59 andvar Exp $	*/
2dcfebd78Scgd 
386a62b8dSjtc /*
486a62b8dSjtc  * misc.c  Phantasia miscellaneous support routines
586a62b8dSjtc  */
686a62b8dSjtc 
7e28fc908Sdholland #include <errno.h>
8e28fc908Sdholland #include <math.h>
9e28fc908Sdholland #include <setjmp.h>
10e28fc908Sdholland #include <signal.h>
11e28fc908Sdholland #include <stdio.h>
12e28fc908Sdholland #include <stdlib.h>
13e28fc908Sdholland #include <string.h>
14e28fc908Sdholland #include <unistd.h>
15e28fc908Sdholland 
16e28fc908Sdholland #include "macros.h"
17e28fc908Sdholland #include "phantdefs.h"
18e28fc908Sdholland #include "phantstruct.h"
19e28fc908Sdholland #include "phantglobs.h"
20e28fc908Sdholland #include "pathnames.h"
21e28fc908Sdholland 
229b1375acShe #undef bool
239209ce5aSross #include <curses.h>
2486a62b8dSjtc 
2586a62b8dSjtc 
265305281bSdholland static double explevel(double);
275305281bSdholland 
285305281bSdholland static void
movelevel(void)29c7a109ccSdholland movelevel(void)
3086a62b8dSjtc {
31092d3130Sjsm 	const struct charstats *statptr; /* for pointing into Stattable */
3286a62b8dSjtc 	double  new;		/* new level */
3386a62b8dSjtc 	double  inc;		/* increment between new and old levels */
3486a62b8dSjtc 
3586a62b8dSjtc 	Changed = TRUE;
3686a62b8dSjtc 
3786a62b8dSjtc 	if (Player.p_type == C_EXPER)
3886a62b8dSjtc 		/* roll a type to use for increment */
3986a62b8dSjtc 		statptr = &Stattable[(int) ROLL(C_MAGIC, C_HALFLING - C_MAGIC + 1)];
4086a62b8dSjtc 	else
4186a62b8dSjtc 		statptr = Statptr;
4286a62b8dSjtc 
4386a62b8dSjtc 	new = explevel(Player.p_experience);
4486a62b8dSjtc 	inc = new - Player.p_level;
4586a62b8dSjtc 	Player.p_level = new;
4686a62b8dSjtc 
4786a62b8dSjtc 	/* add increments to statistics */
4886a62b8dSjtc 	Player.p_strength += statptr->c_strength.increase * inc;
4986a62b8dSjtc 	Player.p_mana += statptr->c_mana.increase * inc;
5086a62b8dSjtc 	Player.p_brains += statptr->c_brains.increase * inc;
5186a62b8dSjtc 	Player.p_magiclvl += statptr->c_magiclvl.increase * inc;
5286a62b8dSjtc 	Player.p_maxenergy += statptr->c_energy.increase * inc;
5386a62b8dSjtc 
5486a62b8dSjtc 	/* rest to maximum upon reaching new level */
5586a62b8dSjtc 	Player.p_energy = Player.p_maxenergy + Player.p_shield;
5686a62b8dSjtc 
5786a62b8dSjtc 	if (Player.p_crowns > 0 && Player.p_level >= 1000.0)
5886a62b8dSjtc 		/* no longer able to be king -- turn crowns into cash */
5986a62b8dSjtc 	{
6086a62b8dSjtc 		Player.p_gold += ((double) Player.p_crowns) * 5000.0;
6186a62b8dSjtc 		Player.p_crowns = 0;
6286a62b8dSjtc 	}
6386a62b8dSjtc 	if (Player.p_level >= 3000.0 && Player.p_specialtype < SC_COUNCIL)
6486a62b8dSjtc 		/* make a member of the council */
6586a62b8dSjtc 	{
6686a62b8dSjtc 		mvaddstr(6, 0, "You have made it to the Council of the Wise.\n");
6786a62b8dSjtc 		addstr("Good Luck on your search for the Holy Grail.\n");
6886a62b8dSjtc 
6986a62b8dSjtc 		Player.p_specialtype = SC_COUNCIL;
7086a62b8dSjtc 
7186a62b8dSjtc 		/* no rings for council and above */
7286a62b8dSjtc 		Player.p_ring.ring_type = R_NONE;
7386a62b8dSjtc 		Player.p_ring.ring_duration = 0;
7486a62b8dSjtc 
7586a62b8dSjtc 		Player.p_lives = 3;	/* three extra lives */
7686a62b8dSjtc 	}
7786a62b8dSjtc 	if (Player.p_level > 9999.0 && Player.p_specialtype != SC_VALAR)
7886a62b8dSjtc 		death("Old age");
7986a62b8dSjtc }
8086a62b8dSjtc 
81092d3130Sjsm const char   *
descrlocation(struct player * playerp,phbool shortflag)82c7a109ccSdholland descrlocation(struct player *playerp, phbool shortflag)
8386a62b8dSjtc {
8486a62b8dSjtc 	double  circle;		/* corresponding circle for coordinates */
85a232aee2Slukem 	int     quadrant;	/* quandrant of grid */
86092d3130Sjsm 	const char   *label;	/* pointer to place name */
87092d3130Sjsm 	static const char *const nametable[4][4] =	/* names of places */
8886a62b8dSjtc 	{
89a232aee2Slukem 		{"Anorien", "Ithilien", "Rohan", "Lorien"},
90a232aee2Slukem 		{"Gondor", "Mordor", "Dunland", "Rovanion"},
91a232aee2Slukem 		{"South Gondor", "Khand", "Eriador", "The Iron Hills"},
92a232aee2Slukem 		{"Far Harad", "Near Harad", "The Northern Waste", "Rhun"}
9386a62b8dSjtc 	};
9486a62b8dSjtc 
9586a62b8dSjtc 	if (playerp->p_specialtype == SC_VALAR)
9686a62b8dSjtc 		return (" is in Valhala");
97a232aee2Slukem 	else
98a232aee2Slukem 		if ((circle = CIRCLE(playerp->p_x, playerp->p_y)) >= 1000.0) {
9986a62b8dSjtc 			if (MAX(fabs(playerp->p_x), fabs(playerp->p_y)) > D_BEYOND)
10086a62b8dSjtc 				label = "The Point of No Return";
10186a62b8dSjtc 			else
10286a62b8dSjtc 				label = "The Ashen Mountains";
103a232aee2Slukem 		} else
104a232aee2Slukem 			if (circle >= 55)
10586a62b8dSjtc 				label = "Morannon";
10686a62b8dSjtc 			else
107a232aee2Slukem 				if (circle >= 35)
108a232aee2Slukem 					label = "Kennaquahair";
109a232aee2Slukem 				else
110a232aee2Slukem 					if (circle >= 20)
111a232aee2Slukem 						label = "The Dead Marshes";
112a232aee2Slukem 					else
113a232aee2Slukem 						if (circle >= 9)
114a232aee2Slukem 							label = "The Outer Waste";
115a232aee2Slukem 						else
116a232aee2Slukem 							if (circle >= 5)
117a232aee2Slukem 								label = "The Moors Adventurous";
118a232aee2Slukem 							else {
11986a62b8dSjtc 								if (playerp->p_x == 0.0 && playerp->p_y == 0.0)
12086a62b8dSjtc 									label = "The Lord's Chamber";
121a232aee2Slukem 								else {
122a232aee2Slukem 									/* this
123a232aee2Slukem 									 *
124a232aee2Slukem 									 * expr
125a232aee2Slukem 									 * essi
126a232aee2Slukem 									 * on
127a232aee2Slukem 									 * is
128a232aee2Slukem 									 * spli
129a232aee2Slukem 									 * t
130a232aee2Slukem 									 * to
131a232aee2Slukem 									 * prev
132a232aee2Slukem 									 * ent
133a232aee2Slukem 									 * comp
134a232aee2Slukem 									 * iler
135a232aee2Slukem 									 *
136a232aee2Slukem 									 * loop
137a232aee2Slukem 									 *
138a232aee2Slukem 									 * with
139a232aee2Slukem 									 *
140a232aee2Slukem 									 * some
141a232aee2Slukem 									 *
142a232aee2Slukem 									 * comp
143a232aee2Slukem 									 * iler
144a232aee2Slukem 									 * s */
14586a62b8dSjtc 									quadrant = ((playerp->p_x > 0.0) ? 1 : 0);
14686a62b8dSjtc 									quadrant += ((playerp->p_y >= 0.0) ? 2 : 0);
14786a62b8dSjtc 									label = nametable[((int) circle) - 1][quadrant];
14886a62b8dSjtc 								}
14986a62b8dSjtc 							}
15086a62b8dSjtc 
15186a62b8dSjtc 	if (shortflag)
152ebb769aeSdholland 		snprintf(Databuf, SZ_DATABUF, "%.29s", label);
15386a62b8dSjtc 	else
154ebb769aeSdholland 		snprintf(Databuf, SZ_DATABUF,
155ebb769aeSdholland 			" is in %s  (%.0f,%.0f)",
156ebb769aeSdholland 			label, playerp->p_x, playerp->p_y);
15786a62b8dSjtc 
15886a62b8dSjtc 	return (Databuf);
15986a62b8dSjtc }
16086a62b8dSjtc 
161a232aee2Slukem void
tradingpost(void)162c7a109ccSdholland tradingpost(void)
16386a62b8dSjtc {
16486a62b8dSjtc 	double  numitems;	/* number of items to purchase */
16586a62b8dSjtc 	double  cost;		/* cost of purchase */
16686a62b8dSjtc 	double  blessingcost;	/* cost of blessing */
16786a62b8dSjtc 	int     ch;		/* input */
168a232aee2Slukem 	int     size;		/* size of the trading post */
169a232aee2Slukem 	int     loop;		/* loop counter */
17086a62b8dSjtc 	int     cheat = 0;	/* number of times player has tried to cheat */
17186a62b8dSjtc 	bool    dishonest = FALSE;	/* set when merchant is dishonest */
17286a62b8dSjtc 
17386a62b8dSjtc 	Player.p_status = S_TRADING;
17486a62b8dSjtc 	writerecord(&Player, Fileloc);
17586a62b8dSjtc 
17686a62b8dSjtc 	clear();
17786a62b8dSjtc 	addstr("You are at a trading post. All purchases must be made with gold.");
17886a62b8dSjtc 
17986a62b8dSjtc 	size = sqrt(fabs(Player.p_x / 100)) + 1;
18086a62b8dSjtc 	size = MIN(7, size);
18186a62b8dSjtc 
18286a62b8dSjtc 	/* set up cost of blessing */
18386a62b8dSjtc 	blessingcost = 1000.0 * (Player.p_level + 5.0);
18486a62b8dSjtc 
18586a62b8dSjtc 	/* print Menu */
18686a62b8dSjtc 	move(7, 0);
18786a62b8dSjtc 	for (loop = 0; loop < size; ++loop)
18886a62b8dSjtc 		/* print Menu */
18986a62b8dSjtc 	{
19086a62b8dSjtc 		if (loop == 6)
19186a62b8dSjtc 			cost = blessingcost;
19286a62b8dSjtc 		else
19386a62b8dSjtc 			cost = Menu[loop].cost;
19486a62b8dSjtc 		printw("(%d) %-12s: %6.0f\n", loop + 1, Menu[loop].item, cost);
19586a62b8dSjtc 	}
19686a62b8dSjtc 
19786a62b8dSjtc 	mvprintw(5, 0, "L:Leave  P:Purchase  S:Sell Gems ? ");
19886a62b8dSjtc 
199a232aee2Slukem 	for (;;) {
20086a62b8dSjtc 		adjuststats();	/* truncate any bad values */
20186a62b8dSjtc 
20286a62b8dSjtc 		/* print some important statistics */
20386a62b8dSjtc 		mvprintw(1, 0, "Gold:   %9.0f  Gems:  %9.0f  Level:   %6.0f  Charms: %6d\n",
20486a62b8dSjtc 		    Player.p_gold, Player.p_gems, Player.p_level, Player.p_charms);
20586a62b8dSjtc 		printw("Shield: %9.0f  Sword: %9.0f  Quicksilver:%3.0f  Blessed: %s\n",
20686a62b8dSjtc 		    Player.p_shield, Player.p_sword, Player.p_quksilver,
20786a62b8dSjtc 		    (Player.p_blessing ? " True" : "False"));
20886a62b8dSjtc 		printw("Brains: %9.0f  Mana:  %9.0f", Player.p_brains, Player.p_mana);
20986a62b8dSjtc 
21086a62b8dSjtc 		move(5, 36);
21186a62b8dSjtc 		ch = getanswer("LPS", FALSE);
21286a62b8dSjtc 		move(15, 0);
21386a62b8dSjtc 		clrtobot();
214a232aee2Slukem 		switch (ch) {
21586a62b8dSjtc 		case 'L':	/* leave */
21686a62b8dSjtc 		case '\n':
21786a62b8dSjtc 			altercoordinates(0.0, 0.0, A_NEAR);
21886a62b8dSjtc 			return;
21986a62b8dSjtc 
22086a62b8dSjtc 		case 'P':	/* make purchase */
22186a62b8dSjtc 			mvaddstr(15, 0, "What what would you like to buy ? ");
22286a62b8dSjtc 			ch = getanswer(" 1234567", FALSE);
22386a62b8dSjtc 			move(15, 0);
22486a62b8dSjtc 			clrtoeol();
22586a62b8dSjtc 
22686a62b8dSjtc 			if (ch - '0' > size)
22786a62b8dSjtc 				addstr("Sorry, this merchant doesn't have that.");
22886a62b8dSjtc 			else
229a232aee2Slukem 				switch (ch) {
23086a62b8dSjtc 				case '1':
23186a62b8dSjtc 					printw("Mana is one per %.0f gold piece.  How many do you want (%.0f max) ? ",
23286a62b8dSjtc 					    Menu[0].cost, floor(Player.p_gold / Menu[0].cost));
23386a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[0].cost;
23486a62b8dSjtc 
23586a62b8dSjtc 					if (cost > Player.p_gold || numitems < 0)
23686a62b8dSjtc 						++cheat;
237a232aee2Slukem 					else {
23886a62b8dSjtc 						cheat = 0;
23986a62b8dSjtc 						Player.p_gold -= cost;
24086a62b8dSjtc 						if (drandom() < 0.02)
24186a62b8dSjtc 							dishonest = TRUE;
24286a62b8dSjtc 						else
24386a62b8dSjtc 							Player.p_mana += numitems;
24486a62b8dSjtc 					}
24586a62b8dSjtc 					break;
24686a62b8dSjtc 
24786a62b8dSjtc 				case '2':
24886a62b8dSjtc 					printw("Shields are %.0f per +1.  How many do you want (%.0f max) ? ",
24986a62b8dSjtc 					    Menu[1].cost, floor(Player.p_gold / Menu[1].cost));
25086a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[1].cost;
25186a62b8dSjtc 
25286a62b8dSjtc 					if (numitems == 0.0)
25386a62b8dSjtc 						break;
25486a62b8dSjtc 					else
255a232aee2Slukem 						if (cost > Player.p_gold || numitems < 0)
256a232aee2Slukem 							++cheat;
257a232aee2Slukem 						else
258a232aee2Slukem 							if (numitems < Player.p_shield)
259a232aee2Slukem 								NOBETTER();
260a232aee2Slukem 							else {
26186a62b8dSjtc 								cheat = 0;
26286a62b8dSjtc 								Player.p_gold -= cost;
26386a62b8dSjtc 								if (drandom() < 0.02)
26486a62b8dSjtc 									dishonest = TRUE;
26586a62b8dSjtc 								else
26686a62b8dSjtc 									Player.p_shield = numitems;
26786a62b8dSjtc 							}
26886a62b8dSjtc 					break;
26986a62b8dSjtc 
27086a62b8dSjtc 				case '3':
27186a62b8dSjtc 					printw("A book costs %.0f gp.  How many do you want (%.0f max) ? ",
27286a62b8dSjtc 					    Menu[2].cost, floor(Player.p_gold / Menu[2].cost));
27386a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[2].cost;
27486a62b8dSjtc 
27586a62b8dSjtc 					if (cost > Player.p_gold || numitems < 0)
27686a62b8dSjtc 						++cheat;
277a232aee2Slukem 					else {
27886a62b8dSjtc 						cheat = 0;
27986a62b8dSjtc 						Player.p_gold -= cost;
28086a62b8dSjtc 						if (drandom() < 0.02)
28186a62b8dSjtc 							dishonest = TRUE;
282a232aee2Slukem 						else
283a232aee2Slukem 							if (drandom() * numitems > Player.p_level / 10.0
284a232aee2Slukem 							    && numitems != 1) {
28586a62b8dSjtc 								printw("\nYou blew your mind!\n");
28686a62b8dSjtc 								Player.p_brains /= 5;
287a232aee2Slukem 							} else {
28886a62b8dSjtc 								Player.p_brains += floor(numitems) * ROLL(20, 8);
28986a62b8dSjtc 							}
29086a62b8dSjtc 					}
29186a62b8dSjtc 					break;
29286a62b8dSjtc 
29386a62b8dSjtc 				case '4':
29486a62b8dSjtc 					printw("Swords are %.0f gp per +1.  How many + do you want (%.0f max) ? ",
29586a62b8dSjtc 					    Menu[3].cost, floor(Player.p_gold / Menu[3].cost));
29686a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[3].cost;
29786a62b8dSjtc 
29886a62b8dSjtc 					if (numitems == 0.0)
29986a62b8dSjtc 						break;
30086a62b8dSjtc 					else
301a232aee2Slukem 						if (cost > Player.p_gold || numitems < 0)
302a232aee2Slukem 							++cheat;
303a232aee2Slukem 						else
304a232aee2Slukem 							if (numitems < Player.p_sword)
305a232aee2Slukem 								NOBETTER();
306a232aee2Slukem 							else {
30786a62b8dSjtc 								cheat = 0;
30886a62b8dSjtc 								Player.p_gold -= cost;
30986a62b8dSjtc 								if (drandom() < 0.02)
31086a62b8dSjtc 									dishonest = TRUE;
31186a62b8dSjtc 								else
31286a62b8dSjtc 									Player.p_sword = numitems;
31386a62b8dSjtc 							}
31486a62b8dSjtc 					break;
31586a62b8dSjtc 
31686a62b8dSjtc 				case '5':
31786a62b8dSjtc 					printw("A charm costs %.0f gp.  How many do you want (%.0f max) ? ",
31886a62b8dSjtc 					    Menu[4].cost, floor(Player.p_gold / Menu[4].cost));
31986a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[4].cost;
32086a62b8dSjtc 
32186a62b8dSjtc 					if (cost > Player.p_gold || numitems < 0)
32286a62b8dSjtc 						++cheat;
323a232aee2Slukem 					else {
32486a62b8dSjtc 						cheat = 0;
32586a62b8dSjtc 						Player.p_gold -= cost;
32686a62b8dSjtc 						if (drandom() < 0.02)
32786a62b8dSjtc 							dishonest = TRUE;
32886a62b8dSjtc 						else
32986a62b8dSjtc 							Player.p_charms += numitems;
33086a62b8dSjtc 					}
33186a62b8dSjtc 					break;
33286a62b8dSjtc 
33386a62b8dSjtc 				case '6':
33486a62b8dSjtc 					printw("Quicksilver is %.0f gp per +1.  How many + do you want (%.0f max) ? ",
33586a62b8dSjtc 					    Menu[5].cost, floor(Player.p_gold / Menu[5].cost));
33686a62b8dSjtc 					cost = (numitems = floor(infloat())) * Menu[5].cost;
33786a62b8dSjtc 
33886a62b8dSjtc 					if (numitems == 0.0)
33986a62b8dSjtc 						break;
34086a62b8dSjtc 					else
341a232aee2Slukem 						if (cost > Player.p_gold || numitems < 0)
342a232aee2Slukem 							++cheat;
343a232aee2Slukem 						else
344a232aee2Slukem 							if (numitems < Player.p_quksilver)
345a232aee2Slukem 								NOBETTER();
346a232aee2Slukem 							else {
34786a62b8dSjtc 								cheat = 0;
34886a62b8dSjtc 								Player.p_gold -= cost;
34986a62b8dSjtc 								if (drandom() < 0.02)
35086a62b8dSjtc 									dishonest = TRUE;
35186a62b8dSjtc 								else
35286a62b8dSjtc 									Player.p_quksilver = numitems;
35386a62b8dSjtc 							}
35486a62b8dSjtc 					break;
35586a62b8dSjtc 
35686a62b8dSjtc 				case '7':
357a232aee2Slukem 					if (Player.p_blessing) {
35886a62b8dSjtc 						addstr("You already have a blessing.");
35986a62b8dSjtc 						break;
36086a62b8dSjtc 					}
36186a62b8dSjtc 					printw("A blessing requires a %.0f gp donation.  Still want one ? ", blessingcost);
36286a62b8dSjtc 					ch = getanswer("NY", FALSE);
36386a62b8dSjtc 
36404afeca6Sveego 					if (ch == 'Y') {
36586a62b8dSjtc 						if (Player.p_gold < blessingcost)
36686a62b8dSjtc 							++cheat;
367a232aee2Slukem 						else {
36886a62b8dSjtc 							cheat = 0;
36986a62b8dSjtc 							Player.p_gold -= blessingcost;
37086a62b8dSjtc 							if (drandom() < 0.02)
37186a62b8dSjtc 								dishonest = TRUE;
37286a62b8dSjtc 							else
37386a62b8dSjtc 								Player.p_blessing = TRUE;
37486a62b8dSjtc 						}
37504afeca6Sveego 					}
37686a62b8dSjtc 					break;
37786a62b8dSjtc 				}
37886a62b8dSjtc 			break;
37986a62b8dSjtc 
38086a62b8dSjtc 		case 'S':	/* sell gems */
38186a62b8dSjtc 			mvprintw(15, 0, "A gem is worth %.0f gp.  How many do you want to sell (%.0f max) ? ",
38286a62b8dSjtc 			    (double) N_GEMVALUE, Player.p_gems);
38386a62b8dSjtc 			numitems = floor(infloat());
38486a62b8dSjtc 
38586a62b8dSjtc 			if (numitems > Player.p_gems || numitems < 0)
38686a62b8dSjtc 				++cheat;
387a232aee2Slukem 			else {
38886a62b8dSjtc 				cheat = 0;
38986a62b8dSjtc 				Player.p_gems -= numitems;
39086a62b8dSjtc 				Player.p_gold += numitems * N_GEMVALUE;
39186a62b8dSjtc 			}
39286a62b8dSjtc 		}
39386a62b8dSjtc 
39486a62b8dSjtc 		if (cheat == 1)
39586a62b8dSjtc 			mvaddstr(17, 0, "Come on, merchants aren't stupid.  Stop cheating.\n");
396a232aee2Slukem 		else
397a232aee2Slukem 			if (cheat == 2) {
39886a62b8dSjtc 				mvaddstr(17, 0, "You had your chance.  This merchant happens to be\n");
39986a62b8dSjtc 				printw("a %.0f level magic user, and you made %s mad!\n",
40086a62b8dSjtc 				    ROLL(Circle * 20.0, 40.0), (drandom() < 0.5) ? "him" : "her");
40186a62b8dSjtc 				altercoordinates(0.0, 0.0, A_FAR);
40286a62b8dSjtc 				Player.p_energy /= 2.0;
40386a62b8dSjtc 				++Player.p_sin;
40486a62b8dSjtc 				more(23);
40586a62b8dSjtc 				return;
406a232aee2Slukem 			} else
407a232aee2Slukem 				if (dishonest) {
40886a62b8dSjtc 					mvaddstr(17, 0, "The merchant stole your money!");
40986a62b8dSjtc 					refresh();
41086a62b8dSjtc 					altercoordinates(Player.p_x - Player.p_x / 10.0,
41186a62b8dSjtc 					    Player.p_y - Player.p_y / 10.0, A_SPECIFIC);
41286a62b8dSjtc 					sleep(2);
41386a62b8dSjtc 					return;
41486a62b8dSjtc 				}
41586a62b8dSjtc 	}
41686a62b8dSjtc }
41786a62b8dSjtc 
418a232aee2Slukem void
displaystats(void)419c7a109ccSdholland displaystats(void)
42086a62b8dSjtc {
42186a62b8dSjtc 	mvprintw(0, 0, "%s%s\n", Player.p_name, descrlocation(&Player, FALSE));
42286a62b8dSjtc 	mvprintw(1, 0, "Level :%7.0f   Energy  :%9.0f(%9.0f)  Mana :%9.0f  Users:%3d\n",
42386a62b8dSjtc 	    Player.p_level, Player.p_energy, Player.p_maxenergy + Player.p_shield,
42486a62b8dSjtc 	    Player.p_mana, Users);
42586a62b8dSjtc 	mvprintw(2, 0, "Quick :%3.0f(%3.0f)  Strength:%9.0f(%9.0f)  Gold :%9.0f  %s\n",
42686a62b8dSjtc 	    Player.p_speed, Player.p_quickness + Player.p_quksilver, Player.p_might,
42786a62b8dSjtc 	    Player.p_strength + Player.p_sword, Player.p_gold, descrstatus(&Player));
42886a62b8dSjtc }
42986a62b8dSjtc 
430a232aee2Slukem void
allstatslist(void)431c7a109ccSdholland allstatslist(void)
43286a62b8dSjtc {
433092d3130Sjsm 	static const char *const flags[] = /* to print value of some bools */
43486a62b8dSjtc 	{
43586a62b8dSjtc 		"False",
43686a62b8dSjtc 		" True"
43786a62b8dSjtc 	};
43886a62b8dSjtc 
43986a62b8dSjtc 	mvprintw(8, 0, "Type: %s\n", descrtype(&Player, FALSE));
44086a62b8dSjtc 
44186a62b8dSjtc 	mvprintw(10, 0, "Experience: %9.0f", Player.p_experience);
44286a62b8dSjtc 	mvprintw(11, 0, "Brains    : %9.0f", Player.p_brains);
44386a62b8dSjtc 	mvprintw(12, 0, "Magic Lvl : %9.0f", Player.p_magiclvl);
44486a62b8dSjtc 	mvprintw(13, 0, "Sin       : %9.5f", Player.p_sin);
44586a62b8dSjtc 	mvprintw(14, 0, "Poison    : %9.5f", Player.p_poison);
44686a62b8dSjtc 	mvprintw(15, 0, "Gems      : %9.0f", Player.p_gems);
447d6d7f053Sjdc 	mvprintw(16, 0, "Age       : %9ld", Player.p_age);
44886a62b8dSjtc 	mvprintw(10, 40, "Holy Water: %9d", Player.p_holywater);
44986a62b8dSjtc 	mvprintw(11, 40, "Amulets   : %9d", Player.p_amulets);
45086a62b8dSjtc 	mvprintw(12, 40, "Charms    : %9d", Player.p_charms);
45186a62b8dSjtc 	mvprintw(13, 40, "Crowns    : %9d", Player.p_crowns);
45286a62b8dSjtc 	mvprintw(14, 40, "Shield    : %9.0f", Player.p_shield);
45386a62b8dSjtc 	mvprintw(15, 40, "Sword     : %9.0f", Player.p_sword);
45486a62b8dSjtc 	mvprintw(16, 40, "Quickslver: %9.0f", Player.p_quksilver);
45586a62b8dSjtc 
45686a62b8dSjtc 	mvprintw(18, 0, "Blessing: %s   Ring: %s   Virgin: %s   Palantir: %s",
457a232aee2Slukem 	    flags[(int)Player.p_blessing],
458a232aee2Slukem 	    flags[Player.p_ring.ring_type != R_NONE],
459a232aee2Slukem 	    flags[(int)Player.p_virgin],
460a232aee2Slukem 	    flags[(int)Player.p_palantir]);
46186a62b8dSjtc }
46286a62b8dSjtc 
463092d3130Sjsm const char   *
descrtype(struct player * playerp,phbool shortflag)464c7a109ccSdholland descrtype(struct player *playerp, phbool shortflag)
46586a62b8dSjtc {
466*9f4a9600Sandvar 	int     type;		/* for calculating result subscript */
467092d3130Sjsm 	static const char *const results[] =/* description table */
46886a62b8dSjtc 	{
46986a62b8dSjtc 		" Magic User", " MU",
47086a62b8dSjtc 		" Fighter", " F ",
47186a62b8dSjtc 		" Elf", " E ",
47286a62b8dSjtc 		" Dwarf", " D ",
47386a62b8dSjtc 		" Halfling", " H ",
47486a62b8dSjtc 		" Experimento", " EX",
47586a62b8dSjtc 		" Super", " S ",
47686a62b8dSjtc 		" King", " K ",
47786a62b8dSjtc 		" Council of Wise", " CW",
47886a62b8dSjtc 		" Ex-Valar", " EV",
47986a62b8dSjtc 		" Valar", " V ",
48086a62b8dSjtc 		" ? ", " ? "
48186a62b8dSjtc 	};
48286a62b8dSjtc 
48386a62b8dSjtc 	type = playerp->p_type;
48486a62b8dSjtc 
485a232aee2Slukem 	switch (playerp->p_specialtype) {
48686a62b8dSjtc 	case SC_NONE:
48786a62b8dSjtc 		type = playerp->p_type;
48886a62b8dSjtc 		break;
48986a62b8dSjtc 
49086a62b8dSjtc 	case SC_KING:
49186a62b8dSjtc 		type = 7;
49286a62b8dSjtc 		break;
49386a62b8dSjtc 
49486a62b8dSjtc 	case SC_COUNCIL:
49586a62b8dSjtc 		type = 8;
49686a62b8dSjtc 		break;
49786a62b8dSjtc 
49886a62b8dSjtc 	case SC_EXVALAR:
49986a62b8dSjtc 		type = 9;
50086a62b8dSjtc 		break;
50186a62b8dSjtc 
50286a62b8dSjtc 	case SC_VALAR:
50386a62b8dSjtc 		type = 10;
50486a62b8dSjtc 		break;
50586a62b8dSjtc 	}
50686a62b8dSjtc 
50786a62b8dSjtc 	type *= 2;		/* calculate offset */
50886a62b8dSjtc 
50986a62b8dSjtc 	if (type > 20)
51086a62b8dSjtc 		/* error */
51186a62b8dSjtc 		type = 22;
51286a62b8dSjtc 
51386a62b8dSjtc 	if (shortflag)
51486a62b8dSjtc 		/* use short descriptions */
51586a62b8dSjtc 		++type;
51686a62b8dSjtc 
517a232aee2Slukem 	if (playerp->p_crowns > 0) {
51886a62b8dSjtc 		strcpy(Databuf, results[type]);
51986a62b8dSjtc 		Databuf[0] = '*';
52086a62b8dSjtc 		return (Databuf);
521a232aee2Slukem 	} else
52286a62b8dSjtc 		return (results[type]);
52386a62b8dSjtc }
52486a62b8dSjtc 
52586a62b8dSjtc long
findname(const char * name,struct player * playerp)526c7a109ccSdholland findname(const char *name, struct player *playerp)
52786a62b8dSjtc {
52886a62b8dSjtc 	long    loc = 0;	/* location in the file */
52986a62b8dSjtc 
5305fb18dd9Sjsm 	fseek(Playersfp, 0L, SEEK_SET);
531a232aee2Slukem 	while (fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp) == 1) {
532a232aee2Slukem 		if (strcmp(playerp->p_name, name) == 0) {
53386a62b8dSjtc 			if (playerp->p_status != S_NOTUSED || Wizard)
53486a62b8dSjtc 				/* found it */
53586a62b8dSjtc 				return (loc);
53686a62b8dSjtc 		}
53786a62b8dSjtc 		loc += SZ_PLAYERSTRUCT;
53886a62b8dSjtc 	}
53986a62b8dSjtc 
54086a62b8dSjtc 	return (-1);
54186a62b8dSjtc }
54286a62b8dSjtc 
54386a62b8dSjtc long
allocrecord(void)544c7a109ccSdholland allocrecord(void)
54586a62b8dSjtc {
54686a62b8dSjtc 	long    loc = 0L;	/* location in file */
54786a62b8dSjtc 
5485fb18dd9Sjsm 	fseek(Playersfp, 0L, SEEK_SET);
549a232aee2Slukem 	while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1) {
55086a62b8dSjtc 		if (Other.p_status == S_NOTUSED)
55186a62b8dSjtc 			/* found an empty record */
55286a62b8dSjtc 			return (loc);
55386a62b8dSjtc 		else
55486a62b8dSjtc 			loc += SZ_PLAYERSTRUCT;
55586a62b8dSjtc 	}
55686a62b8dSjtc 
55786a62b8dSjtc 	/* make a new record */
55886a62b8dSjtc 	initplayer(&Other);
55986a62b8dSjtc 	Player.p_status = S_OFF;
56086a62b8dSjtc 	writerecord(&Other, loc);
56186a62b8dSjtc 
56286a62b8dSjtc 	return (loc);
56386a62b8dSjtc }
56486a62b8dSjtc 
565a232aee2Slukem void
freerecord(struct player * playerp,long loc)566c7a109ccSdholland freerecord(struct player *playerp, long loc)
56786a62b8dSjtc {
56886a62b8dSjtc 	playerp->p_name[0] = CH_MARKDELETE;
56986a62b8dSjtc 	playerp->p_status = S_NOTUSED;
57086a62b8dSjtc 	writerecord(playerp, loc);
57186a62b8dSjtc }
57286a62b8dSjtc 
573a232aee2Slukem void
leavegame(void)574c7a109ccSdholland leavegame(void)
57586a62b8dSjtc {
57686a62b8dSjtc 
57786a62b8dSjtc 	if (Player.p_level < 1.0)
57886a62b8dSjtc 		/* delete character */
57986a62b8dSjtc 		freerecord(&Player, Fileloc);
580a232aee2Slukem 	else {
58186a62b8dSjtc 		Player.p_status = S_OFF;
58286a62b8dSjtc 		writerecord(&Player, Fileloc);
58386a62b8dSjtc 	}
58486a62b8dSjtc 
58586a62b8dSjtc 	cleanup(TRUE);
58686a62b8dSjtc 	/* NOTREACHED */
58786a62b8dSjtc }
58886a62b8dSjtc 
589a232aee2Slukem void
death(const char * how)590c7a109ccSdholland death(const char *how)
59186a62b8dSjtc {
59286a62b8dSjtc 	FILE   *fp;		/* for updating various files */
59386a62b8dSjtc 	int     ch;		/* input */
594092d3130Sjsm 	static const char *const deathmesg[] =
59586a62b8dSjtc 	/* add more messages here, if desired */
59686a62b8dSjtc 	{
59786a62b8dSjtc 		"You have been wounded beyond repair.  ",
59886a62b8dSjtc 		"You have been disemboweled.  ",
59986a62b8dSjtc 		"You've been mashed, mauled, and spit upon.  (You're dead.)\n",
60086a62b8dSjtc 		"You died!  ",
60186a62b8dSjtc 		"You're a complete failure -- you've died!!\n",
60286a62b8dSjtc 		"You have been dealt a fatal blow!  "
60386a62b8dSjtc 	};
60486a62b8dSjtc 
60586a62b8dSjtc 	clear();
60686a62b8dSjtc 
607a232aee2Slukem 	if (strcmp(how, "Stupidity") != 0) {
60886a62b8dSjtc 		if (Player.p_level > 9999.0)
60986a62b8dSjtc 			/* old age */
61086a62b8dSjtc 			addstr("Characters must be retired upon reaching level 10000.  Sorry.");
611a232aee2Slukem 		else
612a232aee2Slukem 			if (Player.p_lives > 0)
61386a62b8dSjtc 				/* extra lives */
61486a62b8dSjtc 			{
61586a62b8dSjtc 				addstr("You should be more cautious.  You've been killed.\n");
61686a62b8dSjtc 				printw("You only have %d more chance(s).\n", --Player.p_lives);
61786a62b8dSjtc 				more(3);
61886a62b8dSjtc 				Player.p_energy = Player.p_maxenergy;
61986a62b8dSjtc 				return;
620a232aee2Slukem 			} else
621a232aee2Slukem 				if (Player.p_specialtype == SC_VALAR) {
62286a62b8dSjtc 					addstr("You had your chances, but Valar aren't totally\n");
62386a62b8dSjtc 					addstr("immortal.  You are now left to wither and die . . .\n");
62486a62b8dSjtc 					more(3);
62586a62b8dSjtc 					Player.p_brains = Player.p_level / 25.0;
62686a62b8dSjtc 					Player.p_energy = Player.p_maxenergy /= 5.0;
62786a62b8dSjtc 					Player.p_quksilver = Player.p_sword = 0.0;
62886a62b8dSjtc 					Player.p_specialtype = SC_COUNCIL;
62986a62b8dSjtc 					return;
630a232aee2Slukem 				} else
631a232aee2Slukem 					if (Player.p_ring.ring_inuse &&
63286a62b8dSjtc 					    (Player.p_ring.ring_type == R_DLREG || Player.p_ring.ring_type == R_NAZREG))
633a232aee2Slukem 						/* good ring in use - saved
634a232aee2Slukem 						 * from death */
63586a62b8dSjtc 					{
63686a62b8dSjtc 						mvaddstr(4, 0, "Your ring saved you from death!\n");
63786a62b8dSjtc 						refresh();
63886a62b8dSjtc 						Player.p_ring.ring_type = R_NONE;
63986a62b8dSjtc 						Player.p_energy = Player.p_maxenergy / 12.0 + 1.0;
64086a62b8dSjtc 						if (Player.p_crowns > 0)
64186a62b8dSjtc 							--Player.p_crowns;
64286a62b8dSjtc 						return;
643a232aee2Slukem 					} else
644a232aee2Slukem 						if (Player.p_ring.ring_type == R_BAD
64586a62b8dSjtc 						    || Player.p_ring.ring_type == R_SPOILED)
646a232aee2Slukem 							/* bad ring in
647a232aee2Slukem 							 * possession; name
648a232aee2Slukem 							 * idiot after player */
64986a62b8dSjtc 						{
65086a62b8dSjtc 							mvaddstr(4, 0,
65186a62b8dSjtc 							    "Your ring has taken control of you and turned you into a monster!\n");
6525fb18dd9Sjsm 							fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, SEEK_SET);
65386a62b8dSjtc 							fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
65486a62b8dSjtc 							strcpy(Curmonster.m_name, Player.p_name);
6555fb18dd9Sjsm 							fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, SEEK_SET);
65686a62b8dSjtc 							fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
65786a62b8dSjtc 							fflush(Monstfp);
65886a62b8dSjtc 						}
65986a62b8dSjtc 	}
66086a62b8dSjtc 	enterscore();		/* update score board */
66186a62b8dSjtc 
66286a62b8dSjtc 	/* put info in last dead file */
66386a62b8dSjtc 	fp = fopen(_PATH_LASTDEAD, "w");
66486a62b8dSjtc 	fprintf(fp, "%s (%s, run by %s, level %.0f, killed by %s)",
66586a62b8dSjtc 	    Player.p_name, descrtype(&Player, TRUE),
66686a62b8dSjtc 	    Player.p_login, Player.p_level, how);
66786a62b8dSjtc 	fclose(fp);
66886a62b8dSjtc 
66986a62b8dSjtc 	/* let other players know */
67086a62b8dSjtc 	fp = fopen(_PATH_MESS, "w");
67186a62b8dSjtc 	fprintf(fp, "%s was killed by %s.", Player.p_name, how);
67286a62b8dSjtc 	fclose(fp);
67386a62b8dSjtc 
67486a62b8dSjtc 	freerecord(&Player, Fileloc);
67586a62b8dSjtc 
67686a62b8dSjtc 	clear();
67786a62b8dSjtc 	move(10, 0);
67886a62b8dSjtc 	addstr(deathmesg[(int) ROLL(0.0, (double) sizeof(deathmesg) / sizeof(char *))]);
67986a62b8dSjtc 	addstr("Care to give it another try ? ");
68086a62b8dSjtc 	ch = getanswer("NY", FALSE);
68186a62b8dSjtc 
682a232aee2Slukem 	if (ch == 'Y') {
68386a62b8dSjtc 		cleanup(FALSE);
68486a62b8dSjtc 		execl(_PATH_GAMEPROG, "phantasia", "-s",
685acae6852Splunky 		    (Wizard ? "-S" : (char *) NULL), (char *) NULL);
68686a62b8dSjtc 		exit(0);
68786a62b8dSjtc 		/* NOTREACHED */
68886a62b8dSjtc 	}
68986a62b8dSjtc 	cleanup(TRUE);
69086a62b8dSjtc 	/* NOTREACHED */
69186a62b8dSjtc }
69286a62b8dSjtc 
693a232aee2Slukem void
writerecord(struct player * playerp,long place)694c7a109ccSdholland writerecord(struct player *playerp, long place)
69586a62b8dSjtc {
6965fb18dd9Sjsm 	fseek(Playersfp, place, SEEK_SET);
69786a62b8dSjtc 	fwrite((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
69886a62b8dSjtc 	fflush(Playersfp);
69986a62b8dSjtc }
70086a62b8dSjtc 
7015305281bSdholland static double
explevel(double experience)702c7a109ccSdholland explevel(double experience)
70386a62b8dSjtc {
70486a62b8dSjtc 	if (experience < 1.1e7)
70586a62b8dSjtc 		return (floor(pow((experience / 1000.0), 0.4875)));
70686a62b8dSjtc 	else
70786a62b8dSjtc 		return (floor(pow((experience / 1250.0), 0.4865)));
70886a62b8dSjtc }
70986a62b8dSjtc 
710a232aee2Slukem void
truncstring(char * string)711c7a109ccSdholland truncstring(char *string)
71286a62b8dSjtc {
713a232aee2Slukem 	int     length;		/* length of string */
71486a62b8dSjtc 
71586a62b8dSjtc 	length = strlen(string);
71686a62b8dSjtc 	while (string[--length] == ' ')
71786a62b8dSjtc 		string[length] = '\0';
71886a62b8dSjtc }
71986a62b8dSjtc 
720a232aee2Slukem void
altercoordinates(double xnew,double ynew,int operation)721c7a109ccSdholland altercoordinates(double xnew, double ynew, int operation)
72286a62b8dSjtc {
723a232aee2Slukem 	switch (operation) {
72486a62b8dSjtc 	case A_FORCED:		/* move with no checks */
72586a62b8dSjtc 		break;
72686a62b8dSjtc 
72786a62b8dSjtc 	case A_NEAR:		/* pick random coordinates near */
72886a62b8dSjtc 		xnew = Player.p_x + ROLL(1.0, 5.0);
72986a62b8dSjtc 		ynew = Player.p_y - ROLL(1.0, 5.0);
73086a62b8dSjtc 		/* fall through for check */
73186a62b8dSjtc 
732fbffadb9Smrg 		/* FALLTHROUGH */
73386a62b8dSjtc 	case A_SPECIFIC:	/* just move player */
73486a62b8dSjtc 		if (Beyond && fabs(xnew) < D_BEYOND && fabs(ynew) < D_BEYOND)
73586a62b8dSjtc 			/*
73686a62b8dSjtc 			 * cannot move back from point of no return
73786a62b8dSjtc 			 * pick the largest coordinate to remain unchanged
73886a62b8dSjtc 			 */
73986a62b8dSjtc 		{
74086a62b8dSjtc 			if (fabs(xnew) > fabs(ynew))
74186a62b8dSjtc 				xnew = SGN(Player.p_x) * MAX(fabs(Player.p_x), D_BEYOND);
74286a62b8dSjtc 			else
74386a62b8dSjtc 				ynew = SGN(Player.p_y) * MAX(fabs(Player.p_y), D_BEYOND);
74486a62b8dSjtc 		}
74586a62b8dSjtc 		break;
74686a62b8dSjtc 
74786a62b8dSjtc 	case A_FAR:		/* pick random coordinates far */
74886a62b8dSjtc 		xnew = Player.p_x + SGN(Player.p_x) * ROLL(50 * Circle, 250 * Circle);
74986a62b8dSjtc 		ynew = Player.p_y + SGN(Player.p_y) * ROLL(50 * Circle, 250 * Circle);
75086a62b8dSjtc 		break;
75186a62b8dSjtc 	}
75286a62b8dSjtc 
75386a62b8dSjtc 	/* now set location flags and adjust coordinates */
75486a62b8dSjtc 	Circle = CIRCLE(Player.p_x = floor(xnew), Player.p_y = floor(ynew));
75586a62b8dSjtc 
75686a62b8dSjtc 	/* set up flags based upon location */
75786a62b8dSjtc 	Throne = Marsh = Beyond = FALSE;
75886a62b8dSjtc 
75986a62b8dSjtc 	if (Player.p_x == 0.0 && Player.p_y == 0.0)
76086a62b8dSjtc 		Throne = TRUE;
761a232aee2Slukem 	else
762a232aee2Slukem 		if (Circle < 35 && Circle >= 20)
76386a62b8dSjtc 			Marsh = TRUE;
764a232aee2Slukem 		else
765a232aee2Slukem 			if (MAX(fabs(Player.p_x), fabs(Player.p_y)) >= D_BEYOND)
76686a62b8dSjtc 				Beyond = TRUE;
76786a62b8dSjtc 
76886a62b8dSjtc 	Changed = TRUE;
76986a62b8dSjtc }
77086a62b8dSjtc 
771a232aee2Slukem void
readrecord(struct player * playerp,long loc)772c7a109ccSdholland readrecord(struct player *playerp, long loc)
77386a62b8dSjtc {
7745fb18dd9Sjsm 	fseek(Playersfp, loc, SEEK_SET);
77586a62b8dSjtc 	fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
77686a62b8dSjtc }
77786a62b8dSjtc 
778a232aee2Slukem void
adjuststats(void)779c7a109ccSdholland adjuststats(void)
78086a62b8dSjtc {
78186a62b8dSjtc 	double  dtemp;		/* for temporary calculations */
78286a62b8dSjtc 
78386a62b8dSjtc 	if (explevel(Player.p_experience) > Player.p_level)
78486a62b8dSjtc 		/* move one or more levels */
78586a62b8dSjtc 	{
78686a62b8dSjtc 		movelevel();
78786a62b8dSjtc 		if (Player.p_level > 5.0)
78886a62b8dSjtc 			Timeout = TRUE;
78986a62b8dSjtc 	}
79086a62b8dSjtc 	if (Player.p_specialtype == SC_VALAR)
79186a62b8dSjtc 		/* valar */
79286a62b8dSjtc 		Circle = Player.p_level / 5.0;
79386a62b8dSjtc 
79486a62b8dSjtc 	/* calculate effective quickness */
79586a62b8dSjtc 	dtemp = ((Player.p_gold + Player.p_gems / 2.0) - 1000.0) / Statptr->c_goldtote
796276fd166Ssimonb 	    - Player.p_level;
79786a62b8dSjtc 	dtemp = MAX(0.0, dtemp);/* gold slows player down */
79886a62b8dSjtc 	Player.p_speed = Player.p_quickness + Player.p_quksilver - dtemp;
79986a62b8dSjtc 
80086a62b8dSjtc 	/* calculate effective strength */
80186a62b8dSjtc 	if (Player.p_poison > 0.0)
80286a62b8dSjtc 		/* poison makes player weaker */
80386a62b8dSjtc 	{
80486a62b8dSjtc 		dtemp = 1.0 - Player.p_poison * Statptr->c_weakness / 800.0;
80586a62b8dSjtc 		dtemp = MAX(0.1, dtemp);
806a232aee2Slukem 	} else
80786a62b8dSjtc 		dtemp = 1.0;
80886a62b8dSjtc 	Player.p_might = dtemp * Player.p_strength + Player.p_sword;
80986a62b8dSjtc 
81086a62b8dSjtc 	/* insure that important things are within limits */
81186a62b8dSjtc 	Player.p_quksilver = MIN(99.0, Player.p_quksilver);
81286a62b8dSjtc 	Player.p_mana = MIN(Player.p_mana,
81386a62b8dSjtc 	    Player.p_level * Statptr->c_maxmana + 1000.0);
81486a62b8dSjtc 	Player.p_brains = MIN(Player.p_brains,
81586a62b8dSjtc 	    Player.p_level * Statptr->c_maxbrains + 200.0);
81686a62b8dSjtc 	Player.p_charms = MIN(Player.p_charms, Player.p_level + 10.0);
81786a62b8dSjtc 
81886a62b8dSjtc 	/*
81986a62b8dSjtc          * some implementations have problems with floating point compare
82086a62b8dSjtc          * we work around it with this stuff
82186a62b8dSjtc          */
82286a62b8dSjtc 	Player.p_gold = floor(Player.p_gold) + 0.1;
82386a62b8dSjtc 	Player.p_gems = floor(Player.p_gems) + 0.1;
82486a62b8dSjtc 	Player.p_mana = floor(Player.p_mana) + 0.1;
82586a62b8dSjtc 
82686a62b8dSjtc 	if (Player.p_ring.ring_type != R_NONE)
82786a62b8dSjtc 		/* do ring things */
82886a62b8dSjtc 	{
82986a62b8dSjtc 		/* rest to max */
83086a62b8dSjtc 		Player.p_energy = Player.p_maxenergy + Player.p_shield;
83186a62b8dSjtc 
83286a62b8dSjtc 		if (Player.p_ring.ring_duration <= 0)
83386a62b8dSjtc 			/* clean up expired rings */
834a232aee2Slukem 			switch (Player.p_ring.ring_type) {
83586a62b8dSjtc 			case R_BAD:	/* ring drives player crazy */
83686a62b8dSjtc 				Player.p_ring.ring_type = R_SPOILED;
83786a62b8dSjtc 				Player.p_ring.ring_duration = (short) ROLL(10.0, 25.0);
83886a62b8dSjtc 				break;
83986a62b8dSjtc 
84086a62b8dSjtc 			case R_NAZREG:	/* ring disappears */
84186a62b8dSjtc 				Player.p_ring.ring_type = R_NONE;
84286a62b8dSjtc 				break;
84386a62b8dSjtc 
84486a62b8dSjtc 			case R_SPOILED:	/* ring kills player */
84586a62b8dSjtc 				death("A cursed ring");
84686a62b8dSjtc 				break;
84786a62b8dSjtc 
84886a62b8dSjtc 			case R_DLREG:	/* this ring doesn't expire */
84986a62b8dSjtc 				Player.p_ring.ring_duration = 0;
85086a62b8dSjtc 				break;
85186a62b8dSjtc 			}
85286a62b8dSjtc 	}
85386a62b8dSjtc 	if (Player.p_age / N_AGE > Player.p_degenerated)
85486a62b8dSjtc 		/* age player slightly */
85586a62b8dSjtc 	{
85686a62b8dSjtc 		++Player.p_degenerated;
85786a62b8dSjtc 		if (Player.p_quickness > 23.0)
85886a62b8dSjtc 			Player.p_quickness *= 0.99;
85986a62b8dSjtc 		Player.p_strength *= 0.97;
86086a62b8dSjtc 		Player.p_brains *= 0.95;
86186a62b8dSjtc 		Player.p_magiclvl *= 0.97;
86286a62b8dSjtc 		Player.p_maxenergy *= 0.95;
86386a62b8dSjtc 		Player.p_quksilver *= 0.95;
86486a62b8dSjtc 		Player.p_sword *= 0.93;
86586a62b8dSjtc 		Player.p_shield *= 0.93;
86686a62b8dSjtc 	}
86786a62b8dSjtc }
86886a62b8dSjtc 
869a232aee2Slukem void
initplayer(struct player * playerp)870c7a109ccSdholland initplayer(struct player *playerp)
87186a62b8dSjtc {
87286a62b8dSjtc 	playerp->p_experience =
87386a62b8dSjtc 	    playerp->p_level =
87486a62b8dSjtc 	    playerp->p_strength =
87586a62b8dSjtc 	    playerp->p_sword =
87686a62b8dSjtc 	    playerp->p_might =
87786a62b8dSjtc 	    playerp->p_energy =
87886a62b8dSjtc 	    playerp->p_maxenergy =
87986a62b8dSjtc 	    playerp->p_shield =
88086a62b8dSjtc 	    playerp->p_quickness =
88186a62b8dSjtc 	    playerp->p_quksilver =
88286a62b8dSjtc 	    playerp->p_speed =
88386a62b8dSjtc 	    playerp->p_magiclvl =
88486a62b8dSjtc 	    playerp->p_mana =
88586a62b8dSjtc 	    playerp->p_brains =
88686a62b8dSjtc 	    playerp->p_poison =
88786a62b8dSjtc 	    playerp->p_gems =
88886a62b8dSjtc 	    playerp->p_sin =
88986a62b8dSjtc 	    playerp->p_1scratch =
89086a62b8dSjtc 	    playerp->p_2scratch = 0.0;
89186a62b8dSjtc 
89286a62b8dSjtc 	playerp->p_gold = ROLL(50.0, 75.0) + 0.1;	/* give some gold */
89386a62b8dSjtc 
89486a62b8dSjtc 	playerp->p_x = ROLL(-125.0, 251.0);
89586a62b8dSjtc 	playerp->p_y = ROLL(-125.0, 251.0);	/* give random x, y */
89686a62b8dSjtc 
89786a62b8dSjtc 	/* clear ring */
89886a62b8dSjtc 	playerp->p_ring.ring_type = R_NONE;
89986a62b8dSjtc 	playerp->p_ring.ring_duration = 0;
90086a62b8dSjtc 	playerp->p_ring.ring_inuse = FALSE;
90186a62b8dSjtc 
90286a62b8dSjtc 	playerp->p_age = 0L;
90386a62b8dSjtc 
90486a62b8dSjtc 	playerp->p_degenerated = 1;	/* don't degenerate initially */
90586a62b8dSjtc 
90686a62b8dSjtc 	playerp->p_type = C_FIGHTER;	/* default */
90786a62b8dSjtc 	playerp->p_specialtype = SC_NONE;
90886a62b8dSjtc 	playerp->p_lives =
90986a62b8dSjtc 	    playerp->p_crowns =
91086a62b8dSjtc 	    playerp->p_charms =
91186a62b8dSjtc 	    playerp->p_amulets =
91286a62b8dSjtc 	    playerp->p_holywater =
91386a62b8dSjtc 	    playerp->p_lastused = 0;
91486a62b8dSjtc 	playerp->p_status = S_NOTUSED;
91586a62b8dSjtc 	playerp->p_tampered = T_OFF;
91686a62b8dSjtc 	playerp->p_istat = I_OFF;
91786a62b8dSjtc 
91886a62b8dSjtc 	playerp->p_palantir =
91986a62b8dSjtc 	    playerp->p_blessing =
92086a62b8dSjtc 	    playerp->p_virgin =
92186a62b8dSjtc 	    playerp->p_blindness = FALSE;
92286a62b8dSjtc 
92386a62b8dSjtc 	playerp->p_name[0] =
92486a62b8dSjtc 	    playerp->p_password[0] =
92586a62b8dSjtc 	    playerp->p_login[0] = '\0';
92686a62b8dSjtc }
92786a62b8dSjtc 
928a232aee2Slukem void
readmessage(void)929c7a109ccSdholland readmessage(void)
93086a62b8dSjtc {
93186a62b8dSjtc 	move(3, 0);
93286a62b8dSjtc 	clrtoeol();
9335fb18dd9Sjsm 	fseek(Messagefp, 0L, SEEK_SET);
93486a62b8dSjtc 	if (fgets(Databuf, SZ_DATABUF, Messagefp) != NULL)
93586a62b8dSjtc 		addstr(Databuf);
93686a62b8dSjtc }
93786a62b8dSjtc 
938a232aee2Slukem void
error(const char * whichfile)939c7a109ccSdholland error(const char *whichfile)
94086a62b8dSjtc {
94173a79751Swiz 	int     (*funcp)(const char *,...);
94286a62b8dSjtc 
943a232aee2Slukem 	if (Windows) {
94486a62b8dSjtc 		funcp = printw;
94586a62b8dSjtc 		clear();
946a232aee2Slukem 	} else
94786a62b8dSjtc 		funcp = printf;
94886a62b8dSjtc 
94950134cf2Sjsm 	(*funcp) ("An unrecoverable error has occurred reading %s.  (%s)\n", whichfile, strerror(errno));
95086a62b8dSjtc 	(*funcp) ("Please run 'setup' to determine the problem.\n");
95186a62b8dSjtc 	cleanup(TRUE);
95286a62b8dSjtc 	/* NOTREACHED */
95386a62b8dSjtc }
95486a62b8dSjtc 
95586a62b8dSjtc double
distance(double x_1,double x_2,double y_1,double y_2)956c7a109ccSdholland distance(double x_1, double x_2, double y_1, double y_2)
95786a62b8dSjtc {
95886a62b8dSjtc 	double  deltax, deltay;
95986a62b8dSjtc 
960c074bb66Sdholland 	deltax = x_1 - x_2;
961c074bb66Sdholland 	deltay = y_1 - y_2;
96286a62b8dSjtc 	return (sqrt(deltax * deltax + deltay * deltay));
96386a62b8dSjtc }
96486a62b8dSjtc 
965a232aee2Slukem void
ill_sig(int whichsig)966c7a109ccSdholland ill_sig(int whichsig)
96786a62b8dSjtc {
96886a62b8dSjtc 	clear();
96986a62b8dSjtc 	if (!(whichsig == SIGINT || whichsig == SIGQUIT))
97086a62b8dSjtc 		printw("Error: caught signal # %d.\n", whichsig);
97186a62b8dSjtc 	cleanup(TRUE);
97286a62b8dSjtc 	/* NOTREACHED */
97386a62b8dSjtc }
97486a62b8dSjtc 
975092d3130Sjsm const char *
descrstatus(struct player * playerp)976c7a109ccSdholland descrstatus(struct player *playerp)
97786a62b8dSjtc {
978a232aee2Slukem 	switch (playerp->p_status) {
97986a62b8dSjtc 	case S_PLAYING:
98086a62b8dSjtc 		if (playerp->p_energy < 0.2 * (playerp->p_maxenergy + playerp->p_shield))
98186a62b8dSjtc 			return ("Low Energy");
982a232aee2Slukem 		else
983a232aee2Slukem 			if (playerp->p_blindness)
98486a62b8dSjtc 				return ("Blind");
98586a62b8dSjtc 			else
98686a62b8dSjtc 				return ("In game");
98786a62b8dSjtc 
98886a62b8dSjtc 	case S_CLOAKED:
98986a62b8dSjtc 		return ("Cloaked");
99086a62b8dSjtc 
99186a62b8dSjtc 	case S_INBATTLE:
99286a62b8dSjtc 		return ("In Battle");
99386a62b8dSjtc 
99486a62b8dSjtc 	case S_MONSTER:
99586a62b8dSjtc 		return ("Encounter");
99686a62b8dSjtc 
99786a62b8dSjtc 	case S_TRADING:
99886a62b8dSjtc 		return ("Trading");
99986a62b8dSjtc 
100086a62b8dSjtc 	case S_OFF:
100186a62b8dSjtc 		return ("Off");
100286a62b8dSjtc 
100386a62b8dSjtc 	case S_HUNGUP:
100486a62b8dSjtc 		return ("Hung up");
100586a62b8dSjtc 
100686a62b8dSjtc 	default:
100786a62b8dSjtc 		return ("");
100886a62b8dSjtc 	}
100986a62b8dSjtc }
101086a62b8dSjtc 
101186a62b8dSjtc double
drandom(void)1012c7a109ccSdholland drandom(void)
101386a62b8dSjtc {
101486a62b8dSjtc 	if (sizeof(int) != 2)
101586a62b8dSjtc 		/* use only low bits */
101686a62b8dSjtc 		return ((double) (random() & 0x7fff) / 32768.0);
101786a62b8dSjtc 	else
101886a62b8dSjtc 		return ((double) random() / 32768.0);
101986a62b8dSjtc }
102086a62b8dSjtc 
1021a232aee2Slukem void
collecttaxes(double gold,double gems)1022c7a109ccSdholland collecttaxes(double gold, double gems)
102386a62b8dSjtc {
102486a62b8dSjtc 	FILE   *fp;		/* to update Goldfile */
102586a62b8dSjtc 	double  dtemp;		/* for temporary calculations */
102686a62b8dSjtc 	double  taxes;		/* tax liability */
102786a62b8dSjtc 
102886a62b8dSjtc 	/* add to cache */
102986a62b8dSjtc 	Player.p_gold += gold;
103086a62b8dSjtc 	Player.p_gems += gems;
103186a62b8dSjtc 
103286a62b8dSjtc 	/* calculate tax liability */
103386a62b8dSjtc 	taxes = N_TAXAMOUNT / 100.0 * (N_GEMVALUE * gems + gold);
103486a62b8dSjtc 
103586a62b8dSjtc 	if (Player.p_gold < taxes)
1036a232aee2Slukem 		/* not enough gold to pay taxes, must convert some gems to
1037a232aee2Slukem 		 * gold */
103886a62b8dSjtc 	{
1039a232aee2Slukem 		dtemp = floor(taxes / N_GEMVALUE + 1.0);	/* number of gems to
1040a232aee2Slukem 								 * convert */
104186a62b8dSjtc 
104286a62b8dSjtc 		if (Player.p_gems >= dtemp)
104386a62b8dSjtc 			/* player has enough to convert */
104486a62b8dSjtc 		{
104586a62b8dSjtc 			Player.p_gems -= dtemp;
104686a62b8dSjtc 			Player.p_gold += dtemp * N_GEMVALUE;
1047a232aee2Slukem 		} else
104886a62b8dSjtc 			/* take everything; this should never happen */
104986a62b8dSjtc 		{
105086a62b8dSjtc 			Player.p_gold += Player.p_gems * N_GEMVALUE;
105186a62b8dSjtc 			Player.p_gems = 0.0;
105286a62b8dSjtc 			taxes = Player.p_gold;
105386a62b8dSjtc 		}
105486a62b8dSjtc 	}
105586a62b8dSjtc 	Player.p_gold -= taxes;
105686a62b8dSjtc 
105786a62b8dSjtc 	if ((fp = fopen(_PATH_GOLD, "r+")) != NULL)
105886a62b8dSjtc 		/* update taxes */
105986a62b8dSjtc 	{
106086a62b8dSjtc 		dtemp = 0.0;
106186a62b8dSjtc 		fread((char *) &dtemp, sizeof(double), 1, fp);
106286a62b8dSjtc 		dtemp += floor(taxes);
10635fb18dd9Sjsm 		fseek(fp, 0L, SEEK_SET);
106486a62b8dSjtc 		fwrite((char *) &dtemp, sizeof(double), 1, fp);
106586a62b8dSjtc 		fclose(fp);
106686a62b8dSjtc 	}
106786a62b8dSjtc }
1068