Home | History | Annotate | Line # | Download | only in rogue
score.c revision 1.6
      1 /*	$NetBSD: score.c,v 1.6 1997/10/12 11:46:01 lukem 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 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)score.c	8.1 (Berkeley) 5/31/93";
     43 #else
     44 __RCSID("$NetBSD: score.c,v 1.6 1997/10/12 11:46:01 lukem Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * score.c
     50  *
     51  * This source herein may be modified and/or distributed by anybody who
     52  * so desires, with the following restrictions:
     53  *    1.)  No portion of this notice shall be removed.
     54  *    2.)  Credit shall not be taken for the creation of this source.
     55  *    3.)  This code is not to be traded, sold, or used for personal
     56  *         gain or profit.
     57  *
     58  */
     59 
     60 #include <stdio.h>
     61 #include "rogue.h"
     62 #include "pathnames.h"
     63 
     64 void
     65 killed_by(monster, other)
     66 	object *monster;
     67 	short other;
     68 {
     69 	char buf[128];
     70 
     71 	md_ignore_signals();
     72 
     73 	if (other != QUIT) {
     74 		rogue.gold = ((rogue.gold * 9) / 10);
     75 	}
     76 
     77 	if (other) {
     78 		switch(other) {
     79 		case HYPOTHERMIA:
     80 			(void) strcpy(buf, "died of hypothermia");
     81 			break;
     82 		case STARVATION:
     83 			(void) strcpy(buf, "died of starvation");
     84 			break;
     85 		case POISON_DART:
     86 			(void) strcpy(buf, "killed by a dart");
     87 			break;
     88 		case QUIT:
     89 			(void) strcpy(buf, "quit");
     90 			break;
     91 		case KFIRE:
     92 			(void) strcpy(buf, "killed by fire");
     93 			break;
     94 		}
     95 	} else {
     96 		(void) strcpy(buf, "Killed by ");
     97 		if (is_vowel(m_names[monster->m_char - 'A'][0])) {
     98 			(void) strcat(buf, "an ");
     99 		} else {
    100 			(void) strcat(buf, "a ");
    101 		}
    102 		(void) strcat(buf, m_names[monster->m_char - 'A']);
    103 	}
    104 	(void) strcat(buf, " with ");
    105 	sprintf(buf+strlen(buf), "%ld gold", rogue.gold);
    106 	if ((!other) && (!no_skull)) {
    107 		clear();
    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, buf);
    126 	} else {
    127 		message(buf, 0);
    128 	}
    129 	message("", 0);
    130 	put_scores(monster, other);
    131 }
    132 
    133 void
    134 win()
    135 {
    136 	unwield(rogue.weapon);		/* disarm and relax */
    137 	unwear(rogue.armor);
    138 	un_put_on(rogue.left_ring);
    139 	un_put_on(rogue.right_ring);
    140 
    141 	clear();
    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 	message("", 0);
    151 	message("", 0);
    152 	id_all();
    153 	sell_pack();
    154 	put_scores((object *) 0, WIN);
    155 }
    156 
    157 void
    158 quit(from_intrpt)
    159 	boolean from_intrpt;
    160 {
    161 	char buf[128];
    162 	short i, orow, ocol;
    163 	boolean mc;
    164 
    165 	orow = ocol = 0;
    166 	mc = FALSE;
    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 void
    202 put_scores(monster, other)
    203 	object *monster;
    204 	short other;
    205 {
    206 	short i, n, rank = 10, x, ne = 0, found_player = -1;
    207 	char scores[10][82];
    208 	char n_names[10][30];
    209 	char buf[128];
    210 	FILE *fp;
    211 	long s;
    212 	boolean pause = score_only;
    213 
    214 	md_lock(1);
    215 
    216 	if ((fp = fopen(_PATH_SCOREFILE, "r+")) == NULL &&
    217 	    (fp = fopen(_PATH_SCOREFILE, "w+")) == NULL) {
    218 		message("cannot read/write/create score file", 0);
    219 		sf_error();
    220 	}
    221 	rewind(fp);
    222 	(void) xxx(1);
    223 
    224 	for (i = 0; i < 10; i++) {
    225 		if (((n = fread(scores[i], sizeof(char), 80, fp)) < 80) && (n != 0)) {
    226 			sf_error();
    227 		} else if (n != 0) {
    228 			xxxx(scores[i], 80);
    229 			if ((n = fread(n_names[i], sizeof(char), 30, fp)) < 30) {
    230 				sf_error();
    231 			}
    232 			xxxx(n_names[i], 30);
    233 		} else {
    234 			break;
    235 		}
    236 		ne++;
    237 		if ((!score_only) && (found_player == -1)) {
    238 			if (!name_cmp(scores[i]+15, login_name)) {
    239 				x = 5;
    240 				while (scores[i][x] == ' ') {
    241 					x++;
    242 				}
    243 				s = lget_number(scores[i] + x);
    244 				if (rogue.gold < s) {
    245 					score_only = 1;
    246 				} else {
    247 					found_player = i;
    248 				}
    249 			}
    250 		}
    251 	}
    252 	if (found_player != -1) {
    253 		ne--;
    254 		for (i = found_player; i < ne; i++) {
    255 			(void) strcpy(scores[i], scores[i+1]);
    256 			(void) strcpy(n_names[i], n_names[i+1]);
    257 		}
    258 	}
    259 	if (!score_only) {
    260 		for (i = 0; i < ne; i++) {
    261 			x = 5;
    262 			while (scores[i][x] == ' ') {
    263 				x++;
    264 			}
    265 			s = lget_number(scores[i] + x);
    266 
    267 			if (rogue.gold >= s) {
    268 				rank = i;
    269 				break;
    270 			}
    271 		}
    272 		if (ne == 0) {
    273 			rank = 0;
    274 		} else if ((ne < 10) && (rank == 10)) {
    275 			rank = ne;
    276 		}
    277 		if (rank < 10) {
    278 			insert_score(scores, n_names, nick_name, rank, ne,
    279 			    monster, other);
    280 			if (ne < 10) {
    281 				ne++;
    282 			}
    283 		}
    284 		rewind(fp);
    285 	}
    286 
    287 	clear();
    288 	mvaddstr(3, 30, "Top  Ten  Rogueists");
    289 	mvaddstr(8, 0, "Rank   Score   Name");
    290 
    291 	md_ignore_signals();
    292 
    293 	(void) xxx(1);
    294 
    295 	for (i = 0; i < ne; i++) {
    296 		if (i == rank) {
    297 			standout();
    298 		}
    299 		if (i == 9) {
    300 			scores[i][0] = '1';
    301 			scores[i][1] = '0';
    302 		} else {
    303 			scores[i][0] = ' ';
    304 			scores[i][1] = i + '1';
    305 		}
    306 		nickize(buf, scores[i], n_names[i]);
    307 		mvaddstr(i+10, 0, buf);
    308 		if (rank < 10) {
    309 			xxxx(scores[i], 80);
    310 			fwrite(scores[i], sizeof(char), 80, fp);
    311 			xxxx(n_names[i], 30);
    312 			fwrite(n_names[i], sizeof(char), 30, fp);
    313 		}
    314 		if (i == rank) {
    315 			standend();
    316 		}
    317 	}
    318 	md_lock(0);
    319 	refresh();
    320 	fclose(fp);
    321 	message("", 0);
    322 	if (pause) {
    323 		message("", 0);
    324 	}
    325 	clean_up("");
    326 }
    327 
    328 void
    329 insert_score(scores, n_names, n_name, rank, n, monster, other)
    330 	char scores[][82];
    331 	char n_names[][30];
    332 	char *n_name;
    333 	short rank, n;
    334 	object *monster;
    335 	int other;
    336 {
    337 	short i;
    338 	char buf[128];
    339 
    340 	if (n > 0) {
    341 		for (i = n; i > rank; i--) {
    342 			if ((i < 10) && (i > 0)) {
    343 				(void) strcpy(scores[i], scores[i-1]);
    344 				(void) strcpy(n_names[i], n_names[i-1]);
    345 			}
    346 		}
    347 	}
    348 	sprintf(buf, "%2d    %6ld   %s: ", rank+1, (long)rogue.gold,
    349 	    login_name);
    350 
    351 	if (other) {
    352 		switch(other) {
    353 		case HYPOTHERMIA:
    354 			(void) strcat(buf, "died of hypothermia");
    355 			break;
    356 		case STARVATION:
    357 			(void) strcat(buf, "died of starvation");
    358 			break;
    359 		case POISON_DART:
    360 			(void) strcat(buf, "killed by a dart");
    361 			break;
    362 		case QUIT:
    363 			(void) strcat(buf, "quit");
    364 			break;
    365 		case WIN:
    366 			(void) strcat(buf, "a total winner");
    367 			break;
    368 		case KFIRE:
    369 			(void) strcpy(buf, "killed by fire");
    370 			break;
    371 		}
    372 	} else {
    373 		(void) strcat(buf, "killed by ");
    374 		if (is_vowel(m_names[monster->m_char - 'A'][0])) {
    375 			(void) strcat(buf, "an ");
    376 		} else {
    377 			(void) strcat(buf, "a ");
    378 		}
    379 		(void) strcat(buf, m_names[monster->m_char - 'A']);
    380 	}
    381 	sprintf(buf+strlen(buf), " on level %d ",  max_level);
    382 	if ((other != WIN) && has_amulet()) {
    383 		(void) strcat(buf, "with amulet");
    384 	}
    385 	for (i = strlen(buf); i < 79; i++) {
    386 		buf[i] = ' ';
    387 	}
    388 	buf[79] = 0;
    389 	(void) strcpy(scores[rank], buf);
    390 	(void) strcpy(n_names[rank], n_name);
    391 }
    392 
    393 boolean
    394 is_vowel(ch)
    395 	short ch;
    396 {
    397 	return( (ch == 'a') ||
    398 		(ch == 'e') ||
    399 		(ch == 'i') ||
    400 		(ch == 'o') ||
    401 		(ch == 'u') );
    402 }
    403 
    404 void
    405 sell_pack()
    406 {
    407 	object *obj;
    408 	short row = 2, val;
    409 	char buf[DCOLS];
    410 
    411 	obj = rogue.pack.next_object;
    412 
    413 	clear();
    414 	mvaddstr(1, 0, "Value      Item");
    415 
    416 	while (obj) {
    417 		if (obj->what_is != FOOD) {
    418 			obj->identified = 1;
    419 			val = get_value(obj);
    420 			rogue.gold += val;
    421 
    422 			if (row < DROWS) {
    423 				sprintf(buf, "%5d      ", val);
    424 				get_desc(obj, buf+11);
    425 				mvaddstr(row++, 0, buf);
    426 			}
    427 		}
    428 		obj = obj->next_object;
    429 	}
    430 	refresh();
    431 	if (rogue.gold > MAX_GOLD) {
    432 		rogue.gold = MAX_GOLD;
    433 	}
    434 	message("", 0);
    435 }
    436 
    437 int
    438 get_value(obj)
    439 	object *obj;
    440 {
    441 	short wc;
    442 	int val;
    443 
    444 	val = 0;
    445 	wc = obj->which_kind;
    446 
    447 	switch(obj->what_is) {
    448 	case WEAPON:
    449 		val = id_weapons[wc].value;
    450 		if ((wc == ARROW) || (wc == DAGGER) || (wc == SHURIKEN) ||
    451 			(wc == DART)) {
    452 			val *= obj->quantity;
    453 		}
    454 		val += (obj->d_enchant * 85);
    455 		val += (obj->hit_enchant * 85);
    456 		break;
    457 	case ARMOR:
    458 		val = id_armors[wc].value;
    459 		val += (obj->d_enchant * 75);
    460 		if (obj->is_protected) {
    461 			val += 200;
    462 		}
    463 		break;
    464 	case WAND:
    465 		val = id_wands[wc].value * (obj->class + 1);
    466 		break;
    467 	case SCROL:
    468 		val = id_scrolls[wc].value * obj->quantity;
    469 		break;
    470 	case POTION:
    471 		val = id_potions[wc].value * obj->quantity;
    472 		break;
    473 	case AMULET:
    474 		val = 5000;
    475 		break;
    476 	case RING:
    477 		val = id_rings[wc].value * (obj->class + 1);
    478 		break;
    479 	}
    480 	if (val <= 0) {
    481 		val = 10;
    482 	}
    483 	return(val);
    484 }
    485 
    486 void
    487 id_all()
    488 {
    489 	short i;
    490 
    491 	for (i = 0; i < SCROLS; i++) {
    492 		id_scrolls[i].id_status = IDENTIFIED;
    493 	}
    494 	for (i = 0; i < WEAPONS; i++) {
    495 		id_weapons[i].id_status = IDENTIFIED;
    496 	}
    497 	for (i = 0; i < ARMORS; i++) {
    498 		id_armors[i].id_status = IDENTIFIED;
    499 	}
    500 	for (i = 0; i < WANDS; i++) {
    501 		id_wands[i].id_status = IDENTIFIED;
    502 	}
    503 	for (i = 0; i < POTIONS; i++) {
    504 		id_potions[i].id_status = IDENTIFIED;
    505 	}
    506 }
    507 
    508 int
    509 name_cmp(s1, s2)
    510 	char *s1, *s2;
    511 {
    512 	short i = 0;
    513 	int r;
    514 
    515 	while(s1[i] != ':') {
    516 		i++;
    517 	}
    518 	s1[i] = 0;
    519 	r = strcmp(s1, s2);
    520 	s1[i] = ':';
    521 	return(r);
    522 }
    523 
    524 void
    525 xxxx(buf, n)
    526 	char *buf;
    527 	short n;
    528 {
    529 	short i;
    530 	unsigned char c;
    531 
    532 	for (i = 0; i < n; i++) {
    533 
    534 		/* It does not matter if accuracy is lost during this assignment */
    535 		c = (unsigned char) xxx(0);
    536 
    537 		buf[i] ^= c;
    538 	}
    539 }
    540 
    541 long
    542 xxx(st)
    543 	boolean st;
    544 {
    545 	static long f, s;
    546 	long r;
    547 
    548 	if (st) {
    549 		f = 37;
    550 		s = 7;
    551 		return(0L);
    552 	}
    553 	r = ((f * s) + 9337) % 8887;
    554 	f = s;
    555 	s = r;
    556 	return(r);
    557 }
    558 
    559 void
    560 nickize(buf, score, n_name)
    561 	char *buf, *score, *n_name;
    562 {
    563 	short i = 15, j;
    564 
    565 	if (!n_name[0]) {
    566 		(void) strcpy(buf, score);
    567 	} else {
    568 		(void) strncpy(buf, score, 16);
    569 
    570 		while (score[i] != ':') {
    571 			i++;
    572 		}
    573 
    574 		(void) strcpy(buf+15, n_name);
    575 		j = strlen(buf);
    576 
    577 		while (score[i]) {
    578 			buf[j++] = score[i++];
    579 		}
    580 		buf[j] = 0;
    581 		buf[79] = 0;
    582 	}
    583 }
    584 
    585 void
    586 center(row, buf)
    587 	short row;
    588 	char *buf;
    589 {
    590 	short margin;
    591 
    592 	margin = ((DCOLS - strlen(buf)) / 2);
    593 	mvaddstr(row, margin, buf);
    594 }
    595 
    596 void
    597 sf_error()
    598 {
    599 	md_lock(0);
    600 	message("", 1);
    601 	clean_up("sorry, score file is out of order");
    602 }
    603