59077bb4ecaaf5ff902eb881fe34db2a1c48a100
[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 "cmd.h"
23
24 #include "game_common.h"
25 #include "game_server.h"
26 #include "game_proxy.h"
27 #include "game_client.h"
28
29 #include "st_play.h"
30 #include "st_goal.h"
31 #include "st_fail.h"
32 #include "st_pause.h"
33 #include "st_level.h"
34 #include "st_shared.h"
35
36 /*---------------------------------------------------------------------------*/
37
38 static int pause_or_exit(void)
39 {
40     if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
41     {
42         return goto_state(&st_pause);
43     }
44     else
45     {
46         progress_stat(GAME_NONE);
47         progress_stop();
48
49         video_clr_grab();
50
51         return goto_state(&st_exit);
52     }
53 }
54
55 /*---------------------------------------------------------------------------*/
56
57 static void set_camera(int c)
58 {
59     config_set_d(CONFIG_CAMERA, c);
60     hud_view_pulse(c);
61 }
62
63 static void toggle_camera(void)
64 {
65     int cam = (config_tst_d(CONFIG_CAMERA, VIEW_MANUAL) ?
66                VIEW_CHASE : VIEW_MANUAL);
67
68     set_camera(cam);
69 }
70
71 static void keybd_camera(int c)
72 {
73     if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
74         set_camera(VIEW_CHASE);
75     if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
76         set_camera(VIEW_LAZY);
77     if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
78         set_camera(VIEW_MANUAL);
79
80     if (config_tst_d(CONFIG_KEY_CAMERA_TOGGLE, c))
81         toggle_camera();
82 }
83
84 static void click_camera(int b)
85 {
86     if (config_tst_d(CONFIG_MOUSE_CAMERA_1, b))
87         set_camera(VIEW_CHASE);
88     if (config_tst_d(CONFIG_MOUSE_CAMERA_2, b))
89         set_camera(VIEW_LAZY);
90     if (config_tst_d(CONFIG_MOUSE_CAMERA_3, b))
91         set_camera(VIEW_MANUAL);
92
93     if (config_tst_d(CONFIG_MOUSE_CAMERA_TOGGLE, b))
94         toggle_camera();
95 }
96
97 static void buttn_camera(int b)
98 {
99     if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
100         set_camera(VIEW_CHASE);
101     if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
102         set_camera(VIEW_LAZY);
103     if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
104         set_camera(VIEW_MANUAL);
105
106     if (config_tst_d(CONFIG_JOYSTICK_CAMERA_TOGGLE, b))
107         toggle_camera();
108 }
109
110 /*---------------------------------------------------------------------------*/
111
112 static int play_ready_gui(void)
113 {
114     int id;
115
116     if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
117     {
118         gui_layout(id, 0, 0);
119         gui_pulse(id, 1.2f);
120     }
121
122     return id;
123 }
124
125 static int play_ready_enter(struct state *st, struct state *prev)
126 {
127     audio_play(AUD_READY, 1.0f);
128     video_set_grab(1);
129
130     hud_view_pulse(config_get_d(CONFIG_CAMERA));
131
132     return play_ready_gui();
133 }
134
135 static void play_ready_paint(int id, float t)
136 {
137     game_client_draw(0, t);
138     hud_view_paint();
139     gui_paint(id);
140 }
141
142 static void play_ready_timer(int id, float dt)
143 {
144     float t = time_state();
145
146     game_client_fly(1.0f - 0.5f * t);
147
148     if (dt > 0.0f && t > 1.0f)
149         goto_state(&st_play_set);
150
151     game_step_fade(dt);
152     hud_view_timer(dt);
153     gui_timer(id, dt);
154 }
155
156 static int play_ready_click(int b, int d)
157 {
158     if (d)
159     {
160         click_camera(b);
161
162         if (b == SDL_BUTTON_LEFT)
163             goto_state(&st_play_loop);
164     }
165     return 1;
166 }
167
168 static int play_ready_keybd(int c, int d)
169 {
170     if (d)
171     {
172         keybd_camera(c);
173
174         if (config_tst_d(CONFIG_KEY_PAUSE, c))
175             goto_state(&st_pause);
176     }
177     return 1;
178 }
179
180 static int play_ready_buttn(int b, int d)
181 {
182     if (d)
183     {
184         buttn_camera(b);
185
186         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
187             return goto_state(&st_play_loop);
188         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
189             return pause_or_exit();
190     }
191     return 1;
192 }
193
194 /*---------------------------------------------------------------------------*/
195
196 static int play_set_gui(void)
197 {
198     int id;
199
200     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
201     {
202         gui_layout(id, 0, 0);
203         gui_pulse(id, 1.2f);
204     }
205
206     return id;
207 }
208
209 static int play_set_enter(struct state *st, struct state *prev)
210 {
211     audio_play(AUD_SET, 1.f);
212
213     return play_set_gui();
214 }
215
216 static void play_set_paint(int id, float t)
217 {
218     game_client_draw(0, t);
219     hud_view_paint();
220     gui_paint(id);
221 }
222
223 static void play_set_timer(int id, float dt)
224 {
225     float t = time_state();
226
227     game_client_fly(0.5f - 0.5f * t);
228
229     if (dt > 0.0f && t > 1.0f)
230         goto_state(&st_play_loop);
231
232     game_step_fade(dt);
233     hud_view_timer(dt);
234     gui_timer(id, dt);
235 }
236
237 static int play_set_click(int b, int d)
238 {
239     if (d)
240     {
241         click_camera(b);
242
243         if (b == SDL_BUTTON_LEFT)
244             goto_state(&st_play_loop);
245     }
246     return 1;
247 }
248
249 static int play_set_keybd(int c, int d)
250 {
251     if (d)
252     {
253         keybd_camera(c);
254
255         if (config_tst_d(CONFIG_KEY_PAUSE, c))
256             goto_state(&st_pause);
257     }
258     return 1;
259 }
260
261 static int play_set_buttn(int b, int d)
262 {
263     if (d)
264     {
265         buttn_camera(b);
266
267         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
268             return goto_state(&st_play_loop);
269         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
270             return pause_or_exit();
271     }
272     return 1;
273 }
274
275 /*---------------------------------------------------------------------------*/
276
277 struct
278 {
279     float R;
280     float L;
281
282     enum
283     {
284         DIR_R = 1,
285         DIR_L
286     } d;
287 } view_rotate;
288
289 #define VIEWR_SET_R(v) do {                                    \
290     view_rotate.R = (v);                                       \
291     view_rotate.d = (v) ? DIR_R : (view_rotate.L ? DIR_L : 0); \
292 } while (0)
293
294 #define VIEWR_SET_L(v) do {                                    \
295     view_rotate.L = (v);                                       \
296     view_rotate.d = (v) ? DIR_L : (view_rotate.R ? DIR_R : 0); \
297 } while (0)
298
299 static int fast_rotate;
300 static int show_hud;
301
302 static int play_loop_gui(void)
303 {
304     int id;
305
306     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
307     {
308         gui_layout(id, 0, 0);
309         gui_pulse(id, 1.2f);
310     }
311
312     return id;
313 }
314
315 static int play_loop_enter(struct state *st, struct state *prev)
316 {
317     VIEWR_SET_R(0);
318     VIEWR_SET_L(0);
319     fast_rotate = 0;
320
321     if (prev == &st_pause)
322         return 0;
323
324     audio_play(AUD_GO, 1.f);
325
326     game_client_fly(0.0f);
327
328     show_hud = 1;
329     hud_update(0);
330
331     return play_loop_gui();
332 }
333
334 static void play_loop_paint(int id, float t)
335 {
336     game_client_draw(0, t);
337
338     if (show_hud)
339         hud_paint();
340
341     if (time_state() < 1.f)
342         gui_paint(id);
343 }
344
345 static void play_loop_timer(int id, float dt)
346 {
347     float k = (fast_rotate ?
348                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.0f :
349                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.0f);
350
351     gui_timer(id, dt);
352     hud_timer(dt);
353     game_set_rot(view_rotate.d == DIR_R ?
354                  view_rotate.R * k :
355                  view_rotate.L * k);
356     game_set_cam(config_get_d(CONFIG_CAMERA));
357
358     game_step_fade(dt);
359
360     game_server_step(dt);
361     game_client_sync(demo_fp);
362     game_client_blend(game_server_blend());
363
364     switch (curr_status())
365     {
366     case GAME_GOAL:
367         progress_stat(GAME_GOAL);
368         goto_state(&st_goal);
369         break;
370
371     case GAME_FALL:
372         progress_stat(GAME_FALL);
373         goto_state(&st_fail);
374         break;
375
376     case GAME_TIME:
377         progress_stat(GAME_TIME);
378         goto_state(&st_fail);
379         break;
380
381     default:
382         progress_step();
383         break;
384     }
385 }
386
387 #ifdef __MAEMO__
388 #define TOUCH_SENSITIVITY       20
389 static int button_pressed = 0;
390 static int button_pressed_x = 0;
391 static int button_pressed_y = 0;
392 static int dragging = 0;
393
394 static void maemo_play_loop_point(int id, int x, int y, int dx, int dy)
395 {
396     if (!dragging && button_pressed)
397     {
398         /* check how much we've moved */
399         if (abs(x - button_pressed_x) > TOUCH_SENSITIVITY ||
400             abs(y - button_pressed_y) > TOUCH_SENSITIVITY)
401             dragging = 1;
402     }
403
404     if (dragging)
405     {
406         if (dx > 0)
407         {
408             view_rotate.d = DIR_R;
409             view_rotate.R = dx / 15.0;
410         }
411         else
412         {
413             view_rotate.d = DIR_L;
414             view_rotate.L = dx / 15.0;
415         }
416     }
417 }
418
419 static void maemo_play_loop_click(int b, int d)
420 {
421     if (d)
422     {
423         int y;
424         SDL_GetMouseState(&button_pressed_x, &y);
425         button_pressed_y = -y + config_get_d(CONFIG_HEIGHT);
426     }
427     else
428     {
429         VIEWR_SET_R(0);
430         VIEWR_SET_L(0);
431     }
432     button_pressed = d;
433     dragging = 0;
434 }
435 #endif
436
437 static void play_loop_point(int id, int x, int y, int dx, int dy)
438 {
439 #ifndef __MAEMO__
440     game_set_pos(dx, dy);
441 #else
442     maemo_play_loop_point(id, x, y, dx, dy);
443 #endif
444 }
445
446 static void play_loop_stick(int id, int a, float v, int bump)
447 {
448     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
449         game_set_z(v);
450     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
451         game_set_x(v);
452     if (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a))
453     {
454         VIEWR_SET_R(0);
455         VIEWR_SET_L(0);
456
457         if (v > 0) VIEWR_SET_R(v);
458         if (v < 0) VIEWR_SET_L(v);
459     }
460 }
461
462 static int play_loop_click(int b, int d)
463 {
464 #ifndef __MAEMO__
465     if (d)
466     {
467         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
468             VIEWR_SET_R(+1);
469         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
470             VIEWR_SET_L(-1);
471
472         click_camera(b);
473     }
474     else
475     {
476         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
477             VIEWR_SET_R(0);
478         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
479             VIEWR_SET_L(0);
480     }
481 #else
482     maemo_play_loop_click(b, d);
483 #endif
484
485     return 1;
486 }
487
488 static int play_loop_keybd(int c, int d)
489 {
490     if (d)
491     {
492         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
493             VIEWR_SET_R(+1);
494         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
495             VIEWR_SET_L(-1);
496         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
497             fast_rotate = 1;
498
499         keybd_camera(c);
500
501         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
502             progress_same_avail())
503         {
504             if (progress_same())
505                 goto_state(&st_play_ready);
506         }
507         if (config_tst_d(CONFIG_KEY_PAUSE, c))
508             goto_state(&st_pause);
509     }
510     else
511     {
512         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
513             VIEWR_SET_R(0);
514         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
515             VIEWR_SET_L(0);
516         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
517             fast_rotate = 0;
518     }
519
520     if (d && c == SDLK_F12 && config_cheat())
521         return goto_state(&st_look);
522
523     if (d && c == SDLK_F6)
524         show_hud = !show_hud;
525
526     if (d && c == SDLK_c && config_cheat())
527     {
528         progress_stat(GAME_GOAL);
529         return goto_state(&st_goal);
530     }
531     return 1;
532 }
533
534 static int play_loop_buttn(int b, int d)
535 {
536     if (d == 1)
537     {
538         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
539             pause_or_exit();
540
541         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
542             VIEWR_SET_R(+1);
543         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
544             VIEWR_SET_L(-1);
545         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
546             fast_rotate = 1;
547
548         buttn_camera(b);
549     }
550     else
551     {
552         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
553             VIEWR_SET_R(0);
554         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
555             VIEWR_SET_L(0);
556         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
557             fast_rotate = 0;
558     }
559     return 1;
560 }
561
562 /*---------------------------------------------------------------------------*/
563
564 static float phi;
565 static float theta;
566
567 static int look_enter(struct state *st, struct state *prev)
568 {
569     phi   = 0;
570     theta = 0;
571     return 0;
572 }
573
574 static void look_leave(struct state *st, struct state *next, int id)
575 {
576 }
577
578 static void look_paint(int id, float t)
579 {
580     game_client_draw(0, t);
581 }
582
583 static void look_point(int id, int x, int y, int dx, int dy)
584 {
585     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
586     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
587
588     if (phi > +90.0f) phi = +90.0f;
589     if (phi < -90.0f) phi = -90.0f;
590
591     if (theta > +180.0f) theta -= 360.0f;
592     if (theta < -180.0f) theta += 360.0f;
593
594     game_look(phi, theta);
595 }
596
597 static int look_keybd(int c, int d)
598 {
599     if (d && c == SDLK_F12)
600         return goto_state(&st_play_loop);
601
602     return 1;
603 }
604
605 static int look_buttn(int b, int d)
606 {
607     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
608         return goto_state(&st_play_loop);
609
610     return 1;
611 }
612
613 /*---------------------------------------------------------------------------*/
614
615 struct state st_play_ready = {
616     play_ready_enter,
617     shared_leave,
618     play_ready_paint,
619     play_ready_timer,
620     NULL,
621     NULL,
622     NULL,
623     play_ready_click,
624     play_ready_keybd,
625     play_ready_buttn
626 };
627
628 struct state st_play_set = {
629     play_set_enter,
630     shared_leave,
631     play_set_paint,
632     play_set_timer,
633     NULL,
634     NULL,
635     NULL,
636     play_set_click,
637     play_set_keybd,
638     play_set_buttn
639 };
640
641 struct state st_play_loop = {
642     play_loop_enter,
643     shared_leave,
644     play_loop_paint,
645     play_loop_timer,
646     play_loop_point,
647     play_loop_stick,
648     shared_angle,
649     play_loop_click,
650     play_loop_keybd,
651     play_loop_buttn
652 };
653
654 struct state st_look = {
655     look_enter,
656     look_leave,
657     look_paint,
658     NULL,
659     look_point,
660     NULL,
661     NULL,
662     NULL,
663     look_keybd,
664     look_buttn
665 };