]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/scores.c
2 static char rcsid
[] = "$NetBSD: scores.c,v 1.4 1995/03/23 08:34:15 cgd Exp $";
5 /* scores.c Larn is copyrighted 1986 by Noah Morgan.
7 * Functions in this file are:
9 * readboard() Function to read in the scoreboard into a static buffer
10 * writeboard() Function to write the scoreboard from readboard()'s buffer
11 * makeboard() Function to create a new scoreboard (wipe out old one)
12 * hashewon() Function to return 1 if player has won a game before, else 0
13 * long paytaxes(x) Function to pay taxes if any are due
14 * winshou() Subroutine to print out the winning scoreboard
15 * shou(x) Subroutine to print out the non-winners scoreboard
16 * showscores() Function to show the scoreboard on the terminal
17 * showallscores() Function to show scores and the iven lists that go with them
18 * sortboard() Function to sort the scoreboard
19 * newscore(score, whoo, whyded, winner) Function to add entry to scoreboard
20 * new1sub(score,i,whoo,taxes) Subroutine to put player into a
21 * new2sub(score,i,whoo,whyded) Subroutine to put player into a
22 * died(x) Subroutine to record who played larn, and what the score was
23 * diedsub(x) Subroutine to print out a line showing player when he is killed
24 * diedlog() Subroutine to read a log file and print it out in ascii format
25 * getplid(name) Function to get players id # from id file
28 #include <sys/types.h>
29 #include <sys/times.h>
33 struct scofmt
/* This is the structure for the scoreboard */
35 long score
; /* the score of the player */
36 long suid
; /* the user id number of the player */
37 short what
; /* the number of the monster that killed player */
38 short level
; /* the level player was on when he died */
39 short hardlev
; /* the level of difficulty player played at */
40 short order
; /* the relative ordering place of this entry */
41 char who
[40]; /* the name of the character */
42 char sciv
[26][2]; /* this is the inventory list of the character */
44 struct wscofmt
/* This is the structure for the winning scoreboard */
46 long score
; /* the score of the player */
47 long timeused
; /* the time used in mobuls to win the game */
48 long taxes
; /* taxes he owes to LRS */
49 long suid
; /* the user id number of the player */
50 short hardlev
; /* the level of difficulty player played at */
51 short order
; /* the relative ordering place of this entry */
52 char who
[40]; /* the name of the character */
55 struct log_fmt
/* 102 bytes struct for the log file */
57 long score
; /* the players score */
58 long diedtime
; /* time when game was over */
59 short cavelev
; /* level in caves */
60 short diff
; /* difficulty player played at */
62 long elapsedtime
; /* real time of game in seconds */
63 long bytout
; /* bytes input and output */
65 long moves
; /* number of moves made by player */
66 short ac
; /* armor class of player */
67 short hp
,hpmax
; /* players hitpoints */
68 short cputime
; /* cpu time needed in seconds */
69 short killed
,spused
;/* monsters killed and spells cast */
70 short usage
; /* usage of the cpu in % */
71 short lev
; /* player level */
73 char who
[12]; /* player name */
74 char what
[46]; /* what happened to player */
77 static struct scofmt sco
[SCORESIZE
]; /* the structure for the scoreboard */
78 static struct wscofmt winr
[SCORESIZE
]; /* struct for the winning scoreboard */
79 static struct log_fmt logg
; /* structure for the log file */
80 static char *whydead
[] = {
81 "quit", "suspended", "self - annihilated", "shot by an arrow",
82 "hit by a dart", "fell into a pit", "fell into a bottomless pit",
83 "a winner", "trapped in solid rock", "killed by a missing save file",
84 "killed by an old save file", "caught by the greedy cheater checker trap",
85 "killed by a protected save file","killed his family and committed suicide",
86 "erased by a wayward finger", "fell through a bottomless trap door",
87 "fell through a trap door", "drank some poisonous water",
88 "fried by an electric shock", "slipped on a volcano shaft",
89 "killed by a stupid act of frustration", "attacked by a revolting demon",
90 "hit by his own magic", "demolished by an unseen attacker",
91 "fell into the dreadful sleep", "killed by an exploding chest",
92 /*26*/ "killed by a missing maze data file", "annihilated in a sphere",
93 "died a post mortem death","wasted by a malloc() failure"
97 * readboard() Function to read in the scoreboard into a static buffer
99 * returns -1 if unable to read in the scoreboard, returns 0 if all is OK
103 if (lopen(scorefile
)<0)
104 { lprcat("Can't read scoreboard\n"); lflush(); return(-1); }
105 lrfill((char*)sco
,sizeof(sco
)); lrfill((char*)winr
,sizeof(winr
));
106 lrclose(); lcreat((char*)0); return(0);
110 * writeboard() Function to write the scoreboard from readboard()'s buffer
112 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
117 if (lcreat(scorefile
)<0)
118 { lprcat("Can't write scoreboard\n"); lflush(); return(-1); }
119 lwrite((char*)sco
,sizeof(sco
)); lwrite((char*)winr
,sizeof(winr
));
120 lwclose(); lcreat((char*)0); return(0);
124 * makeboard() Function to create a new scoreboard (wipe out old one)
126 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
131 for (i
=0; i
<SCORESIZE
; i
++)
133 winr
[i
].taxes
= winr
[i
].score
= sco
[i
].score
= 0;
134 winr
[i
].order
= sco
[i
].order
= i
;
136 if (writeboard()) return(-1);
137 chmod(scorefile
,0660);
142 * hashewon() Function to return 1 if player has won a game before, else 0
144 * This function also sets c[HARDGAME] to appropriate value -- 0 if not a
145 * winner, otherwise the next level of difficulty listed in the winners
146 * scoreboard. This function also sets outstanding_taxes to the value in
147 * the winners scoreboard.
153 if (readboard() < 0) return(0); /* can't find scoreboard */
154 for (i
=0; i
<SCORESIZE
; i
++) /* search through winners scoreboard */
155 if (winr
[i
].suid
== userid
)
156 if (winr
[i
].score
> 0)
158 c
[HARDGAME
]=winr
[i
].hardlev
+1; outstanding_taxes
=winr
[i
].taxes
;
165 * long paytaxes(x) Function to pay taxes if any are due
167 * Enter with the amount (in gp) to pay on the taxes.
168 * Returns amount actually paid.
176 if (readboard()<0) return(0L);
177 for (i
=0; i
<SCORESIZE
; i
++)
178 if (winr
[i
].suid
== userid
) /* look for players winning entry */
179 if (winr
[i
].score
>0) /* search for a winning entry for the player */
182 if (x
< amt
) amt
=x
; /* don't overpay taxes (Ughhhhh) */
183 winr
[i
].taxes
-= amt
;
184 outstanding_taxes
-= amt
;
185 if (writeboard()<0) return(0);
188 return(0L); /* couldn't find user on winning scoreboard */
192 * winshou() Subroutine to print out the winning scoreboard
194 * Returns the number of players on scoreboard that were shown
198 register struct wscofmt
*p
;
199 register int i
,j
,count
;
200 for (count
=j
=i
=0; i
<SCORESIZE
; i
++) /* is there anyone on the scoreboard? */
201 if (winr
[i
].score
!= 0)
205 lprcat("\n Score Difficulty Time Needed Larn Winners List\n");
207 for (i
=0; i
<SCORESIZE
; i
++) /* this loop is needed to print out the */
208 for (j
=0; j
<SCORESIZE
; j
++) /* winners in order */
210 p
= &winr
[j
]; /* pointer to the scoreboard entry */
216 lprintf("%10d %2d %5d Mobuls %s \n",
217 (long)p
->score
,(long)p
->hardlev
,(long)p
->timeused
,p
->who
);
223 return(count
); /* return number of people on scoreboard */
227 * shou(x) Subroutine to print out the non-winners scoreboard
230 * Enter with 0 to list the scores, enter with 1 to list inventories too
231 * Returns the number of players on scoreboard that were shown
236 register int i
,j
,n
,k
;
238 for (count
=j
=i
=0; i
<SCORESIZE
; i
++) /* is the scoreboard empty? */
239 if (sco
[i
].score
!= 0)
243 lprcat("\n Score Difficulty Larn Visitor Log\n");
244 for (i
=0; i
<SCORESIZE
; i
++) /* be sure to print them out in order */
245 for (j
=0; j
<SCORESIZE
; j
++)
246 if (sco
[j
].order
== i
)
251 lprintf("%10d %2d %s ",
252 (long)sco
[j
].score
,(long)sco
[j
].hardlev
,sco
[j
].who
);
253 if (sco
[j
].what
< 256) lprintf("killed by a %s",monster
[sco
[j
].what
].name
);
254 else lprintf("%s",whydead
[sco
[j
].what
- 256]);
255 if (x
!= 263) lprintf(" on %s",levelname
[sco
[j
].level
]);
258 for (n
=0; n
<26; n
++) { iven
[n
]=sco
[j
].sciv
[n
][0]; ivenarg
[n
]=sco
[j
].sciv
[n
][1]; }
261 if (k
==iven
[n
]) { srcount
=0; show3(n
); }
269 return(count
); /* return the number of players just shown */
273 * showscores() Function to show the scoreboard on the terminal
275 * Returns nothing of value
277 static char esb
[] = "The scoreboard is empty.\n";
281 lflush(); lcreat((char*)0); if (readboard()<0) return;
282 i
=winshou(); j
=shou(0);
283 if (i
+j
== 0) lprcat(esb
); else lprc('\n');
288 * showallscores() Function to show scores and the iven lists that go with them
290 * Returns nothing of value
295 lflush(); lcreat((char*)0); if (readboard()<0) return;
296 c
[WEAR
] = c
[WIELD
] = c
[SHIELD
] = -1; /* not wielding or wearing anything */
297 for (i
=0; i
<MAXPOTION
; i
++) potionname
[i
]=potionhide
[i
];
298 for (i
=0; i
<MAXSCROLL
; i
++) scrollname
[i
]=scrollhide
[i
];
299 i
=winshou(); j
=shou(1);
300 if (i
+j
==0) lprcat(esb
); else lprc('\n');
305 * sortboard() Function to sort the scoreboard
307 * Returns 0 if no sorting done, else returns 1
311 register int i
,j
,pos
;
313 for (i
=0; i
<SCORESIZE
; i
++) sco
[i
].order
= winr
[i
].order
= -1;
314 pos
=0; while (pos
< SCORESIZE
)
317 for (i
=0; i
<SCORESIZE
; i
++)
318 if ((sco
[i
].order
< 0) && (sco
[i
].score
>= jdat
))
319 { j
=i
; jdat
=sco
[i
].score
; }
320 sco
[j
].order
= pos
++;
322 pos
=0; while (pos
< SCORESIZE
)
325 for (i
=0; i
<SCORESIZE
; i
++)
326 if ((winr
[i
].order
< 0) && (winr
[i
].score
>= jdat
))
327 { j
=i
; jdat
=winr
[i
].score
; }
328 winr
[j
].order
= pos
++;
334 * newscore(score, whoo, whyded, winner) Function to add entry to scoreboard
335 * int score, winner, whyded;
338 * Enter with the total score in gp in score, players name in whoo,
339 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
340 * ex. newscore(1000, "player 1", 32, 0);
342 newscore(score
, whoo
, whyded
, winner
)
349 if (readboard() < 0) return; /* do the scoreboard */
350 /* if a winner then delete all non-winning scores */
351 if (cheat
) winner
=0; /* if he cheated, don't let him win */
354 for (i
=0; i
<SCORESIZE
; i
++) if (sco
[i
].suid
== userid
) sco
[i
].score
=0;
355 taxes
= score
*TAXRATE
;
356 score
+= 100000*c
[HARDGAME
]; /* bonus for winning */
357 /* if he has a slot on the winning scoreboard update it if greater score */
358 for (i
=0; i
<SCORESIZE
; i
++) if (winr
[i
].suid
== userid
)
359 { new1sub(score
,i
,whoo
,taxes
); return; }
360 /* he had no entry. look for last entry and see if he has a greater score */
361 for (i
=0; i
<SCORESIZE
; i
++) if (winr
[i
].order
== SCORESIZE
-1)
362 { new1sub(score
,i
,whoo
,taxes
); return; }
364 else if (!cheat
) /* for not winning scoreboard */
366 /* if he has a slot on the scoreboard update it if greater score */
367 for (i
=0; i
<SCORESIZE
; i
++) if (sco
[i
].suid
== userid
)
368 { new2sub(score
,i
,whoo
,whyded
); return; }
369 /* he had no entry. look for last entry and see if he has a greater score */
370 for (i
=0; i
<SCORESIZE
; i
++) if (sco
[i
].order
== SCORESIZE
-1)
371 { new2sub(score
,i
,whoo
,whyded
); return; }
376 * new1sub(score,i,whoo,taxes) Subroutine to put player into a
377 * int score,i,whyded,taxes; winning scoreboard entry if his score
378 * char *whoo; is high enough
380 * Enter with the total score in gp in score, players name in whoo,
381 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
382 * slot in scoreboard in i, and the tax bill in taxes.
383 * Returns nothing of value
385 new1sub(score
,i
,whoo
,taxes
)
390 register struct wscofmt
*p
;
393 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
))
395 strcpy(p
->who
,whoo
); p
->score
=score
;
396 p
->hardlev
=c
[HARDGAME
]; p
->suid
=userid
;
397 p
->timeused
=gtime
/100;
402 * new2sub(score,i,whoo,whyded) Subroutine to put player into a
403 * int score,i,whyded,taxes; non-winning scoreboard entry if his
404 * char *whoo; score is high enough
406 * Enter with the total score in gp in score, players name in whoo,
407 * died() reason # in whyded, and slot in scoreboard in i.
408 * Returns nothing of value
410 new2sub(score
,i
,whoo
,whyded
)
416 register struct scofmt
*p
;
418 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
))
420 strcpy(p
->who
,whoo
); p
->score
=score
;
421 p
->what
=whyded
; p
->hardlev
=c
[HARDGAME
];
422 p
->suid
=userid
; p
->level
=level
;
424 { p
->sciv
[j
][0]=iven
[j
]; p
->sciv
[j
][1]=ivenarg
[j
]; }
429 * died(x) Subroutine to record who played larn, and what the score was
432 * if x < 0 then don't show scores
433 * died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
435 * < 256 killed by the monster number
438 * 258 self - annihilated
439 * 259 shot by an arrow
441 * 261 fell into a pit
442 * 262 fell into a bottomless pit
444 * 264 trapped in solid rock
445 * 265 killed by a missing save file
446 * 266 killed by an old save file
447 * 267 caught by the greedy cheater checker trap
448 * 268 killed by a protected save file
449 * 269 killed his family and killed himself
450 * 270 erased by a wayward finger
451 * 271 fell through a bottomless trap door
452 * 272 fell through a trap door
453 * 273 drank some poisonous water
454 * 274 fried by an electric shock
455 * 275 slipped on a volcano shaft
456 * 276 killed by a stupid act of frustration
457 * 277 attacked by a revolting demon
458 * 278 hit by his own magic
459 * 279 demolished by an unseen attacker
460 * 280 fell into the dreadful sleep
461 * 281 killed by an exploding chest
462 * 282 killed by a missing maze data file
463 * 283 killed by a sphere of annihilation
464 * 284 died a post mortem death
465 * 285 malloc() failure
466 * 300 quick quit -- don't put on scoreboard
469 static int scorerror
;
477 if (c
[LIFEPROT
]>0) /* if life protection */
479 switch((x
>0) ? x
: -x
)
481 case 256: case 257: case 262: case 263: case 265: case 266:
482 case 267: case 268: case 269: case 271: case 282: case 284:
483 case 285: case 300: goto invalid
; /* can't be saved */
485 --c
[LIFEPROT
]; c
[HP
]=1; --c
[CONSTITUTION
];
486 cursors(); lprcat("\nYou feel wiiieeeeerrrrrd all over! "); beep();
488 return; /* only case where died() returns */
491 clearvt100(); lflush(); f
=0;
492 if (ckpflag
) unlink(ckpfile
); /* remove checkpoint file if used */
493 if (x
<0) { f
++; x
= -x
; } /* if we are not to display the scores */
494 if ((x
== 300) || (x
== 257)) exit(); /* for quick exit or saved game */
495 if (x
== 263) win
= 1; else win
= 0;
496 c
[GOLD
] += c
[BANKACCOUNT
]; c
[BANKACCOUNT
] = 0;
497 /* now enter the player at the end of the scoreboard */
498 newscore(c
[GOLD
], logname
, x
, win
);
499 diedsub(x
); /* print out the score line */ lflush();
502 if ((wizard
== 0) && (c
[GOLD
] > 0)) /* wizards can't score */
505 if (lappend(logfile
)<0) /* append to file */
507 if (lcreat(logfile
)<0) /* and can't create new log file */
510 lprcat("\nCan't open record file: I can't post your score.\n");
511 sncbr(); resetscroll(); lflush(); exit();
515 strcpy(logg
.who
,loginname
);
516 logg
.score
= c
[GOLD
]; logg
.diff
= c
[HARDGAME
];
519 ch
= *monster
[x
].name
;
520 if (ch
=='a' || ch
=='e' || ch
=='i' || ch
=='o' || ch
=='u')
521 mod
="an"; else mod
="a";
522 sprintf(logg
.what
,"killed by %s %s",mod
,monster
[x
].name
);
524 else sprintf(logg
.what
,"%s",whydead
[x
- 256]);
526 time(&zzz
); /* get cpu time -- write out score info */
529 times(&cputime
); /* get cpu time -- write out score info */
530 logg
.cputime
= i
= (cputime
.tms_utime
+ cputime
.tms_stime
)/60 + c
[CPUTIME
];
531 logg
.lev
=c
[LEVEL
]; logg
.ac
=c
[AC
];
532 logg
.hpmax
=c
[HPMAX
]; logg
.hp
=c
[HP
];
533 logg
.elapsedtime
=(zzz
-initialtime
+59)/60;
534 logg
.usage
=(10000*i
)/(zzz
-initialtime
);
535 logg
.bytin
=c
[BYTESIN
]; logg
.bytout
=c
[BYTESOUT
];
536 logg
.moves
=c
[MOVESMADE
]; logg
.spused
=c
[SPELLSCAST
];
537 logg
.killed
=c
[MONSTKILLED
];
539 lwrite((char*)&logg
,sizeof(struct log_fmt
)); lwclose();
542 /* now for the scoreboard maintenance -- not for a suspended game */
545 if (sortboard()) scorerror
= writeboard();
548 if ((x
==256) || (x
==257) || (f
!= 0)) exit();
549 if (scorerror
== 0) showscores(); /* if we updated the scoreboard */
550 if (x
== 263) mailbill(); exit();
554 * diedsub(x) Subroutine to print out the line showing the player when he is killed
560 register char ch
,*mod
;
561 lprintf("Score: %d, Diff: %d, %s ",(long)c
[GOLD
],(long)c
[HARDGAME
],logname
);
564 ch
= *monster
[x
].name
;
565 if (ch
=='a' || ch
=='e' || ch
=='i' || ch
=='o' || ch
=='u')
566 mod
="an"; else mod
="a";
567 lprintf("killed by %s %s",mod
,monster
[x
].name
);
569 else lprintf("%s",whydead
[x
- 256]);
570 if (x
!= 263) lprintf(" on %s\n",levelname
[level
]); else lprc('\n');
574 * diedlog() Subroutine to read a log file and print it out in ascii format
582 if (lopen(logfile
)<0)
584 lprintf("Can't locate log file <%s>\n",logfile
);
587 if (fstat(fd
,&stbuf
) < 0)
589 lprintf("Can't stat log file <%s>\n",logfile
);
592 for (n
=stbuf
.st_size
/sizeof(struct log_fmt
); n
>0; --n
)
594 lrfill((char*)&logg
,sizeof(struct log_fmt
));
595 p
= ctime(&logg
.diedtime
); p
[16]='\n'; p
[17]=0;
596 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);
598 if (logg
.moves
<=0) logg
.moves
=1;
599 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
));
600 lprintf(" CPU time used: %d seconds, Machine usage: %d.%02d%%\n",(long)(logg
.cputime
),(long)(logg
.usage
/100),(long)(logg
.usage%100
));
601 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
));
602 lprintf(" out bytes per move: %d, time per move: %d ms\n",(long)(logg
.bytout
/logg
.moves
),(long)((logg
.cputime
*1000)/logg
.moves
));
605 lflush(); lrclose(); return;
610 * getplid(name) Function to get players id # from id file
612 * Enter with the name of the players character in name.
613 * Returns the id # of the players character, or -1 if failure.
614 * This routine will try to find the name in the id file, if its not there,
615 * it will try to make a new entry in the file. Only returns -1 if can't
616 * find him in the file, and can't make a new entry in the file.
617 * Format of playerids file:
618 * Id # in ascii \n character name \n
620 static int havepid
= -1; /* playerid # if previously done */
625 register char *p
,*p2
;
627 if (havepid
!= -1) return(havepid
); /* already did it */
628 lflush(); /* flush any pending I/O */
629 sprintf(name
,"%s\n",nam
); /* append a \n to name */
630 if (lopen(playerids
) < 0) /* no file, make it */
632 if ((fd7
=creat(playerids
,0666)) < 0) return(-1); /* can't make it */
633 close(fd7
); goto addone
; /* now append new playerid record to file */
635 for (;;) /* now search for the name in the player id file */
637 p
= lgetl(); if (p
==NULL
) break; /* EOF? */
638 no
= atoi(p
); /* the id # */
639 p2
= lgetl(); if (p2
==NULL
) break; /* EOF? */
640 if (no
>high
) high
=no
; /* accumulate highest id # */
641 if (strcmp(p2
,name
)==0) /* we found him */
643 return(no
); /* his id number */
647 /* if we get here, we didn't find him in the file -- put him there */
649 if (lappend(playerids
) < 0) return(-1); /* can't open file for append */
650 lprintf("%d\n%s",(long)++high
,name
); /* new id # and name */
652 lcreat((char*)0); /* re-open terminal channel */