Correct logic of BSP back/front tests
[neverball] / ball / util.c
index b8baad9..33bafce 100644 (file)
 
 #include <ctype.h>
 #include <string.h>
+#include <assert.h>
 
 #include "gui.h"
 #include "util.h"
-#include "level.h"
 #include "config.h"
 
 /*---------------------------------------------------------------------------*/
 
-static int is_special_name(const char * n)
+static int is_special_name(const char *n)
 {
-    return (strcmp(n, N_("Hard"))==0 || strcmp(n, N_("Medium"))==0 || strcmp(n, N_("Easy"))==0);
+    return (strcmp(n, N_("Hard"))   == 0 ||
+            strcmp(n, N_("Medium")) == 0 ||
+            strcmp(n, N_("Easy"))   == 0);
 }
 
 /*---------------------------------------------------------------------------*/
 
-static int coin_c[4];
-static int coin_n[4];
-static int coin_t[4];
+static int coin_coin[4];
+static int coin_name[4];
+static int coin_time[4];
+
+static int coin_extra_row;
 
 /* Build a Most Coins top three list with default values. */
 
-void gui_most_coins(int id, int n, int i)
+static void gui_most_coins(int id, int e)
 {
     const char *s = "1234567";
 
-    const float *c0 = gui_yel;
-    const float *c1 = gui_grn;
-    const float *c2 = gui_wht;
-    const float *c3 = gui_red;
-
     int j, jd, kd, ld, md;
 
+    coin_extra_row = e;
+
     if ((jd = gui_hstack(id)))
     {
         gui_filler(jd);
@@ -58,39 +59,44 @@ void gui_most_coins(int id, int n, int i)
             {
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n - 2; j++)
-                        coin_c[j] = gui_count(md, 1000, GUI_SML, 0);
+                    for (j = 0; j < NSCORE - 1; j++)
+                        coin_coin[j] = gui_count(md, 1000, GUI_SML, 0);
+
+                    coin_coin[j++] = gui_count(md, 1000, GUI_SML, GUI_SE);
 
-                    coin_c[j++] = gui_count(md, 1000, GUI_SML, GUI_SE);
-                    gui_space(md);
-                    coin_c[j++] = gui_count(md, 1000, GUI_SML, GUI_RGT);
+                    if (e)
+                    {
+                        gui_space(md);
+                        coin_coin[j++] = gui_count(md, 1000, GUI_SML, GUI_RGT);
+                    }
                 }
 
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n; j++)
-                        if      (i == j && i < n - 1)
-                            coin_n[j] = gui_label(md, s, GUI_SML, 0, c1, c1);
-                        else if (i == j)
-                        {
-                            if (j == n - 1) gui_space(md);
-                            coin_n[j] = gui_label(md, s, GUI_SML, 0, c3, c3);
-                        }
-                        else
-                        {
-                            if (j == n - 1) gui_space(md);
-                            coin_n[j] = gui_label(md, s, GUI_SML, 0, c0, c2);
-                        }
+                    for (j = 0; j < NSCORE ; j++)
+                        coin_name[j] = gui_label(md, s, GUI_SML, 0,
+                                                 gui_yel, gui_wht);
+
+                    if (e)
+                    {
+                        gui_space(md);
+                        coin_name[j++] = gui_label(md, s, GUI_SML, 0,
+                                                   gui_yel, gui_wht);
+                    }
                 }
 
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n - 2; j++)
-                        coin_t[j] = gui_clock(md, 359999, GUI_SML, 0);
+                    for (j = 0; j < NSCORE - 1; j++)
+                        coin_time[j] = gui_clock(md, 359999, GUI_SML, 0);
 
-                    coin_t[j++] = gui_clock(md, 359999,  GUI_SML, GUI_SW);
-                    gui_space(md);
-                    coin_t[j++] = gui_clock(md, 359999,  GUI_SML, GUI_LFT);
+                    coin_time[j++] = gui_clock(md, 359999, GUI_SML, GUI_SW);
+
+                    if (e)
+                    {
+                        gui_space(md);
+                        coin_time[j++] = gui_clock(md, 359999, GUI_SML, GUI_LFT);
+                    }
                 }
             }
         }
