Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaClient/Source/Sounds/SoundPlayer.cs
T
Joonas Rikkonen d4c8fd5649 cd50479...1ec6577
commit 1ec6577ad95e1d7ebc8cbfa261c9893c55ea987c
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 17:10:31 2019 +0200

    Don't attempt to move character's hands in UpdateUseItem if the character is disabled. May be causing "attempted to move pulljoint extremely far" errors (see #895)

commit 1aabc5110ce8e0ec208adce6380cbafd255b576f
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 16:47:50 2019 +0200

    removed syringes from the fabricator

commit 0a80a2d8029275f6cffdae580d1ba86cd44dd1a2
Merge: 039c04818 686d4c482
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 16:43:55 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 039c04818e3598f2e4cdef4065434000a48bb229
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 16:43:51 2019 +0200

    Updated: Alien artifacts to spawn inside containers tagged artifactholders in salvage missions & artifact events

commit 686d4c482f692bcc31eb37aeaea0d5074a537fe9
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:42:11 2019 +0200

    Fixed a bunch more buttons/sliders being invisible while pressed

commit 823728511547f14a3dcb4fcf5025db592ea3dac9
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:13:36 2019 +0200

    Reset the unsaved changes when exitting the character editor (Not applied when the switching between the game view and the character editor).

commit 9ecc56db4e73dd2ccd577130014020a1a502ae15
Merge: efbeda895 b824fc3f2
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:04:57 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit efbeda895fceecb8b4701f23ae5a51057aa44efb
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 16:03:54 2019 +0200

    Fix crash on trying to undo limb/joint deletion. Undo does not currently work on these cases, but the ragdoll can be reset to the original state from the reset button.

commit b824fc3f281aed76e8e0ed78ffb16d2213d2fda1
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 16:03:03 2019 +0200

    removed syringe from medical doctor default kit

commit c40c716962c7ae1a16d791b0fbd744627a2283c5
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:01:33 2019 +0200

    Nerfed door welding (takes 4x longer to weld a door shut now)

commit 7a27fcab1cec322fe75bcfad553aa42a37ec7d53
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 16:01:09 2019 +0200

    Fixed StatusEffects not being able to target item components. Caused doors to be impossible to weld and most likely other issues with item StatusEffects as well. Closes #1053

commit 50c09ad85ee44018418f537827c009892f4feb23
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:52:38 2019 +0200

    Fixed depth charges disappearing from loaders when interacting them with both hand slots full. Closes #1046

commit 648a5ba889623f776f184fb30b99133a8551234a
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:47:16 2019 +0200

    Allow to delete the main limb in the character editor. Should not crash anymore.

commit 751bda4d29b81c58c1b1df1653158543a3b7710d
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:46:53 2019 +0200

    Add a bunch of null checks to prevent crashes when the main limb is null.

commit 05c068f7aa35f7070e2cf19c526eff0c8d196b53
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:35:20 2019 +0200

    Syringes removed from medical.xml due to being legacy items we should no longer support.

commit 74389aa95e29fc7415de48230c8f12fd4a0a6847
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:30:13 2019 +0200

    Attempt to fix LightSource removal causing nullref exceptions.

commit 0907516bcac599b5ae20dc8e35f3a66adb8704b5
Merge: 659e1c58d f042994f4
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 15:25:07 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 659e1c58d3fb03d21acf2bb6065d07d1ac2ab914
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 15:24:59 2019 +0200

    Added: Yes to All / No to All to the prompts when deleting multiple containers with contents. Fixed: Aforementioned prompts staying active when quitting the submarine editor. Moved: Logic for the prompts from ItemContainer.cs to SubEditorScreen.cs

commit f042994f4b7eeff8cf5a6a68715c7c6aa6b8ddfb
Merge: 70a968a16 5a2ca22bd
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:08:15 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit 70a968a165c383c2ee765164b4507aa6072e0504
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:08:01 2019 +0200

    Use inventory icons in the extra cargo menu, layout fixes

commit 5a2ca22bde4be43f4577d20ae4e0a1ec20a23497
Merge: b2f89ea59 9ca7440be
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:06:11 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit b2f89ea59d481c109046d1edbd2807a516502b0c
Author: ezjamsen <ezjames.fi@gmail.com>
Date:   Mon Feb 4 15:05:39 2019 +0200

    redesigned walls in junction compartment of Typhon, closing #1042

commit 9ca7440be41a4fc2082b73618e90312af57c0817
Author: itchyOwl <lauri.harkanen@gmail.com>
Date:   Mon Feb 4 15:05:35 2019 +0200

    Add some null checks to prevent crashing if the xml document or the source element is not found.

commit 6d1331e9b86a5583761bc374c5dfb42868b03f45
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 15:03:53 2019 +0200

    Fixed the layout of the extra cargo menu in server settings. Closes #1038

commit 02ac14076ace0418a5d35df7a574f99811d56017
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 14:10:34 2019 +0200

    Fixed fire sounds persisting in menus. Closes #1047

commit d7d4516ddc9e21a2dba8f7bfd14a61ba7a805d4a
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 14:06:03 2019 +0200

    Added a null check to CampaignUI to help diagnose #1029

commit 0fee58808c261cecd4a602db22e39dfb855ee018
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:55:50 2019 +0200

    Fixed "attempting to remove an already removed item" console errors when ending a round.

commit 5e4448024ce00231e9e479aa238ed4b134b07321
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:53:59 2019 +0200

    Fixed crashing when ending a round while a character is outside the sub. Closes #1040

commit e3ff050561a8f823fc8abfddfec9d22ad4dc14a5
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:35:28 2019 +0200

    Fixed battery positioning in charging docks. Closes #1041

commit d383aec6cb7f4525906fa858375729d4ad4fc0d4
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 13:26:00 2019 +0200

    Fixed debugconsole adding the file number after the extension ("file123.txt (2)" instead of "file123 (2).txt"). Closes #1043

commit fee18b7ebcaea949c3f7d0c8027ab6253769b49c
Merge: d03807237 c06e8990f
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 12:10:12 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit d038072372ab1993a44542298fe4031db6448ae4
Author: Eetu <pitkanen.eetu@gmail.com>
Date:   Mon Feb 4 12:10:04 2019 +0200

    Fixed: Setting an item's condition to zero in the SubmarineEditor causing its OnBroken status effects to be fired

commit c06e8990f97af7ae9b4788b281bf2801cdd8e2ff
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 11:43:35 2019 +0200

    Fixed chatbox being deselected in the net lobby when receiving a lobby update from the server. Closes #1019

commit 2962647c37cec01fdb01092b94c25063c74c9ee0
Merge: b77bc3654 c89df8750
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:41:24 2019 +0200

    Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev

commit b77bc365470c8cfb86dc83e4b21e5b36e0e28918
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:41:08 2019 +0200

    Warning about automatic hull generation not working correctly with sloped walls when using the autohull command

commit 85ef20a48532e06ef0c2774aa81e1fb517814b14
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:40:20 2019 +0200

    Log the name of the item in the "attempting to remove an already removed item" error

commit b6e32d4009a898db9c97316bcb6236170a7a92fe
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:39:46 2019 +0200

    Fixed sliders not moving in the PowerContainer interface when an AI character is operating it

commit 4b4848b5caa7bf3893e4ae2f5afef3c980f60e46
Author: Joonas Rikkonen <poe.regalis@gmail.com>
Date:   Mon Feb 4 10:39:09 2019 +0200

    Nullref fix in AIObjectiveGoto

commit c89df87500c9026b3a6e75139391e64a4b1073e7
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 21:02:59 2019 -0300

    whoops

commit a85cc7705ea7611fff02fff7c18bc9826f03029a
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 21:01:51 2019 -0300

    ReduceAffliction loops back around if there's a surplus on index 0

commit e0047df3e089fd8be1646aecbd53b86b69949b13
Author: Juan Pablo Arce <juanjp600@users.noreply.github.com>
Date:   Sun Feb 3 19:45:52 2019 -0300

    Fixed ReduceAffliction freezing

    Thanks to nilanthanimosus for catching this
2019-03-18 21:11:46 +02:00

728 lines
31 KiB
C#

using Barotrauma.Extensions;
using Barotrauma.Sounds;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
public struct DamageSound
{
//the range of inflicted damage where the sound can be played
//(10.0f, 30.0f) would be played when the inflicted damage is between 10 and 30
public readonly Vector2 damageRange;
public readonly string damageType;
public readonly Sound sound;
public readonly string requiredTag;
public DamageSound(Sound sound, Vector2 damageRange, string damageType, string requiredTag = "")
{
this.sound = sound;
this.damageRange = damageRange;
this.damageType = damageType;
this.requiredTag = requiredTag;
}
}
public class BackgroundMusic
{
public readonly string File;
public readonly string Type;
public readonly Vector2 IntensityRange;
public BackgroundMusic(XElement element)
{
this.File = Path.GetFullPath(element.GetAttributeString("file", ""));
this.Type = element.GetAttributeString("type", "").ToLowerInvariant();
this.IntensityRange = element.GetAttributeVector2("intensityrange", new Vector2(0.0f, 100.0f));
}
}
static class SoundPlayer
{
private static ILookup<string, Sound> miscSounds;
//music
private const float MusicLerpSpeed = 1.0f;
private const float UpdateMusicInterval = 5.0f;
const int MaxMusicChannels = 6;
private readonly static Sound[] currentMusic = new Sound[MaxMusicChannels];
private readonly static SoundChannel[] musicChannel = new SoundChannel[MaxMusicChannels];
private readonly static BackgroundMusic[] targetMusic = new BackgroundMusic[MaxMusicChannels];
private static List<BackgroundMusic> musicClips;
private static float updateMusicTimer;
//ambience
private static List<Sound> waterAmbiences = new List<Sound>();
private static SoundChannel[] waterAmbienceChannels = new SoundChannel[2];
private static float ambientSoundTimer;
private static Vector2 ambientSoundInterval = new Vector2(20.0f, 40.0f); //x = min, y = max
//misc
public static List<Sound> FlowSounds = new List<Sound>();
public static List<Sound> SplashSounds = new List<Sound>();
private static SoundChannel[] flowSoundChannels;
private static float[] flowVolumeLeft;
private static float[] flowVolumeRight;
const float FlowSoundRange = 1500.0f;
const float MaxFlowStrength = 400.0f; //the heaviest water sound effect is played when the water flow is this strong
private static SoundChannel[] fireSoundChannels;
private static float[] fireVolumeLeft;
private static float[] fireVolumeRight;
const float FireSoundRange = 1000.0f;
const float FireSoundLargeLimit = 200.0f; //switch to large fire sound when the size of a firesource is above this
private static List<DamageSound> damageSounds;
private static Sound startUpSound;
public static bool Initialized;
public static string OverrideMusicType
{
get;
set;
}
public static float? OverrideMusicDuration;
public static int SoundCount;
public static IEnumerable<object> Init()
{
OverrideMusicType = null;
var soundFiles = GameMain.Instance.GetFilesOfType(ContentType.Sounds);
List<XElement> soundElements = new List<XElement>();
foreach (string soundFile in soundFiles)
{
XDocument doc = XMLExtensions.TryLoadXml(soundFile);
if (doc != null && doc.Root != null)
{
soundElements.AddRange(doc.Root.Elements());
}
}
SoundCount = 1 + soundElements.Count();
var startUpSoundElement = soundElements.Find(e => e.Name.ToString().ToLowerInvariant() == "startupsound");
if (startUpSoundElement != null)
{
startUpSound = GameMain.SoundManager.LoadSound(startUpSoundElement, false);
startUpSound.Play();
}
yield return CoroutineStatus.Running;
List<KeyValuePair<string, Sound>> miscSoundList = new List<KeyValuePair<string, Sound>>();
damageSounds = new List<DamageSound>();
musicClips = new List<BackgroundMusic>();
foreach (XElement soundElement in soundElements)
{
yield return CoroutineStatus.Running;
try
{
switch (soundElement.Name.ToString().ToLowerInvariant())
{
case "music":
musicClips.Add(new BackgroundMusic(soundElement));
break;
case "splash":
SplashSounds.Add(GameMain.SoundManager.LoadSound(soundElement, false));
break;
case "flow":
FlowSounds.Add(GameMain.SoundManager.LoadSound(soundElement, false));
break;
case "waterambience":
waterAmbiences.Add(GameMain.SoundManager.LoadSound(soundElement, false));
break;
case "damagesound":
Sound damageSound = GameMain.SoundManager.LoadSound(soundElement, false);
if (damageSound == null) continue;
string damageSoundType = soundElement.GetAttributeString("damagesoundtype", "None");
damageSounds.Add(new DamageSound(
damageSound,
soundElement.GetAttributeVector2("damagerange", new Vector2(0.0f, 100.0f)),
damageSoundType,
soundElement.GetAttributeString("requiredtag", "")));
break;
default:
Sound sound = GameMain.SoundManager.LoadSound(soundElement, false);
if (sound != null)
{
miscSoundList.Add(new KeyValuePair<string, Sound>(soundElement.Name.ToString().ToLowerInvariant(), sound));
}
break;
}
}
catch (FileNotFoundException e)
{
DebugConsole.ThrowError("Error while initializing SoundPlayer.", e);
}
}
flowSoundChannels = new SoundChannel[FlowSounds.Count];
flowVolumeLeft = new float[FlowSounds.Count];
flowVolumeRight = new float[FlowSounds.Count];
fireSoundChannels = new SoundChannel[2];
fireVolumeLeft = new float[2];
fireVolumeRight = new float[2];
miscSounds = miscSoundList.ToLookup(kvp => kvp.Key, kvp => kvp.Value);
Initialized = true;
yield return CoroutineStatus.Success;
}
public static void Update(float deltaTime)
{
UpdateMusic(deltaTime);
if (startUpSound != null && !GameMain.SoundManager.IsPlaying(startUpSound))
{
startUpSound.Dispose();
startUpSound = null;
}
//stop water sounds if no sub is loaded
if (Submarine.MainSub == null || Screen.Selected != GameMain.GameScreen)
{
for (int i = 0; i < waterAmbienceChannels.Length; i++)
{
if (waterAmbienceChannels[i] == null) continue;
waterAmbienceChannels[i].Dispose();
waterAmbienceChannels[i] = null;
}
for (int i = 0; i < FlowSounds.Count; i++)
{
if (flowSoundChannels[i] == null) continue;
flowSoundChannels[i].Dispose();
flowSoundChannels[i] = null;
}
for (int i = 0; i < fireSoundChannels.Length; i++)
{
if (fireSoundChannels[i] == null) continue;
fireSoundChannels[i].Dispose();
fireSoundChannels[i] = null;
}
fireVolumeLeft[0] = 0.0f; fireVolumeLeft[1] = 0.0f;
fireVolumeRight[0] = 0.0f; fireVolumeRight[1] = 0.0f;
return;
}
float ambienceVolume = 0.8f;
if (Character.Controlled != null)
{
AnimController animController = Character.Controlled.AnimController;
if (animController.HeadInWater)
{
ambienceVolume = 1.0f;
ambienceVolume += animController.Limbs[0].LinearVelocity.Length();
}
}
UpdateWaterAmbience(ambienceVolume);
UpdateWaterFlowSounds(deltaTime);
UpdateRandomAmbience(deltaTime);
UpdateFireSounds(deltaTime);
}
private static void UpdateWaterAmbience(float ambienceVolume)
{
//how fast the sub is moving, scaled to 0.0 -> 1.0
float movementSoundVolume = 0.0f;
foreach (Submarine sub in Submarine.Loaded)
{
float movementFactor = (sub.Velocity == Vector2.Zero) ? 0.0f : sub.Velocity.Length() / 10.0f;
movementFactor = MathHelper.Clamp(movementFactor, 0.0f, 1.0f);
if (Character.Controlled == null || Character.Controlled.Submarine != sub)
{
float dist = Vector2.Distance(GameMain.GameScreen.Cam.WorldViewCenter, sub.WorldPosition);
movementFactor = movementFactor / Math.Max(dist / 1000.0f, 1.0f);
}
movementSoundVolume = Math.Max(movementSoundVolume, movementFactor);
}
if (waterAmbiences.Count > 1)
{
if (waterAmbienceChannels[0] == null || !waterAmbienceChannels[0].IsPlaying)
{
waterAmbienceChannels[0] = waterAmbiences[0].Play(ambienceVolume * (1.0f - movementSoundVolume),"waterambience");
//waterAmbiences[0].Loop(waterAmbienceIndexes[0], ambienceVolume * (1.0f - movementSoundVolume));
waterAmbienceChannels[0].Looping = true;
}
else
{
waterAmbienceChannels[0].Gain = ambienceVolume * (1.0f - movementSoundVolume);
}
if (waterAmbienceChannels[1] == null || !waterAmbienceChannels[1].IsPlaying)
{
waterAmbienceChannels[1] = waterAmbiences[1].Play(ambienceVolume * movementSoundVolume, "waterambience");
//waterAmbienceIndexes[1] = waterAmbiences[1].Loop(waterAmbienceIndexes[1], ambienceVolume * movementSoundVolume);
waterAmbienceChannels[1].Looping = true;
}
else
{
waterAmbienceChannels[1].Gain = ambienceVolume * movementSoundVolume;
}
}
}
private static void UpdateWaterFlowSounds(float deltaTime)
{
if (FlowSounds.Count == 0) { return; }
float[] targetFlowLeft = new float[FlowSounds.Count];
float[] targetFlowRight = new float[FlowSounds.Count];
Vector2 listenerPos = new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y);
foreach (Gap gap in Gap.GapList)
{
if (gap.Open < 0.01f) continue;
float gapFlow = Math.Abs(gap.LerpedFlowForce.X) + Math.Abs(gap.LerpedFlowForce.Y) * 2.5f;
if (gapFlow < 10.0f) continue;
int flowSoundIndex = (int)Math.Floor(MathHelper.Clamp(gapFlow / MaxFlowStrength, 0, FlowSounds.Count));
flowSoundIndex = Math.Min(flowSoundIndex, FlowSounds.Count - 1);
Vector2 diff = gap.WorldPosition - listenerPos;
if (Math.Abs(diff.X) < FlowSoundRange && Math.Abs(diff.Y) < FlowSoundRange)
{
float dist = diff.Length();
float distFallOff = dist / FlowSoundRange;
if (distFallOff >= 0.99f) continue;
//flow at the left side
if (diff.X < 0)
{
targetFlowLeft[flowSoundIndex] = 1.0f - distFallOff;
}
else
{
targetFlowRight[flowSoundIndex] = 1.0f - distFallOff;
}
}
}
for (int i = 0; i < FlowSounds.Count; i++)
{
flowVolumeLeft[i] = (targetFlowLeft[i] < flowVolumeLeft[i]) ?
Math.Max(targetFlowLeft[i], flowVolumeLeft[i] - deltaTime) :
Math.Min(targetFlowLeft[i], flowVolumeLeft[i] + deltaTime);
flowVolumeRight[i] = (targetFlowRight[i] < flowVolumeRight[i]) ?
Math.Max(targetFlowRight[i], flowVolumeRight[i] - deltaTime) :
Math.Min(targetFlowRight[i], flowVolumeRight[i] + deltaTime);
if (flowVolumeLeft[i] < 0.05f && flowVolumeRight[i] < 0.05f)
{
if (flowSoundChannels[i] != null)
{
flowSoundChannels[i].Dispose();
flowSoundChannels[i] = null;
}
}
else
{
Vector2 soundPos = new Vector2(GameMain.SoundManager.ListenerPosition.X + (flowVolumeRight[i] - flowVolumeLeft[i]) * 100, GameMain.SoundManager.ListenerPosition.Y);
if (flowSoundChannels[i] == null || !flowSoundChannels[i].IsPlaying)
{
flowSoundChannels[i] = FlowSounds[i].Play(1.0f, FlowSoundRange, soundPos);
flowSoundChannels[i].Looping = true;
}
flowSoundChannels[i].Gain = Math.Max(flowVolumeRight[i], flowVolumeLeft[i]);
flowSoundChannels[i].Position = new Vector3(soundPos, 0.0f);
}
}
}
private static void UpdateFireSounds(float deltaTime)
{
for (int i = 0; i < fireVolumeLeft.Length; i++)
{
fireVolumeLeft[i] = 0.0f;
fireVolumeRight[i] = 0.0f;
}
Vector2 listenerPos = new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y);
foreach (Hull hull in Hull.hullList)
{
foreach (FireSource fs in hull.FireSources)
{
Vector2 diff = fs.WorldPosition + fs.Size / 2 - listenerPos;
if (Math.Abs(diff.X) < FireSoundRange && Math.Abs(diff.Y) < FireSoundRange)
{
Vector2 diffLeft = (fs.WorldPosition + new Vector2(fs.Size.X, fs.Size.Y / 2)) - listenerPos;
if (diff.X < fs.Size.X / 2.0f) diff.X = 0.0f;
if (diffLeft.X <= 0)
{
float distFallOffLeft = diffLeft.Length() / FireSoundRange;
if (distFallOffLeft < 0.99f)
{
fireVolumeLeft[0] += (1.0f - distFallOffLeft) * (fs.Size.X / FireSoundLargeLimit);
if (fs.Size.X > FireSoundLargeLimit) fireVolumeLeft[1] += (1.0f - distFallOffLeft) * ((fs.Size.X - FireSoundLargeLimit) / FireSoundLargeLimit);
}
}
Vector2 diffRight = (fs.WorldPosition + new Vector2(0.0f, fs.Size.Y / 2)) - listenerPos;
if (diff.X < fs.Size.X / 2.0f) diff.X = 0.0f;
if (diffRight.X >= 0)
{
float distFallOffRight = diffRight.Length() / FireSoundRange;
if (distFallOffRight < 0.99f)
{
fireVolumeRight[0] += 1.0f - distFallOffRight;
if (fs.Size.X > FireSoundLargeLimit) fireVolumeRight[1] += (1.0f - distFallOffRight) * ((fs.Size.X - FireSoundLargeLimit) / FireSoundLargeLimit);
}
}
}
}
}
for (int i = 0; i < fireVolumeLeft.Length; i++)
{
if (fireVolumeLeft[i] < 0.05f && fireVolumeRight[i] < 0.05f)
{
if (fireSoundChannels[i] != null)
{
fireSoundChannels[i].Dispose();
fireSoundChannels[i] = null;
}
}
else
{
Vector2 soundPos = new Vector2(GameMain.SoundManager.ListenerPosition.X + (fireVolumeRight[i] - fireVolumeLeft[i]) * 100, GameMain.SoundManager.ListenerPosition.Y);
if (fireSoundChannels[i] == null || !fireSoundChannels[i].IsPlaying)
{
fireSoundChannels[i] = GetSound(i == 0 ? "fire" : "firelarge").Play(1.0f, FlowSoundRange, soundPos);
fireSoundChannels[i].Looping = true;
}
fireSoundChannels[i].Gain = Math.Max(fireVolumeRight[i], fireVolumeLeft[i]);
fireSoundChannels[i].Position = new Vector3(soundPos, 0.0f);
}
}
}
private static void UpdateRandomAmbience(float deltaTime)
{
if (ambientSoundTimer > 0.0f)
{
ambientSoundTimer -= deltaTime;
}
else
{
PlaySound(
"ambient",
Rand.Range(0.5f, 1.0f),
1000.0f,
new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y) + Rand.Vector(100.0f));
ambientSoundTimer = Rand.Range(ambientSoundInterval.X, ambientSoundInterval.Y);
}
}
public static Sound GetSound(string soundTag)
{
var matchingSounds = miscSounds[soundTag].ToList();
if (matchingSounds.Count == 0) return null;
return matchingSounds[Rand.Int(matchingSounds.Count)];
}
public static SoundChannel PlaySound(string soundTag, float volume = 1.0f)
{
var sound = GetSound(soundTag);
return sound?.Play(volume);
}
public static SoundChannel PlaySound(string soundTag, float volume, float range, Vector2 position, Hull hullGuess = null)
{
var sound = GetSound(soundTag);
if (sound == null) return null;
return PlaySound(sound, sound.BaseGain * volume, range, position, hullGuess);
}
public static SoundChannel PlaySound(Sound sound, float volume, float range, Vector2 position, Hull hullGuess = null)
{
if (Vector2.DistanceSquared(new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y), position) > range * range) return null;
return sound.Play(sound.BaseGain * volume, range, position, muffle: ShouldMuffleSound(Character.Controlled, position, range, hullGuess));
}
private static void UpdateMusic(float deltaTime)
{
if (musicClips == null) return;
if (OverrideMusicType != null && OverrideMusicDuration.HasValue)
{
OverrideMusicDuration -= deltaTime;
if (OverrideMusicDuration <= 0.0f)
{
OverrideMusicType = null;
OverrideMusicDuration = null;
}
}
updateMusicTimer -= deltaTime;
if (updateMusicTimer <= 0.0f)
{
//find appropriate music for the current situation
string currentMusicType = GetCurrentMusicType();
float currentIntensity = GameMain.GameSession?.EventManager != null ?
GameMain.GameSession.EventManager.CurrentIntensity * 100.0f : 0.0f;
IEnumerable<BackgroundMusic> suitableMusic = GetSuitableMusicClips(currentMusicType, currentIntensity);
if (suitableMusic.Count() == 0)
{
targetMusic[0] = null;
}
//switch the music if nothing playing atm or the currently playing clip is not suitable anymore
else if (targetMusic[0] == null || currentMusic[0] == null || !suitableMusic.Any(m => m.File == currentMusic[0].Filename))
{
targetMusic[0] = suitableMusic.GetRandom();
}
//get the appropriate intensity layers for current situation
IEnumerable<BackgroundMusic> suitableIntensityMusic = GetSuitableMusicClips("intensity", currentIntensity);
for (int i = 1; i < MaxMusicChannels; i++)
{
//disable targetmusics that aren't suitable anymore
if (targetMusic[i] != null && !suitableIntensityMusic.Any(m => m.File == targetMusic[i].File))
{
targetMusic[i] = null;
}
}
foreach (BackgroundMusic intensityMusic in suitableIntensityMusic)
{
//already playing, do nothing
if (targetMusic.Any(m => m != null && m.File == intensityMusic.File)) continue;
for (int i = 1; i < MaxMusicChannels; i++)
{
if (targetMusic[i] == null)
{
targetMusic[i] = intensityMusic;
break;
}
}
}
updateMusicTimer = UpdateMusicInterval;
}
for (int i = 0; i < MaxMusicChannels; i++)
{
//nothing should be playing on this channel
if (targetMusic[i] == null)
{
if (musicChannel[i] != null && musicChannel[i].IsPlaying)
{
//mute the channel
musicChannel[i].Gain = MathHelper.Lerp(musicChannel[i].Gain, 0.0f, MusicLerpSpeed * deltaTime);
if (musicChannel[i].Gain < 0.01f) DisposeMusicChannel(i);
}
}
//something should be playing, but the channel is playing nothing or an incorrect clip
else if (currentMusic[i] == null || targetMusic[i].File != currentMusic[i].Filename)
{
//something playing -> mute it first
if (musicChannel[i] != null && musicChannel[i].IsPlaying)
{
musicChannel[i].Gain = MathHelper.Lerp(musicChannel[i].Gain, 0.0f, MusicLerpSpeed * deltaTime);
if (musicChannel[i].Gain < 0.01f) DisposeMusicChannel(i);
}
//channel free now, start playing the correct clip
if (currentMusic[i] == null || (musicChannel[i] == null || !musicChannel[i].IsPlaying))
{
DisposeMusicChannel(i);
currentMusic[i] = GameMain.SoundManager.LoadSound(targetMusic[i].File, true);
musicChannel[i] = currentMusic[i].Play(0.0f, "music");
musicChannel[i].Looping = true;
}
}
else
{
//playing something, lerp volume up
if (musicChannel[i] == null || !musicChannel[i].IsPlaying)
{
musicChannel[i]?.Dispose();
musicChannel[i] = currentMusic[i].Play(0.0f, "music");
musicChannel[i].Looping = true;
}
musicChannel[i].Gain = MathHelper.Lerp(musicChannel[i].Gain, 1.0f, MusicLerpSpeed * deltaTime);
}
}
}
private static void DisposeMusicChannel(int index)
{
musicChannel[index]?.Dispose(); musicChannel[index] = null;
currentMusic[index]?.Dispose(); currentMusic[index] = null;
}
private static IEnumerable<BackgroundMusic> GetSuitableMusicClips(string musicType, float currentIntensity)
{
return musicClips.Where(music =>
music != null &&
music.Type == musicType &&
currentIntensity >= music.IntensityRange.X &&
currentIntensity <= music.IntensityRange.Y);
}
private static string GetCurrentMusicType()
{
if (OverrideMusicType != null) return OverrideMusicType;
if (Character.Controlled != null &&
Level.Loaded != null && Level.Loaded.Ruins != null &&
Level.Loaded.Ruins.Any(r => r.Area.Contains(Character.Controlled.WorldPosition)))
{
return "ruins";
}
Submarine targetSubmarine = Character.Controlled?.Submarine;
if ((targetSubmarine != null && targetSubmarine.AtDamageDepth) ||
(Screen.Selected == GameMain.GameScreen && GameMain.GameScreen.Cam.Position.Y < SubmarineBody.DamageDepth))
{
return "deep";
}
if (targetSubmarine != null)
{
float floodedArea = 0.0f;
float totalArea = 0.0f;
foreach (Hull hull in Hull.hullList)
{
if (hull.Submarine != targetSubmarine) continue;
floodedArea += hull.WaterVolume;
totalArea += hull.Volume;
}
if (totalArea > 0.0f && floodedArea / totalArea > 0.25f) return "flooded";
}
float enemyDistThreshold = 5000.0f;
if (targetSubmarine != null)
{
enemyDistThreshold = Math.Max(enemyDistThreshold, Math.Max(targetSubmarine.Borders.Width, targetSubmarine.Borders.Height) * 2.0f);
}
foreach (Character character in Character.CharacterList)
{
if (character.IsDead || !character.Enabled) continue;
EnemyAIController enemyAI = character.AIController as EnemyAIController;
if (enemyAI == null || (!enemyAI.AttackHumans && !enemyAI.AttackRooms)) continue;
if (targetSubmarine != null)
{
if (Vector2.DistanceSquared(character.WorldPosition, targetSubmarine.WorldPosition) < enemyDistThreshold * enemyDistThreshold)
{
return "monster";
}
}
else if (Character.Controlled != null)
{
if (Vector2.DistanceSquared(character.WorldPosition, Character.Controlled.WorldPosition) < enemyDistThreshold * enemyDistThreshold)
{
return "monster";
}
}
}
if (GameMain.GameSession != null && Timing.TotalTime < GameMain.GameSession.RoundStartTime + 120.0)
{
return "start";
}
return "default";
}
public static bool ShouldMuffleSound(Character listener, Vector2 soundWorldPos, float range, Hull hullGuess)
{
if (listener == null) return false;
float lowpassHFGain = 1.0f;
AnimController animController = listener.AnimController;
if (animController.HeadInWater)
{
lowpassHFGain = 0.2f;
}
lowpassHFGain *= Character.Controlled.LowPassMultiplier;
if (lowpassHFGain < 0.5f) return true;
Hull targetHull = Hull.FindHull(soundWorldPos, hullGuess, true);
if (listener.CurrentHull == null || targetHull == null)
{
return listener.CurrentHull != targetHull;
}
return listener.CurrentHull.GetApproximateDistance(targetHull, range) > range;
}
public static void PlaySplashSound(Vector2 worldPosition, float strength)
{
if (SplashSounds.Count == 0) { return; }
int splashIndex = MathHelper.Clamp((int)(strength + Rand.Range(-2, 2)), 0, SplashSounds.Count - 1);
float range = 800.0f;
var channel = SplashSounds[splashIndex].Play(1.0f, range, worldPosition, muffle: ShouldMuffleSound(Character.Controlled, worldPosition, range, null));
}
public static void PlayDamageSound(string damageType, float damage, PhysicsBody body)
{
Vector2 bodyPosition = body.DrawPosition;
PlayDamageSound(damageType, damage, bodyPosition, 800.0f);
}
public static void PlayDamageSound(string damageType, float damage, Vector2 position, float range = 2000.0f, IEnumerable<string> tags = null)
{
damage = MathHelper.Clamp(damage + Rand.Range(-10.0f, 10.0f), 0.0f, 100.0f);
var sounds = damageSounds.FindAll(s =>
s.damageRange == null ||
(damage >= s.damageRange.X &&
damage <= s.damageRange.Y) &&
s.damageType == damageType &&
(tags == null ? string.IsNullOrEmpty(s.requiredTag) : tags.Contains(s.requiredTag)));
if (!sounds.Any()) return;
int selectedSound = Rand.Int(sounds.Count);
sounds[selectedSound].sound.Play(1.0f, range, position, muffle: ShouldMuffleSound(Character.Controlled, position, range, null));
}
}
}