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