2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on the
13 #include "osregistry.h"
18 static int Joy_ff_enabled = 0;
19 static int Joy_ff_acquired = 0;
20 static SDL_Haptic *haptic = NULL;
21 static int joy_ff_handling_scaler = 0;
22 static int Joy_ff_directional_hit_effect_enabled = 1;
23 static int Joy_rumble = 0;
24 static int Joy_ff_afterburning = 0;
32 static haptic_effect_t pHitEffect1;
33 static haptic_effect_t pHitEffect2;
34 static haptic_effect_t pAfterburn1;
35 static haptic_effect_t pAfterburn2;
36 static haptic_effect_t pShootEffect;
37 static haptic_effect_t pSecShootEffect;
38 static haptic_effect_t pSpring;
39 static haptic_effect_t pDock;
41 static void joy_ff_create_effects();
42 static int joy_ff_effect_playing(haptic_effect_t *eff);
43 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name);
45 extern SDL_Joystick *sdljoy;
52 ff_enabled = os_config_read_uint("Controls", "EnableJoystickFF", 0);
54 if ( !ff_enabled || !SDL_JoystickIsHaptic(sdljoy) ) {
58 if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
59 mprintf((" ERROR: Unable to initialize haptic subsystem\n"));
63 haptic = SDL_HapticOpenFromJoystick(sdljoy);
66 mprintf((" ERROR: Unable to open haptic joystick\n"));
67 SDL_QuitSubSystem(SDL_INIT_HAPTIC);
71 if ( SDL_HapticRumbleSupported(haptic) ) {
72 SDL_HapticRumbleInit(haptic);
76 mprintf((" Rumble : %s\n", Joy_rumble ? "Yes" : "No"));
77 mprintf((" Axes : %d\n", SDL_HapticNumAxes(haptic)));
78 mprintf((" Max effects : %d\n", SDL_HapticNumEffects(haptic)));
79 mprintf((" Running effects : %d\n", SDL_HapticNumEffectsPlaying(haptic)));
81 joy_ff_create_effects();
86 Joy_ff_directional_hit_effect_enabled = os_config_read_uint("Controls", "EnableHitEffect", 1);
91 void joy_ff_shutdown()
93 if ( !Joy_ff_enabled ) {
99 SDL_HapticClose(haptic);
102 SDL_QuitSubSystem(SDL_INIT_HAPTIC);
108 static void joy_ff_create_effects()
110 // clear all SDL errors
113 unsigned int supported = 0;
115 supported = SDL_HapticQuery(haptic);
117 if ( !(supported & SDL_HAPTIC_CONSTANT) ) {
118 mprintf((" Constant Force : not supported\n"));
121 if ( !(supported & SDL_HAPTIC_SINE) ) {
122 mprintf((" Sine Wave : not supported\n"));
125 if ( !(supported & SDL_HAPTIC_SAWTOOTHDOWN) ) {
126 mprintf((" Sawtooth Down : not supported\n"));
129 if ( !(supported & SDL_HAPTIC_SPRING) ) {
130 mprintf((" Spring : not supported\n"));
133 if ( !(supported & SDL_HAPTIC_SQUARE) ) {
134 mprintf((" Square : not supported\n"));
137 if ( !(supported & SDL_HAPTIC_TRIANGLE) ) {
138 mprintf((" Triangle : not supported\n"));
142 if (supported & SDL_HAPTIC_CONSTANT) {
144 memset(&pHitEffect1, 0, sizeof(haptic_effect_t));
146 pHitEffect1.eff.type = SDL_HAPTIC_CONSTANT;
147 pHitEffect1.eff.constant.direction.type = SDL_HAPTIC_POLAR;
148 pHitEffect1.eff.constant.direction.dir[0] = 0;
149 pHitEffect1.eff.constant.length = 300;
150 pHitEffect1.eff.constant.level = 0x7FFF;
151 pHitEffect1.eff.constant.attack_length = 0;
152 pHitEffect1.eff.constant.attack_level = 0x7FFF;
153 pHitEffect1.eff.constant.fade_length = 120;
154 pHitEffect1.eff.constant.fade_level = 1;
156 pHitEffect1.id = SDL_HapticNewEffect(haptic, &pHitEffect1.eff);
158 if (pHitEffect1.id < 0) {
159 mprintf((" Hit effect 1 failed to load:\n %s\n", SDL_GetError()));
161 pHitEffect1.loaded = 1;
165 if (supported & SDL_HAPTIC_SINE) {
167 memset(&pHitEffect2, 0, sizeof(haptic_effect_t));
169 pHitEffect2.eff.type = SDL_HAPTIC_SINE;
170 pHitEffect2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
171 pHitEffect2.eff.periodic.direction.dir[0] = 9000;
172 pHitEffect2.eff.periodic.length = 300;
173 pHitEffect2.eff.periodic.period = 100;
174 pHitEffect2.eff.periodic.magnitude = 0x7FFF;
175 pHitEffect2.eff.periodic.attack_length = 100;
176 pHitEffect2.eff.periodic.fade_length = 100;
178 pHitEffect2.id = SDL_HapticNewEffect(haptic, &pHitEffect2.eff);
180 if (pHitEffect2.id < 0) {
181 mprintf((" Hit effect 2 failed to load:\n %s\n", SDL_GetError()));
183 pHitEffect2.loaded = 1;
187 if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
189 memset(&pShootEffect, 0, sizeof(haptic_effect_t));
191 pShootEffect.eff.type = SDL_HAPTIC_SAWTOOTHDOWN;
192 pShootEffect.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
193 pShootEffect.eff.periodic.direction.dir[0] = 0;
194 pShootEffect.eff.periodic.length = 160;
195 pShootEffect.eff.periodic.period = 20;
196 pShootEffect.eff.periodic.magnitude = 0x7FFF;
197 pShootEffect.eff.periodic.fade_length = 120;
199 pShootEffect.id = SDL_HapticNewEffect(haptic, &pShootEffect.eff);
201 if (pShootEffect.id < 0) {
202 mprintf((" Fire primary effect failed to load:\n %s\n", SDL_GetError()));
204 pShootEffect.loaded = 1;
208 if (supported & SDL_HAPTIC_CONSTANT) {
210 memset(&pSecShootEffect, 0, sizeof(haptic_effect_t));
212 pSecShootEffect.eff.type = SDL_HAPTIC_CONSTANT;
213 pSecShootEffect.eff.constant.direction.type = SDL_HAPTIC_POLAR;
214 pSecShootEffect.eff.constant.direction.dir[0] = 0;
215 pSecShootEffect.eff.constant.length = 200;
216 pSecShootEffect.eff.constant.level = 0x7FFF;
217 pSecShootEffect.eff.constant.attack_length = 50;
218 pSecShootEffect.eff.constant.attack_level = 0x7FFF;
219 pSecShootEffect.eff.constant.fade_length = 100;
220 pSecShootEffect.eff.constant.fade_level = 1;
222 pSecShootEffect.id = SDL_HapticNewEffect(haptic, &pSecShootEffect.eff);
224 if (pSecShootEffect.id < 0) {
225 mprintf((" Fire secondary effect failed to load:\n %s\n", SDL_GetError()));
227 pSecShootEffect.loaded = 1;
231 if (supported & SDL_HAPTIC_SPRING) {
233 memset(&pSpring, 0, sizeof(haptic_effect_t));
235 pSpring.eff.type = SDL_HAPTIC_SPRING;
236 pSpring.eff.condition.length = SDL_HAPTIC_INFINITY;
238 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
239 pSpring.eff.condition.right_sat[i] = 0x7FFF;
240 pSpring.eff.condition.left_sat[i] = 0x7FFF;
241 pSpring.eff.condition.right_coeff[i] = 0x147;
242 pSpring.eff.condition.left_coeff[i] = 0x147;
245 pSpring.id = SDL_HapticNewEffect(haptic, &pSpring.eff);
247 if (pSpring.id < 0) {
248 mprintf((" Spring effect failed to load:\n %s\n", SDL_GetError()));
251 joy_ff_start_effect(&pSpring, "Spring");
255 if (supported & SDL_HAPTIC_SINE) {
257 memset(&pAfterburn1, 0, sizeof(haptic_effect_t));
259 pAfterburn1.eff.type = SDL_HAPTIC_SINE;
260 pAfterburn1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
261 pAfterburn1.eff.periodic.direction.dir[0] = 0;
262 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
263 pAfterburn1.eff.periodic.period = 20;
264 pAfterburn1.eff.periodic.magnitude = 0x3332;
266 pAfterburn1.id = SDL_HapticNewEffect(haptic, &pAfterburn1.eff);
268 if (pAfterburn1.id < 0) {
269 mprintf((" Afterburn effect 1 failed to load:\n %s\n", SDL_GetError()));
271 pAfterburn1.loaded = 1;
275 memset(&pAfterburn2, 0, sizeof(haptic_effect_t));
277 pAfterburn2.eff.type = SDL_HAPTIC_SINE;
278 pAfterburn2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
279 pAfterburn2.eff.periodic.direction.dir[0] = 9000;
280 pAfterburn2.eff.periodic.length = 125;
281 pAfterburn2.eff.periodic.period = 100;
282 pAfterburn2.eff.periodic.magnitude = 0x1999;
284 pAfterburn2.id = SDL_HapticNewEffect(haptic, &pAfterburn2.eff);
286 if (pAfterburn2.id < 0) {
287 mprintf((" Afterburn effect 2 failed to load:\n %s\n", SDL_GetError()));
289 pAfterburn2.loaded = 1;
294 // if (supported & SDL_HAPTIC_SQUARE) {
295 if (supported & SDL_HAPTIC_TRIANGLE) {
297 memset(&pDock, 0, sizeof(haptic_effect_t));
299 // pDock.eff.type = SDL_HAPTIC_SQUARE;
300 pDock.eff.type = SDL_HAPTIC_TRIANGLE;
301 pDock.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
302 pDock.eff.periodic.direction.dir[0] = 9000;
303 pDock.eff.periodic.length = 125;
304 pDock.eff.periodic.period = 100;
305 pDock.eff.periodic.magnitude = 0x3332;
307 pDock.id = SDL_HapticNewEffect(haptic, &pDock.eff);
310 mprintf((" Dock effect failed to load:\n %s\n", SDL_GetError()));
317 static bool joy_ff_can_play()
319 return (Joy_ff_enabled && Joy_ff_acquired);
322 static void joy_ff_update_effect(haptic_effect_t *eff, const char *name)
324 if ( !eff->loaded ) {
328 if ( SDL_HapticUpdateEffect(haptic, eff->id, &eff->eff) < 0 ) {
329 mprintf(("HapticERROR: Unable to update %s:\n %s\n", name, SDL_GetError()));
333 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name)
335 if ( !eff->loaded ) {
339 // nprintf(("Joystick", "FF: Starting effect %s\n", name));
341 if ( SDL_HapticRunEffect(haptic, eff->id, 1) ) {
342 mprintf(("HapticERROR: Unable to run %s:\n %s\n", name, SDL_GetError()));
346 void joy_ff_stop_effects()
348 if ( !Joy_ff_enabled ) {
352 SDL_HapticStopAll(haptic);
355 void joy_ff_mission_init(vector v)
359 joy_ff_handling_scaler = (int) ((vm_vec_mag(&v) + 1.3f) * 5.0f);
361 Joy_ff_afterburning = 0;
363 joy_ff_adjust_handling(0);
365 if ( !joy_ff_effect_playing(&pSpring) ) {
366 joy_ff_start_effect(&pSpring, "Spring");
369 // reset afterburn effects to default values
371 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
372 pAfterburn1.eff.periodic.period = 20;
373 pAfterburn1.eff.periodic.magnitude = 0x3332;
374 pAfterburn1.eff.periodic.attack_length = 0;
376 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
378 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
379 pAfterburn2.eff.periodic.period = 100;
380 pAfterburn2.eff.periodic.magnitude = 0x1999;
381 pAfterburn2.eff.periodic.attack_length = 0;
383 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
385 // reset primary shoot effect to default values
387 pShootEffect.eff.periodic.direction.dir[0] = 0;
388 pShootEffect.eff.periodic.length = 160;
389 pShootEffect.eff.periodic.fade_length = 120;
391 joy_ff_update_effect(&pShootEffect, "pShootEffect");
394 void joy_reacquire_ff()
396 if ( !Joy_ff_enabled ) {
400 if (Joy_ff_acquired) {
404 joy_ff_start_effect(&pSpring, "Spring");
409 void joy_unacquire_ff()
411 if ( !Joy_ff_enabled ) {
415 if ( !Joy_ff_acquired ) {
419 joy_ff_stop_effects();
424 void joy_ff_play_vector_effect(vector *v, float scaler)
429 if ( !joy_ff_can_play() ) {
433 // nprintf(("Joystick", "FF: vec = { %f, %f, %f } s = %f\n", v->xyz.x, v->xyz.y, v->xyz.z, scaler));
434 vm_vec_copy_scale(&vf, v, scaler);
438 if (vf.xyz.y + vf.xyz.z < 0.0f) {
439 y = -vm_vec_mag(&vf);
444 joy_ff_play_dir_effect(-x, -y);
447 void joy_ff_play_dir_effect(float x, float y)
452 if ( !joy_ff_can_play() ) {
456 // allow for at least one of the effects to work
457 if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) {
461 if (joy_ff_effect_playing(&pHitEffect1) || joy_ff_effect_playing(&pHitEffect2)) {
462 nprintf(("Joystick", "FF: HitEffect already playing. Skipping\n"));
466 if (Joy_ff_directional_hit_effect_enabled) {
469 } else if (x < -8000.0f) {
475 } else if (y < -8000.0f) {
479 imag = (int) fl_sqrt(x * x + y * y);
484 degs = (float)atan2(x, y);
485 idegs = (int) (degs * 18000.0f / PI) + 90;
490 while (idegs >= 36000) {
494 if (pHitEffect1.loaded) {
495 pHitEffect1.eff.constant.direction.dir[0] = idegs;
496 pHitEffect1.eff.constant.level = (Sint16)(32767.0f * (imag / 10000.0f));
498 joy_ff_update_effect(&pHitEffect1, "pHitEffect1");
505 if (pHitEffect2.loaded) {
506 pHitEffect2.eff.periodic.direction.dir[0] = idegs;
507 pHitEffect2.eff.periodic.magnitude = (Sint16)(32767.0f * (imag / 10000.0f));
509 joy_ff_update_effect(&pHitEffect2, "pHitEffect2");
513 joy_ff_start_effect(&pHitEffect1, "HitEffect1");
514 joy_ff_start_effect(&pHitEffect2, "HitEffect2");
517 void joy_ff_play_primary_shoot(int gain)
519 if ( !joy_ff_can_play() ) {
523 if ( !pShootEffect.loaded && !Joy_rumble ) {
529 } else if (gain > 10000) {
533 if (pShootEffect.loaded) {
534 SDL_HapticStopEffect(haptic, pShootEffect.id);
536 static int primary_ff_level = 10000;
538 if (gain != primary_ff_level) {
539 pShootEffect.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
541 joy_ff_update_effect(&pShootEffect, "pShootEffect");
543 primary_ff_level = gain;
546 joy_ff_start_effect(&pShootEffect, "ShootEffect");
547 } else if (Joy_rumble) {
548 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, 100);
552 void joy_ff_play_secondary_shoot(int gain)
554 if ( !joy_ff_can_play() ) {
558 if ( !pSecShootEffect.loaded && !Joy_rumble ) {
562 gain = gain * 100 + 2500;
566 } else if (gain > 10000) {
570 if (pSecShootEffect.loaded) {
571 SDL_HapticStopEffect(haptic, pSecShootEffect.id);
573 static int secondary_ff_level = 10000;
575 if (gain != secondary_ff_level) {
576 pSecShootEffect.eff.constant.level = (Sint16)(32767.0f * (gain / 10000.0f));
577 pSecShootEffect.eff.constant.length = (150000 + gain * 25) / 1000;
579 joy_ff_update_effect(&pSecShootEffect, "pSecShootEffect");
581 secondary_ff_level = gain;
582 nprintf(("Joystick", "FF: Secondary force = 0x%04x\n", pSecShootEffect.eff.constant.level));
585 joy_ff_start_effect(&pSecShootEffect, "SecShootEffect");
586 } else if (Joy_rumble) {
587 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, (150000 + gain * 25) / 1000);
591 void joy_ff_adjust_handling(int speed)
596 if ( !joy_ff_can_play() ) {
600 if ( !pSpring.loaded ) {
604 static int last_speed = -1000;
606 if (speed == last_speed) {
612 v = speed * joy_ff_handling_scaler * 2 / 3;
613 // v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
614 v += joy_ff_handling_scaler * 45 - 500;
618 } else if (v > 10000) {
622 coeff = (short)(32767.0f * (v / 10000.0f));
624 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
625 pSpring.eff.condition.right_coeff[i] = coeff;
626 pSpring.eff.condition.left_coeff[i] = coeff;
629 // nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
631 joy_ff_update_effect(&pSpring, "pSpring");
634 static int joy_ff_effect_playing(haptic_effect_t *eff)
636 if ( !eff->loaded ) {
639 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
645 if ( !joy_ff_can_play() ) {
649 if ( !pDock.loaded ) {
653 SDL_HapticStopEffect(haptic, pDock.id);
655 pDock.eff.periodic.magnitude = 0x3332;
657 joy_ff_update_effect(&pDock, "pDock");
659 joy_ff_start_effect(&pDock, "Dock");
662 void joy_ff_play_reload_effect()
664 if ( !joy_ff_can_play() ) {
668 if ( !pDock.loaded ) {
672 SDL_HapticStopEffect(haptic, pDock.id);
674 pDock.eff.periodic.magnitude = 0x1999;
676 joy_ff_update_effect(&pDock, "pDock");
678 joy_ff_start_effect(&pDock, "Dock (Reload)");
681 void joy_ff_afterburn_on()
683 if ( !joy_ff_can_play() ) {
687 if (Joy_ff_afterburning) {
691 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
695 SDL_HapticStopEffect(haptic, pAfterburn1.id);
697 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
698 pAfterburn1.eff.periodic.magnitude = 0x3332;
700 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
702 SDL_HapticStopEffect(haptic, pAfterburn2.id);
704 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
705 pAfterburn2.eff.periodic.magnitude = 0x1999;
707 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
709 joy_ff_start_effect(&pAfterburn1, "Afterburn1");
710 joy_ff_start_effect(&pAfterburn2, "Afterburn2");
712 // nprintf(("Joystick", "FF: Afterburn started\n"));
714 Joy_ff_afterburning = 1;
717 void joy_ff_afterburn_off()
719 if ( !joy_ff_can_play() ) {
723 if ( !Joy_ff_afterburning ) {
727 if (pAfterburn1.loaded) {
728 SDL_HapticStopEffect(haptic, pAfterburn1.id);
731 if (pAfterburn2.loaded) {
732 SDL_HapticStopEffect(haptic, pAfterburn2.id);
735 Joy_ff_afterburning = 0;
737 // nprintf(("Joystick", "FF: Afterburn stopped\n"));
740 void joy_ff_explode()
742 if ( !joy_ff_can_play() ) {
746 if (pAfterburn1.loaded) {
747 SDL_HapticStopEffect(haptic, pAfterburn1.id);
750 if (pAfterburn2.loaded) {
751 SDL_HapticStopEffect(haptic, pAfterburn2.id);
754 if (pShootEffect.loaded) {
755 SDL_HapticStopEffect(haptic, pShootEffect.id);
757 pShootEffect.eff.periodic.direction.dir[0] = 9000;
758 pShootEffect.eff.periodic.length = 500;
759 pShootEffect.eff.periodic.magnitude = 0x7FFF;
760 pShootEffect.eff.periodic.fade_length = 500;
762 joy_ff_update_effect(&pShootEffect, "pShootEffect");
764 joy_ff_start_effect(&pShootEffect, "ShootEffect (Explode)");
765 } else if (Joy_rumble) {
766 SDL_HapticRumblePlay(haptic, 0.75f, 500);
769 Joy_ff_afterburning = 0;
772 void joy_ff_fly_by(int mag)
776 if ( !joy_ff_can_play() ) {
780 if (Joy_ff_afterburning) {
784 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
788 gain = mag * 120 + 4000;
792 } else if (gain > 10000) {
796 SDL_HapticStopEffect(haptic, pAfterburn1.id);
798 pAfterburn1.eff.periodic.length = (6000 * mag + 400000) / 1000;
799 pAfterburn1.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
801 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
803 SDL_HapticStopEffect(haptic, pAfterburn2.id);
805 pAfterburn2.eff.periodic.length = (6000 * mag + 400000) / 1000;
806 pAfterburn2.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
808 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
810 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Fly by)");
811 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Fly by)");
814 void joy_ff_deathroll()
816 if ( !joy_ff_can_play() ) {
820 if (pAfterburn1.loaded && pAfterburn2.loaded) {
821 SDL_HapticStopEffect(haptic, pAfterburn1.id);
823 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
824 pAfterburn1.eff.periodic.period = 200;
825 pAfterburn1.eff.periodic.magnitude = 0x7FFF;
826 pAfterburn1.eff.periodic.attack_length = 200;
828 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
830 SDL_HapticStopEffect(haptic, pAfterburn2.id);
832 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
833 pAfterburn2.eff.periodic.period = 200;
834 pAfterburn2.eff.periodic.magnitude = 0x7FFF;
835 pAfterburn2.eff.periodic.attack_length = 200;
837 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
839 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Death Roll)");
840 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Death Roll)");
842 Joy_ff_afterburning = 1;