Merge branch 'gles'
[neverball] / ball / set.c
index c0836a2..7d0b813 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
-#include <errno.h>
 
 #include "glext.h"
 #include "config.h"
+#include "video.h"
 #include "image.h"
-#include "text.h"
 #include "set.h"
-#include "game.h"
+#include "common.h"
+#include "fs.h"
+
+#include "game_server.h"
+#include "game_client.h"
+#include "game_proxy.h"
 
 /*---------------------------------------------------------------------------*/
 
-static int set;
-static int count;
+struct set
+{
+    char file[PATHMAX];
+
+    char *id;                           /* Internal set identifier           */
+    char *name;                         /* Set name                          */
+    char *desc;                         /* Set description                   */
+    char *shot;                         /* Set screen-shot                   */
+
+    char *user_scores;                  /* User high-score file              */
+    char *cheat_scores;                 /* Cheat mode score file             */
+
+    struct score coin_score;            /* Challenge score                   */
+    struct score time_score;            /* Challenge score                   */
+
+    /* Level info                                                            */
+
+    int   count;                        /* Number of levels                  */
+    char *level_name_v[MAXLVL];         /* List of level file names          */
+};
+
+#define SET_GET(a, i) ((struct set *) array_get((a), (i)))
+
+static Array sets;
+static int   curr;
 
-static struct set set_v[MAXSET];
 static struct level level_v[MAXLVL];
 
 /*---------------------------------------------------------------------------*/
 
-static void put_score(FILE *fp, const struct score *s)
+static void put_score(fs_file fp, const struct score *s)
 {
-    int j;
+    int i;
 
-    for (j = 0; j < NSCORE; j++)
-       fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
+    for (i = RANK_HARD; i <= RANK_EASY; i++)
+        fs_printf(fp, "%d %d %s\n", s->timer[i], s->coins[i], s->player[i]);
 }
 
-/* Store the score of the set. */
-static void set_store_hs(void)
+static int get_score(fs_file fp, struct score *s)
 {
-    const struct set *s = &set_v[set];
-    FILE *fout;
+    char line[MAXSTR];
     int i;
-    const struct level *l;
-    char states[MAXLVL + 1];
 
-    if ((fout = fopen(config_user(s->user_scores), "w")))
+    for (i = RANK_HARD; i <= RANK_EASY; i++)
+    {
+        int n = -1;
+
+        if (!fs_gets(line, sizeof (line), fp))
+            return 0;
+
+        strip_newline(line);
+
+        if (sscanf(line, "%d %d %n", &s->timer[i], &s->coins[i], &n) < 2)
+            return 0;
+
+        if (n < 0)
+            return 0;
+
+        SAFECPY(s->player[i], line + n);
+    }
+
+    return 1;
+}
+
+void set_store_hs(void)
+{
+    const struct set *s = SET_GET(sets, curr);
+    fs_file fp;
+
+    if ((fp = fs_open(config_cheat() ? s->cheat_scores : s->user_scores, "w")))
     {
+        const struct level *l;
+
+        char states[MAXLVL + 2] = "";
+        int i;
+
         for (i = 0; i < s->count; i++)
         {
-            if (level_v[i].is_locked)
+            l = &level_v[i];
+
+            if (l->is_locked)
                 states[i] = 'L';
-            else if (level_v[i].is_completed)
+            else if (l->is_completed)
                 states[i] = 'C';
             else
                 states[i] = 'O';
         }
-        states[s->count] = '\0';
-        fprintf(fout, "%s\n",states);
 
-        put_score(fout, &s->time_score);
-        put_score(fout, &s->coin_score);
+        states[s->count] = '\n';
+
+        fs_puts(states, fp);
+
+        put_score(fp, &s->time_score);
+        put_score(fp, &s->coin_score);
 
         for (i = 0; i < s->count; i++)
         {
-            l = &level_v[i];
+            const struct level *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);
+            put_score(fp, &l->scores[SCORE_TIME]);
+            put_score(fp, &l->scores[SCORE_GOAL]);
+            put_score(fp, &l->scores[SCORE_COIN]);
         }
 
-        fclose(fout);
+        fs_close(fp);
     }
 }
 
