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