2 * Copyright (C) 2003 Robert Kooima
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.
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.
24 #include "game_common.h"
25 #include "game_server.h"
26 #include "game_proxy.h"
27 #include "game_client.h"
34 #include "st_shared.h"
36 /*---------------------------------------------------------------------------*/
38 static int pause_or_exit(void)
40 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
42 return goto_state(&st_pause);
46 progress_stat(GAME_NONE);
51 return goto_state(&st_exit);
55 /*---------------------------------------------------------------------------*/
57 static void set_camera(int c)
59 config_set_d(CONFIG_CAMERA, c);
63 static void toggle_camera(void)
65 int cam = (config_tst_d(CONFIG_CAMERA, VIEW_MANUAL) ?
66 VIEW_CHASE : VIEW_MANUAL);
71 static void keybd_camera(int c)
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);
80 if (config_tst_d(CONFIG_KEY_CAMERA_TOGGLE, c))
84 static void click_camera(int b)
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);
93 if (config_tst_d(CONFIG_MOUSE_CAMERA_TOGGLE, b))
97 static void buttn_camera(int b)
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);
106 if (config_tst_d(CONFIG_JOYSTICK_CAMERA_TOGGLE, b))
110 /*---------------------------------------------------------------------------*/
112 static int play_ready_gui(void)
116 if ((id = gui_label(0, _("Ready?"), GUI_LRG, GUI_ALL, 0, 0)))
118 gui_layout(id, 0, 0);
125 static int play_ready_enter(struct state *st, struct state *prev)
127 audio_play(AUD_READY, 1.0f);
130 hud_view_pulse(config_get_d(CONFIG_CAMERA));
132 return play_ready_gui();
135 static void play_ready_paint(int id, float t)
137 game_client_draw(0, t);
142 static void play_ready_timer(int id, float dt)
144 float t = time_state();
146 game_client_fly(1.0f - 0.5f * t);
148 if (dt > 0.0f && t > 1.0f)
149 goto_state(&st_play_set);
156 static int play_ready_click(int b, int d)
162 if (b == SDL_BUTTON_LEFT)
163 goto_state(&st_play_loop);
168 static int play_ready_keybd(int c, int d)
174 if (config_tst_d(CONFIG_KEY_PAUSE, c))
175 goto_state(&st_pause);
180 static int play_ready_buttn(int b, int d)
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();
194 /*---------------------------------------------------------------------------*/
196 static int play_set_gui(void)
200 if ((id = gui_label(0, _("Set?"), GUI_LRG, GUI_ALL, 0, 0)))
202 gui_layout(id, 0, 0);
209 static int play_set_enter(struct state *st, struct state *prev)
211 audio_play(AUD_SET, 1.f);
213 return play_set_gui();
216 static void play_set_paint(int id, float t)
218 game_client_draw(0, t);
223 static void play_set_timer(int id, float dt)
225 float t = time_state();
227 game_client_fly(0.5f - 0.5f * t);
229 if (dt > 0.0f && t > 1.0f)
230 goto_state(&st_play_loop);
237 static int play_set_click(int b, int d)
243 if (b == SDL_BUTTON_LEFT)
244 goto_state(&st_play_loop);
249 static int play_set_keybd(int c, int d)
255 if (config_tst_d(CONFIG_KEY_PAUSE, c))
256 goto_state(&st_pause);
261 static int play_set_buttn(int b, int d)
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();
275 /*---------------------------------------------------------------------------*/
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); \
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); \
299 static int fast_rotate;
302 static int play_loop_gui(void)
306 if ((id = gui_label(0, _("GO!"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
308 gui_layout(id, 0, 0);
315 static int play_loop_enter(struct state *st, struct state *prev)
321 if (prev == &st_pause)
324 audio_play(AUD_GO, 1.f);
326 game_client_fly(0.0f);
331 return play_loop_gui();
334 static void play_loop_paint(int id, float t)
336 game_client_draw(0, t);
341 if (time_state() < 1.f)
345 static void play_loop_timer(int id, float dt)
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);
353 game_set_rot(view_rotate.d == DIR_R ?
356 game_set_cam(config_get_d(CONFIG_CAMERA));
360 game_server_step(dt);
361 game_client_sync(demo_fp);
362 game_client_blend(game_server_blend());
364 switch (curr_status())
367 progress_stat(GAME_GOAL);
368 goto_state(&st_goal);
372 progress_stat(GAME_FALL);
373 goto_state(&st_fail);
377 progress_stat(GAME_TIME);
378 goto_state(&st_fail);
388 #define TOUCH_SENSITIVITY 20
389 static int button_pressed = 0;
390 static int button_pressed_x = 0;
391 static int button_pressed_y = 0;
392 static int dragging = 0;
394 static void maemo_play_loop_point(int id, int x, int y, int dx, int dy)
396 if (!dragging && button_pressed)
398 /* check how much we've moved */
399 if (abs(x - button_pressed_x) > TOUCH_SENSITIVITY ||
400 abs(y - button_pressed_y) > TOUCH_SENSITIVITY)
408 view_rotate.d = DIR_R;
409 view_rotate.R = dx / 15.0;
413 view_rotate.d = DIR_L;
414 view_rotate.L = dx / 15.0;
419 static void maemo_play_loop_click(int b, int d)
424 SDL_GetMouseState(&button_pressed_x, &y);
425 button_pressed_y = -y + config_get_d(CONFIG_HEIGHT);
437 static void play_loop_point(int id, int x, int y, int dx, int dy)
440 game_set_pos(dx, dy);
442 maemo_play_loop_point(id, x, y, dx, dy);
446 static void play_loop_stick(int id, int a, float v, int bump)
448 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
450 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
452 if (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a))
457 if (v > 0) VIEWR_SET_R(v);
458 if (v < 0) VIEWR_SET_L(v);
462 static int play_loop_click(int b, int d)
467 if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
469 if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
476 if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
478 if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
482 maemo_play_loop_click(b, d);
488 static int play_loop_keybd(int c, int d)
492 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
494 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
496 if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
501 if (config_tst_d(CONFIG_KEY_RESTART, c) &&
502 progress_same_avail())
505 goto_state(&st_play_ready);
507 if (config_tst_d(CONFIG_KEY_PAUSE, c))
508 goto_state(&st_pause);
512 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
514 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
516 if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
520 if (d && c == SDLK_F12 && config_cheat())
521 return goto_state(&st_look);
523 if (d && c == SDLK_F6)
524 show_hud = !show_hud;
526 if (d && c == SDLK_c && config_cheat())
528 progress_stat(GAME_GOAL);
529 return goto_state(&st_goal);
534 static int play_loop_buttn(int b, int d)
538 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
541 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
543 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
545 if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
552 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
554 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
556 if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
562 /*---------------------------------------------------------------------------*/
567 static int look_enter(struct state *st, struct state *prev)
574 static void look_leave(struct state *st, struct state *next, int id)
578 static void look_paint(int id, float t)
580 game_client_draw(0, t);
583 static void look_point(int id, int x, int y, int dx, int dy)
585 phi += 90.0f * dy / config_get_d(CONFIG_HEIGHT);
586 theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
588 if (phi > +90.0f) phi = +90.0f;
589 if (phi < -90.0f) phi = -90.0f;
591 if (theta > +180.0f) theta -= 360.0f;
592 if (theta < -180.0f) theta += 360.0f;
594 game_look(phi, theta);
597 static int look_keybd(int c, int d)
599 if (d && c == SDLK_F12)
600 return goto_state(&st_play_loop);
605 static int look_buttn(int b, int d)
607 if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
608 return goto_state(&st_play_loop);
613 /*---------------------------------------------------------------------------*/
615 struct state st_play_ready = {
628 struct state st_play_set = {
641 struct state st_play_loop = {
654 struct state st_look = {