Merge branch 'gles'
[neverball] / ball / set.c
index f1b6b8c..7d0b813 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #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 "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 count;                    /* number of sets */
+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          */
+};
 
-static struct set set_v[MAXSET];     /* array of sets */
+#define SET_GET(a, i) ((struct set *) array_get((a), (i)))
 
-static struct set *current_set;      /* currently selected set */
+static Array sets;
+static int   curr;
 
-static struct level level_v[MAXLVL]; /* levels of the current set  */
+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;
-    for (j = 0; j < NSCORE; j++)
-       fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
+    int i;
+
+    for (i = RANK_HARD; i <= RANK_EASY; i++)
+        fs_printf(fp, "%d %d %s\n", s->timer[i], s->coins[i], s->player[i]);
 }
 
-static void set_store_hs(void)
-/* Store the score of the set */
+static int get_score(fs_file fp, struct score *s)
 {
-    const struct set *s = current_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];
-            put_score(fout, &l->time_score);
-            put_score(fout, &l->goal_score);
-            put_score(fout, &l->coin_score);
-        }
+            const struct level *l = &level_v[i];
 
-        fclose(fout);
-    }
-}
+            put_score(fp, &l->scores[SCORE_TIME]);
+            put_score(fp, &l->scores[SCORE_GOAL]);
+            put_score(fp, &l->scores[SCORE_COIN]);
+        }
 
-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;
+        fs_close(fp);
     }
-    return res;
 }
 
 static void set_load_hs(void)
-/* 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 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));
-        for (i = 0; i < s->count && res; i++)
+        char states[MAXLVL + 2];
+
+        if (fs_gets(states, sizeof (states), fp))
         {
-            if (states[i] == 'L')
+            struct level *l;
+            int i, n = MIN(strlen(states) - 1, s->count);
+
+            for (i = 0; i < n; i++)
             {
-                level_v[i].is_locked = 1;
-                level_v[i].is_completed = 0;
+                l = &level_v[i];
+
+                l->is_locked    = (states[i] == 'L');
+                l->is_completed = (states[i] == 'C');
             }
-            else if (states[i] == 'C')
-            {
-                level_v[i].is_locked = 0;
-                level_v[i].is_completed = 1;
-            } 
-            else if (states[i] == 'O')
+
+            get_score(fp, &s->time_score);
+            get_score(fp, &s->coin_score);
+
+            for (i = 0; i < n; i++)
             {
-                level_v[i].is_locked = 0;
-                level_v[i].is_completed = 0;
+                l = &level_v[i];
+
+                get_score(fp, &l->scores[SCORE_TIME]);
+                get_score(fp, &l->scores[SCORE_GOAL]);
+                get_score(fp, &l->scores[SCORE_COIN]);
             }
-            else
-                res = 0;
-        }
-        
-        res = res &&
-            get_score(fin, &s->time_score) &&
-            get_score(fin, &s->coin_score);
-        
-        for (i = 0; i < s->count && res; i++)
-        {
-            l = &level_v[i];
-            res = get_score(fin, &l->time_score) &&
-                  get_score(fin, &l->goal_score) &&
-                  get_score(fin, &l->coin_score);
         }
 
-        fclose(fin);
-    }
-    
-    if (!res && errno != ENOENT)
-    {
-        fprintf(stderr, _("Error while loading user high-score file '%s': "),
-                fn);
-        if (errno)
-            perror(NULL);
-        else
-            fprintf(stderr, _("Incorrect format\n"));
+        fs_close(fp);
     }
 }
 
-static char *chomp(char *str)
-/* Remove trailing \n if any */
-{
-    char *p = str + strlen(str) - 1;
-    if (p >= str && *p == '\n') *p = 0;
-    return str;
-}
+/*---------------------------------------------------------------------------*/
 
 static int set_load(struct set *s, const char *filename)
