fix 'free mode' score handling
[neverball] / ball / st_goal.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 "demo.h"
22 #include "level.h"
23 #include "audio.h"
24 #include "config.h"
25
26 #include "st_goal.h"
27 #include "st_save.h"
28 #include "st_over.h"
29 #include "st_done.h"
30 #include "st_start.h"
31 #include "st_level.h"
32
33 /*---------------------------------------------------------------------------*/
34
35 #define GOAL_NEXT 2
36 #define GOAL_SAME 3
37 #define GOAL_SAVE 4
38
39 static int high;
40 static int time_i;
41 static int coin_i;
42     
43 static int balls_id;
44 static int coins_id;
45 static int score_id;
46
47 static int goal_action(int i)
48 {
49     char player[MAXNAM];
50     size_t l;
51
52     audio_play(AUD_MENU, 1.0f);
53
54     config_get_s(CONFIG_PLAYER, player, MAXNAM);
55     l = strlen(player);
56
57     switch (i)
58     {
59     case GOAL_SAVE:
60         while (level_count())
61             ;
62         return goto_state(&st_save);
63
64     case GOAL_NEXT:
65         while (level_count())
66             ;
67         if (level_exit(NULL, 1))
68             return goto_state(&st_level);
69         else if (level_mode() == MODE_CHALLENGE)
70             return goto_state(&st_done);
71         else
72             return goto_state(&st_start);
73
74     case GOAL_SAME:
75         while (level_count())
76             ;
77         if (level_exit(NULL, 0))
78             return goto_state(&st_level);
79         else
80             /* return goto_state(&st_done); */
81             /* This case can't occurs */
82             return 0;
83
84     case GUI_CL:
85         gui_keyboard_lock();
86         break;
87
88     case GUI_BS:
89         if (l > 0)
90         {
91             player[l - 1] = 0;
92
93             config_set_s(CONFIG_PLAYER, player);
94             level_name(curr_level(), player, time_i, coin_i);
95             set_most_coins(curr_level(), coin_i);
96             set_best_times(curr_level(), time_i);
97         }
98         break;
99
100     default:
101         if (l < MAXNAM - 1)
102         {
103             player[l + 0] = gui_keyboard_char((char) i);
104             player[l + 1] = 0;
105
106             config_set_s(CONFIG_PLAYER, player);
107             level_name(curr_level(), player, time_i, coin_i);
108             set_most_coins(curr_level(), coin_i);
109             set_best_times(curr_level(), time_i);
110         }
111     }
112     return 1;
113 }
114
115 static int goal_enter(void)
116 {
117     const char *s1 = _("New Record");
118     const char *s2 = _("GOAL");
119
120     int id, jd, kd;
121
122     time_i = 3;
123     coin_i = 3;
124     high   = level_sort(&time_i, &coin_i);
125
126     if ((id = gui_vstack(0)))
127     {
128         int gid;
129
130         if (high)
131             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
132         else
133             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
134
135         gui_space(id);
136
137         if (level_mode() == MODE_CHALLENGE)
138         {
139             if ((jd = gui_hstack(id)))
140             {
141                 if ((kd = gui_harray(jd)))
142                 {
143                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
144                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
145                 }
146                 if ((kd = gui_harray(jd)))
147                 {
148                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
149                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
150                 }
151                 if ((kd = gui_harray(jd)))
152                 {
153                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
154                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
155                 }
156
157                 gui_set_count(balls_id, curr_balls());
158                 gui_set_count(score_id, curr_score());
159                 gui_set_count(coins_id, curr_coins());
160             }
161         }
162         
163         gui_space(id);
164
165         if ((jd = gui_harray(id)))
166         {
167             gui_most_coins(jd, 3, 1);
168             gui_best_times(jd, 3, 1);
169         }
170
171         gui_space(id);
172
173         if ((jd = gui_harray(id)))
174         {
175             gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
176             
177             if (level_mode() != MODE_CHALLENGE)
178                 gui_state(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
179
180             if (level_mode() == MODE_FREE) ;
181             else if (level_last())
182                 gui_start(jd, _("Finish"),  GUI_SML, GOAL_NEXT, 0);
183             else
184                 gui_start(jd, _("Next Level"), GUI_SML, GOAL_NEXT, 0);
185         }
186
187         if (high) gui_keyboard(id);
188
189         gui_layout(id, 0, 0);
190         gui_pulse(gid, 1.2f);
191     }
192
193     set_most_coins(curr_level(), coin_i);
194     set_best_times(curr_level(), time_i);
195
196     audio_music_fade_out(2.0f);
197
198     config_clr_grab();
199
200     return id;
201 }
202
203 static void goal_leave(int id)
204 {
205     gui_delete(id);
206 }
207
208 static void goal_paint(int id, float st)
209 {
210     game_draw(0, st);
211     gui_paint(id);
212 }
213
214 static void goal_timer(int id, float dt)
215 {
216     static float DT = 0.0f;
217
218     float g[3] = { 0.0f, 9.8f, 0.0f };
219
220     DT += dt;
221
222     if (time_state() < 1.f)
223     {
224         game_step(g, dt, 0);
225         demo_play_step(dt);
226     }
227     else if (DT > 0.05f)
228     {
229         if (level_count())
230         {
231             int coins = curr_coins();
232             int score = curr_score();
233             int balls = curr_balls();
234
235             if (gui_value(coins_id) != coins)
236             {
237                 gui_set_count(coins_id, coins);
238                 gui_pulse(coins_id, 1.1f);
239             }
240             if (gui_value(score_id) != score)
241             {
242                 gui_set_count(score_id, score);
243                 gui_pulse(score_id, 1.1f);
244             }
245             if (gui_value(balls_id) != balls)
246             {
247                 gui_set_count(balls_id, balls);
248                 gui_pulse(balls_id, 2.0f);
249             }
250         }
251
252         DT = 0.0f;
253     }
254
255     gui_timer(id, dt);
256     audio_timer(dt);
257 }
258
259 static void goal_point(int id, int x, int y, int dx, int dy)
260 {
261     gui_pulse(gui_point(id, x, y), 1.2f);
262 }
263
264 static void goal_stick(int id, int a, int v)
265 {
266     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
267         gui_pulse(gui_stick(id, v, 0), 1.2f);
268     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
269         gui_pulse(gui_stick(id, 0, v), 1.2f);
270 }
271
272 static int goal_click(int b, int d)
273 {
274     if (b <= 0 && d == 1)
275         return goal_action(gui_token(gui_click()));
276     return 1;
277 }
278
279 static int goal_keybd(int c, int d)
280 {
281     if (d && c == SDLK_ESCAPE)
282         goto_state(&st_over);
283     return 1;
284 }
285
286 static int goal_buttn(int b, int d)
287 {
288     if (d)
289     {
290         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
291             return goal_click(0, 1);
292         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
293             return goto_state(&st_over);
294     }
295     return 1;
296 }
297
298 /*---------------------------------------------------------------------------*/
299
300 struct state st_goal = {
301     goal_enter,
302     goal_leave,
303     goal_paint,
304     goal_timer,
305     goal_point,
306     goal_stick,
307     goal_click,
308     goal_keybd,
309     goal_buttn,
310     1, 0
311 };
312