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)
20 void GenericCommand_addtolist(float request, float argc)
24 case CMD_REQUEST_COMMAND:
30 if(cvar_string(argv(1)) == "") // cvar was empty
32 cvar_set(argv(1), argv(2));
34 else // add it to the end of the list if the list doesn't already have it
36 argc = tokenizebyseparator(cvar_string(argv(1)), " ");
37 for(i = 0; i < argc; ++i)
38 if(argv(i) == argv(2))
39 return; // already in list
41 cvar_set(argv(1), strcat(argv(2), " ", cvar_string(argv(1))));
49 case CMD_REQUEST_USAGE:
51 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable [value]"));
52 print(" Where 'variable' is what to add to the list,\n");
53 print(" and 'value' is any extra optional paramaters to add with quotes.");
59 void GenericCommand_dumpcommands(float request)
63 case CMD_REQUEST_COMMAND:
66 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
67 fh = fopen(filename, FILE_WRITE);
73 CMD_Write("dump of server console commands:\n");
74 GameCommand_macro_write_aliases(fh);
76 CMD_Write("\ndump of networked client only commands:\n");
77 ClientCommand_macro_write_aliases(fh);
79 CMD_Write("\ndump of common commands:\n");
80 CommonCommand_macro_write_aliases(fh);
82 CMD_Write("\ndump of ban commands:\n");
89 CMD_Write("dump of client commands:\n");
90 LocalCommand_macro_write_aliases(fh);
94 CMD_Write("\ndump of generic commands:\n");
95 GenericCommand_macro_write_aliases(fh);
97 print("Completed dump of aliases in ^2", GetProgramCommandPrefix(), "_dump.txt^7.\n");
103 print("^1Error: ^7Could not dump to file!\n");
109 case CMD_REQUEST_USAGE:
111 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
112 print(" No arguments required.\n");
118 void GenericCommand_maplist(float request, float argc)
122 case CMD_REQUEST_COMMAND:
129 case "add": // appends new maps to the maplist
133 if (!fexists(strcat("maps/", argv(2), ".bsp")))
135 print("maplist: ERROR: ", argv(2), " does not exist!\n");
139 if(cvar_string("g_maplist") == "")
140 cvar_set("g_maplist", argv(2));
142 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
146 break; // go to usage
149 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
153 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
155 for(i = 0; i < argc; ++i)
156 if(argv(i) != argv(2))
157 tmp_string = strcat(tmp_string, " ", argv(i));
159 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
160 cvar_set("g_maplist", tmp_string);
164 break; // go to usage
167 case "shuffle": // randomly shuffle the maplist
169 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
173 case "cleanup": // scans maplist and only adds back the ones which are really usable
176 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
177 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
179 for(i = 0; i < argc; ++i)
180 if(MapInfo_CheckMap(argv(i)))
181 tmp_string = strcat(tmp_string, " ", argv(i));
183 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
184 cvar_set("g_maplist", tmp_string);
194 print("Incorrect parameters for ^2maplist^7\n");
195 case CMD_REQUEST_USAGE:
197 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
198 print(" No arguments required.\n");
204 void GenericCommand_settemp(float request, float argc)
208 case CMD_REQUEST_COMMAND:
212 if(cvar_settemp(argv(1), argv(2)))
213 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
215 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
222 print("Incorrect parameters for ^2settemp^7\n");
223 case CMD_REQUEST_USAGE:
225 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
226 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
232 void GenericCommand_settemp_restore(float request, float argc)
236 case CMD_REQUEST_COMMAND:
238 float i = cvar_settemp_restore();
241 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
243 dprint("Nothing to restore.\n");
249 case CMD_REQUEST_USAGE:
251 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
252 print(" No arguments required.\n");
258 /* use this when creating a new command, making sure to place it in alphabetical order... also,
259 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
260 void GenericCommand_(float request)
264 case CMD_REQUEST_COMMAND:
271 case CMD_REQUEST_USAGE:
273 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
274 print(" No arguments required.\n");
281 // ==================================
282 // Macro system for server commands
283 // ==================================
285 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
286 #define GENERIC_COMMANDS(request,arguments,command) \
287 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
288 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
289 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
290 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
291 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
292 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
295 void GenericCommand_macro_help()
297 #define GENERIC_COMMAND(name,function,description) \
298 { print(" ^2", name, "^7: ", description, "\n"); }
300 GENERIC_COMMANDS(0, 0, "")
301 #undef GENERIC_COMMAND
306 float GenericCommand_macro_command(float argc, string command)
308 #define GENERIC_COMMAND(name,function,description) \
309 { if(name == strtolower(argv(0))) { function; return TRUE; } }
311 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
312 #undef GENERIC_COMMAND
317 float GenericCommand_macro_usage(float argc)
319 #define GENERIC_COMMAND(name,function,description) \
320 { if(name == strtolower(argv(1))) { function; return TRUE; } }
322 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
323 #undef GENERIC_COMMAND
328 void GenericCommand_macro_write_aliases(float fh)
330 #define GENERIC_COMMAND(name,function,description) \
331 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
333 GENERIC_COMMANDS(0, 0, "")
334 #undef GENERIC_COMMAND
340 // ===========================================
341 // Main Common Function For Generic Commands
342 // ===========================================
343 // Commands spread out among all programs (menu, client, and server)
345 float GenericCommand(string command)
347 float argc = tokenize_console(command);
352 // Guide for working with argc arguments by example:
353 // argc: 1 - 2 - 3 - 4
354 // argv: 0 - 1 - 2 - 3
355 // cmd vote - master - login - password
357 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
359 return TRUE; // handled by one of the above GenericCommand_* functions
361 else if(argc >= 3 && argv(0) == "red")
363 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
364 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
367 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
370 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
372 n = floor(random() * 6 + 2);
375 for(i = 0; i < n; ++i)
377 s2 = strcat(s2, "AH");
381 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
387 s = strcat(s, " ", s2);
389 s = strcat(s2, " ", s);
391 s2 = substring(s, strlen(s) - 2, 2);
392 if(s2 == "AH" || s2 == "AY")
395 s = strcat(s, " ))");
398 s = substring(s, 0, strlen(s) - 1);
401 s = strconv(1, 0, 0, s);
403 localcmd(strcat(argv(1), " ", s));
407 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
409 // test case for terencehill's color codes
410 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
414 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
417 for(i = 0; i < n; ++i)
419 c = substring(s, i, 1);
426 if(substring(s, i+1, 1) == "^")
432 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
433 c = strcat(rgb_to_hexcolor(rgb), c);
438 localcmd(strcat(argv(1), " ", s2));