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()));
254 if (supported & SDL_HAPTIC_SINE) {
256 memset(&pAfterburn1, 0, sizeof(haptic_effect_t));
258 pAfterburn1.eff.type = SDL_HAPTIC_SINE;
259 pAfterburn1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
260 pAfterburn1.eff.periodic.direction.dir[0] = 0;
261 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
262 pAfterburn1.eff.periodic.period = 20;
263 pAfterburn1.eff.periodic.magnitude = 0x3332;
265 pAfterburn1.id = SDL_HapticNewEffect(haptic, &pAfterburn1.eff);
267 if (pAfterburn1.id < 0) {
268 mprintf((" Afterburn effect 1 failed to load:\n %s\n", SDL_GetError()));
270 pAfterburn1.loaded = 1;
274 memset(&pAfterburn2, 0, sizeof(haptic_effect_t));
276 pAfterburn2.eff.type = SDL_HAPTIC_SINE;
277 pAfterburn2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
278 pAfterburn2.eff.periodic.direction.dir[0] = 9000;
279 pAfterburn2.eff.periodic.length = 125;
280 pAfterburn2.eff.periodic.period = 100;
281 pAfterburn2.eff.periodic.magnitude = 0x1999;
283 pAfterburn2.id = SDL_HapticNewEffect(haptic, &pAfterburn2.eff);
285 if (pAfterburn2.id < 0) {
286 mprintf((" Afterburn effect 2 failed to load:\n %s\n", SDL_GetError()));
288 pAfterburn2.loaded = 1;
293 // if (supported & SDL_HAPTIC_SQUARE) {
294 if (supported & SDL_HAPTIC_TRIANGLE) {
296 memset(&pDock, 0, sizeof(haptic_effect_t));
298 // pDock.eff.type = SDL_HAPTIC_SQUARE;
299 pDock.eff.type = SDL_HAPTIC_TRIANGLE;
300 pDock.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
301 pDock.eff.periodic.direction.dir[0] = 9000;
302 pDock.eff.periodic.length = 125;
303 pDock.eff.periodic.period = 100;
304 pDock.eff.periodic.magnitude = 0x3332;
306 pDock.id = SDL_HapticNewEffect(haptic, &pDock.eff);
309 mprintf((" Dock effect failed to load:\n %s\n", SDL_GetError()));
316 static bool joy_ff_can_play()
318 return (Joy_ff_enabled && Joy_ff_acquired);
321 static void joy_ff_update_effect(haptic_effect_t *eff, const char *name)
323 if ( !eff->loaded ) {
327 if ( SDL_HapticUpdateEffect(haptic, eff->id, &eff->eff) < 0 ) {
328 mprintf(("HapticERROR: Unable to update %s:\n %s\n", name, SDL_GetError()));
332 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name)
334 if ( !eff->loaded ) {
338 // nprintf(("Joystick", "FF: Starting effect %s\n", name));
340 if ( SDL_HapticRunEffect(haptic, eff->id, 1) ) {
341 mprintf(("HapticERROR: Unable to run %s:\n %s\n", name, SDL_GetError()));
345 void joy_ff_stop_effects()
347 if ( !Joy_ff_enabled ) {
351 SDL_HapticStopAll(haptic);
354 void joy_ff_mission_init(vector v)
358 joy_ff_handling_scaler = (int) ((vm_vec_mag(&v) + 1.3f) * 5.0f);
360 Joy_ff_afterburning = 0;
362 joy_ff_adjust_handling(0);
364 if ( !joy_ff_effect_playing(&pSpring) ) {
365 joy_ff_start_effect(&pSpring, "Spring");
368 // reset afterburn effects to default values
370 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
371 pAfterburn1.eff.periodic.period = 20;
372 pAfterburn1.eff.periodic.magnitude = 0x3332;
373 pAfterburn1.eff.periodic.attack_length = 0;
375 joy_ff_update_effect(&pAfterburn1, "pAfterburn1 (init)");
377 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
378 pAfterburn2.eff.periodic.period = 100;
379 pAfterburn2.eff.periodic.magnitude = 0x1999;
380 pAfterburn2.eff.periodic.attack_length = 0;
382 joy_ff_update_effect(&pAfterburn2, "pAfterburn2 (init)");
384 // reset primary shoot effect to default values
386 pShootEffect.eff.periodic.direction.dir[0] = 0;
387 pShootEffect.eff.periodic.length = 160;
388 pShootEffect.eff.periodic.fade_length = 120;
390 joy_ff_update_effect(&pShootEffect, "pShootEffect (init)");
393 void joy_reacquire_ff()
395 if ( !Joy_ff_enabled ) {
399 if (Joy_ff_acquired) {
403 joy_ff_start_effect(&pSpring, "Spring");
408 void joy_unacquire_ff()
410 if ( !Joy_ff_enabled ) {
414 if ( !Joy_ff_acquired ) {
418 joy_ff_stop_effects();
423 void joy_ff_play_vector_effect(vector *v, float scaler)
428 if ( !joy_ff_can_play() ) {
432 // nprintf(("Joystick", "FF: vec = { %f, %f, %f } s = %f\n", v->xyz.x, v->xyz.y, v->xyz.z, scaler));
433 vm_vec_copy_scale(&vf, v, scaler);
437 if (vf.xyz.y + vf.xyz.z < 0.0f) {
438 y = -vm_vec_mag(&vf);
443 joy_ff_play_dir_effect(-x, -y);
446 void joy_ff_play_dir_effect(float x, float y)
451 if ( !joy_ff_can_play() ) {
455 // allow for at least one of the effects to work
456 if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) {
460 if (joy_ff_effect_playing(&pHitEffect1) || joy_ff_effect_playing(&pHitEffect2)) {
461 nprintf(("Joystick", "FF: HitEffect already playing. Skipping\n"));
465 if (Joy_ff_directional_hit_effect_enabled) {
468 } else if (x < -8000.0f) {
474 } else if (y < -8000.0f) {
478 imag = (int) fl_sqrt(x * x + y * y);
483 degs = (float)atan2(x, y);
484 idegs = (int) (degs * 18000.0f / PI) + 90;
489 while (idegs >= 36000) {
493 if (pHitEffect1.loaded) {
494 pHitEffect1.eff.constant.direction.dir[0] = idegs;
495 pHitEffect1.eff.constant.level = (Sint16)(32767.0f * (imag / 10000.0f));
497 joy_ff_update_effect(&pHitEffect1, "pHitEffect1");
504 if (pHitEffect2.loaded) {
505 pHitEffect2.eff.periodic.direction.dir[0] = idegs;
506 pHitEffect2.eff.periodic.magnitude = (Sint16)(32767.0f * (imag / 10000.0f));
508 joy_ff_update_effect(&pHitEffect2, "pHitEffect2");
512 joy_ff_start_effect(&pHitEffect1, "HitEffect1");
513 joy_ff_start_effect(&pHitEffect2, "HitEffect2");
516 void joy_ff_play_primary_shoot(int gain)
518 if ( !joy_ff_can_play() ) {
522 if ( !pShootEffect.loaded && !Joy_rumble ) {
528 if (pShootEffect.loaded) {
529 SDL_HapticStopEffect(haptic, pShootEffect.id);
531 static int primary_ff_level = 10000;
533 if (gain != primary_ff_level) {
534 pShootEffect.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
536 joy_ff_update_effect(&pShootEffect, "pShootEffect");
538 primary_ff_level = gain;
541 joy_ff_start_effect(&pShootEffect, "ShootEffect");
542 } else if (Joy_rumble) {
543 SDL_HapticRumblePlay(haptic, gain / 10000.0f, 100);
547 void joy_ff_play_secondary_shoot(int gain)
549 if ( !joy_ff_can_play() ) {
553 if ( !pSecShootEffect.loaded && !Joy_rumble ) {
557 gain = gain * 100 + 2500;
561 if (pSecShootEffect.loaded) {
562 SDL_HapticStopEffect(haptic, pSecShootEffect.id);
564 static int secondary_ff_level = 10000;
566 if (gain != secondary_ff_level) {
567 pSecShootEffect.eff.constant.level = (Sint16)(32767.0f * (gain / 10000.0f));
568 pSecShootEffect.eff.constant.length = (150000 + gain * 25) / 1000;
570 joy_ff_update_effect(&pSecShootEffect, "pSecShootEffect");
572 secondary_ff_level = gain;
573 nprintf(("Joystick", "FF: Secondary force = 0x%04x\n", pSecShootEffect.eff.constant.level));
576 joy_ff_start_effect(&pSecShootEffect, "SecShootEffect");
577 } else if (Joy_rumble) {
578 SDL_HapticRumblePlay(haptic, gain / 10000.0f, (150000 + gain * 25) / 1000);
582 void joy_ff_adjust_handling(int speed)
587 if ( !joy_ff_can_play() ) {
591 if ( !pSpring.loaded ) {
595 static int last_speed = -1000;
597 if (speed == last_speed) {
603 v = speed * joy_ff_handling_scaler * 2 / 3;
604 // v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
605 v += joy_ff_handling_scaler * 45 - 500;
609 coeff = (short)(32767.0f * (v / 10000.0f));
611 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
612 pSpring.eff.condition.right_coeff[i] = coeff;
613 pSpring.eff.condition.left_coeff[i] = coeff;
616 // nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
618 joy_ff_update_effect(&pSpring, "pSpring");
621 static int joy_ff_effect_playing(haptic_effect_t *eff)
623 if ( !eff->loaded ) {
626 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
632 if ( !joy_ff_can_play() ) {
636 if ( !pDock.loaded ) {
640 SDL_HapticStopEffect(haptic, pDock.id);
642 pDock.eff.periodic.magnitude = 0x3332;
644 joy_ff_update_effect(&pDock, "pDock");
646 joy_ff_start_effect(&pDock, "Dock");
649 void joy_ff_play_reload_effect()
651 if ( !joy_ff_can_play() ) {
655 if ( !pDock.loaded ) {
659 SDL_HapticStopEffect(haptic, pDock.id);
661 pDock.eff.periodic.magnitude = 0x1999;
663 joy_ff_update_effect(&pDock, "pDock (reload)");
665 joy_ff_start_effect(&pDock, "Dock (Reload)");
668 void joy_ff_afterburn_on()
670 if ( !joy_ff_can_play() ) {
674 if (Joy_ff_afterburning) {
678 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
682 SDL_HapticStopEffect(haptic, pAfterburn1.id);
684 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
685 pAfterburn1.eff.periodic.magnitude = 0x3332;
687 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
689 SDL_HapticStopEffect(haptic, pAfterburn2.id);
691 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
692 pAfterburn2.eff.periodic.magnitude = 0x1999;
694 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
696 joy_ff_start_effect(&pAfterburn1, "Afterburn1");
697 joy_ff_start_effect(&pAfterburn2, "Afterburn2");
699 // nprintf(("Joystick", "FF: Afterburn started\n"));
701 Joy_ff_afterburning = 1;
704 void joy_ff_afterburn_off()
706 if ( !joy_ff_can_play() ) {
710 if ( !Joy_ff_afterburning ) {
714 if (pAfterburn1.loaded) {
715 SDL_HapticStopEffect(haptic, pAfterburn1.id);
718 if (pAfterburn2.loaded) {
719 SDL_HapticStopEffect(haptic, pAfterburn2.id);
722 Joy_ff_afterburning = 0;
724 // nprintf(("Joystick", "FF: Afterburn stopped\n"));
727 void joy_ff_explode()
729 if ( !joy_ff_can_play() ) {
733 if (pAfterburn1.loaded) {
734 SDL_HapticStopEffect(haptic, pAfterburn1.id);
737 if (pAfterburn2.loaded) {
738 SDL_HapticStopEffect(haptic, pAfterburn2.id);
741 if (pShootEffect.loaded) {
742 SDL_HapticStopEffect(haptic, pShootEffect.id);
744 pShootEffect.eff.periodic.direction.dir[0] = 9000;
745 pShootEffect.eff.periodic.length = 500;
746 pShootEffect.eff.periodic.magnitude = 0x7FFF;
747 pShootEffect.eff.periodic.fade_length = 500;
749 joy_ff_update_effect(&pShootEffect, "pShootEffect (explode)");
751 joy_ff_start_effect(&pShootEffect, "ShootEffect (Explode)");
752 } else if (Joy_rumble) {
753 SDL_HapticRumblePlay(haptic, 1.0f, 500);
756 Joy_ff_afterburning = 0;
759 void joy_ff_fly_by(int mag)
763 if ( !joy_ff_can_play() ) {
767 if (Joy_ff_afterburning) {
771 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
775 gain = mag * 120 + 4000;
779 SDL_HapticStopEffect(haptic, pAfterburn1.id);
781 pAfterburn1.eff.periodic.length = (6000 * mag + 400000) / 1000;
782 pAfterburn1.eff.periodic.magnitude = (Sint16)(26212.0f * (gain / 10000.0f));
784 joy_ff_update_effect(&pAfterburn1, "pAfterburn1 (flyby)");
786 SDL_HapticStopEffect(haptic, pAfterburn2.id);
788 pAfterburn2.eff.periodic.length = (6000 * mag + 400000) / 1000;
789 pAfterburn2.eff.periodic.magnitude = (Sint16)(13106.0f * (gain / 10000.0f));
791 joy_ff_update_effect(&pAfterburn2, "pAfterburn2 (flyby)");
793 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Fly by)");
794 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Fly by)");
797 void joy_ff_deathroll()
799 if ( !joy_ff_can_play() ) {
803 if (pAfterburn1.loaded && pAfterburn2.loaded) {
804 SDL_HapticStopEffect(haptic, pAfterburn1.id);
806 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
807 pAfterburn1.eff.periodic.period = 200;
808 pAfterburn1.eff.periodic.magnitude = 0x7FFF;
809 pAfterburn1.eff.periodic.attack_length = 200;
811 joy_ff_update_effect(&pAfterburn1, "pAfterburn1 (deathroll)");
813 SDL_HapticStopEffect(haptic, pAfterburn2.id);
815 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
816 pAfterburn2.eff.periodic.period = 200;
817 pAfterburn2.eff.periodic.magnitude = 0x7FFF;
818 pAfterburn2.eff.periodic.attack_length = 200;
820 joy_ff_update_effect(&pAfterburn2, "pAfterburn2 (deathroll)");
822 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Death Roll)");
823 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Death Roll)");
825 Joy_ff_afterburning = 1;