Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[neverball] / ball / st_start.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 "gui.h"
16 #include "set.h"
17 #include "util.h"
18 #include "game.h"
19 #include "levels.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "st_shared.h"
23
24 #include "st_set.h"
25 #include "st_over.h"
26 #include "st_level.h"
27 #include "st_start.h"
28 #include "st_title.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 #define START_BACK      -1
33 #define START_PRACTICE  -2
34 #define START_NORMAL    -3
35 #define START_CHALLENGE -4
36
37 static int shot_id;
38 static int status_id;
39
40 /*---------------------------------------------------------------------------*/
41
42 /* Create a level selector button based upon its existence and status. */
43
44 static void gui_level(int id, int i)
45 {
46     const GLfloat *fore, *back;
47     const struct level *l;
48
49     int jd;
50
51     if (!set_level_exists(curr_set(), i))
52     {
53         gui_space(id);
54         return;
55     }
56
57     l = get_level(i);
58
59     if (!l->is_locked)
60     {
61         fore = l->is_bonus     ? gui_grn : gui_wht;
62         back = l->is_completed ? fore    : gui_yel;
63     }
64     else
65         fore = back = gui_gry;
66
67     jd = gui_label(id, l->repr, GUI_SML, GUI_ALL, back, fore);
68
69     gui_active(jd, i, 0);
70 }
71
72 static void start_over_level(int i)
73 {
74     const struct level *l = get_level(i);
75     if (!l->is_locked || config_cheat())
76     {
77         gui_set_image(shot_id, l->shot);
78
79         set_most_coins(&l->score.most_coins, -1);
80
81         if (curr_mode() == MODE_PRACTICE)
82         {
83             set_best_times(&l->score.best_times, -1, 0);
84             if (l->is_bonus)
85                 gui_set_label(status_id,
86                               _("Play this bonus level in practice mode"));
87             else
88                 gui_set_label(status_id,
89                               _("Play this level in practice mode"));
90         }
91         else
92         {
93             set_best_times(&l->score.unlock_goal, -1, 1);
94             if (l->is_bonus)
95                 gui_set_label(status_id,
96                               _("Play this bonus level in normal mode"));
97             else
98                 gui_set_label(status_id, _("Play this level in normal mode"));
99         }
100         if (config_cheat())
101         {
102             gui_set_label(status_id, l->file);
103         }
104         return;
105     }
106     else if (l->is_bonus)
107         gui_set_label(status_id,
108                       _("Play in challenge mode to unlock extra bonus levels"));
109     else
110         gui_set_label(status_id,
111                       _("Finish previous levels to unlock this level"));
112 }
113
114 static void start_over(int id)
115 {
116     int i;
117
118     gui_pulse(id, 1.2f);
119     if (id == 0)
120         return;
121
122     i = gui_token(id);
123
124
125     switch (i)
126     {
127     case START_CHALLENGE:
128         gui_set_image(shot_id, set_shot(curr_set()));
129         set_most_coins(set_coin_score(curr_set()), -1);
130         set_best_times(set_time_score(curr_set()), -1, 0);
131         gui_set_label(status_id, _("Challenge all levels from the first one"));
132         break;
133
134     case START_NORMAL:
135         gui_set_label(status_id, _("Collect coins and unlock next level"));
136         break;
137
138     case START_PRACTICE:
139         gui_set_label(status_id, _("Train yourself without time nor coin"));
140         break;
141     }
142
143     if (i >= 0)
144         start_over_level(i);
145 }
146
147 /*---------------------------------------------------------------------------*/
148
149 static int start_action(int i)
150 {
151     int mode = curr_mode();
152
153     audio_play(AUD_MENU, 1.0f);
154
155     switch (i)
156     {
157     case START_BACK:
158         return goto_state(&st_set);
159     case START_NORMAL:
160         mode_set(MODE_NORMAL);
161         return goto_state(&st_start);
162     case START_PRACTICE:
163         mode_set(MODE_PRACTICE);
164         return goto_state(&st_start);
165     }
166
167     if (i == START_CHALLENGE)
168     {
169         /* On cheat, start challenge mode where you want */
170         if (config_cheat())
171         {
172             mode_set(MODE_CHALLENGE);
173             return goto_state(&st_start);
174         }
175         i = 0;
176         mode = MODE_CHALLENGE;
177     }
178
179     if (i >= 0)
180     {
181         const struct level *l = get_level(i);
182
183         if (!l->is_locked || config_cheat())
184         {
185             if (level_play(l, mode))
186             {
187                 return goto_state(&st_level);
188             }
189             else
190             {
191                 level_stop();
192                 return 1;
193             }
194         }
195     }
196     return 1;
197 }
198
199 static int start_enter(void)
200 {
201     int w = config_get_d(CONFIG_WIDTH);
202     int h = config_get_d(CONFIG_HEIGHT);
203     int m = curr_mode();
204     int i, j;
205
206     int id, jd, kd, ld;
207
208     /* Deactivate cheat */
209
210     if (m == MODE_CHALLENGE && !config_cheat())
211     {
212         mode_set(MODE_NORMAL);
213         m = MODE_NORMAL;
214     }
215
216     if ((id = gui_vstack(0)))
217     {
218         if ((jd = gui_hstack(id)))
219         {
220
221             gui_label(jd, set_name(curr_set()), GUI_SML, GUI_ALL,
222                       gui_yel, gui_red);
223             gui_filler(jd);
224             gui_start(jd, _("Back"),  GUI_SML, START_BACK, 0);
225         }
226
227
228         if ((jd = gui_harray(id)))
229         {
230             shot_id = gui_image(jd, set_shot(curr_set()), 7 * w / 16, 7 * h / 16);
231
232             if ((kd = gui_varray(jd)))
233             {
234                 if ((ld = gui_harray(kd)))
235                 {
236                     gui_state(ld, _("Practice"), GUI_SML, START_PRACTICE,
237                               m == MODE_PRACTICE);
238                     gui_state(ld, _("Normal"),   GUI_SML, START_NORMAL,
239                               m == MODE_NORMAL);
240                 }
241                 for (i = 0; i < 5; i++)
242                     if ((ld = gui_harray(kd)))
243                         for (j = 4; j >= 0; j--)
244                             gui_level(ld, i * 5 + j);
245
246                 gui_state(kd, _("Challenge"), GUI_SML, START_CHALLENGE,
247                           m == MODE_CHALLENGE);
248             }
249         }
250         gui_space(id);
251
252         if ((jd = gui_harray(id)))
253         {
254             gui_most_coins(jd, 0);
255             gui_best_times(jd, 0);
256         }
257         gui_space(id);
258
259         status_id = gui_label(id, _("Choose a level to play"), GUI_SML, GUI_ALL,
260                               gui_yel, gui_wht);
261
262         gui_layout(id, 0, 0);
263
264         set_most_coins(NULL, -1);
265         set_best_times(NULL, -1, m != MODE_PRACTICE);
266     }
267
268     audio_music_fade_to(0.5f, "bgm/inter.ogg");
269
270     return id;
271 }
272
273 static void start_point(int id, int x, int y, int dx, int dy)
274 {
275     start_over(gui_point(id, x, y));
276 }
277
278 static void start_stick(int id, int a, int v)
279 {
280     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
281     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
282
283     start_over(gui_stick(id, x, y));
284 }
285
286 static int start_keybd(int c, int d)
287 {
288     if (d && c == SDLK_c && config_cheat())
289     {
290         set_cheat();
291         return goto_state(&st_start);
292     }
293
294     if (d && c == SDLK_F12)
295     {
296         int i;
297
298         /* Iterate over all levels, taking a screenshot of each. */
299
300         for (i = 0; i < MAXLVL; i++)
301             if (set_level_exists(curr_set(), i))
302                 level_snap(i);
303     }
304
305     return 1;
306 }
307
308 static int start_buttn(int b, int d)
309 {
310     if (d)
311     {
312         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
313             return start_action(gui_token(gui_click()));
314         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
315             return start_action(START_BACK);
316     }
317     return 1;
318 }
319
320 /*---------------------------------------------------------------------------*/
321
322 struct state st_start = {
323     start_enter,
324     shared_leave,
325     shared_paint,
326     shared_timer,
327     start_point,
328     start_stick,
329     shared_angle,
330     shared_click,
331     start_keybd,
332     start_buttn,
333     1, 0
334 };