1 /* $NetBSD: score.c,v 1.14 2008/01/14 03:50:02 dholland Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
40 __RCSID("$NetBSD: score.c,v 1.14 2008/01/14 03:50:02 dholland Exp $");
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
58 #include "pathnames.h"
60 static void center(short, const char *);
61 static int get_value(const object
*);
62 static void id_all(void);
63 static void sell_pack(void);
64 static void sf_error(void) __attribute__((__noreturn__
));
67 killed_by(const object
*monster
, short other
)
69 const char *mechanism
= "killed by something unknown (?)";
70 char mechanism_buf
[128];
72 char message_buf
[128];
77 rogue
.gold
= ((rogue
.gold
* 9) / 10);
83 mechanism
= "died of hypothermia";
86 mechanism
= "died of starvation";
89 mechanism
= "killed by a dart";
95 mechanism
= "killed by fire";
99 if (is_vowel(m_names
[monster
->m_char
- 'A'][0])) {
104 snprintf(mechanism_buf
, sizeof(mechanism_buf
),
106 article
, m_names
[monster
->m_char
- 'A']);
107 mechanism
= mechanism_buf
;
109 snprintf(message_buf
, sizeof(message_buf
),
110 "%s with %ld gold", mechanism
, rogue
.gold
);
112 if ((!other
) && (!no_skull
)) {
114 mvaddstr(4, 32, "__---------__");
115 mvaddstr(5, 30, "_~ ~_");
116 mvaddstr(6, 29, "/ \\");
117 mvaddstr(7, 28, "~ ~");
118 mvaddstr(8, 27, "/ \\");
119 mvaddstr(9, 27, "| XXXX XXXX |");
120 mvaddstr(10, 27, "| XXXX XXXX |");
121 mvaddstr(11, 27, "| XXX XXX |");
122 mvaddstr(12, 28, "\\ @ /");
123 mvaddstr(13, 29, "--\\ @@@ /--");
124 mvaddstr(14, 30, "| | @@@ | |");
125 mvaddstr(15, 30, "| | | |");
126 mvaddstr(16, 30, "| vvVvvvvvvvVvv |");
127 mvaddstr(17, 30, "| ^^^^^^^^^^^ |");
128 mvaddstr(18, 31, "\\_ _/");
129 mvaddstr(19, 33, "~---------~");
130 center(21, nick_name
);
131 center(22, message_buf
);
133 messagef(0, "%s", message_buf
);
135 messagef(0, "%s", ""); /* gcc objects to just "" */
136 put_scores(monster
, other
);
142 unwield(rogue
.weapon
); /* disarm and relax */
144 un_put_on(rogue
.left_ring
);
145 un_put_on(rogue
.right_ring
);
148 mvaddstr(10, 11, "@ @ @@@ @ @ @ @ @ @@@ @ @ @");
149 mvaddstr(11, 11, " @ @ @ @ @ @ @ @ @ @ @ @@ @ @");
150 mvaddstr(12, 11, " @ @ @ @ @ @ @ @ @ @ @ @ @ @");
151 mvaddstr(13, 11, " @ @ @ @ @ @ @ @ @ @ @ @@");
152 mvaddstr(14, 11, " @ @@@ @@@ @@ @@ @@@ @ @ @");
153 mvaddstr(17, 11, "Congratulations, you have been admitted to the");
154 mvaddstr(18, 11, "Fighters' Guild. You return home, sell all your");
155 mvaddstr(19, 11, "treasures at great profit and retire into comfort.");
156 messagef(0, "%s", ""); /* gcc objects to just "" */
157 messagef(0, "%s", ""); /* gcc objects to just "" */
160 put_scores(NULL
, WIN
);
164 quit(boolean from_intrpt
)
180 for (i
= 0; i
< DCOLS
; i
++) {
181 buf
[i
] = mvinch(0, i
);
185 messagef(1, "really quit?");
186 if (rgetchar() != 'y') {
190 for (i
= 0; i
< DCOLS
; i
++) {
191 mvaddch(0, i
, buf
[i
]);
200 clean_up(byebye_string
);
203 killed_by(NULL
, QUIT
);
207 * The score file on disk is up to ten entries of the form
208 * score block [80 bytes]
209 * nickname block [30 bytes]
211 * The score block is to be parsed as follows:
212 * bytes 0-1 Rank (" 1" to "10")
213 * bytes 2-4 space padding
214 * bytes 5-15 Score/gold
215 * byte 15 up to a ':' Login name
216 * past the ':' Death mechanism
218 * The nickname block is an alternate name to be printed in place of the
219 * login name. Both blocks are supposed to contain a null-terminator.
229 #define NUM_SCORE_ENTRIES 10
231 static void make_score(struct score_entry
*, const object
*, int);
235 pad_spaces(char *str
, size_t len
)
238 for (x
=strlen(str
); x
<len
-1; x
++) {
246 unpad_spaces(char *str
)
249 for (x
=strlen(str
); x
>0 && str
[x
-1]==' '; x
--);
255 read_score_entry(struct score_entry
*se
, FILE *fp
)
257 char score_block
[80];
258 char nickname_block
[30];
261 n
= fread(score_block
, 1, sizeof(score_block
), fp
);
266 if (n
!= sizeof(score_block
)) {
270 n
= fread(nickname_block
, 1, sizeof(nickname_block
), fp
);
271 if (n
!= sizeof(nickname_block
)) {
275 xxxx(score_block
, sizeof(score_block
));
276 xxxx(nickname_block
, sizeof(nickname_block
));
278 /* Ensure null termination */
279 score_block
[sizeof(score_block
)-1] = 0;
280 nickname_block
[sizeof(nickname_block
)-1] = 0;
282 /* If there are other nulls in the score block, file is corrupt */
283 if (strlen(score_block
)!=sizeof(score_block
)-1) {
286 /* but this is NOT true of the nickname block */
288 /* quash trailing spaces */
289 unpad_spaces(score_block
);
290 unpad_spaces(nickname_block
);
292 for (x
=5; score_block
[x
] == ' '; x
++);
293 se
->gold
= lget_number(score_block
+x
);
295 for (x
=15; score_block
[x
] != 0 && score_block
[x
] != ':'; x
++);
296 if (score_block
[x
] == 0) {
299 score_block
[x
++] = 0;
300 strlcpy(se
->username
, score_block
+15, sizeof(se
->username
));
302 strlcpy(se
->death
, score_block
+x
, sizeof(se
->death
));
303 strlcpy(se
->nickname
, nickname_block
, sizeof(se
->nickname
));
310 write_score_entry(const struct score_entry
*se
, int rank
, FILE *fp
)
312 char score_block
[80];
313 char nickname_block
[30];
315 /* avoid writing crap to score file */
316 memset(score_block
, 0, sizeof(score_block
));
317 memset(nickname_block
, 0, sizeof(nickname_block
));
319 snprintf(score_block
, sizeof(score_block
),
321 rank
+1, se
->gold
, se
->username
, se
->death
);
322 strlcpy(nickname_block
, se
->nickname
, sizeof(nickname_block
));
324 /* pad blocks out with spaces */
325 pad_spaces(score_block
, sizeof(score_block
));
326 /*pad_spaces(nickname_block, sizeof(nickname_block)); -- wrong! */
328 xxxx(score_block
, sizeof(score_block
));
329 xxxx(nickname_block
, sizeof(nickname_block
));
331 fwrite(score_block
, 1, sizeof(score_block
), fp
);
332 fwrite(nickname_block
, 1, sizeof(nickname_block
), fp
);
336 put_scores(const object
*monster
, short other
)
338 short i
, rank
=-1, found_player
= -1, numscores
= 0;
339 struct score_entry scores
[NUM_SCORE_ENTRIES
];
342 boolean dopause
= score_only
;
347 if ((fp
= fopen(_PATH_SCOREFILE
, "r+")) == NULL
&&
348 (fp
= fopen(_PATH_SCOREFILE
, "w+")) == NULL
) {
350 messagef(0, "cannot read/write/create score file");
357 for (numscores
= 0; numscores
< NUM_SCORE_ENTRIES
; numscores
++) {
358 if (read_score_entry(&scores
[numscores
], fp
) == 0) {
363 /* Search the score list. */
364 for (i
=0; i
<numscores
; i
++) {
365 if (!strcmp(scores
[i
].username
, login_name
)) {
366 /* found our score */
367 if (rogue
.gold
< scores
[i
].gold
) {
368 /* we didn't do as well as last time */
371 /* we did better; mark entry for removal */
378 /* Remove a superseded entry, if any. */
379 if (found_player
!= -1) {
381 for (i
= found_player
; i
< numscores
; i
++) {
382 scores
[i
] = scores
[i
+1];
386 /* If we're going to insert ourselves, do it now */
389 /* if we aren't better than anyone, add at end. */
392 /* Otherwise, find our slot. */
393 for (i
= 0; i
< numscores
; i
++) {
394 if (rogue
.gold
>= scores
[i
].gold
) {
400 if (rank
< NUM_SCORE_ENTRIES
) {
402 for (i
= numscores
; i
> rank
; i
--) {
403 scores
[i
] = scores
[i
-1];
407 /* Put our info in the slot */
408 make_score(&scores
[rank
], monster
, other
);
411 /* Now rewrite the score file */
417 for (i
= 0; i
< numscores
; i
++) {
418 write_score_entry(&scores
[i
], i
, fp
);
424 /* Display the scores */
427 mvaddstr(3, 30, "Top Ten Rogueists");
428 mvaddstr(8, 0, "Rank Score Name");
430 for (i
= 0; i
< numscores
; i
++) {
435 if (scores
[i
].nickname
[0]) {
436 name
= scores
[i
].nickname
;
438 name
= scores
[i
].username
;
441 mvprintw(i
+10, 0, "%2d %6ld %s: %s",
442 i
+1, scores
[i
].gold
, name
, scores
[i
].death
);
449 messagef(0, "%s", ""); /* gcc objects to just "" */
451 messagef(0, "%s", "");
458 make_score(struct score_entry
*se
, const object
*monster
, int other
)
460 const char *death
= "bolts from the blue (?)";
461 const char *hasamulet
;
464 se
->gold
= rogue
.gold
;
465 strlcpy(se
->username
, login_name
, sizeof(se
->username
));
470 death
= "died of hypothermia";
473 death
= "died of starvation";
476 death
= "killed by a dart";
482 death
= "a total winner";
485 death
= "killed by fire";
489 const char *mn
, *article
;
491 mn
= m_names
[monster
->m_char
- 'A'];
492 if (is_vowel(mn
[0])) {
498 snprintf(deathbuf
, sizeof(deathbuf
),
499 "killed by %s %s", article
, mn
);
503 if (other
!= WIN
&& has_amulet()) {
504 hasamulet
= " with amulet";
509 snprintf(se
->death
, sizeof(se
->death
), "%s on level %d%s",
510 death
, max_level
, hasamulet
);
512 strlcpy(se
->nickname
, nick_name
, sizeof(se
->nickname
));
518 return( (ch
== 'a') ||
532 obj
= rogue
.pack
.next_object
;
535 mvaddstr(1, 0, "Value Item");
538 if (obj
->what_is
!= FOOD
) {
540 val
= get_value(obj
);
544 get_desc(obj
, buf
, sizeof(buf
));
545 mvprintw(row
++, 0, "%5d %s", val
, buf
);
548 obj
= obj
->next_object
;
551 if (rogue
.gold
> MAX_GOLD
) {
552 rogue
.gold
= MAX_GOLD
;
554 messagef(0, "%s", ""); /* gcc objects to just "" */
558 get_value(const object
*obj
)
564 wc
= obj
->which_kind
;
566 switch(obj
->what_is
) {
568 val
= id_weapons
[wc
].value
;
569 if ((wc
== ARROW
) || (wc
== DAGGER
) || (wc
== SHURIKEN
) ||
571 val
*= obj
->quantity
;
573 val
+= (obj
->d_enchant
* 85);
574 val
+= (obj
->hit_enchant
* 85);
577 val
= id_armors
[wc
].value
;
578 val
+= (obj
->d_enchant
* 75);
579 if (obj
->is_protected
) {
584 val
= id_wands
[wc
].value
* (obj
->class + 1);
587 val
= id_scrolls
[wc
].value
* obj
->quantity
;
590 val
= id_potions
[wc
].value
* obj
->quantity
;
596 val
= id_rings
[wc
].value
* (obj
->class + 1);
610 for (i
= 0; i
< SCROLS
; i
++) {
611 id_scrolls
[i
].id_status
= IDENTIFIED
;
613 for (i
= 0; i
< WEAPONS
; i
++) {
614 id_weapons
[i
].id_status
= IDENTIFIED
;
616 for (i
= 0; i
< ARMORS
; i
++) {
617 id_armors
[i
].id_status
= IDENTIFIED
;
619 for (i
= 0; i
< WANDS
; i
++) {
620 id_wands
[i
].id_status
= IDENTIFIED
;
622 for (i
= 0; i
< POTIONS
; i
++) {
623 id_potions
[i
].id_status
= IDENTIFIED
;
628 xxxx(char *buf
, short n
)
633 for (i
= 0; i
< n
; i
++) {
635 /* It does not matter if accuracy is lost during this assignment */
636 c
= (unsigned char)xxx(0);
653 r
= ((f
* s
) + 9337) % 8887;
660 center(short row
, const char *buf
)
664 margin
= ((DCOLS
- strlen(buf
)) / 2);
665 mvaddstr(row
, margin
, buf
);
672 messagef(1, "%s", ""); /* gcc objects to just "" */
673 clean_up("sorry, score file is out of order");