Colour For Circle (More - Updated) [by Jason Berg]
Snippet Posted Wednesday, August 12th @ 11:25:39 PM, by George Greer in the Utils dept.
Added Jul 6, 1998. Click the link below to read it or download it.

From: Jason Berg <gameguru42@hotmail.com>
Subject: Colour Codes... Updated

This is a change from the origional code by Jorgen Sigvardsson
The code will run slightly faster here because it know when to run
and when to not run.
My comments end with JB
Jason Berg <Gameguru42@hotmail.com>

Original code done by :
Jorgen Sigvardsson <di4sig@sce.hks.se>
Subject: Colour Codes

Sorry about the second file but I forgot to change my color codes back to
what they were before.

Instructions
  * Edit your comm.c file.
  * Add these function to comm.c
  * Be sure these go in above write_to_output JB
int count_chars(const char *txt, char character)
{
   int i, cnt = 0;

   for(i = 0; txt[i]; i++)
      if(txt[i] == character)
         cnt++;

   return cnt;
}
                    /* Added const to both routines to fix 2 warnings */
char *parse_color(const char *txt, struct descriptor_data *t)
{
   char *new_txt;
   char *toret;
   register int i, j = 0;

   i = count_chars(txt, '&'); /* count how many control-chars there
                                 are in the string */

   new_txt = malloc(i * 5 + strlen(txt) + 1); /* no ansi-escape code is larger
                                                 than 5 bytes so a 5 * times
                                                 the '&' appears + strlen(txt)
                                                 + 1 character big buffer
                                                 should be enough */

if (!i || t->connected == CON_GET_NAME){ /* Returns same string if there is no
change needed JB*/
  strcpy(new_txt, txt); /* Fixes a warning and a strange bug with (I)MOTD's van
ishing JB */
  return new_txt;
}



   /* the parser.. Huge but fast */
if (clr(t->character, C_NRM)){   /* checks to see if character has neccisary co
lor on JB */
   for(i = 0; txt[i]; i++)
   {
      if(txt[i] == '&')
      {
         i++;


 switch(txt[i])

 {

    case '0' :                         /* Removed the color check code's here t
o */

      strcpy(new_txt + j, KNRM);       /* speed up the exchange JB */

      j += 4;
              break;
            case '1' :

      strcpy(new_txt + j, KRED);
              j += 5;
              break;
            case '2' :

      strcpy(new_txt + j, KGRN);
              j += 5;
              break;

    case '3' :

      strcpy(new_txt + j, KYEL);

      j += 5;

      break;

    case '4' :

      strcpy(new_txt + j, KBLU);

      j += 5;

      break;
            case '5' :
              strcpy(new_txt + j, KMAG);
              j += 5;
              break;
            case '6' :
              strcpy(new_txt + j, KCYN);
              j += 5;
              break;
            case '7' :
              strcpy(new_txt + j, KWHT);
              j += 5;
              break;
            case '8' : /* both cases did same thing JB */
            case 'b' :
              strcpy(new_txt + j, KBLD);
              j += 4;
              break;
            case '9' :
              strcpy(new_txt + j, KBLK);
              j += 5;
              break;
            case 'u' :
              strcpy(new_txt + j, KUND);
              j += 4;
              break;
            case 'd' :
              strcpy(new_txt + j, KDAR);
              j += 4;
              break;
            case 'R' :
              strcpy(new_txt + j, KBRED);
              j += 5;
              break;
            case 'G' :
              strcpy(new_txt + j, KBGRN);
              j += 5;
              break;
            case 'Y' :
              strcpy(new_txt + j, KBYEL);
              j += 5;
              break;
            case 'B' :
              strcpy(new_txt + j, KBBLU);
              j += 5;
              break;
            case 'M' :
              strcpy(new_txt + j, KBMAG);
              j += 5;
              break;
            case 'C' :
              strcpy(new_txt + j, KBCYN);
              j += 5;
              break;
            case 'W' :
              strcpy(new_txt + j, KBWHT);
              j += 5;
              break;
            case 'S' :
              strcpy(new_txt + j, KBBLK);
              j += 5;
              break;
            case '&' : new_txt[j] = txt[i]; j++; break;

            default:  break;
         }

      }
      else
      {
         new_txt[j] = txt[i]; j++;
      }
   }

} else {                       /* Quick little function in case they don't have
 color */
   for(i = 0; txt[i]; i++)     /* Only transfers the & character JB */
     if(txt[i] == '&'){
        i++;

if (txt[i] == '&'){

   new_txt[j] = txt[i];

   j++;
        }
     } else {
        new_txt[j] = txt[i]; j++;
     }
}



   new_txt[j] = '\0'; /* terminate the string */
   toret = strdup(new_txt); /* create a new string with no eventual memoryloss
*/
   free(new_txt); /* free the old buffer */

   return toret; /* the colorized buffer */
}

 * Change the function write_to_output(const char *txt, struct descriptor_data
*t)
   to this:

