1 |
/* |
2 |
CrossFire, A Multiplayer game for X-windows |
3 |
|
4 |
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 |
Copyright (C) 1992 Frank Tore Johansen |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
|
21 |
The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 |
*/ |
23 |
|
24 |
#include <global.h> |
25 |
#ifndef __CEXTRACT__ |
26 |
# include <sproto.h> |
27 |
#endif |
28 |
|
29 |
/* |
30 |
* The score structure is used when treating new high-scores |
31 |
*/ |
32 |
|
33 |
typedef struct scr |
34 |
{ |
35 |
char name[64]; // name */ |
36 |
char title[64]; // Title */ |
37 |
char killer[64]; // name (+ title) or "quit" */ |
38 |
sint64 exp; // Experience */ |
39 |
char maplevel[128]; // Killed on what level */ |
40 |
int maxhp, maxsp, maxgrace; // Max hp, sp, grace when killed */ |
41 |
int position; // Position in the highscore list */ |
42 |
} score; |
43 |
|
44 |
/* |
45 |
* spool works mostly like strtok(char *, ":"), but it can also |
46 |
* log a specified error message if something goes wrong. |
47 |
*/ |
48 |
|
49 |
char * |
50 |
spool (char *bp, char *error) |
51 |
{ |
52 |
static char *prev_pos = NULL; |
53 |
char *next_pos; |
54 |
|
55 |
if (bp == NULL) |
56 |
{ |
57 |
if (prev_pos == NULL) |
58 |
{ |
59 |
LOG (llevError, "Called spool (%s) with NULL without previous call.\n", error); |
60 |
return NULL; |
61 |
} |
62 |
bp = prev_pos; |
63 |
} |
64 |
if (*bp == '\0') |
65 |
{ |
66 |
LOG (llevError, "spool: End of line at %s\n", error); |
67 |
return NULL; |
68 |
} |
69 |
if ((next_pos = strchr (bp, ':')) != NULL) |
70 |
{ |
71 |
*next_pos = '\0'; |
72 |
prev_pos = next_pos + 1; |
73 |
} |
74 |
else |
75 |
prev_pos = NULL; |
76 |
return bp; |
77 |
} |
78 |
|
79 |
/* |
80 |
* Does what it says, copies the contents of the first score structure |
81 |
* to the second one. |
82 |
*/ |
83 |
|
84 |
static void |
85 |
copy_score (score *sc1, score *sc2) |
86 |
{ |
87 |
assign (sc2->name , sc1->name); |
88 |
assign (sc2->title , sc1->title); |
89 |
assign (sc2->killer , sc1->killer); |
90 |
assign (sc2->maplevel, sc1->maplevel); |
91 |
sc2->exp = sc1->exp; |
92 |
sc2->maxhp = sc1->maxhp; |
93 |
sc2->maxsp = sc1->maxsp; |
94 |
sc2->maxgrace = sc1->maxgrace; |
95 |
} |
96 |
|
97 |
/* |
98 |
* Writes the given score structure to a static buffer, and returns |
99 |
* a pointer to it. |
100 |
*/ |
101 |
|
102 |
static char * |
103 |
put_score (score *sc) |
104 |
{ |
105 |
static char buf[MAX_BUF]; |
106 |
|
107 |
sprintf (buf, "%s:%s:%" I64_PFd ":%s:%s:%d:%d:%d", sc->name, sc->title, (long long) sc->exp, sc->killer, sc->maplevel, |
108 |
sc->maxhp, sc->maxsp, sc->maxgrace); |
109 |
return buf; |
110 |
} |
111 |
|
112 |
/* |
113 |
* The oposite of put_score, get_score reads from the given buffer into |
114 |
* a static score structure, and returns a pointer to it. |
115 |
*/ |
116 |
|
117 |
static score * |
118 |
get_score (char *bp) |
119 |
{ |
120 |
static score sc; |
121 |
char *cp; |
122 |
|
123 |
if ((cp = strchr (bp, '\n')) != NULL) |
124 |
*cp = '\0'; |
125 |
|
126 |
if ((cp = spool (bp, "name")) == NULL) |
127 |
return 0; |
128 |
|
129 |
assign (sc.name, cp); |
130 |
|
131 |
if ((cp = spool (0, "title")) == NULL) |
132 |
return 0; |
133 |
|
134 |
assign (sc.title, cp); |
135 |
|
136 |
if ((cp = spool (0, "score")) == NULL) |
137 |
return 0; |
138 |
|
139 |
long long exp; |
140 |
sscanf (cp, "%" I64_PFd, &exp); |
141 |
sc.exp = exp; |
142 |
|
143 |
if ((cp = spool (0, "killer")) == NULL) |
144 |
return 0; |
145 |
|
146 |
assign (sc.killer, cp); |
147 |
|
148 |
if ((cp = spool (0, "map")) == NULL) |
149 |
return 0; |
150 |
|
151 |
assign (sc.maplevel, cp); |
152 |
|
153 |
if ((cp = spool (0, "maxhp")) == NULL) |
154 |
return 0; |
155 |
|
156 |
sscanf (cp, "%d", &sc.maxhp); |
157 |
|
158 |
if ((cp = spool (0, "maxsp")) == NULL) |
159 |
return 0; |
160 |
|
161 |
sscanf (cp, "%d", &sc.maxsp); |
162 |
|
163 |
if ((cp = spool (0, "maxgrace")) == NULL) |
164 |
return 0; |
165 |
|
166 |
sscanf (cp, "%d", &sc.maxgrace); |
167 |
return ≻ |
168 |
} |
169 |
|
170 |
static char * |
171 |
draw_one_high_score (score *sc) |
172 |
{ |
173 |
static char retbuf[MAX_BUF]; |
174 |
|
175 |
if (!strncmp (sc->killer, "quit", MAX_NAME)) |
176 |
sprintf (retbuf, "%3d %10lld %s the %s quit the game on map %s [%d][%d][%d].", |
177 |
sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace); |
178 |
else if (!strncmp (sc->killer, "left", MAX_NAME)) |
179 |
sprintf (retbuf, "%3d %10lld %s the %s left the game on map %s [%d][%d][%d].", |
180 |
sc->position, (long long) sc->exp, sc->name, sc->title, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace); |
181 |
else |
182 |
sprintf (retbuf, "%3d %10lld %s the %s was killed by %s on map %s [%d][%d][%d].", |
183 |
sc->position, (long long) sc->exp, sc->name, sc->title, sc->killer, sc->maplevel, sc->maxhp, sc->maxsp, sc->maxgrace); |
184 |
return retbuf; |
185 |
} |
186 |
|
187 |
/* |
188 |
* add_score() adds the given score-structure to the high-score list, but |
189 |
* only if it was good enough to deserve a place. |
190 |
*/ |
191 |
|
192 |
static score * |
193 |
add_score (score *new_score) |
194 |
{ |
195 |
FILE *fp; |
196 |
static score old_score; |
197 |
score *tmp_score, pscore[HIGHSCORE_LENGTH]; |
198 |
char buf[MAX_BUF], filename[MAX_BUF], *bp; |
199 |
int nrofscores = 0, flag = 0, i, comp; |
200 |
|
201 |
new_score->position = HIGHSCORE_LENGTH + 1; |
202 |
old_score.position = -1; |
203 |
sprintf (filename, "%s/%s", settings.localdir, HIGHSCORE); |
204 |
if ((fp = open_and_uncompress (filename, 1, &comp)) != NULL) |
205 |
{ |
206 |
while (fgets (buf, MAX_BUF, fp) != NULL && nrofscores < HIGHSCORE_LENGTH) |
207 |
{ |
208 |
if ((tmp_score = get_score (buf)) == NULL) |
209 |
break; |
210 |
if (!flag && new_score->exp >= tmp_score->exp) |
211 |
{ |
212 |
copy_score (new_score, &pscore[nrofscores]); |
213 |
new_score->position = nrofscores; |
214 |
flag = 1; |
215 |
if (++nrofscores >= HIGHSCORE_LENGTH) |
216 |
break; |
217 |
} |
218 |
if (!strcmp (new_score->name, tmp_score->name)) |
219 |
{ /* Another entry */ |
220 |
copy_score (tmp_score, &old_score); |
221 |
old_score.position = nrofscores; |
222 |
if (flag) |
223 |
continue; |
224 |
} |
225 |
copy_score (tmp_score, &pscore[nrofscores++]); |
226 |
} |
227 |
close_and_delete (fp, comp); |
228 |
} |
229 |
if (old_score.position != -1 && old_score.exp >= new_score->exp) |
230 |
return &old_score; /* Did not beat old score */ |
231 |
if (!flag && nrofscores < HIGHSCORE_LENGTH) |
232 |
copy_score (new_score, &pscore[nrofscores++]); |
233 |
if ((fp = fopen (filename, "w")) == NULL) |
234 |
{ |
235 |
LOG (llevError, "Cannot write to highscore file %s: %s\n", filename, strerror (errno)); |
236 |
return NULL; |
237 |
} |
238 |
for (i = 0; i < nrofscores; i++) |
239 |
{ |
240 |
bp = put_score (&pscore[i]); |
241 |
fprintf (fp, "%s\n", bp); |
242 |
} |
243 |
fclose (fp); |
244 |
if (flag) |
245 |
{ |
246 |
|
247 |
/* Eneq(@csd.uu.se): Patch to fix error in adding a new score to the |
248 |
hiscore-list */ |
249 |
if (old_score.position == -1) |
250 |
return new_score; |
251 |
return &old_score; |
252 |
} |
253 |
new_score->position = -1; |
254 |
if (old_score.position != -1) |
255 |
return &old_score; |
256 |
if (nrofscores) |
257 |
{ |
258 |
copy_score (&pscore[nrofscores - 1], &old_score); |
259 |
return &old_score; |
260 |
} |
261 |
LOG (llevError, "Highscore error.\n"); |
262 |
return NULL; |
263 |
} |
264 |
|
265 |
void |
266 |
check_score (object *op) |
267 |
{ |
268 |
score new_score; |
269 |
score *old_score; |
270 |
|
271 |
if (op->stats.exp == 0) |
272 |
return; |
273 |
|
274 |
if (!op->contr->name_changed) |
275 |
{ |
276 |
if (op->stats.exp > 0) |
277 |
{ |
278 |
new_draw_info (NDI_UNIQUE, 0, op, "As you haven't changed your name, you won't"); |
279 |
new_draw_info (NDI_UNIQUE, 0, op, "get into the high-score list."); |
280 |
} |
281 |
return; |
282 |
} |
283 |
|
284 |
if (op->contr->explore) |
285 |
{ |
286 |
new_draw_info (NDI_UNIQUE, 0, op, "Since you were in explore mode,"); |
287 |
new_draw_info (NDI_UNIQUE, 0, op, "you can't enter the high-score list."); |
288 |
return; |
289 |
} |
290 |
|
291 |
assign (new_score.name, op->name); |
292 |
assign (new_score.title, op->title.length () ? &op->title : op->contr->title); |
293 |
assign (new_score.killer, op->contr->killer[0] ? op->contr->killer : "a dungeon collapse"); |
294 |
assign (new_score.maplevel, op->map ? op->map->name ? op->map->name : op->map->path : ""); |
295 |
|
296 |
new_score.exp = op->stats.exp; |
297 |
new_score.maxhp = op->stats.maxhp; |
298 |
new_score.maxsp = op->stats.maxsp; |
299 |
new_score.maxgrace = op->stats.maxgrace; |
300 |
|
301 |
if ((old_score = add_score (&new_score)) == NULL) |
302 |
{ |
303 |
new_draw_info (NDI_UNIQUE, 0, op, "Error in the highscore list."); |
304 |
return; |
305 |
} |
306 |
|
307 |
if (new_score.position == -1) |
308 |
{ |
309 |
new_score.position = HIGHSCORE_LENGTH + 1; /* Not strictly correct... */ |
310 |
if (!strcmp (old_score->name, new_score.name)) |
311 |
new_draw_info (NDI_UNIQUE, 0, op, "You didn't beat your last highscore:"); |
312 |
else |
313 |
new_draw_info (NDI_UNIQUE, 0, op, "You didn't enter the highscore list:"); |
314 |
new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (old_score)); |
315 |
new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (&new_score)); |
316 |
return; |
317 |
} |
318 |
|
319 |
if (old_score->exp >= new_score.exp) |
320 |
new_draw_info (NDI_UNIQUE, 0, op, "You didn't beat your last score:"); |
321 |
else |
322 |
new_draw_info (NDI_UNIQUE, 0, op, "You beat your last score:"); |
323 |
|
324 |
new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (old_score)); |
325 |
new_draw_info (NDI_UNIQUE, 0, op, draw_one_high_score (&new_score)); |
326 |
} |
327 |
|
328 |
/* displays the high score file. object is the calling object |
329 |
* (null if being called via command line.) max is the maximum |
330 |
* number of scores to display. match, if set, is the name or class |
331 |
* to match to. |
332 |
*/ |
333 |
|
334 |
void |
335 |
display_high_score (object *op, int max, const char *match) |
336 |
{ |
337 |
const size_t maxchar = 80; |
338 |
FILE *fp; |
339 |
char buf[MAX_BUF], *scorebuf, *bp, *cp; |
340 |
int i = 0, j = 0, comp; |
341 |
score *sc; |
342 |
|
343 |
sprintf (buf, "%s/%s", settings.localdir, HIGHSCORE); |
344 |
if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL) |
345 |
{ |
346 |
LOG (llevError, "Cannot open highscore file %s: %s\n", buf, strerror (errno)); |
347 |
if (op != NULL) |
348 |
new_draw_info (NDI_UNIQUE, 0, op, "There is no highscore file."); |
349 |
return; |
350 |
} |
351 |
if (op != NULL) |
352 |
clear_win_info (op); |
353 |
new_draw_info (NDI_UNIQUE, 0, op, "Nr Score Who [max hp][max sp][max grace]"); |
354 |
|
355 |
while (fgets (buf, MAX_BUF, fp) != NULL) |
356 |
{ |
357 |
if (j >= HIGHSCORE_LENGTH || i >= (max - 1)) |
358 |
break; |
359 |
if ((sc = get_score (buf)) == NULL) |
360 |
break; |
361 |
sc->position = ++j; |
362 |
if (match == NULL) |
363 |
{ |
364 |
scorebuf = draw_one_high_score (sc); |
365 |
i++; |
366 |
} |
367 |
else |
368 |
{ |
369 |
if (!strcasecmp (sc->name, match) || !strcasecmp (sc->title, match)) |
370 |
{ |
371 |
scorebuf = draw_one_high_score (sc); |
372 |
i++; |
373 |
} |
374 |
else |
375 |
continue; |
376 |
} |
377 |
/* Replaced what seemed to an overly complicated word wrap method |
378 |
* still word wraps, but assumes at most 2 lines of data. |
379 |
* mw - 2-12-97 |
380 |
*/ |
381 |
assign (buf, scorebuf); |
382 |
|
383 |
cp = buf; |
384 |
while (strlen (cp) > maxchar) |
385 |
{ |
386 |
bp = cp + maxchar - 1; |
387 |
while (*bp != ' ' && bp > cp) |
388 |
bp--; |
389 |
*bp = '\0'; |
390 |
|
391 |
if (op == NULL) |
392 |
LOG (llevDebug, "%s\n", cp); |
393 |
else |
394 |
new_draw_info (NDI_UNIQUE, 0, op, cp); |
395 |
|
396 |
sprintf (buf, " %s", bp + 1); |
397 |
cp = buf; |
398 |
i++; |
399 |
} |
400 |
|
401 |
if (op == NULL) |
402 |
LOG (llevDebug, "%s\n", buf); |
403 |
else |
404 |
new_draw_info (NDI_UNIQUE, 0, op, buf); |
405 |
} |
406 |
|
407 |
close_and_delete (fp, comp); |
408 |
} |