Oolite
Loading...
Searching...
No Matches
PlayerEntitySound.m
Go to the documentation of this file.
1/*
2
3PlayerEntitySound.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "PlayerEntitySound.h"
26#import "OOEquipmentType.h"
27#import "OOMaths.h"
28#import "OOSound.h"
29#import "OOSoundSourcePool.h"
30#import "ResourceManager.h"
31#import "Universe.h"
32
33// Sizes of sound source pools
34enum {
40};
41
52
53static NSDictionary* weaponShotMiss;
54static NSDictionary* weaponShotHit;
55static NSDictionary* weaponShieldHit;
56static NSDictionary* weaponUnshieldedHit;
57static NSDictionary* weaponLaunched;
58
59static const Vector kInterfaceBeepPosition = { 0.0f, -0.2f, 0.5f };
60static const Vector kInterfaceWarningPosition = { 0.0f, -0.2f, 0.4f };
61static const Vector kBreakPatternPosition = { 0.0f, 0.0f, 1.0f };
62static const Vector kEcmPosition = { 0.2f, 0.6f, -0.1f };
63static const Vector kWitchspacePosition = { 0.0f, -0.3f, -0.3f };
64// maybe these should actually track engine positions
65static const Vector kAfterburner1Position = { -0.1f, 0.0f, -1.0f };
66static const Vector kAfterburner2Position = { 0.1f, 0.0f, -1.0f };
67
68@implementation PlayerEntity (Sound)
69
71{
72 [self destroySound];
73
74 sInterfaceBeepSource = [[OOSoundSource alloc] init];
75 [sInterfaceBeepSource setPosition:kInterfaceBeepPosition];
76
77 sBreakPatternSource = [[OOSoundSource alloc] init];
78 [sBreakPatternSource setPosition:kBreakPatternPosition];
79
80 sEcmSource = [[OOSoundSource alloc] init];
81 [sEcmSource setPosition:kEcmPosition];
82
84 [sHyperspaceSoundSource setPosition:kWitchspacePosition];
85
86 sBuySellSourcePool = [[OOSoundSourcePool alloc] initWithCount:kBuySellSourcePoolSize minRepeatTime:0.0];
87 sWarningSoundPool = [[OOSoundSourcePool alloc] initWithCount:kWarningPoolSize minRepeatTime:0.0];
88 sWeaponSoundPool = [[OOSoundSourcePool alloc] initWithCount:kWeaponPoolSize minRepeatTime:0.0];
89 sDamageSoundPool = [[OOSoundSourcePool alloc] initWithCount:kDamagePoolSize minRepeatTime:0.1]; // Repeat time limit is to avoid playing a scrape sound every frame on glancing scrapes. This does limit the number of laser hits that can be played in a furrball, though; maybe lasers and scrapes should use different pools.
90 sMiscSoundPool = [[OOSoundSourcePool alloc] initWithCount:kMiscPoolSize minRepeatTime:0.0];
91
92 // Two sources with the same sound are used to simulate looping.
93 OOSound* afterburnerSound = [ResourceManager ooSoundNamed:@"afterburner1.ogg" inFolder:@"Sounds"];
94 sAfterburnerSources[0] = [[OOSoundSource alloc] initWithSound:afterburnerSound];
95 [sAfterburnerSources[0] setPosition:kAfterburner1Position];
96 sAfterburnerSources[1] = [[OOSoundSource alloc] initWithSound:afterburnerSound];
97 [sAfterburnerSources[1] setPosition:kAfterburner2Position];
98}
99
100// sets up the sound key dictionaries for all the available weapons/missiles/mines defined.
102{
103 NSArray* eqTypes = [OOEquipmentType allEquipmentTypes];
104 NSMutableDictionary* shotMissSounds = [NSMutableDictionary dictionary];
105 NSMutableDictionary* shotHitSounds = [NSMutableDictionary dictionary];
106 NSMutableDictionary* shieldHitSounds = [NSMutableDictionary dictionary];
107 NSMutableDictionary* unshieldedHitSounds = [NSMutableDictionary dictionary];
108 NSMutableDictionary* weaponLaunchedSounds = [NSMutableDictionary dictionary];
109 NSEnumerator* eqTypeEnum = nil;
110 OOEquipmentType* eqType = nil;
111
112 // special case: turrets aren't defined with a "EQ_WEAPON" prefix, and plasma shots don't have a matching equipment item,
113 // so add a unique entry here. this could be overridden if an OXP creates an equipment item with this key.
114 // plasma shots don't make a sound when fired, so we only need to provide for the hit player sound keys.
115 [shieldHitSounds setObject:@"[player-hit-by-weapon]" forKey:@"EQ_WEAPON_PLASMA_SHOT"];
116 [unshieldedHitSounds setObject:@"[player-direct-hit]" forKey:@"EQ_WEAPON_PLASMA_SHOT"];
117// grab a local copy of the sound identifiers for weapons to make the process of looking up a sound ref as fast as possible
118// but we must ensure that no nil values are used for setObject
119#define OO_ASSIGN_SOUNDSTR_TO_SOUNDS(soundStr, sounds) \
120 do { \
121 fxString = [eqType soundStr]; \
122 if (!fxString) \
123 fxString = @""; \
124 [sounds setObject:fxString forKey:[eqType identifier]]; \
125 } while (0)
126
127 for (eqTypeEnum = [eqTypes objectEnumerator]; (eqType = [eqTypeEnum nextObject]);) {
128 NSString* fxString = nil;
129 if ([[eqType identifier] hasPrefix:@"EQ_WEAPON"]) {
130 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxShotMissName, shotMissSounds);
131 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxShotHitName, shotHitSounds);
132 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxShieldHitName, shieldHitSounds);
133 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxUnshieldedHitName, unshieldedHitSounds);
134 }
135 if ([eqType isMissileOrMine]) {
136 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxWeaponLaunchedName, weaponLaunchedSounds);
137 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxShieldHitName, shieldHitSounds);
138 OO_ASSIGN_SOUNDSTR_TO_SOUNDS(fxUnshieldedHitName, unshieldedHitSounds);
139 }
140 }
141
147
148 weaponShotMiss = [[NSDictionary alloc] initWithDictionary:shotMissSounds];
149 weaponShotHit = [[NSDictionary alloc] initWithDictionary:shotHitSounds];
150 weaponShieldHit = [[NSDictionary alloc] initWithDictionary:shieldHitSounds];
151 weaponUnshieldedHit = [[NSDictionary alloc] initWithDictionary:unshieldedHitSounds];
152 weaponLaunched = [[NSDictionary alloc] initWithDictionary:weaponLaunchedSounds];
153}
154
177
178- (void)playInterfaceBeep:(NSString*)beepKey
179{
180#if OOLITE_WINDOWS
181 if ([self status] == STATUS_START_GAME) {
182 return;
183 }
184#endif
186}
187
189{
191}
192
193- (void)boop
194{
195 [self playInterfaceBeep:@"[general-boop]"];
196}
197
199{
200 [self playInterfaceBeep:@"[ident-on]"];
201}
202
204{
205 [self playInterfaceBeep:@"[ident-off]"];
206}
207
209{
210 [self playInterfaceBeep:@"[ident-locked-on]"];
211}
212
214{
215 [self playInterfaceBeep:@"[missile-armed]"];
216}
217
219{
220 [self playInterfaceBeep:@"[mine-armed]"];
221}
222
224{
225 [self playInterfaceBeep:@"[missile-safe]"];
226}
227
229{
230 [self playInterfaceBeep:@"[missile-locked-on]"];
231}
232
234{
235 [self playInterfaceBeep:@"[next-equipment-selected]"];
236}
237
239{
240 [self playInterfaceBeep:@"[next-missile-selected]"];
241}
242
244{
245 [self playInterfaceBeep:@"[weapons-online]"];
246}
247
249{
250 [self playInterfaceBeep:@"[weapons-offline]"];
251}
252
254{
255 [self playInterfaceBeep:@"[cargo-jettisoned]"];
256}
257
259{
260 [self playInterfaceBeep:@"[autopilot-on]"];
261}
262
264{
265 // only if still alive
266 if (energy > 0.0) {
267 [self playInterfaceBeep:@"[autopilot-off]"];
268 }
269}
270
272{
273 [self playInterfaceBeep:@"[autopilot-out-of-range]"];
274}
275
277{
278 [self playInterfaceBeep:@"[autopilot-cannot-dock-with-target]"];
279}
280
282{
283 [self playInterfaceBeep:@"[save-overwrite-yes]"];
284}
285
287{
288 [self playInterfaceBeep:@"[save-overwrite-no]"];
289}
290
292{
293 [self playInterfaceBeep:@"[hold-full]"];
294}
295
297{
298 [self playInterfaceBeep:@"[jump-mass-locked]"];
299}
300
302{
303 [self playInterfaceBeep:@"[target-lost]"];
304}
305
307{
308 [self playInterfaceBeep:@"[no-target-in-memory]"];
309}
310
312{
313 [self playInterfaceBeep:@"[target-switched]"];
314}
315
317{
318 [self playInterfaceBeep:@"[witch-no-target]"];
319}
320
322{
323 [self playInterfaceBeep:@"[witch-no-fuel]"];
324}
325
327{
328 [self playInterfaceBeep:@"[hyperspace-blocked]"];
329}
330
332{
333 [self playInterfaceBeep:@"[witch-too-far]"];
334}
335
337{
338 [self playInterfaceBeep:@"[cloaking-device-on]"];
339}
340
342{
343 [self playInterfaceBeep:@"[cloaking-device-off]"];
344}
345
347{
348 [self playInterfaceBeep:@"[menu-navigation-up]"];
349}
350
352{
353 [self playInterfaceBeep:@"[menu-navigation-down]"];
354}
355
357{
358 [self playInterfaceBeep:@"[menu-navigation-not]"];
359}
360
362{
363 [self playInterfaceBeep:@"[menu-next-page]"];
364}
365
367{
368 [self playInterfaceBeep:@"[menu-previous-page]"];
369}
370
372{
373 [self playInterfaceBeep:@"[dismissed-report-screen]"];
374}
375
377{
378 [self playInterfaceBeep:@"[dismissed-mission-screen]"];
379}
380
382{
383 [self playInterfaceBeep:@"[changed-option]"];
384}
385
386- (void)updateFuelScoopSoundWithInterval:(OOTimeDelta)delta_t
387{
388 static double scoopSoundPlayTime = 0.0;
389 scoopSoundPlayTime -= delta_t;
390 if (scoopSoundPlayTime < 0.0) {
391 if (![sInterfaceBeepSource isPlaying]) {
392 /* TODO: this should use the scoop position, not the standard
393 * interface beep position */
394 [self playInterfaceBeep:@"[scoop]"];
395 scoopSoundPlayTime = 0.5;
396 } else
397 scoopSoundPlayTime = 0.0;
398 }
399 if (![self scoopOverride]) {
400 scoopSoundPlayTime = 0.0;
401 }
402}
403
404// time delay method for playing afterburner sounds
405// this overlaps two sounds each 2 seconds long, but with a 0.75s
406// crossfade
408{
409 static uint8_t which = 0;
410
411 if (!afterburner_engaged) // end the loop cycle
412 {
413 afterburnerSoundLooping = NO;
414 }
415
416 if (afterburnerSoundLooping) {
418 which = !which;
419
420 [self performSelector:@selector(updateAfterburnerSound)
421 withObject:NULL
422 afterDelay:1.25]; // and swap sounds in 1.25s time
423 }
424}
425
427{
428 if (!afterburnerSoundLooping) {
429 afterburnerSoundLooping = YES;
431 }
432}
433
435{
436 // Do nothing, stop is detected in updateAfterburnerSound
437}
438
440{
441 [self playInterfaceBeep:@"[cloaking-device-insufficent-energy]"];
442}
443
445{
446 [sBuySellSourcePool playSoundWithKey:@"[buy-commodity]"];
447}
448
450{
451 [sBuySellSourcePool playSoundWithKey:@"[buy-ship]"];
452}
453
455{
456 [sBuySellSourcePool playSoundWithKey:@"[sell-commodity]"];
457}
458
460{
461 [sBuySellSourcePool playSoundWithKey:@"[could-not-buy-commodity]"];
462}
463
465{
466 [sBuySellSourcePool playSoundWithKey:@"[could-not-sell-commodity]"];
467}
468
470{
471 [sBuySellSourcePool playSoundWithKey:@"[could-not-buy-ship]"];
472}
473
475{
476 [sHyperspaceSoundSource playCustomSoundWithKey:@"[hyperspace-countdown-begun]"];
477}
478
480{
481 [sHyperspaceSoundSource playCustomSoundWithKey:@"[galactic-hyperspace-countdown-begun]"];
482}
483
485{
486 [sHyperspaceSoundSource playCustomSoundWithKey:@"[hyperspace-countdown-aborted]"];
487}
488
490{
491 if (![sEcmSource isPlaying])
492 [sEcmSource playCustomSoundWithKey:@"[player-hit-by-ecm]"];
493}
494
496{
497 if (![sEcmSource isPlaying])
498 [sEcmSource playCustomSoundWithKey:@"[player-fired-ecm]"];
499}
500
502{
503 [sBreakPatternSource playCustomSoundWithKey:@"[player-launch-from-station]"];
504}
505
507{
508 [sBreakPatternSource playCustomSoundWithKey:@"[player-dock-with-station]"];
509}
510
512{
513 [sBreakPatternSource playCustomSoundWithKey:@"[player-exit-witchspace]"];
514}
515
517{
518 [sWarningSoundPool playSoundWithKey:@"[hostile-warning]" priority:1 position:kInterfaceWarningPosition];
519}
520
522{
523 [sWarningSoundPool playSoundWithKey:@"[alert-condition-red]" priority:2 position:kInterfaceWarningPosition];
524}
525
526- (void)playIncomingMissile:(Vector)missileVector
527{
528 [sWarningSoundPool playSoundWithKey:@"[incoming-missile]" priority:3 position:missileVector];
529}
530
532{
533 [sWarningSoundPool playSoundWithKey:@"[energy-low]" priority:0.5 position:kInterfaceWarningPosition];
534}
535
537{
538 [sWarningSoundPool playSoundWithKey:@"[autopilot-denied]" priority:1 position:kInterfaceWarningPosition];
539}
540
542{
543 [sWarningSoundPool playSoundWithKey:@"[witchdrive-failure]" priority:1.5 position:kWitchspacePosition];
544}
545
547{
548 [sWarningSoundPool playSoundWithKey:@"[witchdrive-malfunction]" priority:1.5 position:kWitchspacePosition];
549}
550
552{
553 [sWarningSoundPool playSoundWithKey:@"[witch-blocked-by-@]" priority:1.3 position:kWitchspacePosition];
554}
555
557{
558 [sWarningSoundPool playSoundWithKey:@"[witch-too-far]" priority:1.3 position:kWitchspacePosition];
559}
560
562{
563 [sWarningSoundPool playSoundWithKey:@"[witch-no-fuel]" priority:1.3 position:kWitchspacePosition];
564}
565
567{
568 [sWarningSoundPool playSoundWithKey:@"[fuel-leak]" priority:0.5 position:kWitchspacePosition];
569}
570
571- (void)playShieldHit:(Vector)attackVector weaponIdentifier:(NSString*)weaponIdentifier
572{
573 NSString* identifier = [weaponShieldHit objectForKey:weaponIdentifier];
574 if (!identifier)
575 identifier = @"[player-hit-by-weapon]";
576 [sDamageSoundPool playSoundWithKey:identifier position:attackVector];
577}
578
579- (void)playDirectHit:(Vector)attackVector weaponIdentifier:(NSString*)weaponIdentifier
580{
581 NSString* identifier = [weaponUnshieldedHit objectForKey:weaponIdentifier];
582 if (!identifier)
583 identifier = @"[player-direct-hit]";
584 [sDamageSoundPool playSoundWithKey:identifier position:attackVector];
585}
586
587- (void)playScrapeDamage:(Vector)attackVector
588{
589 [sDamageSoundPool playSoundWithKey:@"[player-scrape-damage]" position:attackVector];
590}
591
592- (void)playLaserHit:(BOOL)hit offset:(Vector)weaponOffset weaponIdentifier:(NSString*)weaponIdentifier
593{
594 NSString* identifier = nil;
595 if (hit) {
596 identifier = [weaponShotHit objectForKey:weaponIdentifier];
597 if (!identifier)
598 identifier = @"[player-laser-hit]";
599 [sWeaponSoundPool playSoundWithKey:identifier priority:1.0 expiryTime:0.05 overlap:YES position:weaponOffset];
600 } else {
601 identifier = [weaponShotMiss objectForKey:weaponIdentifier];
602 if (!identifier)
603 identifier = @"[player-laser-miss]";
604 [sWeaponSoundPool playSoundWithKey:identifier priority:1.0 expiryTime:0.05 overlap:YES position:weaponOffset];
605 }
606}
607
608- (void)playWeaponOverheated:(Vector)weaponOffset
609{
610 [sWeaponSoundPool playSoundWithKey:@"[weapon-overheat]" overlap:NO position:weaponOffset];
611}
612
613- (void)playMissileLaunched:(Vector)weaponOffset weaponIdentifier:(NSString*)weaponIdentifier
614{
615 NSString* identifier = [weaponLaunched objectForKey:weaponIdentifier];
616 if (!identifier)
617 identifier = @"[missile_launched]";
618 [sWeaponSoundPool playSoundWithKey:identifier position:weaponOffset];
619}
620
621- (void)playMineLaunched:(Vector)weaponOffset weaponIdentifier:(NSString*)weaponIdentifier
622{
623 NSString* identifier = [weaponLaunched objectForKey:weaponIdentifier];
624 if (!identifier)
625 identifier = @"[mine_launched]";
626 [sWeaponSoundPool playSoundWithKey:identifier position:weaponOffset];
627}
628
630{
631 [sMiscSoundPool playSoundWithKey:@"[escape-pod-scooped]" position:kInterfaceBeepPosition];
632}
633
635{
636 [sMiscSoundPool playSoundWithKey:@"[aegis-planet]" position:kInterfaceBeepPosition];
637}
638
640{
641 [sMiscSoundPool playSoundWithKey:@"[aegis-station]" position:kInterfaceBeepPosition];
642}
643
645{
646 [sMiscSoundPool playSoundWithKey:@"[game-over]"];
647}
648
649- (void)playLegacyScriptSound:(NSString*)key
650{
652}
653
654@end
#define DESTROY(x)
return nil
double OOTimeDelta
Definition OOTypes.h:193
static OOSoundSourcePool * sDamageSoundPool
static OOSoundSource * sEcmSource
static NSDictionary * weaponUnshieldedHit
@ kDamagePoolSize
@ kMiscPoolSize
@ kWeaponPoolSize
@ kBuySellSourcePoolSize
@ kWarningPoolSize
static OOSoundSourcePool * sWeaponSoundPool
static OOSoundSource * sHyperspaceSoundSource
static OOSoundSourcePool * sBuySellSourcePool
static const Vector kAfterburner2Position
static const Vector kEcmPosition
static NSDictionary * weaponShotHit
static const Vector kWitchspacePosition
static OOSoundSource * sBreakPatternSource
static OOSoundSource * sAfterburnerSources[2]
static NSDictionary * weaponLaunched
static const Vector kInterfaceWarningPosition
static const Vector kBreakPatternPosition
static OOSoundSource * sInterfaceBeepSource
#define OO_ASSIGN_SOUNDSTR_TO_SOUNDS(soundStr, sounds)
static OOSoundSourcePool * sMiscSoundPool
static OOSoundSourcePool * sWarningSoundPool
static const Vector kInterfaceBeepPosition
static const Vector kAfterburner1Position
static NSDictionary * weaponShieldHit
static NSDictionary * weaponShotMiss
NSArray * allEquipmentTypes()
void playSoundWithKey:priority:expiryTime:overlap:position:(NSString *key,[priority] float priority,[expiryTime] OOTimeDelta expiryTime,[overlap] BOOL overlap,[position] Vector position)
void playSoundWithKey:position:(NSString *key,[position] Vector position)
void playSoundWithKey:priority:position:(NSString *key,[priority] float priority,[position] Vector position)
void playSoundWithKey:(NSString *key)
void playSoundWithKey:overlap:position:(NSString *key,[overlap] BOOL overlap,[position] Vector position)
void playSoundWithKey:priority:(NSString *key,[priority] float priority)
void playCustomSoundWithKey:(NSString *key)
Definition Universe.m:9985
void playSound:(OOSound *inSound)
void setPosition:(Vector inPosition)
id soundWithCustomSoundKey:(NSString *key)
Definition Universe.m:9950
void playInterfaceBeep:(NSString *beepKey)
OOSound * ooSoundNamed:inFolder:(NSString *fileName,[inFolder] NSString *folderName)