Renamed a variable in parse_args from not_miss to missing and changed code
[neverball] / ball / st_play_end.c
1 /*   
2  * Copyright (C) 2003 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 <string.h>
16
17 #include "gui.h"
18 #include "set.h"
19 #include "game.h"
20 #include "util.h"
21 #include "levels.h"
22 #include "audio.h"
23 #include "config.h"
24 #include "demo.h"
25 #include "st_shared.h"
26
27 #include "st_play_end.h"
28 #include "st_save.h"
29 #include "st_over.h"
30 #include "st_done.h"
31 #include "st_start.h"
32 #include "st_level.h"
33 #include "st_name.h"
34
35 /*---------------------------------------------------------------------------*/
36
37 #define PLAY_END_NEXT 2
38 #define PLAY_END_SAME 3
39 #define PLAY_END_SAVE 4
40 #define PLAY_END_BACK 5
41 #define PLAY_END_DONE 6
42 #define PLAY_END_NAME 7
43 #define PLAY_END_OVER 8
44
45 static int balls_id;
46 static int coins_id;
47 static int score_id;
48
49 extern struct state st_play_end_bis;
50
51 static int play_end_action(int i)
52 {
53     audio_play(AUD_MENU, 1.0f);
54
55     switch (i)
56     {
57     case PLAY_END_BACK:
58     case PLAY_END_OVER:
59         return goto_end_level();
60
61     case PLAY_END_SAVE:
62         return goto_save(&st_play_end_bis, &st_play_end_bis);
63
64     case PLAY_END_NAME:
65         return goto_name(&st_play_end_bis, &st_play_end_bis);
66         
67     case PLAY_END_DONE:
68         return goto_state(&st_done);
69         
70     case PLAY_END_NEXT:
71         level_next();
72         return goto_state(&st_level);
73
74     case PLAY_END_SAME:
75         return goto_state(&st_level);
76     }
77
78     return 1;
79 }
80
81 static int play_end_init(int *gidp)
82 {
83     const struct level_game *lg = curr_lg();
84     int mode  = lg->mode;
85     int state = lg->state;
86     const struct level *l = lg->level;
87
88     int id, jd, kd;
89     int high;
90
91     high = (lg->time_rank < 3) || (lg->goal_rank < 3) || (lg->coin_rank < 3);
92
93     if ((id = gui_vstack(0)))
94     {
95         int gid;
96
97         if (state == GAME_SPEC)
98             gid = gui_label(id, _("SPECIAL GOAL"), GUI_MED, GUI_ALL,
99                             gui_blu, gui_grn);
100         else if (state == GAME_FALL)
101             gid = gui_label(id, _("Fall-out!"), GUI_MED, GUI_ALL,
102                             gui_gry, gui_red);
103         else if (state == GAME_TIME)
104             gid = gui_label(id, _("Time's Up!"), GUI_MED, GUI_ALL,
105                             gui_gry, gui_red);
106         else if (high)
107             gid = gui_label(id, _("New Record"), GUI_MED, GUI_ALL,
108                             gui_grn, gui_grn);
109         else
110             gid = gui_label(id, _("GOAL"), GUI_LRG, GUI_ALL,
111                             gui_blu, gui_grn);
112
113         gui_space(id);
114
115         if (mode == MODE_CHALLENGE && 
116             (lg->state == GAME_GOAL || lg->state == GAME_SPEC || l->is_bonus))
117         {
118             int coins = lg->coins;
119             int score = lg->score - coins;
120             int balls = lg->balls - count_extra_balls(score, coins);
121             if ((jd = gui_hstack(id)))
122             {
123                 if ((kd = gui_harray(jd)))
124                 {
125                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
126                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
127                 }
128                 if ((kd = gui_harray(jd)))
129                 {
130                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
131                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
132                 }
133                 if ((kd = gui_harray(jd)))
134                 {
135                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
136                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
137                 }
138
139                 gui_set_count(balls_id, balls);
140                 gui_set_count(score_id, score);
141                 gui_set_count(coins_id, coins);
142             }
143         }
144         else
145         {
146             balls_id = score_id = coins_id = 0;
147         }
148         
149         if (state == GAME_GOAL)
150         {
151             gui_space(id);
152             if ((jd = gui_harray(id)))
153             {
154                 gui_most_coins(jd, 1);
155                 gui_best_times(jd, 1);
156             }
157         }
158
159         gui_space(id);
160
161         if ((jd = gui_harray(id)))
162         {
163             int nlid = 0, rlid = 0;
164             int b = 0;
165
166             if (lg->win)
167                 gui_start(jd, _("Finish"), GUI_SML, PLAY_END_DONE, 0);
168             else
169                 nlid = gui_maybe(jd, _("Next Level"),  PLAY_END_NEXT,
170                                  lg->next_level != NULL);
171
172             if (lg->dead)
173                 gui_start(jd, _("Game Over"), GUI_SML, PLAY_END_OVER, 0);
174             else
175             {
176                 b = mode != MODE_CHALLENGE || ((state == GAME_FALL || state == GAME_TIME) && !lg->dead && !l->is_bonus);
177                 rlid = gui_maybe(jd, _("Retry Level"), PLAY_END_SAME, b);
178             }
179             
180             gui_maybe(jd, _("Save Replay"), PLAY_END_SAVE, demo_play_saved());
181
182             /* default is next if the next level is newly unkocked */
183             if (nlid != 0 && lg->unlock)
184                  gui_focus(nlid);
185             else if (b)
186                  gui_focus(rlid);
187         }
188
189         if (high)
190             gui_state(id, _("Change Player Name"),  GUI_SML, PLAY_END_NAME, 0);
191
192         gui_layout(id, 0, 0);
193         if (gidp) *gidp = gid;
194     }
195
196     if (state == GAME_GOAL)
197     {
198         set_most_coins(&l->coin_score, lg->coin_rank);
199         if (mode == MODE_CHALLENGE || mode == MODE_NORMAL)
200             set_best_times(&l->goal_score, lg->goal_rank, 1);
201         else
202             set_best_times(&l->time_score, lg->time_rank, 0);
203     }
204
205     config_clr_grab();
206
207     return id;
208 }
209
210 static int play_end_enter(void)
211 {
212     int gid;
213     int r;
214     
215     r = play_end_init(&gid);
216     
217     gui_pulse(gid, 1.2f);
218     audio_music_fade_out(2.0f);
219     return r; 
220 }
221
222 static int play_end_bis_enter(void)
223 {
224     level_update_player_name();
225     return play_end_init(NULL);
226 }
227
228 static void play_end_timer(int id, float dt)
229 {
230     static float DT = 0.0f;
231
232     float gg[3] = { 0.0f, 9.8f, 0.0f };
233     float gf[3] = { 0.0f, -9.8f, 0.0f };
234     int state = curr_lg()->state;
235
236     DT += dt;
237
238     if (time_state() < 2.f)
239         game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf, dt, NULL);
240     
241     if (time_state() > 1.f && DT > 0.05f && coins_id != 0)
242     {
243         int coins = gui_value(coins_id);
244         if (coins > 0)
245         {
246             int score = gui_value(score_id);
247             int balls = gui_value(balls_id);
248
249             gui_set_count(coins_id, coins - 1);
250             gui_pulse(coins_id, 1.1f);
251             
252             gui_set_count(score_id, score + 1);
253             gui_pulse(score_id, 1.1f);
254
255             if ((score + 1) % 100 == 0)
256             {
257                 gui_set_count(balls_id, balls + 1);
258                 gui_pulse(balls_id, 2.0f);
259                 audio_play(AUD_BALL, 1.0f);
260             }
261         }
262
263         DT = 0.0f;
264     }
265
266     gui_timer(id, dt);
267     audio_timer(dt);
268 }
269
270 static int play_end_buttn(int b, int d)
271 {
272     if (d)
273     {
274         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
275             return play_end_action(gui_token(gui_click()));
276         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
277             return play_end_action(PLAY_END_BACK);
278     }
279     return 1;
280 }
281
282 /*---------------------------------------------------------------------------*/
283
284 struct state st_play_end = {
285     play_end_enter,
286     shared_leave,
287     shared_paint,
288     play_end_timer,
289     shared_point,
290     shared_stick,
291     shared_click,
292     NULL,
293     play_end_buttn,
294     1, 0
295 };
296
297 struct state st_play_end_bis = {
298     play_end_bis_enter,
299     shared_leave,
300     shared_paint,
301     shared_timer,
302     shared_point,
303     shared_stick,
304     shared_click,
305     NULL,
306     play_end_buttn,
307     1, 0
308 };