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