1 // =========================================================
2 // Generic program common command code, reworked by Samual
3 // Last updated: December 28th, 2011
4 // =========================================================
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
20 #define NUM_MARKUPS 41
22 string markup_from[NUM_MARKUPS];
23 string markup_to[NUM_MARKUPS];
24 void GameCommand_MarkupInit()
31 markup_from[i] = "&alien"; markup_to[i] = "\x12"; ++i;
32 markup_from[i] = "&:-)"; markup_to[i] = "\x13"; ++i;
33 markup_from[i] = "&:-("; markup_to[i] = "\x14"; ++i;
34 markup_from[i] = "&x-P"; markup_to[i] = "\x15"; ++i;
35 markup_from[i] = "&:-/"; markup_to[i] = "\x16"; ++i;
36 markup_from[i] = "&:-D"; markup_to[i] = "\x17"; ++i;
37 markup_from[i] = "&<<"; markup_to[i] = "\x18"; ++i;
38 markup_from[i] = "&>>"; markup_to[i] = "\x19"; ++i;
39 markup_from[i] = "&dot"; markup_to[i] = "\x1a"; ++i;
40 markup_from[i] = "&^_"; markup_to[i] = "\x1b"; ++i;
41 markup_from[i] = "&ysplat"; markup_to[i] = "\x1c"; ++i;
42 markup_from[i] = "&-]"; markup_to[i] = "\x1d"; ++i;
43 markup_from[i] = "&--"; markup_to[i] = "\x1e"; ++i;
44 markup_from[i] = "&[-"; markup_to[i] = "\x1f"; ++i;
45 markup_from[i] = "&s<"; markup_to[i] = "\x2c"; ++i;
46 markup_from[i] = "&s>"; markup_to[i] = "\x2e"; ++i;
47 markup_from[i] = "&<-"; markup_to[i] = "\x7f"; ++i;
48 markup_from[i] = "&[="; markup_to[i] = "\x80"; ++i;
49 markup_from[i] = "&=="; markup_to[i] = "\x81"; ++i;
50 markup_from[i] = "&=]"; markup_to[i] = "\x82"; ++i;
51 markup_from[i] = "&r!"; markup_to[i] = "\x84"; ++i;
52 markup_from[i] = "&|o|"; markup_to[i] = "\x85"; ++i;
53 markup_from[i] = "&|u|"; markup_to[i] = "\x86"; ++i;
54 markup_from[i] = "&|i|"; markup_to[i] = "\x87"; ++i;
55 markup_from[i] = "&|c|"; markup_to[i] = "\x88"; ++i;
56 markup_from[i] = "&[c]"; markup_to[i] = "\x89"; ++i;
57 markup_from[i] = "&[n]"; markup_to[i] = "\x8a"; ++i;
58 markup_from[i] = "&[]"; markup_to[i] = "\x8b"; ++i;
59 markup_from[i] = "&r?"; markup_to[i] = "\x8c"; ++i;
60 markup_from[i] = "&|>"; markup_to[i] = "\x8d"; ++i;
61 markup_from[i] = "&splat0"; markup_to[i] = "\x8e"; ++i;
62 markup_from[i] = "&splat1"; markup_to[i] = "\x8f"; ++i;
63 markup_from[i] = "&[["; markup_to[i] = "\x90"; ++i;
64 markup_from[i] = "&]]"; markup_to[i] = "\x91"; ++i;
65 markup_from[i] = "&splat2"; markup_to[i] = "\x9a"; ++i;
66 markup_from[i] = "&)("; markup_to[i] = "\x9b"; ++i;
67 markup_from[i] = "&splat3"; markup_to[i] = "\x9c"; ++i;
68 markup_from[i] = "&(."; markup_to[i] = "\x9d"; ++i;
69 markup_from[i] = "&.."; markup_to[i] = "\x9e"; ++i;
70 markup_from[i] = "&.)"; markup_to[i] = "\x9f"; ++i;
71 markup_from[i] = "&<|"; markup_to[i] = "\xff"; ++i;
74 string GameCommand_Markup(string s2)
76 float red, ccase, i, j;
79 GameCommand_MarkupInit();
85 for(i = 0; i < strlen(s2); ++i)
87 for(j = 0; j < NUM_MARKUPS; ++j)
89 s3 = substring(s2, i, strlen(markup_from[j]));
90 if (s3 == markup_from[j])
92 s = strcat(s, markup_to[j]);
93 i += strlen(markup_from[j]) - 1;
100 if(substring(s2, i, 2) == "&&")
102 s = strcat(s, strconv(ccase, red, red, "&"));
105 else if(substring(s2, i, 2) == "&d")
111 else if(substring(s2, i, 2) == "&a")
117 else if(substring(s2, i, 2) == "&n")
124 s = strcat(s, strconv(ccase, red, red, substring(s2, i, 1)));
131 void GenericCommand_addtolist(float request, float argc)
135 case CMD_REQUEST_COMMAND:
141 if(cvar_string(argv(1)) == "") // cvar was empty
143 cvar_set(argv(1), argv(2));
145 else // add it to the end of the list if the list doesn't already have it
147 argc = tokenizebyseparator(cvar_string(argv(1)), " ");
148 for(i = 0; i < argc; ++i)
149 if(argv(i) == argv(2))
150 return; // already in list
152 cvar_set(argv(1), strcat(argv(2), " ", cvar_string(argv(1))));
160 case CMD_REQUEST_USAGE:
162 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable [value]"));
163 print(" Where 'variable' is what to add to the list,\n");
164 print(" and 'value' is any extra optional paramaters to add with quotes.");
170 void GenericCommand_maplist(float request, float argc)
174 case CMD_REQUEST_COMMAND:
181 case "add": // appends new maps to the maplist
185 if (!fexists(strcat("maps/", argv(2), ".bsp")))
187 print("maplist: ERROR: ", argv(2), " does not exist!\n");
191 if(cvar_string("g_maplist") == "")
192 cvar_set("g_maplist", argv(2));
194 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
198 break; // go to usage
201 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
205 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
207 for(i = 0; i < argc; ++i)
208 if(argv(i) != argv(2))
209 tmp_string = strcat(tmp_string, " ", argv(i));
211 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
212 cvar_set("g_maplist", tmp_string);
216 break; // go to usage
219 case "shuffle": // randomly shuffle the maplist
221 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
225 case "cleanup": // scans maplist and only adds back the ones which are really usable
228 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
229 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
231 for(i = 0; i < argc; ++i)
232 if(MapInfo_CheckMap(argv(i)))
233 tmp_string = strcat(tmp_string, " ", argv(i));
235 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
236 cvar_set("g_maplist", tmp_string);
246 case CMD_REQUEST_USAGE:
248 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
249 print(" No arguments required.\n");
255 void GenericCommand_settemp(float request, float argc)
259 case CMD_REQUEST_COMMAND:
263 if(cvar_settemp(argv(1), argv(2)))
264 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
266 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
273 print("Incorrect parameters for ^2settemp^7\n");
274 case CMD_REQUEST_USAGE:
276 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
277 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
283 void GenericCommand_settemp_restore(float request, float argc)
287 case CMD_REQUEST_COMMAND:
289 float i = cvar_settemp_restore();
292 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
294 dprint("Nothing to restore.\n");
300 case CMD_REQUEST_USAGE:
302 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
303 print(" No arguments required.\n");
309 /* use this when creating a new command, making sure to place it in alphabetical order... also,
310 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
311 void GenericCommand_(float request)
315 case CMD_REQUEST_COMMAND:
322 case CMD_REQUEST_USAGE:
324 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " ")));
325 print(" No arguments required.\n");
332 // ==================================
333 // Macro system for server commands
334 // ==================================
336 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
337 #define GENERIC_COMMANDS(request,arguments,command) \
338 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
339 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
340 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
341 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
342 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
345 void GenericCommand_macro_help()
347 #define GENERIC_COMMAND(name,function,description) \
348 { print(" ^2", name, "^7: ", description, "\n"); }
350 GENERIC_COMMANDS(0, 0, "")
351 #undef GENERIC_COMMAND
356 float GenericCommand_macro_command(float argc, string command)
358 #define GENERIC_COMMAND(name,function,description) \
359 { if(name == strtolower(argv(0))) { function; return TRUE; } }
361 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
362 #undef GENERIC_COMMAND
367 float GenericCommand_macro_usage(float argc)
369 #define GENERIC_COMMAND(name,function,description) \
370 { if(name == strtolower(argv(1))) { function; return TRUE; } }
372 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
373 #undef GENERIC_COMMAND
379 // ===========================================
380 // Main Common Function For Generic Commands
381 // ===========================================
382 // Commands spread out among all programs (menu, client, and server)
384 float GenericCommand(string command)
386 float argc = tokenize_console(command);
391 // Guide for working with argc arguments by example:
392 // argc: 1 - 2 - 3 - 4
393 // argv: 0 - 1 - 2 - 3
394 // cmd vote - master - login - password
396 if(strtolower(argv(0)) == "help")
400 GenericCommand_macro_help();
403 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
408 else if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
410 return TRUE; // handled by one of the above GenericCommand_* functions
412 else if(argc >= 3 && argv(0) == "red")
414 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
415 localcmd(strcat(argv(1), " ", GameCommand_Markup(s)));
418 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
421 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
423 n = floor(random() * 6 + 2);
426 for(i = 0; i < n; ++i)
428 s2 = strcat(s2, "AH");
432 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
438 s = strcat(s, " ", s2);
440 s = strcat(s2, " ", s);
442 s2 = substring(s, strlen(s) - 2, 2);
443 if(s2 == "AH" || s2 == "AY")
446 s = strcat(s, " ))");
449 s = substring(s, 0, strlen(s) - 1);
452 s = strconv(1, 0, 0, s);
454 localcmd(strcat(argv(1), " ", s));
458 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
460 // test case for terencehill's color codes
461 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
465 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
468 for(i = 0; i < n; ++i)
470 c = substring(s, i, 1);
477 if(substring(s, i+1, 1) == "^")
483 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
484 c = strcat(rgb_to_hexcolor(rgb), c);
489 localcmd(strcat(argv(1), " ", s2));