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;
52 char states[MAXLVL+1];
54 if ((fout = fopen(config_user(s->user_scores), "w")))
56 for (i = 0 ; i < s->count ; i++)
58 if (level_v[i].is_locked)
60 else if (level_v[i].is_completed)
65 states[s->count] = '\0';
66 fprintf(fout, "%s\n",states);
68 put_score(fout, &s->time_score);
69 put_score(fout, &s->coin_score);
71 for (i = 0 ; i < s->count ; i++)
74 put_score(fout, &l->time_score);
75 put_score(fout, &l->goal_score);
76 put_score(fout, &l->coin_score);
83 static int get_score(FILE *fp, struct score *s)
87 for (j = 0; j < NSCORE && res; j++)
89 res = (fscanf(fp, "%d %d %s\n",
90 &s->timer[j], &s->coins[j], s->player[j])) == 3;
95 static void set_load_hs(void)
96 /* Get the score of the set */
98 struct set *s = current_set;
103 const char *fn = config_user(s->user_scores);
104 char states[MAXLVL+1];
106 if ((fin = fopen(fn, "r")))
108 res = ((fscanf(fin, "%s\n", states) == 1) &&
109 (strlen(states) == s->count));
110 for (i = 0 ; i < s->count && res ; i++)
112 if (states[i] == 'L')
114 level_v[i].is_locked = 1;
115 level_v[i].is_completed = 0;
117 else if (states[i] == 'C')
119 level_v[i].is_locked = 0;
120 level_v[i].is_completed = 1;
122 else if (states[i] == 'O')
124 level_v[i].is_locked = 0;
125 level_v[i].is_completed = 0;
132 get_score(fin, &s->time_score) &&
133 get_score(fin, &s->coin_score);
135 for (i = 0; i < s->count && res; i++)
138 res = get_score(fin, &l->time_score) &&
139 get_score(fin, &l->goal_score) &&
140 get_score(fin, &l->coin_score);
146 if (!res && errno != ENOENT)
148 fprintf(stderr, _("Error while loading user high-score file '%s': "), fn);
152 fprintf(stderr, _("Incorrect format\n"));
156 static char* chomp(char *str)
157 /* Remove trailing \n if any */
159 char *p = str + strlen(str) - 1;
160 if (p >= str && *p == '\n') *p = 0;
164 static int set_load(struct set *s, const char *filename)
171 /* Open the datafile */
173 fin = fopen(filename, "r");
176 fprintf(stderr, _("Cannot load the set file '%s':"), filename);
181 /* Raz the set structure */
183 memset(s, 0, sizeof(struct set));
185 /* Set some sane values in case the scores hs is missing. */
187 score_init_hs(&s->time_score, 359999, 0);
188 score_init_hs(&s->coin_score, 359999, 0);
190 /* Load set metadata */
192 strcpy(s->file, filename);
193 if ((res = fgets(buf, MAXSTR, fin) != NULL))
194 strcpy(s->name, chomp(buf));
195 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
196 strcpy(s->desc, chomp(buf));
197 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
198 strcpy(s->setname, chomp(buf));
199 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
200 strcpy(s->shot, chomp(buf));
201 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
202 sscanf(buf, "%d %d %d %d %d %d",
203 &s->time_score.timer[0],
204 &s->time_score.timer[1],
205 &s->time_score.timer[2],
206 &s->coin_score.coins[0],
207 &s->coin_score.coins[1],
208 &s->coin_score.coins[2]);
209 strcpy(s->user_scores, "neverballhs-");
210 strcat(s->user_scores, s->setname);
212 /* Count levels levels. */
216 while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
219 /* Close the file, since it's no more needed */
223 /* Load the levels states (stored in the user highscore file) */
224 s->locked = s->count;
226 if ((fin = fopen(config_user(s->user_scores), "r")))
228 char states[MAXLVL + 1];
230 if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
232 for (i=0; i<s->count; i++)
234 if (states[i] == 'O')
236 else if (states[i] == 'C')
245 if (s->locked == s->count)
246 s->locked = s->count-1;
251 /*---------------------------------------------------------------------------*/
257 char filename[MAXSTR];
264 if ((fin = fopen(config_data(SET_FILE), "r")))
267 while (count < MAXSET && res)
269 set = &(set_v[count]);
271 /* clean the set data */
273 res = (fgets(filename, MAXSTR, fin) != NULL);
278 res = set_load(set, config_data(filename));
291 /*---------------------------------------------------------------------------*/
293 int set_exists(int i)
295 return (0 <= i && i < count);
298 const struct set *get_set(int i)
300 return set_exists(i) ? &set_v[i] : NULL;
303 /*---------------------------------------------------------------------------*/
305 int set_unlocked(const struct set *s)
306 /* Are all levels (even extra bonus) unlocked? */
308 return s->locked == 0;
311 int set_completed(const struct set *s)
312 /* Are all levels (even extra bonus) completed? */
314 return s->completed == s->count;
317 int set_level_exists(const struct set *s, int i)
318 /* Is the level i of the set exists */
320 return (i >= 0) && (i < s->count);
323 /*---------------------------------------------------------------------------*/
325 static void set_load_levels(void)
326 /* Load more the levels of the current set */
336 fin = fopen(current_set->file, "r");
340 /* Skip the five first lines */
342 fgets(buf, MAXSTR, fin);
343 for(i=0; i<current_set->count && res; i++)
346 res = (fscanf(fin, "%s", name) == 1);
349 level_load(config_data(name), l);
351 /* Initialize set related info */
352 l->set = current_set;
355 sprintf(l->numbername, _("B%d"), bnb++);
357 sprintf(l->numbername, "%02d", nb++);
361 level_v[0].is_locked = 0; /* unlock the first level */
363 assert(i == current_set->count);
368 assert(set_exists(i));
369 current_set = &set_v[i];
374 const struct set *curr_set(void)
379 const struct level *get_level(int i)
381 return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
384 /*---------------------------------------------------------------------------*/
386 static int score_time_comp(const struct score *S, int i, int j)
388 if (S->timer[i] < S->timer[j])
391 if (S->timer[i] == S->timer[j] &&
392 S->coins[i] > S->coins[j])
398 static int score_coin_comp(const struct score *S, int i, int j)
400 if (S->coins[i] > S->coins[j])
403 if (S->coins[i] == S->coins[j] &&
404 S->timer[i] < S->timer[j])
410 static void score_swap(struct score *S, int i, int j)
415 strncpy(player, S->player[i], MAXNAM);
416 strncpy(S->player[i], S->player[j], MAXNAM);
417 strncpy(S->player[j], player, MAXNAM);
420 S->timer[i] = S->timer[j];
424 S->coins[i] = S->coins[j];
428 static int score_time_insert(struct score *s, const char* player, int timer, int coins)
432 strncpy(s->player[3], player, MAXNAM);
436 for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
437 score_swap(s, i + 1, i);
441 static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
445 strncpy(s->player[3], player, MAXNAM);
449 for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
450 score_swap(s, i + 1, i);
454 static int level_score_update(struct level_game *lg, const char *player)
455 /* Update the level score rank according to coins and timer */
457 int timer = lg->timer;
458 int coins = lg->coins;
459 struct level * l = &level_v[lg->level->number];
461 lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
463 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
464 lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
468 lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
470 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
473 static int set_score_update(struct level_game *lg, const char *player)
474 /* Update the set score rank according to score and times */
476 int timer = lg->times;
477 int coins = lg->score;
478 struct set * s = current_set;
480 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
481 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
482 return (lg->score_rank < 3 || lg->times_rank < 3);
486 void score_change_name(struct level_game *lg, const char *player)
487 /* Update the player name for set and level high-score */
489 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
490 struct set * s = current_set;
491 struct level *l = &level_v[lg->level->number];
492 UPDATE(lg->time_rank, l->time_score);
493 UPDATE(lg->goal_rank, l->goal_score);
494 UPDATE(lg->coin_rank, l->coin_score);
495 UPDATE(lg->score_rank, s->coin_score);
496 UPDATE(lg->times_rank, s->time_score);
500 static struct level *next_level(int i)
502 /* Return the ith level, or NULL */
503 return set_level_exists(current_set, i+1) ? &level_v[i+1] : NULL;
506 static struct level *next_normal_level(int i)
507 /* Return the next notmal level (starting for i)
508 * Return NULL if there is not a such level */
510 for (i++; i < current_set->count; i++)
511 if (!level_v[i].is_bonus)
516 void set_finish_level(struct level_game *lg, const char *player)
517 /* Inform the set that a level is finished.
518 * Update next_level and score rank fields */
520 struct set *s = current_set;
521 int ln = lg->level->number; /* curent level number */
522 struct level *cl = &level_v[ln]; /* current level */
523 struct level *nl = NULL; /* next level*/
524 int dirty = 0; /* HS should be saved? */
526 assert(s == cl->set);
529 if (lg->state == GAME_GOAL)
532 dirty = level_score_update(lg, player);
533 dirty = set_score_update(lg, player) || dirty;
535 /* if no set, no next level */
538 /* if no set, return */
539 lg->next_level = NULL;
543 /* Complete the level */
544 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
546 /* Complete the level */
547 if (!cl->is_completed)
549 cl->is_completed = 1;
555 /* Identify the follwing level */
556 nl = next_level(ln + lg->state_value);
559 /* skip bonuses if unlocked in non challenge mode*/
560 if(nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
561 nl = next_normal_level(nl->number);
563 else if (lg->mode == MODE_CHALLENGE)
566 else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
568 /* On fail, identify the next level (only in bonus for challenge) */
569 nl = next_normal_level(ln);
570 /* Next level may be unavailable */
571 if (!cl->is_bonus && nl != NULL && nl->is_locked)
573 /* Fail a bonus level but win the set! */
574 else if (nl == NULL && lg->mode == MODE_CHALLENGE)
578 /* Win ! => unlock all levels */
585 /* unlock the next level if needed */
586 if(nl != NULL && nl->is_locked)
588 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
598 /* got the next level */
606 /*---------------------------------------------------------------------------*/
608 void level_snap(int i)
610 char filename[MAXSTR];
612 /* Convert the level name to a BMP filename. */
614 memset(filename, 0, MAXSTR);
615 strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
616 strcat(filename, ".bmp");
618 /* Initialize the game for a snapshot. */
620 if (game_init(&level_v[i], 0, 0))
622 /* Render the level and grab the screen. */
628 SDL_GL_SwapBuffers();
630 image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
635 /* Open each level of the current set */
638 current_set->locked = 0;
639 for (i=0; i < current_set->count; i++)
640 level_v[i].is_locked = 0;
644 /*---------------------------------------------------------------------------*/