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 /*---------------------------------------------------------------------------*/
31 static struct set set_v[MAXSET];
32 static struct level level_v[MAXLVL];
34 /*---------------------------------------------------------------------------*/
36 static void put_score(FILE *fp, const struct score *s)
40 for (j = 0; j < NSCORE; j++)
41 fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
44 /* Store the score of the set. */
45 static void set_store_hs(void)
47 const struct set *s = &set_v[set];
50 const struct level *l;
51 char states[MAXLVL + 1];
53 if ((fout = fopen(config_user(s->user_scores), "w")))
55 for (i = 0; i < s->count; i++)
57 if (level_v[i].is_locked)
59 else if (level_v[i].is_completed)
64 states[s->count] = '\0';
65 fprintf(fout, "%s\n",states);
67 put_score(fout, &s->time_score);
68 put_score(fout, &s->coin_score);
70 for (i = 0; i < s->count; i++)
74 put_score(fout, &l->score.best_times);
75 put_score(fout, &l->score.unlock_goal);
76 put_score(fout, &l->score.most_coins);
83 static int get_score(FILE *fp, struct score *s)
88 for (j = 0; j < NSCORE && res; j++)
90 res = fscanf(fp, "%d %d %s\n",
98 /* Get the score of the set. */
99 static void set_load_hs(void)
101 struct set *s = &set_v[set];
106 const char *fn = config_user(s->user_scores);
107 char states[MAXLVL + 1];
109 if ((fin = fopen(fn, "r")))
111 res = fscanf(fin, "%s\n", states) == 1 && 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;
128 level_v[i].is_locked = 0;
129 level_v[i].is_completed = 0;
138 get_score(fin, &s->time_score) &&
139 get_score(fin, &s->coin_score);
141 for (i = 0; i < s->count && res; i++)
144 res = get_score(fin, &l->score.best_times) &&
145 get_score(fin, &l->score.unlock_goal) &&
146 get_score(fin, &l->score.most_coins);
152 if (!res && errno != ENOENT)
155 _("Error while loading user high-score file '%s': %s\n"),
156 fn, errno ? strerror(errno) : _("Incorrect format"));
160 static char *strip_eol(char *str)
162 char *c = str + strlen(str) - 1;
164 while (c >= str && (*c == '\n' || *c =='\r'))
170 static int set_load(struct set *s, const char *filename)
176 fin = fopen(config_data(filename), "r");
180 fprintf(stderr, _("Cannot load the set file '%s': %s\n"),
181 filename, strerror(errno));
185 memset(s, 0, sizeof (struct set));
187 /* Set some sane values in case the scores hs is missing. */
189 score_init_hs(&s->time_score, 359999, 0);
190 score_init_hs(&s->coin_score, 359999, 0);
192 /* Load set metadata. */
194 strcpy(s->file, filename);
196 if ((res = fgets(buf, MAXSTR, fin) != NULL))
197 strcpy(s->name, strip_eol(buf));
198 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
199 strcpy(s->desc, strip_eol(buf));
200 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
201 strcpy(s->id, strip_eol(buf));
202 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
203 strcpy(s->shot, strip_eol(buf));
204 if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
205 sscanf(buf, "%d %d %d %d %d %d",
206 &s->time_score.timer[0],
207 &s->time_score.timer[1],
208 &s->time_score.timer[2],
209 &s->coin_score.coins[0],
210 &s->coin_score.coins[1],
211 &s->coin_score.coins[2]);
213 strcpy(s->user_scores, "neverballhs-");
214 strcat(s->user_scores, s->id);
220 while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
225 /* Load the levels states (stored in the user high score file) */
227 s->locked = s->count;
230 if ((fin = fopen(config_user(s->user_scores), "r")))
232 char states[MAXLVL + 1];
234 if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
236 for (i = 0; i < s->count; i++)
238 if (states[i] == 'O')
240 else if (states[i] == 'C')
249 if (s->locked == s->count)
250 s->locked = s->count-1;
255 /*---------------------------------------------------------------------------*/
265 if ((fin = fopen(config_data(SET_FILE), "r")))
267 while (count < MAXSET && fgets(name, MAXSTR, fin))
268 if (set_load(&set_v[count], strip_eol(name)))
277 /*---------------------------------------------------------------------------*/
279 int set_exists(int i)
281 return (0 <= i && i < count);
284 const struct set *get_set(int i)
286 return set_exists(i) ? &set_v[i] : NULL;
289 /*---------------------------------------------------------------------------*/
291 int set_unlocked(const struct set *s)
293 return s->locked == 0;
296 int set_completed(const struct set *s)
298 return s->completed == s->count;
301 int set_level_exists(const struct set *s, int i)
303 return (i >= 0) && (i < s->count);
306 /*---------------------------------------------------------------------------*/
308 static void set_load_levels(void)
319 const char *roman[] = {
321 "I", "II", "III", "IV", "V",
322 "VI", "VII", "VIII", "IX", "X",
323 "XI", "XII", "XIII", "XIV", "XV",
324 "XVI", "XVII", "XVIII", "XIX", "XX",
325 "XXI", "XXII", "XXIII", "XXIV", "XXV"
328 if ((fin = fopen(config_data(set_v[set].file), "r")))
332 /* Skip the five first lines */
333 for (i = 0; i < 5; i++)
334 fgets(buf, MAXSTR, fin);
336 for (i = 0; i < set_v[set].count && res; i++)
340 res = (fscanf(fin, "%s", name) == 1);
345 /* Initialize set related info */
346 l->set = &set_v[set];
350 sprintf(l->repr, "%s", roman[bnb++]);
352 sprintf(l->repr, "%02d", nb++);
357 level_v[0].is_locked = 0; /* unlock the first level */
361 assert(i == set_v[set].count);
372 const struct set *curr_set(void)
377 const struct level *get_level(int i)
379 return (i >= 0 && i < set_v[set].count) ? &level_v[i] : NULL;
382 /*---------------------------------------------------------------------------*/
384 /* Update the level score rank according to coins and timer. */
385 static int level_score_update(struct level_game *lg, const char *player)
387 int timer = lg->timer;
388 int coins = lg->coins;
389 struct level *l = &level_v[lg->level->number];
391 lg->time_rank = score_time_insert(&l->score.best_times,
392 player, timer, coins);
394 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
395 lg->goal_rank = score_time_insert(&l->score.unlock_goal,
396 player, timer, coins);
400 lg->coin_rank = score_coin_insert(&l->score.most_coins,
401 player, timer, coins);
403 return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
406 /* Update the set score rank according to score and times. */
407 static int set_score_update(struct level_game *lg, const char *player)
409 int timer = lg->times;
410 int coins = lg->score;
411 struct set *s = &set_v[set];
413 lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
414 lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
416 return (lg->score_rank < 3 || lg->times_rank < 3);
420 /* Update the player name for set and level high-score. */
421 void score_change_name(struct level_game *lg, const char *player)
423 struct set *s = &set_v[set];
424 struct level *l = &level_v[lg->level->number];
426 strncpy(l->score.best_times.player [lg->time_rank], player, MAXNAM);
427 strncpy(l->score.unlock_goal.player[lg->goal_rank], player, MAXNAM);
428 strncpy(l->score.most_coins.player [lg->coin_rank], player, MAXNAM);
430 strncpy(s->coin_score.player[lg->score_rank], player, MAXNAM);
431 strncpy(s->time_score.player[lg->times_rank], player, MAXNAM);
436 static struct level *next_level(int i)
438 return set_level_exists(&set_v[set], i + 1) ? &level_v[i + 1] : NULL;
441 static struct level *next_normal_level(int i)
443 for (i++; i < set_v[set].count; i++)
444 if (!level_v[i].is_bonus)
450 /* Inform the set that a level is finished. Update next level and score
453 void set_finish_level(struct level_game *lg, const char *player)
455 struct set *s = &set_v[set];
456 int ln = lg->level->number; /* Current level number */
457 struct level *cl = &level_v[ln]; /* Current level */
458 struct level *nl = NULL; /* Next level */
459 int dirty = 0; /* Should the score be saved? */
461 assert(s == cl->set);
463 /* if no set, no next level */
466 /* if no set, return */
467 lg->next_level = NULL;
471 /* On level completed */
472 if (lg->status == GAME_GOAL)
474 /* Update level scores */
475 dirty = level_score_update(lg, player);
477 /* Complete the level */
478 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
480 /* Complete the level */
481 if (!cl->is_completed)
483 cl->is_completed = 1;
490 /* On goal reached */
491 if (lg->status == GAME_GOAL)
493 /* Identify the following level */
497 /* skip bonuses if unlocked in non challenge mode */
498 if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
499 nl = next_normal_level(nl->number);
501 else if (lg->mode == MODE_CHALLENGE)
504 else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
506 /* On fail, identify the next level (only in bonus for challenge) */
507 nl = next_normal_level(ln);
508 /* Next level may be unavailable */
509 if (!cl->is_bonus && nl != NULL && nl->is_locked)
511 /* Fail a bonus level but win the set! */
512 else if (nl == NULL && lg->mode == MODE_CHALLENGE)
519 /* update set score */
520 set_score_update(lg, player);
521 /* unlock all levels */
526 /* unlock the next level if needed */
527 if (nl != NULL && nl->is_locked)
529 if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
540 /* got the next level */
548 /*---------------------------------------------------------------------------*/
550 void level_snap(int i)
552 char filename[MAXSTR];
555 /* Convert the level name to a PNG filename. */
557 memset(filename, 0, MAXSTR);
559 ext = strrchr(level_v[i].file, '.');
560 strncpy(filename, level_v[i].file,
561 ext ? ext - level_v[i].file : strlen(level_v[i].file));
562 strcat(filename, ".png");
564 /* Initialize the game for a snapshot. */
566 if (game_init(&level_v[i], 0, 0))
570 if ((shadow = config_get_d(CONFIG_SHADOW)))
571 config_set_d(CONFIG_SHADOW, 0);
573 /* Render the level and grab the screen. */
579 SDL_GL_SwapBuffers();
581 image_snap(filename);
584 config_set_d(CONFIG_SHADOW, 1);
589 /* Open each level of the current set */
593 set_v[set].locked = 0;
595 for (i = 0; i < set_v[set].count; i++)
596 level_v[i].is_locked = 0;
600 /*---------------------------------------------------------------------------*/