]> icculus.org git repositories - btb/d2x.git/blob - main/kconfig.c
add axis invert cvars
[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 * joyaxis_text[4] = { "X1", "Y1", "X2", "Y2" };
93 //char * mouseaxis_text[2] = { "L/R", "F/B" };
94
95 int invert_text[2] = { TNUM_N, TNUM_Y };
96
97 #ifndef USE_LINUX_JOY
98 #if defined(SDL_INPUT)
99 char *joyaxis_text[JOY_MAX_AXES];
100 #else
101         int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
102 //      int joyaxis_text[4] = { TNUM_X1, TNUM_Y1, TNUM_X2, TNUM_Y2 };
103 #endif
104 #endif
105
106 int mouseaxis_text[3] = { TNUM_L_R, TNUM_F_B, TNUM_Z1 };
107
108 #if !defined OGL && !defined SDL_INPUT
109 char * key_text[256] = {         \
110 "","ESC","1","2","3","4","5","6","7","8","9","0","-",                   \
111 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",                           \
112 "P","[","]","\x83","LCTRL","A","S","D","F",        \
113 "G","H","J","K","L",";","'","`",        \
114 "LSHFT","\\","Z","X","C","V","B","N","M",",",      \
115 ".","/","RSHFT","PAD*","LALT","SPC",      \
116 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",        \
117 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",   \
118 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0", \
119 "PAD.","","","","F11","F12","","","","","","","","","",         \
120 "","","","","","","","","","","","","","","","","","","","",     \
121 "","","","","","","","","","","","","","","","","","","","",     \
122 "","","","","","","","","","","","","","","","","","",           \
123 "PAD\x83","RCTRL","","","","","","","","","","","","","", \
124 "","","","","","","","","","","PAD/","","","RALT","",      \
125 "","","","","","","","","","","","","","HOME","\x82","PGUP",     \
126 "","\x81","","\x7f","","END","\x80","PGDN","INS",       \
127 "DEL","","","","","","","","","","","","","","","","","",     \
128 "","","","","","","","","","","","","","","","","","","","",     \
129 "","","","","","","" };
130 #endif /* OGL */
131
132 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 };
133
134 //extern void GameLoop(int, int );
135
136 extern void transfer_energy_to_shield(fix);
137 extern void CyclePrimary(),CycleSecondary(),InitMarkerInput();
138 extern ubyte DefiningMarkerMessage;
139 extern char CybermouseActive;
140
141 control_info Controls;
142
143 fix Cruise_speed=0;
144
145 // macros for drawing lo/hi res kconfig screens (see scores.c as well)
146
147 #define LHX(x)          ((x)*(MenuHires?2:1))
148 #define LHY(y)          ((y)*(MenuHires?2.4:1))
149
150
151 #define BT_KEY                          0
152 //#define BT_MOUSE_BUTTON       1
153 #define BT_MOUSE_AXIS           2
154 //#define BT_JOY_BUTTON                 3
155 #define BT_JOY_AXIS                     4
156 #define BT_INVERT                               5
157
158 char *btype_text[] = { "BT_KEY", "BT_MOUSE_BUTTON", "BT_MOUSE_AXIS", "BT_JOY_BUTTON", "BT_JOY_AXIS", "BT_INVERT" };
159
160 #define INFO_Y 28
161
162 typedef struct kc_item {
163         short id;                               // The id of this item
164         short x, y;                             
165         short w1;
166         short w2;
167         short u,d,l,r;
168         //short text_num1;
169         char *text;
170         ubyte type;
171         ubyte value;            // what key,button,etc
172 } kc_item;
173
174 int Num_items=28;
175 kc_item *All_items;
176
177 ubyte kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS];
178
179 //----------- WARNING!!!!!!! -------------------------------------------
180 // THESE NEXT FOUR BLOCKS OF DATA ARE GENERATED BY PRESSING DEL+F12 WHEN
181 // IN THE KEYBOARD CONFIG SCREEN.  BASICALLY, THAT PROCEDURE MODIFIES THE
182 // U,D,L,R FIELDS OF THE ARRAYS AND DUMPS THE NEW ARRAYS INTO KCONFIG.COD
183 //-------------------------------------------------------------------------
184 /*ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
185 {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},
186 {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},
187 {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},
188 {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},
189 {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},
190 {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},
191 {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},
192 };*/                                                                              
193
194 ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {
195 {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},
196 {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},
197 {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},
198 {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},
199 {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},
200 {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},
201 {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},
202 };
203
204
205
206 kc_item kc_keyboard[NUM_KEY_CONTROLS] = {
207         {  0, 15, 49, 71, 26, 55,  2, 55,  1,"Pitch forward", BT_KEY, 255 },
208         {  1, 15, 49,100, 26, 50,  3,  0, 24,"Pitch forward", BT_KEY, 255 },
209         {  2, 15, 57, 71, 26,  0,  4, 25,  3,"Pitch backward", BT_KEY, 255 },
210         {  3, 15, 57,100, 26,  1,  5,  2, 26,"Pitch backward", BT_KEY, 255 },
211         {  4, 15, 65, 71, 26,  2,  6, 27,  5,"Turn left", BT_KEY, 255 },
212         {  5, 15, 65,100, 26,  3,  7,  4, 28,"Turn left", BT_KEY, 255 },
213         {  6, 15, 73, 71, 26,  4,  8, 29,  7,"Turn right", BT_KEY, 255 },
214         {  7, 15, 73,100, 26,  5,  9,  6, 34,"Turn right", BT_KEY, 255 },
215         {  8, 15, 85, 71, 26,  6, 10, 35,  9,"Slide on", BT_KEY, 255 },
216         {  9, 15, 85,100, 26,  7, 11,  8, 36,"Slide on", BT_KEY, 255 },
217         { 10, 15, 93, 71, 26,  8, 12, 37, 11,"Slide left", BT_KEY, 255 },
218         { 11, 15, 93,100, 26,  9, 13, 10, 44,"Slide left", BT_KEY, 255 },
219         { 12, 15,101, 71, 26, 10, 14, 45, 13,"Slide right", BT_KEY, 255 },
220         { 13, 15,101,100, 26, 11, 15, 12, 30,"Slide right", BT_KEY, 255 },
221         { 14, 15,109, 71, 26, 12, 16, 31, 15,"Slide up", BT_KEY, 255 },
222         { 15, 15,109,100, 26, 13, 17, 14, 32,"Slide up", BT_KEY, 255 },
223         { 16, 15,117, 71, 26, 14, 18, 33, 17,"Slide down", BT_KEY, 255 },
224         { 17, 15,117,100, 26, 15, 19, 16, 46,"Slide down", BT_KEY, 255 },
225         { 18, 15,129, 71, 26, 16, 20, 47, 19,"Bank on", BT_KEY, 255 },
226         { 19, 15,129,100, 26, 17, 21, 18, 38,"Bank on", BT_KEY, 255 },
227         { 20, 15,137, 71, 26, 18, 22, 39, 21,"Bank left", BT_KEY, 255 },
228         { 21, 15,137,100, 26, 19, 23, 20, 40,"Bank left", BT_KEY, 255 },
229         { 22, 15,145, 71, 26, 20, 48, 41, 23,"Bank right", BT_KEY, 255 },
230         { 23, 15,145,100, 26, 21, 49, 22, 42,"Bank right", BT_KEY, 255 },
231         { 24,158, 49, 83, 26, 51, 26,  1, 25,"Fire primary", BT_KEY, 255 },
232         { 25,158, 49,112, 26, 54, 27, 24,  2,"Fire primary", BT_KEY, 255 },
233         { 26,158, 57, 83, 26, 24, 28,  3, 27,"Fire secondary", BT_KEY, 255 },
234         { 27,158, 57,112, 26, 25, 29, 26,  4,"Fire secondary", BT_KEY, 255 },
235         { 28,158, 65, 83, 26, 26, 34,  5, 29,"Fire flare", BT_KEY, 255 },
236         { 29,158, 65,112, 26, 27, 35, 28,  6,"Fire flare", BT_KEY, 255 },
237         { 30,158,105, 83, 26, 44, 32, 13, 31,"Accelerate", BT_KEY, 255 },
238         { 31,158,105,112, 26, 45, 33, 30, 14,"Accelerate", BT_KEY, 255 },
239         { 32,158,113, 83, 26, 30, 46, 15, 33,"reverse", BT_KEY, 255 },
240         { 33,158,113,112, 26, 31, 47, 32, 16,"reverse", BT_KEY, 255 },
241         { 34,158, 73, 83, 26, 28, 36,  7, 35,"Drop Bomb", BT_KEY, 255 },
242         { 35,158, 73,112, 26, 29, 37, 34,  8,"Drop Bomb", BT_KEY, 255 },
243         { 36,158, 85, 83, 26, 34, 44,  9, 37,"REAR VIEW", BT_KEY, 255 },
244         { 37,158, 85,112, 26, 35, 45, 36, 10,"REAR VIEW", BT_KEY, 255 },
245         { 38,158,133, 83, 26, 46, 40, 19, 39,"Cruise Faster", BT_KEY, 255 },
246         { 39,158,133,112, 26, 47, 41, 38, 20,"Cruise Faster", BT_KEY, 255 },
247         { 40,158,141, 83, 26, 38, 42, 21, 41,"Cruise Slower", BT_KEY, 255 },
248         { 41,158,141,112, 26, 39, 43, 40, 22,"Cruise Slower", BT_KEY, 255 },
249         { 42,158,149, 83, 26, 40, 52, 23, 43,"Cruise Off", BT_KEY, 255 },
250         { 43,158,149,112, 26, 41, 53, 42, 48,"Cruise Off", BT_KEY, 255 },
251         { 44,158, 93, 83, 26, 36, 30, 11, 45,"Automap", BT_KEY, 255 },
252         { 45,158, 93,112, 26, 37, 31, 44, 12,"Automap", BT_KEY, 255 },
253         { 46,158,121, 83, 26, 32, 38, 17, 47,"Afterburner", BT_KEY, 255 },
254         { 47,158,121,112, 26, 33, 39, 46, 18,"Afterburner", BT_KEY, 255 },
255         { 48, 15,161, 71, 26, 22, 50, 43, 49,"Cycle Primary", BT_KEY, 255 },
256         { 49, 15,161,100, 26, 23, 51, 48, 52,"Cycle Primary", BT_KEY, 255 },
257         { 50, 15,169, 71, 26, 48,  1, 53, 51,"Cycle Second", BT_KEY, 255 },
258         { 51, 15,169,100, 26, 49, 24, 50, 54,"Cycle Second", BT_KEY, 255 },
259         { 52,158,163, 83, 26, 42, 54, 49, 53,"Headlight", BT_KEY, 255 },
260         { 53,158,163,112, 26, 43, 55, 52, 50,"Headlight", BT_KEY, 255 },
261         { 54,158,171, 83, 26, 52, 56, 51, 55,"Energy->Shield", BT_KEY, 255 },
262         { 55,158,171,112, 26, 53,  0, 54,  0,"Energy->Shield", BT_KEY, 255 },
263    { 56,158,179,83,  26, 54,  0, 0,  0, "Toggle Bomb",  BT_KEY,255},
264 };
265 char *kc_key_bind_text[NUM_KEY_CONTROLS] = {
266         "+lookdown",
267         "+lookdown",
268         "+lookup",
269         "+lookup",
270         "+left",
271         "+left",
272         "+right",
273         "+right",
274         "+strafe",
275         "+strafe",
276         "+moveleft",
277         "+moveleft",
278         "+moveright",
279         "+moveright",
280         "+moveup",
281         "+moveup",
282         "+movedown",
283         "+movedown",
284         "+bank",
285         "+bank",
286         "+bankleft",
287         "+bankleft",
288         "+bankright",
289         "+bankright",
290         "+attack",
291         "+attack",
292         "+attack2",
293         "+attack2",
294         "+flare",
295         "+flare",
296         "+forward",
297         "+forward",
298         "+back",
299         "+back",
300         "+bomb",
301         "+bomb",
302         "+rearview",
303         "+rearview",
304         "+cruiseup",
305         "+cruiseup",
306         "+cruisedown",
307         "+cruisedown",
308         "+cruiseoff",
309         "+cruiseoff",
310         "+automap",
311         "+automap",
312         "+afterburner",
313         "+afterburner",
314         "+cycle",
315         "+cycle",
316         "+cycle2",
317         "+cycle2",
318         "+headlight",
319         "+headlight",
320         "+nrgshield",
321         "+nrgshield",
322         "+togglebomb",
323 };
324 kc_item kc_joystick[NUM_OTHER_CONTROLS] = {
325         { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
326         { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
327         { 13, 22, 99, 51, 40, 24, 15, 24, 14,"Pitch U/D", BT_JOY_AXIS, 255 },
328         { 14, 22, 99, 99,  8, 15, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
329         { 15, 22,107, 51, 40, 13, 14, 18, 16,"Turn L/R", BT_JOY_AXIS, 255 },
330         { 16, 22,107, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
331         { 17,164, 99, 58, 40, 16, 19, 14, 18,"Slide L/R", BT_JOY_AXIS, 255 },
332         { 18,164, 99,106,  8, 23, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
333         { 19,164,107, 58, 40, 17, 21, 16, 20,"Slide U/D", BT_JOY_AXIS, 255 },
334         { 20,164,107,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
335         { 21,164,117, 58, 40, 19, 23, 20, 22,"Bank L/R", BT_JOY_AXIS, 255 },
336         { 22,164,117,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
337         { 23,164,125, 58, 40, 21, 18, 22, 24,"throttle", BT_JOY_AXIS, 255 },
338         { 24,164,125,106,  8, 22, 13, 23, 13,"throttle", BT_INVERT, 255 },
339 };
340
341 kc_item kc_mouse[NUM_OTHER_CONTROLS] = {
342         { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
343         { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
344         { 13, 22,154, 51, 40, 24, 15, 24, 14,"Pitch U/D", BT_MOUSE_AXIS, 255 },
345         { 14, 22,154, 99,  8, 15, 16, 13, 17,"Pitch U/D", BT_INVERT, 255 },
346         { 15, 22,162, 51, 40, 13, 14, 18, 16,"Turn L/R", BT_MOUSE_AXIS, 255 },
347         { 16, 22,162, 99,  8, 14, 17, 15, 19,"Turn L/R", BT_INVERT, 255 },
348         { 17,164,154, 58, 40, 16, 19, 14, 18,"Slide L/R", BT_MOUSE_AXIS, 255 },
349         { 18,164,154,106,  8, 23, 20, 17, 15,"Slide L/R", BT_INVERT, 255 },
350         { 19,164,162, 58, 40, 17, 21, 16, 20,"Slide U/D", BT_MOUSE_AXIS, 255 },
351         { 20,164,162,106,  8, 18, 22, 19, 21,"Slide U/D", BT_INVERT, 255 },
352         { 21,164,172, 58, 40, 19, 23, 20, 22,"Bank L/R", BT_MOUSE_AXIS, 255 },
353         { 22,164,172,106,  8, 20, 24, 21, 23,"Bank L/R", BT_INVERT, 255 },
354         { 23,164,180, 58, 40, 21, 18, 22, 24,"throttle", BT_MOUSE_AXIS, 255 },
355         { 24,164,180,106,  8, 22, 13, 23, 13,"throttle", BT_INVERT, 255 },
356 };
357
358 kc_axis_map kc_other_axismap[NUM_OTHER_CONTROLS] = {
359         AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE,
360         AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE, AXIS_NONE,
361         AXIS_PITCH,
362         AXIS_NONE,
363         AXIS_TURN,
364         AXIS_NONE,
365         AXIS_LEFTRIGHT,
366         AXIS_NONE,
367         AXIS_UPDOWN,
368         AXIS_NONE,
369         AXIS_BANK,
370         AXIS_NONE,
371         AXIS_THROTTLE,
372         AXIS_NONE,
373 };
374
375 kc_item kc_d2x[NUM_D2X_CONTROLS] = {
376 //        id,x,y,w1,w2,u,d,l,r,text_num1,type,value
377         {  0, 15, 49, 71, 26, 19,  2, 27,  1, "WEAPON 1", BT_KEY, 255},
378         {  1, 15, 49,100, 26, 18,  3,  0,  2, "WEAPON 1", BT_KEY, 255},
379         {  2, 15, 57, 71, 26,  0,  4,  1,  3, "WEAPON 2", BT_KEY, 255},
380         {  3, 15, 57,100, 26,  1,  5,  2,  4, "WEAPON 2", BT_KEY, 255},
381         {  4, 15, 65, 71, 26,  2,  6,  3,  5, "WEAPON 3", BT_KEY, 255},
382         {  5, 15, 65,100, 26,  3,  7,  4,  6, "WEAPON 3", BT_KEY, 255},
383         {  6, 15, 73, 71, 26,  4,  8,  5,  7, "WEAPON 4", BT_KEY, 255},
384         {  7, 15, 73,100, 26,  5,  9,  6,  8, "WEAPON 4", BT_KEY, 255},
385         {  8, 15, 81, 71, 26,  6, 10,  7,  9, "WEAPON 5", BT_KEY, 255},
386         {  9, 15, 81,100, 26,  7, 11,  8, 10, "WEAPON 5", BT_KEY, 255},
387
388         { 10, 15, 89, 71, 26,  8, 12,  9, 11, "WEAPON 6", BT_KEY, 255},
389         { 11, 15, 89,100, 26,  9, 13, 10, 12, "WEAPON 6", BT_KEY, 255},
390         { 12, 15, 97, 71, 26, 10, 14, 11, 13, "WEAPON 7", BT_KEY, 255},
391         { 13, 15, 97,100, 26, 11, 15, 12, 14, "WEAPON 7", BT_KEY, 255},
392         { 14, 15,105, 71, 26, 12, 16, 13, 15, "WEAPON 8", BT_KEY, 255},
393         { 15, 15,105,100, 26, 13, 17, 14, 16, "WEAPON 8", BT_KEY, 255},
394         { 16, 15,113, 71, 26, 14, 18, 15, 17, "WEAPON 9", BT_KEY, 255},
395         { 17, 15,113,100, 26, 15, 19, 16, 18, "WEAPON 9", BT_KEY, 255},
396         { 18, 15,121, 71, 26, 16,  1, 17, 19, "WEAPON 0", BT_KEY, 255},
397         { 19, 15,121,100, 26, 17,  0, 18,  0, "WEAPON 0", BT_KEY, 255},
398
399         //{ 20, 15,131, 71, 26, 18, 22, 19, 21, "CYC PRIMARY", BT_KEY, 255},
400         //{ 21, 15,131,100, 26, 19, 23, 20, 22, "CYC PRIMARY", BT_KEY, 255},
401         //{ 22, 15,139, 71, 26, 20, 24, 21, 23, "CYC SECONDARY", BT_KEY, 255},
402         //{ 23, 15,139,100, 26, 21, 25, 22, 24, "CYC SECONDARY", BT_KEY, 255},
403         //{ 24,  8,147, 78, 26, 22, 26, 23, 25, "TOGGLE_PRIM AUTO", BT_KEY, 255},
404         //{ 25,  8,147,107, 26, 23, 27, 24, 26, "TOGGLE_PRIM_AUTO", BT_KEY, 255},
405         //{ 26,  8,155, 78, 26, 24,  1, 25, 27, "TOGGLE SEC AUTO", BT_KEY, 255},
406         //{ 27,  8,155,107, 26, 25,  0, 26,  0, "TOGGLE SEC AUTO", BT_KEY, 255},
407 };
408
409 //added on 2/4/99 by Victor Rachels to add new keys system
410 ubyte default_kconfig_d2x_settings[MAX_D2X_CONTROLS] = {
411  0x2 ,0xff,0x3 ,0xff,0x4 ,0xff,0x5 ,0xff,0x6 ,0xff,0x7 ,0xff,0x8 ,0xff,0x9 ,
412  0xff,0xa ,0xff,0xb ,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
413  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
414 //end this section addition - VR
415
416 void kc_drawitem( kc_item *item, int is_current );
417 void kc_change_key( kc_item * item );
418 void kc_next_joyaxis(kc_item *item);  //added by WraithX on 11/22/00
419 void kc_change_joyaxis( kc_item * item );
420 void kc_change_mouseaxis( kc_item * item );
421 void kc_change_invert( kc_item * item );
422 void kconfig_read_fcs( int raw_axis );
423 void kconfig_set_fcs_button( int btn, int button );
424 void kconfig_read_external_controls( void );
425
426 // the following methods added by WraithX, 4/17/00
427 int isJoyRotationKey(int test_key)
428 {
429         if (test_key == kc_joystick[11].value ||
430             test_key == kc_joystick[12].value)
431         {
432                 return 1;
433         }// end if
434
435         // else...
436         return 0;
437 }// method isJoyRotationKey
438
439 int isMouseRotationKey(int test_key)
440 {
441         if (test_key == kc_mouse[11].value ||
442             test_key == kc_mouse[12].value)
443         {
444                 return 1;
445         }// end if
446
447         // else...
448         return 0;
449 }// method isMouseRotationKey
450
451 int isKeyboardRotationKey(int test_key)
452 {
453         if (test_key == kc_keyboard[0].value ||
454             test_key == kc_keyboard[1].value ||
455             test_key == kc_keyboard[2].value ||
456             test_key == kc_keyboard[3].value ||
457             test_key == kc_keyboard[4].value ||
458             test_key == kc_keyboard[5].value ||
459             test_key == kc_keyboard[6].value ||
460             test_key == kc_keyboard[7].value ||
461             test_key == kc_keyboard[20].value ||
462             test_key == kc_keyboard[21].value ||
463             test_key == kc_keyboard[22].value ||
464             test_key == kc_keyboard[23].value)
465         {
466                 return 1;
467         }// end if
468
469         // else...
470         return 0;
471 }// method isKeyboardRotationKey
472 // end addition - WraithX
473
474 int kconfig_is_axes_used(int axis)
475 {
476         int i;
477         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
478                 if (( kc_joystick[i].type == BT_JOY_AXIS ) && (kc_joystick[i].value == axis ))
479                         return 1;
480         }
481         return 0;
482 }
483
484 #ifdef TABLE_CREATION
485 int find_item_at( kc_item * items, int nitems, int x, int y )
486 {
487         int i;
488         
489         for (i=0; i<nitems; i++ )       {
490                 if ( ((items[i].x+items[i].w1)==x) && (items[i].y==y))
491                         return i;
492         }
493         return -1;
494 }
495
496 int find_next_item_up( kc_item * items, int nitems, int citem )
497 {
498         int x, y, i;
499
500         y = items[citem].y;
501         x = items[citem].x+items[citem].w1;
502         
503         do {    
504                 y--;
505                 if ( y < 0 ) {
506                         y = grd_curcanv->cv_bitmap.bm_h-1;
507                         x--;
508                         if ( x < 0 ) {
509                                 x = grd_curcanv->cv_bitmap.bm_w-1;
510                         }
511                 }
512                 i = find_item_at( items, nitems, x, y );
513         } while ( i < 0 );
514         
515         return i;
516 }
517
518 int find_next_item_down( kc_item * items, int nitems, int citem )
519 {
520         int x, y, i;
521
522         y = items[citem].y;
523         x = items[citem].x+items[citem].w1;
524         
525         do {    
526                 y++;
527                 if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
528                         y = 0;
529                         x++;
530                         if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
531                                 x = 0;
532                         }
533                 }
534                 i = find_item_at( items, nitems, x, y );
535         } while ( i < 0 );
536         
537         return i;
538 }
539
540 int find_next_item_right( kc_item * items, int nitems, int citem )
541 {
542         int x, y, i;
543
544         y = items[citem].y;
545         x = items[citem].x+items[citem].w1;
546         
547         do {    
548                 x++;
549                 if ( x > grd_curcanv->cv_bitmap.bm_w-1 ) {
550                         x = 0;
551                         y++;
552                         if ( y > grd_curcanv->cv_bitmap.bm_h-1 ) {
553                                 y = 0;
554                         }
555                 }
556                 i = find_item_at( items, nitems, x, y );
557         } while ( i < 0 );
558         
559         return i;
560 }
561
562 int find_next_item_left( kc_item * items, int nitems, int citem )
563 {
564         int x, y, i;
565
566         y = items[citem].y;
567         x = items[citem].x+items[citem].w1;
568         
569         do {    
570                 x--;
571                 if ( x < 0 ) {
572                         x = grd_curcanv->cv_bitmap.bm_w-1;
573                         y--;
574                         if ( y < 0 ) {
575                                 y = grd_curcanv->cv_bitmap.bm_h-1;
576                         }
577                 }
578                 i = find_item_at( items, nitems, x, y );
579         } while ( i < 0 );
580         
581         return i;
582 }
583 #endif
584
585 #ifdef NEWMENU_MOUSE
586 int get_item_height(kc_item *item)
587 {
588         int w, h, aw;
589         char btext[10];
590
591         if (item->value==255) {
592                 strcpy(btext, "");
593         } else {
594                 switch( item->type )    {
595                         case BT_KEY:
596                                 strncpy( btext, key_text[item->value], 10 ); break;
597                         case BT_MOUSE_AXIS:
598                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
599                         case BT_JOY_AXIS:
600 #ifdef USE_LINUX_JOY
601                                 sprintf( btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number (item->value) );
602 #elif defined(SDL_INPUT)
603                                 if (joyaxis_text[item->value])
604                                         strncpy(btext, joyaxis_text[item->value], 10);
605                                 else
606                                         sprintf(btext, "AXIS%2d", item->value + 1);
607 #else
608                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
609 #endif
610                                 break;
611                         case BT_INVERT:
612                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
613                 }
614         }
615         gr_get_string_size(btext, &w, &h, &aw  );
616
617         return h;
618 }
619 #endif
620
621 void kconfig_sub(kc_item * items,int nitems, char * title)
622 {
623         grs_canvas * save_canvas;
624         grs_font * save_font;
625         int old_keyd_repeat;
626 #ifdef NEWMENU_MOUSE
627         int mouse_state, omouse_state, mx, my, x1, x2, y1, y2;
628         int close_x, close_y, close_size;
629 #endif
630
631         int i,k,ocitem,citem;
632         int time_stopped = 0;
633
634         All_items = items;
635         Num_items = nitems;
636
637         if (!((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence)) )
638         {
639                 time_stopped = 1;
640                 stop_time();
641         }
642
643         save_canvas = grd_curcanv;
644
645
646         gr_set_current_canvas(NULL);
647         save_font = grd_curcanv->cv_font;
648
649         game_flush_inputs();
650         old_keyd_repeat = keyd_repeat;
651         keyd_repeat = 1;
652
653         //gr_clear_canvas( BM_XRGB(0,0,0) );
654
655         nm_draw_background(0, 0, grd_curcanv->cv_bitmap.bm_w - 1, grd_curcanv->cv_bitmap.bm_h - 1);
656    gr_palette_load (gr_palette);
657
658         grd_curcanv->cv_font = MEDIUM3_FONT;
659
660         {
661                 char * p;
662                 p = strchr( title, '\n' );
663                 if ( p ) *p = 32;
664                 gr_string( 0x8000, LHY(8), title );
665                 if ( p ) *p = '\n';
666         }
667
668
669 //      if ( items == kc_keyboard )     {
670 //              gr_string( 0x8000, 8, "Keyboard" );
671 //      } else if ( items == kc_joystick )      {
672 //              gr_string( 0x8000, 8, "Joysticks" );
673 //      } else if ( items == kc_mouse ) {
674 //              gr_string( 0x8000, 8, "Mouse" );
675 //      }
676
677 #ifdef NEWMENU_MOUSE
678         close_x = close_y = MenuHires?15:7;
679         close_size = MenuHires?10:5;
680         /*
681         gr_setcolor( BM_XRGB(0, 0, 0) );
682         gr_rect(close_x, close_y, close_x + close_size, close_y + close_size);
683         gr_setcolor( BM_XRGB(21, 21, 21) );
684         gr_rect(close_x + LHX(1), close_y + LHX(1), close_x + close_size - LHX(1), close_y + close_size - LHX(1));
685         */
686 #endif
687
688         grd_curcanv->cv_font = GAME_FONT;
689         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
690
691         gr_string( 0x8000, LHY(20), TXT_KCONFIG_STRING_1 );
692         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
693         if ( items == kc_keyboard )     {
694                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
695                 gr_setcolor( BM_XRGB(31,27,6) );
696                 
697                 gr_scanline( LHX(98), LHX(106), LHY(42) );
698                 gr_scanline( LHX(120), LHX(128), LHY(42) );
699                 gr_pixel( LHX(98), LHY(43) );                                           
700                 gr_pixel( LHX(98), LHY(44) );                                           
701                 gr_pixel( LHX(128), LHY(43) );                                          
702                 gr_pixel( LHX(128), LHY(44) );                                          
703                 
704                 gr_string( LHX(109), LHY(40), "OR" );
705
706                 gr_scanline( LHX(253), LHX(261), LHY(42) );
707                 gr_scanline( LHX(274), LHX(283), LHY(42) );
708                 gr_pixel( LHX(253), LHY(43) );                                          
709                 gr_pixel( LHX(253), LHY(44) );                                          
710                 gr_pixel( LHX(283), LHY(43) );                                          
711                 gr_pixel( LHX(283), LHY(44) );                                          
712
713                 gr_string( LHX(264), LHY(40), "OR" );
714
715         } if ( items == kc_joystick )   {
716                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
717                 gr_setcolor( BM_XRGB(31,27,6) );
718                 gr_scanline( LHX(18), LHX(138), LHY(64+18) );
719                 gr_scanline( LHX(181), LHX(294), LHY(64+18) );
720                 gr_string( 0x8000, LHY(62+18), TXT_CONTROL_JOYSTICK );
721                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
722                 gr_string( LHX(81), LHY(82+8), TXT_AXIS );
723                 gr_string( LHX(111), LHY(82+8), TXT_INVERT );
724                 gr_string( LHX(230), LHY(82+8), TXT_AXIS );
725                 gr_string( LHX(260), LHY(82+8), TXT_INVERT );
726
727         } else if ( items == kc_mouse ) {
728                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
729                 gr_setcolor( BM_XRGB(31,27,6) );
730                 gr_scanline( LHX(18), LHX(143), LHY(119+18) );
731                 gr_scanline( LHX(174), LHX(294), LHY(119+18) );
732                 gr_string( 0x8000, LHY(117+18), TXT_CONTROL_MOUSE );
733                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
734                 gr_string( LHX(81), LHY(137+8), TXT_AXIS );
735                 gr_string( LHX(111), LHY(137+8), TXT_INVERT );
736                 gr_string( LHX(230), LHY(137+8), TXT_AXIS );
737                 gr_string( LHX(260), LHY(137+8), TXT_INVERT );
738         }
739         else if ( items == kc_d2x )
740         {
741                 gr_set_fontcolor( BM_XRGB(31,27,6), -1 );
742                 gr_setcolor( BM_XRGB(31,27,6) );
743
744                 gr_scanline( LHX(98), LHX(106), LHY(42) );
745                 gr_scanline( LHX(120), LHX(128), LHY(42) );
746                 gr_pixel( LHX(98), LHY(43) );
747                 gr_pixel( LHX(98), LHY(44) );
748                 gr_pixel( LHX(128), LHY(43) );
749                 gr_pixel( LHX(128), LHY(44) );
750
751                 gr_string(LHX(109), LHY(40), "OR");
752         }
753
754         for (i=0; i<nitems; i++ )       {
755                 kc_drawitem( &items[i], 0 );
756         }
757
758         citem = 0;
759         while(items[citem].id == -1)
760                 citem++;
761         kc_drawitem( &items[citem], 1 );
762
763         newmenu_show_cursor();
764
765 #ifdef NEWMENU_MOUSE
766         mouse_state = omouse_state = 0;
767 #endif
768
769         while(1)                {
770         //      Windows addendum to allow for kconfig input.
771                 gr_update();
772
773                 //see if redbook song needs to be restarted
774                 songs_check_redbook_repeat();
775
776                 k = key_inkey();
777
778 #ifdef NEWMENU_MOUSE
779                 omouse_state = mouse_state;
780                 mouse_state = mouse_button_state(0);
781 #endif
782
783                 if ( !time_stopped ) {
784                         #ifdef NETWORK
785                         if (multi_menu_poll() == -1)
786                                 k = -2;
787                         #endif
788                 }
789                 ocitem = citem;
790                 switch( k )     {
791                 case KEY_BACKSP:
792                         Int3();
793                         break;
794                 case KEY_COMMAND+KEY_SHIFTED+KEY_3:
795                 case KEY_PRINT_SCREEN:
796                         save_screen_shot(0);
797                         break;                                                  
798                 case KEY_CTRLED+KEY_D:
799                         items[citem].value = 255;
800                         kc_drawitem( &items[citem], 1 );
801                         break;
802                 case KEY_CTRLED+KEY_R:  
803                         if ( items==kc_keyboard )       {
804                                 for (i=0; i<NUM_KEY_CONTROLS; i++ )             {
805                                         items[i].value=default_kconfig_settings[0][i];
806                                         kc_drawitem( &items[i], 0 );
807                                 }
808                         } else if ( items==kc_d2x ) {
809                                 for(i=0;i<NUM_D2X_CONTROLS;i++)
810                                 {
811                                         items[i].value=default_kconfig_d2x_settings[i];
812                                         kc_drawitem( &items[i], 0 );
813                                 }
814                         } else {
815                                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
816                                                 items[i].value = default_kconfig_settings[Config_control_type][i];
817                                                 kc_drawitem( &items[i], 0 );
818                                         }
819                         }
820                         kc_drawitem( &items[citem], 1 );
821                         break;
822                 case KEY_DELETE:
823                         items[citem].value=255;
824                         kc_drawitem( &items[citem], 1 );
825                         break;
826                 case KEY_UP:            
827                 case KEY_PAD8:
828 #ifdef TABLE_CREATION
829                         if (items[citem].u==-1) items[citem].u=find_next_item_up( items,nitems, citem);
830 #endif
831                         citem = items[citem].u; 
832                         break;
833                 
834                 case KEY_DOWN:  
835                 case KEY_PAD2:
836 #ifdef TABLE_CREATION
837                         if (items[citem].d==-1) items[citem].d=find_next_item_down( items,nitems, citem);
838 #endif
839                         citem = items[citem].d; 
840                         break;
841                 case KEY_LEFT:  
842                 case KEY_PAD4:
843 #ifdef TABLE_CREATION
844                         if (items[citem].l==-1) items[citem].l=find_next_item_left( items,nitems, citem);
845 #endif
846                         citem = items[citem].l; 
847                         break;
848                 case KEY_RIGHT:         
849                 case KEY_PAD6:
850 #ifdef TABLE_CREATION
851                         if (items[citem].r==-1) items[citem].r=find_next_item_right( items,nitems, citem);
852 #endif
853                         citem = items[citem].r; 
854                         break;
855                 case KEY_ENTER: 
856                 case KEY_PADENTER:      
857                         switch( items[citem].type )     {
858                         case BT_KEY:            kc_change_key( &items[citem] ); break;
859                         case BT_MOUSE_AXIS:     kc_change_mouseaxis( &items[citem] ); break;
860                         case BT_JOY_AXIS:       kc_change_joyaxis( &items[citem] ); break;
861                         case BT_INVERT:         kc_change_invert( &items[citem] ); break;
862                         }
863                         break;
864                 //the following case added by WraithX on 11/22/00 to work around the weird joystick bug...
865                 case KEY_SPACEBAR:
866                         switch(items[citem].type)
867                         {
868                         case BT_JOY_AXIS:
869                                 kc_next_joyaxis(&items[citem]);
870                                 break;
871                         }
872                         break;
873                 //end addition by WraithX
874                 case -2:        
875                 case KEY_ESC:
876                         grd_curcanv->cv_font    = save_font;
877
878                         gr_set_current_canvas( save_canvas );
879                         keyd_repeat = old_keyd_repeat;
880                         game_flush_inputs();
881                         newmenu_hide_cursor();
882                         if (time_stopped)
883                                 start_time();
884                         return;
885 #ifdef TABLE_CREATION
886                 case KEY_DEBUGGED+KEY_F12:      {
887                         FILE * fp;
888                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
889                                 kc_keyboard[i].u = find_next_item_up( kc_keyboard,NUM_KEY_CONTROLS, i);
890                                 kc_keyboard[i].d = find_next_item_down( kc_keyboard,NUM_KEY_CONTROLS, i);
891                                 kc_keyboard[i].l = find_next_item_left( kc_keyboard,NUM_KEY_CONTROLS, i);
892                                 kc_keyboard[i].r = find_next_item_right( kc_keyboard,NUM_KEY_CONTROLS, i);
893                         }
894                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
895                                 kc_joystick[i].u = find_next_item_up( kc_joystick,NUM_OTHER_CONTROLS, i);
896                                 kc_joystick[i].d = find_next_item_down( kc_joystick,NUM_OTHER_CONTROLS, i);
897                                 kc_joystick[i].l = find_next_item_left( kc_joystick,NUM_OTHER_CONTROLS, i);
898                                 kc_joystick[i].r = find_next_item_right( kc_joystick,NUM_OTHER_CONTROLS, i);
899                         }
900                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
901                                 kc_mouse[i].u = find_next_item_up( kc_mouse,NUM_OTHER_CONTROLS, i);
902                                 kc_mouse[i].d = find_next_item_down( kc_mouse,NUM_OTHER_CONTROLS, i);
903                                 kc_mouse[i].l = find_next_item_left( kc_mouse,NUM_OTHER_CONTROLS, i);
904                                 kc_mouse[i].r = find_next_item_right( kc_mouse,NUM_OTHER_CONTROLS, i);
905                         }
906                         fp = fopen( "kconfig.cod", "wt" );
907
908                         fprintf( fp, "ubyte default_kconfig_settings[CONTROL_MAX_TYPES][MAX_CONTROLS] = {\n" );
909                         for (i=0; i<CONTROL_MAX_TYPES; i++ )    {
910                                 int j;
911                                 fprintf( fp, "{0x%x", kconfig_settings[i][0] );
912                                 for (j=1; j<MAX_CONTROLS; j++ )
913                                         fprintf( fp, ",0x%x", kconfig_settings[i][j] );
914                                 fprintf( fp, "},\n" );
915                         }
916                         fprintf( fp, "};\n" );
917                 
918                         fprintf( fp, "\nkc_item kc_keyboard[NUM_KEY_CONTROLS] = {\n" );
919                         for (i=0; i<NUM_KEY_CONTROLS; i++ )     {
920                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
921                                         kc_keyboard[i].id, kc_keyboard[i].x, kc_keyboard[i].y, kc_keyboard[i].w1, kc_keyboard[i].w2,
922                                         kc_keyboard[i].u, kc_keyboard[i].d, kc_keyboard[i].l, kc_keyboard[i].r,
923                                         34, kc_keyboard[i].text, 34, btype_text[kc_keyboard[i].type] );
924                         }
925                         fprintf( fp, "};" );
926
927                         fprintf( fp, "\nkc_item kc_joystick[NUM_OTHER_CONTROLS] = {\n" );
928                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
929                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n",
930                                         kc_joystick[i].id, kc_joystick[i].x, kc_joystick[i].y, kc_joystick[i].w1, kc_joystick[i].w2,
931                                         kc_joystick[i].u, kc_joystick[i].d, kc_joystick[i].l, kc_joystick[i].r,
932                                         34, kc_joystick[i].text, 34, btype_text[kc_joystick[i].type] );
933                         }
934                         fprintf( fp, "};" );
935
936                         fprintf( fp, "\nkc_item kc_mouse[NUM_OTHER_CONTROLS] = {\n" );
937                         for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
938                                 fprintf( fp, "\t{ %2d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%c%s%c, %s, 255 },\n", 
939                                         kc_mouse[i].id, kc_mouse[i].x, kc_mouse[i].y, kc_mouse[i].w1, kc_mouse[i].w2,
940                                         kc_mouse[i].u, kc_mouse[i].d, kc_mouse[i].l, kc_mouse[i].r,
941                                         34, kc_mouse[i].text, 34, btype_text[kc_mouse[i].type] );
942                         }
943                         fprintf( fp, "};" );
944
945                         fclose(fp);
946
947                         }
948                         break;
949 #endif
950                 }
951
952 #ifdef NEWMENU_MOUSE
953                 if ( (mouse_state && !omouse_state) || (mouse_state && omouse_state) ) {
954                         int item_height;
955                         
956                         mouse_get_pos(&mx, &my);
957                         for (i=0; i<nitems; i++ )       {
958                                 item_height = get_item_height( &items[i] );
959                                 x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[i].x) + LHX(items[i].w1);
960                                 x2 = x1 + LHX(items[i].w2);
961                                 y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[i].y);
962                                 y2 = y1 + LHX(item_height);
963                                 if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
964                                         citem = i;
965                                         break;
966                                 }
967                         }
968                 }
969                 else if ( !mouse_state && omouse_state ) {
970                         int item_height;
971                         
972                         mouse_get_pos(&mx, &my);
973                         item_height = get_item_height( &items[citem] );
974                         x1 = grd_curcanv->cv_bitmap.bm_x + LHX(items[citem].x) + LHX(items[citem].w1);
975                         x2 = x1 + LHX(items[citem].w2);
976                         y1 = grd_curcanv->cv_bitmap.bm_y + LHY(items[citem].y);
977                         y2 = y1 + LHY(item_height);
978                         if (((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2))) {
979                                 newmenu_hide_cursor();
980                                 switch( items[citem].type )     {
981                                 case BT_KEY:                            kc_change_key( &items[citem] ); break;
982                                 case BT_MOUSE_AXIS:             kc_change_mouseaxis( &items[citem] ); break;
983                                 case BT_JOY_AXIS:               kc_change_joyaxis( &items[citem] ); break;
984                                 case BT_INVERT:                         kc_change_invert( &items[citem] ); break;
985                                 }
986                                 newmenu_show_cursor();
987                         } else {
988                                 x1 = grd_curcanv->cv_bitmap.bm_x + close_x + LHX(1);
989                                 x2 = x1 + close_size - LHX(1);
990                                 y1 = grd_curcanv->cv_bitmap.bm_y + close_y + LHX(1);
991                                 y2 = y1 + close_size - LHX(1);
992                                 if ( ((mx > x1) && (mx < x2)) && ((my > y1) && (my < y2)) ) {
993                                         grd_curcanv->cv_font    = save_font;
994                                         gr_set_current_canvas( save_canvas );
995                                         keyd_repeat = old_keyd_repeat;
996                                         game_flush_inputs();
997                                         newmenu_hide_cursor();
998                                         if (time_stopped)
999                                                 start_time();
1000                                         return;
1001                                 }
1002                         }
1003
1004                 }
1005 #endif // NEWMENU_MOUSE
1006
1007                 if (ocitem!=citem)      {
1008                         newmenu_hide_cursor();
1009                         kc_drawitem( &items[ocitem], 0 );
1010                         kc_drawitem( &items[citem], 1 );
1011                         newmenu_show_cursor();
1012                 }
1013         }
1014 }
1015
1016
1017 void kc_drawitem( kc_item *item, int is_current )
1018 {
1019         int x, w, h, aw;
1020         char btext[16];
1021
1022         if (is_current)
1023                 gr_set_fontcolor( BM_XRGB(20,20,29), -1 );
1024         else
1025                 gr_set_fontcolor( BM_XRGB(15,15,24), -1 );
1026    gr_string( LHX(item->x), LHY(item->y), item->text );
1027
1028         if (item->value==255) {
1029                 strcpy( btext, "" );
1030         } else {
1031                 switch( item->type )    {
1032                         case BT_KEY:
1033                                 strncpy( btext, key_text[item->value], 10 ); break;
1034                         case BT_MOUSE_AXIS:
1035                                 strncpy( btext, Text_string[mouseaxis_text[item->value]], 10 ); break;
1036                         case BT_JOY_AXIS:
1037 #ifdef USE_LINUX_JOY
1038                                 sprintf(btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number(item->value));
1039 #elif defined(SDL_INPUT)
1040                                 if (joyaxis_text[item->value])
1041                                         strncpy(btext, joyaxis_text[item->value], 10);
1042                                 else
1043                                         sprintf(btext, "AXIS%2d", item->value + 1);
1044 #else
1045                                 strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
1046 #endif
1047                                 break;
1048                         case BT_INVERT:
1049                                 strncpy( btext, Text_string[invert_text[item->value]], 10 ); break;
1050                 }
1051         }
1052         if (item->w1) {
1053                 gr_get_string_size(btext, &w, &h, &aw  );
1054
1055                 if (is_current)
1056                         gr_setcolor( BM_XRGB(21,0,24) );
1057                 else
1058                         gr_setcolor( BM_XRGB(16,0,19) );
1059                 gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1060                 
1061                 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1062
1063                 x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1064         
1065                 gr_string( x, LHY(item->y), btext );
1066         }
1067 }
1068
1069
1070 static int looper=0;
1071
1072 void kc_drawquestion( kc_item *item )
1073 {
1074         int c, x, w, h, aw;
1075
1076         gr_get_string_size("?", &w, &h, &aw  );
1077
1078         c = BM_XRGB(21,0,24);
1079
1080         //@@gr_setcolor( gr_fade_table[fades[looper]*256+c] );
1081         gr_setcolor(BM_XRGB(21*fades[looper]/31,0,24*fades[looper]/31));
1082         looper++;
1083         if (looper>63) looper=0;
1084
1085         gr_urect( LHX(item->w1+item->x), LHY(item->y-1), LHX(item->w1+item->x+item->w2), LHY(item->y)+h );
1086         
1087         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1088
1089         x = LHX(item->w1+item->x)+((LHX(item->w2)-w)/2);
1090    
1091         gr_string( x, LHY(item->y), "?" );
1092 gr_update();
1093 }
1094
1095 void kc_change_key( kc_item * item )
1096 {
1097         int i,n,f,k;
1098         ubyte keycode;
1099
1100         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1101         
1102         gr_string( 0x8000, LHY(INFO_Y), TXT_PRESS_NEW_KEY );
1103
1104         game_flush_inputs();
1105         keycode=255;
1106         k=255;
1107         
1108         while( (k!=KEY_ESC) && (keycode==255) ) 
1109         {                               
1110                 #ifdef NETWORK
1111                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1112                         multi_menu_poll();
1113                 #endif
1114 //              if ( Game_mode & GM_MULTI )
1115 //                      GameLoop( 0, 0 );                               // Continue
1116                 k = key_inkey();
1117                 timer_delay(f0_1/10);
1118                 kc_drawquestion( item );
1119         
1120                 for (i=0; i<256; i++ )  {
1121                         if (keyd_pressed[i] && (strlen(key_text[i])>0)) {
1122                                 f = 0;
1123                                 for (n=0; n<sizeof(system_keys); n++ )
1124                                         if ( system_keys[n] == i )
1125                                                 f=1;
1126                                 if (!f) 
1127                                         keycode=i;
1128                         }
1129                 }
1130         }
1131
1132         if (k!=KEY_ESC) {
1133                 for (i=0; i<Num_items; i++ )    {
1134                         n = item - All_items;
1135                         if ( (i!=n) && (All_items[i].type==BT_KEY) && (All_items[i].value==keycode) )           {
1136                                 All_items[i].value = 255;
1137                                 kc_drawitem( &All_items[i], 0 );
1138                         }
1139                 }
1140                 item->value = keycode;
1141         }
1142         kc_drawitem( item, 1 );
1143
1144         gr_set_fontcolor( BM_XRGB(28,28,28), BM_XRGB(0,0,0) );
1145
1146         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1147
1148         game_flush_inputs();
1149
1150 }
1151
1152
1153 // 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
1154 void kc_next_joyaxis(kc_item *item)
1155 {
1156         int n, i, k, max, tries;
1157         ubyte code = 0;
1158
1159         k = 255;
1160         n = 0;
1161         i = 0;
1162
1163         // I modelled this ifdef after the code in the kc_change_joyaxis method.
1164         // So, if somethin's not workin here, it might not be workin there either.
1165         max = JOY_MAX_AXES;
1166         tries = 1;
1167         code = (item->value + 1) % max;
1168
1169         if (code != 255)
1170         {
1171                 for (i = 0; i < Num_items; i++)
1172                 {
1173                         n = item - All_items;
1174                         if ((i != n) && (All_items[i].type == BT_JOY_AXIS) && (All_items[i].value == code))
1175                         {
1176                                 if (tries > max)
1177                                         return; // all axes allocated already
1178                                 i = -1; // -1 so the i++ will push back to 0
1179                                 code = (item->value + ++tries) % max; // try next axis
1180                         }//end if
1181                 }//end for
1182
1183                 item->value = code;
1184         }//end if
1185
1186         kc_drawitem(item, 1);
1187         nm_restore_background(0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h);
1188         game_flush_inputs();
1189
1190 }//method kc_next_joyaxis
1191 //end addition by WraithX
1192
1193
1194 void kc_change_joyaxis( kc_item * item )
1195 {
1196         int axis[JOY_MAX_AXES];
1197         int old_axis[JOY_MAX_AXES];
1198         int numaxis = joy_num_axes;
1199         int n,i,k;
1200         ubyte code;
1201
1202         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1203         
1204         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_JOY_AXIS );
1205
1206         game_flush_inputs();
1207         code=255;
1208         k=255;
1209
1210         joystick_read_raw_axis( JOY_ALL_AXIS, old_axis );
1211
1212         while( (k!=KEY_ESC) && (code==255))     
1213         {                               
1214                 #ifdef NETWORK
1215                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1216                         multi_menu_poll();
1217                 #endif
1218 //              if ( Game_mode & GM_MULTI )
1219 //                      GameLoop( 0, 0 );                               // Continue
1220                 k = key_inkey();
1221                 timer_delay(f0_1/10);
1222
1223                 if (k == KEY_PRINT_SCREEN)
1224                         save_screen_shot(0);
1225
1226                 kc_drawquestion( item );
1227
1228                 joystick_read_raw_axis( JOY_ALL_AXIS, axis );
1229
1230                 for (i=0; i<numaxis; i++ )      {
1231                         if ( abs(axis[i]-old_axis[i])>100 )
1232                         {
1233                                 code = i;
1234                                 con_printf(CON_DEBUG, "Axis Movement detected: Axis %i\n", i);
1235                         }
1236                         //old_axis[i] = axis[i];
1237                 }
1238                 for (i=0; i<Num_items; i++ )    
1239                  {
1240                         n = item - All_items;
1241                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) 
1242                                 code = 255;
1243                  }
1244         
1245         }
1246         if (code!=255)  {
1247                 for (i=0; i<Num_items; i++ )    {
1248                         n = item - All_items;
1249                         if ( (i!=n) && (All_items[i].type==BT_JOY_AXIS) && (All_items[i].value==code) ) {
1250                                 All_items[i].value = 255;
1251                                 kc_drawitem( &All_items[i], 0 );
1252                         }
1253                 }
1254
1255                 item->value = code;                                      
1256         }
1257         kc_drawitem( item, 1 );
1258         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1259         game_flush_inputs();
1260
1261 }
1262
1263 void kc_change_mouseaxis( kc_item * item )
1264 {
1265         int i,n,k;
1266         ubyte code;
1267         int dx,dy;
1268 #ifdef SDL_INPUT
1269         int dz;
1270 #endif
1271
1272         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
1273         
1274         gr_string( 0x8000, LHY(INFO_Y), TXT_MOVE_NEW_MSE_AXIS );
1275
1276         game_flush_inputs();
1277         code=255;
1278         k=255;
1279
1280         mouse_get_delta( &dx, &dy );
1281
1282         while( (k!=KEY_ESC) && (code==255))     
1283         {                               
1284                 #ifdef NETWORK
1285                 if ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME) && (!Endlevel_sequence))
1286                         multi_menu_poll();
1287                 #endif
1288 //              if ( Game_mode & GM_MULTI )
1289 //                      GameLoop( 0, 0 );                               // Continue
1290                 k = key_inkey();
1291                 timer_delay(f0_1/10);
1292
1293                 if (k == KEY_PRINT_SCREEN)
1294                         save_screen_shot(0);
1295
1296                 kc_drawquestion( item );
1297
1298 #ifdef SDL_INPUT
1299                 mouse_get_delta_z( &dx, &dy, &dz );
1300 #else
1301                 mouse_get_delta( &dx, &dy );
1302 #endif
1303                 if ( abs(dx)>20 ) code = 0;
1304                 if ( abs(dy)>20 ) code = 1;
1305 #ifdef SDL_INPUT
1306                 if ( abs(dz)>20 ) code = 2;
1307 #endif
1308         }
1309         if (code!=255)  {
1310                 for (i=0; i<Num_items; i++ )    {
1311                         n = item - All_items;
1312                         if ( (i!=n) && (All_items[i].type==BT_MOUSE_AXIS) && (All_items[i].value==code) )               {
1313                                 All_items[i].value = 255;
1314                                 kc_drawitem( &All_items[i], 0 );
1315                         }
1316                 }
1317                 item->value = code;
1318         }
1319         kc_drawitem( item, 1 );
1320         nm_restore_background( 0, LHY(INFO_Y), LHX(310), grd_curcanv->cv_font->ft_h );
1321         game_flush_inputs();
1322
1323 }
1324
1325
1326 void kc_change_invert( kc_item * item )
1327 {
1328         game_flush_inputs();
1329
1330         if (item->value)
1331                 item->value = 0;
1332         else 
1333                 item->value = 1;
1334
1335         kc_drawitem( item, 1 );
1336
1337 }
1338
1339 #include "screens.h"
1340
1341 void kconfig(int n, char * title)
1342 {
1343         int i, j;
1344         grs_bitmap *save_bm;
1345
1346         set_screen_mode( SCREEN_MENU );
1347
1348         kc_set_controls();
1349
1350         //save screen
1351         save_bm = gr_create_bitmap( grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
1352         Assert( save_bm != NULL );
1353         
1354         gr_bm_bitblt(grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_w, 
1355                                         0, 0, 0, 0, &grd_curcanv->cv_bitmap, save_bm );
1356
1357         switch(n)       {
1358         case 0:kconfig_sub( kc_keyboard, NUM_KEY_CONTROLS, title );break;
1359         case 1:kconfig_sub( kc_joystick, NUM_OTHER_CONTROLS, title );break;
1360         case 2:kconfig_sub( kc_mouse, NUM_OTHER_CONTROLS, title ); break;
1361         case 4:kconfig_sub( kc_d2x, NUM_D2X_CONTROLS, title ); break;
1362         default:
1363                 Int3();
1364                 return;
1365         }
1366
1367         //restore screen
1368         gr_bitmap(0, 0, save_bm);
1369         gr_free_bitmap(save_bm);
1370
1371 #if 0 // set_screen_mode always calls this later... right?
1372         reset_cockpit();                //force cockpit redraw next time
1373 #endif
1374
1375         // Update save values...
1376         
1377         for (i=0; i<NUM_KEY_CONTROLS; i++ )     
1378                 kconfig_settings[0][i] = kc_keyboard[i].value;
1379
1380         for (j=0; j<256; j++)
1381                 if (key_binding(j)) {
1382                         for (i = 0; i < NUM_KEY_CONTROLS; i++)
1383                                 if (!stricmp(key_binding(j), kc_key_bind_text[i])) {
1384                                         cmd_appendf("unbind %s", key_text[j]);
1385                                         break;
1386                                 }
1387                         for (i = 0; i < NUM_D2X_CONTROLS; i++)
1388                                 if (kc_d2x[i].type == BT_KEY && !stricmp(key_binding(j), kc_d2x[i].text)) {
1389                                         cmd_appendf("unbind %s", key_text[j]);
1390                                         break;
1391                                 }
1392                 }
1393
1394         for (i=0; i<NUM_KEY_CONTROLS; i++ )
1395                 if (kc_keyboard[i].value != 255)
1396                         cmd_appendf("bind %s \"%s\"", key_text[kc_keyboard[i].value], kc_key_bind_text[i]);
1397
1398         for (i = 0; i < NUM_D2X_CONTROLS; i++)
1399                 if (kc_d2x[i].value != 255)
1400                         cmd_appendf("bind %s \"%s\"", key_text[kc_d2x[i].value], kc_d2x[i].text);
1401
1402         if ( (Config_control_type>0) && (Config_control_type<5)) { 
1403                 for (i = 0; i < NUM_OTHER_CONTROLS; i++) {
1404                         kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
1405
1406                         if (kc_joystick[i].type == BT_JOY_AXIS && kc_joystick[i].value != 255) {
1407                                 cvar_setint(&joy_advaxes[kc_joystick[i].value], kc_other_axismap[i]);
1408                                 cvar_setint(&joy_invert[kc_joystick[i].value], kc_joystick[i+1].value);
1409                         }
1410                 }
1411
1412         } else if (Config_control_type > 4) {
1413                 for (i = 0; i < NUM_OTHER_CONTROLS; i++) {
1414                         kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
1415
1416                         if (kc_mouse[i].type == BT_MOUSE_AXIS && kc_mouse[i].value != 255) {
1417                                 cvar_setint(&mouse_axes[kc_mouse[i].value], kc_other_axismap[i]);
1418                                 cvar_setint(&mouse_invert[kc_mouse[i].value], kc_mouse[i+1].value);
1419                         }
1420                 }
1421         }
1422
1423         while (cmd_queue_process())
1424                 ;
1425 }
1426
1427
1428 void kconfig_read_fcs( int raw_axis )
1429 {
1430         int raw_button, button, axis_min[4], axis_center[4], axis_max[4];
1431
1432         if (Config_control_type!=CONTROL_THRUSTMASTER_FCS) return;
1433
1434         joy_get_cal_vals(axis_min, axis_center, axis_max);
1435
1436         if ( axis_max[3] > 1 )
1437                 raw_button = (raw_axis*100)/axis_max[3];
1438         else
1439                 raw_button = 0;
1440
1441         if ( raw_button > 88 )
1442                 button = 0;
1443         else if ( raw_button > 63 )
1444                 button = 7;
1445         else if ( raw_button > 39 )
1446                 button = 11;
1447         else if ( raw_button > 15 )
1448                 button = 15;
1449         else    
1450                 button = 19;
1451
1452         kconfig_set_fcs_button( 19, button );
1453         kconfig_set_fcs_button( 15, button );
1454         kconfig_set_fcs_button( 11, button );
1455         kconfig_set_fcs_button( 7, button );
1456 }
1457                 
1458
1459 void kconfig_set_fcs_button( int btn, int button )
1460 {
1461         int state,time_down,upcount,downcount;
1462         state = time_down = upcount = downcount = 0;
1463
1464         if ( joy_get_button_state(btn) ) {
1465                 if ( btn==button )      {
1466                         state = 1;
1467                         time_down = FrameTime;
1468                 } else {
1469                         upcount=1;
1470                 }
1471         } else {
1472                 if ( btn==button )      {
1473                         state = 1;
1474                         time_down = FrameTime;
1475                         downcount=1;
1476                 } else {
1477                         upcount=1;
1478                 }
1479         }                               
1480                         
1481         joy_set_btn_values( btn, state, time_down, downcount, upcount );
1482                                         
1483 }
1484
1485
1486
1487 fix Last_angles_p = 0;
1488 fix Last_angles_b = 0;
1489 fix Last_angles_h = 0;
1490 ubyte Last_angles_read = 0;
1491
1492 extern int                      VR_sensitivity;
1493                                                 
1494 int VR_sense_range[3] = { 25, 50, 75 };
1495
1496 #if 0 // unused
1497 read_head_tracker()
1498 {
1499         fix yaw, pitch, roll;
1500         int buttons;
1501
1502 //------ read vfx1 helmet --------
1503         if (vfx1_installed) {
1504                 vfx_get_data(&yaw,&pitch,&roll,&buttons);
1505         } else if (iglasses_headset_installed)  {
1506                 iglasses_read_headset( &yaw, &pitch, &roll );
1507         } else if (Victor_headset_installed)   {
1508                 victor_read_headset_filtered( &yaw, &pitch, &roll );
1509         } else {
1510                 return;
1511         }
1512
1513         Use_player_head_angles = 0;
1514         if ( Last_angles_read ) {
1515                 fix yaw1 = yaw;
1516                 
1517                 yaw1 = yaw;
1518                 if ( (Last_angles_h < (F1_0/4) ) && (yaw > ((F1_0*3)/4) ) )     
1519                         yaw1 -= F1_0;
1520                 else if ( (yaw < (F1_0/4) ) && (Last_angles_h > ((F1_0*3)/4) ) )        
1521                         yaw1 += F1_0;
1522         
1523                 Controls.pitch_time     += fixmul((pitch- Last_angles_p)*VR_sense_range[VR_sensitivity],FrameTime);
1524                 Controls.heading_time+= fixmul((yaw1 -  Last_angles_h)*VR_sense_range[VR_sensitivity],FrameTime);
1525                 Controls.bank_time      += fixmul((roll - Last_angles_b)*VR_sense_range[VR_sensitivity],FrameTime);
1526         }
1527         Last_angles_read = 1;
1528         Last_angles_p = pitch;
1529         Last_angles_h = yaw;
1530         Last_angles_b = roll;
1531 }
1532 #endif
1533
1534 #define PH_SCALE        8
1535
1536 #ifndef __MSDOS__
1537 #define JOYSTICK_READ_TIME      (F1_0/40)               //      Read joystick at 40 Hz.
1538 #else
1539 #define JOYSTICK_READ_TIME      (F1_0/10)               //      Read joystick at 10 Hz.
1540 #endif
1541
1542 fix     LastReadTime = 0;
1543
1544 fix     joy_axis[JOY_MAX_AXES];
1545
1546 ubyte                   kc_use_external_control = 0;
1547 ubyte                   kc_enable_external_control = 0;
1548 ubyte                   kc_external_intno = 0;
1549 ext_control_info        *kc_external_control = NULL;
1550 char                    *kc_external_name = NULL;
1551 ubyte                   kc_external_version = 0;
1552 extern int Automap_active;
1553
1554 void kconfig_init_external_controls(int intno, int address)
1555 {
1556         int i;
1557         kc_external_intno = intno;
1558         kc_external_control     = (ext_control_info *)address;
1559         kc_use_external_control = 1;
1560         kc_enable_external_control  = 1;
1561
1562         i = FindArg ( "-xname" );
1563         if ( i )        
1564                 kc_external_name = Args[i+1];
1565         else
1566                 kc_external_name = "External Controller";
1567
1568    for (i=0;i<strlen (kc_external_name);i++)
1569     if (kc_external_name[i]=='_')
1570           kc_external_name[i]=' '; 
1571
1572         i = FindArg ( "-xver" );
1573         if ( i )
1574                 kc_external_version = atoi(Args[i+1]);
1575         
1576         printf( "%s int: 0x%x, data: 0x%p, ver:%d\n", kc_external_name, kc_external_intno, kc_external_control, kc_external_version );
1577
1578 }
1579
1580
1581 fix Next_toggle_time[3]={0,0,0};
1582
1583 int allowed_to_toggle(int i)
1584 {
1585   //used for keeping tabs of when its ok to toggle headlight,primary,and secondary
1586  
1587         if (Next_toggle_time[i] > GameTime)
1588                 if (Next_toggle_time[i] < GameTime + (F1_0/8))  //      In case time is bogus, never wait > 1 second.
1589                         return 0;
1590
1591         Next_toggle_time[i] = GameTime + (F1_0/8);
1592
1593         return 1;
1594 }
1595
1596
1597 void controls_read_all()
1598 {
1599         int i;
1600         int slide_on, bank_on;
1601         int dx, dy;
1602 #ifdef SDL_INPUT
1603         int dz;
1604 #endif
1605         int idx, idy;
1606         fix ctime;
1607         fix mouse_axis[3] = {0,0,0};
1608         int raw_joy_axis[JOY_MAX_AXES];
1609         fix kp, kh;
1610         ubyte channel_masks;
1611         int use_mouse, use_joystick;
1612
1613         use_mouse=0;
1614
1615         {
1616                 fix temp = Controls.heading_time;
1617                 fix temp1 = Controls.pitch_time;
1618                 memset( &Controls, 0, sizeof(control_info) );
1619                 Controls.heading_time = temp;
1620                 Controls.pitch_time = temp1;
1621         }
1622         slide_on = 0;
1623         bank_on = 0;
1624
1625         ctime = timer_get_fixed_seconds();
1626
1627         //---------  Read Joystick -----------
1628         if ( (LastReadTime + JOYSTICK_READ_TIME > ctime) && (Config_control_type!=CONTROL_THRUSTMASTER_FCS) ) {
1629 # ifndef __MSDOS__
1630                 if ((ctime < 0) && (LastReadTime >= 0))
1631 # else
1632                 if ((ctime < 0) && (LastReadTime > 0))
1633 # endif
1634                         LastReadTime = ctime;
1635                 use_joystick=1;
1636         } else if ((Config_control_type>0) && (Config_control_type<5) ) {
1637                 LastReadTime = ctime;
1638                 channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
1639
1640                 for (i = 0; i < joy_num_axes; i++)
1641                 {
1642 #ifndef SDL_INPUT
1643                         if (channel_masks&(1<<i))       {
1644 #endif
1645                                 int joy_null_value = 10;
1646
1647                                 if ( (i==3) && (Config_control_type==CONTROL_THRUSTMASTER_FCS) )        {
1648                                         kconfig_read_fcs( raw_joy_axis[i] );
1649                                 } else {
1650                                         raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
1651         
1652                                         if (kc_joystick[23].value==i)           // If this is the throttle
1653                                                 joy_null_value = 20;            // Then use a larger dead-zone
1654         
1655                                         if (raw_joy_axis[i] > joy_null_value) 
1656                                           raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
1657                                         else if (raw_joy_axis[i] < -joy_null_value)
1658                                           raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
1659                                         else
1660                                           raw_joy_axis[i] = 0;
1661                                         joy_axis[i]     = (raw_joy_axis[i]*FrameTime)/128;      
1662                                 }
1663 #ifndef SDL_INPUT
1664                         } else {
1665                                 joy_axis[i] = 0;
1666                         }
1667 #endif
1668                 }       
1669                 use_joystick=1;
1670         } else {
1671                 for (i = 0; i < joy_num_axes; i++)
1672                         joy_axis[i] = 0;
1673                 use_joystick=0;
1674         }
1675
1676         if (Config_control_type==5 && !CybermouseActive) {
1677                 //---------  Read Mouse -----------
1678 #ifdef SDL_INPUT
1679                 mouse_get_delta_z( &dx, &dy, &dz );
1680 #else
1681                 mouse_get_delta( &dx, &dy );
1682 #endif
1683                 mouse_axis[0] = (dx*FrameTime)/35;
1684                 mouse_axis[1] = (dy*FrameTime)/25;
1685 #ifdef SDL_INPUT
1686                 mouse_axis[2] = (dz*FrameTime);
1687 #endif
1688                 //mprintf(( 0, "Mouse %d,%d 0x%x\n", mouse_axis[0], mouse_axis[1], FrameTime ));
1689                 use_mouse=1;
1690         } else if (Config_control_type==6 && !CybermouseActive) {
1691                 //---------  Read Cyberman -----------
1692                 mouse_get_cyberman_pos(&idx,&idy );
1693                 mouse_axis[0] = (idx*FrameTime)/128;
1694                 mouse_axis[1] = (idy*FrameTime)/128;
1695                 use_mouse=1;
1696         } else if (CybermouseActive) {
1697 //              ReadOWL (kc_external_control);
1698 //              CybermouseAdjust();
1699         } else {
1700                 mouse_axis[0] = 0;
1701                 mouse_axis[1] = 0;
1702                 use_mouse=0;
1703         }
1704
1705 //------------- Read slide_on -------------
1706         
1707         // From console...
1708         slide_on |= console_control_state(CONCNTL_STRAFE);
1709
1710 //------------- Read bank_on ---------------
1711
1712         // From console...
1713         bank_on |= console_control_state(CONCNTL_BANK);
1714
1715 //------------ Read pitch_time -----------
1716         if ( !slide_on )        {
1717                 // mprintf((0, "pitch: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
1718                 kp = 0;
1719
1720                 // From console...
1721                 kp += console_control_down_time(CONCNTL_LOOKDOWN) / (PH_SCALE * 2);
1722                 kp -= console_control_down_time(CONCNTL_LOOKUP) / (PH_SCALE * 2);
1723
1724                 if (kp == 0)
1725                         Controls.pitch_time = 0;
1726                 else if (kp > 0) {
1727                         if (Controls.pitch_time < 0)
1728                                 Controls.pitch_time = 0;
1729                 } else // kp < 0
1730                         if (Controls.pitch_time > 0)
1731                                 Controls.pitch_time = 0;
1732                 Controls.pitch_time += kp;
1733         
1734                 // From joystick...
1735                 if ( (use_joystick)&&(kc_joystick[13].value < 255 ))    {
1736                         if ( !kc_joystick[14].value )           // If not inverted...
1737                                 Controls.pitch_time -= (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
1738                         else
1739                                 Controls.pitch_time += (joy_axis[kc_joystick[13].value]*Config_joystick_sensitivity)/8;
1740                 }
1741         
1742                 // From mouse...
1743                 //mprintf(( 0, "UM: %d, PV: %d\n", use_mouse, kc_mouse[13].value ));
1744                 if ( (use_mouse)&&(kc_mouse[13].value < 255) )  {
1745                         if ( !kc_mouse[14].value )              // If not inverted...
1746                                 Controls.pitch_time -= (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
1747                         else
1748                                 Controls.pitch_time += (mouse_axis[kc_mouse[13].value]*Config_joystick_sensitivity)/8;
1749                 }
1750         } else {
1751                 Controls.pitch_time = 0;
1752         }
1753
1754
1755 // the following "if" added by WraithX, 4/14/00
1756 // done so that dead players can't move
1757 if (!Player_is_dead)
1758 {
1759 //----------- Read vertical_thrust_time -----------------
1760
1761         if ( slide_on ) {
1762                 // From console...
1763                 Controls.vertical_thrust_time += console_control_down_time(CONCNTL_LOOKDOWN);
1764                 Controls.vertical_thrust_time -= console_control_down_time(CONCNTL_LOOKUP);
1765
1766                 // From joystick...
1767                 if ((use_joystick)&&( kc_joystick[13].value < 255 ))    {
1768                         if ( !kc_joystick[14].value )           // If not inverted...
1769                                 Controls.vertical_thrust_time += joy_axis[kc_joystick[13].value];
1770                         else
1771                                 Controls.vertical_thrust_time -= joy_axis[kc_joystick[13].value];
1772                 }
1773         
1774                 // From mouse...
1775                 if ( (use_mouse)&&(kc_mouse[13].value < 255 ))  {
1776                         if ( !kc_mouse[14].value )              // If not inverted...
1777                                 Controls.vertical_thrust_time -= mouse_axis[kc_mouse[13].value];
1778                         else
1779                                 Controls.vertical_thrust_time += mouse_axis[kc_mouse[13].value];
1780                 }
1781         }
1782
1783         // From console...
1784         Controls.vertical_thrust_time += console_control_down_time(CONCNTL_MOVEUP);
1785         Controls.vertical_thrust_time -= console_control_down_time(CONCNTL_MOVEDOWN);
1786
1787         // From joystick...
1788         if ((use_joystick)&&( kc_joystick[19].value < 255 ))    {
1789                 if ( !kc_joystick[20].value )           // If not inverted...
1790                         Controls.vertical_thrust_time += joy_axis[kc_joystick[19].value];
1791                 else
1792                         Controls.vertical_thrust_time -= joy_axis[kc_joystick[19].value];
1793         }
1794
1795         // From mouse...
1796         if ( (use_mouse)&&(kc_mouse[19].value < 255 ))  {
1797                 if ( !kc_mouse[20].value )              // If not inverted...
1798                         Controls.vertical_thrust_time += mouse_axis[kc_mouse[19].value];
1799                 else
1800                         Controls.vertical_thrust_time -= mouse_axis[kc_mouse[19].value];
1801         }
1802
1803 }// end "if" added by WraithX
1804
1805 //---------- Read heading_time -----------
1806
1807         if (!slide_on && !bank_on)      {
1808                 //mprintf((0, "heading: %7.3f %7.3f: %7.3f\n", f2fl(k4), f2fl(k6), f2fl(Controls.heading_time)));
1809                 kh = 0;
1810
1811                 // From console...
1812                 kh -= console_control_down_time(CONCNTL_LEFT) / PH_SCALE;
1813                 kh += console_control_down_time(CONCNTL_RIGHT) / PH_SCALE;
1814
1815                 if (kh == 0)
1816                         Controls.heading_time = 0;
1817                 else if (kh > 0) {
1818                         if (Controls.heading_time < 0)
1819                                 Controls.heading_time = 0;
1820                 } else // kh < 0
1821                         if (Controls.heading_time > 0)
1822                                 Controls.heading_time = 0;
1823                 Controls.heading_time += kh;
1824
1825                 // From joystick...
1826                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
1827                         if ( !kc_joystick[16].value )           // If not inverted...
1828                                 Controls.heading_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
1829                         else
1830                                 Controls.heading_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
1831                 }
1832         
1833                 // From mouse...
1834                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
1835                         if ( !kc_mouse[16].value )              // If not inverted...
1836                                 Controls.heading_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
1837                         else
1838                                 Controls.heading_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
1839                 }
1840         } else {
1841                 Controls.heading_time = 0;
1842         }
1843
1844 // the following "if" added by WraithX, 4/14/00
1845 // done so that dead players can't move
1846 if (!Player_is_dead)
1847 {
1848 //----------- Read sideways_thrust_time -----------------
1849
1850         if ( slide_on ) {
1851                 // From console...
1852                 Controls.sideways_thrust_time -= console_control_down_time(CONCNTL_LEFT);
1853                 Controls.sideways_thrust_time += console_control_down_time(CONCNTL_RIGHT);
1854
1855                 // From joystick...
1856                 if ( (use_joystick)&&(kc_joystick[15].value < 255 ))    {
1857                         if ( !kc_joystick[16].value )           // If not inverted...
1858                                 Controls.sideways_thrust_time += joy_axis[kc_joystick[15].value];
1859                         else
1860                                 Controls.sideways_thrust_time -= joy_axis[kc_joystick[15].value];
1861                 }
1862                 
1863                 // From mouse...
1864                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
1865                         if ( !kc_mouse[16].value )              // If not inverted...
1866                                 Controls.sideways_thrust_time += mouse_axis[kc_mouse[15].value];
1867                         else
1868                                 Controls.sideways_thrust_time -= mouse_axis[kc_mouse[15].value];
1869                 }
1870         }
1871
1872         // From console...
1873         Controls.sideways_thrust_time -= console_control_down_time(CONCNTL_MOVELEFT);
1874         Controls.sideways_thrust_time += console_control_down_time(CONCNTL_MOVERIGHT);
1875
1876         // From joystick...
1877         if ( (use_joystick)&&(kc_joystick[17].value < 255 ))    {
1878                 if ( !kc_joystick[18].value )           // If not inverted...
1879                         Controls.sideways_thrust_time -= joy_axis[kc_joystick[17].value];
1880                 else
1881                         Controls.sideways_thrust_time += joy_axis[kc_joystick[17].value];
1882         }
1883
1884         // From mouse...
1885         if ( (use_mouse)&&(kc_mouse[17].value < 255 ))  {
1886                 if ( !kc_mouse[18].value )              // If not inverted...
1887                         Controls.sideways_thrust_time += mouse_axis[kc_mouse[17].value];
1888                 else
1889                         Controls.sideways_thrust_time -= mouse_axis[kc_mouse[17].value];
1890         }
1891 }// end "if" added by WraithX
1892
1893 //----------- Read bank_time -----------------
1894
1895         if ( bank_on )  {
1896                 // From console...
1897                 Controls.bank_time -= console_control_down_time(CONCNTL_LEFT);
1898                 Controls.bank_time += console_control_down_time(CONCNTL_RIGHT);
1899
1900                 // From joystick...
1901                 if ( (use_joystick)&&(kc_joystick[15].value < 255) )    {
1902                         if ( !kc_joystick[16].value )           // If not inverted...
1903                                 Controls.bank_time -= (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
1904                         else
1905                                 Controls.bank_time += (joy_axis[kc_joystick[15].value]*Config_joystick_sensitivity)/8;
1906                 }
1907         
1908                 // From mouse...
1909                 if ( (use_mouse)&&(kc_mouse[15].value < 255 ))  {
1910                         if ( !kc_mouse[16].value )              // If not inverted...
1911                                 Controls.bank_time += (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
1912                         else
1913                                 Controls.bank_time -= (mouse_axis[kc_mouse[15].value]*Config_joystick_sensitivity)/8;
1914                 }
1915         }
1916
1917         // From console...
1918         Controls.bank_time += console_control_down_time(CONCNTL_BANKLEFT);
1919         Controls.bank_time -= console_control_down_time(CONCNTL_BANKRIGHT);
1920
1921         // From joystick...
1922         if ( (use_joystick)&&(kc_joystick[21].value < 255) )    {
1923                 if ( !kc_joystick[22].value )           // If not inverted...
1924                         Controls.bank_time -= joy_axis[kc_joystick[21].value];
1925                 else
1926                         Controls.bank_time += joy_axis[kc_joystick[21].value];
1927         }
1928
1929         // From mouse...
1930         if ( (use_mouse)&&(kc_mouse[21].value < 255 ))  {
1931                 if ( !kc_mouse[22].value )              // If not inverted...
1932                         Controls.bank_time += mouse_axis[kc_mouse[21].value];
1933                 else
1934                         Controls.bank_time -= mouse_axis[kc_mouse[21].value];
1935         }
1936
1937 // the following "if" added by WraithX, 4/14/00
1938 // done so that dead players can't move
1939 if (!Player_is_dead)
1940 {
1941 //----------- Read forward_thrust_time -------------
1942
1943         // From console...
1944         Controls.forward_thrust_time += console_control_down_time(CONCNTL_FORWARD);
1945         Controls.forward_thrust_time -= console_control_down_time(CONCNTL_BACK);
1946
1947         // From joystick...
1948         if ( (use_joystick)&&(kc_joystick[23].value < 255 ))    {
1949                 if ( !kc_joystick[24].value )           // If not inverted...
1950                         Controls.forward_thrust_time -= joy_axis[kc_joystick[23].value];
1951                 else
1952                         Controls.forward_thrust_time += joy_axis[kc_joystick[23].value];
1953         }
1954
1955         // From mouse...
1956         if ( (use_mouse)&&(kc_mouse[23].value < 255 ))  {
1957                 if ( !kc_mouse[24].value )              // If not inverted...
1958                         Controls.forward_thrust_time -= mouse_axis[kc_mouse[23].value];
1959                 else
1960                         Controls.forward_thrust_time += mouse_axis[kc_mouse[23].value];
1961         }
1962
1963 //----------- Read afterburner_state -------------
1964
1965         // From console...
1966         Controls.afterburner_state |= console_control_state(CONCNTL_AFTERBURN);
1967
1968 //-------Read headlight key--------------------------
1969
1970         // From console...
1971         Controls.headlight_count += console_control_down_count(CONCNTL_HEADLIGHT);
1972
1973 //--------Read Cycle Primary Key------------------
1974
1975         // From console...
1976         Controls.cycle_primary_count += console_control_down_count(CONCNTL_CYCLE);
1977
1978 //--------Read Cycle Secondary Key------------------
1979
1980         // From console...
1981         Controls.cycle_secondary_count += console_control_down_count(CONCNTL_CYCLE2);
1982
1983 //--------Read Toggle Bomb key----------------------
1984
1985         // From console...
1986         if (console_control_down_count(CONCNTL_TOGGLEBOMB))
1987          {
1988           int bomb = Secondary_last_was_super[PROXIMITY_INDEX]?PROXIMITY_INDEX:SMART_MINE_INDEX;
1989
1990                          if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX] &&
1991                                   !Players[Player_num].secondary_ammo[SMART_MINE_INDEX])
1992                            {
1993                                  digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
1994                                  HUD_init_message ("No bombs available!");
1995                                 }
1996                          else
1997                                 {       
1998                                  if (Players[Player_num].secondary_ammo[bomb]==0)
1999                                         {
2000                                          digi_play_sample_once( SOUND_BAD_SELECTION, F1_0 );
2001                                          HUD_init_message ("No %s available!",(bomb==SMART_MINE_INDEX)?"Smart mines":"Proximity bombs");
2002                                         }
2003                                   else
2004                                         {
2005                                Secondary_last_was_super[PROXIMITY_INDEX]=!Secondary_last_was_super[PROXIMITY_INDEX];
2006                                          digi_play_sample_once( SOUND_GOOD_SELECTION_SECONDARY, F1_0 );
2007                                         }
2008                                 }
2009                         }
2010           
2011 //---------Read Energy->Shield key----------
2012
2013         // From console...
2014         if ((Players[Player_num].flags & PLAYER_FLAGS_CONVERTER) && console_control_state(CONCNTL_NRGSHIELD))
2015                 transfer_energy_to_shield(console_control_down_time(CONCNTL_NRGSHIELD));
2016
2017 //----------- Read fire_primary_down_count
2018
2019         // From console...
2020         Controls.fire_primary_down_count += console_control_down_count(CONCNTL_ATTACK);
2021
2022 //----------- Read fire_primary_state
2023
2024         // From console...
2025         Controls.fire_primary_state |= console_control_state(CONCNTL_ATTACK);
2026
2027 //----------- Read fire_secondary_down_count
2028
2029         // From console...
2030         Controls.fire_secondary_down_count += console_control_down_count(CONCNTL_ATTACK2);
2031
2032 //----------- Read fire_secondary_state
2033
2034         // From console...
2035         Controls.fire_secondary_state |= console_control_state(CONCNTL_ATTACK2);
2036
2037 //----------- Read fire_flare_down_count
2038
2039         // From console...
2040         Controls.fire_flare_down_count += console_control_down_count(CONCNTL_FLARE);
2041
2042 //----------- Read drop_bomb_down_count
2043
2044         // From console...
2045         Controls.drop_bomb_down_count += console_control_down_count(CONCNTL_BOMB);
2046
2047 //----------- Read rear_view_down_count
2048
2049         // From console...
2050         Controls.rear_view_down_count += console_control_down_count(CONCNTL_REARVIEW);
2051
2052 //----------- Read rear_view_down_state
2053
2054         // From console...
2055         Controls.rear_view_down_state |= console_control_state(CONCNTL_REARVIEW);
2056
2057 }//end "if" added by WraithX
2058
2059 //----------- Read automap_down_count
2060
2061         // From console...
2062         Controls.automap_down_count += console_control_down_count(CONCNTL_AUTOMAP);
2063
2064 //----------- Read automap_state
2065
2066         // From console...
2067         Controls.automap_state |= console_control_state(CONCNTL_AUTOMAP);
2068
2069 //----------- Read stupid-cruise-control-type of throttle.
2070         {
2071                 // From console...
2072                 Cruise_speed += console_control_down_time(CONCNTL_CRUISEUP);
2073                 Cruise_speed -= console_control_down_time(CONCNTL_CRUISEDOWN);
2074
2075                 // From console...
2076                 if (console_control_down_count(CONCNTL_CRUISEOFF))
2077                         Cruise_speed = 0;
2078
2079                 if (Cruise_speed > i2f(100) ) Cruise_speed = i2f(100);
2080                 if (Cruise_speed < 0 ) Cruise_speed = 0;
2081         
2082                 if (Controls.forward_thrust_time==0)
2083                         Controls.forward_thrust_time = fixmul(Cruise_speed,FrameTime)/100;
2084         }
2085
2086 #if 0
2087         read_head_tracker();
2088
2089         // Read external controls
2090         if (kc_use_external_control || CybermouseActive)
2091                 kconfig_read_external_controls();
2092 #endif
2093
2094 //----------- Clamp values between -FrameTime and FrameTime
2095         if (FrameTime > F1_0 )
2096                 mprintf( (1, "Bogus frame time of %.2f seconds\n", f2fl(FrameTime) ));
2097
2098         if (Controls.pitch_time > FrameTime/2 ) Controls.pitch_time = FrameTime/2;
2099         if (Controls.vertical_thrust_time > FrameTime ) Controls.vertical_thrust_time = FrameTime;
2100         if (Controls.heading_time > FrameTime ) Controls.heading_time = FrameTime;
2101         if (Controls.sideways_thrust_time > FrameTime ) Controls.sideways_thrust_time = FrameTime;
2102         if (Controls.bank_time > FrameTime ) Controls.bank_time = FrameTime;
2103         if (Controls.forward_thrust_time > FrameTime ) Controls.forward_thrust_time = FrameTime;
2104 //      if (Controls.afterburner_time > FrameTime ) Controls.afterburner_time = FrameTime;
2105
2106         if (Controls.pitch_time < -FrameTime/2 ) Controls.pitch_time = -FrameTime/2;
2107         if (Controls.vertical_thrust_time < -FrameTime ) Controls.vertical_thrust_time = -FrameTime;
2108         if (Controls.heading_time < -FrameTime ) Controls.heading_time = -FrameTime;
2109         if (Controls.sideways_thrust_time < -FrameTime ) Controls.sideways_thrust_time = -FrameTime;
2110         if (Controls.bank_time < -FrameTime ) Controls.bank_time = -FrameTime;
2111         if (Controls.forward_thrust_time < -FrameTime ) Controls.forward_thrust_time = -FrameTime;
2112 //      if (Controls.afterburner_time < -FrameTime ) Controls.afterburner_time = -FrameTime;
2113
2114
2115 //--------- Don't do anything if in debug mode
2116         #ifndef RELEASE
2117         if ( keyd_pressed[KEY_DELETE] ) {
2118                 memset( &Controls, 0, sizeof(control_info) );
2119         }
2120         #endif
2121 }
2122
2123
2124 void reset_cruise(void)
2125 {
2126         Cruise_speed=0;
2127 }
2128
2129
2130 void kc_set_controls()
2131 {
2132         int i, j;
2133
2134         for (i=0; i<NUM_KEY_CONTROLS; i++ )
2135                 kc_keyboard[i].value = 255;
2136
2137         for (i=0; i<NUM_OTHER_CONTROLS; i++ )
2138                 kc_joystick[i].value = kc_mouse[i].value = 255;
2139
2140         for (i=0; i<NUM_D2X_CONTROLS; i++ )
2141                 kc_d2x[i].value = 255;
2142
2143         if ( (Config_control_type>0) && (Config_control_type<5)) {
2144                 for (i=0; i<NUM_OTHER_CONTROLS; i++ ) {
2145                         if (kc_joystick[i].type == BT_INVERT )  {
2146                                 kc_joystick[i].value = kconfig_settings[Config_control_type][i] ? 1 : 0;
2147                                 kconfig_settings[Config_control_type][i] = kc_joystick[i].value;
2148                         }
2149                 }
2150         } else if (Config_control_type > 4) {
2151                 for (i=0; i<NUM_OTHER_CONTROLS; i++ )   {
2152                         if (kc_mouse[i].type == BT_INVERT )     {
2153                                 kc_mouse[i].value = kconfig_settings[Config_control_type][i] ? 1 : 0;
2154                                 kconfig_settings[Config_control_type][i] = kc_mouse[i].value;
2155                         }
2156                 }
2157         }
2158         for (j = 0; j < 256; j++)
2159                 if (key_binding(j)) {
2160                         for (i = 0; i < NUM_KEY_CONTROLS; i++)
2161                                 if (kc_keyboard[i].value == 255
2162                                         && !stricmp(key_binding(j), kc_key_bind_text[i])) {
2163                                         kc_keyboard[i].value = j;
2164                                         break;
2165                                 }
2166                 }
2167
2168         for(j = 0; j < 256; j++)
2169                 if (key_binding(j)) {
2170                         for (i = 0; i < NUM_D2X_CONTROLS; i++)
2171                                 if (kc_d2x[i].value == 255
2172                                         && !stricmp(key_binding(j), kc_d2x[i].text)) {
2173                                         kc_d2x[i].value = j;
2174                                         break;
2175                                 }
2176                 }
2177
2178         for (i = 0; i < 3; i++) {
2179                 int inv = mouse_invert[i].intval;
2180                 switch (mouse_axes[i].intval) {
2181                         case AXIS_PITCH:     kc_mouse[13].value = i; kc_mouse[14].value = inv; break;
2182                         case AXIS_TURN:      kc_mouse[15].value = i; kc_mouse[16].value = inv; break;
2183                         case AXIS_LEFTRIGHT: kc_mouse[17].value = i; kc_mouse[18].value = inv; break;
2184                         case AXIS_UPDOWN:    kc_mouse[19].value = i; kc_mouse[20].value = inv; break;
2185                         case AXIS_BANK:      kc_mouse[21].value = i; kc_mouse[22].value = inv; break;
2186                         case AXIS_THROTTLE:  kc_mouse[23].value = i; kc_mouse[24].value = inv; break;
2187                         case AXIS_NONE:      break;
2188                         default:
2189                                 Int3();
2190                                 break;
2191                 }
2192         }
2193
2194         for (i = 0; i < 6; i++) {
2195                 int inv = joy_invert[i].intval;
2196                 switch (joy_advaxes[i].intval) {
2197                         case AXIS_PITCH:     kc_joystick[13].value = i; kc_joystick[14].value = inv; break;
2198                         case AXIS_TURN:      kc_joystick[15].value = i; kc_joystick[16].value = inv; break;
2199                         case AXIS_LEFTRIGHT: kc_joystick[17].value = i; kc_joystick[18].value = inv; break;
2200                         case AXIS_UPDOWN:    kc_joystick[19].value = i; kc_joystick[20].value = inv; break;
2201                         case AXIS_BANK:      kc_joystick[21].value = i; kc_joystick[22].value = inv; break;
2202                         case AXIS_THROTTLE:  kc_joystick[23].value = i; kc_joystick[24].value = inv; break;
2203                         case AXIS_NONE:      break;
2204                         default:
2205                                 Int3();
2206                                 break;
2207                 }
2208         }
2209 }
2210
2211 #if 0 // no mac support for vr headset
2212
2213 void kconfig_center_headset()
2214 {
2215         if (vfx1_installed)
2216                 vfx_center_headset();
2217 //      } else if (iglasses_headset_installed)  {
2218 //      } else if (Victor_headset_installed)   {
2219 //      } else {
2220 //      }
2221
2222 }
2223
2224 #endif // end of #if for kconfig_center_headset
2225
2226 void CybermouseAdjust ()
2227  {
2228 /*      if ( Player_num > -1 )  {
2229                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
2230                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
2231                 Auto_leveling_on = 0;
2232
2233                 if ( kc_external_version > 0 ) {                
2234                         vms_matrix tempm, ViewMatrix;
2235                         vms_angvec * Kconfig_abs_movement;
2236                         char * oem_message;
2237         
2238                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(control_info));
2239         
2240                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
2241                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
2242                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
2243                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
2244                         }
2245                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
2246                         if (oem_message[0] != '\0' )
2247                                 HUD_init_message( oem_message );
2248                 }
2249         }*/
2250
2251         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
2252         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
2253         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
2254         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
2255         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
2256         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
2257 //      Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
2258 //      Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
2259         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
2260         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
2261         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
2262         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
2263         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
2264         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
2265 //      Controls.automap_down_count += kc_external_control->automap_down_count;
2266 //      Controls.automap_state |= kc_external_control->automap_state;
2267   } 
2268
2269 char GetKeyValue (char key)
2270   {
2271         mprintf ((0,"Returning %c!\n",kc_keyboard[(int)key].value));
2272         return (kc_keyboard[(int)key].value);
2273   }
2274
2275
2276 #if 0 // unused
2277 extern object *obj_find_first_of_type (int);
2278 void kconfig_read_external_controls()
2279 {
2280         //union REGS r;
2281    int i;
2282
2283         if ( !kc_enable_external_control ) return;
2284
2285         if ( kc_external_version == 0 ) 
2286                 memset( kc_external_control, 0, sizeof(ext_control_info));
2287         else if ( kc_external_version > 0 )     {
2288         
2289                 if (kc_external_version>=4)
2290                         memset( kc_external_control, 0, sizeof(advanced_ext_control_info));
2291       else if (kc_external_version>0)     
2292                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 );
2293                 else if (kc_external_version>2)
2294                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof(vms_matrix) +4 );
2295
2296                 if ( kc_external_version > 1 ) {
2297                         // Write ship pos and angles to external controls...
2298                         ubyte *temp_ptr = (ubyte *)kc_external_control;
2299                         vms_vector *ship_pos;
2300                         vms_matrix *ship_orient;
2301                         memset( kc_external_control, 0, sizeof(ext_control_info)+sizeof(vms_angvec) + 64 + sizeof(vms_vector)+sizeof(vms_matrix) );
2302                         temp_ptr += sizeof(ext_control_info) + sizeof(vms_angvec) + 64;
2303                         ship_pos = (vms_vector *)temp_ptr;
2304                         temp_ptr += sizeof(vms_vector);
2305                         ship_orient = (vms_matrix *)temp_ptr;
2306                         // Fill in ship postion...
2307                         *ship_pos = Objects[Players[Player_num].objnum].pos;
2308                         // Fill in ship orientation...
2309                         *ship_orient = Objects[Players[Player_num].objnum].orient;
2310                 }
2311     if (kc_external_version>=4)
2312           {
2313            advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
2314  
2315       temp_ptr->headlight_state=(Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON);
2316                 temp_ptr->primary_weapon_flags=Players[Player_num].primary_weapon_flags;
2317                 temp_ptr->secondary_weapon_flags=Players[Player_num].secondary_weapon_flags;
2318       temp_ptr->current_primary_weapon=Primary_weapon;
2319       temp_ptr->current_secondary_weapon=Secondary_weapon;
2320
2321       temp_ptr->current_guidebot_command=Escort_goal_object;
2322
2323            temp_ptr->force_vector=ExtForceVec;
2324                 temp_ptr->force_matrix=ExtApplyForceMatrix;
2325            for (i=0;i<3;i++)
2326        temp_ptr->joltinfo[i]=ExtJoltInfo[i];  
2327       for (i=0;i<2;i++)
2328                    temp_ptr->x_vibrate_info[i]=ExtXVibrateInfo[i];
2329                 temp_ptr->x_vibrate_clear=ExtXVibrateClear;
2330            temp_ptr->game_status=ExtGameStatus;
2331    
2332       memset ((void *)&ExtForceVec,0,sizeof(vms_vector));
2333       memset ((void *)&ExtApplyForceMatrix,0,sizeof(vms_matrix));
2334       
2335       for (i=0;i<3;i++)
2336                  ExtJoltInfo[i]=0;
2337       for (i=0;i<2;i++)
2338                  ExtXVibrateInfo[i]=0;
2339       ExtXVibrateClear=0;
2340      }
2341         }
2342
2343         if ( Automap_active )                   // (If in automap...)
2344                 kc_external_control->automap_state = 1;
2345         //memset(&r,0,sizeof(r));
2346
2347   #if 0
2348  
2349         int386 ( kc_external_intno, &r, &r);            // Read external info...
2350
2351   #endif 
2352
2353         if ( Player_num > -1 )  {
2354                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_TURNROLL);    // Turn off roll when turning
2355                 Objects[Players[Player_num].objnum].mtype.phys_info.flags &= (~PF_LEVELLING);   // Turn off leveling to nearest side.
2356                 Auto_leveling_on = 0;
2357
2358                 if ( kc_external_version > 0 ) {                
2359                         vms_matrix tempm, ViewMatrix;
2360                         vms_angvec * Kconfig_abs_movement;
2361                         char * oem_message;
2362         
2363                         Kconfig_abs_movement = (vms_angvec *)((uint)kc_external_control + sizeof(ext_control_info));
2364         
2365                         if ( Kconfig_abs_movement->p || Kconfig_abs_movement->b || Kconfig_abs_movement->h )    {
2366                                 vm_angles_2_matrix(&tempm,Kconfig_abs_movement);
2367                                 vm_matrix_x_matrix(&ViewMatrix,&Objects[Players[Player_num].objnum].orient,&tempm);
2368                                 Objects[Players[Player_num].objnum].orient = ViewMatrix;                
2369                         }
2370                         oem_message = (char *)((uint)Kconfig_abs_movement + sizeof(vms_angvec));
2371                         if (oem_message[0] != '\0' )
2372                                 HUD_init_message( oem_message );
2373                 }
2374         }
2375
2376         Controls.pitch_time += fixmul(kc_external_control->pitch_time,FrameTime);                                               
2377         Controls.vertical_thrust_time += fixmul(kc_external_control->vertical_thrust_time,FrameTime);
2378         Controls.heading_time += fixmul(kc_external_control->heading_time,FrameTime);
2379         Controls.sideways_thrust_time += fixmul(kc_external_control->sideways_thrust_time ,FrameTime);
2380         Controls.bank_time += fixmul(kc_external_control->bank_time ,FrameTime);
2381         Controls.forward_thrust_time += fixmul(kc_external_control->forward_thrust_time ,FrameTime);
2382         Controls.rear_view_down_count += kc_external_control->rear_view_down_count;     
2383         Controls.rear_view_down_state |= kc_external_control->rear_view_down_state;     
2384         Controls.fire_primary_down_count += kc_external_control->fire_primary_down_count;
2385         Controls.fire_primary_state |= kc_external_control->fire_primary_state;
2386         Controls.fire_secondary_state |= kc_external_control->fire_secondary_state;
2387         Controls.fire_secondary_down_count += kc_external_control->fire_secondary_down_count;
2388         Controls.fire_flare_down_count += kc_external_control->fire_flare_down_count;
2389         Controls.drop_bomb_down_count += kc_external_control->drop_bomb_down_count;     
2390         Controls.automap_down_count += kc_external_control->automap_down_count;
2391         Controls.automap_state |= kc_external_control->automap_state;
2392         
2393    if (kc_external_version>=3)
2394          {
2395                 ubyte *temp_ptr = (ubyte *)kc_external_control;
2396                 temp_ptr += (sizeof(ext_control_info) + sizeof(vms_angvec) + 64 + sizeof(vms_vector) + sizeof (vms_matrix));
2397   
2398            if (*(temp_ptr))
2399                  Controls.cycle_primary_count=(*(temp_ptr));
2400            if (*(temp_ptr+1))
2401                  Controls.cycle_secondary_count=(*(temp_ptr+1));
2402
2403                 if (*(temp_ptr+2))
2404                  Controls.afterburner_state=(*(temp_ptr+2));
2405                 if (*(temp_ptr+3))
2406                  Controls.headlight_count=(*(temp_ptr+3));
2407          }
2408    if (kc_external_version>=4)
2409          {
2410      int i;
2411           advanced_ext_control_info *temp_ptr=(advanced_ext_control_info *)kc_external_control;
2412      
2413      for (i=0;i<128;i++)
2414            if (temp_ptr->keyboard[i])
2415                         key_putkey (i);
2416
2417      if (temp_ptr->Reactor_blown)
2418       {
2419        if (Game_mode & GM_MULTI)
2420                     net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
2421                  else
2422                          do_controlcen_destroyed_stuff(obj_find_first_of_type (OBJ_CNTRLCEN));
2423            }
2424     }
2425   
2426 }
2427 #endif
2428