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 // write sv_cmd aliases
74 CMD_Write("dump of server console commands:\n");
75 GameCommand_macro_write_aliases(fh);
77 // write client only aliases
78 CMD_Write("\ndump of networked client only commands:\n");
79 ClientCommand_macro_write_aliases(fh);
81 // write common aliases
82 CMD_Write("\ndump of common commands:\n");
83 CommonCommand_macro_write_aliases(fh);
86 CMD_Write("\ndump of ban commands:\n");
92 // write cl_cmd aliases
93 CMD_Write("dump of client commands:\n");
94 LocalCommand_macro_write_aliases(fh);
98 // write generic commands
99 CMD_Write("\ndump of generic commands:\n");
100 GenericCommand_macro_write_aliases(fh);
102 print("Completed dump of aliases in ^2", 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 "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
158 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
160 for(i = 0; i < argc; ++i)
161 if(argv(i) != argv(2))
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);
169 break; // go to usage
172 case "shuffle": // randomly shuffle the maplist
174 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
178 case "cleanup": // scans maplist and only adds back the ones which are really usable
181 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
182 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
184 for(i = 0; i < argc; ++i)
185 if(MapInfo_CheckMap(argv(i)))
186 tmp_string = strcat(tmp_string, " ", argv(i));
188 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
189 cvar_set("g_maplist", tmp_string);
199 print("Incorrect parameters for ^2maplist^7\n");
200 case CMD_REQUEST_USAGE:
202 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
203 print(" No arguments required.\n");
209 void GenericCommand_settemp(float request, float argc)
213 case CMD_REQUEST_COMMAND:
217 if(cvar_settemp(argv(1), argv(2)))
218 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
220 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
227 print("Incorrect parameters for ^2settemp^7\n");
228 case CMD_REQUEST_USAGE:
230 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
231 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
237 void GenericCommand_settemp_restore(float request, float argc)
241 case CMD_REQUEST_COMMAND:
243 float i = cvar_settemp_restore();
246 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
248 dprint("Nothing to restore.\n");
254 case CMD_REQUEST_USAGE:
256 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
257 print(" No arguments required.\n");
263 /* use this when creating a new command, making sure to place it in alphabetical order... also,
264 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
265 void GenericCommand_(float request)
269 case CMD_REQUEST_COMMAND:
276 case CMD_REQUEST_USAGE:
278 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
279 print(" No arguments required.\n");
286 // ==================================
287 // Macro system for server commands
288 // ==================================
290 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
291 #define GENERIC_COMMANDS(request,arguments,command) \
292 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
293 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
294 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
295 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
296 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
297 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
300 void GenericCommand_macro_help()
302 #define GENERIC_COMMAND(name,function,description) \
303 { if(strtolower(description) != string_null) { print(" ^2", name, "^7: ", description, "\n"); } }
305 GENERIC_COMMANDS(0, 0, "")
306 #undef GENERIC_COMMAND
311 float GenericCommand_macro_command(float argc, string command)
313 #define GENERIC_COMMAND(name,function,description) \
314 { if(name == strtolower(argv(0))) { function; return TRUE; } }
316 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
317 #undef GENERIC_COMMAND
322 float GenericCommand_macro_usage(float argc)
324 #define GENERIC_COMMAND(name,function,description) \
325 { if(name == strtolower(argv(1))) { function; return TRUE; } }
327 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
328 #undef GENERIC_COMMAND
333 void GenericCommand_macro_write_aliases(float fh)
335 #define GENERIC_COMMAND(name,function,description) \
336 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
338 GENERIC_COMMANDS(0, 0, "")
339 #undef GENERIC_COMMAND
345 // ===========================================
346 // Main Common Function For Generic Commands
347 // ===========================================
348 // Commands spread out among all programs (menu, client, and server)
350 float GenericCommand(string command)
352 float argc = tokenize_console(command);
357 // Guide for working with argc arguments by example:
358 // argc: 1 - 2 - 3 - 4
359 // argv: 0 - 1 - 2 - 3
360 // cmd vote - master - login - password
362 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
364 return TRUE; // handled by one of the above GenericCommand_* functions
366 else if(argc >= 3 && argv(0) == "red")
368 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
369 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
372 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
375 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
377 n = floor(random() * 6 + 2);
380 for(i = 0; i < n; ++i)
382 s2 = strcat(s2, "AH");
386 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
392 s = strcat(s, " ", s2);
394 s = strcat(s2, " ", s);
396 s2 = substring(s, strlen(s) - 2, 2);
397 if(s2 == "AH" || s2 == "AY")
400 s = strcat(s, " ))");
403 s = substring(s, 0, strlen(s) - 1);
406 s = strconv(1, 0, 0, s);
408 localcmd(strcat(argv(1), " ", s));
412 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
414 // test case for terencehill's color codes
415 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
419 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
422 for(i = 0; i < n; ++i)
424 c = substring(s, i, 1);
431 if(substring(s, i+1, 1) == "^")
437 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
438 c = strcat(rgb_to_hexcolor(rgb), c);
443 localcmd(strcat(argv(1), " ", s2));