Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
flyjoystick.cpp
Go to the documentation of this file.
1 #include "in_joystick.h"
2 
3 #include "flyjoystick.h"
4 #include "firekeyboard.h"
5 #include "flykeyboard.h"
6 #include "vs_globals.h"
7 #include "config_xml.h"
8 #include "in_kb_data.h"
9 
10 FlyByJoystick::FlyByJoystick( unsigned int configfile ) : FlyByKeyboard( configfile )
11 {
12  for (int i = 0; i < MAX_JOYSTICKS; i++)
13  if ( (unsigned int) joystick[i]->player == configfile )
14  whichjoystick.push_back( i );
15  //remember keybindings from config file?
16 
17  //this below is outdated
18 #if 0
19  //why does the compiler not allow this?//check out my queued events section in firekeyboard.cpp
20  BindButton( 0, FireKeyboard::FireKey );
21  BindButton( 1, FireKeyboard::MissileKey );
22 #endif
23 }
24 
25 #if 0
26 void FlyByJoystick::JShelt( KBSTATE k, float, float, int )
27 {
28  if (k == DOWN) {
29  FlyByKeyboard::SheltonKey( std::string(), DOWN );
30  FlyByKeyboard::SheltonKey( std::string(), DOWN );
31  FlyByKeyboard::SheltonKey( std::string(), DOWN );
32  }
33  if (k == UP) {}
34 }
35 void FlyByJoystick::JAB( KBSTATE k, float, float, int )
36 {
37  if (k == PRESS) {
38  FlyByKeyboard::ABKey( std::string(), PRESS );
39  FlyByKeyboard::ABKey( std::string(), DOWN );
40  }
41  if (k == DOWN)
42  FlyByKeyboard::ABKey( std::string(), DOWN );
43 }
44 
45 void FlyByJoystick::JAccelKey( KBSTATE k, float, float, int )
46 {
47  FlyByKeyboard::AccelKey( std::string(), k );
48 }
49 void FlyByJoystick::JDecelKey( KBSTATE k, float, float, int )
50 {
51  FlyByKeyboard::DecelKey( std::string(), k );
52 }
53 
54 #endif
55 
57 {
58  static bool clamp_joystick_axes = XMLSupport::parse_bool( vs_config->getVariable( "joystick", "clamp_axes", "true" ) );
59  static bool nonlinear_throttle_nav =
60  XMLSupport::parse_bool( vs_config->getVariable( "joystick", "nonlinear_throttle_nav", "true" ) );
61  static bool nonlinear_throttle_combat =
62  XMLSupport::parse_bool( vs_config->getVariable( "joystick", "nonlinear_throttle_combat", "false" ) );
63  static float expfactorn = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_expfactor_nav", "6.0" ) );
64  static float pfactorn = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_pfactor_nav", "2.0" ) );
65  static float expamountn = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_expamount_nav", "1.0" ) );
66  static float pamountn = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_pamount_nav", "0.0" ) );
67  static float expfactorc = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_expfactor_combat", "6.0" ) );
68  static float pfactorc = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_pfactor_combat", "2.0" ) );
69  static float expamountc = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_expamount_combat", "1.0" ) );
70  static float pamountc = XMLSupport::parse_float( vs_config->getVariable( "joystick", "nonlinear_pamount_combat", "0.0" ) );
71  desired_ang_velocity = Vector( 0, 0, 0 );
72  for (unsigned int i = 0; i < this->whichjoystick.size(); i++) {
73  int which_joystick = this->whichjoystick[i];
74  if (which_joystick < MAX_JOYSTICKS) {
75  int joy_nr;
76 
77  joy_nr = vs_config->axis_joy[AXIS_Y];
78  if (joy_nr != -1)
79  joy_nr = which_joystick;
80  if ( joy_nr != -1 && joystick[joy_nr]->isAvailable() ) {
81  int config_axis = joystick[joy_nr]->axis_axis[AXIS_Y];
82  if (config_axis != -1) {
83  bool inverse = joystick[joy_nr]->axis_inverse[AXIS_Y];
84  float axis_value = -joystick[joy_nr]->joy_axis[config_axis];
85  if (inverse) axis_value = -axis_value;
86  if (clamp_joystick_axes) {
87  if (axis_value < -1)
88  axis_value = -1;
89  if (axis_value > 1)
90  axis_value = 1;
91  }
93  Up( axis_value );
94 
95  else if (joy_mode&joyModeInertialXY)
96  DirectThrustUp( axis_value );
97 
98  else if (joy_mode&joyModeInertialXZ)
99  DirectThrustFront( -axis_value );
100  }
101  }
102  joy_nr = vs_config->axis_joy[AXIS_X];
103  if (joy_nr != -1)
104  joy_nr = which_joystick;
105  if ( joy_nr != -1 && joystick[joy_nr]->isAvailable() ) {
106  int config_axis = joystick[joy_nr]->axis_axis[AXIS_X];
107  if (config_axis != -1) {
108  bool inverse = joystick[joy_nr]->axis_inverse[AXIS_X];
109  float axis_value = -joystick[joy_nr]->joy_axis[config_axis];
110  if (inverse) axis_value = -axis_value;
111  if (clamp_joystick_axes) {
112  if (axis_value < -1)
113  axis_value = -1;
114  if (axis_value > 1)
115  axis_value = 1;
116  }
117  if (joy_mode == joyModeNormal)
118  Right( axis_value );
119 
120  else if (joy_mode&joyModeRoll)
121  RollRight( -axis_value );
122 
124  DirectThrustRight( axis_value );
125 
126  else if (joy_mode&joyModeBank)
127  Right( axis_value ), RollRight( -axis_value );
128  }
129  }
130  joy_nr = vs_config->axis_joy[AXIS_Z];
131  if (joy_nr != -1)
132  joy_nr = which_joystick;
133  if ( joy_nr != -1 && joystick[joy_nr]->isAvailable() ) {
134  int config_axis = joystick[joy_nr]->axis_axis[AXIS_Z];
135  if (config_axis != -1) {
136  bool inverse = joystick[joy_nr]->axis_inverse[AXIS_Z];
137  float axis_value = -joystick[joy_nr]->joy_axis[config_axis];
138  if (inverse) axis_value = -axis_value;
139  if (clamp_joystick_axes) {
140  if (axis_value < -1)
141  axis_value = -1;
142  if (axis_value > 1)
143  axis_value = 1;
144  }
145  if ( (joy_mode == joyModeNormal) || (joy_mode&joyModeBank) )
146  RollRight( axis_value );
147 
148  else if (joy_mode&joyModeRoll)
149  Right( axis_value );
150 
151  else if (joy_mode&joyModeInertialXY)
152  DirectThrustFront( axis_value );
153 
154  else if (joy_mode&joyModeInertialXZ)
155  DirectThrustUp( axis_value );
156  }
157  }
158  joy_nr = vs_config->axis_joy[AXIS_THROTTLE];
159  if (joy_nr != -1)
160  joy_nr = which_joystick;
161  if ( joy_nr != -1 && joystick[joy_nr]->isAvailable() ) {
162  int config_axis = joystick[joy_nr]->axis_axis[AXIS_THROTTLE];
163  if (config_axis != -1) {
164  bool inverse = joystick[joy_nr]->axis_inverse[AXIS_THROTTLE];
165  float axis_value = -joystick[joy_nr]->joy_axis[config_axis];
166  if (inverse) axis_value = -axis_value;
168  if (axis_value > 1)
169  axis_value = 1;
170  if (axis_value < -1)
171  axis_value = -1; //this code due to paolodinca
172 
173  //put axis from 0 to 2
174  axis_value++;
175  //put axis from 0 to 1
176  axis_value = axis_value/2; //thanks!
177  if (nonlinear_throttle_nav && !cpu->combat_mode) {
178  static float norm = float(exp( expfactorn )-1);
179  axis_value =
180  float( expamountn*(exp( expfactorn*axis_value )-1)/norm+pamountn*pow( axis_value, pfactorn ) );
181  } else if (nonlinear_throttle_combat && cpu->combat_mode) {
182  static float norm = float(exp( expfactorc )-1);
183  axis_value =
184  float( expamountc*(exp( expfactorc*axis_value )-1)/norm+pamountc*pow( axis_value, pfactorc ) );
185  }
186  cpu->set_speed = axis_value*cpu->max_speed();
187  desired_velocity = Vector( 0, 0, cpu->set_speed );
188  }
189  }
190  }
191  }
192  FlyByKeyboard::Execute( false );
193 }
195