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 /*---------------------------------------------------------------------------*/
36 static void put_score(FILE *fp, const struct score *s)
39 for (j = 0; j < NSCORE; j++)
40 fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
43 static void set_store_hs(const struct set *s)
44 /* Store the score of the set */
48 const struct level *l;
49 char states[MAXLVL + 1];
50 struct level *level_v = s->level_v;
52 if ((fout = fopen(config_user(s->user_scores), "w")))
54 for (i = 0; i < s->count; i++)
56 if (level_v[i].is_locked)
58 else if (level_v[i].is_completed)
63 states[s->count] = '\0';
64 fprintf(fout, "%s\n",states);
66 put_score(fout, &s->time_score);
67 put_score(fout, &s->coin_score);
69 for (i = 0; i < s->count; i++)
72 put_score(fout, &l->score.best_times);
73 put_score(fout, &l->score.unlock_goal);
74 put_score(fout, &l->score.most_coins);
81 static int get_score(FILE *fp, struct score *s)
85 for (j = 0; j < NSCORE && res; j++)
87 res = (fscanf(fp, "%d %d %s\n",
88 &s->timer[j], &s->coins[j], s->player[j])) == 3;
93 static void set_load_hs(struct set *s)
94 /* Get the score of the set */
100 const char *fn = config_user(s->user_scores);
101 char states[MAXLVL + 1];
102 struct level *level_v = s->level_v;
104 /* Load the levels states (stored in the user highscore file) */
106 s->locked = s->count;
109 if ((fin = fopen(fn, "r")))
111 res = ((fscanf(fin, "%s\n", states) == 1) &&
112 (strlen(states) == s->count));
113 for (i = 0; i < s->count && res; i++)
118 level_v[i].is_locked = 1;
119 level_v[i].is_completed = 0;
123 level_v[i].is_locked = 0;
124 level_v[i].is_completed = 1;
130 level_v[i].is_locked = 0;
131 level_v[i].is_completed = 0;
141 get_score(fin, &s->time_score) &&
142 get_score(fin, &s->coin_score);
144 for (i = 0; i < s->count && res; i++)
147 res = get_score(fin, &l->score.best_times) &&
148 get_score(fin, &l->score.unlock_goal) &&
149 get_score(fin, &l->score.most_coins);
155 s->level_v[0].is_locked = 0; /* unlock the first level */
156 if (s->locked == s->count)
157 s->locked = s->count-1;
159 if (!res && errno != ENOENT)
162 _("Error while loading user high-score file '%s': %s\n"),
163 fn, errno ? strerror(errno) : _("Incorrect format"));
167 /* Remove trailing \n if any */
169 static char *chomp(char *str)
171 char *p = str + strlen(str) - 1;
172 if (p >= str && *p == '\n')
177 static int set_load(struct set *s, const char *filename)
187 fin = fopen(config_data(filename), "r");
191 fprintf(stderr, _("Cannot load the set file '%s': %s\n"),
192 filename, strerror(errno));
196 memset(s, 0, sizeof (struct set));
198 /* Set some sane values in case the scores hs is missing. */
200 score_init_hs(&s->time_score, 359999, 0);
201 score_init_hs(&s->coin_score, 359999, 0);
203 /* Load set metadata. */
205 strcpy(s->file, filename);
207 if ((res = fgets(buf, MAXSTR, fin) != NULL))
208 strcpy(s->name, chomp(buf));
209 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
210 strcpy(s->desc, chomp(buf));
211 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
212 strcpy(s->setname, chomp(buf));
213 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
214 strcpy(s->shot, chomp(buf));
215 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
216 sscanf(buf, "%d %d %d %d %d %d",
217 &s->time_score.timer[0],
218 &s->time_score.timer[1],
219 &s->time_score.timer[2],
220 &s->coin_score.coins[0],
221 &s->coin_score.coins[1],
222 &s->coin_score.coins[2]);
224 strcpy(s->user_scores, "neverballhs-");
225 strcat(s->user_scores, s->setname);
229 for (i=0 ; i < MAXLVL && (res = (fscanf(fin, "%s", name) == 1)) ; i++)
233 level_load(config_data(name), l);
235 /* Initialize set related info */
239 sprintf(l->repr, _("B%d"), bnb++);
241 sprintf(l->repr, "%02d", nb++);
250 /* Load scores and user level state */
257 /*---------------------------------------------------------------------------*/
263 char filename[MAXSTR];
268 if ((fin = fopen(config_data(SET_FILE), "r")))
270 while (count < MAXSET && fgets(filename, MAXSTR, fin))
273 set = &(set_v[count]);
275 if (set_load(set, filename))
285 /*---------------------------------------------------------------------------*/
287 int set_exists(int i)
289 return (0 <= i && i < count);
292 const struct set *get_set(int i)
294 return set_exists(i) ? &set_v[i] : NULL;
297 /*---------------------------------------------------------------------------*/
299 int set_unlocked(const struct set *s)
300 /* Are all levels (even extra bonus) unlocked? */
302 return s->locked == 0;
305 int set_completed(const struct set *s)
306 /* Are all levels (even extra bonus) completed? */
308 return s->completed == s->count;
311 int set_level_exists(const struct set *s, int i)
312 /* Does the level i of the set exist? */
314 return (i >= 0) && (i < s->count);
317 /*---------------------------------------------------------------------------*/
321 current_set = &set_v[i];
324 const struct set *curr_set(void)
329 const struct level *get_level(int i)
331 return (i >= 0 && i < current_set->count) ? ¤t_set->level_v[i] : NULL;
334 /*---------------------------------------------------------------------------*/
336 static int score_time_comp(const struct score *S, int i, int j)
338 if (S->timer[i] < S->timer[j])
341 if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
347 static int score_coin_comp(const struct score *S, int i, int j)
349 if (S->coins[i] > S->coins[j])
352 if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
358 static void score_swap(struct score *S, int i, int j)
363 strncpy(player, S->player[i], MAXNAM);
364 strncpy(S->player[i], S->player[j], MAXNAM);
365 strncpy(S->player[j], player, MAXNAM);
368 S->timer[i] = S->timer[j];
372 S->coins[i] = S->coins[j];
376 static int score_time_insert(struct score *s, const char *player, int timer,
381 strncpy(s->player[3], player, MAXNAM);
385 for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
386 score_swap(s, i + 1, i);
391 static int score_coin_insert(struct score *s, const char *player, int timer,
396 strncpy(s->player[3], player, MAXNAM);
400 for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
401 score_swap(s, i + 1, i);
406 static int level_score_update(struct level_game *lg, const char *player)
407 /* Update the level score rank according to coins and timer */
409 int timer = lg->timer;
410 int coins = lg->coins;
411 struct level *l = ¤t_set->level_v[lg->level->number];
413 lg->time_rank = score_time_insert(&l->score.best_times,
414 player, timer, coins);
416 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
417 lg->goal_rank = score_time_insert(&l->score.unlock_goal,
418 player, timer, coins);
422 lg->coin_rank = score_coin_insert(&l->score.most_coins,
423 player, timer, coins);
425 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
428 static int set_score_update(struct level_game *lg, const char *player)
429 /* Update the set score rank according to score and times */
431 int timer = lg->times;
432 int coins = lg->score;
433 struct set *s = current_set;
435 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
436 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
437 return (lg->score_rank < 3 || lg->times_rank < 3);
441 void score_change_name(struct level_game *lg, const char *player)
442 /* Update the player name for set and level high-score */
444 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
445 struct set *s = current_set;
446 struct level *l = &s->level_v[lg->level->number];
447 UPDATE(lg->time_rank, l->score.best_times);
448 UPDATE(lg->goal_rank, l->score.unlock_goal);
449 UPDATE(lg->coin_rank, l->score.most_coins);
450 UPDATE(lg->score_rank, s->coin_score);
451 UPDATE(lg->times_rank, s->time_score);
455 static struct level *next_level(int i)
457 /* Return the ith level, or NULL */
458 return set_level_exists(current_set, i + 1) ? ¤t_set->level_v[i + 1] : NULL;
461 static struct level *next_normal_level(int i)
462 /* Return the next normal level (starting for i)
463 * Return NULL if there is not a such level */
465 for (i++; i < current_set->count; i++)
466 if (!current_set->level_v[i].is_bonus)
467 return ¤t_set->level_v[i];
471 void set_finish_level(struct level_game *lg, const char *player)
472 /* Inform the set that a level is finished.
473 * Update next_level and score rank fields */
475 struct set *s = current_set;
476 int ln = lg->level->number; /* curent level number */
477 struct level *cl = &s->level_v[ln]; /* current level */
478 struct level *nl = NULL; /* next level */
479 int dirty = 0; /* HS should be saved? */
481 assert(s == cl->set);
483 /* if no set, no next level */
486 /* if no set, return */
487 lg->next_level = NULL;
491 /* On level completed */
492 if (lg->state == GAME_GOAL)
494 /* Update level scores */
495 dirty = level_score_update(lg, player);
497 /* Complete the level */
498 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
500 /* Complete the level */
501 if (!cl->is_completed)
503 cl->is_completed = 1;
510 /* On goal reached */
511 if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
513 /* Identify the following level */
514 nl = next_level(ln + lg->state_value);
517 /* skip bonuses if unlocked in non challenge mode */
518 if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
519 nl = next_normal_level(nl->number);
521 else if (lg->mode == MODE_CHALLENGE)
524 else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
526 /* On fail, identify the next level (only in bonus for challenge) */
527 nl = next_normal_level(ln);
528 /* Next level may be unavailable */
529 if (!cl->is_bonus && nl != NULL && nl->is_locked)
531 /* Fail a bonus level but win the set! */
532 else if (nl == NULL && lg->mode == MODE_CHALLENGE)
539 /* update set score */
540 set_score_update(lg, player);
541 /* unlock all levels */
546 /* unlock the next level if needed */
547 if (nl != NULL && nl->is_locked)
549 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
560 /* got the next level */
568 /*---------------------------------------------------------------------------*/
570 void level_snap(int i)
572 char filename[MAXSTR];
574 struct level *level_v = current_set->level_v;
576 /* Convert the level name to a PNG filename. */
578 memset(filename, 0, MAXSTR);
580 ext = strrchr(level_v[i].file, '.');
581 strncpy(filename, level_v[i].file,
582 ext ? ext - level_v[i].file : strlen(level_v[i].file));
583 strcat(filename, ".png");
585 /* Initialize the game for a snapshot. */
587 if (game_init(&level_v[i], 0, 0))
591 if ((shadow = config_get_d(CONFIG_SHADOW)))
592 config_set_d(CONFIG_SHADOW, 0);
594 /* Render the level and grab the screen. */
600 SDL_GL_SwapBuffers();
602 image_snap(filename);
605 config_set_d(CONFIG_SHADOW, 1);
610 /* Open each level of the current set */
613 current_set->locked = 0;
614 for (i = 0; i < current_set->count; i++)
615 current_set->level_v[i].is_locked = 0;
619 /*---------------------------------------------------------------------------*/