Cheat mode is now activated by setting player's name to CHEATER instead
[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 "game.h"
18 #include "demo.h"
19 #include "levels.h"
20 #include "audio.h"
21 #include "config.h"
22 #include "st_shared.h"
23
24 #include "st_play.h"
25 #include "st_goal.h"
26 #include "st_fall_out.h"
27 #include "st_time_out.h"
28 #include "st_start.h"
29 #include "st_pause.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 static int view_rotate;
34
35 static int pause_or_exit(void)
36 {
37     if (SDL_GetModState() & (KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_META))
38     {
39         level_stop(GAME_NONE, curr_clock(), curr_coins());
40         config_clr_grab();
41         return goto_state(&st_start);
42     }
43     else
44         return goto_pause();
45 }
46
47 /*---------------------------------------------------------------------------*/
48
49 static int play_ready_enter(void)
50 {
51     int id;
52
53     if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
54     {
55         gui_layout(id, 0, 0);
56         gui_pulse(id, 1.2f);
57     }
58
59     audio_play(AUD_READY, 1.0f);
60     config_set_grab(1);
61
62     return id;
63 }
64
65 static void play_ready_timer(int id, float dt)
66 {
67     float t = time_state();
68
69     game_set_fly(1.0f - 0.5f * t);
70
71     if (dt > 0.0f && t > 1.0f)
72         goto_state(&st_play_set);
73
74     game_step_fade(dt);
75     gui_timer(id, dt);
76     audio_timer(dt);
77 }
78
79 static int play_ready_click(int b, int d)
80 {
81     return (b < 0 && d == 1) ? goto_state(&st_play_loop) : 1;
82 }
83
84 static int play_ready_keybd(int c, int d)
85 {
86     if (d)
87         if (config_tst_d(CONFIG_KEY_PAUSE, c))
88             goto_pause();
89     return 1;
90 }
91
92 static int play_ready_buttn(int b, int d)
93 {
94     if (d)
95     {
96         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
97             return goto_state(&st_play_loop);
98         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
99             return pause_or_exit();
100     }
101     return 1;
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 static int play_set_enter(void)
107 {
108     int id;
109
110     if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
111     {
112         gui_layout(id, 0, 0);
113         gui_pulse(id, 1.2f);
114     }
115
116     audio_play(AUD_SET, 1.f);
117
118     clear_pause();
119
120     return id;
121 }
122
123 static void play_set_timer(int id, float dt)
124 {
125     float t = time_state();
126
127     game_set_fly(0.5f - 0.5f * t);
128
129     if (dt > 0.0f && t > 1.0f)
130         goto_state(&st_play_loop);
131
132     game_step_fade(dt);
133     gui_timer(id, dt);
134     audio_timer(dt);
135 }
136
137 static int play_set_click(int b, int d)
138 {
139     if (b < 0 && d == 1)
140     {
141         game_set_fly(0.0f);
142         return goto_state(&st_play_loop);
143     }
144     return 1;
145 }
146
147 static int play_set_keybd(int c, int d)
148 {
149     if (d)
150         if (config_tst_d(CONFIG_KEY_PAUSE, c))
151             goto_pause();
152     return 1;
153 }
154
155 static int play_set_buttn(int b, int d)
156 {
157     if (d)
158     {
159         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
160             return goto_state(&st_play_loop);
161         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
162             return pause_or_exit();
163     }
164     return 1;
165 }
166
167 /*---------------------------------------------------------------------------*/
168
169 static int nohud = 0;
170
171 static int play_loop_enter(void)
172 {
173     int id;
174
175     if (is_paused())
176     {
177         clear_pause();
178         view_rotate = 0;
179         return 0;
180     }
181
182     if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
183     {
184         gui_layout(id, 0, 0);
185         gui_pulse(id, 1.2f);
186     }
187
188     audio_play(AUD_GO, 1.f);
189
190     game_set_fly(0.f);
191     view_rotate = 0;
192
193     hud_view_pulse(config_get_d(CONFIG_CAMERA));
194
195     nohud = 0;
196
197     hud_update(0);
198
199     return id;
200 }
201
202 static void play_loop_paint(int id, float st)
203 {
204     game_draw(0, st);
205
206     if (!nohud)
207         hud_paint();
208
209     if (time_state() < 1.f)
210         gui_paint(id);
211 }
212
213 #define CAMERA_MODIFIERS (KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_META)
214
215 static void play_loop_timer(int id, float dt)
216 {
217     float k = ((SDL_GetModState() & CAMERA_MODIFIERS) ?
218                (float) config_get_d(CONFIG_ROTATE_FAST) / 100.f:
219                (float) config_get_d(CONFIG_ROTATE_SLOW) / 100.f);
220
221     static float at = 0;
222
223     float g[3] = { 0.0f, -9.8f, 0.0f };
224
225     at = (7 * at + dt) / 8;
226
227     gui_timer(id, at);
228     hud_timer(at);
229     game_set_rot(view_rotate * k);
230
231     switch (game_step(g, at, 1))
232     {
233     case GAME_GOAL:
234         level_stop(GAME_GOAL, curr_clock(), curr_coins());
235         goto_state(&st_goal);
236         break;
237
238     case GAME_FALL:
239         level_stop(GAME_FALL, curr_clock(), curr_coins());
240         goto_state(&st_fall_out);
241         break;
242
243     case GAME_TIME:
244         level_stop(GAME_TIME, curr_clock(), curr_coins());
245         goto_state(&st_time_out);
246         break;
247
248     default:
249         break;
250     }
251
252     game_step_fade(dt);
253     demo_play_step(at);
254     audio_timer(dt);
255 }
256
257 static void play_loop_point(int id, int x, int y, int dx, int dy)
258 {
259     game_set_pos(dx, dy);
260 }
261
262 static void play_loop_stick(int id, int a, int k)
263 {
264     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
265         game_set_z(k);
266     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
267         game_set_x(k);
268 }
269
270 static int play_loop_click(int b, int d)
271 {
272     view_rotate = d ? b : 0;
273     return 1;
274 }
275
276 static int play_loop_keybd(int c, int d)
277 {
278     if (d)
279     {
280         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
281             view_rotate = +1;
282         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
283             view_rotate = -1;
284
285         if (config_tst_d(CONFIG_KEY_CAMERA_1, c))
286         {
287             config_set_d(CONFIG_CAMERA, 0);
288             hud_view_pulse(0);
289         }
290         if (config_tst_d(CONFIG_KEY_CAMERA_2, c))
291         {
292             config_set_d(CONFIG_CAMERA, 1);
293             hud_view_pulse(1);
294         }
295         if (config_tst_d(CONFIG_KEY_CAMERA_3, c))
296         {
297             config_set_d(CONFIG_CAMERA, 2);
298             hud_view_pulse(2);
299         }
300         if (config_tst_d(CONFIG_KEY_RESTART, c) &&
301             curr_lg()->mode != MODE_CHALLENGE)
302         {
303             level_stop(GAME_NONE, curr_clock(), curr_coins());
304             level_play(curr_lg()->level, curr_lg()->mode);
305             goto_state(&st_play_ready);
306         }
307         if (config_tst_d(CONFIG_KEY_PAUSE, c))
308             goto_pause();
309     }
310     else
311     {
312         if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
313             view_rotate = 0;
314         if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
315             view_rotate = 0;
316     }
317
318     if (d && c == SDLK_F12 && config_cheat())
319         return goto_state(&st_look);
320
321     if (d && c == SDLK_F6)
322         nohud = !nohud;
323
324     if (d && c == SDLK_c && config_cheat())
325     {
326         level_stop(GAME_GOAL, curr_clock(), curr_coins());
327         return goto_state(&st_goal);
328     }
329     return 1;
330 }
331
332 static int play_loop_buttn(int b, int d)
333 {
334     if (d == 1)
335     {
336         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
337             pause_or_exit();
338
339         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
340             view_rotate = +1;
341         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
342             view_rotate = -1;
343
344         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_1, b))
345         {
346             config_set_d(CONFIG_CAMERA, 0);
347             hud_view_pulse(0);
348         }
349         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_2, b))
350         {
351             config_set_d(CONFIG_CAMERA, 1);
352             hud_view_pulse(1);
353         }
354         if (config_tst_d(CONFIG_JOYSTICK_CAMERA_3, b))
355         {
356             config_set_d(CONFIG_CAMERA, 2);
357             hud_view_pulse(2);
358         }
359     }
360     else
361     {
362         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
363             view_rotate = 0;
364         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
365             view_rotate = 0;
366     }
367     return 1;
368 }
369
370 /*---------------------------------------------------------------------------*/
371
372 static float phi;
373 static float theta;
374
375 static int look_enter(void)
376 {
377     phi   = 0;
378     theta = 0;
379     return 0;
380 }
381
382 static void look_leave(int id)
383 {
384 }
385
386 static void look_paint(int id, float st)
387 {
388     game_draw(0, st);
389 }
390
391 static void look_point(int id, int x, int y, int dx, int dy)
392 {
393     phi   +=  90.0f * dy / config_get_d(CONFIG_HEIGHT);
394     theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
395
396     if (phi > +90.0f) phi = +90.0f;
397     if (phi < -90.0f) phi = -90.0f;
398
399     if (theta > +180.0f) theta -= 360.0f;
400     if (theta < -180.0f) theta += 360.0f;
401
402     game_look(phi, theta);
403 }
404
405 static int look_keybd(int c, int d)
406 {
407     if (d && c == SDLK_F12)
408         return goto_state(&st_play_loop);
409
410     return 1;
411 }
412
413 static int look_buttn(int b, int d)
414 {
415     if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
416         return goto_state(&st_play_loop);
417
418     return 1;
419 }
420
421 /*---------------------------------------------------------------------------*/
422
423 struct state st_play_ready = {
424     play_ready_enter,
425     shared_leave,
426     shared_paint,
427     play_ready_timer,
428     NULL,
429     NULL,
430     play_ready_click,
431     play_ready_keybd,
432     play_ready_buttn,
433     1, 0
434 };
435
436 struct state st_play_set = {
437     play_set_enter,
438     shared_leave,
439     shared_paint,
440     play_set_timer,
441     NULL,
442     NULL,
443     play_set_click,
444     play_set_keybd,
445     play_set_buttn,
446     1, 0
447 };
448
449 struct state st_play_loop = {
450     play_loop_enter,
451     shared_leave,
452     play_loop_paint,
453     play_loop_timer,
454     play_loop_point,
455     play_loop_stick,
456     play_loop_click,
457     play_loop_keybd,
458     play_loop_buttn,
459     0, 0
460 };
461
462 struct state st_look = {
463     look_enter,
464     look_leave,
465     look_paint,
466     NULL,
467     look_point,
468     NULL,
469     NULL,
470     look_keybd,
471     look_buttn,
472     0, 0
473 };