Added a tweak to place the GUI into "recently moved" mode upon level end. This will...
[neverball] / ball / demo.c
index 06bd19c..9368943 100644 (file)
 #include <string.h>
 #include <time.h>
 
-#ifndef _WIN32
-#include <unistd.h>
-#endif
-
 #include "demo.h"
 #include "game.h"
 #include "audio.h"
 #include "solid.h"
 #include "config.h"
 #include "binary.h"
+#include "text.h"
 
 /*---------------------------------------------------------------------------*/
 
-#define MAGIC     0x4E425251 /* Replay file magic number (should not change) */
-#define OLD_MAGIC 0x4E425250 /* Replay file magic number for neverball 1.4.0 */
-#define REPLAY_VERSION  1    /* Replay file format version (can change)      */
+#define MAGIC           0x52424EAF
+#define DEMO_VERSION    4
 
-#define DEMO_FPS_CAP 200 /* FPS replay limit, keeps size down on monster systems */
+#define DATELEN 20
 
 static FILE *demo_fp;
 
-static struct demo demos[MAXDEMO]; /* Array of scanned demos */
-
-static int count; /* number of scanned demos */
+static struct demo demos[MAXDEMO]; /* Array of scanned demos  */
+static int         count;          /* Number of scanned demos */
 
 /*---------------------------------------------------------------------------*/
 
 void demo_dump_info(const struct demo *d)
-/* This function dump the info of a demo structure
- * It's only a function for debugging, no need of I18N */
 {
     printf("Name:         %s\n"
            "File:         %s\n"
-           "NB Version:   %s\n"
            "Time:         %d\n"
            "Coins:        %d\n"
            "Mode:         %d\n"
@@ -68,153 +60,168 @@ void demo_dump_info(const struct demo *d)
            "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->timer, d->coins, d->mode, d->status, 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);
 }
 
-/* Open a demo file, fill the demo information structure.  If success, return
- * the file pointer positioned after the header.  If fail, return NULL. */
-
-FILE *demo_header_read(const char *filename, struct demo *d)
+static time_t make_time_from_utc(struct tm *tm)
 {
-    FILE *fp;
+    struct tm local, *utc;
+    time_t t;
 
-    char *basename;
-    char buf[MAXSTR];
+    t = mktime(tm);
 
-    if ((fp = fopen(filename, FMODE_RB)))
-    {
-        int magic;
-        int version;
-        int t;
+    local = *localtime(&t);
+    utc   =  gmtime(&t);
 
-        get_index(fp, &magic);
-        get_index(fp, &version);
+    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 ;
 
-        get_index(fp, &t);
+    return mktime(&local);
+}
 
-        if (magic == MAGIC && version == REPLAY_VERSION && t)
-        {
-            d->timer = t;
-            strncpy(d->filename, filename, PATHMAX);
+static int demo_header_read(FILE *fp, struct demo *d)
+{
+    int magic;
+    int version;
+    int t;
 
-            /* Remove the directory delimiter */
+    struct tm date;
+    char datestr[DATELEN];
 
-            basename = strrchr(filename, '/');
+    get_index(fp, &magic);
+    get_index(fp, &version);
 
-#ifdef _WIN32
-            if (!basename)
-                basename = strrchr(filename, '\\');
-            else
-            {
-                char *tmp;
-                if ((tmp = strrchr(basename, '\\')))
-                    basename = tmp;
-            }
-#endif
-            strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
+    get_index(fp, &t);
 
-            /* Remove the extension */
-            t = strlen(buf) - strlen(REPLAY_EXT);
-            if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
-                buf[t] = '\0';
-            strncpy(d->name, buf, PATHMAX);
-            d->name[PATHMAX - 1] = '\0';
+    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);
-            get_index (fp, (int *)&d->date);
+        get_index(fp, &d->coins);
+        get_index(fp, &d->status);
+        get_index(fp, &d->mode);
 
-            fread(d->player, 1, MAXNAM, fp);
+        get_string(fp, d->player, MAXNAM);
 
-            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_string(fp, datestr, DATELEN);
+        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);
 
-            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);
+        /* Convert certain values to valid structure member values. */
 
-            fread(d->nb_version, 1, 20, fp);
+        date.tm_year -= 1900;
+        date.tm_mon  -= 1;
 
-            return fp;
-        }
-        fclose(fp);
+        d->date = make_time_from_utc(&date);
+
+        get_string(fp, d->shot, PATHMAX);
+        get_string(fp, d->file, PATHMAX);
+
+        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);
+
+        return 1;
     }
