]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/scores.c
1 /* $NetBSD: scores.c,v 1.12 2004/02/13 11:36:08 wiz Exp $ */
4 * scores.c Larn is copyrighted 1986 by Noah Morgan.
6 * Functions in this file are:
8 * readboard() Function to read in the scoreboard into a static buffer
9 * writeboard() Function to write the scoreboard from readboard()'s buffer
10 * makeboard() Function to create a new scoreboard (wipe out old one)
11 * hashewon() Function to return 1 if player has won a game before, else 0
12 * long paytaxes(x) Function to pay taxes if any are due winshou()
13 * ubroutine to print out the winning scoreboard shou(x)
14 * ubroutine to print out the non-winners scoreboard showscores()
15 * unction to show the scoreboard on the terminal showallscores()
16 * Function to show scores and the iven lists that go with them sortboard()
17 * unction to sort the scoreboard newscore(score, whoo, whyded, winner)
18 * Function to add entry to scoreboard new1sub(score,i,whoo,taxes)
19 * Subroutine to put player into a new2sub(score,i,whoo,whyded)
20 * Subroutine to put player into a died(x) Subroutine to record who
21 * played larn, and what the score was diedsub(x) Subroutine to print out a
22 * line showing player when he is killed diedlog() Subroutine to read a
23 * log file and print it out in ascii format getplid(name)
24 * on to get players id # from id file
27 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: scores.c,v 1.12 2004/02/13 11:36:08 wiz Exp $");
31 #include <sys/types.h>
32 #include <sys/times.h>
41 struct scofmt
{ /* This is the structure for the scoreboard */
42 long score
; /* the score of the player */
43 long suid
; /* the user id number of the player */
44 short what
; /* the number of the monster that killed
46 short level
; /* the level player was on when he died */
47 short hardlev
;/* the level of difficulty player played at */
48 short order
; /* the relative ordering place of this entry */
49 char who
[40];/* the name of the character */
50 char sciv
[26][2]; /* this is the inventory list of the
53 struct wscofmt
{ /* This is the structure for the winning
55 long score
; /* the score of the player */
56 long timeused
; /* the time used in mobuls to win the
58 long taxes
; /* taxes he owes to LRS */
59 long suid
; /* the user id number of the player */
60 short hardlev
;/* the level of difficulty player played at */
61 short order
; /* the relative ordering place of this entry */
62 char who
[40];/* the name of the character */
65 struct log_fmt
{ /* 102 bytes struct for the log file */
66 long score
; /* the players score */
67 time_t diedtime
; /* time when game was over */
68 short cavelev
;/* level in caves */
69 short diff
; /* difficulty player played at */
71 long elapsedtime
; /* real time of game in seconds */
72 long bytout
; /* bytes input and output */
74 long moves
; /* number of moves made by player */
75 short ac
; /* armor class of player */
76 short hp
, hpmax
; /* players hitpoints */
77 short cputime
;/* CPU time needed in seconds */
78 short killed
, spused
; /* monsters killed and spells cast */
79 short usage
; /* usage of the CPU in % */
80 short lev
; /* player level */
82 char who
[12];/* player name */
83 char what
[46]; /* what happened to player */
86 static struct scofmt sco
[SCORESIZE
]; /* the structure for the scoreboard */
87 static struct wscofmt winr
[SCORESIZE
]; /* struct for the winning scoreboard */
88 static struct log_fmt logg
; /* structure for the log file */
89 static char *whydead
[] = {
90 "quit", "suspended", "self - annihilated", "shot by an arrow",
91 "hit by a dart", "fell into a pit", "fell into a bottomless pit",
92 "a winner", "trapped in solid rock", "killed by a missing save file",
93 "killed by an old save file", "caught by the greedy cheater checker trap",
94 "killed by a protected save file", "killed his family and committed suicide",
95 "erased by a wayward finger", "fell through a bottomless trap door",
96 "fell through a trap door", "drank some poisonous water",
97 "fried by an electric shock", "slipped on a volcano shaft",
98 "killed by a stupid act of frustration", "attacked by a revolting demon",
99 "hit by his own magic", "demolished by an unseen attacker",
100 "fell into the dreadful sleep", "killed by an exploding chest",
101 /* 26 */ "killed by a missing maze data file", "annihilated in a sphere",
102 "died a post mortem death", "wasted by a malloc() failure"
106 * readboard() Function to read in the scoreboard into a static buffer
108 * returns -1 if unable to read in the scoreboard, returns 0 if all is OK
116 i
= lopen(scorefile
);
119 lprcat("Can't read scoreboard\n");
123 lrfill((char *) sco
, sizeof(sco
));
124 lrfill((char *) winr
, sizeof(winr
));
131 * writeboard() Function to write the scoreboard from readboard()'s buffer
133 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
142 i
= lcreat(scorefile
);
145 lprcat("Can't write scoreboard\n");
149 lwrite((char *) sco
, sizeof(sco
));
150 lwrite((char *) winr
, sizeof(winr
));
157 * makeboard() Function to create a new scoreboard (wipe out old one)
159 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
165 for (i
= 0; i
< SCORESIZE
; i
++) {
166 winr
[i
].taxes
= winr
[i
].score
= sco
[i
].score
= 0;
167 winr
[i
].order
= sco
[i
].order
= i
;
172 chmod(scorefile
, 0660);
178 * hashewon() Function to return 1 if player has won a game before, else 0
180 * This function also sets c[HARDGAME] to appropriate value -- 0 if not a
181 * winner, otherwise the next level of difficulty listed in the winners
182 * scoreboard. This function also sets outstanding_taxes to the value in
183 * the winners scoreboard.
191 return (0); /* can't find scoreboard */
192 for (i
= 0; i
< SCORESIZE
; i
++) /* search through winners scoreboard */
193 if (winr
[i
].suid
== userid
)
194 if (winr
[i
].score
> 0) {
195 c
[HARDGAME
] = winr
[i
].hardlev
+ 1;
196 outstanding_taxes
= winr
[i
].taxes
;
203 * long paytaxes(x) Function to pay taxes if any are due
205 * Enter with the amount (in gp) to pay on the taxes.
206 * Returns amount actually paid.
218 for (i
= 0; i
< SCORESIZE
; i
++)
219 if (winr
[i
].suid
== userid
) /* look for players winning
221 if (winr
[i
].score
> 0) { /* search for a winning
222 * entry for the player */
225 amt
= x
; /* don't overpay taxes
227 winr
[i
].taxes
-= amt
;
228 outstanding_taxes
-= amt
;
229 if (writeboard() < 0)
233 return (0L); /* couldn't find user on winning scoreboard */
237 * winshou() Subroutine to print out the winning scoreboard
239 * Returns the number of players on scoreboard that were shown
246 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is there anyone on
248 if (winr
[i
].score
!= 0) {
253 lprcat("\n Score Difficulty Time Needed Larn Winners List\n");
255 for (i
= 0; i
< SCORESIZE
; i
++) /* this loop is needed to
257 for (j
= 0; j
< SCORESIZE
; j
++) { /* winners in order */
258 p
= &winr
[j
]; /* pointer to the scoreboard
263 lprintf("%10d %2d %5d Mobuls %s \n",
264 (long) p
->score
, (long) p
->hardlev
, (long) p
->timeused
, p
->who
);
270 return (count
); /* return number of people on scoreboard */
274 * shou(x) Subroutine to print out the non-winners scoreboard
277 * Enter with 0 to list the scores, enter with 1 to list inventories too
278 * Returns the number of players on scoreboard that were shown
286 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is the scoreboard
288 if (sco
[i
].score
!= 0) {
293 lprcat("\n Score Difficulty Larn Visitor Log\n");
294 for (i
= 0; i
< SCORESIZE
; i
++) /* be sure to print them out
296 for (j
= 0; j
< SCORESIZE
; j
++)
297 if (sco
[j
].order
== i
) {
300 lprintf("%10d %2d %s ",
301 (long) sco
[j
].score
, (long) sco
[j
].hardlev
, sco
[j
].who
);
302 if (sco
[j
].what
< 256)
303 lprintf("killed by a %s", monster
[sco
[j
].what
].name
);
305 lprintf("%s", whydead
[sco
[j
].what
- 256]);
307 lprintf(" on %s", levelname
[sco
[j
].level
]);
309 for (n
= 0; n
< 26; n
++) {
310 iven
[n
] = sco
[j
].sciv
[n
][0];
311 ivenarg
[n
] = sco
[j
].sciv
[n
][1];
313 for (k
= 1; k
< 99; k
++)
314 for (n
= 0; n
< 26; n
++)
326 return (count
); /* return the number of players just shown */
330 * showscores() Function to show the scoreboard on the terminal
332 * Returns nothing of value
334 static char esb
[] = "The scoreboard is empty.\n";
353 * showallscores() Function to show scores and the iven lists that go with them
355 * Returns nothing of value
365 c
[WEAR
] = c
[WIELD
] = c
[SHIELD
] = -1; /* not wielding or wearing
367 for (i
= 0; i
< MAXPOTION
; i
++)
368 potionname
[i
] = potionhide
[i
];
369 for (i
= 0; i
< MAXSCROLL
; i
++)
370 scrollname
[i
] = scrollhide
[i
];
381 * sortboard() Function to sort the scoreboard
383 * Returns 0 if no sorting done, else returns 1
390 for (i
= 0; i
< SCORESIZE
; i
++)
391 sco
[i
].order
= winr
[i
].order
= -1;
393 while (pos
< SCORESIZE
) {
395 for (i
= 0; i
< SCORESIZE
; i
++)
396 if ((sco
[i
].order
< 0) && (sco
[i
].score
>= jdat
)) {
400 sco
[j
].order
= pos
++;
403 while (pos
< SCORESIZE
) {
405 for (i
= 0; i
< SCORESIZE
; i
++)
406 if ((winr
[i
].order
< 0) && (winr
[i
].score
>= jdat
)) {
408 jdat
= winr
[i
].score
;
410 winr
[j
].order
= pos
++;
416 * newscore(score, whoo, whyded, winner) Function to add entry to scoreboard
417 * int score, winner, whyded;
420 * Enter with the total score in gp in score, players name in whoo,
421 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
422 * ex. newscore(1000, "player 1", 32, 0);
425 newscore(score
, whoo
, whyded
, winner
)
433 return; /* do the scoreboard */
434 /* if a winner then delete all non-winning scores */
436 winner
= 0; /* if he cheated, don't let him win */
438 for (i
= 0; i
< SCORESIZE
; i
++)
439 if (sco
[i
].suid
== userid
)
441 taxes
= score
* TAXRATE
;
442 score
+= 100000 * c
[HARDGAME
]; /* bonus for winning */
444 * if he has a slot on the winning scoreboard update it if
447 for (i
= 0; i
< SCORESIZE
; i
++)
448 if (winr
[i
].suid
== userid
) {
449 new1sub(score
, i
, whoo
, taxes
);
453 * he had no entry. look for last entry and see if he has a
456 for (i
= 0; i
< SCORESIZE
; i
++)
457 if (winr
[i
].order
== SCORESIZE
- 1) {
458 new1sub(score
, i
, whoo
, taxes
);
461 } else if (!cheat
) { /* for not winning scoreboard */
463 * if he has a slot on the scoreboard update it if greater
466 for (i
= 0; i
< SCORESIZE
; i
++)
467 if (sco
[i
].suid
== userid
) {
468 new2sub(score
, i
, whoo
, whyded
);
472 * he had no entry. look for last entry and see if he has a
475 for (i
= 0; i
< SCORESIZE
; i
++)
476 if (sco
[i
].order
== SCORESIZE
- 1) {
477 new2sub(score
, i
, whoo
, whyded
);
484 * new1sub(score,i,whoo,taxes) Subroutine to put player into a
485 * int score,i,whyded,taxes; winning scoreboard entry if his score
486 * char *whoo; is high enough
488 * Enter with the total score in gp in score, players name in whoo,
489 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
490 * slot in scoreboard in i, and the tax bill in taxes.
491 * Returns nothing of value
494 new1sub(score
, i
, whoo
, taxes
)
502 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
503 strcpy(p
->who
, whoo
);
505 p
->hardlev
= c
[HARDGAME
];
507 p
->timeused
= gltime
/ 100;
512 * new2sub(score,i,whoo,whyded) Subroutine to put player into a
513 * int score,i,whyded,taxes; non-winning scoreboard entry if his
514 * char *whoo; score is high enough
516 * Enter with the total score in gp in score, players name in whoo,
517 * died() reason # in whyded, and slot in scoreboard in i.
518 * Returns nothing of value
521 new2sub(score
, i
, whoo
, whyded
)
529 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
530 strcpy(p
->who
, whoo
);
533 p
->hardlev
= c
[HARDGAME
];
536 for (j
= 0; j
< 26; j
++) {
537 p
->sciv
[j
][0] = iven
[j
];
538 p
->sciv
[j
][1] = ivenarg
[j
];
544 * died(x) Subroutine to record who played larn, and what the score was
547 * if x < 0 then don't show scores
548 * died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
550 * < 256 killed by the monster number
553 * 258 self - annihilated
554 * 259 shot by an arrow
556 * 261 fell into a pit
557 * 262 fell into a bottomless pit
559 * 264 trapped in solid rock
560 * 265 killed by a missing save file
561 * 266 killed by an old save file
562 * 267 caught by the greedy cheater checker trap
563 * 268 killed by a protected save file
564 * 269 killed his family and killed himself
565 * 270 erased by a wayward finger
566 * 271 fell through a bottomless trap door
567 * 272 fell through a trap door
568 * 273 drank some poisonous water
569 * 274 fried by an electric shock
570 * 275 slipped on a volcano shaft
571 * 276 killed by a stupid act of frustration
572 * 277 attacked by a revolting demon
573 * 278 hit by his own magic
574 * 279 demolished by an unseen attacker
575 * 280 fell into the dreadful sleep
576 * 281 killed by an exploding chest
577 * 282 killed by a missing maze data file
578 * 283 killed by a sphere of annihilation
579 * 284 died a post mortem death
580 * 285 malloc() failure
581 * 300 quick quit -- don't put on scoreboard
584 static int scorerror
;
592 if (c
[LIFEPROT
] > 0) { /* if life protection */
593 switch ((x
> 0) ? x
: -x
) {
608 goto invalid
; /* can't be saved */
614 lprcat("\nYou feel wiiieeeeerrrrrd all over! ");
618 return; /* only case where died() returns */
625 unlink(ckpfile
);/* remove checkpoint file if used */
629 } /* if we are not to display the scores */
630 if ((x
== 300) || (x
== 257))
631 exit(0); /* for quick exit or saved game */
636 c
[GOLD
] += c
[BANKACCOUNT
];
638 /* now enter the player at the end of the scoreboard */
639 newscore(c
[GOLD
], logname
, x
, win
);
640 diedsub(x
); /* print out the score line */
644 if ((wizard
== 0) && (c
[GOLD
] > 0)) { /* wizards can't score */
647 if (lappend(logfile
) < 0) { /* append to file */
648 if (lcreat(logfile
) < 0) { /* and can't create new
651 lprcat("\nCan't open record file: I can't post your score.\n");
658 chmod(logfile
, 0660);
662 strcpy(logg
.who
, loginname
);
663 logg
.score
= c
[GOLD
];
664 logg
.diff
= c
[HARDGAME
];
666 ch
= *monster
[x
].name
;
667 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
671 snprintf(logg
.what
, sizeof(logg
.what
),
672 "killed by %s %s", mod
, monster
[x
].name
);
674 snprintf(logg
.what
, sizeof(logg
.what
),
675 "%s", whydead
[x
- 256]);
676 logg
.cavelev
= level
;
677 time(&zzz
); /* get CPU time -- write out score info */
680 times(&cputime
);/* get CPU time -- write out score info */
681 logg
.cputime
= i
= (cputime
.tms_utime
+ cputime
.tms_stime
) / 60 + c
[CPUTIME
];
684 logg
.hpmax
= c
[HPMAX
];
686 logg
.elapsedtime
= (zzz
- initialtime
+ 59) / 60;
687 logg
.usage
= (10000 * i
) / (zzz
- initialtime
);
688 logg
.bytin
= c
[BYTESIN
];
689 logg
.bytout
= c
[BYTESOUT
];
690 logg
.moves
= c
[MOVESMADE
];
691 logg
.spused
= c
[SPELLSCAST
];
692 logg
.killed
= c
[MONSTKILLED
];
694 lwrite((char *) &logg
, sizeof(struct log_fmt
));
699 * now for the scoreboard maintenance -- not for a suspended
704 scorerror
= writeboard();
707 if ((x
== 256) || (x
== 257) || (f
!= 0))
710 showscores(); /* if we updated the scoreboard */
717 * diedsub(x) Subroutine to print out the line showing the player when he is killed
725 lprintf("Score: %d, Diff: %d, %s ", (long) c
[GOLD
], (long) c
[HARDGAME
], logname
);
727 ch
= *monster
[x
].name
;
728 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
732 lprintf("killed by %s %s", mod
, monster
[x
].name
);
734 lprintf("%s", whydead
[x
- 256]);
736 lprintf(" on %s\n", levelname
[level
]);
742 * diedlog() Subroutine to read a log file and print it out in ascii format
751 if (lopen(logfile
) < 0) {
752 lprintf("Can't locate log file <%s>\n", logfile
);
755 if (fstat(fd
, &stbuf
) < 0) {
756 lprintf("Can't stat log file <%s>\n", logfile
);
759 for (n
= stbuf
.st_size
/ sizeof(struct log_fmt
); n
> 0; --n
) {
760 lrfill((char *) &logg
, sizeof(struct log_fmt
));
761 p
= ctime(&logg
.diedtime
);
764 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);
768 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
));
769 lprintf(" CPU time used: %d seconds, Machine usage: %d.%02d%%\n", (long) (logg
.cputime
), (long) (logg
.usage
/ 100), (long) (logg
.usage
% 100));
770 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
));
771 lprintf(" out bytes per move: %d, time per move: %d ms\n", (long) (logg
.bytout
/ logg
.moves
), (long) ((logg
.cputime
* 1000) / logg
.moves
));
781 * getplid(name) Function to get players id # from id file
783 * Enter with the name of the players character in name.
784 * Returns the id # of the players character, or -1 if failure.
785 * This routine will try to find the name in the id file, if its not there,
786 * it will try to make a new entry in the file. Only returns -1 if can't
787 * find him in the file, and can't make a new entry in the file.
788 * Format of playerids file:
789 * Id # in ascii \n character name \n
791 static int havepid
= -1; /* playerid # if previously done */
796 int fd7
, high
= 999, no
;
800 return (havepid
); /* already did it */
801 lflush(); /* flush any pending I/O */
802 snprintf(name
, sizeof(name
), "%s\n", nam
);/* append a \n to name */
803 if (lopen(playerids
) < 0) { /* no file, make it */
804 if ((fd7
= creat(playerids
, 0666)) < 0)
805 return (-1); /* can't make it */
807 goto addone
; /* now append new playerid record to file */
809 for (;;) { /* now search for the name in the player id
814 no
= atoi(p
); /* the id # */
819 high
= no
; /* accumulate highest id # */
820 if (strcmp(p2
, name
) == 0) { /* we found him */
821 return (no
); /* his id number */
825 /* if we get here, we didn't find him in the file -- put him there */
827 if (lappend(playerids
) < 0)
828 return (-1); /* can't open file for append */
829 lprintf("%d\n%s", (long) ++high
, name
); /* new id # and name */
831 lcreat((char *) 0); /* re-open terminal channel */
834 #endif /* UIDSCORE */