]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/score.c
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"
61 killed_by(const object
*monster
, short other
)
63 const char *mechanism
= "killed by something unknown (?)";
64 char mechanism_buf
[128];
66 char message_buf
[128];
71 rogue
.gold
= ((rogue
.gold
* 9) / 10);
77 mechanism
= "died of hypothermia";
80 mechanism
= "died of starvation";
83 mechanism
= "killed by a dart";
89 mechanism
= "killed by fire";
93 if (is_vowel(m_names
[monster
->m_char
- 'A'][0])) {
98 snprintf(mechanism_buf
, sizeof(mechanism_buf
),
100 article
, m_names
[monster
->m_char
- 'A']);
101 mechanism
= mechanism_buf
;
103 snprintf(message_buf
, sizeof(message_buf
),
104 "%s with %ld gold", mechanism
, rogue
.gold
);
106 if ((!other
) && (!no_skull
)) {
108 mvaddstr(4, 32, "__---------__");
109 mvaddstr(5, 30, "_~ ~_");
110 mvaddstr(6, 29, "/ \\");
111 mvaddstr(7, 28, "~ ~");
112 mvaddstr(8, 27, "/ \\");
113 mvaddstr(9, 27, "| XXXX XXXX |");
114 mvaddstr(10, 27, "| XXXX XXXX |");
115 mvaddstr(11, 27, "| XXX XXX |");
116 mvaddstr(12, 28, "\\ @ /");
117 mvaddstr(13, 29, "--\\ @@@ /--");
118 mvaddstr(14, 30, "| | @@@ | |");
119 mvaddstr(15, 30, "| | | |");
120 mvaddstr(16, 30, "| vvVvvvvvvvVvv |");
121 mvaddstr(17, 30, "| ^^^^^^^^^^^ |");
122 mvaddstr(18, 31, "\\_ _/");
123 mvaddstr(19, 33, "~---------~");
124 center(21, nick_name
);
125 center(22, message_buf
);
127 messagef(0, "%s", message_buf
);
129 messagef(0, "%s", ""); /* gcc objects to just "" */
130 put_scores(monster
, other
);
136 unwield(rogue
.weapon
); /* disarm and relax */
138 un_put_on(rogue
.left_ring
);
139 un_put_on(rogue
.right_ring
);
142 mvaddstr(10, 11, "@ @ @@@ @ @ @ @ @ @@@ @ @ @");
143 mvaddstr(11, 11, " @ @ @ @ @ @ @ @ @ @ @ @@ @ @");
144 mvaddstr(12, 11, " @ @ @ @ @ @ @ @ @ @ @ @ @ @");
145 mvaddstr(13, 11, " @ @ @ @ @ @ @ @ @ @ @ @@");
146 mvaddstr(14, 11, " @ @@@ @@@ @@ @@ @@@ @ @ @");
147 mvaddstr(17, 11, "Congratulations, you have been admitted to the");
148 mvaddstr(18, 11, "Fighters' Guild. You return home, sell all your");
149 mvaddstr(19, 11, "treasures at great profit and retire into comfort.");
150 messagef(0, "%s", ""); /* gcc objects to just "" */
151 messagef(0, "%s", ""); /* gcc objects to just "" */
154 put_scores(NULL
, WIN
);
158 quit(boolean from_intrpt
)
174 for (i
= 0; i
< DCOLS
; i
++) {
175 buf
[i
] = mvinch(0, i
);
179 messagef(1, "really quit?");
180 if (rgetchar() != 'y') {
184 for (i
= 0; i
< DCOLS
; i
++) {
185 mvaddch(0, i
, buf
[i
]);
194 clean_up(byebye_string
);
197 killed_by(NULL
, QUIT
);
201 * The score file on disk is up to ten entries of the form
202 * score block [80 bytes]
203 * nickname block [30 bytes]
205 * The score block is to be parsed as follows:
206 * bytes 0-1 Rank (" 1" to "10")
207 * bytes 2-4 space padding
208 * bytes 5-15 Score/gold
209 * byte 15 up to a ':' Login name
210 * past the ':' Death mechanism
212 * The nickname block is an alternate name to be printed in place of the
213 * login name. Both blocks are supposed to contain a null-terminator.
223 #define NUM_SCORE_ENTRIES 10
225 static void make_score(struct score_entry
*, const object
*, int);
229 pad_spaces(char *str
, size_t len
)
232 for (x
=strlen(str
); x
<len
-1; x
++) {
240 unpad_spaces(char *str
)
243 for (x
=strlen(str
); x
>0 && str
[x
-1]==' '; x
--);
249 read_score_entry(struct score_entry
*se
, FILE *fp
)
251 char score_block
[80];
252 char nickname_block
[30];
255 n
= fread(score_block
, 1, sizeof(score_block
), fp
);
260 if (n
!= sizeof(score_block
)) {
264 n
= fread(nickname_block
, 1, sizeof(nickname_block
), fp
);
265 if (n
!= sizeof(nickname_block
)) {
269 xxxx(score_block
, sizeof(score_block
));
270 xxxx(nickname_block
, sizeof(nickname_block
));
272 /* Ensure null termination */
273 score_block
[sizeof(score_block
)-1] = 0;
274 nickname_block
[sizeof(nickname_block
)-1] = 0;
276 /* If there are other nulls in the score block, file is corrupt */
277 if (strlen(score_block
)!=sizeof(score_block
)-1) {
280 /* but this is NOT true of the nickname block */
282 /* quash trailing spaces */
283 unpad_spaces(score_block
);
284 unpad_spaces(nickname_block
);
286 for (x
=5; score_block
[x
] == ' '; x
++);
287 se
->gold
= lget_number(score_block
+x
);
289 for (x
=15; score_block
[x
] != 0 && score_block
[x
] != ':'; x
++);
290 if (score_block
[x
] == 0) {
293 score_block
[x
++] = 0;
294 strlcpy(se
->username
, score_block
+15, sizeof(se
->username
));
296 strlcpy(se
->death
, score_block
+x
, sizeof(se
->death
));
297 strlcpy(se
->nickname
, nickname_block
, sizeof(se
->nickname
));
304 write_score_entry(const struct score_entry
*se
, int rank
, FILE *fp
)
306 char score_block
[80];
307 char nickname_block
[30];
309 /* avoid writing crap to score file */
310 memset(score_block
, 0, sizeof(score_block
));
311 memset(nickname_block
, 0, sizeof(nickname_block
));
313 snprintf(score_block
, sizeof(score_block
),
315 rank
+1, se
->gold
, se
->username
, se
->death
);
316 strlcpy(nickname_block
, se
->nickname
, sizeof(nickname_block
));
318 /* pad blocks out with spaces */
319 pad_spaces(score_block
, sizeof(score_block
));
320 /*pad_spaces(nickname_block, sizeof(nickname_block)); -- wrong! */
322 xxxx(score_block
, sizeof(score_block
));
323 xxxx(nickname_block
, sizeof(nickname_block
));
325 fwrite(score_block
, 1, sizeof(score_block
), fp
);
326 fwrite(nickname_block
, 1, sizeof(nickname_block
), fp
);
330 put_scores(const object
*monster
, short other
)
332 short i
, rank
=-1, found_player
= -1, numscores
= 0;
333 struct score_entry scores
[NUM_SCORE_ENTRIES
];
336 boolean dopause
= score_only
;
341 if ((fp
= fopen(_PATH_SCOREFILE
, "r+")) == NULL
&&
342 (fp
= fopen(_PATH_SCOREFILE
, "w+")) == NULL
) {
344 messagef(0, "cannot read/write/create score file");
351 for (numscores
= 0; numscores
< NUM_SCORE_ENTRIES
; numscores
++) {
352 if (read_score_entry(&scores
[numscores
], fp
) == 0) {
357 /* Search the score list. */
358 for (i
=0; i
<numscores
; i
++) {
359 if (!strcmp(scores
[i
].username
, login_name
)) {
360 /* found our score */
361 if (rogue
.gold
< scores
[i
].gold
) {
362 /* we didn't do as well as last time */
365 /* we did better; mark entry for removal */
372 /* Remove a superseded entry, if any. */
373 if (found_player
!= -1) {
375 for (i
= found_player
; i
< numscores
; i
++) {
376 scores
[i
] = scores
[i
+1];
380 /* If we're going to insert ourselves, do it now */
383 /* if we aren't better than anyone, add at end. */
386 /* Otherwise, find our slot. */
387 for (i
= 0; i
< numscores
; i
++) {
388 if (rogue
.gold
>= scores
[i
].gold
) {
394 if (rank
< NUM_SCORE_ENTRIES
) {
396 for (i
= numscores
; i
> rank
; i
--) {
397 scores
[i
] = scores
[i
-1];
401 /* Put our info in the slot */
402 make_score(&scores
[rank
], monster
, other
);
405 /* Now rewrite the score file */
411 for (i
= 0; i
< numscores
; i
++) {
412 write_score_entry(&scores
[i
], i
, fp
);
418 /* Display the scores */
421 mvaddstr(3, 30, "Top Ten Rogueists");
422 mvaddstr(8, 0, "Rank Score Name");
424 for (i
= 0; i
< numscores
; i
++) {
429 if (scores
[i
].nickname
[0]) {
430 name
= scores
[i
].nickname
;
432 name
= scores
[i
].username
;
435 mvprintw(i
+10, 0, "%2d %6ld %s: %s",
436 i
+1, scores
[i
].gold
, name
, scores
[i
].death
);
443 messagef(0, "%s", ""); /* gcc objects to just "" */
445 messagef(0, "%s", "");
452 make_score(struct score_entry
*se
, const object
*monster
, int other
)
454 const char *death
= "bolts from the blue (?)";
455 const char *hasamulet
;
458 se
->gold
= rogue
.gold
;
459 strlcpy(se
->username
, login_name
, sizeof(se
->username
));
464 death
= "died of hypothermia";
467 death
= "died of starvation";
470 death
= "killed by a dart";
476 death
= "a total winner";
479 death
= "killed by fire";
483 const char *mn
, *article
;
485 mn
= m_names
[monster
->m_char
- 'A'];
486 if (is_vowel(mn
[0])) {
492 snprintf(deathbuf
, sizeof(deathbuf
),
493 "killed by %s %s", article
, mn
);
497 if (other
!= WIN
&& has_amulet()) {
498 hasamulet
= " with amulet";
503 snprintf(se
->death
, sizeof(se
->death
), "%s on level %d%s",
504 death
, max_level
, hasamulet
);
506 strlcpy(se
->nickname
, nick_name
, sizeof(se
->nickname
));
512 return( (ch
== 'a') ||
526 obj
= rogue
.pack
.next_object
;
529 mvaddstr(1, 0, "Value Item");
532 if (obj
->what_is
!= FOOD
) {
534 val
= get_value(obj
);
538 get_desc(obj
, buf
, sizeof(buf
));
539 mvprintw(row
++, 0, "%5d %s", val
, buf
);
542 obj
= obj
->next_object
;
545 if (rogue
.gold
> MAX_GOLD
) {
546 rogue
.gold
= MAX_GOLD
;
548 messagef(0, "%s", ""); /* gcc objects to just "" */
552 get_value(const object
*obj
)
558 wc
= obj
->which_kind
;
560 switch(obj
->what_is
) {
562 val
= id_weapons
[wc
].value
;
563 if ((wc
== ARROW
) || (wc
== DAGGER
) || (wc
== SHURIKEN
) ||
565 val
*= obj
->quantity
;
567 val
+= (obj
->d_enchant
* 85);
568 val
+= (obj
->hit_enchant
* 85);
571 val
= id_armors
[wc
].value
;
572 val
+= (obj
->d_enchant
* 75);
573 if (obj
->is_protected
) {
578 val
= id_wands
[wc
].value
* (obj
->class + 1);
581 val
= id_scrolls
[wc
].value
* obj
->quantity
;
584 val
= id_potions
[wc
].value
* obj
->quantity
;
590 val
= id_rings
[wc
].value
* (obj
->class + 1);
604 for (i
= 0; i
< SCROLS
; i
++) {
605 id_scrolls
[i
].id_status
= IDENTIFIED
;
607 for (i
= 0; i
< WEAPONS
; i
++) {
608 id_weapons
[i
].id_status
= IDENTIFIED
;
610 for (i
= 0; i
< ARMORS
; i
++) {
611 id_armors
[i
].id_status
= IDENTIFIED
;
613 for (i
= 0; i
< WANDS
; i
++) {
614 id_wands
[i
].id_status
= IDENTIFIED
;
616 for (i
= 0; i
< POTIONS
; i
++) {
617 id_potions
[i
].id_status
= IDENTIFIED
;
622 xxxx(char *buf
, short n
)
627 for (i
= 0; i
< n
; i
++) {
629 /* It does not matter if accuracy is lost during this assignment */
630 c
= (unsigned char)xxx(0);
647 r
= ((f
* s
) + 9337) % 8887;
654 center(short row
, const char *buf
)
658 margin
= ((DCOLS
- strlen(buf
)) / 2);
659 mvaddstr(row
, margin
, buf
);
666 messagef(1, "%s", ""); /* gcc objects to just "" */
667 clean_up("sorry, score file is out of order");