summaryrefslogtreecommitdiff
path: root/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AudioMan.cs
blob: 3c4d300390fbf2e2b1fec4fc79e86d2e15eae275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Rendering;

public class AudioMan : MonoBehaviour
{
	[Serializable]
	public class BiomeAmbients
	{
		public string m_name = "";

		[BitMask(typeof(Heightmap.Biome))]
		public Heightmap.Biome m_biome;

		public List<AudioClip> m_randomAmbientClips = new List<AudioClip>();

		public List<AudioClip> m_randomAmbientClipsDay = new List<AudioClip>();

		public List<AudioClip> m_randomAmbientClipsNight = new List<AudioClip>();
	}

	private enum Snapshot
	{
		Default,
		Menu,
		Indoor
	}

	private static AudioMan m_instance;

	[Header("Mixers")]
	public AudioMixerGroup m_ambientMixer;

	public AudioMixer m_masterMixer;

	public float m_snapshotTransitionTime = 2f;

	[Header("Wind")]
	public AudioClip m_windAudio;

	public float m_windMinVol;

	public float m_windMaxVol = 1f;

	public float m_windMinPitch = 0.5f;

	public float m_windMaxPitch = 1.5f;

	public float m_windVariation = 0.2f;

	public float m_windIntensityPower = 1.5f;

	[Header("Ocean")]
	public AudioClip m_oceanAudio;

	public float m_oceanVolumeMax = 1f;

	public float m_oceanVolumeMin = 1f;

	public float m_oceanFadeSpeed = 0.1f;

	public float m_oceanMoveSpeed = 0.1f;

	public float m_oceanDepthTreshold = 10f;

	[Header("Random ambients")]
	public float m_ambientFadeTime = 2f;

	public float m_randomAmbientInterval = 5f;

	public float m_randomAmbientChance = 0.5f;

	public float m_randomMinPitch = 0.9f;

	public float m_randomMaxPitch = 1.1f;

	public float m_randomMinVol = 0.2f;

	public float m_randomMaxVol = 0.4f;

	public float m_randomPan = 0.2f;

	public float m_randomFadeIn = 0.2f;

	public float m_randomFadeOut = 2f;

	public float m_randomMinDistance = 5f;

	public float m_randomMaxDistance = 20f;

	public List<BiomeAmbients> m_randomAmbients = new List<BiomeAmbients>();

	public GameObject m_randomAmbientPrefab;

	private AudioSource m_oceanAmbientSource;

	private AudioSource m_ambientLoopSource;

	private AudioSource m_windLoopSource;

	private AudioClip m_queuedAmbientLoop;

	private float m_queuedAmbientVol;

	private float m_ambientVol;

	private float m_randomAmbientTimer;

	private bool m_stopAmbientLoop;

	private bool m_indoor;

	private float m_oceanUpdateTimer;

	private bool m_haveOcean;

	private Vector3 m_avgOceanPoint = Vector3.zero;

	private Snapshot m_currentSnapshot;

	public static AudioMan instance => m_instance;

