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