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