colour.c
colour.c · 4.4 KB
From the archive coloursnip.ZIP
Download the archive « Back to the archive contents
/*Original code, written by Davion. */
#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "merc.h"
#define PC_COL 16
int get_colour args ( (const char *name ) );
int get_field args ( (const char *name ) );
char * get_pc_code args ( ( CHAR_DATA *ch, char *symbol ) );
int symbol_lookup args ( ( const char symbol ) );
const struct colour_type colour_table[] =
{
{ "White", "x", "\e[0m" },
{ "DRed", "r", "\e[0;31m" },
{ "DGreen", "g", "\e[0;32m" },
{ "DYellow", "y", "\e[0;33m" },
{ "DBlue", "b", "\e[0;34m" },
{ "DPurple", "m", "\e[0;35m" },
{ "DCyan", "c", "\e[0;36m" },
{ "Dwhite", "w", "\e[0;37m" },
{ "DGrey", "D", "\e[1;30m" },
{ "BRed", "R", "\e[1;31m" },
{ "BGreen", "G", "\e[1;32m" },
{ "BYellow", "Y", "\e[1;33m" },
{ "BBlue", "B", "\e[1;34m" },
{ "BPink", "M", "\e[1;35m" },
{ "BCyan", "C", "\e[1;36m" },
{ "BWhite", "W", "\e[1;37m" },
{ "Enemy_attack", "a", NULL },
{ "Self_attack", "A", NULL },
{ "Other_attack", "e", NULL },
{ "Damage_colour", "E", NULL },
{ "Score_pre", "f", NULL },
{ "Score_stat", "F", NULL },
{ "Room_descr", "h", NULL },
{ "Room_name", "H", NULL },
{ "Room_objs", "i", NULL },
{ "Room_people", "I", NULL },
{ "Say_text", "j", NULL },
{ "Say_name", "J", NULL },
{ "Yell_text", "k", NULL },
{ "Yell_name", "K", NULL },
{ NULL, NULL, NULL }
};
char *colour_string( const char *txt, CHAR_DATA *ch )
{
const char *string = txt;
static char buf[MSL*4];
char symbol;
char *coloured, *code;
int i;
bool found;
buf[0] = '\0';
coloured = buf;
for(;*string != '\0' ; string++ )
{
found = FALSE;
if(*string == '{' )
{ string++;
symbol = *string;
if( ( i = symbol_lookup(symbol) ) == -1 )
i = 0;
if( i >= 16 )
code = get_pc_code(ch, colour_table[i].symbol );
else
code = colour_table[i].code;
while(*code != '\0' )
{
*coloured++ = *code++;
*coloured = '\0';
}
found = TRUE;
}
if(!found)
{
*coloured++ = *string;
*coloured = '\0';
}
}
*coloured = '\0';
return buf;
}
char * get_colour_name(char *code )
{
int i;
for ( i = 0; colour_table[i].name != NULL ; i++ )
{
if(!str_cmp(code, colour_table[i].code ) )
return colour_table[i].name;
}
return "White";
}
int symbol_lookup( const char symbol )
{
int i;
for ( i = 0; colour_table[i].name != NULL ; i++ )
{
if( symbol == *colour_table[i].symbol )
return i;
}
return -1;
}
char * get_pc_code ( CHAR_DATA *ch, char *symbol )
{
int i;
if(!ch || IS_NPC(ch))
return colour_table[0].code;
if( ( i = ( symbol_lookup(symbol[0]) ) - PC_COL ) <= -1 )
return colour_table[0].code;
return ch->pcdata->colour[i];
}
int get_colour(const char *name )
{
int i;
for( i = 0; i < PC_COL ; i++ )
if(!str_cmp(name, colour_table[i].name ) )
return i;
return -1;
}
int get_field(const char *name )
{
int i;
for( i = PC_COL; colour_table[i].name != NULL ; i++ )
if(!str_cmp(name, colour_table[i].name ) )
return i;
return -1;
}
void do_colour(CHAR_DATA *ch, char *argument )
{
char arg[MSL], arg2[MSL];
int field, colour, i;
if(IS_NPC(ch) )
{ send_to_char("Mobs don't need to set colour.\n\r",ch);
return;
}
argument = one_argument(argument, arg );
argument = one_argument(argument, arg2 );
if( ( field = get_field(arg) ) == -1 )
{ send_to_char("Thats an invalid Field. Valad fields are:\n\r",ch);
for(i = PC_COL; colour_table[i].name != NULL ; i++ )
printf_to_char(ch, "%s ", colour_table[i].name );
return;
}
if( ( colour = get_colour(arg2) ) == -1 )
{ send_to_char("That isn't a valid colour. Valid colours are:\n\r",ch);
for(i = 0; i < PC_COL ; i++ )
printf_to_char(ch, "%s ", colour_table[i].name );
return;
}
printf_to_char(ch, "Field %s, changed to {%c%s{x.\n\r", colour_table[field].name, colour_table[colour].symbol[0], colour_table[colour].name );
ch->pcdata->colour[field-PC_COL] = colour_table[colour].code;
}