--[[------------------------------------------------------------------------------------------------------------------
Класс zone_sound. Проигровка звуков при входе в зону. Разрешение проигровки рандомной озвучки в зоне.
Чугай Саша
--------------------------------------------------------------------------------------------------------------------]]

-- типы звуков для зон
local sound_types = {
	floor_wooden = {
		"ambient\\floor_creak1",
		"ambient\\floor_creak2",
		"ambient\\floor_creak3"
	},
	random = {
		"ambient\\random\\new_drone1",
		"ambient\\random\\new_drone2",
		"ambient\\random\\rnd_3dmbridge",
		"ambient\\random\\rnd_ak47_1",
		"ambient\\random\\rnd_ak47_2",
		"ambient\\random\\rnd_crow",
		"ambient\\random\\rnd_disgusting",
		"ambient\\random\\rnd_distantmortar3",
		"ambient\\random\\rnd_dog6",
		"ambient\\random\\rnd_drone1",
		"ambient\\random\\rnd_drone2",
		"ambient\\random\\rnd_fallscream",
		"ambient\\random\\rnd_horror3",
		"ambient\\random\\rnd_horror4",
		"ambient\\random\\rnd_m-16_3",
		"ambient\\random\\rnd_m-16_4",
		"ambient\\random\\rnd_m-249",
		"ambient\\random\\rnd_moan",
		"ambient\\random\\rnd_moan1",
		"ambient\\random\\rnd_moan2",
		"ambient\\random\\rnd_moan3",
		"ambient\\random\\rnd_moan4",
		"ambient\\random\\rnd_moan5",
		"ambient\\random\\rnd_scr1",
		"ambient\\random\\rnd_scr2",
		"ambient\\random\\rnd_scr3",
		"ambient\\random\\rnd_scr4",
		"ambient\\random\\rnd_scr5",
		"ambient\\random\\rnd_scr7",
		"ambient\\random\\rnd_scr8",
		"ambient\\random\\rnd_scr9",
		"ambient\\random\\rnd_scr10",
		"ambient\\random\\rnd_the_horror1",
		"ambient\\random\\rnd_the_horror2",
		"ambient\\random\\rnd_the_horror3",
		"ambient\\random\\rnd_the_horror4",
		"ambient\\random\\rnd_thunder",
		"ambient\\random\\rnd_wolfhowl01",
		"ambient\\random\\rnd_wolfhowl02",
		"ambient\\random\\rt_coo1-m",
		"ambient\\random\\rt_sickened1",
		"ambient\\random\\rt_sickened2",
		"ambient\\random\\rt_swamp_thing1"
	}
}


-- включить в dest все элементы из src. Таблицы индексированы
function table_include( dest, src )
	if dest and src then
		for i, v in src do
			table.insert( dest, v )
		end
	end
end

-- прочитать число из ini
function r_num( spawn_ini, section, line, default )
	if spawn_ini:line_exist( section, line ) then
		return spawn_ini:r_float( section, line )
	else
		return default
	end
end

----------------------------------------------------------------------------------------------------------------------
local pos, ang

class "zone_sound"

function zone_sound:__init( zone, binder, ini )
	self.zone        = zone
	self.sound_names = {}

	local sect = "sound"

	if ini:line_exist( sect, "snd" ) then
		table_include( self.sound_names, parse_names( ini:r_string( sect, "snd" ) ) )
	end

	if ini:line_exist( sect, "type" ) then
		local t = parse_names( ini:r_string( sect, "type" ) )

		for i, v in t do
			if v == "random" then
				self.random_pos = true
			end

			table_include( self.sound_names, sound_types[v] )
		end
	end

	if #self.sound_names == 0 then
		utils.abort( "restrictor '%s': no sounds specified in section 'sound'", zone:name() )
	end

--	printf( table.concat( self.sound_names, "  " ) )
	self.delay   = r_num( ini, sect, "delay", 0 )
	self.idle    = r_num( ini, sect, "idle",  0 ) * 1000 -- секунды->мс
	self.rnd     = r_num( ini, sect, "rnd", 100 )

	self.idle_end = 0
end

function zone_sound:on_enter( obj )
	if self.idle_end < game.time() then
		self.idle_end = game.time() + self.idle

		if get_clsid( obj ) == clsid.actor and math.random( 100 ) <= self.rnd then
			pos = obj:position()

			if self.random_pos then
				ang = math.pi / 180.0 * math.random( 0, 35900 ) * 0.01
				pos:add( vector():set( math.cos( ang ), 0, math.sin( ang ) ):mul( math.random( 1500, 5000 ) * 0.01 ) )
--				printf( "%f", obj:position():distance_to( pos ) )
			end

			sound_object( self.sound_names[math.random(1, #self.sound_names)] ):play_at_pos( obj, pos, self.delay )
		end
	end
end
