Proper ball shadow removal in poser mode. Patch from Elviz.
[neverball] / ball / set.c
index 7e725a5..866e797 100644 (file)
 #include "glext.h"
 #include "config.h"
 #include "image.h"
+#include "text.h"
 #include "set.h"
 #include "game.h"
 
 /*---------------------------------------------------------------------------*/
 
+static int set;
+static int count;
 
-static struct set   set_v[MAXSET];
-static int          set_count;
-static struct set  *current_set;
+static struct set set_v[MAXSET];
 static struct level level_v[MAXLVL];
 
 /*---------------------------------------------------------------------------*/
@@ -44,7 +45,7 @@ static void put_score(FILE *fp, const struct score *s)
 /* Store the score of the set. */
 static void set_store_hs(void)
 {
-    const struct set *s = current_set;
+    const struct set *s = &set_v[set];
     FILE *fout;
     int i;
     const struct level *l;
@@ -98,7 +99,7 @@ static int get_score(FILE *fp, struct score *s)
 /* Get the score of the set. */
 static void set_load_hs(void)
 {
-    struct set *s = current_set;
+    struct set *s = &set_v[set];
     FILE *fin;
     int i;
     int res = 0;
@@ -152,8 +153,8 @@ static void set_load_hs(void)
     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"));
     }
 }
 
@@ -177,7 +178,7 @@ static int set_load(struct set *s, const char *filename)
 
     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;
     }
@@ -259,26 +260,26 @@ int set_init()
     FILE *fin;
     char  name[MAXSTR];
 
-    current_set = NULL;
-    set_count   = 0;
+    set   = 0;
+    count = 0;
 
     if ((fin = fopen(config_data(SET_FILE), "r")))
     {
-        while (set_count < MAXSET && fgets(name, MAXSTR, fin))
-            if (set_load(&set_v[set_count], strip_eol(name)))
-                set_count++;
+        while (count < MAXSET && fgets(name, MAXSTR, fin))
+            if (set_load(&set_v[count], strip_eol(name)))
+                count++;
 
         fclose(fin);
     }
 
-    return set_count;
+    return count;
 }
 
 /*---------------------------------------------------------------------------*/
 
 int  set_exists(int i)
 {
-    return (0 <= i && i < set_count);
+    return (0 <= i && i < count);
 }
 
 const struct set *get_set(int i)
@@ -288,20 +289,17 @@ 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);
 }
@@ -320,15 +318,15 @@ static void set_load_levels(void)
     int nb = 1, bnb = 1;
 
     const char *roman[] = {
-        NULL,
-        "I", "II", "III", "IV", "V",
-        "VI", "VII", "VIII", "IX", "X",
-        "XI", "XII", "XIII", "XIV", "XV",
-        "XVI", "XVII", "XVIII", "XIX", "XX",
+        "",
+        "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 ((fin = fopen(config_data(current_set->file), "r")))
+    if ((fin = fopen(config_data(set_v[set].file), "r")))
     {
         res = 1;
 
@@ -336,7 +334,7 @@ static void set_load_levels(void)
         for (i = 0; i < 5; i++)
             fgets(buf, MAXSTR, fin);
 
-        for (i = 0; i < current_set->count && res; i++)
+        for (i = 0; i < set_v[set].count && res; i++)
         {
             l = &level_v[i];
 
@@ -346,8 +344,8 @@ static void set_load_levels(void)
             level_load(name, l);
 
             /* Initialize set related info */
-            l->set        = current_set;
-            l->number     = i;
+            l->set    = &set_v[set];
+            l->number = i;
 
             if (l->is_bonus)
                 sprintf(l->repr, "%s", roman[bnb++]);
@@ -361,30 +359,31 @@ static void set_load_levels(void)
         fclose(fin);
     }
 
-    assert(i == current_set->count);
+    assert(i == set_v[set].count);
 }
 
 void set_goto(int i)
 {
-    current_set = &set_v[i];
+    set = i;
+
     set_load_levels();
     set_load_hs();
 }
 
 const struct set *curr_set(void)
 {
-    return current_set;
+    return &set_v[set];
 }
 
 const struct level *get_level(int i)
 {
-    return (i >= 0 && i < current_set->count) ? &level_v[i] : NULL;
+    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;
@@ -405,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 set   *s = &set_v[set];
     struct level *l = &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);
+
+    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) ? &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++)
+    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; /* current level number */
-    struct level *cl = &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);
 
@@ -469,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);
@@ -488,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 */
@@ -565,11 +564,6 @@ void level_snap(int i)
 
     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();
@@ -579,9 +573,6 @@ void level_snap(int i)
         SDL_GL_SwapBuffers();
 
         image_snap(filename);
-
-        if (shadow)
-            config_set_d(CONFIG_SHADOW, 1);
     }
 }
 
@@ -589,8 +580,10 @@ 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++)
+
+    set_v[set].locked = 0;
+
+    for (i = 0; i < set_v[set].count; i++)
         level_v[i].is_locked = 0;
 }