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