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