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 const struct level *search_level(const char *levelname)
337 for (s=0 ; s < count ; s++)
338 for (l=0 ; l < set_v[s].count ; l++)
339 if (strcmp(set_v[s].level_v[l].levelname, levelname) == 0)
340 return &set_v[s].level_v[l];
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] && S->coins[i] > S->coins[j])
357 static int score_coin_comp(const struct score *S, int i, int j)
359 if (S->coins[i] > S->coins[j])
362 if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
368 static void score_swap(struct score *S, int i, int j)
373 strncpy(player, S->player[i], MAXNAM);
374 strncpy(S->player[i], S->player[j], MAXNAM);
375 strncpy(S->player[j], player, MAXNAM);
378 S->timer[i] = S->timer[j];
382 S->coins[i] = S->coins[j];
386 static int score_time_insert(struct score *s, const char *player, int timer,
391 strncpy(s->player[3], player, MAXNAM);
395 for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
396 score_swap(s, i + 1, i);
401 static int score_coin_insert(struct score *s, const char *player, int timer,
406 strncpy(s->player[3], player, MAXNAM);
410 for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
411 score_swap(s, i + 1, i);
416 static int level_score_update(struct level_game *lg, const char *player)
417 /* Update the level score rank according to coins and timer */
419 int timer = lg->timer;
420 int coins = lg->coins;
421 struct level *l = ¤t_set->level_v[lg->level->number];
423 lg->time_rank = score_time_insert(&l->score.best_times,
424 player, timer, coins);
426 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
427 lg->goal_rank = score_time_insert(&l->score.unlock_goal,
428 player, timer, coins);
432 lg->coin_rank = score_coin_insert(&l->score.most_coins,
433 player, timer, coins);
435 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
438 static int set_score_update(struct level_game *lg, const char *player)
439 /* Update the set score rank according to score and times */
441 int timer = lg->times;
442 int coins = lg->score;
443 struct set *s = current_set;
445 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
446 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
447 return (lg->score_rank < 3 || lg->times_rank < 3);
451 void score_change_name(struct level_game *lg, const char *player)
452 /* Update the player name for set and level high-score */
454 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
455 struct set *s = current_set;
456 struct level *l = &s->level_v[lg->level->number];
457 UPDATE(lg->time_rank, l->score.best_times);
458 UPDATE(lg->goal_rank, l->score.unlock_goal);
459 UPDATE(lg->coin_rank, l->score.most_coins);
460 UPDATE(lg->score_rank, s->coin_score);
461 UPDATE(lg->times_rank, s->time_score);
465 static struct level *next_level(int i)
467 /* Return the ith level, or NULL */
468 return set_level_exists(current_set, i + 1) ? ¤t_set->level_v[i + 1] : NULL;
471 static struct level *next_normal_level(int i)
472 /* Return the next normal level (starting for i)
473 * Return NULL if there is not a such level */
475 for (i++; i < current_set->count; i++)
476 if (!current_set->level_v[i].is_bonus)
477 return ¤t_set->level_v[i];
481 void set_finish_level(struct level_game *lg, const char *player)
482 /* Inform the set that a level is finished.
483 * Update next_level and score rank fields */
485 struct set *s = current_set;
486 int ln = lg->level->number; /* curent level number */
487 struct level *cl = &s->level_v[ln]; /* current level */
488 struct level *nl = NULL; /* next level */
489 int dirty = 0; /* HS should be saved? */
491 assert(s == cl->set);
493 /* if no set, no next level */
496 /* if no set, return */
497 lg->next_level = NULL;
501 /* On level completed */
502 if (lg->state == GAME_GOAL)
504 /* Update level scores */
505 dirty = level_score_update(lg, player);
507 /* Complete the level */
508 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
510 /* Complete the level */
511 if (!cl->is_completed)
513 cl->is_completed = 1;
520 /* On goal reached */
521 if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
523 /* Identify the following level */
524 nl = next_level(ln + lg->state_value);
527 /* skip bonuses if unlocked in non challenge mode */
528 if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
529 nl = next_normal_level(nl->number);
531 else if (lg->mode == MODE_CHALLENGE)
534 else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
536 /* On fail, identify the next level (only in bonus for challenge) */
537 nl = next_normal_level(ln);
538 /* Next level may be unavailable */
539 if (!cl->is_bonus && nl != NULL && nl->is_locked)
541 /* Fail a bonus level but win the set! */
542 else if (nl == NULL && lg->mode == MODE_CHALLENGE)
549 /* update set score */
550 set_score_update(lg, player);
551 /* unlock all levels */
556 /* unlock the next level if needed */
557 if (nl != NULL && nl->is_locked)
559 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
570 /* got the next level */
578 /*---------------------------------------------------------------------------*/
580 void level_snap(int i)
582 char filename[MAXSTR];
584 struct level *level_v = current_set->level_v;
586 /* Convert the level name to a PNG filename. */
588 memset(filename, 0, MAXSTR);
590 ext = strrchr(level_v[i].file, '.');
591 strncpy(filename, level_v[i].file,
592 ext ? ext - level_v[i].file : strlen(level_v[i].file));
593 strcat(filename, ".png");
595 /* Initialize the game for a snapshot. */
597 if (game_init(&level_v[i], 0, 0))
601 if ((shadow = config_get_d(CONFIG_SHADOW)))
602 config_set_d(CONFIG_SHADOW, 0);
604 /* Render the level and grab the screen. */
610 SDL_GL_SwapBuffers();
612 image_snap(filename);
615 config_set_d(CONFIG_SHADOW, 1);
620 /* Open each level of the current set */
623 current_set->locked = 0;
624 for (i = 0; i < current_set->count; i++)
625 current_set->level_v[i].is_locked = 0;
629 /*---------------------------------------------------------------------------*/