void write_to_output(const char *txt, struct descriptor_data *t)
{
  int size;
  char *new_txt;

  new_txt = parse_color(txt, t);

  size = strlen(new_txt);

  /* if we're in the overflow state already, ignore this new output */
  if (t->bufptr < 0)
    return;

  /* if we have enough space, just write to buffer and that's it! */
  if (t->bufspace >= size) {
    strcpy(t->output + t->bufptr, new_txt);
    t->bufspace -= size;
    t->bufptr += size;
    free(new_txt);
    return;
  }
  /*
   * If we're already using the large buffer, or if even the large buffer
   * is too small to handle this new text, chuck the text and switch to the
   * overflow state.
   */
  if (t->large_outbuf || ((size + strlen(t->output)) > LARGE_BUFSIZE)) {
    t->bufptr = -1;
    buf_overflows++;
    free(new_txt);
    return;
  }
  buf_switches++;

  /* if the pool has a buffer in it, grab it */
  if (bufpool != NULL) {
    t->large_outbuf = bufpool;
    bufpool = bufpool->next;
  } else {                      /* else create a new one */
    CREATE(t->large_outbuf, struct txt_block, 1);
    CREATE(t->large_outbuf->text, char, LARGE_BUFSIZE);
    buf_largecount++;
  }

  strcpy(t->large_outbuf->text, t->output);     /* copy to big buffer */
  t->output = t->large_outbuf->text;    /* make big buffer primary */
  strcat(t->output, new_txt);   /* now add new text */

  /* calculate how much space is left in the buffer */
  t->bufspace = LARGE_BUFSIZE - 1 - strlen(t->output);

  /* set the pointer for the next write */
  t->bufptr = strlen(t->output);

  free(new_txt);
}

 * My screen.h

/* ************************************************************************
*   File: screen.h                                      Part of CircleMUD *
*  Usage: header file with ANSI color codes for online color              *
*                                                                         *
*  All rights reserved.  See license.doc for complete information.        *
*                                                                         *
*  Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
*  CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991.               *
************************************************************************ */

#define KNRM  "\033[0m"
#define KRED  "\033[31m"
#define KGRN  "\033[32m"
#define KYEL  "\033[33m"
#define KBLU  "\033[34m"
#define KMAG  "\033[35m"
#define KCYN  "\033[36m"
#define KWHT  "\033[37m"
#define KBLK  "\033[30m"
#define KBLD  "\033[1m"
#define KBLN  "\033[5m"
#define KNUL  ""
#define KCLR  "\033[2J"
#define KUND  "\033[4m"
#define KDAR  "\033[2m"

#define KBRED  "\033[41m"
#define KBGRN  "\033[42m"
#define KBYEL  "\033[43m"
#define KBBLU  "\033[44m"
#define KBMAG  "\033[45m"
#define KBCYN  "\033[46m"
#define KBWHT  "\033[47m"
#define KBBLK  "\033[40m"

/* conditional color.  pass it a pointer to a char_data and a color level. */
#define C_OFF   0
#define C_SPR   1
#define C_NRM   2
#define C_CMP   3
#define _clrlevel(ch) ((PRF_FLAGGED((ch), PRF_COLOR_1) ? 1 : 0) + \
                       (PRF_FLAGGED((ch), PRF_COLOR_2) ? 2 : 0))
