Correct logic of BSP back/front tests
[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         progress_exit();
81         return goto_state(&st_done);
82
83     case GUI_MOST_COINS:
84     case GUI_BEST_TIMES:
85     case GUI_UNLOCK_GOAL:
86         gui_score_set(i);
87         resume = 1;
88         return goto_state(&st_goal);
89
90     case GOAL_NEXT:
91         if (progress_next())
92             return goto_state(&st_level);
93         break;
94
95     case GOAL_SAME:
96         if (progress_same())
97             return goto_state(&st_level);
98         break;
99     }
100
101     return 1;
102 }
103
104 static int goal_enter(void)
105 {
106     const char *s1 = _("New Record");
107     const char *s2 = _("GOAL");
108
109     int id, jd, kd;
110
111     const struct level *l = get_level(curr_level());
112
113     int high = progress_lvl_high();
114
115     if (new_name)
116     {
117         progress_rename(0);
118         new_name = 0;
119     }
120
121     if ((id = gui_vstack(0)))
122     {
123         int gid;
124
125         if (high)
126             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
127         else
128             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
129
130         gui_space(id);
131
132         if (curr_mode() == MODE_CHALLENGE)
133         {
134             int coins, score, balls;
135             char msg[MAXSTR] = "";
136             int i;
137
138             /* Reverse-engineer initial score and balls. */
139
140             if (resume)
141             {
142                 coins = 0;
143                 score = curr_score();
144                 balls = curr_balls();
145             }
146             else
147             {
148                 coins = curr_coins();
149                 score = curr_score() - coins;
150                 balls = curr_balls();
151
152                 for (i = curr_score(); i > score; i--)
153                     if (progress_reward_ball(i))
154                         balls--;
155             }
156
157             sprintf(msg, ngettext("%d new bonus level",
158                                   "%d new bonus levels",
159                                   curr_bonus()), curr_bonus());
160
161             if ((jd = gui_hstack(id)))
162             {
163
164                 if ((kd = gui_harray(jd)))
165                 {
166                     balls_id = gui_count(kd, 100, GUI_MED, GUI_NE);
167                     gui_label(kd, _("Balls"), GUI_SML, 0, gui_wht, gui_wht);
168                 }
169                 if ((kd = gui_harray(jd)))
170                 {
171                     score_id = gui_count(kd, 1000, GUI_MED, 0);
172                     gui_label(kd, _("Score"), GUI_SML, 0, gui_wht, gui_wht);
173                 }
174                 if ((kd = gui_harray(jd)))
175                 {
176                     coins_id = gui_count(kd, 100, GUI_MED, 0);
177                     gui_label(kd, _("Coins"), GUI_SML, GUI_NW, gui_wht, gui_wht);
178                 }
179
180                 gui_set_count(balls_id, balls);
181                 gui_set_count(score_id, score);
182                 gui_set_count(coins_id, coins);
183
184             }
185
186             gui_label(id, msg, GUI_SML, GUI_BOT, 0, 0);
187
188             gui_space(id);
189         }
190         else
191         {
192             balls_id = score_id = coins_id = 0;
193         }
194
195         if ((jd = gui_hstack(id)))
196             gui_score_board(jd, GUI_MOST_COINS |
197                                 GUI_BEST_TIMES |
198                                 GUI_UNLOCK_GOAL, 1, high);
199
200         gui_space(id);
201
202         if ((jd = gui_harray(id)))
203         {
204             if (progress_done())
205                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
206
207             if (progress_next_avail())
208                 gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
209
210             if (progress_same_avail())
211                 gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
212
213             if (demo_saved())
214                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
215         }
216
217         if (!resume)
218             gui_pulse(gid, 1.2f);
219
220         gui_layout(id, 0, 0);
221
222     }
223
224     set_score_board(&l->score.most_coins,  progress_coin_rank(),
225                     &l->score.best_times,  progress_time_rank(),
226                     &l->score.unlock_goal, progress_goal_rank());
227
228     audio_music_fade_out(2.0f);
229
230     config_clr_grab();
231
232     /* Reset hack. */
233     resume = 0;
234
235     return id;
236 }
237
238 static void goal_timer(int id, float dt)
239 {
240     static float t = 0.0f;
241
242     float g[3] = { 0.0f, 9.8f, 0.0f };
243
244     t += dt;
245
246     if (time_state() < 1.f)
247     {
248         demo_play_step();
249         game_step(g, dt, 0);
250     }
251     else if (t > 0.05f && coins_id)
252     {
253         int coins = gui_value(coins_id);
254
255         if (coins > 0)
256         {
257             int score = gui_value(score_id);
258             int balls = gui_value(balls_id);
259
260             gui_set_count(coins_id, coins - 1);
261             gui_pulse(coins_id, 1.1f);
262
263             gui_set_count(score_id, score + 1);
264             gui_pulse(score_id, 1.1f);
265
266             if (progress_reward_ball(score + 1))
267             {
268                 gui_set_count(balls_id, balls + 1);
269                 gui_pulse(balls_id, 2.0f);
270                 audio_play(AUD_BALL, 1.0f);
271             }
272         }
273         t = 0.0f;
274     }
275
276     gui_timer(id, dt);
277 }
278
279 static int goal_keybd(int c, int d)
280 {
281     if (d)
282     {
283         if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
284             return goal_action(gui_score_next(gui_score_get()));
285         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
286             return goal_action(GOAL_SAME);
287     }
288
289     return 1;
290 }
291
292 static int goal_buttn(int b, int d)
293 {
294     if (d)
295     {
296         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
297             return goal_action(gui_token(gui_click()));
298         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
299             return goal_action(GOAL_BACK);
300     }
301     return 1;
302 }
303
304 static void goal_leave(int id)
305 {
306     /* HACK:  don't run animation if only "visiting" a state. */
307     st_goal.timer = resume ? shared_timer : goal_timer;
308
309     gui_delete(id);
310 }
311
312 /*---------------------------------------------------------------------------*/
313
314 struct state st_goal = {
315     goal_enter,
316     goal_leave,
317     shared_paint,
318     goal_timer,
319     shared_point,
320     shared_stick,
321     shared_angle,
322     shared_click,
323     goal_keybd,
324     goal_buttn,
325     1, 0
326 };
327