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