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 string original_cvar = argv(1);
31 string tmp_string = argv(2);
33 if(cvar_string(original_cvar) == "") // cvar was empty
35 cvar_set(original_cvar, tmp_string);
37 else // add it to the end of the list if the list doesn't already have it
39 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
41 for(i = 0; i < argc; ++i)
42 if(argv(i) == tmp_string)
43 return; // already in list
45 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
53 case CMD_REQUEST_USAGE:
55 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable [value]"));
56 print(" Where 'variable' is what to add to the list,\n");
57 print(" and 'value' is any extra optional paramaters to add with quotes.");
63 void GenericCommand_dumpcommands(float request)
67 case CMD_REQUEST_COMMAND:
70 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
71 fh = fopen(filename, FILE_WRITE);
77 CMD_Write("dump of server console commands:\n");
78 GameCommand_macro_write_aliases(fh);
80 CMD_Write("\ndump of networked client only commands:\n");
81 ClientCommand_macro_write_aliases(fh);
83 CMD_Write("\ndump of common commands:\n");
84 CommonCommand_macro_write_aliases(fh);
86 CMD_Write("\ndump of ban commands:\n");
93 CMD_Write("dump of client commands:\n");
94 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 case CMD_REQUEST_USAGE:
237 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
238 print(" No arguments required.\n");
244 void GenericCommand_settemp(float request, float argc)
248 case CMD_REQUEST_COMMAND:
252 if(cvar_settemp(argv(1), argv(2)))
253 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
255 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
262 print("Incorrect parameters for ^2settemp^7\n");
263 case CMD_REQUEST_USAGE:
265 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
266 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
272 void GenericCommand_settemp_restore(float request, float argc)
276 case CMD_REQUEST_COMMAND:
278 float i = cvar_settemp_restore();
281 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
283 dprint("Nothing to restore.\n");
289 case CMD_REQUEST_USAGE:
291 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
292 print(" No arguments required.\n");
298 /* use this when creating a new command, making sure to place it in alphabetical order... also,
299 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
300 void GenericCommand_(float request)
304 case CMD_REQUEST_COMMAND:
311 case CMD_REQUEST_USAGE:
313 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
314 print(" No arguments required.\n");
321 // ==================================
322 // Macro system for server commands
323 // ==================================
325 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
326 #define GENERIC_COMMANDS(request,arguments,command) \
327 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
328 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
329 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
330 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
331 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
332 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
333 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
336 void GenericCommand_macro_help()
338 #define GENERIC_COMMAND(name,function,description) \
339 { print(" ^2", name, "^7: ", description, "\n"); }
341 GENERIC_COMMANDS(0, 0, "")
342 #undef GENERIC_COMMAND
347 float GenericCommand_macro_command(float argc, string command)
349 #define GENERIC_COMMAND(name,function,description) \
350 { if(name == strtolower(argv(0))) { function; return TRUE; } }
352 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
353 #undef GENERIC_COMMAND
358 float GenericCommand_macro_usage(float argc)
360 #define GENERIC_COMMAND(name,function,description) \
361 { if(name == strtolower(argv(1))) { function; return TRUE; } }
363 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
364 #undef GENERIC_COMMAND
369 void GenericCommand_macro_write_aliases(float fh)
371 #define GENERIC_COMMAND(name,function,description) \
372 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
374 GENERIC_COMMANDS(0, 0, "")
375 #undef GENERIC_COMMAND
381 // ===========================================
382 // Main Common Function For Generic Commands
383 // ===========================================
384 // Commands spread out among all programs (menu, client, and server)
386 float GenericCommand(string command)
388 float argc = tokenize_console(command);
393 // Guide for working with argc arguments by example:
394 // argc: 1 - 2 - 3 - 4
395 // argv: 0 - 1 - 2 - 3
396 // cmd vote - master - login - password
398 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
400 return TRUE; // handled by one of the above GenericCommand_* functions
402 else if(argc >= 3 && argv(0) == "red")
404 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
405 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
408 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
411 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
413 n = floor(random() * 6 + 2);
416 for(i = 0; i < n; ++i)
418 s2 = strcat(s2, "AH");
422 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
428 s = strcat(s, " ", s2);
430 s = strcat(s2, " ", s);
432 s2 = substring(s, strlen(s) - 2, 2);
433 if(s2 == "AH" || s2 == "AY")
436 s = strcat(s, " ))");
439 s = substring(s, 0, strlen(s) - 1);
442 s = strconv(1, 0, 0, s);
444 localcmd(strcat(argv(1), " ", s));
448 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
450 // test case for terencehill's color codes
451 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
455 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
458 for(i = 0; i < n; ++i)
460 c = substring(s, i, 1);
467 if(substring(s, i+1, 1) == "^")
473 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
474 c = strcat(rgb_to_hexcolor(rgb), c);
479 localcmd(strcat(argv(1), " ", s2));