File size: 3,623 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
MapVar("g_EnvSound", false)
MapVar("g_EnvSoundChannel", false)
MapVar("g_EnvSoundTimeEnd", 0)
MapVar("g_EnvSoundFadeOut", 3000)

if FirstLoad then
	g_EnvSndDebugPrints = false
end

function ToggleEnvSndDebugPrints()
	g_EnvSndDebugPrints = not g_EnvSndDebugPrints
end

function EnvSndDebugPrint(...)
	if g_EnvSndDebugPrints then
		print(...)
	end
end

local listener_size = point(const.SlabSizeX, const.SlabSizeY, 0)

function IsPointInsideRoom(pos)
	return not not EnumVolumes("Room", sizebox(pos - listener_size, listener_size * 2))
end

function IsListenerInsideRoom(listener_pos)
	return IsPointInsideRoom(listener_pos or GetListenerPos())
end

local function GetEnvObjects(pos)
	local pos_zero_Z = pos:SetZ(0)
	local max_range = GetLocationMaxRange()
	local pt_range = point(max_range, max_range, const.SanePosMaxZ)
	local box_range = box(pos_zero_Z - pt_range, pos_zero_Z + pt_range)
	
	return MapGet(box_range)
end

MapVar("g_LastEnvLocations", false)
MapVar("g_LastEnvLocationsPos", false)

function EnvironmentSoundUpdate()
	if IsEditorActive() then return end

	local pos = GetListenerPos()
	local cam_pos = camera.GetPos()
	local high = cam_pos:z() - pos:z() > 8 * guim and "High" or "Low"
	
	local locations = g_LastEnvLocations
	if not g_LastEnvLocations or not g_LastEnvLocationsPos or g_LastEnvLocationsPos:Dist(pos) > guim then
		local objs = GetEnvObjects(pos)
	
		locations = GetEnvironmentLocation(pos, objs)
		g_LastEnvLocations = locations
		g_LastEnvLocationsPos = pos
	end
	
	local sound, env_sound_fade, env_sound_volume = GetAtmosphericSound(locations, high)
	if env_sound_volume then
		env_sound_volume = MulDivTrunc(env_sound_volume, 1000, 100)
		env_sound_volume = IsListenerInsideRoom(pos) and (env_sound_volume / 2) or env_sound_volume
	end

	if g_EnvSound ~= sound then
		if g_EnvSoundChannel then
			SetSoundVolume(g_EnvSoundChannel, -1, g_EnvSoundFadeOut)
			EnvSndDebugPrint(string.format("Stopping '%s', Fade Out: %d", g_EnvSound, g_EnvSoundFadeOut))
		end
		g_EnvSoundChannel = false
		if sound then
			g_EnvSoundChannel = PlaySound(sound, env_sound_volume, g_EnvSoundFadeOut)
			local duration = GetSoundDuration(g_EnvSoundChannel) or 0
			g_EnvSoundTimeEnd = RealTime() + duration
			EnvSndDebugPrint(string.format("Playing '%s' for %d, '%s' Fade Out: %d", sound, duration, g_EnvSound, g_EnvSoundFadeOut))
		end
	elseif g_EnvSoundChannel then
		SetSoundVolume(g_EnvSoundChannel, env_sound_volume, g_EnvSoundFadeOut)
		if RealTime() - g_EnvSoundTimeEnd >= 0 then
			SetSoundVolume(g_EnvSoundChannel, -1, g_EnvSoundFadeOut)
			g_EnvSoundChannel = PlaySound(sound, env_sound_volume, g_EnvSoundFadeOut)
			local duration = GetSoundDuration(g_EnvSoundChannel) or 0
			g_EnvSoundTimeEnd = RealTime() + duration
			EnvSndDebugPrint(string.format("Re-Playing '%s' for %d, Fade Out: %d", sound, duration, g_EnvSoundFadeOut))
		end
	end
	g_EnvSound = sound
	g_EnvSoundFadeOut = env_sound_fade or 3000
end

function OnMsg.DoneMap()
	if g_EnvSoundChannel then
		StopSound(g_EnvSoundChannel)
	end
end

function OnMsg.GameEnterEditor()
	EnvSndDebugPrint(string.format("Stopping environmental sounds in editor"))
	StopSound(g_EnvSoundChannel)
	g_EnvSound = false
	g_EnvSoundChannel = false
end

MapGameTimeRepeat("EnvSound", 333, EnvironmentSoundUpdate)

function OnMsg.GatherSounds(used_sounds)
	local atmo_sounds = Presets.SoundPreset.ATMOSPHERIC or {}
	for _, preset in ipairs(atmo_sounds) do
		if not preset.Regions or table.find(preset.Regions, "Jungle") or table.find(preset.Regions, "Underground") then
			for _, bank in ipairs(preset) do
				used_sounds[bank.file] = true
			end
		end
	end
end