2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
26 /*---------------------------------------------------------------------------*/
28 static int count; /* number of sets */
30 static struct set set_v[MAXSET]; /* array of sets */
32 static struct set *current_set; /* currently selected set */
34 static struct level level_v[MAXLVL]; /* levels of the current set */
36 /*---------------------------------------------------------------------------*/
38 static void put_score(FILE *fp, const struct score *s)
41 for (j = 0; j < NSCORE; j++)
42 fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
45 static void set_store_hs(void)
46 /* Store the score of the set */
48 const struct set *s = current_set;
51 const struct level *l;
53 if ((fout = fopen(config_user(s->user_scores), "w")))
56 for (i = s->count - 1 ; i >=0 ; i--)
59 if (!level_v[i].is_locked)
62 fprintf(fout, "%d\n", code);
64 put_score(fout, &s->time_score);
65 put_score(fout, &s->coin_score);
67 for (i = 0 ; i < s->count ; i++)
70 put_score(fout, &l->time_score);
71 put_score(fout, &l->goal_score);
72 put_score(fout, &l->coin_score);
79 static int get_score(FILE *fp, struct score *s)
83 for (j = 0; j < NSCORE && res; j++)
85 res = (fscanf(fp, "%d %d %s\n",
86 &s->timer[j], &s->coins[j], s->player[j])) == 3;
91 static void set_load_hs(void)
92 /* Get the score of the set */
94 struct set *s = current_set;
99 const char *fn = config_user(s->user_scores);
101 if ((fin = fopen(fn, "r")))
104 res = (fscanf(fin, "%d\n", &code) == 1);
105 for (i = 0 ; i < s->count ; i++)
108 level_v[i].is_locked = 0;
111 res = res && (code != 0) &&
112 get_score(fin, &s->time_score) &&
113 get_score(fin, &s->coin_score);
115 for (i = 0; i <= s->count && res; i++)
118 res = get_score(fin, &l->time_score) &&
119 get_score(fin, &l->goal_score) &&
120 get_score(fin, &l->coin_score);
126 if (!res && errno != ENOENT)
128 fprintf(stderr, _("Error while loading user high-score file '%s': "), fn);
132 fprintf(stderr, _("Incorrect format\n"));
136 static char* chomp(char *str)
137 /* Remove trailing \n if any */
139 char *p = str + strlen(str) - 1;
140 if (p >= str && *p == '\n') *p = 0;
144 static int set_load(struct set *s, const char *filename)
151 /* Open the datafile */
153 fin = fopen(filename, "r");
156 fprintf(stderr, _("Cannot load the set file '%s':"), filename);
161 /* Raz the set structure */
163 memset(s, 0, sizeof(struct set));
165 /* Set some sane values in case the scores hs is missing. */
167 score_init_hs(&s->time_score, 359999, 0);
168 score_init_hs(&s->coin_score, 359999, 0);
170 /* Load set metadata */
172 strcpy(s->file, filename);
173 if ((res = fgets(buf, MAXSTR, fin) != NULL))
174 strcpy(s->name, chomp(buf));
175 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
176 strcpy(s->desc, chomp(buf));
177 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
178 strcpy(s->setname, chomp(buf));
179 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
180 strcpy(s->shot, chomp(buf));
181 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
182 sscanf(buf, "%d %d %d %d %d %d",
183 &s->time_score.timer[0],
184 &s->time_score.timer[1],
185 &s->time_score.timer[2],
186 &s->coin_score.coins[0],
187 &s->coin_score.coins[1],
188 &s->coin_score.coins[2]);
189 strcpy(s->user_scores, "neverballhs-");
190 strcat(s->user_scores, s->setname);
192 /* Count levels levels. */
196 while (s->count < MAXLVL && fgets(buf, MAXSTR, fin))
199 /* Close the file, since it's no more needed */
203 /* Load the level limit (stored in the user highscore file) */
207 if ((fin = fopen(config_user(s->user_scores), "r")))
209 fscanf(fin, "%d\n", &s->limit);
210 if (s->limit > s->count)
218 /*---------------------------------------------------------------------------*/
224 char filename[MAXSTR];
231 if ((fin = fopen(config_data(SET_FILE), "r")))
234 while (count < MAXSET && res)
236 set = &(set_v[count]);
238 /* clean the set data */
240 res = (fgets(filename, MAXSTR, fin) != NULL);
245 res = set_load(set, config_data(filename));
258 /*---------------------------------------------------------------------------*/
260 int set_exists(int i)
262 return (0 <= i && i < count);
265 const struct set *get_set(int i)
267 return set_exists(i) ? &set_v[i] : NULL;
270 /*---------------------------------------------------------------------------*/
272 int set_completed(const struct set *s)
273 /* Are all levels (even extra bonus) completed? */
275 return 0; /*s->limit >= s->count;*/
278 int set_level_exists(const struct set *s, int i)
279 /* Is the level i of the set exists */
281 return (i >= 0) && (i < s->count);
284 /*---------------------------------------------------------------------------*/
286 static void set_load_levels(void)
287 /* Load more the levels of the current set */
297 fin = fopen(current_set->file, "r");
301 /* Skip the five first lines */
303 fgets(buf, MAXSTR, fin);
304 for(i=0; i<current_set->count && res; i++)
307 res = (fgets(buf, MAXSTR, fin) != NULL) &&
308 (sscanf(buf, "%s", name) == 1);
311 level_load(config_data(name), l);
313 /* Initialize set related info */
314 l->set = current_set;
317 sprintf(l->numbername, "B%d", bnb++);
319 sprintf(l->numbername, "%02d", nb++);
320 l->is_locked = 1; /*i > current_set->limit;*/
323 assert(i == current_set->count);
328 assert(set_exists(i));
329 current_set = &set_v[i];
334 const struct set *curr_set(void)
339 const struct level *get_level(int i)
341 return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
344 /*---------------------------------------------------------------------------*/
346 static int score_time_comp(const struct score *S, int i, int j)
348 if (S->timer[i] < S->timer[j])
351 if (S->timer[i] == S->timer[j] &&
352 S->coins[i] > S->coins[j])
358 static int score_coin_comp(const struct score *S, int i, int j)
360 if (S->coins[i] > S->coins[j])
363 if (S->coins[i] == S->coins[j] &&
364 S->timer[i] < S->timer[j])
370 static void score_swap(struct score *S, int i, int j)
375 strncpy(player, S->player[i], MAXNAM);
376 strncpy(S->player[i], S->player[j], MAXNAM);
377 strncpy(S->player[j], player, MAXNAM);
380 S->timer[i] = S->timer[j];
384 S->coins[i] = S->coins[j];
388 static int score_time_insert(struct score *s, const char* player, int timer, int coins)
392 strncpy(s->player[3], player, MAXNAM);
396 for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
397 score_swap(s, i + 1, i);
401 static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
405 strncpy(s->player[3], player, MAXNAM);
409 for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
410 score_swap(s, i + 1, i);
414 static int level_score_update(struct level_game *lg, const char *player)
415 /* Update the level score rank according to coins and timer */
417 int timer = lg->timer;
418 int coins = lg->coins;
419 struct level * l = &level_v[lg->level->number];
421 lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
423 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
424 lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
428 lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
430 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
433 static int set_score_update(struct level_game *lg, const char *player)
434 /* Update the set score rank according to score and times */
436 int timer = lg->times;
437 int coins = lg->score;
438 struct set * s = current_set;
440 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
441 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
442 return (lg->score_rank < 3 || lg->times_rank < 3);
446 void score_change_name(struct level_game *lg, const char *player)
447 /* Update the player name for set and level high-score */
449 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
450 struct set * s = current_set;
451 struct level *l = &level_v[lg->level->number];
452 UPDATE(lg->time_rank, l->time_score);
453 UPDATE(lg->goal_rank, l->goal_score);
454 UPDATE(lg->coin_rank, l->coin_score);
455 UPDATE(lg->score_rank, s->coin_score);
456 UPDATE(lg->times_rank, s->time_score);
460 void set_finish_level(struct level_game *lg, const char *player)
461 /* Inform the set that a level is finished.
462 * Update next_level and score rank fields */
464 struct set *s = current_set;
465 int level = lg->level->number;
469 dirty = level_score_update(lg, player);
470 dirty = set_score_update(lg, player) || dirty;
472 /* compute the next level */
475 /* if no set, return */
476 lg->next_level = NULL;
480 level++; /* level is the next level */
482 /* if the next level is not oppened */
483 if (level_v[level].is_locked)
484 if ((lg->mode == MODE_CHALLENGE) ||
485 (lg->mode == MODE_NORMAL && (level < 20 || level > 20)))
487 level_v[level].is_locked = 0;
492 /* got the next level */
493 if (lg->mode == MODE_CHALLENGE && level >= 20)
494 lg->next_level = NULL; /* End the challenge */
495 else if (level < s->count && !level_v[level].is_locked)
496 lg->next_level = &level_v[level];
498 lg->next_level = NULL;
505 /*---------------------------------------------------------------------------*/
507 void level_snap(int i)
509 char filename[MAXSTR];
511 /* Convert the level name to a BMP filename. */
513 memset(filename, 0, MAXSTR);
514 strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
515 strcat(filename, ".bmp");
517 /* Initialize the game for a snapshot. */
519 if (game_init(&level_v[i], 0, 0))
521 /* Render the level and grab the screen. */
527 SDL_GL_SwapBuffers();
529 image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
534 /* Open each level of the current set */
537 for (i=0; i < current_set->count; i++)
538 level_v[i].is_locked = 0;
542 /*---------------------------------------------------------------------------*/