#define clr(ch,lvl) (_clrlevel(ch) >= (lvl))
#define CCNRM(ch,lvl)  (clr((ch),(lvl))?KNRM:KNUL)
#define CCBLK(ch,lvl)  (clr((ch),(lvl))?KBLK:KNUL)
#define CCRED(ch,lvl)  (clr((ch),(lvl))?KRED:KNUL)
#define CCGRN(ch,lvl)  (clr((ch),(lvl))?KGRN:KNUL)
#define CCYEL(ch,lvl)  (clr((ch),(lvl))?KYEL:KNUL)
#define CCBLU(ch,lvl)  (clr((ch),(lvl))?KBLU:KNUL)
#define CCMAG(ch,lvl)  (clr((ch),(lvl))?KMAG:KNUL)
#define CCCYN(ch,lvl)  (clr((ch),(lvl))?KCYN:KNUL)
#define CCWHT(ch,lvl)  (clr((ch),(lvl))?KWHT:KNUL)
#define CCBLD(ch,lvl)  (clr((ch),(lvl))?KBLD:KNUL)
#define CCBLN(ch,lvl)  (clr((ch),(lvl))?KBLN:KNUL)
#define CCCLR(ch,lvl)  (clr((ch),(lvl))?KCLR:KNUL)
#define CCUND(ch,lvl)  (clr((ch),(lvl))?KUND:KNUL)
#define CCDAR(ch,lvl)  (clr((ch),(lvl))?KDAR:KNUL)

#define CCBBLK(ch,lvl)  (clr((ch),(lvl))?KBBLK:KNUL)
#define CCBRED(ch,lvl)  (clr((ch),(lvl))?KBRED:KNUL)
#define CCBGRN(ch,lvl)  (clr((ch),(lvl))?KBGRN:KNUL)
#define CCBYEL(ch,lvl)  (clr((ch),(lvl))?KBYEL:KNUL)
#define CCBBLU(ch,lvl)  (clr((ch),(lvl))?KBBLU:KNUL)
#define CCBMAG(ch,lvl)  (clr((ch),(lvl))?KBMAG:KNUL)
#define CCBCYN(ch,lvl)  (clr((ch),(lvl))?KBCYN:KNUL)
#define CCBWHT(ch,lvl)  (clr((ch),(lvl))?KBWHT:KNUL)

#define COLOR_LEV(ch) (_clrlevel(ch))

#define QNRM CCNRM(ch,C_SPR)
#define QBLK CCBLK(ch,C_SPR)
#define QRED CCRED(ch,C_SPR)
#define QGRN CCGRN(ch,C_SPR)
#define QYEL CCYEL(ch,C_SPR)
#define QBLU CCBLU(ch,C_SPR)
#define QMAG CCMAG(ch,C_SPR)
#define QCYN CCCYN(ch,C_SPR)
#define QWHT CCWHT(ch,C_SPR)
#define QBLD CCBLD(ch,C_SPR)
#define QBLN CCBLN(ch,C_SPR)
#define QCLR CCBLN(ch,C_SPR)
#define QUND CCUND(ch,C_SPR)
#define QDAR CCDAR(ch,C_SPR)
#define QBBLK CCBBLK(ch,C_SPR)
#define QBRED CCBRED(ch,C_SPR)
#define QBGRN CCBGRN(ch,C_SPR)
#define QBYEL CCBYEL(ch,C_SPR)
#define QBBLU CCBBLU(ch,C_SPR)
#define QBMAG CCBMAG(ch,C_SPR)
#define QBCYN CCBCYN(ch,C_SPR)
#define QBWHT CCBWHT(ch,C_SPR)

 * Compile and you're done.
 * How to use it + color-codes

How to use it:

Wherever whenever you feel like adding some color (for those who have
colorlevel >= NORMAL) you just do something like this:

&1Red&0 &4Blue&0 and so on.. Works in room_descs, gossips, tells, you name it.

Here's a list of the color-codes:

0 Normal attributes
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
8 bold
9 black

u underline
d dark
b bright
R red bground
G green brgound
B Blue bground
Y yellow brgound
M magenta bground
C Cyan bground
W white bground
S black bground (S as in Svart (swedish))
& the character &

Anything other than these characters, will produce nothing.


<< Colour For Circle (More) [by Jorgen Sigvardsson] | Reply | View as text | Threaded | Counting Objects [by John Evans] >>

 


Related Links
  CircleMUD
download
Related Articles
More by greerga
 
 

CircleMUD Snippets
 
Note: Not all of these snippets will work perfectly with your version of code, so be prepared to fix one or two bugs that may arise, and please let me know what you needed to do to fix it. Sending a corrected version is always welcome.
Finally, if you wish to use any of the snippets from this page, you are more than welcome, just mention the authors in your credits. If you wish to release any of these snippets to the public on another site, contact me FIRST.