Implement game state interpolation (WIP)
[neverball] / ball / st_demo.c
index 3b136d4..7e388dc 100644 (file)
 #include "gui.h"
 #include "hud.h"
 #include "set.h"
-#include "game.h"
 #include "demo.h"
-#include "levels.h"
+#include "progress.h"
 #include "audio.h"
-#include "solid.h"
 #include "config.h"
-#include "st_shared.h"
 #include "util.h"
 #include "common.h"
+#include "demo_dir.h"
+#include "speed.h"
+
+#include "game_common.h"
+#include "game_server.h"
+#include "game_client.h"
 
 #include "st_demo.h"
 #include "st_title.h"
+#include "st_shared.h"
 
 /*---------------------------------------------------------------------------*/
 
 #define DEMO_LINE 4
 #define DEMO_STEP 8
 
+static Array items;
+
 static int first = 0;
 static int total = 0;
+static int last  = 0;
+
+static int last_viewed = 0;
 
 /*---------------------------------------------------------------------------*/
 
@@ -64,40 +73,86 @@ static int demo_action(int i)
         break;
 
     default:
-        if (level_replay(demo_get(i)->filename))
+        if (progress_replay(DIR_ITEM_GET(items, i)->path))
+        {
+            last_viewed = i;
             demo_play_goto(0);
             return goto_state(&st_demo_play);
+        }
+        break;
     }
     return 1;
 }
 
 /*---------------------------------------------------------------------------*/
 
