[color chat]

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

[color chat]

Post by Archive-Poster »

Hi everyone, I just make another code can show color chat, I test it ,and work well, so I post it

Code: Select all

/*
*
* @param  id -- set 0 to all player on server, else to one player who index equal id.
* @param  cid -- get the color index, don't set it equal 0.
* @param  color -- set the color (0 -- don't change; 1 -- Red color; 2 -- Blue color; 3 -- Grey color)
* @param  message -- cons message.
*
*/
public print_color(id, cid, color, const message[], any:...)
{
 new msg[192]
 vformat(msg, charsmax(msg), message, 5)
 /*      //if you want to use ML, enable
 replace_all(msg, charsmax(msg), "!g", "^x04")
 replace_all(msg, charsmax(msg), "!n", "^x01")
 replace_all(msg, charsmax(msg), "!t", "^x03")*/
 new param
 if (!cid) return
 else param = cid
 new team[32]
 get_user_team(param, team, 31)
 switch (color)
 {
  case 0: msg_teaminfo(param, team)
  case 1: msg_teaminfo(param, "TERRORIST")
  case 2: msg_teaminfo(param, "CT")
  case 3: msg_teaminfo(param, "SPECTATOR")
 }
 if (id) msg_saytext(id, param, msg)
 else msg_saytext(0, param, msg)
 if (color != 0) msg_teaminfo(param, team)
}
msg_saytext(id, cid, msg[])
{
 message_begin(id?MSG_ONE:MSG_ALL, get_user_msgid("SayText"), {0,0,0}, id)
 write_byte(cid)
 write_string(msg)
 message_end()
}
msg_teaminfo(id, team[])
{
 message_begin(MSG_ONE, get_user_msgid("TeamInfo"), {0,0,0}, id)
 write_byte(id)
 write_string(team)
 message_end()
} 
color settings:

0 -- Don't change
1 -- Red color
2 -- Blue color
3 -- Grey color
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Re: [color chat]

Post by Archive-Poster »

Code: Select all

stock ChatColor(const id, const input[], any:...)
{
    new count = 1, players[32]
    static msg[191]
    vformat(msg, 190, input, 3)
    
    replace_all(msg, 190, "!g", "^4") // Green Color
    replace_all(msg, 190, "!y", "^1") // Default Color
    replace_all(msg, 190, "!team", "^3") // Team Color
    replace_all(msg, 190, "!team2", "^0") // Team2 Color
    
    if (id) players[0] = id; else get_players(players, count, "ch")
    {
        for (new i = 0; i < count; i++)
        {
            if (is_user_connected(players[i]))
            {
                message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, players[i])
                write_byte(players[i]);
                write_string(msg);
                message_end();
            }
        }
    }
} 
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Re: [color chat]

Post by Archive-Poster »

Here is the version I currently use:

Code: Select all

enum COLOR_PRINT
{
	COLOR_RANDOM = -4,
	COLOR_GREY,
	COLOR_BLUE,
	COLOR_RED,
}
stock client_print_color(id, {COLOR_PRINT,_}:color_id, message[], any:...)
{
	static SayText, TeamInfo;
	if( !SayText )
		SayText = get_user_msgid("SayText");
	if( !TeamInfo )
		TeamInfo = get_user_msgid("TeamInfo");
	new const TeamName[][] = 
	{
		"",
		"TERRORIST",
		"CT",
		"SPECTATOR"
	};

	new ColorChange;
	if( _:color_id < 0 )
	{
		ColorChange = _:color_id;
		for( new i=1; i<=32; i++ )
		{
			if(is_user_connected(i))
			{
				color_id = COLOR_PRINT:i;
				break;
			}
		}
		if( _:color_id <= 0 )
			return;

		if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
		{
			write_byte(_:color_id);
			switch( COLOR_PRINT:ColorChange )
			{
				case COLOR_RED: write_string(TeamName[1]);
				case COLOR_BLUE: write_string(TeamName[2]);
				case COLOR_GREY: write_string(TeamName[3]);
				default: write_string(TeamName[random_num(1,3)]);
			}
			message_end();
			ColorChange = get_user_team(_:color_id);	
		}
	}

	new message2[128];
	message2[0] = '^1'; //The message must start with a color, this is for normal colors
	vformat(message2[1], 127, message, 4);

	if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, SayText, _, id) )
	{
		write_byte(_:color_id);
		write_string(message2);
		message_end();
	}

	if( ColorChange && message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
	{
		write_byte(_:color_id);
		write_string(TeamName[ColorChange]);
		message_end();
	}
}
And without the enum (just makes it a single function for easy adding to other plugins):

