]> icculus.org git repositories - crow/jumpnbump.git/blob - dos/input.c
Added AI by Ricardo Cruz. This also adds mouse controls.
[crow/jumpnbump.git] / dos / input.c
1 /*
2  * input.h
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2002 Florian Schulze - crow@icculus.org
6  *
7  * This file is part of Jump'n'Bump.
8  *
9  * Jump'n'Bump is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Jump'n'Bump is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "globals.h"
25
26 static int num_mouse_buttons;
27
28 int init_joy(void)
29 {
30         int c1;
31
32         outportb(0x201, 0);
33         while (c1 < 0x7fff) {
34                 if ((inportb(0x201) & 1) == 0)
35                         break;
36                 c1++;
37         }
38         if (c1 != 0x7fff)
39                 return 1;
40         else
41                 return 0;
42 }
43
44
45 void read_joy(void)
46 {
47         int c1;
48         int x, y;
49         int s1 = 0;
50         char flag;
51
52         c1 = x = y = flag = 0;
53         outportb(0x201, 0);
54
55         while (1) {
56
57                 s1 = inportb(0x201);
58
59                 if (x == 0) {
60                         if ((s1 & 1) == 0)
61                                 x = c1;
62                 }
63                 if (y == 0) {
64                         if ((s1 & 2) == 0)
65                                 y = c1;
66                 }
67                 if (x != 0 && y != 0)
68                         break;
69
70                 c1++;
71                 if (c1 == 0x7fff) {
72                         flag = 1;
73                         break;
74                 }
75
76         }
77
78         if (flag == 0) {
79                 joy.raw_x = x;
80                 joy.raw_y = y;
81
82                 if (joy.raw_x < joy.calib_data.x2)
83                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x2 - joy.calib_data.x1);
84                 else
85                         joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x3 - joy.calib_data.x2);
86                 if (joy.raw_y < joy.calib_data.y2)
87                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y2 - joy.calib_data.y1);
88                 else
89                         joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y3 - joy.calib_data.y2);
90
91                 if (joy.x < -1024)
92                         joy.x = -1024;
93                 if (joy.x > 1024)
94                         joy.x = 1024;
95                 if (joy.y < -1024)
96                         joy.y = -1024;
97                 if (joy.y > 1024)
98                         joy.y = 1024;
99
100                 s1 = inportb(0x201);
101                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
102                 joy.but2 = (((s1 >> 5) & 1) ^ 1);
103         } else {
104                 joy.raw_x = joy.calib_data.x2;
105                 joy.raw_y = joy.calib_data.y2;
106
107                 joy.x = joy.y = 0;
108
109                 joy.but1 = joy.but2 = 0;
110         }
111
112 }
113
114
115 int calib_joy(int type)
116 {
117         int c1;
118         int x, y;
119         int s1 = 0;
120         int num_times;
121         char flag = 0;
122
123         while (joy.but1 == 1) {
124                 s1 = inportb(0x201);
125                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
126                 if (key_pressed(1) == 1) {
127                         while (key_pressed(1) == 1);
128                         return 1;
129                 }
130         }
131
132         num_times = 0;
133
134         while (joy.but1 == 0) {
135
136                 c1 = x = y = flag = 0;
137                 outportb(0x201, 0);
138
139                 while (1) {
140
141                         s1 = inportb(0x201);
142
143                         if (x == 0) {
144                                 if ((s1 & 1) == 0)
145                                         x = c1;
146                         }
147                         if (y == 0) {
148                                 if ((s1 & 2) == 0)
149                                         y = c1;
150                         }
151                         if (x != 0 && y != 0)
152                                 break;
153
154                         c1++;
155                         if (c1 == 0x7fff) {
156                                 flag = 1;
157                                 break;
158                         }
159
160                 }
161
162                 joy.raw_x = x;
163                 joy.raw_y = y;
164
165                 s1 = inportb(0x201);
166                 joy.but1 = (((s1 >> 4) & 1) ^ 1);
167
168                 if (num_times < 0x7fffffff)
169                         num_times++;
170
171                 if (flag == 1)
172                         break;
173
174                 if (key_pressed(1) == 1) {
175                         while (key_pressed(1) == 1);
176                         return 1;
177                 }
178
179         }
180
181         if (num_times < 16)
182                 return 1;
183
184         if (flag == 0) {
185
186                 switch (type) {
187                 case 0:
188                         joy.calib_data.x1 = joy.raw_x;
189                         joy.calib_data.y1 = joy.raw_y;
190                         break;
191                 case 1:
192                         joy.calib_data.x3 = joy.raw_x;
193                         joy.calib_data.y3 = joy.raw_y;
194                         break;
195                 case 2:
196                         joy.calib_data.x2 = joy.raw_x;
197                         joy.calib_data.y2 = joy.raw_y;
198                         break;
199                 }
200
201                 while (joy.but1 == 1) {
202                         s1 = inportb(0x201);
203                         joy.but1 = (((s1 >> 4) & 1) ^ 1);
204                 }
205
206         }
207
208         return 0;
209
210 }
211
212
213 int init_mouse(int *_num_buttons)
214 {
215         __dpmi_regs regs;
216         int mouse_enabled, num_mouse_buttons;
217
218         regs.x.ax = 0;
219         __dpmi_int(0x33, &regs);
220         if (regs.x.ax == 0xffff) {
221                 mouse_enabled = 1;
222                 num_mouse_buttons = regs.x.bx;
223                 if (force2 == 1)
224                         num_mouse_buttons = 2;
225                 if (force3 == 1)
226                         num_mouse_buttons = 3;
227         } else {
228                 mouse_enabled = 0;
229         }
230         if (_num_buttons)
231                 _num_buttons = num_mouse_buttons;
232
233         return mouse_enabled;
234 }
235
236
237 void read_mouse(void)
238 {
239
240         regs.x.ax = 3;
241         __dpmi_int(0x33, &regs);
242         mouse.but1 = regs.x.bx & 1;
243         mouse.but2 = (regs.x.bx & 2) >> 1;
244         mouse.but3 = (regs.x.bx & 4) >> 2;
245
246 }
247
248
249 void update_player_actions(void)
250 {
251         if (main_info.mouse_enabled == 1)
252                 read_mouse();
253         if (main_info.joy_enabled == 1)
254                 read_joy();
255         player[0].action_left   = key_pressed(KEY_PL1_LEFT) == 1;
256         player[0].action_right  = key_pressed(KEY_PL1_RIGHT) == 1;
257         player[0].action_up     = key_pressed(KEY_PL1_JUMP) == 1;
258         player[1].action_left   = key_pressed(KEY_PL2_LEFT) == 1;
259         player[1].action_right  = key_pressed(KEY_PL2_RIGHT) == 1;
260         player[1].action_up     = key_pressed(KEY_PL2_JUMP) == 1;
261         player[2].action_left   = key_pressed(KEY_PL3_LEFT) == 1;
262         player[2].action_right  = key_pressed(KEY_PL3_RIGHT) == 1;
263         player[2].action_up     = key_pressed(KEY_PL3_JUMP) == 1;
264         player[3].action_left   = key_pressed(KEY_PL4_LEFT) == 1;
265         player[3].action_right  = key_pressed(KEY_PL4_RIGHT) == 1;
266         player[3].action_up     = key_pressed(KEY_PL4_JUMP) == 1;
267 }
268
269
270 void init_inputs(void)
271 {
272         main_info.mouse_enabled = init_mouse(&num_mouse_buttons);
273         main_info.joy_enabled = init_joy(&num_mouse_buttons);
274 }
275