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