Home | History | Annotate | Line # | Download | only in rogue
score.c revision 1.5
      1 /*	$NetBSD: score.c,v 1.5 1995/04/22 10:28:26 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Timothy C. Stoehr.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)score.c	8.1 (Berkeley) 5/31/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: score.c,v 1.5 1995/04/22 10:28:26 cgd Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * score.c
     49  *
     50  * This source herein may be modified and/or distributed by anybody who
     51  * so desires, with the following restrictions:
     52  *    1.)  No portion of this notice shall be removed.
     53  *    2.)  Credit shall not be taken for the creation of this source.
     54  *    3.)  This code is not to be traded, sold, or used for personal
     55  *         gain or profit.
     56  *
     57  */
     58 
     59 #include <stdio.h>
     60 #include "rogue.h"
     61 #include "pathnames.h"
     62 
     63 extern char login_name[];
     64 extern char *m_names[];
     65 extern short max_level;
     66 extern boolean score_only, no_skull, msg_cleared;
     67 extern char *byebye_string, *nick_name;
     68 
     69 killed_by(monster, other)
     70 object *monster;
     71 short other;
     72 {
     73 	char buf[128];
     74 
     75 	md_ignore_signals();
     76 
     77 	if (other != QUIT) {
     78 		rogue.gold = ((rogue.gold * 9) / 10);
     79 	}
     80 
     81 	if (other) {
     82 		switch(other) {
     83 		case HYPOTHERMIA:
     84 			(void) strcpy(buf, "died of hypothermia");
     85 			break;
     86 		case STARVATION:
     87 			(void) strcpy(buf, "died of starvation");
     88 			break;
     89 		case POISON_DART:
     90 			(void) strcpy(buf, "killed by a dart");
     91 			break;
     92 		case QUIT:
     93 			(void) strcpy(buf, "quit");
     94 			break;
     95 		case KFIRE:
     96 			(void) strcpy(buf, "killed by fire");
     97 			break;
     98 		}
     99 	} else {
    100 		(void) strcpy(buf, "Killed by ");
    101 		if (is_vowel(m_names[monster->m_char - 'A'][0])) {
    102 			(void) strcat(buf, "an ");
    103 		} else {
    104 			(void) strcat(buf, "a ");
    105 		}
    106 		(void) strcat(buf, m_names[monster->m_char - 'A']);
    107 	}
    108 	(void) strcat(buf, " with ");
    109 	sprintf(buf+strlen(buf), "%ld gold", rogue.gold);
    110 	if ((!other) && (!no_skull)) {
    111 		clear();
    112 		mvaddstr(4, 32, "__---------__");
    113 		mvaddstr(5, 30, "_~             ~_");
    114 		mvaddstr(6, 29, "/                 \\");
    115 		mvaddstr(7, 28, "~                   ~");
    116 		mvaddstr(8, 27, "/                     \\");
    117 		mvaddstr(9, 27, "|    XXXX     XXXX    |");
    118 		mvaddstr(10, 27, "|    XXXX     XXXX    |");
    119 		mvaddstr(11, 27, "|    XXX       XXX    |");
    120 		mvaddstr(12, 28, "\\         @         /");
    121 		mvaddstr(13, 29, "--\\     @@@     /--");
    122 		mvaddstr(14, 30, "| |    @@@    | |");
    123 		mvaddstr(15, 30, "| |           | |");
    124 		mvaddstr(16, 30, "| vvVvvvvvvvVvv |");
    125 		mvaddstr(17, 30, "|  ^^^^^^^^^^^  |");
    126 		mvaddstr(18, 31, "\\_           _/");
    127 		mvaddstr(19, 33, "~---------~");
    128 		center(21, nick_name);
    129 		center(22, buf);
    130 	} else {
    131 		message(buf, 0);
    132 	}
    133 	message("", 0);
    134 	put_scores(monster, other);
    135 }
    136 
    137 win()
    138 {
    139 	unwield(rogue.weapon);		/* disarm and relax */
    140 	unwear(rogue.armor);
    141 	un_put_on(rogue.left_ring);
    142 	un_put_on(rogue.right_ring);
    143 
    144 	clear();
    145 	mvaddstr(10, 11, "@   @  @@@   @   @      @  @  @   @@@   @   @   @");
    146 	mvaddstr(11, 11, " @ @  @   @  @   @      @  @  @  @   @  @@  @   @");
    147 	mvaddstr(12, 11, "  @   @   @  @   @      @  @  @  @   @  @ @ @   @");
    148 	mvaddstr(13, 11, "  @   @   @  @   @      @  @  @  @   @  @  @@");
    149 	mvaddstr(14, 11, "  @    @@@    @@@        @@ @@    @@@   @   @   @");
    150 	mvaddstr(17, 11, "Congratulations,  you have  been admitted  to  the");
    151 	mvaddstr(18, 11, "Fighters' Guild.   You return home,  sell all your");
    152 	mvaddstr(19, 11, "treasures at great profit and retire into comfort.");
    153 	message("", 0);
    154 	message("", 0);
    155 	id_all();
    156 	sell_pack();
    157 	put_scores((object *) 0, WIN);
    158 }
    159 
    160 quit(from_intrpt)
    161 boolean from_intrpt;
    162 {
    163 	char buf[128];
    164 	short i, orow, ocol;
    165 	boolean mc;
    166 
    167 	md_ignore_signals();
    168 
    169 	if (from_intrpt) {
    170 		orow = rogue.row;
    171 		ocol = rogue.col;
    172 
    173 		mc = msg_cleared;
    174 
    175 		for (i = 0; i < DCOLS; i++) {
    176 			buf[i] = mvinch(0, i);
    177 		}
    178 	}
    179 	check_message();
    180 	message("really quit?", 1);
    181 	if (rgetchar() != 'y') {
    182 		md_heed_signals();
    183 		check_message();
    184 		if (from_intrpt) {
    185 			for (i = 0; i < DCOLS; i++) {
    186 				mvaddch(0, i, buf[i]);
    187 			}
    188 			msg_cleared = mc;
    189 			move(orow, ocol);
    190 			refresh();
    191 		}
    192 		return;
    193 	}
    194 	if (from_intrpt) {
    195 		clean_up(byebye_string);
    196 	}
    197 	check_message();
    198 	killed_by((object *) 0, QUIT);
    199 }
    200 
    201 put_scores(monster, other)
    202 object *monster;
    203 short other;
    204 {
    205 	short i, n, rank = 10, x, ne = 0, found_player = -1;
    206 	char scores[10][82];
    207 	char n_names[10][30];
    208 	char buf[128];
    209 	FILE *fp;
    210 	long s;
    211 	boolean pause = score_only;
    212 
    213 	md_lock(1);
    214 
    215 	if ((fp = fopen(_PATH_SCOREFILE, "r+")) == NULL &&
    216 	    (fp = fopen(_PATH_SCOREFILE, "w+")) == NULL) {
    217 		message("cannot read/write/create score file", 0);
    218 		sf_error();
    219 	}
    220 	rewind(fp);
    221 	(void) xxx(1);
    222 
    223 	for (i = 0; i < 10; i++) {
    224 		if (((n = fread(scores[i], sizeof(char), 80, fp)) < 80) && (n != 0)) {
    225 			sf_error();
    226 		} else if (n != 0) {
    227 			xxxx(scores[i], 80);
    228 			if ((n = fread(n_names[i], sizeof(char), 30, fp)) < 30) {
    229 				sf_error();
    230 			}
    231 			xxxx(n_names[i], 30);
    232 		} else {
    233 			break;
    234 		}
    235 		ne++;
    236 		if ((!score_only) && (found_player == -1)) {
    237 			if (!name_cmp(scores[i]+15, login_name)) {
    238 				x = 5;
    239 				while (scores[i][x] == ' ') {
    240 					x++;
    241 				}
    242 				s = lget_number(scores[i] + x);
    243 				if (rogue.gold < s) {
    244 					score_only = 1;
    245 				} else {
    246 					found_player = i;
    247 				}
    248 			}
    249 		}
    250 	}
    251 	if (found_player != -1) {
    252 		ne--;
    253 		for (i = found_player; i < ne; i++) {
    254 			(void) strcpy(scores[i], scores[i+1]);
    255 			(void) strcpy(n_names[i], n_names[i+1]);
    256 		}
    257 	}
    258 	if (!score_only) {
    259 		for (i = 0; i < ne; i++) {
    260 			x = 5;
    261 			while (scores[i][x] == ' ') {
    262 				x++;
    263 			}
    264 			s = lget_number(scores[i] + x);
    265 
    266 			if (rogue.gold >= s) {
    267 				rank = i;
    268 				break;
    269 			}
    270 		}
    271 		if (ne == 0) {
    272 			rank = 0;
    273 		} else if ((ne < 10) && (rank == 10)) {
    274 			rank = ne;
    275 		}
    276 		if (rank < 10) {
    277 			insert_score(scores, n_names, nick_name, rank, ne, monster,
    278 				other);
    279 			if (ne < 10) {
    280 				ne++;
    281 			}
    282 		}
    283 		rewind(fp);
    284 	}
    285 
    286 	clear();
    287 	mvaddstr(3, 30, "Top  Ten  Rogueists");
    288 	mvaddstr(8, 0, "Rank   Score   Name");
    289 
    290 	md_ignore_signals();
    291 
    292 	(void) xxx(1);
    293 
    294 	for (i = 0; i < ne; i++) {
    295 		if (i == rank) {
    296 			standout();
    297 		}
    298 		if (i == 9) {
    299 			scores[i][0] = '1';
    300 			scores[i][1] = '0';
    301 		} else {
    302 			scores[i][0] = ' ';
    303 			scores[i][1] = i + '1';
    304 		}
    305 		nickize(buf, scores[i], n_names[i]);
    306 		mvaddstr(i+10, 0, buf);
    307 		if (rank < 10) {
    308 			xxxx(scores[i], 80);
    309 			fwrite(scores[i], sizeof(char), 80, fp);
    310 			xxxx(n_names[i], 30);
    311 			fwrite(n_names[i], sizeof(char), 30, fp);
    312 		}
    313 		if (i == rank) {
    314 			standend();
    315 		}
    316 	}
    317 	md_lock(0);
    318 	refresh();
    319 	fclose(fp);
    320 	message("", 0);
    321 	if (pause) {
    322 		message("", 0);
    323 	}
    324 	clean_up("");
    325 }
    326 
    327 insert_score(scores, n_names, n_name, rank, n, monster, other)
    328 char scores[][82];
    329 char n_names[][30];
    330 char *n_name;
    331 short rank, n;
    332 object *monster;
    333 {
    334 	short i;
    335 	char buf[128];
    336 
    337 	if (n > 0) {
    338 		for (i = n; i > rank; i--) {
    339 			if ((i < 10) && (i > 0)) {
    340 				(void) strcpy(scores[i], scores[i-1]);
    341 				(void) strcpy(n_names[i], n_names[i-1]);
    342 			}
    343 		}
    344 	}
    345 	sprintf(buf, "%2d    %6d   %s: ", rank+1, rogue.gold, login_name);
    346 
    347 	if (other) {
    348 		switch(other) {
    349 		case HYPOTHERMIA:
    350 			(void) strcat(buf, "died of hypothermia");
    351 			break;
    352 		case STARVATION:
    353 			(void) strcat(buf, "died of starvation");
    354 			break;
    355 		case POISON_DART:
    356 			(void) strcat(buf, "killed by a dart");
    357 			break;
    358 		case QUIT:
    359 			(void) strcat(buf, "quit");
    360 			break;
    361 		case WIN:
    362 			(void) strcat(buf, "a total winner");
    363 			break;
    364 		case KFIRE:
    365 			(void) strcpy(buf, "killed by fire");
    366 			break;
    367 		}
    368 	} else {
    369 		(void) strcat(buf, "killed by ");
    370 		if (is_vowel(m_names[monster->m_char - 'A'][0])) {
    371 			(void) strcat(buf, "an ");
    372 		} else {
    373 			(void) strcat(buf, "a ");
    374 		}
    375 		(void) strcat(buf, m_names[monster->m_char - 'A']);
    376 	}
    377 	sprintf(buf+strlen(buf), " on level %d ",  max_level);
    378 	if ((other != WIN) && has_amulet()) {
    379 		(void) strcat(buf, "with amulet");
    380 	}
    381 	for (i = strlen(buf); i < 79; i++) {
    382 		buf[i] = ' ';
    383 	}
    384 	buf[79] = 0;
    385 	(void) strcpy(scores[rank], buf);
    386 	(void) strcpy(n_names[rank], n_name);
    387 }
    388 
    389 is_vowel(ch)
    390 short ch;
    391 {
    392 	return( (ch == 'a') ||
    393 		(ch == 'e') ||
    394 		(ch == 'i') ||
    395 		(ch == 'o') ||
    396 		(ch == 'u') );
    397 }
    398 
    399 sell_pack()
    400 {
    401 	object *obj;
    402 	short row = 2, val;
    403 	char buf[DCOLS];
    404 
    405 	obj = rogue.pack.next_object;
    406 
    407 	clear();
    408 	mvaddstr(1, 0, "Value      Item");
    409 
    410 	while (obj) {
    411 		if (obj->what_is != FOOD) {
    412 			obj->identified = 1;
    413 			val = get_value(obj);
    414 			rogue.gold += val;
    415 
    416 			if (row < DROWS) {
    417 				sprintf(buf, "%5d      ", val);
    418 				get_desc(obj, buf+11);
    419 				mvaddstr(row++, 0, buf);
    420 			}
    421 		}
    422 		obj = obj->next_object;
    423 	}
    424 	refresh();
    425 	if (rogue.gold > MAX_GOLD) {
    426 		rogue.gold = MAX_GOLD;
    427 	}
    428 	message("", 0);
    429 }
    430 
    431 get_value(obj)
    432 object *obj;
    433 {
    434 	short wc;
    435 	int val;
    436 
    437 	wc = obj->which_kind;
    438 
    439 	switch(obj->what_is) {
    440 	case WEAPON:
    441 		val = id_weapons[wc].value;
    442 		if ((wc == ARROW) || (wc == DAGGER) || (wc == SHURIKEN) ||
    443 			(wc == DART)) {
    444 			val *= obj->quantity;
    445 		}
    446 		val += (obj->d_enchant * 85);
    447 		val += (obj->hit_enchant * 85);
    448 		break;
    449 	case ARMOR:
    450 		val = id_armors[wc].value;
    451 		val += (obj->d_enchant * 75);
    452 		if (obj->is_protected) {
    453 			val += 200;
    454 		}
    455 		break;
    456 	case WAND:
    457 		val = id_wands[wc].value * (obj->class + 1);
    458 		break;
    459 	case SCROL:
    460 		val = id_scrolls[wc].value * obj->quantity;
    461 		break;
    462 	case POTION:
    463 		val = id_potions[wc].value * obj->quantity;
    464 		break;
    465 	case AMULET:
    466 		val = 5000;
    467 		break;
    468 	case RING:
    469 		val = id_rings[wc].value * (obj->class + 1);
    470 		break;
    471 	}
    472 	if (val <= 0) {
    473 		val = 10;
    474 	}
    475 	return(val);
    476 }
    477 
    478 id_all()
    479 {
    480 	short i;
    481 
    482 	for (i = 0; i < SCROLS; i++) {
    483 		id_scrolls[i].id_status = IDENTIFIED;
    484 	}
    485 	for (i = 0; i < WEAPONS; i++) {
    486 		id_weapons[i].id_status = IDENTIFIED;
    487 	}
    488 	for (i = 0; i < ARMORS; i++) {
    489 		id_armors[i].id_status = IDENTIFIED;
    490 	}
    491 	for (i = 0; i < WANDS; i++) {
    492 		id_wands[i].id_status = IDENTIFIED;
    493 	}
    494 	for (i = 0; i < POTIONS; i++) {
    495 		id_potions[i].id_status = IDENTIFIED;
    496 	}
    497 }
    498 
    499 name_cmp(s1, s2)
    500 char *s1, *s2;
    501 {
    502 	short i = 0;
    503 	int r;
    504 
    505 	while(s1[i] != ':') {
    506 		i++;
    507 	}
    508 	s1[i] = 0;
    509 	r = strcmp(s1, s2);
    510 	s1[i] = ':';
    511 	return(r);
    512 }
    513 
    514 xxxx(buf, n)
    515 char *buf;
    516 short n;
    517 {
    518 	short i;
    519 	unsigned char c;
    520 
    521 	for (i = 0; i < n; i++) {
    522 
    523 		/* It does not matter if accuracy is lost during this assignment */
    524 		c = (unsigned char) xxx(0);
    525 
    526 		buf[i] ^= c;
    527 	}
    528 }
    529 
    530 long
    531 xxx(st)
    532 boolean st;
    533 {
    534 	static long f, s;
    535 	long r;
    536 
    537 	if (st) {
    538 		f = 37;
    539 		s = 7;
    540 		return(0L);
    541 	}
    542 	r = ((f * s) + 9337) % 8887;
    543 	f = s;
    544 	s = r;
    545 	return(r);
    546 }
    547 
    548 nickize(buf, score, n_name)
    549 char *buf, *score, *n_name;
    550 {
    551 	short i = 15, j;
    552 
    553 	if (!n_name[0]) {
    554 		(void) strcpy(buf, score);
    555 	} else {
    556 		(void) strncpy(buf, score, 16);
    557 
    558 		while (score[i] != ':') {
    559 			i++;
    560 		}
    561 
    562 		(void) strcpy(buf+15, n_name);
    563 		j = strlen(buf);
    564 
    565 		while (score[i]) {
    566 			buf[j++] = score[i++];
    567 		}
    568 		buf[j] = 0;
    569 		buf[79] = 0;
    570 	}
    571 }
    572 
    573 center(row, buf)
    574 short row;
    575 char *buf;
    576 {
    577 	short margin;
    578 
    579 	margin = ((DCOLS - strlen(buf)) / 2);
    580 	mvaddstr(row, margin, buf);
    581 }
    582 
    583 sf_error()
    584 {
    585 	md_lock(0);
    586 	message("", 1);
    587 	clean_up("sorry, score file is out of order");
    588 }
    589