Fix repeated animation on return from replay save screen
[neverball] / ball / st_goal.c
index 9a47c49..4d622a8 100644 (file)
@@ -1,5 +1,5 @@
-/*   
- * Copyright (C) 2003 Robert Kooima
+/*
+ * Copyright (C) 2007 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
  * it under the  terms of the GNU General  Public License as published
  * General Public License for more details.
  */
 
-#include <string.h>
+#include <stdio.h>
 
 #include "gui.h"
-#include "set.h"
-#include "game.h"
 #include "util.h"
-#include "demo.h"
-#include "level.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_done.h"
 #include "st_start.h"
 #include "st_level.h"
+#include "st_name.h"
+#include "st_shared.h"
 
 /*---------------------------------------------------------------------------*/
 
-#define GOAL_NEXT 2
-#define GOAL_SAME 3
-#define GOAL_SAVE 4
-#define GOAL_BACK 5
-#define GOAL_DONE 6
+#define GOAL_NEXT 1
+#define GOAL_SAME 2
+#define GOAL_SAVE 3
+#define GOAL_BACK 4
+#define GOAL_DONE 5
+#define GOAL_OVER 6
+#define GOAL_LAST 7
 
-static int high;
-static int time_i;
-static int coin_i;
-    
 static int balls_id;
 static int coins_id;
 static int score_id;
 
-extern struct state st_goal_bis;
+static int resume;
 
 static int goal_action(int i)
 {
-    char player[MAXNAM];
-    size_t l;
-
     audio_play(AUD_MENU, 1.0f);
 
-    config_get_s(CONFIG_PLAYER, player, MAXNAM);
-    l = strlen(player);
-
     switch (i)
     {
     case GOAL_BACK:
-       if (level_mode() == MODE_CHALLENGE)
-           return goto_state(&st_over);
-       else
-           return goto_state(&st_start);
+        /* Fall through. */
+
+    case GOAL_OVER:
+        progress_stop();
+        return goto_state(&st_over);
 
     case GOAL_SAVE:
-        while (level_count())
-            ;
-        return goto_save(&st_goal_bis);
+        progress_stop();
+        return goto_save(&st_goal, &st_goal);
 
-    case GOAL_DONE:
-       while (level_count())
-           ;
-       goto_state(&st_done);
-       
-    case GOAL_NEXT:
-        while (level_count())
-            ;
-       level_next();
-       return goto_state(&st_level);
+    case GUI_NAME:
+        progress_stop();
+        return goto_name(&st_goal, &st_goal, 0);
 
-    case GOAL_SAME:
-        while (level_count())
-            ;
-       return goto_state(&st_level);
+    case GOAL_DONE:
+        progress_stop();
+        progress_exit();
+        return goto_state(&st_done);
 
-    case GUI_CL:
-        gui_keyboard_lock();
-        break;
+    case GOAL_LAST:
+        progress_stop();
+        return goto_state(&st_start);
 
-    case GUI_BS:
-        if (l > 0)
-        {
-            player[l - 1] = 0;
+    case GUI_SCORE_COIN:
+    case GUI_SCORE_TIME:
+    case GUI_SCORE_GOAL:
+        gui_score_set(i);
+        return goto_state(&st_goal);
 
-            config_set_s(CONFIG_PLAYER, player);
-            level_name(curr_level(), player, time_i, coin_i);
-            set_most_coins(curr_level(), coin_i);
-            set_best_times(curr_level(), time_i);
-        }
+    case GOAL_NEXT:
+        if (progress_next())
+            return goto_state(&st_level);
         break;
 
-    default:
-        if (l < MAXNAM - 1)
-        {
-            player[l + 0] = gui_keyboard_char((char) i);
-            player[l + 1] = 0;
-
-            config_set_s(CONFIG_PLAYER, player);
-            level_name(curr_level(), player, time_i, coin_i);
-            set_most_coins(curr_level(), coin_i);
-            set_best_times(curr_level(), time_i);
-        }
+    case GOAL_SAME:
+        if (progress_same())
+            return goto_state(&st_level);
+        break;
     }
+
     return 1;
 }
 
-static int goal_init(int * gidp)
+static int goal_gui(void)
 {
     const char *s1 = _("New Record");
     const char *s2 = _("GOAL");
 
-    int id, jd, kd;
+    int id, jd, kd, ld, md;
+
+    int high = progress_lvl_high();
+    int level = curr_level();
 
     if ((id = gui_vstack(0)))
     {
@@ -136,178 +122,181 @@ static int goal_init(int * gidp)
 
         gui_space(id);
 
-       if (level_mode() == MODE_CHALLENGE)
-       {
-           if ((jd = gui_hstack(id)))
-           {
-                if ((kd = gui_harray(jd)))
+        if (curr_mode() == MODE_CHALLENGE)
+        {
+            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)))
+            {
+                gui_filler(jd);
+
+                if ((kd = gui_vstack(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)))
-               {
-                   coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
-                   gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-               }
-
-               gui_set_count(balls_id, curr_balls());
-               gui_set_count(score_id, curr_score());
-               gui_set_count(coins_id, curr_coins());
-           }
-       }
-       
-       gui_space(id);
+                    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_filler(jd);
+            }
 
-        if ((jd = gui_harray(id)))
+            gui_space(id);
+        }
+        else
         {
-            gui_most_coins(jd, 3, 1);
-            gui_best_times(jd, 3, 1);
+            balls_id = score_id = coins_id = 0;
         }
 
