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