]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/joy_ff.cpp
PXO, works and everything (probably)
[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 = 0x6665;
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 = 0x3332;
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         v = speed * joy_ff_handling_scaler * 2 / 3;
648 //      v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
649         v += joy_ff_handling_scaler * 45 - 500;
650
651         if (v < 0) {
652                 v = 0;
653         } else if (v > 10000) {
654                 v = 10000;
655         }
656
657         coeff = (short)(32767.0f * (v / 10000.0f));
658
659         for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
660                 pSpring.eff.condition.right_coeff[i] = coeff;
661                 pSpring.eff.condition.left_coeff[i] = coeff;
662         }
663
664 //      nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
665
666         SDL_HapticUpdateEffect(haptic, pSpring.id, &pSpring.eff);
667 }
668
669 static int joy_ff_effect_playing(haptic_effect_t *eff)
670 {
671         if ( !eff->loaded ) {
672                 return 0;
673         } else {
674                 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
675         }
676 }
677
678 void joy_ff_docked()
679 {
680         if ( !Joy_ff_enabled ) {
681                 return;
682         }
683
684         if ( !Joy_ff_acquired ) {
685                 return;
686         }
687
688         if ( !pDock.loaded ) {
689                 return;
690         }
691
692         SDL_HapticStopEffect(haptic, pDock.id);
693
694         pDock.eff.periodic.magnitude = 0x7fff;
695
696         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
697                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
698         }
699
700         joy_ff_start_effect(&pDock, "Dock");
701 }
702
703 void joy_ff_play_reload_effect()
704 {
705         if ( !Joy_ff_enabled ) {
706                 return;
707         }
708
709         if ( !Joy_ff_acquired ) {
710                 return;
711         }
712
713         if ( !pDock.loaded ) {
714                 return;
715         }
716
717         SDL_HapticStopEffect(haptic, pDock.id);
718
719         pDock.eff.periodic.magnitude = 0x3fff;
720
721         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
722                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
723         }
724
725         joy_ff_start_effect(&pDock, "Dock (Reload)");
726 }
727
728 static int Joy_ff_afterburning = 0;
729
730 void joy_ff_afterburn_on()
731 {
732         if ( !Joy_ff_enabled ) {
733                 return;
734         }
735
736         if ( !Joy_ff_acquired ) {
737                 return;
738         }
739
740         if (Joy_ff_afterburning) {
741                 return;
742         }
743
744         if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
745                 return;
746         }
747
748         SDL_HapticStopEffect(haptic, pAfterburn1.id);
749
750         pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
751         pAfterburn1.eff.periodic.period = 20;
752         pAfterburn1.eff.periodic.magnitude = 0x3fff;
753         pAfterburn1.eff.periodic.attack_length = 0;
754
755         if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
756                 mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
757         }
758
759         SDL_HapticStopEffect(haptic, pAfterburn2.id);
760
761         pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
762         pAfterburn2.eff.periodic.period = 100;
763         pAfterburn2.eff.periodic.magnitude = 0x3fff;
764         pAfterburn2.eff.periodic.attack_length = 0;
765
766         if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
767                 mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
768         }
769
770         joy_ff_start_effect(&pAfterburn1, "Afterburn1");
771         joy_ff_start_effect(&pAfterburn2, "Afterburn2");
772
773 //      nprintf(("Joystick", "FF: Afterburn started\n"));
774
775         Joy_ff_afterburning = 1;
776 }
777
778 void joy_ff_afterburn_off()
779 {
780         if ( !Joy_ff_enabled ) {
781                 return;
782         }
783
784         if ( !Joy_ff_acquired ) {
785                 return;
786         }
787
788         if ( !Joy_ff_afterburning ) {
789                 return;
790         }
791
792         if (pAfterburn1.loaded) {
793                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
794         }
795
796         if (pAfterburn2.loaded) {
797                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
798         }
799
800         Joy_ff_afterburning = 0;
801
802 //      nprintf(("Joystick", "FF: Afterburn stopped\n"));
803 }
804
805 void joy_ff_explode()
806 {
807         if ( !Joy_ff_enabled ) {
808                 return;
809         }
810
811         if ( !Joy_ff_acquired ) {
812                 return;
813         }
814 /*
815         if (pDeathroll1.loaded) {
816                 SDL_HapticStopEffect(haptic, pDeathroll1.id);
817         }
818
819         if (pDeathroll2.loaded) {
820                 SDL_HapticStopEffect(haptic, pDeathroll2.id);
821         }
822
823         if (pExplode.loaded) {
824                 SDL_HapticStopEffect(haptic, pExplode.id);
825         }
826
827         joy_ff_start_effect(&pExplode, "Explode");
828 */
829
830         if (pAfterburn1.loaded) {
831                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
832         }
833
834         if (pAfterburn2.loaded) {
835                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
836         }
837
838         if (pShootEffect.loaded) {
839                 SDL_HapticStopEffect(haptic, pShootEffect.id);
840
841                 pShootEffect.eff.periodic.direction.dir[0] = 9000;
842                 pShootEffect.eff.periodic.length = 500;
843                 pShootEffect.eff.periodic.magnitude = 0x7FFF;
844                 pShootEffect.eff.periodic.fade_length = 500;
845
846                 if ( SDL_HapticUpdateEffect(haptic, pShootEffect.id, &pShootEffect.eff) < 0 ) {
847                         mprintf(("HapticERROR:  Unable to update pShootEffect:\n  %s\n", SDL_GetError()));
848                 }
849
850                 joy_ff_start_effect(&pShootEffect, "ShootEffect (Explode)");
851         }
852
853         Joy_ff_afterburning = 0;
854 }
855
856 void joy_ff_fly_by(int mag)
857 {
858         int gain;
859
860         if ( !Joy_ff_enabled ) {
861                 return;
862         }
863
864         if ( !Joy_ff_acquired ) {
865                 return;
866         }
867
868         if (Joy_ff_afterburning) {
869                 return;
870         }
871
872         if ( !(pAfterburn1.loaded && pAfterburn2.loaded) ) {
873                 return;
874         }
875
876         gain = mag * 120 + 4000;
877
878         if (gain < 1) {
879                 gain = 1;
880         } else if (gain > 10000) {
881                 gain = 10000;
882         }
883
884         SDL_HapticStopEffect(haptic, pAfterburn1.id);
885
886         pAfterburn1.eff.periodic.length = (6000 * mag + 400000) / 1000;
887         pAfterburn1.eff.periodic.period = 20;
888         pAfterburn1.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
889         pAfterburn1.eff.periodic.attack_length = 0;
890
891         if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
892                 mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
893         }
894
895
896         SDL_HapticStopEffect(haptic, pAfterburn2.id);
897
898         pAfterburn2.eff.periodic.length = (6000 * mag + 400000) / 1000;
899         pAfterburn2.eff.periodic.period = 100;
900         pAfterburn2.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
901         pAfterburn2.eff.periodic.attack_length = 0;
902
903         if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
904                 mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
905         }
906
907         joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Fly by)");
908         joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Fly by)");
909 }
910
911 void joy_ff_deathroll()
912 {
913         if ( !Joy_ff_enabled ) {
914                 return;
915         }
916
917         if ( !Joy_ff_acquired ) {
918                 return;
919         }
920 /*
921         if (pDeathroll1.loaded) {
922                 SDL_HapticStopEffect(haptic, pDeathroll1.id);
923         }
924
925         if (pDeathroll2.loaded) {
926                 SDL_HapticStopEffect(haptic, pDeathroll2.id);
927         }
928
929         joy_ff_start_effect(&pDeathroll1, "Deathroll1");
930         joy_ff_start_effect(&pDeathroll2, "Deathroll2");
931 */
932
933         if (pAfterburn1.loaded && pAfterburn2.loaded) {
934                 SDL_HapticStopEffect(haptic, pAfterburn1.id);
935
936                 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
937                 pAfterburn1.eff.periodic.period = 200;
938                 pAfterburn1.eff.periodic.magnitude = 0x7FFF;
939                 pAfterburn1.eff.periodic.attack_length = 200;
940
941                 if ( SDL_HapticUpdateEffect(haptic, pAfterburn1.id, &pAfterburn1.eff) < 0 ) {
942                         mprintf(("HapticERROR:  Unable to update pAfterburn1:\n  %s\n", SDL_GetError()));
943                 }
944
945                 SDL_HapticStopEffect(haptic, pAfterburn2.id);
946
947                 pAfterburn2.eff.periodic.length = SDL_HAPTIC_INFINITY;
948                 pAfterburn2.eff.periodic.period = 200;
949                 pAfterburn2.eff.periodic.magnitude = 0x7FFF;
950                 pAfterburn2.eff.periodic.attack_length = 200;
951
952                 if ( SDL_HapticUpdateEffect(haptic, pAfterburn2.id, &pAfterburn2.eff) < 0 ) {
953                         mprintf(("HapticERROR:  Unable to update pAfterburn2:\n  %s\n", SDL_GetError()));
954                 }
955
956                 joy_ff_start_effect(&pAfterburn1, "Afterburn1 (Death Roll)");
957                 joy_ff_start_effect(&pAfterburn2, "Afterburn2 (Death Roll)");
958
959                 Joy_ff_afterburning = 1;
960         }
961 }