D:\ValheimDev\Dumps\Old\assembly_valheim\AudioMan.cs D:\ValheimDev\Dumps\Latest\assembly_valheim\AudioMan.cs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
.  using System.Linq;
  using CircularBuffer;
using UnityEngine; using UnityEngine;
using UnityEngine.Audio; using UnityEngine.Audio;
using UnityEngine.Rendering; using UnityEngine.Rendering;
   
public class AudioMan : MonoBehaviour public class AudioMan : MonoBehaviour
{ {
    public static AudioMan instance     public static AudioMan instance
    {     {
        get         get
        {         {
            return AudioMan.m_instance;             return AudioMan.m_instance;
        }         }
    }     }
   
    private void Awake()     private void Awake()
    {     {
        if (AudioMan.m_instance != null)         if (AudioMan.m_instance != null)
        {         {
            ZLog.Log("Audioman already exist, destroying self");             ZLog.Log("Audioman already exist, destroying self");
.            UnityEngine.Object.DestroyImmediate(base.gameObject);             UnityEngine.Object.Destroy(base.gameObject);
            return;             return;
        }         }
        AudioMan.m_instance = this;         AudioMan.m_instance = this;
        UnityEngine.Object.DontDestroyOnLoad(base.gameObject);         UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
        GameObject gameObject = new GameObject("ocean_ambient_loop");         GameObject gameObject = new GameObject("ocean_ambient_loop");
        gameObject.transform.SetParent(base.transform);         gameObject.transform.SetParent(base.transform);
        this.m_oceanAmbientSource = gameObject.AddComponent<AudioSource>();         this.m_oceanAmbientSource = gameObject.AddComponent<AudioSource>();
        this.m_oceanAmbientSource.loop = true;         this.m_oceanAmbientSource.loop = true;
        this.m_oceanAmbientSource.spatialBlend = 0.75f;         this.m_oceanAmbientSource.spatialBlend = 0.75f;
        this.m_oceanAmbientSource.outputAudioMixerGroup = this.m_ambientMixer;         this.m_oceanAmbientSource.outputAudioMixerGroup = this.m_ambientMixer;
        this.m_oceanAmbientSource.maxDistance = 128f;         this.m_oceanAmbientSource.maxDistance = 128f;
        this.m_oceanAmbientSource.minDistance = 40f;         this.m_oceanAmbientSource.minDistance = 40f;
        this.m_oceanAmbientSource.spread = 90f;         this.m_oceanAmbientSource.spread = 90f;
        this.m_oceanAmbientSource.rolloffMode = AudioRolloffMode.Linear;         this.m_oceanAmbientSource.rolloffMode = AudioRolloffMode.Linear;
        this.m_oceanAmbientSource.clip = this.m_oceanAudio;         this.m_oceanAmbientSource.clip = this.m_oceanAudio;
        this.m_oceanAmbientSource.bypassReverbZones = true;         this.m_oceanAmbientSource.bypassReverbZones = true;
        this.m_oceanAmbientSource.dopplerLevel = 0f;         this.m_oceanAmbientSource.dopplerLevel = 0f;
        this.m_oceanAmbientSource.volume = 0f;         this.m_oceanAmbientSource.volume = 0f;
        this.m_oceanAmbientSource.priority = 0;         this.m_oceanAmbientSource.priority = 0;
        this.m_oceanAmbientSource.Play();         this.m_oceanAmbientSource.Play();
        GameObject gameObject2 = new GameObject("ambient_loop");         GameObject gameObject2 = new GameObject("ambient_loop");
        gameObject2.transform.SetParent(base.transform);         gameObject2.transform.SetParent(base.transform);
        this.m_ambientLoopSource = gameObject2.AddComponent<AudioSource>();         this.m_ambientLoopSource = gameObject2.AddComponent<AudioSource>();
        this.m_ambientLoopSource.loop = true;         this.m_ambientLoopSource.loop = true;
        this.m_ambientLoopSource.spatialBlend = 0f;         this.m_ambientLoopSource.spatialBlend = 0f;
        this.m_ambientLoopSource.outputAudioMixerGroup = this.m_ambientMixer;         this.m_ambientLoopSource.outputAudioMixerGroup = this.m_ambientMixer;
        this.m_ambientLoopSource.bypassReverbZones = true;         this.m_ambientLoopSource.bypassReverbZones = true;
        this.m_ambientLoopSource.priority = 0;         this.m_ambientLoopSource.priority = 0;
        this.m_ambientLoopSource.volume = 0f;         this.m_ambientLoopSource.volume = 0f;
        GameObject gameObject3 = new GameObject("wind_loop");         GameObject gameObject3 = new GameObject("wind_loop");
        gameObject3.transform.SetParent(base.transform);         gameObject3.transform.SetParent(base.transform);
        this.m_windLoopSource = gameObject3.AddComponent<AudioSource>();         this.m_windLoopSource = gameObject3.AddComponent<AudioSource>();
        this.m_windLoopSource.loop = true;         this.m_windLoopSource.loop = true;
        this.m_windLoopSource.spatialBlend = 0f;         this.m_windLoopSource.spatialBlend = 0f;
        this.m_windLoopSource.outputAudioMixerGroup = this.m_ambientMixer;         this.m_windLoopSource.outputAudioMixerGroup = this.m_ambientMixer;
        this.m_windLoopSource.bypassReverbZones = true;         this.m_windLoopSource.bypassReverbZones = true;
        this.m_windLoopSource.clip = this.m_windAudio;         this.m_windLoopSource.clip = this.m_windAudio;
        this.m_windLoopSource.volume = 0f;         this.m_windLoopSource.volume = 0f;
        this.m_windLoopSource.priority = 0;         this.m_windLoopSource.priority = 0;
        this.m_windLoopSource.Play();         this.m_windLoopSource.Play();
.          if (this.m_enableShieldDomeHum)
          {
              GameObject gameObject4 = UnityEngine.Object.Instantiate<GameObject>(this.m_shieldHumPrefab);
              gameObject4.transform.SetParent(base.transform);
              this.m_shieldHumSource = gameObject4.GetComponent<AudioSource>();
          }
          this.m_maxLavaLoops = this.GetLoopingMaxConcurrency(this.m_lavaLoopPrefab.GetComponent<ZSFX>());
    }     }
   
    private void Start()     private void Start()
    {     {
        if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null)         if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null)
        {         {
            AudioListener.volume = 0f;             AudioListener.volume = 0f;
            return;             return;
        }         }
        AudioListener.volume = PlayerPrefs.GetFloat("MasterVolume", AudioListener.volume);         AudioListener.volume = PlayerPrefs.GetFloat("MasterVolume", AudioListener.volume);
        AudioMan.SetSFXVolume(PlayerPrefs.GetFloat("SfxVolume", AudioMan.GetSFXVolume()));         AudioMan.SetSFXVolume(PlayerPrefs.GetFloat("SfxVolume", AudioMan.GetSFXVolume()));
    }     }
   
    private void OnApplicationQuit()     private void OnApplicationQuit()
    {     {
        this.StopAllAudio();         this.StopAllAudio();
    }     }
   
    private void OnDestroy()     private void OnDestroy()
    {     {
        if (AudioMan.m_instance == this)         if (AudioMan.m_instance == this)
        {         {
            AudioMan.m_instance = null;             AudioMan.m_instance = null;
        }         }
    }     }
   
    private void StopAllAudio()     private void StopAllAudio()
    {     {
        AudioSource[] array = UnityEngine.Object.FindObjectsOfType(typeof(AudioSource)) as AudioSource[];         AudioSource[] array = UnityEngine.Object.FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
        for (int i = 0; i < array.Length; i++)         for (int i = 0; i < array.Length; i++)
        {         {
            array[i].Stop();             array[i].Stop();
        }         }
    }     }
   
    private void Update()     private void Update()
    {     {
        float deltaTime = Time.deltaTime;         float deltaTime = Time.deltaTime;
.          this.m_zoneSystemValid = ZoneSystem.instance != null;
          this.m_envManValid = EnvMan.instance != null;
          this.m_listenerPos = this.GetActiveAudioListener().transform.position;
        this.UpdateAmbientLoop(deltaTime);         this.UpdateAmbientLoop(deltaTime);
        this.UpdateRandomAmbient(deltaTime);         this.UpdateRandomAmbient(deltaTime);
.          this.UpdateLavaAmbient(deltaTime);
        this.UpdateSnapshots(deltaTime);         this.UpdateSnapshots(deltaTime);
.          this.UpdateLoopingConcurrency();
          this.UpdateShieldHum();
      }
   
      private void UpdateShieldHum()
      {
          if (!this.m_enableShieldDomeHum)
          {
              return;
          }
          if (ShieldGenerator.HasShields())
          {
              if (!this.m_shieldHumSource.isPlaying)
              {
                  this.m_shieldHumSource.Play();
              }
              this.m_shieldHumSource.transform.position = ShieldGenerator.GetClosestShieldPoint(this.GetActiveAudioListener().transform.position);
              return;
          }
          this.m_shieldHumSource.Stop();
    }     }
   
    private void FixedUpdate()     private void FixedUpdate()
    {     {
        float fixedDeltaTime = Time.fixedDeltaTime;         float fixedDeltaTime = Time.fixedDeltaTime;
.          this.m_zoneSystemValid = ZoneSystem.instance != null;
          this.m_envManValid = EnvMan.instance != null;
        this.UpdateOceanAmbiance(fixedDeltaTime);         this.UpdateOceanAmbiance(fixedDeltaTime);
        this.UpdateWindAmbience(fixedDeltaTime);         this.UpdateWindAmbience(fixedDeltaTime);
    }     }
   
    public static float GetSFXVolume()     public static float GetSFXVolume()
    {     {
        if (AudioMan.m_instance == null)         if (AudioMan.m_instance == null)
        {         {
            return 1f;             return 1f;
        }         }
        float num;         float num;
        AudioMan.m_instance.m_masterMixer.GetFloat("SfxVol", out num);         AudioMan.m_instance.m_masterMixer.GetFloat("SfxVol", out num);
        if (num <= -80f)         if (num <= -80f)
        {         {
            return 0f;             return 0f;
        }         }
        return Mathf.Pow(10f, num / 10f);         return Mathf.Pow(10f, num / 10f);
    }     }
   
    public static void SetSFXVolume(float vol)     public static void SetSFXVolume(float vol)
    {     {
        if (AudioMan.m_instance == null)         if (AudioMan.m_instance == null)
        {         {
            return;             return;
        }         }
        float num = ((vol > 0f) ? (Mathf.Log10(Mathf.Clamp(vol, 0.001f, 1f)) * 10f) : (-80f));         float num = ((vol > 0f) ? (Mathf.Log10(Mathf.Clamp(vol, 0.001f, 1f)) * 10f) : (-80f));
        AudioMan.m_instance.m_masterMixer.SetFloat("SfxVol", num);         AudioMan.m_instance.m_masterMixer.SetFloat("SfxVol", num);
        AudioMan.m_instance.m_masterMixer.SetFloat("GuiVol", num);         AudioMan.m_instance.m_masterMixer.SetFloat("GuiVol", num);
    }     }
   
    private void UpdateRandomAmbient(float dt)     private void UpdateRandomAmbient(float dt)
    {     {
        if (this.InMenu())         if (this.InMenu())
        {         {
            return;             return;
        }         }
        this.m_randomAmbientTimer += dt;         this.m_randomAmbientTimer += dt;
        if (this.m_randomAmbientTimer > this.m_randomAmbientInterval)         if (this.m_randomAmbientTimer > this.m_randomAmbientInterval)
        {         {
            this.m_randomAmbientTimer = 0f;             this.m_randomAmbientTimer = 0f;
            if (UnityEngine.Random.value <= this.m_randomAmbientChance)             if (UnityEngine.Random.value <= this.m_randomAmbientChance)
            {             {
                float num = 0f;                 float num = 0f;
                AudioClip audioClip;                 AudioClip audioClip;
                if (this.SelectRandomAmbientClip(out audioClip, out num))                 if (this.SelectRandomAmbientClip(out audioClip, out num))
                {                 {
                    Vector3 randomAmbiencePoint = this.GetRandomAmbiencePoint();                     Vector3 randomAmbiencePoint = this.GetRandomAmbiencePoint();
                    GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_randomAmbientPrefab, randomAmbiencePoint, Quaternion.identity, base.transform);                     GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_randomAmbientPrefab, randomAmbiencePoint, Quaternion.identity, base.transform);
                    ZSFX component = gameObject.GetComponent<ZSFX>();                     ZSFX component = gameObject.GetComponent<ZSFX>();
                    component.m_audioClips = new AudioClip[] { audioClip };                     component.m_audioClips = new AudioClip[] { audioClip };
                    component.Play();                     component.Play();
                    TimedDestruction component2 = gameObject.GetComponent<TimedDestruction>();                     TimedDestruction component2 = gameObject.GetComponent<TimedDestruction>();
                    if (num > 0f)                     if (num > 0f)
                    {                     {
                        component.m_fadeOutDelay = 0f;                         component.m_fadeOutDelay = 0f;
                        component.m_fadeOutDuration = num;                         component.m_fadeOutDuration = num;
                        component.m_fadeOutOnAwake = true;                         component.m_fadeOutOnAwake = true;
                        component2.m_timeout = num + 2f;                         component2.m_timeout = num + 2f;
                    }                     }
                    else                     else
                    {                     {
                        component.m_fadeOutDelay = audioClip.length - 1f;                         component.m_fadeOutDelay = audioClip.length - 1f;
                        component.m_fadeOutDuration = 1f;                         component.m_fadeOutDuration = 1f;
                        component.m_fadeOutOnAwake = true;                         component.m_fadeOutOnAwake = true;
                        component2.m_timeout = audioClip.length * 1.5f;                         component2.m_timeout = audioClip.length * 1.5f;
                    }                     }
                    component2.Trigger();                     component2.Trigger();
                }                 }
            }             }
        }         }
    }     }
   
