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