function normalize(val)
	if val > 100 then
		val = 100
	elseif val < 1 then
		val  = 1
	end
	
	return val
end


function read_if_exist(ltx, section, value, default)
	if ltx:line_exist(section,value) then
		return ltx:r_float(section,value)
	else
		return default
	end
end

function Get_Fuck_1()
	local ltx = system_ini()
	
	local rpm = read_if_exist(ltx, section,"rpm", 1)
	local mag_size = ltx:r_float(section,"ammo_mag_size")
	local f_modes = read_if_exist(ltx, section, "f_mode", 1)
	
	if rpm > 100 then
		rpm = (mag_size*(60/((mag_size/(rpm/60)) + 5.5)))/2.4
	else
		rpm = rpm/14
	end
	
	if mag_size < 5	then
		rpm = 30
	end	  
	
	return normalize(rpm)
end

function Get_Fuck_2()
	local ltx = system_ini()
			
	local hit_power = ltx:r_float(section,"hit_power")
	--local hit_rate = ltx:r_float(section,"hit_rate")
	local hit_rate = read_if_exist(ltx, section, "hit_rate", 1)
	local ammo_list	= ltx:r_string(section,"ammo_class")
	
	local pos  = string.find(ammo_list,",",1)
	local k_hit
	if nil ~= pos then
		local ammo = string.sub(ammo_list,1,pos - 1)
		k_hit				= ltx:r_float(ammo, "k_hit");
	else
		k_hit				= ltx:r_float(ammo_list, "k_hit");
	end
		
	
	local hit = hit_rate + math.log10(2*hit_power*k_hit)
	
	
	val =  28*hit

	return normalize(val)
end

function GetDamageMP(section)
	local ltx = system_ini()
	local val = ltx:r_float(section,"hit_power")
		
	if val < 1.05 then
		val = val*100-30
	else
		val = 75 + math.pow(val/4,2)*25
	end
	
	return normalize(val)	
end

function Get_Fuck_3()
	local ltx = system_ini()
			
	local misfire_probability	= ltx:r_float(section,"misfire_probability")
	--local misfire_condition_k = ltx:r_float(section,"misfire_condition_k")
	local misfire_condition_k  = read_if_exist(ltx, section, "misfire_condition_k", 1)
	local condition_shot_dec = ltx:r_float(section,"condition_shot_dec")

	local ammo_list			= ltx:r_string(section,"ammo_class")
	
	local pos  = string.find(ammo_list,",",1)
	local impair
	if nil ~= pos then
		local ammo = string.sub(ammo_list,1,pos - 1)
		impair				= ltx:r_float(ammo, "impair");
	else
		impair				= ltx:r_float(ammo_list, "impair");
	end
		local n = 3000
		local cond = 1 - impair*condition_shot_dec*n
			if cond < 0 then cond = 0 
		end
		local prob
			if cond > 0.95 then prob = 0
			else
			prob = (0.95 - cond)*(misfire_condition_k - misfire_probability) + misfire_probability
		end

		local val = 100*(1 - prob*7) 
			if val < 20 then val = 20
		end
		
	return normalize(val)
end

function Get_Fuck_4()
	local ltx = system_ini()
			
	local fire_dispersion_base	= ltx:r_float(section,"fire_dispersion_base")
	--local disp_rate = ltx:r_float(section,"disp_rate")
	local disp_rate = read_if_exist(ltx, section, "disp_rate", 1)
	local ammo_list			= ltx:r_string(section,"ammo_class")
	
	local pos  = string.find(ammo_list,",",1)
	local k_disp
	if nil ~= pos then
		local ammo = string.sub(ammo_list,1,pos - 1)
		k_disp				= ltx:r_float(ammo, "k_disp");
	else
		k_disp				= ltx:r_float(ammo_list, "k_disp");
	end
	
	
	local fir = disp_rate + math.log10(0.9/(fire_dispersion_base*k_disp))
	
	
	val =  28*fir

	return normalize(val)
end

function GetRPM(section)
	local ltx = system_ini()

	local rpm = read_if_exist(ltx,section,"rpm",-1)
	if rpm == -1 then return 0 end

	if rpm > 200 then
		rpm = math.pow((rpm - 200)/720,2)*100 + 15
	else
		rpm = rpm/14
	end

	local mag_size = read_if_exist(ltx,section,"ammo_mag_size",-1)
	if mag_size > -1 and mag_size < 5	then
		rpm = 30
	end

	return normalize(rpm)
end

function GetDamage(section)
	local ltx = system_ini()
	local val = read_if_exist(ltx,section,"hit_power",-1)
	if val == -1 then return 0 end
	
	val = val + 0.2
	val = (math.pow(val, 3)/(math.pow(val, 3)+1)) 
	
	
	return normalize(100*val)
end

function GetHandling(section)
	local ltx = system_ini()
			
	local misfire_probability	= ltx:r_float(section,"misfire_probability")
	--local misfire_condition_k = ltx:r_float(section,"misfire_condition_k")
	local misfire_condition_k  = read_if_exist(ltx, section, "misfire_condition_k", 1)
	local condition_shot_dec = ltx:r_float(section,"condition_shot_dec")

	local ammo_list			= ltx:r_string(section,"ammo_class")
	
	local pos  = string.find(ammo_list,",",1)
	local impair
	if nil ~= pos then
		local ammo = string.sub(ammo_list,1,pos - 1)
		impair				= ltx:r_float(ammo, "impair");
	else
		impair				= ltx:r_float(ammo_list, "impair");
	end
		local n = 3000
		local cond = 1 - impair*condition_shot_dec*n
			if cond < 0 then cond = 0 
		end
		local prob
			if cond > 0.95 then prob = 0
			else
			prob = (0.95 - cond)*(misfire_condition_k - misfire_probability) + misfire_probability
		end

		local val = 100*(1 - prob*7) 
			if val < 20 then val = 20
		end
		
	return normalize(val)
end

function GetAccuracy(section)
	local ltx = system_ini()

	if (not ltx:line_exist(section,"ammo_class")) then return 0 end
	local ammo_list = ltx:r_string(section,"ammo_class")

	local ammo
	local pos = string.find(ammo_list,",",1)
	if nil ~= pos then
		ammo = string.sub(ammo_list,1,pos - 1)
	else
		ammo = ammo_list
	end

	local k_disp = read_if_exist(ltx,ammo,"k_disp",-1);
	if k_disp == -1 then return 0 end
	if k_disp > 3.5 then k_disp = 1.0 end -- shotgun pellets = 10

	local fire_dispersion_base = read_if_exist(ltx,section,"fire_dispersion_base",-1)
	if fire_dispersion_base == -1 then return 0 end

	local accfactor = fire_dispersion_base*k_disp
	if accfactor > 1.5 then accfactor = 1.5 end
	local val = 100 - accfactor/0.015
	val = val * val / 100

	return normalize(val)
end