Fix repeated animation on return from replay save screen
[neverball] / ball / st_goal.c
index 01ad29d..4d622a8 100644 (file)
  * General Public License for more details.
  */
 
-#include <stdio.h> /* Necessary for sprintf() line 128 */.
+#include <stdio.h>
 
 #include "gui.h"
-#include "game.h"
 #include "util.h"
-#include "levels.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_SAVE 3
 #define GOAL_BACK 4
 #define GOAL_DONE 5
-#define GOAL_NAME 6
-#define GOAL_OVER 7
+#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 be_back_soon;
+static int resume;
 
 static int goal_action(int i)
 {
@@ -60,76 +61,60 @@ static int goal_action(int i)
         /* Fall through. */
 
     case GOAL_OVER:
-        level_stop();
+        progress_stop();
         return goto_state(&st_over);
 
     case GOAL_SAVE:
-        be_back_soon = 1;
-
-        level_stop();
+        progress_stop();
         return goto_save(&st_goal, &st_goal);
 
-    case GOAL_NAME:
-        new_name = 1;
-        be_back_soon = 1;
-
-        level_stop();
-        return goto_name(&st_goal, &st_goal);
+    case GUI_NAME:
+        progress_stop();
+        return goto_name(&st_goal, &st_goal, 0);
 
     case GOAL_DONE:
-        level_stop();
+        progress_stop();
+        progress_exit();
         return goto_state(&st_done);
 
+    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);
+        return goto_state(&st_goal);
+
     case GOAL_NEXT:
-        level_next();
-        return goto_state(&st_level);
+        if (progress_next())
+            return goto_state(&st_level);
+        break;
 
     case GOAL_SAME:
-        level_same();
-        return goto_state(&st_level);
+        if (progress_same())
+            return goto_state(&st_level);
+        break;
     }
 
     return 1;
 }
 
-static int goal_enter(void)
+static int goal_gui(void)
 {
     const char *s1 = _("New Record");
     const char *s2 = _("GOAL");
-    const char *s3 = _("Congratulations!");
-
-    int id, jd, kd;
-
-          struct level_game *lg = curr_lg(); /* Note: const removed.  Is this really needed? */
-    const struct level *l = lg->level;
 
-    int high;
+    int id, jd, kd, ld, md;
 
-    high = (lg->time_rank < 3) || (lg->goal_rank < 3) || (lg->coin_rank < 3);
-
-    /* Reset hack. */
-    be_back_soon = 0;
-
-    if (new_name)
-    {
-        level_update_player_name();
-        new_name = 0;
-    }
+    int high = progress_lvl_high();
+    int level = curr_level();
 
     if ((id = gui_vstack(0)))
     {
         int gid;
 
-        if(lg->mode == MODE_CHALLENGE && lg->bonus > 0)
-        {
-            lg->bonus = 0;
-            lg->bonusid += 1;
-            char buf[MAXSTR] = {0};
-            sprintf(buf, "You Have Unlocked Bonus Level %i!", lg->bonusid);
-            gid = gui_label(id, s3, GUI_MED, GUI_ALL, gui_grn, gui_red);
-            gid = gui_label(id, buf, GUI_SML, GUI_ALL, gui_grn, gui_blu);
-        }
-
         if (high)
             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
         else
@@ -137,143 +122,184 @@ static int goal_enter(void)
 
         gui_space(id);
 
-        if (lg->mode == MODE_CHALLENGE)
+        if (curr_mode() == MODE_CHALLENGE)
         {
-            int coins = lg->coins;
-            int score = lg->score - coins;
-            int balls = lg->balls - count_extra_balls(score, coins);
+            int coins, score, balls;
+            char msg[MAXSTR] = "";
+            int i;
+
+            /* Reverse-engineer initial score and balls. */
+
+            if (resume)
+            {
+                coins = 0;
+                score = curr_score();
+                balls = curr_balls();
+            }
+            else
+            {
+                coins = curr_coins();
+                score = curr_score() - coins;
+                balls = curr_balls();
+
+                for (i = curr_score(); i > score; i--)
+                    if (progress_reward_ball(i))
+                        balls--;
+            }
+
+            sprintf(msg, ngettext("%d new bonus level",
+                                  "%d new bonus levels",
+                                  curr_bonus()), curr_bonus());
 
             if ((jd = gui_hstack(id)))
             {
-                if ((kd = gui_harray(jd)))
-                {
-                    balls_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
-                    gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-                }
-                if ((kd = gui_harray(jd)))
-                {
-                    score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
-                    gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-                }
-                if ((kd = gui_harray(jd)))
+                gui_filler(jd);
+
+                if ((kd = gui_vstack(jd)))
                 {
-                    coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
-                    gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, 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);
                 }
 
-                gui_set_count(balls_id, balls);
-                gui_set_count(score_id, score);
-                gui_set_count(coins_id, coins);
+                gui_filler(jd);
             }
+
+            gui_space(id);
         }
         else
         {
             balls_id = score_id = coins_id = 0;
         }
 
-        gui_space(id);
-
-        if ((jd = gui_harray(id)))
-        {
-            gui_most_coins(jd, 1);
-            gui_best_times(jd, 1);
-        }
+        gui_score_board(id, (GUI_SCORE_COIN |
+                             GUI_SCORE_TIME |
+                             GUI_SCORE_GOAL), 1, high);
 
         gui_space(id);
 
         if ((jd = gui_harray(id)))
         {
-            int next_id = 0, retry_id = 0;
-
-            if (lg->win)
+            if      (progress_done())
                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
-            else
-                next_id = gui_maybe(jd, _("Next Level"),  GOAL_NEXT,
-                                    lg->next_level != NULL);
+            else if (progress_last())
+                gui_start(jd, _("Finish"), GUI_SML, GOAL_LAST, 0);
 
-            if (lg->dead)
-                gui_start(jd, _("Game Over"), GUI_SML, GOAL_OVER, 0);
-            else
-            {
-                retry_id = gui_maybe(jd, _("Retry Level"), GOAL_SAME,
-                                     lg->mode != MODE_CHALLENGE);
-            }
+            if (progress_next_avail())
+                gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
 
-            gui_maybe(jd, _("Save Replay"), GOAL_SAVE, demo_saved());
+            if (progress_same_avail())
+                gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
 
-            /* Default is next if the next level is newly unlocked. */
-
-            if (next_id && lg->unlock)
-                gui_focus(next_id);
-            else if (lg->mode != MODE_CHALLENGE)
-                gui_focus(retry_id);
+            if (demo_saved())
+                gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
         }
 
-        /* FIXME, I'm ugly. */
-
-        if (high)
-            gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
+        if (!resume)
+            gui_pulse(gid, 1.2f);
 
-        gui_pulse(gid, 1.2f);
         gui_layout(id, 0, 0);
 
     }
 
