xref: /csrg-svn/games/larn/scores.c (revision 36998)
1*36998Sbostic /* scores.c			 Larn is copyrighted 1986 by Noah Morgan.
2*36998Sbostic  *
3*36998Sbostic  *	Functions in this file are:
4*36998Sbostic  *
5*36998Sbostic  *	readboard() 	Function to read in the scoreboard into a static buffer
6*36998Sbostic  *	writeboard()	Function to write the scoreboard from readboard()'s buffer
7*36998Sbostic  *	makeboard() 	Function to create a new scoreboard (wipe out old one)
8*36998Sbostic  *	hashewon()	 Function to return 1 if player has won a game before, else 0
9*36998Sbostic  *	long paytaxes(x)	 Function to pay taxes if any are due
10*36998Sbostic  *	winshou()		Subroutine to print out the winning scoreboard
11*36998Sbostic  *	shou(x)			Subroutine to print out the non-winners scoreboard
12*36998Sbostic  *	showscores()		Function to show the scoreboard on the terminal
13*36998Sbostic  *	showallscores()	Function to show scores and the iven lists that go with them
14*36998Sbostic  *	sortboard()		Function to sort the scoreboard
15*36998Sbostic  *	newscore(score, whoo, whyded, winner) 	Function to add entry to scoreboard
16*36998Sbostic  *	new1sub(score,i,whoo,taxes) 		  Subroutine to put player into a
17*36998Sbostic  *	new2sub(score,i,whoo,whyded)	 	  Subroutine to put player into a
18*36998Sbostic  *	died(x) 	Subroutine to record who played larn, and what the score was
19*36998Sbostic  *	diedsub(x) Subroutine to print out a line showing player when he is killed
20*36998Sbostic  *	diedlog() 	Subroutine to read a log file and print it out in ascii format
21*36998Sbostic  *	getplid(name)		Function to get players id # from id file
22*36998Sbostic  *
23*36998Sbostic  */
24*36998Sbostic #include <sys/types.h>
25*36998Sbostic #include <sys/times.h>
26*36998Sbostic #include <sys/stat.h>
27*36998Sbostic #include "header.h"
28*36998Sbostic 
29*36998Sbostic struct scofmt			/*	This is the structure for the scoreboard 		*/
30*36998Sbostic 	{
31*36998Sbostic 	long score;			/* the score of the player 							*/
32*36998Sbostic 	long suid;			/* the user id number of the player 				*/
33*36998Sbostic 	short what;			/* the number of the monster that killed player 	*/
34*36998Sbostic 	short level;		/* the level player was on when he died 			*/
35*36998Sbostic 	short hardlev;		/* the level of difficulty player played at 		*/
36*36998Sbostic 	short order;		/* the relative ordering place of this entry 		*/
37*36998Sbostic 	char who[40];		/* the name of the character 						*/
38*36998Sbostic 	char sciv[26][2];	/* this is the inventory list of the character 		*/
39*36998Sbostic 	};
40*36998Sbostic struct wscofmt			/* This is the structure for the winning scoreboard */
41*36998Sbostic 	{
42*36998Sbostic 	long score;			/* the score of the player 							*/
43*36998Sbostic 	long timeused;		/* the time used in mobuls to win the game 			*/
44*36998Sbostic 	long taxes;			/* taxes he owes to LRS 							*/
45*36998Sbostic 	long suid;			/* the user id number of the player 				*/
46*36998Sbostic 	short hardlev;		/* the level of difficulty player played at 		*/
47*36998Sbostic 	short order;		/* the relative ordering place of this entry 		*/
48*36998Sbostic 	char who[40];		/* the name of the character 						*/
49*36998Sbostic 	};
50*36998Sbostic 
51*36998Sbostic struct log_fmt			/* 102 bytes struct for the log file 				*/
52*36998Sbostic 	{
53*36998Sbostic 	long score;			/* the players score 								*/
54*36998Sbostic 	long diedtime;		/* time when game was over 							*/
55*36998Sbostic 	short cavelev;		/* level in caves 									*/
56*36998Sbostic 	short diff;			/* difficulty player played at 						*/
57*36998Sbostic #ifdef EXTRA
58*36998Sbostic 	long elapsedtime;	/* real time of game in seconds 					*/
59*36998Sbostic 	long bytout;		/* bytes input and output 							*/
60*36998Sbostic 	long bytin;
61*36998Sbostic 	long moves;			/* number of moves made by player 					*/
62*36998Sbostic 	short ac;			/* armor class of player 							*/
63*36998Sbostic 	short hp,hpmax;		/* players hitpoints 								*/
64*36998Sbostic 	short cputime;		/* cpu time needed in seconds 						*/
65*36998Sbostic 	short killed,spused;/* monsters killed and spells cast 					*/
66*36998Sbostic 	short usage;		/* usage of the cpu in % 							*/
67*36998Sbostic 	short lev;			/* player level 									*/
68*36998Sbostic #endif
69*36998Sbostic 	char who[12];		/* player name 										*/
70*36998Sbostic 	char what[46];		/* what happened to player 							*/
71*36998Sbostic 	};
72*36998Sbostic 
73*36998Sbostic static struct scofmt sco[SCORESIZE];	/* the structure for the scoreboard  */
74*36998Sbostic static struct wscofmt winr[SCORESIZE];	/* struct for the winning scoreboard */
75*36998Sbostic static struct log_fmt logg;				/* structure for the log file 		 */
76*36998Sbostic static char *whydead[] = {
77*36998Sbostic 	"quit", "suspended", "self - annihilated", "shot by an arrow",
78*36998Sbostic 	"hit by a dart", "fell into a pit", "fell into a bottomless pit",
79*36998Sbostic 	"a winner", "trapped in solid rock", "killed by a missing save file",
80*36998Sbostic 	"killed by an old save file", "caught by the greedy cheater checker trap",
81*36998Sbostic 	"killed by a protected save file","killed his family and committed suicide",
82*36998Sbostic 	"erased by a wayward finger", "fell through a bottomless trap door",
83*36998Sbostic 	"fell through a trap door", "drank some poisonous water",
84*36998Sbostic 	"fried by an electric shock", "slipped on a volcano shaft",
85*36998Sbostic 	"killed by a stupid act of frustration", "attacked by a revolting demon",
86*36998Sbostic 	"hit by his own magic", "demolished by an unseen attacker",
87*36998Sbostic 	"fell into the dreadful sleep", "killed by an exploding chest",
88*36998Sbostic /*26*/	"killed by a missing maze data file", "annihilated in a sphere",
89*36998Sbostic 	"died a post mortem death","wasted by a malloc() failure"
90*36998Sbostic 	};
91*36998Sbostic 
92*36998Sbostic /*
93*36998Sbostic  *	readboard() 	Function to read in the scoreboard into a static buffer
94*36998Sbostic  *
95*36998Sbostic  *	returns -1 if unable to read in the scoreboard, returns 0 if all is OK
96*36998Sbostic  */
readboard()97*36998Sbostic readboard()
98*36998Sbostic 	{
99*36998Sbostic 	if (lopen(scorefile)<0)
100*36998Sbostic 	  { lprcat("Can't read scoreboard\n"); lflush(); return(-1); }
101*36998Sbostic 	lrfill((char*)sco,sizeof(sco));		lrfill((char*)winr,sizeof(winr));
102*36998Sbostic 	lrclose();  lcreat((char*)0);  return(0);
103*36998Sbostic 	}
104*36998Sbostic 
105*36998Sbostic /*
106*36998Sbostic  *	writeboard()	Function to write the scoreboard from readboard()'s buffer
107*36998Sbostic  *
108*36998Sbostic  *	returns -1 if unable to write the scoreboard, returns 0 if all is OK
109*36998Sbostic  */
writeboard()110*36998Sbostic writeboard()
111*36998Sbostic 	{
112*36998Sbostic 	set_score_output();
113*36998Sbostic 	if (lcreat(scorefile)<0)
114*36998Sbostic 	  { lprcat("Can't write scoreboard\n"); lflush(); return(-1); }
115*36998Sbostic 	lwrite((char*)sco,sizeof(sco));		lwrite((char*)winr,sizeof(winr));
116*36998Sbostic 	lwclose();  lcreat((char*)0);  return(0);
117*36998Sbostic 	}
118*36998Sbostic 
119*36998Sbostic /*
120*36998Sbostic  *	makeboard() 		Function to create a new scoreboard (wipe out old one)
121*36998Sbostic  *
122*36998Sbostic  *	returns -1 if unable to write the scoreboard, returns 0 if all is OK
123*36998Sbostic  */
makeboard()124*36998Sbostic makeboard()
125*36998Sbostic 	{
126*36998Sbostic 	register int i;
127*36998Sbostic 	for (i=0; i<SCORESIZE; i++)
128*36998Sbostic 		{
129*36998Sbostic 		winr[i].taxes = winr[i].score = sco[i].score = 0;
130*36998Sbostic 		winr[i].order = sco[i].order = i;
131*36998Sbostic 		}
132*36998Sbostic 	if (writeboard()) return(-1);
133*36998Sbostic 	chmod(scorefile,0660);
134*36998Sbostic 	return(0);
135*36998Sbostic 	}
136*36998Sbostic 
137*36998Sbostic /*
138*36998Sbostic  *	hashewon()	 Function to return 1 if player has won a game before, else 0
139*36998Sbostic  *
140*36998Sbostic  *	This function also sets c[HARDGAME] to appropriate value -- 0 if not a
141*36998Sbostic  *	winner, otherwise the next level of difficulty listed in the winners
142*36998Sbostic  *	scoreboard.  This function also sets outstanding_taxes to the value in
143*36998Sbostic  *	the winners scoreboard.
144*36998Sbostic  */
hashewon()145*36998Sbostic hashewon()
146*36998Sbostic 	{
147*36998Sbostic 	register int i;
148*36998Sbostic 	c[HARDGAME] = 0;
149*36998Sbostic 	if (readboard() < 0) return(0);	/* can't find scoreboard */
150*36998Sbostic 	for (i=0; i<SCORESIZE; i++)	/* search through winners scoreboard */
151*36998Sbostic 	   if (winr[i].suid == userid)
152*36998Sbostic 		  if (winr[i].score > 0)
153*36998Sbostic 			{
154*36998Sbostic 			c[HARDGAME]=winr[i].hardlev+1;  outstanding_taxes=winr[i].taxes;
155*36998Sbostic 			return(1);
156*36998Sbostic 			}
157*36998Sbostic 	return(0);
158*36998Sbostic 	}
159*36998Sbostic 
160*36998Sbostic /*
161*36998Sbostic  *	long paytaxes(x)		 Function to pay taxes if any are due
162*36998Sbostic  *
163*36998Sbostic  *	Enter with the amount (in gp) to pay on the taxes.
164*36998Sbostic  *	Returns amount actually paid.
165*36998Sbostic  */
paytaxes(x)166*36998Sbostic long paytaxes(x)
167*36998Sbostic 	long x;
168*36998Sbostic 	{
169*36998Sbostic 	register int i;
170*36998Sbostic 	register long amt;
171*36998Sbostic 	if (x<0) return(0L);
172*36998Sbostic 	if (readboard()<0) return(0L);
173*36998Sbostic 	for (i=0; i<SCORESIZE; i++)
174*36998Sbostic 		if (winr[i].suid == userid)	/* look for players winning entry */
175*36998Sbostic 			if (winr[i].score>0) /* search for a winning entry for the player */
176*36998Sbostic 				{
177*36998Sbostic 				amt = winr[i].taxes;
178*36998Sbostic 				if (x < amt) amt=x;		/* don't overpay taxes (Ughhhhh) */
179*36998Sbostic 				winr[i].taxes -= amt;
180*36998Sbostic 				outstanding_taxes -= amt;
181*36998Sbostic 				if (writeboard()<0) return(0);
182*36998Sbostic 				return(amt);
183*36998Sbostic 				}
184*36998Sbostic 	return(0L);	/* couldn't find user on winning scoreboard */
185*36998Sbostic 	}
186*36998Sbostic 
187*36998Sbostic /*
188*36998Sbostic  *	winshou()		Subroutine to print out the winning scoreboard
189*36998Sbostic  *
190*36998Sbostic  *	Returns the number of players on scoreboard that were shown
191*36998Sbostic  */
winshou()192*36998Sbostic winshou()
193*36998Sbostic 	{
194*36998Sbostic 	register struct wscofmt *p;
195*36998Sbostic 	register int i,j,count;
196*36998Sbostic 	for (count=j=i=0; i<SCORESIZE; i++) /* is there anyone on the scoreboard? */
197*36998Sbostic 		if (winr[i].score != 0)
198*36998Sbostic 			{ j++; break; }
199*36998Sbostic 	if (j)
200*36998Sbostic 		{
201*36998Sbostic 		lprcat("\n  Score    Difficulty   Time Needed   Larn Winners List\n");
202*36998Sbostic 
203*36998Sbostic 		for (i=0; i<SCORESIZE; i++)	/* this loop is needed to print out the */
204*36998Sbostic 		  for (j=0; j<SCORESIZE; j++) /* winners in order */
205*36998Sbostic 			{
206*36998Sbostic 			p = &winr[j];	/* pointer to the scoreboard entry */
207*36998Sbostic 			if (p->order == i)
208*36998Sbostic 				{
209*36998Sbostic 				if (p->score)
210*36998Sbostic 					{
211*36998Sbostic 					count++;
212*36998Sbostic 					lprintf("%10d     %2d      %5d Mobuls   %s \n",
213*36998Sbostic 					(long)p->score,(long)p->hardlev,(long)p->timeused,p->who);
214*36998Sbostic 					}
215*36998Sbostic 				break;
216*36998Sbostic 				}
217*36998Sbostic 			}
218*36998Sbostic 		}
219*36998Sbostic 	return(count);	/* return number of people on scoreboard */
220*36998Sbostic 	}
221*36998Sbostic 
222*36998Sbostic /*
223*36998Sbostic  *	shou(x)			Subroutine to print out the non-winners scoreboard
224*36998Sbostic  *		int x;
225*36998Sbostic  *
226*36998Sbostic  *	Enter with 0 to list the scores, enter with 1 to list inventories too
227*36998Sbostic  *	Returns the number of players on scoreboard that were shown
228*36998Sbostic  */
shou(x)229*36998Sbostic shou(x)
230*36998Sbostic 	int x;
231*36998Sbostic 	{
232*36998Sbostic 	register int i,j,n,k;
233*36998Sbostic 	int count;
234*36998Sbostic 	for (count=j=i=0; i<SCORESIZE; i++)	/* is the scoreboard empty? */
235*36998Sbostic 		if (sco[i].score!= 0)
236*36998Sbostic 			{ j++; break; }
237*36998Sbostic 	if (j)
238*36998Sbostic 		{
239*36998Sbostic 		lprcat("\n   Score   Difficulty   Larn Visitor Log\n");
240*36998Sbostic 		for (i=0; i<SCORESIZE; i++) /* be sure to print them out in order */
241*36998Sbostic 		  for (j=0; j<SCORESIZE; j++)
242*36998Sbostic 			if (sco[j].order == i)
243*36998Sbostic 				{
244*36998Sbostic 				if (sco[j].score)
245*36998Sbostic 					{
246*36998Sbostic 					count++;
247*36998Sbostic 					lprintf("%10d     %2d       %s ",
248*36998Sbostic 						(long)sco[j].score,(long)sco[j].hardlev,sco[j].who);
249*36998Sbostic 					if (sco[j].what < 256) lprintf("killed by a %s",monster[sco[j].what].name);
250*36998Sbostic 						else lprintf("%s",whydead[sco[j].what - 256]);
251*36998Sbostic 					if (x != 263) lprintf(" on %s",levelname[sco[j].level]);
252*36998Sbostic 					if (x)
253*36998Sbostic 						{
254*36998Sbostic 						for (n=0; n<26; n++) { iven[n]=sco[j].sciv[n][0]; ivenarg[n]=sco[j].sciv[n][1]; }
255*36998Sbostic 						for (k=1; k<99; k++)
256*36998Sbostic 						  for (n=0; n<26; n++)
257*36998Sbostic 							if (k==iven[n])  { srcount=0; show3(n); }
258*36998Sbostic 						lprcat("\n\n");
259*36998Sbostic 						}
260*36998Sbostic 					else lprc('\n');
261*36998Sbostic 					}
262*36998Sbostic 				j=SCORESIZE;
263*36998Sbostic 				}
264*36998Sbostic 		}
265*36998Sbostic 	return(count);	/* return the number of players just shown */
266*36998Sbostic 	}
267*36998Sbostic 
268*36998Sbostic /*
269*36998Sbostic  *	showscores()		Function to show the scoreboard on the terminal
270*36998Sbostic  *
271*36998Sbostic  *	Returns nothing of value
272*36998Sbostic  */
273*36998Sbostic static char esb[] = "The scoreboard is empty.\n";
showscores()274*36998Sbostic showscores()
275*36998Sbostic 	{
276*36998Sbostic 	register int i,j;
277*36998Sbostic 	lflush();  lcreat((char*)0);  if (readboard()<0) return;
278*36998Sbostic 	i=winshou();	j=shou(0);
279*36998Sbostic 	if (i+j == 0) lprcat(esb); else lprc('\n');
280*36998Sbostic 	lflush();
281*36998Sbostic 	}
282*36998Sbostic 
283*36998Sbostic /*
284*36998Sbostic  *	showallscores()	Function to show scores and the iven lists that go with them
285*36998Sbostic  *
286*36998Sbostic  *	Returns nothing of value
287*36998Sbostic  */
showallscores()288*36998Sbostic showallscores()
289*36998Sbostic 	{
290*36998Sbostic 	register int i,j;
291*36998Sbostic 	lflush();  lcreat((char*)0);  if (readboard()<0) return;
292*36998Sbostic 	c[WEAR] = c[WIELD] = c[SHIELD] = -1;  /* not wielding or wearing anything */
293*36998Sbostic 	for (i=0; i<MAXPOTION; i++) potionname[i][0]=' ';
294*36998Sbostic 	for (i=0; i<MAXSCROLL; i++) scrollname[i][0]=' ';
295*36998Sbostic 	i=winshou();  j=shou(1);
296*36998Sbostic 	if (i+j==0) lprcat(esb); else lprc('\n');
297*36998Sbostic 	lflush();
298*36998Sbostic 	}
299*36998Sbostic 
300*36998Sbostic /*
301*36998Sbostic  *	sortboard()		Function to sort the scoreboard
302*36998Sbostic  *
303*36998Sbostic  *	Returns 0 if no sorting done, else returns 1
304*36998Sbostic  */
sortboard()305*36998Sbostic sortboard()
306*36998Sbostic 	{
307*36998Sbostic 	register int i,j,pos;
308*36998Sbostic 	long jdat;
309*36998Sbostic 	for (i=0; i<SCORESIZE; i++) sco[i].order = winr[i].order = -1;
310*36998Sbostic 	pos=0;  while (pos < SCORESIZE)
311*36998Sbostic 		{
312*36998Sbostic 		jdat=0;
313*36998Sbostic 		for (i=0; i<SCORESIZE; i++)
314*36998Sbostic 			if ((sco[i].order < 0) && (sco[i].score >= jdat))
315*36998Sbostic 				{ j=i;  jdat=sco[i].score; }
316*36998Sbostic 		sco[j].order = pos++;
317*36998Sbostic 		}
318*36998Sbostic 	pos=0;  while (pos < SCORESIZE)
319*36998Sbostic 		{
320*36998Sbostic 		jdat=0;
321*36998Sbostic 		for (i=0; i<SCORESIZE; i++)
322*36998Sbostic 			if ((winr[i].order < 0) && (winr[i].score >= jdat))
323*36998Sbostic 				{ j=i;  jdat=winr[i].score; }
324*36998Sbostic 		winr[j].order = pos++;
325*36998Sbostic 		}
326*36998Sbostic 	return(1);
327*36998Sbostic 	}
328*36998Sbostic 
329*36998Sbostic /*
330*36998Sbostic  *	newscore(score, whoo, whyded, winner) 	Function to add entry to scoreboard
331*36998Sbostic  *		int score, winner, whyded;
332*36998Sbostic  *		char *whoo;
333*36998Sbostic  *
334*36998Sbostic  *	Enter with the total score in gp in score,  players name in whoo,
335*36998Sbostic  *		died() reason # in whyded, and TRUE/FALSE in winner if a winner
336*36998Sbostic  *	ex.		newscore(1000, "player 1", 32, 0);
337*36998Sbostic  */
newscore(score,whoo,whyded,winner)338*36998Sbostic newscore(score, whoo, whyded, winner)
339*36998Sbostic 	long score;
340*36998Sbostic 	int winner, whyded;
341*36998Sbostic 	char *whoo;
342*36998Sbostic 	{
343*36998Sbostic 	register int i;
344*36998Sbostic 	long taxes;
345*36998Sbostic 	if (readboard() < 0) return; 	/*	do the scoreboard	*/
346*36998Sbostic 	/* if a winner then delete all non-winning scores */
347*36998Sbostic 	if (cheat) winner=0;	/* if he cheated, don't let him win */
348*36998Sbostic 	if (winner)
349*36998Sbostic 		{
350*36998Sbostic 		for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid) sco[i].score=0;
351*36998Sbostic 		taxes = score*TAXRATE;
352*36998Sbostic 		score += 100000*c[HARDGAME];	/* bonus for winning */
353*36998Sbostic 	/* if he has a slot on the winning scoreboard update it if greater score */
354*36998Sbostic 		for (i=0; i<SCORESIZE; i++) if (winr[i].suid == userid)
355*36998Sbostic 				{ new1sub(score,i,whoo,taxes); return; }
356*36998Sbostic 	/* he had no entry. look for last entry and see if he has a greater score */
357*36998Sbostic 		for (i=0; i<SCORESIZE; i++) if (winr[i].order == SCORESIZE-1)
358*36998Sbostic 				{ new1sub(score,i,whoo,taxes); return; }
359*36998Sbostic 		}
360*36998Sbostic 	else if (!cheat) /* for not winning scoreboard */
361*36998Sbostic 		{
362*36998Sbostic 	/* if he has a slot on the scoreboard update it if greater score */
363*36998Sbostic 		for (i=0; i<SCORESIZE; i++) if (sco[i].suid == userid)
364*36998Sbostic 				{ new2sub(score,i,whoo,whyded); return; }
365*36998Sbostic 	/* he had no entry. look for last entry and see if he has a greater score */
366*36998Sbostic 		for (i=0; i<SCORESIZE; i++) if (sco[i].order == SCORESIZE-1)
367*36998Sbostic 				{ new2sub(score,i,whoo,whyded); return; }
368*36998Sbostic 		}
369*36998Sbostic 	}
370*36998Sbostic 
371*36998Sbostic /*
372*36998Sbostic  *	new1sub(score,i,whoo,taxes) 	  Subroutine to put player into a
373*36998Sbostic  *		int score,i,whyded,taxes;		  winning scoreboard entry if his score
374*36998Sbostic  *		char *whoo; 					  is high enough
375*36998Sbostic  *
376*36998Sbostic  *	Enter with the total score in gp in score,  players name in whoo,
377*36998Sbostic  *		died() reason # in whyded, and TRUE/FALSE in winner if a winner
378*36998Sbostic  *		slot in scoreboard in i, and the tax bill in taxes.
379*36998Sbostic  *	Returns nothing of value
380*36998Sbostic  */
new1sub(score,i,whoo,taxes)381*36998Sbostic new1sub(score,i,whoo,taxes)
382*36998Sbostic 	long score,taxes;
383*36998Sbostic 	int i;
384*36998Sbostic 	char *whoo;
385*36998Sbostic 	{
386*36998Sbostic 	register struct wscofmt *p;
387*36998Sbostic 	p = &winr[i];
388*36998Sbostic 	p->taxes += taxes;
389*36998Sbostic 	if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
390*36998Sbostic 		{
391*36998Sbostic 		strcpy(p->who,whoo);  		p->score=score;
392*36998Sbostic 		p->hardlev=c[HARDGAME];		p->suid=userid;
393*36998Sbostic 		p->timeused=gtime/100;
394*36998Sbostic 		}
395*36998Sbostic 	}
396*36998Sbostic 
397*36998Sbostic /*
398*36998Sbostic  *	new2sub(score,i,whoo,whyded)	 	  Subroutine to put player into a
399*36998Sbostic  *		int score,i,whyded,taxes;		  non-winning scoreboard entry if his
400*36998Sbostic  *		char *whoo; 					  score is high enough
401*36998Sbostic  *
402*36998Sbostic  *	Enter with the total score in gp in score,  players name in whoo,
403*36998Sbostic  *		died() reason # in whyded, and slot in scoreboard in i.
404*36998Sbostic  *	Returns nothing of value
405*36998Sbostic  */
new2sub(score,i,whoo,whyded)406*36998Sbostic new2sub(score,i,whoo,whyded)
407*36998Sbostic 	long score;
408*36998Sbostic 	int i,whyded;
409*36998Sbostic 	char *whoo;
410*36998Sbostic 	{
411*36998Sbostic 	register int j;
412*36998Sbostic 	register struct scofmt *p;
413*36998Sbostic 	p = &sco[i];
414*36998Sbostic 	if ((score >= p->score) || (c[HARDGAME] > p->hardlev))
415*36998Sbostic 		{
416*36998Sbostic 		strcpy(p->who,whoo);  p->score=score;
417*36998Sbostic 		p->what=whyded;       p->hardlev=c[HARDGAME];
418*36998Sbostic 		p->suid=userid;		  p->level=level;
419*36998Sbostic 		for (j=0; j<26; j++)
420*36998Sbostic 			{ p->sciv[j][0]=iven[j]; p->sciv[j][1]=ivenarg[j]; }
421*36998Sbostic 		}
422*36998Sbostic 	}
423*36998Sbostic 
424*36998Sbostic /*
425*36998Sbostic  *	died(x) 	Subroutine to record who played larn, and what the score was
426*36998Sbostic  *		int x;
427*36998Sbostic  *
428*36998Sbostic  *	if x < 0 then don't show scores
429*36998Sbostic  *	died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
430*36998Sbostic  *
431*36998Sbostic  *		< 256	killed by the monster number
432*36998Sbostic  *		256		quit
433*36998Sbostic  *		257		suspended
434*36998Sbostic  *		258		self - annihilated
435*36998Sbostic  *		259		shot by an arrow
436*36998Sbostic  *		260		hit by a dart
437*36998Sbostic  *		261		fell into a pit
438*36998Sbostic  *		262		fell into a bottomless pit
439*36998Sbostic  *		263		a winner
440*36998Sbostic  *		264		trapped in solid rock
441*36998Sbostic  *		265		killed by a missing save file
442*36998Sbostic  *		266		killed by an old save file
443*36998Sbostic  *		267		caught by the greedy cheater checker trap
444*36998Sbostic  *		268		killed by a protected save file
445*36998Sbostic  *		269		killed his family and killed himself
446*36998Sbostic  *		270		erased by a wayward finger
447*36998Sbostic  *		271		fell through a bottomless trap door
448*36998Sbostic  *		272		fell through a trap door
449*36998Sbostic  *		273		drank some poisonous water
450*36998Sbostic  *		274		fried by an electric shock
451*36998Sbostic  *		275		slipped on a volcano shaft
452*36998Sbostic  *		276		killed by a stupid act of frustration
453*36998Sbostic  *		277		attacked by a revolting demon
454*36998Sbostic  *		278		hit by his own magic
455*36998Sbostic  *		279		demolished by an unseen attacker
456*36998Sbostic  *		280		fell into the dreadful sleep
457*36998Sbostic  *		281		killed by an exploding chest
458*36998Sbostic  *		282		killed by a missing maze data file
459*36998Sbostic  *		283		killed by a sphere of annihilation
460*36998Sbostic  *		284		died a post mortem death
461*36998Sbostic  *		285		malloc() failure
462*36998Sbostic  *		300		quick quit -- don't put on scoreboard
463*36998Sbostic  */
464*36998Sbostic 
465*36998Sbostic static int scorerror;
died(x)466*36998Sbostic died(x)
467*36998Sbostic 	int x;
468*36998Sbostic 	{
469*36998Sbostic 	register int f,win;
470*36998Sbostic 	char ch,*mod;
471*36998Sbostic 	long zzz,i;
472*36998Sbostic 	struct tms cputime;
473*36998Sbostic 	if (c[LIFEPROT]>0) /* if life protection */
474*36998Sbostic 		{
475*36998Sbostic 		switch((x>0) ? x : -x)
476*36998Sbostic 			{
477*36998Sbostic 			case 256: case 257: case 262: case 263: case 265: case 266:
478*36998Sbostic 			case 267: case 268: case 269: case 271: case 282: case 284:
479*36998Sbostic 			case 285: case 300:  goto invalid; /* can't be saved */
480*36998Sbostic 			};
481*36998Sbostic 		--c[LIFEPROT]; c[HP]=1; --c[CONSTITUTION];
482*36998Sbostic 		cursors(); lprcat("\nYou feel wiiieeeeerrrrrd all over! "); beep();
483*36998Sbostic 		lflush();  sleep(4);
484*36998Sbostic 		return; /* only case where died() returns */
485*36998Sbostic 		}
486*36998Sbostic invalid:
487*36998Sbostic 	clearvt100();  lflush();  f=0;
488*36998Sbostic 	if (ckpflag) unlink(ckpfile);	/* remove checkpoint file if used */
489*36998Sbostic 	if (x<0) { f++; x = -x; }	/* if we are not to display the scores */
490*36998Sbostic 	if ((x == 300) || (x == 257))  exit();	/* for quick exit or saved game */
491*36998Sbostic 	if (x == 263)  win = 1;  else  win = 0;
492*36998Sbostic 	c[GOLD] += c[BANKACCOUNT];   c[BANKACCOUNT] = 0;
493*36998Sbostic 		/*	now enter the player at the end of the scoreboard */
494*36998Sbostic 	newscore(c[GOLD], logname, x, win);
495*36998Sbostic 	diedsub(x);	/* print out the score line */  lflush();
496*36998Sbostic 
497*36998Sbostic 	set_score_output();
498*36998Sbostic 	if ((wizard == 0) && (c[GOLD] > 0)) 	/*	wizards can't score		*/
499*36998Sbostic 		{
500*36998Sbostic #ifndef NOLOG
501*36998Sbostic 		if (lappend(logfile)<0)  /* append to file */
502*36998Sbostic 			{
503*36998Sbostic 			if (lcreat(logfile)<0) /* and can't create new log file */
504*36998Sbostic 		    	{
505*36998Sbostic 				lcreat((char*)0);
506*36998Sbostic 				lprcat("\nCan't open record file:  I can't post your score.\n");
507*36998Sbostic 				sncbr();  resetscroll();  lflush();  exit();
508*36998Sbostic 				}
509*36998Sbostic 			chmod(logfile,0660);
510*36998Sbostic 			}
511*36998Sbostic 		strcpy(logg.who,loginname);
512*36998Sbostic 		logg.score = c[GOLD];		logg.diff = c[HARDGAME];
513*36998Sbostic 		if (x < 256)
514*36998Sbostic 			{
515*36998Sbostic 			ch = *monster[x].name;
516*36998Sbostic 			if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
517*36998Sbostic 				mod="an";  else mod="a";
518*36998Sbostic 			sprintf(logg.what,"killed by %s %s",mod,monster[x].name);
519*36998Sbostic 			}
520*36998Sbostic 		else sprintf(logg.what,"%s",whydead[x - 256]);
521*36998Sbostic 		logg.cavelev=level;
522*36998Sbostic 		time(&zzz);	  /* get cpu time -- write out score info */
523*36998Sbostic 		logg.diedtime=zzz;
524*36998Sbostic #ifdef EXTRA
525*36998Sbostic 		times(&cputime);  /* get cpu time -- write out score info */
526*36998Sbostic 		logg.cputime = i = (cputime.tms_utime + cputime.tms_stime)/60 + c[CPUTIME];
527*36998Sbostic 		logg.lev=c[LEVEL];			logg.ac=c[AC];
528*36998Sbostic 		logg.hpmax=c[HPMAX];		logg.hp=c[HP];
529*36998Sbostic 		logg.elapsedtime=(zzz-initialtime+59)/60;
530*36998Sbostic 		logg.usage=(10000*i)/(zzz-initialtime);
531*36998Sbostic 		logg.bytin=c[BYTESIN];		logg.bytout=c[BYTESOUT];
532*36998Sbostic 		logg.moves=c[MOVESMADE];	logg.spused=c[SPELLSCAST];
533*36998Sbostic 		logg.killed=c[MONSTKILLED];
534*36998Sbostic #endif
535*36998Sbostic 		lwrite((char*)&logg,sizeof(struct log_fmt));	 lwclose();
536*36998Sbostic #endif NOLOG
537*36998Sbostic 
538*36998Sbostic /*	now for the scoreboard maintenance -- not for a suspended game 	*/
539*36998Sbostic 		if (x != 257)
540*36998Sbostic 			{
541*36998Sbostic 			if (sortboard())  scorerror = writeboard();
542*36998Sbostic 			}
543*36998Sbostic 		}
544*36998Sbostic 	if ((x==256) || (x==257) || (f != 0)) exit();
545*36998Sbostic 	if (scorerror == 0) showscores();	/* if we updated the scoreboard */
546*36998Sbostic 	if (x == 263) mailbill();		exit();
547*36998Sbostic 	}
548*36998Sbostic 
549*36998Sbostic /*
550*36998Sbostic  *	diedsub(x) Subroutine to print out the line showing the player when he is killed
551*36998Sbostic  *		int x;
552*36998Sbostic  */
diedsub(x)553*36998Sbostic diedsub(x)
554*36998Sbostic int x;
555*36998Sbostic 	{
556*36998Sbostic 	register char ch,*mod;
557*36998Sbostic 	lprintf("Score: %d, Diff: %d,  %s ",(long)c[GOLD],(long)c[HARDGAME],logname);
558*36998Sbostic 	if (x < 256)
559*36998Sbostic 		{
560*36998Sbostic 		ch = *monster[x].name;
561*36998Sbostic 		if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
562*36998Sbostic 			mod="an";  else mod="a";
563*36998Sbostic 		lprintf("killed by %s %s",mod,monster[x].name);
564*36998Sbostic 		}
565*36998Sbostic 	else lprintf("%s",whydead[x - 256]);
566*36998Sbostic 	if (x != 263) lprintf(" on %s\n",levelname[level]);  else lprc('\n');
567*36998Sbostic 	}
568*36998Sbostic 
569*36998Sbostic /*
570*36998Sbostic  *	diedlog() 	Subroutine to read a log file and print it out in ascii format
571*36998Sbostic  */
diedlog()572*36998Sbostic diedlog()
573*36998Sbostic 	{
574*36998Sbostic 	register int n;
575*36998Sbostic 	register char *p;
576*36998Sbostic 	struct stat stbuf;
577*36998Sbostic 	lcreat((char*)0);
578*36998Sbostic 	if (lopen(logfile)<0)
579*36998Sbostic 		{
580*36998Sbostic 		lprintf("Can't locate log file <%s>\n",logfile);
581*36998Sbostic 		return;
582*36998Sbostic 		}
583*36998Sbostic 	if (fstat(fd,&stbuf) < 0)
584*36998Sbostic 		{
585*36998Sbostic 		lprintf("Can't  stat log file <%s>\n",logfile);
586*36998Sbostic 		return;
587*36998Sbostic 		}
588*36998Sbostic 	for (n=stbuf.st_size/sizeof(struct log_fmt); n>0; --n)
589*36998Sbostic 		{
590*36998Sbostic 		lrfill((char*)&logg,sizeof(struct log_fmt));
591*36998Sbostic 		p = ctime(&logg.diedtime); p[16]='\n'; p[17]=0;
592*36998Sbostic 		lprintf("Score: %d, Diff: %d,  %s %s on %d at %s",(long)(logg.score),(long)(logg.diff),logg.who,logg.what,(long)(logg.cavelev),p+4);
593*36998Sbostic #ifdef EXTRA
594*36998Sbostic 		if (logg.moves<=0) logg.moves=1;
595*36998Sbostic 		lprintf("  Experience Level: %d,  AC: %d,  HP: %d/%d,  Elapsed Time: %d minutes\n",(long)(logg.lev),(long)(logg.ac),(long)(logg.hp),(long)(logg.hpmax),(long)(logg.elapsedtime));
596*36998Sbostic 		lprintf("  CPU time used: %d seconds,  Machine usage: %d.%02d%%\n",(long)(logg.cputime),(long)(logg.usage/100),(long)(logg.usage%100));
597*36998Sbostic 		lprintf("  BYTES in: %d, out: %d, moves: %d, deaths: %d, spells cast: %d\n",(long)(logg.bytin),(long)(logg.bytout),(long)(logg.moves),(long)(logg.killed),(long)(logg.spused));
598*36998Sbostic 		lprintf("  out bytes per move: %d,  time per move: %d ms\n",(long)(logg.bytout/logg.moves),(long)((logg.cputime*1000)/logg.moves));
599*36998Sbostic #endif
600*36998Sbostic 		}
601*36998Sbostic 		lflush();  lrclose();  return;
602*36998Sbostic 	}
603*36998Sbostic 
604*36998Sbostic #ifndef UIDSCORE
605*36998Sbostic /*
606*36998Sbostic  *	getplid(name)		Function to get players id # from id file
607*36998Sbostic  *
608*36998Sbostic  *	Enter with the name of the players character in name.
609*36998Sbostic  *	Returns the id # of the players character, or -1 if failure.
610*36998Sbostic  *	This routine will try to find the name in the id file, if its not there,
611*36998Sbostic  *	it will try to make a new entry in the file.  Only returns -1 if can't
612*36998Sbostic  *	find him in the file, and can't make a new entry in the file.
613*36998Sbostic  *	Format of playerids file:
614*36998Sbostic  *			Id # in ascii     \n     character name     \n
615*36998Sbostic  */
616*36998Sbostic static int havepid= -1;	/* playerid # if previously done */
getplid(nam)617*36998Sbostic getplid(nam)
618*36998Sbostic 	char *nam;
619*36998Sbostic 	{
620*36998Sbostic 	int fd7,high=999,no;
621*36998Sbostic 	register char *p,*p2;
622*36998Sbostic 	char name[80];
623*36998Sbostic 	if (havepid != -1) return(havepid);	/* already did it */
624*36998Sbostic 	lflush();	/* flush any pending I/O */
625*36998Sbostic 	sprintf(name,"%s\n",nam);	/* append a \n to name */
626*36998Sbostic 	if (lopen(playerids) < 0)	/* no file, make it */
627*36998Sbostic 		{
628*36998Sbostic 		if ((fd7=creat(playerids,0666)) < 0)  return(-1); /* can't make it */
629*36998Sbostic 		close(fd7);  goto addone;	/* now append new playerid record to file */
630*36998Sbostic 		}
631*36998Sbostic 	for (;;)	/* now search for the name in the player id file */
632*36998Sbostic 		{
633*36998Sbostic 		p = lgetl();  if (p==NULL) break;	/* EOF? */
634*36998Sbostic 		no = atoi(p);	/* the id # */
635*36998Sbostic 		p2= lgetl();  if (p2==NULL) break;	/* EOF? */
636*36998Sbostic 		if (no>high) high=no;	/* accumulate highest id # */
637*36998Sbostic 		if (strcmp(p2,name)==0)	/* we found him */
638*36998Sbostic 			{
639*36998Sbostic 			return(no);	/* his id number */
640*36998Sbostic 			}
641*36998Sbostic 		}
642*36998Sbostic 	lrclose();
643*36998Sbostic 	/* if we get here, we didn't find him in the file -- put him there */
644*36998Sbostic addone:
645*36998Sbostic 	if (lappend(playerids) < 0) return(-1);	/* can't open file for append */
646*36998Sbostic 	lprintf("%d\n%s",(long)++high,name);  /* new id # and name */
647*36998Sbostic 	lwclose();
648*36998Sbostic 	lcreat((char*)0);	/* re-open terminal channel */
649*36998Sbostic 	return(high);
650*36998Sbostic 	}
651*36998Sbostic #endif UIDSCORE
652*36998Sbostic 
653