-/*
+/*
* Copyright (C) 2003 Robert Kooima
*
* NEVERBALL is free software; you can redistribute it and/or modify
FILE *fout;
int i;
const struct level *l;
- int lim = s->limit;
-
- if (lim <= s->count)
- lim = s->count - 1;
+ char states[MAXLVL + 1];
if ((fout = fopen(config_user(s->user_scores), "w")))
{
- fprintf(fout, "%d\n", s->limit);
-
- put_score(fout, &s->time_score);
- put_score(fout, &s->coin_score);
-
- for (i = 0 ; i <= lim ; i++)
- {
- l = &level_v[i];
- put_score(fout, &l->time_score);
- put_score(fout, &l->goal_score);
- put_score(fout, &l->coin_score);
- }
-
- fclose(fout);
+ for (i = 0; i < s->count; i++)
+ {
+ if (level_v[i].is_locked)
+ states[i] = 'L';
+ else if (level_v[i].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);
+
+ 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);
+ }
+
+ fclose(fout);
}
}
for (j = 0; j < NSCORE && res; j++)
{
res = (fscanf(fp, "%d %d %s\n",
- &s->timer[j], &s->coins[j], s->player[j])) == 3;
+ &s->timer[j], &s->coins[j], s->player[j])) == 3;
}
return res;
}
int res = 0;
struct level *l;
const char *fn = config_user(s->user_scores);
- int lim;
+ char states[MAXLVL + 1];
if ((fin = fopen(fn, "r")))
{
- res = (fscanf(fin, "%d\n", &lim) == 1) &&
- s->limit == lim &&
- get_score(fin, &s->time_score) &&
- get_score(fin, &s->coin_score);
-
- if (lim >= s->count)
- lim = s->count - 1;
-
- for (i = 0; i <= lim && 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);
+ res = ((fscanf(fin, "%s\n", states) == 1) &&
+ (strlen(states) == s->count));
+ for (i = 0; i < s->count && res; i++)
+ {
+ if (states[i] == 'L')
+ {
+ level_v[i].is_locked = 1;
+ level_v[i].is_completed = 0;
+ }
+ else if (states[i] == 'C')
+ {
+ level_v[i].is_locked = 0;
+ level_v[i].is_completed = 1;
+ }
+ else if (states[i] == 'O')
+ {
+ level_v[i].is_locked = 0;
+ level_v[i].is_completed = 0;
+ }
+ 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"));
+ fprintf(stderr, _("Error while loading user high-score file '%s': "),
+ fn);
+ if (errno)
+ perror(NULL);
+ else
+ fprintf(stderr, _("Incorrect format\n"));
}
}
-static const char * numbernames[] = {
- "01", "02", "03", "04", "05",
- "06", "07", "08", "09", "10",
- "11", "12", "13", "14", "15",
- "16", "17", "18", "19", "20",
- N_("B1"), N_("B2"), N_("B3"), N_("B4"), N_("B5")};
-
+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 void set_init_levels(struct set *s)
+static int set_load(struct set *s, const char *filename)
/* Count levels */
{
FILE *fin;
char buf[MAXSTR];
+ int res = 0;
- /* Load the levels list. */
-
- s->count = 0;
+ /* Open the datafile */
- if ((fin = fopen(config_data(s->init_levels), "r")))
+ fin = fopen(filename, "r");
+ if (fin == NULL)
{
- while (count < MAXLVL && fgets(buf, MAXSTR, fin))
- s->count++;
- fclose(fin);
+ fprintf(stderr, _("Cannot load the set file '%s':"), filename);
+ perror(NULL);
+ return 0;
}
- /* Load the highscore level limit */
-
- s->limit = 0;
-
- if ((fin = fopen(config_user(s->user_scores), "r")))
- {
- fscanf(fin, "%d\n", &s->limit);
- if (s->limit > s->count)
- s->limit = 0;
- fclose(fin);
- }
-
-}
+ /* Raz the set structure */
-static void set_init_hs(void)
-/* Fill set end levels hs with initial values (score-*.txt) */
-{
- struct set *s = current_set;
- char buf[MAXSTR];
- FILE *fin;
- int res = 0;
- const char *fn = config_data(s->init_scores);
+ 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 file is missing. */
score_init_hs(&s->time_score, 359999, 0);
score_init_hs(&s->coin_score, 359999, 0);
- /* Load the initial high scores file. */
+ /* 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. */
- if ((fin = fopen(fn, "r")))
- {
- res = fgets(buf, MAXSTR, fin) != NULL;
-
- res = res && (sscanf(buf, "%d %d %d %d %d %d",
- &s->time_score.timer[0],
- &s->coin_score.coins[0],
- &s->time_score.timer[1],
- &s->coin_score.coins[1],
- &s->time_score.timer[2],
- &s->coin_score.coins[2]) == 6);
- fclose(fin);
- }
-
- if (!res)
+ 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")))
{
- fprintf(stderr, _("Error while loading initial high-score file '%s': "), fn);
- if (errno)
- perror(NULL);
- else
- fprintf(stderr, _("Incorrect format\n"));
+ 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()
{
FILE *fin;
- struct set * set;
+ struct set *set;
+ char filename[MAXSTR];
int res;
current_set = NULL;
-
+
count = 0;
if ((fin = fopen(config_data(SET_FILE), "r")))
{
- res = 1;
- while (count < MAXSET && res)
- {
- set = &(set_v[count]);
-
- /* clean the set data */
- memset(set, 0, sizeof(struct set));
-
- res = fscanf(fin, "%s %s %s %s\n",
- set->init_levels,
- set->init_scores,
- set->user_scores,
- set->shot) == 4 &&
- fgets(set->name, MAXSTR, fin) &&
- fgets(set->desc, MAXSTR, fin);
- if (res)
- {
- char *p = set->name + strlen(set->name) - 1;
- char *q = set->desc + strlen(set->desc) - 1;
- set->number = count;
-
- if (*p == '\n') *p = 0;
- if (*q == '\n') *q = 0;
-
- set_init_levels(set);
-
- count++;
- }
+ res = 1;
+ while (count < MAXSET && res)
+ {
+ set = &(set_v[count]);
+
+ /* 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++;
+ }
+ }
}
fclose(fin);
/*---------------------------------------------------------------------------*/
-int set_extra_bonus_opened(const struct set *s)
-/* Are extra bonus openned (ie challenge completed)? */
+int set_unlocked(const struct set *s)
+/* Are all levels (even extra bonus) unlocked? */
{
- return s->limit >= 20;
+ return s->locked == 0;
}
int set_completed(const struct set *s)
/* Are all levels (even extra bonus) completed? */
{
- return s->limit >= s->count;
+ return s->completed == s->count;
}
int set_level_exists(const struct set *s, int i)
-/* Is the level i of the set exists */
+/* Does the level i of the set exist? */
{
return (i >= 0) && (i < s->count);
}
FILE *fin;
char buf[MAXSTR];
char name[MAXSTR];
- struct level * l;
+ struct level *l;
+
+ int i = 0, res;
+ int nb = 1, bnb = 1;
+
+ fin = fopen(current_set->file, "r");
+ assert(fin != NULL);
- int i=0, res;
-
- if ((fin = fopen(config_data(current_set->init_levels), "r")))
+ 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++)
{
- res = 1;
- for(i=0; i<current_set->count && res; i++)
- {
- l = &level_v[i];
- res = (fgets(buf, MAXSTR, fin) != NULL) &&
- (sscanf(buf, "%s ", name) == 1);
- assert(res);
-
- level_load(config_data(name), l);
-
- /* Initialize set related info */
- l->set = current_set;
- l->number = i;
- l->numbername = numbernames[i];
- l->is_locked = i > current_set->limit;
- l->is_bonus = i >= 20;
- }
- fclose(fin);
+ l = &level_v[i];
+ res = (fscanf(fin, "%s", name) == 1);
+ assert(res);
+
+ level_load(config_data(name), l);
+
+ /* 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;
}
+ level_v[0].is_locked = 0; /* unlock the first level */
+ fclose(fin);
assert(i == current_set->count);
}
assert(set_exists(i));
current_set = &set_v[i];
set_load_levels();
- set_init_hs();
set_load_hs();
}
const struct level *get_level(int i)
{
- return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
+ return (i >= 0 && i < current_set->count) ? &level_v[i] : NULL;
}
/*---------------------------------------------------------------------------*/
static int score_time_comp(const struct score *S, int i, int j)
{
- if (S->timer[i] < S->timer[j])
+ if (S->timer[i] < S->timer[j])
return 1;
- if (S->timer[i] == S->timer[j] &&
- S->coins[i] > S->coins[j])
+ if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
return 1;
return 0;
static int score_coin_comp(const struct score *S, int i, int j)
{
- if (S->coins[i] > S->coins[j])
+ if (S->coins[i] > S->coins[j])
return 1;
- if (S->coins[i] == S->coins[j] &&
- S->timer[i] < S->timer[j])
+ if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
return 1;
return 0;
S->coins[j] = tmp;
}
-static int score_time_insert(struct score *s, const char* player, int timer, int coins)
+static int score_time_insert(struct score *s, const char *player, int timer,
+ int coins)
{
int i;
-
+
strncpy(s->player[3], player, MAXNAM);
s->timer[3] = timer;
s->coins[3] = coins;
for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
score_swap(s, i + 1, i);
- return i+1;
+
+ return i + 1;
}
-static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
+static int score_coin_insert(struct score *s, const char *player, int timer,
+ int coins)
{
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 i+1;
+
+ return i + 1;
}
static int level_score_update(struct level_game *lg, const char *player)
{
int timer = lg->timer;
int coins = lg->coins;
- struct level * l = &level_v[lg->level->number];
+ 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);
+ lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
else
- lg->goal_rank = 3;
+ lg->goal_rank = 3;
lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
{
int timer = lg->times;
int coins = lg->score;
- struct set * s = current_set;
+ struct set *s = current_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);
/* 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 = current_set;
struct level *l = &level_v[lg->level->number];
UPDATE(lg->time_rank, l->time_score);
UPDATE(lg->goal_rank, l->goal_score);
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;
+}
+
+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.
+/* Inform the set that a level is finished.
* Update next_level and score rank fields */
{
struct set *s = current_set;
- int level = lg->level->number;
- int dirty = 0;
-
- /* Update scores */
- dirty = level_score_update(lg, player);
- dirty = set_score_update(lg, player) || dirty;
-
- /* compute the next level */
+ 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;
+ 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;
+ }
+
+ /* Win ! */
+ if (lg->win)
+ {
+ /* update set score */
+ set_score_update(lg, player);
+ /* unlock all levels */
+ set_cheat();
+ dirty = 1;
}
-
- level++; /* level is the next level */
-
- /* if the next level is not oppened */
- if (s->limit < level)
- if ((lg->mode == MODE_CHALLENGE) ||
- (lg->mode == MODE_NORMAL && (level < 20 || level > 20)))
- {
- level_v[level].is_locked = 0;
- s->limit = level;
- dirty = 1;
- }
-
- /* got the next level */
- if (lg->mode == MODE_CHALLENGE && level >= 20)
- lg->next_level = NULL; /* End the challenge */
- else if (level < s->count && level <= s->limit)
- lg->next_level = &level_v[level];
- else
- lg->next_level = NULL;
+
+ /* 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;
+ }
+
+ /* got the next level */
+ lg->next_level = nl;
/* Update file */
if (dirty)
- set_store_hs();
+ set_store_hs();
}
/*---------------------------------------------------------------------------*/
{
char filename[MAXSTR];
- /* Convert the level name to a BMP filename. */
+ /* Convert the level name to a PNG filename. */
memset(filename, 0, MAXSTR);
- strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
- strcat(filename, ".bmp");
+ strcpy(filename, strstr(level_v[i].file, "map-"));
+ strcpy(strstr(filename, "."), ".png");
/* Initialize the game for a snapshot. */
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();
game_draw(1, 0);
SDL_GL_SwapBuffers();
- image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
+ image_snap(filename, config_get_d(CONFIG_WIDTH),
+ config_get_d(CONFIG_HEIGHT));
+
+ if (shadow)
+ config_set_d(CONFIG_SHADOW, 1);
}
}
void set_cheat(void)
/* Open each level of the current set */
{
- current_set->limit = current_set->count;
+ int i;
+ current_set->locked = 0;
+ for (i = 0; i < current_set->count; i++)
+ level_v[i].is_locked = 0;
}
/*---------------------------------------------------------------------------*/
-