1 // =========================================================
2 // Generic program common command code, reworked 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_maplist(float request, float argc)
63 case CMD_REQUEST_COMMAND:
70 case "add": // appends new maps to the maplist
74 if (!fexists(strcat("maps/", argv(2), ".bsp")))
76 print("maplist: ERROR: ", argv(2), " does not exist!\n");
80 if(cvar_string("g_maplist") == "")
81 cvar_set("g_maplist", argv(2));
83 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
90 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
94 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
96 for(i = 0; i < argc; ++i)
97 if(argv(i) != argv(2))
98 tmp_string = strcat(tmp_string, " ", argv(i));
100 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
101 cvar_set("g_maplist", tmp_string);
105 break; // go to usage
108 case "shuffle": // randomly shuffle the maplist
110 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
114 case "cleanup": // scans maplist and only adds back the ones which are really usable
117 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
118 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
120 for(i = 0; i < argc; ++i)
121 if(MapInfo_CheckMap(argv(i)))
122 tmp_string = strcat(tmp_string, " ", argv(i));
124 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
125 cvar_set("g_maplist", tmp_string);
135 case CMD_REQUEST_USAGE:
137 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
138 print(" No arguments required.\n");
144 void GenericCommand_settemp(float request, float argc)
148 case CMD_REQUEST_COMMAND:
152 if(cvar_settemp(argv(1), argv(2)))
153 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
155 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
162 print("Incorrect parameters for ^2settemp^7\n");
163 case CMD_REQUEST_USAGE:
165 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
166 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
172 void GenericCommand_settemp_restore(float request, float argc)
176 case CMD_REQUEST_COMMAND:
178 float i = cvar_settemp_restore();
181 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
183 dprint("Nothing to restore.\n");
189 case CMD_REQUEST_USAGE:
191 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
192 print(" No arguments required.\n");
198 /* use this when creating a new command, making sure to place it in alphabetical order... also,
199 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
200 void GenericCommand_(float request)
204 case CMD_REQUEST_COMMAND:
211 case CMD_REQUEST_USAGE:
213 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " ")));
214 print(" No arguments required.\n");
221 // ==================================
222 // Macro system for server commands
223 // ==================================
225 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
226 #define GENERIC_COMMANDS(request,arguments,command) \
227 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
228 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
229 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
230 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
231 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
234 void GenericCommand_macro_help()
236 #define GENERIC_COMMAND(name,function,description) \
237 { print(" ^2", name, "^7: ", description, "\n"); }
239 GENERIC_COMMANDS(0, 0, "")
240 #undef GENERIC_COMMAND
245 float GenericCommand_macro_command(float argc, string command)
247 #define GENERIC_COMMAND(name,function,description) \
248 { if(name == strtolower(argv(0))) { function; return TRUE; } }
250 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
251 #undef GENERIC_COMMAND
256 float GenericCommand_macro_usage(float argc)
258 #define GENERIC_COMMAND(name,function,description) \
259 { if(name == strtolower(argv(1))) { function; return TRUE; } }
261 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
262 #undef GENERIC_COMMAND
268 // ===========================================
269 // Main Common Function For Generic Commands
270 // ===========================================
271 // Commands spread out among all programs (menu, client, and server)
273 float GenericCommand(string command)
275 float argc = tokenize_console(command);
280 // Guide for working with argc arguments by example:
281 // argc: 1 - 2 - 3 - 4
282 // argv: 0 - 1 - 2 - 3
283 // cmd vote - master - login - password
285 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
287 return TRUE; // handled by one of the above GenericCommand_* functions
289 else if(argc >= 3 && argv(0) == "red")
291 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
292 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
295 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
298 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
300 n = floor(random() * 6 + 2);
303 for(i = 0; i < n; ++i)
305 s2 = strcat(s2, "AH");
309 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
315 s = strcat(s, " ", s2);
317 s = strcat(s2, " ", s);
319 s2 = substring(s, strlen(s) - 2, 2);
320 if(s2 == "AH" || s2 == "AY")
323 s = strcat(s, " ))");
326 s = substring(s, 0, strlen(s) - 1);
329 s = strconv(1, 0, 0, s);
331 localcmd(strcat(argv(1), " ", s));
335 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
337 // test case for terencehill's color codes
338 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
342 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
345 for(i = 0; i < n; ++i)
347 c = substring(s, i, 1);
354 if(substring(s, i+1, 1) == "^")
360 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
361 c = strcat(rgb_to_hexcolor(rgb), c);
366 localcmd(strcat(argv(1), " ", s2));