.    private Vector3 GetRandomAmbiencePoint()     private void UpdateLavaAmbient(float dt)
    {     {
.          this.ScanForLava();
          this.UpdateLavaAmbientLoops();
          if (this.InMenu())
          {
              return;
          }
          if (this.m_validLavaPositions.Size == 0 || !this.m_envManValid || EnvMan.instance.GetCurrentBiome() != Heightmap.Biome.AshLands)
          {
              return;
          }
          this.m_lavaAmbientTimer += dt;
          if (this.m_lavaAmbientTimer < this.m_lavaNoiseInterval)
          {
              return;
          }
          this.m_lavaAmbientTimer = 0f;
          if (UnityEngine.Random.value > this.m_lavaNoiseChance)
          {
              return;
          }
          int i = 0;
        Vector3 vector = Vector3.zero;         Vector3 vector = Vector3.zero;
.        Camera mainCamera = Utils.GetMainCamera();          while (i < 5) 
        if (Player.m_localPlayer)          { 
              vector = this.m_validLavaPositions[UnityEngine.Random.Range(0, this.m_validLavaPositions.Size - 1)]; 
              float num = vector.DistanceTo(this.GetActiveAudioListener().transform.position); 
              if (num > this.m_lavaNoiseMinDistance && num < this.m_lavaNoiseMaxDistance) 
              { 
                  break; 
              } 
              i++; 
          } 
          if (i != 5) 
          { 
              GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_randomAmbientPrefab, vector, Quaternion.identity, base.transform); 
              ZSFX component = gameObject.GetComponent<ZSFX>(); 
              AudioClip audioClip = this.m_randomLavaNoises[UnityEngine.Random.Range(0, this.m_randomLavaNoises.Count - 1)]; 
              component.m_audioClips = new AudioClip[] { audioClip }; 
              component.Play(); 
              TimedDestruction component2 = gameObject.GetComponent<TimedDestruction>(); 
              component2.m_timeout = audioClip.length; 
              component2.Trigger(); 
          } 
      } 
   
      private void UpdateLavaAmbientLoops() 
      { 
          if (Time.frameCount % 24 != 0) 
          { 
              return; 
          } 
          if (this.m_ambientLavaLoops.Count < this.m_maxLavaLoops && this.m_validLavaPositions.Size > 0 && this.m_envManValid && EnvMan.instance.GetCurrentBiome() == Heightmap.Biome.AshLands) 
          { 
              Vector3 vector = this.m_validLavaPositions[UnityEngine.Random.Range(0, this.m_validLavaPositions.Size - 1)]; 
              float num = float.PositiveInfinity; 
              foreach (ZSFX zsfx3 in this.m_ambientLavaLoops) 
              { 
                  Vector3 position = zsfx3.transform.position; 
                  float num2 = position.DistanceTo(vector); 
                  if (num2 < num && zsfx3.transform.position != position) 
                  { 
                      num = num2; 
                  } 
              } 
              if (num <= this.m_minDistanceBetweenLavaLoops) 
              { 
                  return; 
              } 
              ZSFX component = UnityEngine.Object.Instantiate<GameObject>(this.m_lavaLoopPrefab, vector, Quaternion.identity).GetComponent<ZSFX>(); 
              component.OnDestroyingSfx += delegate(ZSFX zsfx) 
              { 
                  if (this.m_ambientLavaLoops.Contains(zsfx)) 
                  { 
                      this.m_ambientLavaLoops.Remove(zsfx); 
                  } 
              }; 
              this.m_ambientLavaLoops.Add(component); 
          } 
          for (int i = this.m_ambientLavaLoops.Count - 1; i >= 0; i--) 
        {         {
.            vector = Player.m_localPlayer.transform.position;             ZSFX zsfx2 = this.m_ambientLavaLoops[i]; 
              if (zsfx2.gameObject.transform.position.DistanceTo(this.m_listenerPos) >= this.m_maxLavaLoopDistance) 
              { 
                  zsfx2.GetComponent<TimedDestruction>().Trigger();
                  this.m_ambientLavaLoops.Remove(zsfx2); 
              } 
          } 
      } 
   
      private void ScanForLava() 
      { 
          if (Time.frameCount % 12 != 0 || !this.m_envManValid || EnvMan.instance.GetCurrentBiome() != Heightmap.Biome.AshLands || !this.m_zoneSystemValid) 
          { 
              return; 
        }         }
.        else if (mainCamera)          Vector2 vector = UnityEngine.Random.insideUnitCircle.normalized; 
          vector *= UnityEngine.Random.Range(2f, this.m_lavaScanRadius); 
          Vector3 vector2 = this.m_listenerPos + new Vector3(vector.x, 0f, vector.y); 
          if (ZoneSystem.instance.IsLava(ref vector2, false)) 
        {         {
.            vector = mainCamera.transform.position;             this.m_validLavaPositions.PushFront(vector2);
        }         }
.      }
   
      private Vector3 GetRandomAmbiencePoint()
      {
        float num = UnityEngine.Random.value * 3.1415927f * 2f;         float num = UnityEngine.Random.value * 3.1415927f * 2f;
        float num2 = UnityEngine.Random.Range(this.m_randomMinDistance, this.m_randomMaxDistance);         float num2 = UnityEngine.Random.Range(this.m_randomMinDistance, this.m_randomMaxDistance);
.        return vector + new Vector3(Mathf.Sin(num) * num2, 0f, Mathf.Cos(num) * num2);         return this.m_listenerPos + new Vector3(Mathf.Sin(num) * num2, 0f, Mathf.Cos(num) * num2);
    }     }
   
    private bool SelectRandomAmbientClip(out AudioClip clip, out float fadeoutDuration)     private bool SelectRandomAmbientClip(out AudioClip clip, out float fadeoutDuration)
    {     {
        fadeoutDuration = 0f;         fadeoutDuration = 0f;
        clip = null;         clip = null;
.        if (EnvMan.instance == null)         if (!this.m_envManValid)
        {         {
            return false;             return false;
        }         }
        EnvSetup currentEnvironment = EnvMan.instance.GetCurrentEnvironment();         EnvSetup currentEnvironment = EnvMan.instance.GetCurrentEnvironment();
        AudioMan.BiomeAmbients biomeAmbients;         AudioMan.BiomeAmbients biomeAmbients;
        if (currentEnvironment != null && !string.IsNullOrEmpty(currentEnvironment.m_ambientList))         if (currentEnvironment != null && !string.IsNullOrEmpty(currentEnvironment.m_ambientList))
        {         {
            biomeAmbients = this.GetAmbients(currentEnvironment.m_ambientList);             biomeAmbients = this.GetAmbients(currentEnvironment.m_ambientList);
        }         }
        else         else
        {         {
            biomeAmbients = this.GetBiomeAmbients(EnvMan.instance.GetCurrentBiome());             biomeAmbients = this.GetBiomeAmbients(EnvMan.instance.GetCurrentBiome());
        }         }
        if (biomeAmbients == null)         if (biomeAmbients == null)
        {         {
            return false;             return false;
        }         }
        fadeoutDuration = biomeAmbients.m_forceFadeout;         fadeoutDuration = biomeAmbients.m_forceFadeout;
        List<AudioClip> list = new List<AudioClip>(biomeAmbients.m_randomAmbientClips);         List<AudioClip> list = new List<AudioClip>(biomeAmbients.m_randomAmbientClips);
.        List<AudioClip> list2 = (EnvMan.instance.IsDaylight() ? biomeAmbients.m_randomAmbientClipsDay : biomeAmbients.m_randomAmbientClipsNight);         List<AudioClip> list2 = (EnvMan.IsDaylight() ? biomeAmbients.m_randomAmbientClipsDay : biomeAmbients.m_randomAmbientClipsNight);
        list.AddRange(list2);         list.AddRange(list2);
        if (list.Count == 0)         if (list.Count == 0)
        {         {
            return false;             return false;
        }         }
        clip = list[UnityEngine.Random.Range(0, list.Count)];         clip = list[UnityEngine.Random.Range(0, list.Count)];
        return true;         return true;
    }     }
   
    private void UpdateAmbientLoop(float dt)     private void UpdateAmbientLoop(float dt)
    {     {
.        if (EnvMan.instance == null)         if (!this.m_envManValid)
        {         {
            this.m_ambientLoopSource.Stop();             this.m_ambientLoopSource.Stop();
            return;             return;
        }         }
        if (this.m_queuedAmbientLoop || this.m_stopAmbientLoop)         if (this.m_queuedAmbientLoop || this.m_stopAmbientLoop)
        {         {
            if (this.m_ambientLoopSource.isPlaying && this.m_ambientLoopSource.volume > 0f)             if (this.m_ambientLoopSource.isPlaying && this.m_ambientLoopSource.volume > 0f)
            {             {
                this.m_ambientLoopSource.volume = Mathf.MoveTowards(this.m_ambientLoopSource.volume, 0f, dt / this.m_ambientFadeTime);                 this.m_ambientLoopSource.volume = Mathf.MoveTowards(this.m_ambientLoopSource.volume, 0f, dt / this.m_ambientFadeTime);
                return;                 return;
            }             }
            this.m_ambientLoopSource.Stop();             this.m_ambientLoopSource.Stop();
            this.m_stopAmbientLoop = false;             this.m_stopAmbientLoop = false;
            if (this.m_queuedAmbientLoop)             if (this.m_queuedAmbientLoop)
            {             {
                this.m_ambientLoopSource.clip = this.m_queuedAmbientLoop;                 this.m_ambientLoopSource.clip = this.m_queuedAmbientLoop;
                this.m_ambientLoopSource.volume = 0f;                 this.m_ambientLoopSource.volume = 0f;
                this.m_ambientLoopSource.Play();                 this.m_ambientLoopSource.Play();
                this.m_ambientVol = this.m_queuedAmbientVol;                 this.m_ambientVol = this.m_queuedAmbientVol;
                this.m_queuedAmbientLoop = null;                 this.m_queuedAmbientLoop = null;
                return;                 return;
            }             }
        }         }
        else if (this.m_ambientLoopSource.isPlaying)         else if (this.m_ambientLoopSource.isPlaying)
        {         {
            this.m_ambientLoopSource.volume = Mathf.MoveTowards(this.m_ambientLoopSource.volume, this.m_ambientVol, dt / this.m_ambientFadeTime);             this.m_ambientLoopSource.volume = Mathf.MoveTowards(this.m_ambientLoopSource.volume, this.m_ambientVol, dt / this.m_ambientFadeTime);
        }         }
    }     }
   
    public void SetIndoor(bool indoor)     public void SetIndoor(bool indoor)
    {     {
        this.m_indoor = indoor;         this.m_indoor = indoor;
    }     }
   
    private bool InMenu()     private bool InMenu()
    {     {
        return FejdStartup.instance != null || Menu.IsVisible() || (Game.instance && Game.instance.WaitingForRespawn()) || TextViewer.IsShowingIntro();         return FejdStartup.instance != null || Menu.IsVisible() || (Game.instance && Game.instance.WaitingForRespawn()) || TextViewer.IsShowingIntro();
    }     }
   
    private void UpdateSnapshots(float dt)     private void UpdateSnapshots(float dt)
    {     {
        if (this.InMenu())         if (this.InMenu())
        {         {
            this.SetSnapshot(AudioMan.Snapshot.Menu);             this.SetSnapshot(AudioMan.Snapshot.Menu);
            return;             return;
        }         }
        if (this.m_indoor)         if (this.m_indoor)
        {         {
            this.SetSnapshot(AudioMan.Snapshot.Indoor);             this.SetSnapshot(AudioMan.Snapshot.Indoor);
            return;             return;
        }         }
        this.SetSnapshot(AudioMan.Snapshot.Default);         this.SetSnapshot(AudioMan.Snapshot.Default);
    }     }
   
    private void SetSnapshot(AudioMan.Snapshot snapshot)     private void SetSnapshot(AudioMan.Snapshot snapshot)
    {     {
        if (this.m_currentSnapshot == snapshot)         if (this.m_currentSnapshot == snapshot)
        {         {
            return;             return;
        }         }
        this.m_currentSnapshot = snapshot;         this.m_currentSnapshot = snapshot;
        switch (snapshot)         switch (snapshot)
        {         {
        case AudioMan.Snapshot.Default:         case AudioMan.Snapshot.Default:
            this.m_masterMixer.FindSnapshot("Default").TransitionTo(this.m_snapshotTransitionTime);             this.m_masterMixer.FindSnapshot("Default").TransitionTo(this.m_snapshotTransitionTime);
            return;             return;
        case AudioMan.Snapshot.Menu:         case AudioMan.Snapshot.Menu:
            this.m_masterMixer.FindSnapshot("Menu").TransitionTo(this.m_snapshotTransitionTime);             this.m_masterMixer.FindSnapshot("Menu").TransitionTo(this.m_snapshotTransitionTime);
            return;             return;
        case AudioMan.Snapshot.Indoor:         case AudioMan.Snapshot.Indoor:
            this.m_masterMixer.FindSnapshot("Indoor").TransitionTo(this.m_snapshotTransitionTime);             this.m_masterMixer.FindSnapshot("Indoor").TransitionTo(this.m_snapshotTransitionTime);
            return;             return;
        default:         default:
            return;             return;
        }         }
    }     }
   
    public void StopAmbientLoop()     public void StopAmbientLoop()
    {     {
        this.m_queuedAmbientLoop = null;         this.m_queuedAmbientLoop = null;
        this.m_stopAmbientLoop = true;         this.m_stopAmbientLoop = true;
    }     }
   
    public void QueueAmbientLoop(AudioClip clip, float vol)     public void QueueAmbientLoop(AudioClip clip, float vol)
    {     {
        if (this.m_queuedAmbientLoop == clip && this.m_queuedAmbientVol == vol)         if (this.m_queuedAmbientLoop == clip && this.m_queuedAmbientVol == vol)
        {         {
            return;             return;
        }         }
        if (this.m_queuedAmbientLoop == null && this.m_ambientLoopSource.clip == clip && this.m_ambientVol == vol)         if (this.m_queuedAmbientLoop == null && this.m_ambientLoopSource.clip == clip && this.m_ambientVol == vol)
        {         {
            return;             return;
        }         }
        this.m_queuedAmbientLoop = clip;         this.m_queuedAmbientLoop = clip;
        this.m_queuedAmbientVol = vol;         this.m_queuedAmbientVol = vol;
        this.m_stopAmbientLoop = false;         this.m_stopAmbientLoop = false;
    }     }
   
    private void UpdateWindAmbience(float dt)     private void UpdateWindAmbience(float dt)
    {     {
.        if (ZoneSystem.instance == null)         if (!this.m_zoneSystemValid || !this.m_envManValid)
        {         {
            this.m_windLoopSource.volume = 0f;             this.m_windLoopSource.volume = 0f;
            return;             return;
        }         }
        float num = EnvMan.instance.GetWindIntensity();         float num = EnvMan.instance.GetWindIntensity();
        num = Mathf.Pow(num, this.m_windIntensityPower);         num = Mathf.Pow(num, this.m_windIntensityPower);
        num += num * Mathf.Sin(Time.time) * Mathf.Sin(Time.time * 1.54323f) * Mathf.Sin(Time.time * 2.31237f) * this.m_windVariation;         num += num * Mathf.Sin(Time.time) * Mathf.Sin(Time.time * 1.54323f) * Mathf.Sin(Time.time * 2.31237f) * this.m_windVariation;
        this.m_windLoopSource.volume = Mathf.Lerp(this.m_windMinVol, this.m_windMaxVol, num);         this.m_windLoopSource.volume = Mathf.Lerp(this.m_windMinVol, this.m_windMaxVol, num);
        this.m_windLoopSource.pitch = Mathf.Lerp(this.m_windMinPitch, this.m_windMaxPitch, num);         this.m_windLoopSource.pitch = Mathf.Lerp(this.m_windMinPitch, this.m_windMaxPitch, num);
    }     }
   
    private void UpdateOceanAmbiance(float dt)     private void UpdateOceanAmbiance(float dt)
    {     {
.        if (ZoneSystem.instance == null)         if (!this.m_zoneSystemValid || !this.m_envManValid)
        {         {
            this.m_oceanAmbientSource.volume = 0f;             this.m_oceanAmbientSource.volume = 0f;
            return;             return;
        }         }
        this.m_oceanUpdateTimer += dt;         this.m_oceanUpdateTimer += dt;
        if (this.m_oceanUpdateTimer > 2f)         if (this.m_oceanUpdateTimer > 2f)
        {         {
            this.m_oceanUpdateTimer = 0f;             this.m_oceanUpdateTimer = 0f;
            this.m_haveOcean = this.FindAverageOceanPoint(out this.m_avgOceanPoint);             this.m_haveOcean = this.FindAverageOceanPoint(out this.m_avgOceanPoint);
        }         }
        if (this.m_haveOcean)         if (this.m_haveOcean)
        {         {
            float windIntensity = EnvMan.instance.GetWindIntensity();             float windIntensity = EnvMan.instance.GetWindIntensity();
            float num = Mathf.Lerp(this.m_oceanVolumeMin, this.m_oceanVolumeMax, windIntensity);             float num = Mathf.Lerp(this.m_oceanVolumeMin, this.m_oceanVolumeMax, windIntensity);
            this.m_oceanAmbientSource.volume = Mathf.MoveTowards(this.m_oceanAmbientSource.volume, num, this.m_oceanFadeSpeed * dt);             this.m_oceanAmbientSource.volume = Mathf.MoveTowards(this.m_oceanAmbientSource.volume, num, this.m_oceanFadeSpeed * dt);
            this.m_oceanAmbientSource.transform.position = Vector3.Lerp(this.m_oceanAmbientSource.transform.position, this.m_avgOceanPoint, this.m_oceanMoveSpeed);             this.m_oceanAmbientSource.transform.position = Vector3.Lerp(this.m_oceanAmbientSource.transform.position, this.m_avgOceanPoint, this.m_oceanMoveSpeed);
            return;             return;
        }         }
        this.m_oceanAmbientSource.volume = Mathf.MoveTowards(this.m_oceanAmbientSource.volume, 0f, this.m_oceanFadeSpeed * dt);         this.m_oceanAmbientSource.volume = Mathf.MoveTowards(this.m_oceanAmbientSource.volume, 0f, this.m_oceanFadeSpeed * dt);
    }     }
   
    private bool FindAverageOceanPoint(out Vector3 point)     private bool FindAverageOceanPoint(out Vector3 point)
    {     {
.        Camera mainCamera = Utils.GetMainCamera();  
        if (mainCamera == null)  
        {  
            point = Vector3.zero;  
            return false;  
        }  
        Vector3 vector = Vector3.zero;         Vector3 vector = Vector3.zero;
        int num = 0;         int num = 0;
.        Vector3 position = mainCamera.transform.position;          Vector2i zone = ZoneSystem.instance.GetZone(this.m_listenerPos);
        Vector2i zone = ZoneSystem.instance.GetZone(position);  
        for (int i = -1; i <= 1; i++)         for (int i = -1; i <= 1; i++)
        {         {
            for (int j = -1; j <= 1; j++)             for (int j = -1; j <= 1; j++)
            {             {
                Vector2i vector2i = zone;                 Vector2i vector2i = zone;
                vector2i.x += j;                 vector2i.x += j;
                vector2i.y += i;                 vector2i.y += i;
                Vector3 zonePos = ZoneSystem.instance.GetZonePos(vector2i);                 Vector3 zonePos = ZoneSystem.instance.GetZonePos(vector2i);
                if (this.IsOceanZone(zonePos))                 if (this.IsOceanZone(zonePos))
                {                 {
                    num++;                     num++;
                    vector += zonePos;                     vector += zonePos;
                }                 }
            }             }
        }         }
        if (num > 0)         if (num > 0)
        {         {
            vector /= (float)num;             vector /= (float)num;
            point = vector;             point = vector;
            point.y = ZoneSystem.instance.m_waterLevel;             point.y = ZoneSystem.instance.m_waterLevel;
            return true;             return true;
        }         }
        point = Vector3.zero;         point = Vector3.zero;
        return false;         return false;
    }     }
   
    private bool IsOceanZone(Vector3 centerPos)     private bool IsOceanZone(Vector3 centerPos)
    {     {
        float groundHeight = ZoneSystem.instance.GetGroundHeight(centerPos);         float groundHeight = ZoneSystem.instance.GetGroundHeight(centerPos);
        return ZoneSystem.instance.m_waterLevel - groundHeight > this.m_oceanDepthTreshold;         return ZoneSystem.instance.m_waterLevel - groundHeight > this.m_oceanDepthTreshold;
    }     }
   
    private AudioMan.BiomeAmbients GetAmbients(string name)     private AudioMan.BiomeAmbients GetAmbients(string name)
    {     {
        foreach (AudioMan.BiomeAmbients biomeAmbients in this.m_randomAmbients)         foreach (AudioMan.BiomeAmbients biomeAmbients in this.m_randomAmbients)
        {         {
            if (biomeAmbients.m_name == name)             if (biomeAmbients.m_name == name)
            {             {
                return biomeAmbients;                 return biomeAmbients;
            }             }
        }         }
        return null;         return null;
    }     }
   
    private AudioMan.BiomeAmbients GetBiomeAmbients(Heightmap.Biome biome)     private AudioMan.BiomeAmbients GetBiomeAmbients(Heightmap.Biome biome)
    {     {
        foreach (AudioMan.BiomeAmbients biomeAmbients in this.m_randomAmbients)         foreach (AudioMan.BiomeAmbients biomeAmbients in this.m_randomAmbients)
        {         {
            if ((biomeAmbients.m_biome & biome) != Heightmap.Biome.None)             if ((biomeAmbients.m_biome & biome) != Heightmap.Biome.None)
            {             {
                return biomeAmbients;                 return biomeAmbients;
            }             }
        }         }
        return null;         return null;
    }     }
   
