Those sets haven't been merged.
[neverball] / ball / demo.c
index e0ef49e..2903b7f 100644 (file)
 #include "solid.h"
 #include "config.h"
 #include "binary.h"
+#include "text.h"
 
 /*---------------------------------------------------------------------------*/
 
 #define MAGIC           0x52424EAF
-#define DEMO_VERSION    1
+#define DEMO_VERSION    5
+
+#define DATELEN 20
 
 static FILE *demo_fp;
 
@@ -40,7 +43,6 @@ 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"
@@ -58,13 +60,32 @@ 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);
 }
 
+static time_t make_time_from_utc(struct tm *tm)
+{
+    struct tm local, *utc;
+    time_t t;
+
+    t = mktime(tm);
+
+    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)
 {
     int magic;
@@ -72,7 +93,7 @@ static int demo_header_read(FILE *fp, struct demo *d)
     int t;
 
     struct tm date;
-    char datestr[20];
+    char datestr[DATELEN];
 
     get_index(fp, &magic);
     get_index(fp, &version);
@@ -84,13 +105,12 @@ static int demo_header_read(FILE *fp, struct demo *d)
         d->timer = t;
 
         get_index(fp, &d->coins);
-        get_index(fp, &d->state);
+        get_index(fp, &d->status);
         get_index(fp, &d->mode);
 
-#if 0
-        get_index(fp, (int *) &d->date);
-#endif
-        fread(datestr, 1, 20, fp);
+        get_string(fp, d->player, MAXNAM);
+
+        get_string(fp, datestr, DATELEN);
         sscanf(datestr,
                "%d-%d-%dT%d:%d:%d",
                &date.tm_year,
@@ -100,20 +120,15 @@ static int demo_header_read(FILE *fp, struct demo *d)
                &date.tm_min,
                &date.tm_sec);
 
-        /* Convert some values to valid structure member values. */
+        /* Convert certain values to valid structure member values. */
 
         date.tm_year -= 1900;
         date.tm_mon  -= 1;
 
-        d->date = mktime(&date);
+        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_string(fp, d->shot, PATHMAX);
+        get_string(fp, d->file, PATHMAX);
 
         get_index(fp, &d->time);
         get_index(fp, &d->goal);
@@ -121,56 +136,40 @@ static int demo_header_read(FILE *fp, struct demo *d)
         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_open(const char *filename, struct demo *d)
+static char *bname(const char *name, const char *suffix)
 {
-    FILE *fp;
-
-    if ((fp = fopen(filename, FMODE_RB)))
-    {
-        if (demo_header_read(fp, d))
-        {
-            char buf[MAXSTR];
-            char *basename;
-            int l;
+    static char buf[MAXSTR];
 
-            strncpy(d->filename, filename, MAXSTR);
+    char *base;
+    size_t l;
 
-            /* Remove the directory delimiter */
+    /* Remove the directory delimiter */
 
-            basename = strrchr(filename, '/');
+    base = strrchr(name, '/');
 #ifdef _WIN32
-            if (!basename)
-                basename = strrchr(filename, '\\');
-            else
-            {
-                char *tmp;
-                if ((tmp = strrchr(basename, '\\')))
-                    basename = tmp;
-            }
+    if (!base)
+        base = strrchr(name, '\\');
+    else
+    {
+        char *tmp;
+        if ((tmp = strrchr(base, '\\')))
+            base = tmp;
+    }
 #endif
-            strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
+    strncpy(buf, base ? base + 1 : name, MAXSTR);
 
-            /* Remove the extension */
+    /* Remove the extension */
 
-            l = strlen(buf) - strlen(REPLAY_EXT);
-            if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
-                buf[l] = '\0';
+    l = strlen(buf) - strlen(suffix);
+    if ((l > 1) && (strcmp(buf + l, suffix) == 0))
+        buf[l] = '\0';
 
-            strncpy(d->name, buf, PATHMAX);
-            d->name[PATHMAX - 1] = '\0';
-
-            return fp;
-        }
-        fclose(fp);
-    }
-    return NULL;
+    return buf;
 }
 
 static void demo_header_write(FILE *fp, struct demo *d)
@@ -179,9 +178,9 @@ static void demo_header_write(FILE *fp, struct demo *d)
     int version = DEMO_VERSION;
     int zero  = 0;
 
-    char datestr[20];
+    char datestr[DATELEN];
 
-    strftime(datestr, 20, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
+    strftime(datestr, DATELEN, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
 
     put_index(fp, &magic);
     put_index(fp, &version);
@@ -189,39 +188,18 @@ static void demo_header_write(FILE *fp, struct demo *d)
     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);
+    put_string(fp, d->player);
+    put_string(fp, datestr);
 
-    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_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);
-
-    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);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -233,9 +211,17 @@ static void demo_scan_file(const char *filename)
     FILE *fp;
     struct demo *d = &demos[count];
 
