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");
67 void GenericCommand_dumpcommands(float request)
71 case CMD_REQUEST_COMMAND:
74 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
75 fh = fopen(filename, FILE_WRITE);
80 CMD_Write("dump of server console commands:\n");
81 GameCommand_macro_write_aliases(fh);
83 CMD_Write("\ndump of networked client only commands:\n");
84 ClientCommand_macro_write_aliases(fh);
86 CMD_Write("\ndump of common commands:\n");
87 CommonCommand_macro_write_aliases(fh);
89 CMD_Write("\ndump of ban commands:\n");
90 BanCommand_macro_write_aliases(fh);
94 CMD_Write("dump of client commands:\n");
95 LocalCommand_macro_write_aliases(fh);
98 CMD_Write("\ndump of generic commands:\n");
99 GenericCommand_macro_write_aliases(fh);
101 print("Completed dump of aliases in ^2", GetProgramCommandPrefix(), "_dump.txt^7.\n");
107 print("^1Error: ^7Could not dump to file!\n");
113 case CMD_REQUEST_USAGE:
115 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
116 print(" No arguments required.\n");
122 void GenericCommand_maplist(float request, float argc)
126 case CMD_REQUEST_COMMAND:
133 case "add": // appends new maps to the maplist
137 if (!fexists(strcat("maps/", argv(2), ".bsp")))
139 print("maplist: ERROR: ", argv(2), " does not exist!\n");
143 if(cvar_string("g_maplist") == "")
144 cvar_set("g_maplist", argv(2));
146 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
150 break; // go to usage
153 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
157 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
159 for(i = 0; i < argc; ++i)
160 if(argv(i) != argv(2))
161 tmp_string = strcat(tmp_string, " ", argv(i));
163 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
164 cvar_set("g_maplist", tmp_string);
168 break; // go to usage
171 case "shuffle": // randomly shuffle the maplist
173 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
177 case "cleanup": // scans maplist and only adds back the ones which are really usable
180 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
181 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
183 for(i = 0; i < argc; ++i)
184 if(MapInfo_CheckMap(argv(i)))
185 tmp_string = strcat(tmp_string, " ", argv(i));
187 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
188 cvar_set("g_maplist", tmp_string);
198 print("Incorrect parameters for ^2maplist^7\n");
199 case CMD_REQUEST_USAGE:
201 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
202 print(" No arguments required.\n");
208 void GenericCommand_removefromlist(float request, float argc)
212 case CMD_REQUEST_COMMAND:
217 string original_cvar = argv(1);
218 string removal = argv(2);
221 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
223 for(i = 0; i < argc; ++i)
224 if(argv(i) != removal)
225 tmp_string = strcat(tmp_string, " ", argv(i));
227 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
228 cvar_set(original_cvar, tmp_string);
235 print("Incorrect parameters for ^2removefromlist^7\n");
236 case CMD_REQUEST_USAGE:
238 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value"));
239 print(" Where 'variable' is what cvar to remove 'value' from.\n");
245 void GenericCommand_settemp(float request, float argc)
249 case CMD_REQUEST_COMMAND:
253 if(cvar_settemp(argv(1), argv(2)))
254 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
256 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
263 print("Incorrect parameters for ^2settemp^7\n");
264 case CMD_REQUEST_USAGE:
266 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
267 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
273 void GenericCommand_settemp_restore(float request, float argc)
277 case CMD_REQUEST_COMMAND:
279 float i = cvar_settemp_restore();
282 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
284 dprint("Nothing to restore.\n");
290 case CMD_REQUEST_USAGE:
292 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
293 print(" No arguments required.\n");
299 /* use this when creating a new command, making sure to place it in alphabetical order... also,
300 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
301 void GenericCommand_(float request)
305 case CMD_REQUEST_COMMAND:
312 case CMD_REQUEST_USAGE:
314 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
315 print(" No arguments required.\n");
322 // ==================================
323 // Macro system for server commands
324 // ==================================
326 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
327 #define GENERIC_COMMANDS(request,arguments,command) \
328 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
329 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
330 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
331 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
332 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
333 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
334 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
337 void GenericCommand_macro_help()
339 #define GENERIC_COMMAND(name,function,description) \
340 { print(" ^2", name, "^7: ", description, "\n"); }
342 GENERIC_COMMANDS(0, 0, "")
343 #undef GENERIC_COMMAND
348 float GenericCommand_macro_command(float argc, string command)
350 #define GENERIC_COMMAND(name,function,description) \
351 { if(name == strtolower(argv(0))) { function; return TRUE; } }
353 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
354 #undef GENERIC_COMMAND
359 float GenericCommand_macro_usage(float argc)
361 #define GENERIC_COMMAND(name,function,description) \
362 { if(name == strtolower(argv(1))) { function; return TRUE; } }
364 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
365 #undef GENERIC_COMMAND
370 void GenericCommand_macro_write_aliases(float fh)
372 #define GENERIC_COMMAND(name,function,description) \
373 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
375 GENERIC_COMMANDS(0, 0, "")
376 #undef GENERIC_COMMAND
382 // ===========================================
383 // Main Common Function For Generic Commands
384 // ===========================================
385 // Commands spread out among all programs (menu, client, and server)
387 float GenericCommand(string command)
389 float argc = tokenize_console(command);
394 // Guide for working with argc arguments by example:
395 // argc: 1 - 2 - 3 - 4
396 // argv: 0 - 1 - 2 - 3
397 // cmd vote - master - login - password
399 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
401 return TRUE; // handled by one of the above GenericCommand_* 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), " ", GenericCommand_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));