@@ -98,115 +104,285 @@ void gui_most_coins(int id, int n, int i)
     }
 }
 
-/* Set the Most Coins top three list values for level i. */
+/* Set the Most Coins top three list values. */
 
-void set_most_coins(int level, int n)
+static void set_most_coins(const struct score *s, int hilight)
 {
-    int j, spe;
-    const char * name;
+    const char *name;
+    int j;
 
-    for (j = 0; j < n; j++)
+    if (s == NULL)
     {
-       name = level_coin_n(level, j);
-       spe = is_special_name(name);
-        gui_set_count(coin_c[j], level_coin_c(level, j));
-        gui_set_label(coin_n[j], spe ? _(name) : name);
-        gui_set_clock(coin_t[j], level_coin_t(level, j));
+        for (j = 0; j < NSCORE + coin_extra_row ; j++)
+        {
+            gui_set_count(coin_coin[j], -1);
+            gui_set_label(coin_name[j], "");
+            gui_set_clock(coin_time[j], -1);
+        }
+    }
+    else
+    {
+        for (j = 0; j < NSCORE + coin_extra_row; j++)
+        {
+            name = s->player[j];
+
+            if (j == hilight)
+            {
+                if (j < NSCORE)
+                    gui_set_color(coin_name[j], gui_grn, gui_grn);
+                else
+                    gui_set_color(coin_name[j], gui_red, gui_red);
+            }
+            else
+                gui_set_color(coin_name[j], gui_yel, gui_wht);
+
+            gui_set_count(coin_coin[j], s->coins[j]);
+            gui_set_label(coin_name[j], is_special_name(name) ? _(name) : name);
+            gui_set_clock(coin_time[j], s->timer[j]);
+        }
     }
 }
 
 /*---------------------------------------------------------------------------*/
 
-static int time_c[4];
-static int time_n[4];
-static int time_t[4];
+static int time_label;
+
+static int time_coin[4];
+static int time_name[4];
+static int time_time[4];
+
+static int time_extra_row;
 
 /* Build a Best Times top three list with default values. */
 
