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_nextframe(float request, float arguments, string command)
215 case CMD_REQUEST_COMMAND:
217 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
222 case CMD_REQUEST_USAGE:
224 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command..."));
225 print(" Where command will be executed next frame of this VM\n");
231 void GenericCommand_removefromlist(float request, float argc)
235 case CMD_REQUEST_COMMAND:
240 string original_cvar = argv(1);
241 string removal = argv(2);
244 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
246 for(i = 0; i < argc; ++i)
247 if(argv(i) != removal)
248 tmp_string = strcat(tmp_string, " ", argv(i));
250 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
251 cvar_set(original_cvar, tmp_string);
258 print("Incorrect parameters for ^2removefromlist^7\n");
259 case CMD_REQUEST_USAGE:
261 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value"));
262 print(" Where 'variable' is what cvar to remove 'value' from.\n");
263 print("See also: ^2addtolist^7\n");
269 void GenericCommand_settemp(float request, float argc)
273 case CMD_REQUEST_COMMAND:
277 if(cvar_settemp(argv(1), argv(2)))
278 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
280 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
287 print("Incorrect parameters for ^2settemp^7\n");
288 case CMD_REQUEST_USAGE:
290 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
291 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
292 print("See also: ^2settemp_restore^7\n");
298 void GenericCommand_settemp_restore(float request, float argc)
302 case CMD_REQUEST_COMMAND:
304 float i = cvar_settemp_restore();
307 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
309 dprint("Nothing to restore.\n");
315 case CMD_REQUEST_USAGE:
317 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
318 print(" No arguments required.\n");
319 print("See also: ^2settemp^7\n");
325 /* use this when creating a new command, making sure to place it in alphabetical order... also,
326 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
327 void GenericCommand_(float request)
331 case CMD_REQUEST_COMMAND:
338 case CMD_REQUEST_USAGE:
340 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
341 print(" No arguments required.\n");
348 // ==================================
349 // Macro system for server commands
350 // ==================================
352 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
353 #define GENERIC_COMMANDS(request,arguments,command) \
354 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
355 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
356 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
357 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
358 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
359 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
360 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
361 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
364 void GenericCommand_macro_help()
366 #define GENERIC_COMMAND(name,function,description) \
367 { print(" ^2", name, "^7: ", description, "\n"); }
369 GENERIC_COMMANDS(0, 0, "")
370 #undef GENERIC_COMMAND
375 float GenericCommand_macro_command(float argc, string command)
377 #define GENERIC_COMMAND(name,function,description) \
378 { if(name == strtolower(argv(0))) { function; return TRUE; } }
380 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
381 #undef GENERIC_COMMAND
386 float GenericCommand_macro_usage(float argc)
388 #define GENERIC_COMMAND(name,function,description) \
389 { if(name == strtolower(argv(1))) { function; return TRUE; } }
391 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
392 #undef GENERIC_COMMAND
397 void GenericCommand_macro_write_aliases(float fh)
399 #define GENERIC_COMMAND(name,function,description) \
400 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
402 GENERIC_COMMANDS(0, 0, "")
403 #undef GENERIC_COMMAND
409 // ===========================================
410 // Main Common Function For Generic Commands
411 // ===========================================
412 // Commands spread out among all programs (menu, client, and server)
414 float GenericCommand(string command)
416 float argc = tokenize_console(command);
421 // Guide for working with argc arguments by example:
422 // argc: 1 - 2 - 3 - 4
423 // argv: 0 - 1 - 2 - 3
424 // cmd vote - master - login - password
426 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
428 return TRUE; // handled by one of the above GenericCommand_* functions
430 else if(argc >= 3 && argv(0) == "red")
432 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
433 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
436 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
439 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
441 n = floor(random() * 6 + 2);
444 for(i = 0; i < n; ++i)
446 s2 = strcat(s2, "AH");
450 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
456 s = strcat(s, " ", s2);
458 s = strcat(s2, " ", s);
460 s2 = substring(s, strlen(s) - 2, 2);
461 if(s2 == "AH" || s2 == "AY")
464 s = strcat(s, " ))");
467 s = substring(s, 0, strlen(s) - 1);
470 s = strconv(1, 0, 0, s);
472 localcmd(strcat(argv(1), " ", s));
476 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
478 // test case for terencehill's color codes
479 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
483 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
486 for(i = 0; i < n; ++i)
488 c = substring(s, i, 1);
495 if(substring(s, i+1, 1) == "^")
501 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
502 c = strcat(rgb_to_hexcolor(rgb), c);
507 localcmd(strcat(argv(1), " ", s2));