Retab ball/level.h.
[neverball] / ball / demo.c
index 235c525..ecb9304 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * 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 <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 0x4E425251
+#define MAGIC           0x52424EAF
+#define DEMO_VERSION    1
 
 static FILE *demo_fp;
 
-static char name[MAXDEMO][MAXNAM];
-static char shot[MAXDEMO][PATHMAX];
-static int  score[MAXDEMO];
-static int  timer[MAXDEMO];
-static int  count;
+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);
+}
+
+static int demo_header_read(FILE *fp, struct demo *d)
+{
+    int magic;
+    int version;
+    int t;
+
+    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);
+        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->times);
+
+        fread(d->nb_version, 1, 20, fp);
+
+        return 1;
+    }
+    return 0;
+}
+
+static void demo_header_write(FILE *fp, struct demo *d)
+{
+    int magic = MAGIC;
+    int version = DEMO_VERSION;
+    int zero  = 0;
+
+    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);
+}
+
+/* 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);
+}
 
 /*---------------------------------------------------------------------------*/
+
+/* Scan another file (used by demo_scan). */
+
+static void demo_scan_file(const char *filename)
+{
+    FILE *fp;
+    struct demo *d = &demos[count];
+
+    if ((fp = fopen(config_user(filename), FMODE_RB)))
+    {
+        if (demo_header_read(fp, d))
+        {
+            char buf[PATHMAX];
+            int l;
+
+            strncpy(d->filename, config_user(filename), MAXSTR);
+
+            strncpy(buf, filename, PATHMAX);
+            l = strlen(buf) - strlen(REPLAY_EXT);
+
+            if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
+                buf[l] = '\0';
+
+            strncpy(d->name, buf, PATHMAX);
+            d->name[PATHMAX - 1] = '\0';
+
+            count++;
+        }
+        fclose(fp);
+    }
+}
+
 #ifdef _WIN32
 
 int demo_scan(void)
 {
     WIN32_FIND_DATA d;
     HANDLE h;
-    FILE *fp;
 
     count = 0;
 
@@ -57,27 +197,7 @@ int demo_scan(void)
     if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
     {
         do
-            if ((fp = fopen(config_user(d.cFileName), FMODE_RB)))
-            {
-                int magic;
-                int t, c;
-
-                /* Note the name and screen shot of each replay file. */
-
-                get_index(fp, &magic);
-                get_index(fp, &t);
-                get_index(fp, &c);
-
-                if (magic == MAGIC && t)
-                {
-                    fread(shot[count], 1, PATHMAX, fp);
-                    timer[count] = t;
-                    score[count] = c;
-                    strncpy(name[count], d.cFileName, MAXNAM);
-                    count++;
-                }
-                fclose(fp);
-            }
+            demo_scan_file(d.cFileName);
         while (count < MAXDEMO && FindNextFile(h, &d));
 
         FindClose(h);
@@ -91,7 +211,6 @@ int demo_scan(void)
 int demo_scan(void)
 {
     struct dirent *ent;
-    FILE *fp;
     DIR  *dp;
 
     count = 0;
@@ -101,27 +220,7 @@ int demo_scan(void)
     if ((dp = opendir(config_user(""))))
     {
         while (count < MAXDEMO && (ent = readdir(dp)))
-            if ((fp = fopen(config_user(ent->d_name), FMODE_RB)))
-            {
-                int magic;
-                int t, c;
-
-                /* Note the name and screen shot of each replay file. */
-
-                get_index(fp, &magic);
-                get_index(fp, &t);
-                get_index(fp, &c);
-
-                if (magic == MAGIC && t)
-                {
-                    fread(shot[count], 1, PATHMAX, fp);
-                    timer[count] = t;
-                    score[count] = c;
-                    strncpy(name[count], ent->d_name, MAXNAM);
-                    count++;
-                }
-                fclose(fp);
-            }
+            demo_scan_file(ent->d_name);
 
         closedir(dp);
     }
@@ -133,27 +232,20 @@ const char *demo_pick(void)
 {
     int n = demo_scan();
 
-    return (n > 0) ? name[(rand() >> 4) % n] : NULL;
+    return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
 }
 
-const char *demo_name(int i)
+const struct demo *demo_get(int i)
 {
-    return (0 <= i && i < count) ? name[i] : NULL;
+    return (0 <= i && i < count) ? &demos[i] : NULL;
 }
 
-const char *demo_shot(int i)
+const char *date_to_str(time_t i)
 {
-    return (0 <= i && i < count) ? shot[i] : NULL;
-}
-
-int demo_coins(int i)
-{
-    return (0 <= i && i < count) ? score[i] : 0;
-}
-
-int demo_clock(int i)
-{
-    return (0 <= i && i < count) ? timer[i] : 0;
+    static char str[MAXSTR];
+    struct tm *tm = localtime(&i);
+    strftime(str, MAXSTR, "%c", tm);
+    return str;
 }
 
 /*---------------------------------------------------------------------------*/
@@ -161,8 +253,11 @@ int demo_clock(int i)
 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;
@@ -178,7 +273,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;
@@ -188,44 +283,40 @@ void demo_unique(char *name)
 /*---------------------------------------------------------------------------*/
 
 int demo_play_init(const char *name,
-                   const char *file,
-                   const char *back,
-                   const char *grad,
-                   const char *song,
-                   const char *shot,
-                   int t, int g, int s, int c, int b)
+                   const struct level *level,
+                   const struct level_game *lg)
 {
-    int magic = MAGIC;
-    int zero  = 0;
-    int st    = t;
-    int sg    = g;
-    int ss    = s;
-    int sc    = c;
-    int sb    = b;
+    struct demo demo;
 
-    /* Initialize the replay file.  Write the header. */
+    memset(&demo, 0, sizeof (demo));
 
-    if (name && (demo_fp = fopen(config_user(name), FMODE_WB)))
-    {
-        put_index(demo_fp, &magic);
-        put_index(demo_fp, &zero);
-        put_index(demo_fp, &zero);
+    strncpy(demo.filename, config_user(name), MAXSTR);
+    strcat(demo.filename, REPLAY_EXT);
+
+    demo.mode = lg->mode;
+    demo.date = time(NULL);
 
-        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);
+    config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
 
-        put_index(demo_fp, &st);
-        put_index(demo_fp, &sg);
-        put_index(demo_fp, &ss);
-        put_index(demo_fp, &sc);
-        put_index(demo_fp, &sb);
+    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);
 
-        audio_music_fade_to(2.0f, song);
+    demo.time  = lg->time;
+    demo.goal  = lg->goal;
+    demo.score = lg->score;
+    demo.balls = lg->balls;
+    demo.times = lg->times;
 
-        return game_init(file, back, grad, t, (g == 0));
+    strncpy(demo.nb_version, VERSION, 20);
+
+    if (demo.filename && (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;
 }
@@ -239,103 +330,89 @@ void demo_play_step(float dt)
     }
 }
 
-void demo_play_stat(int coins, int timer)
-{
-    int c = coins;
-    int t = timer;
+/* 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 score and time. */
-
-        fseek(demo_fp, 4, SEEK_SET);
-
-        put_index(demo_fp, &t);
-        put_index(demo_fp, &c);
-
-        fseek(demo_fp, 0, SEEK_END);
+        demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
+        fclose(demo_fp);
+        demo_fp = NULL;
     }
 }
 
