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