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);
429 if (button_pressed && !dragging)
430 goto_state(&st_pause);
439 static void play_loop_point(int id, int x, int y, int dx, int dy)
442 game_set_pos(dx, dy);
444 maemo_play_loop_point(id, x, y, dx, dy);
448 static void play_loop_stick(int id, int a, float v, int bump)
450 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
452 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
454 if (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a))
459 if (v > 0) VIEWR_SET_R(v);
460 if (v < 0) VIEWR_SET_L(v);
464 static int play_loop_click(int b, int d)
469 if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
471 if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
478 if (config_tst_d(CONFIG_MOUSE_CAMERA_R, b))
480 if (config_tst_d(CONFIG_MOUSE_CAMERA_L, b))
484 maemo_play_loop_click(b, d);
490 static int play_loop_keybd(int c, int d)
494 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
496 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
498 if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
503 if (config_tst_d(CONFIG_KEY_RESTART, c) &&
504 progress_same_avail())
507 goto_state(&st_play_ready);
509 if (config_tst_d(CONFIG_KEY_PAUSE, c))
510 goto_state(&st_pause);
514 if (config_tst_d(CONFIG_KEY_CAMERA_R, c))
516 if (config_tst_d(CONFIG_KEY_CAMERA_L, c))
518 if (config_tst_d(CONFIG_KEY_ROTATE_FAST, c))
522 if (d && c == SDLK_F12 && config_cheat())
523 return goto_state(&st_look);
525 if (d && c == SDLK_F6)
526 show_hud = !show_hud;
528 if (d && c == SDLK_c && config_cheat())
530 progress_stat(GAME_GOAL);
531 return goto_state(&st_goal);
536 static int play_loop_buttn(int b, int d)
540 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
543 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
545 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
547 if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
554 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_R, b))
556 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_L, b))
558 if (config_tst_d(CONFIG_JOYSTICK_ROTATE_FAST, b))
564 /*---------------------------------------------------------------------------*/
569 static int look_enter(struct state *st, struct state *prev)
576 static void look_leave(struct state *st, struct state *next, int id)
580 static void look_paint(int id, float t)
582 game_client_draw(0, t);
585 static void look_point(int id, int x, int y, int dx, int dy)
587 phi += 90.0f * dy / config_get_d(CONFIG_HEIGHT);
588 theta += 180.0f * dx / config_get_d(CONFIG_WIDTH);
590 if (phi > +90.0f) phi = +90.0f;
591 if (phi < -90.0f) phi = -90.0f;
593 if (theta > +180.0f) theta -= 360.0f;
594 if (theta < -180.0f) theta += 360.0f;
596 game_look(phi, theta);
599 static int look_keybd(int c, int d)
601 if (d && c == SDLK_F12)
602 return goto_state(&st_play_loop);
607 static int look_buttn(int b, int d)
609 if (d && config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
610 return goto_state(&st_play_loop);
615 /*---------------------------------------------------------------------------*/
617 struct state st_play_ready = {
631 struct state st_play_set = {
645 struct state st_play_loop = {
659 struct state st_look = {