********************************************************************************************************************************
This is my code from Vampire Wars: The Catalyst @ catalyst.genesismuds.com:62666.

Feel free to use this in your code base, but give credit where credit is due.




First, scroll through merc.h, find the old color code stuff, and replace it with this:
********************************************************************************************************************************


// added by Oberon

	#define END_CHAR	"#e"
	#define BLINK		"#i"
	#define ULINE		"#u"

	#define BLACK		"#k"
	#define WHITE		"#w"
	#define RED		"#r"
	#define LIGHTRED	"#R"
	#define GREEN		"#g"
	#define LIGHTGREEN	"#G"
	#define BROWN		"#n"
	#define YELLOW		"#l"
	#define BLUE		"#b"
	#define LIGHTBLUE	"#B"
	#define MAGENTA		"#m"
	#define LIGHTMAGENTA	"#M"
	#define CYAN		"#c"
	#define LIGHTCYAN	"#C"
	#define GREY		"#y"

	#define WHITE_BG	"#W"	
	#define RED_BG		"#0"
	#define LIGHTRED_BG	"#1"
	#define GREEN_BG	"#2"
	#define LIGHTGREEN_BG	"#3"
	#define BROWN_BG	"#N"
	#define YELLOW_BG	"#L"
	#define BLUE_BG		"#4"
	#define LIGHTBLUE_BG	"#5"
	#define MAGENTA_BG	"#6"
	#define LIGHTMAGENTA_BG	"#7"
	#define CYAN_BG		"#8"
	#define LIGHTCYAN_BG	"#9"
	#define GREY_BG		"#Y"

	#define RANDOM_COLOR	"#s"
	#define RANDOM_BG_COLOR	"#S"

	#define MAX_COLORS	32


	#define BLINKING	"[5m"
	#define UNDERLINE	"[4m"

	#define NO_COLOR 	"[0m"
	#define D_BLACK		"[0;30m"
	#define D_GREY		"[1;30m"
	#define D_RED		"[0;31m"
	#define L_RED		"[1;31m"
	#define D_GREEN		"[0;32m"
	#define L_GREEN		"[1;32m"
	#define D_BROWN     	"[0;33m" 	
 	#define D_YELLOW    	"[1;33m" 
 	#define D_BLUE    	"[0;34m" 
 	#define L_BLUE    	"[1;34m" 
 	#define D_MAGENTA   	"[0;35m" 
 	#define L_MAGENTA     	"[1;35m" 
 	#define D_CYAN    	"[0;36m" 
 	#define L_CYAN    	"[1;36m" 
 	#define NORMAL    	"[0;37m" 
 	#define L_WHITE     	"[1;37m" 

	#define D_RED_BG	"[1;41m"
	#define D_GREEN_BG	"[1;42m"
	#define D_BROWN_BG	"[1;43m"
	#define D_BLUE_BG	"[1;44m"
	#define D_MAGENTA_BG	"[1;45m"
	#define D_CYAN_BG	"[1;46m"
	#define L_GREY_BG	"[1;47m"
	#define D_GREY_BG	"[1;100m"
	#define L_RED_BG	"[1;101m"
	#define L_GREEN_BG	"[0;102m"
	#define D_YELLOW_BG	"[0;103m"
	#define L_BLUE_BG	"[1;104m"
	#define L_MAGENTA_BG	"[1;105m"
	#define L_CYAN_BG	"[0;106m"
	#define L_WHITE_BG	"[0;107m"
	


 #define ADD_COLOUR(_player,_str,_col,_str_size) \
 { \
 if (!IS_NPC(_player) && IS_SET(_player->act, PLR_ANSI)) \
 { \
     int to_move = strlen(_str)+1; \
     int to_insert = strlen(_col); \
     if ( to_move + to_insert > _str_size ) \
     { \
     to_insert = _str_size - to_move; \
     } \
 memmove( _str + to_insert, _str, to_move );          /* move the string along */\
 memcpy( _str, _col, to_insert );                     /* slip the colour code in before it */\
 strncat( _str, END_CHAR, _str_size - strlen(_str));     /* append the normal colour code */\
 } \
 } \ 

 #define SCALE_COLS 4

 #define COL_SCALE(_swh_str,_swh_ch,_swh_curr,_swh_max,_swh_str_size) \
 ADD_COLOUR(_swh_ch, _swh_str, \
 (_swh_curr < 1) ? LIGHTRED : \
 (_swh_curr < _swh_max) ? \
 scale[(SCALE_COLS * _swh_curr) / ((_swh_max > 0) \
 ? _swh_max : 1)] : LIGHTCYAN, _swh_str_size)

// end of added/modifications to col_scale and add_colour




********************************************************************************************************************************
Now, in comm.c, this is the new write to buffer.  Everything but the color aspect is exactly like the old one, but
to make this simple, am just copying and pasting the entire function.
********************************************************************************************************************************