-    set_most_coins(&l->score.most_coins, lg->coin_rank);
-
-    if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
-        set_best_times(&l->score.unlock_goal, lg->goal_rank, 1);
-    else
-        set_best_times(&l->score.best_times, lg->time_rank, 0);
+    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();
 
     return id;
 }
 
-static void goal_timer(int id, float dt)
+static int goal_enter(struct state *st, struct state *prev)
 {
-    static float DT = 0.0f;
+    if (prev == &st_name)
+        progress_rename(0);
 
-    float g[3] = { 0.0f, 9.8f, 0.0f };
-
-    DT += 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)
-    {
-        game_step(g, dt, 0);
-        demo_play_step(dt);
-    }
-    else if (DT > 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;
 
-        if (coins > 0)
+        t += dt;
+
+        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);
 
-            gui_set_count(coins_id, coins - 1);
-            gui_pulse(coins_id, 1.1f);
+            if (coins > 0)
+            {
+                int score = gui_value(score_id);
+                int balls = gui_value(balls_id);
 
-            gui_set_count(score_id, score + 1);
-            gui_pulse(score_id, 1.1f);
+                gui_set_count(coins_id, coins - 1);
+                gui_pulse(coins_id, 1.1f);
 
-            if ((score + 1) % 100 == 0)
-            {
-                gui_set_count(balls_id, balls + 1);
-                gui_pulse(balls_id, 2.0f);
-                audio_play(AUD_BALL, 1.0f);
+                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);
+                }
             }
+            t = 0.0f;
         }
-        DT = 0.0f;
     }
 
     gui_timer(id, dt);
 }
 
+static int goal_keybd(int c, int d)
+{
+    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;
+}
+
 static int goal_buttn(int b, int d)
 {
     if (d)
@@ -286,25 +312,18 @@ 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 = be_back_soon ? shared_timer : goal_timer;
-
-    gui_delete(id);
-}
-
 /*---------------------------------------------------------------------------*/
 
 struct state st_goal = {
     goal_enter,
-    goal_leave,
+    shared_leave,
     shared_paint,
     goal_timer,
     shared_point,
     shared_stick,
+    shared_angle,
     shared_click,
-    NULL,
+    goal_keybd,
     goal_buttn,
     1, 0
 };