-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeeds.cpp
309 lines (239 loc) · 8.73 KB
/
Deeds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright 2011 Matt DeVore
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/** \file Deeds.cpp
* Module that maintains a database of the player's accomplishments,
* including which levels have been beaten at what difficulties,
* and what the best time and score are for the beaten levels.
*/
#include "StdAfx.h"
#include "Fixed.h"
#include "Deeds.h"
using namespace std;
const int MAX_STRINGLEN = 100;
const short WORST_TIME = 60*99+59;
const short WORST_SCORE = -1;
const char *CFG_FILE = "Config11.dat";
const char *DIFFNAME_DANGNABIT = "Dangnabit (TRY ME FIRST)";
const char *DIFFNAME_MYDEARCHILD = "My Dear Child (HARD)";
const char *DIFFNAME_DANGERDANGER = "Danger! Danger! (VERY HARD)";
static int level_availability[NUM_LEVELS];
/** the best times.
* <ul>
* <li>first half contains best time in seconds</li>
* <li>second half contains the score the player
* ended up getting in that time.</li>
* </ul>
*/
static pair<short, short> best_times[NUM_LEVELS*NUM_DIFFICULTYLEVELS];
/** the best scores.
* <ul>
* <li>first half contains the best score</li>
* <li>second half contains the time it took
* that player to get that score in seconds</li>
* </ul>
*/
static pair<short, short> best_scores[NUM_LEVELS*NUM_DIFFICULTYLEVELS];
static int video_mode;
static int sync_rate;
static int GetDeedId(int level, int difficulty) throw() {
assert(level >= 0 && level < NUM_LEVELS);
assert(difficulty >= 0 && difficulty < NUM_DIFFICULTYLEVELS);
return (level * NUM_DIFFICULTYLEVELS) + difficulty;
}
static int Read(HANDLE file, int *total_read, int *total_size, bool *success,
int max) {
unsigned char result;
(*total_size)++;
if (!*success) {
result = 0;
} else {
DWORD read;
*success = ReadFile(file, (void *)&result, 1, &read, 0);
*total_read += read;
if ((int)result > max) {
*success = false;
}
}
return int(result);
}
static void Write(HANDLE file, int *total_written, int *total_size,
bool *success, int value) {
unsigned char write = (unsigned char)value;
(*total_size)++;
if (*success) {
DWORD written;
*success = WriteFile(file, (void *)&write, 1, &written, 0);
*total_written += written;
}
}
static bool Initialize(HANDLE file) throw() {
bool success = true;
int total_read = 0;
int total_size = 0;
video_mode = Read(
file, &total_read, &total_size, &success, NUM_VIDEOMODES-1);
sync_rate = Read(file, &total_read, &total_size, &success, MAX_SYNCRATE);
// read our accomplishments from the file
// first read level availability
for(int i = 0; i < NUM_LEVELS; i++) {
level_availability[i] = Read(file, &total_read, &total_size, &success,
LEVELAVAIL_DANGERDANGER);
}
// now read our best times and best scores, in that order
for(int i = 0; i < NUM_LEVELS * NUM_DIFFICULTYLEVELS; i++) {
DWORD read;
success = success && ReadFile(file, (void *)(best_times+i),
sizeof(pair<short, short>), &read, 0);
total_read += read;
success = success && ReadFile(file, (void *)(best_scores+i),
sizeof(pair<short, short>), &read, 0);
total_read += read;
total_size += sizeof(pair<short, short>) * 2;
}
return total_read == total_size && success;
}
bool DeeInitialize() throw() {
HANDLE file = CreateFile(CFG_FILE,
GENERIC_READ, 0, 0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if (INVALID_HANDLE_VALUE != file) {
bool success = Initialize(file);
CloseHandle(file);
if (success) {
return false;
}
}
video_mode = DEFAULT_VIDEOMODE;
sync_rate = DEFAULT_SYNCRATE;
// setup the default best score and times
for(int i = 0; i < NUM_LEVELS; i++) {
level_availability[i] = LEVELAVAIL_NONE;
for(int j = 0; j < NUM_DIFFICULTYLEVELS; j++) {
int magic_index = i * NUM_DIFFICULTYLEVELS + j;
best_scores[magic_index].first = WORST_SCORE;
best_scores[magic_index].second = WORST_TIME;
best_times[magic_index].first = WORST_TIME;
best_times[magic_index].second = WORST_SCORE;
}
}
level_availability[0] = LEVELAVAIL_DANGERDANGER;
return true;
}
void DeeRelease() throw(DeedsWriteException) {
int total_written = 0;
int total_size = 0;
bool success = true;
HANDLE file = CreateFile(CFG_FILE, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
Write(file, &total_written, &total_size, &success, video_mode);
Write(file, &total_written, &total_size, &success, sync_rate);
// first write the level availabilities
for(int i = 0; i < NUM_LEVELS; i++) {
Write(file, &total_written, &total_size, &success, level_availability[i]);
}
// now write best times and scores
for(int i = 0; i < NUM_LEVELS * NUM_DIFFICULTYLEVELS; i++) {
DWORD written;
success = success && WriteFile(file, (void *)(best_times+i),
sizeof(pair<short,short>), &written, 0);
total_written += written;
success = success && WriteFile(file, (void *)(best_scores+i),
sizeof(pair<short,short>), &written, 0);
total_written += written;
total_size += sizeof(pair<short, short>) * 2;
}
CloseHandle(file);
if (total_size != total_written || !success) {
throw DeedsWriteException();
}
}
int DeeLevelAvailability(unsigned int level) throw() {
// make sure the level parameter is within range
assert(level < NUM_LEVELS);
return level_availability[level];
}
void DeeLevelComplete(int level, int difficulty,
FIXEDNUM since_start,
int score, int next_level) throw() {
// catch the timer before it has too much time to tick
// and it registers an unfair amount of time
short time = (short)FixedCnvFrom<long>(since_start);
int deed_id = GetDeedId(level, difficulty);
if(time > WORST_TIME) {
time = WORST_TIME;
}
// see if this guy has the same time as before,
// but got a higher score
if(best_times[deed_id].first == time) {
if(best_times[deed_id].second < score) {
// the score was beaten
best_times[deed_id].second = (short)score;
}
} else if(best_times[deed_id].first > time) {
// the time was beaten, and so both the time
// and score are overwritten
best_times[deed_id].first = time;
best_times[deed_id].second = (short)score;
}
// see if this guy has the same score as before,
// but got a better time
if(best_scores[deed_id].first == score) {
if(best_scores[deed_id].second > time) {
// the time was beaten
best_scores[deed_id].second = time;
}
} else if(best_scores[deed_id].first < score) {
// the score was beaten, and so both the time
// and score are overwritten
best_scores[deed_id].first = (short)score;
best_scores[deed_id].second = time;
}
// see if we should update the level availability array
if(next_level < NUM_LEVELS
&& level_availability[next_level] < difficulty + 1) {
level_availability[next_level] = difficulty+1;
}
// write the accomplishment to the configuration file
DeeRelease();
}
bool DeeHasBeatenLevel(int level, int difficulty) throw() {
int deed_id = GetDeedId(level, difficulty);
return WORST_TIME != best_times[deed_id].first
&& WORST_SCORE != best_scores[deed_id].first;
}
pair<FIXEDNUM, int> DeeGetBestTimeAndScore(int level,
int difficulty) throw() {
assert(DeeHasBeatenLevel(level, difficulty));
int deed_id = GetDeedId(level, difficulty);
return pair<FIXEDNUM, int>(best_times[deed_id].first,
best_scores[deed_id].first);
}
int DeeGetScoreAtBestTime(int level, int difficulty) throw() {
assert(DeeHasBeatenLevel(level, difficulty));
return best_times[GetDeedId(level, difficulty)].second;
}
FIXEDNUM DeeGetTimeAtBestScore(int level, int difficulty) throw() {
assert(DeeHasBeatenLevel(level, difficulty));
return best_scores[GetDeedId(level, difficulty)].second;
}
int DeeVideoMode() {return video_mode;}
void DeeSetVideoMode(int vm) {
assert(vm >= 0 && vm < NUM_VIDEOMODES);
video_mode = vm;
}
int DeeSyncRate() {return sync_rate;}
void DeeSetSyncRate(int sr) {
assert(sr >= MIN_SYNCRATE && sr <= MAX_SYNCRATE);
sync_rate = sr;
}