-/* Count levels */
 {
-    FILE *fin;
-    char buf[MAXSTR];
-    int res = 0;
-    
-    /* Open the datafile */
-
-    fin = fopen(filename, "r");
-    if (fin == NULL)
+    fs_file fin;
+    char *scores, *level_name;
+
+    /* Skip "Misc" set when not in dev mode. */
+
+    if (strcmp(filename, SET_MISC) == 0 && !config_cheat())
+        return 0;
+
+    fin = fs_open(filename, "r");
+
+    if (!fin)
     {
-        fprintf(stderr, _("Cannot load the set file '%s':"), filename);
-        perror(NULL);
+        fprintf(stderr, L_("Failure to load set file '%s'\n"), filename);
         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 are missing. */
 
-    /* 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 */
-    
-    strcpy(s->file, filename);
-    if ((res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->name, chomp(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->desc, chomp(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->setname, chomp(buf));
-    if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
-        strcpy(s->shot, chomp(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]);
-    strcpy(s->user_scores, "neverballhs-");
-    strcat(s->user_scores, s->setname);
-
-    /* Count levels 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")))
+
+    SAFECPY(s->file, filename);
+
+    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))
     {
-        char states[MAXLVL + 1];
-        int i;
-        if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
+        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]);
+
+        free(scores);
+
+        s->user_scores  = concat_string("Scores/", s->id, ".txt",       NULL);
+        s->cheat_scores = concat_string("Scores/", s->id, "-cheat.txt", NULL);
+
+        s->count = 0;
+
+        while (s->count < MAXLVL && read_line(&level_name, fin))
         {
-            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;
-                }
-            }
+            s->level_name_v[s->count] = level_name;
+            s->count++;
         }
-        fclose(fin);
+
+        fs_close(fin);
+
+        return 1;
     }
-    if (s->locked == s->count)
-        s->locked = s->count-1;
 
-    return 1;
-}
+    free(s->name);
+    free(s->desc);
+    free(s->id);
+    free(s->shot);
 
-/*---------------------------------------------------------------------------*/
+    fs_close(fin);
 
-void set_init()
-{
-    FILE *fin;
-    struct set *set;
-    char filename[MAXSTR];
-    int res;
+    return 0;
+}
 
-    current_set = NULL;
-    
-    count = 0;
+static void set_free(struct set *s)
+{
+    int i;
 
-    if ((fin = fopen(config_data(SET_FILE), "r")))
-    {
-        res = 1;
-        while (count < MAXSET && res)
-        {
-            set = &(set_v[count]);
+    free(s->name);
+    free(s->desc);
+    free(s->id);
+    free(s->shot);
 
-            /* clean the set data */
-            
-            res = (fgets(filename, MAXSTR, fin) != NULL);
-            if (res)
-            {
-                chomp(filename);
-
-                res = set_load(set, config_data(filename));
-                if (res)
-                {
-                    set->number = count;
-                    count++;
-                }
-            }
-        }
+    free(s->user_scores);
+    free(s->cheat_scores);
 
-        fclose(fin);
-    }
+    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)
-/* Are all levels (even extra bonus) unlocked? */
-{
-    return s->locked == 0;
+    return 0;
 }
 
-int  set_completed(const struct set *s)
-/* Are all levels (even extra bonus) completed? */
+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)
-/* Does the level i of the set exist? */
+int set_init()
 {
-    return (i >= 0) && (i < s->count);
-}
+    fs_file fin;
+    char *name;
 
-/*---------------------------------------------------------------------------*/
+    Array items;
+    int i;
 
-static void set_load_levels(void)
-/* Load more the levels of the current set */
-{
-    FILE *fin;
-    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);
-    
-    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++)
+    if (sets)
+        set_quit();
+
+    sets = array_new(sizeof (struct set));
+    curr = 0;
+
+    /*
+     * First, load the sets listed in the set file, preserving order.
+     */
+
+    if ((fin = fs_open(SET_FILE, "r")))
     {
-        l = &level_v[i];
-        res = (fscanf(fin, "%s", name) == 1);
-        assert(res);
+        while (read_line(&name, fin))
+        {
+            struct set *s = array_add(sets);
 
-        level_load(config_data(name), l);
+            if (!set_load(s, name))
+                array_del(sets);
 
-        /* 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;
+            free(name);
+        }
+        fs_close(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();
-}
-
-const struct set *curr_set(void)
-{
-    return current_set;
-}
+    /*
+     * Then, scan for any remaining set description files, and add
+     * them after the first group in alphabetic order.
+     */
 
-const struct level *get_level(int i)
-{
-    return (i >= 0 && i < current_set->count) ? &level_v[i] : NULL;
-}
+    if ((items = fs_dir_scan("", is_unseen_set)))
+    {
+        array_sort(items, cmp_dir_items);
 
-/*---------------------------------------------------------------------------*/
+        for (i = 0; i < array_len(items); i++)
+        {
+            struct set *s = array_add(sets);
 
-static int score_time_comp(const struct score *S, int i, int j)
-{
-    if (S->timer[i] < S->timer[j])
-        return 1;
+            if (!set_load(s, DIR_ITEM_GET(items, i)->path))
+                array_del(sets);
+        }
 
-    if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
-        return 1;
+        fs_dir_free(items);
+    }
 
-    return 0;
+    return array_len(sets);
 }
 
-static int score_coin_comp(const struct score *S, int i, int j)
+void set_quit(void)
 {
-    if (S->coins[i] > S->coins[j])
-        return 1;
+    int i;
 
-    if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
-        return 1;
+    for (i = 0; i < array_len(sets); i++)
+        set_free(array_get(sets, i));
 
-    return 0;
+    array_free(sets);
+    sets = NULL;
 }
 
-static void score_swap(struct score *S, int i, int j)
-{
-    char player[MAXNAM];
-    int  tmp;
-
-    strncpy(player,       S->player[i], MAXNAM);
-    strncpy(S->player[i], S->player[j], MAXNAM);
-    strncpy(S->player[j], player,       MAXNAM);
+/*---------------------------------------------------------------------------*/
 
-    tmp         = S->timer[i];
-    S->timer[i] = S->timer[j];
-    S->timer[j] = tmp;
+int set_exists(int i)
+{
+    return sets ? 0 <= i && i < array_len(sets) : 0;
+}
 
-    tmp         = S->coins[i];
-    S->coins[i] = S->coins[j];
-    S->coins[j] = tmp;
+const char *set_id(int i)
+{
+    return set_exists(i) ? SET_GET(sets, i)->id : NULL;
 }
 
-static int score_time_insert(struct score *s, const char *player, int timer,
-                             int coins)
+const char *set_name(int i)
 {
-    int i;
+    return set_exists(i) ? _(SET_GET(sets, i)->name) : NULL;
+}
 
-    strncpy(s->player[3], player, MAXNAM);
-    s->timer[3] = timer;
-    s->coins[3] = coins;
+const char *set_desc(int i)
+{
+    return set_exists(i) ? _(SET_GET(sets, i)->desc) : NULL;
+}
 
-    for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
-        score_swap(s, i + 1, i);
+const char *set_shot(int i)
+{
+    return set_exists(i) ? SET_GET(sets, i)->shot : NULL;
+}
 
-    return i + 1;
+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;
 }
 
-static int score_coin_insert(struct score *s, const char *player, int timer,
-                             int coins)
+/*---------------------------------------------------------------------------*/
+
+static void set_load_levels(void)
 {
+    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"
+    };
+
+    struct set *s = SET_GET(sets, curr);
+    int regular = 1, bonus = 1;
     int i;
 
-    strncpy(s->player[3], player, MAXNAM);
-    s->timer[3] = timer;
-    s->coins[3] = coins;
+    for (i = 0; i < s->count; i++)
+    {
+        struct level *l = &level_v[i];
 
-    for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
-        score_swap(s, i + 1, i);
+        level_load(s->level_name_v[i], l);
 
-    return i + 1;
-}
+        l->number = i;
 
-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 = &level_v[lg->level->number];
-
-    lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
-        
-    if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
-    else
-        lg->goal_rank = 3;
+        if (l->is_bonus)
+            SAFECPY(l->name, roman[bonus++]);
+        else
+            sprintf(l->name, "%02d", regular++);
 
-    lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
+        l->is_locked = (i > 0);
+        l->is_completed = 0;
 
-    return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
+        if (i > 0)
+            level_v[i - 1].next = l;
+    }
 }
 
-static int set_score_update(struct level_game *lg, const char *player)
-/* Update the set score rank according to score and times */
+void set_goto(int i)
 {
-    int timer = lg->times;
-    int coins = lg->score;
-    struct set *s = current_set;
+    curr = i;
 
-    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);
+    set_load_levels();
+    set_load_hs();
 }
 
-
-void score_change_name(struct level_game *lg, const char *player)
-/* Update the player name for set and level high-score */
+int curr_set(void)
 {
-#define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
-    struct set *s = current_set;
-    struct level *l = &level_v[lg->level->number];
-    UPDATE(lg->time_rank, l->time_score);
-    UPDATE(lg->goal_rank, l->goal_score);
-    UPDATE(lg->coin_rank, l->coin_score);
-    UPDATE(lg->score_rank, s->coin_score);
-    UPDATE(lg->times_rank, s->time_score);
-    set_store_hs();
+    return curr;
 }
 
-static struct level *next_level(int i)
+struct level *get_level(int i)
 {
-/* Return the ith level, or NULL */
-    return set_level_exists(current_set, i + 1) ? &level_v[i + 1] : NULL;
+    return (i >= 0 && i < SET_GET(sets, curr)->count) ? &level_v[i] : 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 (!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 */
+int set_score_update(int timer, int coins, int *score_rank, int *times_rank)
 {
-    struct set *s = current_set;
-    int ln = lg->level->number; /* curent level number */
-    struct level *cl = &level_v[ln];    /* current level */
-    struct level *nl = NULL;    /* next level */
-    int dirty = 0;              /* HS should 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->state == 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->state == GAME_GOAL || lg->state == GAME_SPEC)
-    {
-        /* Identify the following level */
-        nl = next_level(ln + lg->state_value);
-        if (nl != NULL)
-        {
-            /* skip bonuses if unlocked in non challenge mode */
-            if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
-                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 *filename;
 
     /* Convert the level name to a PNG filename. */
 
-    memset(filename, 0, MAXSTR);
-    strcpy(filename, strstr(level_v[i].file, "map-"));
-    strcpy(strstr(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);
 
-        image_snap(filename, config_get_d(CONFIG_WIDTH),
-                   config_get_d(CONFIG_HEIGHT));
+        SDL_GL_SwapBuffers();
     }
+
+    free(filename);
 }
 
 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++)
-        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;
+    }
+}
 
 /*---------------------------------------------------------------------------*/