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(const struct set *s)
46 /* Store the score of the set */
50 const struct set_level * sl;
56 if ((fout = fopen(config_user(s->user_scores), "w")))
58 fprintf(fout, "%d\n", s->limit);
60 put_score(fout, &s->time_score);
61 put_score(fout, &s->coin_score);
63 for (i = 0 ; i <= l ; i++)
66 put_score(fout, &sl->time_score);
67 put_score(fout, &sl->goal_score);
68 put_score(fout, &sl->coin_score);
75 static int get_score(FILE *fp, struct score *s)
79 for (j = 0; j < NSCORE && res; j++)
81 res = (fscanf(fp, "%d %d %s\n",
82 &s->timer[j], &s->coins[j], s->player[j])) == 3;
87 static void set_load_hs(struct set *s)
88 /* Get the score of the set and set the limit */
93 struct set_level * sl;
94 const char *fn = config_user(s->user_scores);
97 if ((fin = fopen(fn, "r")))
99 res = (fscanf(fin, "%d\n", &l) == 1) &&
101 get_score(fin, &s->time_score) &&
102 get_score(fin, &s->coin_score);
107 for (i = 0; i <= l && res; i++)
110 res = get_score(fin, &sl->time_score) &&
111 get_score(fin, &sl->goal_score) &&
112 get_score(fin, &sl->coin_score);
118 if (!res && errno != ENOENT)
120 fprintf(stderr, _("Error while loading user high-score file '%s': "), fn);
124 fprintf(stderr, _("Incorrect format\n"));
128 static const char * numbernames[] = {
129 "01", "02", "03", "04", "05",
130 "06", "07", "08", "09", "10",
131 "11", "12", "13", "14", "15",
132 "16", "17", "18", "19", "20",
133 N_("B1"), N_("B2"), N_("B3"), N_("B4"), N_("B5")};
136 static void set_init_levels(struct set *s)
141 struct set_level * sl;
143 /* Load the levels list. */
147 if ((fin = fopen(config_data(s->init_levels), "r")))
149 while (count < MAXLVL && fgets(buf, MAXSTR, fin))
151 sl = &s->levels[s->count];
152 sl->numbername = numbernames[s->count];
158 /* Load the highscore level limit */
162 if ((fin = fopen(config_user(s->user_scores), "r")))
164 fscanf(fin, "%d\n", &s->limit);
165 if (s->limit > s->count)
172 static void score_init_hs(struct score *s, int timer, int coins)
175 strcpy(s->player[0], "Hard");
176 strcpy(s->player[1], "Medium");
177 strcpy(s->player[2], "Easy");
178 for (i = 0; i < NSCORE; i++)
186 static void set_init_hs(struct set *s)
192 struct set_level * sl;
193 const char *fn = config_data(s->init_scores);
195 /* Set some sane values in case the scores file is missing. */
196 score_init_hs(&s->time_score, 359999, 0);
197 score_init_hs(&s->coin_score, 359999, 0);
199 for (i = 0; i < s->count; i++)
202 score_init_hs(&sl->time_score, 59999, 0);
203 score_init_hs(&sl->goal_score, 59999, 0);
204 score_init_hs(&sl->coin_score, 59999, 0);
207 /* Load the default high scores file. */
209 if ((fin = fopen(fn, "r")))
211 res = fgets(buf, MAXSTR, fin) != NULL;
213 res = res && (sscanf(buf, "%d %d %d %d %d %d",
214 &s->time_score.timer[0],
215 &s->coin_score.coins[0],
216 &s->time_score.timer[1],
217 &s->coin_score.coins[1],
218 &s->time_score.timer[2],
219 &s->coin_score.coins[2]) == 6);
221 for (i = 0; i < s->count && res; i++)
224 res = (fgets(buf, MAXSTR, fin) != NULL) &&
225 sscanf(buf, "%d %d %d %d %d %d %d %d %d",
226 &(sl->time_score.timer[0]),
227 &(sl->goal_score.timer[0]),
228 &(sl->coin_score.coins[0]),
229 &(sl->time_score.timer[1]),
230 &(sl->goal_score.timer[1]),
231 &(sl->coin_score.coins[1]),
232 &(sl->time_score.timer[2]),
233 &(sl->goal_score.timer[2]),
234 &(sl->coin_score.coins[2])) == 9;
242 fprintf(stderr, _("Error while loading initial high-score file '%s': "), fn);
246 fprintf(stderr, _("Incorrect format\n"));
250 /*---------------------------------------------------------------------------*/
262 if ((fin = fopen(config_data(SET_FILE), "r")))
265 while (count < MAXSET && res)
267 set = &(set_v[count]);
269 /* clean the set data */
270 memset(set, 0, sizeof(struct set));
272 res = fscanf(fin, "%s %s %s %s\n",
277 fgets(set->name, MAXSTR, fin) &&
278 fgets(set->desc, MAXSTR, fin);
281 char *p = set->name + strlen(set->name) - 1;
282 char *q = set->desc + strlen(set->desc) - 1;
285 if (*p == '\n') *p = 0;
286 if (*q == '\n') *q = 0;
288 set_init_levels(set);
298 /*---------------------------------------------------------------------------*/
300 int set_exists(int i)
302 return (0 <= i && i < count);
305 const struct set *get_set(int i)
307 return set_exists(i) ? &set_v[i] : NULL;
310 /*---------------------------------------------------------------------------*/
312 int set_extra_bonus_opened(const struct set *s)
313 /* Are extra bonus openned (ie challenge completed)? */
315 return s->limit >= 20;
318 int set_completed(const struct set *s)
319 /* Are all levels (even extra bonus) completed? */
321 return s->limit >= s->count;
324 /*---------------------------------------------------------------------------*/
326 int set_level_exists(const struct set *s, int i)
327 /* Is the level i of the set exists */
329 return (i >= 0) && (i < s->count);
332 int set_level_opened(const struct set *s, int i)
333 /* Is the level i of the set completed? */
335 return (i >= 0) && (i <= s->limit);
338 int set_level_extra_bonus(const struct set *s, int i)
339 /* Is the level i of the set a extra-bonus level? */
341 return (i >= 20) && (i < s->count);
344 /*---------------------------------------------------------------------------*/
346 static void set_load_levels(void)
347 /* Load more the levels of the current set */
356 if ((fin = fopen(config_data(current_set->init_levels), "r")))
359 for(i=0; i<current_set->count && res; i++)
362 res = (fgets(buf, MAXSTR, fin) != NULL) &&
363 (sscanf(buf, "%s %s %s %s %d %d %s",
372 level_load(config_data(name), l);
376 assert(i == current_set->count);
381 assert(set_exists(i));
382 current_set = &set_v[i];
383 set_init_hs(current_set);
384 set_load_hs(current_set);
388 const struct set *curr_set(void)
393 const struct level *get_level(int i)
395 return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
398 /*---------------------------------------------------------------------------*/
400 static int score_time_comp(const struct score *S, int i, int j)
402 if (S->timer[i] < S->timer[j])
405 if (S->timer[i] == S->timer[j] &&
406 S->coins[i] > S->coins[j])
412 static int score_coin_comp(const struct score *S, int i, int j)
414 if (S->coins[i] > S->coins[j])
417 if (S->coins[i] == S->coins[j] &&
418 S->timer[i] < S->timer[j])
424 static void score_swap(struct score *S, int i, int j)
429 strncpy(player, S->player[i], MAXNAM);
430 strncpy(S->player[i], S->player[j], MAXNAM);
431 strncpy(S->player[j], player, MAXNAM);
434 S->timer[i] = S->timer[j];
438 S->coins[i] = S->coins[j];
442 static int score_time_insert(struct score *s, const char* player, int timer, int coins)
446 strncpy(s->player[3], player, MAXNAM);
450 for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
451 score_swap(s, i + 1, i);
455 static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
459 strncpy(s->player[3], player, MAXNAM);
463 for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
464 score_swap(s, i + 1, i);
468 static int level_score_update(struct level_game *lg, const char *player)
469 /* Update the level score rank according to coins and timer */
471 int timer = lg->timer;
472 int coins = lg->coins;
473 struct set_level * sl = ¤t_set->levels[lg->level];
475 lg->time_rank = score_time_insert(&sl->time_score, player, timer, coins);
477 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
478 lg->goal_rank = score_time_insert(&sl->goal_score, player, timer, coins);
482 lg->coin_rank = score_coin_insert(&sl->coin_score, player, timer, coins);
484 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
487 static int set_score_update(struct level_game *lg, const char *player)
488 /* Update the set score rank according to score and times */
490 int timer = lg->times;
491 int coins = lg->score;
492 struct set * s = current_set;
494 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
495 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
496 return (lg->score_rank < 3 || lg->times_rank < 3);
500 void score_change_name(struct level_game *lg, const char *player)
501 /* Update the player name for set and level high-score */
503 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
504 struct set * s = current_set;
505 struct set_level *l = &s->levels[lg->level];
506 UPDATE(lg->time_rank, l->time_score);
507 UPDATE(lg->goal_rank, l->goal_score);
508 UPDATE(lg->coin_rank, l->coin_score);
509 UPDATE(lg->score_rank, s->coin_score);
510 UPDATE(lg->times_rank, s->time_score);
514 void set_finish_level(struct level_game *lg, const char *player)
515 /* Inform the set that a level is finished.
516 * Update next_level and score rank fields */
518 struct set *s = current_set;
519 int level = lg->level;
523 dirty = level_score_update(lg, player);
524 dirty = set_score_update(lg, player) || dirty;
526 /* compute the next level */
529 /* if no set, return */
534 level++; /* level is the next level */
536 /* if the next level is not oppened */
537 if (s->limit < level)
538 if ((lg->mode == MODE_CHALLENGE) ||
539 (lg->mode == MODE_NORMAL && (level < 20 || level > 20)))
545 /* got the next level */
546 if (lg->mode == MODE_CHALLENGE && level >= 20)
547 lg->next_level = -1; /* End the challenge */
548 else if (level < s->count && level <= s->limit)
549 lg->next_level = level;
558 /*---------------------------------------------------------------------------*/
560 void level_snap(int i)
562 char filename[MAXSTR];
564 /* Convert the level name to a BMP filename. */
566 memset(filename, 0, MAXSTR);
567 strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
568 strcat(filename, ".bmp");
570 /* Initialize the game for a snapshot. */
572 if (game_init(&level_v[i], 0, 0))
574 /* Render the level and grab the screen. */
580 SDL_GL_SwapBuffers();
582 image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
587 /* Open each level of the current set */
589 current_set->limit = current_set->count;
593 /*---------------------------------------------------------------------------*/