-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;
-    }
-    return res;
-}
-
-/* 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 set *s = SET_GET(sets, curr);
+    fs_file fp;
 
-    if ((fin = fopen(fn, "r")))
+    if ((fp = fs_open(config_cheat() ? s->cheat_scores : s->user_scores, "r")))
     {
-        res = fscanf(fin, "%s\n", states) == 1 && strlen(states) == s->count;
+        char states[MAXLVL + 2];
 
-        for (i = 0; i < s->count && res; i++)
+        if (fs_gets(states, sizeof (states), fp))
         {
-            switch (states[i])
+            struct level *l;
+            int i, n = MIN(strlen(states) - 1, s->count);
+
+            for (i = 0; i < n; i++)
             {
-            case 'L':
-                level_v[i].is_locked = 1;
-                level_v[i].is_completed = 0;
-                break;
-
-            case 'C':
-                level_v[i].is_locked = 0;
-                level_v[i].is_completed = 1;
-                break;
-
-            case 'O':
-                level_v[i].is_locked = 0;
-                level_v[i].is_completed = 0;
-                break;
-
-            default:
-                res = 0;
+                l = &level_v[i];
+
+                l->is_locked    = (states[i] == 'L');
+                l->is_completed = (states[i] == 'C');
             }
-        }
 
-        res = res &&
-            get_score(fin, &s->time_score) &&
-            get_score(fin, &s->coin_score);
+            get_score(fp, &s->time_score);
+            get_score(fp, &s->coin_score);
 
-        for (i = 0; i < s->count && res; i++)
-        {
-            l = &level_v[i];
-            res = get_score(fin, &l->score.best_times) &&
-                  get_score(fin, &l->score.unlock_goal) &&
-                  get_score(fin, &l->score.most_coins);
-        }
+            for (i = 0; i < n; i++)
+            {
+                l = &level_v[i];
 
-        fclose(fin);
-    }
+                get_score(fp, &l->scores[SCORE_TIME]);
+                get_score(fp, &l->scores[SCORE_GOAL]);
+                get_score(fp, &l->scores[SCORE_COIN]);
+            }
+        }
 
-    if (!res && errno != ENOENT)
-    {
-        fprintf(stderr,
-                L_("Error while loading user high-score file '%s': %s\n"),
-                fn, errno ? strerror(errno) : L_("Incorrect format"));
+        fs_close(fp);
     }
 }
 
-static char *strip_eol(char *str)
-{
-    char *c = str + strlen(str) - 1;
-
-    while (c >= str && (*c == '\n' || *c =='\r'))
-        *c-- = '\0';
-
-    return str;
-}
+/*---------------------------------------------------------------------------*/
 
 static int set_load(struct set *s, const char *filename)
 {
-    FILE *fin;
-    char buf[MAXSTR];
-    int res;
+    fs_file fin;
+    char *scores, *level_name;
+
+    /* Skip "Misc" set when not in dev mode. */
 
-    fin = fopen(config_data(filename), "r");
+    if (strcmp(filename, SET_MISC) == 0 && !config_cheat())
+        return 0;
+
+    fin = fs_open(filename, "r");
 
     if (!fin)
     {
-        fprintf(stderr, L_("Cannot load the set file '%s': %s\n"),
-                filename, strerror(errno));
+        fprintf(stderr, L_("Failure to load set file '%s'\n"), filename);
         return 0;
     }
 
     memset(s, 0, sizeof (struct set));
 
-    /* Set some sane values in case the scores hs is missing. */
+    /* Set some sane values in case the scores are missing. */
 
     score_init_hs(&s->time_score, 359999, 0);
     score_init_hs(&s->coin_score, 359999, 0);
 
-    /* Load set metadata. */
-
-    strcpy(s->file, filename);
+    SAFECPY(s->file, filename);
 
-    if ((res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->name, strip_eol(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->desc, strip_eol(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->id, strip_eol(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        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],
-                &s->time_score.timer[1],
-                &s->time_score.timer[2],
-                &s->coin_score.coins[0],
-                &s->coin_score.coins[1],
-                &s->coin_score.coins[2]);
+    if (read_line(&s->name, fin) &&
+        read_line(&s->desc, fin) &&
+        read_line(&s->id,   fin) &&
+        read_line(&s->shot, fin) &&
+        read_line(&scores,  fin))
+    {
+        sscanf(scores, "%d %d %d %d %d %d",
+               &s->time_score.timer[RANK_HARD],
+               &s->time_score.timer[RANK_MEDM],
+               &s->time_score.timer[RANK_EASY],
+               &s->coin_score.coins[RANK_HARD],
+               &s->coin_score.coins[RANK_MEDM],
+               &s->coin_score.coins[RANK_EASY]);
 
-    strcpy(s->user_scores, "neverballhs-");
-    strcat(s->user_scores, s->id);
+        free(scores);
 
-    /* Count levels. */
+        s->user_scores  = concat_string("Scores/", s->id, ".txt",       NULL);
+        s->cheat_scores = concat_string("Scores/", s->id, "-cheat.txt", NULL);
 
-    s->count = 0;
+        s->count = 0;
 
-    while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
-        s->count++;
+        while (s->count < MAXLVL && read_line(&level_name, fin))
+        {
+            s->level_name_v[s->count] = level_name;
+            s->count++;
+        }
 
-    fclose(fin);
+        fs_close(fin);
 
-    /* Load the levels states (stored in the user high score file) */
+        return 1;
+    }
 
-    s->locked = s->count;
-    s->completed = 0;
+    free(s->name);
+    free(s->desc);
+    free(s->id);
+    free(s->shot);
 
-    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;
+    fs_close(fin);
 
-    return 1;
+    return 0;
 }
 
-/*---------------------------------------------------------------------------*/
-
-int set_init()
+static void set_free(struct set *s)
 {
-    FILE *fin;
-    char  name[MAXSTR];
-
-    set   = 0;
-    count = 0;
+    int i;
 
-    if ((fin = fopen(config_data(SET_FILE), "r")))
-    {
-        while (count < MAXSET && fgets(name, MAXSTR, fin))
-            if (set_load(&set_v[count], strip_eol(name)))
-                count++;
+    free(s->name);
+    free(s->desc);
+    free(s->id);
+    free(s->shot);
 
-        fclose(fin);
-    }
+    free(s->user_scores);
+    free(s->cheat_scores);
 
-    return count;
+    for (i = 0; i < s->count; i++)
+        free(s->level_name_v[i]);
 }
 
 /*---------------------------------------------------------------------------*/
 
-int  set_exists(int i)
+static int cmp_dir_items(const void *A, const void *B)
 {
-    return (0 <= i && i < count);
+    const struct dir_item *a = A, *b = B;
+    return strcmp(a->path, b->path);
 }
 
-const struct set *get_set(int i)
+static int set_is_loaded(const char *path)
 {
-    return set_exists(i) ? &set_v[i] : NULL;
-}
+    int i;
 
-/*---------------------------------------------------------------------------*/
+    for (i = 0; i < array_len(sets); i++)
+        if (strcmp(SET_GET(sets, i)->file, path) == 0)
+            return 1;
 
-int set_unlocked(const struct set *s)
-{
-    return s->locked == 0;
+    return 0;
 }
 
-int set_completed(const struct set *s)
+static int is_unseen_set(struct dir_item *item)
 {
-    return s->completed == s->count;
+    return (str_starts_with(base_name(item->path), "set-") &&
+            str_ends_with(item->path, ".txt") &&
+            !set_is_loaded(item->path));
 }
 
-int set_level_exists(const struct set *s, int i)
+int set_init()
 {
-    return (i >= 0) && (i < s->count);
-}
-
-/*---------------------------------------------------------------------------*/
+    fs_file fin;
+    char *name;
 
-static void set_load_levels(void)
-{
-    FILE *fin;
-    struct level *l;
+    Array items;
+    int i;
 
-    char buf[MAXSTR];
-    char name[MAXSTR];
+    if (sets)
+        set_quit();
 
-    int i = 0, res;
-    int nb = 1, bnb = 1;
+    sets = array_new(sizeof (struct set));
+    curr = 0;
 
-    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"
-    };
+    /*
+     * First, load the sets listed in the set file, preserving order.
+     */
 
-    if ((fin = fopen(config_data(set_v[set].file), "r")))
+    if ((fin = fs_open(SET_FILE, "r")))
     {
-        res = 1;
-
-        /* Skip the five first lines */
-        for (i = 0; i < 5; i++)
-            fgets(buf, MAXSTR, fin);
-
-        for (i = 0; i < set_v[set].count && res; i++)
+        while (read_line(&name, fin))
         {
-            l = &level_v[i];
+            struct set *s = array_add(sets);
 
-            res = (fscanf(fin, "%s", name) == 1);
-            assert(res);
+            if (!set_load(s, name))
+                array_del(sets);
 
-            level_load(name, l);
+            free(name);
+        }
+        fs_close(fin);
+    }
 
-            /* Initialize set related info */
-            l->set    = &set_v[set];
-            l->number = i;
+    /*
+     * Then, scan for any remaining set description files, and add
+     * them after the first group in alphabetic order.
+     */
 
-            if (l->is_bonus)
-                sprintf(l->repr, "%s", roman[bnb++]);
-            else
-                sprintf(l->repr, "%02d", nb++);
+    if ((items = fs_dir_scan("", is_unseen_set)))
+    {
+        array_sort(items, cmp_dir_items);
 
-            l->is_locked    = 1;
-            l->is_completed = 0;
+        for (i = 0; i < array_len(items); i++)
+        {
+            struct set *s = array_add(sets);
+
+            if (!set_load(s, DIR_ITEM_GET(items, i)->path))
+                array_del(sets);
         }
-        level_v[0].is_locked = 0; /* unlock the first level */
-        fclose(fin);
+
+        fs_dir_free(items);
     }
 
-    assert(i == set_v[set].count);
+    return array_len(sets);
 }
 
-void set_goto(int i)
+void set_quit(void)
 {
-    set = i;
+    int i;
 
-    set_load_levels();
-    set_load_hs();
+    for (i = 0; i < array_len(sets); i++)
+        set_free(array_get(sets, i));
+
+    array_free(sets);
+    sets = NULL;
 }
 
-const struct set *curr_set(void)
+/*---------------------------------------------------------------------------*/
+
+int set_exists(int i)
 {
-    return &set_v[set];
+    return sets ? 0 <= i && i < array_len(sets) : 0;
 }
 
-const struct level *get_level(int i)
+const char *set_id(int i)
 {
-    return (i >= 0 && i < set_v[set].count) ? &level_v[i] : NULL;
+    return set_exists(i) ? SET_GET(sets, i)->id : NULL;
 }
 
-/*---------------------------------------------------------------------------*/
-
-/* Update the level score rank according to coins and timer. */
-static int level_score_update(struct level_game *lg, const char *player)
+const char *set_name(int i)
 {
-    int timer = lg->timer;
-    int coins = lg->coins;
-    struct level *l = &level_v[lg->level->number];
-
-    lg->time_rank = score_time_insert(&l->score.best_times,
-                                      player, timer, coins);
+    return set_exists(i) ? _(SET_GET(sets, i)->name) : NULL;
+}
 
-    if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        lg->goal_rank = score_time_insert(&l->score.unlock_goal,
-                                          player, timer, coins);
-    else
-        lg->goal_rank = 3;
+const char *set_desc(int i)
+{
+    return set_exists(i) ? _(SET_GET(sets, i)->desc) : NULL;
+}
 
-    lg->coin_rank = score_coin_insert(&l->score.most_coins,
-                                      player, timer, coins);
+const char *set_shot(int i)
+{
+    return set_exists(i) ? SET_GET(sets, i)->shot : NULL;
+}
 
-    return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
+const struct score *set_score(int i, int s)
+{
+    if (set_exists(i))
+    {
+        if (s == SCORE_TIME) return &SET_GET(sets, i)->time_score;
+        if (s == SCORE_COIN) return &SET_GET(sets, i)->coin_score;
+    }
+    return NULL;
 }
 
-/* Update the set score rank according to score and times. */
-static int set_score_update(struct level_game *lg, const char *player)
+/*---------------------------------------------------------------------------*/
+
+static void set_load_levels(void)
 {
-    int timer = lg->times;
-    int coins = lg->score;
-    struct set *s = &set_v[set];
+    static 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"
+    };
 
-    lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
-    lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
+    struct set *s = SET_GET(sets, curr);
+    int regular = 1, bonus = 1;
+    int i;
 
-    return (lg->score_rank < 3 || lg->times_rank < 3);
-}
+    for (i = 0; i < s->count; i++)
+    {
+        struct level *l = &level_v[i];
 
-/* Update the player name for set and level high-score. */
-void score_change_name(struct level_game *lg, const char *player)
-{
-    struct set   *s = &set_v[set];
-    struct level *l = &level_v[lg->level->number];
+        level_load(s->level_name_v[i], l);
+
+        l->number = i;
 
-    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);
+        if (l->is_bonus)
+            SAFECPY(l->name, roman[bonus++]);
+        else
+            sprintf(l->name, "%02d", regular++);
 
-    strncpy(s->coin_score.player[lg->score_rank], player, MAXNAM);
-    strncpy(s->time_score.player[lg->times_rank], player, MAXNAM);
+        l->is_locked = (i > 0);
+        l->is_completed = 0;
 
-    set_store_hs();
+        if (i > 0)
+            level_v[i - 1].next = l;
+    }
 }
 
