Added AI by Ricardo Cruz. This also adds mouse controls.
[crow/jumpnbump.git] / sdl / input.c
1 /*
2  * input.c
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
6  *
7  * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
8  *
9  * This file is part of Jump'n'Bump.
10  *
11  * Jump'n'Bump is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * Jump'n'Bump is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include "globals.h"
27
28 static int num_joys=0;
29 static SDL_Joystick *joys[4];
30
31 /* assumes joysticks have at least one button, could check numbuttons first? */
32 #define JOY_LEFT(num) (num_joys>num && SDL_JoystickGetAxis(joys[num], 0)<-3200)
33 #define JOY_RIGHT(num) (num_joys>num && SDL_JoystickGetAxis(joys[num], 0)>3200)
34 /* I find using the vertical axis to be annoying -- dnb */
35 #define JOY_JUMP(num) (num_joys>num && SDL_JoystickGetButton(joys[num], 0))
36
37 int calib_joy(int type)
38 {
39         return 1;
40 }
41
42 void update_player_actions(void)
43 {
44         int tmp;
45
46         if (client_player_num < 0) {
47                 tmp = (key_pressed(KEY_PL1_LEFT) == 1) || JOY_LEFT(3);
48                 if (tmp != player[0].action_left)
49                         tellServerPlayerMoved(0, MOVEMENT_LEFT, tmp);
50                 tmp = (key_pressed(KEY_PL1_RIGHT) == 1) || JOY_RIGHT(3);
51                 if (tmp != player[0].action_right)
52                         tellServerPlayerMoved(0, MOVEMENT_RIGHT, tmp);
53                 tmp = (key_pressed(KEY_PL1_JUMP) == 1) || JOY_JUMP(3);
54                 if (tmp != player[0].action_up)
55                         tellServerPlayerMoved(0, MOVEMENT_UP, tmp);
56
57                 tmp = (key_pressed(KEY_PL2_LEFT) == 1) || JOY_LEFT(2);
58                 if (tmp != player[1].action_left)
59                         tellServerPlayerMoved(1, MOVEMENT_LEFT, tmp);
60                 tmp = (key_pressed(KEY_PL2_RIGHT) == 1) || JOY_RIGHT(2);
61                 if (tmp != player[1].action_right)
62                         tellServerPlayerMoved(1, MOVEMENT_RIGHT, tmp);
63                 tmp = (key_pressed(KEY_PL2_JUMP) == 1) || JOY_JUMP(2);
64                 if (tmp != player[1].action_up)
65                         tellServerPlayerMoved(1, MOVEMENT_UP, tmp);
66
67                 tmp = (key_pressed(KEY_PL3_LEFT) == 1) || JOY_LEFT(1);
68                 if (tmp != player[2].action_left)
69                         tellServerPlayerMoved(2, MOVEMENT_LEFT, tmp);
70                 tmp = (key_pressed(KEY_PL3_RIGHT) == 1) || JOY_RIGHT(1);
71                 if (tmp != player[2].action_right)
72                         tellServerPlayerMoved(2, MOVEMENT_RIGHT, tmp);
73                 tmp = (key_pressed(KEY_PL3_JUMP) == 1) || JOY_JUMP(1);
74                 if (tmp != player[2].action_up)
75                         tellServerPlayerMoved(2, MOVEMENT_UP, tmp);
76
77                 tmp = (key_pressed(KEY_PL4_LEFT) == 1) || JOY_LEFT(0);
78                 if (tmp != player[3].action_left)
79                 tellServerPlayerMoved(3, MOVEMENT_LEFT, tmp);
80                 tmp = (key_pressed(KEY_PL4_RIGHT) == 1) || JOY_RIGHT(0);
81                 if (tmp != player[3].action_right)
82                 tellServerPlayerMoved(3, MOVEMENT_RIGHT, tmp);
83                 tmp = (key_pressed(KEY_PL4_JUMP) == 1) || JOY_JUMP(0);
84                 if (tmp != player[3].action_up)
85                 tellServerPlayerMoved(3, MOVEMENT_UP, tmp);
86         } else {
87                 tmp = (key_pressed(KEY_PL1_LEFT) == 1) || JOY_LEFT(0);
88                 if (tmp != player[client_player_num].action_left)
89                         tellServerPlayerMoved(client_player_num, MOVEMENT_LEFT, tmp);
90                 tmp = (key_pressed(KEY_PL1_RIGHT) == 1) || JOY_RIGHT(0);
91                 if (tmp != player[client_player_num].action_right)
92                         tellServerPlayerMoved(client_player_num, MOVEMENT_RIGHT, tmp);
93                 tmp = (key_pressed(KEY_PL1_JUMP) == 1) || JOY_JUMP(0);
94                 if (tmp != player[client_player_num].action_up)
95                         tellServerPlayerMoved(client_player_num, MOVEMENT_UP, tmp);
96         }
97 }
98
99 void init_inputs(void)
100 {
101         int i;
102
103         num_joys = SDL_NumJoysticks();
104         for(i = 0; i < 4 && i < num_joys; ++i)
105                 joys[i] = SDL_JoystickOpen(i);
106
107         main_info.mouse_enabled = 0;
108         main_info.joy_enabled = 0;
109 }