-static void demo_replay(int id, int i)
+static struct thumb
+{
+    int item;
+    int shot;
+    int name;
+} thumbs[DEMO_STEP];
+
+static int gui_demo_thumbs(int id)
 {
     int w = config_get_d(CONFIG_WIDTH);
     int h = config_get_d(CONFIG_HEIGHT);
-    int jd;
 
-    char nam[MAXNAM + 3];
+    int jd, kd, ld;
+    int i, j;
 
-    if ((jd = gui_vstack(id)))
-    {
-        gui_space(jd);
-        gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
+    struct thumb *thumb;
 
-        nam[MAXNAM - 1] = '\0';
-        strncpy(nam, demo_get(i)->name, MAXNAM);
-        if (nam[MAXNAM - 1] != '\0')
-        {
-            nam[MAXNAM - 2] = '.';
-            nam[MAXNAM - 1] = '.';
-            nam[MAXNAM + 0] = '.';
-            nam[MAXNAM + 1] = '\0';
-        }
-        gui_state(jd, nam, GUI_SML, i, 0);
+    if ((jd = gui_varray(id)))
+        for (i = first; i < first + DEMO_STEP; i += DEMO_LINE)
+            if ((kd = gui_harray(jd)))
+            {
+                for (j = i + DEMO_LINE - 1; j >= i; j--)
+                {
+                    thumb = &thumbs[j % DEMO_STEP];
+
+                    thumb->item = j;
+
+                    if (j < total)
+                    {
+                        if ((ld = gui_vstack(kd)))
+                        {
+                            gui_space(ld);
+
+                            thumb->shot = gui_image(ld, " ", w / 6, h / 6);
+                            thumb->name = gui_state(ld, " ", GUI_SML, j, 0);
 
-        gui_active(jd, i, 0);
+                            gui_set_trunc(thumb->name, TRUNC_TAIL);
+
+                            gui_active(ld, j, 0);
+                        }
+                    }
+                    else
+                    {
+                        gui_space(kd);
+
+                        thumb->shot = 0;
+                        thumb->name = 0;
+                    }
+                }
+            }
+
+    return jd;
+}
+
+static void gui_demo_update_thumbs(void)
+{
+    struct dir_item *item;
+    struct demo *demo;
+    int i;
+
+    for (i = 0; i < ARRAYSIZE(thumbs) && thumbs[i].shot && thumbs[i].name; i++)
+    {
+        item = DIR_ITEM_GET(items, thumbs[i].item);
+        demo = item->data;
+
+        gui_set_image(thumbs[i].shot, demo ? demo->shot : "");
+        gui_set_label(thumbs[i].name, demo ? demo->name : base_name(item->path));
     }
 }
 
@@ -105,114 +160,102 @@ static int name_id;
 static int time_id;
 static int coin_id;
 static int date_id;
-static int mode_id;
 static int status_id;
 static int player_id;
 
-static int gui_demo_status(int id, const struct demo *d)
+static int gui_demo_status(int id)
 {
-    char noname[MAXNAM];
-    const char *mode, *status;
-    int i, j, k;
-    int jd, kd, ld, md;
+    const char *status;
+    int jd, kd, ld;
+    int s;
 
-    if (d == NULL)
-    {
-        /* Build a long name */
-        memset(noname, 'M', MAXNAM - 1);
-        noname[MAXNAM - 1] = '\0';
+    /* Find the longest status string. */
 
-        /* Get a long mode */
-        mode = mode_to_str(0, 0);
-        j = strlen(mode);
+    for (status = "", s = GAME_NONE; s < GAME_MAX; s++)
+        if (strlen(status_to_str(s)) > strlen(status))
+            status = status_to_str(s);
 
-        for (i = 1; i <= MODE_COUNT; i++)
-        {
-            k = strlen(mode_to_str(i, 0));
+    /* Build info bar with dummy values. */
+
+    if ((jd = gui_hstack(id)))
+    {
+        gui_filler(jd);
 
-            if (k > j)
+        if ((kd = gui_hstack(jd)))
+        {
+            if ((ld = gui_vstack(kd)))
             {
-                j = k;
-                mode = mode_to_str(i, 0);
+                gui_filler(ld);
+
+                time_id   = gui_clock(ld, 35000,  GUI_SML, GUI_NE);
+                coin_id   = gui_count(ld, 100,    GUI_SML, 0);
+                status_id = gui_label(ld, status, GUI_SML, GUI_SE,
+                                      gui_red, gui_red);
+
+                gui_filler(ld);
             }
-        }
 
-        /* Get a long status */
-        status = status_to_str(0);
-        j = strlen(status);
-        for (i = 1; i <= GAME_FALL; i++)
-        {
-            k = strlen(status_to_str(i));
-            if (k > j)
+            if ((ld = gui_vstack(kd)))
             {
-                j = k;
-                status = status_to_str(i);
+                gui_filler(ld);
+
+                gui_label(ld, _("Time"),   GUI_SML, GUI_NW, gui_wht, gui_wht);
+                gui_label(ld, _("Coins"),  GUI_SML, 0,      gui_wht, gui_wht);
+                gui_label(ld, _("Status"), GUI_SML, GUI_SW, gui_wht, gui_wht);
+
+                gui_filler(ld);
             }
         }
-    }
-    else
-    {
-        mode = mode_to_str(d->mode, 0);
-        status = status_to_str(d->status);
-    }
 
-    if ((jd = gui_hstack(id)))
-    {
+        gui_space(jd);
+
         if ((kd = gui_vstack(jd)))
         {
-            if ((ld = gui_harray(kd)))
-            {
-                if ((md = gui_vstack(ld)))
-                {
-                    player_id = gui_label(md, (d ? d->player : noname),
-                                          GUI_SML, GUI_RGT, 0, 0);
-                    coin_id = gui_count(md, (d ? d->coins : 100),
-                                        GUI_SML, GUI_RGT);
-                    status_id = gui_label(md, status, GUI_SML, GUI_RGT,
-                                         gui_red, gui_red);
-                }
-                if ((md = gui_vstack(ld)))
-                {
-                    gui_label(md, _("Player"), GUI_SML, GUI_LFT,
-                              gui_wht, gui_wht);
-                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
-                              gui_wht, gui_wht);
-                    gui_label(md, _("State"), GUI_SML, GUI_LFT,
-                              gui_wht, gui_wht);
-                }
-                if ((md = gui_vstack(ld)))
-                {
-                    name_id = gui_label(md, (d ? d->name : noname),
-                                        GUI_SML, GUI_RGT, 0, 0);
-                    time_id = gui_clock(md, (d ? d->timer : 35000),
-                                        GUI_SML, GUI_RGT);
-                    mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
-                }
-            }
-            date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
-                                GUI_SML, GUI_RGT, 0, 0);
+            gui_filler(kd);
+
+            name_id   = gui_label(kd, " ", GUI_SML, GUI_NE, 0, 0);
+            player_id = gui_label(kd, " ", GUI_SML, 0,      0, 0);
+            date_id   = gui_label(kd, date_to_str(time(NULL)),
+                                  GUI_SML, GUI_SE, 0, 0);
+
+            gui_filler(kd);
+
+            gui_set_trunc(name_id,   TRUNC_TAIL);
+            gui_set_trunc(player_id, TRUNC_TAIL);
         }
+
         if ((kd = gui_vstack(jd)))
         {
-            gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_filler(kd);
+
+            gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
+            gui_label(kd, _("Player"), GUI_SML, 0,      gui_wht, gui_wht);
+            gui_label(kd, _("Date"),   GUI_SML, GUI_SW, gui_wht, gui_wht);
+
+            gui_filler(kd);
         }
-        if (d && d->status == GAME_GOAL)
-            gui_set_color(status_id, gui_grn, gui_grn);
+
+        gui_filler(jd);
     }
+
     return jd;
 }
 
 static void gui_demo_update_status(int i)
 {
-    const struct demo *d = demo_get(i);
+    const struct demo *d;
+
+    if (!total)
+        return;
+
+    d = DEMO_GET(items, i < total ? i : 0);
+
+    if (!d)
+        return;
 
     gui_set_label(name_id,   d->name);
     gui_set_label(date_id,   date_to_str(d->date));
     gui_set_label(player_id, d->player);
-    gui_set_label(mode_id,   mode_to_str(d->mode, 0));
 
     if (d->status == GAME_GOAL)
         gui_set_color(status_id, gui_grn, gui_grn);
@@ -226,37 +269,30 @@ static void gui_demo_update_status(int i)
 
 /*---------------------------------------------------------------------------*/
 
-static int demo_enter(void)
+static int demo_gui(void)
 {
-    int i, j;
-    int id, jd, kd;
+    int id, jd;
 
     id = gui_vstack(0);
 
-    if ((total = demo_scan()))
+    if (total)
     {
         if ((jd = gui_hstack(id)))
         {
 
             gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
             gui_filler(jd);
-            gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
+            gui_navig(jd, first > 0, first + DEMO_STEP < total);
         }
 
-        if ((jd = gui_varray(id)))
-            for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
-                if ((kd = gui_harray(jd)))
-                {
-                    for (j = i + DEMO_LINE - 1; j >= i; j--)
-                        if (j < total)
-                            demo_replay(kd, j);
-                        else
-                            gui_space(kd);
-                }
-        gui_filler(id);
-        gui_demo_status(id, NULL);
+        gui_demo_thumbs(id);
+        gui_space(id);
+        gui_demo_status(id);
+
         gui_layout(id, 0, 0);
-        gui_demo_update_status(0);
+
+        gui_demo_update_thumbs();
+        gui_demo_update_status(last_viewed);
     }
     else
     {
@@ -264,9 +300,44 @@ static int demo_enter(void)
         gui_layout(id, 0, 0);
     }
 
+    return id;
+}
+
+static int demo_enter(struct state *st, struct state *prev)
+{
+    if (!items || (prev == &st_demo_del))
+    {
+        if (items)
+        {
+            demo_dir_free(items);
+            items = NULL;
+        }
+
+        items = demo_dir_scan();
+        total = array_len(items);
+    }
+
+    first       = first < total ? first : 0;
+    last        = MIN(first + DEMO_STEP - 1, total - 1);
+    last_viewed = MIN(MAX(first, last_viewed), last);
+
+    if (total)
+        demo_dir_load(items, first, last);
+
     audio_music_fade_to(0.5f, "bgm/inter.ogg");
 
-    return id;
+    return demo_gui();
+}
+
+static void demo_leave(struct state *st, struct state *next, int id)
+{
+    if (next == &st_title)
+    {
+        demo_dir_free(items);
+        items = NULL;
+    }
+
+    gui_delete(id);
 }
 
 static void demo_timer(int id, float dt)
@@ -282,16 +353,16 @@ static void demo_point(int id, int x, int y, int dx, int dy)
     int jd = shared_point_basic(id, x, y);
     int i  = gui_token(jd);
 
-    if (jd && i >= 0)
+    if (jd && i >= 0 && !GUI_ISMSK(i))
         gui_demo_update_status(i);
 }
 
-static void demo_stick(int id, int a, int v)
+static void demo_stick(int id, int a, float v, int bump)
 {
-    int jd = shared_stick_basic(id, a, v);
+    int jd = shared_stick_basic(id, a, v, bump);
     int i  = gui_token(jd);
 
-    if (jd && i >= 0)
+    if (jd && i >= 0 && !GUI_ISMSK(i))
         gui_demo_update_status(i);
 }
 
@@ -311,43 +382,73 @@ static int demo_buttn(int b, int d)
 
 static int standalone;
 static int demo_paused;
+static int show_hud;
+static int check_compat;
+static int speed;
+
+static float prelude;
 
 void demo_play_goto(int s)
 {
-    standalone = s;
+    standalone   = s;
+    check_compat = 1;
 }
 
-static int demo_play_enter(void)
+static int demo_play_gui(void)
 {
     int id;
 
+    if ((id = gui_vstack(0)))
+    {
+        gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
+        gui_layout(id, 0, 0);
+        gui_pulse(id, 1.2f);
+    }
+
+    return id;
+}
+
+static int demo_play_enter(struct state *st, struct state *prev)
+{
     if (demo_paused)
     {
         demo_paused = 0;
+        prelude = 0;
         audio_music_fade_in(0.5f);
         return 0;
     }
 
-    if ((id = gui_vstack(0)))
+    /*
+     * Post-1.5.1 replays include view data in the first update, this
+     * line is currently left in for compatibility with older replays.
+     */
+    game_client_fly(0.0f);
+
+    if (check_compat && !game_compat_map)
     {
-        gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
-        gui_layout(id, 0, 0);
-        gui_pulse(id, 1.2f);
+        goto_state(&st_demo_compat);
+        return 0;
     }
 
-    hud_update(0);
+    prelude = 1.0f;
 
-    game_set_fly(0.f);
+    speed = SPEED_NORMAL;
+    demo_speed_set(speed);
 
-    return id;
+    show_hud = 1;
+    hud_update(0);
+
+    return demo_play_gui();
 }
 
-static void demo_play_paint(int id, float st)
+static void demo_play_paint(int id, float t)
 {
-    game_draw(0, st);
-    hud_paint();
+    game_client_draw(0, t, demo_play_blend());
+
+    if (show_hud)
+        hud_paint();
 
-    if (time_state() < 1.f)
+    if (time_state() < prelude)
         gui_paint(id);
 }
 
@@ -357,13 +458,50 @@ static void demo_play_timer(int id, float dt)
     gui_timer(id, dt);
     hud_timer(dt);
 
-    /* Spin or skip depending on how fast the demo wants to run. */
+    /* Pause briefly before starting playback. */
+
+    if (time_state() < prelude)
+        return;
 
     if (!demo_replay_step(dt))
     {
         demo_paused = 0;
         goto_state(&st_demo_end);
     }
+    else
+        progress_step();
+}
+
+static void set_speed(int d)
+{
+    if (d > 0) speed = SPEED_UP(speed);
+    if (d < 0) speed = SPEED_DN(speed);
+
+    demo_speed_set(speed);
+    hud_speed_pulse(speed);
+}
+
+static void demo_play_stick(int id, int a, float v, int bump)
+{
+    if (!bump)
+        return;
+
+    if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
+    {
+        if (v < 0) set_speed(+1);
+        if (v > 0) set_speed(-1);
+    }
+}
+
+static int demo_play_click(int b, int d)
+{
+    if (d)
+    {
+        if (b == SDL_BUTTON_WHEELUP)   set_speed(+1);
+        if (b == SDL_BUTTON_WHEELDOWN) set_speed(-1);
+    }
+
+    return 1;
 }
 
 static int demo_play_keybd(int c, int d)
