Re-add demo base name handling.
[neverball] / ball / set.c
index 1a27594..73f44ed 100644 (file)
@@ -31,8 +31,6 @@ static struct set set_v[MAXSET];     /* array of sets */
 
 static struct set *current_set;      /* currently selected set */
 
-static struct level level_v[MAXLVL]; /* levels of the current set  */
-
 /*---------------------------------------------------------------------------*/
 
 static void put_score(FILE *fp, const struct score *s)
@@ -42,14 +40,14 @@ static void put_score(FILE *fp, const struct score *s)
        fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
 }
 
-static void set_store_hs(void)
+static void set_store_hs(const struct set *s)
 /* Store the score of the set */
 {
-    const struct set *s = current_set;
     FILE *fout;
     int i;
     const struct level *l;
     char states[MAXLVL + 1];
+    struct level *level_v = s->level_v;
 
     if ((fout = fopen(config_user(s->user_scores), "w")))
     {
@@ -71,9 +69,9 @@ static void set_store_hs(void)
         for (i = 0; i < s->count; i++)
         {
             l = &level_v[i];
-            put_score(fout, &l->time_score);
-            put_score(fout, &l->goal_score);
-            put_score(fout, &l->coin_score);
+            put_score(fout, &l->score.best_times);
+            put_score(fout, &l->score.unlock_goal);
+            put_score(fout, &l->score.most_coins);
         }
 
         fclose(fout);
@@ -92,16 +90,21 @@ static int get_score(FILE *fp, struct score *s)
     return res;
 }
 
-static void set_load_hs(void)
+static void set_load_hs(struct set *s)
 /* Get the score of the set */
 {
-    struct set *s = current_set;
     FILE *fin;
     int i;
     int res = 0;
     struct level *l;
     const char *fn = config_user(s->user_scores);
     char states[MAXLVL + 1];
+    struct level *level_v = s->level_v;
+    
+    /* Load the levels states (stored in the user highscore file) */
+
+    s->locked = s->count;
+    s->completed = 0;
 
     if ((fin = fopen(fn, "r")))
     {
@@ -109,23 +112,29 @@ static void set_load_hs(void)
                (strlen(states) == s->count));
         for (i = 0; i < s->count && res; i++)
         {
-            if (states[i] == 'L')
+            switch (states[i])
             {
+            case 'L':
                 level_v[i].is_locked = 1;
                 level_v[i].is_completed = 0;
-            }
-            else if (states[i] == 'C')
-            {
+                break;
+
+            case 'C':
                 level_v[i].is_locked = 0;
                 level_v[i].is_completed = 1;
-            }
-            else if (states[i] == 'O')
-            {
+                s->completed += 1;
+                s->locked -= 1;
+                break;
+
+            case 'O':
                 level_v[i].is_locked = 0;
                 level_v[i].is_completed = 0;
-            }
-            else
+                s->locked -= 1;
+                break;
+
+            default:
                 res = 0;
+            }
         }
 
         res = res &&
@@ -135,62 +144,66 @@ static void set_load_hs(void)
         for (i = 0; i < s->count && res; i++)
         {
             l = &level_v[i];
-            res = get_score(fin, &l->time_score) &&
-                  get_score(fin, &l->goal_score) &&
-                  get_score(fin, &l->coin_score);
+            res = get_score(fin, &l->score.best_times) &&
+                  get_score(fin, &l->score.unlock_goal) &&
+                  get_score(fin, &l->score.most_coins);
         }
 
         fclose(fin);
     }
+    
+    s->level_v[0].is_locked = 0; /* unlock the first level */
+    if (s->locked == s->count)
+        s->locked = s->count-1;
 
     if (!res && errno != ENOENT)
     {
-        fprintf(stderr, _("Error while loading user high-score file '%s': "),
-                fn);
-        if (errno)
-            perror(NULL);
-        else
-            fprintf(stderr, _("Incorrect format\n"));
+        fprintf(stderr,
+                _("Error while loading user high-score file '%s': %s\n"),
+                fn, errno ? strerror(errno) : _("Incorrect format"));
     }
 }
 
-static char *chomp(char *str)
 /* Remove trailing \n if any */
+
+static char *chomp(char *str)
 {
     char *p = str + strlen(str) - 1;
-    if (p >= str && *p == '\n') *p = 0;
+    if (p >= str && *p == '\n')
+        *p = 0;
     return str;
 }
 
 static int set_load(struct set *s, const char *filename)
