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