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;
364 void joy_reacquire_ff()
366 if ( !Joy_ff_enabled ) {
370 if (Joy_ff_acquired) {
374 joy_ff_start_effect(&pSpring, "Spring");
379 void joy_unacquire_ff()
381 if ( !Joy_ff_enabled ) {
385 if ( !Joy_ff_acquired ) {
389 joy_ff_stop_effects();
394 void joy_ff_play_vector_effect(vector *v, float scaler)
399 if ( !joy_ff_can_play() ) {
403 // nprintf(("Joystick", "FF: vec = { %f, %f, %f } s = %f\n", v->xyz.x, v->xyz.y, v->xyz.z, scaler));
404 vm_vec_copy_scale(&vf, v, scaler);
408 if (vf.xyz.y + vf.xyz.z < 0.0f) {
409 y = -vm_vec_mag(&vf);
414 joy_ff_play_dir_effect(-x, -y);
417 void joy_ff_play_dir_effect(float x, float y)
422 if ( !joy_ff_can_play() ) {
426 // allow for at least one of the effects to work
427 if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) {
431 if (joy_ff_effect_playing(&pHitEffect1) || joy_ff_effect_playing(&pHitEffect2)) {
432 nprintf(("Joystick", "FF: HitEffect already playing. Skipping\n"));
436 if (Joy_ff_directional_hit_effect_enabled) {
439 } else if (x < -8000.0f) {
445 } else if (y < -8000.0f) {
449 imag = (int) fl_sqrt(x * x + y * y);
454 degs = (float)atan2(x, y);
455 idegs = (int) (degs * 18000.0f / PI) + 90;
460 while (idegs >= 36000) {
464 if (pHitEffect1.loaded) {
465 pHitEffect1.eff.constant.direction.dir[0] = idegs;
466 pHitEffect1.eff.constant.level = (Sint16)(32767.0f * (imag / 10000.0f));
468 joy_ff_update_effect(&pHitEffect1, "pHitEffect1");
475 if (pHitEffect2.loaded) {
476 pHitEffect2.eff.periodic.direction.dir[0] = idegs;
477 pHitEffect2.eff.periodic.magnitude = (Sint16)(32767.0f * (imag / 10000.0f));
479 joy_ff_update_effect(&pHitEffect2, "pHitEffect2");
483 joy_ff_start_effect(&pHitEffect1, "HitEffect1");
484 joy_ff_start_effect(&pHitEffect2, "HitEffect2");
487 void joy_ff_play_primary_shoot(int gain)
489 if ( !joy_ff_can_play() ) {
493 if ( !pShootEffect.loaded && !Joy_rumble ) {
499 } else if (gain > 10000) {
503 if (pShootEffect.loaded) {
504 SDL_HapticStopEffect(haptic, pShootEffect.id);
506 static int primary_ff_level = 10000;
508 if (gain != primary_ff_level) {
509 pShootEffect.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
511 joy_ff_update_effect(&pShootEffect, "pShootEffect");
513 primary_ff_level = gain;
516 joy_ff_start_effect(&pShootEffect, "ShootEffect");
517 } else if (Joy_rumble) {
518 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, 100);
522 void joy_ff_play_secondary_shoot(int gain)
524 if ( !joy_ff_can_play() ) {
528 if ( !pSecShootEffect.loaded && !Joy_rumble ) {
532 gain = gain * 100 + 2500;
536 } else if (gain > 10000) {
540 if (pSecShootEffect.loaded) {
541 SDL_HapticStopEffect(haptic, pSecShootEffect.id);
543 static int secondary_ff_level = 10000;
545 if (gain != secondary_ff_level) {
546 pSecShootEffect.eff.constant.level = (Sint16)(32767.0f * (gain / 10000.0f));
547 pSecShootEffect.eff.constant.length = (150000 + gain * 25) / 1000;
549 joy_ff_update_effect(&pSecShootEffect, "pSecShootEffect");
551 secondary_ff_level = gain;
552 nprintf(("Joystick", "FF: Secondary force = 0x%04x\n", pSecShootEffect.eff.constant.level));
555 joy_ff_start_effect(&pSecShootEffect, "SecShootEffect");
556 } else if (Joy_rumble) {
557 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, (150000 + gain * 25) / 1000);
561 void joy_ff_adjust_handling(int speed)
566 if ( !joy_ff_can_play() ) {
570 if ( !pSpring.loaded ) {
574 static int last_speed = -1000;
576 if (speed == last_speed) {
582 v = speed * joy_ff_handling_scaler * 2 / 3;
583 // v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
584 v += joy_ff_handling_scaler * 45 - 500;
588 } else if (v > 10000) {
592 coeff = (short)(32767.0f * (v / 10000.0f));
594 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
595 pSpring.eff.condition.right_coeff[i] = coeff;
596 pSpring.eff.condition.left_coeff[i] = coeff;
599 // nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
601 joy_ff_update_effect(&pSpring, "pSpring");
604 static int joy_ff_effect_playing(haptic_effect_t *eff)
606 if ( !eff->loaded ) {
609 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
615 if ( !joy_ff_can_play() ) {
619 if ( !pDock.loaded ) {
623 SDL_HapticStopEffect(haptic, pDock.id);
625 pDock.eff.periodic.magnitude = 0x3332;
627 joy_ff_update_effect(&pDock, "pDock");
629 joy_ff_start_effect(&pDock, "Dock");
632 void joy_ff_play_reload_effect()
634 if ( !joy_ff_can_play() ) {
638 if ( !pDock.loaded ) {
642 SDL_HapticStopEffect(haptic, pDock.id);
644 pDock.eff.periodic.magnitude = 0x1999;
646 joy_ff_update_effect(&pDock, "pDock");
648 joy_ff_start_effect(&pDock, "Dock (Reload)");
651 void joy_ff_afterburn_on()
653 if ( !joy_ff_can_play() ) {
657 if (Joy_ff_afterburning) {
661 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
665 SDL_HapticStopEffect(haptic, pAfterburn1.id);
667 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
668 pAfterburn1.eff.periodic.magnitude = 0x3332;
670 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
672 SDL_HapticStopEffect(haptic, pAfterburn2.id);
674 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
675 pAfterburn2.eff.periodic.magnitude = 0x1999;
677 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
679 joy_ff_start_effect(&pAfterburn1, "Afterburn1");
680 joy_ff_start_effect(&pAfterburn2, "Afterburn2");
682 // nprintf(("Joystick", "FF: Afterburn started\n"));
684 Joy_ff_afterburning = 1;
687 void joy_ff_afterburn_off()
689 if ( !joy_ff_can_play() ) {
693 if ( !Joy_ff_afterburning ) {
697 if (pAfterburn1.loaded) {
698 SDL_HapticStopEffect(haptic, pAfterburn1.id);
701 if (pAfterburn2.loaded) {
702 SDL_HapticStopEffect(haptic, pAfterburn2.id);
705 Joy_ff_afterburning = 0;
707 // nprintf(("Joystick", "FF: Afterburn stopped\n"));
710 void joy_ff_explode()
712 if ( !joy_ff_can_play() ) {
716 if (pAfterburn1.loaded) {
717 SDL_HapticStopEffect(haptic, pAfterburn1.id);
720 if (pAfterburn2.loaded) {
721 SDL_HapticStopEffect(haptic, pAfterburn2.id);
724 if (pShootEffect.loaded) {
725 SDL_HapticStopEffect(haptic, pShootEffect.id);
727 pShootEffect.eff.periodic.direction.dir[0] = 9000;
728 pShootEffect.eff.periodic.length = 500;
729 pShootEffect.eff.periodic.magnitude = 0x7FFF;
730 pShootEffect.eff.periodic.fade_length = 500;
732 joy_ff_update_effect(&pShootEffect, "pShootEffect");
734 joy_ff_start_effect(&pShootEffect, "ShootEffect (Explode)");
735 } else if (Joy_rumble) {
736 SDL_HapticRumblePlay(haptic, 0.75f, 500);
739 Joy_ff_afterburning = 0;
742 void joy_ff_fly_by(int mag)
746 if ( !joy_ff_can_play() ) {
750 if (Joy_ff_afterburning) {
754 if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
758 gain = mag * 120 + 4000;
762 } else if (gain > 10000) {
766 SDL_HapticStopEffect(haptic, pAfterburn1.id);
768 pAfterburn1.eff.periodic.length = (6000 * mag + 400000) / 1000;
769 pAfterburn1.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
771 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
773 SDL_HapticStopEffect(haptic, pAfterburn2.id);
775 pAfterburn2.eff.periodic.length = (6000 * mag + 400000) / 1000;
776 pAfterburn2.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
778 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
780 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Fly by)");
781 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Fly by)");
784 void joy_ff_deathroll()
786 if ( !joy_ff_can_play() ) {
790 if (pAfterburn1.loaded && pAfterburn2.loaded) {
791 SDL_HapticStopEffect(haptic, pAfterburn1.id);
793 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
794 pAfterburn1.eff.periodic.period = 200;
795 pAfterburn1.eff.periodic.magnitude = 0x7FFF;
796 pAfterburn1.eff.periodic.attack_length = 200;
798 joy_ff_update_effect(&pAfterburn1, "pAfterburn1");
800 SDL_HapticStopEffect(haptic, pAfterburn2.id);
802 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
803 pAfterburn2.eff.periodic.period = 200;
804 pAfterburn2.eff.periodic.magnitude = 0x7FFF;
805 pAfterburn2.eff.periodic.attack_length = 200;
807 joy_ff_update_effect(&pAfterburn2, "pAfterburn2");
809 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Death Roll)");
810 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Death Roll)");
812 Joy_ff_afterburning = 1;