	private void Awake()
	{
		if (m_instance != null)
		{
			ZLog.Log("Audioman already exist, destroying self");
			UnityEngine.Object.DestroyImmediate(base.gameObject);
			return;
		}
		m_instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		GameObject gameObject = new GameObject("ocean_ambient_loop");
		gameObject.transform.SetParent(base.transform);
		m_oceanAmbientSource = gameObject.AddComponent<AudioSource>();
		m_oceanAmbientSource.loop = true;
		m_oceanAmbientSource.spatialBlend = 0.75f;
		m_oceanAmbientSource.outputAudioMixerGroup = m_ambientMixer;
		m_oceanAmbientSource.maxDistance = 128f;
		m_oceanAmbientSource.minDistance = 40f;
		m_oceanAmbientSource.spread = 90f;
		m_oceanAmbientSource.rolloffMode = AudioRolloffMode.Linear;
		m_oceanAmbientSource.clip = m_oceanAudio;
		m_oceanAmbientSource.bypassReverbZones = true;
		m_oceanAmbientSource.dopplerLevel = 0f;
		m_oceanAmbientSource.volume = 0f;
		m_oceanAmbientSource.Play();
		GameObject gameObject2 = new GameObject("ambient_loop");
		gameObject2.transform.SetParent(base.transform);
		m_ambientLoopSource = gameObject2.AddComponent<AudioSource>();
		m_ambientLoopSource.loop = true;
		m_ambientLoopSource.spatialBlend = 0f;
		m_ambientLoopSource.outputAudioMixerGroup = m_ambientMixer;
		m_ambientLoopSource.bypassReverbZones = true;
		m_ambientLoopSource.volume = 0f;
		GameObject gameObject3 = new GameObject("wind_loop");
		gameObject3.transform.SetParent(base.transform);
		m_windLoopSource = gameObject3.AddComponent<AudioSource>();
		m_windLoopSource.loop = true;
		m_windLoopSource.spatialBlend = 0f;
		m_windLoopSource.outputAudioMixerGroup = m_ambientMixer;
		m_windLoopSource.bypassReverbZones = true;
		m_windLoopSource.clip = m_windAudio;
		m_windLoopSource.volume = 0f;
		m_windLoopSource.Play();
		if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null)
		{
			AudioListener.volume = 0f;
			return;
		}
		AudioListener.volume = PlayerPrefs.GetFloat("MasterVolume", AudioListener.volume);
		SetSFXVolume(PlayerPrefs.GetFloat("SfxVolume", GetSFXVolume()));
	}

	private void OnDestroy()
	{
		if (m_instance == this)
		{
			m_instance = null;
		}
	}

	private void Update()
	{
		float deltaTime = Time.deltaTime;
		UpdateAmbientLoop(deltaTime);
		UpdateRandomAmbient(deltaTime);
		UpdateSnapshots(deltaTime);
	}

	private void FixedUpdate()
	{
		float fixedDeltaTime = Time.fixedDeltaTime;
		UpdateOceanAmbiance(fixedDeltaTime);
		UpdateWindAmbience(fixedDeltaTime);
	}

	public static float GetSFXVolume()
	{
		if (m_instance == null)
		{
			return 1f;
		}
		m_instance.m_masterMixer.GetFloat("SfxVol", out var value);
		return Mathf.Pow(10f, value / 20f);
	}

	public static void SetSFXVolume(float vol)
	{
		if (!(m_instance == null))
		{
			float value = Mathf.Log(Mathf.Clamp(vol, 0.001f, 1f)) * 10f;
			m_instance.m_masterMixer.SetFloat("SfxVol", value);
		}
	}

	private void UpdateRandomAmbient(float dt)
	{
		if (InMenu())
		{
			return;
		}
		m_randomAmbientTimer += dt;
		if (!(m_randomAmbientTimer > m_randomAmbientInterval))
		{
			return;
		}
		m_randomAmbientTimer = 0f;
		if (UnityEngine.Random.value <= m_randomAmbientChance)
		{
			AudioClip audioClip = SelectRandomAmbientClip();
			if ((bool)audioClip)
			{
				Vector3 randomAmbiencePoint = GetRandomAmbiencePoint();
				GameObject obj = UnityEngine.Object.Instantiate(m_randomAmbientPrefab, randomAmbiencePoint, Quaternion.identity, base.transform);
				obj.GetComponent<AudioSource>().pitch = UnityEngine.Random.Range(m_randomMinPitch, m_randomMaxPitch);
				ZSFX component = obj.GetComponent<ZSFX>();
				component.m_audioClips = new AudioClip[1] { audioClip };
				component.Play();
				component.FadeOut();
			}
		}
	}

	private Vector3 GetRandomAmbiencePoint()
	{
		Vector3 vector = Vector3.zero;
		Camera mainCamera = Utils.GetMainCamera();
		if ((bool)Player.m_localPlayer)
		{
			vector = Player.m_localPlayer.transform.position;
		}
		else if ((bool)mainCamera)
		{
			vector = mainCamera.transform.position;
		}
		float f = UnityEngine.Random.value * (float)Math.PI * 2f;
		float num = UnityEngine.Random.Range(m_randomMinDistance, m_randomMaxDistance);
		return vector + new Vector3(Mathf.Sin(f) * num, 0f, Mathf.Cos(f) * num);
	}

	private AudioClip SelectRandomAmbientClip()
	{
		if (EnvMan.instance == null)
		{
			return null;
		}
		EnvSetup currentEnvironment = EnvMan.instance.GetCurrentEnvironment();
		BiomeAmbients biomeAmbients = null;
		biomeAmbients = ((currentEnvironment == null || string.IsNullOrEmpty(currentEnvironment.m_ambientList)) ? GetBiomeAmbients(EnvMan.instance.GetCurrentBiome()) : GetAmbients(currentEnvironment.m_ambientList));
		if (biomeAmbients == null)
		{
			return null;
		}
		List<AudioClip> list = new List<AudioClip>(biomeAmbients.m_randomAmbientClips);
		List<AudioClip> collection = (EnvMan.instance.IsDaylight() ? biomeAmbients.m_randomAmbientClipsDay : biomeAmbients.m_randomAmbientClipsNight);
		list.AddRange(collection);
		if (list.Count == 0)
		{
			return null;
		}
		return list[UnityEngine.Random.Range(0, list.Count)];
	}

	private void UpdateAmbientLoop(float dt)
	{
		if (EnvMan.instance == null)
		{
			m_ambientLoopSource.Stop();
		}
		else if ((bool)m_queuedAmbientLoop || m_stopAmbientLoop)
		{
			if (!m_ambientLoopSource.isPlaying || m_ambientLoopSource.volume <= 0f)
			{
				m_ambientLoopSource.Stop();
				m_stopAmbientLoop = false;
				if ((bool)m_queuedAmbientLoop)
				{
					m_ambientLoopSource.clip = m_queuedAmbientLoop;
					m_ambientLoopSource.volume = 0f;
					m_ambientLoopSource.Play();
					m_ambientVol = m_queuedAmbientVol;
					m_queuedAmbientLoop = null;
				}
			}
			else
			{
				m_ambientLoopSource.volume = Mathf.MoveTowards(m_ambientLoopSource.volume, 0f, dt / m_ambientFadeTime);
			}
		}
		else if (m_ambientLoopSource.isPlaying)
		{
			m_ambientLoopSource.volume = Mathf.MoveTowards(m_ambientLoopSource.volume, m_ambientVol, dt / m_ambientFadeTime);
		}
	}

	public void SetIndoor(bool indoor)
	{
		m_indoor = indoor;
	}

	private bool InMenu()
	{
		if (!(FejdStartup.instance != null) && !Menu.IsVisible() && (!Game.instance || !Game.instance.WaitingForRespawn()))
		{
			return TextViewer.IsShowingIntro();
		}
		return true;
	}

	private void UpdateSnapshots(float dt)
	{
		if (InMenu())
		{
			SetSnapshot(Snapshot.Menu);
		}
		else if (m_indoor)
		{
			SetSnapshot(Snapshot.Indoor);
		}
		else
		{
			SetSnapshot(Snapshot.Default);
		}
	}

	private void SetSnapshot(Snapshot snapshot)
	{
		if (m_currentSnapshot != snapshot)
		{
			m_currentSnapshot = snapshot;
			switch (snapshot)
			{
			case Snapshot.Default:
				m_masterMixer.FindSnapshot("Default").TransitionTo(m_snapshotTransitionTime);
				break;
			case Snapshot.Indoor:
				m_masterMixer.FindSnapshot("Indoor").TransitionTo(m_snapshotTransitionTime);
				break;
			case Snapshot.Menu:
				m_masterMixer.FindSnapshot("Menu").TransitionTo(m_snapshotTransitionTime);
				break;
			}
		}
	}

	public void StopAmbientLoop()
	{
		m_queuedAmbientLoop = null;
		m_stopAmbientLoop = true;
	}

	public void QueueAmbientLoop(AudioClip clip, float vol)
	{
		if ((!(m_queuedAmbientLoop == clip) || m_queuedAmbientVol != vol) && (!(m_queuedAmbientLoop == null) || !(m_ambientLoopSource.clip == clip) || m_ambientVol != vol))
		{
			m_queuedAmbientLoop = clip;
			m_queuedAmbientVol = vol;
			m_stopAmbientLoop = false;
		}
	}

	private void UpdateWindAmbience(float dt)
	{
		if (ZoneSystem.instance == null)
		{
			m_windLoopSource.volume = 0f;
			return;
		}
		float windIntensity = EnvMan.instance.GetWindIntensity();
		windIntensity = Mathf.Pow(windIntensity, m_windIntensityPower);
		windIntensity += windIntensity * Mathf.Sin(Time.time) * Mathf.Sin(Time.time * 1.54323f) * Mathf.Sin(Time.time * 2.31237f) * m_windVariation;
		m_windLoopSource.volume = Mathf.Lerp(m_windMinVol, m_windMaxVol, windIntensity);
		m_windLoopSource.pitch = Mathf.Lerp(m_windMinPitch, m_windMaxPitch, windIntensity);
	}

	private void UpdateOceanAmbiance(float dt)
	{
		if (ZoneSystem.instance == null)
		{
			m_oceanAmbientSource.volume = 0f;
			return;
		}
		m_oceanUpdateTimer += dt;
		if (m_oceanUpdateTimer > 2f)
		{
			m_oceanUpdateTimer = 0f;
			m_haveOcean = FindAverageOceanPoint(out m_avgOceanPoint);
		}
		if (m_haveOcean)
		{
			float windIntensity = EnvMan.instance.GetWindIntensity();
			float target = Mathf.Lerp(m_oceanVolumeMin, m_oceanVolumeMax, windIntensity);
			m_oceanAmbientSource.volume = Mathf.MoveTowards(m_oceanAmbientSource.volume, target, m_oceanFadeSpeed * dt);
			m_oceanAmbientSource.transform.position = Vector3.Lerp(m_oceanAmbientSource.transform.position, m_avgOceanPoint, m_oceanMoveSpeed);
		}
		else
		{
			m_oceanAmbientSource.volume = Mathf.MoveTowards(m_oceanAmbientSource.volume, 0f, m_oceanFadeSpeed * dt);
		}
	}

	private bool FindAverageOceanPoint(out Vector3 point)
	{
		Camera mainCamera = Utils.GetMainCamera();
		if (mainCamera == null)
		{
			point = Vector3.zero;
			return false;
		}
		Vector3 zero = Vector3.zero;
		int num = 0;
		Vector3 position = mainCamera.transform.position;
		Vector2i zone = ZoneSystem.instance.GetZone(position);
		for (int i = -1; i <= 1; i++)
		{
			for (int j = -1; j <= 1; j++)
			{
				Vector2i id = zone;
				id.x += j;
				id.y += i;
				Vector3 zonePos = ZoneSystem.instance.GetZonePos(id);
				if (IsOceanZone(zonePos))
				{
					num++;
					zero += zonePos;
				}
			}
		}
		if (num > 0)
		{
			zero /= (float)num;
			point = zero;
			point.y = ZoneSystem.instance.m_waterLevel;
			return true;
		}
		point = Vector3.zero;
		return false;
	}

	private bool IsOceanZone(Vector3 centerPos)
	{
		float groundHeight = ZoneSystem.instance.GetGroundHeight(centerPos);
		if (ZoneSystem.instance.m_waterLevel - groundHeight > m_oceanDepthTreshold)
		{
			return true;
		}
		return false;
	}

	private BiomeAmbients GetAmbients(string name)
	{
		foreach (BiomeAmbients randomAmbient in m_randomAmbients)
		{
			if (randomAmbient.m_name == name)
			{
				return randomAmbient;
			}
		}
		return null;
	}

	private BiomeAmbients GetBiomeAmbients(Heightmap.Biome biome)
	{
		foreach (BiomeAmbients randomAmbient in m_randomAmbients)
		{
			if ((randomAmbient.m_biome & biome) != 0)
			{
				return randomAmbient;
			}
		}
		return null;
	}
}