238 lines (210 with data), 7.4 kB
/* Dodinas's Colour Server
Shattered World Colour Codes
----------------------------
{r = dark red {R = bright red
{g = dark green {G = bright green
{y = dark yellow {Y = bright yellow
{b = dark blue {B = bright blue
{p = dark purple {P = bright purple
{c = dark cyan {C = bright cyan
{w = grey {W = white
{k = black } = reset to standard
{!r = inverse red {!p = inverse purple
{!g = inverse green {!c = inverse cyan
{!y = inverse yellow{!w = inverse grey
{!b = inverse blue
Specials:
{#l = coloured line
{#h = heading colour
{#s = syntax colour (eg. for hilighting: type 'look')
{#c = colour another standard colour
*/
/* Trax - added result function 010613 */
/* Dodinas - added csay function (coloured "say") 010616 */
#include <ansi.h>
#define STRINGS "RO/lib/strings"
array colours = [ "r",ESC+"[0m"+ESC+"[31;2m","g",ESC+"[0m"+ESC+"[32;2m",
"y",ESC+"[0m"+ESC+"[33;2m","b",ESC+"[0m"+ESC+"[34;2m",
"p",ESC+"[0m"+ESC+"[35;2m","c",ESC+"[0m"+ESC+"[36;2m",
"w",ESC+"[0m"+ESC+"[37;2m","k",ESC+"[0m"+ESC+"[30;1m",
"R",ESC+"[31;1m","G",ESC+"[32;1m","Y",ESC+"[33;1m",
"B",ESC+"[34;1m","P",ESC+"[35;1m","C",ESC+"[36;1m",
"W",ESC+"[39;1m","!r",ESC+"[41;1m","!g",ESC+"[42;1m",
"!y",ESC+"[43;1m","!b",ESC+"[44;1m","!p",ESC+"[45;1m",
"!c",ESC+"[46;1m","!w",ESC+"[47;1m",
"#l",ESC+"[32;1m"
+"----------------------------------------------------------------------------"
+ESC+"[0m","#s",ESC+"[33;1m","#c",ESC+"[32;1m" ];
/* Added by Trax 010613 */
result((array|int|string) str, pob) {
if (!pob) pob=this_player();
if (pob->query_store("hilite_on"))
return colour(str, pob);
else
return stripcolour(str, pob);
}
cwrite((array|int|string) str) {
write(result(str, this_player()));
return 1;
}
ctell_object(pob, (array|int|string) str) {
tell_object(pob, result(str, pob));
return 1;
}
/* Talk - This function is designed for use with npcs. The function takes
the string and broadcasts it to players in the room of the npc
in their "say" hilite colour, with any text in *'s replaced with
hilighting. */
talk(str, npc) {
if (!str) return;
if (!npc) npc = caller();
str = str + "\n";
array objs = contents(environment(npc));
int i,x,y,a,b;
string tmp,norm,hili;
for (i = 0; i < sizeof(objs); i++) {
if (!(objs[i] == 0 || objs[i] == npc)) {
if (!objs[i]->query_store("hilite_on"))
tell_object(objs[i], str);
else {
x = objs[i]->query_store("hilite_say");
if (x == 30) y = 38;
else if ((x >= 31) && (x <= 37)) y = x + 100;
else if (x == 38) y = 39;
else if (x == 39) y = 38;
else if ((x >= 41) && (x <= 46)) y = x - 10;
else if (x == 47) y == 39;
else if ((x >= 131) && (x <= 136)) y = x - 100;
else if (x == 137) y = 39;
else y = 39;
tmp = str;
if (x < 100) {
norm = ESC+"["+x+";1m";
} else {
x = x-100;
norm = ESC+"[0m"+ESC+"["+x+";2m";
}
if (y < 100) {
hili = ESC+"["+y+";1m";
} else {
y = y-100;
hili = ESC+"[0m"+ESC+"["+y+";2m";
}
b = 0;
tmp = norm + tmp;
for (a = 0; a < sizeof(tmp); a++) {
if (tmp[a] == '*') {
if (b == 0) {
tmp = tmp[..a-1] + hili + tmp[a+1..];
b = 1;
continue;
} else {
tmp = tmp[..a-1] + norm + tmp[a+1..];
b = 0;
continue;
}
}
}
tmp = tmp + ESC + "[0m";
tell_object(objs[i], tmp);
}
}
}
tell_object(environment(npc),str);
return 1;
}
/* This function takes a string, replaces the colour codes with
ansi colour characters, and returns the coloured string. */
colour((array|int|string) str, pob) {
if (arrayp(str)) {
int i;
for (i=0;i < sizeof(str); i++) {
str[i] = convertcolour(str[i], pob);
}
}
if (stringp(str)) str = convertcolour(str, pob);
return str;
}
stripcolour((array|int|string) str, pob) {
if (arrayp(str)) {
int i;
for (i=0;i < sizeof(str); i++) {
str[i] = nocolour(str[i], pob);
}
}
if (stringp(str)) str = nocolour(str, pob);
return str;
}
convertcolour(str, pob) {
/* Heading Colour is Dark Green */
int pos, x, i, j;
if (!pob) pob = this_player();
x = pob->query_store("hilite_things");
if (x > 100)
str = STRINGS->string_replace(str,"{#h",(ESC+"[0m"+ESC+"[32;2m"));
else
str = STRINGS->string_replace(str,"{#h",(ESC+"[0m"+ESC+"[32;1m"));
for (i = 0; i < sizeof(str); i++) {
if (str[i] == '}') {
str = str[..i-1] + ESC + "[0m" + str[i+1..];
continue;
}
if (str[i] == '{') {
if ((str[i+1] == '{') || (str[i+1] == '}')) {
str = str[..i-1] + str[i+1..];
continue;
}
for (j = 0; j < sizeof(colours); j = j+2) {
if (str[i+1..(i+sizeof(colours[j]))] != colours[j][0..(sizeof(colours[j])-1)]) continue;
str = str[..i-1] + colours[j+1] + str[i+1+sizeof(colours[j])..];
break;
}
}
}
str += ESC+"[0m";
return str;
}
/* Strips the string of colour symbols */
nocolour(str, pob) {
/* Heading Colour is Dark Green */
int pos, i, j;
str = STRINGS->string_replace(str,"{#h","");
for (i = 0; i < sizeof(str); i++) {
if (str[i] == '}') {
str = str[..i-1] + str[i+1..];
continue;
}
if (str[i] == '{') {
for (j = 0; j < sizeof(colours); j = j+2) {
if (str[i+1..(i+sizeof(colours[j]))] != colours[j][0..(sizeof(colours[j])-1)]) continue;
str = str[..i-1] + str[i+1+sizeof(colours[j])..];
break;
}
}
}
str += ESC+"[0m";
return str;
}
/* say() stripping colour for those without it */
csay(string str,(object|array|int) avoid, origin) {
object env;
array whoto;
int i;
if (objectp(avoid)) avoid = [ avoid ];
if (!origin) {
origin = this_player();
}
if (!avoid) {
avoid = [ origin ];
} else {
avoid += [ origin ];
}
/* do the tell_object stuff */
env = environment(origin);
whoto = contents (origin);
if (env) whoto += contents(env);
for (i = 0; i < sizeof(whoto); i++ ) {
if (living(whoto[i]) && (index (avoid, whoto[i]) < 0))
tell_object(whoto[i], result(str, whoto[i]));
}
}