Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[neverball] / ball / st_play.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 "hud.h"
17 #include "game.h"
18 #include "demo.h"
19 #include "levels.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "st_shared.h"
23
24 #include "st_play.h"
25 #include "st_goal.h"
26 #include "st_fall_out.h"
27 #include "st_time_out.h"
28 #include "st_over.h"
29 #include "st_pause.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 static int view_rotate;
34
35 static int pause_or_exit(void)
36 {
37     if (SDL_GetModState() & KMOD_SHIFT)
38     {
39         level_stat(GAME_NONE, curr_clock(), curr_coins());
40         level_stop();
41         config_clr_grab();
42
43         return goto_state(&st_over);
44     }
45     return goto_pause();
46 }
47
48 /*---------------------------------------------------------------------------*/
49
50 static int play_ready_enter(void)
51 {
52     int id;
53
54     if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
55     {
56         gui_layout(id, 0, 0);
57         gui_pulse(id, 1.2f);
58     }
59
60     audio_play(AUD_READY, 1.0f);
61     config_set_grab(1);
62
63     return id;
64 }
65
66 static void play_ready_timer(int id, float dt)
67 {
68     float t = time_state();
69
70     game_set_fly(1.0f - 0.5f * t);
71
72     if (dt > 0.0f && t > 1.0f)
73         goto_state(&st_play_set);
74
75     game_step_fade(dt);
76     gui_timer(id, dt);
77 }
78
79 static int play_ready_click(int b, int d)
80 {
81     return (b < 0 && d == 1) ? goto_state(&st_play_loop) : 1;
82 }
83
84 static int play_ready_keybd(int c, int d)
85 {
86     if (d)
87         if (config_tst_d(CONFIG_KEY_PAUSE, c))
88             goto_pause();
89     return 1;
90 }
91
92 static int play_ready_buttn(int b, int d)
93 {
94     if (d)
95     {
96         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
97             return goto_state(&st_play_loop);
98         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
99             return pause_or_exit();
100     }
101     return 1;
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 static int play_set_enter(void)
107 {
108     int id;
109
110     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
111     {
112         gui_layout(id, 0, 0);
113         gui_pulse(id, 1.2f);
114     }
115
116     audio_play(AUD_SET, 1.f);
117
118     clear_pause();
119
120     return id;
121 }
122
123 static void play_set_timer(int id, float dt)
124 {
125     float t = time_state();
126
127     game_set_fly(0.5f - 0.5f * t);
128
129     if (dt > 0.0f && t > 1.0f)
130         goto_state(&st_play_loop);
131
132     game_step_fade(dt);
133     gui_timer(id, dt);
134 }
135
136 static int play_set_click(int b, int d)
137 {
138     if (b < 0 && d == 1)
139     {
140         game_set_fly(0.0f);
141         return goto_state(&st_play_loop);
142     }
143     return 1;
144 }
145
146 static int play_set_keybd(int c, int d)
147 {
148     if (d)
149         if (config_tst_d(CONFIG_KEY_PAUSE, c))
150             goto_pause();
151     return 1;
152 }
153
154 static int play_set_buttn(int b, int d)
155 {
156     if (d)
157     {
158         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
159             return goto_state(&st_play_loop);
160         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
161             return pause_or_exit();
162     }
163     return 1;
164 }
165
166 /*---------------------------------------------------------------------------*/
167
168 static int show_hud;
169
170 static int play_loop_enter(void)
171 {
172     int id;
173
174     if (is_paused())
175     {
176         clear_pause();
177         view_rotate = 0;
178         return 0;
179     }
180
181     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
182     {
183         gui_layout(id, 0, 0);
184         gui_pulse(id, 1.2f);
185     }
186
187     audio_play(AUD_GO, 1.f);
188
189     game_set_fly(0.f);
190     view_rotate = 0;
191
192     hud_view_pulse(config_get_d(CONFIG_CAMERA));
193
194     show_hud = 1;
195
196     hud_update(0);
197
198     return id;
199 }
200
201 static void play_loop_paint(int id, float st)
202 {
203     game_draw(0, st);
204
205     if (show_hud)
206         hud_paint();
207
208     if (time_state() < 1.f)
209         gui_paint(id);
210 }
211
212 static void play_loop_timer(int id, float dt)
213 {
214     float k = ((SDL_GetModState() & KMOD_SHIFT) ?
215                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.0f :
216                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.0f);
217
218     float g[3] = { 0.0f, -9.8f, 0.0f };
219
220     gui_timer(id, dt);
221     hud_timer(dt);
222     game_set_rot(view_rotate * k);
223     game_set_cam(config_get_d(CONFIG_CAMERA));
224
225     game_step_fade(dt);
226     demo_play_step();
227
228     switch (game_step(g, dt, 1))
229     {
230     case GAME_GOAL:
231         level_stat(GAME_GOAL, curr_clock(), curr_coins());
232         gui_stuck();
233         goto_state(&st_goal);
234         break;
235
236     case GAME_FALL:
237         level_stat(GAME_FALL, curr_clock(), curr_coins());
238         gui_stuck();
239         goto_state(&st_fall_out);
240         break;
241
242     case GAME_TIME:
243         level_stat(GAME_TIME, curr_clock(), curr_coins());
244         gui_stuck();
245         goto_state(&st_time_out);
246         break;
247
248     default:
249         break;
250     }
251 }
252
253 static void play_loop_point(int id, int x, int y, int dx, int dy)
254 {
255     game_set_pos(dx, dy);
256 }
257
258 static void play_loop_stick(int id, int a, int k)
259 {
260     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
261         game_set_z(k);
262     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
263         game_set_x(k);
264 }
265
266 static int play_loop_click(int b, int d)
267 {
268     view_rotate = d ? b : 0;
269     return 1;
270 }
271
272 static int play_loop_keybd(int c, int d)
273 {
274     if (d)
275     {
276         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
277             view_rotate = +1;
278         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
279             view_rotate = -1;
280
281         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
282         {
283             config_set_d(CONFIG_CAMERA, 0);
284             hud_view_pulse(0);
285         }
286         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
287         {
288             config_set_d(CONFIG_CAMERA, 1);
289             hud_view_pulse(1);
290         }
291         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
292         {
293             config_set_d(CONFIG_CAMERA, 2);
294             hud_view_pulse(2);
295         }
296         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
297             curr_lg()->mode != MODE_CHALLENGE)
298         {
299             level_same();
300             goto_state(&st_play_ready);
301         }
302         if (config_tst_d(CONFIG_KEY_PAUSE, c))
303             goto_pause();
304     }
305     else
306     {
307         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
308             view_rotate = 0;
309         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
310             view_rotate = 0;
311     }
312
313     if (d && c == SDLK_F12 && config_cheat())
314         return goto_state(&st_look);
315
316     if (d && c == SDLK_F6)
317         show_hud = !show_hud;
318
319     if (d && c == SDLK_c && config_cheat())
320     {
321         level_stat(GAME_GOAL, curr_clock(), curr_coins());
322         return goto_state(&st_goal);
323     }
324     return 1;
325 }
326
327 static int play_loop_buttn(int b, int d)
328 {
329     if (d == 1)
330     {
331         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
332             pause_or_exit();
333
334         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
335             view_rotate = +1;
336         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
337             view_rotate = -1;
338
339         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
340         {
341             config_set_d(CONFIG_CAMERA, 0);
342             hud_view_pulse(0);
343         }
344         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
345         {
346             config_set_d(CONFIG_CAMERA, 1);
347             hud_view_pulse(1);
348         }
349         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
350         {
351             config_set_d(CONFIG_CAMERA, 2);
352             hud_view_pulse(2);
353         }
354     }
355     else
356     {
357         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
358             view_rotate = 0;
359         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
360             view_rotate = 0;
361     }
362     return 1;
363 }
364
365 /*---------------------------------------------------------------------------*/
366
367 static float phi;
368 static float theta;
369
370 static int look_enter(void)
371 {
372     phi   = 0;
373     theta = 0;
374     return 0;
375 }
376
377 static void look_leave(int id)
378 {
379 }
380
381 static void look_paint(int id, float st)
382 {
383     game_draw(0, st);
384 }
385
386 static void look_point(int id, int x, int y, int dx, int dy)
387 {
388     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
389     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
390
391     if (phi > +90.0f) phi = +90.0f;
392     if (phi < -90.0f) phi = -90.0f;
393
394     if (theta > +180.0f) theta -= 360.0f;
395     if (theta < -180.0f) theta += 360.0f;
396
397     game_look(phi, theta);
398 }
399
400 static int look_keybd(int c, int d)
401 {
402     if (d && c == SDLK_F12)
403         return goto_state(&st_play_loop);
404
405     return 1;
406 }
407
408 static int look_buttn(int b, int d)
409 {
410     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
411         return goto_state(&st_play_loop);
412
413     return 1;
414 }
415
416 /*---------------------------------------------------------------------------*/
417
418 struct state st_play_ready = {
419     play_ready_enter,
420     shared_leave,
421     shared_paint,
422     play_ready_timer,
423     NULL,
424     NULL,
425     NULL,
426     play_ready_click,
427     play_ready_keybd,
428     play_ready_buttn,
429     1, 0
430 };
431
432 struct state st_play_set = {
433     play_set_enter,
434     shared_leave,
435     shared_paint,
436     play_set_timer,
437     NULL,
438     NULL,
439     NULL,
440     play_set_click,
441     play_set_keybd,
442     play_set_buttn,
443     1, 0
444 };
445
446 struct state st_play_loop = {
447     play_loop_enter,
448     shared_leave,
449     play_loop_paint,
450     play_loop_timer,
451     play_loop_point,
452     play_loop_stick,
453     shared_angle,
454     play_loop_click,
455     play_loop_keybd,
456     play_loop_buttn,
457     0, 0
458 };
459
460 struct state st_look = {
461     look_enter,
462     look_leave,
463     look_paint,
464     NULL,
465     look_point,
466     NULL,
467     NULL,
468     NULL,
469     look_keybd,
470     look_buttn,
471     0, 0
472 };