-    if ((fp = demo_header_open(config_user(filename), d)))
+    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);
     }
 }
@@ -300,33 +286,22 @@ const struct demo *demo_get(int i)
 const char *date_to_str(time_t i)
 {
     static char str[MAXSTR];
-    struct tm local, *utc;
+    const char *fmt;
 
-    /* Replay date/time is stored as UTC.  The code below computes the actual
-     * local time.  Needless to say, this is an ugly hack...
+    /* 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.
      */
 
-    local = *localtime(&i);
-    utc   =  gmtime(&i);
-
-    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 ;
-
-    /* Normalize values. */
-    i = mktime(&local);
-
-    strftime(str, MAXSTR, "%c", localtime(&i));
-
-    return str;
+    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];
@@ -349,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;
@@ -386,9 +361,7 @@ int demo_play_init(const char *name,
     demo.balls = lg->balls;
     demo.times = lg->times;
 
-    strncpy(demo.nb_version, VERSION, 20);
-
-    if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
+    if ((demo_fp = fopen(demo.filename, FMODE_WB)))
     {
         demo_header_write(demo_fp, &demo);
         audio_music_fade_to(2.0f, level->song);
@@ -397,45 +370,61 @@ int demo_play_init(const char *name,
     return 0;
 }
 
-void demo_play_step(float dt)
+void demo_play_step()
+{
+    if (demo_fp)
+        input_put(demo_fp);
+}
+
+void demo_play_stat(const struct level_game *lg)
 {
     if (demo_fp)
     {
-        put_float(demo_fp, &dt);
-        put_game_state(demo_fp);
+        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);
     }
 }
 
-/* Update the demo header using the final level state. */
-
-void demo_play_stop(const struct level_game *lg)
+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);
     }
 }
@@ -461,27 +450,32 @@ 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)
 {
-    demo_fp = demo_header_open(name, &demo_replay);
+    demo_status = GAME_NONE;
+    demo_fp     = fopen(name, FMODE_RB);
 
-    if (demo_fp)
+    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;
 
         if (lg)
         {
-            lg->mode = demo_replay.mode;
+            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;
+            lg->time  = demo_replay.time;
+            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);
         }
@@ -491,25 +485,28 @@ int demo_replay_init(const char *name, struct level_game *lg)
     return 0;
 }
 
-int demo_replay_step(float *dt)
+int demo_replay_step(float dt)
 {
-    const float g[3] = { 0.0f, -9.8f, 0.0f };
-    int sv;
+    const float gdn[3] = { 0.0f, -9.8f, 0.0f };
+    const float gup[3] = { 0.0f, +9.8f, 0.0f };
 
     if (demo_fp)
     {
-        get_float(demo_fp, dt);
-
-        if (feof(demo_fp) == 0)
+        if (input_get(demo_fp))
         {
-            /* Play out current game state for particles, clock, etc. */
-
-            game_step(g, *dt, &sv);
+            /* Play out current game state. */
 
-            /* Load real current game state from file. */
+            switch (demo_status)
+            {
+            case GAME_NONE:
+                demo_status = game_step(gdn, dt, 1); break;
+            case GAME_GOAL:
+                (void)        game_step(gup, dt, 0); break;
+            default:
+                (void)        game_step(gdn, dt, 0); break;
+            }
 
-            if (get_game_state(demo_fp))
-                return 1;
+            return 1;
         }
     }
     return 0;