~case/dbns

ref: 0f9985edf4d2b9e98aaac388d30fd4ab8c4722cf dbns/src/colorize.c -rw-r--r-- 6.4 KiB
0f9985edcase rm extern 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/****************************************************************************
 * [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame      |   \\._.//   *
 * -----------------------------------------------------------|   (0...0)   *
 * SMAUG 1.4 (C) 1994, 1995, 1996, 1998  by Derek Snider      |   ).:.(   *
 * -----------------------------------------------------------|    {o o}    *
 * SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus,      |   / ' ' \   *
 * Scryn, Rennard, Swordbearer, Gorog, Grishnakh, Nivek,      |~'~.VxvxV.~'~*
 * Tricops and Fireblade                                      |             *
 * ------------------------------------------------------------------------ *
 * Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael        *
 * Chastain, Michael Quan, and Mitchell Tse.                                *
 * Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,          *
 * Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.     *
 * ------------------------------------------------------------------------ *
 *                          Colorize module by Altrag                       *
 ****************************************************************************/


#include <sys/types.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "mud.h"

struct at_color_type at_color_table[AT_MAXCOLOR];

void load_colors(void)
{
	extern char strArea[MAX_INPUT_LENGTH];
	extern FILE *fpArea;
	char buf[20];
	int at;
  
	for (at = 0; at < AT_MAXCOLOR; ++at)
	{
		sprintf(buf, "%d", at);
		at_color_table[at].name = str_dup(buf);
		at_color_table[at].def_color = AT_GREY;
	}
	strcpy(strArea, COLOR_FILE);
	if (!(fpArea = fopen(COLOR_FILE, "r")))
	{
		bug("Load_colors: can't open color file -- bland city time!");
		return;
	}
	for (;;)
	{
		at = (feof(fpArea) ? -1 : fread_number(fpArea));
		if (at < 0)
			break;
		if (at < AT_COLORBASE || at >= AT_TOPCOLOR)
		{
			bug("Load_colors: color %d invalid.", at);
			fread_to_eol(fpArea);
			continue;
		}
		at -= AT_COLORBASE;
		DISPOSE(at_color_table[at].name);
		at_color_table[at].name = fread_string_nohash(fpArea);
		at_color_table[at].def_color = fread_number(fpArea);
	}
	fclose(fpArea);
	fpArea = NULL;
}

void save_colors(void)
{
	FILE *fp;
	int at;
  
	if (!(fp = fopen(COLOR_FILE, "w")))
	{
		bug("Save_colors: can't open color file.");
		return;
	}
	for (at = 0; at < AT_MAXCOLOR; ++at)
		fprintf(fp, "%d %s~ %d\n", at+AT_COLORBASE, at_color_table[at].name,
			at_color_table[at].def_color);
	fprintf(fp, "-1\n");
	fclose(fp);
}