.      public bool RequestPlaySound(ZSFX sfx)
      {
          if (sfx.IsLooping())
          {
              this.RegisterLoopingSound(sfx);
              return true;
          }
          if (sfx.m_maxConcurrentSources <= 0)
          {
              return true;
          }
          int hash = sfx.m_hash;
          float time = Time.time;
          Vector3 position = sfx.gameObject.transform.position;
          int num = 0;
          foreach (AudioMan.SoundHash soundHash in this.m_soundList)
          {
              if (hash == soundHash.hash)
              {
                  if (time - soundHash.playTime < this.m_concurrencyThreshold && Vector3.Distance(soundHash.position, position) < sfx.GetConcurrencyDistance())
                  {
                      num++;
                  }
                  if (num >= sfx.m_maxConcurrentSources)
                  {
                      return false;
                  }
              }
          }
          this.m_soundList.PushFront(new AudioMan.SoundHash(hash, time, position));
          return true;
      }
   
      private void RegisterLoopingSound(ZSFX sfx)
      {
          if (this.GetLoopingMaxConcurrency(sfx) < 1)
          {
              return;
          }
          int num = 0;
          using (List<ZSFX>.Enumerator enumerator = this.m_loopingSfx.GetEnumerator())
          {
              while (enumerator.MoveNext())
              {
                  if (enumerator.Current.m_hash == sfx.m_hash)
                  {
                      num++;
                  }
              }
          }
          if (num > sfx.m_maxConcurrentSources)
          {
              sfx.ConcurrencyDisable();
          }
          this.m_loopingSfx.Add(sfx);
          sfx.OnDestroyingSfx += delegate(ZSFX zsfx)
          {
              this.m_loopingSfx.Remove(zsfx);
          };
      }
   
      private int GetLoopingMaxConcurrency(ZSFX sfx)
      {
          if (sfx.m_maxConcurrentSources < 0)
          {
              return -1;
          }
          if (sfx.m_maxConcurrentSources != 0)
          {
              return sfx.m_maxConcurrentSources;
          }
          return this.m_forcedMaxConcurrentLoops;
      }
   
      private void UpdateLoopingConcurrency()
      {
          if (Time.frameCount % 16 != 0)
          {
              return;
          }
          Camera cam = Utils.GetMainCamera();
          this.m_checkedHashes.Clear();
          using (List<ZSFX>.Enumerator enumerator = this.m_loopingSfx.GetEnumerator())
          {
              Func<ZSFX, float> <>9__1;
              while (enumerator.MoveNext())
              {
                  ZSFX sfx = enumerator.Current;
                  if (!this.m_checkedHashes.Contains(sfx.m_hash))
                  {
                      this.m_checkedHashes.Add(sfx.m_hash);
                      int maxConcurrentSources = sfx.m_maxConcurrentSources;
                      IEnumerable<ZSFX> enumerable = this.m_loopingSfx.Where((ZSFX x) => x.m_hash == sfx.m_hash);
                      Func<ZSFX, float> func;
                      if ((func = <>9__1) == null)
                      {
                          func = (<>9__1 = (ZSFX x) => Vector3.Distance(x.gameObject.transform.position, cam.transform.position));
                      }
                      List<ZSFX> list = enumerable.OrderBy(func).ToList<ZSFX>();
                      for (int i = 0; i < list.Count; i++)
                      {
                          if (i > maxConcurrentSources)
                          {
                              list[i].ConcurrencyDisable();
                          }
                          else
                          {
                              list[i].ConcurrencyEnable();
                          }
                      }
                  }
              }
          }
      }
   
      public AudioListener GetActiveAudioListener()
      {
          if (this.m_activeAudioListener && this.m_activeAudioListener.isActiveAndEnabled)
          {
              return this.m_activeAudioListener;
          }
          AudioListener[] array = UnityEngine.Object.FindObjectsOfType<AudioListener>(false);
          this.m_activeAudioListener = Array.Find<AudioListener>(array, (AudioListener l) => l.enabled);
          return this.m_activeAudioListener;
      }
   
      private bool m_zoneSystemValid;
   
      private bool m_envManValid;
   
      private readonly List<ZSFX> m_loopingSfx = new List<ZSFX>();
   
      private readonly List<int> m_checkedHashes = new List<int>();
   
      private AudioListener m_activeAudioListener;
   
    private static AudioMan m_instance;     private static AudioMan m_instance;
   
    [Header("Mixers")]     [Header("Mixers")]
    public AudioMixerGroup m_ambientMixer;     public AudioMixerGroup m_ambientMixer;
   
    public AudioMixerGroup m_guiMixer;     public AudioMixerGroup m_guiMixer;
   
    public AudioMixer m_masterMixer;     public AudioMixer m_masterMixer;
   
    public float m_snapshotTransitionTime = 2f;     public float m_snapshotTransitionTime = 2f;
   
    [Header("Wind")]     [Header("Wind")]
    public AudioClip m_windAudio;     public AudioClip m_windAudio;
   
    public float m_windMinVol;     public float m_windMinVol;
   
    public float m_windMaxVol = 1f;     public float m_windMaxVol = 1f;
   
    public float m_windMinPitch = 0.5f;     public float m_windMinPitch = 0.5f;
   
    public float m_windMaxPitch = 1.5f;     public float m_windMaxPitch = 1.5f;
   
    public float m_windVariation = 0.2f;     public float m_windVariation = 0.2f;
   
    public float m_windIntensityPower = 1.5f;     public float m_windIntensityPower = 1.5f;
   
    [Header("Ocean")]     [Header("Ocean")]
    public AudioClip m_oceanAudio;     public AudioClip m_oceanAudio;
   
    public float m_oceanVolumeMax = 1f;     public float m_oceanVolumeMax = 1f;
   
    public float m_oceanVolumeMin = 1f;     public float m_oceanVolumeMin = 1f;
   
    public float m_oceanFadeSpeed = 0.1f;     public float m_oceanFadeSpeed = 0.1f;
   
    public float m_oceanMoveSpeed = 0.1f;     public float m_oceanMoveSpeed = 0.1f;
   
    public float m_oceanDepthTreshold = 10f;     public float m_oceanDepthTreshold = 10f;
   
    [Header("Random ambients")]     [Header("Random ambients")]
    public float m_ambientFadeTime = 2f;     public float m_ambientFadeTime = 2f;
   