void write_to_buffer( DESCRIPTOR_DATA *d, const char *txt, int length, int anti_trig )
 {
    char* outp;
    int triggered;
    int size;
    bool ansi, grfx = FALSE;

// added by Oberon

	bool newline = FALSE, returnline = FALSE;

	char options[MAX_COLORS+2] = {'e','i', 'u', 'k', 'w', 'r', 'R', 'g', 'G', 'n', 'l', 'b', 'B', 'm', 'M', 'c', 'C', 'y', 'W', '0',
					'1', '2', '3', 'N', 'L', '4', '5', '6', '7', '8', '9', 'Y', 's', 'S'};

	char *theColors[MAX_COLORS] = {NORMAL,BLINKING,UNDERLINE,D_BLACK,L_WHITE,D_RED,L_RED,D_GREEN, L_GREEN,
		D_BROWN, D_YELLOW, D_BLUE, L_BLUE, D_MAGENTA, L_MAGENTA, D_CYAN, L_CYAN, D_GREY, L_WHITE_BG, D_RED_BG,
		L_RED_BG, D_GREEN_BG, L_GREEN_BG, D_BROWN_BG, D_YELLOW_BG, D_BLUE_BG, L_BLUE_BG, D_MAGENTA_BG, L_MAGENTA_BG,
		D_CYAN_BG, L_CYAN_BG,D_GREY_BG}; 

	char buffer[MAX_STRING_LENGTH];

	int i=0, j=0, h=0;

// end of added


    /*
     * Find length in case caller didn't.
     */
    if ( length <= 0 )
        length = strlen(txt);

    if ( length >= MAX_STRING_LENGTH )
    {
        bug( "Write_to_buffer: Way too big. Closing.", 0 );
        return;
    }

    if (d->character == NULL ||
        (d->connected != CON_PLAYING))
        ansi = FALSE;
    else ansi = (IS_SET(d->character->act, PLR_ANSI)) ? TRUE : FALSE;

       /*
       * Initial \n\r if needed.
       */
    if ( d->outtop == 0 && !d->fcommand )
    {
       d->outbuf[0]    = '\n';
       d->outbuf[1]    = '\r';
       d->outtop    = 2;
    }


    /*
     * Expand d->outbuf for ansi info
     */

// added by Oberon

	for(i=0;i<length;i++)
	{
		if(txt[i] == '#')
		{	
			i++;
			switch (txt[i])
			{
				case '#':	buffer[j++] = '#';
						break;
				
				case '+':	buffer[j++] = '%';
						break;

				case '-':	buffer[j++] = '~';
						break;
			
				default:	
						if(ansi)
						{
							for(h=0; h<(MAX_COLORS+2);h++)
							{
								if(txt[i] == options[h])
								{
									buffer[j++] = '\e';
									if(h==MAX_COLORS)
										h = number_range(4,17);
									if(h==(MAX_COLORS+1))
										h = number_range(18, 31);
									strcpy(buffer+j, theColors[h]);
									j += strlen(theColors[h]);   
									break;
								}
							}
						}
						break;
			}
		}
		else
		{
			if((txt[i] == '\n') && (i>length-3))
				newline = TRUE;
			if((txt[i] == '\r') && (i>length-3))
				returnline = TRUE;
			buffer[j++] = txt[i];
		}
	}
	if(newline)
		--j;
	if(returnline)
		--j;
	buffer[j++] = '\e';
	strcpy(buffer+j, theColors[0]);
	j+= strlen(theColors[0]);
	if(newline)
		buffer[j++] = '\n';
	if(returnline)
		buffer[j++] = '\r';
	buffer[j] = '\0';
// end of added

    length = j;

    /* grow or shrink the buffer if need be */
    for ( size = d->outsize/2; size < length + d->outtop; size += 1000 )
    {
    }
    if ( size != d->outsize )
    {
        outp = alloc_mem( size );
        strncpy( outp, d->outbuf, d->outtop );
        free_mem( d->outbuf, d->outsize );
        d->outbuf = outp;
        d->outsize = size;
    }
   /*
    * Cap the max buffer length at 25k
    */
    if ( d->outtop + length > 25000)
    {
        length = 25000 - d->outtop;
    }
    /*
    * Copy to the buffer.
    */
    anti_trig = 0;  /* disable for now */

    if ( !anti_trig )
    {
       strncpy( d->outbuf + d->outtop, buffer, length );
       d->outtop += length;
    }
    else
    {
       triggered = 0;
       outp = d->outbuf + d->outtop;
       while ( length > 0 )
       {
          /* copy a char from txt */
          *(outp++) = *(txt++);
          ++(d->outtop);
          --length;

          if ( (!triggered) && (*(txt-1) == ' ') && (1 ==
 number_range(1,5)) )
          {
             /* anti-trigger code - Genghis
              * there's a 1/5 chance of a space beingremoved */
             --outp;
             --(d->outtop);
             triggered = 1;
          }
          else if ( *txt != ' ' )
          {
             triggered = 0;
          }
       }
       *(outp++) = '\0';
    }

    return;
 }


********************************************************************************************************************************
The 2 continual function calls in the forloop is bad form, and I apologize.  Even with that, it still takes up far less memory
and should run faster than the monsterous case stack.
********************************************************************************************************************************