#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 "common.h"
+#include "fs.h"
#include "game_server.h"
#include "game_client.h"
{
char file[PATHMAX];
- char *id; /* Internal set identifier */
- char *name; /* Set name */
- char *desc; /* Set description */
- char *shot; /* Set screen-shot */
+ char *id; /* Internal set identifier */
+ char *name; /* Set name */
+ char *desc; /* Set description */
+ char *shot; /* Set screen-shot */
- char user_scores[PATHMAX]; /* User high-score file */
+ 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 */
+ struct score coin_score; /* Challenge score */
+ struct score time_score; /* Challenge score */
- /* Level info */
+ /* Level info */
- int count; /* Number of levels */
- char *level_name_v[MAXLVL]; /* List of level file names */
+ int count; /* Number of levels */
+ char *level_name_v[MAXLVL]; /* List of level file names */
};
-static int set_state = 0;
+#define SET_GET(a, i) ((struct set *) array_get((a), (i)))
-static int set;
-static int count;
+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]);
}
-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.fast_unlock);
- 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);
- }
-}
-
-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;
}
-/* 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.fast_unlock) &&
- 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 int set_load(struct set *s, const char *filename)
{
- FILE *fin;
+ fs_file fin;
char *scores, *level_name;
- fin = fopen(config_data(filename), "r");
+ /* 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, 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;
}
score_init_hs(&s->time_score, 359999, 0);
score_init_hs(&s->coin_score, 359999, 0);
- strncpy(s->file, filename, PATHMAX - 1);
+ SAFECPY(s->file, filename);
if (read_line(&s->name, fin) &&
read_line(&s->desc, fin) &&
read_line(&scores, fin))
{
sscanf(scores, "%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]);
+ &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);
- strncpy(s->user_scores, "neverballhs-", PATHMAX - 1);
- strncat(s->user_scores, s->id, PATHMAX - 1 - strlen("neverballhs-"));
+ 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++;
}
- fclose(fin);
+ fs_close(fin);
return 1;
}
free(s->id);
free(s->shot);
- fclose(fin);
+ fs_close(fin);
return 0;
}
+static void set_free(struct set *s)
+{
+ int i;
+
+ free(s->name);
+ free(s->desc);
+ free(s->id);
+ free(s->shot);
+
+ free(s->user_scores);
+ free(s->cheat_scores);
+
+ for (i = 0; i < s->count; i++)
+ free(s->level_name_v[i]);
+}
+
+/*---------------------------------------------------------------------------*/
+
+static int cmp_dir_items(const void *A, const void *B)
+{
+ const struct dir_item *a = A, *b = B;
+ return strcmp(a->path, b->path);
+}
+
+static int set_is_loaded(const char *path)
+{
+ int i;
+
+ for (i = 0; i < array_len(sets); i++)
+ if (strcmp(SET_GET(sets, i)->file, path) == 0)
+ return 1;
+
+ return 0;
+}
+
+static int is_unseen_set(struct dir_item *item)
+{
+ return (str_starts_with(base_name(item->path), "set-") &&
+ str_ends_with(item->path, ".txt") &&
+ !set_is_loaded(item->path));
+}
+
int set_init()
{
- FILE *fin;
+ fs_file fin;
char *name;
- if (set_state)
- set_free();
+ Array items;
+ int i;
+
+ if (sets)
+ set_quit();
+
+ sets = array_new(sizeof (struct set));
+ curr = 0;
- set = 0;
- count = 0;
+ /*
+ * First, load the sets listed in the set file, preserving order.
+ */
- if ((fin = fopen(config_data(SET_FILE), "r")))
+ if ((fin = fs_open(SET_FILE, "r")))
{
- while (count < MAXSET && read_line(&name, fin))
+ while (read_line(&name, fin))
{
- if (set_load(&set_v[count], name))
- count++;
+ struct set *s = array_add(sets);
+
+ if (!set_load(s, name))
+ array_del(sets);
free(name);
}
- fclose(fin);
+ fs_close(fin);
+ }
+
+ /*
+ * Then, scan for any remaining set description files, and add
+ * them after the first group in alphabetic order.
+ */
+
+ 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);
+
+ if (!set_load(s, DIR_ITEM_GET(items, i)->path))
+ array_del(sets);
+ }
- set_state = 1;
+ fs_dir_free(items);
}
- return count;
+ return array_len(sets);
}
-void set_free(void)
+void set_quit(void)
{
- int i, j;
-
- for (i = 0; i < count; i++)
- {
- free(set_v[i].name);
- free(set_v[i].desc);
- free(set_v[i].id);
- free(set_v[i].shot);
+ int i;
- for (j = 0; j < set_v[i].count; j++)
- free(set_v[i].level_name_v[j]);
- }
+ for (i = 0; i < array_len(sets); i++)
+ set_free(array_get(sets, i));
- set_state = 0;
+ array_free(sets);
+ sets = NULL;
}
/*---------------------------------------------------------------------------*/
int set_exists(int i)
{
- return (0 <= i && i < count);
+ return sets ? 0 <= i && i < array_len(sets) : 0;
}
const char *set_id(int i)
{
- return set_exists(i) ? set_v[i].id : NULL;
+ return set_exists(i) ? SET_GET(sets, i)->id : NULL;
}
const char *set_name(int i)
{
- return set_exists(i) ? _(set_v[i].name) : NULL;
+ return set_exists(i) ? _(SET_GET(sets, i)->name) : NULL;
}
const char *set_desc(int i)
{
- return set_exists(i) ? _(set_v[i].desc) : NULL;
+ return set_exists(i) ? _(SET_GET(sets, i)->desc) : NULL;
}
const char *set_shot(int i)
{
- return set_exists(i) ? set_v[i].shot : NULL;
-}
-
-const struct score *set_time_score(int i)
-{
- return set_exists(i) ? &set_v[i].time_score : NULL;
+ return set_exists(i) ? SET_GET(sets, i)->shot : NULL;
}
-const struct score *set_coin_score(int i)
+const struct score *set_score(int i, int s)
{
- return set_exists(i) ? &set_v[i].coin_score : NULL;
+ 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;
}
/*---------------------------------------------------------------------------*/
-int set_level_exists(int s, int i)
-{
- return (i >= 0 && i < set_v[s].count);
-}
-
static void set_load_levels(void)
{
- struct level *l;
- int nb = 1, bnb = 1;
-
- int i;
-
- const char *roman[] = {
+ static const char *roman[] = {
"",
- "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"
};
- for (i = 0; i < set_v[set].count; i++)
+ struct set *s = SET_GET(sets, curr);
+ int regular = 1, bonus = 1;
+ int i;
+
+ for (i = 0; i < s->count; i++)
{
- l = &level_v[i];
+ struct level *l = &level_v[i];
- level_load(set_v[set].level_name_v[i], l);
+ level_load(s->level_name_v[i], l);
- l->set = &set_v[set];
l->number = i;
if (l->is_bonus)
- sprintf(l->name, "%s", roman[bnb++]);
+ SAFECPY(l->name, roman[bonus++]);
else
- sprintf(l->name, "%02d", nb++);
+ sprintf(l->name, "%02d", regular++);
- l->is_locked = 1;
+ l->is_locked = (i > 0);
l->is_completed = 0;
- }
-
- /* Unlock first level. */
- level_v[0].is_locked = 0;
+ if (i > 0)
+ level_v[i - 1].next = l;
+ }
}
void set_goto(int i)
{
- set = i;
+ curr = i;
set_load_levels();
set_load_hs();
int curr_set(void)
{
- return set;
+ return curr;
}
struct level *get_level(int i)
{
- return (i >= 0 && i < set_v[set].count) ? &level_v[i] : NULL;
+ return (i >= 0 && i < SET_GET(sets, curr)->count) ? &level_v[i] : NULL;
}
/*---------------------------------------------------------------------------*/
int set_score_update(int timer, int coins, int *score_rank, int *times_rank)
{
- struct set *s = &set_v[set];
- char player[MAXSTR] = "";
+ struct set *s = SET_GET(sets, curr);
+ const char *player = config_get_s(CONFIG_PLAYER);
- config_get_s(CONFIG_PLAYER, player, MAXSTR);
+ score_coin_insert(&s->coin_score, score_rank, player, timer, coins);
+ score_time_insert(&s->time_score, times_rank, player, timer, coins);
- if (score_rank)
- *score_rank = score_coin_insert(&s->coin_score, player, timer, coins);
-
- if (times_rank)
- *times_rank = score_time_insert(&s->time_score, player, timer, coins);
-
- if ((score_rank && *score_rank < 3) || (times_rank && *times_rank < 3))
+ if ((score_rank && *score_rank < RANK_LAST) ||
+ (times_rank && *times_rank < RANK_LAST))
return 1;
else
return 0;
void set_rename_player(int score_rank, int times_rank, const char *player)
{
- struct set *s = &set_v[set];
+ struct set *s = SET_GET(sets, curr);
- strncpy(s->coin_score.player[score_rank], player, MAXNAM);
- strncpy(s->time_score.player[times_rank], player, MAXNAM);
+ 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_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. */
video_clear();
- game_set_fly(1.f, game_client_file());
+ game_client_fly(1.0f);
game_kill_fade();
- game_client_step(NULL);
- game_draw(1, 0);
-
+ game_client_draw(POSE_LEVEL, 0);
image_snap(filename);
SDL_GL_SwapBuffers();
}
+
+ free(filename);
}
void set_cheat(void)
{
int i;
- for (i = 0; i < set_v[set].count; i++)
+ for (i = 0; i < SET_GET(sets, curr)->count; i++)
{
level_v[i].is_locked = 0;
level_v[i].is_completed = 1;