-/*
+/*
* Copyright (C) 2003 Robert Kooima
*
* NEVERBALL is free software; you can redistribute it and/or modify
#include <string.h>
#include <time.h>
-#ifndef _WIN32
-#include <unistd.h>
-#endif
-
-#include "set.h"
#include "demo.h"
#include "game.h"
-#include "level.h"
#include "audio.h"
#include "solid.h"
#include "config.h"
/*---------------------------------------------------------------------------*/
-#define MAGIC 0x4E425252
-#define DEMO_FPS_CAP 200 /* FPS replay limit, keeps size down on monster systems */
+#define MAGIC 0x52424EAF
+#define DEMO_VERSION 1
static FILE *demo_fp;
+static struct demo demos[MAXDEMO]; /* Array of scanned demos */
+static int count; /* Number of scanned demos */
+
+/*---------------------------------------------------------------------------*/
+
+void demo_dump_info(const struct demo *d)
+{
+ printf("Name: %s\n"
+ "File: %s\n"
+ "NB Version: %s\n"
+ "Time: %d\n"
+ "Coins: %d\n"
+ "Mode: %d\n"
+ "State: %d\n"
+ "Date: %s"
+ "Player: %s\n"
+ "Shot: %s\n"
+ "Level: %s\n"
+ " Back: %s\n"
+ " Grad: %s\n"
+ " Song: %s\n"
+ "Time: %d\n"
+ "Goal: %d\n"
+ "Score: %d\n"
+ "Balls: %d\n"
+ "Total Time: %d\n",
+ d->name, d->filename,
+ d->nb_version,
+ d->timer, d->coins, d->mode, d->state, ctime(&d->date),
+ d->player,
+ d->shot, d->file, d->back, d->grad, d->song,
+ d->time, d->goal, d->score, d->balls, d->times);
+}
-/* Demo information structure (header) */
-struct demo
+static time_t make_time_from_utc(struct tm *tm)
{
- char name[MAXNAM]; /* demo filename */
-
- /* magic */
- int timer; /* elapsed time */
- int coins; /* coin number */
- int state; /* how the replay end */
- int mode; /* game mode */
- time_t date; /* date of creation */
- char player[MAXNAM]; /* player name */
- char shot[PATHMAX]; /* image filename */
- char file[PATHMAX]; /* level filename */
- char back[PATHMAX]; /* level bg filename */
- char grad[PATHMAX]; /* level gradiant filename */
- char song[PATHMAX]; /* level song filename */
- int time; /* time limit (! training mode) */
- int goal; /* coin to open the goal (! training mode) */
- int score; /* sum of coins (challenge mode) */
- int balls; /* number of balls (challenge mode) */
- int total_time; /* total time (challenge mode) */
-};
-
-
-static struct demo demos[MAXDEMO]; /* Array of scanned demos */
-
-static int count; /* number of scanned demos */
+ struct tm local, *utc;
+ time_t t;
-/*---------------------------------------------------------------------------*/
+ t = mktime(tm);
-void demo_dump_info(struct demo * d)
-/* This function dump the info of a demo structure*/
+ local = *localtime(&t);
+ utc = gmtime(&t);
+
+ local.tm_year += local.tm_year - utc->tm_year;
+ local.tm_mon += local.tm_mon - utc->tm_mon ;
+ local.tm_mday += local.tm_mday - utc->tm_mday;
+ local.tm_hour += local.tm_hour - utc->tm_hour;
+ local.tm_min += local.tm_min - utc->tm_min ;
+ local.tm_sec += local.tm_sec - utc->tm_sec ;
+
+ return mktime(&local);
+}
+
+static int demo_header_read(FILE *fp, struct demo *d)
{
- printf("Filename: %s\n"
- "Time: %d\n"
- "Coins: %d\n"
- "Mode: %d\n"
- "State: %d\n"
- "Date: %s"
- "Player: %s\n"
- "Shot: %s\n"
- "Level: %s\n"
- " Back: %s\n"
- " Grad: %s\n"
- " Song: %s\n"
- "Time: %d\n"
- "Goal: %d\n"
- "Score: %d\n"
- "Balls: %d\n"
- "Tot Time: %d\n",
- d->name,
- d->timer, d->coins, d->mode, d->state, ctime(&d->date),
- d->player,
- d->shot, d->file, d->back, d->grad, d->song,
- d->time, d->goal, d->score, d->balls, d->total_time);
+ int magic;
+ int version;
+ int t;
+
+ struct tm date;
+ char datestr[20];
+
+ get_index(fp, &magic);
+ get_index(fp, &version);
+
+ get_index(fp, &t);
+
+ if (magic == MAGIC && version == DEMO_VERSION && t)
+ {
+ d->timer = t;
+
+ get_index(fp, &d->coins);
+ get_index(fp, &d->state);
+ get_index(fp, &d->mode);
+
+#if 0
+ get_index(fp, (int *) &d->date);
+#endif
+ fread(datestr, 1, 20, fp);
+ sscanf(datestr,
+ "%d-%d-%dT%d:%d:%d",
+ &date.tm_year,
+ &date.tm_mon,
+ &date.tm_mday,
+ &date.tm_hour,
+ &date.tm_min,
+ &date.tm_sec);
+
+ /* Convert certain values to valid structure member values. */
+
+ date.tm_year -= 1900;
+ date.tm_mon -= 1;
+
+ d->date = make_time_from_utc(&date);
+
+ fread(d->player, 1, MAXNAM, fp);
+
+ fread(d->shot, 1, PATHMAX, fp);
+ fread(d->file, 1, PATHMAX, fp);
+ fread(d->back, 1, PATHMAX, fp);
+ fread(d->grad, 1, PATHMAX, fp);
+ fread(d->song, 1, PATHMAX, fp);
+
+ get_index(fp, &d->time);
+ get_index(fp, &d->goal);
+ get_index(fp, &d->score);
+ get_index(fp, &d->balls);
+ get_index(fp, &d->times);
+
+ fread(d->nb_version, 1, 20, fp);
+
+ return 1;
+ }
+ return 0;
}
-static FILE * demo_header(const char * filename, struct demo * d)
-/* Open a demo file, fill the demo information structure
-If success, return the file pointer positioned after the header
-If fail, return null */
+static char *bname(const char *name, const char *suffix)
{
- FILE *fp;
- if ((fp = fopen(config_user(filename), FMODE_RB)))
+ static char buf[MAXSTR];
+
+ char *base;
+ size_t l;
+
+ /* Remove the directory delimiter */
+
+ base = strrchr(name, '/');
+#ifdef _WIN32
+ if (!base)
+ base = strrchr(name, '\\');
+ else
{
- int magic;
- int t;
-
- get_index(fp, &magic);
- get_index(fp, &t);
-
- if (magic == MAGIC && t)
- {
- strncpy(d->name, filename, MAXNAM);
- d->timer = t;
- get_index(fp, &d->coins);
- get_index(fp, &d->state);
- get_index(fp, &d->mode);
- get_index(fp, (int*)&d->date);
- fread(d->player, 1, MAXNAM, fp);
- fread(d->shot, 1, PATHMAX, fp);
- fread(d->file, 1, PATHMAX, fp);
- fread(d->back, 1, PATHMAX, fp);
- fread(d->grad, 1, PATHMAX, fp);
- fread(d->song, 1, PATHMAX, fp);
- get_index(fp, &d->time);
- get_index(fp, &d->goal);
- get_index(fp, &d->score);
- get_index(fp, &d->balls);
- get_index(fp, &d->total_time);
-
- return fp;
- }
- fclose(fp);
+ char *tmp;
+ if ((tmp = strrchr(base, '\\')))
+ base = tmp;
}
- return NULL;
+#endif
+ strncpy(buf, base ? base + 1 : name, MAXSTR);
+
+ /* Remove the extension */
+
+ l = strlen(buf) - strlen(suffix);
+ if ((l > 1) && (strcmp(buf + l, suffix) == 0))
+ buf[l] = '\0';
+
+ return buf;
+}
+
+static void demo_header_write(FILE *fp, struct demo *d)
+{
+ int magic = MAGIC;
+ int version = DEMO_VERSION;
+ int zero = 0;
+
+ char datestr[20];
+
+ strftime(datestr, 20, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
+
+ put_index(fp, &magic);
+ put_index(fp, &version);
+ put_index(fp, &zero);
+ put_index(fp, &zero);
+ put_index(fp, &zero);
+ put_index(fp, &d->mode);
+
+#if 0
+ put_index(fp, (int *) &d->date);
+#endif
+ fwrite(datestr, 1, 20, fp);
+
+ fwrite(d->player, 1, MAXNAM, fp);
+
+ fwrite(d->shot, 1, PATHMAX, fp);
+ fwrite(d->file, 1, PATHMAX, fp);
+ fwrite(d->back, 1, PATHMAX, fp);
+ fwrite(d->grad, 1, PATHMAX, fp);
+ fwrite(d->song, 1, PATHMAX, fp);
+
+ put_index(fp, &d->time);
+ put_index(fp, &d->goal);
+ put_index(fp, &d->score);
+ put_index(fp, &d->balls);
+ put_index(fp, &d->times);
+
+ fwrite(d->nb_version, 1, 20, fp);
+}
+
+/* Update the demo header using the final level state. */
+
+void demo_header_stop(FILE *fp, int coins, int timer, int state)
+{
+ long pos = ftell(fp);
+
+ fseek(fp, 8, SEEK_SET);
+ put_index(fp, &timer);
+ put_index(fp, &coins);
+ put_index(fp, &state);
+ fseek(fp, pos, SEEK_SET);
}
/*---------------------------------------------------------------------------*/
-static void demo_scan_file(const char * filename)
-/* Scan a other file (used by demo_scan */
+/* Scan another file (used by demo_scan). */
+
+static void demo_scan_file(const char *filename)
{
FILE *fp;
- if ((fp = demo_header(filename, &demos[count])))
+ struct demo *d = &demos[count];
+
+ if ((fp = fopen(config_user(filename), FMODE_RB)))
{
- count++;
- fclose(fp);
+ if (demo_header_read(fp, d))
+ {
+ strncpy(d->filename, config_user(filename), MAXSTR);
+ strncpy(d->name, bname(filename, REPLAY_EXT), PATHMAX);
+ d->name[PATHMAX - 1] = '\0';
+
+ count++;
+ }
+ fclose(fp);
}
}
if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
{
do
- demo_scan_file(d.cFileName);
+ demo_scan_file(d.cFileName);
while (count < MAXDEMO && FindNextFile(h, &d));
FindClose(h);
if ((dp = opendir(config_user(""))))
{
while (count < MAXDEMO && (ent = readdir(dp)))
- demo_scan_file(ent->d_name);
+ demo_scan_file(ent->d_name);
closedir(dp);
}
{
int n = demo_scan();
- return (n > 0) ? demos[(rand() >> 4) % n].name : NULL;
-}
-
-const char *demo_name(int i)
-{
- return (0 <= i && i < count) ? demos[i].name : NULL;
-}
-
-const char *demo_shot(int i)
-{
- return (0 <= i && i < count) ? demos[i].shot : NULL;
-}
-
-int demo_coins(int i)
-{
- return (0 <= i && i < count) ? demos[i].coins : 0;
-}
-
-int demo_state(int i)
-{
- return (0 <= i && i < count) ? demos[i].state : 0;
-}
-
-int demo_mode(int i)
-{
- return (0 <= i && i < count) ? demos[i].mode : 0;
-}
-
-int demo_clock(int i)
-{
- return (0 <= i && i < count) ? demos[i].timer : 0;
+ return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
}
-time_t demo_date(int i)
+const struct demo *demo_get(int i)
{
- return (0 <= i && i < count) ? demos[i].date : 0;
+ return (0 <= i && i < count) ? &demos[i] : NULL;
}
-const char * demo_str_date(int i)
+const char *date_to_str(time_t i)
{
- time_t d = demo_date(i);
- char * res = ctime(&d);
- char * n = strchr(res, '\n');
- if (n) *n = '\0';
- return res;
-}
+ static char str[MAXSTR];
-const char * demo_player(int i)
-{
- return (0 <= i && i < count) ? demos[i].player : "";
+ strftime(str, MAXSTR, "%c", localtime(&i));
+ return str;
}
/*---------------------------------------------------------------------------*/
int demo_exists(char *name)
{
FILE *fp;
+ char buf[MAXSTR];
- if ((fp = fopen(config_user(name), "r")))
+ strcpy(buf, config_user(name));
+ strcat(buf, REPLAY_EXT);
+ if ((fp = fopen(buf, "r")))
{
fclose(fp);
return 1;
/*---------------------------------------------------------------------------*/
int demo_play_init(const char *name,
- const char *file,
- const char *back,
- const char *grad,
- const char *song,
- const char *shot,
- int mode, int tim, int goal, int score, int balls, int total_time)
+ const struct level *level,
+ const struct level_game *lg)
{
- int magic = MAGIC;
- int zero = 0;
- int date = time(NULL);
+ struct demo demo;
+
+ memset(&demo, 0, sizeof (demo));
+
+ strncpy(demo.filename, config_user(name), MAXSTR);
+ strcat(demo.filename, REPLAY_EXT);
- char player[MAXNAM];
- config_get_s(CONFIG_PLAYER, player, MAXNAM);
+ demo.mode = lg->mode;
+ demo.date = time(NULL);
- /* Initialize the replay file. Write the header. */
+ config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
- if (name && (demo_fp = fopen(config_user(name), FMODE_WB)))
+ strncpy(demo.shot, level->shot, PATHMAX);
+ strncpy(demo.file, level->file, PATHMAX);
+ strncpy(demo.back, level->back, PATHMAX);
+ strncpy(demo.grad, level->grad, PATHMAX);
+ strncpy(demo.song, level->song, PATHMAX);
+
+ demo.time = lg->time;
+ demo.goal = lg->goal;
+ demo.score = lg->score;
+ demo.balls = lg->balls;
+ demo.times = lg->times;
+
+ strncpy(demo.nb_version, VERSION, 20);
+
+ if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
{
- put_index(demo_fp, &magic);
- put_index(demo_fp, &zero);
- put_index(demo_fp, &zero);
- put_index(demo_fp, &zero);
- put_index(demo_fp, &mode);
- put_index(demo_fp, &date);
-
- fwrite(player, 1, MAXNAM, demo_fp);
-
- fwrite(shot, 1, PATHMAX, demo_fp);
- fwrite(file, 1, PATHMAX, demo_fp);
- fwrite(back, 1, PATHMAX, demo_fp);
- fwrite(grad, 1, PATHMAX, demo_fp);
- fwrite(song, 1, PATHMAX, demo_fp);
-
- put_index(demo_fp, &tim);
- put_index(demo_fp, &goal);
- put_index(demo_fp, &score);
- put_index(demo_fp, &balls);
- put_index(demo_fp, &total_time);
-
- audio_music_fade_to(2.0f, song);
-
- return game_init(file, back, grad, tim, (goal == 0));
+ demo_header_write(demo_fp, &demo);
+ audio_music_fade_to(2.0f, level->song);
+ return game_init(level, lg->time, lg->goal);
}
return 0;
}
void demo_play_step(float dt)
{
- static float fps_track = 0.0f;
- static float fps_cap = 1.0f / (float) DEMO_FPS_CAP;
-
if (demo_fp)
{
- fps_track += dt;
- if (fps_track > fps_cap)
- {
- put_float(demo_fp, &fps_track);
- put_game_state(demo_fp);
- fps_track = 0.0f;
- }
+ put_float(demo_fp, &dt);
+ put_game_state(demo_fp);
}
}
-void demo_play_stop(int coins, int timer, int state)
+/* Update the demo header using the final level state. */
+
+void demo_play_stop(const struct level_game *lg)
{
if (demo_fp)
{
- /* Update the demo header using the final coins and time. */
-
- fseek(demo_fp, 4, SEEK_SET);
-
- put_index(demo_fp, &timer);
- put_index(demo_fp, &coins);
- put_index(demo_fp, &state);
-
- fclose(demo_fp);
- demo_fp = NULL;
+ demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
+ fclose(demo_fp);
+ demo_fp = NULL;
}
}
+int demo_play_saved(void)
+{
+ return demo_exists(USER_REPLAY_FILE);
+}
+
void demo_play_save(const char *name)
{
char src[PATHMAX];
char dst[PATHMAX];
- if (name && demo_exists(USER_REPLAY_FILE) && strcmp(name, USER_REPLAY_FILE) != 0)
+ if (name && demo_exists(USER_REPLAY_FILE)
+ && strcmp(name, USER_REPLAY_FILE) != 0)
{
strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
- strncpy(dst, config_user(name), PATHMAX);
+ strcat(src, REPLAY_EXT);
+ strncpy(dst, config_user(name), PATHMAX);
+ strcat(dst, REPLAY_EXT);
- rename(src, dst);
+#ifdef _WIN32
+ if (demo_exists(name))
+ remove(dst);
+#endif
+ rename(src, dst);
}
}
/*---------------------------------------------------------------------------*/
-static char demo_replay_name[MAXSTR];
+static int demo_load_level(const struct demo *demo, struct level *level)
+{
+ if (level_load(demo->file, level))
+ {
+ level->time = demo->time;
+ level->goal = demo->goal;
+ return 1;
+ }
+ return 0;
+}
+
+static struct demo demo_replay; /* The current demo */
+static struct level demo_level_replay; /* The current level demo-ed*/
+
+const struct demo *curr_demo_replay(void)
+{
+ return &demo_replay;
+}
-int demo_replay_init(const char *name, int *m, int *s, int *c, int *g, int *t)
+/* Internally load a replay and fill the lg structure (if not NULL) */
+
+int demo_replay_init(const char *name, struct level_game *lg)
{
- struct demo d;
+ demo_fp = fopen(name, FMODE_RB);
- if ((demo_fp = demo_header(name, &d)))
+ if (demo_fp && demo_header_read(demo_fp, &demo_replay))
{
- strncpy(demo_replay_name, name, MAXSTR);
- if (m) *m = d.mode;
- if (s) *s = d.score;
- if (g) *g = d.goal;
- if (t) *t = d.total_time;
-
- if (g)
- {
- audio_music_fade_to(0.5f, d.song);
- return game_init(d.file, d.back, d.grad, d.time, (d.goal == 0));
- }
- else
- return game_init(d.file, d.back, d.grad, d.time, 1);
+ strncpy(demo_replay.filename, name, MAXSTR);
+ strncpy(demo_replay.name, bname(name, REPLAY_EXT), PATHMAX);
+
+ if (!demo_load_level(&demo_replay, &demo_level_replay))
+ return 0;
+
+ if (lg)
+ {
+ lg->mode = demo_replay.mode;
+ lg->score = demo_replay.score;
+ lg->times = demo_replay.times;
+ lg->time = demo_replay.time;
+ lg->goal = demo_replay.goal;
+
+ /* A normal replay demo */
+ audio_music_fade_to(0.5f, demo_replay.song);
+ return game_init(&demo_level_replay, demo_replay.time,
+ demo_replay.goal);
+ }
+ else /* A title screen demo */
+ return game_init(&demo_level_replay, demo_replay.time, 0);
}
-
return 0;
}
int demo_replay_step(float *dt)
{
const float g[3] = { 0.0f, -9.8f, 0.0f };
+ int sv;
if (demo_fp)
{
{
/* Play out current game state for particles, clock, etc. */
- game_step(g, *dt, 1);
+ game_step(g, *dt, &sv);
/* Load real current game state from file. */
-
+
if (get_game_state(demo_fp))
return 1;
}
fclose(demo_fp);
demo_fp = NULL;
- if (d) unlink(config_user(demo_replay_name));
+ if (d) remove(demo_replay.filename);
}
}
+void demo_replay_dump_info(void)
+{
+ demo_dump_info(&demo_replay);
+}
+
/*---------------------------------------------------------------------------*/