void do_colorize(CHAR_DATA *ch, char *argument)
{
#define MAX_COLOR 16
	static const char colors[MAX_COLOR] = "xrgObpcwzRGYBPCW";
	static const char *color_names[MAX_COLOR] =
		{ "black", "blood", "dgreen", "orange", "dblue", "purple", "cyan",
		  "grey", "dgrey", "red", "green", "yellow", "blue", "pink", "lblue",
		  "white" };
	char arg[MAX_INPUT_LENGTH], *a;
	int color;
	int at;
	bool blink = false;
	CHAR_DATA *victim;
	bool def = false;
	int level = get_trust(ch);
  
	argument = one_argument(argument, arg);
	if (level >= LEVEL_SUB_IMPLEM &&
	    (!str_cmp(arg, "def") || !str_cmp(arg, "default")))
	{
		def = true;
		argument = one_argument(argument, arg);
		victim = NULL;
	}
	else if (level >= LEVEL_SUB_IMPLEM &&
		 (victim = get_char_room(ch, arg)) != NULL)
	{
		if (level < get_trust(victim))
		{
			send_to_char("They might not like that.\n\r", ch);
			return;
		}
		argument = one_argument(argument, arg);
	}
	else
		victim = ch;
	if (!def && IS_NPC(victim))
	{
		send_to_char("Not on NPCs.\n\r", ch);
		return;
	}
	if (!*arg)
	{
		for (at = 0; at < AT_MAXCOLOR; ++at)
		{
			if (def || victim->pcdata->colorize[at] == -1)
				color = at_color_table[at].def_color;
			else
				color = victim->pcdata->colorize[at];
			if (color >= AT_BLINK)
			{
				blink = true;
				color -= AT_BLINK;
			}
			else
				blink = false;
			pager_printf_color(ch, "%-8s: %s%c%-15s&D",
					   at_color_table[at].name,
					   (blink ? "+}" : " &"), colors[color],
					   color_names[color]);
			if (at%3 == 2)
				send_to_pager("\n\r", ch);
		}
		if (at%3 != 0)
			send_to_pager("\n\r", ch);
		send_to_pager("\n\rValid colors are:\n\r", ch);
		for (color = 0; color < MAX_COLOR; ++color)
		{
			if (color)
				set_pager_color(color, ch);
			else
				set_pager_color(AT_PLAIN, ch);
			pager_printf(ch, "%-9s", color_names[color]);
			if (color%8 == 7)
				send_to_pager("\n\r", ch);
		}
		if (color%8 != 0)
			send_to_pager("\n\r", ch);
		set_pager_color(AT_GREY + AT_BLINK, ch);
		send_to_pager("\n\rBlinking ", ch);
		set_pager_color(AT_GREY, ch);
		send_to_pager("can be specified by prefixing the color name with a +."
			      "\n\r", ch);
		set_pager_color(AT_PLAIN, ch);
		return;
	}
	for (at = 0; at < AT_MAXCOLOR; ++at)
		if (!str_prefix(arg, at_color_table[at].name))
			break;
	if (at == AT_MAXCOLOR)
	{
		send_to_char("Not a settable color.\n\r", ch);
		return;
	}
	argument = one_argument(argument, arg);
	if (!*arg)
	{
		send_to_char("Set it to what color?\n\r", ch);
		return;
	}
	if (!str_cmp(arg, "def") || !str_cmp(arg, "default"))
	{
		if (def)
			send_to_char("That *IS* the default!\n\r", ch);
		else
		{
			victim->pcdata->colorize[at] = -1;
			send_to_char("Color set to default.\n\r", ch);
		}
		return;
	}
	if (*arg == '+')
	{
		a = arg+1;
		if (!*a)
		{
			if (def)
			{
				if (at_color_table[at].def_color >= AT_BLINK)
				{
					at_color_table[at].def_color -= AT_BLINK;
					send_to_char("Default color will no longer blink.\n\r", ch);
				}
				else
				{
					at_color_table[at].def_color += AT_BLINK;
					send_to_char("Default color will now blink.\n\r", ch);
				}
				save_colors();
			}
			else
			{
				if (victim->pcdata->colorize[at] == -1)
					victim->pcdata->colorize[at] = at_color_table[at].def_color;
				if (victim->pcdata->colorize[at] >= AT_BLINK)
				{
					victim->pcdata->colorize[at] -= AT_BLINK;
					send_to_char("Color will no longer blink.\n\r", ch);
				}
				else
				{
					victim->pcdata->colorize[at] += AT_BLINK;
					send_to_char("Color will now blink.\n\r", ch);
				}
			}
			return;
		}
		blink = true;
	}
	else
		a = arg;
	for (color = 0; color < MAX_COLOR; ++color)
		if (!str_prefix(a, color_names[color]))
			break;
	if (color == MAX_COLOR)
	{
		send_to_char("Invalid color name.\n\r", ch);
		return;
	}
	if (blink)
		color += AT_BLINK;
	if (def)
	{
		at_color_table[at].def_color = color;
		send_to_char("Default color set.\n\r", ch);
		save_colors();
	}
	else
	{
		victim->pcdata->colorize[at] = color;
		send_to_char("Color set.\n\r", ch);
	}
}