1 // =========================================================
2 // Generic program common command code, written by Samual
3 // Last updated: December 28th, 2011
4 // =========================================================
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
21 // =======================
22 // Command Sub-Functions
23 // =======================
25 void GenericCommand_addtolist(float request, float argc)
29 case CMD_REQUEST_COMMAND:
35 string original_cvar = argv(1);
36 string tmp_string = argv(2);
38 if(cvar_string(original_cvar) == "") // cvar was empty
40 cvar_set(original_cvar, tmp_string);
42 else // add it to the end of the list if the list doesn't already have it
44 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
46 for(i = 0; i < argc; ++i)
47 if(argv(i) == tmp_string)
48 return; // already in list
50 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
57 print("Incorrect parameters for ^2addtolist^7\n");
58 case CMD_REQUEST_USAGE:
60 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value"));
61 print(" Where 'variable' is what to add 'value' to.\n");
62 print("See also: ^2removefromlist^7\n");
68 void GenericCommand_dumpcommands(float request)
72 case CMD_REQUEST_COMMAND:
75 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
76 fh = fopen(filename, FILE_WRITE);
81 CMD_Write("dump of server console commands:\n");
82 GameCommand_macro_write_aliases(fh);
84 CMD_Write("\ndump of networked client only commands:\n");
85 ClientCommand_macro_write_aliases(fh);
87 CMD_Write("\ndump of common commands:\n");
88 CommonCommand_macro_write_aliases(fh);
90 CMD_Write("\ndump of ban commands:\n");
91 BanCommand_macro_write_aliases(fh);
95 CMD_Write("dump of client commands:\n");
96 LocalCommand_macro_write_aliases(fh);
99 CMD_Write("\ndump of generic commands:\n");
100 GenericCommand_macro_write_aliases(fh);
102 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
108 print("^1Error: ^7Could not dump to file!\n");
114 case CMD_REQUEST_USAGE:
116 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
117 print(" No arguments required.\n");
123 void GenericCommand_maplist(float request, float argc)
127 case CMD_REQUEST_COMMAND:
134 case "add": // appends new maps to the maplist
138 if (!fexists(strcat("maps/", argv(2), ".bsp")))
140 print("maplist: ERROR: ", argv(2), " does not exist!\n");
144 if(cvar_string("g_maplist") == "")
145 cvar_set("g_maplist", argv(2));
147 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
151 break; // go to usage
154 case "cleanup": // scans maplist and only adds back the ones which are really usable
157 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
158 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
160 for(i = 0; i < argc; ++i)
161 if(MapInfo_CheckMap(argv(i)))
162 tmp_string = strcat(tmp_string, " ", argv(i));
164 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
165 cvar_set("g_maplist", tmp_string);
170 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
174 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
176 for(i = 0; i < argc; ++i)
177 if(argv(i) != argv(2))
178 tmp_string = strcat(tmp_string, " ", argv(i));
180 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
181 cvar_set("g_maplist", tmp_string);
185 break; // go to usage
188 case "shuffle": // randomly shuffle the maplist
190 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
199 print("Incorrect parameters for ^2maplist^7\n");
200 case CMD_REQUEST_USAGE:
202 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]"));
203 print(" Where 'action' is the command to complete,\n");
204 print(" and 'map' is what it acts upon (if required).\n");
205 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
211 void GenericCommand_removefromlist(float request, float argc)
215 case CMD_REQUEST_COMMAND:
220 string original_cvar = argv(1);
221 string removal = argv(2);
224 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
226 for(i = 0; i < argc; ++i)
227 if(argv(i) != removal)
228 tmp_string = strcat(tmp_string, " ", argv(i));
230 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
231 cvar_set(original_cvar, tmp_string);
238 print("Incorrect parameters for ^2removefromlist^7\n");
239 case CMD_REQUEST_USAGE:
241 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value"));
242 print(" Where 'variable' is what cvar to remove 'value' from.\n");
243 print("See also: ^2addtolist^7\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(), " settemp \"cvar\" \"arguments\"\n"));
271 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
272 print("See also: ^2settemp_restore^7\n");
278 void GenericCommand_settemp_restore(float request, float argc)
282 case CMD_REQUEST_COMMAND:
284 float i = cvar_settemp_restore();
287 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
289 dprint("Nothing to restore.\n");
295 case CMD_REQUEST_USAGE:
297 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
298 print(" No arguments required.\n");
299 print("See also: ^2settemp^7\n");
305 /* use this when creating a new command, making sure to place it in alphabetical order... also,
306 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
307 void GenericCommand_(float request)
311 case CMD_REQUEST_COMMAND:
318 case CMD_REQUEST_USAGE:
320 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
321 print(" No arguments required.\n");
328 // ==================================
329 // Macro system for server commands
330 // ==================================
332 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
333 #define GENERIC_COMMANDS(request,arguments,command) \
334 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
335 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
336 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
337 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
338 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
339 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
340 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
343 void GenericCommand_macro_help()
345 #define GENERIC_COMMAND(name,function,description) \
346 { print(" ^2", name, "^7: ", description, "\n"); }
348 GENERIC_COMMANDS(0, 0, "")
349 #undef GENERIC_COMMAND
354 float GenericCommand_macro_command(float argc, string command)
356 #define GENERIC_COMMAND(name,function,description) \
357 { if(name == strtolower(argv(0))) { function; return TRUE; } }
359 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
360 #undef GENERIC_COMMAND
365 float GenericCommand_macro_usage(float argc)
367 #define GENERIC_COMMAND(name,function,description) \
368 { if(name == strtolower(argv(1))) { function; return TRUE; } }
370 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
371 #undef GENERIC_COMMAND
376 void GenericCommand_macro_write_aliases(float fh)
378 #define GENERIC_COMMAND(name,function,description) \
379 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
381 GENERIC_COMMANDS(0, 0, "")
382 #undef GENERIC_COMMAND
388 // ===========================================
389 // Main Common Function For Generic Commands
390 // ===========================================
391 // Commands spread out among all programs (menu, client, and server)
393 float GenericCommand(string command)
395 float argc = tokenize_console(command);
400 // Guide for working with argc arguments by example:
401 // argc: 1 - 2 - 3 - 4
402 // argv: 0 - 1 - 2 - 3
403 // cmd vote - master - login - password
405 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
407 return TRUE; // handled by one of the above GenericCommand_* functions
409 else if(argc >= 3 && argv(0) == "red")
411 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
412 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
415 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
418 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
420 n = floor(random() * 6 + 2);
423 for(i = 0; i < n; ++i)
425 s2 = strcat(s2, "AH");
429 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
435 s = strcat(s, " ", s2);
437 s = strcat(s2, " ", s);
439 s2 = substring(s, strlen(s) - 2, 2);
440 if(s2 == "AH" || s2 == "AY")
443 s = strcat(s, " ))");
446 s = substring(s, 0, strlen(s) - 1);
449 s = strconv(1, 0, 0, s);
451 localcmd(strcat(argv(1), " ", s));
455 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
457 // test case for terencehill's color codes
458 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
462 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
465 for(i = 0; i < n; ++i)
467 c = substring(s, i, 1);
474 if(substring(s, i+1, 1) == "^")
480 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
481 c = strcat(rgb_to_hexcolor(rgb), c);
486 localcmd(strcat(argv(1), " ", s2));