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