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