-    return NULL;
+    return 0;
 }
 
-static FILE *demo_header_write(struct demo *d)
-/* Create a new demo file, write 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)
 {
-    int magic = MAGIC;
-    int version = REPLAY_VERSION;
-    int zero  = 0;
+    static char buf[MAXSTR];
 
-    FILE *fp;
+    char *base;
+    size_t l;
+
+    /* Remove the directory delimiter */
 
-    if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
+    base = strrchr(name, '/');
+#ifdef _WIN32
+    if (!base)
+        base = strrchr(name, '\\');
+    else
     {
-        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);
-        put_index(fp, (int *) &d->date);
-
-        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);
-
-        return 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;
 }
 
-void demo_header_stop(FILE *fp, int coins, int timer, int state)
-/* Update the demo header using the final level state. */
+static void demo_header_write(FILE *fp, struct demo *d)
 {
-    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);
+    int magic = MAGIC;
+    int version = DEMO_VERSION;
+    int zero  = 0;
+
+    char datestr[DATELEN];
+
+    strftime(datestr, DATELEN, "%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);
+
+    put_string(fp, d->player);
+    put_string(fp, datestr);
+
+    put_string(fp, d->shot);
+    put_string(fp, d->file);
+
+    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);
 }
 
 /*---------------------------------------------------------------------------*/
 
+/* Scan another file (used by demo_scan). */
+
 static void demo_scan_file(const char *filename)
-/* Scan another file (used by demo_scan */
 {
     FILE *fp;
-    if ((fp = demo_header_read(config_user(filename), &demos[count])))
+    struct demo *d = &demos[count];
+
+    if ((fp = fopen(config_user(filename), FMODE_RB)))
     {
-        count++;
+        if (demo_header_read(fp, d))
+        {
+            strncpy(d->filename, config_user(filename), MAXSTR);
+            strncpy(d->name, bname(text_from_locale(d->filename), REPLAY_EXT),
+                    PATHMAX);
+            d->name[PATHMAX - 1] = '\0';
+
+            count++;
+        }
         fclose(fp);
     }
 }
@@ -271,7 +278,7 @@ const char *demo_pick(void)
     return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
 }
 
-const struct demo *get_demo(int i)
+const struct demo *demo_get(int i)
 {
     return (0 <= i && i < count) ? &demos[i] : NULL;
 }
@@ -279,14 +286,22 @@ const struct demo *get_demo(int i)
 const char *date_to_str(time_t i)
 {
     static char str[MAXSTR];
-    struct tm *tm = localtime(&i);
-    strftime (str, MAXSTR, "%c", tm);
-    return str;
+    const char *fmt;
+
+    /* TRANSLATORS:  here is the format of the date shown at the
+       replay selection screen (and possibly elsewhere).  The default
+       format is necessarily locale-independent.  See strftime(3) for
+       details on the format.
+     */
+
+    fmt = /* xgettext:no-c-format */ L_("%Y-%m-%d %H:%M:%S");
+    strftime(str, MAXSTR, fmt, localtime(&i));
+    return text_from_locale(str);
 }
 
 /*---------------------------------------------------------------------------*/
 
