]> icculus.org git repositories - btb/d2x.git/blob - main/kconfig.c
merge old and new keyboard config screen code
[btb/d2x.git] / main / kconfig.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Routines to configure keyboard, joystick, etc..
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #ifdef WINDOWS
25 #include "desw.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <ctype.h>
33
34 #include "error.h"
35 #include "pstypes.h"
36 #include "gr.h"
37 #include "mono.h"
38 #include "key.h"
39 #include "palette.h"
40 #include "game.h"
41 #include "gamefont.h"
42 #include "iff.h"
43 #include "u_mem.h"
44 #include "joy.h"
45 #include "mouse.h"
46 #include "kconfig.h"
47 #include "gauges.h"
48 #include "joydefs.h"
49 #include "songs.h"
50 #include "render.h"
51 #include "digi.h"
52 #include "newmenu.h"
53 #include "endlevel.h"
54 #include "multi.h"
55 #include "timer.h"
56 #include "text.h"
57 #include "player.h"
58 #include "menu.h"
59 #include "automap.h"
60 #include "args.h"
61 #include "lighting.h"
62 #include "ai.h"
63 #include "cntrlcen.h"
64 #if defined (TACTILE)
65  #include "tactile.h"
66 #endif
67
68 #include "collide.h"
69
70 #ifdef USE_LINUX_JOY
71 #include "joystick.h"
72 #endif
73 #include "console.h"
74
75 ubyte ExtGameStatus=1;
76
77 vms_vector ExtForceVec;
78 vms_matrix ExtApplyForceMatrix;
79
80 int ExtJoltInfo[3]={0,0,0};
81 int ExtXVibrateInfo[2]={0,0};
82 int ExtYVibrateInfo[2]={0,0};
83 ubyte ExtXVibrateClear=0;
84 ubyte ExtYVibrateClear=0;
85
86 #define TABLE_CREATION 1
87
88 // Array used to 'blink' the cursor while waiting for a keypress.
89 sbyte fades[64] = { 1,1,1,2,2,3,4,4,5,6,8,9,10,12,13,15,16,17,19,20,22,23,24,26,27,28,28,29,30,30,31,31,31,31,31,30,30,29,28,28,27,26,24,23,22,20,19,17,16,15,13,12,10,9,8,6,5,4,4,3,2,2,1,1 };
90
91 //char * invert_text[2] = { "N", "Y" };
92 //char * joybutton_text[28] = { "BTN 1", "BTN 2", "BTN 3", "BTN 4", "", "TRIG", "LEFT", "HAT \x81", "RIGHT", "", "", "HAT \x80", "MID", "", "", "HAT \x7f", "", "", "", "HAT \x82", "TRIG", "LEFT", "RIGHT", "", "UP","DOWN","LEFT", "RIGHT" };
93 //char * joyaxis_text[4] = { "X1", "Y1", "X2", "Y2" };
94 //char * mouseaxis_text[2] = { "L/R", "F/B" };
95 //char * mousebutton_text[3] = { "Left", "Right", "Mid" };
96
97 int invert_text[2] = { TNUM_N, TNUM_Y };
98
99 #ifndef USE_LINUX_JOY
100 #ifdef WINDOWS
101         int joybutton_text[28] = 
102         { TNUM_BTN_1, TNUM_BTN_2, TNUM_BTN_3, TNUM_BTN_4,
103           -1, -1, -1, -1,
104           -1, -1, -1, -1,
105           -1, -1, -1, -1,
106           TNUM_HAT_L, TNUM_HAT_R, TNUM_HAT_U, TNUM_HAT_D,
107           -1, -1, -1, -1,
108           -1, -1, -1, -1
109         };
110         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
111 #elif defined(SDL_INPUT)
112 char *joybutton_text[JOY_MAX_BUTTONS];
113 char *joyaxis_text[JOY_MAX_AXES];
114 #else
115         int joybutton_text[28] = 
116         { TNUM_BTN_1, TNUM_BTN_2, TNUM_BTN_3, TNUM_BTN_4,
117           -1, TNUM_TRIG, TNUM_LEFT, TNUM_HAT_L,
118          TNUM_RIGHT, -1, TNUM_HAT2_D, TNUM_HAT_R,
119          TNUM_MID, -1, TNUM_HAT2_R, TNUM_HAT_U,
120          TNUM_HAT2_L, -1, TNUM_HAT2_U, TNUM_HAT_D,
121          TNUM_TRIG, TNUM_LEFT, TNUM_RIGHT, -1, 
122          TNUM_UP, TNUM_DOWN, TNUM_LEFT, TNUM_RIGHT };
123
124         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
125 //      int joyaxis_text[4] = { TNUM_X1, TNUM_Y1, TNUM_X2, TNUM_Y2 };
126 #endif
127 #endif
128
129 int mouseaxis_text[3] = { TNUM_L_R, TNUM_F_B, TNUM_Z1 };
130 #ifndef MACINTOSH
131 int mousebutton_text[3] = { TNUM_LEFT, TNUM_RIGHT, TNUM_MID };
132 char * mousebutton_textra[13] = { "M4", "M5", "M6", "M7", "M8", "M9", "M10","M11","M12","M13","M14","M15","M16" };//text for buttons above 3. -MPM
133 #else
134 char *mousebutton_text[3] = { "Btn", "", "" };          // only one silly mouse button on the mac
135 #endif
136
137 #ifdef MACINTOSH
138 char * key_text[256] = {         
139 "","S","D","F","H","G","Z","X","C","V","","B","Q", "W", "E", "R",
140 "Y","T","1","2","3","4","6","5","=","9","7","-", "8", "0", "]", "O",
141 "U","[","I","P","RET","L","J","'","K", ";", "\\", ",", "/", "N", "M", ".",
142 "TAB","SPC","`","DEL","","ESC","","APL","SHIFT","CAPSL","OPTN","CTRL","","","","A",
143 "","PAD.","","PAD*","","PAD+","","NMLCK","","","","PAD/","ENTER","","PAD-","",
144 "","PAD=","PAD0","PAD1","PAD2","PAD3","PAD4","PAD5","PAD6","PAD7","","PAD8","PAD9","","","",
145 "F5","F6","F7","","F8","F9","","F11","","F13","","F14","","F10","","F12",
146 "","PAUSE","HELP","HOME","PGUP","DEL","","END","F2","","F1","LARW","RARW","DARW","UARW","",
147 "","","","","","","","","","","","","","","","",
148 "","","","","","","","","","","","","","","","",
149 "","","","","","","","","","","","","","","","",
150 "","","","","","","","","","","","","","","","",
151 "","","","","","","","","","","","","","","","",
152 "","","","","","","","","","","","","","","","",
153 "","","","","","","","","","","","","","","","",
154 "","","","","","","","","","","","","","","",""};
155 #else
156 #if !defined OGL && !defined SDL_INPUT
157 char * key_text[256] = {         \
158 "","ESC","1","2","3","4","5","6","7","8","9","0","-",                   \
159 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",                           \
160 "P","[","]","\x83","LCTRL","A","S","D","F",        \
161 "G","H","J","K","L",";","'","`",        \
162 "LSHFT","\\","Z","X","C","V","B","N","M",",",      \
163 ".","/","RSHFT","PAD*","LALT","SPC",      \
164 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",        \
165 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",   \
166 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0", \
167 "PAD.","","","","F11","F12","","","","","","","","","",         \
168 "","","","","","","","","","","","","","","","","","","","",     \
169 "","","","","","","","","","","","","","","","","","","","",     \
170 "","","","","","","","","","","","","","","","","","",           \
171 "PAD\x83","RCTRL","","","","","","","","","","","","","", \
172 "","","","","","","","","","","PAD/","","","RALT","",      \
173 "","","","","","","","","","","","","","HOME","\x82","PGUP",     \
174 "","\x81","","\x7f","","END","\x80","PGDN","INS",       \
175 "DEL","","","","","","","","","","","","","","","","","",     \
176 "","","","","","","","","","","","","","","","","","","","",     \
177 "","","","","","","" };
178 #endif /* OGL */
179 #endif
180
181 #ifdef D2X_KEYS
182 //added/removed by Victor Rachels for adding rebindable keys for these
183 // KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0
184 ubyte system_keys[] = { KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_MINUS, KEY_EQUAL, KEY_PRINT_SCREEN };
185 #else
186 ubyte system_keys[] = { KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS, KEY_EQUAL, KEY_PRINT_SCREEN };
187 #endif
188
189 //extern void GameLoop(int, int );
190
191 extern void transfer_energy_to_shield(fix);
192 extern void CyclePrimary(),CycleSecondary(),InitMarkerInput();
193 extern ubyte DefiningMarkerMessage;
194 extern char CybermouseActive;
195
196 #ifdef WINDOWS
197 extern int joydefsw_do_button();
198 extern int joydefsw_do_winjoybutton(int *axis);
199 extern joydefsw_win_joyselect(char *title);
200 #endif
201
202 control_info Controls;
203
204 fix Cruise_speed=0;
205
206 // macros for drawing lo/hi res kconfig screens (see scores.c as well)
207
208 #define LHX(x)          ((x)*(MenuHires?2:1))
209 #define LHY(y)          ((y)*(MenuHires?2.4:1))
210
211
212 #define BT_KEY                          0
213 #define BT_MOUSE_BUTTON         1
214 #define BT_MOUSE_AXIS           2
215 #define BT_JOY_BUTTON           3
216 #define BT_JOY_AXIS                     4
217 #define BT_INVERT                               5
218
219 char *btype_text[] = { "BT_KEY", "BT_MOUSE_BUTTON", "BT_MOUSE_AXIS", "BT_JOY_BUTTON", "BT_JOY_AXIS", "BT_INVERT" };
220
221 #define INFO_Y 28
222
223 typedef struct kc_item {
224         short id;                               // The id of this item
225         short x, y;                             
226         short w1;
227         short w2;
228         short u,d,l,r;
229         //short text_num1;
230         char *text;
231         ubyte type;
232         ubyte value;            // what key,button,etc
233 } kc_item;
234
235 int Num_items=28;
236 kc_item *All_items;
237
238 ubyte kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS];
239
240 //added on 2/4/99 by Victor Rachels to add d1x new keys
241 ubyte kconfig_d2x_settings[MAX_D2X_CONTROLS];
242 //end this section addition - VR
243
244 //----------- WARNING!!!!!!! -------------------------------------------
245 // THESE NEXT FOUR BLOCKS OF DATA ARE GENERATED BY PRESSING DEL+F12 WHEN
246 // IN THE KEYBOARD CONFIG SCREEN.  BASICALLY, THAT PROCEDURE MODIFIES THE
247 // U,D,L,R FIELDS OF THE ARRAYS AND DUMPS THE NEW ARRAYS INTO KCONFIG.COD
248 //-------------------------------------------------------------------------
249 /*ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
250 {0xc8,0x48,0xd0,0x50,0xcb,0x4b,0xcd,0x4d,0x38,0xff,0xff,0x4f,0xff,0x51,0xff,0x4a,0xff,0x4e,0xff,0xff,0x10,0x47,0x12,0x49,0x1d,0x9d,0x39,0xff,0x21,0xff,0x1e,0xff,0x2c,0xff,0x30,0xff,0x13,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf,0xff,0x1f,0xff,0x33,0xff,0x34,0xff,0x23,0xff,0x14,0xff,0xff,0xff,0x0,0x0},
251 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
252 {0x5,0xc,0xff,0xff,0xff,0xff,0x7,0xf,0x13,0xb,0xff,0x6,0x8,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
253 {0x0,0x1,0xff,0xff,0x2,0xff,0x7,0xf,0x13,0xb,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
254 {0x3,0x0,0x1,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
255 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
256 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
257 };*/                                                                              
258
259 #ifndef MACINTOSH
260 ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
261 {0xc8,0x48,0xd0,0x50,0xcb,0x4b,0xcd,0x4d,0x38,0xff,0xff,0x4f,0xff,0x51,0xff,0x4a,0xff,0x4e,0xff,0xff,0x10,0x47,0x12,0x49,0x1d,0x9d,0x39,0xff,0x21,0xff,0x1e,0xff,0x2c,0xff,0x30,0xff,0x13,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf,0xff,0x1f,0xff,0x33,0xff,0x34,0xff,0x23,0xff,0x14,0xff,0xff,0xff,0x0,0x0},
262 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
263 {0x5,0xc,0xff,0xff,0xff,0xff,0x7,0xf,0x13,0xb,0xff,0x6,0x8,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0},
264 {0x0,0x1,0xff,0xff,0x2,0xff,0x7,0xf,0x13,0xb,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
265 {0x3,0x0,0x1,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
266 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
267 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
268 #ifdef WINDOWS
269 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
270 #endif
271 };
272
273
274
275 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
276         {  0, 15, 49, 71, 26, 55,  2, 55,  1,"Pitch forward", BT_KEY, 255 },
277         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
278         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
279         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
280         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
281         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
282         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
283         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
284         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
285         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
286         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
287         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
288         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
289         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
290         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
291         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
292         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
293         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
294         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
295         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
296         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
297         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
298         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
299         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
300         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
301         { 25,158, 49,112, 26, 54, 27, 24,  2,"Fire primary", BT_KEY, 255 },
302         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
303         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
304         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
305         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
306         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
307         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
308         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
309         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
310         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
311         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
312         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
313         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
314         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
315         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
316         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
317         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
318         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
319         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
320         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
321         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
322         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
323         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
324         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
325         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
326         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
327         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
328         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
329         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
330         { 54,158,171, 83, 26, 52, 56, 51, 55,"Energy->Shield", BT_KEY, 255 },
331         { 55,158,171,112, 26, 53,  0, 54,  0,"Energy->Shield", BT_KEY, 255 },
332    { 56,158,179,83,  26, 54,  0, 0,  0, "Toggle Bomb",  BT_KEY,255},
333 };
334 char *kc_bind_text[NUM_KEY_CONTROLS] = {
335         "+lookdown",
336         "+lookdown",
337         "+lookup",
338         "+lookup",
339         "+left",
340         "+left",
341         "+right",
342         "+right",
343         "+strafe",
344         "+strafe",
345         "+moveleft",
346         "+moveleft",
347         "+moveright",
348         "+moveright",
349         "+moveup",
350         "+moveup",
351         "+movedown",
352         "+movedown",
353         "+bank",
354         "+bank",
355         "+bankleft",
356         "+bankleft",
357         "+bankright",
358         "+bankright",
359         "+attack",
360         "+attack",
361         "+attack2",
362         "+attack2",
363         "+flare",
364         "+flare",
365         "+forward",
366         "+forward",
367         "+back",
368         "+back",
369         "+bomb",
370         "+bomb",
371         "+rearview",
372         "+rearview",
373         "+cruiseup",
374         "+cruiseup",
375         "+cruisedown",
376         "+cruisedown",
377         "+cruiseoff",
378         "+cruiseoff",
379         "+automap",
380         "+automap",
381         "+afterburner",
382         "+afterburner",
383         "+cycle",
384         "+cycle",
385         "+cycle2",
386         "+cycle2",
387         "+headlight",
388         "+headlight",
389         "+nrgshield",
390         "+nrgshield",
391         "+togglebomb",
392 };
393 kc_item kc_joystick[NUM_OTHER_CONTROLS] = {
394         {  0, 25, 46, 85, 40, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
395         {  1, 25, 54, 85, 40,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
396         {  2, 25, 85, 85, 40, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
397         {  3, 25, 93, 85, 40,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
398         {  4, 25, 62, 85, 40,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
399         {  5,180, 46, 79, 40, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
400         {  6,180, 54, 79, 40,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
401         {  7,180, 62, 79, 40,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
402         {  8,180, 70, 79, 40,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
403         {  9,180, 78, 79, 40,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
404         { 10,180, 90, 79, 40,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
405         { 11,180, 98, 79, 40, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
406         { 12,180,106, 79, 40, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
407         { 13, 22,154, 51, 40, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
408         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
409         { 15, 22,162, 51, 40, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
410         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
411         { 17,164,154, 58, 40, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
412         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
413         { 19,164,162, 58, 40, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
414         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
415         { 21,164,172, 58, 40, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
416         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
417         { 23,164,180, 58, 40, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
418         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
419         { 25, 25,109, 85, 40,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
420         { 26, 25, 70, 85, 40,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
421         { 27, 25,117, 85, 40, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
422         { 28,180,114, 79, 40, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
423         { 29,180,122, 79, 40, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
424         { 30, 25,125, 85, 40, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
425 };
426 kc_item kc_superjoy[NUM_OTHER_CONTROLS] = {
427         {  0, 25, 46, 85, 26, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
428         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
429         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
430         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
431         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
432         {  5,180, 46, 79, 26, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
433         {  6,180, 54, 79, 26,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
434         {  7,180, 62, 79, 26,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
435         {  8,180, 70, 79, 26,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
436         {  9,180, 78, 79, 26,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
437         { 10,180, 90, 79, 26,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
438         { 11,180, 98, 79, 26, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
439         { 12,180,106, 79, 26, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
440         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
441         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
442         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
443         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
444         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
445         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
446         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
447         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
448         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
449         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
450         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
451         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
452         { 25, 25,109, 85, 26,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
453         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
454         { 27, 25,117, 85, 26, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
455         { 28,180,114, 79, 26, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
456         { 29,180,122, 79, 26, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
457         { 30, 25,125, 85, 26, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
458 };
459
460 kc_item kc_mouse[NUM_OTHER_CONTROLS] = {
461         {  0, 25, 46, 85, 26, 12,  1, 24,  5,"Fire primary", BT_MOUSE_BUTTON, 255 },
462         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_MOUSE_BUTTON, 255 },
463         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_MOUSE_BUTTON, 255 },
464         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_MOUSE_BUTTON, 255 },
465         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_MOUSE_BUTTON, 255 },
466         {  5,180, 46, 59, 26, 24,  6,  0,  1,"Slide on", BT_MOUSE_BUTTON, 255 },
467         {  6,180, 54, 59, 26,  5,  7,  1,  4,"Slide left", BT_MOUSE_BUTTON, 255 },
468         {  7,180, 62, 59, 26,  6,  8,  4, 26,"Slide right", BT_MOUSE_BUTTON, 255 },
469         {  8,180, 70, 59, 26,  7,  9, 26,  9,"Slide up", BT_MOUSE_BUTTON, 255 },
470         {  9,180, 78, 59, 26,  8, 10,  8,  2,"Slide down", BT_MOUSE_BUTTON, 255 },
471         { 10,180, 90, 59, 26,  9, 11,  2,  3,"Bank on", BT_MOUSE_BUTTON, 255 },
472         { 11,180, 98, 59, 26, 10, 12,  3, 12,"Bank left", BT_MOUSE_BUTTON, 255 },
473         { 12,180,106, 59, 26, 11,  0, 11, 25,"Bank right", BT_MOUSE_BUTTON, 255 },
474         { 13,103,138, 58, 26, 27, 15, 25, 14,"Pitch U/D", BT_MOUSE_AXIS, 255 },
475         { 14,103,138,106,  8, 23, 16, 13, 15,"Pitch U/D", BT_INVERT, 255 },
476         { 15,103,146, 58, 26, 13, 17, 14, 16,"Turn L/R", BT_MOUSE_AXIS, 255 },
477         { 16,103,146,106,  8, 14, 18, 15, 17,"Turn L/R", BT_INVERT, 255 },
478         { 17,103,154, 58, 26, 15, 19, 16, 18,"Slide L/R", BT_MOUSE_AXIS, 255 },
479         { 18,103,154,106,  8, 16, 20, 17, 19,"Slide L/R", BT_INVERT, 255 },
480         { 19,103,162, 58, 26, 17, 21, 18, 20,"Slide U/D", BT_MOUSE_AXIS, 255 },
481         { 20,103,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
482         { 21,103,170, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_MOUSE_AXIS, 255 },
483         { 22,103,170,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
484         { 23,103,182, 58, 26, 21, 14, 22, 24,"throttle", BT_MOUSE_AXIS, 255 },
485         { 24,103,182,106,  8, 22,  5, 23,  0,"throttle", BT_INVERT, 255 },
486         { 25, 25,109, 85, 26,  3, 27, 12, 13,"REAR VIEW", BT_MOUSE_BUTTON, 255 },
487         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_MOUSE_BUTTON, 255 },
488         { 27, 25,117, 85, 26, 25, 13, 25, 13,"Afterburner", BT_MOUSE_BUTTON, 255 },
489 };
490
491 #ifdef D2X_KEYS
492 //added on 2/4/99 by Victor Rachels to add d1x new keys
493 kc_item kc_d2x[NUM_D2X_CONTROLS] = {
494 //        id,x,y,w1,w2,u,d,l,r,text_num1,type,value
495         {  0, 15, 49, 71, 26, 27,  2, 27,  1, "WEAPON 1", BT_KEY, 255},
496         {  1, 15, 49,100, 26, 26,  3,  0,  2, "WEAPON 1", BT_JOY_BUTTON, 255},
497         {  2, 15, 57, 71, 26,  0,  4,  1,  3, "WEAPON 2", BT_KEY, 255},
498         {  3, 15, 57,100, 26,  1,  5,  2,  4, "WEAPON 2", BT_JOY_BUTTON, 255},
499         {  4, 15, 65, 71, 26,  2,  6,  3,  5, "WEAPON 3", BT_KEY, 255},
500         {  5, 15, 65,100, 26,  3,  7,  4,  6, "WEAPON 3", BT_JOY_BUTTON, 255},
501         {  6, 15, 73, 71, 26,  4,  8,  5,  7, "WEAPON 4", BT_KEY, 255},
502         {  7, 15, 73,100, 26,  5,  9,  6,  8, "WEAPON 4", BT_JOY_BUTTON, 255},
503         {  8, 15, 81, 71, 26,  6, 10,  7,  9, "WEAPON 5", BT_KEY, 255},
504         {  9, 15, 81,100, 26,  7, 11,  8, 10, "WEAPON 5", BT_JOY_BUTTON, 255},
505
506         { 10, 15, 89, 71, 26,  8, 12,  9, 11, "WEAPON 6", BT_KEY, 255},
507         { 11, 15, 89,100, 26,  9, 13, 10, 12, "WEAPON 6", BT_JOY_BUTTON, 255},
508         { 12, 15, 97, 71, 26, 10, 14, 11, 13, "WEAPON 7", BT_KEY, 255},
509         { 13, 15, 97,100, 26, 11, 15, 12, 14, "WEAPON 7", BT_JOY_BUTTON, 255},
510         { 14, 15,105, 71, 26, 12, 16, 13, 15, "WEAPON 8", BT_KEY, 255},
511         { 15, 15,105,100, 26, 13, 17, 14, 16, "WEAPON 8", BT_JOY_BUTTON, 255},
512         { 16, 15,113, 71, 26, 14, 18, 15, 17, "WEAPON 9", BT_KEY, 255},
513         { 17, 15,113,100, 26, 15, 19, 16, 18, "WEAPON 9", BT_JOY_BUTTON, 255},
514         { 18, 15,121, 71, 26, 16, 20, 17, 19, "WEAPON 0", BT_KEY, 255},
515         { 19, 15,121,100, 26, 17, 21, 18, 20, "WEAPON 0", BT_JOY_BUTTON, 255},
516
517         //{ 20, 15,131, 71, 26, 18, 22, 19, 21, "CYC PRIMARY", BT_KEY, 255},
518         //{ 21, 15,131,100, 26, 19, 23, 20, 22, "CYC PRIMARY", BT_JOY_BUTTON, 255},
519         //{ 22, 15,139, 71, 26, 20, 24, 21, 23, "CYC SECONDARY", BT_KEY, 255},
520         //{ 23, 15,139,100, 26, 21, 25, 22, 24, "CYC SECONDARY", BT_JOY_BUTTON, 255},
521         //{ 24,  8,147, 78, 26, 22, 26, 23, 25, "TOGGLE_PRIM AUTO", BT_KEY, 255},
522         //{ 25,  8,147,107, 26, 23, 27, 24, 26, "TOGGLE_PRIM_AUTO", BT_JOY_BUTTON, 255},
523         //{ 26,  8,155, 78, 26, 24,  1, 25, 27, "TOGGLE SEC AUTO", BT_KEY, 255},
524         //{ 27,  8,155,107, 26, 25,  0, 26,  0, "TOGGLE SEC AUTO", BT_JOY_BUTTON, 255},
525 };
526 //end this section addition - VR
527 #endif
528
529 #else           // ifndef MACINTOSH (following are macintosh controls)
530
531 ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
532 {0x5b,0x7e,0x54,0x7d,0x56,0x7b,0x58,0x7c,0x3a,0xff,0xff,0x53,0xff,0x55,0xff,0x4e,0xff,0x45,0xff,0xff,0xc,0x59,0xe,0x5c,0x3b,0x24,0x31,0xff,0x3,0xff,0x3f,0xff,0x6,0xff,0xb,0xff,0xf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x30,0xff,0x1,0xff,0x2b,0xff,0x2f,0xff,0x4,0xff,0x11,0xff,0xff,0xff,0x0,0x0},
533 {0x0,0x1,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
534 {0x0,0x3,0xff,0xff,0xff,0xff,0xb,0xc,0x9,0xa,0xff,0x1,0x2,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0},
535 {0x5,0x4,0xff,0xff,0x6,0xff,0x3,0x2,0x0,0x1,0xff,0x8,0xa,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x3,0x1,0xb,0x7,0xd,0xe,0xf,0xc,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
536 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
537 {0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0},
538 {0x0,0x1,0xff,0xff,0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0},
539 };
540
541 ubyte default_firebird_settings[MAX_CONTROLS] =
542 {0x0,0x1,0xff,0xff,0x2,0xff,0x4,0x6,0x5,0x7,0xff,0xb,0xc,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0x03,0x0,0xff,0x3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0};
543
544 ubyte default_mousestick_settings[MAX_CONTROLS] =
545 {0x2,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3,0x4,0x1,0x0,0x0,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x0,0x0,0x0,0x0,0x0};
546
547 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
548         {  0, 15, 49, 71, 26, 55,  2, 55,  1,"Pitch forward", BT_KEY, 255 },
549         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
550         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
551         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
552         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
553         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
554         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
555         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
556         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
557         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
558         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
559         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
560         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
561         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
562         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
563         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
564         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
565         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
566         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
567         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
568         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
569         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
570         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
571         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
572         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
573         { 25,158, 49,112, 26, 54, 27, 24,  2,"Fire primary", BT_KEY, 255 },
574         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
575         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
576         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
577         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
578         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
579         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
580         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
581         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
582         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
583         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
584         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
585         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
586         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
587         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
588         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
589         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
590         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
591         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
592         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
593         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
594         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
595         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
596         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
597         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
598         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
599         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
600         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
601         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
602         { 54,158,171, 83, 26, 52, 25, 51, 55,"Energy->Shield", BT_KEY, 255 },
603         { 55,158,171,112, 26, 53,  0, 54,  0,"Energy->Shield", BT_KEY, 255 },
604 };
605 kc_item kc_joystick[NUM_OTHER_CONTROLS] = {
606         {  0, 25, 46, 85, 28, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
607         {  1, 25, 54, 85, 28,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
608         {  2, 25, 85, 85, 28, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
609         {  3, 25, 93, 85, 28,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
610         {  4, 25, 62, 85, 28,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
611         {  5,180, 46, 79, 28, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
612         {  6,180, 54, 79, 28,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
613         {  7,180, 62, 79, 28,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
614         {  8,180, 70, 79, 28,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
615         {  9,180, 78, 79, 28,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
616         { 10,180, 90, 79, 28,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
617         { 11,180, 98, 79, 28, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
618         { 12,180,106, 79, 28, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
619         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
620         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
621         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
622         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
623         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
624         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
625         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
626         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
627         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
628         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
629         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
630         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
631         { 25, 25,109, 85, 28,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
632         { 26, 25, 70, 85, 28,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
633         { 27, 25,117, 85, 28, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
634         { 28,180,114, 79, 28, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
635         { 29,180,122, 79, 28, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
636         { 30, 25,125, 85, 28, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
637 };
638 kc_item kc_superjoy[NUM_OTHER_CONTROLS] = {
639         {  0, 25, 46, 85, 26, 15,  1, 24,  5,"Fire primary", BT_JOY_BUTTON, 255 },
640         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_JOY_BUTTON, 255 },
641         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_JOY_BUTTON, 255 },
642         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_JOY_BUTTON, 255 },
643         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_JOY_BUTTON, 255 },
644         {  5,180, 46, 79, 26, 23,  6,  0,  1,"Slide on", BT_JOY_BUTTON, 255 },
645         {  6,180, 54, 79, 26,  5,  7,  1,  4,"Slide left", BT_JOY_BUTTON, 255 },
646         {  7,180, 62, 79, 26,  6,  8,  4, 26,"Slide right", BT_JOY_BUTTON, 255 },
647         {  8,180, 70, 79, 26,  7,  9, 26,  9,"Slide up", BT_JOY_BUTTON, 255 },
648         {  9,180, 78, 79, 26,  8, 10,  8,  2,"Slide down", BT_JOY_BUTTON, 255 },
649         { 10,180, 90, 79, 26,  9, 11,  2,  3,"Bank on", BT_JOY_BUTTON, 255 },
650         { 11,180, 98, 79, 26, 10, 12,  3, 12,"Bank left", BT_JOY_BUTTON, 255 },
651         { 12,180,106, 79, 26, 11, 28, 11, 25,"Bank right", BT_JOY_BUTTON, 255 },
652         { 13, 22,154, 51, 26, 24, 15, 30, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
653         { 14, 22,154, 99,  8, 30, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
654         { 15, 22,162, 51, 26, 13,  0, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
655         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
656         { 17,164,154, 58, 26, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
657         { 18,164,154,106,  8, 29, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
658         { 19,164,162, 58, 26, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
659         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
660         { 21,164,172, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
661         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
662         { 23,164,180, 58, 26, 21,  5, 22, 24,"throttle", BT_JOY_AXIS, 255 },
663         { 24,164,180,106,  8, 22, 13, 23,  0,"throttle", BT_INVERT, 255 },
664         { 25, 25,109, 85, 26,  3, 27, 12, 28,"REAR VIEW", BT_JOY_BUTTON, 255 },
665         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_JOY_BUTTON, 255 },
666         { 27, 25,117, 85, 26, 25, 30, 28, 29,"Afterburner", BT_JOY_BUTTON, 255 },
667         { 28,180,114, 79, 26, 12, 29, 25, 27,"Cycle Primary", BT_JOY_BUTTON, 255 },
668         { 29,180,122, 79, 26, 28, 18, 27, 30,"Cycle Secondary", BT_JOY_BUTTON, 255 },
669         { 30, 25,125, 85, 26, 27, 14, 29, 13,"Headlight", BT_JOY_BUTTON, 255 },
670 };
671
672 kc_item kc_mouse[NUM_OTHER_CONTROLS] = {
673         {  0, 25, 46, 85, 26, 28,  1, 28,  5,"Fire primary", BT_MOUSE_BUTTON, 255 },
674         {  1, 25, 54, 85, 26,  0,  4,  5,  6,"Fire secondary", BT_MOUSE_BUTTON, 255 },
675         {  2, 25, 85, 85, 26, 26,  3,  9, 10,"Accelerate", BT_MOUSE_BUTTON, 255 },
676         {  3, 25, 93, 85, 26,  2, 25, 10, 11,"reverse", BT_MOUSE_BUTTON, 255 },
677         {  4, 25, 62, 85, 26,  1, 26,  6,  7,"Fire flare", BT_MOUSE_BUTTON, 255 },
678         {  5,180, 46, 59, 26, 24,  6,  0,  1,"Slide on", BT_MOUSE_BUTTON, 255 },
679         {  6,180, 54, 59, 26,  5,  7,  1,  4,"Slide left", BT_MOUSE_BUTTON, 255 },
680         {  7,180, 62, 59, 26,  6,  8,  4, 26,"Slide right", BT_MOUSE_BUTTON, 255 },
681         {  8,180, 70, 59, 26,  7,  9, 26,  9,"Slide up", BT_MOUSE_BUTTON, 255 },
682         {  9,180, 78, 59, 26,  8, 10,  8,  2,"Slide down", BT_MOUSE_BUTTON, 255 },
683         { 10,180, 90, 59, 26,  9, 11,  2,  3,"Bank on", BT_MOUSE_BUTTON, 255 },
684         { 11,180, 98, 59, 26, 10, 12,  3, 12,"Bank left", BT_MOUSE_BUTTON, 255 },
685         { 12,180,106, 59, 26, 11, 28, 11, 25,"Bank right", BT_MOUSE_BUTTON, 255 },
686         { 13,103,138, 58, 26, 27, 15, 27, 14,"Pitch U/D", BT_MOUSE_AXIS, 255 },
687         { 14,103,138,106,  8, 23, 16, 13, 15,"Pitch U/D", BT_INVERT, 255 },
688         { 15,103,146, 58, 26, 13, 17, 14, 16,"Turn L/R", BT_MOUSE_AXIS, 255 },
689         { 16,103,146,106,  8, 14, 18, 15, 17,"Turn L/R", BT_INVERT, 255 },
690         { 17,103,154, 58, 26, 15, 19, 16, 18,"Slide L/R", BT_MOUSE_AXIS, 255 },
691         { 18,103,154,106,  8, 16, 20, 17, 19,"Slide L/R", BT_INVERT, 255 },
692         { 19,103,162, 58, 26, 17, 21, 18, 20,"Slide U/D", BT_MOUSE_AXIS, 255 },
693         { 20,103,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
694         { 21,103,170, 58, 26, 19, 23, 20, 22,"Bank L/R", BT_MOUSE_AXIS, 255 },
695         { 22,103,170,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
696         { 23,103,182, 58, 26, 21, 14, 22, 24,"throttle", BT_MOUSE_AXIS, 255 },
697         { 24,103,182,106,  8, 22,  5, 23, 28,"throttle", BT_INVERT, 255 },
698         { 25, 25,109, 85, 26,  3, 27, 12, 27,"REAR VIEW", BT_MOUSE_BUTTON, 255 },
699         { 26, 25, 70, 85, 26,  4,  2,  7,  8,"Drop Bomb", BT_MOUSE_BUTTON, 255 },
700         { 27, 25,117, 85, 26, 25, 13, 25, 13,"Afterburner", BT_MOUSE_BUTTON, 255 },
701 };
702
703 #endif
704
705 //added on 2/4/99 by Victor Rachels to add new keys system
706 ubyte default_kconfig_d2x_settings[MAX_D2X_CONTROLS] = {
707  0x2 ,0xff,0x3 ,0xff,0x4 ,0xff,0x5 ,0xff,0x6 ,0xff,0x7 ,0xff,0x8 ,0xff,0x9 ,
708  0xff,0xa ,0xff,0xb ,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
709  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
710 //end this section addition - VR
711
712 void kc_drawitem( kc_item *item, int is_current );
713 void kc_change_key( kc_item * item );
714 void kc_change_joybutton( kc_item * item );
715 void kc_change_mousebutton( kc_item * item );
716 void kc_next_joyaxis(kc_item *item);  //added by WraithX on 11/22/00
717 void kc_change_joyaxis( kc_item * item );
718 void kc_change_mouseaxis( kc_item * item );
719 void kc_change_invert( kc_item * item );
720 void kconfig_read_fcs( int raw_axis );
721 void kconfig_set_fcs_button( int btn, int button );
722 void kconfig_read_external_controls( void );
723
724 // the following methods added by WraithX, 4/17/00
725 int isJoyRotationKey(int test_key)
726 {
727         if (test_key == kc_joystick[11].value ||
728             test_key == kc_joystick[12].value)
729         {
730                 return 1;
731         }// end if
732
733         // else...
734         return 0;
735 }// method isJoyRotationKey
736
737 int isMouseRotationKey(int test_key)
738 {
739         if (test_key == kc_mouse[11].value ||
740             test_key == kc_mouse[12].value)
741         {
742                 return 1;
743         }// end if
744
745         // else...
746         return 0;
747 }// method isMouseRotationKey
748
749 int isKeyboardRotationKey(int test_key)
750 {
751         if (test_key == kc_keyboard[0].value ||
752             test_key == kc_keyboard[1].value ||
753             test_key == kc_keyboard[2].value ||
754             test_key == kc_keyboard[3].value ||
755             test_key == kc_keyboard[4].value ||
756             test_key == kc_keyboard[5].value ||
757             test_key == kc_keyboard[6].value ||
758             test_key == kc_keyboard[7].value ||
759             test_key == kc_keyboard[20].value ||
760             test_key == kc_keyboard[21].value ||
761             test_key == kc_keyboard[22].value ||
762             test_key == kc_keyboard[23].value)
763         {
764                 return 1;
765         }// end if
766
767         // else...
768         return 0;
769 }// method isKeyboardRotationKey
770 // end addition - WraithX
771
772 int kconfig_is_axes_used(int axis)
773 {
774         int i;
775         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
776                 if (( kc_joystick[i].type == BT_JOY_AXIS ) && (kc_joystick[i].value == axis ))
777                         return 1;
778         }
779         return 0;
780 }
781
782 #ifdef TABLE_CREATION
783 int find_item_at( kc_item * items, int nitems, int x, int y )
784 {
785         int i;
786         
787         for (i=0; i<nitems; i++ )       {
788                 if ( ((items[i].x+items[i].w1)==x) && (items[i].y==y))
789                         return i;
790         }
791         return -1;
792 }
793
794 int find_next_item_up( kc_item * items, int nitems, int citem )
795 {
796         int x, y, i;
797
798         y = items[citem].y;
799         x = items[citem].x+items[citem].w1;
800         
801         do {    
802                 y--;
803                 if ( y < 0 ) {
804                         y = grd_curcanv->cv_bitmap.bm_h-1;
805                         x--;
806                         if ( x < 0 ) {
807                                 x = grd_curcanv->cv_bitmap.bm_w-1;
808                         }
809                 }
810                 i = find_item_at( items, nitems, x, y );
811         } while ( i < 0 );
812         
813         return i;
814 }
815
816 int find_next_item_down( kc_item * items, int nitems, int citem )
817 {
818         int x, y, i;
819
820         y = items[citem].y;
821         x = items[citem].x+items[citem].w1;
822         
823         do {    
824                 y++;
825                 if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
826                         y = 0;
827                         x++;
828                         if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
829                                 x = 0;
830                         }
831                 }
832                 i = find_item_at( items, nitems, x, y );
833         } while ( i < 0 );
834         
835         return i;
836 }
837
838 int find_next_item_right( kc_item * items, int nitems, int citem )
839 {
840         int x, y, i;
841
842         y = items[citem].y;
843         x = items[citem].x+items[citem].w1;
844         
845         do {    
846                 x++;
847                 if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
848                         x = 0;
849                         y++;
850                         if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
851                                 y = 0;
852                         }
853                 }
854                 i = find_item_at( items, nitems, x, y );
855         } while ( i < 0 );
856         
857         return i;
858 }
859
860 int find_next_item_left( kc_item * items, int nitems, int citem )
861 {
862         int x, y, i;
863
864         y = items[citem].y;
865         x = items[citem].x+items[citem].w1;
866         
867         do {    
868                 x--;
869                 if ( x < 0 ) {
870                         x = grd_curcanv->cv_bitmap.bm_w-1;
871                         y--;
872                         if ( y < 0 ) {
873                                 y = grd_curcanv->cv_bitmap.bm_h-1;
874                         }
875                 }
876                 i = find_item_at( items, nitems, x, y );
877         } while ( i < 0 );
878         
879         return i;
880 }
881 #endif
882
883 #ifdef NEWMENU_MOUSE
884 int get_item_height(kc_item *item)
885 {
886         int w, h, aw;
887         char btext[10];
888
889         if (item->value==255) {
890                 strcpy(btext, "");
891         } else {
892                 switch( item->type )    {
893                         case BT_KEY:
894                                 strncpy( btext, key_text[item->value], 10 ); break;
895                         case BT_MOUSE_BUTTON:
896 #ifndef MACINTOSH
897                                 strncpy( btext, Text_string[mousebutton_text[item->value]], 10); break;
898 #else
899                                 strncpy( btext, mousebutton_text[item->value], 10 ); break;
900 #endif
901                         case BT_MOUSE_AXIS:
902                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
903                         case BT_JOY_BUTTON:
904 #ifdef USE_LINUX_JOY
905                                 sprintf(btext, "J%d B%d", j_button[item->value].joydev, j_Get_joydev_button_number(item->value));
906 #elif defined(SDL_INPUT)
907                                 if (joybutton_text[item->value])
908                                         strncpy(btext, joybutton_text[item->value], 10);
909                                 else
910                                         sprintf(btext, "BTN%2d", item->value + 1);
911 #else
912                                 if ( joybutton_text[item->value] !=-1 )
913                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
914                                 else
915                                         sprintf( btext, "BTN%d", item->value );
916 #endif
917                                 break;
918                         case BT_JOY_AXIS:
919 #ifdef USE_LINUX_JOY
920                                 sprintf( btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number (item->value) );
921 #elif defined(SDL_INPUT)
922                                 if (joyaxis_text[item->value])
923                                         strncpy(btext, joyaxis_text[item->value], 10);
924                                 else
925                                         sprintf(btext, "AXIS%2d", item->value + 1);
926 #else
927                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
928 #endif
929                                 break;
930                         case BT_INVERT:
931                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
932                 }
933         }
934         gr_get_string_size(btext, &w, &h, &aw  );
935
936         return h;
937 }
938 #endif
939
940 void kconfig_sub(kc_item * items,int nitems, char * title)
941 {
942         grs_canvas * save_canvas;
943         grs_font * save_font;
944         int old_keyd_repeat;
945 #ifdef NEWMENU_MOUSE
946         int mouse_state, omouse_state, mx, my, x1, x2, y1, y2;
947         int close_x, close_y, close_size;
948 #endif
949
950         int i,k,ocitem,citem;
951         int time_stopped = 0;
952
953         All_items = items;
954         Num_items = nitems;
955
956         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
957         {
958                 time_stopped = 1;
959                 stop_time();
960         }
961
962 //      if (Config_control_type == CONTROL_WINJOYSTICK) {
963 //#ifdef WINDOWS
964 //              joydefsw_win_joyselect(title2); title = title2;
965 //#else
966 //              Int3(); // Get Samir...
967 //#endif
968 //      }
969
970         save_canvas = grd_curcanv;
971
972
973         gr_set_current_canvas(NULL);
974         save_font = grd_curcanv->cv_font;
975
976 #ifdef WINDOWS
977 KConfigPaint:
978 #endif
979         game_flush_inputs();
980         old_keyd_repeat = keyd_repeat;
981         keyd_repeat = 1;
982
983         //gr_clear_canvas( BM_XRGB(0,0,0) );
984
985         nm_draw_background(0, 0, grd_curcanv->cv_bitmap.bm_w - 1, grd_curcanv->cv_bitmap.bm_h - 1);
986    gr_palette_load (gr_palette);
987
988         grd_curcanv->cv_font = MEDIUM3_FONT;
989
990         {
991                 char * p;
992                 p = strchr( title, '\n' );
993                 if ( p ) *p = 32;
994                 gr_string( 0x8000, LHY(8), title );
995                 if ( p ) *p = '\n';
996         }
997
998
999 //      if ( items == kc_keyboard )     {
1000 //              gr_string( 0x8000, 8, "Keyboard" );
1001 //      } else if ( items == kc_joystick )      {
1002 //              gr_string( 0x8000, 8, "Joysticks" );
1003 //      } else if ( items == kc_mouse ) {
1004 //              gr_string( 0x8000, 8, "Mouse" );
1005 //      }
1006
1007 #ifdef NEWMENU_MOUSE
1008         close_x = close_y = MenuHires?15:7;
1009         close_size = MenuHires?10:5;
1010         gr_setcolor( BM_XRGB(0, 0, 0) );
1011         gr_rect(close_x, close_y, close_x + close_size, close_y + close_size);
1012         gr_setcolor( BM_XRGB(21, 21, 21) );
1013         gr_rect(close_x + LHX(1), close_y + LHX(1), close_x + close_size - LHX(1), close_y + close_size - LHX(1));
1014 #endif
1015
1016         grd_curcanv->cv_font = GAME_FONT;
1017         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1018
1019         #ifndef MACINTOSH
1020         gr_string( 0x8000, LHY(20), TXT_KCONFIG_STRING_1 );
1021         #else
1022         gr_string( 0x8000, LHY(20), "Enter changes, ctrl-d deletes, ctrl-r resets defaults, ESC exits");
1023         #endif
1024         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1025         if ( items == kc_keyboard )     {
1026                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
1027                 gr_setcolor( BM_XRGB(31,27,6) );
1028                 
1029                 gr_scanline( LHX(98), LHX(106), LHY(42) );
1030                 gr_scanline( LHX(120), LHX(128), LHY(42) );
1031                 gr_pixel( LHX(98), LHY(43) );                                           
1032                 gr_pixel( LHX(98), LHY(44) );                                           
1033                 gr_pixel( LHX(128), LHY(43) );                                          
1034                 gr_pixel( LHX(128), LHY(44) );                                          
1035                 
1036                 gr_string( LHX(109), LHY(40), "OR" );
1037
1038                 gr_scanline( LHX(253), LHX(261), LHY(42) );
1039                 gr_scanline( LHX(274), LHX(283), LHY(42) );
1040                 gr_pixel( LHX(253), LHY(43) );                                          
1041                 gr_pixel( LHX(253), LHY(44) );                                          
1042                 gr_pixel( LHX(283), LHY(43) );                                          
1043                 gr_pixel( LHX(283), LHY(44) );                                          
1044
1045                 gr_string( LHX(264), LHY(40), "OR" );
1046
1047         } if ( items == kc_joystick )   {
1048                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
1049                 gr_setcolor( BM_XRGB(31,27,6) );
1050                 gr_scanline( LHX(18), LHX(135), LHY(37) );
1051                 gr_scanline( LHX(181), LHX(294), LHY(37) );
1052                 gr_scanline( LHX(18), LHX(144), LHY(119+18) );
1053                 gr_scanline( LHX(174), LHX(294), LHY(119+18) );
1054                 gr_string( 0x8000, LHY(35), TXT_BUTTONS );
1055                 gr_string( 0x8000,LHY(117+18), TXT_AXES );
1056                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1057                 gr_string( LHX(81), LHY(137+8), TXT_AXIS );
1058                 gr_string( LHX(111), LHY(137+8), TXT_INVERT );
1059                 gr_string( LHX(230), LHY(137+8), TXT_AXIS );
1060                 gr_string( LHX(260), LHY(137+8), TXT_INVERT );
1061         } else if ( items == kc_mouse ) {
1062                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
1063                 gr_setcolor( BM_XRGB(31,27,6) );
1064                 gr_scanline( LHX(18), LHX(135), LHY(37) );
1065                 gr_scanline( LHX(181), LHX(294), LHY(37) );
1066                 gr_scanline( LHX(18), LHX(144), LHY(119+5) );
1067                 gr_scanline( LHX(174), LHX(294), LHY(119+5) );
1068                 gr_string( 0x8000, LHY(35), TXT_BUTTONS );
1069                 gr_string( 0x8000,LHY(117+5), TXT_AXES );
1070                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1071                 gr_string( LHX(169), LHY(129), TXT_AXIS );
1072                 gr_string( LHX(199), LHY(129), TXT_INVERT );
1073         }
1074 #ifdef D2X_KEYS
1075         else if ( items == kc_d2x )
1076         {
1077                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
1078                 gr_setcolor( BM_XRGB(31,27,6) );
1079
1080                 gr_string(LHX( 94), LHY(40), "KB");
1081                 gr_string(LHX(121), LHY(40), "JOY");
1082         }
1083 #endif
1084
1085         for (i=0; i<nitems; i++ )       {
1086                 kc_drawitem( &items[i], 0 );
1087         }
1088
1089         citem = 0;
1090         kc_drawitem( &items[citem], 1 );
1091
1092         newmenu_show_cursor();
1093
1094 #ifdef NEWMENU_MOUSE
1095         mouse_state = omouse_state = 0;
1096 #endif
1097
1098         while(1)                {
1099         //      Windows addendum to allow for kconfig input.
1100                 gr_update();
1101
1102                 //see if redbook song needs to be restarted
1103                 songs_check_redbook_repeat();
1104
1105                 k = key_inkey();
1106
1107 #ifdef NEWMENU_MOUSE
1108                 omouse_state = mouse_state;
1109                 mouse_state = mouse_button_state(0);
1110 #endif
1111
1112                 if ( !time_stopped ) {
1113                         #ifdef NETWORK
1114                         if (multi_menu_poll() == -1)
1115                                 k = -2;
1116                         #endif
1117                 }
1118                 ocitem = citem;
1119                 switch( k )     {
1120                 case KEY_BACKSP:
1121                         Int3();
1122                         break;
1123                 case KEY_COMMAND+KEY_SHIFTED+KEY_3:
1124                 case KEY_PRINT_SCREEN:
1125                         save_screen_shot(0);
1126                         break;                                                  
1127                 case KEY_CTRLED+KEY_D:
1128                         items[citem].value = 255;
1129                         kc_drawitem( &items[citem], 1 );
1130                         break;
1131                 case KEY_CTRLED+KEY_R:  
1132                         if ( items==kc_keyboard )       {
1133                                 for (i=0; i<NUM_KEY_CONTROLS; i++ )             {
1134                                         items[i].value=default_kconfig_settings[0][i];
1135                                         kc_drawitem( &items[i], 0 );
1136                                 }
1137 #ifdef D2X_KEYS
1138                         } else if ( items==kc_d2x ) {
1139                                 for(i=0;i<NUM_D2X_CONTROLS;i++)
1140                                 {
1141                                         items[i].value=default_kconfig_d2x_settings[i];
1142                                         kc_drawitem( &items[i], 0 );
1143                                 }
1144 #endif
1145                         } else {
1146                                 #ifdef MACINTOSH
1147                           // hack for firebire and mousestick default controls since I made
1148                           // them the same control type -- dumb dumb dumb
1149                                 if (joy_have_firebird())
1150                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1151                                                 items[i].value = default_firebird_settings[i];
1152                                                 kc_drawitem( &items[i], 0 );
1153                                         }
1154                                 else if (joy_have_mousestick())
1155                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1156                                                 items[i].value = default_mousestick_settings[i];
1157                                                 kc_drawitem( &items[i], 0 );
1158                                         }
1159                                 else
1160                                 #endif  // note link to above else
1161                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1162                                                 items[i].value = default_kconfig_settings[Config_control_type][i];
1163                                                 kc_drawitem( &items[i], 0 );
1164                                         }
1165                         }
1166                         kc_drawitem( &items[citem], 1 );
1167                         break;
1168                 case KEY_DELETE:
1169                         items[citem].value=255;
1170                         kc_drawitem( &items[citem], 1 );
1171                         break;
1172                 case KEY_UP:            
1173                 case KEY_PAD8:
1174 #ifdef TABLE_CREATION
1175                         if (items[citem].u==-1) items[citem].u=find_next_item_up( items,nitems, citem);
1176 #endif
1177                         citem = items[citem].u; 
1178                         break;
1179                 
1180                 case KEY_DOWN:  
1181                 case KEY_PAD2:
1182 #ifdef TABLE_CREATION
1183                         if (items[citem].d==-1) items[citem].d=find_next_item_down( items,nitems, citem);
1184 #endif
1185                         citem = items[citem].d; 
1186                         break;
1187                 case KEY_LEFT:  
1188                 case KEY_PAD4:
1189 #ifdef TABLE_CREATION
1190                         if (items[citem].l==-1) items[citem].l=find_next_item_left( items,nitems, citem);
1191 #endif
1192                         citem = items[citem].l; 
1193                         break;
1194                 case KEY_RIGHT:         
1195                 case KEY_PAD6:
1196 #ifdef TABLE_CREATION
1197                         if (items[citem].r==-1) items[citem].r=find_next_item_right( items,nitems, citem);
1198 #endif
1199                         citem = items[citem].r; 
1200                         break;
1201                 case KEY_ENTER: 
1202                 case KEY_PADENTER:      
1203                         switch( items[citem].type )     {
1204                         case BT_KEY:            kc_change_key( &items[citem] ); break;
1205                         case BT_MOUSE_BUTTON:   kc_change_mousebutton( &items[citem] ); break;
1206                         case BT_MOUSE_AXIS:     kc_change_mouseaxis( &items[citem] ); break;
1207                         case BT_JOY_BUTTON:     kc_change_joybutton( &items[citem] ); break;
1208                         case BT_JOY_AXIS:       kc_change_joyaxis( &items[citem] ); break;
1209                         case BT_INVERT:         kc_change_invert( &items[citem] ); break;
1210                         }
1211                         break;
1212                 //the following case added by WraithX on 11/22/00 to work around the weird joystick bug...
1213                 case KEY_SPACEBAR:
1214                         switch(items[citem].type)
1215                         {
1216                         case BT_JOY_AXIS:
1217                                 kc_next_joyaxis(&items[citem]);
1218                                 break;
1219                         }
1220                         break;
1221                 //end addition by WraithX
1222                 case -2:        
1223                 case KEY_ESC:
1224                         grd_curcanv->cv_font    = save_font;
1225
1226                         gr_set_current_canvas( save_canvas );
1227                         keyd_repeat = old_keyd_repeat;
1228                         game_flush_inputs();
1229                         newmenu_hide_cursor();
1230                         if (time_stopped)
1231                                 start_time();
1232                         return;
1233 #ifdef TABLE_CREATION
1234                 case KEY_DEBUGGED+KEY_F12:      {
1235                         FILE * fp;
1236                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
1237                                 kc_keyboard[i].u = find_next_item_up( kc_keyboard,NUM_KEY_CONTROLS, i);
1238                                 kc_keyboard[i].d = find_next_item_down( kc_keyboard,NUM_KEY_CONTROLS, i);
1239                                 kc_keyboard[i].l = find_next_item_left( kc_keyboard,NUM_KEY_CONTROLS, i);
1240                                 kc_keyboard[i].r = find_next_item_right( kc_keyboard,NUM_KEY_CONTROLS, i);
1241                         }
1242                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1243                                 kc_joystick[i].u = find_next_item_up( kc_joystick,NUM_OTHER_CONTROLS, i);
1244                                 kc_joystick[i].d = find_next_item_down( kc_joystick,NUM_OTHER_CONTROLS, i);
1245                                 kc_joystick[i].l = find_next_item_left( kc_joystick,NUM_OTHER_CONTROLS, i);
1246                                 kc_joystick[i].r = find_next_item_right( kc_joystick,NUM_OTHER_CONTROLS, i);
1247                         }
1248                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1249                                 kc_mouse[i].u = find_next_item_up( kc_mouse,NUM_OTHER_CONTROLS, i);
1250                                 kc_mouse[i].d = find_next_item_down( kc_mouse,NUM_OTHER_CONTROLS, i);
1251                                 kc_mouse[i].l = find_next_item_left( kc_mouse,NUM_OTHER_CONTROLS, i);
1252                                 kc_mouse[i].r = find_next_item_right( kc_mouse,NUM_OTHER_CONTROLS, i);
1253                         }
1254                         fp = fopen( "kconfig.cod", "wt" );
1255
1256                         fprintf( fp, "ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {\n" );
1257                         for (i=0; i<CONTROL_MAX_TYPES; i++ )    {
1258                                 int j;
1259                                 fprintf( fp, "{0x%x", kconfig_settings[i][0] );
1260                                 for (j=1; j<MAX_CONTROLS; j++ )
1261                                         fprintf( fp, ",0x%x", kconfig_settings[i][j] );
1262                                 fprintf( fp, "},\n" );
1263                         }
1264                         fprintf( fp, "};\n" );
1265                 
1266                         fprintf( fp, "\nkc_item kc_keyboard[NUM_KEY_CONTROLS] = {\n" );
1267                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
1268                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1269                                         kc_keyboard[i].id, kc_keyboard[i].x, kc_keyboard[i].y, kc_keyboard[i].w1, kc_keyboard[i].w2,
1270                                         kc_keyboard[i].u, kc_keyboard[i].d, kc_keyboard[i].l, kc_keyboard[i].r,
1271                                         34, kc_keyboard[i].text, 34, btype_text[kc_keyboard[i].type] );
1272                         }
1273                         fprintf( fp, "};" );
1274
1275                         fprintf( fp, "\nkc_item kc_joystick[NUM_OTHER_CONTROLS] = {\n" );
1276                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1277                                 if (kc_joystick[i].type == BT_JOY_BUTTON)
1278                                         fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1279                                                 kc_joystick[i].id, kc_joystick[i].x, kc_joystick[i].y, kc_joystick[i].w1, kc_joystick[i].w2,
1280                                                 kc_joystick[i].u, kc_joystick[i].d, kc_joystick[i].l, kc_joystick[i].r,
1281                                                 34, kc_joystick[i].text, 34, btype_text[kc_joystick[i].type] );
1282                 else
1283                                         fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1284                                                 kc_joystick[i].id, kc_joystick[i].x, kc_joystick[i].y, kc_joystick[i].w1, kc_joystick[i].w2,
1285                                                 kc_joystick[i].u, kc_joystick[i].d, kc_joystick[i].l, kc_joystick[i].r,
1286                                                 34, kc_joystick[i].text, 34, btype_text[kc_joystick[i].type] );
1287                         }
1288                         fprintf( fp, "};" );
1289
1290                         fprintf( fp, "\nkc_item kc_mouse[NUM_OTHER_CONTROLS] = {\n" );
1291                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
1292                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
1293                                         kc_mouse[i].id, kc_mouse[i].x, kc_mouse[i].y, kc_mouse[i].w1, kc_mouse[i].w2,
1294                                         kc_mouse[i].u, kc_mouse[i].d, kc_mouse[i].l, kc_mouse[i].r,
1295                                         34, kc_mouse[i].text, 34, btype_text[kc_mouse[i].type] );
1296                         }
1297                         fprintf( fp, "};" );
1298
1299                         fclose(fp);
1300
1301                         }
1302                         break;
1303 #endif
1304                 }
1305
1306 #ifdef NEWMENU_MOUSE
1307                 if ( (mouse_state && !omouse_state) || (mouse_state && omouse_state) ) {
1308                         int item_height;
1309                         
1310                         mouse_get_pos(&mx, &my);
1311                         for (i=0; i<nitems; i++ )       {
1312                                 item_height = get_item_height( &items[i] );
1313                                 x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[i].x) + LHX(items[i].w1);
1314                                 x2 = x1 + LHX(items[i].w2);
1315                                 y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[i].y);
1316                                 y2 = y1 + LHX(item_height);
1317                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1318                                         citem = i;
1319                                         break;
1320                                 }
1321                         }
1322                 }
1323                 else if ( !mouse_state && omouse_state ) {
1324                         int item_height;
1325                         
1326                         mouse_get_pos(&mx, &my);
1327                         item_height = get_item_height( &items[citem] );
1328                         x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[citem].x) + LHX(items[citem].w1);
1329                         x2 = x1 + LHX(items[citem].w2);
1330                         y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[citem].y);
1331                         y2 = y1 + LHY(item_height);
1332                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
1333                                 newmenu_hide_cursor();
1334                                 switch( items[citem].type )     {
1335                                 case BT_KEY:                            kc_change_key( &items[citem] ); break;
1336                                 case BT_MOUSE_BUTTON:   kc_change_mousebutton( &items[citem] ); break;
1337                                 case BT_MOUSE_AXIS:             kc_change_mouseaxis( &items[citem] ); break;
1338                                 case BT_JOY_BUTTON:             kc_change_joybutton( &items[citem] ); break;
1339                                 case BT_JOY_AXIS:               kc_change_joyaxis( &items[citem] ); break;
1340                                 case BT_INVERT:                         kc_change_invert( &items[citem] ); break;
1341                                 }
1342                                 newmenu_show_cursor();
1343                         } else {
1344                                 x1 = grd_curcanv->cv_bitmap.bm_x + close_x + LHX(1);
1345                                 x2 = x1 + close_size - LHX(1);
1346                                 y1 = grd_curcanv->cv_bitmap.bm_y + close_y + LHX(1);
1347                                 y2 = y1 + close_size - LHX(1);
1348                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
1349                                         grd_curcanv->cv_font    = save_font;
1350                                         gr_set_current_canvas( save_canvas );
1351                                         keyd_repeat = old_keyd_repeat;
1352                                         game_flush_inputs();
1353                                         newmenu_hide_cursor();
1354                                         if (time_stopped)
1355                                                 start_time();
1356                                         return;
1357                                 }
1358                         }
1359
1360                 }
1361 #endif // NEWMENU_MOUSE
1362
1363                 if (ocitem!=citem)      {
1364                         newmenu_hide_cursor();
1365                         kc_drawitem( &items[ocitem], 0 );
1366                         kc_drawitem( &items[citem], 1 );
1367                         newmenu_show_cursor();
1368                 }
1369         }
1370 }
1371
1372
1373 void kc_drawitem( kc_item *item, int is_current )
1374 {
1375         int x, w, h, aw;
1376         char btext[16];
1377
1378         if (is_current)
1379                 gr_set_fontcolor( BM_XRGB(20,20,29), -1 );
1380         else
1381                 gr_set_fontcolor( BM_XRGB(15,15,24), -1 );
1382    gr_string( LHX(item->x), LHY(item->y), item->text );
1383
1384         if (item->value==255) {
1385                 strcpy( btext, "" );
1386         } else {
1387                 switch( item->type )    {
1388                         case BT_KEY:
1389                                 strncpy( btext, key_text[item->value], 10 ); break;
1390                         case BT_MOUSE_BUTTON:
1391                                 #ifndef MACINTOSH
1392                                 //strncpy( btext, Text_string[mousebutton_text[item->value]], 10 ); break;
1393                                 strncpy( btext, (item->value <= 3)?Text_string[mousebutton_text[item->value]]:mousebutton_textra[item->value-3], 10 ); break;
1394                                 #else
1395                                 strncpy( btext, mousebutton_text[item->value], 10 ); break;
1396                                 #endif
1397                         case BT_MOUSE_AXIS:
1398                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
1399                         case BT_JOY_BUTTON:
1400 #ifdef USE_LINUX_JOY
1401                                 sprintf(btext, "J%d B%d", j_button[item->value].joydev, j_Get_joydev_button_number(item->value));
1402 #elif defined(SDL_INPUT)
1403                                 if (joybutton_text[item->value])
1404                                         strncpy(btext, joybutton_text[item->value], 10);
1405                                 else
1406                                         sprintf(btext, "BTN%2d", item->value + 1);
1407 #else
1408 # ifndef MACINTOSH
1409 #  ifdef WINDOWS
1410                                 if (joybutton_text[item->value] != -1) 
1411                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
1412                                 else 
1413                                         sprintf( btext, "BTN%2d", item->value+1 );
1414 #  else
1415                                 if ( joybutton_text[item->value] !=-1 )
1416                                         strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
1417                                 else
1418                                         sprintf( btext, "BTN%d", item->value );
1419 #  endif
1420 # else
1421                                 strncpy( btext, joy_btn_name( item->value ), 10);
1422                                 if (btext == NULL)
1423                                         sprintf( btext, "BTN%d", item->value );
1424 # endif
1425 #endif
1426                                 break;
1427                         case BT_JOY_AXIS:
1428 #ifdef USE_LINUX_JOY
1429                                 sprintf(btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number(item->value));
1430 #elif defined(SDL_INPUT)
1431                                 if (joyaxis_text[item->value])
1432                                         strncpy(btext, joyaxis_text[item->value], 10);
1433                                 else
1434                                         sprintf(btext, "AXIS%2d", item->value + 1);
1435 #else
1436                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
1437 #endif
1438                                 break;
1439                         case BT_INVERT:
1440                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
1441                 }
1442         }
1443         if (item->w1) {
1444                 gr_get_string_size(btext, &w, &h, &aw  );
1445
1446                 if (is_current)
1447                         gr_setcolor( BM_XRGB(21,0,24) );
1448                 else
1449                         gr_setcolor( BM_XRGB(16,0,19) );
1450                 gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1451                 
1452                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1453
1454                 x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1455         
1456                 gr_string( x, LHY(item->y), btext );
1457         }
1458 }
1459
1460
1461 static int looper=0;
1462
1463 void kc_drawquestion( kc_item *item )
1464 {
1465         int c, x, w, h, aw;
1466
1467         gr_get_string_size("?", &w, &h, &aw  );
1468
1469         c = BM_XRGB(21,0,24);
1470
1471         //@@gr_setcolor( gr_fade_table[fades[looper]*256+c] );
1472         gr_setcolor(BM_XRGB(21*fades[looper]/31,0,24*fades[looper]/31));
1473         looper++;
1474         if (looper>63) looper=0;
1475
1476         gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1477         
1478         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1479
1480         x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1481    
1482         gr_string( x, LHY(item->y), "?" );
1483 gr_update();
1484 }
1485
1486 void kc_change_key( kc_item * item )
1487 {
1488         int i,n,f,k;
1489         ubyte keycode;
1490
1491         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1492         
1493         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_KEY );
1494
1495         game_flush_inputs();
1496         keycode=255;
1497         k=255;
1498         
1499         while( (k!=KEY_ESC) && (keycode==255) ) 
1500         {                               
1501                 #ifdef NETWORK
1502                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1503                         multi_menu_poll();
1504                 #endif
1505 //              if ( Game_mode & GM_MULTI )
1506 //                      GameLoop( 0, 0 );                               // Continue
1507                 k = key_inkey();
1508                 timer_delay(f0_1/10);
1509                 kc_drawquestion( item );
1510         
1511                 for (i=0; i<256; i++ )  {
1512                         if (keyd_pressed[i] && (strlen(key_text[i])>0)) {
1513                                 f = 0;
1514                                 for (n=0; n<sizeof(system_keys); n++ )
1515                                         if ( system_keys[n] == i )
1516                                                 f=1;
1517                                 if (!f) 
1518                                         keycode=i;
1519                         }
1520                 }
1521         }
1522
1523         if (k!=KEY_ESC) {
1524                 for (i=0; i<Num_items; i++ )    {
1525                         n = item - All_items;
1526                         if ( (i!=n) && (All_items[i].type==BT_KEY) && (All_items[i].value==keycode) )           {
1527                                 All_items[i].value = 255;
1528                                 kc_drawitem( &All_items[i], 0 );
1529                         }
1530                 }
1531                 item->value = keycode;
1532         }
1533         kc_drawitem( item, 1 );
1534
1535         gr_set_fontcolor( BM_XRGB(28,28,28), BM_XRGB(0,0,0) );
1536
1537         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1538
1539         game_flush_inputs();
1540
1541 }
1542
1543 void kc_change_joybutton( kc_item * item )
1544 {
1545         int n,i,k;
1546         ubyte code;
1547
1548         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1549         
1550         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_JBUTTON );
1551
1552         game_flush_inputs();
1553         code=255;
1554         k=255;
1555         
1556         while( (k!=KEY_ESC) && (code==255))     
1557         {                               
1558                 #ifdef NETWORK
1559                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1560                         multi_menu_poll();
1561                 #endif
1562 //              if ( Game_mode & GM_MULTI )
1563 //                      GameLoop( 0, 0 );                               // Continue
1564                 k = key_inkey();
1565                 timer_delay(f0_1/10);
1566
1567                 if (k == KEY_PRINT_SCREEN)
1568                         save_screen_shot(0);
1569
1570                 kc_drawquestion( item );
1571
1572                 WIN(code = joydefsw_do_button());
1573                 
1574 #ifdef MACINTOSH
1575                 code = joy_do_buttons();
1576 #endif
1577                 
1578 #if !defined(WINDOWS) && !defined(MACINTOSH)
1579                 if (Config_control_type==CONTROL_THRUSTMASTER_FCS)      {
1580                         int axis[4];
1581                         joystick_read_raw_axis( JOY_ALL_AXIS, axis );
1582                         kconfig_read_fcs( axis[3] );
1583                         if ( joy_get_button_state(7) ) code = 7;
1584                         if ( joy_get_button_state(11) ) code = 11;
1585                         if ( joy_get_button_state(15) ) code = 15;
1586                         if ( joy_get_button_state(19) ) code = 19;
1587                         for (i=0; i<4; i++ )    {
1588                                 if ( joy_get_button_state(i) )
1589                                         code = i;
1590                         }
1591                 } else if (Config_control_type==CONTROL_FLIGHTSTICK_PRO) {
1592                         for (i=4; i<20; i++ )   {
1593                                 if ( joy_get_button_state(i)  ) {
1594                                         code = i;
1595                                         mprintf(( 0, "JB: %d\n", code ));
1596                                 }
1597                         }
1598                 } else {
1599                         for (i = 0; i < JOY_MAX_BUTTONS; i++)
1600                         {
1601                                 if ( joy_get_button_state(i) )
1602                                         code = i;
1603                         }
1604                 }
1605 #endif
1606         }
1607         if (code!=255)  {
1608                 for (i=0; i<Num_items; i++ )    {
1609                         n = item - All_items;
1610                         if ( (i!=n) && (All_items[i].type==BT_JOY_BUTTON) && (All_items[i].value==code) ) {
1611                                 All_items[i].value = 255;
1612                                 kc_drawitem( &All_items[i], 0 );
1613                         }
1614                 }
1615                 item->value = code;
1616         }
1617         kc_drawitem( item, 1 );
1618         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1619         game_flush_inputs();
1620 }
1621
1622 void kc_change_mousebutton( kc_item * item )
1623 {
1624         int n,i,b,k;
1625         ubyte code;
1626
1627         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1628         
1629         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_MBUTTON );
1630
1631         game_flush_inputs();
1632         code=255;
1633         k=255;
1634         
1635         while( (k!=KEY_ESC) && (code==255))     
1636         {                               
1637                 #ifdef NETWORK
1638                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1639                         multi_menu_poll();
1640                 #endif
1641 //              if ( Game_mode & GM_MULTI )
1642 //                      GameLoop( 0, 0 );                               // Continue
1643                 k = key_inkey();
1644                 timer_delay(f0_1/10);
1645
1646                 if (k == KEY_PRINT_SCREEN)
1647                         save_screen_shot(0);
1648
1649                 kc_drawquestion( item );
1650
1651                 b = mouse_get_btns();
1652                 for (i = 0; i < 16; i++ ) {
1653                         if ( b & (1<<i) )       
1654                                 code = i;
1655                 }
1656         }
1657         if (code!=255)  {
1658                 for (i=0; i<Num_items; i++ )    {
1659                         n = item - All_items;
1660                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_BUTTON) && (All_items[i].value==code) )             {
1661                                 All_items[i].value = 255;
1662                                 kc_drawitem( &All_items[i], 0 );
1663                         }
1664                 }
1665                 item->value = code;
1666         }
1667         kc_drawitem( item, 1 );
1668         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1669         game_flush_inputs();
1670
1671 }
1672
1673
1674 // the following function added by WraithX on 11/22/00 to work around the weird joystick bug... - modified my Matt Mueller to skip already allocated axes
1675 void kc_next_joyaxis(kc_item *item)
1676 {
1677         int n, i, k, max, tries;
1678         ubyte code = 0;
1679
1680         k = 255;
1681         n = 0;
1682         i = 0;
1683
1684         // I modelled this ifdef after the code in the kc_change_joyaxis method.
1685         // So, if somethin's not workin here, it might not be workin there either.
1686         max = JOY_MAX_AXES;
1687         tries = 1;
1688         code = (item->value + 1) % max;
1689
1690         if (code != 255)
1691         {
1692                 for (i = 0; i < Num_items; i++)
1693                 {
1694                         n = item - All_items;
1695                         if ((i != n) && (All_items[i].type == BT_JOY_AXIS) && (All_items[i].value == code))
1696                         {
1697                                 if (tries > max)
1698                                         return; // all axes allocated already
1699                                 i = -1; // -1 so the i++ will push back to 0
1700                                 code = (item->value + ++tries) % max; // try next axis
1701                         }//end if
1702                 }//end for
1703
1704                 item->value = code;
1705         }//end if
1706
1707         kc_drawitem(item, 1);
1708         nm_restore_background(0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h);
1709         game_flush_inputs();
1710
1711 }//method kc_next_joyaxis
1712 //end addition by WraithX
1713
1714
1715 void kc_change_joyaxis( kc_item * item )
1716 {
1717         int axis[JOY_MAX_AXES];
1718         int old_axis[JOY_MAX_AXES];
1719         int numaxis = joy_num_axes;
1720         int n,i,k;
1721         ubyte code;
1722
1723         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1724         
1725         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_JOY_AXIS );
1726
1727         game_flush_inputs();
1728         code=255;
1729         k=255;
1730
1731 #ifdef WINDOWS
1732         joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, old_axis );
1733 #else
1734         joystick_read_raw_axis( JOY_ALL_AXIS, old_axis );
1735 #endif
1736
1737         while( (k!=KEY_ESC) && (code==255))     
1738         {                               
1739                 #ifdef NETWORK
1740                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1741                         multi_menu_poll();
1742                 #endif
1743 //              if ( Game_mode & GM_MULTI )
1744 //                      GameLoop( 0, 0 );                               // Continue
1745                 k = key_inkey();
1746                 timer_delay(f0_1/10);
1747
1748                 if (k == KEY_PRINT_SCREEN)
1749                         save_screen_shot(0);
1750
1751                 kc_drawquestion( item );
1752
1753 #ifdef WINDOWS
1754                 joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, axis );
1755 #else
1756                 joystick_read_raw_axis( JOY_ALL_AXIS, axis );
1757 #endif
1758
1759                 for (i=0; i<numaxis; i++ )      {
1760 #if defined (MACINTOSH)
1761                         if ( abs(axis[i]-old_axis[i])>100 )
1762 #elif defined(WINDOWS)
1763                         if ( abs(axis[i]-old_axis[i])>1024 )
1764 #else
1765                         if ( abs(axis[i]-old_axis[i])>200 )
1766 #endif
1767                         {
1768                                 code = i;
1769                                 con_printf(CON_DEBUG, "Axis Movement detected: Axis %i\n", i);
1770                         }
1771                         //old_axis[i] = axis[i];
1772                 }
1773                 for (i=0; i<Num_items; i++ )    
1774                  {
1775                         n = item - All_items;
1776                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) 
1777                                 code = 255;
1778                  }
1779         
1780         }
1781         if (code!=255)  {
1782                 for (i=0; i<Num_items; i++ )    {
1783                         n = item - All_items;
1784                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) {
1785                                 All_items[i].value = 255;
1786                                 kc_drawitem( &All_items[i], 0 );
1787                         }
1788                 }
1789
1790                 item->value = code;                                      
1791         }
1792         kc_drawitem( item, 1 );
1793         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1794         game_flush_inputs();
1795
1796 }
1797
1798 void kc_change_mouseaxis( kc_item * item )
1799 {
1800         int i,n,k;
1801         ubyte code;
1802         int dx,dy;
1803 #ifdef SDL_INPUT
1804         int dz;
1805 #endif
1806
1807         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1808         
1809         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_MSE_AXIS );
1810
1811         game_flush_inputs();
1812         code=255;
1813         k=255;
1814
1815         mouse_get_delta( &dx, &dy );
1816
1817         while( (k!=KEY_ESC) && (code==255))     
1818         {                               
1819                 #ifdef NETWORK
1820                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1821                         multi_menu_poll();
1822                 #endif
1823 //              if ( Game_mode & GM_MULTI )
1824 //                      GameLoop( 0, 0 );                               // Continue
1825                 k = key_inkey();
1826                 timer_delay(f0_1/10);
1827
1828                 if (k == KEY_PRINT_SCREEN)
1829                         save_screen_shot(0);
1830
1831                 kc_drawquestion( item );
1832
1833 #ifdef SDL_INPUT
1834                 mouse_get_delta_z( &dx, &dy, &dz );
1835 #else
1836                 mouse_get_delta( &dx, &dy );
1837 #endif
1838                 if ( abs(dx)>20 ) code = 0;
1839                 if ( abs(dy)>20 ) code = 1;
1840 #ifdef SDL_INPUT
1841                 if ( abs(dz)>20 ) code = 2;
1842 #endif
1843         }
1844         if (code!=255)  {
1845                 for (i=0; i<Num_items; i++ )    {
1846                         n = item - All_items;
1847                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_AXIS) && (All_items[i].value==code) )               {
1848                                 All_items[i].value = 255;
1849                                 kc_drawitem( &All_items[i], 0 );
1850                         }
1851                 }
1852                 item->value = code;
1853         }
1854         kc_drawitem( item, 1 );
1855         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1856         game_flush_inputs();
1857
1858 }
1859
1860
1861 void kc_change_invert( kc_item * item )
1862 {
1863         game_flush_inputs();
1864
1865         if (item->value)
1866                 item->value = 0;
1867         else 
1868                 item->value = 1;
1869
1870         kc_drawitem( item, 1 );
1871
1872 }
1873
1874 #include "screens.h"
1875
1876 void kconfig(int n, char * title)
1877 {
1878         int i, j;
1879         grs_bitmap *save_bm;
1880
1881         set_screen_mode( SCREEN_MENU );
1882
1883         kc_set_controls();
1884
1885         //save screen
1886         save_bm = gr_create_bitmap( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
1887         Assert( save_bm != NULL );
1888         
1889         gr_bm_bitblt(grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_w, 
1890                                         0, 0, 0, 0, &grd_curcanv->cv_bitmap, save_bm );
1891
1892         switch(n)       {
1893         case 0:kconfig_sub( kc_keyboard, NUM_KEY_CONTROLS, title );break;
1894         case 1:kconfig_sub( kc_joystick, NUM_OTHER_CONTROLS, title );break;
1895         case 2:kconfig_sub( kc_mouse, NUM_OTHER_CONTROLS, title ); break;
1896         case 3:kconfig_sub( kc_superjoy, NUM_OTHER_CONTROLS, title); break;
1897 #ifdef D2X_KEYS
1898         //added on 2/4/99 by Victor Rachels for new keys menu
1899         case 4:kconfig_sub( kc_d2x, NUM_D2X_CONTROLS, title ); break;
1900         //end this section addition - VR
1901 #endif
1902         default:
1903                 Int3();
1904                 return;
1905         }
1906
1907         //restore screen
1908         gr_bitmap(0, 0, save_bm);
1909         gr_free_bitmap(save_bm);
1910
1911 #if 0 // set_screen_mode always calls this later... right?
1912         reset_cockpit();                //force cockpit redraw next time
1913 #endif
1914
1915         // Update save values...
1916         
1917         for (i=0; i<NUM_KEY_CONTROLS; i++ )     
1918                 kconfig_settings[0][i] = kc_keyboard[i].value;
1919
1920         for (j=0; j<256; j++)
1921                 if (key_binding(j))
1922                         for (i=0; i<NUM_KEY_CONTROLS; i++ )
1923                                 if (!strncmp(key_binding(j), kc_bind_text[i], 13)) {
1924                                         cmd_appendf("unbind %s", key_text[j]);
1925                                         break;
1926                                 }
1927
1928         for (i=0; i<NUM_KEY_CONTROLS; i++ )
1929                 if (kc_keyboard[i].value != 255)
1930                         cmd_appendf("bind %s \"%s\"", key_text[kc_keyboard[i].value], kc_bind_text[i]);
1931
1932         while (cmd_queue_process())
1933                 ;
1934
1935         if ( (Config_control_type>0) && (Config_control_type<5)) { 
1936                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1937                         kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
1938         } else if (Config_control_type>4 && Config_control_type<CONTROL_WINJOYSTICK) {
1939                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1940                         kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
1941         }
1942         else if (Config_control_type == CONTROL_WINJOYSTICK) {
1943                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   
1944                         kconfig_settings[Config_control_type][i] = kc_superjoy[i].value;
1945         }
1946
1947 #ifdef D2X_KEYS
1948         for (i=0; i<NUM_D2X_CONTROLS; i++)
1949                 kconfig_d2x_settings[i] = kc_d2x[i].value;
1950 #endif
1951 }
1952
1953
1954 void kconfig_read_fcs( int raw_axis )
1955 {
1956         int raw_button, button, axis_min[4], axis_center[4], axis_max[4];
1957
1958         if (Config_control_type!=CONTROL_THRUSTMASTER_FCS) return;
1959
1960         joy_get_cal_vals(axis_min, axis_center, axis_max);
1961
1962         if ( axis_max[3] > 1 )
1963                 raw_button = (raw_axis*100)/axis_max[3];
1964         else
1965                 raw_button = 0;
1966
1967         if ( raw_button > 88 )
1968                 button = 0;
1969         else if ( raw_button > 63 )
1970                 button = 7;
1971         else if ( raw_button > 39 )
1972                 button = 11;
1973         else if ( raw_button > 15 )
1974                 button = 15;
1975         else    
1976                 button = 19;
1977
1978         kconfig_set_fcs_button( 19, button );
1979         kconfig_set_fcs_button( 15, button );
1980         kconfig_set_fcs_button( 11, button );
1981         kconfig_set_fcs_button( 7, button );
1982 }
1983                 
1984
1985 void kconfig_set_fcs_button( int btn, int button )
1986 {
1987         int state,time_down,upcount,downcount;
1988         state = time_down = upcount = downcount = 0;
1989
1990         if ( joy_get_button_state(btn) ) {
1991                 if ( btn==button )      {
1992                         state = 1;
1993                         time_down = FrameTime;
1994                 } else {
1995                         upcount=1;
1996                 }
1997         } else {
1998                 if ( btn==button )      {
1999                         state = 1;
2000                         time_down = FrameTime;
2001                         downcount=1;
2002                 } else {
2003                         upcount=1;
2004                 }
2005         }                               
2006                         
2007         joy_set_btn_values( btn, state, time_down, downcount, upcount );
2008                                         
2009 }
2010
2011
2012
2013 fix Last_angles_p = 0;
2014 fix Last_angles_b = 0;
2015 fix Last_angles_h = 0;
2016 ubyte Last_angles_read = 0;
2017
2018 extern int                      VR_sensitivity;
2019                                                 
2020 int VR_sense_range[3] = { 25, 50, 75 };
2021
2022 #if 0 //ndef MACINTOSH
2023 read_head_tracker()
2024 {
2025 #ifndef WINDOWS
2026
2027         fix yaw, pitch, roll;
2028         int buttons;
2029
2030 //------ read vfx1 helmet --------
2031         if (vfx1_installed) {
2032                 vfx_get_data(&yaw,&pitch,&roll,&buttons);
2033         } else if (iglasses_headset_installed)  {
2034                 iglasses_read_headset( &yaw, &pitch, &roll );
2035         } else if (Victor_headset_installed)   {
2036                 victor_read_headset_filtered( &yaw, &pitch, &roll );
2037         } else {
2038                 return;
2039         }
2040
2041         Use_player_head_angles = 0;
2042         if ( Last_angles_read ) {
2043                 fix yaw1 = yaw;
2044                 
2045                 yaw1 = yaw;
2046                 if ( (Last_angles_h < (F1_0/4) ) && (yaw > ((F1_0*3)/4) ) )     
2047                         yaw1 -= F1_0;
2048                 else if ( (yaw < (F1_0/4) ) && (Last_angles_h > ((F1_0*3)/4) ) )        
2049                         yaw1 += F1_0;
2050         
2051                 Controls.pitch_time     += fixmul((pitch- Last_angles_p)*VR_sense_range[VR_sensitivity],FrameTime);
2052                 Controls.heading_time+= fixmul((yaw1 -  Last_angles_h)*VR_sense_range[VR_sensitivity],FrameTime);
2053                 Controls.bank_time      += fixmul((roll - Last_angles_b)*VR_sense_range[VR_sensitivity],FrameTime);
2054         }
2055         Last_angles_read = 1;
2056         Last_angles_p = pitch;
2057         Last_angles_h = yaw;
2058         Last_angles_b = roll;
2059 #endif
2060 }
2061 #endif
2062
2063 #define PH_SCALE        8
2064
2065 #ifndef __MSDOS__ // WINDOWS
2066 #define JOYSTICK_READ_TIME      (F1_0/40)               //      Read joystick at 40 Hz.
2067 #else
2068 #define JOYSTICK_READ_TIME      (F1_0/10)               //      Read joystick at 10 Hz.
2069 #endif
2070
2071 fix     LastReadTime = 0;
2072
2073 fix     joy_axis[JOY_MAX_AXES];
2074
2075 ubyte                   kc_use_external_control = 0;
2076 ubyte                   kc_enable_external_control = 0;
2077 ubyte                   kc_external_intno = 0;
2078 ext_control_info        *kc_external_control = NULL;
2079 char                    *kc_external_name = NULL;
2080 ubyte                   kc_external_version = 0;
2081 extern int Automap_active;
2082
2083 void kconfig_init_external_controls(int intno, int address)
2084 {
2085         int i;
2086         kc_external_intno = intno;
2087         kc_external_control     = (ext_control_info *)address;
2088         kc_use_external_control = 1;
2089         kc_enable_external_control  = 1;
2090
2091         i = FindArg ( "-xname" );
2092         if ( i )        
2093                 kc_external_name = Args[i+1];
2094         else
2095                 kc_external_name = "External Controller";
2096
2097    for (i=0;i<strlen (kc_external_name);i++)
2098     if (kc_external_name[i]=='_')
2099           kc_external_name[i]=' '; 
2100
2101         i = FindArg ( "-xver" );
2102         if ( i )
2103                 kc_external_version = atoi(Args[i+1]);
2104         
2105         printf( "%s int: 0x%x, data: 0x%p, ver:%d\n", kc_external_name, kc_external_intno, kc_external_control, kc_external_version );
2106
2107 }
2108
2109 #if !defined(MACINTOSH)
2110 /*void kconfig_read_external_controls()
2111 {
2112         union REGS r;
2113
2114         if ( !kc_enable_external_control && !CybermouseActive) 
2115                 return;
2116
2117         if ( kc_external_version == 0 ) 
2118                 memset( kc_external_control, 0, sizeof(control_info) );
2119         else if ( kc_external_version > 0 )     {
2120                 memset( kc_external_control, 0, sizeof(control_info)+sizeof(vms_angvec) + 64 );
2121                 if ( kc_external_version > 1 ) {
2122                         // Write ship pos and angles to external controls...
2123                         ubyte *temp_ptr = (ubyte *)kc_external_control;
2124                         vms_vector *ship_pos;
2125                         vms_matrix *ship_orient;
2126                         memset( kc_external_control, 0, sizeof(control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector)+sizeof(vms_matrix) );
2127                         temp_ptr += sizeof(control_info)+sizeof(vms_angvec) + 64;
2128                         ship_pos = (vms_vector *)temp_ptr;
2129                         temp_ptr += sizeof(vms_vector);
2130                         ship_orient = (vms_matrix *)temp_ptr;
2131                         // Fill in ship postion...
2132                         *ship_pos = Objects[Players[Player_num].objnum].pos;
2133                         // Fill in ship orientation...
2134                         *ship_orient = Objects[Players[Player_num].objnum].orient;
2135                 }
2136         }
2137
2138         if ( Automap_active )                    // (If in automap...)
2139                 kc_external_control->automap_state = 1;
2140         memset(&r,0,sizeof(r));
2141
2142 #ifndef WINDOWS
2143   
2144    if (!CybermouseActive)
2145         int386 ( kc_external_intno, &r, &r);            // Read external info...
2146 //      else
2147   //            ReadOWL (kc_external_control);
2148
2149 #endif
2150
2151         if ( Player_num > -1 )  {
2152                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
2153                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
2154                 Auto_leveling_on = 0;
2155
2156                 if ( kc_external_version > 0 ) {                
2157                         vms_matrix tempm, ViewMatrix;
2158                         vms_angvec * Kconfig_abs_movement;
2159                         char * oem_message;
2160         
2161                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(control_info));
2162         
2163                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
2164                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
2165                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
2166                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
2167                         }
2168                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
2169                         if (oem_message[0] != '\0' )
2170                                 HUD_init_message( oem_message );
2171                 }
2172         }
2173
2174         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
2175         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
2176         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
2177         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
2178         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
2179         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
2180         Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
2181         Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
2182         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
2183         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
2184         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
2185         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
2186         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
2187         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
2188         Controls.automap_down_count += kc_external_control->automap_down_count;
2189         Controls.automap_state |= kc_external_control->automap_state;
2190 } */
2191 #endif
2192
2193 #ifdef WINDOWS
2194 void controls_read_all_win()
2195 {
2196         int i;
2197         int slide_on, bank_on;
2198         int dx, dy;
2199         fix ctime;
2200         fix mouse_axis[2];
2201         int raw_joy_axis[7];
2202         int mouse_buttons;
2203         fix k0, k1, k2, k3, kp;
2204         fix k4, k5, k6, k7, kh;
2205         ubyte channel_masks;
2206         int use_mouse, use_joystick;
2207         int speed_factor=1;
2208
2209         if (Game_turbo_mode)
2210                 speed_factor = 2;
2211         
2212         {
2213                 fix temp = Controls.heading_time;
2214                 fix temp1 = Controls.pitch_time;
2215                 memset( &Controls, 0, sizeof(control_info) );
2216                 Controls.heading_time = temp;
2217                 Controls.pitch_time = temp1;
2218         }
2219         slide_on = 0;
2220         bank_on = 0;
2221
2222         ctime = timer_get_fixed_seconds();
2223
2224 //      DO JOYSTICK (X,Y,Z  R,U,V)
2225 //      ----------------------------------------------------------------------------
2226         if ( (LastReadTime + JOYSTICK_READ_TIME) > ctime ) {
2227                 if ((ctime < 0) && (LastReadTime > 0))
2228                         LastReadTime = ctime;
2229                 use_joystick=1;
2230         } else if ((Config_control_type==CONTROL_WINJOYSTICK)) {
2231                 LastReadTime = ctime;
2232                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS+JOY_EXT_AXIS, raw_joy_axis );
2233
2234                 for (i=0; i<7; i++ )    {
2235                         if (channel_masks&(1<<i))       {
2236                                 int joy_null_value = 28;
2237
2238                         // DO POV if joystick has a hat.
2239                         //      --------------------------------------------------------------------
2240                                 if (i == 3) {
2241                                         joydefsw_do_winjoybutton(raw_joy_axis);
2242                                 }
2243                                 else {
2244                                 // DO AXIS
2245                                 //      --------------------------------------------------------------------    
2246                                 //      mprintf((0, "(%d)=%d,", i, raw_joy_axis[i]));
2247                                         raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2248                                 //      mprintf((0, "%d  ",raw_joy_axis[i]));
2249                                 //      if (i%2) mprintf((0,"\n"));
2250
2251         
2252                                         if (kc_superjoy[23].value==i)           // If this is the throttle
2253                                                 joy_null_value = 32;                            // Then use a larger dead-zone
2254         
2255                                         if (raw_joy_axis[i] > (joy_null_value-2)) 
2256                                                 raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2257                                         else if (raw_joy_axis[i] < -(joy_null_value+2))
2258                                                 raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2259                                         else
2260                                                 raw_joy_axis[i] = 0;
2261                                         joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2262                                 }
2263                         } else {
2264                                 joy_axis[i] = 0;
2265                         }
2266                 }       
2267                 use_joystick=1;
2268
2269         } else {
2270                 for (i=0; i<6; i++ )
2271                         joy_axis[i] = 0;
2272                 use_joystick=0;
2273         }
2274
2275 //      DO MOUSE 
2276 //      ----------------------------------------------------------------------------
2277         if (Config_control_type==5) {
2278                 mouse_get_delta( &dx, &dy );
2279                 mouse_axis[0] = (dx*FrameTime)/35;
2280                 mouse_axis[1] = (dy*FrameTime)/25;
2281                 mouse_buttons = mouse_get_btns();
2282                 //mprintf(( 0, "Mouse %d,%d b:%d, 0x%x\n", mouse_axis[0], mouse_axis[1], mouse_buttons, FrameTime ));
2283                 use_mouse=1;
2284    } else {
2285                 mouse_axis[0] = 0;
2286                 mouse_axis[1] = 0;
2287                 mouse_buttons = 0;
2288                 use_mouse=0;
2289         }
2290
2291 //------------- Read slide_on -------------
2292         
2293         // From keyboard...
2294         if ( kc_keyboard[8].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[8].value ];
2295         if ( kc_keyboard[9].value < 255 ) slide_on |= keyd_pressed[ kc_keyboard[9].value ];
2296         // From joystick...
2297         if ((use_joystick)&&(kc_superjoy[5].value<255)) slide_on |= joy_get_button_state( kc_superjoy[5].value );
2298         // From mouse...
2299         if ((use_mouse)&&(kc_mouse[5].value<255)) slide_on |= mouse_buttons & (1<<kc_mouse[5].value);
2300
2301 //------------- Read bank_on ---------------
2302
2303         // From keyboard...
2304         if ( kc_keyboard[18].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[18].value ];
2305         if ( kc_keyboard[19].value < 255 ) bank_on |= keyd_pressed[ kc_keyboard[19].value ];
2306         // From joystick...
2307         if ( (use_joystick)&&(kc_superjoy[10].value < 255 )) bank_on |= joy_get_button_state( kc_superjoy[10].value );
2308         // From mouse...
2309         if ( (use_mouse)&&(kc_mouse[10].value < 255 )) bank_on |= mouse_buttons & (1<<kc_mouse[10].value);
2310
2311 //------------ Read pitch_time -----------
2312         if ( !slide_on )        {
2313                 // mprintf((0, "pitch: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2314                 kp = 0;
2315                 k0 = speed_factor*key_down_time( kc_keyboard[0].value )/2;      // Divide by two since we want pitch to go slower
2316                 k1 = speed_factor*key_down_time( kc_keyboard[1].value )/2;
2317                 k2 = speed_factor*key_down_time( kc_keyboard[2].value )/2;
2318                 k3 = speed_factor*key_down_time( kc_keyboard[3].value )/2;
2319
2320                 // From keyboard...
2321                 if ( kc_keyboard[0].value < 255 ) kp += k0/PH_SCALE;
2322                 if ( kc_keyboard[1].value < 255 ) kp += k1/PH_SCALE;
2323                 if ( kc_keyboard[2].value < 255 ) kp -= k2/PH_SCALE;
2324                 if ( kc_keyboard[3].value < 255 ) kp -= k3/PH_SCALE;
2325
2326                 // From Cyberman...
2327                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2328                         kp += mouse_button_down_time(MB_PITCH_FORWARD)/(PH_SCALE*2);
2329                         kp -= mouse_button_down_time(MB_PITCH_BACKWARD)/(PH_SCALE*2);
2330                 }
2331         
2332                 if (kp == 0)
2333                         Controls.pitch_time = 0;
2334                 else if (kp > 0) {
2335                         if (Controls.pitch_time < 0)
2336                                 Controls.pitch_time = 0;
2337                 } else // kp < 0
2338                         if (Controls.pitch_time > 0)
2339                                 Controls.pitch_time = 0;
2340                 Controls.pitch_time += kp;
2341         
2342                 // From joystick...
2343                 if ( (use_joystick)&&(kc_superjoy[13].value < 255 ))    {
2344                         if ( !kc_superjoy[14].value )           // If not inverted...
2345                                 Controls.pitch_time -= (joy_axis[kc_superjoy[13].value]*Config_joystick_sensitivity)/8;
2346                         else
2347                                 Controls.pitch_time += (joy_axis[kc_superjoy[13].value]*Config_joystick_sensitivity)/8;
2348                 }
2349         
2350                 // From mouse...
2351                 //mprintf(( 0, "UM: %d, PV: %d\n", use_mouse, kc_mouse[13].value ));
2352                 if ( (use_mouse)&&(kc_mouse[13].value < 255) )  {
2353                         if ( !kc_mouse[14].value )              // If not inverted...
2354                                 Controls.pitch_time -= (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2355                         else
2356                                 Controls.pitch_time += (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
2357                 }
2358         } else {
2359                 Controls.pitch_time = 0;
2360         }
2361
2362
2363 //----------- Read vertical_thrust_time -----------------
2364
2365         if ( slide_on ) {
2366                 k0 = speed_factor*key_down_time( kc_keyboard[0].value );
2367                 k1 = speed_factor*key_down_time( kc_keyboard[1].value );
2368                 k2 = speed_factor*key_down_time( kc_keyboard[2].value );
2369                 k3 = speed_factor*key_down_time( kc_keyboard[3].value );
2370
2371                 // From keyboard...
2372                 if ( kc_keyboard[0].value < 255 ) Controls.vertical_thrust_time += k0;
2373                 if ( kc_keyboard[1].value < 255 ) Controls.vertical_thrust_time += k1;
2374                 if ( kc_keyboard[2].value < 255 ) Controls.vertical_thrust_time -= k2;
2375                 if ( kc_keyboard[3].value < 255 ) Controls.vertical_thrust_time -= k3;
2376
2377                 // From Cyberman...
2378                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2379                         Controls.vertical_thrust_time -= mouse_button_down_time(MB_PITCH_FORWARD);
2380                         Controls.vertical_thrust_time += mouse_button_down_time(MB_PITCH_BACKWARD);
2381                 }
2382         
2383                 // From joystick...
2384                 if ((use_joystick)&&( kc_superjoy[13].value < 255 ))    {
2385                         if ( !kc_superjoy[14].value )           // If not inverted...
2386                                 Controls.vertical_thrust_time += joy_axis[kc_superjoy[13].value];
2387                         else
2388                                 Controls.vertical_thrust_time -= joy_axis[kc_superjoy[13].value];
2389                 }
2390         
2391                 // From mouse...
2392                 if ( (use_mouse)&&(kc_mouse[13].value < 255 ))  {
2393                         if ( !kc_mouse[14].value )              // If not inverted...
2394                                 Controls.vertical_thrust_time -= mouse_axis[kc_mouse[13].value];
2395                         else
2396                                 Controls.vertical_thrust_time += mouse_axis[kc_mouse[13].value];
2397                 }
2398         }
2399
2400         // From keyboard...
2401         if ( kc_keyboard[14].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[14].value );
2402         if ( kc_keyboard[15].value < 255 ) Controls.vertical_thrust_time += speed_factor*key_down_time( kc_keyboard[15].value );
2403         if ( kc_keyboard[16].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[16].value );
2404         if ( kc_keyboard[17].value < 255 ) Controls.vertical_thrust_time -= speed_factor*key_down_time( kc_keyboard[17].value );
2405         
2406         // From joystick...
2407         if ((use_joystick)&&( kc_superjoy[19].value < 255 ))    {
2408                 if ( !kc_superjoy[20].value )           // If not inverted...
2409                         Controls.vertical_thrust_time += joy_axis[kc_superjoy[19].value];
2410                 else
2411                         Controls.vertical_thrust_time -= joy_axis[kc_superjoy[19].value];
2412         }
2413
2414         // From joystick buttons
2415         if ( (use_joystick)&&(kc_superjoy[8].value < 255 )) Controls.vertical_thrust_time += joy_get_button_down_time( kc_superjoy[8].value );
2416         if ( (use_joystick)&&(kc_superjoy[9].value < 255 )) Controls.vertical_thrust_time -= joy_get_button_down_time( kc_superjoy[9].value );
2417
2418         // From mouse buttons
2419         if ( (use_mouse)&&(kc_mouse[8].value < 255 )) Controls.vertical_thrust_time += mouse_button_down_time( kc_mouse[8].value );
2420         if ( (use_mouse)&&(kc_mouse[9].value < 255 )) Controls.vertical_thrust_time -= mouse_button_down_time( kc_mouse[9].value );
2421
2422         // From mouse...
2423         if ( (use_mouse)&&(kc_mouse[19].value < 255 ))  {
2424                 if ( !kc_mouse[20].value )              // If not inverted...
2425                         Controls.vertical_thrust_time += mouse_axis[kc_mouse[19].value];
2426                 else
2427                         Controls.vertical_thrust_time -= mouse_axis[kc_mouse[19].value];
2428         }
2429
2430         // From Cyberman...
2431         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2432                 Controls.vertical_thrust_time += mouse_button_down_time(MB_Z_UP)/2;
2433                 Controls.vertical_thrust_time -= mouse_button_down_time(MB_Z_DOWN)/2;
2434         }
2435
2436 //---------- Read heading_time -----------
2437
2438         if (!slide_on && !bank_on)      {
2439                 //mprintf((0, "heading: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
2440                 kh = 0;
2441                 k4 = speed_factor*key_down_time( kc_keyboard[4].value );
2442                 k5 = speed_factor*key_down_time( kc_keyboard[5].value );
2443                 k6 = speed_factor*key_down_time( kc_keyboard[6].value );
2444                 k7 = speed_factor*key_down_time( kc_keyboard[7].value );
2445
2446                 // From keyboard...
2447                 if ( kc_keyboard[4].value < 255 ) kh -= k4/PH_SCALE;
2448                 if ( kc_keyboard[5].value < 255 ) kh -= k5/PH_SCALE;
2449                 if ( kc_keyboard[6].value < 255 ) kh += k6/PH_SCALE;
2450                 if ( kc_keyboard[7].value < 255 ) kh += k7/PH_SCALE;
2451
2452                 // From Cyberman...
2453                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2454                         kh -= mouse_button_down_time(MB_HEAD_LEFT)/PH_SCALE;
2455                         kh += mouse_button_down_time(MB_HEAD_RIGHT)/PH_SCALE;
2456                 }
2457         
2458                 if (kh == 0)
2459                         Controls.heading_time = 0;
2460                 else if (kh > 0) {
2461                         if (Controls.heading_time < 0)
2462                                 Controls.heading_time = 0;
2463                 } else // kh < 0
2464                         if (Controls.heading_time > 0)
2465                                 Controls.heading_time = 0;
2466                 Controls.heading_time += kh;
2467
2468                 // From joystick...
2469                 if ( (use_joystick)&&(kc_superjoy[15].value < 255 ))    {
2470                         if ( !kc_superjoy[16].value )           // If not inverted...
2471                                 Controls.heading_time += (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2472                         else
2473                                 Controls.heading_time -= (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2474                 }
2475         
2476                 // From mouse...
2477                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2478                         if ( !kc_mouse[16].value )              // If not inverted...
2479                                 Controls.heading_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2480                         else
2481                                 Controls.heading_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2482                 }
2483         } else {
2484                 Controls.heading_time = 0;
2485         }
2486
2487 //----------- Read sideways_thrust_time -----------------
2488
2489         if ( slide_on ) {
2490                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
2491                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
2492                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
2493                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
2494
2495                 // From keyboard...
2496                 if ( kc_keyboard[4].value < 255 ) Controls.sideways_thrust_time -= k0;
2497                 if ( kc_keyboard[5].value < 255 ) Controls.sideways_thrust_time -= k1;
2498                 if ( kc_keyboard[6].value < 255 ) Controls.sideways_thrust_time += k2;
2499                 if ( kc_keyboard[7].value < 255 ) Controls.sideways_thrust_time += k3;
2500         
2501                 // From joystick...
2502                 if ( (use_joystick)&&(kc_superjoy[15].value < 255 ))    {
2503                         if ( !kc_superjoy[16].value )           // If not inverted...
2504                                 Controls.sideways_thrust_time += joy_axis[kc_superjoy[15].value];
2505                         else
2506                                 Controls.sideways_thrust_time -= joy_axis[kc_superjoy[15].value];
2507                 }
2508                 
2509                 // From cyberman
2510                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2511                         Controls.sideways_thrust_time -= mouse_button_down_time(MB_HEAD_LEFT);
2512                         Controls.sideways_thrust_time += mouse_button_down_time(MB_HEAD_RIGHT);
2513                 }
2514         
2515                 // From mouse...
2516                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2517                         if ( !kc_mouse[16].value )              // If not inverted...
2518                                 Controls.sideways_thrust_time += mouse_axis[kc_mouse[15].value];
2519                         else
2520                                 Controls.sideways_thrust_time -= mouse_axis[kc_mouse[15].value];
2521                 }
2522         }
2523
2524         // From keyboard...
2525         if ( kc_keyboard[10].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[10].value );
2526         if ( kc_keyboard[11].value < 255 ) Controls.sideways_thrust_time -= speed_factor*key_down_time( kc_keyboard[11].value );
2527         if ( kc_keyboard[12].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[12].value );
2528         if ( kc_keyboard[13].value < 255 ) Controls.sideways_thrust_time += speed_factor*key_down_time( kc_keyboard[13].value );
2529         
2530         // From joystick...
2531         if ( (use_joystick)&&(kc_superjoy[17].value < 255 ))    {
2532                 if ( !kc_superjoy[18].value )           // If not inverted...
2533                         Controls.sideways_thrust_time -= joy_axis[kc_superjoy[17].value];
2534                 else
2535                         Controls.sideways_thrust_time += joy_axis[kc_superjoy[17].value];
2536         }
2537
2538         // From joystick buttons
2539         if ( (use_joystick)&&(kc_superjoy[6].value < 255 )) Controls.sideways_thrust_time -= joy_get_button_down_time( kc_superjoy[6].value );
2540         if ( (use_joystick)&&(kc_superjoy[7].value < 255 )) Controls.sideways_thrust_time += joy_get_button_down_time( kc_superjoy[7].value );
2541
2542         // From mouse buttons
2543         if ( (use_mouse)&&(kc_mouse[6].value < 255 )) Controls.sideways_thrust_time -= mouse_button_down_time( kc_mouse[6].value );
2544         if ( (use_mouse)&&(kc_mouse[7].value < 255 )) Controls.sideways_thrust_time += mouse_button_down_time( kc_mouse[7].value );
2545
2546         // From mouse...
2547         if ( (use_mouse)&&(kc_mouse[17].value < 255 ))  {
2548                 if ( !kc_mouse[18].value )              // If not inverted...
2549                         Controls.sideways_thrust_time += mouse_axis[kc_mouse[17].value];
2550                 else
2551                         Controls.sideways_thrust_time -= mouse_axis[kc_mouse[17].value];
2552         }
2553
2554 //----------- Read bank_time -----------------
2555
2556         if ( bank_on )  {
2557                 k0 = speed_factor*key_down_time( kc_keyboard[4].value );
2558                 k1 = speed_factor*key_down_time( kc_keyboard[5].value );
2559                 k2 = speed_factor*key_down_time( kc_keyboard[6].value );
2560                 k3 = speed_factor*key_down_time( kc_keyboard[7].value );
2561
2562                 // From keyboard...
2563                 if ( kc_keyboard[4].value < 255 ) Controls.bank_time += k0;
2564                 if ( kc_keyboard[5].value < 255 ) Controls.bank_time += k1;
2565                 if ( kc_keyboard[6].value < 255 ) Controls.bank_time -= k2;
2566                 if ( kc_keyboard[7].value < 255 ) Controls.bank_time -= k3;
2567
2568                 // From Cyberman...
2569                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2570                         Controls.bank_time -= mouse_button_down_time(MB_HEAD_LEFT);
2571                         Controls.bank_time += mouse_button_down_time(MB_HEAD_RIGHT);
2572                 }
2573
2574                 // From joystick...
2575                 if ( (use_joystick)&&(kc_superjoy[15].value < 255) )    {
2576                         if ( !kc_superjoy[16].value )           // If not inverted...
2577                                 Controls.bank_time -= (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2578                         else
2579                                 Controls.bank_time += (joy_axis[kc_superjoy[15].value]*Config_joystick_sensitivity)/8;
2580                 }
2581         
2582                 // From mouse...
2583                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
2584                         if ( !kc_mouse[16].value )              // If not inverted...
2585                                 Controls.bank_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2586                         else
2587                                 Controls.bank_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
2588                 }
2589         }
2590
2591         // From keyboard...
2592         if ( kc_keyboard[20].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[20].value );
2593         if ( kc_keyboard[21].value < 255 ) Controls.bank_time += speed_factor*key_down_time( kc_keyboard[21].value );
2594         if ( kc_keyboard[22].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[22].value );
2595         if ( kc_keyboard[23].value < 255 ) Controls.bank_time -= speed_factor*key_down_time( kc_keyboard[23].value );
2596
2597         // From joystick...
2598         if ( (use_joystick)&&(kc_superjoy[21].value < 255) )    {
2599                 if ( !kc_superjoy[22].value )           // If not inverted...
2600                         Controls.bank_time -= joy_axis[kc_superjoy[21].value];
2601                 else
2602                         Controls.bank_time += joy_axis[kc_superjoy[21].value];
2603         }
2604
2605         // From joystick buttons
2606         if ( (use_joystick)&&(kc_superjoy[11].value < 255 )) Controls.bank_time += joy_get_button_down_time( kc_superjoy[11].value );
2607         if ( (use_joystick)&&(kc_superjoy[12].value < 255 )) Controls.bank_time -= joy_get_button_down_time( kc_superjoy[12].value );
2608
2609         // From mouse buttons
2610         if ( (use_mouse)&&(kc_mouse[11].value < 255 )) Controls.bank_time += mouse_button_down_time( kc_mouse[11].value );
2611         if ( (use_mouse)&&(kc_mouse[12].value < 255 )) Controls.bank_time -= mouse_button_down_time( kc_mouse[12].value );
2612
2613         // From mouse...
2614         if ( (use_mouse)&&(kc_mouse[21].value < 255 ))  {
2615                 if ( !kc_mouse[22].value )              // If not inverted...
2616                         Controls.bank_time += mouse_axis[kc_mouse[21].value];
2617                 else
2618                         Controls.bank_time -= mouse_axis[kc_mouse[21].value];
2619         }
2620
2621         // From Cyberman
2622         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
2623                 Controls.bank_time += mouse_button_down_time(MB_BANK_LEFT);
2624                 Controls.bank_time -= mouse_button_down_time(MB_BANK_RIGHT);
2625         }
2626
2627 //----------- Read forward_thrust_time -------------
2628
2629         // From keyboard...
2630         if ( kc_keyboard[30].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[30].value );
2631         if ( kc_keyboard[31].value < 255 ) Controls.forward_thrust_time += speed_factor*key_down_time( kc_keyboard[31].value );
2632         if ( kc_keyboard[32].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[32].value );
2633         if ( kc_keyboard[33].value < 255 ) Controls.forward_thrust_time -= speed_factor*key_down_time( kc_keyboard[33].value );
2634
2635         // From joystick...
2636         if ( (use_joystick)&&(kc_superjoy[23].value < 255 ))    {
2637                 if ( !kc_superjoy[24].value )           // If not inverted...
2638                         Controls.forward_thrust_time -= joy_axis[kc_superjoy[23].value];
2639                 else
2640                         Controls.forward_thrust_time += joy_axis[kc_superjoy[23].value];
2641         }
2642
2643         // From joystick buttons
2644         if ( (use_joystick)&&(kc_superjoy[2].value < 255 )) Controls.forward_thrust_time += joy_get_button_down_time( kc_superjoy[2].value );
2645         if ( (use_joystick)&&(kc_superjoy[3].value < 255 )) Controls.forward_thrust_time -= joy_get_button_down_time( kc_superjoy[3].value );
2646
2647         // From mouse...
2648         if ( (use_mouse)&&(kc_mouse[23].value < 255 ))  {
2649                 if ( !kc_mouse[24].value )              // If not inverted...
2650                         Controls.forward_thrust_time -= mouse_axis[kc_mouse[23].value];
2651                 else
2652                         Controls.forward_thrust_time += mouse_axis[kc_mouse[23].value];
2653         }
2654
2655         // From mouse buttons
2656         if ( (use_mouse)&&(kc_mouse[2].value < 255 )) Controls.forward_thrust_time += mouse_button_down_time( kc_mouse[2].value );
2657         if ( (use_mouse)&&(kc_mouse[3].value < 255 )) Controls.forward_thrust_time -= mouse_button_down_time( kc_mouse[3].value );
2658
2659 //----------- Read afterburner_state -------------
2660
2661         // From keyboard...
2662         if ( kc_keyboard[46].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[46].value];
2663         if ( kc_keyboard[47].value < 255 ) Controls.afterburner_state |= keyd_pressed[kc_keyboard[47].value];
2664
2665         if ( (use_mouse)&&(kc_mouse[27].value < 255 )) Controls.afterburner_state |= mouse_button_state(kc_mouse[27].value);
2666
2667         if ( (use_joystick)&&(kc_superjoy[27].value < 255 )) Controls.afterburner_state |= joy_get_button_state(kc_superjoy[27].value);
2668
2669 //-------Read headlight key--------------------------
2670         if (kc_keyboard[52].value < 255 )
2671                    Controls.headlight_count=key_down_count(kc_keyboard[52].value);
2672         if (kc_keyboard[53].value < 255 )
2673                    Controls.headlight_count=key_down_count(kc_keyboard[53].value);
2674                   if ((use_joystick)&&(kc_superjoy[30].value < 255 )) 
2675          Controls.headlight_count=joy_get_button_down_cnt(kc_superjoy[30].value);
2676                  
2677 //--------Read Cycle Primary Key------------------
2678
2679                   if (kc_keyboard[48].value<255)
2680                          Controls.cycle_primary_count=key_down_count(kc_keyboard[48].value);
2681                   if (kc_keyboard[49].value<255)
2682                         Controls.cycle_primary_count+=key_down_count(kc_keyboard[49].value);
2683              if ((use_joystick)&&(kc_superjoy[28].value < 255 )) 
2684          Controls.cycle_primary_count+=joy_get_button_down_cnt(kc_superjoy[28].value);
2685
2686 //--------Read Cycle Secondary Key------------------
2687
2688                   if (kc_keyboard[50].value<255)
2689                          Controls.cycle_secondary_count=key_down_count(kc_keyboard[50].value);
2690                   if (kc_keyboard[51].value<255)
2691                         Controls.cycle_secondary_count+=key_down_count(kc_keyboard[51].value);
2692              if ((use_joystick)&&(kc_superjoy[29].value < 255 )) 
2693                         Controls.cycle_secondary_count=joy_get_button_down_cnt(kc_superjoy[29].value);
2694
2695 //--------Read Toggle Bomb key----------------------
2696
2697                   if (kc_keyboard[56].value<255 && key_down_count(kc_keyboard[56].value))
2698          {
2699           int bomb = Secondary_last_was_super[PROXIMITY_INDEX]?PROXIMITY_INDEX:SMART_MINE_INDEX;
2700
2701                          if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX] &&
2702                                   !Players[Player_num].secondary_ammo[SMART_MINE_INDEX])
2703                            {
2704                                  digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
2705                                  HUD_init_message ("No bombs available!");
2706                                 }
2707                          else
2708                                 {       
2709                                  if (Players[Player_num].secondary_ammo[bomb]==0)
2710                                         {
2711                                          digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
2712                                          HUD_init_message ("No %s available!",(bomb==SMART_MINE_INDEX)?"Smart mines":"Proximity bombs");
2713                                         }
2714                                   else
2715                                         {
2716                                Secondary_last_was_super[PROXIMITY_INDEX]=!Secondary_last_was_super[PROXIMITY_INDEX];
2717                                          digi_play_sample_once( SOUND_GOOD_SELECTION_SECONDARY, F1_0 );
2718                                         }
2719                                 }
2720                         }
2721           
2722 //---------Read Energy->Shield key----------
2723
2724         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[54].value])
2725                 transfer_energy_to_shield(key_down_time(kc_keyboard[54].value));
2726         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && keyd_pressed[kc_keyboard[55].value])
2727                 transfer_energy_to_shield(key_down_time(kc_keyboard[55].value));
2728
2729 //----------- Read fire_primary_down_count
2730         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[24].value);
2731         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_down_count += key_down_count(kc_keyboard[25].value);
2732         if ((use_joystick)&&(kc_superjoy[0].value < 255 )) Controls.fire_primary_down_count += joy_get_button_down_cnt(kc_superjoy[0].value);
2733         if ((use_mouse)&&(kc_mouse[0].value < 255 )) Controls.fire_primary_down_count += mouse_button_down_count(kc_mouse[0].value);
2734
2735 //----------- Read fire_primary_state
2736         if (kc_keyboard[24].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[24].value];
2737         if (kc_keyboard[25].value < 255 ) Controls.fire_primary_state |= keyd_pressed[kc_keyboard[25].value];
2738         if ((use_joystick)&&(kc_superjoy[0].value < 255 )) Controls.fire_primary_state |= joy_get_button_state(kc_superjoy[0].value);
2739         if ((use_mouse)&&(kc_mouse[0].value < 255) ) Controls.fire_primary_state |= mouse_button_state(kc_mouse[0].value);
2740
2741 //----------- Read fire_secondary_down_count
2742         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[26].value);
2743         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_down_count += key_down_count(kc_keyboard[27].value);
2744         if ((use_joystick)&&(kc_superjoy[1].value < 255 )) Controls.fire_secondary_down_count += joy_get_button_down_cnt(kc_superjoy[1].value);
2745         if ((use_mouse)&&(kc_mouse[1].value < 255 )) Controls.fire_secondary_down_count += mouse_button_down_count(kc_mouse[1].value);
2746
2747 //----------- Read fire_secondary_state
2748         if (kc_keyboard[26].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[26].value];
2749         if (kc_keyboard[27].value < 255 ) Controls.fire_secondary_state |= keyd_pressed[kc_keyboard[27].value];
2750         if ((use_joystick)&&(kc_superjoy[1].value < 255 )) Controls.fire_secondary_state |= joy_get_button_state(kc_superjoy[1].value);
2751         if ((use_mouse)&&(kc_mouse[1].value < 255) ) Controls.fire_secondary_state |= mouse_button_state(kc_mouse[1].value);
2752
2753 //----------- Read fire_flare_down_count
2754         if (kc_keyboard[28].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[28].value);
2755         if (kc_keyboard[29].value < 255 ) Controls.fire_flare_down_count += key_down_count(kc_keyboard[29].value);
2756         if ((use_joystick)&&(kc_superjoy[4].value < 255 )) Controls.fire_flare_down_count += joy_get_button_down_cnt(kc_superjoy[4].value);
2757         if ((use_mouse)&&(kc_mouse[4].value < 255 )) Controls.fire_flare_down_count += mouse_button_down_count(kc_mouse[4].value);
2758
2759 //----------- Read drop_bomb_down_count
2760         if (kc_keyboard[34].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[34].value);
2761         if (kc_keyboard[35].value < 255 ) Controls.drop_bomb_down_count += key_down_count(kc_keyboard[35].value);
2762         if ((use_joystick)&&(kc_superjoy[26].value < 255 )) Controls.drop_bomb_down_count += joy_get_button_down_cnt(kc_superjoy[26].value);
2763         if ((use_mouse)&&(kc_mouse[26].value < 255 )) Controls.drop_bomb_down_count += mouse_button_down_count(kc_mouse[26].value);
2764
2765 //----------- Read rear_view_down_count
2766         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[36].value);
2767         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_count += key_down_count(kc_keyboard[37].value);
2768         if ((use_joystick)&&(kc_superjoy[25].value < 255 )) Controls.rear_view_down_count += joy_get_button_down_cnt(kc_superjoy[25].value);
2769         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_count += mouse_button_down_count(kc_mouse[25].value);
2770
2771 //----------- Read rear_view_down_state
2772         if (kc_keyboard[36].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[36].value];
2773         if (kc_keyboard[37].value < 255 ) Controls.rear_view_down_state |= keyd_pressed[kc_keyboard[37].value];
2774         if ((use_joystick)&&(kc_superjoy[25].value < 255 )) Controls.rear_view_down_state |= joy_get_button_state(kc_superjoy[25].value);
2775         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_state |= mouse_button_state(kc_mouse[25].value);
2776
2777 //----------- Read automap_down_count
2778         if (kc_keyboard[44].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[44].value);
2779         if (kc_keyboard[45].value < 255 ) Controls.automap_down_count += key_down_count(kc_keyboard[45].value);
2780
2781 //----------- Read automap_state
2782         if (kc_keyboard[44].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[44].value];
2783         if (kc_keyboard[45].value < 255 ) Controls.automap_state |= keyd_pressed[kc_keyboard[45].value];
2784
2785 //----------- Read stupid-cruise-control-type of throttle.
2786         {
2787                 if ( kc_keyboard[38].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[38].value)*5,FrameTime);
2788                 if ( kc_keyboard[39].value < 255 ) Cruise_speed += fixdiv(speed_factor*key_down_time(kc_keyboard[39].value)*5,FrameTime);
2789                 if ( kc_keyboard[40].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[40].value)*5,FrameTime);
2790                 if ( kc_keyboard[41].value < 255 ) Cruise_speed -= fixdiv(speed_factor*key_down_time(kc_keyboard[41].value)*5,FrameTime);
2791                 if ( (kc_keyboard[42].value < 255) && (key_down_count(kc_keyboard[42].value)) )
2792                         Cruise_speed = 0;
2793                 if ( (kc_keyboard[43].value < 255) && (key_down_count(kc_keyboard[43].value)) )
2794                         Cruise_speed = 0;
2795         
2796                 if (Cruise_speed > i2f(100) ) Cruise_speed = i2f(100);
2797                 if (Cruise_speed < 0 ) Cruise_speed = 0;
2798         
2799                 if (Controls.forward_thrust_time==0)
2800                         Controls.forward_thrust_time = fixmul(Cruise_speed,FrameTime)/100;
2801         }
2802
2803         //read_head_tracker();
2804
2805         // Read external controls
2806         if (kc_use_external_control || CybermouseActive)
2807                 kconfig_read_external_controls();
2808
2809 //----------- Clamp values between -FrameTime and FrameTime
2810         if (FrameTime > F1_0 )
2811                 mprintf( (1, "Bogus frame time of %.2f seconds\n", f2fl(FrameTime) ));
2812
2813         if (Controls.pitch_time > FrameTime/2 ) Controls.pitch_time = FrameTime/2;
2814         if (Controls.vertical_thrust_time > FrameTime ) Controls.vertical_thrust_time = FrameTime;
2815         if (Controls.heading_time > FrameTime ) Controls.heading_time = FrameTime;
2816         if (Controls.sideways_thrust_time > FrameTime ) Controls.sideways_thrust_time = FrameTime;
2817         if (Controls.bank_time > FrameTime ) Controls.bank_time = FrameTime;
2818         if (Controls.forward_thrust_time > FrameTime ) Controls.forward_thrust_time = FrameTime;
2819 //      if (Controls.afterburner_time > FrameTime ) Controls.afterburner_time = FrameTime;
2820
2821         if (Controls.pitch_time < -FrameTime/2 ) Controls.pitch_time = -FrameTime/2;
2822         if (Controls.vertical_thrust_time < -FrameTime ) Controls.vertical_thrust_time = -FrameTime;
2823         if (Controls.heading_time < -FrameTime ) Controls.heading_time = -FrameTime;
2824         if (Controls.sideways_thrust_time < -FrameTime ) Controls.sideways_thrust_time = -FrameTime;
2825         if (Controls.bank_time < -FrameTime ) Controls.bank_time = -FrameTime;
2826         if (Controls.forward_thrust_time < -FrameTime ) Controls.forward_thrust_time = -FrameTime;
2827 //      if (Controls.afterburner_time < -FrameTime ) Controls.afterburner_time = -FrameTime;
2828
2829
2830 //--------- Don't do anything if in debug mode
2831         #ifndef RELEASE
2832         if ( keyd_pressed[KEY_DELETE] ) {
2833                 memset( &Controls, 0, sizeof(control_info) );
2834         }
2835         #endif
2836 }
2837 #else
2838
2839
2840 fix Next_toggle_time[3]={0,0,0};
2841
2842 int allowed_to_toggle(int i)
2843 {
2844   //used for keeping tabs of when its ok to toggle headlight,primary,and secondary
2845  
2846         if (Next_toggle_time[i] > GameTime)
2847                 if (Next_toggle_time[i] < GameTime + (F1_0/8))  //      In case time is bogus, never wait > 1 second.
2848                         return 0;
2849
2850         Next_toggle_time[i] = GameTime + (F1_0/8);
2851
2852         return 1;
2853 }
2854
2855
2856 #ifdef D2X_KEYS
2857 //added on 2/7/99 by Victor Rachels for jostick state setting
2858 int d2x_joystick_ostate[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
2859 //end this section adition - VR
2860 #endif
2861
2862
2863 void controls_read_all()
2864 {
2865         int i;
2866         int slide_on, bank_on;
2867         int dx, dy;
2868 #ifdef SDL_INPUT
2869         int dz;
2870 #endif
2871         int idx, idy;
2872         fix ctime;
2873         fix mouse_axis[3] = {0,0,0};
2874         int raw_joy_axis[JOY_MAX_AXES];
2875         int mouse_buttons;
2876         fix kp, kh;
2877         ubyte channel_masks;
2878         int use_mouse, use_joystick;
2879
2880         mouse_buttons=0;
2881         use_mouse=0;
2882
2883         {
2884                 fix temp = Controls.heading_time;
2885                 fix temp1 = Controls.pitch_time;
2886                 memset( &Controls, 0, sizeof(control_info) );
2887                 Controls.heading_time = temp;
2888                 Controls.pitch_time = temp1;
2889         }
2890         slide_on = 0;
2891         bank_on = 0;
2892
2893         ctime = timer_get_fixed_seconds();
2894
2895         //---------  Read Joystick -----------
2896 #ifndef MACINTOSH
2897         if ( (LastReadTime + JOYSTICK_READ_TIME > ctime) && (Config_control_type!=CONTROL_THRUSTMASTER_FCS) ) {
2898 # ifndef __MSDOS__
2899                 if ((ctime < 0) && (LastReadTime >= 0))
2900 # else
2901                 if ((ctime < 0) && (LastReadTime > 0))
2902 # endif
2903                         LastReadTime = ctime;
2904                 use_joystick=1;
2905         } else if ((Config_control_type>0) && (Config_control_type<5) ) {
2906                 LastReadTime = ctime;
2907                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
2908
2909                 for (i = 0; i < joy_num_axes; i++)
2910                 {
2911 #ifndef SDL_INPUT
2912                         if (channel_masks&(1<<i))       {
2913 #endif
2914                                 int joy_null_value = 10;
2915
2916                                 if ( (i==3) && (Config_control_type==CONTROL_THRUSTMASTER_FCS) )        {
2917                                         kconfig_read_fcs( raw_joy_axis[i] );
2918                                 } else {
2919                                         raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2920         
2921                                         if (kc_joystick[23].value==i)           // If this is the throttle
2922                                                 joy_null_value = 20;            // Then use a larger dead-zone
2923         
2924                                         if (raw_joy_axis[i] > joy_null_value) 
2925                                           raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2926                                         else if (raw_joy_axis[i] < -joy_null_value)
2927                                           raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2928                                         else
2929                                           raw_joy_axis[i] = 0;
2930                                         joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2931                                 }
2932 #ifndef SDL_INPUT
2933                         } else {
2934                                 joy_axis[i] = 0;
2935                         }
2936 #endif
2937                 }       
2938                 use_joystick=1;
2939         } else {
2940                 for (i = 0; i < joy_num_axes; i++)
2941                         joy_axis[i] = 0;
2942                 use_joystick=0;
2943         }
2944 #else   // MACINTOSH
2945         //---------  Read Joystick -----------
2946         if ((Config_control_type>0) && (Config_control_type<5) ) {
2947                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
2948                 for (i=0; i<4; i++ )    {
2949                         if (channel_masks&(1<<i))       {
2950                                 int joy_null_value = 10;
2951
2952                                 raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
2953
2954                                 if (kc_joystick[23].value==i)           // If this is the throttle
2955                                         joy_null_value = 20;                            // Then use a larger dead-zone
2956
2957                                 if (raw_joy_axis[i] > joy_null_value) 
2958                                         raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
2959                                 else if (raw_joy_axis[i] < -joy_null_value)
2960                                         raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
2961                                 else
2962                                         raw_joy_axis[i] = 0;
2963                                 joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
2964                         } else {
2965                                 joy_axis[i] = 0;
2966                         }
2967                 }       
2968                 use_joystick=1;
2969         } else {
2970                 for (i=0; i<4; i++ )
2971                         joy_axis[i] = 0;
2972                 use_joystick=0;
2973         }
2974 #endif          // ifndef MACINTOSH
2975
2976         if (Config_control_type==5 && !CybermouseActive) {
2977                 //---------  Read Mouse -----------
2978 #ifdef SDL_INPUT
2979                 mouse_get_delta_z( &dx, &dy, &dz );
2980 #else
2981                 mouse_get_delta( &dx, &dy );
2982 #endif
2983                 mouse_axis[0] = (dx*FrameTime)/35;
2984                 mouse_axis[1] = (dy*FrameTime)/25;
2985 #ifdef SDL_INPUT
2986                 mouse_axis[2] = (dz*FrameTime);
2987 #endif
2988                 mouse_buttons = mouse_get_btns();
2989                 //mprintf(( 0, "Mouse %d,%d b:%d, 0x%x\n", mouse_axis[0], mouse_axis[1], mouse_buttons, FrameTime ));
2990                 use_mouse=1;
2991         } else if (Config_control_type==6 && !CybermouseActive) {
2992                 //---------  Read Cyberman -----------
2993                 mouse_get_cyberman_pos(&idx,&idy );
2994                 mouse_axis[0] = (idx*FrameTime)/128;
2995                 mouse_axis[1] = (idy*FrameTime)/128;
2996                 mouse_buttons = mouse_get_btns();
2997                 use_mouse=1;
2998         } else if (CybermouseActive) {
2999 //              ReadOWL (kc_external_control);
3000 //              CybermouseAdjust();
3001         } else {
3002                 mouse_axis[0] = 0;
3003                 mouse_axis[1] = 0;
3004                 mouse_buttons = 0;
3005                 use_mouse=0;
3006         }
3007
3008 #ifdef D2X_KEYS
3009         //added on 2/4/99 by Victor Rachels for d1x keys
3010         //--------- Read primary weapon select -------------
3011         //the following "if" added by WraithX to stop deadies from switchin weapons, 4/14/00
3012         if (!Player_is_dead)
3013         {
3014                 {
3015                         int d2x_joystick_state[10];
3016
3017                         for(i=0;i<10;i++)
3018                                 d2x_joystick_state[i] = joy_get_button_state(kc_d2x[i*2+1].value);
3019
3020
3021                         //----------------Weapon 1----------------
3022                         if(key_down_count(kc_d2x[0].value) ||
3023                            (joy_get_button_state(kc_d2x[1].value) &&
3024                                 (d2x_joystick_state[0]!=d2x_joystick_ostate[0]) ) )
3025                         {
3026                                 //int i, valu=0;
3027                                 do_weapon_select(0,0);
3028                                 /*
3029                                 for(i=MAX_PRIMARY_WEAPONS;i<MAX_PRIMARY_WEAPONS+NEWPRIMS;i++)
3030                                         if(primary_order[i]>primary_order[valu]&&player_has_weapon(i,0))
3031                                                 valu = i;
3032                                 LaserPowSelected = valu;
3033                                 */
3034                         }
3035                         //----------------Weapon 2----------------
3036                         if(key_down_count(kc_d2x[2].value) ||
3037                            (joy_get_button_state(kc_d2x[3].value) &&
3038                                 (d2x_joystick_state[1]!=d2x_joystick_ostate[1]) ) )
3039                                 do_weapon_select(1,0);
3040                         //----------------Weapon 3----------------
3041                         if(key_down_count(kc_d2x[4].value) ||
3042                            (joy_get_button_state(kc_d2x[5].value) &&
3043                                 (d2x_joystick_state[2]!=d2x_joystick_ostate[2]) ) )
3044                                 do_weapon_select(2,0);
3045                         //----------------Weapon 4----------------
3046                         if(key_down_count(kc_d2x[6].value) ||
3047                            (joy_get_button_state(kc_d2x[7].value) &&
3048                                 (d2x_joystick_state[3]!=d2x_joystick_ostate[3]) ) )
3049                                 do_weapon_select(3,0);
3050                         //----------------Weapon 5----------------
3051                         if(key_down_count(kc_d2x[8].value) ||
3052                            (joy_get_button_state(kc_d2x[9].value) &&
3053                                 (d2x_joystick_state[4]!=d2x_joystick_ostate[4]) ) )
3054                                 do_weapon_select(4,0);
3055  
3056                         //--------- Read secondary weapon select ----------
3057                         //----------------Weapon 6----------------
3058                         if(key_down_count(kc_d2x[10].value) ||
3059                            (joy_get_button_state(kc_d2x[11].value) &&
3060                                 (d2x_joystick_state[5]!=d2x_joystick_ostate[5]) ) )
3061                                 do_weapon_select(0,1);
3062                         //----------------Weapon 7----------------
3063                         if(key_down_count(kc_d2x[12].value) ||
3064                            (joy_get_button_state(kc_d2x[13].value) &&
3065                                 (d2x_joystick_state[6]!=d2x_joystick_ostate[6]) ) )
3066                                 do_weapon_select(1,1);
3067                         //----------------Weapon 8----------------
3068                         if(key_down_count(kc_d2x[14].value) ||
3069                            (joy_get_button_state(kc_d2x[15].value) &&
3070                                 (d2x_joystick_state[7]!=d2x_joystick_ostate[7]) ) )
3071                                 do_weapon_select(2,1);
3072                         //----------------Weapon 9----------------
3073                         if(key_down_count(kc_d2x[16].value) ||
3074                            (joy_get_button_state(kc_d2x[17].value) &&
3075                                 (d2x_joystick_state[8]!=d2x_joystick_ostate[8]) ) )
3076                                 do_weapon_select(3,1);
3077                         //----------------Weapon 0----------------
3078                         if(key_down_count(kc_d2x[18].value) ||
3079                            (joy_get_button_state(kc_d2x[19].value) &&
3080                                 (d2x_joystick_state[9]!=d2x_joystick_ostate[9]) ) )
3081                                 do_weapon_select(4,1);
3082                         memcpy(d2x_joystick_ostate,d2x_joystick_state,10*sizeof(int));
3083                 }
3084                 //end this section addition - VR
3085
3086
3087         }//end "if (!Player_is_dead)" - WraithX
3088 #endif
3089
3090
3091 //------------- Read slide_on -------------
3092         
3093         // From console...
3094         slide_on |= console_control_state(CONCNTL_STRAFE);
3095
3096         // From joystick...
3097         if ((use_joystick)&&(kc_joystick[5].value<255)) slide_on |= joy_get_button_state( kc_joystick[5].value );
3098         // From mouse...
3099         if ((use_mouse)&&(kc_mouse[5].value<255)) slide_on |= mouse_buttons & (1<<kc_mouse[5].value);
3100
3101 //------------- Read bank_on ---------------
3102
3103         // From console...
3104         bank_on |= console_control_state(CONCNTL_BANK);
3105
3106         // From joystick...
3107         if ( (use_joystick)&&(kc_joystick[10].value < 255 )) bank_on |= joy_get_button_state( kc_joystick[10].value );
3108         // From mouse...
3109         if ( (use_mouse)&&(kc_mouse[10].value < 255 )) bank_on |= mouse_buttons & (1<<kc_mouse[10].value);
3110
3111 //------------ Read pitch_time -----------
3112         if ( !slide_on )        {
3113                 // mprintf((0, "pitch: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
3114                 kp = 0;
3115
3116                 // From console...
3117                 kp += console_control_down_time(CONCNTL_LOOKDOWN) / (PH_SCALE * 2);
3118                 kp -= console_control_down_time(CONCNTL_LOOKUP) / (PH_SCALE * 2);
3119
3120                 // From Cyberman...
3121                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3122                         kp += mouse_button_down_time(MB_PITCH_FORWARD)/(PH_SCALE*2);
3123                         kp -= mouse_button_down_time(MB_PITCH_BACKWARD)/(PH_SCALE*2);
3124                 }
3125         
3126                 if (kp == 0)
3127                         Controls.pitch_time = 0;
3128                 else if (kp > 0) {
3129                         if (Controls.pitch_time < 0)
3130                                 Controls.pitch_time = 0;
3131                 } else // kp < 0
3132                         if (Controls.pitch_time > 0)
3133                                 Controls.pitch_time = 0;
3134                 Controls.pitch_time += kp;
3135         
3136                 // From joystick...
3137                 if ( (use_joystick)&&(kc_joystick[13].value < 255 ))    {
3138                         if ( !kc_joystick[14].value )           // If not inverted...
3139                                 Controls.pitch_time -= (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
3140                         else
3141                                 Controls.pitch_time += (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
3142                 }
3143         
3144                 // From mouse...
3145                 //mprintf(( 0, "UM: %d, PV: %d\n", use_mouse, kc_mouse[13].value ));
3146                 if ( (use_mouse)&&(kc_mouse[13].value < 255) )  {
3147                         if ( !kc_mouse[14].value )              // If not inverted...
3148                                 Controls.pitch_time -= (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
3149                         else
3150                                 Controls.pitch_time += (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
3151                 }
3152         } else {
3153                 Controls.pitch_time = 0;
3154         }
3155
3156
3157 // the following "if" added by WraithX, 4/14/00
3158 // done so that dead players can't move
3159 if (!Player_is_dead)
3160 {
3161 //----------- Read vertical_thrust_time -----------------
3162
3163         if ( slide_on ) {
3164                 // From console...
3165                 Controls.vertical_thrust_time += console_control_down_time(CONCNTL_LOOKDOWN);
3166                 Controls.vertical_thrust_time -= console_control_down_time(CONCNTL_LOOKUP);
3167
3168                 // From Cyberman...
3169                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3170                         Controls.vertical_thrust_time -= mouse_button_down_time(MB_PITCH_FORWARD);
3171                         Controls.vertical_thrust_time += mouse_button_down_time(MB_PITCH_BACKWARD);
3172                 }
3173         
3174                 // From joystick...
3175                 if ((use_joystick)&&( kc_joystick[13].value < 255 ))    {
3176                         if ( !kc_joystick[14].value )           // If not inverted...
3177                                 Controls.vertical_thrust_time += joy_axis[kc_joystick[13].value];
3178                         else
3179                                 Controls.vertical_thrust_time -= joy_axis[kc_joystick[13].value];
3180                 }
3181         
3182                 // From mouse...
3183                 if ( (use_mouse)&&(kc_mouse[13].value < 255 ))  {
3184                         if ( !kc_mouse[14].value )              // If not inverted...
3185                                 Controls.vertical_thrust_time -= mouse_axis[kc_mouse[13].value];
3186                         else
3187                                 Controls.vertical_thrust_time += mouse_axis[kc_mouse[13].value];
3188                 }
3189         }
3190
3191         // From console...
3192         Controls.vertical_thrust_time += console_control_down_time(CONCNTL_MOVEUP);
3193         Controls.vertical_thrust_time -= console_control_down_time(CONCNTL_MOVEDOWN);
3194
3195         // From joystick...
3196         if ((use_joystick)&&( kc_joystick[19].value < 255 ))    {
3197                 if ( !kc_joystick[20].value )           // If not inverted...
3198                         Controls.vertical_thrust_time += joy_axis[kc_joystick[19].value];
3199                 else
3200                         Controls.vertical_thrust_time -= joy_axis[kc_joystick[19].value];
3201         }
3202
3203         // From joystick buttons
3204         if ( (use_joystick)&&(kc_joystick[8].value < 255 )) Controls.vertical_thrust_time += joy_get_button_down_time( kc_joystick[8].value );
3205         if ( (use_joystick)&&(kc_joystick[9].value < 255 )) Controls.vertical_thrust_time -= joy_get_button_down_time( kc_joystick[9].value );
3206
3207         // From mouse buttons
3208         if ( (use_mouse)&&(kc_mouse[8].value < 255 )) Controls.vertical_thrust_time += mouse_button_down_time( kc_mouse[8].value );
3209         if ( (use_mouse)&&(kc_mouse[9].value < 255 )) Controls.vertical_thrust_time -= mouse_button_down_time( kc_mouse[9].value );
3210
3211         // From mouse...
3212         if ( (use_mouse)&&(kc_mouse[19].value < 255 ))  {
3213                 if ( !kc_mouse[20].value )              // If not inverted...
3214                         Controls.vertical_thrust_time += mouse_axis[kc_mouse[19].value];
3215                 else
3216                         Controls.vertical_thrust_time -= mouse_axis[kc_mouse[19].value];
3217         }
3218
3219         // From Cyberman...
3220         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3221                 Controls.vertical_thrust_time += mouse_button_down_time(MB_Z_UP)/2;
3222                 Controls.vertical_thrust_time -= mouse_button_down_time(MB_Z_DOWN)/2;
3223         }
3224 }// end "if" added by WraithX
3225
3226 //---------- Read heading_time -----------
3227
3228         if (!slide_on && !bank_on)      {
3229                 //mprintf((0, "heading: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
3230                 kh = 0;
3231
3232                 // From console...
3233                 kh -= console_control_down_time(CONCNTL_LEFT) / PH_SCALE;
3234                 kh += console_control_down_time(CONCNTL_RIGHT) / PH_SCALE;
3235
3236                 // From Cyberman...
3237                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3238                         kh -= mouse_button_down_time(MB_HEAD_LEFT)/PH_SCALE;
3239                         kh += mouse_button_down_time(MB_HEAD_RIGHT)/PH_SCALE;
3240                 }
3241         
3242                 if (kh == 0)
3243                         Controls.heading_time = 0;
3244                 else if (kh > 0) {
3245                         if (Controls.heading_time < 0)
3246                                 Controls.heading_time = 0;
3247                 } else // kh < 0
3248                         if (Controls.heading_time > 0)
3249                                 Controls.heading_time = 0;
3250                 Controls.heading_time += kh;
3251
3252                 // From joystick...
3253                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
3254                         if ( !kc_joystick[16].value )           // If not inverted...
3255                                 Controls.heading_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3256                         else
3257                                 Controls.heading_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3258                 }
3259         
3260                 // From mouse...
3261                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3262                         if ( !kc_mouse[16].value )              // If not inverted...
3263                                 Controls.heading_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3264                         else
3265                                 Controls.heading_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3266                 }
3267         } else {
3268                 Controls.heading_time = 0;
3269         }
3270
3271 // the following "if" added by WraithX, 4/14/00
3272 // done so that dead players can't move
3273 if (!Player_is_dead)
3274 {
3275 //----------- Read sideways_thrust_time -----------------
3276
3277         if ( slide_on ) {
3278                 // From console...
3279                 Controls.sideways_thrust_time -= console_control_down_time(CONCNTL_LEFT);
3280                 Controls.sideways_thrust_time += console_control_down_time(CONCNTL_RIGHT);
3281
3282                 // From joystick...
3283                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
3284                         if ( !kc_joystick[16].value )           // If not inverted...
3285                                 Controls.sideways_thrust_time += joy_axis[kc_joystick[15].value];
3286                         else
3287                                 Controls.sideways_thrust_time -= joy_axis[kc_joystick[15].value];
3288                 }
3289                 
3290                 // From cyberman
3291                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3292                         Controls.sideways_thrust_time -= mouse_button_down_time(MB_HEAD_LEFT);
3293                         Controls.sideways_thrust_time += mouse_button_down_time(MB_HEAD_RIGHT);
3294                 }
3295         
3296                 // From mouse...
3297                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3298                         if ( !kc_mouse[16].value )              // If not inverted...
3299                                 Controls.sideways_thrust_time += mouse_axis[kc_mouse[15].value];
3300                         else
3301                                 Controls.sideways_thrust_time -= mouse_axis[kc_mouse[15].value];
3302                 }
3303         }
3304
3305         // From console...
3306         Controls.sideways_thrust_time -= console_control_down_time(CONCNTL_MOVELEFT);
3307         Controls.sideways_thrust_time += console_control_down_time(CONCNTL_MOVERIGHT);
3308
3309         // From joystick...
3310         if ( (use_joystick)&&(kc_joystick[17].value < 255 ))    {
3311                 if ( !kc_joystick[18].value )           // If not inverted...
3312                         Controls.sideways_thrust_time -= joy_axis[kc_joystick[17].value];
3313                 else
3314                         Controls.sideways_thrust_time += joy_axis[kc_joystick[17].value];
3315         }
3316
3317         // From joystick buttons
3318         if ( (use_joystick)&&(kc_joystick[6].value < 255 )) Controls.sideways_thrust_time -= joy_get_button_down_time( kc_joystick[6].value );
3319         if ( (use_joystick)&&(kc_joystick[7].value < 255 )) Controls.sideways_thrust_time += joy_get_button_down_time( kc_joystick[7].value );
3320
3321         // From mouse buttons
3322         if ( (use_mouse)&&(kc_mouse[6].value < 255 )) Controls.sideways_thrust_time -= mouse_button_down_time( kc_mouse[6].value );
3323         if ( (use_mouse)&&(kc_mouse[7].value < 255 )) Controls.sideways_thrust_time += mouse_button_down_time( kc_mouse[7].value );
3324
3325         // From mouse...
3326         if ( (use_mouse)&&(kc_mouse[17].value < 255 ))  {
3327                 if ( !kc_mouse[18].value )              // If not inverted...
3328                         Controls.sideways_thrust_time += mouse_axis[kc_mouse[17].value];
3329                 else
3330                         Controls.sideways_thrust_time -= mouse_axis[kc_mouse[17].value];
3331         }
3332 }// end "if" added by WraithX
3333
3334 //----------- Read bank_time -----------------
3335
3336         if ( bank_on )  {
3337                 // From console...
3338                 Controls.bank_time -= console_control_down_time(CONCNTL_LEFT);
3339                 Controls.bank_time += console_control_down_time(CONCNTL_RIGHT);
3340
3341                 // From Cyberman...
3342                 if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3343                         Controls.bank_time -= mouse_button_down_time(MB_HEAD_LEFT);
3344                         Controls.bank_time += mouse_button_down_time(MB_HEAD_RIGHT);
3345                 }
3346
3347                 // From joystick...
3348                 if ( (use_joystick)&&(kc_joystick[15].value < 255) )    {
3349                         if ( !kc_joystick[16].value )           // If not inverted...
3350                                 Controls.bank_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3351                         else
3352                                 Controls.bank_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
3353                 }
3354         
3355                 // From mouse...
3356                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
3357                         if ( !kc_mouse[16].value )              // If not inverted...
3358                                 Controls.bank_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3359                         else
3360                                 Controls.bank_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
3361                 }
3362         }
3363
3364         // From console...
3365         Controls.bank_time += console_control_down_time(CONCNTL_BANKLEFT);
3366         Controls.bank_time -= console_control_down_time(CONCNTL_BANKRIGHT);
3367
3368         // From joystick...
3369         if ( (use_joystick)&&(kc_joystick[21].value < 255) )    {
3370                 if ( !kc_joystick[22].value )           // If not inverted...
3371                         Controls.bank_time -= joy_axis[kc_joystick[21].value];
3372                 else
3373                         Controls.bank_time += joy_axis[kc_joystick[21].value];
3374         }
3375
3376         // From joystick buttons
3377         if ( (use_joystick)&&(kc_joystick[11].value < 255 )) Controls.bank_time += joy_get_button_down_time( kc_joystick[11].value );
3378         if ( (use_joystick)&&(kc_joystick[12].value < 255 )) Controls.bank_time -= joy_get_button_down_time( kc_joystick[12].value );
3379
3380         // From mouse buttons
3381         if ( (use_mouse)&&(kc_mouse[11].value < 255 )) Controls.bank_time += mouse_button_down_time( kc_mouse[11].value );
3382         if ( (use_mouse)&&(kc_mouse[12].value < 255 )) Controls.bank_time -= mouse_button_down_time( kc_mouse[12].value );
3383
3384         // From mouse...
3385         if ( (use_mouse)&&(kc_mouse[21].value < 255 ))  {
3386                 if ( !kc_mouse[22].value )              // If not inverted...
3387                         Controls.bank_time += mouse_axis[kc_mouse[21].value];
3388                 else
3389                         Controls.bank_time -= mouse_axis[kc_mouse[21].value];
3390         }
3391
3392         // From Cyberman
3393         if ((use_mouse)&&(Config_control_type==CONTROL_CYBERMAN))       {
3394                 Controls.bank_time += mouse_button_down_time(MB_BANK_LEFT);
3395                 Controls.bank_time -= mouse_button_down_time(MB_BANK_RIGHT);
3396         }
3397
3398 // the following "if" added by WraithX, 4/14/00
3399 // done so that dead players can't move
3400 if (!Player_is_dead)
3401 {
3402 //----------- Read forward_thrust_time -------------
3403
3404         // From console...
3405         Controls.forward_thrust_time += console_control_down_time(CONCNTL_FORWARD);
3406         Controls.forward_thrust_time -= console_control_down_time(CONCNTL_BACK);
3407
3408         // From joystick...
3409         if ( (use_joystick)&&(kc_joystick[23].value < 255 ))    {
3410                 if ( !kc_joystick[24].value )           // If not inverted...
3411                         Controls.forward_thrust_time -= joy_axis[kc_joystick[23].value];
3412                 else
3413                         Controls.forward_thrust_time += joy_axis[kc_joystick[23].value];
3414         }
3415
3416         // From joystick buttons
3417         if ( (use_joystick)&&(kc_joystick[2].value < 255 )) Controls.forward_thrust_time += joy_get_button_down_time( kc_joystick[2].value );
3418         if ( (use_joystick)&&(kc_joystick[3].value < 255 )) Controls.forward_thrust_time -= joy_get_button_down_time( kc_joystick[3].value );
3419
3420         // From mouse...
3421         if ( (use_mouse)&&(kc_mouse[23].value < 255 ))  {
3422                 if ( !kc_mouse[24].value )              // If not inverted...
3423                         Controls.forward_thrust_time -= mouse_axis[kc_mouse[23].value];
3424                 else
3425                         Controls.forward_thrust_time += mouse_axis[kc_mouse[23].value];
3426         }
3427
3428         // From mouse buttons
3429         if ( (use_mouse)&&(kc_mouse[2].value < 255 )) Controls.forward_thrust_time += mouse_button_down_time( kc_mouse[2].value );
3430         if ( (use_mouse)&&(kc_mouse[3].value < 255 )) Controls.forward_thrust_time -= mouse_button_down_time( kc_mouse[3].value );
3431
3432 //----------- Read afterburner_state -------------
3433
3434         // From console...
3435         Controls.afterburner_state |= console_control_state(CONCNTL_LEFT);
3436
3437         if ( (use_mouse)&&(kc_mouse[27].value < 255 )) Controls.afterburner_state |= mouse_button_state(kc_mouse[27].value);
3438
3439         if ( (use_joystick)&&(kc_joystick[27].value < 255 )) Controls.afterburner_state |= joy_get_button_state(kc_joystick[27].value);
3440
3441 //-------Read headlight key--------------------------
3442
3443         // From console...
3444         Controls.headlight_count += console_control_down_count(CONCNTL_HEADLIGHT);
3445
3446                   if ((use_joystick)&&(kc_joystick[30].value < 255 )) 
3447                Controls.headlight_count+=joy_get_button_down_cnt(kc_joystick[30].value);
3448
3449 //--------Read Cycle Primary Key------------------
3450
3451         // From console...
3452         Controls.cycle_primary_count += console_control_down_count(CONCNTL_CYCLE);
3453
3454              if ((use_joystick)&&(kc_joystick[28].value < 255 )) 
3455          Controls.cycle_primary_count+=joy_get_button_down_cnt(kc_joystick[28].value);
3456
3457
3458 //--------Read Cycle Secondary Key------------------
3459
3460         // From console...
3461         Controls.cycle_secondary_count += console_control_down_count(CONCNTL_CYCLE2);
3462
3463              if ((use_joystick)&&(kc_joystick[29].value < 255 )) 
3464                         Controls.cycle_secondary_count=joy_get_button_down_cnt(kc_joystick[29].value);
3465
3466 //--------Read Toggle Bomb key----------------------
3467
3468         // From console...
3469         if (console_control_down_count(CONCNTL_TOGGLEBOMB))
3470          {
3471           int bomb = Secondary_last_was_super[PROXIMITY_INDEX]?PROXIMITY_INDEX:SMART_MINE_INDEX;
3472
3473                          if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX] &&
3474                                   !Players[Player_num].secondary_ammo[SMART_MINE_INDEX])
3475                            {
3476                                  digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
3477                                  HUD_init_message ("No bombs available!");
3478                                 }
3479                          else
3480                                 {       
3481                                  if (Players[Player_num].secondary_ammo[bomb]==0)
3482                                         {
3483                                          digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
3484                                          HUD_init_message ("No %s available!",(bomb==SMART_MINE_INDEX)?"Smart mines":"Proximity bombs");
3485                                         }
3486                                   else
3487                                         {
3488                                Secondary_last_was_super[PROXIMITY_INDEX]=!Secondary_last_was_super[PROXIMITY_INDEX];
3489                                          digi_play_sample_once( SOUND_GOOD_SELECTION_SECONDARY, F1_0 );
3490                                         }
3491                                 }
3492                         }
3493           
3494 //---------Read Energy->Shield key----------
3495
3496         // From console...
3497         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && console_control_state(CONCNTL_NRGSHIELD))
3498                 transfer_energy_to_shield(console_control_down_time(CONCNTL_NRGSHIELD));
3499
3500 //----------- Read fire_primary_down_count
3501
3502         // From console...
3503         Controls.fire_primary_down_count += console_control_down_count(CONCNTL_ATTACK);
3504
3505         if ((use_joystick)&&(kc_joystick[0].value < 255 )) Controls.fire_primary_down_count += joy_get_button_down_cnt(kc_joystick[0].value);
3506         if ((use_mouse)&&(kc_mouse[0].value < 255 )) Controls.fire_primary_down_count += mouse_button_down_count(kc_mouse[0].value);
3507
3508 //----------- Read fire_primary_state
3509
3510         // From console...
3511         Controls.fire_primary_state |= console_control_state(CONCNTL_ATTACK);
3512
3513         if ((use_joystick)&&(kc_joystick[0].value < 255 )) Controls.fire_primary_state |= joy_get_button_state(kc_joystick[0].value);
3514         if ((use_mouse)&&(kc_mouse[0].value < 255) ) Controls.fire_primary_state |= mouse_button_state(kc_mouse[0].value);
3515
3516 //----------- Read fire_secondary_down_count
3517
3518         // From console...
3519         Controls.fire_secondary_down_count += console_control_down_count(CONCNTL_ATTACK2);
3520
3521         if ((use_joystick)&&(kc_joystick[1].value < 255 )) Controls.fire_secondary_down_count += joy_get_button_down_cnt(kc_joystick[1].value);
3522         if ((use_mouse)&&(kc_mouse[1].value < 255 )) Controls.fire_secondary_down_count += mouse_button_down_count(kc_mouse[1].value);
3523
3524 //----------- Read fire_secondary_state
3525
3526         // From console...
3527         Controls.fire_secondary_state |= console_control_state(CONCNTL_ATTACK2);
3528
3529         if ((use_joystick)&&(kc_joystick[1].value < 255 )) Controls.fire_secondary_state |= joy_get_button_state(kc_joystick[1].value);
3530         if ((use_mouse)&&(kc_mouse[1].value < 255) ) Controls.fire_secondary_state |= mouse_button_state(kc_mouse[1].value);
3531
3532 //----------- Read fire_flare_down_count
3533
3534         // From console...
3535         Controls.fire_flare_down_count += console_control_down_count(CONCNTL_FLARE);
3536
3537         if ((use_joystick)&&(kc_joystick[4].value < 255 )) Controls.fire_flare_down_count += joy_get_button_down_cnt(kc_joystick[4].value);
3538         if ((use_mouse)&&(kc_mouse[4].value < 255 )) Controls.fire_flare_down_count += mouse_button_down_count(kc_mouse[4].value);
3539
3540 //----------- Read drop_bomb_down_count
3541
3542         // From console...
3543         Controls.drop_bomb_down_count += console_control_down_count(CONCNTL_BOMB);
3544
3545         if ((use_joystick)&&(kc_joystick[26].value < 255 )) Controls.drop_bomb_down_count += joy_get_button_down_cnt(kc_joystick[26].value);
3546         if ((use_mouse)&&(kc_mouse[26].value < 255 )) Controls.drop_bomb_down_count += mouse_button_down_count(kc_mouse[26].value);
3547
3548 //----------- Read rear_view_down_count
3549
3550         // From console...
3551         Controls.rear_view_down_count += console_control_down_count(CONCNTL_REARVIEW);
3552
3553         if ((use_joystick)&&(kc_joystick[25].value < 255 )) Controls.rear_view_down_count += joy_get_button_down_cnt(kc_joystick[25].value);
3554         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_count += mouse_button_down_count(kc_mouse[25].value);
3555
3556 //----------- Read rear_view_down_state
3557
3558         // From console...
3559         Controls.rear_view_down_state |= console_control_state(CONCNTL_REARVIEW);
3560
3561         if ((use_joystick)&&(kc_joystick[25].value < 255 )) Controls.rear_view_down_state |= joy_get_button_state(kc_joystick[25].value);
3562         if ((use_mouse)&&(kc_mouse[25].value < 255 )) Controls.rear_view_down_state |= mouse_button_state(kc_mouse[25].value);
3563
3564 }//end "if" added by WraithX
3565
3566 //----------- Read automap_down_count
3567
3568         // From console...
3569         Controls.automap_down_count += console_control_down_count(CONCNTL_AUTOMAP);
3570
3571 //----------- Read automap_state
3572
3573         // From console...
3574         Controls.automap_state |= console_control_state(CONCNTL_AUTOMAP);
3575
3576 //----------- Read stupid-cruise-control-type of throttle.
3577         {
3578                 // From console...
3579                 Cruise_speed += console_control_down_time(CONCNTL_CRUISEUP);
3580                 Cruise_speed -= console_control_down_time(CONCNTL_CRUISEDOWN);
3581
3582                 if ( (kc_keyboard[42].value < 255) && (key_down_count(kc_keyboard[42].value)) )
3583                         Cruise_speed = 0;
3584                 if ( (kc_keyboard[43].value < 255) && (key_down_count(kc_keyboard[43].value)) )
3585                         Cruise_speed = 0;
3586
3587                 // From console...
3588                 if (console_control_down_count(CONCNTL_CRUISEOFF))
3589                         Cruise_speed = 0;
3590
3591                 if (Cruise_speed > i2f(100) ) Cruise_speed = i2f(100);
3592                 if (Cruise_speed < 0 ) Cruise_speed = 0;
3593         
3594                 if (Controls.forward_thrust_time==0)
3595                         Controls.forward_thrust_time = fixmul(Cruise_speed,FrameTime)/100;
3596         }
3597
3598 #if !defined(MACINTOSH)
3599         //read_head_tracker();
3600
3601         // Read external controls
3602         // if (kc_use_external_control || CybermouseActive)
3603         //      kconfig_read_external_controls();
3604 #endif
3605
3606 //----------- Clamp values between -FrameTime and FrameTime
3607         if (FrameTime > F1_0 )
3608                 mprintf( (1, "Bogus frame time of %.2f seconds\n", f2fl(FrameTime) ));
3609
3610         if (Controls.pitch_time > FrameTime/2 ) Controls.pitch_time = FrameTime/2;
3611         if (Controls.vertical_thrust_time > FrameTime ) Controls.vertical_thrust_time = FrameTime;
3612         if (Controls.heading_time > FrameTime ) Controls.heading_time = FrameTime;
3613         if (Controls.sideways_thrust_time > FrameTime ) Controls.sideways_thrust_time = FrameTime;
3614         if (Controls.bank_time > FrameTime ) Controls.bank_time = FrameTime;
3615         if (Controls.forward_thrust_time > FrameTime ) Controls.forward_thrust_time = FrameTime;
3616 //      if (Controls.afterburner_time > FrameTime ) Controls.afterburner_time = FrameTime;
3617
3618         if (Controls.pitch_time < -FrameTime/2 ) Controls.pitch_time = -FrameTime/2;
3619         if (Controls.vertical_thrust_time < -FrameTime ) Controls.vertical_thrust_time = -FrameTime;
3620         if (Controls.heading_time < -FrameTime ) Controls.heading_time = -FrameTime;
3621         if (Controls.sideways_thrust_time < -FrameTime ) Controls.sideways_thrust_time = -FrameTime;
3622         if (Controls.bank_time < -FrameTime ) Controls.bank_time = -FrameTime;
3623         if (Controls.forward_thrust_time < -FrameTime ) Controls.forward_thrust_time = -FrameTime;
3624 //      if (Controls.afterburner_time < -FrameTime ) Controls.afterburner_time = -FrameTime;
3625
3626
3627 //--------- Don't do anything if in debug mode
3628         #ifndef RELEASE
3629         if ( keyd_pressed[KEY_DELETE] ) {
3630                 memset( &Controls, 0, sizeof(control_info) );
3631         }
3632         #endif
3633 }
3634 #endif
3635
3636
3637 void reset_cruise(void)
3638 {
3639         Cruise_speed=0;
3640 }
3641
3642
3643 void kc_set_controls()
3644 {
3645         int i, j;
3646
3647 #if 0
3648         for (i=0; i<NUM_KEY_CONTROLS; i++ )     
3649                 kc_keyboard[i].value = kconfig_settings[0][i];
3650 #else
3651
3652         for (i=0; i<NUM_KEY_CONTROLS; i++ )
3653                 kc_keyboard[i].value = 255;
3654
3655         for (j=0; j<256; j++)
3656                 if (key_binding(j))
3657                         for (i=0; i<NUM_KEY_CONTROLS; i++ )
3658                                 if (kc_keyboard[i].value == 255
3659                                         && !strncmp(key_binding(j), kc_bind_text[i], 13)) {
3660                                         kc_keyboard[i].value = j;
3661                                         break;
3662                                 }
3663 #endif
3664
3665         if ( (Config_control_type>0) && (Config_control_type<5)) {
3666                 for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
3667                         kc_joystick[i].value = kconfig_settings[Config_control_type][i];
3668                         if (kc_joystick[i].type == BT_INVERT )  {
3669                                 if (kc_joystick[i].value!=1)
3670                                         kc_joystick[i].value    = 0;
3671                                 kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
3672                         }
3673                 }
3674         } else if (Config_control_type>4 && Config_control_type<CONTROL_WINJOYSTICK) {
3675                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
3676                         kc_mouse[i].value = kconfig_settings[Config_control_type][i];
3677                         if (kc_mouse[i].type == BT_INVERT )     {
3678                                 if (kc_mouse[i].value!=1)
3679                                         kc_mouse[i].value       = 0;
3680                                 kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
3681                         }
3682                 }
3683         } else if (Config_control_type == CONTROL_WINJOYSTICK) {
3684                 for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
3685                         kc_superjoy[i].value = kconfig_settings[Config_control_type][i];
3686                         if (kc_superjoy[i].type == BT_INVERT )  {
3687                                 if (kc_superjoy[i].value!=1)
3688                                         kc_superjoy[i].value    = 0;
3689                                 kconfig_settings[Config_control_type][i] = kc_superjoy[i].value;
3690                         }
3691                 }
3692         }
3693
3694 #ifdef D2X_KEYS
3695         for (i=0; i<NUM_D2X_CONTROLS; i++ )
3696                 kc_d2x[i].value = kconfig_d2x_settings[i];
3697 #endif
3698 }
3699
3700 #if 0 //ndef MACINTOSH  // no mac support for vr headset
3701
3702 void kconfig_center_headset()
3703 {
3704 #ifndef WINDOWS
3705         if (vfx1_installed)
3706                 vfx_center_headset();
3707 #endif
3708 //      } else if (iglasses_headset_installed)  {
3709 //      } else if (Victor_headset_installed)   {
3710 //      } else {
3711 //      }
3712
3713 }
3714
3715 #endif  // end of #ifndef MACINTOSH for kconfig_center_headset
3716
3717 void CybermouseAdjust ()
3718  {
3719 /*      if ( Player_num > -1 )  {
3720                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
3721                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
3722                 Auto_leveling_on = 0;
3723
3724                 if ( kc_external_version > 0 ) {                
3725                         vms_matrix tempm, ViewMatrix;
3726                         vms_angvec * Kconfig_abs_movement;
3727                         char * oem_message;
3728         
3729                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(control_info));
3730         
3731                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
3732                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
3733                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
3734                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
3735                         }
3736                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
3737                         if (oem_message[0] != '\0' )
3738                                 HUD_init_message( oem_message );
3739                 }
3740         }*/
3741
3742         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
3743         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
3744         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
3745         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
3746         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
3747         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
3748 //      Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
3749 //      Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
3750         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
3751         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
3752         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
3753         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
3754         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
3755         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
3756 //      Controls.automap_down_count += kc_external_control->automap_down_count;
3757 //      Controls.automap_state |= kc_external_control->automap_state;
3758   } 
3759
3760 char GetKeyValue (char key)
3761   {
3762         mprintf ((0,"Returning %c!\n",kc_keyboard[(int)key].value));
3763         return (kc_keyboard[(int)key].value);
3764   }
3765
3766 #if !defined(MACINTOSH)
3767
3768
3769 extern object *obj_find_first_of_type (int);
3770 void kconfig_read_external_controls()
3771 {
3772         //union REGS r;
3773    int i;
3774
3775         if ( !kc_enable_external_control ) return;
3776
3777         if ( kc_external_version == 0 ) 
3778                 memset( kc_external_control, 0, sizeof(ext_control_info));
3779         else if ( kc_external_version > 0 )     {
3780         
3781                 if (kc_external_version>=4)
3782                         memset( kc_external_control, 0, sizeof(advanced_ext_control_info));
3783       else if (kc_external_version>0)     
3784                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 );
3785                 else if (kc_external_version>2)
3786                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof(vms_matrix) +4 );
3787
3788                 if ( kc_external_version > 1 ) {
3789                         // Write ship pos and angles to external controls...
3790                         ubyte *temp_ptr = (ubyte *)kc_external_control;
3791                         vms_vector *ship_pos;
3792                         vms_matrix *ship_orient;
3793                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector)+sizeof(vms_matrix) );
3794                         temp_ptr += sizeof(ext_control_info) + sizeof(vms_angvec) + 64;
3795                         ship_pos = (vms_vector *)temp_ptr;
3796                         temp_ptr += sizeof(vms_vector);
3797                         ship_orient = (vms_matrix *)temp_ptr;
3798                         // Fill in ship postion...
3799                         *ship_pos = Objects[Players[Player_num].objnum].pos;
3800                         // Fill in ship orientation...
3801                         *ship_orient = Objects[Players[Player_num].objnum].orient;
3802                 }
3803     if (kc_external_version>=4)
3804           {
3805            advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
3806  
3807       temp_ptr->headlight_state=(Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON);
3808                 temp_ptr->primary_weapon_flags=Players[Player_num].primary_weapon_flags;
3809                 temp_ptr->secondary_weapon_flags=Players[Player_num].secondary_weapon_flags;
3810       temp_ptr->current_primary_weapon=Primary_weapon;
3811       temp_ptr->current_secondary_weapon=Secondary_weapon;
3812
3813       temp_ptr->current_guidebot_command=Escort_goal_object;
3814
3815            temp_ptr->force_vector=ExtForceVec;
3816                 temp_ptr->force_matrix=ExtApplyForceMatrix;
3817            for (i=0;i<3;i++)
3818        temp_ptr->joltinfo[i]=ExtJoltInfo[i];  
3819       for (i=0;i<2;i++)
3820                    temp_ptr->x_vibrate_info[i]=ExtXVibrateInfo[i];
3821                 temp_ptr->x_vibrate_clear=ExtXVibrateClear;
3822            temp_ptr->game_status=ExtGameStatus;
3823    
3824       memset ((void *)&ExtForceVec,0,sizeof(vms_vector));
3825       memset ((void *)&ExtApplyForceMatrix,0,sizeof(vms_matrix));
3826       
3827       for (i=0;i<3;i++)
3828                  ExtJoltInfo[i]=0;
3829       for (i=0;i<2;i++)
3830                  ExtXVibrateInfo[i]=0;
3831       ExtXVibrateClear=0;
3832      }
3833         }
3834
3835         if ( Automap_active )                   // (If in automap...)
3836                 kc_external_control->automap_state = 1;
3837         //memset(&r,0,sizeof(r));
3838
3839   #if 0
3840  
3841         int386 ( kc_external_intno, &r, &r);            // Read external info...
3842
3843   #endif 
3844
3845         if ( Player_num > -1 )  {
3846                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
3847                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
3848                 Auto_leveling_on = 0;
3849
3850                 if ( kc_external_version > 0 ) {                
3851                         vms_matrix tempm, ViewMatrix;
3852                         vms_angvec * Kconfig_abs_movement;
3853                         char * oem_message;
3854         
3855                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(ext_control_info));
3856         
3857                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
3858                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
3859                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
3860                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
3861                         }
3862                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
3863                         if (oem_message[0] != '\0' )
3864                                 HUD_init_message( oem_message );
3865                 }
3866         }
3867
3868         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
3869         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
3870         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
3871         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
3872         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
3873         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
3874         Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
3875         Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
3876         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
3877         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
3878         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
3879         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
3880         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
3881         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
3882         Controls.automap_down_count += kc_external_control->automap_down_count;
3883         Controls.automap_state |= kc_external_control->automap_state;
3884         
3885    if (kc_external_version>=3)
3886          {
3887                 ubyte *temp_ptr = (ubyte *)kc_external_control;
3888                 temp_ptr += (sizeof(ext_control_info) + sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof (vms_matrix));
3889   
3890            if (*(temp_ptr))
3891                  Controls.cycle_primary_count=(*(temp_ptr));
3892            if (*(temp_ptr+1))
3893                  Controls.cycle_secondary_count=(*(temp_ptr+1));
3894
3895                 if (*(temp_ptr+2))
3896                  Controls.afterburner_state=(*(temp_ptr+2));
3897                 if (*(temp_ptr+3))
3898                  Controls.headlight_count=(*(temp_ptr+3));
3899          }
3900    if (kc_external_version>=4)
3901          {
3902      int i;
3903           advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
3904      
3905      for (i=0;i<128;i++)
3906            if (temp_ptr->keyboard[i])
3907                         key_putkey (i);
3908
3909      if (temp_ptr->Reactor_blown)
3910       {
3911        if (Game_mode & GM_MULTI)
3912                     net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
3913                  else
3914                          do_controlcen_destroyed_stuff(obj_find_first_of_type (OBJ_CNTRLCEN));
3915            }
3916     }
3917   
3918 }
3919 #endif
3920