ball/st_goal: change Challenge info area layout
[neverball] / ball / st_goal.c
1 /*
2  * Copyright (C) 2007 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdio.h>
16
17 #include "gui.h"
18 #include "util.h"
19 #include "progress.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "video.h"
23 #include "demo.h"
24
25 #include "game_common.h"
26 #include "game_server.h"
27 #include "game_client.h"
28
29 #include "st_goal.h"
30 #include "st_save.h"
31 #include "st_over.h"
32 #include "st_done.h"
33 #include "st_start.h"
34 #include "st_level.h"
35 #include "st_name.h"
36 #include "st_shared.h"
37
38 /*---------------------------------------------------------------------------*/
39
40 #define GOAL_NEXT 1
41 #define GOAL_SAME 2
42 #define GOAL_SAVE 3
43 #define GOAL_BACK 4
44 #define GOAL_DONE 5
45 #define GOAL_OVER 6
46 #define GOAL_LAST 7
47
48 static int balls_id;
49 static int coins_id;
50 static int score_id;
51
52 /* Bread crumbs. */
53
54 static int new_name;
55 static int resume;
56
57 static int goal_action(int i)
58 {
59     audio_play(AUD_MENU, 1.0f);
60
61     switch (i)
62     {
63     case GOAL_BACK:
64         /* Fall through. */
65
66     case GOAL_OVER:
67         progress_stop();
68         return goto_state(&st_over);
69
70     case GOAL_SAVE:
71         resume = 1;
72
73         progress_stop();
74         return goto_save(&st_goal, &st_goal);
75
76     case GUI_NAME:
77         new_name = 1;
78         resume = 1;
79
80         progress_stop();
81         return goto_name(&st_goal, &st_goal, 0);
82
83     case GOAL_DONE:
84         progress_stop();
85         progress_exit();
86         return goto_state(&st_done);
87
88     case GOAL_LAST:
89         progress_stop();
90         return goto_state(&st_start);
91
92     case GUI_MOST_COINS:
93     case GUI_BEST_TIMES:
94     case GUI_FAST_UNLOCK:
95         gui_score_set(i);
96         resume = 1;
97         return goto_state(&st_goal);
98
99     case GOAL_NEXT:
100         if (progress_next())
101             return goto_state(&st_level);
102         break;
103
104     case GOAL_SAME:
105         if (progress_same())
106             return goto_state(&st_level);
107         break;
108     }
109
110     return 1;
111 }
112
113 static int goal_enter(void)
114 {
115     const char *s1 = _("New Record");
116     const char *s2 = _("GOAL");
117
118     int id, jd, kd, ld, md;
119
120     const struct level *l = get_level(curr_level());
121
122     int high = progress_lvl_high();
123
124     if (new_name)
125     {
126         progress_rename(0);
127         new_name = 0;
128     }
129
130     if ((id = gui_vstack(0)))
131     {
132         int gid;
133
134         if (high)
135             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
136         else
137             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
138
139         gui_space(id);
140
141         if (curr_mode() == MODE_CHALLENGE)
142         {
143             int coins, score, balls;
144             char msg[MAXSTR] = "";
145             int i;
146
147             /* Reverse-engineer initial score and balls. */
148
149             if (resume)
150             {
151                 coins = 0;
152                 score = curr_score();
153                 balls = curr_balls();
154             }
155             else
156             {
157                 coins = curr_coins();
158                 score = curr_score() - coins;
159                 balls = curr_balls();
160
161                 for (i = curr_score(); i > score; i--)
162                     if (progress_reward_ball(i))
163                         balls--;
164             }
165
166             sprintf(msg, ngettext("%d new bonus level",
167                                   "%d new bonus levels",
168                                   curr_bonus()), curr_bonus());
169
170             if ((jd = gui_hstack(id)))
171             {
172                 gui_filler(jd);
173
174                 if ((kd = gui_vstack(jd)))
175                 {
176                     if ((ld = gui_hstack(kd)))
177                     {
178                         if ((md = gui_harray(ld)))
179                         {
180                             balls_id = gui_count(md, 100, GUI_MED, GUI_NE);
181                             gui_label(md, _("Balls"), GUI_SML, 0,
182                                       gui_wht, gui_wht);
183                         }
184                         if ((md = gui_harray(ld)))
185                         {
186                             score_id = gui_count(md, 1000, GUI_MED, 0);
187                             gui_label(md, _("Score"), GUI_SML, 0,
188                                       gui_wht, gui_wht);
189                         }
190                         if ((md = gui_harray(ld)))
191                         {
192                             coins_id = gui_count(md, 100, GUI_MED, 0);
193                             gui_label(md, _("Coins"), GUI_SML, GUI_NW,
194                                       gui_wht, gui_wht);
195                         }
196
197                         gui_set_count(balls_id, balls);
198                         gui_set_count(score_id, score);
199                         gui_set_count(coins_id, coins);
200                     }
201
202                     gui_label(kd, msg, GUI_SML, GUI_BOT, 0, 0);
203                 }
204
205                 gui_filler(jd);
206             }
207
208             gui_space(id);
209         }
210         else
211         {
212             balls_id = score_id = coins_id = 0;
213         }
214
215         if ((jd = gui_hstack(id)))
216             gui_score_board(jd, GUI_MOST_COINS |
217                                 GUI_BEST_TIMES |
218                                 GUI_FAST_UNLOCK, 1, high);
219
220         gui_space(id);
221
222         if ((jd = gui_harray(id)))
223         {
224             if      (progress_done())
225                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
226             else if (progress_last())
227                 gui_start(jd, _("Finish"), GUI_SML, GOAL_LAST, 0);
228
229             if (progress_next_avail())
230                 gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
231
232             if (progress_same_avail())
233                 gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
234
235             if (demo_saved())
236                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
237         }
238
239         if (!resume)
240             gui_pulse(gid, 1.2f);
241
242         gui_layout(id, 0, 0);
243
244     }
245
246     set_score_board(&l->score.most_coins,  progress_coin_rank(),
247                     &l->score.best_times,  progress_time_rank(),
248                     &l->score.fast_unlock, progress_goal_rank());
249
250     audio_music_fade_out(2.0f);
251
252     video_clr_grab();
253
254     /* Reset hack. */
255     resume = 0;
256
257     return id;
258 }
259
260 static void goal_timer(int id, float dt)
261 {
262     static float t = 0.0f;
263
264     t += dt;
265
266     if (time_state() < 1.f)
267     {
268         game_server_step(dt);
269         game_client_step(demo_file());
270     }
271     else if (t > 0.05f && coins_id)
272     {
273         int coins = gui_value(coins_id);
274
275         if (coins > 0)
276         {
277             int score = gui_value(score_id);
278             int balls = gui_value(balls_id);
279
280             gui_set_count(coins_id, coins - 1);
281             gui_pulse(coins_id, 1.1f);
282
283             gui_set_count(score_id, score + 1);
284             gui_pulse(score_id, 1.1f);
285
286             if (progress_reward_ball(score + 1))
287             {
288                 gui_set_count(balls_id, balls + 1);
289                 gui_pulse(balls_id, 2.0f);
290                 audio_play(AUD_BALL, 1.0f);
291             }
292         }
293         t = 0.0f;
294     }
295
296     gui_timer(id, dt);
297 }
298
299 static int goal_keybd(int c, int d)
300 {
301     if (d)
302     {
303         if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
304             return goal_action(gui_score_next(gui_score_get()));
305         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
306             return goal_action(GOAL_SAME);
307     }
308
309     return 1;
310 }
311
312 static int goal_buttn(int b, int d)
313 {
314     if (d)
315     {
316         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
317             return goal_action(gui_token(gui_click()));
318         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
319             return goal_action(GOAL_BACK);
320     }
321     return 1;
322 }
323
324 static void goal_leave(int id)
325 {
326     /* HACK:  don't run animation if only "visiting" a state. */
327     st_goal.timer = resume ? shared_timer : goal_timer;
328
329     gui_delete(id);
330 }
331
332 /*---------------------------------------------------------------------------*/
333
334 struct state st_goal = {
335     goal_enter,
336     goal_leave,
337     shared_paint,
338     goal_timer,
339     shared_point,
340     shared_stick,
341     shared_angle,
342     shared_click,
343     goal_keybd,
344     goal_buttn,
345     1, 0
346 };
347