Updates for a new interface to the networking
[crack-attack.git] / src / Score.cxx
blob697588921fe17f6fc4c7d4c5131a18a498177c83
1 /*
2 * Score.cxx
3 * Daniel Nelson - 12/3/1
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
26 * Handles the score for solo games.
29 #include <cstring>
30 #include <fstream>
32 using namespace std;
34 #include "TextureLoader.h"
35 #include "Game.h"
36 #include "MetaState.h"
37 #include "Score.h"
39 int Score::score;
40 int Score::backlog;
41 int Score::top_combo_multiplier;
42 int Score::top_combo_score;
43 ComboRank Score::combo_record[GC_SCORE_MULT_LENGTH];
44 short Score::digits[GC_NUMBER_DIGITS];
45 short Score::previous_digits[GC_NUMBER_DIGITS];
46 int Score::fade_timer;
47 GLfloat Score::inverse_timer_start;
48 int Score::n_digits_displayed;
49 int Score::special_block_scores[BF_NUMBER_SPECIAL]
50 = { 30, // black
51 30, // white
52 5, // special purple
53 5, // special blue
54 8, // special green
55 15, // special yellow
56 10, }; // special orange
58 int Score::player_rank;
59 Rank Score::record[GC_SCORE_REC_LENGTH];
61 void Score::initialize ( )
63 if (!(MetaState::mode & CM_SOLO)) return;
65 score = backlog = top_combo_multiplier = top_combo_score = 0;
66 n_digits_displayed = GC_MIN_NUMBER_DIGITS_DISPLAYED;
67 for (int n = GC_NUMBER_DIGITS; n--; ) {
68 digits[n] = previous_digits[n] = 0;
69 fade_timer = 0;
70 inverse_timer_start = 1.0f;
72 player_rank = -1;
74 if (!readScoreRecord()) setupDefaultScoreRecord();
75 if (!readMultRecord()) setupDefaultMultRecord();
78 void Score::cleanUp ( )
80 if ((MetaState::mode & CM_SOLO) && player_rank != -1)
81 writeScoreRecord();
84 int Score::gameFinish ( )
86 // first do non-victory deciding scores
87 for (int n = GC_SCORE_MULT_LENGTH; n--; )
88 if (top_combo_multiplier > combo_record[n].multiplier) {
89 player_rank = n;
91 // insert player
92 for (int n = 0; n < player_rank; n++) {
93 strncpy(combo_record[n].name, combo_record[n + 1].name, GC_PLAYER_NAME_LENGTH);
94 combo_record[n].multiplier = combo_record[n + 1].multiplier;
96 strncpy(combo_record[player_rank].name, MetaState::player_name,
97 GC_PLAYER_NAME_LENGTH);
98 combo_record[player_rank].multiplier = top_combo_multiplier;
99 break;
102 // now decide the winner
103 for (int n = GC_SCORE_REC_LENGTH; n--; )
104 if (score > record[n].score) {
105 player_rank = n;
107 // insert player
108 for (int n = 0; n < player_rank; n++) {
109 strncpy(record[n].name, record[n + 1].name, GC_PLAYER_NAME_LENGTH);
110 record[n].score = record[n + 1].score;
112 strncpy(record[player_rank].name, MetaState::player_name,
113 GC_PLAYER_NAME_LENGTH);
114 record[player_rank].score = score;
116 return GS_WON;
118 return GS_LOST;
121 bool Score::readMultRecord ( )
123 char file_name[256];
124 char buffer[256];
125 TextureLoader::buildLocalDataFileName(GC_MULT_FILE_NAME, file_name);
126 ifstream new_file(file_name);
127 if (new_file.fail()) return false;
129 for (int n = GC_SCORE_MULT_LENGTH; n--; ) {
130 new_file.getline(combo_record[n].name, GC_PLAYER_NAME_LENGTH);
131 new_file.getline(buffer, 256);
132 combo_record[n].multiplier = atoi(buffer);
133 if (new_file.fail()) return false;
135 return true;
138 bool Score::readScoreRecord ( )
140 char file_name[256];
141 TextureLoader::buildLocalDataFileName((MetaState::mode & CM_X)
142 ? GC_X_REC_FILE_NAME : GC_REC_FILE_NAME, file_name);
143 ifstream file(file_name);
144 if (file.fail()) return false;
146 char buffer[256];
147 for (int n = GC_SCORE_REC_LENGTH; n--; ) {
148 file.getline(record[n].name, GC_PLAYER_NAME_LENGTH);
149 file.getline(buffer, 256);
150 record[n].score = atoi(buffer);
151 if (file.fail()) return false;
153 return true;
156 void Score::writeScoreRecord ( )
158 char file_name[256];
159 TextureLoader::buildLocalDataFileName((MetaState::mode & CM_X)
160 ? GC_X_REC_FILE_NAME : GC_REC_FILE_NAME, file_name);
161 ofstream file(file_name);
162 if (file.fail()) {
163 cerr << "Error writing to score record file '" << file_name << "'." << endl;
164 exit(1);
167 for (int n = GC_SCORE_REC_LENGTH; n--; )
168 file << record[n].name << '\n' << record[n].score << '\n';
169 file.close();
171 // mult record
172 TextureLoader::buildLocalDataFileName(GC_MULT_FILE_NAME, file_name);
173 ofstream mult(file_name);
174 if (mult.fail()) {
175 cerr << "Error writing to score record file '" << file_name << "'." << endl;
176 exit(1);
178 for (int n = GC_SCORE_MULT_LENGTH; n--; )
179 mult << combo_record[n].name << '\n' << combo_record[n].multiplier << '\n';
180 mult.close();
184 void Score::setupDefaultMultRecord ( )
186 ifstream new_file(GC_DEFAULT_MULT_FILE_NAME);
187 if (new_file.fail()) {
188 cerr << "Error opening data file '" << GC_DEFAULT_MULT_FILE_NAME << "'." << endl;
189 exit(1);
192 char buffer[256];
193 for (int n = GC_SCORE_MULT_LENGTH; n--; ) {
194 new_file.getline(combo_record[n].name, GC_PLAYER_NAME_LENGTH);
195 new_file.getline(buffer, 256);
196 combo_record[n].multiplier = atoi(buffer);
197 if (new_file.fail()) break;
199 writeScoreRecord();
202 void Score::setupDefaultScoreRecord ( )
204 ifstream file(GC_DEFAULT_REC_FILE_NAME);
205 if (file.fail()) {
206 cerr << "Error opening data file '" << GC_DEFAULT_REC_FILE_NAME << "'." << endl;
207 exit(1);
210 int n;
211 for (n = GC_SCORE_REC_LENGTH; n--; ) {
212 file.getline(record[n].name, GC_PLAYER_NAME_LENGTH);
213 if (file.fail()) break;
215 if (n != -1)
216 while (n--)
217 strncpy(record[n].name, GC_SCORE_REC_DEFAULT_NAME,
218 GC_PLAYER_NAME_LENGTH);
219 for (n = GC_SCORE_REC_LENGTH; n--; )
220 record[n].score
221 = ((n + 1) * GC_SCORE_DEFAULT_TOP_SCORE) / GC_SCORE_REC_LENGTH;
223 writeScoreRecord();