]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/scores.c
1 /* $NetBSD: scores.c,v 1.21 2012/06/19 05:30:44 dholland 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.21 2012/06/19 05:30:44 dholland 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 int32_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 const 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"
105 static int readboard(void);
106 static int writeboard(void);
107 static int winshou(void);
108 static int shou(int);
109 static int sortboard(void);
110 static void newscore(long, char *, int, int);
111 static void new1sub(long, int, char *, long);
112 static void new2sub(long, int, char *, int);
113 static void diedsub(int);
116 * readboard() Function to read in the scoreboard into a static buffer
118 * returns -1 if unable to read in the scoreboard, returns 0 if all is OK
127 i
= lopen(scorefile
);
131 lprcat("Can't read scoreboard\n");
135 lrfill((char *) sco
, sizeof(sco
));
136 lrfill((char *) winr
, sizeof(winr
));
143 * writeboard() Function to write the scoreboard from readboard()'s buffer
145 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
155 i
= lcreat(scorefile
);
159 lprcat("Can't write scoreboard\n");
163 lwrite((char *) sco
, sizeof(sco
));
164 lwrite((char *) winr
, sizeof(winr
));
171 * makeboard() Function to create a new scoreboard (wipe out old one)
173 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
180 for (i
= 0; i
< SCORESIZE
; i
++) {
181 winr
[i
].taxes
= winr
[i
].score
= sco
[i
].score
= 0;
182 winr
[i
].order
= sco
[i
].order
= i
;
188 chmod(scorefile
, 0660);
195 * hashewon() Function to return 1 if player has won a game before, else 0
197 * This function also sets c[HARDGAME] to appropriate value -- 0 if not a
198 * winner, otherwise the next level of difficulty listed in the winners
199 * scoreboard. This function also sets outstanding_taxes to the value in
200 * the winners scoreboard.
208 return (0); /* can't find scoreboard */
209 for (i
= 0; i
< SCORESIZE
; i
++) /* search through winners scoreboard */
210 if (winr
[i
].suid
== userid
)
211 if (winr
[i
].score
> 0) {
212 c
[HARDGAME
] = winr
[i
].hardlev
+ 1;
213 outstanding_taxes
= winr
[i
].taxes
;
220 * long paytaxes(x) Function to pay taxes if any are due
222 * Enter with the amount (in gp) to pay on the taxes.
223 * Returns amount actually paid.
234 for (i
= 0; i
< SCORESIZE
; i
++)
235 if (winr
[i
].suid
== userid
) /* look for players winning
237 if (winr
[i
].score
> 0) { /* search for a winning
238 * entry for the player */
241 amt
= x
; /* don't overpay taxes
243 winr
[i
].taxes
-= amt
;
244 outstanding_taxes
-= amt
;
246 if (writeboard() < 0)
250 return (0L); /* couldn't find user on winning scoreboard */
254 * winshou() Subroutine to print out the winning scoreboard
256 * Returns the number of players on scoreboard that were shown
263 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is there anyone on
265 if (winr
[i
].score
!= 0) {
270 lprcat("\n Score Difficulty Time Needed Larn Winners List\n");
272 for (i
= 0; i
< SCORESIZE
; i
++) /* this loop is needed to
274 for (j
= 0; j
< SCORESIZE
; j
++) { /* winners in order */
275 p
= &winr
[j
]; /* pointer to the scoreboard
280 lprintf("%10ld %2ld %5ld Mobuls %s \n",
281 (long) p
->score
, (long) p
->hardlev
, (long) p
->timeused
, p
->who
);
287 return (count
); /* return number of people on scoreboard */
291 * shou(x) Subroutine to print out the non-winners scoreboard
294 * Enter with 0 to list the scores, enter with 1 to list inventories too
295 * Returns the number of players on scoreboard that were shown
302 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is the scoreboard
304 if (sco
[i
].score
!= 0) {
309 lprcat("\n Score Difficulty Larn Visitor Log\n");
310 for (i
= 0; i
< SCORESIZE
; i
++) /* be sure to print them out
312 for (j
= 0; j
< SCORESIZE
; j
++)
313 if (sco
[j
].order
== i
) {
316 lprintf("%10ld %2ld %s ",
317 (long) sco
[j
].score
, (long) sco
[j
].hardlev
, sco
[j
].who
);
318 if (sco
[j
].what
< 256)
319 lprintf("killed by a %s", monster
[sco
[j
].what
].name
);
321 lprintf("%s", whydead
[sco
[j
].what
- 256]);
323 lprintf(" on %s", levelname
[sco
[j
].level
]);
325 for (n
= 0; n
< 26; n
++) {
326 iven
[n
] = sco
[j
].sciv
[n
][0];
327 ivenarg
[n
] = sco
[j
].sciv
[n
][1];
329 for (k
= 1; k
< 99; k
++)
330 for (n
= 0; n
< 26; n
++)
342 return (count
); /* return the number of players just shown */
346 * showscores() Function to show the scoreboard on the terminal
348 * Returns nothing of value
350 static char esb
[] = "The scoreboard is empty.\n";
369 * showallscores() Function to show scores and the iven lists that go with them
371 * Returns nothing of value
381 c
[WEAR
] = c
[WIELD
] = c
[SHIELD
] = -1; /* not wielding or wearing
383 for (i
= 0; i
< MAXPOTION
; i
++)
384 potionname
[i
] = potionhide
[i
];
385 for (i
= 0; i
< MAXSCROLL
; i
++)
386 scrollname
[i
] = scrollhide
[i
];
397 * sortboard() Function to sort the scoreboard
399 * Returns 0 if no sorting done, else returns 1
406 for (i
= 0; i
< SCORESIZE
; i
++)
407 sco
[i
].order
= winr
[i
].order
= -1;
409 while (pos
< SCORESIZE
) {
411 for (i
= 0; i
< SCORESIZE
; i
++)
412 if ((sco
[i
].order
< 0) && (sco
[i
].score
>= jdat
)) {
416 sco
[j
].order
= pos
++;
419 while (pos
< SCORESIZE
) {
421 for (i
= 0; i
< SCORESIZE
; i
++)
422 if ((winr
[i
].order
< 0) && (winr
[i
].score
>= jdat
)) {
424 jdat
= winr
[i
].score
;
426 winr
[j
].order
= pos
++;
432 * newscore(score, whoo, whyded, winner) Function to add entry to scoreboard
433 * int score, winner, whyded;
436 * Enter with the total score in gp in score, players name in whoo,
437 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
438 * ex. newscore(1000, "player 1", 32, 0);
441 newscore(long score
, char *whoo
, int whyded
, int winner
)
446 return; /* do the scoreboard */
447 /* if a winner then delete all non-winning scores */
449 winner
= 0; /* if he cheated, don't let him win */
451 for (i
= 0; i
< SCORESIZE
; i
++)
452 if (sco
[i
].suid
== userid
)
454 taxes
= score
* TAXRATE
;
455 score
+= 100000 * c
[HARDGAME
]; /* bonus for winning */
457 * if he has a slot on the winning scoreboard update it if
460 for (i
= 0; i
< SCORESIZE
; i
++)
461 if (winr
[i
].suid
== userid
) {
462 new1sub(score
, i
, whoo
, taxes
);
466 * he had no entry. look for last entry and see if he has a
469 for (i
= 0; i
< SCORESIZE
; i
++)
470 if (winr
[i
].order
== SCORESIZE
- 1) {
471 new1sub(score
, i
, whoo
, taxes
);
474 } else if (!cheat
) { /* for not winning scoreboard */
476 * if he has a slot on the scoreboard update it if greater
479 for (i
= 0; i
< SCORESIZE
; i
++)
480 if (sco
[i
].suid
== userid
) {
481 new2sub(score
, i
, whoo
, whyded
);
485 * he had no entry. look for last entry and see if he has a
488 for (i
= 0; i
< SCORESIZE
; i
++)
489 if (sco
[i
].order
== SCORESIZE
- 1) {
490 new2sub(score
, i
, whoo
, whyded
);
497 * new1sub(score,i,whoo,taxes) Subroutine to put player into a
498 * int score,i,whyded,taxes; winning scoreboard entry if his score
499 * char *whoo; is high enough
501 * Enter with the total score in gp in score, players name in whoo,
502 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
503 * slot in scoreboard in i, and the tax bill in taxes.
504 * Returns nothing of value
507 new1sub(long score
, int i
, char *whoo
, long taxes
)
512 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
513 strcpy(p
->who
, whoo
);
515 p
->hardlev
= c
[HARDGAME
];
517 p
->timeused
= gltime
/ 100;
522 * new2sub(score,i,whoo,whyded) Subroutine to put player into a
523 * int score,i,whyded,taxes; non-winning scoreboard entry if his
524 * char *whoo; score is high enough
526 * Enter with the total score in gp in score, players name in whoo,
527 * died() reason # in whyded, and slot in scoreboard in i.
528 * Returns nothing of value
531 new2sub(long score
, int i
, char *whoo
, int whyded
)
536 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
537 strcpy(p
->who
, whoo
);
540 p
->hardlev
= c
[HARDGAME
];
543 for (j
= 0; j
< 26; j
++) {
544 p
->sciv
[j
][0] = iven
[j
];
545 p
->sciv
[j
][1] = ivenarg
[j
];
551 * died(x) Subroutine to record who played larn, and what the score was
554 * if x < 0 then don't show scores
555 * died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
557 * < 256 killed by the monster number
560 * 258 self - annihilated
561 * 259 shot by an arrow
563 * 261 fell into a pit
564 * 262 fell into a bottomless pit
566 * 264 trapped in solid rock
567 * 265 killed by a missing save file
568 * 266 killed by an old save file
569 * 267 caught by the greedy cheater checker trap
570 * 268 killed by a protected save file
571 * 269 killed his family and killed himself
572 * 270 erased by a wayward finger
573 * 271 fell through a bottomless trap door
574 * 272 fell through a trap door
575 * 273 drank some poisonous water
576 * 274 fried by an electric shock
577 * 275 slipped on a volcano shaft
578 * 276 killed by a stupid act of frustration
579 * 277 attacked by a revolting demon
580 * 278 hit by his own magic
581 * 279 demolished by an unseen attacker
582 * 280 fell into the dreadful sleep
583 * 281 killed by an exploding chest
584 * 282 killed by a missing maze data file
585 * 283 killed by a sphere of annihilation
586 * 284 died a post mortem death
587 * 285 malloc() failure
588 * 300 quick quit -- don't put on scoreboard
591 static int scorerror
;
599 if (c
[LIFEPROT
] > 0) { /* if life protection */
600 switch ((x
> 0) ? x
: -x
) {
615 goto invalid
; /* can't be saved */
621 lprcat("\nYou feel wiiieeeeerrrrrd all over! ");
625 return; /* only case where died() returns */
632 unlink(ckpfile
);/* remove checkpoint file if used */
636 } /* if we are not to display the scores */
637 if ((x
== 300) || (x
== 257))
638 exit(0); /* for quick exit or saved game */
643 c
[GOLD
] += c
[BANKACCOUNT
];
645 /* now enter the player at the end of the scoreboard */
646 newscore(c
[GOLD
], logname
, x
, win
);
647 diedsub(x
); /* print out the score line */
651 if ((wizard
== 0) && (c
[GOLD
] > 0)) { /* wizards can't score */
655 if (lappend(logfile
) < 0) { /* append to file */
656 if (lcreat(logfile
) < 0) { /* and can't create new
659 lprcat("\nCan't open record file: I can't post your score.\n");
667 chmod(logfile
, 0660);
673 strcpy(logg
.who
, loginname
);
674 logg
.score
= c
[GOLD
];
675 logg
.diff
= c
[HARDGAME
];
677 ch
= *monster
[x
].name
;
678 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
682 snprintf(logg
.what
, sizeof(logg
.what
),
683 "killed by %s %s", mod
, monster
[x
].name
);
685 snprintf(logg
.what
, sizeof(logg
.what
),
686 "%s", whydead
[x
- 256]);
687 logg
.cavelev
= level
;
688 time(&zzz
); /* get CPU time -- write out score info */
691 times(&cputime
);/* get CPU time -- write out score info */
692 logg
.cputime
= i
= (cputime
.tms_utime
+ cputime
.tms_stime
) / 60 + c
[CPUTIME
];
695 logg
.hpmax
= c
[HPMAX
];
697 logg
.elapsedtime
= (zzz
- initialtime
+ 59) / 60;
698 logg
.usage
= (10000 * i
) / (zzz
- initialtime
);
699 logg
.bytin
= c
[BYTESIN
];
700 logg
.bytout
= c
[BYTESOUT
];
701 logg
.moves
= c
[MOVESMADE
];
702 logg
.spused
= c
[SPELLSCAST
];
703 logg
.killed
= c
[MONSTKILLED
];
705 lwrite((char *) &logg
, sizeof(struct log_fmt
));
710 * now for the scoreboard maintenance -- not for a suspended
716 scorerror
= writeboard();
720 if ((x
== 256) || (x
== 257) || (f
!= 0))
723 showscores(); /* if we updated the scoreboard */
730 * diedsub(x) Subroutine to print out the line showing the player when he is killed
739 lprintf("Score: %ld, Diff: %ld, %s ", (long) c
[GOLD
], (long) c
[HARDGAME
], logname
);
741 ch
= *monster
[x
].name
;
742 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
746 lprintf("killed by %s %s", mod
, monster
[x
].name
);
748 lprintf("%s", whydead
[x
- 256]);
750 lprintf(" on %s\n", levelname
[level
]);
756 * diedlog() Subroutine to read a log file and print it out in ascii format
763 static char q
[] = "?";
768 if (lopen(logfile
) < 0) {
769 lprintf("Can't locate log file <%s>\n", logfile
);
772 if (fstat(io_infd
, &stbuf
) < 0) {
773 lprintf("Can't stat log file <%s>\n", logfile
);
776 for (n
= stbuf
.st_size
/ sizeof(struct log_fmt
); n
> 0; --n
) {
777 lrfill((char *) &logg
, sizeof(struct log_fmt
));
779 if ((p
= ctime(&t
)) == NULL
)
785 lprintf("Score: %ld, Diff: %ld, %s %s on %ld at %s", (long) (logg
.score
), (long) (logg
.diff
), logg
.who
, logg
.what
, (long) (logg
.cavelev
), p
+ 4);
789 lprintf(" Experience Level: %ld, AC: %ld, HP: %ld/%ld, Elapsed Time: %ld minutes\n", (long) (logg
.lev
), (long) (logg
.ac
), (long) (logg
.hp
), (long) (logg
.hpmax
), (long) (logg
.elapsedtime
));
790 lprintf(" CPU time used: %ld seconds, Machine usage: %ld.%02ld%%\n", (long) (logg
.cputime
), (long) (logg
.usage
/ 100), (long) (logg
.usage
% 100));
791 lprintf(" BYTES in: %ld, out: %ld, moves: %ld, deaths: %ld, spells cast: %ld\n", (long) (logg
.bytin
), (long) (logg
.bytout
), (long) (logg
.moves
), (long) (logg
.killed
), (long) (logg
.spused
));
792 lprintf(" out bytes per move: %ld, time per move: %ld ms\n", (long) (logg
.bytout
/ logg
.moves
), (long) ((logg
.cputime
* 1000) / logg
.moves
));
802 * getplid(name) Function to get players id # from id file
804 * Enter with the name of the players character in name.
805 * Returns the id # of the players character, or -1 if failure.
806 * This routine will try to find the name in the id file, if its not there,
807 * it will try to make a new entry in the file. Only returns -1 if can't
808 * find him in the file, and can't make a new entry in the file.
809 * Format of playerids file:
810 * Id # in ascii \n character name \n
812 static int havepid
= -1; /* playerid # if previously done */
817 int fd7
, high
= 999, no
;
821 return (havepid
); /* already did it */
822 lflush(); /* flush any pending I/O */
823 snprintf(name
, sizeof(name
), "%s\n", nam
);/* append a \n to name */
824 if (lopen(playerids
) < 0) { /* no file, make it */
825 if ((fd7
= creat(playerids
, 0664)) < 0)
826 return (-1); /* can't make it */
828 goto addone
; /* now append new playerid record to file */
830 for (;;) { /* now search for the name in the player id
835 no
= atoi(p
); /* the id # */
840 high
= no
; /* accumulate highest id # */
841 if (strcmp(p2
, name
) == 0) { /* we found him */
842 return (no
); /* his id number */
846 /* if we get here, we didn't find him in the file -- put him there */
848 if (lappend(playerids
) < 0)
849 return (-1); /* can't open file for append */
850 lprintf("%ld\n%s", (long) ++high
, name
); /* new id # and name */
852 lcreat((char *) 0); /* re-open terminal channel */
855 #endif /* UIDSCORE */