Split SOL data structures into base, varying and rendering parts
[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
363     switch (curr_status())
364     {
365     case GAME_GOAL:
366         progress_stat(GAME_GOAL);
367         goto_state(&st_goal);
368         break;
369
370     case GAME_FALL:
371         progress_stat(GAME_FALL);
372         goto_state(&st_fail);
373         break;
374
375     case GAME_TIME:
376         progress_stat(GAME_TIME);
377         goto_state(&st_fail);
378         break;
379
380     default:
381         progress_step();
382         break;
383     }
384 }
385
386 static void play_loop_point(int id, int x, int y, int dx, int dy)
387 {
388     game_set_pos(dx, dy);
389 }
390
391 static void play_loop_stick(int id, int a, float v, int bump)
392 {
393     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
394         game_set_z(v);
395     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
396         game_set_x(v);
397     if (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a))
398     {
399         VIEWR_SET_R(0);
400         VIEWR_SET_L(0);
401
402         if (v > 0) VIEWR_SET_R(v);
403         if (v < 0) VIEWR_SET_L(v);
404     }
405 }
406
407 static int play_loop_click(int b, int d)
408 {
409     if (d)
410     {
411         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
412             VIEWR_SET_R(+1);
413         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
414             VIEWR_SET_L(-1);
415
416         click_camera(b);
417     }
418     else
419     {
420         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
421             VIEWR_SET_R(0);
422         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
423             VIEWR_SET_L(0);
424     }
425
426     return 1;
427 }
428
429 static int play_loop_keybd(int c, int d)
430 {
431     if (d)
432     {
433         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
434             VIEWR_SET_R(+1);
435         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
436             VIEWR_SET_L(-1);
437         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
438             fast_rotate = 1;
439
440         keybd_camera(c);
441
442         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
443             progress_same_avail())
444         {
445             if (progress_same())
446                 goto_state(&st_play_ready);
447         }
448         if (config_tst_d(CONFIG_KEY_PAUSE, c))
449             goto_state(&st_pause);
450     }
451     else
452     {
453         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
454             VIEWR_SET_R(0);
455         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
456             VIEWR_SET_L(0);
457         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
458             fast_rotate = 0;
459     }
460
461     if (d && c == SDLK_F12 && config_cheat())
462         return goto_state(&st_look);
463
464     if (d && c == SDLK_F6)
465         show_hud = !show_hud;
466
467     if (d && c == SDLK_c && config_cheat())
468     {
469         progress_stat(GAME_GOAL);
470         return goto_state(&st_goal);
471     }
472     return 1;
473 }
474
475 static int play_loop_buttn(int b, int d)
476 {
477     if (d == 1)
478     {
479         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
480             pause_or_exit();
481
482         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
483             VIEWR_SET_R(+1);
484         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
485             VIEWR_SET_L(-1);
486         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
487             fast_rotate = 1;
488
489         buttn_camera(b);
490     }
491     else
492     {
493         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
494             VIEWR_SET_R(0);
495         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
496             VIEWR_SET_L(0);
497         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
498             fast_rotate = 0;
499     }
500     return 1;
501 }
502
503 /*---------------------------------------------------------------------------*/
504
505 static float phi;
506 static float theta;
507
508 static int look_enter(struct state *st, struct state *prev)
509 {
510     phi   = 0;
511     theta = 0;
512     return 0;
513 }
514
515 static void look_leave(struct state *st, struct state *next, int id)
516 {
517 }
518
519 static void look_paint(int id, float t)
520 {
521     game_client_draw(0, t);
522 }
523
524 static void look_point(int id, int x, int y, int dx, int dy)
525 {
526     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
527     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
528
529     if (phi > +90.0f) phi = +90.0f;
530     if (phi < -90.0f) phi = -90.0f;
531
532     if (theta > +180.0f) theta -= 360.0f;
533     if (theta < -180.0f) theta += 360.0f;
534
535     game_look(phi, theta);
536 }
537
538 static int look_keybd(int c, int d)
539 {
540     if (d && c == SDLK_F12)
541         return goto_state(&st_play_loop);
542
543     return 1;
544 }
545
546 static int look_buttn(int b, int d)
547 {
548     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
549         return goto_state(&st_play_loop);
550
551     return 1;
552 }
553
554 /*---------------------------------------------------------------------------*/
555
556 struct state st_play_ready = {
557     play_ready_enter,
558     shared_leave,
559     play_ready_paint,
560     play_ready_timer,
561     NULL,
562     NULL,
563     NULL,
564     play_ready_click,
565     play_ready_keybd,
566     play_ready_buttn
567 };
568
569 struct state st_play_set = {
570     play_set_enter,
571     shared_leave,
572     play_set_paint,
573     play_set_timer,
574     NULL,
575     NULL,
576     NULL,
577     play_set_click,
578     play_set_keybd,
579     play_set_buttn
580 };
581
582 struct state st_play_loop = {
583     play_loop_enter,
584     shared_leave,
585     play_loop_paint,
586     play_loop_timer,
587     play_loop_point,
588     play_loop_stick,
589     shared_angle,
590     play_loop_click,
591     play_loop_keybd,
592     play_loop_buttn
593 };
594
595 struct state st_look = {
596     look_enter,
597     look_leave,
598     look_paint,
599     NULL,
600     look_point,
601     NULL,
602     NULL,
603     NULL,
604     look_keybd,
605     look_buttn
606 };