@@ -375,6 +513,9 @@ static int demo_play_keybd(int c, int d)
             demo_paused = 1;
             return goto_state(&st_demo_end);
         }
+
+        if (c == SDLK_F6)
+            show_hud = !show_hud;
     }
     return 1;
 }
@@ -385,8 +526,9 @@ static int demo_play_buttn(int b, int d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
         {
-            if (!(SDL_GetModState() & KMOD_SHIFT))
+            if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
                 demo_paused = 1;
+
             return goto_state(&st_demo_end);
         }
     }
@@ -419,7 +561,7 @@ static int demo_end_action(int i)
         return 0;
     case DEMO_REPLAY:
         demo_replay_stop(0);
-        level_replay(curr_demo_replay()->filename);
+        progress_replay(curr_demo());
         return goto_state(&st_demo_play);
     case DEMO_CONTINUE:
         return goto_state(&st_demo_play);
@@ -427,7 +569,7 @@ static int demo_end_action(int i)
     return 1;
 }
 
-static int demo_end_enter(void)
+static int demo_end_gui(void)
 {
     int id, jd, kd;
 
@@ -467,14 +609,19 @@ static int demo_end_enter(void)
         gui_layout(id, 0, 0);
     }
 
+    return id;
+}
+
+static int demo_end_enter(struct state *st, struct state *prev)
+{
     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
 
-    return id;
+    return demo_end_gui();
 }
 