+        gui_score_board(id, (GUI_SCORE_COIN |
+                             GUI_SCORE_TIME |
+                             GUI_SCORE_GOAL), 1, high);
+
         gui_space(id);
 
         if ((jd = gui_harray(id)))
         {
-            gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
-           
-           if (level_mode() != MODE_CHALLENGE)
-                gui_state(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
-           
-           if (level_mode() == MODE_CHALLENGE && level_last())
-                gui_start(jd, _("Finish"),  GUI_SML, GOAL_DONE, 0);
-           else if (level_opened(curr_level()+1))
-                gui_start(jd, _("Next Level"), GUI_SML, GOAL_NEXT, 0);
-            else
-                gui_label(jd, _("Next Level"), GUI_SML, GUI_ALL, gui_blk, gui_blk);
+            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);
+
+            if (progress_same_avail())
+                gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
+
+            if (demo_saved())
+                gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
         }
 
-        if (high) gui_keyboard(id);
+        if (!resume)
+            gui_pulse(gid, 1.2f);
 
         gui_layout(id, 0, 0);
-       if (gidp) *gidp = gid;
+
     }
 
-    set_most_coins(curr_level(), coin_i);
-    set_best_times(curr_level(), time_i);
+    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());
 
-    config_clr_grab();
 
     return id;
 }
 
-static int goal_enter(void)
-{
-    int gid;
-    int r;
-    
-    time_i = 3;
-    coin_i = 3;
-    high   = level_sort(&time_i, &coin_i);
-
-    r = goal_init(&gid);
-    
-    gui_pulse(gid, 1.2f);
-    audio_music_fade_out(2.0f);
-    return r; 
-}
-
-static int goal_bis_enter(void)
-{
-    return goal_init(NULL);
-}
-
-static void goal_leave(int id)
+static int goal_enter(struct state *st, struct state *prev)
 {
-    gui_delete(id);
-}
+    if (prev == &st_name)
+        progress_rename(0);
 
-static void goal_paint(int id, float st)
-{
-    game_draw(0, st);
-    gui_paint(id);
+    audio_music_fade_out(2.0f);
+    video_clr_grab();
+    resume = (prev == &st_goal || prev == &st_name || prev == &st_save);
+    return goal_gui();
 }
 
 static void goal_timer(int id, float dt)
 {
-    static float DT = 0.0f;
-
-    float g[3] = { 0.0f, 9.8f, 0.0f };
+    if (!resume)
+    {
+        static float t = 0.0f;
 
-    DT += dt;
+        t += dt;
 
-    if (time_state() < 1.f)
-    {
-        game_step(g, dt, 0);
-        demo_play_step(dt);
-    }
-    else if (DT > 0.05f)
-    {
-        if (level_count())
+        if (time_state() < 1.f)
+        {
+            game_server_step(dt);
+            game_client_sync(demo_fp);
+        }
+        else if (t > 0.05f && coins_id)
         {
-            int coins = curr_coins();
-            int score = curr_score();
-            int balls = curr_balls();
+            int coins = gui_value(coins_id);
 
-            if (gui_value(coins_id) != coins)
+            if (coins > 0)
             {
-                gui_set_count(coins_id, coins);
+                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);
-            }
-            if (gui_value(score_id) != score)
-            {
-                gui_set_count(score_id, score);
+
+                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 (gui_value(balls_id) != balls)
-            {
-                gui_set_count(balls_id, balls);
-                gui_pulse(balls_id, 2.0f);
-            }
+            t = 0.0f;
         }
-
-        DT = 0.0f;
     }
 
     gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void goal_bis_timer(int id, float dt)
-{
-    gui_timer(id, dt);
-    audio_timer(dt);
-}
-
-static void goal_point(int id, int x, int y, int dx, int dy)
-{
-    gui_pulse(gui_point(id, x, y), 1.2f);
-}
-
-static void goal_stick(int id, int a, int v)
-{
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
-        gui_pulse(gui_stick(id, v, 0), 1.2f);
-    if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
-        gui_pulse(gui_stick(id, 0, v), 1.2f);
-}
-
-static int goal_click(int b, int d)
-{
-    if (b <= 0 && d == 1)
-        return goal_action(gui_token(gui_click()));
-    return 1;
 }
 
 static int goal_keybd(int c, int d)
 {
-    if (d && c == SDLK_ESCAPE)
-       goal_action(GOAL_BACK);
+    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;
 }
 
@@ -316,9 +305,9 @@ static int goal_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return goal_click(0, 1);
+            return goal_action(gui_token(gui_click()));
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-           goal_action(GOAL_BACK);
+            return goal_action(GOAL_BACK);
     }
     return 1;
 }
@@ -327,26 +316,15 @@ static int goal_buttn(int b, int d)
 
 struct state st_goal = {
     goal_enter,
-    goal_leave,
-    goal_paint,
+    shared_leave,
+    shared_paint,
     goal_timer,
-    goal_point,
-    goal_stick,
-    goal_click,
+    shared_point,
+    shared_stick,
+    shared_angle,
+    shared_click,
     goal_keybd,
     goal_buttn,
     1, 0
 };
 
-struct state st_goal_bis = {
-    goal_bis_enter,
-    goal_leave,
-    goal_paint,
-    goal_bis_timer,
-    goal_point,
-    goal_stick,
-    goal_click,
-    goal_keybd,
-    goal_buttn,
-    1, 0
-};