-- Chat commands table.
epsilonScript.ChatCommands = {};

-- Function to add chat commands.
function epsilonScript.AddChatCmd( name, cmd )
	epsilonScript.ChatCommands[ name ] = cmd;
end;

-- Function for chat to make players 'hear text' on range.
function epsilonScript.PrintMessageToArea( vec, size, text )
	for k, v in pairs( player.GetAll() ) do
		if ( ( v:EyePos() - vec ):Length() <= size ) then
			v:PrintMessage( HUD_PRINTTALK, text );
		end;
	end;
end;

-- Called when a player types and enters a text message.
function GM:PlayerSay( ply, text, public )
	if ( string.sub( text, 1, 2 ) == "//" ) then
		return "[OOC] " .. string.Trim( string.sub( text, 3 ) );
	end;
	
	if ( string.sub( text, 1, 3 ) == ".//" ) then
		epsilonScript.PrintMessageToArea( ply:EyePos(), 390, "[LOCAL-OOC] " .. ply:Name() .. ": " .. string.Trim( string.sub( text, 4 ) ) );
		return "";
	end;
	
	if ( string.find( text, "/" ) == 1 ) then
		local command = string.lower( string.sub( string.Explode( " ", text )[ 1 ], 2 ) );
		
		if ( epsilonScript.ChatCommands[ command ] ) then
			ply:ConCommand( epsilonScript.ChatCommands[ command ] .. string.sub( text, string.len( command ) + 2 ) );
		else
			ply:ChatPrint( "Invalid Command" );
		end;
		
		return "";
	end;
	
	epsilonScript.PrintMessageToArea( ply:EyePos(), 390, ply:Name() .. ": " .. text );
	return "";
end;

epsilonScript.AddChatCmd( "newchar", "eps_Createnewcharmenu" );
epsilonScript.AddChatCmd( "selectchar", "eps_startselectchar" );

concommand.Add( "eps_me", function( ply, cmd, args )
	epsilonScript.PrintMessageToArea( ply:EyePos(), 230, "**" .. ply:Name() .. " " .. table.concat( args, " " ) );
	return "";
end );
epsilonScript.AddChatCmd( "me", "eps_me" );

concommand.Add( "eps_yell", function( ply, cmd, args )
	epsilonScript.PrintMessageToArea( ply:EyePos(), 300, "[YELL] " .. ply:Name() .. ": " .. table.concat( args, " " ) );
	return "";
end );
epsilonScript.AddChatCmd( "y", "eps_yell" );

concommand.Add( "eps_whisper", function( ply, cmd, args )
	epsilonScript.PrintMessageToArea( ply:EyePos(), 200,"[WHISPER] " .. ply:Name() .. ": " .. table.concat( args, " " ) );
	return "";
end );
epsilonScript.AddChatCmd( "w", "eps_whisper" );

concommand.Add( "eps_restartmap", function( ply, cmd, args )
	if ( ply:EntIndex() == 0 or ply:IsAdmin() ) then
		game.ConsoleCommand( "changelevel " .. game.GetMap() .. "\n" );
	end;
end );
epsilonScript.AddChatCmd( "restartmap", "eps_restartmap" );