Fix repeated animation on return from replay save screen
[neverball] / ball / st_goal.c
index f6d26d2..4d622a8 100644 (file)
 #include <stdio.h>
 
 #include "gui.h"
-#include "game.h"
 #include "util.h"
 #include "progress.h"
 #include "audio.h"
 #include "config.h"
+#include "video.h"
 #include "demo.h"
 
+#include "game_common.h"
+#include "game_server.h"
+#include "game_client.h"
+
 #include "st_goal.h"
 #include "st_save.h"
 #include "st_over.h"
 #define GOAL_BACK 4
 #define GOAL_DONE 5
 #define GOAL_OVER 6
+#define GOAL_LAST 7
 
 static int balls_id;
 static int coins_id;
 static int score_id;
 
-/* Bread crumbs. */
-
-static int new_name;
 static int resume;
 
 static int goal_action(int i)
@@ -63,15 +65,10 @@ static int goal_action(int i)
         return goto_state(&st_over);
 
     case GOAL_SAVE:
-        resume = 1;
-
         progress_stop();
         return goto_save(&st_goal, &st_goal);
 
     case GUI_NAME:
-        new_name = 1;
-        resume = 1;
-
         progress_stop();
         return goto_name(&st_goal, &st_goal, 0);
 
@@ -80,11 +77,14 @@ static int goal_action(int i)
         progress_exit();
         return goto_state(&st_done);
 
-    case GUI_MOST_COINS:
-    case GUI_BEST_TIMES:
-    case GUI_UNLOCK_GOAL:
+    case GOAL_LAST:
+        progress_stop();
+        return goto_state(&st_start);
+
+    case GUI_SCORE_COIN:
+    case GUI_SCORE_TIME:
+    case GUI_SCORE_GOAL:
         gui_score_set(i);
-        resume = 1;
         return goto_state(&st_goal);
 
     case GOAL_NEXT:
@@ -101,22 +101,15 @@ static int goal_action(int i)
     return 1;
 }
 
-static int goal_enter(void)
+static int goal_gui(void)
 {
     const char *s1 = _("New Record");
     const char *s2 = _("GOAL");
 
-    int id, jd, kd;
-
-    const struct level *l = get_level(curr_level());
+    int id, jd, kd, ld, md;
 
     int high = progress_lvl_high();
-
-    if (new_name)
-    {
-        progress_rename(0);
-        new_name = 0;
-    }
+    int level = curr_level();
 
     if ((id = gui_vstack(0)))
     {
@@ -160,31 +153,42 @@ static int goal_enter(void)
 
             if ((jd = gui_hstack(id)))
             {
+                gui_filler(jd);
 
-                if ((kd = gui_harray(jd)))
+                if ((kd = gui_vstack(jd)))
                 {
-                    balls_id = gui_count(kd, 100, GUI_MED, GUI_NE);
-                    gui_label(kd, _("Balls"), GUI_SML, 0, gui_wht, gui_wht);
+                    if ((ld = gui_hstack(kd)))
+                    {
+                        if ((md = gui_harray(ld)))
+                        {
+                            balls_id = gui_count(md, 100, GUI_MED, GUI_NE);
+                            gui_label(md, _("Balls"), GUI_SML, 0,
+                                      gui_wht, gui_wht);
+                        }
+                        if ((md = gui_harray(ld)))
+                        {
+                            score_id = gui_count(md, 1000, GUI_MED, 0);
+                            gui_label(md, _("Score"), GUI_SML, 0,
+                                      gui_wht, gui_wht);
+                        }
+                        if ((md = gui_harray(ld)))
+                        {
+                            coins_id = gui_count(md, 100, GUI_MED, 0);
+                            gui_label(md, _("Coins"), GUI_SML, GUI_NW,
+                                      gui_wht, gui_wht);
+                        }
+
+                        gui_set_count(balls_id, balls);
+                        gui_set_count(score_id, score);
+                        gui_set_count(coins_id, coins);
+                    }
+
+                    gui_label(kd, msg, GUI_SML, GUI_BOT, 0, 0);
                 }
-                if ((kd = gui_harray(jd)))
-                {
-                    score_id = gui_count(kd, 1000, GUI_MED, 0);
-                    gui_label(kd, _("Score"), GUI_SML, 0, gui_wht, gui_wht);
-                }
-                if ((kd = gui_harray(jd)))
-                {
-                    coins_id = gui_count(kd, 100, GUI_MED, 0);
-                    gui_label(kd, _("Coins"), GUI_SML, GUI_NW, gui_wht, gui_wht);
-                }
-
-                gui_set_count(balls_id, balls);
-                gui_set_count(score_id, score);
-                gui_set_count(coins_id, coins);
 
+                gui_filler(jd);
             }
 
-            gui_label(id, msg, GUI_SML, GUI_BOT, 0, 0);
-
             gui_space(id);
         }
         else
@@ -192,15 +196,18 @@ static int goal_enter(void)
             balls_id = score_id = coins_id = 0;
         }
 