-static void demo_end_paint(int id, float st)
+static void demo_end_paint(int id, float t)
 {
-    game_draw(0, st);
+    game_client_draw(0, t, demo_play_blend());
     gui_paint(id);
 
     if (demo_paused)
@@ -497,6 +644,7 @@ static int demo_end_buttn(int b, int d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
             return demo_end_action(gui_token(gui_click()));
+
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
         {
             if (demo_paused)
@@ -513,12 +661,11 @@ static int demo_end_buttn(int b, int d)
 static int demo_del_action(int i)
 {
     audio_play(AUD_MENU, 1.0f);
-
     demo_replay_stop(i == DEMO_DEL);
     return goto_state(&st_demo);
 }
 
-static int demo_del_enter(void)
+static int demo_del_gui(void)
 {
     int id, jd, kd;
 
@@ -535,11 +682,17 @@ static int demo_del_enter(void)
         gui_pulse(kd, 1.2f);
         gui_layout(id, 0, 0);
     }
-    audio_music_fade_out(2.0f);
 
     return id;
 }
 
+static int demo_del_enter(struct state *st, struct state *prev)
+{
+    audio_music_fade_out(2.0f);
+
+    return demo_del_gui();
+}
+
 static int demo_del_buttn(int b, int d)
 {
     if (d)
@@ -554,9 +707,55 @@ static int demo_del_buttn(int b, int d)
 
 /*---------------------------------------------------------------------------*/
 
+static int demo_compat_gui(void)
+{
+    int id;
+
+    if ((id = gui_vstack(0)))
+    {
+        gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
+        gui_space(id);
+        gui_multi(id, _("The current replay was recorded with a\\"
+                        "different (or unknown) version of this level.\\"
+                        "Be prepared to encounter visual errors.\\"),
+                  GUI_SML, GUI_ALL, gui_wht, gui_wht);
+
+        gui_layout(id, 0, 0);
+    }
+
+    return id;
+}
+
+static int demo_compat_enter(struct state *st, struct state *prev)
+{
+    check_compat = 0;
+
+    return demo_compat_gui();
+}
+
+static void demo_compat_timer(int id, float dt)
+{
+    game_step_fade(dt);
+    gui_timer(id, dt);
+}
+
+static int demo_compat_buttn(int b, int d)
+{
+    if (d)
+    {
+        if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
+            return goto_state(&st_demo_play);
+        if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
+            return goto_state(&st_demo_end);
+    }
+    return 1;
+}
+
+/*---------------------------------------------------------------------------*/
+
 struct state st_demo = {
     demo_enter,
-    shared_leave,
+    demo_leave,
     shared_paint,
     demo_timer,
     demo_point,
@@ -564,8 +763,7 @@ struct state st_demo = {
     shared_angle,
     shared_click,
     NULL,
-    demo_buttn,
-    1, 0
+    demo_buttn
 };
 
 struct state st_demo_play = {
@@ -574,12 +772,11 @@ struct state st_demo_play = {
     demo_play_paint,
     demo_play_timer,
     NULL,
+    demo_play_stick,
     NULL,
-    NULL,
-    NULL,
+    demo_play_click,
     demo_play_keybd,
-    demo_play_buttn,
-    1, 0
+    demo_play_buttn
 };
 
 struct state st_demo_end = {
@@ -592,8 +789,7 @@ struct state st_demo_end = {
     shared_angle,
     shared_click,
     demo_end_keybd,
-    demo_end_buttn,
-    1, 0
+    demo_end_buttn
 };
 
 struct state st_demo_del = {
@@ -606,6 +802,18 @@ struct state st_demo_del = {
     shared_angle,
     shared_click,
     NULL,
-    demo_del_buttn,
-    1, 0
+    demo_del_buttn
+};
+
+struct state st_demo_compat = {
+    demo_compat_enter,
+    shared_leave,
+    shared_paint,
+    demo_compat_timer,
+    shared_point,
+    shared_stick,
+    shared_angle,
+    shared_click,
+    NULL,
+    demo_compat_buttn
 };