-/*
+/*
* Copyright (C) 2003 Robert Kooima
*
* NEVERBALL is free software; you can redistribute it and/or modify
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <math.h>
+#include <errno.h>
-#include "level.h"
-#include "glext.h"
-#include "image.h"
-#include "game.h"
-#include "geom.h"
-#include "demo.h"
-#include "hud.h"
-#include "audio.h"
#include "config.h"
+#include "demo.h"
+#include "text.h"
+#include "level.h"
+#include "mode.h"
+#include "set.h"
+#include "solid.h"
/*---------------------------------------------------------------------------*/
-struct score
-{
- char time_n[4][MAXNAM];
- int time_t[4];
- int time_c[4];
-
- char coin_n[4][MAXNAM];
- int coin_t[4];
- int coin_c[4];
-};
-
-struct level
-{
- char file[MAXSTR];
- char back[MAXSTR];
- char grad[MAXSTR];
- char shot[MAXSTR];
- char song[MAXSTR];
- int time;
- int goal;
-
- GLuint text;
-};
-
-static int score; /* Current coin total */
-static int coins; /* Current coin count */
-static int balls; /* Current life count */
-static int goal; /* Current goal count */
-
-static int level; /* Current level number */
-static int count; /* Number of levels */
-static int limit; /* Last opened (locked) level */
-static int status; /* Status of current level */
-
-static int mode; /* Current play mode */
-
-static int coins_total;
-static int times_total;
-
-static struct level level_v[MAXLVL];
-static struct score score_v[MAXLVL];
-
-static char scores_file[MAXSTR];
-
-/*---------------------------------------------------------------------------*/
-
-static void level_store_hs(const char *filename)
+static void scan_dict(struct level *l, const struct s_file *fp)
{
- FILE *fout;
-
- if ((fout = fopen(config_user(filename), "w")))
- {
- int i;
- int j;
-
- for (i = 0; i < limit; i++)
- for (j = 0; j < 3; j++)
- {
- if (strlen(score_v[i].time_n[j]) == 0)
- strcpy(score_v[i].time_n[j], DEFAULT_PLAYER);
- if (strlen(score_v[i].coin_n[j]) == 0)
- strcpy(score_v[i].coin_n[j], DEFAULT_PLAYER);
-
- fprintf(fout, "%d %d %s\n",
- score_v[i].time_t[j],
- score_v[i].time_c[j],
- score_v[i].time_n[j]);
- fprintf(fout, "%d %d %s\n",
- score_v[i].coin_t[j],
- score_v[i].coin_c[j],
- score_v[i].coin_n[j]);
- }
-
- fclose(fout);
- }
-}
-
-static void level_load_hs(const char *filename)
-{
- FILE *fin;
-
- limit = 1;
+ int i;
- if ((fin = fopen(config_user(filename), "r")))
+ for (i = 0; i < fp->dc; i++)
{
- int i;
-
- for (i = 0; i < count; i++)
+ char *k = fp->av + fp->dv[i].ai;
+ char *v = fp->av + fp->dv[i].aj;
+
+ if (strcmp(k, "message") == 0)
+ strncpy(l->message, v, MAXSTR);
+ else if (strcmp(k, "back") == 0)
+ strncpy(l->back, v, PATHMAX);
+ else if (strcmp(k, "song") == 0)
+ strncpy(l->song, v, PATHMAX);
+ else if (strcmp(k, "grad") == 0)
+ strncpy(l->grad, v, PATHMAX);
+ else if (strcmp(k, "shot") == 0)
+ strncpy(l->shot, v, PATHMAX);
+ else if (strcmp(k, "goal") == 0)
{
- if (fscanf(fin, "%d %d %s",
- &score_v[i].time_t[0],
- &score_v[i].time_c[0],
- score_v[i].time_n[0]) == 3 &&
- fscanf(fin, "%d %d %s",
- &score_v[i].coin_t[0],
- &score_v[i].coin_c[0],
- score_v[i].coin_n[0]) == 3 &&
- fscanf(fin, "%d %d %s",
- &score_v[i].time_t[1],
- &score_v[i].time_c[1],
- score_v[i].time_n[1]) == 3 &&
- fscanf(fin, "%d %d %s",
- &score_v[i].coin_t[1],
- &score_v[i].coin_c[1],
- score_v[i].coin_n[1]) == 3 &&
- fscanf(fin, "%d %d %s",
- &score_v[i].time_t[2],
- &score_v[i].time_c[2],
- score_v[i].time_n[2]) == 3 &&
- fscanf(fin, "%d %d %s",
- &score_v[i].coin_t[2],
- &score_v[i].coin_c[2],
- score_v[i].coin_n[2]) == 3)
- limit = i + 1;
+ l->goal = atoi(v);
+ l->score.most_coins.coins[2] = l->goal;
}
-
- fclose(fin);
- }
-}
-
-/*---------------------------------------------------------------------------*/
-
-static void level_init_rc(const char *filename)
-{
- FILE *fin;
- char buf[MAXSTR];
-
- count = 0;
- level = 0;
- coins = 0;
- score = 0;
- balls = 0;
-
- /* Load the levels list. */
-
- if ((fin = fopen(config_data(filename), "r")))
- {
- while (count < MAXLVL && fgets(buf, MAXSTR, fin))
+ else if (strcmp(k, "time") == 0)
{
- sscanf(buf, "%s %s %s %s %d %d %s",
- level_v[count].file,
- level_v[count].back,
- level_v[count].shot,
- level_v[count].grad,
- &level_v[count].time,
- &level_v[count].goal,
- level_v[count].song);
- count++;
+ l->time = atoi(v);
+ l->score.best_times.timer[2] = l->time;
+ l->score.unlock_goal.timer[2] = l->time;
}
- fclose(fin);
+ else if (strcmp(k, "time_hs") == 0)
+ sscanf(v, "%d %d",
+ &l->score.best_times.timer[0],
+ &l->score.best_times.timer[1]);
+ else if (strcmp(k, "goal_hs") == 0)
+ sscanf(v, "%d %d",
+ &l->score.unlock_goal.timer[0],
+ &l->score.unlock_goal.timer[1]);
+ else if (strcmp(k, "coin_hs") == 0)
+ sscanf(v, "%d %d",
+ &l->score.most_coins.coins[0],
+ &l->score.most_coins.coins[1]);
+ else if (strcmp(k, "version") == 0)
+ strncpy(l->version, v, MAXSTR);
+ else if (strcmp(k, "author") == 0)
+ strncpy(l->author, v, MAXSTR);
+ else if (strcmp(k, "bonus") == 0)
+ l->is_bonus = atoi(v) ? 1 : 0;
}
}
-static void level_init_hs(const char *filename)
+int level_load(const char *filename, struct level *level)
{
- char buf[MAXSTR];
- FILE *fin;
- int i = 0;
-
- /* Set some sane values in case the scores file is missing. */
+ struct s_file sol;
- for (i = 0; i < MAXLVL; i++)
- {
- strcpy(score_v[i].time_n[0], "Hard");
- strcpy(score_v[i].time_n[1], "Medium");
- strcpy(score_v[i].time_n[2], "Easy");
-
- score_v[i].time_t[0] = i ? 59999 : 359999;
- score_v[i].time_t[1] = i ? 59999 : 359999;
- score_v[i].time_t[2] = i ? 59999 : 359999;
-
- score_v[i].time_c[0] = 0;
- score_v[i].time_c[1] = 0;
- score_v[i].time_c[2] = 0;
-
- strcpy(score_v[i].coin_n[0], "Hard");
- strcpy(score_v[i].coin_n[1], "Medium");
- strcpy(score_v[i].coin_n[2], "Easy");
-
- score_v[i].coin_t[0] = i ? 59999 : 359999;
- score_v[i].coin_t[1] = i ? 59999 : 359999;
- score_v[i].coin_t[2] = i ? 59999 : 359999;
+ int money;
+ int i;
- score_v[i].coin_c[0] = 0;
- score_v[i].coin_c[1] = 0;
- score_v[i].coin_c[2] = 0;
- }
+ memset(level, 0, sizeof (struct level));
+ memset(&sol, 0, sizeof (sol));
- /* Load the default high scores file. */
+#define format \
+ L_("Error while loading level file '%s': %s\n")
+#define default_error \
+ L_("Not a valid level file")
- if ((fin = fopen(config_data(filename), "r")))
+ if (!sol_load_only_head(&sol, config_data(filename)))
{
- for (i = 0; i < MAXLVL && fgets(buf, MAXSTR, fin); i++)
- sscanf(buf, "%d %d %d %d %d %d",
- &score_v[i].time_t[0], &score_v[i].coin_c[0],
- &score_v[i].time_t[1], &score_v[i].coin_c[1],
- &score_v[i].time_t[2], &score_v[i].coin_c[2]);
-
- fclose(fin);
+ const char *error = errno ? strerror(errno) : default_error;
+ fprintf(stderr, format, filename, error);
+ return 0;
}
-}
-
-/*---------------------------------------------------------------------------*/
-const char *level_shot(int i)
-{
- return level_v[i].shot;
-}
-
-const char *level_time_n(int i, int j)
-{
- return score_v[i].time_n[j];
-}
-
-const char *level_coin_n(int i, int j)
-{
- return score_v[i].coin_n[j];
+#undef format
+#undef default_error
+
+ strcpy(level->file, filename);
+
+ /* Init hs with default values */
+ score_init_hs(&level->score.best_times, 59999, 0);
+ score_init_hs(&level->score.unlock_goal, 59999, 0);
+ score_init_hs(&level->score.most_coins, 59999, 0);
+
+ /* Compute money and default max money */
+ money = 0;
+ for (i = 0; i < sol.hc; i++)
+ if (sol.hv[i].t == ITEM_COIN)
+ money += sol.hv[i].n;
+ level->score.most_coins.coins[0] = money;
+
+ if (sol.dc > 0)
+ scan_dict(level, &sol);
+
+ /* Compute initial hs default values */
+
+#define HOP(t, c) \
+ if (t[2] c t[0]) \
+ t[0] = t[1] = t[2]; \
+ else if (t[2] c t[1]) \
+ t[1] = (t[0] + t[2]) / 2
+
+ HOP(level->score.best_times.timer, <=);
+ HOP(level->score.unlock_goal.timer, <=);
+ HOP(level->score.most_coins.coins, >=);
+
+ sol_free(&sol);
+
+ return 1;
+}
+
+void level_dump(const struct level *l)
+{
+ printf("filename: %s\n"
+ "version: %s\n"
+ "author: %s\n"
+ "time limit: %d\n"
+ "goal count: %d\n"
+ "time hs: %d %d %d\n"
+ "goal hs: %d %d %d\n"
+ "coin hs: %d %d %d\n"
+ "message: %s\n"
+ "background: %s\n"
+ "gradient: %s\n"
+ "screenshot: %s\n"
+ "song: %s\n",
+ l->file,
+ l->version,
+ l->author,
+ l->time,
+ l->goal,
+ l->score.best_times.timer[0],
+ l->score.best_times.timer[1],
+ l->score.best_times.timer[2],
+ l->score.unlock_goal.timer[0],
+ l->score.unlock_goal.timer[1],
+ l->score.unlock_goal.timer[2],
+ l->score.most_coins.coins[0],
+ l->score.most_coins.coins[1],
+ l->score.most_coins.coins[2],
+ l->message,
+ l->back,
+ l->grad,
+ l->shot,
+ l->song);
}
/*---------------------------------------------------------------------------*/
-/* Return the coin count for the Most Coins or Best Time score. */
-
-int level_coin_c(int i, int j)
-{
- if (j < 0)
- return score;
- else
- return score_v[i].coin_c[j];
-}
-
-int level_time_c(int i, int j)
-{
- return score_v[i].time_c[j];
-}
-
-/*---------------------------------------------------------------------------*/
-/* Return the time for the Most Coins or Best Time score. */
-
-int level_coin_t(int i, int j)
-{
- return score_v[i].coin_t[j];
-}
-
-int level_time_t(int i, int j)
-{
- if (j < 0)
- return level_v[i].time - curr_clock();
- else
- return score_v[i].time_t[j];
-}
-
-/*---------------------------------------------------------------------------*/
-
-void level_init(const char *init_levels,
- const char *init_scores,
- const char *user_scores)
-{
- memset(level_v, 0, sizeof (struct level) * MAXLVL);
- memset(score_v, 0, sizeof (struct score) * MAXLVL);
- level_init_rc(init_levels);
- level_init_hs(init_scores);
- level_load_hs(user_scores);
-
- strncpy(scores_file, user_scores, MAXSTR);
-
- level = 0;
-
-}
-
-void level_cheat(void)
-/* Open each level of the set */
-{
- limit = count;
-}
-
-void level_free(void)
-{
- int i;
-
- level_store_hs(scores_file);
-
- for (i = 0; i < count; i++)
- if (glIsTexture(level_v[i].text))
- glDeleteTextures(1, &level_v[i].text);
-
- count = 0;
-}
-
-int level_exists(int i)
-{
- return (0 < i && i < count);
-}
-
-int level_opened(int i)
-{
- return level_exists(i) && (0 < i && i < count && i <= limit);
-}
-
-int level_locked(int i)
-{
- return level_opened(i) && (i == limit) && (level_v[i].goal > 0);
-}
-
-/*---------------------------------------------------------------------------*/
-
-int curr_times_total(void) { return times_total; }
-int curr_coins_total(void) { return coins_total; }
-
-int curr_count(void) { return count; }
-int curr_score(void) { return score; }
-int curr_coins(void) { return coins; }
-int curr_balls(void) { return balls; }
-int curr_level(void) { return level; }
-int curr_goal (void) { return goal; }
-
-/*---------------------------------------------------------------------------*/
-
-static int score_time_comp(const struct score *S, int i, int j)
-{
- if (S->time_t[i] < S->time_t[j])
- return 1;
-
- if (S->time_t[i] == S->time_t[j] &&
- S->time_c[i] > S->time_c[j])
- return 1;
-
- return 0;
-}
-
-static int score_coin_comp(const struct score *S, int i, int j)
-{
- if (S->coin_c[i] > S->coin_c[j])
- return 1;
-
- if (S->coin_c[i] == S->coin_c[j] &&
- S->coin_t[i] < S->coin_t[j])
- return 1;
-
- return 0;
-}
-
-/*---------------------------------------------------------------------------*/
-
-static void score_time_swap(struct score *S, int i, int j)
-{
- char n[MAXNAM];
- int t;
- int c;
-
- strncpy(n, S->time_n[i], MAXNAM);
- strncpy(S->time_n[i], S->time_n[j], MAXNAM);
- strncpy(S->time_n[j], n, MAXNAM);
-
- t = S->time_t[i];
- S->time_t[i] = S->time_t[j];
- S->time_t[j] = t;
-
- c = S->time_c[i];
- S->time_c[i] = S->time_c[j];
- S->time_c[j] = c;
-}
-
-static void score_coin_swap(struct score *S, int i, int j)
-{
- char n[MAXNAM];
- int t;
- int c;
-
- strncpy(n, S->coin_n[i], MAXNAM);
- strncpy(S->coin_n[i], S->coin_n[j], MAXNAM);
- strncpy(S->coin_n[j], n, MAXNAM);
-
- t = S->coin_t[i];
- S->coin_t[i] = S->coin_t[j];
- S->coin_t[j] = t;
-
- c = S->coin_c[i];
- S->coin_c[i] = S->coin_c[j];
- S->coin_c[j] = c;
-}
-
-/*---------------------------------------------------------------------------*/
+static unsigned int do_level_init = 1;
int level_replay(const char *filename)
{
- status = GAME_NONE;
-
- return demo_replay_init(filename, &score, &coins, &balls, &goal);
+ return demo_replay_init(filename, curr_lg());
}
-static int level_play_go(void)
-/* Start to play the current level */
+int level_play(const struct level *l, int m)
{
- status = GAME_NONE;
- coins = 0;
- goal = (mode == MODE_FREE) ? 0 : level_v[level].goal;
-
- return demo_play_init(USER_REPLAY_FILE,
- level_v[level].file,
- level_v[level].back,
- level_v[level].grad,
- level_v[level].song,
- level_v[level].shot,
- level_v[level].time,
- goal, score, coins, balls);
-}
+ struct level_game *lg = curr_lg();
-int level_play(const char *filename, int i, int m)
-/* Start to play a level sequence from the `i'th level */
-{
- mode = m;
- level = i;
-
- score = 0;
- balls = 3;
- coins_total = 0;
- times_total = 0;
-
- return level_play_go();
-}
-
-/*---------------------------------------------------------------------------*/
-
-void level_stat(int s)
-{
- if ((status = s) == GAME_GOAL)
+ if (do_level_init)
{
- coins_total += coins;
- }
-
- demo_play_stat(curr_coins(), level_v[level].time - curr_clock());
-}
-
-int level_dead(void)
-{
- return (balls <= 0);
-}
+ memset(lg, 0, sizeof (struct level_game));
-int level_last(void)
-{
- return (level + 1 == count);
-}
+ lg->mode = m;
+ lg->level = l;
+ lg->balls = 3;
+ }
-int level_exit(const char *filename, int next)
-{
- times_total += level_v[level].time - curr_clock();
+ lg->goal = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->goal;
+ lg->time = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->time;
- demo_play_stop(filename);
+ /* Clear other fields. */
- switch (status)
- {
- case GAME_GOAL:
- level++;
- if (limit < level)
- limit = level;
-
- level_store_hs(scores_file);
- if (!next) level--;
- break;
-
- case GAME_TIME:
- case GAME_FALL:
- if (mode == MODE_CHALLENGE)
- balls--;
- break;
- }
-
- /* Load the next level. */
+ lg->status = GAME_NONE;
+ lg->coins = 0;
+ lg->timer = lg->time;
+ lg->coin_rank = lg->goal_rank = lg->time_rank =
+ lg->score_rank = lg->times_rank = 3;
- if (status && level < count && balls >= 0)
- return level_play_go();
+ lg->win = lg->dead = lg->unlock = 0;
+ lg->next_level = NULL;
- return 0;
+ return demo_play_init(USER_REPLAY_FILE, lg->level, lg);
}
-int level_sort(int *time_i, int *coin_i)
+void level_stat(int status, int clock, int coins)
{
- int i, clock;
- char player[MAXNAM];
-
- if (mode == MODE_FREE)
- clock = curr_clock();
- else
- clock = level_v[level].time - curr_clock();
+ struct level_game *lg = curr_lg();
- config_get_s(CONFIG_PLAYER, player, MAXNAM);
+ int mode = lg->mode;
+ int timer = (mode == MODE_PRACTICE) ? clock : lg->time - clock;
- /* Insert the time record into the high score list. */
-
- strncpy(score_v[level].time_n[3], player, MAXNAM);
- score_v[level].time_c[3] = coins;
- score_v[level].time_t[3] = clock;
-
- for (i = 2; i >= 0 && score_time_comp(score_v + level, i + 1, i); i--)
- {
- score_time_swap(score_v + level, i + 1, i);
- *time_i = i;
- }
-
- /* Insert the coin record into the high score list. */
-
- strncpy(score_v[level].coin_n[3], player, MAXNAM);
- score_v[level].coin_c[3] = coins;
- score_v[level].coin_t[3] = clock;
-
- for (i = 2; i >= 0 && score_coin_comp(score_v + level, i + 1, i); i--)
- {
- score_coin_swap(score_v + level, i + 1, i);
- *coin_i = i;
- }
-
- return (*time_i < 3 || *coin_i < 3);
-}
-
-int level_done(int *time_i, int *coin_i)
-{
- int i;
char player[MAXNAM];
config_get_s(CONFIG_PLAYER, player, MAXNAM);
- /* Note a global high score. */
-
- strncpy(score_v[0].time_n[3], player, MAXNAM);
- score_v[0].time_c[3] = coins_total;
- score_v[0].time_t[3] = times_total;
+ lg->status = status;
+ lg->coins = coins;
+ lg->timer = timer;
- strncpy(score_v[0].coin_n[3], player, MAXNAM);
- score_v[0].coin_c[3] = coins_total;
- score_v[0].coin_t[3] = times_total;
-
- if (level == count)
+ if (mode == MODE_CHALLENGE)
{
- /* Insert the time record into the global high score list. */
+ /* sum time */
+ lg->times += timer;
- for (i = 2; i >= 0 && score_time_comp(score_v, i + 1, i); i--)
+ /* sum coins an earn extra balls */
+ if (status == GAME_GOAL || lg->level->is_bonus)
{
- score_time_swap(score_v, i + 1, i);
- *time_i = i;
+ lg->balls += count_extra_balls(lg->score, coins);
+ lg->score += coins;
}
- /* Insert the coin record into the global high score list. */
-
- for (i = 2; i >= 0 && score_coin_comp(score_v, i + 1, i); i--)
+ /* lose ball and game */
+ else
{
- score_coin_swap(score_v, i + 1, i);
- *coin_i = i;
+ lg->dead = (lg->balls <= 0);
+ lg->balls--;
}
}
- return (*time_i < 3 || *coin_i < 3);
+ set_finish_level(lg, player);
+
+ demo_play_stat(lg);
}
-int level_score(int n)
+void level_stop(void)
{
- int sound = AUD_COIN;
- int value = 0;
-
- coins += n;
-
- /* Pulse the coin counter based on the value of the grabbed coin. */
-
- if (n >= 10) hud_coin_pulse(2.00f);
- else if (n >= 5) hud_coin_pulse(1.50f);
- else hud_coin_pulse(1.25f);
+ demo_play_stop();
+ do_level_init = 1;
+}
- /* Check for goal open. */
+int level_next(void)
+{
+ struct level_game *lg = curr_lg();
- if (goal > 0)
- {
- if (n >= 10) hud_goal_pulse(2.00f);
- else if (n >= 5) hud_goal_pulse(1.50f);
- else hud_goal_pulse(1.25f);
+ level_stop();
+ lg->level = lg->next_level;
+ do_level_init = 0;
- if (goal - n <= 0)
- {
- sound = AUD_SWITCH;
- value = 1;
- hud_goal_pulse(2.0f);
- }
-
- goal = (goal > n) ? (goal - n) : 0;
- }
-
- audio_play(sound, 1.f);
- return value;
+ return level_play(lg->level, lg->mode);
}
-int level_count(void)
+int level_same(void)
{
- if (mode != MODE_CHALLENGE)
- return 0;
- if (coins > 0)
- {
- score++;
- coins--;
-
- if (score % 100 == 0)
- {
- balls += 1;
- audio_play(AUD_BALL, 1.0f);
- }
- return 1;
- }
- return 0;
+ level_stop();
+ do_level_init = 0;
+ return level_play(curr_lg()->level, curr_lg()->mode);
}
/*---------------------------------------------------------------------------*/
-void level_name(int i, const char *name, int time_i, int coin_i)
+int count_extra_balls(int old_score, int coins)
{
- strncpy(score_v[i].time_n[time_i], name, MAXNAM);
- strncpy(score_v[i].coin_n[coin_i], name, MAXNAM);
+ return ((old_score % 100) + coins) / 100;
}
-void level_snap(int i)
+void level_update_player_name(void)
{
- char filename[MAXSTR];
-
- /* Convert the level name to a BMP filename. */
-
- memset(filename, 0, MAXSTR);
- strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
- strcat(filename, ".bmp");
-
- /* Initialize the game for a snapshot. */
-
- if (game_init(level_v[i].file, level_v[i].back, level_v[i].grad, 0, 1))
- {
- /* Render the level and grab the screen. */
+ char player[MAXNAM];
- config_clear();
- game_set_fly(1.f);
- game_kill_fade();
- game_draw(1, 0);
- SDL_GL_SwapBuffers();
+ config_get_s(CONFIG_PLAYER, player, MAXNAM);
- image_snap(filename);
- }
+ score_change_name(curr_lg(), player);
}
/*---------------------------------------------------------------------------*/
-int level_mode(void)
+const char *status_to_str(int m)
{
- return mode;
+ switch (m)
+ {
+ case GAME_NONE: return _("Aborted");
+ case GAME_TIME: return _("Time-out");
+ case GAME_GOAL: return _("Success");
+ case GAME_FALL: return _("Fall-out");
+ default: return _("Unknown");
+ }
}
+/*---------------------------------------------------------------------------*/