Restored replay overwrite fix (Windows); didn't seem to be related to the failed...
[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         goto_state(&st_goal);
235         break;
236
237     case GAME_FALL:
238         level_stat(GAME_FALL, curr_clock(), curr_coins());
239         goto_state(&st_fall_out);
240         break;
241
242     case GAME_TIME:
243         level_stat(GAME_TIME, curr_clock(), curr_coins());
244         goto_state(&st_time_out);
245         break;
246
247     default:
248         break;
249     }
250
251     game_step_fade(dt);
252     demo_play_step(at);
253     audio_timer(dt);
254 }
255
256 static void play_loop_point(int id, int x, int y, int dx, int dy)
257 {
258     game_set_pos(dx, dy);
259 }
260
261 static void play_loop_stick(int id, int a, int k)
262 {
263     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
264         game_set_z(k);
265     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
266         game_set_x(k);
267 }
268
269 static int play_loop_click(int b, int d)
270 {
271     view_rotate = d ? b : 0;
272     return 1;
273 }
274
275 static int play_loop_keybd(int c, int d)
276 {
277     if (d)
278     {
279         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
280             view_rotate = +1;
281         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
282             view_rotate = -1;
283
284         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
285         {
286             config_set_d(CONFIG_CAMERA, 0);
287             hud_view_pulse(0);
288         }
289         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
290         {
291             config_set_d(CONFIG_CAMERA, 1);
292             hud_view_pulse(1);
293         }
294         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
295         {
296             config_set_d(CONFIG_CAMERA, 2);
297             hud_view_pulse(2);
298         }
299         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
300             curr_lg()->mode != MODE_CHALLENGE)
301         {
302             level_same();
303             goto_state(&st_play_ready);
304         }
305         if (config_tst_d(CONFIG_KEY_PAUSE, c))
306             goto_pause();
307     }
308     else
309     {
310         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
311             view_rotate = 0;
312         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
313             view_rotate = 0;
314     }
315
316     if (d && c == SDLK_F12 && config_cheat())
317         return goto_state(&st_look);
318
319     if (d && c == SDLK_F6)
320         nohud = !nohud;
321
322     if (d && c == SDLK_c && config_cheat())
323     {
324         level_stat(GAME_GOAL, curr_clock(), curr_coins());
325         return goto_state(&st_goal);
326     }
327     return 1;
328 }
329
330 static int play_loop_buttn(int b, int d)
331 {
332     if (d == 1)
333     {
334         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
335             pause_or_exit();
336
337         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
338             view_rotate = +1;
339         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
340             view_rotate = -1;
341
342         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
343         {
344             config_set_d(CONFIG_CAMERA, 0);
345             hud_view_pulse(0);
346         }
347         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
348         {
349             config_set_d(CONFIG_CAMERA, 1);
350             hud_view_pulse(1);
351         }
352         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
353         {
354             config_set_d(CONFIG_CAMERA, 2);
355             hud_view_pulse(2);
356         }
357     }
358     else
359     {
360         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
361             view_rotate = 0;
362         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
363             view_rotate = 0;
364     }
365     return 1;
366 }
367
368 /*---------------------------------------------------------------------------*/
369
370 static float phi;
371 static float theta;
372
373 static int look_enter(void)
374 {
375     phi   = 0;
376     theta = 0;
377     return 0;
378 }
379
380 static void look_leave(int id)
381 {
382 }
383
384 static void look_paint(int id, float st)
385 {
386     game_draw(0, st);
387 }
388
389 static void look_point(int id, int x, int y, int dx, int dy)
390 {
391     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
392     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
393
394     if (phi > +90.0f) phi = +90.0f;
395     if (phi < -90.0f) phi = -90.0f;
396
397     if (theta > +180.0f) theta -= 360.0f;
398     if (theta < -180.0f) theta += 360.0f;
399
400     game_look(phi, theta);
401 }
402
403 static int look_keybd(int c, int d)
404 {
405     if (d && c == SDLK_F12)
406         return goto_state(&st_play_loop);
407
408     return 1;
409 }
410
411 static int look_buttn(int b, int d)
412 {
413     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
414         return goto_state(&st_play_loop);
415
416     return 1;
417 }
418
419 /*---------------------------------------------------------------------------*/
420
421 struct state st_play_ready = {
422     play_ready_enter,
423     shared_leave,
424     shared_paint,
425     play_ready_timer,
426     NULL,
427     NULL,
428     play_ready_click,
429     play_ready_keybd,
430     play_ready_buttn,
431     1, 0
432 };
433
434 struct state st_play_set = {
435     play_set_enter,
436     shared_leave,
437     shared_paint,
438     play_set_timer,
439     NULL,
440     NULL,
441     play_set_click,
442     play_set_keybd,
443     play_set_buttn,
444     1, 0
445 };
446
447 struct state st_play_loop = {
448     play_loop_enter,
449     shared_leave,
450     play_loop_paint,
451     play_loop_timer,
452     play_loop_point,
453     play_loop_stick,
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     look_keybd,
469     look_buttn,
470     0, 0
471 };