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