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