import os, sys, string, re

def get_soundlevel( token ):
	if token.find( "SNDLVL_NONE" ) > -1:
		return 0.0	
	if token.find( "SNDLVL_TALKING" ) > -1:
		return 60.0
	if token.find( "SNDLVL_STATIC" ) > -1:
		return 66.0
	if token.find( "SNDLVL_NORM" ) > -1:
		return 75.0
	if token.find( "SNDLVL_GUNFIRE" ) > -1:
		return 140.0

#	print token
	if token.find("SNDLVL_") > -1:
		token = token[7:]
#	print token	
	if token.find("dB") > -1:
		token = token[:-2]
	if token.find("db") > -1:
		token = token[:-2]
	if token.find("Db") > -1:
		token = token[:-2]
	if token.find("DB") > -1:
		token = token[:-2]
	print token	

	return string.atof( token )



		

def mult_values( s, mult ):
	if s.find("VOL_NORM") > -1:
		return "\"%f\"" % (mult)
	
	value_split = s.split(",")
	value_min = string.atof(value_split[0])
	if len(value_split) > 1:
		value_max = string.atof(value_split[1])
		return "\"%f, %f\"" % (value_min * mult, value_max * mult)

	return "\"%f\"" % (value_min * mult)


def mult_line( line, token, mult ):
	line_split = line.split(  )


	if len(line_split) > 1:
		match =  line_split[0].find( token )
		if match > -1:
			if token.find( "soundlevel" ) > -1:
				mult_result = get_soundlevel( line_split[1][1:-1] )
				file_output.write( "\t\"soundlevel\"\t\"SNDLVL_%idB\"\n" % ( mult_result * mult ) )
 				return 1
			else:
				mult_result = mult_values( line_split[1][1:-1], mult )
				file_output.write( "\t\"%s\"\t%s\n" % ( token, mult_result ) )
 				return 1

	return 0



file_output = open("mvm_level_sounds.txt", "w" )
file_output.write("// THIS FILE IS AUTOMATICALLY GENERATED VIA mvm_make_level_sounds.py!!!\n// DO NOT EDIT BY HAND!\n\n\n\n")


default_volume_mult = 0.7
file_input = open("game_sounds_weapons.txt")
file_lines = file_input.readlines()

for line in file_lines:

	matched = mult_line( line, "volume", default_volume_mult )

#	if matched == 0:
#		matched = mult_line( line, "soundlevel", 0.9 )	

	if matched == 0:		
		file_output.write( line )
file_input.close()		

file_input = open("game_sounds_player.txt")
file_lines = file_input.readlines()

for line in file_lines:

	matched = mult_line( line, "volume", default_volume_mult )

#	if matched == 0:
#		matched = mult_line( line, "soundlevel", 0.9 )	

	if matched == 0:		
		file_output.write( line )
file_input.close()		

file_input = open("game_sounds_physics.txt")
file_lines = file_input.readlines()

for line in file_lines:

	matched = mult_line( line, "volume", default_volume_mult )

#	if matched == 0:
#		matched = mult_line( line, "soundlevel", 0.9 )	

	if matched == 0:		
		file_output.write( line )
file_input.close()		

file_input = open("game_sounds_footsteps.txt")
file_lines = file_input.readlines()

for line in file_lines:

	matched = mult_line( line, "volume", default_volume_mult * 0.5)

#	if matched == 0:
#		matched = mult_line( line, "soundlevel", 0.9 )	

	if matched == 0:		
		file_output.write( line )
file_input.close()		

file_input = open("game_sounds.txt")
file_lines = file_input.readlines()

for line in file_lines:

	matched = mult_line( line, "volume", default_volume_mult )

#	if matched == 0:
#		matched = mult_line( line, "soundlevel", 0.9 )	

	if matched == 0:		
		file_output.write( line )
file_input.close()		

# file_input = open("game_sounds_vo.txt")
# file_lines = file_input.readlines()

# for line in file_lines:

# 	matched = mult_line( line, "volume", default_volume_mult )

# #	if matched == 0:
# #		matched = mult_line( line, "soundlevel", 0.9 )	

# 	if matched == 0:		
# 		file_output.write( line )
# file_input.close()		

# file_input = open("game_sounds_vo_handmade.txt")
# file_lines = file_input.readlines()

# for line in file_lines:

# 	matched = mult_line( line, "volume", default_volume_mult )

# #	if matched == 0:
# #		matched = mult_line( line, "soundlevel", 0.9 )	

# 	if matched == 0:		
# 		file_output.write( line )
# file_input.close()		







file_output.close()	
	
