global level and set loading at the begin of the game
authormrtout <mrtout@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Wed, 2 Aug 2006 14:25:26 +0000 (14:25 +0000)
committermrtout <mrtout@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Wed, 2 Aug 2006 14:25:26 +0000 (14:25 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@530 78b8d119-cf0a-0410-b17c-f493084dd1d7

ball/main.c
ball/set.c
ball/set.h
ball/st_set.c

index 0077026..35fa3cf 100644 (file)
@@ -416,6 +416,10 @@ int main(int argc, char *argv[])
 
     SDL_WM_SetCaption(TITLE, TITLE);
 
+    /* Load levels and sets. */
+
+    set_init();
+    
     /* Initialize the run state. */
 
     init_state(&st_null);
index 87df3b9..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")))
     {
@@ -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")))
     {
@@ -119,11 +122,14 @@ static void set_load_hs(void)
             case 'C':
                 level_v[i].is_locked = 0;
                 level_v[i].is_completed = 1;
+                s->completed += 1;
+                s->locked -= 1;
                 break;
 
             case 'O':
                 level_v[i].is_locked = 0;
                 level_v[i].is_completed = 0;
+                s->locked -= 1;
                 break;
 
             default:
@@ -145,6 +151,10 @@ static void set_load_hs(void)
 
         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)
     {
@@ -169,6 +179,10 @@ static int set_load(struct set *s, const char *filename)
     FILE *fin;
     char buf[MAXSTR];
     int res;
+    struct level *l;
+    char name[MAXSTR];
+    int i = 0;
+    int nb = 1, bnb = 1;
 
     fin = fopen(config_data(filename), "r");
 
@@ -210,41 +224,32 @@ static int set_load(struct set *s, const char *filename)
     strcpy(s->user_scores, "neverballhs-");
     strcat(s->user_scores, s->setname);
 
-    /* Count 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);
 
-    fclose(fin);
+        /* 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;
+    }
 
-    /* Load the levels states (stored in the user highscore file) */
+    s->count = i;
 
-    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;
 }
@@ -311,56 +316,9 @@ int  set_level_exists(const struct set *s, int i)
 
 /*---------------------------------------------------------------------------*/
 
-static void set_load_levels(void)
-{
-    FILE *fin;
-    struct level *l;
-
-    char buf[MAXSTR];
-    char name[MAXSTR];
-
-    int i = 0, res;
-    int nb = 1, bnb = 1;
-
-    if ((fin = fopen(config_data(current_set->file), "r")))
-    {
-        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->repr, _("B%d"), bnb++);
-            else
-                sprintf(l->repr, "%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)
 {
     current_set = &set_v[i];
-    set_load_levels();
-    set_load_hs();
 }
 
 const struct set *curr_set(void)
@@ -370,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;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -450,7 +408,7 @@ 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->score.best_times,
                                       player, timer, coins);
@@ -485,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];
+    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)
@@ -505,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;
 }
 
@@ -516,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? */
 
@@ -604,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);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -613,6 +571,7 @@ 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. */
 
@@ -653,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;
 }
 
 
index 88a24d6..f636a4c 100644 (file)
@@ -33,6 +33,10 @@ struct set
     int count;                /* number of levels */
     int locked;               /* number of locked levels */
     int completed;            /* number of completed levels */
+
+    /* levels */
+    
+    struct level level_v[MAXLVL];
 };
 
 /*---------------------------------------------------------------------------*/
index f4880bf..6be173b 100644 (file)
@@ -94,8 +94,6 @@ static int set_enter(void)
 
     int id, jd, kd;
 
-    set_init();
-
     /* Reset last set if it does not exist */
     if (!set_exists(last_set))
     {