Unshadowed k2 into k3 in main.c
[open-adventure.git] / score.c
1 #include <stdlib.h>
2 #include "advent.h"
3 #include "dungeon.h"
4
5 /*
6  * scoring and wrap-up
7  */
8
9 static long mxscor;     /* ugh..the price for having score() not exit. */
10
11 long score(enum termination mode)
12 /* mode is 'scoregame' if scoring, 'quitgame' if quitting, 'endgame' if died
13  * or won */
14 {
15     long score = 0;
16
17     /*  The present scoring algorithm is as follows:
18      *     Objective:          Points:        Present total possible:
19      *  Getting well into cave   25                    25
20      *  Each treasure < chest    12                    60
21      *  Treasure chest itself    14                    14
22      *  Each treasure > chest    16                   224
23      *  Surviving             (MAX-NUM)*10             30
24      *  Not quitting              4                     4
25      *  Reaching "game.closng"   25                    25
26      *  "Closed": Quit/Killed    10
27      *            Klutzed        25
28      *            Wrong way      30
29      *            Success        45                    45
30      *  Came to Witt's End        1                     1
31      *  Round out the total       2                     2
32      *                                       TOTAL:   430
33      *  Points can also be deducted for using hints or too many turns, or for
34      *  saving intermediate positions. */
35
36     /*  First tally up the treasures.  Must be in building and not broken.
37      *  Give the poor guy 2 points just for finding each treasure. */
38     mxscor = 0;
39     for (int i = 1; i <= NOBJECTS; i++) {
40         if (!objects[i].is_treasure)
41             continue;
42         if (objects[i].inventory != 0) {
43             long k = 12;
44             if (i == CHEST)k = 14;
45             if (i > CHEST)k = 16;
46             if (game.prop[i] >= 0)
47                 score += 2;
48             if (game.place[i] == LOC_BUILDING && game.prop[i] == 0)
49                 score += k - 2;
50             mxscor += k;
51         }
52     }
53
54     /*  Now look at how he finished and how far he got.  NDEATHS and
55      *  game.numdie tell us how well he survived.  game.dflag will tell us
56      *  if he ever got suitably deep into the cave.  game.closng still
57      *  indicates whether he reached the endgame.  And if he got as far as
58      *  "cave closed" (indicated by "game.closed"), then bonus is zero for
59      *  mundane exits or 133, 134, 135 if he blew it (so to speak). */
60     score += (NDEATHS - game.numdie) * 10;
61     mxscor += NDEATHS * 10;
62     if (mode == endgame)
63         score += 4;
64     mxscor += 4;
65     if (game.dflag != 0)score += 25;
66     mxscor += 25;
67     if (game.closng)score += 25;
68     mxscor += 25;
69     if (game.closed) {
70         if (game.bonus == 0)
71             score += 10;
72         if (game.bonus == SPLATTER_MESSAGE)
73             score += 25;
74         if (game.bonus == DEFEAT_MESSAGE)
75             score += 30;
76         if (game.bonus == VICTORY_MESSAGE)
77             score += 45;
78     }
79     mxscor += 45;
80
81     /* Did he come to Witt's End as he should? */
82     if (game.place[MAGAZINE] == LOC_WITTSEND)
83         score += 1;
84     mxscor += 1;
85
86     /* Round it off. */
87     score += 2;
88     mxscor += 2;
89
90     /* Deduct for hints/turns/saves. Hints < 4 are special; see database desc. */
91     for (long i = 0; i < NHINTS; i++) {
92         if (game.hinted[i])
93             score = score - hints[i].penalty;
94     }
95     if (game.novice)
96         score -= 5;
97     if (game.clshnt)
98         score -= 10;
99     score = score - game.trnluz - game.saved;
100
101     /* Return to score command if that's where we came from. */
102     if (mode == scoregame) {
103         rspeak(GARNERED_POINTS, score, mxscor, game.turns, game.turns);
104     }
105
106     return score;
107 }
108
109 void terminate(enum termination mode)
110 /* End of game.  Let's tell him all about it. */
111 {
112     long points = score(mode);
113
114     if (points + game.trnluz + 1 >= mxscor && game.trnluz != 0)
115         rspeak(TOOK_LONG);
116     if (points + game.saved + 1 >= mxscor && game.saved != 0)
117         rspeak(WITHOUT_SUSPENDS);
118     rspeak(TOTAL_SCORE, points, mxscor, game.turns, game.turns);
119     for (long i = 1; i <= (long)NCLASSES; i++) {
120         if (classes[i].threshold >= points) {
121             speak(classes[i].message);
122             i = classes[i].threshold + 1 - points;
123             rspeak(NEXT_HIGHER, i, i);
124             exit(0);
125         }
126     }
127     rspeak(OFF_SCALE);
128     rspeak(NO_HIGHER);
129     exit(0);
130 }
131
132 /* end */