Added a tweak to place the GUI into "recently moved" mode upon level end. This will...
[neverball] / ball / set.c
index 73f44ed..40e1427 100644 (file)
 #include "glext.h"
 #include "config.h"
 #include "image.h"
+#include "text.h"
 #include "set.h"
 #include "game.h"
 
 /*---------------------------------------------------------------------------*/
 
-static int count;                    /* number of sets */
+static int set;
+static int count;
 
-static struct set set_v[MAXSET];     /* array of sets */
-
-static struct set *current_set;      /* currently selected set */
+static struct set set_v[MAXSET];
+static struct level level_v[MAXLVL];
 
 /*---------------------------------------------------------------------------*/
 
 static void put_score(FILE *fp, const struct score *s)
 {
     int j;
+
     for (j = 0; j < NSCORE; j++)
        fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
 }
 
-static void set_store_hs(const struct set *s)
-/* Store the score of the set */
+/* Store the score of the set. */
+static void set_store_hs(void)
 {
+    const struct set *s = &set_v[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")))
     {
@@ -69,6 +71,7 @@ static void set_store_hs(const struct set *s)
         for (i = 0; i < s->count; i++)
         {
             l = &level_v[i];
+
             put_score(fout, &l->score.best_times);
             put_score(fout, &l->score.unlock_goal);
             put_score(fout, &l->score.most_coins);
@@ -82,34 +85,32 @@ static int get_score(FILE *fp, struct score *s)
 {
     int j;
     int res = 1;
+
     for (j = 0; j < NSCORE && res; j++)
     {
-       res = (fscanf(fp, "%d %d %s\n",
-                     &s->timer[j], &s->coins[j], s->player[j])) == 3;
+        res = fscanf(fp, "%d %d %s\n",
+                     &s->timer[j],
+                     &s->coins[j],
+                     s->player[j]) == 3;
     }
     return res;
 }
 
-static void set_load_hs(struct set *s)
-/* Get the score of the set */
+/* Get the score of the set. */
+static void set_load_hs(void)
 {
+    struct set *s = &set_v[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")))
     {
-        res = ((fscanf(fin, "%s\n", states) == 1) &&
-               (strlen(states) == s->count));
+        res = fscanf(fin, "%s\n", states) == 1 && strlen(states) == s->count;
+
         for (i = 0; i < s->count && res; i++)
         {
             switch (states[i])
@@ -122,14 +123,11 @@ static void set_load_hs(struct set *s)
             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:
@@ -151,26 +149,22 @@ static void set_load_hs(struct set *s)
 
         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': %s\n"),
-                fn, errno ? strerror(errno) : _("Incorrect format"));
+                L_("Error while loading user high-score file '%s': %s\n"),
+                fn, errno ? strerror(errno) : L_("Incorrect format"));
     }
 }
 
-/* Remove trailing \n if any */
-
-static char *chomp(char *str)
+static char *strip_eol(char *str)
 {
-    char *p = str + strlen(str) - 1;
-    if (p >= str && *p == '\n')
-        *p = 0;
+    char *c = str + strlen(str) - 1;
+
+    while (c >= str && (*c == '\n' || *c =='\r'))
+        *c-- = '\0';
+
     return str;
 }
 
@@ -179,16 +173,12 @@ 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");
 
     if (!fin)
     {
-        fprintf(stderr, _("Cannot load the set file '%s': %s\n"),
+        fprintf(stderr, L_("Cannot load the set file '%s': %s\n"),
                 filename, strerror(errno));
         return 0;
     }
@@ -205,13 +195,13 @@ static int set_load(struct set *s, const char *filename)
     strcpy(s->file, filename);
 
     if ((res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->name, chomp(buf));
+        strcpy(s->name, strip_eol(buf));
     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->desc, chomp(buf));
+        strcpy(s->desc, strip_eol(buf));
     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->setname, chomp(buf));
+        strcpy(s->id, strip_eol(buf));
     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->shot, chomp(buf));
+        strcpy(s->shot, strip_eol(buf));
     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
         sscanf(buf, "%d %d %d %d %d %d",
                 &s->time_score.timer[0],
@@ -222,64 +212,67 @@ static int set_load(struct set *s, const char *filename)
                 &s->coin_score.coins[2]);
 
     strcpy(s->user_scores, "neverballhs-");
-    strcat(s->user_scores, s->setname);
+    strcat(s->user_scores, s->id);
 
-    /* Load levels. */
+    /* Count levels. */
 
-    for (i=0 ; i < MAXLVL && (res = (fscanf(fin, "%s", name) == 1)) ; i++)
-    {
-        l = &s->level_v[i];
+    s->count = 0;
 
-        level_load(config_data(name), l);
+    while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
+        s->count++;
 
-        /* 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 high score file) */
 
-    fclose(fin);
-   
-    /* Load scores and user level state */
-    
-    set_load_hs(s);
+    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;
 
     return 1;
 }
 
 /*---------------------------------------------------------------------------*/
 
-void set_init()
+int set_init()
 {
     FILE *fin;
-    struct set *set;
-    char filename[MAXSTR];
+    char  name[MAXSTR];
 
-    current_set = NULL;
+    set   = 0;
     count = 0;
 
     if ((fin = fopen(config_data(SET_FILE), "r")))
     {
-        while (count < MAXSET && fgets(filename, MAXSTR, fin))
-        {
-            chomp(filename);
-            set = &(set_v[count]);
-
-            if (set_load(set, filename))
-            {
-                set->number = count;
+        while (count < MAXSET && fgets(name, MAXSTR, fin))
+            if (set_load(&set_v[count], strip_eol(name)))
                 count++;
-            }
-        }
+
         fclose(fin);
     }
+
+    return count;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -296,119 +289,105 @@ const struct set *get_set(int i)
 
 /*---------------------------------------------------------------------------*/
 
-int  set_unlocked(const struct set *s)
-/* Are all levels (even extra bonus) unlocked? */
+int set_unlocked(const struct set *s)
 {
     return s->locked == 0;
 }
 
-int  set_completed(const struct set *s)
-/* Are all levels (even extra bonus) completed? */
+int set_completed(const struct set *s)
 {
     return s->completed == s->count;
 }
 
-int  set_level_exists(const struct set *s, int i)
-/* Does the level i of the set exist? */
+int set_level_exists(const struct set *s, int i)
 {
     return (i >= 0) && (i < s->count);
 }
 
 /*---------------------------------------------------------------------------*/
 
-void set_goto(int i)
-{
-    current_set = &set_v[i];
-}
-
-const struct set *curr_set(void)
+static void set_load_levels(void)
 {
-    return current_set;
-}
+    FILE *fin;
+    struct level *l;
 
-const struct level *get_level(int i)
-{
-    return (i >= 0 && i < current_set->count) ? &current_set->level_v[i] : NULL;
-}
+    char buf[MAXSTR];
+    char name[MAXSTR];
 
-/*---------------------------------------------------------------------------*/
+    int i = 0, res;
+    int nb = 1, bnb = 1;
 
-static int score_time_comp(const struct score *S, int i, int j)
-{
-    if (S->timer[i] < S->timer[j])
-        return 1;
+    const char *roman[] = {
+        "",
+        "I",   "II",   "III",   "IV",   "V",
+        "VI",  "VII",  "VIII",  "IX",   "X",
+        "XI",  "XII",  "XIII",  "XIV",  "XV",
+        "XVI", "XVII", "XVIII", "XIX",  "XX",
+        "XXI", "XXII", "XXIII", "XXIV", "XXV"
+    };
 
-    if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
-        return 1;
+    if ((fin = fopen(config_data(set_v[set].file), "r")))
+    {
+        res = 1;
 
-    return 0;
-}
+        /* Skip the five first lines */
+        for (i = 0; i < 5; i++)
+            fgets(buf, MAXSTR, fin);
 
-static int score_coin_comp(const struct score *S, int i, int j)
-{
-    if (S->coins[i] > S->coins[j])
-        return 1;
+        for (i = 0; i < set_v[set].count && res; i++)
+        {
+            l = &level_v[i];
 
-    if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
-        return 1;
+            res = (fscanf(fin, "%s", name) == 1);
+            assert(res);
 
-    return 0;
-}
+            level_load(name, l);
 
-static void score_swap(struct score *S, int i, int j)
-{
-    char player[MAXNAM];
-    int  tmp;
+            /* Initialize set related info */
+            l->set    = &set_v[set];
+            l->number = i;
 
-    strncpy(player,       S->player[i], MAXNAM);
-    strncpy(S->player[i], S->player[j], MAXNAM);
-    strncpy(S->player[j], player,       MAXNAM);
+            if (l->is_bonus)
+                sprintf(l->repr, "%s", roman[bnb++]);
+            else
+                sprintf(l->repr, "%02d", nb++);
 
-    tmp         = S->timer[i];
-    S->timer[i] = S->timer[j];
-    S->timer[j] = tmp;
+            l->is_locked    = 1;
+            l->is_completed = 0;
+        }
+        level_v[0].is_locked = 0; /* unlock the first level */
+        fclose(fin);
+    }
 
-    tmp         = S->coins[i];
-    S->coins[i] = S->coins[j];
-    S->coins[j] = tmp;
+    assert(i == set_v[set].count);
 }
 
-static int score_time_insert(struct score *s, const char *player, int timer,
-                             int coins)
+void set_goto(int i)
 {
-    int i;
-
-    strncpy(s->player[3], player, MAXNAM);
-    s->timer[3] = timer;
-    s->coins[3] = coins;
+    set = i;
 
-    for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
-        score_swap(s, i + 1, i);
-
-    return i + 1;
+    set_load_levels();
+    set_load_hs();
 }
 
-static int score_coin_insert(struct score *s, const char *player, int timer,
-                             int coins)
+const struct set *curr_set(void)
 {
-    int i;
-
-    strncpy(s->player[3], player, MAXNAM);
-    s->timer[3] = timer;
-    s->coins[3] = coins;
-
-    for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
-        score_swap(s, i + 1, i);
+    return &set_v[set];
+}
 
-    return i + 1;
+const struct level *get_level(int i)
+{
+    return (i >= 0 && i < set_v[set].count) ? &level_v[i] : NULL;
 }
 
+/*---------------------------------------------------------------------------*/
+
+/* Update the level score rank according to coins and timer. */
 static int level_score_update(struct level_game *lg, const char *player)
-/* Update the level score rank according to coins and timer */
 {
     int timer = lg->timer;
     int coins = lg->coins;
-    struct level *l = &current_set->level_v[lg->level->number];
+    struct level *l = &level_v[lg->level->number];
 
     lg->time_rank = score_time_insert(&l->score.best_times,
                                       player, timer, coins);
@@ -425,58 +404,58 @@ static int level_score_update(struct level_game *lg, const char *player)
     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
 }
 
+/* Update the set score rank according to score and times. */
 static int set_score_update(struct level_game *lg, const char *player)
-/* Update the set score rank according to score and times */
 {
     int timer = lg->times;
     int coins = lg->score;
-    struct set *s = current_set;
+    struct set *s = &set_v[set];
 
     lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
     lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
+
     return (lg->score_rank < 3 || lg->times_rank < 3);
 }
 
-
+/* Update the player name for set and level high-score. */
 void score_change_name(struct level_game *lg, const char *player)
-/* Update the player name for set and level high-score */
 {
-#define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
-    struct set *s = current_set;
-    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(s);
+    struct set   *s = &set_v[set];
+    struct level *l = &level_v[lg->level->number];
+
+    strncpy(l->score.best_times.player [lg->time_rank], player, MAXNAM);
+    strncpy(l->score.unlock_goal.player[lg->goal_rank], player, MAXNAM);
+    strncpy(l->score.most_coins.player [lg->coin_rank], player, MAXNAM);
+
+    strncpy(s->coin_score.player[lg->score_rank], player, MAXNAM);
+    strncpy(s->time_score.player[lg->times_rank], player, MAXNAM);
+
+    set_store_hs();
 }
 
 static struct level *next_level(int i)
 {
-/* Return the ith level, or NULL */
-    return set_level_exists(current_set, i + 1) ? &current_set->level_v[i + 1] : NULL;
+    return set_level_exists(&set_v[set], i + 1) ? &level_v[i + 1] : NULL;
 }
 
 static struct level *next_normal_level(int i)
-/* Return the next normal level (starting for i)
- * Return NULL if there is not a such level */
 {
-    for (i++; i < current_set->count; i++)
-        if (!current_set->level_v[i].is_bonus)
-            return &current_set->level_v[i];
+    for (i++; i < set_v[set].count; i++)
+        if (!level_v[i].is_bonus)
+            return &level_v[i];
+
     return NULL;
 }
 
+/*---------------------------------------------------------------------------*/
+
 void set_finish_level(struct level_game *lg, const char *player)
-/* Inform the set that a level is finished.
- * Update next_level and score rank fields */
 {
-    struct set *s = current_set;
-    int ln = lg->level->number; /* curent level number */
-    struct level *cl = &s->level_v[ln];    /* current level */
-    struct level *nl = NULL;    /* next level */
-    int dirty = 0;              /* HS should be saved? */
+    struct set *s = &set_v[set];
+    int ln = lg->level->number;      /* Current level number       */
+    struct level *cl = &level_v[ln]; /* Current level              */
+    struct level *nl = NULL;         /* Next level                 */
+    int dirty = 0;                   /* Should the score be saved? */
 
     assert(s == cl->set);
 
@@ -489,7 +468,7 @@ void set_finish_level(struct level_game *lg, const char *player)
     }
 
     /* On level completed */
-    if (lg->state == GAME_GOAL)
+    if (lg->status == GAME_GOAL)
     {
         /* Update level scores */
         dirty = level_score_update(lg, player);
@@ -508,10 +487,10 @@ void set_finish_level(struct level_game *lg, const char *player)
     }
 
     /* On goal reached */
-    if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
+    if (lg->status == GAME_GOAL)
     {
         /* Identify the following level */
-        nl = next_level(ln + lg->state_value);
+        nl = next_level(ln);
         if (nl != NULL)
         {
             /* skip bonuses if unlocked in non challenge mode */
@@ -562,7 +541,7 @@ void set_finish_level(struct level_game *lg, const char *player)
 
     /* Update file */
     if (dirty)
-        set_store_hs(s);
+        set_store_hs();
 }
 
 /*---------------------------------------------------------------------------*/
@@ -571,7 +550,6 @@ 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. */
 
@@ -610,9 +588,11 @@ void set_cheat(void)
 /* Open each level of the current set */
 {
     int i;
-    current_set->locked = 0;
-    for (i = 0; i < current_set->count; i++)
-        current_set->level_v[i].is_locked = 0;
+
+    set_v[set].locked = 0;
+
+    for (i = 0; i < set_v[set].count; i++)
+        level_v[i].is_locked = 0;
 }