Added tilt sensor abstraction.
[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 "levels.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_NAME 6
42 #define GOAL_OVER 7
43
44 static int balls_id;
45 static int coins_id;
46 static int score_id;
47
48 /* Bread crumbs. */
49
50 static int new_name;
51 static int be_back_soon;
52
53 static int goal_action(int i)
54 {
55     audio_play(AUD_MENU, 1.0f);
56
57     switch (i)
58     {
59     case GOAL_BACK:
60         /* Fall through. */
61
62     case GOAL_OVER:
63         level_stop();
64         return goto_state(&st_over);
65
66     case GOAL_SAVE:
67         be_back_soon = 1;
68
69         level_stop();
70         return goto_save(&st_goal, &st_goal);
71
72     case GOAL_NAME:
73         new_name = 1;
74         be_back_soon = 1;
75
76         level_stop();
77         return goto_name(&st_goal, &st_goal, 0);
78
79     case GOAL_DONE:
80         level_stop();
81         return goto_state(&st_done);
82
83     case GOAL_NEXT:
84         level_next();
85         return goto_state(&st_level);
86
87     case GOAL_SAME:
88         level_same();
89         return goto_state(&st_level);
90     }
91
92     return 1;
93 }
94
95 static int goal_enter(void)
96 {
97     const char *s1 = _("New Record");
98     const char *s2 = _("GOAL");
99     const char *s3 = _("Congratulations!");
100
101     int id, jd, kd;
102
103     struct level_game *lg = curr_lg();
104     const struct level *l = lg->level;
105
106     int high;
107
108     high = (lg->time_rank < 3) || (lg->goal_rank < 3) || (lg->coin_rank < 3);
109
110     /* Reset hack. */
111     be_back_soon = 0;
112
113     if (new_name)
114     {
115         level_update_player_name();
116         new_name = 0;
117     }
118
119     if ((id = gui_vstack(0)))
120     {
121         int gid;
122
123         if (lg->mode == MODE_CHALLENGE && lg->bonus)
124         {
125             char buf[MAXSTR];
126
127             sprintf(buf, _("You have unlocked bonus level %s!"),
128                     lg->bonus_repr);
129
130             gid = gui_label(id, s3,  GUI_MED, GUI_ALL, gui_grn, gui_red);
131             gid = gui_label(id, buf, GUI_SML, GUI_ALL, gui_blu, gui_grn);
132
133             lg->bonus = 0;
134             lg->bonus_repr = NULL;
135         }
136
137         if (high)
138             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
139         else
140             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
141
142         gui_space(id);
143
144         if (lg->mode == MODE_CHALLENGE)
145         {
146             int coins = lg->coins;
147             int score = lg->score - coins;
148             int balls = lg->balls - count_extra_balls(score, coins);
149
150             if ((jd = gui_hstack(id)))
151             {
152                 if ((kd = gui_harray(jd)))
153                 {
154                     balls_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
155                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
156                 }
157                 if ((kd = gui_harray(jd)))
158                 {
159                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
160                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
161                 }
162                 if ((kd = gui_harray(jd)))
163                 {
164                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
165                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
166                 }
167
168                 gui_set_count(balls_id, balls);
169                 gui_set_count(score_id, score);
170                 gui_set_count(coins_id, coins);
171             }
172         }
173         else
174         {
175             balls_id = score_id = coins_id = 0;
176         }
177
178         gui_space(id);
179
180         if ((jd = gui_harray(id)))
181         {
182             gui_most_coins(jd, 1);
183             gui_best_times(jd, 1);
184         }
185
186         gui_space(id);
187
188         if ((jd = gui_harray(id)))
189         {
190             int next_id = 0, retry_id = 0;
191
192             if (lg->win)
193                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
194             else
195                 next_id = gui_maybe(jd, _("Next Level"),  GOAL_NEXT,
196                                     lg->next_level != NULL);
197
198             if (lg->dead)
199                 gui_start(jd, _("Game Over"), GUI_SML, GOAL_OVER, 0);
200             else
201             {
202                 retry_id = gui_maybe(jd, _("Retry Level"), GOAL_SAME,
203                                      lg->mode != MODE_CHALLENGE);
204             }
205
206             gui_maybe(jd, _("Save Replay"), GOAL_SAVE, demo_saved());
207
208             /* Default is next if the next level is newly unlocked. */
209
210             if (next_id && lg->unlock)
211                 gui_focus(next_id);
212             else if (lg->mode != MODE_CHALLENGE)
213                 gui_focus(retry_id);
214         }
215
216         /* FIXME, I'm ugly. */
217
218         if (high)
219             gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
220
221         gui_pulse(gid, 1.2f);
222         gui_layout(id, 0, 0);
223
224     }
225
226     set_most_coins(&l->score.most_coins, lg->coin_rank);
227
228     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
229         set_best_times(&l->score.unlock_goal, lg->goal_rank, 1);
230     else
231         set_best_times(&l->score.best_times, lg->time_rank, 0);
232
233     audio_music_fade_out(2.0f);
234
235     config_clr_grab();
236
237     return id;
238 }
239
240 static void goal_timer(int id, float dt)
241 {
242     static float t = 0.0f;
243
244     float g[3] = { 0.0f, 9.8f, 0.0f };
245
246     t += dt;
247
248     if (time_state() < 1.f)
249     {
250         demo_play_step();
251         game_step(g, dt, 0);
252     }
253     else if (t > 0.05f && coins_id)
254     {
255         int coins = gui_value(coins_id);
256
257         if (coins > 0)
258         {
259             int score = gui_value(score_id);
260             int balls = gui_value(balls_id);
261
262             gui_set_count(coins_id, coins - 1);
263             gui_pulse(coins_id, 1.1f);
264
265             gui_set_count(score_id, score + 1);
266             gui_pulse(score_id, 1.1f);
267
268             if ((score + 1) % 100 == 0)
269             {
270                 gui_set_count(balls_id, balls + 1);
271                 gui_pulse(balls_id, 2.0f);
272                 audio_play(AUD_BALL, 1.0f);
273             }
274         }
275         t = 0.0f;
276     }
277
278     gui_timer(id, dt);
279 }
280
281 static int goal_buttn(int b, int d)
282 {
283     if (d)
284     {
285         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
286             return goal_action(gui_token(gui_click()));
287         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
288             return goal_action(GOAL_BACK);
289     }
290     return 1;
291 }
292
293 static void goal_leave(int id)
294 {
295     /* HACK:  don't run animation if only "visiting" a state. */
296     st_goal.timer = be_back_soon ? shared_timer : goal_timer;
297
298     gui_delete(id);
299 }
300
301 /*---------------------------------------------------------------------------*/
302
303 struct state st_goal = {
304     goal_enter,
305     goal_leave,
306     shared_paint,
307     goal_timer,
308     shared_point,
309     shared_stick,
310     shared_angle,
311     shared_click,
312     NULL,
313     goal_buttn,
314     1, 0
315 };
316