.      [Min(1f)]
    public float m_randomAmbientInterval = 5f;     public float m_randomAmbientInterval = 5f;
   
.      [Range(0f, 1f)]
    public float m_randomAmbientChance = 0.5f;     public float m_randomAmbientChance = 0.5f;
   
    public float m_randomMinDistance = 5f;     public float m_randomMinDistance = 5f;
   
    public float m_randomMaxDistance = 20f;     public float m_randomMaxDistance = 20f;
   
    public List<AudioMan.BiomeAmbients> m_randomAmbients = new List<AudioMan.BiomeAmbients>();     public List<AudioMan.BiomeAmbients> m_randomAmbients = new List<AudioMan.BiomeAmbients>();
   
    public GameObject m_randomAmbientPrefab;     public GameObject m_randomAmbientPrefab;
   
.      [Header("Lava Ambience")]
      [Min(10f)]
      public float m_lavaScanRadius = 40f;
   
      [Min(0f)]
      public float m_lavaNoiseMinDistance = 2f;
   
      [Min(10f)]
      public float m_lavaNoiseMaxDistance = 10f;
   
      [Min(1f)]
      public float m_lavaNoiseInterval = 2.5f;
   
      [Range(0f, 1f)]
      public float m_lavaNoiseChance = 0.25f;
   
      public List<AudioClip> m_randomLavaNoises;
   
      public GameObject m_lavaLoopPrefab;
   
      [Space(16f)]
      public int m_maxLavaLoops;
   
      public float m_minDistanceBetweenLavaLoops = 10f;
   
      public float m_maxLavaLoopDistance = 40f;
   
      [Header("Shield Dome Hum")]
      public bool m_enableShieldDomeHum = true;
   
      public GameObject m_shieldHumPrefab;
   
      [Header("ZSFX Settings")]
      [Min(0f)]
      [global::Tooltip("How soon a sound trying to play after the same one counts as concurrent")]
      public float m_concurrencyThreshold = 0.2f;
   
      [Min(0f)]
      [global::Tooltip("Automatically makes sure no looping sounds are playing more than this many at a time. ZSFX components that have a max concurrency value set will use that instead.")]
      public int m_forcedMaxConcurrentLoops = 5;
   
    private AudioSource m_oceanAmbientSource;     private AudioSource m_oceanAmbientSource;
   
    private AudioSource m_ambientLoopSource;     private AudioSource m_ambientLoopSource;
   
    private AudioSource m_windLoopSource;     private AudioSource m_windLoopSource;
   