Code: Select all

stock client_print_color(id, color_id, message[], any:...)
{
	static SayText, TeamInfo;
	if( !SayText )
		SayText = get_user_msgid("SayText");
	if( !TeamInfo )
		TeamInfo = get_user_msgid("TeamInfo");
	new const TeamName[][] = 
	{
		"",
		"TERRORIST",
		"CT",
		"SPECTATOR"
	};

	new ColorChange;
	if( color_id < 0 )
	{
		ColorChange = color_id;
		for( new i=1; i<=32; i++ )
		{
			if(is_user_connected(i))
			{
				color_id = i;
				break;
			}
		}
		if( color_id <= 0 )
			return;

		if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
		{
			write_byte(color_id);
			switch( ColorChange )
			{
				case -1: write_string(TeamName[1]);
				case -2: write_string(TeamName[2]);
				case -3: write_string(TeamName[3]);
				default: write_string(TeamName[random_num(1,3)]);
			}
			message_end();
			ColorChange = get_user_team(color_id);	
		}
	}

	new message2[128];
	message2[0] = '^1'; //The message must start with a color, this is for normal colors
	vformat(message2[1], 127, message, 4);

	if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, SayText, _, id) )
	{
		write_byte(color_id);
		write_string(message2);
		message_end();
	}

	if( ColorChange && message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
	{
		write_byte(color_id);
		write_string(TeamName[ColorChange]);
		message_end();
	}
}
All you have to do is not put any color codes in the message.
In other words:

Code: Select all

client_print_color(id, COLOR_RED, "This is normal colored text.")
client_print_color(id, COLOR_RED, "^3This is red colored text.")
client_print_color(id, COLOR_RED, "^4This is green colored text.")

client_print_color(id, COLOR_BLUE, "This is normal colored text.")
client_print_color(id, COLOR_BLUE, "^3This is blue colored text.")
client_print_color(id, COLOR_BLUE, "^4This is green colored text.")

client_print_color(id, COLOR_GREY, "This is normal colored text.")
client_print_color(id, COLOR_GREY, "^3This is grey colored text.")
client_print_color(id, COLOR_GREY, "^4This is green colored text.")

client_print_color(id, COLOR_RANDOM, "This is normal colored text.")
client_print_color(id, COLOR_RANDOM, "^3This is random colored text.")
client_print_color(id, COLOR_RANDOM, "^4This is green colored text.")
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Re: [color chat]

Post by Archive-Poster »

I mean show the default color settings !

Code: Select all

0 -- Don't change
1 -- Red color
2 -- Blue color
3 -- Grey color
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Re: [color chat]

Post by Archive-Poster »

Added COLOR_TEAM:

With enum:

Code: Select all

enum COLOR_PRINT
{
	COLOR_TEAM = -5,
	COLOR_RANDOM,
	COLOR_GREY,
	COLOR_BLUE,
	COLOR_RED,
}

stock client_print_color(id, {_,COLOR_PRINT}:color_id, message[], any:...)
{
	static SayText, TeamInfo;
	if( !SayText )
		SayText = get_user_msgid("SayText");
	if( !TeamInfo )
		TeamInfo = get_user_msgid("TeamInfo");

	if( COLOR_PRINT:color_id == COLOR_TEAM )
	{
		new message2[128];
		message2[0] = '^1'; //The message must start with a color, this is for normal colors
		vformat(message2[1], 127, message, 4);

		if( id )
		{
			message_begin( MSG_ONE_UNRELIABLE, SayText, _, id);
			write_byte(id);
			write_string(message2);
			message_end();
		}
		else
		{
			for( new i; i<=32; i++ )
			{
				if( is_user_connected(i) )
				{
					message_begin( MSG_ONE_UNRELIABLE, SayText, _, i);
					write_byte(i);
					write_string(message2);
					message_end();
				}
			}
		}
		return;
	}

	new const TeamName[][] = 
	{
		"",
		"TERRORIST",
		"CT",
		"SPECTATOR"
	};

	new ColorChange;
	if( color_id < 0 )
	{
		ColorChange = color_id;
		for( new i=1; i<=32; i++ )
		{
			if(is_user_connected(i))
			{
				color_id = i;
				break;
			}
		}
		if( color_id <= 0 )
			return;

		if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
		{
			write_byte(color_id);
			switch( COLOR_PRINT:ColorChange )
			{
				case COLOR_RED: write_string(TeamName[1]);
				case COLOR_BLUE: write_string(TeamName[2]);
				case COLOR_GREY: write_string(TeamName[3]);
				default: write_string(TeamName[random_num(1,3)]);
			}
			message_end();
			ColorChange = get_user_team(color_id);	
		}
	}

	new message2[128];
	message2[0] = '^1'; //The message must start with a color, this is for normal colors
	vformat(message2[1], 127, message, 4);

	if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, SayText, _, id) )
	{
		write_byte(color_id);
		write_string(message2);
		message_end();
	}

	if( ColorChange && message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
	{
		write_byte(color_id);
		write_string(TeamName[ColorChange]);
		message_end();
	}
}
Without enum:

