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