Call the standalone level 00, not 99
[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     union cmd cmd;
318
319     VIEWR_SET_R(0);
320     VIEWR_SET_L(0);
321     fast_rotate = 0;
322
323     if (prev == &st_pause)
324         return 0;
325
326     audio_play(AUD_GO, 1.f);
327
328     game_server_fly(0.0f);
329
330     /* End first update. */
331
332     cmd.type = CMD_END_OF_UPDATE;
333     game_proxy_enq(&cmd);
334     game_client_sync(demo_fp);
335
336     show_hud = 1;
337
338     hud_update(0);
339
340     return play_loop_gui();
341 }
342
343 static void play_loop_paint(int id, float t)
344 {
345     game_client_draw(0, t);
346
347     if (show_hud)
348         hud_paint();
349
350     if (time_state() < 1.f)
351         gui_paint(id);
352 }
353
354 static void play_loop_timer(int id, float dt)
355 {
356     float k = (fast_rotate ?
357                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.0f :
358                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.0f);
359
360     gui_timer(id, dt);
361     hud_timer(dt);
362     game_set_rot(view_rotate.d == DIR_R ?
363                  view_rotate.R * k :
364                  view_rotate.L * k);
365     game_set_cam(config_get_d(CONFIG_CAMERA));
366
367     game_step_fade(dt);
368
369     game_server_step(dt);
370     game_client_sync(demo_fp);
371
372     switch (curr_status())
373     {
374     case GAME_GOAL:
375         progress_stat(GAME_GOAL);
376         goto_state(&st_goal);
377         break;
378
379     case GAME_FALL:
380         progress_stat(GAME_FALL);
381         goto_state(&st_fail);
382         break;
383
384     case GAME_TIME:
385         progress_stat(GAME_TIME);
386         goto_state(&st_fail);
387         break;
388
389     default:
390         progress_step();
391         break;
392     }
393 }
394
395 static void play_loop_point(int id, int x, int y, int dx, int dy)
396 {
397     game_set_pos(dx, dy);
398 }
399
400 static void play_loop_stick(int id, int a, float v, int bump)
401 {
402     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
403         game_set_z(v);
404     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
405         game_set_x(v);
406     if (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a))
407     {
408         VIEWR_SET_R(0);
409         VIEWR_SET_L(0);
410
411         if (v > 0) VIEWR_SET_R(v);
412         if (v < 0) VIEWR_SET_L(v);
413     }
414 }
415
416 static int play_loop_click(int b, int d)
417 {
418     if (d)
419     {
420         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
421             VIEWR_SET_R(+1);
422         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
423             VIEWR_SET_L(-1);
424
425         click_camera(b);
426     }
427     else
428     {
429         if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
430             VIEWR_SET_R(0);
431         if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
432             VIEWR_SET_L(0);
433     }
434
435     return 1;
436 }
437
438 static int play_loop_keybd(int c, int d)
439 {
440     if (d)
441     {
442         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
443             VIEWR_SET_R(+1);
444         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
445             VIEWR_SET_L(-1);
446         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
447             fast_rotate = 1;
448
449         keybd_camera(c);
450
451         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
452             progress_same_avail())
453         {
454             if (progress_same())
455                 goto_state(&st_play_ready);
456         }
457         if (config_tst_d(CONFIG_KEY_PAUSE, c))
458             goto_state(&st_pause);
459     }
460     else
461     {
462         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
463             VIEWR_SET_R(0);
464         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
465             VIEWR_SET_L(0);
466         if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
467             fast_rotate = 0;
468     }
469
470     if (d && c == SDLK_F12 && config_cheat())
471         return goto_state(&st_look);
472
473     if (d && c == SDLK_F6)
474         show_hud = !show_hud;
475
476     if (d && c == SDLK_c && config_cheat())
477     {
478         progress_stat(GAME_GOAL);
479         return goto_state(&st_goal);
480     }
481     return 1;
482 }
483
484 static int play_loop_buttn(int b, int d)
485 {
486     if (d == 1)
487     {
488         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
489             pause_or_exit();
490
491         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
492             VIEWR_SET_R(+1);
493         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
494             VIEWR_SET_L(-1);
495         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
496             fast_rotate = 1;
497
498         buttn_camera(b);
499     }
500     else
501     {
502         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
503             VIEWR_SET_R(0);
504         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
505             VIEWR_SET_L(0);
506         if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
507             fast_rotate = 0;
508     }
509     return 1;
510 }
511
512 /*---------------------------------------------------------------------------*/
513
514 static float phi;
515 static float theta;
516
517 static int look_enter(struct state *st, struct state *prev)
518 {
519     phi   = 0;
520     theta = 0;
521     return 0;
522 }
523
524 static void look_leave(struct state *st, struct state *next, int id)
525 {
526 }
527
528 static void look_paint(int id, float t)
529 {
530     game_client_draw(0, t);
531 }
532
533 static void look_point(int id, int x, int y, int dx, int dy)
534 {
535     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
536     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
537
538     if (phi > +90.0f) phi = +90.0f;
539     if (phi < -90.0f) phi = -90.0f;
540
541     if (theta > +180.0f) theta -= 360.0f;
542     if (theta < -180.0f) theta += 360.0f;
543
544     game_look(phi, theta);
545 }
546
547 static int look_keybd(int c, int d)
548 {
549     if (d && c == SDLK_F12)
550         return goto_state(&st_play_loop);
551
552     return 1;
553 }
554
555 static int look_buttn(int b, int d)
556 {
557     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
558         return goto_state(&st_play_loop);
559
560     return 1;
561 }
562
563 /*---------------------------------------------------------------------------*/
564
565 struct state st_play_ready = {
566     play_ready_enter,
567     shared_leave,
568     play_ready_paint,
569     play_ready_timer,
570     NULL,
571     NULL,
572     NULL,
573     play_ready_click,
574     play_ready_keybd,
575     play_ready_buttn
576 };
577
578 struct state st_play_set = {
579     play_set_enter,
580     shared_leave,
581     play_set_paint,
582     play_set_timer,
583     NULL,
584     NULL,
585     NULL,
586     play_set_click,
587     play_set_keybd,
588     play_set_buttn
589 };
590
591 struct state st_play_loop = {
592     play_loop_enter,
593     shared_leave,
594     play_loop_paint,
595     play_loop_timer,
596     play_loop_point,
597     play_loop_stick,
598     shared_angle,
599     play_loop_click,
600     play_loop_keybd,
601     play_loop_buttn
602 };
603
604 struct state st_look = {
605     look_enter,
606     look_leave,
607     look_paint,
608     NULL,
609     look_point,
610     NULL,
611     NULL,
612     NULL,
613     look_keybd,
614     look_buttn
615 };