- Binary is now relocatable
[crack-attack.git] / src / Score.cxx
blob1e378b85e26c154067b46e117003b3bf01760347
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 short Score::digits[GC_NUMBER_DIGITS];
42 short Score::previous_digits[GC_NUMBER_DIGITS];
43 int Score::fade_timer;
44 GLfloat Score::inverse_timer_start;
45 int Score::n_digits_displayed;
46 int Score::special_block_scores[BF_NUMBER_SPECIAL]
47 = { 30, // black
48 30, // white
49 5, // special purple
50 5, // special blue
51 8, // special green
52 15, // special yellow
53 10, }; // special orange
55 int Score::player_rank;
56 Rank Score::record[GC_SCORE_REC_LENGTH];
58 void Score::initialize ( )
60 if (!(MetaState::mode & CM_SOLO)) return;
62 score = backlog = 0;
63 n_digits_displayed = GC_MIN_NUMBER_DIGITS_DISPLAYED;
64 for (int n = GC_NUMBER_DIGITS; n--; ) {
65 digits[n] = previous_digits[n] = 0;
66 fade_timer = 0;
67 inverse_timer_start = 1.0f;
69 player_rank = -1;
71 if (!readScoreRecord()) setupDefaultScoreRecord();
74 void Score::cleanUp ( )
76 if ((MetaState::mode & CM_SOLO) && player_rank != -1)
77 writeScoreRecord();
80 int Score::gameFinish ( )
82 for (int n = GC_SCORE_REC_LENGTH; n--; )
83 if (score > record[n].score) {
84 player_rank = n;
86 // insert player
87 for (int n = 0; n < player_rank; n++) {
88 strncpy(record[n].name, record[n + 1].name, GC_PLAYER_NAME_LENGTH);
89 record[n].score = record[n + 1].score;
91 strncpy(record[player_rank].name, MetaState::player_name,
92 GC_PLAYER_NAME_LENGTH);
93 record[player_rank].score = score;
95 return GS_WON;
97 return GS_LOST;
100 bool Score::readScoreRecord ( )
102 char file_name[256];
103 TextureLoader::buildLocalDataFileName((MetaState::mode & CM_X)
104 ? GC_X_REC_FILE_NAME : GC_REC_FILE_NAME, file_name);
105 ifstream file(file_name);
106 if (file.fail()) return false;
108 char buffer[256];
109 for (int n = GC_SCORE_REC_LENGTH; n--; ) {
110 file.getline(record[n].name, GC_PLAYER_NAME_LENGTH);
111 file.getline(buffer, 256);
112 record[n].score = atoi(buffer);
113 if (file.fail()) return false;
115 return true;
118 void Score::writeScoreRecord ( )
120 char file_name[256];
121 TextureLoader::buildLocalDataFileName((MetaState::mode & CM_X)
122 ? GC_X_REC_FILE_NAME : GC_REC_FILE_NAME, file_name);
123 ofstream file(file_name);
124 if (file.fail()) {
125 cerr << "Error writing to score record file '" << file_name << "'." << endl;
126 exit(1);
129 for (int n = GC_SCORE_REC_LENGTH; n--; )
130 file << record[n].name << '\n' << record[n].score << '\n';
133 void Score::setupDefaultScoreRecord ( )
135 ifstream file(GC_DEFAULT_REC_FILE_NAME);
136 if (file.fail()) {
137 cerr << "Error opening data file '" << GC_DEFAULT_REC_FILE_NAME << "'." << endl;
138 exit(1);
141 int n;
142 for (n = GC_SCORE_REC_LENGTH; n--; ) {
143 file.getline(record[n].name, GC_PLAYER_NAME_LENGTH);
144 if (file.fail()) break;
146 if (n != -1)
147 while (n--)
148 strncpy(record[n].name, GC_SCORE_REC_DEFAULT_NAME,
149 GC_PLAYER_NAME_LENGTH);
150 for (n = GC_SCORE_REC_LENGTH; n--; )
151 record[n].score
152 = ((n + 1) * GC_SCORE_DEFAULT_TOP_SCORE) / GC_SCORE_REC_LENGTH;
154 writeScoreRecord();