-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiscores.c
297 lines (239 loc) · 7.17 KB
/
hiscores.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
286
287
288
289
290
291
292
293
294
295
296
297
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (C) 1995 Ronny Wester
Copyright (C) 2003 Jeremy Chin
Copyright (C) 2003-2007 Lucas Martin-King
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------------
hiscores.c - high score functions
Author: $Author: lmartinking $
Rev: $Revision: 250 $
URL: $HeadURL: svn://svn.icculus.org/cdogs-sdl/trunk/src/hiscores.c $
ID: $Id: hiscores.c 250 2007-07-06 16:38:43Z lmartinking $
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "gamedata.h"
#include "hiscores.h"
#include "grafx.h"
#include "blit.h"
#include "actors.h"
#include "pics.h"
#include "text.h"
#include "sounds.h"
#include "input.h"
#include "files.h"
#include "utils.h"
struct Entry {
char name[20];
int head;
int body;
int arms;
int legs;
int skin;
int hair;
int score;
int missions;
int lastMission;
};
#define MAX_ENTRY 20
static struct Entry allTimeHigh[MAX_ENTRY];
static struct Entry todaysHigh[MAX_ENTRY];
static int EnterTable(struct Entry *table, struct PlayerData *data)
{
int i, j;
for (i = 0; i < MAX_ENTRY; i++) {
if (data->totalScore > table[i].score) {
for (j = MAX_ENTRY - 1; j > i; j--)
table[j] = table[j - 1];
strcpy(table[i].name, data->name);
table[i].score = data->totalScore;
table[i].missions = data->missions;
table[i].lastMission = data->lastMission;
table[i].head = data->head;
table[i].arms = data->arms;
table[i].body = data->body;
table[i].legs = data->legs;
table[i].skin = data->skin;
table[i].hair = data->hair;
return i;
}
}
return -1;
}
void EnterHighScore(struct PlayerData *data)
{
data->allTime = EnterTable(allTimeHigh, data);
data->today = EnterTable(todaysHigh, data);
}
/*
static void DisplayCharacterUsed( int x, int y, struct Entry *entry )
{
TOffsetPic body, head;
TranslationTable table;
SetCharacterColors( table, entry->arms, entry->body, entry->legs, entry->skin, entry->hair);
body.dx = cBodyOffset[ BODY_UNARMED][ DIRECTION_DOWN].dx;
body.dy = cBodyOffset[ BODY_UNARMED][ DIRECTION_DOWN].dy;
body.picIndex = cBodyPic[ BODY_UNARMED][ DIRECTION_DOWN][ STATE_IDLE];
head.dx = cNeckOffset[ BODY_UNARMED][ DIRECTION_DOWN].dx + cHeadOffset[ entry->head][ DIRECTION_DOWN].dx;
head.dy = cNeckOffset[ BODY_UNARMED][ DIRECTION_DOWN].dy + cHeadOffset[ entry->head][ DIRECTION_DOWN].dy;
head.picIndex = cHeadPic[ entry->head][ DIRECTION_DOWN][ STATE_IDLE];
DrawTTPic( x + body.dx, y + body.dy, gPics[ body.picIndex], table, gRLEPics[ body.picIndex]);
DrawTTPic( x + head.dx, y + head.dy, gPics[ head.picIndex], table, gRLEPics[ head.picIndex]);
}
*/
static void DisplayAt(int x, int y, const char *s, int hilite)
{
if (hilite)
TextStringWithTableAt(x, y, s, &tableFlamed);
else
TextStringAt(x, y, s);
}
static int DisplayEntry(int x, int y, int index, struct Entry *e,
int hilite)
{
char s[10];
#define INDEX_OFFSET 15
#define SCORE_OFFSET 40
#define MISSIONS_OFFSET 60
#define MISSION_OFFSET 80
#define NAME_OFFSET 85
sprintf(s, "%d.", index + 1);
DisplayAt(x + INDEX_OFFSET - TextWidth(s), y, s, hilite);
sprintf(s, "%d", e->score);
DisplayAt(x + SCORE_OFFSET - TextWidth(s), y, s, hilite);
sprintf(s, "%d", e->missions);
DisplayAt(x + MISSIONS_OFFSET - TextWidth(s), y, s, hilite);
sprintf(s, "(%d)", e->lastMission + 1);
DisplayAt(x + MISSION_OFFSET - TextWidth(s), y, s, hilite);
DisplayAt(x + NAME_OFFSET, y, e->name, hilite);
return 1 + TextHeight();
}
static int DisplayPage(const char *title, int index, struct Entry *e,
int hilite1, int hilite2)
{
int x = 80;
int y = 5;
TextStringAt(5, 5, title);
while (index < MAX_ENTRY && e[index].score > 0 && x < 300) {
y += DisplayEntry(x, y, index, &e[index], index == hilite1
|| index == hilite2);
if (y > 198 - TextHeight()) {
y = 20;
x += 100;
}
index++;
}
CopyToScreen();
return index;
}
void DisplayAllTimeHighScores(void *bkg)
{
int index = 0;
while (index < MAX_ENTRY && allTimeHigh[index].score > 0) {
memcpy(GetDstScreen(), bkg, SCREEN_MEMSIZE);
index = DisplayPage("All time high scores:", index, allTimeHigh,
gPlayer1Data.allTime,
gOptions.twoPlayers ? gPlayer2Data.
allTime : -1);
Wait();
}
}
void DisplayTodaysHighScores(void *bkg)
{
int index = 0;
while (index < MAX_ENTRY && todaysHigh[index].score > 0) {
memcpy(GetDstScreen(), bkg, SCREEN_MEMSIZE);
index = DisplayPage("Today's highest score:", index, todaysHigh,
gPlayer1Data.today,
gOptions.twoPlayers ? gPlayer2Data.
today : -1);
Wait();
}
}
#define MAGIC 4711
#define SCORES_FILE "scores.dat"
void SaveHighScores(void)
{
int magic;
FILE *f;
time_t t;
struct tm *tp;
debug("begin\n");
f = fopen(GetConfigFilePath(SCORES_FILE), "wb");
if (f != NULL) {
magic = MAGIC;
fwrite(&magic, sizeof(magic), 1, f);
fwrite(allTimeHigh, sizeof(allTimeHigh), 1, f);
t = time(NULL);
tp = localtime(&t);
debug("time now, y: %d m: %d d: %d\n", tp->tm_year, tp->tm_mon, tp->tm_mday);
magic = tp->tm_year;
fwrite(&magic, sizeof(magic), 1, f);
magic = tp->tm_mon;
fwrite(&magic, sizeof(magic), 1, f);
magic = tp->tm_mday;
fwrite(&magic, sizeof(magic), 1, f);
debug("writing today's high: %d\n", todaysHigh[0].score);
fwrite(todaysHigh, sizeof(todaysHigh), 1, f);
fclose(f);
debug("saved high scores\n");
} else
printf("Unable to open %s\n", SCORES_FILE);
}
void LoadHighScores(void)
{
int magic;
FILE *f;
int y, m, d;
time_t t;
struct tm *tp;
debug("Reading hi-scores...\n");
memset(allTimeHigh, 0, sizeof(allTimeHigh));
memset(todaysHigh, 0, sizeof(todaysHigh));
f = fopen(GetConfigFilePath(SCORES_FILE), "rb");
if (f != NULL) {
int i;
fread(&magic, sizeof(magic), 1, f);
if (magic != MAGIC) {
debug("Scores file magic doesn't match!\n");
fclose(f);
return;
}
//for (i = 0; i < MAX_ENTRY; i++) {
fread(allTimeHigh, sizeof(allTimeHigh), 1, f);
//}
t = time(NULL);
tp = localtime(&t);
fread(&y, sizeof(y), 1, f);
fread(&m, sizeof(m), 1, f);
fread(&d, sizeof(d), 1, f);
debug("scores time, y: %d m: %d d: %d\n", y, m, d);
if (tp->tm_year == y && tp->tm_mon == m && tp->tm_mday == d) {
fread(todaysHigh, sizeof(todaysHigh), 1, f);
debug("reading today's high: %d\n", todaysHigh[0].score);
}
fclose(f);
} else {
printf("Unable to open %s\n", SCORES_FILE);
}
}