]>
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>
43 struct scofmt
{ /* This is the structure for the scoreboard */
44 long score
; /* the score of the player */
45 long suid
; /* the user id number of the player */
46 short what
; /* the number of the monster that killed
48 short level
; /* the level player was on when he died */
49 short hardlev
;/* the level of difficulty player played at */
50 short order
; /* the relative ordering place of this entry */
51 char who
[40];/* the name of the character */
52 char sciv
[26][2]; /* this is the inventory list of the
55 struct wscofmt
{ /* This is the structure for the winning
57 long score
; /* the score of the player */
58 long timeused
; /* the time used in mobuls to win the
60 long taxes
; /* taxes he owes to LRS */
61 long suid
; /* the user id number of the player */
62 short hardlev
;/* the level of difficulty player played at */
63 short order
; /* the relative ordering place of this entry */
64 char who
[40];/* the name of the character */
67 struct log_fmt
{ /* 102 bytes struct for the log file */
68 long score
; /* the players score */
69 int32_t diedtime
; /* time when game was over */
70 short cavelev
;/* level in caves */
71 short diff
; /* difficulty player played at */
73 long elapsedtime
; /* real time of game in seconds */
74 long bytout
; /* bytes input and output */
76 long moves
; /* number of moves made by player */
77 short ac
; /* armor class of player */
78 short hp
, hpmax
; /* players hitpoints */
79 short cputime
;/* CPU time needed in seconds */
80 short killed
, spused
; /* monsters killed and spells cast */
81 short usage
; /* usage of the CPU in % */
82 short lev
; /* player level */
84 char who
[12];/* player name */
85 char what
[46]; /* what happened to player */
88 static struct scofmt sco
[SCORESIZE
]; /* the structure for the scoreboard */
89 static struct wscofmt winr
[SCORESIZE
]; /* struct for the winning scoreboard */
90 static struct log_fmt logg
; /* structure for the log file */
91 static const char *whydead
[] = {
92 "quit", "suspended", "self - annihilated", "shot by an arrow",
93 "hit by a dart", "fell into a pit", "fell into a bottomless pit",
94 "a winner", "trapped in solid rock", "killed by a missing save file",
95 "killed by an old save file", "caught by the greedy cheater checker trap",
96 "killed by a protected save file", "killed his family and committed suicide",
97 "erased by a wayward finger", "fell through a bottomless trap door",
98 "fell through a trap door", "drank some poisonous water",
99 "fried by an electric shock", "slipped on a volcano shaft",
100 "killed by a stupid act of frustration", "attacked by a revolting demon",
101 "hit by his own magic", "demolished by an unseen attacker",
102 "fell into the dreadful sleep", "killed by an exploding chest",
103 /* 26 */ "killed by a missing maze data file", "annihilated in a sphere",
104 "died a post mortem death", "wasted by a malloc() failure"
107 static int readboard(void);
108 static int writeboard(void);
109 static int winshou(void);
110 static int shou(int);
111 static int sortboard(void);
112 static void newscore(long, char *, int, int);
113 static void new1sub(long, int, char *, long);
114 static void new2sub(long, int, char *, int);
115 static void diedsub(int);
118 * readboard() Function to read in the scoreboard into a static buffer
120 * returns -1 if unable to read in the scoreboard, returns 0 if all is OK
129 i
= lopen(scorefile
);
133 lprcat("Can't read scoreboard\n");
137 lrfill((char *) sco
, sizeof(sco
));
138 lrfill((char *) winr
, sizeof(winr
));
145 * writeboard() Function to write the scoreboard from readboard()'s buffer
147 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
157 i
= lcreat(scorefile
);
161 lprcat("Can't write scoreboard\n");
165 lwrite((char *) sco
, sizeof(sco
));
166 lwrite((char *) winr
, sizeof(winr
));
173 * makeboard() Function to create a new scoreboard (wipe out old one)
175 * returns -1 if unable to write the scoreboard, returns 0 if all is OK
182 for (i
= 0; i
< SCORESIZE
; i
++) {
183 winr
[i
].taxes
= winr
[i
].score
= sco
[i
].score
= 0;
184 winr
[i
].order
= sco
[i
].order
= i
;
190 chmod(scorefile
, 0660);
197 * hashewon() Function to return 1 if player has won a game before, else 0
199 * This function also sets c[HARDGAME] to appropriate value -- 0 if not a
200 * winner, otherwise the next level of difficulty listed in the winners
201 * scoreboard. This function also sets outstanding_taxes to the value in
202 * the winners scoreboard.
210 return (0); /* can't find scoreboard */
211 for (i
= 0; i
< SCORESIZE
; i
++) /* search through winners scoreboard */
212 if (winr
[i
].suid
== userid
)
213 if (winr
[i
].score
> 0) {
214 c
[HARDGAME
] = winr
[i
].hardlev
+ 1;
215 outstanding_taxes
= winr
[i
].taxes
;
222 * long paytaxes(x) Function to pay taxes if any are due
224 * Enter with the amount (in gp) to pay on the taxes.
225 * Returns amount actually paid.
236 for (i
= 0; i
< SCORESIZE
; i
++)
237 if (winr
[i
].suid
== userid
) /* look for players winning
239 if (winr
[i
].score
> 0) { /* search for a winning
240 * entry for the player */
243 amt
= x
; /* don't overpay taxes
245 winr
[i
].taxes
-= amt
;
246 outstanding_taxes
-= amt
;
248 if (writeboard() < 0)
252 return (0L); /* couldn't find user on winning scoreboard */
256 * winshou() Subroutine to print out the winning scoreboard
258 * Returns the number of players on scoreboard that were shown
265 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is there anyone on
267 if (winr
[i
].score
!= 0) {
272 lprcat("\n Score Difficulty Time Needed Larn Winners List\n");
274 for (i
= 0; i
< SCORESIZE
; i
++) /* this loop is needed to
276 for (j
= 0; j
< SCORESIZE
; j
++) { /* winners in order */
277 p
= &winr
[j
]; /* pointer to the scoreboard
282 lprintf("%10ld %2ld %5ld Mobuls %s \n",
283 (long) p
->score
, (long) p
->hardlev
, (long) p
->timeused
, p
->who
);
289 return (count
); /* return number of people on scoreboard */
293 * shou(x) Subroutine to print out the non-winners scoreboard
296 * Enter with 0 to list the scores, enter with 1 to list inventories too
297 * Returns the number of players on scoreboard that were shown
304 for (count
= j
= i
= 0; i
< SCORESIZE
; i
++) /* is the scoreboard
306 if (sco
[i
].score
!= 0) {
311 lprcat("\n Score Difficulty Larn Visitor Log\n");
312 for (i
= 0; i
< SCORESIZE
; i
++) /* be sure to print them out
314 for (j
= 0; j
< SCORESIZE
; j
++)
315 if (sco
[j
].order
== i
) {
318 lprintf("%10ld %2ld %s ",
319 (long) sco
[j
].score
, (long) sco
[j
].hardlev
, sco
[j
].who
);
320 if (sco
[j
].what
< 256)
321 lprintf("killed by a %s", monster
[sco
[j
].what
].name
);
323 lprintf("%s", whydead
[sco
[j
].what
- 256]);
325 lprintf(" on %s", levelname
[sco
[j
].level
]);
327 for (n
= 0; n
< 26; n
++) {
328 iven
[n
] = sco
[j
].sciv
[n
][0];
329 ivenarg
[n
] = sco
[j
].sciv
[n
][1];
331 for (k
= 1; k
< 99; k
++)
332 for (n
= 0; n
< 26; n
++)
344 return (count
); /* return the number of players just shown */
348 * showscores() Function to show the scoreboard on the terminal
350 * Returns nothing of value
352 static char esb
[] = "The scoreboard is empty.\n";
371 * showallscores() Function to show scores and the iven lists that go with them
373 * Returns nothing of value
383 c
[WEAR
] = c
[WIELD
] = c
[SHIELD
] = -1; /* not wielding or wearing
385 for (i
= 0; i
< MAXPOTION
; i
++)
386 potionname
[i
] = potionhide
[i
];
387 for (i
= 0; i
< MAXSCROLL
; i
++)
388 scrollname
[i
] = scrollhide
[i
];
399 * sortboard() Function to sort the scoreboard
401 * Returns 0 if no sorting done, else returns 1
408 for (i
= 0; i
< SCORESIZE
; i
++)
409 sco
[i
].order
= winr
[i
].order
= -1;
411 while (pos
< SCORESIZE
) {
413 for (i
= 0; i
< SCORESIZE
; i
++)
414 if ((sco
[i
].order
< 0) && (sco
[i
].score
>= jdat
)) {
418 sco
[j
].order
= pos
++;
421 while (pos
< SCORESIZE
) {
423 for (i
= 0; i
< SCORESIZE
; i
++)
424 if ((winr
[i
].order
< 0) && (winr
[i
].score
>= jdat
)) {
426 jdat
= winr
[i
].score
;
428 winr
[j
].order
= pos
++;
434 * newscore(score, whoo, whyded, winner) Function to add entry to scoreboard
435 * int score, winner, whyded;
438 * Enter with the total score in gp in score, players name in whoo,
439 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
440 * ex. newscore(1000, "player 1", 32, 0);
443 newscore(long score
, char *whoo
, int whyded
, int winner
)
448 return; /* do the scoreboard */
449 /* if a winner then delete all non-winning scores */
451 winner
= 0; /* if he cheated, don't let him win */
453 for (i
= 0; i
< SCORESIZE
; i
++)
454 if (sco
[i
].suid
== userid
)
456 taxes
= score
* TAXRATE
;
457 score
+= 100000 * c
[HARDGAME
]; /* bonus for winning */
459 * if he has a slot on the winning scoreboard update it if
462 for (i
= 0; i
< SCORESIZE
; i
++)
463 if (winr
[i
].suid
== userid
) {
464 new1sub(score
, i
, whoo
, taxes
);
468 * he had no entry. look for last entry and see if he has a
471 for (i
= 0; i
< SCORESIZE
; i
++)
472 if (winr
[i
].order
== SCORESIZE
- 1) {
473 new1sub(score
, i
, whoo
, taxes
);
476 } else if (!cheat
) { /* for not winning scoreboard */
478 * if he has a slot on the scoreboard update it if greater
481 for (i
= 0; i
< SCORESIZE
; i
++)
482 if (sco
[i
].suid
== userid
) {
483 new2sub(score
, i
, whoo
, whyded
);
487 * he had no entry. look for last entry and see if he has a
490 for (i
= 0; i
< SCORESIZE
; i
++)
491 if (sco
[i
].order
== SCORESIZE
- 1) {
492 new2sub(score
, i
, whoo
, whyded
);
499 * new1sub(score,i,whoo,taxes) Subroutine to put player into a
500 * int score,i,whyded,taxes; winning scoreboard entry if his score
501 * char *whoo; is high enough
503 * Enter with the total score in gp in score, players name in whoo,
504 * died() reason # in whyded, and TRUE/FALSE in winner if a winner
505 * slot in scoreboard in i, and the tax bill in taxes.
506 * Returns nothing of value
509 new1sub(long score
, int i
, char *whoo
, long taxes
)
514 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
515 strcpy(p
->who
, whoo
);
517 p
->hardlev
= c
[HARDGAME
];
519 p
->timeused
= gltime
/ 100;
524 * new2sub(score,i,whoo,whyded) Subroutine to put player into a
525 * int score,i,whyded,taxes; non-winning scoreboard entry if his
526 * char *whoo; score is high enough
528 * Enter with the total score in gp in score, players name in whoo,
529 * died() reason # in whyded, and slot in scoreboard in i.
530 * Returns nothing of value
533 new2sub(long score
, int i
, char *whoo
, int whyded
)
538 if ((score
>= p
->score
) || (c
[HARDGAME
] > p
->hardlev
)) {
539 strcpy(p
->who
, whoo
);
542 p
->hardlev
= c
[HARDGAME
];
545 for (j
= 0; j
< 26; j
++) {
546 p
->sciv
[j
][0] = iven
[j
];
547 p
->sciv
[j
][1] = ivenarg
[j
];
553 * died(x) Subroutine to record who played larn, and what the score was
556 * if x < 0 then don't show scores
557 * died() never returns! (unless c[LIFEPROT] and a reincarnatable death!)
559 * < 256 killed by the monster number
562 * 258 self - annihilated
563 * 259 shot by an arrow
565 * 261 fell into a pit
566 * 262 fell into a bottomless pit
568 * 264 trapped in solid rock
569 * 265 killed by a missing save file
570 * 266 killed by an old save file
571 * 267 caught by the greedy cheater checker trap
572 * 268 killed by a protected save file
573 * 269 killed his family and killed himself
574 * 270 erased by a wayward finger
575 * 271 fell through a bottomless trap door
576 * 272 fell through a trap door
577 * 273 drank some poisonous water
578 * 274 fried by an electric shock
579 * 275 slipped on a volcano shaft
580 * 276 killed by a stupid act of frustration
581 * 277 attacked by a revolting demon
582 * 278 hit by his own magic
583 * 279 demolished by an unseen attacker
584 * 280 fell into the dreadful sleep
585 * 281 killed by an exploding chest
586 * 282 killed by a missing maze data file
587 * 283 killed by a sphere of annihilation
588 * 284 died a post mortem death
589 * 285 malloc() failure
590 * 300 quick quit -- don't put on scoreboard
593 static int scorerror
;
601 if (c
[LIFEPROT
] > 0) { /* if life protection */
602 switch ((x
> 0) ? x
: -x
) {
617 goto invalid
; /* can't be saved */
623 lprcat("\nYou feel wiiieeeeerrrrrd all over! ");
627 return; /* only case where died() returns */
634 unlink(ckpfile
);/* remove checkpoint file if used */
638 } /* if we are not to display the scores */
639 if ((x
== 300) || (x
== 257))
640 exit(0); /* for quick exit or saved game */
645 c
[GOLD
] += c
[BANKACCOUNT
];
647 /* now enter the player at the end of the scoreboard */
648 newscore(c
[GOLD
], logname
, x
, win
);
649 diedsub(x
); /* print out the score line */
653 if ((wizard
== 0) && (c
[GOLD
] > 0)) { /* wizards can't score */
657 if (lappend(logfile
) < 0) { /* append to file */
658 if (lcreat(logfile
) < 0) { /* and can't create new
661 lprcat("\nCan't open record file: I can't post your score.\n");
669 chmod(logfile
, 0660);
675 strcpy(logg
.who
, loginname
);
676 logg
.score
= c
[GOLD
];
677 logg
.diff
= c
[HARDGAME
];
679 ch
= *monster
[x
].name
;
680 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
684 snprintf(logg
.what
, sizeof(logg
.what
),
685 "killed by %s %s", mod
, monster
[x
].name
);
687 snprintf(logg
.what
, sizeof(logg
.what
),
688 "%s", whydead
[x
- 256]);
689 logg
.cavelev
= level
;
690 time(&zzz
); /* get CPU time -- write out score info */
693 times(&cputime
);/* get CPU time -- write out score info */
694 logg
.cputime
= i
= (cputime
.tms_utime
+ cputime
.tms_stime
) / 60 + c
[CPUTIME
];
697 logg
.hpmax
= c
[HPMAX
];
699 logg
.elapsedtime
= (zzz
- initialtime
+ 59) / 60;
700 logg
.usage
= (10000 * i
) / (zzz
- initialtime
);
701 logg
.bytin
= c
[BYTESIN
];
702 logg
.bytout
= c
[BYTESOUT
];
703 logg
.moves
= c
[MOVESMADE
];
704 logg
.spused
= c
[SPELLSCAST
];
705 logg
.killed
= c
[MONSTKILLED
];
707 lwrite((char *) &logg
, sizeof(struct log_fmt
));
712 * now for the scoreboard maintenance -- not for a suspended
718 scorerror
= writeboard();
722 if ((x
== 256) || (x
== 257) || (f
!= 0))
725 showscores(); /* if we updated the scoreboard */
732 * diedsub(x) Subroutine to print out the line showing the player when he is killed
741 lprintf("Score: %ld, Diff: %ld, %s ", (long) c
[GOLD
], (long) c
[HARDGAME
], logname
);
743 ch
= *monster
[x
].name
;
744 if (ch
== 'a' || ch
== 'e' || ch
== 'i' || ch
== 'o' || ch
== 'u')
748 lprintf("killed by %s %s", mod
, monster
[x
].name
);
750 lprintf("%s", whydead
[x
- 256]);
752 lprintf(" on %s\n", levelname
[level
]);
758 * diedlog() Subroutine to read a log file and print it out in ascii format
765 static char q
[] = "?";
770 if (lopen(logfile
) < 0) {
771 lprintf("Can't locate log file <%s>\n", logfile
);
774 if (fstat(io_infd
, &stbuf
) < 0) {
775 lprintf("Can't stat log file <%s>\n", logfile
);
778 for (n
= stbuf
.st_size
/ sizeof(struct log_fmt
); n
> 0; --n
) {
779 lrfill((char *) &logg
, sizeof(struct log_fmt
));
781 if ((p
= ctime(&t
)) == NULL
)
787 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);
791 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
));
792 lprintf(" CPU time used: %ld seconds, Machine usage: %ld.%02ld%%\n", (long) (logg
.cputime
), (long) (logg
.usage
/ 100), (long) (logg
.usage
% 100));
793 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
));
794 lprintf(" out bytes per move: %ld, time per move: %ld ms\n", (long) (logg
.bytout
/ logg
.moves
), (long) ((logg
.cputime
* 1000) / logg
.moves
));
804 * getplid(name) Function to get players id # from id file
806 * Enter with the name of the players character in name.
807 * Returns the id # of the players character, or -1 if failure.
808 * This routine will try to find the name in the id file, if its not there,
809 * it will try to make a new entry in the file. Only returns -1 if can't
810 * find him in the file, and can't make a new entry in the file.
811 * Format of playerids file:
812 * Id # in ascii \n character name \n
814 static int havepid
= -1; /* playerid # if previously done */
819 int fd7
, high
= 999, no
;
823 return (havepid
); /* already did it */
824 lflush(); /* flush any pending I/O */
825 snprintf(name
, sizeof(name
), "%s\n", nam
);/* append a \n to name */
826 if (lopen(playerids
) < 0) { /* no file, make it */
827 if ((fd7
= creat(playerids
, 0664)) < 0)
828 return (-1); /* can't make it */
830 goto addone
; /* now append new playerid record to file */
832 for (;;) { /* now search for the name in the player id
837 no
= atoi(p
); /* the id # */
842 high
= no
; /* accumulate highest id # */
843 if (strcmp(p2
, name
) == 0) { /* we found him */
844 return (no
); /* his id number */
848 /* if we get here, we didn't find him in the file -- put him there */
850 if (lappend(playerids
) < 0)
851 return (-1); /* can't open file for append */
852 lprintf("%ld\n%s", (long) ++high
, name
); /* new id # and name */
854 lcreat((char *) 0); /* re-open terminal channel */
857 #endif /* UIDSCORE */