-static struct level *next_level(int i)
+void set_goto(int i)
 {
-    return set_level_exists(&set_v[set], i + 1) ? &level_v[i + 1] : NULL;
+    curr = i;
+
+    set_load_levels();
+    set_load_hs();
 }
 
-static struct level *next_normal_level(int i)
+int curr_set(void)
 {
-    for (i++; i < set_v[set].count; i++)
-        if (!level_v[i].is_bonus)
-            return &level_v[i];
+    return curr;
+}
 
-    return NULL;
+struct level *get_level(int i)
+{
+    return (i >= 0 && i < SET_GET(sets, curr)->count) ? &level_v[i] : NULL;
 }
 
 /*---------------------------------------------------------------------------*/
 
-void set_finish_level(struct level_game *lg, const char *player)
+int set_score_update(int timer, int coins, int *score_rank, int *times_rank)
 {
-    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);
-
-    /* if no set, no next level */
-    if (s == NULL)
-    {
-        /* if no set, return */
-        lg->next_level = NULL;
-        return;
-    }
-
-    /* On level completed */
-    if (lg->status == GAME_GOAL)
-    {
-        /* Update level scores */
-        dirty = level_score_update(lg, player);
-
-        /* Complete the level */
-        if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        {
-            /* Complete the level */
-            if (!cl->is_completed)
-            {
-                cl->is_completed = 1;
-                s->completed += 1;
-                dirty = 1;
-            }
-        }
-    }
-
-    /* On goal reached */
-    if (lg->status == GAME_GOAL)
-    {
-        /* Identify the following level */
-        nl = next_level(ln);
-        if (nl != NULL)
-        {
-            /* skip bonuses if unlocked in any mode */
-            if (nl->is_bonus)
-            {
-                if(lg->mode == MODE_CHALLENGE && nl->is_locked > 0)
-                {
-                    lg->bonus = 1; /* Show GUI message */
-                    nl->is_locked = 0; /* Unlock bonus level */
-                }
-                nl = next_normal_level(nl->number);
-            }
-        }
-        else if (lg->mode == MODE_CHALLENGE)
-            lg->win = 1;
-    }
-    else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
-    {
-        /* On fail, identify the next level (only in bonus for challenge) */
-        nl = next_normal_level(ln);
-        /* Next level may be unavailable */
-        if (!cl->is_bonus && nl != NULL && nl->is_locked)
-            nl = NULL;
-        /* Fail a bonus level but win the set! */
-        else if (nl == NULL && lg->mode == MODE_CHALLENGE)
-            lg->win = 1;
-    }
+    struct set *s = SET_GET(sets, curr);
+    const char *player = config_get_s(CONFIG_PLAYER);
 