-void gui_best_times(int id, int n, int i)
+static void gui_best_times(int id, int e)
 {
     const char *s = "1234567";
 
-    const float *c0 = gui_yel;
-    const float *c1 = gui_grn;
-    const float *c2 = gui_wht;
-    const float *c3 = gui_red;
-
     int j, jd, kd, ld, md;
 
+    time_extra_row = e;
+
     if ((jd = gui_hstack(id)))
     {
         gui_filler(jd);
 
         if ((kd = gui_vstack(jd)))
         {
-            gui_label(kd, _("Best Times"), GUI_SML, GUI_TOP, 0, 0);
+            time_label = gui_label(kd, "XXX", GUI_SML, GUI_TOP, 0, 0);
 
             if ((ld = gui_hstack(kd)))
             {
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n - 2; j++)
-                        time_t[j] = gui_clock(md, 359999, GUI_SML, 0);
+                    for (j = 0; j < NSCORE - 1; j++)
+                        time_time[j] = gui_clock(md, 359999, GUI_SML, 0);
+
+                    time_time[j++] = gui_clock(md, 359999, GUI_SML, GUI_SE);
 
-                    time_t[j++] = gui_clock(md, 359999, GUI_SML, GUI_SE);
-                    gui_space(md);
-                    time_t[j++] = gui_clock(md, 359999, GUI_SML, GUI_RGT);
+                    if (e)
+                    {
+                        gui_space(md);
+                        time_time[j++] = gui_clock(md, 359999, GUI_SML, GUI_RGT);
+                    }
                 }
 
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n; j++)
-                        if      (i == j && i < n - 1)
-                            time_n[j] = gui_label(md, s, GUI_SML, 0, c1, c1);
-                        else if (i == j)
-                        {
-                            if (j == n - 1) gui_space(md);
-                            time_n[j] = gui_label(md, s, GUI_SML, 0, c3, c3);
-                        }
-                        else
-                        {
-                            if (j == n - 1) gui_space(md);
-                            time_n[j] = gui_label(md, s, GUI_SML, 0, c0, c2);
-                        }
+                    for (j = 0; j < NSCORE; j++)
+                        time_name[j] = gui_label(md, s, GUI_SML, 0,
+                                                 gui_yel, gui_wht);
+
+                    if (e)
+                    {
+                        gui_space(md);
+                        time_name[j++] = gui_label(md, s, GUI_SML, 0,
+                                                   gui_yel, gui_wht);
+                    }
                 }
 
                 if ((md = gui_vstack(ld)))
                 {
-                    for (j = 0; j < n - 2; j++)
-                        time_c[j] = gui_count(md, 1000, GUI_SML, 0);
+                    for (j = 0; j < NSCORE - 1; j++)
+                        time_coin[j] = gui_count(md, 1000, GUI_SML, 0);
+
+                    time_coin[j++] = gui_count(md, 1000, GUI_SML, GUI_SW);
+
+                    if (e)
+                    {
+                        gui_space(md);
+                        time_coin[j++] = gui_count(md, 1000, GUI_SML, GUI_LFT);
+                    }
+                }
+            }
+        }
+        gui_filler(jd);
+    }
+}
+
+/* Set the Best Times top three list values. */
+
+static void set_best_times(const struct score *s, int hilight, int goal)
+{
+    const char *name;
+    int j;
+
+    gui_set_label(time_label, goal ? _("Unlock Goal") : _("Best Times"));
+
+    if (s == NULL)
+    {
+        for (j = 0; j < NSCORE + time_extra_row ; j++)
+        {
+            gui_set_clock(time_time[j], -1);
+            gui_set_label(time_name[j], "");
+            gui_set_count(time_coin[j], -1);
+        }
+    }
+    else
+    {
+        for (j = 0; j < NSCORE + time_extra_row; j++)
+        {
+            name = s->player[j];
+
+            if (j == hilight)
+            {
+                if (j < NSCORE)
+                    gui_set_color(time_name[j], gui_grn, gui_grn);
+                else
+                    gui_set_color(time_name[j], gui_red, gui_red);
+            }
+            else
+                gui_set_color(time_name[j], gui_yel, gui_wht);
+
+            gui_set_clock(time_time[j], s->timer[j]);
+            gui_set_label(time_name[j], is_special_name(name) ? _(name) : name);
+            gui_set_count(time_coin[j], s->coins[j]);
+        }
+    }
+}
+
+/*---------------------------------------------------------------------------*/
+
+static int score_type = GUI_MOST_COINS;
+
+void gui_score_board(int id, unsigned int types, int e, int h)
+{
+    int jd, kd, ld;
+
+    assert((types & GUI_MOST_COINS)  == GUI_MOST_COINS ||
+           (types & GUI_BEST_TIMES)  == GUI_BEST_TIMES ||
+           (types & GUI_UNLOCK_GOAL) == GUI_UNLOCK_GOAL );
+
+    /* Make sure current score type matches the spec. */
 
-                    time_c[j++] = gui_count(md, 1000, GUI_SML, GUI_SW);
-                    gui_space(md);
-                    time_c[j++] = gui_count(md, 1000, GUI_SML, GUI_LFT);
+    while ((types & score_type) != score_type)
+        score_type = gui_score_next(score_type);
+
+    gui_filler(id);
+
+    if ((jd = gui_hstack(id)))
+    {
+        gui_filler(jd);
+
+        if ((kd = gui_vstack(jd)))
+        {
+            gui_filler(kd);
+
+            if ((types & GUI_MOST_COINS) == GUI_MOST_COINS)
+                gui_state(kd, _("Most Coins"),  GUI_SML, GUI_MOST_COINS,
+                          score_type == GUI_MOST_COINS);
+            if ((types & GUI_BEST_TIMES) == GUI_BEST_TIMES)
+                gui_state(kd, _("Best Times"),  GUI_SML, GUI_BEST_TIMES,
+                          score_type == GUI_BEST_TIMES);
+            if ((types & GUI_UNLOCK_GOAL) == GUI_UNLOCK_GOAL)
+                gui_state(kd, _("Unlock Goal"), GUI_SML, GUI_UNLOCK_GOAL,
+                          score_type == GUI_UNLOCK_GOAL);
+
+            if (h)
+            {
+                gui_space(kd);
+
+                if ((ld = gui_hstack(kd)))
+                {
+                    gui_filler(ld);
+                    gui_state(ld, _("Change Name"), GUI_SML, GUI_NAME, 0);
+                    gui_filler(ld);
                 }
             }
+
+            gui_filler(kd);
         }
+
         gui_filler(jd);
     }
+
+    gui_filler(id);
+
+    switch (score_type)
+    {
+    case GUI_MOST_COINS:
+        gui_most_coins(id, e);
+        break;
+
+    case GUI_BEST_TIMES:
+        gui_best_times(id, e);
+        break;
+
+    case GUI_UNLOCK_GOAL:
+        gui_best_times(id, e);
+        break;
+    }
+
+    gui_filler(id);
+}
+
+void set_score_board(const struct score *smc, int hmc,
+                     const struct score *sbt, int hbt,
+                     const struct score *sug, int hug)
+{
+    switch (score_type)
+    {
+    case GUI_MOST_COINS:
+        set_most_coins(smc, hmc);
+        break;
+
+    case GUI_BEST_TIMES:
+        set_best_times(sbt, hbt, 0);
+        break;
+
+    case GUI_UNLOCK_GOAL:
+        set_best_times(sug, hug, 1);
+        break;
+    }
 }
 
