1 // =========================================================
2 // Generic program common command code, reworked by Samual
3 // Last updated: December 28th, 2011
4 // =========================================================
6 string GetProgamCommandPrefix()
19 #define NUM_MARKUPS 41
21 string markup_from[NUM_MARKUPS];
22 string markup_to[NUM_MARKUPS];
23 void GameCommand_MarkupInit()
30 markup_from[i] = "&alien"; markup_to[i] = "\x12"; ++i;
31 markup_from[i] = "&:-)"; markup_to[i] = "\x13"; ++i;
32 markup_from[i] = "&:-("; markup_to[i] = "\x14"; ++i;
33 markup_from[i] = "&x-P"; markup_to[i] = "\x15"; ++i;
34 markup_from[i] = "&:-/"; markup_to[i] = "\x16"; ++i;
35 markup_from[i] = "&:-D"; markup_to[i] = "\x17"; ++i;
36 markup_from[i] = "&<<"; markup_to[i] = "\x18"; ++i;
37 markup_from[i] = "&>>"; markup_to[i] = "\x19"; ++i;
38 markup_from[i] = "&dot"; markup_to[i] = "\x1a"; ++i;
39 markup_from[i] = "&^_"; markup_to[i] = "\x1b"; ++i;
40 markup_from[i] = "&ysplat"; markup_to[i] = "\x1c"; ++i;
41 markup_from[i] = "&-]"; markup_to[i] = "\x1d"; ++i;
42 markup_from[i] = "&--"; markup_to[i] = "\x1e"; ++i;
43 markup_from[i] = "&[-"; markup_to[i] = "\x1f"; ++i;
44 markup_from[i] = "&s<"; markup_to[i] = "\x2c"; ++i;
45 markup_from[i] = "&s>"; markup_to[i] = "\x2e"; ++i;
46 markup_from[i] = "&<-"; markup_to[i] = "\x7f"; ++i;
47 markup_from[i] = "&[="; markup_to[i] = "\x80"; ++i;
48 markup_from[i] = "&=="; markup_to[i] = "\x81"; ++i;
49 markup_from[i] = "&=]"; markup_to[i] = "\x82"; ++i;
50 markup_from[i] = "&r!"; markup_to[i] = "\x84"; ++i;
51 markup_from[i] = "&|o|"; markup_to[i] = "\x85"; ++i;
52 markup_from[i] = "&|u|"; markup_to[i] = "\x86"; ++i;
53 markup_from[i] = "&|i|"; markup_to[i] = "\x87"; ++i;
54 markup_from[i] = "&|c|"; markup_to[i] = "\x88"; ++i;
55 markup_from[i] = "&[c]"; markup_to[i] = "\x89"; ++i;
56 markup_from[i] = "&[n]"; markup_to[i] = "\x8a"; ++i;
57 markup_from[i] = "&[]"; markup_to[i] = "\x8b"; ++i;
58 markup_from[i] = "&r?"; markup_to[i] = "\x8c"; ++i;
59 markup_from[i] = "&|>"; markup_to[i] = "\x8d"; ++i;
60 markup_from[i] = "&splat0"; markup_to[i] = "\x8e"; ++i;
61 markup_from[i] = "&splat1"; markup_to[i] = "\x8f"; ++i;
62 markup_from[i] = "&[["; markup_to[i] = "\x90"; ++i;
63 markup_from[i] = "&]]"; markup_to[i] = "\x91"; ++i;
64 markup_from[i] = "&splat2"; markup_to[i] = "\x9a"; ++i;
65 markup_from[i] = "&)("; markup_to[i] = "\x9b"; ++i;
66 markup_from[i] = "&splat3"; markup_to[i] = "\x9c"; ++i;
67 markup_from[i] = "&(."; markup_to[i] = "\x9d"; ++i;
68 markup_from[i] = "&.."; markup_to[i] = "\x9e"; ++i;
69 markup_from[i] = "&.)"; markup_to[i] = "\x9f"; ++i;
70 markup_from[i] = "&<|"; markup_to[i] = "\xff"; ++i;
73 string GameCommand_Markup(string s2)
75 float red, ccase, i, j;
78 GameCommand_MarkupInit();
84 for(i = 0; i < strlen(s2); ++i)
86 for(j = 0; j < NUM_MARKUPS; ++j)
88 s3 = substring(s2, i, strlen(markup_from[j]));
89 if (s3 == markup_from[j])
91 s = strcat(s, markup_to[j]);
92 i += strlen(markup_from[j]) - 1;
99 if(substring(s2, i, 2) == "&&")
101 s = strcat(s, strconv(ccase, red, red, "&"));
104 else if(substring(s2, i, 2) == "&d")
110 else if(substring(s2, i, 2) == "&a")
116 else if(substring(s2, i, 2) == "&n")
123 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
130 void GenericCommand_addtolist(float request)
134 case CMD_REQUEST_COMMAND:
140 if(cvar_string(s) == "")
144 n = tokenizebyseparator(cvar_string(s), " ");
145 for(i = 0; i < n; ++i)
147 return; // already in list
148 cvar_set(s, strcat(s2, " ", cvar_string(s)));
155 case CMD_REQUEST_USAGE:
157 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " ")));
158 print(" No arguments required.\n");
164 /* use this when creating a new command, making sure to place it in alphabetical order... also,
165 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
166 void GenericCommand_(float request)
170 case CMD_REQUEST_COMMAND:
177 case CMD_REQUEST_USAGE:
179 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " ")));
180 print(" No arguments required.\n");
187 // ==================================
188 // Macro system for server commands
189 // ==================================
191 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
192 #define GENERIC_COMMANDS(request,arguments,command) \
193 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "") \
194 GENERIC_COMMAND("", GenericCommand_(request, arguments), "") \
197 void GenericCommand_macro_help()
199 #define GENERIC_COMMAND(name,function,description) \
200 { print(" ^2", name, "^7: ", description, "\n"); }
202 GENERIC_COMMANDS(0, 0, "")
203 #undef GENERIC_COMMAND
208 float GenericCommand_macro_command(float argc, string command)
210 #define GENERIC_COMMAND(name,function,description) \
211 { if(name == strtolower(argv(0))) { function; return TRUE; } }
213 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
214 #undef GENERIC_COMMAND
219 float GenericCommand_macro_usage(float argc)
221 #define GENERIC_COMMAND(name,function,description) \
222 { if(name == strtolower(argv(1))) { function; return TRUE; } }
224 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
225 #undef GENERIC_COMMAND
231 // ===========================================
232 // Main Common Function For Generic Commands
233 // ===========================================
234 // Commands spread out among all programs (menu, client, and server)
236 float GenericCommand(string command)
238 float argc = tokenize_console(command);
240 // Guide for working with argc arguments by example:
241 // argc: 1 - 2 - 3 - 4
242 // argv: 0 - 1 - 2 - 3
243 // cmd vote - master - login - password
245 if(argv(0) == "help")
247 print(" maplist add map\n");
248 print(" maplist remove map\n");
249 print(" maplist shuffle\n");
250 print(" maplist cleanup\n");
251 print(" maplist maplist\n");
252 print(" maplist lsmaps\n");
253 print(" maplist lsnewmaps\n");
254 print(" addtolist variable addedvalue\n");
255 print(" settemp cvar value\n");
256 print(" settemp_restore\n");
260 if(argv(0) == "maplist")
262 if(argv(1) == "add" && argc == 3)
264 if (!fexists(strcat("maps/", argv(2), ".bsp")))
266 print("maplist: ERROR: ", argv(2), " does not exist!\n");
269 if(cvar_string("g_maplist") == "")
270 cvar_set("g_maplist", argv(2));
272 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
275 else if(argv(1) == "remove" && argc == 3)
278 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
280 for(i = 0; i < n; ++i)
282 s2 = strcat(s2, " ", argv(i));
283 s2 = substring(s2, 1, strlen(s2) - 1);
284 cvar_set("g_maplist", s2);
287 else if(argv(1) == "shuffle" && argc == 2)
289 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
292 else if(argv(1) == "cleanup")
295 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
296 n = tokenizebyseparator(cvar_string("g_maplist"), " ");
298 for(i = 0; i < n; ++i)
299 if(MapInfo_CheckMap(argv(i)))
300 s2 = strcat(s2, " ", argv(i));
301 s2 = substring(s2, 1, strlen(s2) - 1);
302 cvar_set("g_maplist", s2);
306 else if(argc >= 3 && argv(0) == "red")
308 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
309 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
312 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
315 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
317 n = floor(random() * 6 + 2);
320 for(i = 0; i < n; ++i)
322 s2 = strcat(s2, "AH");
326 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
332 s = strcat(s, " ", s2);
334 s = strcat(s2, " ", s);
336 s2 = substring(s, strlen(s) - 2, 2);
337 if(s2 == "AH" || s2 == "AY")
340 s = strcat(s, " ))");
343 s = substring(s, 0, strlen(s) - 1);
346 s = strconv(1, 0, 0, s);
348 localcmd(strcat(argv(1), " ", s));
352 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
354 // test case for terencehill's color codes
355 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
359 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
362 for(i = 0; i < n; ++i)
364 c = substring(s, i, 1);
371 if(substring(s, i+1, 1) == "^")
377 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
378 c = strcat(rgb_to_hexcolor(rgb), c);
383 localcmd(strcat(argv(1), " ", s2));
387 else if(argv(0) == "addtolist") {
392 if(cvar_string(s) == "")
395 n = tokenizebyseparator(cvar_string(s), " ");
396 for(i = 0; i < n; ++i)
398 return TRUE; // already in list
399 cvar_set(s, strcat(s2, " ", cvar_string(s)));
405 } else if(argv(0) == "cp") {
409 for(i = 2; i < argc; ++i)
410 s = strcat(s, " ", argv(i));
411 centerprint(unescape(s));
416 else if(argv(0) == "settemp") {
417 cvar_settemp(argv(1), argv(2));
420 else if(argv(0) == "settemp_restore") {
421 cvar_settemp_restore();