]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/joy_ff.cpp
avoid updating pSpring when it isn't needed
[taylor/freespace2.git] / src / io / joy_ff.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
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 
6  * source.
7  *
8 */ 
9
10
11 #include "pstypes.h"
12 #include "vecmat.h"
13 #include "osregistry.h"
14 #include "joy_ff.h"
15 #include "osapi.h"
16
17
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
25 typedef struct {
26         SDL_HapticEffect eff;
27         int id;
28         int loaded;
29 } haptic_effect_t;
30
31 static haptic_effect_t pHitEffect1;
32 static haptic_effect_t pHitEffect2;
33 static haptic_effect_t pAfterburn1;
34 static haptic_effect_t pAfterburn2;
35 static haptic_effect_t pShootEffect;
36 static haptic_effect_t pSecShootEffect;
37 static haptic_effect_t pSpring;
38 static haptic_effect_t pDock;
39 //static haptic_effect_t pDeathroll1;
40 //static haptic_effect_t pDeathroll2;
41 //static haptic_effect_t pExplode;
42
43 static void joy_ff_create_effects();
44 static int joy_ff_effect_playing(haptic_effect_t *eff);
45 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name);
46
47 extern SDL_Joystick *sdljoy;
48
49
50 int joy_ff_init()
51 {
52         int ff_enabled = 0;
53
54         ff_enabled = os_config_read_uint("Controls", "EnableJoystickFF", 0);
55
56         if ( !ff_enabled || !SDL_JoystickIsHaptic(sdljoy) ) {
57                 return 0;
58         }
59
60         if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
61                 mprintf(("  ERROR: Unable to initialize haptic subsystem\n"));
62                 return -1;
63         }
64
65         haptic = SDL_HapticOpenFromJoystick(sdljoy);
66
67         if (haptic == NULL) {
68                 mprintf(("  ERROR: Unable to open haptic joystick\n"));
69                 SDL_QuitSubSystem(SDL_INIT_HAPTIC);
70                 return -1;
71         }
72
73         if ( SDL_HapticRumbleSupported(haptic) ) {
74                 SDL_HapticRumbleInit(haptic);
75                 Joy_rumble = 1;
76         }
77
78         mprintf(("  Rumble  : %s\n", Joy_rumble ? "Yes" : "No"));
79         mprintf(("  Axes    : %d\n", SDL_HapticNumAxes(haptic)));
80         mprintf(("  Max effects     : %d\n", SDL_HapticNumEffects(haptic)));
81         mprintf(("  Running effects : %d\n", SDL_HapticNumEffectsPlaying(haptic)));
82
83         joy_ff_create_effects();
84
85         Joy_ff_enabled = 1;
86         Joy_ff_acquired = 1;
87
88         Joy_ff_directional_hit_effect_enabled = os_config_read_uint("Controls", "EnableHitEffect", 1);
89
90         return 0;
91 }
92
93 void joy_ff_shutdown()
94 {
95         if ( !Joy_ff_enabled ) {
96                 return;
97         }
98
99         Joy_rumble = 0;
100
101         SDL_HapticClose(haptic);
102         haptic = NULL;
103
104         SDL_QuitSubSystem(SDL_INIT_HAPTIC);
105
106         Joy_ff_acquired = 0;
107         Joy_ff_enabled = 0;
108 }
109
110 static void joy_ff_create_effects()
111 {
112         // clear all SDL errors
113         SDL_ClearError();
114
115         unsigned int supported = 0;
116
117         supported = SDL_HapticQuery(haptic);
118
119         if ( !(supported & SDL_HAPTIC_CONSTANT) ) {
120                 mprintf(("  Constant Force  : not supported\n"));
121         }
122
123         if ( !(supported & SDL_HAPTIC_SINE) ) {
124                 mprintf(("  Sine Wave       : not supported\n"));
125                 Warning(LOCATION, "Sine Wave: not supported");
126         }
127
128         if ( !(supported & SDL_HAPTIC_SAWTOOTHDOWN) ) {
129                 mprintf(("  Sawtooth Down   : not supported\n"));
130         }
131
132         if ( !(supported & SDL_HAPTIC_SPRING) ) {
133                 mprintf(("  Spring          : not supported\n"));
134         //      Error(LOCATION, "Spring: not supported");
135         }
136 /*
137         if ( !(supported & SDL_HAPTIC_SQUARE) ) {
138                 mprintf(("  Square          : not supported\n"));
139         }
140 */
141         if ( !(supported & SDL_HAPTIC_TRIANGLE) ) {
142                 mprintf(("  Triangle        : not supported\n"));
143         }
144
145
146         if (supported & SDL_HAPTIC_CONSTANT) {
147                 // pHitEffect1
148                 memset(&pHitEffect1, 0, sizeof(haptic_effect_t));
149
150                 pHitEffect1.eff.type = SDL_HAPTIC_CONSTANT;
151                 pHitEffect1.eff.constant.direction.type = SDL_HAPTIC_POLAR;
152                 pHitEffect1.eff.constant.direction.dir[0] = 0;
153                 pHitEffect1.eff.constant.length = 300;
154                 pHitEffect1.eff.constant.level = 0x7FFF;
155                 pHitEffect1.eff.constant.attack_length = 0;
156                 pHitEffect1.eff.constant.attack_level = 0x7FFF;
157                 pHitEffect1.eff.constant.fade_length = 120;
158                 pHitEffect1.eff.constant.fade_level = 1;
159
160                 pHitEffect1.id = SDL_HapticNewEffect(haptic, &pHitEffect1.eff);
161
162                 if (pHitEffect1.id < 0) {
163                         mprintf(("    Hit effect 1 failed to load:\n      %s\n", SDL_GetError()));
164                 } else {
165                         pHitEffect1.loaded = 1;
166                 }
167         }
168
169         if (supported & SDL_HAPTIC_SINE) {
170                 // pHitEffect2
171                 memset(&pHitEffect2, 0, sizeof(haptic_effect_t));
172
173                 pHitEffect2.eff.type = SDL_HAPTIC_SINE;
174                 pHitEffect2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
175                 pHitEffect2.eff.periodic.direction.dir[0] = 9000;
176                 pHitEffect2.eff.periodic.length = 300;
177                 pHitEffect2.eff.periodic.period = 100;
178                 pHitEffect2.eff.periodic.magnitude = 0x7FFF;
179                 pHitEffect2.eff.periodic.attack_length = 100;
180                 pHitEffect2.eff.periodic.fade_length = 100;
181
182                 pHitEffect2.id = SDL_HapticNewEffect(haptic, &pHitEffect2.eff);
183
184                 if (pHitEffect2.id < 0) {
185                         mprintf(("    Hit effect 2 failed to load:\n      %s\n", SDL_GetError()));
186                 } else {
187                         pHitEffect2.loaded = 1;
188                 }
189         }
190
191         if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
192                 // pShootEffect
193                 memset(&pShootEffect, 0, sizeof(haptic_effect_t));
194
195                 pShootEffect.eff.type = SDL_HAPTIC_SAWTOOTHDOWN;
196                 pShootEffect.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
197                 pShootEffect.eff.periodic.direction.dir[0] = 0;
198                 pShootEffect.eff.periodic.length = 160;
199                 pShootEffect.eff.periodic.period = 20;
200                 pShootEffect.eff.periodic.magnitude = 0x7FFF;
201                 pShootEffect.eff.periodic.fade_length = 120;
202
203                 pShootEffect.id = SDL_HapticNewEffect(haptic, &pShootEffect.eff);
204
205                 if (pShootEffect.id < 0) {
206                         mprintf(("    Fire primary effect failed to load:\n      %s\n", SDL_GetError()));
207                 } else {
208                         pShootEffect.loaded = 1;
209                 }
210         }
211
212         if (supported & SDL_HAPTIC_CONSTANT) {
213                 // pSecShootEffect
214                 memset(&pSecShootEffect, 0, sizeof(haptic_effect_t));
215
216                 pSecShootEffect.eff.type = SDL_HAPTIC_CONSTANT;
217                 pSecShootEffect.eff.constant.direction.type = SDL_HAPTIC_POLAR;
218                 pSecShootEffect.eff.constant.direction.dir[0] = 0;
219                 pSecShootEffect.eff.constant.length = 200;
220                 pSecShootEffect.eff.constant.level = 0x7FFF;
221                 pSecShootEffect.eff.constant.attack_length = 50;
222                 pSecShootEffect.eff.constant.attack_level = 0x7FFF;
223                 pSecShootEffect.eff.constant.fade_length = 100;
224                 pSecShootEffect.eff.constant.fade_level = 1;
225
226                 pSecShootEffect.id = SDL_HapticNewEffect(haptic, &pSecShootEffect.eff);
227
228                 if (pSecShootEffect.id < 0) {
229                         mprintf(("    Fire secondary effect failed to load:\n      %s\n", SDL_GetError()));
230                 } else {
231                         pSecShootEffect.loaded = 1;
232                 }
233         }
234
235         if (supported & SDL_HAPTIC_SPRING) {
236                 // pSpring
237                 memset(&pSpring, 0, sizeof(haptic_effect_t));
238
239                 pSpring.eff.type = SDL_HAPTIC_SPRING;
240                 pSpring.eff.condition.length = SDL_HAPTIC_INFINITY;
241
242                 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
243                         pSpring.eff.condition.right_sat[i] = 0x7FFF;
244                         pSpring.eff.condition.left_sat[i] = 0x7FFF;
245                         pSpring.eff.condition.right_coeff[i] = 0x147;
246                         pSpring.eff.condition.left_coeff[i] = 0x147;
247                 }
248
249                 pSpring.id = SDL_HapticNewEffect(haptic, &pSpring.eff);
250
251                 if (pSpring.id < 0) {
252                         mprintf(("    Spring effect failed to load:\n      %s\n", SDL_GetError()));
253                 } else {
254                         pSpring.loaded = 1;
255                         joy_ff_start_effect(&pSpring, "Spring");
256                 }
257         }
258
259         if (supported & SDL_HAPTIC_SINE) {
260                 // pAfterburn1
261                 memset(&pAfterburn1, 0, sizeof(haptic_effect_t));
262
263                 pAfterburn1.eff.type = SDL_HAPTIC_SINE;
264                 pAfterburn1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
265                 pAfterburn1.eff.periodic.direction.dir[0] = 0;
266                 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
267                 pAfterburn1.eff.periodic.period = 20;
268                 pAfterburn1.eff.periodic.magnitude = 0x3332;
269
270                 pAfterburn1.id = SDL_HapticNewEffect(haptic, &pAfterburn1.eff);
271
272                 if (pAfterburn1.id < 0) {
273                         mprintf(("    Afterburn effect 1 failed to load:\n      %s\n", SDL_GetError()));
274                 } else {
275                         pAfterburn1.loaded = 1;
276                 }
277
278                 // pAfterburn2
279                 memset(&pAfterburn2, 0, sizeof(haptic_effect_t));
280
281                 pAfterburn2.eff.type = SDL_HAPTIC_SINE;
282                 pAfterburn2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
283                 pAfterburn2.eff.periodic.direction.dir[0] = 9000;
284                 pAfterburn2.eff.periodic.length = 125;
285                 pAfterburn2.eff.periodic.period = 100;
286                 pAfterburn2.eff.periodic.magnitude = 0x1999;
287
288                 pAfterburn2.id = SDL_HapticNewEffect(haptic, &pAfterburn2.eff);
289
290                 if (pAfterburn2.id < 0) {
291                         mprintf(("    Afterburn effect 2 failed to load:\n      %s\n", SDL_GetError()));
292                 } else {
293                         pAfterburn2.loaded = 1;
294                 }
295         }
296
297
298 //      if (supported & SDL_HAPTIC_SQUARE) {
299         if (supported & SDL_HAPTIC_TRIANGLE) {
300                 // pDock
301                 memset(&pDock, 0, sizeof(haptic_effect_t));
302
303         //      pDock.eff.type = SDL_HAPTIC_SQUARE;
304                 pDock.eff.type = SDL_HAPTIC_TRIANGLE;
305                 pDock.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
306                 pDock.eff.periodic.direction.dir[0] = 9000;
307                 pDock.eff.periodic.length = 125;
308                 pDock.eff.periodic.period = 100;
309                 pDock.eff.periodic.magnitude = 0x3332;
310
311                 pDock.id = SDL_HapticNewEffect(haptic, &pDock.eff);
312
313                 if (pDock.id < 0) {
314                         mprintf(("    Dock effect failed to load:\n      %s\n", SDL_GetError()));
315                 } else {
316                         pDock.loaded = 1;
317                 }
318         }
319
320 /*
321         if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
322                 // pExplode
323                 memset(&pExplode, 0, sizeof(haptic_effect_t));
324
325                 pExplode.eff.type = SDL_HAPTIC_SAWTOOTHDOWN;
326                 pExplode.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
327                 pExplode.eff.periodic.direction.dir[0] = 9000;
328                 pExplode.eff.periodic.length = 500;
329                 pExplode.eff.periodic.period = 20;
330                 pExplode.eff.periodic.magnitude = 0x7FFF;
331                 pExplode.eff.periodic.attack_length = 0;
332                 pExplode.eff.periodic.fade_length = 500;
333
334                 pExplode.id = SDL_HapticNewEffect(haptic, &pExplode.eff);
335
336                 if (pExplode.id < 0) {
337                         mprintf(("    Explosion effect failed to load:\n      %s\n", SDL_GetError()));
338                 } else {
339                         pExplode.loaded = 1;
340                 }
341         }
342
343         if (supported & SDL_HAPTIC_SINE) {
344                 // pDeathroll1
345                 memset(&pDeathroll1, 0, sizeof(haptic_effect_t));
346
347                 pDeathroll1.eff.type = SDL_HAPTIC_SINE;
348                 pDeathroll1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
349                 pDeathroll1.eff.periodic.direction.dir[0] = 0;
350                 pDeathroll1.eff.periodic.length = SDL_HAPTIC_INFINITY;
351                 pDeathroll1.eff.periodic.period = 200;
352                 pDeathroll1.eff.periodic.magnitude = 0x7FFF;
353                 pDeathroll1.eff.periodic.attack_length = 200;
354
355                 pDeathroll1.id = SDL_HapticNewEffect(haptic, &pDeathroll1.eff);
356
357                 if (pDeathroll1.id < 0) {
358                         mprintf(("    Deathroll effect 1 failed to load:\n      %s\n", SDL_GetError()));
359                 } else {
360                         pDeathroll1.loaded = 1;
361                 }
362
363                 // pDeathroll2
364                 memset(&pDeathroll2, 0, sizeof(haptic_effect_t));
365
366                 pDeathroll2.eff.type = SDL_HAPTIC_SINE;
367                 pDeathroll2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
368                 pDeathroll2.eff.periodic.direction.dir[0] = 9000;
369                 pDeathroll2.eff.periodic.length = SDL_HAPTIC_INFINITY;
370                 pDeathroll2.eff.periodic.period = 200;
371                 pDeathroll2.eff.periodic.magnitude = 0x7FFF;
372                 pDeathroll2.eff.periodic.attack_length = 200;
373
374                 pDeathroll2.id = SDL_HapticNewEffect(haptic, &pDeathroll2.eff);
375
376                 if (pDeathroll2.id < 0) {
377                         mprintf(("    Deathroll effect 2 failed to load:\n      %s\n", SDL_GetError()));
378                 } else {
379                         pDeathroll2.loaded = 1;
380                 }
381         }
382 */
383 }
384
385 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name)
386 {
387         if ( !eff->loaded ) {
388                 return;
389         }
390
391 //      nprintf(("Joystick", "FF: Starting effect %s\n", name));
392
393         if ( SDL_HapticRunEffect(haptic, eff->id, 1) ) {
394                 mprintf(("HapticERROR:  Unable to run %s:\n  %s\n", name, SDL_GetError()));
395         }
396 }
397
398 void joy_ff_stop_effects()
399 {
400         if ( !Joy_ff_enabled ) {
401                 return;
402         }
403
404         SDL_HapticStopAll(haptic);
405 }
406
407 void joy_ff_mission_init(vector v)
408 {
409         v.xyz.z = 0.0f;
410
411         joy_ff_handling_scaler = (int) ((vm_vec_mag(&v) + 1.3f) * 5.0f);
412 }
413
414 void joy_reacquire_ff()
415 {
416         if ( !Joy_ff_enabled ) {
417                 return;
418         }
419
420         if (Joy_ff_acquired) {
421                 return;
422         }
423
424         joy_ff_start_effect(&pSpring, "Spring");
425
426         Joy_ff_acquired = 1;
427 }
428
429 void joy_unacquire_ff()
430 {
431         if ( !Joy_ff_enabled ) {
432                 return;
433         }
434
435         if ( !Joy_ff_acquired ) {
436                 return;
437         }
438
439         joy_ff_stop_effects();
440
441         Joy_ff_acquired = 0;
442 }
443
444 void joy_ff_play_vector_effect(vector *v, float scaler)
445 {
446         vector vf;
447         float x, y;
448
449 //      nprintf(("Joystick", "FF: vec = { %f, %f, %f } s = %f\n", v->xyz.x, v->xyz.y, v->xyz.z, scaler));
450         vm_vec_copy_scale(&vf, v, scaler);
451         x = vf.xyz.x;
452         vf.xyz.x = 0.0f;
453
454         if (vf.xyz.y + vf.xyz.z < 0.0f) {
455                 y = -vm_vec_mag(&vf);
456         } else {
457                 y = vm_vec_mag(&vf);
458         }
459
460         joy_ff_play_dir_effect(-x, -y);
461 }
462
463 void joy_ff_play_dir_effect(float x, float y)
464 {
465         int idegs, imag;
466         float degs;
467
468         if ( !Joy_ff_enabled ) {
469                 return;
470         }
471
472         if ( !Joy_ff_acquired ) {
473                 return;
474         }
475
476         // allow for at least one of the effects to work
477         if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) {
478                 return;
479         }
480
481         if (joy_ff_effect_playing(&pHitEffect1) || joy_ff_effect_playing(&pHitEffect2)) {
482                 nprintf(("Joystick", "FF: HitEffect already playing.  Skipping\n"));
483                 return;
484         }
485
486         if (Joy_ff_directional_hit_effect_enabled) {
487                 if (x > 8000.0f) {
488                         x = 8000.0f;
489                 } else if (x < -8000.0f) {
490                         x = -8000.0f;
491                 }
492
493                 if (y > 8000.0f) {
494                         y = 8000.0f;
495                 } else if (y < -8000.0f) {
496                         y = -8000.0f;
497                 }
498
499                 imag = (int) fl_sqrt(x * x + y * y);
500                 if (imag > 10000) {
501                         imag = 10000;
502                 }
503
504                 degs = (float)atan2(x, y);
505                 idegs = (int) (degs * 18000.0f / PI) + 90;
506                 while (idegs < 0) {
507                         idegs += 36000;
508                 }
509
510                 while (idegs >= 36000) {
511                         idegs -= 36000;
512                 }
513
514                 if (pHitEffect1.loaded) {
515                         pHitEffect1.eff.constant.direction.dir[0] = idegs;
516                         pHitEffect1.eff.constant.level = (Sint16)(32767.0f * (imag / 10000.0f));
517
518                         if ( SDL_HapticUpdateEffect(haptic, pHitEffect1.id, &pHitEffect1.eff) < 0 ) {
519                                 mprintf(("HapticERROR:  Unable to update pHitEffect1:\n  %s\n", SDL_GetError()));
520                         }
521                 }
522
523                 idegs += 9000;
524                 if (idegs >= 36000)
525                         idegs -= 36000;
526
527                 if (pHitEffect2.loaded) {
528                         pHitEffect2.eff.periodic.direction.dir[0] = idegs;
529                         pHitEffect2.eff.periodic.magnitude = (Sint16)(32767.0f * (imag / 10000.0f));
530
531                         if ( SDL_HapticUpdateEffect(haptic, pHitEffect2.id, &pHitEffect2.eff) < 0 ) {
532                                 mprintf(("HapticERROR:  Unable to update pHitEffect2:\n  %s\n", SDL_GetError()));
533                         }
534                 }
535         }
536
537         joy_ff_start_effect(&pHitEffect1, "HitEffect1");
538         joy_ff_start_effect(&pHitEffect2, "HitEffect2");
539 }
540
541 static int primary_ff_level = 10000;
542
543 void joy_ff_play_primary_shoot(int gain)
544 {
545         if ( !Joy_ff_enabled ) {
546                 return;
547         }
548
549         if ( !Joy_ff_acquired ) {
550                 return;
551         }
552
553         if ( !pShootEffect.loaded && !Joy_rumble ) {
554                 return;
555         }
556
557         if (gain < 1) {
558                 gain = 1;
559         } else if (gain > 10000) {
560                 gain = 10000;
561         }
562
563         if (pShootEffect.loaded) {
564                 SDL_HapticStopEffect(haptic, pShootEffect.id);
565
566                 if (gain != primary_ff_level) {
567                         pShootEffect.eff.periodic.direction.dir[0] = 0;
568                         pShootEffect.eff.periodic.length = 160;
569                         pShootEffect.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
570                         pShootEffect.eff.periodic.fade_length = 120;
571
572                         if ( SDL_HapticUpdateEffect(haptic, pShootEffect.id, &pShootEffect.eff) < 0 ) {
573                                 mprintf(("HapticERROR:  Unable to update pShootEffect:\n  %s\n", SDL_GetError()));
574                         }
575
576                         primary_ff_level = gain;
577                 }
578
579                 joy_ff_start_effect(&pShootEffect, "ShootEffect");
580         } else if (Joy_rumble) {
581                 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, 100);
582         }
583 }
584
585 static int secondary_ff_level = 10000;
586
587 void joy_ff_play_secondary_shoot(int gain)
588 {
589         if ( !Joy_ff_enabled ) {
590                 return;
591         }
592
593         if ( !Joy_ff_acquired ) {
594                 return;
595         }
596
597         if ( !pSecShootEffect.loaded && !Joy_rumble ) {
598                 return;
599         }
600
601         gain = gain * 100 + 2500;
602
603         if (gain < 1) {
604                 gain = 1;
605         } else if (gain > 10000) {
606                 gain = 10000;
607         }
608
609         if (pSecShootEffect.loaded) {
610                 SDL_HapticStopEffect(haptic, pSecShootEffect.id);
611
612                 if (gain != secondary_ff_level) {
613                         pSecShootEffect.eff.constant.level = (Sint16)(32767.0f * (gain / 10000.0f));
614                         pSecShootEffect.eff.constant.length = (150000 + gain * 25) / 1000;
615
616                         if ( SDL_HapticUpdateEffect(haptic, pSecShootEffect.id, &pSecShootEffect.eff) < 0 ) {
617                                 mprintf(("HapticERROR:  Unable to update pSecShootEffect:\n  %s\n", SDL_GetError()));
618                         }
619
620                         secondary_ff_level = gain;
621                         nprintf(("Joystick", "FF: Secondary force = 0x%04x\n", pSecShootEffect.eff.constant.level));
622                 }
623
624                 joy_ff_start_effect(&pSecShootEffect, "SecShootEffect");
625         } else if (Joy_rumble) {
626                 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, (150000 + gain * 25) / 1000);
627         }
628 }
629
630 void joy_ff_adjust_handling(int speed)
631 {
632         int v;
633         short coeff = 0;
634
635         if ( !Joy_ff_enabled ) {
636                 return;
637         }
638
639         if ( !Joy_ff_acquired ) {
640                 return;
641         }
642
643         if ( !pSpring.loaded ) {
644                 return;
645         }
646
647         static int last_speed = -1000;
648
649         if (speed == last_speed) {
650                 return;
651         }
652
653         last_speed = speed;
654
655         v = speed * joy_ff_handling_scaler * 2 / 3;
656 //      v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
657         v += joy_ff_handling_scaler * 45 - 500;
658
659         if (v < 0) {
660                 v = 0;
661         } else if (v > 10000) {
662                 v = 10000;
663         }
664
665         coeff = (short)(32767.0f * (v / 10000.0f));
666
667         for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
668                 pSpring.eff.condition.right_coeff[i] = coeff;
669                 pSpring.eff.condition.left_coeff[i] = coeff;
670         }
671
672 //      nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
673
674         SDL_HapticUpdateEffect(haptic, pSpring.id, &pSpring.eff);
675 }
676
677 static int joy_ff_effect_playing(haptic_effect_t *eff)
678 {
679         if ( !eff->loaded ) {
680                 return 0;
681         } else {
682                 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
683         }
684 }
685
686 void joy_ff_docked()
687 {
688         if ( !Joy_ff_enabled ) {
689                 return;
690         }
691
692         if ( !Joy_ff_acquired ) {
693                 return;
694         }
695
696         if ( !pDock.loaded ) {
697                 return;
698         }
699
700         SDL_HapticStopEffect(haptic, pDock.id);
701
702         pDock.eff.periodic.magnitude = 0x7fff;
703
704         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
705                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
706         }
707
708         joy_ff_start_effect(&pDock, "Dock");
709 }
710
711 void joy_ff_play_reload_effect()
712 {
713         if ( !Joy_ff_enabled ) {
714                 return;
715         }
716
717         if ( !Joy_ff_acquired ) {
718                 return;
719         }
720
721         if ( !pDock.loaded ) {
722                 return;
723         }
724
725         SDL_HapticStopEffect(haptic, pDock.id);
726
727         pDock.eff.periodic.magnitude = 0x3fff;
728
729         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
730                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
731         }
732
733         joy_ff_start_effect(&pDock, "Dock (Reload)");
734 }
735
736 static int Joy_ff_afterburning = 0;
737
738 void joy_ff_afterburn_on()
739 {
740         if ( !Joy_ff_enabled ) {
741                 return;
742         }
743
744         if ( !Joy_ff_acquired ) {
745                 return;
746         }
747
748         if (Joy_ff_afterburning) {
749                 return;
750         }
751
752         if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
753                 return;
754         }
755
756         SDL_HapticStopEffect(haptic, pAfterburn1.id);
757
758         pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
759         pAfterburn1.eff.periodic.period = 20;
760         pAfterburn1.eff.periodic.magnitude = 0x3332;
761         pAfterburn1.eff.periodic.attack_length = 0;
762
763         if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
764                 mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
765         }
766
767         SDL_HapticStopEffect(haptic, pAfterburn2.id);
768
769         pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
770         pAfterburn2.eff.periodic.period = 100;
771         pAfterburn2.eff.periodic.magnitude = 0x1999;
772         pAfterburn2.eff.periodic.attack_length = 0;
773
774         if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
775                 mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
776         }
777
778         joy_ff_start_effect(&pAfterburn1, "Afterburn1");
779         joy_ff_start_effect(&pAfterburn2, "Afterburn2");
780
781 //      nprintf(("Joystick", "FF: Afterburn started\n"));
782
783         Joy_ff_afterburning = 1;
784 }
785
786 void joy_ff_afterburn_off()
787 {
788         if ( !Joy_ff_enabled ) {
789                 return;
790         }
791
792         if ( !Joy_ff_acquired ) {
793                 return;
794         }
795
796         if ( !Joy_ff_afterburning ) {
797                 return;
798         }
799
800         if (pAfterburn1.loaded) {
801                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
802         }
803
804         if (pAfterburn2.loaded) {
805                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
806         }
807
808         Joy_ff_afterburning = 0;
809
810 //      nprintf(("Joystick", "FF: Afterburn stopped\n"));
811 }
812
813 void joy_ff_explode()
814 {
815         if ( !Joy_ff_enabled ) {
816                 return;
817         }
818
819         if ( !Joy_ff_acquired ) {
820                 return;
821         }
822 /*
823         if (pDeathroll1.loaded) {
824                 SDL_HapticStopEffect(haptic, pDeathroll1.id);
825         }
826
827         if (pDeathroll2.loaded) {
828                 SDL_HapticStopEffect(haptic, pDeathroll2.id);
829         }
830
831         if (pExplode.loaded) {
832                 SDL_HapticStopEffect(haptic, pExplode.id);
833         }
834
835         joy_ff_start_effect(&pExplode, "Explode");
836 */
837
838         if (pAfterburn1.loaded) {
839                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
840         }
841
842         if (pAfterburn2.loaded) {
843                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
844         }
845
846         if (pShootEffect.loaded) {
847                 SDL_HapticStopEffect(haptic, pShootEffect.id);
848
849                 pShootEffect.eff.periodic.direction.dir[0] = 9000;
850                 pShootEffect.eff.periodic.length = 500;
851                 pShootEffect.eff.periodic.magnitude = 0x7FFF;
852                 pShootEffect.eff.periodic.fade_length = 500;
853
854                 if ( SDL_HapticUpdateEffect(haptic, pShootEffect.id, &pShootEffect.eff) < 0 ) {
855                         mprintf(("HapticERROR:  Unable to update pShootEffect:\n  %s\n", SDL_GetError()));
856                 }
857
858                 joy_ff_start_effect(&pShootEffect, "ShootEffect (Explode)");
859         }
860
861         Joy_ff_afterburning = 0;
862 }
863
864 void joy_ff_fly_by(int mag)
865 {
866         int gain;
867
868         if ( !Joy_ff_enabled ) {
869                 return;
870         }
871
872         if ( !Joy_ff_acquired ) {
873                 return;
874         }
875
876         if (Joy_ff_afterburning) {
877                 return;
878         }
879
880         if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
881                 return;
882         }
883
884         gain = mag * 120 + 4000;
885
886         if (gain < 1) {
887                 gain = 1;
888         } else if (gain > 10000) {
889                 gain = 10000;
890         }
891
892         SDL_HapticStopEffect(haptic, pAfterburn1.id);
893
894         pAfterburn1.eff.periodic.length = (6000 * mag + 400000) / 1000;
895         pAfterburn1.eff.periodic.period = 20;
896         pAfterburn1.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
897         pAfterburn1.eff.periodic.attack_length = 0;
898
899         if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
900                 mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
901         }
902
903
904         SDL_HapticStopEffect(haptic, pAfterburn2.id);
905
906         pAfterburn2.eff.periodic.length = (6000 * mag + 400000) / 1000;
907         pAfterburn2.eff.periodic.period = 100;
908         pAfterburn2.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
909         pAfterburn2.eff.periodic.attack_length = 0;
910
911         if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
912                 mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
913         }
914
915         joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Fly by)");
916         joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Fly by)");
917 }
918
919 void joy_ff_deathroll()
920 {
921         if ( !Joy_ff_enabled ) {
922                 return;
923         }
924
925         if ( !Joy_ff_acquired ) {
926                 return;
927         }
928 /*
929         if (pDeathroll1.loaded) {
930                 SDL_HapticStopEffect(haptic, pDeathroll1.id);
931         }
932
933         if (pDeathroll2.loaded) {
934                 SDL_HapticStopEffect(haptic, pDeathroll2.id);
935         }
936
937         joy_ff_start_effect(&pDeathroll1, "Deathroll1");
938         joy_ff_start_effect(&pDeathroll2, "Deathroll2");
939 */
940
941         if (pAfterburn1.loaded && pAfterburn2.loaded) {
942                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
943
944                 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
945                 pAfterburn1.eff.periodic.period = 200;
946                 pAfterburn1.eff.periodic.magnitude = 0x7FFF;
947                 pAfterburn1.eff.periodic.attack_length = 200;
948
949                 if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
950                         mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
951                 }
952
953                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
954
955                 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
956                 pAfterburn2.eff.periodic.period = 200;
957                 pAfterburn2.eff.periodic.magnitude = 0x7FFF;
958                 pAfterburn2.eff.periodic.attack_length = 200;
959
960                 if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
961                         mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
962                 }
963
964                 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Death Roll)");
965                 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Death Roll)");
966
967                 Joy_ff_afterburning = 1;
968         }
969 }