-        if ((jd = gui_hstack(id)))
-            gui_score_board(jd, 1, high);
+        gui_score_board(id, (GUI_SCORE_COIN |
+                             GUI_SCORE_TIME |
+                             GUI_SCORE_GOAL), 1, high);
 
         gui_space(id);
 
         if ((jd = gui_harray(id)))
         {
-            if (progress_done())
+            if      (progress_done())
                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
+            else if (progress_last())
+                gui_start(jd, _("Finish"), GUI_SML, GOAL_LAST, 0);
 
             if (progress_next_avail())
                 gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
@@ -219,56 +226,62 @@ static int goal_enter(void)
 
     }
 
-    set_score_board(&l->score.most_coins,  progress_coin_rank(),
-                    &l->score.best_times,  progress_time_rank(),
-                    &l->score.unlock_goal, progress_goal_rank());
+    set_score_board(level_score(level, SCORE_COIN), progress_coin_rank(),
+                    level_score(level, SCORE_TIME), progress_time_rank(),
+                    level_score(level, SCORE_GOAL), progress_goal_rank());
 
-    audio_music_fade_out(2.0f);
-
-    config_clr_grab();
-
-    /* Reset hack. */
-    resume = 0;
 
     return id;
 }
 
-static void goal_timer(int id, float dt)
+static int goal_enter(struct state *st, struct state *prev)
 {
-    static float t = 0.0f;
-
-    float g[3] = { 0.0f, 9.8f, 0.0f };
+    if (prev == &st_name)
+        progress_rename(0);
 
-    t += dt;
+    audio_music_fade_out(2.0f);
+    video_clr_grab();
+    resume = (prev == &st_goal || prev == &st_name || prev == &st_save);
+    return goal_gui();
+}
 
-    if (time_state() < 1.f)
-    {
-        demo_play_step();
-        game_step(g, dt, 0);
-    }
-    else if (t > 0.05f && coins_id)
+static void goal_timer(int id, float dt)
+{
+    if (!resume)
     {
-        int coins = gui_value(coins_id);
+        static float t = 0.0f;
+
+        t += dt;
 
-        if (coins > 0)
+        if (time_state() < 1.f)
         {
-            int score = gui_value(score_id);
-            int balls = gui_value(balls_id);
+            game_server_step(dt);
+            game_client_sync(demo_fp);
+        }
+        else if (t > 0.05f && coins_id)
+        {
+            int coins = gui_value(coins_id);
+
+            if (coins > 0)
+            {
+                int score = gui_value(score_id);
+                int balls = gui_value(balls_id);
 
-            gui_set_count(coins_id, coins - 1);
-            gui_pulse(coins_id, 1.1f);
+                gui_set_count(coins_id, coins - 1);
+                gui_pulse(coins_id, 1.1f);
 
-            gui_set_count(score_id, score + 1);
-            gui_pulse(score_id, 1.1f);
+                gui_set_count(score_id, score + 1);
+                gui_pulse(score_id, 1.1f);
 
-            if (progress_reward_ball(score + 1))
-            {
-                gui_set_count(balls_id, balls + 1);
-                gui_pulse(balls_id, 2.0f);
-                audio_play(AUD_BALL, 1.0f);
+                if (progress_reward_ball(score + 1))
+                {
+                    gui_set_count(balls_id, balls + 1);
+                    gui_pulse(balls_id, 2.0f);
+                    audio_play(AUD_BALL, 1.0f);
+                }
             }
+            t = 0.0f;
         }
-        t = 0.0f;
     }
 
     gui_timer(id, dt);
@@ -276,8 +289,13 @@ static void goal_timer(int id, float dt)
 
 static int goal_keybd(int c, int d)
 {
-    if (d && config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
-        return goal_action(gui_score_next(gui_score_get()));
+    if (d)
+    {
+        if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
+            return goal_action(gui_score_next(gui_score_get()));
+        if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
+            return goal_action(GOAL_SAME);
+    }
 
     return 1;
 }
@@ -294,19 +312,11 @@ static int goal_buttn(int b, int d)
     return 1;
 }
 
-static void goal_leave(int id)
-{
-    /* HACK:  don't run animation if only "visiting" a state. */
-    st_goal.timer = resume ? shared_timer : goal_timer;
-
-    gui_delete(id);
-}
-
 /*---------------------------------------------------------------------------*/
 
 struct state st_goal = {
     goal_enter,
-    goal_leave,
+    shared_leave,
     shared_paint,
     goal_timer,
     shared_point,