-    /* Win ! */
-    if (lg->win)
-    {
-        /* update set score */
-        set_score_update(lg, player);
-        /* unlock all levels */
-        set_cheat();
-        dirty = 1;
-    }
+    score_coin_insert(&s->coin_score, score_rank, player, timer, coins);
+    score_time_insert(&s->time_score, times_rank, player, timer, coins);
 
-    /* unlock the next level if needed */
-    if (nl != NULL && nl->is_locked)
-    {
-        if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        {
-            lg->unlock = 1;
-            nl->is_locked = 0;
-            s->locked -= 1;
-            dirty = 1;
-        }
-        else
-            nl = NULL;
-    }
+    if ((score_rank && *score_rank < RANK_LAST) ||
+        (times_rank && *times_rank < RANK_LAST))
+        return 1;
+    else
+        return 0;
+}
 
-    /* got the next level */
-    lg->next_level = nl;
+void set_rename_player(int score_rank, int times_rank, const char *player)
+{
+    struct set *s = SET_GET(sets, curr);
 
-    /* Update file */
-    if (dirty)
-        set_store_hs();
+    SAFECPY(s->coin_score.player[score_rank], player);
+    SAFECPY(s->time_score.player[times_rank], player);
 }
 
 /*---------------------------------------------------------------------------*/
 
