-- Function to check if a player has the flag.
function _R.Player:HasFlag( flag )
	for k, v in pairs( self.epsilonScriptData[ self.CharID ].Flags ) do
		if ( v == flag ) then
			return true;
		end;
	end;
	
	return false;
end;

-- Help function to return the current characters inventory.
function _R.Player:GetActiveInventory()
	return self.epsilonScriptData[ self.CharID ].Inventory;
end;

-- Function to insert a item for the players inventory.
function _R.Player:GiveItem( item, amount )
	local Inventory = self:GetActiveInventory();
	local InventoryItems = self:GetActiveInventory().Items;
	
	if ( InventoryItems[ item ] ) then
		InventoryItems[ item ] = InventoryItems[ item ] + amount;
	else
		InventoryItems[ item ] = 0;
		InventoryItems[ item ] = InventoryItems[ item ] + amount;
	end
	
	Inventory.Weight = Inventory.Weight + epsilonScript.Items.GetItem( item ).Weight;

	umsg.Start( "GiveItem", self );
		umsg.String( item );
		umsg.Long( amount );
		umsg.Float( Inventory.Weight );
	umsg.End();
end;

-- Function to remove a item for the players inventory.
function _R.Player:RemoveItem( item, amount )
	local Inventory = self:GetActiveInventory();
	local InventoryItems = self:GetActiveInventory().Items;
	
	InventoryItems[ item ] = InventoryItems[ item ] - amount;
	Inventory.Weight = Inventory.Weight - epsilonScript.Items.GetItem( item ).Weight;
	
	if ( InventoryItems[ item ] == 0 ) then
		InventoryItems[ item ] = nil;
	end;
	
	umsg.Start( "RemoveItem", self );
		umsg.String( item );
		umsg.Long( amount );
		umsg.Float( Inventory.Weight );
	umsg.End();
end;

-- Function to check if the player has the item.
function _R.Player:HasItem( item )
	local Inventory = self:GetActiveInventory().Items;
	
	for k, amount in pairs( Inventory ) do
		if ( k == item ) then
			if ( amount >= 1 ) then
				return true;
			else
				return false;
			end;
		end;
	end;
end;

-- Function to send the players saved inventory items.
function _R.Player:SendSavedInventory()
	local Inventory = self:GetActiveInventory()
	
	if ( !Inventory.Items ) then
		return;
	end;
	
	datastream.StreamToClients( self, "RecieveSavedInventory", { Inventory.Items, Inventory.Weight, Inventory.MaxWeight } );
end;

-- Function to check if the player has inventory space for the item.
function _R.Player:HasInventorySpace( Item )
	if ( ( self:GetActiveInventory().Weight + epsilonScript.Items.GetItem( Item ).Weight ) > self:GetActiveInventory().MaxWeight ) then
		return false;
	end;
	
	return true;
end;

-- Function to spawn a item.
function _R.Player:SpawnItem( ID )
	if ( epsilonScript.Items.Stored[ ID ] == nil ) then
		self:ChatPrint( "Item class doesn't exist." );
		return;
	end;
	
	local tr = self:EyeTrace( 90 );
	local x, y, z = tr.HitPos.x, tr.HitPos.y, tr.HitPos.z + 16;
	
	local Item = ents.Create( "eps_item" );
	Item:SetModel( epsilonScript.Items.GetItem( ID ).Model );
	Item:SetPos( Vector( x, y, z ) );
	Item.InfoTable = epsilonScript.Items.GetItem( ID );
	Item:Spawn();
end;

function _R.Player:UpdateFlags()
	for k, v in pairs( self:GetCharInfo( "Flags" ) ) do
		umsg.Start( "CPHF", self );
			umsg.String( v );
		umsg.End();
	end;
end;

-- Function to update a players money, better than setting a NWInt cuz everyone doesn't need to know your money ;).
function _R.Player:UpdateMoney()
	umsg.Start( "UpdateMoney", ply );
		umsg.Long( self:GetCharInfo( "Money" ) );
	umsg.End();
end;

-- Function to check if a player can afford the amount.
function _R.Player:CanAfford( amount )
	if ( amount == nil ) then 
		Error( "Function 'ply:CanAfford()' Given amount is nil!" );
	end;
	
	return tonumber( self:GetCharInfo( "Money" ) ) >= amount;
end;

function _R.Player:SendNotify( msg )
	umsg.Start( "RN", self );
		umsg.String( msg );
	umsg.End();
end;

function _R.Player:TakeAnimatedLegs()
	if ( ValidEntity( self.AnimatedLegs ) ) then
		self.AnimatedLegs:Remove();
	end;
	
	self.AnimatedLegs = nil;
end;

function _R.Player:GiveAnimatedLegs()
	self:TakeAnimatedLegs();
	
	local legs = ents.Create( "eps_legs" );
	
	if ( !self:InVehicle() ) then
		legs:SetPos( self:GetPos() + ( self:GetForward() * -16 ) );
	elseif ( ValidEntity( self:GetVehicle() ) ) then
		local entityModel = string.lower( self:GetVehicle():GetModel() );
		
		if ( string.find( entityModel, "chair" ) or string.find( entityModel, "seat" ) ) then
			legs:SetPos( self:GetPos() + ( self:GetForward() * -8 ) );
		else
			legs:SetPos( self:GetPos() );
		end;
	else
		legs:SetPos( self:GetPos() + ( self:GetForward() * -8 ) );
	end;
	
	legs:SetAngles( Angle(0, ( self:GetAngles().y ), 0) );
	legs:DrawShadow( false );
	legs:SetParent( self );
	legs:SetModel( self:GetModel() );
	legs:SetOwner( self );
	legs:Spawn();
	
	self.AnimatedLegs = legs;
end;

function _R.Player:Ragdoll( int )
	self.Weapons = {};
	
	self.RagdollEntity = ents.Create( "prop_ragdoll" );
	self.RagdollEntity:SetModel( self:GetModel() );
	self.RagdollEntity:SetPos( self:GetPos() );
	self.RagdollEntity:SetAngles( self:GetAngles() );
	self.RagdollEntity:Spawn();

	for k, v in pairs( self:GetWeapons() ) do
		table.insert( self.Weapons, v:GetClass() );
	end;
	
	self:StripWeapons()
	self:SpectateEntity( self.RagdollEntity )
	self:Spectate( OBS_MODE_CHASE )

	timer.Create( "Wakeupplayer", int, 1, function() 
		self:Spawn();
		self:SetPos( self.RagdollEntity:GetPos() + Vector( 0, 0, 32 ) );
		
		for k, v in pairs( self.Weapons ) do
			self:Give( v );
			self.Weapons = nil;
		end;
		
		if ( self.RagdollEntity != NULL ) then
			self.RagdollEntity:Remove();
		end;
		
		self.RagdollEntity = nil;
	end, self );
end;