-void demo_play_stop(const char *name)
+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 (demo_fp)
+    if (name && demo_exists(USER_REPLAY_FILE)
+        && strcmp(name, USER_REPLAY_FILE) != 0)
     {
-        fclose(demo_fp);
-        demo_fp = NULL;
+        strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
+        strcat(src, REPLAY_EXT);
+        strncpy(dst, config_user(name), PATHMAX);
+        strcat(dst, REPLAY_EXT);
 
-        /* Rename the temporary replay file to its given name. */
+        rename(src, dst);
+    }
+}
 
-        if (name)
-        {
-            strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
-            strncpy(dst, config_user(name),             PATHMAX);
+/*---------------------------------------------------------------------------*/
 
-            rename(src, dst);
-        }
+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*/
 
-static char demo_replay_name[MAXSTR];
+const struct demo *curr_demo_replay(void)
+{
+    return &demo_replay;
+}
+
+/* Internally load a replay and fill the lg structure (if not NULL) */
 
-int demo_replay_init(const char *name, int *s, int *c, int *b, int *g)
+int demo_replay_init(const char *name, struct level_game *lg)
 {
-    char shot[PATHMAX];
-    char file[PATHMAX];
-    char back[PATHMAX];
-    char grad[PATHMAX];
-    char song[PATHMAX];
+    demo_fp = fopen(name, FMODE_RB);
 
-    int magic;
-    int zero;
-    int st;
-    int sg;
-    int ss;
-    int sc;
-    int sb;
-    
-    if ((demo_fp = fopen(config_user(name), FMODE_RB)))
+    if (demo_fp && demo_header_read(demo_fp, &demo_replay))
     {
-        strncpy(demo_replay_name, name, MAXSTR);
-
-        get_index(demo_fp, &magic);
-
-        if (magic == MAGIC)
-            {
-            get_index(demo_fp, &zero);
-            get_index(demo_fp, &zero);
-
-            fread(shot, 1, PATHMAX, demo_fp);
-            fread(file, 1, PATHMAX, demo_fp);
-            fread(back, 1, PATHMAX, demo_fp);
-            fread(grad, 1, PATHMAX, demo_fp);
-            fread(song, 1, PATHMAX, demo_fp);
-
-            get_index(demo_fp, &st);
-            get_index(demo_fp, &sg);
-            get_index(demo_fp, &ss);
-            get_index(demo_fp, &sc);
-            get_index(demo_fp, &sb);
-
-            if (g) *g = (int) sg;
-            if (s) *s = (int) ss;
-            if (c) *c = (int) sc;
-            if (b) *b = (int) sb;
-
-            if (g)
-            {
-                audio_music_fade_to(0.5f, song);
-                return game_init(file, back, grad, st, (*g == 0));
-            }
-            else
-                return game_init(file, back, grad, st, 1);
+        strncpy(demo_replay.filename, name, MAXSTR);
+
+        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);
         }
-        fclose(demo_fp);
+        else /* A title screen demo */
+            return game_init(&demo_level_replay, demo_replay.time, 0);
     }
     return 0;
 }
@@ -343,6 +420,7 @@ int demo_replay_init(const char *name, int *s, int *c, int *b, int *g)
 int demo_replay_step(float *dt)
 {
     const float g[3] = { 0.0f, -9.8f, 0.0f };
+    int sv;
 
     if (demo_fp)
     {
@@ -352,10 +430,10 @@ int demo_replay_step(float *dt)
         {
             /* 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;
         }
@@ -370,8 +448,13 @@ void demo_replay_stop(int d)
         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);
+}
+
 /*---------------------------------------------------------------------------*/