-void level_snap(int i)
+void level_snap(int i, const char *path)
 {
-    char filename[MAXSTR];
-    char *ext;
+    char *filename;
 
     /* Convert the level name to a PNG filename. */
 
-    memset(filename, 0, MAXSTR);
-
-    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");
+    filename = concat_string(path,
+                             "/",
+                             base_name_sans(level_v[i].file, ".sol"),
+                             ".png",
+                             NULL);
 
     /* Initialize the game for a snapshot. */
 
-    if (game_init(&level_v[i], 0, 0))
+    if (game_client_init(level_v[i].file))
     {
+        union cmd cmd;
+        cmd.type = CMD_GOAL_OPEN;
+        game_proxy_enq(&cmd);
+        game_client_sync(NULL);
+
         /* Render the level and grab the screen. */
 
-        config_clear();
-        game_set_fly(1.f);
+        video_clear();
+        game_client_fly(1.0f);
         game_kill_fade();
-        game_draw(1, 0);
-        SDL_GL_SwapBuffers();
-
+        game_client_draw(POSE_LEVEL, 0);
         image_snap(filename);
+
+        SDL_GL_SwapBuffers();
     }
+
+    free(filename);
 }
 
 void set_cheat(void)
-/* Open each level of the current set */
 {
     int i;
 
-    set_v[set].locked = 0;
-
-    for (i = 0; i < set_v[set].count; i++)
-        level_v[i].is_locked = 0;
+    for (i = 0; i < SET_GET(sets, curr)->count; i++)
+    {
+        level_v[i].is_locked    = 0;
+        level_v[i].is_completed = 1;
+    }
 }
 
-
 /*---------------------------------------------------------------------------*/