local meta = FindMetaTable( "Entity" );
if( !meta ) then

	return;

end

function meta:GetPosCenter( )
	local pos = self:OBBCenter()
	local ang = self:GetAngles()
	local pos_center = self:GetPos() + ang:Up() * pos.z + ang:Forward() * pos.x + ang:Right() * pos.y
	return pos_center
end

function meta:FireOutput( output_name )
	if !self.output[output_name] then return end
	for k, v in pairs( self.output[output_name] ) do
		local output_exp = string.Explode( ",", v )
		local output_ents = ents.FindByName( output_exp[1] )
		local output = output_exp[2]
		local output_params = output_exp[3]
		local output_delay = output_exp[4]
		local output_once = output_exp[5]
		for k, v in pairs( output_ents ) do
			v:Fire( output, output_params, tonumber(output_delay) )
			//Msg( "Fired output to " .. v:GetName() .. ": Output: " .. output .. "; params: " .. output_params .. "; delay: " .. output_delay .. "\n" )
		end
		if output_once == "-1" then
			self.newoutputs = {}
			for k, v in pairs( self.output ) do
				if v != output_exp[1] then
					self.newoutputs[k] = v
				end
			end
			self.output = self.newoutputs
			self.newoutputs = nil
		end
	end
end

local meta = FindMetaTable( "Player" );
if( !meta ) then
	return;
end

/*------------------------------------
   SetCritChance
------------------------------------*/
function meta:SetCritChance(value)
	self:SetNetworkedInt("CritChance", value)
end

/*------------------------------------
   AddCritChance
------------------------------------*/
function meta:AddCritChance(value)
	self:SetNetworkedInt("CritChance", self:GetCritChance() +value)
end

/*------------------------------------
   GetCritChance
------------------------------------*/
function meta:GetCritChance()
	return self:GetNetworkedInt("CritChance")
end

/*------------------------------------
   RefreshAmmo
------------------------------------*/
function meta:RefreshAmmo()
end

local meta = FindMetaTable( "Weapon" );
if( !meta ) then
	return;
end

/*------------------------------------
   GetWeaponType
------------------------------------*/
function meta:GetWeaponType()
	local weapons_melee = { "tf_weapon_bat", "tf_weapon_bonesaw", "tf_weapon_bottle", "tf_weapon_club", "tf_weapon_fireaxe", "tf_weapon_fists", "tf_weapon_sapper", "tf_weapon_shovel", "tf_weapon_wrench" }
	local weapons_primary = { "tf_weapon_medigun", "tf_weapon_pistol", "tf_weapon_flamethrower" }
	if table.HasValue( weapons_melee, self:GetClass() ) then return "MELEE"
	elseif table.HasValue( weapons_primary, self:GetClass() ) then return "PRIMARY"
	else return "SECONDARY" end
end

/*------------------------------------
   Critical
------------------------------------*/
function meta:Critical()
	if GetGlobalBool( "humiliation" ) then return true end
	local critchance_wep = self.CritChance
	local critchance = self.Owner:GetCritChance() +critchance_wep
	if critchance <= 0 then return false end
	local rand = math.random(1,100 /critchance)
	if rand == 100 /critchance then return true end
	return false
end

/*------------------------------------
   CriticalHit
------------------------------------*/
function meta:CriticalHit(pos)
	local critsound = "player/crit_hit" .. math.random(1,5) .. ".wav"
	if string.find(critsound, "1") then critsound = "player/crit_hit.wav" end
	WorldSound( critsound, pos, 115 )
	ParticleEffect( "crit_text", pos, Angle(0,0,0) )
end

/*------------------------------------
   GetDamageFalloff
------------------------------------*/
function meta:GetDamageFalloff(dist,maxdist,maxdmg,mindmg)
	local dmg = (1-(dist /maxdist)) *maxdmg
	if !mindmg or dmg >= mindmg then return dmg end
	if dmg < mindmg then return mindmg end
end
