Only save set score when player clicks "Finish" in Goal screen.
[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, 1, high);
197
198         gui_space(id);
199
200         if ((jd = gui_harray(id)))
201         {
202             if (progress_done())
203                 gui_start(jd, _("Finish"), GUI_SML, GOAL_DONE, 0);
204
205             if (progress_next_avail())
206                 gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
207
208             if (progress_same_avail())
209                 gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
210
211             if (demo_saved())
212                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
213         }
214
215         if (!resume)
216             gui_pulse(gid, 1.2f);
217
218         gui_layout(id, 0, 0);
219
220     }
221
222     set_score_board(&l->score.most_coins,  progress_coin_rank(),
223                     &l->score.best_times,  progress_time_rank(),
224                     &l->score.unlock_goal, progress_goal_rank());
225
226     audio_music_fade_out(2.0f);
227
228     config_clr_grab();
229
230     /* Reset hack. */
231     resume = 0;
232
233     return id;
234 }
235
236 static void goal_timer(int id, float dt)
237 {
238     static float t = 0.0f;
239
240     float g[3] = { 0.0f, 9.8f, 0.0f };
241
242     t += dt;
243
244     if (time_state() < 1.f)
245     {
246         demo_play_step();
247         game_step(g, dt, 0);
248     }
249     else if (t > 0.05f && coins_id)
250     {
251         int coins = gui_value(coins_id);
252
253         if (coins > 0)
254         {
255             int score = gui_value(score_id);
256             int balls = gui_value(balls_id);
257
258             gui_set_count(coins_id, coins - 1);
259             gui_pulse(coins_id, 1.1f);
260
261             gui_set_count(score_id, score + 1);
262             gui_pulse(score_id, 1.1f);
263
264             if (progress_reward_ball(score + 1))
265             {
266                 gui_set_count(balls_id, balls + 1);
267                 gui_pulse(balls_id, 2.0f);
268                 audio_play(AUD_BALL, 1.0f);
269             }
270         }
271         t = 0.0f;
272     }
273
274     gui_timer(id, dt);
275 }
276
277 static int goal_keybd(int c, int d)
278 {
279     if (d && config_tst_d(CONFIG_KEY_SCORE_NEXT, c))
280         return goal_action(gui_score_next(gui_score_get()));
281
282     return 1;
283 }
284
285 static int goal_buttn(int b, int d)
286 {
287     if (d)
288     {
289         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
290             return goal_action(gui_token(gui_click()));
291         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
292             return goal_action(GOAL_BACK);
293     }
294     return 1;
295 }
296
297 static void goal_leave(int id)
298 {
299     /* HACK:  don't run animation if only "visiting" a state. */
300     st_goal.timer = resume ? shared_timer : goal_timer;
301
302     gui_delete(id);
303 }
304
305 /*---------------------------------------------------------------------------*/
306
307 struct state st_goal = {
308     goal_enter,
309     goal_leave,
310     shared_paint,
311     goal_timer,
312     shared_point,
313     shared_stick,
314     shared_angle,
315     shared_click,
316     goal_keybd,
317     goal_buttn,
318     1, 0
319 };
320