-- Player first spawn.
function GM:PlayerInitialSpawn( ply )
	self.BaseClass:PlayerInitialSpawn( ply );
	
	-- Load the player.
	epsilonScript.LoadPlayer( ply );
end;

-- Each player spawn.
function GM:PlayerSpawn( ply )
	self.BaseClass:PlayerSpawn( ply );
	
	if ( !ply.Ready ) then
		ply:Freeze( true );
	else
		if ( !epsilonScript.Holster.Stored.NeverHolsteredWeapons[ ply:GetActiveWeapon():GetClass() ] ) then
			epsilonScript.Holster.PlayerHolster( ply )
		end;
		
		ply:Give( "weapon_physgun" );
		ply:Give( "weapon_physcannon" );
		ply:Give( "gmod_tool" );
	end;
	
	-- Set the player's speed.
	GAMEMODE:SetPlayerSpeed( ply, epsilonScript.Config:Get( "WalkSpeed" ), epsilonScript.Config:Get( "RunSpeed" ) );
	
	-- Class playerspawn function.
	if ( ply.Ready and type( epsilonScript.Classes.GetClass( ply:Team() ).PlayerSpawnHook ) == "function" ) then
		epsilonScript.Classes.GetClass( ply:Team() ).PlayerSpawnHook( ply );
	end;
end;

-- Sets the player model.
function GM:PlayerSetModel( ply )
	if ( ply.Ready ) then
		local TeamModel = epsilonScript.Classes.GetClass( ply:Team() ).Model;
		
		if ( TeamModel != nil ) then
			ply:SetModel( TeamModel );
		else
			ply:SetModel( ply:GetCharInfo( "Model" ) );
		end;
	end;
end;

-- Called when a player's weapons should be given.
function GM:PlayerLoadout( ply )
	if ( ply.Ready ) then
		-- Remove all the previous weapons from the player.
		ply:StripWeapons();
		
		-- Loop through the team table.
		for k, v in pairs( epsilonScript.Classes.Stored ) do
			if ( ply:Team() == v.TeamID ) then
				for k2, v2 in pairs( v.Weapons ) do
					ply:Give( v2 );
				end;
			end;
		end;
	end;
end;

-- Modifies the damage received. 
local HurtSoundDelay = 0
function GM:ScalePlayerDamage( ply, hitgroup, dmginfo )
	self.BaseClass:ScalePlayerDamage( ply, hitgroup, dmginfo );
	
	if ( HurtSoundDelay < CurTime() ) then
		HurtSoundDelay = CurTime() + 0.3;
		
		if ( string.find( string.lower( ply:GetModel() ), "female" ) ) then
			if ( hitgroup == HITGROUP_RIGHTLEG or hitgroup == HITGROUP_LEFTLEG ) then
				ply:EmitSound( "vo/npc/female01/myleg0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_RIGHTARM or hitgroup == HITGROUP_LEFTARM ) then
				ply:EmitSound( "vo/npc/female01/myarm0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_GEAR or hitgroup == HITGROUP_STOMACH ) then
				ply:EmitSound( "vo/npc/female01/hitingut0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_HEAD ) then
				ply:EmitSound( "vo/npc/female01/pain0" .. math.random( 1, 9 ) .. ".wav" );
			end;
		end;
		
		if ( string.find( string.lower( ply:GetModel() ), "male" ) ) then
			if ( hitgroup == HITGROUP_RIGHTLEG or hitgroup == HITGROUP_LEFTLEG ) then
				ply:EmitSound( "vo/npc/male01/myleg0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_RIGHTARM or hitgroup == HITGROUP_LEFTARM ) then
				ply:EmitSound( "vo/npc/male01/myarm0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_GEAR or hitgroup == HITGROUP_STOMACH ) then
				ply:EmitSound( "vo/npc/male01/hitingut0" .. math.random( 1, 2 ) .. ".wav" );
			elseif ( hitgroup == HITGROUP_HEAD ) then
				ply:EmitSound( "vo/npc/male01/pain0" .. math.random( 1, 9 ) .. ".wav" );
			end;
		end;
	end;
end;

-- When a player disconnects.
function GM:PlayerDisconnected( ply )
	epsilonScript.SavePlayer( ply );
end;

-- Decides if the player can suicide or not.
function GM:CanPlayerSuicide( ply )
	return epsilonScript.Config:Get( "AllowSuicide" );
end;

-- Called when a player attempts to spawn a SWEP.
function GM:PlayerSpawnSWEP( ply, class, weapon )
	if ( !ply:IsSuperAdmin() ) then
		return false;
	else
		return true;
	end;
end;

-- Called when a player is given a SWEP.
function GM:PlayerGiveSWEP( ply, class, weapon )
	if ( !ply:IsSuperAdmin() ) then
		return false;
	else
		return true;
	end;
end;

-- Called when a player attempts to spawn a SENT.
function GM:PlayerSpawnSENT( ply, class )
	if ( !ply:IsSuperAdmin() ) then
		return false;
	else
		return true;
	end;
end;

-- Disables the beeeeeeeeeeep sound on death.
function GM:PlayerDeathSound()
	return true;
end;

-- Function when player enters a vehicle.
function GM:PlayerEnteredVehicle( ply, vehicle, role )
	self.BaseClass:PlayerEnteredVehicle( ply, vehicle, role );
	
	ply:TakeAnimatedLegs();
end;

-- Function when player exits the vehicle.
function GM:PlayerLeaveVehicle( ply, vehicle )
	self.BaseClass:PlayerLeaveVehicle( ply, vehicle )
	
	ply:TakeAnimatedLegs();
end;