]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/joy_ff.cpp
reset ff afterburn flag at start of mission
[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 static int Joy_ff_afterburning = 0;
25
26 typedef struct {
27         SDL_HapticEffect eff;
28         int id;
29         int loaded;
30 } haptic_effect_t;
31
32 static haptic_effect_t pHitEffect1;
33 static haptic_effect_t pHitEffect2;
34 static haptic_effect_t pAfterburn1;
35 static haptic_effect_t pAfterburn2;
36 static haptic_effect_t pShootEffect;
37 static haptic_effect_t pSecShootEffect;
38 static haptic_effect_t pSpring;
39 static haptic_effect_t pDock;
40 //static haptic_effect_t pDeathroll1;
41 //static haptic_effect_t pDeathroll2;
42 //static haptic_effect_t pExplode;
43
44 static void joy_ff_create_effects();
45 static int joy_ff_effect_playing(haptic_effect_t *eff);
46 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name);
47
48 extern SDL_Joystick *sdljoy;
49
50
51 int joy_ff_init()
52 {
53         int ff_enabled = 0;
54
55         ff_enabled = os_config_read_uint("Controls", "EnableJoystickFF", 0);
56
57         if ( !ff_enabled || !SDL_JoystickIsHaptic(sdljoy) ) {
58                 return 0;
59         }
60
61         if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
62                 mprintf(("  ERROR: Unable to initialize haptic subsystem\n"));
63                 return -1;
64         }
65
66         haptic = SDL_HapticOpenFromJoystick(sdljoy);
67
68         if (haptic == NULL) {
69                 mprintf(("  ERROR: Unable to open haptic joystick\n"));
70                 SDL_QuitSubSystem(SDL_INIT_HAPTIC);
71                 return -1;
72         }
73
74         if ( SDL_HapticRumbleSupported(haptic) ) {
75                 SDL_HapticRumbleInit(haptic);
76                 Joy_rumble = 1;
77         }
78
79         mprintf(("  Rumble  : %s\n", Joy_rumble ? "Yes" : "No"));
80         mprintf(("  Axes    : %d\n", SDL_HapticNumAxes(haptic)));
81         mprintf(("  Max effects     : %d\n", SDL_HapticNumEffects(haptic)));
82         mprintf(("  Running effects : %d\n", SDL_HapticNumEffectsPlaying(haptic)));
83
84         joy_ff_create_effects();
85
86         Joy_ff_enabled = 1;
87         Joy_ff_acquired = 1;
88
89         Joy_ff_directional_hit_effect_enabled = os_config_read_uint("Controls", "EnableHitEffect", 1);
90
91         return 0;
92 }
93
94 void joy_ff_shutdown()
95 {
96         if ( !Joy_ff_enabled ) {
97                 return;
98         }
99
100         Joy_rumble = 0;
101
102         SDL_HapticClose(haptic);
103         haptic = NULL;
104
105         SDL_QuitSubSystem(SDL_INIT_HAPTIC);
106
107         Joy_ff_acquired = 0;
108         Joy_ff_enabled = 0;
109 }
110
111 static void joy_ff_create_effects()
112 {
113         // clear all SDL errors
114         SDL_ClearError();
115
116         unsigned int supported = 0;
117
118         supported = SDL_HapticQuery(haptic);
119
120         if ( !(supported & SDL_HAPTIC_CONSTANT) ) {
121                 mprintf(("  Constant Force  : not supported\n"));
122         }
123
124         if ( !(supported & SDL_HAPTIC_SINE) ) {
125                 mprintf(("  Sine Wave       : not supported\n"));
126                 Warning(LOCATION, "Sine Wave: not supported");
127         }
128
129         if ( !(supported & SDL_HAPTIC_SAWTOOTHDOWN) ) {
130                 mprintf(("  Sawtooth Down   : not supported\n"));
131         }
132
133         if ( !(supported & SDL_HAPTIC_SPRING) ) {
134                 mprintf(("  Spring          : not supported\n"));
135         //      Error(LOCATION, "Spring: not supported");
136         }
137 /*
138         if ( !(supported & SDL_HAPTIC_SQUARE) ) {
139                 mprintf(("  Square          : not supported\n"));
140         }
141 */
142         if ( !(supported & SDL_HAPTIC_TRIANGLE) ) {
143                 mprintf(("  Triangle        : not supported\n"));
144         }
145
146
147         if (supported & SDL_HAPTIC_CONSTANT) {
148                 // pHitEffect1
149                 memset(&pHitEffect1, 0, sizeof(haptic_effect_t));
150
151                 pHitEffect1.eff.type = SDL_HAPTIC_CONSTANT;
152                 pHitEffect1.eff.constant.direction.type = SDL_HAPTIC_POLAR;
153                 pHitEffect1.eff.constant.direction.dir[0] = 0;
154                 pHitEffect1.eff.constant.length = 300;
155                 pHitEffect1.eff.constant.level = 0x7FFF;
156                 pHitEffect1.eff.constant.attack_length = 0;
157                 pHitEffect1.eff.constant.attack_level = 0x7FFF;
158                 pHitEffect1.eff.constant.fade_length = 120;
159                 pHitEffect1.eff.constant.fade_level = 1;
160
161                 pHitEffect1.id = SDL_HapticNewEffect(haptic, &pHitEffect1.eff);
162
163                 if (pHitEffect1.id < 0) {
164                         mprintf(("    Hit effect 1 failed to load:\n      %s\n", SDL_GetError()));
165                 } else {
166                         pHitEffect1.loaded = 1;
167                 }
168         }
169
170         if (supported & SDL_HAPTIC_SINE) {
171                 // pHitEffect2
172                 memset(&pHitEffect2, 0, sizeof(haptic_effect_t));
173
174                 pHitEffect2.eff.type = SDL_HAPTIC_SINE;
175                 pHitEffect2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
176                 pHitEffect2.eff.periodic.direction.dir[0] = 9000;
177                 pHitEffect2.eff.periodic.length = 300;
178                 pHitEffect2.eff.periodic.period = 100;
179                 pHitEffect2.eff.periodic.magnitude = 0x7FFF;
180                 pHitEffect2.eff.periodic.attack_length = 100;
181                 pHitEffect2.eff.periodic.fade_length = 100;
182
183                 pHitEffect2.id = SDL_HapticNewEffect(haptic, &pHitEffect2.eff);
184
185                 if (pHitEffect2.id < 0) {
186                         mprintf(("    Hit effect 2 failed to load:\n      %s\n", SDL_GetError()));
187                 } else {
188                         pHitEffect2.loaded = 1;
189                 }
190         }
191
192         if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
193                 // pShootEffect
194                 memset(&pShootEffect, 0, sizeof(haptic_effect_t));
195
196                 pShootEffect.eff.type = SDL_HAPTIC_SAWTOOTHDOWN;
197                 pShootEffect.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
198                 pShootEffect.eff.periodic.direction.dir[0] = 0;
199                 pShootEffect.eff.periodic.length = 160;
200                 pShootEffect.eff.periodic.period = 20;
201                 pShootEffect.eff.periodic.magnitude = 0x7FFF;
202                 pShootEffect.eff.periodic.fade_length = 120;
203
204                 pShootEffect.id = SDL_HapticNewEffect(haptic, &pShootEffect.eff);
205
206                 if (pShootEffect.id < 0) {
207                         mprintf(("    Fire primary effect failed to load:\n      %s\n", SDL_GetError()));
208                 } else {
209                         pShootEffect.loaded = 1;
210                 }
211         }
212
213         if (supported & SDL_HAPTIC_CONSTANT) {
214                 // pSecShootEffect
215                 memset(&pSecShootEffect, 0, sizeof(haptic_effect_t));
216
217                 pSecShootEffect.eff.type = SDL_HAPTIC_CONSTANT;
218                 pSecShootEffect.eff.constant.direction.type = SDL_HAPTIC_POLAR;
219                 pSecShootEffect.eff.constant.direction.dir[0] = 0;
220                 pSecShootEffect.eff.constant.length = 200;
221                 pSecShootEffect.eff.constant.level = 0x7FFF;
222                 pSecShootEffect.eff.constant.attack_length = 50;
223                 pSecShootEffect.eff.constant.attack_level = 0x7FFF;
224                 pSecShootEffect.eff.constant.fade_length = 100;
225                 pSecShootEffect.eff.constant.fade_level = 1;
226
227                 pSecShootEffect.id = SDL_HapticNewEffect(haptic, &pSecShootEffect.eff);
228
229                 if (pSecShootEffect.id < 0) {
230                         mprintf(("    Fire secondary effect failed to load:\n      %s\n", SDL_GetError()));
231                 } else {
232                         pSecShootEffect.loaded = 1;
233                 }
234         }
235
236         if (supported & SDL_HAPTIC_SPRING) {
237                 // pSpring
238                 memset(&pSpring, 0, sizeof(haptic_effect_t));
239
240                 pSpring.eff.type = SDL_HAPTIC_SPRING;
241                 pSpring.eff.condition.length = SDL_HAPTIC_INFINITY;
242
243                 for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
244                         pSpring.eff.condition.right_sat[i] = 0x7FFF;
245                         pSpring.eff.condition.left_sat[i] = 0x7FFF;
246                         pSpring.eff.condition.right_coeff[i] = 0x147;
247                         pSpring.eff.condition.left_coeff[i] = 0x147;
248                 }
249
250                 pSpring.id = SDL_HapticNewEffect(haptic, &pSpring.eff);
251
252                 if (pSpring.id < 0) {
253                         mprintf(("    Spring effect failed to load:\n      %s\n", SDL_GetError()));
254                 } else {
255                         pSpring.loaded = 1;
256                         joy_ff_start_effect(&pSpring, "Spring");
257                 }
258         }
259
260         if (supported & SDL_HAPTIC_SINE) {
261                 // pAfterburn1
262                 memset(&pAfterburn1, 0, sizeof(haptic_effect_t));
263
264                 pAfterburn1.eff.type = SDL_HAPTIC_SINE;
265                 pAfterburn1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
266                 pAfterburn1.eff.periodic.direction.dir[0] = 0;
267                 pAfterburn1.eff.periodic.length = SDL_HAPTIC_INFINITY;
268                 pAfterburn1.eff.periodic.period = 20;
269                 pAfterburn1.eff.periodic.magnitude = 0x3332;
270
271                 pAfterburn1.id = SDL_HapticNewEffect(haptic, &pAfterburn1.eff);
272
273                 if (pAfterburn1.id < 0) {
274                         mprintf(("    Afterburn effect 1 failed to load:\n      %s\n", SDL_GetError()));
275                 } else {
276                         pAfterburn1.loaded = 1;
277                 }
278
279                 // pAfterburn2
280                 memset(&pAfterburn2, 0, sizeof(haptic_effect_t));
281
282                 pAfterburn2.eff.type = SDL_HAPTIC_SINE;
283                 pAfterburn2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
284                 pAfterburn2.eff.periodic.direction.dir[0] = 9000;
285                 pAfterburn2.eff.periodic.length = 125;
286                 pAfterburn2.eff.periodic.period = 100;
287                 pAfterburn2.eff.periodic.magnitude = 0x1999;
288
289                 pAfterburn2.id = SDL_HapticNewEffect(haptic, &pAfterburn2.eff);
290
291                 if (pAfterburn2.id < 0) {
292                         mprintf(("    Afterburn effect 2 failed to load:\n      %s\n", SDL_GetError()));
293                 } else {
294                         pAfterburn2.loaded = 1;
295                 }
296         }
297
298
299 //      if (supported & SDL_HAPTIC_SQUARE) {
300         if (supported & SDL_HAPTIC_TRIANGLE) {
301                 // pDock
302                 memset(&pDock, 0, sizeof(haptic_effect_t));
303
304         //      pDock.eff.type = SDL_HAPTIC_SQUARE;
305                 pDock.eff.type = SDL_HAPTIC_TRIANGLE;
306                 pDock.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
307                 pDock.eff.periodic.direction.dir[0] = 9000;
308                 pDock.eff.periodic.length = 125;
309                 pDock.eff.periodic.period = 100;
310                 pDock.eff.periodic.magnitude = 0x3332;
311
312                 pDock.id = SDL_HapticNewEffect(haptic, &pDock.eff);
313
314                 if (pDock.id < 0) {
315                         mprintf(("    Dock effect failed to load:\n      %s\n", SDL_GetError()));
316                 } else {
317                         pDock.loaded = 1;
318                 }
319         }
320
321 /*
322         if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
323                 // pExplode
324                 memset(&pExplode, 0, sizeof(haptic_effect_t));
325
326                 pExplode.eff.type = SDL_HAPTIC_SAWTOOTHDOWN;
327                 pExplode.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
328                 pExplode.eff.periodic.direction.dir[0] = 9000;
329                 pExplode.eff.periodic.length = 500;
330                 pExplode.eff.periodic.period = 20;
331                 pExplode.eff.periodic.magnitude = 0x7FFF;
332                 pExplode.eff.periodic.attack_length = 0;
333                 pExplode.eff.periodic.fade_length = 500;
334
335                 pExplode.id = SDL_HapticNewEffect(haptic, &pExplode.eff);
336
337                 if (pExplode.id < 0) {
338                         mprintf(("    Explosion effect failed to load:\n      %s\n", SDL_GetError()));
339                 } else {
340                         pExplode.loaded = 1;
341                 }
342         }
343
344         if (supported & SDL_HAPTIC_SINE) {
345                 // pDeathroll1
346                 memset(&pDeathroll1, 0, sizeof(haptic_effect_t));
347
348                 pDeathroll1.eff.type = SDL_HAPTIC_SINE;
349                 pDeathroll1.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
350                 pDeathroll1.eff.periodic.direction.dir[0] = 0;
351                 pDeathroll1.eff.periodic.length = SDL_HAPTIC_INFINITY;
352                 pDeathroll1.eff.periodic.period = 200;
353                 pDeathroll1.eff.periodic.magnitude = 0x7FFF;
354                 pDeathroll1.eff.periodic.attack_length = 200;
355
356                 pDeathroll1.id = SDL_HapticNewEffect(haptic, &pDeathroll1.eff);
357
358                 if (pDeathroll1.id < 0) {
359                         mprintf(("    Deathroll effect 1 failed to load:\n      %s\n", SDL_GetError()));
360                 } else {
361                         pDeathroll1.loaded = 1;
362                 }
363
364                 // pDeathroll2
365                 memset(&pDeathroll2, 0, sizeof(haptic_effect_t));
366
367                 pDeathroll2.eff.type = SDL_HAPTIC_SINE;
368                 pDeathroll2.eff.periodic.direction.type = SDL_HAPTIC_POLAR;
369                 pDeathroll2.eff.periodic.direction.dir[0] = 9000;
370                 pDeathroll2.eff.periodic.length = SDL_HAPTIC_INFINITY;
371                 pDeathroll2.eff.periodic.period = 200;
372                 pDeathroll2.eff.periodic.magnitude = 0x7FFF;
373                 pDeathroll2.eff.periodic.attack_length = 200;
374
375                 pDeathroll2.id = SDL_HapticNewEffect(haptic, &pDeathroll2.eff);
376
377                 if (pDeathroll2.id < 0) {
378                         mprintf(("    Deathroll effect 2 failed to load:\n      %s\n", SDL_GetError()));
379                 } else {
380                         pDeathroll2.loaded = 1;
381                 }
382         }
383 */
384 }
385
386 static void joy_ff_start_effect(haptic_effect_t *eff, const char *name)
387 {
388         if ( !eff->loaded ) {
389                 return;
390         }
391
392 //      nprintf(("Joystick", "FF: Starting effect %s\n", name));
393
394         if ( SDL_HapticRunEffect(haptic, eff->id, 1) ) {
395                 mprintf(("HapticERROR:  Unable to run %s:\n  %s\n", name, SDL_GetError()));
396         }
397 }
398
399 void joy_ff_stop_effects()
400 {
401         if ( !Joy_ff_enabled ) {
402                 return;
403         }
404
405         SDL_HapticStopAll(haptic);
406 }
407
408 void joy_ff_mission_init(vector v)
409 {
410         v.xyz.z = 0.0f;
411
412         joy_ff_handling_scaler = (int) ((vm_vec_mag(&v) + 1.3f) * 5.0f);
413
414         Joy_ff_afterburning = 0;
415 }
416
417 void joy_reacquire_ff()
418 {
419         if ( !Joy_ff_enabled ) {
420                 return;
421         }
422
423         if (Joy_ff_acquired) {
424                 return;
425         }
426
427         joy_ff_start_effect(&pSpring, "Spring");
428
429         Joy_ff_acquired = 1;
430 }
431
432 void joy_unacquire_ff()
433 {
434         if ( !Joy_ff_enabled ) {
435                 return;
436         }
437
438         if ( !Joy_ff_acquired ) {
439                 return;
440         }
441
442         joy_ff_stop_effects();
443
444         Joy_ff_acquired = 0;
445 }
446
447 void joy_ff_play_vector_effect(vector *v, float scaler)
448 {
449         vector vf;
450         float x, y;
451
452 //      nprintf(("Joystick", "FF: vec = { %f, %f, %f } s = %f\n", v->xyz.x, v->xyz.y, v->xyz.z, scaler));
453         vm_vec_copy_scale(&vf, v, scaler);
454         x = vf.xyz.x;
455         vf.xyz.x = 0.0f;
456
457         if (vf.xyz.y + vf.xyz.z < 0.0f) {
458                 y = -vm_vec_mag(&vf);
459         } else {
460                 y = vm_vec_mag(&vf);
461         }
462
463         joy_ff_play_dir_effect(-x, -y);
464 }
465
466 void joy_ff_play_dir_effect(float x, float y)
467 {
468         int idegs, imag;
469         float degs;
470
471         if ( !Joy_ff_enabled ) {
472                 return;
473         }
474
475         if ( !Joy_ff_acquired ) {
476                 return;
477         }
478
479         // allow for at least one of the effects to work
480         if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) {
481                 return;
482         }
483
484         if (joy_ff_effect_playing(&pHitEffect1) || joy_ff_effect_playing(&pHitEffect2)) {
485                 nprintf(("Joystick", "FF: HitEffect already playing.  Skipping\n"));
486                 return;
487         }
488
489         if (Joy_ff_directional_hit_effect_enabled) {
490                 if (x > 8000.0f) {
491                         x = 8000.0f;
492                 } else if (x < -8000.0f) {
493                         x = -8000.0f;
494                 }
495
496                 if (y > 8000.0f) {
497                         y = 8000.0f;
498                 } else if (y < -8000.0f) {
499                         y = -8000.0f;
500                 }
501
502                 imag = (int) fl_sqrt(x * x + y * y);
503                 if (imag > 10000) {
504                         imag = 10000;
505                 }
506
507                 degs = (float)atan2(x, y);
508                 idegs = (int) (degs * 18000.0f / PI) + 90;
509                 while (idegs < 0) {
510                         idegs += 36000;
511                 }
512
513                 while (idegs >= 36000) {
514                         idegs -= 36000;
515                 }
516
517                 if (pHitEffect1.loaded) {
518                         pHitEffect1.eff.constant.direction.dir[0] = idegs;
519                         pHitEffect1.eff.constant.level = (Sint16)(32767.0f * (imag / 10000.0f));
520
521                         if ( SDL_HapticUpdateEffect(haptic, pHitEffect1.id, &pHitEffect1.eff) < 0 ) {
522                                 mprintf(("HapticERROR:  Unable to update pHitEffect1:\n  %s\n", SDL_GetError()));
523                         }
524                 }
525
526                 idegs += 9000;
527                 if (idegs >= 36000)
528                         idegs -= 36000;
529
530                 if (pHitEffect2.loaded) {
531                         pHitEffect2.eff.periodic.direction.dir[0] = idegs;
532                         pHitEffect2.eff.periodic.magnitude = (Sint16)(32767.0f * (imag / 10000.0f));
533
534                         if ( SDL_HapticUpdateEffect(haptic, pHitEffect2.id, &pHitEffect2.eff) < 0 ) {
535                                 mprintf(("HapticERROR:  Unable to update pHitEffect2:\n  %s\n", SDL_GetError()));
536                         }
537                 }
538         }
539
540         joy_ff_start_effect(&pHitEffect1, "HitEffect1");
541         joy_ff_start_effect(&pHitEffect2, "HitEffect2");
542 }
543
544 static int primary_ff_level = 10000;
545
546 void joy_ff_play_primary_shoot(int gain)
547 {
548         if ( !Joy_ff_enabled ) {
549                 return;
550         }
551
552         if ( !Joy_ff_acquired ) {
553                 return;
554         }
555
556         if ( !pShootEffect.loaded && !Joy_rumble ) {
557                 return;
558         }
559
560         if (gain < 1) {
561                 gain = 1;
562         } else if (gain > 10000) {
563                 gain = 10000;
564         }
565
566         if (pShootEffect.loaded) {
567                 SDL_HapticStopEffect(haptic, pShootEffect.id);
568
569                 if (gain != primary_ff_level) {
570                         pShootEffect.eff.periodic.direction.dir[0] = 0;
571                         pShootEffect.eff.periodic.length = 160;
572                         pShootEffect.eff.periodic.magnitude = (Sint16)(32767.0f * (gain / 10000.0f));
573                         pShootEffect.eff.periodic.fade_length = 120;
574
575                         if ( SDL_HapticUpdateEffect(haptic, pShootEffect.id, &pShootEffect.eff) < 0 ) {
576                                 mprintf(("HapticERROR:  Unable to update pShootEffect:\n  %s\n", SDL_GetError()));
577                         }
578
579                         primary_ff_level = gain;
580                 }
581
582                 joy_ff_start_effect(&pShootEffect, "ShootEffect");
583         } else if (Joy_rumble) {
584                 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, 100);
585         }
586 }
587
588 static int secondary_ff_level = 10000;
589
590 void joy_ff_play_secondary_shoot(int gain)
591 {
592         if ( !Joy_ff_enabled ) {
593                 return;
594         }
595
596         if ( !Joy_ff_acquired ) {
597                 return;
598         }
599
600         if ( !pSecShootEffect.loaded && !Joy_rumble ) {
601                 return;
602         }
603
604         gain = gain * 100 + 2500;
605
606         if (gain < 1) {
607                 gain = 1;
608         } else if (gain > 10000) {
609                 gain = 10000;
610         }
611
612         if (pSecShootEffect.loaded) {
613                 SDL_HapticStopEffect(haptic, pSecShootEffect.id);
614
615                 if (gain != secondary_ff_level) {
616                         pSecShootEffect.eff.constant.level = (Sint16)(32767.0f * (gain / 10000.0f));
617                         pSecShootEffect.eff.constant.length = (150000 + gain * 25) / 1000;
618
619                         if ( SDL_HapticUpdateEffect(haptic, pSecShootEffect.id, &pSecShootEffect.eff) < 0 ) {
620                                 mprintf(("HapticERROR:  Unable to update pSecShootEffect:\n  %s\n", SDL_GetError()));
621                         }
622
623                         secondary_ff_level = gain;
624                         nprintf(("Joystick", "FF: Secondary force = 0x%04x\n", pSecShootEffect.eff.constant.level));
625                 }
626
627                 joy_ff_start_effect(&pSecShootEffect, "SecShootEffect");
628         } else if (Joy_rumble) {
629                 SDL_HapticRumblePlay(haptic, (gain / 10000.0f) * 0.5f, (150000 + gain * 25) / 1000);
630         }
631 }
632
633 void joy_ff_adjust_handling(int speed)
634 {
635         int v;
636         short coeff = 0;
637
638         if ( !Joy_ff_enabled ) {
639                 return;
640         }
641
642         if ( !Joy_ff_acquired ) {
643                 return;
644         }
645
646         if ( !pSpring.loaded ) {
647                 return;
648         }
649
650         static int last_speed = -1000;
651
652         if (speed == last_speed) {
653                 return;
654         }
655
656         last_speed = speed;
657
658         v = speed * joy_ff_handling_scaler * 2 / 3;
659 //      v += joy_ff_handling_scaler * joy_ff_handling_scaler * 6 / 7 + 250;
660         v += joy_ff_handling_scaler * 45 - 500;
661
662         if (v < 0) {
663                 v = 0;
664         } else if (v > 10000) {
665                 v = 10000;
666         }
667
668         coeff = (short)(32767.0f * (v / 10000.0f));
669
670         for (int i = 0; i < SDL_HapticNumAxes(haptic); i++) {
671                 pSpring.eff.condition.right_coeff[i] = coeff;
672                 pSpring.eff.condition.left_coeff[i] = coeff;
673         }
674
675 //      nprintf(("Joystick", "FF: New handling force = 0x%04x\n", coeff));
676
677         SDL_HapticUpdateEffect(haptic, pSpring.id, &pSpring.eff);
678 }
679
680 static int joy_ff_effect_playing(haptic_effect_t *eff)
681 {
682         if ( !eff->loaded ) {
683                 return 0;
684         } else {
685                 return (SDL_HapticGetEffectStatus(haptic, eff->id) > 0);
686         }
687 }
688
689 void joy_ff_docked()
690 {
691         if ( !Joy_ff_enabled ) {
692                 return;
693         }
694
695         if ( !Joy_ff_acquired ) {
696                 return;
697         }
698
699         if ( !pDock.loaded ) {
700                 return;
701         }
702
703         SDL_HapticStopEffect(haptic, pDock.id);
704
705         pDock.eff.periodic.magnitude = 0x7fff;
706
707         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
708                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
709         }
710
711         joy_ff_start_effect(&pDock, "Dock");
712 }
713
714 void joy_ff_play_reload_effect()
715 {
716         if ( !Joy_ff_enabled ) {
717                 return;
718         }
719
720         if ( !Joy_ff_acquired ) {
721                 return;
722         }
723
724         if ( !pDock.loaded ) {
725                 return;
726         }
727
728         SDL_HapticStopEffect(haptic, pDock.id);
729
730         pDock.eff.periodic.magnitude = 0x3fff;
731
732         if ( SDL_HapticUpdateEffect(haptic, pDock.id, &pDock.eff) < 0 ) {
733                 mprintf(("HapticERROR:  Unable to update pDock:\n  %s\n", SDL_GetError()));
734         }
735
736         joy_ff_start_effect(&pDock, "Dock (Reload)");
737 }
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 = 0x3332;
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 = 0x1999;
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 }