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