-/* Count levels */
 {
     FILE *fin;
     char buf[MAXSTR];
-    int res = 0;
+    int res;
+    struct level *l;
+    char name[MAXSTR];
+    int i = 0;
+    int nb = 1, bnb = 1;
 
-    /* Open the datafile */
+    fin = fopen(config_data(filename), "r");
 
-    fin = fopen(filename, "r");
-    if (fin == NULL)
+    if (!fin)
     {
-        fprintf(stderr, _("Cannot load the set file '%s':"), filename);
-        perror(NULL);
+        fprintf(stderr, _("Cannot load the set file '%s': %s\n"),
+                filename, strerror(errno));
         return 0;
     }
 
-    /* Raz the set structure */
-
-    memset(s, 0, sizeof(struct set));
+    memset(s, 0, sizeof (struct set));
 
     /* Set some sane values in case the scores hs is missing. */
 
     score_init_hs(&s->time_score, 359999, 0);
     score_init_hs(&s->coin_score, 359999, 0);
 
-    /* Load set metadata */
+    /* Load set metadata. */
 
     strcpy(s->file, filename);
+
     if ((res = fgets(buf, MAXSTR, fin) != NULL))
         strcpy(s->name, chomp(buf));
     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
@@ -207,44 +220,36 @@ static int set_load(struct set *s, const char *filename)
                 &s->coin_score.coins[0],
                 &s->coin_score.coins[1],
                 &s->coin_score.coins[2]);
+
     strcpy(s->user_scores, "neverballhs-");
     strcat(s->user_scores, s->setname);
 
-    /* Count levels levels. */
+    /* Load levels. */
 
-    s->count = 0;
+    for (i=0 ; i < MAXLVL && (res = (fscanf(fin, "%s", name) == 1)) ; i++)
+    {
+        l = &s->level_v[i];
 
-    while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
-        s->count++;
+        level_load(config_data(name), l);
 
-    /* Close the file, since it's no more needed */
+        /* Initialize set related info */
+        l->set        = s;
+        l->number     = i;
+        if (l->is_bonus)
+            sprintf(l->repr, _("B%d"), bnb++);
+        else
+            sprintf(l->repr, "%02d", nb++);
+        l->is_locked    = 1;
+        l->is_completed = 0;
+    }
 
-    fclose(fin);
+    s->count = i;
 
-    /* Load the levels states (stored in the user highscore file) */
-    s->locked = s->count;
-    s->completed = 0;
-    if ((fin = fopen(config_user(s->user_scores), "r")))
-    {
-        char states[MAXLVL + 1];
-        int i;
-        if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
-        {
-            for (i = 0; i < s->count; i++)
-            {
-                if (states[i] == 'O')
-                    s->locked -= 1;
-                else if (states[i] == 'C')
-                {
-                    s->completed += 1;
-                    s->locked -= 1;
-                }
-            }
-        }
-        fclose(fin);
-    }
-    if (s->locked == s->count)
-        s->locked = s->count-1;
+    fclose(fin);
+   
+    /* Load scores and user level state */
+    
+    set_load_hs(s);
 
     return 1;
 }
@@ -256,35 +261,23 @@ void set_init()
     FILE *fin;
     struct set *set;
     char filename[MAXSTR];
-    int res;
 
     current_set = NULL;
-
     count = 0;
 
     if ((fin = fopen(config_data(SET_FILE), "r")))
     {
-        res = 1;
-        while (count < MAXSET && res)
+        while (count < MAXSET && fgets(filename, MAXSTR, fin))
         {
+            chomp(filename);
             set = &(set_v[count]);
 
-            /* clean the set data */
-
-            res = (fgets(filename, MAXSTR, fin) != NULL);
-            if (res)
+            if (set_load(set, filename))
             {
-                chomp(filename);
-
-                res = set_load(set, config_data(filename));
-                if (res)
-                {
-                    set->number = count;
-                    count++;
-                }
+                set->number = count;
+                count++;
             }
         }
-
         fclose(fin);
     }
 }
@@ -323,55 +316,9 @@ int  set_level_exists(const struct set *s, int i)
 
 /*---------------------------------------------------------------------------*/
 
-static void set_load_levels(void)
-/* Load more the levels of the current set */
-{
-    FILE *fin;
-    char buf[MAXSTR];
-    char name[MAXSTR];
-    struct level *l;
-
-    int i = 0, res;
-    int nb = 1, bnb = 1;
-
-    fin = fopen(current_set->file, "r");
-    assert(fin != NULL);
-
-    res = 1;
-
-    /* Skip the five first lines */
-    for(i = 0; i < 5; i++)
-        fgets(buf, MAXSTR, fin);
-
-    for(i = 0; i < current_set->count && res; i++)
-    {
-        l = &level_v[i];
-        res = (fscanf(fin, "%s", name) == 1);
-        assert(res);
-
-        level_load(config_data(name), l);
-
-        /* Initialize set related info */
-        l->set        = current_set;
-        l->number     = i;
-        if (l->is_bonus)
-            sprintf(l->numbername, _("B%d"), bnb++);
-        else
-            sprintf(l->numbername, "%02d", nb++);
-        l->is_locked    = 1;
-        l->is_completed = 0;
-    }
-    level_v[0].is_locked = 0; /* unlock the first level */
-    fclose(fin);
-    assert(i == current_set->count);
-}
-
 void set_goto(int i)
 {
-    assert(set_exists(i));
     current_set = &set_v[i];
-    set_load_levels();
-    set_load_hs();
 }
 
 const struct set *curr_set(void)