-/* Set the Best Times top three list values for level i. */
+void gui_score_set(int t)
+{
+    score_type = t;
+}
 
-void set_best_times(int level, int n)
+int  gui_score_get(void)
 {
-    int j, spe;
-    const char * name;
+    return score_type;
+}
 
-    for (j = 0; j < n; j++)
+int  gui_score_next(int t)
+{
+    switch (t)
     {
-       name = level_time_n(level, j);
-       spe = is_special_name(name);
-        gui_set_clock(time_t[j], level_time_t(level, j));
-        gui_set_label(time_n[j], spe ? _(name) : name);
-        gui_set_count(time_c[j], level_time_c(level, j));
+    case GUI_MOST_COINS:  return GUI_BEST_TIMES;
+    case GUI_BEST_TIMES:  return GUI_UNLOCK_GOAL;
+    case GUI_UNLOCK_GOAL: return GUI_MOST_COINS;
+
+    default:
+        return GUI_MOST_COINS;
     }
 }
 
 /*---------------------------------------------------------------------------*/
 
 static int lock = 1;
-static int keyd[127]; 
+static int keyd[127];
 
 void gui_keyboard(int id)
 {
@@ -322,3 +498,43 @@ char gui_keyboard_char(char c)
 }
 
 /*---------------------------------------------------------------------------*/
+
+/*
+ * XXX Watch  out when  using these  functions. Be  sure to  check for
+ * GUI_NULL in addition to GUI_NEXT and GUI_PREV when using the latter
+ * two as labels for a switch with a default label.
+ */
+
+int gui_navig(int id, int prev, int next)
+{
+    int jd;
+
+    if ((jd = gui_hstack(id)))
+    {
+        if (next || prev)
+        {
+            gui_maybe(jd, _("Next"), GUI_NEXT, GUI_NULL, next);
+            gui_maybe(jd, _("Prev"), GUI_PREV, GUI_NULL, prev);
+        }
+
+        gui_start(jd, _("Back"), GUI_SML, GUI_BACK, 0);
+    }
+    return jd;
+}
+
+int gui_maybe(int id, const char *label, int etoken, int dtoken, int enabled)
+{
+    int bd;
+
+    if (!enabled)
+    {
+        bd = gui_state(id, label, GUI_SML, dtoken, 0);
+        gui_set_color(bd, gui_gry, gui_gry);
+    }
+    else
+        bd = gui_state(id, label, GUI_SML, etoken, 0);
+
+    return bd;
+}
+
+/*---------------------------------------------------------------------------*/