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