@@ -381,7 +328,7 @@ const struct set *curr_set(void)
 
 const struct level *get_level(int i)
 {
-    return (i >= 0 && i < current_set->count) ? &level_v[i] : NULL;
+    return (i >= 0 && i < current_set->count) ? &current_set->level_v[i] : NULL;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -461,16 +408,19 @@ static int level_score_update(struct level_game *lg, const char *player)
 {
     int timer = lg->timer;
     int coins = lg->coins;
-    struct level *l = &level_v[lg->level->number];
+    struct level *l = &current_set->level_v[lg->level->number];
 
-    lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
+    lg->time_rank = score_time_insert(&l->score.best_times,
+                                      player, timer, coins);
 
     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
+        lg->goal_rank = score_time_insert(&l->score.unlock_goal,
+                                          player, timer, coins);
     else
         lg->goal_rank = 3;
 
-    lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
+    lg->coin_rank = score_coin_insert(&l->score.most_coins,
+                                      player, timer, coins);
 
     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
 }
@@ -493,19 +443,19 @@ void score_change_name(struct level_game *lg, const char *player)
 {
 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
     struct set *s = current_set;
-    struct level *l = &level_v[lg->level->number];
-    UPDATE(lg->time_rank, l->time_score);
-    UPDATE(lg->goal_rank, l->goal_score);
-    UPDATE(lg->coin_rank, l->coin_score);
+    struct level *l = &s->level_v[lg->level->number];
+    UPDATE(lg->time_rank, l->score.best_times);
+    UPDATE(lg->goal_rank, l->score.unlock_goal);
+    UPDATE(lg->coin_rank, l->score.most_coins);
     UPDATE(lg->score_rank, s->coin_score);
     UPDATE(lg->times_rank, s->time_score);
-    set_store_hs();
+    set_store_hs(s);
 }
 
 static struct level *next_level(int i)
 {
 /* Return the ith level, or NULL */
-    return set_level_exists(current_set, i + 1) ? &level_v[i + 1] : NULL;
+    return set_level_exists(current_set, i + 1) ? &current_set->level_v[i + 1] : NULL;
 }
 
 static struct level *next_normal_level(int i)
@@ -513,8 +463,8 @@ static struct level *next_normal_level(int i)
  * Return NULL if there is not a such level */
 {
     for (i++; i < current_set->count; i++)
-        if (!level_v[i].is_bonus)
-            return &level_v[i];
+        if (!current_set->level_v[i].is_bonus)
+            return &current_set->level_v[i];
     return NULL;
 }
 
@@ -524,7 +474,7 @@ void set_finish_level(struct level_game *lg, const char *player)
 {
     struct set *s = current_set;
     int ln = lg->level->number; /* curent level number */
-    struct level *cl = &level_v[ln];    /* current level */
+    struct level *cl = &s->level_v[ln];    /* current level */
     struct level *nl = NULL;    /* next level */
     int dirty = 0;              /* HS should be saved? */
 
@@ -612,7 +562,7 @@ void set_finish_level(struct level_game *lg, const char *player)
 
     /* Update file */
     if (dirty)
-        set_store_hs();
+        set_store_hs(s);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -620,12 +570,17 @@ void set_finish_level(struct level_game *lg, const char *player)
 void level_snap(int i)
 {
     char filename[MAXSTR];
+    char *ext;
+    struct level *level_v = current_set->level_v;
 
     /* Convert the level name to a PNG filename. */
 
     memset(filename, 0, MAXSTR);
-    strcpy(filename, strstr(level_v[i].file, "map-"));
-    strcpy(strstr(filename, "."), ".png");
+
+    ext = strrchr(level_v[i].file, '.');
+    strncpy(filename, level_v[i].file,
+            ext ? ext - level_v[i].file : strlen(level_v[i].file));
+    strcat(filename, ".png");
 
     /* Initialize the game for a snapshot. */
 
@@ -644,8 +599,7 @@ void level_snap(int i)
         game_draw(1, 0);
         SDL_GL_SwapBuffers();
 
-        image_snap(filename, config_get_d(CONFIG_WIDTH),
-                   config_get_d(CONFIG_HEIGHT));
+        image_snap(filename);
 
         if (shadow)
             config_set_d(CONFIG_SHADOW, 1);
@@ -658,7 +612,7 @@ void set_cheat(void)
     int i;
     current_set->locked = 0;
     for (i = 0; i < current_set->count; i++)
-        level_v[i].is_locked = 0;
+        current_set->level_v[i].is_locked = 0;
 }