-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores.c
285 lines (208 loc) · 5.27 KB
/
scores.c
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
#include "scores.h"
#include "options.h"
#include "player.h"
#include "fletcher.h"
#include "replay.h"
#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { SCORES_FILE_LINE_WIDTH = 48 };
static const char* SCORES_ID = "XorCurses__scores";
ctr_t* scores = 0;
char** map_names = 0;
static void (*score_update_callback)(lvl_t level, ctr_t moves) = 0;
int create_scores()
{
int i;
if (scores)
destroy_scores();
scores = malloc(sizeof(*scores) * (MAX_LEVEL + 1));
map_names = calloc(MAX_LEVEL + 1, sizeof(*map_names));
if (!scores || !map_names)
return 0;
for (i = 1; i <= MAX_LEVEL; ++i)
{
char* fn = options_map_filename(i);
if (!fn)
{
err_msg("failed to obtain map filename %d\n", i);
return 0;
}
map_names[i] = xor_map_read_name(fn, &scores[i]);
if (!map_names[i])
{
err_msg("failed to read map name %d\n", i);
free(fn);
return 0;
}
free(fn);
}
return 1;
}
void destroy_scores()
{
int i;
if (!scores)
return;
free(scores);
for (i = 1; i <= MAX_LEVEL; ++i)
if (map_names[i])
free(map_names[i]);
free(map_names);
}
static int load_scores_df(struct df* df)
{
int i;
for (i = 1; i <= MAX_LEVEL; ++i)
{
uint8_t level;
uint16_t best_moves;
char* map_name;
if (!df_read_hex_byte(df, &level))
{
debug("failed to read level number %d from scores file\n", i);
return 0;
}
if (!df_read_hex_word(df, &best_moves))
{
debug("failed to read best moves %d from scores file\n", i);
return 0;
}
if (!(map_name = df_read_string(df, MAPNAME_MAXCHARS)))
{
debug("failed to read map name %d from scores file\n", i);
return 0;
}
if (strcmp(map_name, map_names[i]) != 0)
{
debug("failed to match map name %d from scores file\n"
"with currently loaded maps\n", i);
return 0;
}
free(map_name);
scores[i] = best_moves;
}
return 1;
}
int load_scores()
{
if (!scores)
return 0;
if (!options->user_dir)
return 0;
debug("loading scores file\n");
char *fname = options_file_path(".xorcurses", options->user_dir);
if (!fname)
return 0;
FILE *fp = fopen(fname, "r");
free(fname);
if (!fp)
{
save_scores();
return 0;
}
struct df* df = df_open(fp, DF_READ, SCORES_ID,
SCORES_FILE_LINE_WIDTH);
if (!df)
{
ctr_t tmp[MAX_LEVEL + 1];
int i;
debug("failed to load scores file\n");
debug("attempting import of old scores dump file\n");
fseek(fp, 0L, SEEK_SET);
fread(tmp, sizeof(ctr_t), MAX_LEVEL + 1, fp);
/* the old dump file based scores file didn't save the score
for map 15.
*/
for (i = 1; i < MAX_LEVEL; ++i)
{
if (tmp[i] <= 0 || tmp[i] > 2000)
{
debug("invalid score for map %d: %d \n", i, tmp[i]);
i = 0;
break;
}
}
for (--i; i > 0; --i)
scores[i] = tmp[i];
fclose(fp);
save_scores();
return 0;
}
load_scores_df(df);
df_close(df);
fclose(fp);
return 1;
}
void set_score(lvl_t level, ctr_t moves)
{
if (!scores)
return;
if (level < MIN_LEVEL || level > MAX_LEVEL)
return;
if (moves > MAX_MOVES)
return;
if (!replay.hasexit)
return;
if (scores[level] > moves)
{
scores[level] = moves;
save_scores();
if (score_update_callback)
(score_update_callback)(level, moves);
}
}
int save_scores()
{
if (!scores)
return 0;
if (!options->user_dir)
return 0;
debug("saving scores file\n");
char *fname = options_file_path(".xorcurses", options->user_dir);
if (!fname)
return 0;
FILE *fp = fopen(fname, "w");
free(fname);
if (!fp)
return 0;
struct df* df = df_open(fp, DF_WRITE, SCORES_ID,
SCORES_FILE_LINE_WIDTH);
if (!df)
{
debug("failed to write scores file\n");
fclose(fp);
return 0;
}
int i;
int ret = 0;
for (i = 1; i <= MAX_LEVEL; ++i)
{
uint8_t level = i & 0x0f;
if (!df_write_hex_byte(df, level))
{
debug("failed to write level number %d to scores file\n", i);
goto fail;
}
if (!df_write_hex_word(df, scores[i]))
{
debug("failed to write best moves %d to scores file\n", i);
goto fail;
}
if (!df_write_string(df, map_names[i], MAPNAME_MAXCHARS))
{
debug("failed to write map name %d to scores file\n", i);
goto fail;
}
}
ret = 1;
fail:
df_close(df);
fclose(fp);
return ret;
}
void set_score_update_cb(void (*score_update_cb)(lvl_t level, ctr_t moves))
{
score_update_callback = score_update_cb;
}