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 void GenericCommand_maplist(float request, float argc)
168 case CMD_REQUEST_COMMAND:
175 case "add": // appends new maps to the maplist
179 if (!fexists(strcat("maps/", argv(2), ".bsp")))
181 print("maplist: ERROR: ", argv(2), " does not exist!\n");
185 if(cvar_string("g_maplist") == "")
186 cvar_set("g_maplist", argv(2));
188 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
192 break; // go to usage
195 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
199 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
201 for(i = 0; i < argc; ++i)
202 if(argv(i) != argv(2))
203 tmp_string = strcat(tmp_string, " ", argv(i));
205 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
206 cvar_set("g_maplist", tmp_string);
210 break; // go to usage
213 case "shuffle": // randomly shuffle the maplist
215 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
219 case "cleanup": // scans maplist and only adds back the ones which are really usable
222 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
223 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
225 for(i = 0; i < argc; ++i)
226 if(MapInfo_CheckMap(argv(i)))
227 tmp_string = strcat(tmp_string, " ", argv(i));
229 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
230 cvar_set("g_maplist", tmp_string);
240 case CMD_REQUEST_USAGE:
242 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " maplist command [map]"))); // todo
243 print(" No arguments required.\n");
249 void GenericCommand_settemp(float request, float argc)
253 case CMD_REQUEST_COMMAND:
257 if(cvar_settemp(argv(1), argv(2)))
258 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
260 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
267 print("Incorrect parameters for ^2settemp^7\n");
268 case CMD_REQUEST_USAGE:
270 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " settemp \"cvar\" \"arguments\"\n"));
271 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
277 void GenericCommand_settemp_restore(float request, float argc)
281 case CMD_REQUEST_COMMAND:
283 float i = cvar_settemp_restore();
286 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
288 dprint("Nothing to restore.\n");
294 case CMD_REQUEST_USAGE:
296 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " settemp_restore\n"));
297 print(" No arguments required.\n");
303 /* use this when creating a new command, making sure to place it in alphabetical order... also,
304 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
305 void GenericCommand_(float request)
309 case CMD_REQUEST_COMMAND:
316 case CMD_REQUEST_USAGE:
318 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(caller), " ")));
319 print(" No arguments required.\n");
326 // ==================================
327 // Macro system for server commands
328 // ==================================
330 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
331 #define GENERIC_COMMANDS(request,arguments,command) \
332 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
333 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
334 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
335 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
336 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
339 void GenericCommand_macro_help()
341 #define GENERIC_COMMAND(name,function,description) \
342 { print(" ^2", name, "^7: ", description, "\n"); }
344 GENERIC_COMMANDS(0, 0, "")
345 #undef GENERIC_COMMAND
350 float GenericCommand_macro_command(float argc, string command)
352 #define GENERIC_COMMAND(name,function,description) \
353 { if(name == strtolower(argv(0))) { function; return TRUE; } }
355 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
356 #undef GENERIC_COMMAND
361 float GenericCommand_macro_usage(float argc)
363 #define GENERIC_COMMAND(name,function,description) \
364 { if(name == strtolower(argv(1))) { function; return TRUE; } }
366 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
367 #undef GENERIC_COMMAND
373 // ===========================================
374 // Main Common Function For Generic Commands
375 // ===========================================
376 // Commands spread out among all programs (menu, client, and server)
378 float GenericCommand(string command)
380 float argc = tokenize_console(command);
382 // Guide for working with argc arguments by example:
383 // argc: 1 - 2 - 3 - 4
384 // argv: 0 - 1 - 2 - 3
385 // cmd vote - master - login - password
387 if(strtolower(argv(0)) == "help")
391 GenericCommand_macro_help();
394 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
399 else if(GenericCommand_macro_command(argc)) // continue as usual and scan for normal commands
401 return TRUE; // handled by one of the above LocalCommand_* functions
403 else if(argc >= 3 && argv(0) == "red")
405 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
406 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
409 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
412 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
414 n = floor(random() * 6 + 2);
417 for(i = 0; i < n; ++i)
419 s2 = strcat(s2, "AH");
423 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
429 s = strcat(s, " ", s2);
431 s = strcat(s2, " ", s);
433 s2 = substring(s, strlen(s) - 2, 2);
434 if(s2 == "AH" || s2 == "AY")
437 s = strcat(s, " ))");
440 s = substring(s, 0, strlen(s) - 1);
443 s = strconv(1, 0, 0, s);
445 localcmd(strcat(argv(1), " ", s));
449 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
451 // test case for terencehill's color codes
452 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
456 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
459 for(i = 0; i < n; ++i)
461 c = substring(s, i, 1);
468 if(substring(s, i+1, 1) == "^")
474 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
475 c = strcat(rgb_to_hexcolor(rgb), c);
480 localcmd(strcat(argv(1), " ", s2));