.      private AudioSource m_shieldHumSource;
   
    private AudioClip m_queuedAmbientLoop;     private AudioClip m_queuedAmbientLoop;
   
    private float m_queuedAmbientVol;     private float m_queuedAmbientVol;
   
    private float m_ambientVol;     private float m_ambientVol;
   
    private float m_randomAmbientTimer;     private float m_randomAmbientTimer;
   
    private bool m_stopAmbientLoop;     private bool m_stopAmbientLoop;
   
    private bool m_indoor;     private bool m_indoor;
   
    private float m_oceanUpdateTimer;     private float m_oceanUpdateTimer;
   
    private bool m_haveOcean;     private bool m_haveOcean;
   
    private Vector3 m_avgOceanPoint = Vector3.zero;     private Vector3 m_avgOceanPoint = Vector3.zero;
   
.      private Vector3 m_listenerPos = Vector3.zero;
   
      private float m_lavaAmbientTimer;
   
      private CircularBuffer<Vector3> m_validLavaPositions = new CircularBuffer<Vector3>(128);
   
      private List<ZSFX> m_ambientLavaLoops = new List<ZSFX>();
   
    private AudioMan.Snapshot m_currentSnapshot;     private AudioMan.Snapshot m_currentSnapshot;
   
.      private readonly CircularBuffer<AudioMan.SoundHash> m_soundList = new CircularBuffer<AudioMan.SoundHash>(512);
   
    [Serializable]     [Serializable]
    public class BiomeAmbients     public class BiomeAmbients
    {     {
        public string m_name = "";         public string m_name = "";
   
        public float m_forceFadeout = 3f;         public float m_forceFadeout = 3f;
   
        [BitMask(typeof(Heightmap.Biome))]         [BitMask(typeof(Heightmap.Biome))]
        public Heightmap.Biome m_biome;         public Heightmap.Biome m_biome;
   
        public List<AudioClip> m_randomAmbientClips = new List<AudioClip>();         public List<AudioClip> m_randomAmbientClips = new List<AudioClip>();
   
        public List<AudioClip> m_randomAmbientClipsDay = new List<AudioClip>();         public List<AudioClip> m_randomAmbientClipsDay = new List<AudioClip>();
   
        public List<AudioClip> m_randomAmbientClipsNight = new List<AudioClip>();         public List<AudioClip> m_randomAmbientClipsNight = new List<AudioClip>();
    }     }
   
    private enum Snapshot     private enum Snapshot
    {     {
        Default,         Default,
        Menu,         Menu,
        Indoor         Indoor
.      }
   
      private class SoundHash
      {
          public SoundHash(int h, float pt, Vector3 pos)
          {
              this.hash = h;
              this.playTime = pt;
              this.position = pos;
          }
   
          public int hash;
   
          public float playTime;
   
          public Vector3 position;
    }     }
} }