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