Formatting stuff. Convert tabs to spaces, shorten lines to 80, etc. First
[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 -4
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     const struct set *s = curr_set();
62     const struct level *l;
63     int jd = 0;
64     const GLfloat *fore, *back;
65     
66     if (! set_level_exists(s, i))
67     {
68         gui_space(id);
69         return;
70     }
71     
72     l = get_level(i);
73     
74     if (! l->is_locked)
75     {
76         fore =  l->is_bonus ? gui_grn : gui_wht;
77         back = l->is_completed ? fore : gui_yel;
78     }
79     else
80         fore = back = gui_gry;
81     jd = gui_label(id, l->numbername, GUI_SML, GUI_ALL, back, fore);
82     gui_active(jd, i, 0);
83 }
84
85 static void start_over_level(i)
86 {
87     const struct level *l = get_level(i);
88     if (! l->is_locked || config_get_d(CONFIG_CHEAT))
89     {
90         gui_set_image(shot_id, l->shot);
91
92         set_most_coins(&l->coin_score, -1);
93
94         if (config_get_d(CONFIG_MODE) == MODE_PRACTICE)
95         {
96             set_best_times(&l->time_score, -1, 0);
97             if (l->is_bonus)
98                 gui_set_label(status_id, _("Play this bonus level in practice mode"));
99             else
100                 gui_set_label(status_id, _("Play this level in practice mode"));
101         }
102         else
103         {
104             set_best_times(&l->goal_score, -1, 1);
105             if (l->is_bonus)
106                 gui_set_label(status_id, _("Play this bonus level in normal mode"));
107             else
108                 gui_set_label(status_id, _("Play this level in normal mode"));
109         }
110         if (config_get_d(CONFIG_CHEAT))
111         {
112             gui_set_label(status_id, l->file);
113         }
114         return;
115     }
116     else if (l->is_bonus)
117         gui_set_label(status_id, _("Play in challenge mode to unlock extra bonus levels"));
118     else
119         gui_set_label(status_id, _("Finish previous levels to unlock this level"));
120 }
121
122 static void start_over(id)
123 {
124     int i;
125     gui_pulse(id, 1.2f);
126     if (id == 0)
127         return;
128     
129     i = gui_token(id);
130     
131
132     switch (i)
133     {
134     case START_CHALLENGE:
135         gui_set_image(shot_id, curr_set()->shot);
136         set_most_coins(&curr_set()->coin_score, -1);
137         set_best_times(&curr_set()->time_score, -1, 0);
138         gui_set_label(status_id, _("Challenge all levels from the first one"));
139         break;
140         
141     case START_NORMAL:
142         gui_set_label(status_id, _("Collect coins and unlock next level"));
143         break;
144         
145     case START_PRACTICE:
146         gui_set_label(status_id, _("Train yourself without time nor coin"));
147         break;
148     }
149     
150     if (i >= 0)
151         start_over_level(i);
152 }
153
154 /*---------------------------------------------------------------------------*/
155
156 static int start_action(int i)
157 {
158     int mode = config_get_d(CONFIG_MODE);
159     audio_play(AUD_MENU, 1.0f);
160
161     if (i == START_BACK)
162         return goto_state(&st_set);
163     else if (i == START_NORMAL)
164     {
165         config_set_d(CONFIG_MODE, MODE_NORMAL);
166         return goto_state(&st_start);
167     }
168     else if (i == START_PRACTICE)
169     {
170         config_set_d(CONFIG_MODE, MODE_PRACTICE);
171         return goto_state(&st_start);
172     }
173     
174     if (i == START_CHALLENGE)
175     {
176         /* On cheat, start challenge mode where you want */
177         if (config_get_d(CONFIG_CHEAT))
178         {
179             config_set_d(CONFIG_MODE, MODE_CHALLENGE);
180             return goto_state(&st_start);
181         }
182         i = 0;
183         mode = MODE_CHALLENGE;
184     }
185
186     if (i>=0)
187     {
188         const struct level *l = get_level(i);
189         if (!l->is_locked || config_get_d(CONFIG_CHEAT))
190         {
191             level_play(l, mode);
192             return goto_state(&st_level);
193         }
194     }
195     return 1;
196 }
197
198 static int start_enter(void)
199 {
200     int w = config_get_d(CONFIG_WIDTH);
201     int h = config_get_d(CONFIG_HEIGHT);
202     int m = config_get_d(CONFIG_MODE);
203     int i, j;
204
205     int id, jd, kd, ld;
206
207     /* Desactivate cheat */
208     if (m == MODE_CHALLENGE && !config_get_d(CONFIG_CHEAT))
209     {
210         m = MODE_NORMAL;
211         config_set_d(CONFIG_MODE, m);
212     }
213     
214     if ((id = gui_vstack(0)))
215     {
216         if ((jd = gui_hstack(id)))
217         {
218             
219             gui_label(jd, _(curr_set()->name), GUI_SML, GUI_ALL, gui_yel, gui_red);
220             gui_filler(jd);
221             if (set_completed(curr_set()))
222             {
223                 gui_label(jd, _("Set Complete"), GUI_SML, GUI_ALL, gui_yel, gui_grn);
224                 gui_filler(jd);
225             }
226             gui_start(jd, _("Back"),  GUI_SML, START_BACK, 0);
227         }
228
229         
230         if ((jd = gui_harray(id)))
231         {
232             shot_id = gui_image(jd, curr_set()->shot, 7 * w / 16, 7 * h / 16);
233
234             if ((kd = gui_varray(jd)))
235             {
236                 if ((ld = gui_harray(kd)))
237                 {
238                     gui_state(ld, _("Practice"), GUI_SML, START_PRACTICE, m == MODE_PRACTICE);
239                     gui_state(ld, _("Normal"),   GUI_SML, START_NORMAL,   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                 gui_state(kd, _("Challenge"), GUI_SML, START_CHALLENGE , m == MODE_CHALLENGE);
246             }
247         }
248         gui_space(id);
249
250         if ((jd = gui_harray(id)))
251         {
252             gui_most_coins(jd, 0);
253             gui_best_times(jd, 0);
254         }
255         
256         gui_space(id);
257         
258         status_id = gui_label(id, _("Choose a level to play"), GUI_SML, GUI_ALL, gui_yel, gui_wht);
259         
260         gui_layout(id, 0, 0);
261         
262         set_most_coins(NULL, -1);
263         set_best_times(NULL, -1, m != MODE_PRACTICE);
264     }
265
266     return id;
267 }
268
269 static void start_point(int id, int x, int y, int dx, int dy)
270 {
271     start_over(gui_point(id, x, y));
272 }
273
274 static void start_stick(int id, int a, int v)
275 {
276     int x = (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a)) ? v : 0;
277     int y = (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a)) ? v : 0;
278     
279     start_over(gui_stick(id, x, y));
280 }
281
282 static int start_keybd(int c, int d)
283 {
284     if (d && c == SDLK_c && config_get_d(CONFIG_CHEAT))
285     {
286         set_cheat();
287         return goto_state(&st_start);
288     }
289                          
290     if (d && c == SDLK_F12)
291     {
292         int i;
293
294         /* Iterate over all levels, taking a screenshot of each. */
295
296         for (i = 0; i < MAXLVL; i++)
297             if (set_level_exists(curr_set(), i))
298                 level_snap(i);
299     }
300
301     return 1;
302 }
303
304 static int start_buttn(int b, int d)
305 {
306     if (d)
307     {
308         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
309             return start_action(gui_token(gui_click()));
310         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
311             return start_action(START_BACK);
312     }
313     return 1;
314 }
315
316 /*---------------------------------------------------------------------------*/
317
318 struct state st_start = {
319     start_enter,
320     shared_leave,
321     shared_paint,
322     shared_timer,
323     start_point,
324     start_stick,
325     shared_click,
326     start_keybd,
327     start_buttn,
328     1, 0
329 };