-int demo_exists(char *name)
+int demo_exists(const char *name)
 {
     FILE *fp;
     char buf[MAXSTR];
@@ -309,7 +324,7 @@ void demo_unique(char *name)
 
     for (i = 1; i < 100; i++)
     {
-        sprintf(name, _("replay%02d"), i);
+        sprintf(name, "replay%02d", i);
 
         if (!demo_exists(name))
             return;
@@ -324,9 +339,9 @@ int demo_play_init(const char *name,
 {
     struct demo demo;
 
-    memset(&demo, 0, sizeof(demo));
+    memset(&demo, 0, sizeof (demo));
 
-    strncpy(demo.filename, config_user(name), PATHMAX);
+    strncpy(demo.filename, config_user(name), MAXSTR);
     strcat(demo.filename, REPLAY_EXT);
 
     demo.mode = lg->mode;
@@ -346,65 +361,73 @@ int demo_play_init(const char *name,
     demo.balls = lg->balls;
     demo.times = lg->times;
 
-    strncpy(demo.nb_version, VERSION, 20);
-
-    demo_fp = demo_header_write(&demo);
-
-    if (demo_fp == NULL)
-        return 0;
-    else
+    if ((demo_fp = fopen(demo.filename, FMODE_WB)))
     {
+        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)
+    {
+        put_float(demo_fp, &dt);
+        put_game_state(demo_fp);
+    }
+}
 
+void demo_play_stat(const struct level_game *lg)
+{
     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;
-        }
+        long pos = ftell(demo_fp);
+
+        fseek(demo_fp, 8, SEEK_SET);
+
+        put_index(demo_fp, &lg->timer);
+        put_index(demo_fp, &lg->coins);
+        put_index(demo_fp, &lg->status);
+
+        fseek(demo_fp, pos, SEEK_SET);
     }
 }
 
-void demo_play_stop(const struct level_game *lg)
-/* Update the demo header using the final level state. */
+void demo_play_stop(void)
 {
     if (demo_fp)
     {
-        demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
         fclose(demo_fp);
         demo_fp = NULL;
     }
 }
 
-int demo_play_saved(void)
+int demo_saved(void)
 {
     return demo_exists(USER_REPLAY_FILE);
 }
 
-void demo_play_save(const char *name)
+void demo_rename(const char *name)
 {
-    char src[PATHMAX];
-    char dst[PATHMAX];
+    char src[MAXSTR];
+    char dst[MAXSTR];
 
-    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);
+        strcpy(src, config_user(USER_REPLAY_FILE));
         strcat(src, REPLAY_EXT);
-        strncpy(dst, config_user(name), PATHMAX);
+
+        strcpy(dst, config_user(name));
         strcat(dst, REPLAY_EXT);
 
+#ifdef _WIN32
+        if (demo_exists(name))
+            remove(dst);
+#endif
         rename(src, dst);
     }
 }
@@ -412,7 +435,6 @@ void demo_play_save(const char *name)
 /*---------------------------------------------------------------------------*/
 
 static int demo_load_level(const struct demo *demo, struct level *level)
-/* Load the level of the demo and fill the level structure */
 {
     if (level_load(demo->file, level))
     {
@@ -420,8 +442,7 @@ static int demo_load_level(const struct demo *demo, struct level *level)
         level->goal = demo->goal;
         return 1;
     }
-    else
-        return 0;
+    return 0;
 }
 
 static struct demo  demo_replay;       /* The current demo */
@@ -432,12 +453,19 @@ const struct demo *curr_demo_replay(void)
     return &demo_replay;
 }
 
-/* Internally load a replay and fill the lg structure (if not NULL) */
+static int demo_status = GAME_NONE;
 
 int demo_replay_init(const char *name, struct level_game *lg)
 {
-    if ((demo_fp = demo_header_read(name, &demo_replay)))
+    demo_status = GAME_NONE;
+    demo_fp     = fopen(name, FMODE_RB);
+
+    if (demo_fp && demo_header_read(demo_fp, &demo_replay))
     {
+        strncpy(demo_replay.filename, name, MAXSTR);
+        strncpy(demo_replay.name, bname(text_from_locale(demo_replay.filename),
+                REPLAY_EXT), PATHMAX);
+
         if (!demo_load_level(&demo_replay, &demo_level_replay))
             return 0;
 
@@ -450,21 +478,19 @@ int demo_replay_init(const char *name, struct level_game *lg)
             lg->goal = demo_replay.goal;
 
             /* A normal replay demo */
-            audio_music_fade_to(0.5f, demo_replay.song);
+            audio_music_fade_to(0.5f, demo_level_replay.song);
             return game_init(&demo_level_replay, demo_replay.time,
                              demo_replay.goal);
         }
-        else                    /* A title screen demo */
+        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)
     {
@@ -474,7 +500,10 @@ int demo_replay_step(float *dt)
         {
             /* Play out current game state for particles, clock, etc. */
 
-            game_step(g, *dt, &sv);
+            if (demo_status == GAME_NONE)
+                demo_status = game_step(g, *dt, 1);
+            else
+                game_step(g, *dt, 0);
 
             /* Load real current game state from file. */
 
@@ -492,7 +521,7 @@ void demo_replay_stop(int d)
         fclose(demo_fp);
         demo_fp = NULL;
 
-        if (d) unlink(demo_replay.filename);
+        if (d) remove(demo_replay.filename);
     }
 }