Code: Select all

stock client_print_color(id, color_id, message[], any:...)
{
	static SayText, TeamInfo;
	if( !SayText )
		SayText = get_user_msgid("SayText");
	if( !TeamInfo )
		TeamInfo = get_user_msgid("TeamInfo");

	if( color_id == -5 )
	{
		new message2[128];
		message2[0] = '^1'; //The message must start with a color, this is for normal colors
		vformat(message2[1], 127, message, 4);

		if( id )
		{
			message_begin( MSG_ONE_UNRELIABLE, SayText, _, id);
			write_byte(id);
			write_string(message2);
			message_end();
		}
		else
		{
			for( new i; i<=32; i++ )
			{
				if( is_user_connected(i) )
				{
					message_begin( MSG_ONE_UNRELIABLE, SayText, _, i);
					write_byte(i);
					write_string(message2);
					message_end();
				}
			}
		}
		return;
	}

	new const TeamName[][] = 
	{
		"",
		"TERRORIST",
		"CT",
		"SPECTATOR"
	};

	new ColorChange;
	if( color_id < 0 )
	{
		ColorChange = color_id;
		for( new i=1; i<=32; i++ )
		{
			if(is_user_connected(i))
			{
				color_id = i;
				break;
			}
		}
		if( color_id <= 0 )
			return;

		if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
		{
			write_byte(color_id);
			switch( ColorChange )
			{
				case -1: write_string(TeamName[1]);
				case -2: write_string(TeamName[2]);
				case -3: write_string(TeamName[3]);
				default: write_string(TeamName[random_num(1,3)]);
			}
			message_end();
			ColorChange = get_user_team(color_id);	
		}
	}

	new message2[128];
	message2[0] = '^1'; //The message must start with a color, this is for normal colors
	vformat(message2[1], 127, message, 4);

	if( message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, SayText, _, id) )
	{
		write_byte(color_id);
		write_string(message2);
		message_end();
	}

	if( ColorChange && message_begin( id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, TeamInfo, _, id) )
	{
		write_byte(color_id);
		write_string(TeamName[ColorChange]);
		message_end();
	}
}
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

Re: [color chat]

Post by Archive-Poster »

Here is mine lol

Code: Select all

enum _:Colors {
    DONT_CHANGE,
    TERRORIST,
    CT,
    SPECTATOR
}

ColorChat(id, COLOR=DONT_CHANGE, fmt[], any:...)
{
    new szMsg[192]
    szMsg[0] = 0x04
    vformat(szMsg[1], charsmax(szMsg)-1, fmt, 4)

    new szTeam[11], MSG_DEST = id ? MSG_ONE : MSG_ALL

    static const szTeamNames[Colors][] = {"UNASSIGNED", "TERRORIST", "CT", "SPECTATOR"}

    if( COLOR )
    {
        Send_TeamInfo(id, szTeamNames[COLOR], MSG_DEST)
    }

    static iSayText

    if( iSayText || (iSayText = get_user_msgid("SayText")) )
    {
        message_begin(MSG_DEST, iSayText, _, id)
        {
            write_byte(id ? id : 1)
            write_string(szMsg)
        }
        message_end()
    }

    if( COLOR )
    {
        if( id || is_user_connected(1) )
        {
            get_user_team(id ? id : 1, szTeam, charsmax(szTeam))
            Send_TeamInfo(id, szTeam, MSG_DEST)
        }
        else
        {
            Send_TeamInfo(0, "UNASSIGNED", MSG_DEST)
        }
    }
}

Send_TeamInfo(const id, const szTeam[], MSG_DEST)
{
    static iTeamInfo
    if( iTeamInfo || (iTeamInfo = get_user_msgid("TeamInfo")) )
    {
        message_begin(MSG_DEST, iTeamInfo, _, id)
        {
            write_byte(id ? id : 1)
            write_string(szTeam)
        }
        message_end()
    }
} 
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest