Minor code clean-up.
[neverball] / ball / set.c
index a7eaf89..9e0cb6f 100644 (file)
@@ -163,34 +163,31 @@ static char *chomp(char *str)
 }
 
 static int set_load(struct set *s, const char *filename)
-/* Count levels */
 {
     FILE *fin;
-    char buf[MAXSTR];
-    int res = 0;
 
-    /* Open the datafile */
+    char buf[MAXSTR];
+    int res;
 
-    fin = fopen(filename, "r");
-    if (fin == NULL)
+    fin = fopen(config_data(filename), "r");
+    if (!fin)
     {
         fprintf(stderr, _("Cannot load the set file '%s':"), filename);
         perror(NULL);
         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, config_data(filename));
 
-    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,23 +204,24 @@ 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. */
+    /* Count levels. */
 
     s->count = 0;
 
     while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
         s->count++;
 
-    /* Close the file, since it's no more needed */
-
     fclose(fin);
 
     /* 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];
@@ -253,8 +251,9 @@ static int set_load(struct set *s, const char *filename)
 
 void set_init()
 {
-    FILE *fin;
     struct set *set;
+    FILE *fin;
+
     char filename[MAXSTR];
     int res;
 
@@ -268,15 +267,13 @@ void set_init()
         while (count < MAXSET && res)
         {
             set = &(set_v[count]);
-
-            /* clean the set data */
-
             res = (fgets(filename, MAXSTR, fin) != NULL);
+
             if (res)
             {
                 chomp(filename);
 
-                res = set_load(set, config_data(filename));
+                res = set_load(set, filename);
                 if (res)
                 {
                     set->number = count;
@@ -324,51 +321,52 @@ 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;
+    struct level *l;
+
     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);
+    if ((fin = fopen(current_set->file, "r")))
+    {
+        res = 1;
 
-    res = 1;
+        /* Skip the five first lines */
+        for (i = 0; i < 5; i++)
+            fgets(buf, MAXSTR, fin);
 
-    /* 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];
 
-    for(i = 0; i < current_set->count && res; i++)
-    {
-        l = &level_v[i];
-        res = (fscanf(fin, "%s", name) == 1);
-        assert(res);
+            res = (fscanf(fin, "%s", name) == 1);
+            assert(res);
 
-        level_load(config_data(name), l);
+            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;
+            /* 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);
     }
-    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();
@@ -620,17 +618,26 @@ void set_finish_level(struct level_game *lg, const char *player)
 void level_snap(int i)
 {
     char filename[MAXSTR];
+    char *ext;
 
     /* 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. */
 
     if (game_init(&level_v[i], 0, 0))
     {
+        int shadow;
+
+        if ((shadow = config_get_d(CONFIG_SHADOW)))
+            config_set_d(CONFIG_SHADOW, 0);
+
         /* Render the level and grab the screen. */
 
         config_clear();
@@ -641,6 +648,9 @@ void level_snap(int i)
 
         image_snap(filename, config_get_d(CONFIG_WIDTH),
                    config_get_d(CONFIG_HEIGHT));
+
+        if (shadow)
+            config_set_d(CONFIG_SHADOW, 1);
     }
 }