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