/*
 * Silvercode - cname.c v1.3
 * Coloured names (done reasonably correctly - ahem)
 * ---------------------------------------------------------------------------
 *
 * Note: "colour" is spelt with a 'u'. Please don't bastardise the English
 *       language more than it already is :o)
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "include/config.h"
#include "include/player.h"
#include "include/proto.h"

void cname(player *p, char *str)
{
  int pos = 0;

  if (!*str)
  {
    tell_player(p, " Format: cname <colour codes>\n"
                   "  (use \"cname clear\" to clear all colours)\n");
    if (p->cname[0])
      TELLPLAYER(p, "  Your current colour code is: %s\n", p->cname);
    return;
  }

  if (!strcasecmp(str, "clear"))
  {
    p->cname[0] = 0;
    tell_player(p, " All colours removed from your name.\n");
    return;
  }

  if (strlen(str) < strlen(p->name))
  {
    TELLPLAYER(p, " You have supplied only %d colour codes and you need %d.\n", strlen(str), strlen(p->name));
    return;
  }
  else if (strlen(str) > strlen(p->name))
  {
    TELLPLAYER(p, " You have supplied %d colour codes and you only need %d.\n", strlen(str), strlen(p->name));
    return;
  }

  /* check validity */

  for (pos = 0 ; pos < strlen(str) ; pos++)
    if (!valid_char_col(str[pos]))
    {
      TELLPLAYER(p, " '%c' is not a valid colour character. ", str[pos]);
      if (p->cname[0])
        tell_player(p, "Coloured name not changed.\n");
      else
        tell_player(p, "Coloured name not set.\n");
      return;
    }

  strncpy(p->cname, str, MAX_NAME);
  p->cname[MAX_NAME+1] = 0;  

  TELLPLAYER(p, " Colour code set to: %s\n"
                " People will now view your name as: %s\n", p->cname, get_cname(p, p));
}

/* show someones cname
    p = persons name, v = the viewing person

    By giving the pointer to the viewing person we can allow them to
    muffle cnames - a feature I love! :o)

 */

char *get_cname(player *p, player *v)
{
  static char result[MAX_NAME*4];  /* overkill i know */
  int pos = 0, rpos = 0;
  char last = 'N';

  /* if no viewer defined, show non-coloured */

  if (!v)
    return p->name;

  /* don't show them cnames if they don't want it */

  if (v->misc_flags & NO_CNAME)
    return p->name;

 /* if they don't have a cname then return */

  if (!p->cname[0])
    return p->name;

  for (pos = 0 ; pos < strlen(p->name) ; pos++)
  {
    if (last != p->cname[pos])
    {
      result[rpos++] = '^';
      result[rpos++] = p->cname[pos];
      last = p->cname[pos];
    }
    result[rpos++] = p->name[pos];
  }

  /* terminate colour codes - if necessary */

  if (p->cname[strlen(p->cname)] != 'N')
  {
    result[rpos++] = '^';
    result[rpos++] = 'N';
  }

  /* and end */

  result[rpos] = 0;
  return result;
} 
  
void toggle_cname(player *p)
{
  p->misc_flags ^= NO_CNAME;

  if (p->misc_flags & NO_CNAME)
    tell_player(p, " You are sensibly ignoring coloured names.\n");
  else
    tell_player(p, " You are viewing coloured names.\n");
}  

void cname_version()
{
  sprintf(stack, " -=*> Coloured name code v1.3 (by Silver) installed.\n");
  stack = strchr(stack, 0);
}