]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Re-write ban command system
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
1 // =========================================================
2 //  Generic program common command code, written by Samual
3 //  Last updated: December 28th, 2011
4 // =========================================================
5
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
8 {
9         #ifdef SVQC
10         return "sv_cmd";
11         #endif
12         #ifdef CSQC
13         return "cl_cmd";
14         #endif
15         #ifdef MENUQC
16         return "menu_cmd";
17         #endif
18 }
19
20
21 // =======================
22 //  Command Sub-Functions
23 // =======================
24
25 void GenericCommand_addtolist(float request, float argc)
26 {
27         switch(request)
28         {
29                 case CMD_REQUEST_COMMAND:
30                 {
31                         float i;
32                         
33                         if(argc >= 2)
34                         {
35                                 string original_cvar = argv(1);
36                                 string tmp_string = argv(2);
37                                 
38                                 if(cvar_string(original_cvar) == "") // cvar was empty
39                                 {
40                                         cvar_set(original_cvar, tmp_string);
41                                 }
42                                 else // add it to the end of the list if the list doesn't already have it
43                                 {
44                                         argc = tokenizebyseparator(cvar_string(original_cvar), " ");
45                                         
46                                         for(i = 0; i < argc; ++i)
47                                                 if(argv(i) == tmp_string)
48                                                         return; // already in list
49                                                         
50                                         cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
51                                 }
52                                 return;
53                         }
54                 }
55                         
56                 default:
57                         print("Incorrect parameters for ^2addtolist^7\n");
58                 case CMD_REQUEST_USAGE:
59                 {
60                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value"));
61                         print("  Where 'variable' is what to add 'value' to.\n");
62                         return;
63                 }
64         }
65 }
66
67 void GenericCommand_dumpcommands(float request)
68 {
69         switch(request)
70         {
71                 case CMD_REQUEST_COMMAND:
72                 {
73                         float fh;
74                         string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
75                         fh = fopen(filename, FILE_WRITE);
76                         
77                         if(fh >= 0)
78                         {
79                                 #ifdef SVQC
80                                         CMD_Write("dump of server console commands:\n");
81                                         GameCommand_macro_write_aliases(fh);
82                                         
83                                         CMD_Write("\ndump of networked client only commands:\n");
84                                         ClientCommand_macro_write_aliases(fh);
85                                         
86                                         CMD_Write("\ndump of common commands:\n");
87                                         CommonCommand_macro_write_aliases(fh);
88
89                                         CMD_Write("\ndump of ban commands:\n");
90                                         BanCommand_macro_write_aliases(fh);
91                                 #endif
92                                                                 
93                                 #ifdef CSQC
94                                         CMD_Write("dump of client commands:\n");
95                                         LocalCommand_macro_write_aliases(fh);
96                                 #endif
97                                 
98                                 CMD_Write("\ndump of generic commands:\n");
99                                 GenericCommand_macro_write_aliases(fh);
100                                 
101                                 print("Completed dump of aliases in ^2", GetProgramCommandPrefix(), "_dump.txt^7.\n");
102                                 
103                                 fclose(fh);
104                         }
105                         else
106                         {
107                                 print("^1Error: ^7Could not dump to file!\n");
108                         }
109                         return;
110                 }
111                         
112                 default:
113                 case CMD_REQUEST_USAGE:
114                 {
115                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
116                         print("  No arguments required.\n");
117                         return;
118                 }
119         }
120 }
121
122 void GenericCommand_maplist(float request, float argc)
123 {
124         switch(request)
125         {
126                 case CMD_REQUEST_COMMAND:
127                 {
128                         string tmp_string;
129                         float i;
130                         
131                         switch(argv(1))
132                         {
133                                 case "add": // appends new maps to the maplist
134                                 {
135                                         if(argc == 3)
136                                         {
137                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
138                                                 {
139                                                         print("maplist: ERROR: ", argv(2), " does not exist!\n");
140                                                         break;
141                                                 }
142                                                 
143                                                 if(cvar_string("g_maplist") == "")
144                                                         cvar_set("g_maplist", argv(2));
145                                                 else
146                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
147                                                         
148                                                 return;
149                                         }
150                                         break; // go to usage
151                                 }
152                                 
153                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
154                                 {
155                                         if(argc == 3)
156                                         {
157                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
158                                                 
159                                                 for(i = 0; i < argc; ++i)
160                                                         if(argv(i) != argv(2))
161                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
162                                                                 
163                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
164                                                 cvar_set("g_maplist", tmp_string);
165                                                 
166                                                 return;
167                                         }
168                                         break; // go to usage
169                                 }
170                                 
171                                 case "shuffle": // randomly shuffle the maplist
172                                 {
173                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
174                                         return;
175                                 }
176                                 
177                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
178                                 {
179                                         MapInfo_Enumerate();
180                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
181                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
182                                         
183                                         for(i = 0; i < argc; ++i)
184                                                 if(MapInfo_CheckMap(argv(i)))
185                                                         tmp_string = strcat(tmp_string, " ", argv(i));
186                                                         
187                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
188                                         cvar_set("g_maplist", tmp_string);
189                                         
190                                         return;
191                                 }
192                                         
193                                 default: break;
194                         }
195                 }
196                         
197                 default:
198                         print("Incorrect parameters for ^2maplist^7\n");
199                 case CMD_REQUEST_USAGE:
200                 {
201                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
202                         print("  No arguments required.\n");
203                         return;
204                 }
205         }
206 }
207
208 void GenericCommand_removefromlist(float request, float argc)
209 {
210         switch(request)
211         {
212                 case CMD_REQUEST_COMMAND:
213                 {
214                         if(argc == 3)
215                         {
216                                 float i;
217                                 string original_cvar = argv(1);
218                                 string removal = argv(2);
219                                 string tmp_string;
220                                 
221                                 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
222                                 
223                                 for(i = 0; i < argc; ++i)
224                                         if(argv(i) != removal)
225                                                 tmp_string = strcat(tmp_string, " ", argv(i));
226                                                 
227                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
228                                 cvar_set(original_cvar, tmp_string);
229                                 
230                                 return;
231                         }
232                 }
233                         
234                 default:
235                         print("Incorrect parameters for ^2removefromlist^7\n");
236                 case CMD_REQUEST_USAGE:
237                 {
238                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value"));
239                         print("  Where 'variable' is what cvar to remove 'value' from.\n");
240                         return;
241                 }
242         }
243 }
244
245 void GenericCommand_settemp(float request, float argc)
246 {
247         switch(request)
248         {
249                 case CMD_REQUEST_COMMAND:
250                 {
251                         if(argc >= 3)
252                         {
253                                 if(cvar_settemp(argv(1), argv(2)))
254                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
255                                 else
256                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
257                         
258                                 return;
259                         }
260                 }
261                         
262                 default:
263                         print("Incorrect parameters for ^2settemp^7\n");
264                 case CMD_REQUEST_USAGE:
265                 {
266                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
267                         print("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
268                         return;
269                 }
270         }
271 }
272
273 void GenericCommand_settemp_restore(float request, float argc)
274 {
275         switch(request)
276         {
277                 case CMD_REQUEST_COMMAND:
278                 {
279                         float i = cvar_settemp_restore();
280                         
281                         if(i)
282                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
283                         else
284                                 dprint("Nothing to restore.\n");
285                         
286                         return;
287                 }
288                         
289                 default:
290                 case CMD_REQUEST_USAGE:
291                 {
292                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
293                         print("  No arguments required.\n");
294                         return;
295                 }
296         }
297 }
298
299 /* use this when creating a new command, making sure to place it in alphabetical order... also,
300 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
301 void GenericCommand_(float request)
302 {
303         switch(request)
304         {
305                 case CMD_REQUEST_COMMAND:
306                 {
307                         
308                         return;
309                 }
310                         
311                 default:
312                 case CMD_REQUEST_USAGE:
313                 {
314                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
315                         print("  No arguments required.\n");
316                         return;
317                 }
318         }
319 }
320 */
321
322 // ==================================
323 //  Macro system for server commands
324 // ==================================
325
326 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
327 #define GENERIC_COMMANDS(request,arguments,command) \
328         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
329         GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
330         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
331         GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
332         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
333         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
334         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
335         /* nothing */
336
337 void GenericCommand_macro_help()
338 {
339         #define GENERIC_COMMAND(name,function,description) \
340                 { print("  ^2", name, "^7: ", description, "\n"); }
341                 
342         GENERIC_COMMANDS(0, 0, "")
343         #undef GENERIC_COMMAND
344         
345         return;
346 }
347
348 float GenericCommand_macro_command(float argc, string command)
349 {
350         #define GENERIC_COMMAND(name,function,description) \
351                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
352                 
353         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
354         #undef GENERIC_COMMAND
355         
356         return FALSE;
357 }
358
359 float GenericCommand_macro_usage(float argc)
360 {
361         #define GENERIC_COMMAND(name,function,description) \
362                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
363                 
364         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
365         #undef GENERIC_COMMAND
366         
367         return FALSE;
368 }
369
370 void GenericCommand_macro_write_aliases(float fh)
371 {
372         #define GENERIC_COMMAND(name,function,description) \
373                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
374         
375         GENERIC_COMMANDS(0, 0, "")
376         #undef GENERIC_COMMAND
377         
378         return;
379 }
380         
381
382 // ===========================================
383 //  Main Common Function For Generic Commands
384 // ===========================================
385 // Commands spread out among all programs (menu, client, and server) 
386
387 float GenericCommand(string command)
388 {
389         float argc = tokenize_console(command);
390         float n, j, f, i;
391         string s, s2, c;
392         vector rgb;
393
394         // Guide for working with argc arguments by example:
395         // argc:   1    - 2      - 3     - 4
396         // argv:   0    - 1      - 2     - 3 
397         // cmd     vote - master - login - password
398         
399         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
400         {
401                 return TRUE; // handled by one of the above GenericCommand_* functions
402         }
403         else if(argc >= 3 && argv(0) == "red")
404         {
405                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
406                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
407                 return TRUE;
408         }
409         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
410         {
411                 // other test case
412                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
413
414                 n = floor(random() * 6 + 2);
415
416                 s2 = "";
417                 for(i = 0; i < n; ++i)
418                 {
419                         s2 = strcat(s2, "AH");
420                 }
421
422                 if(random() < 0.1)
423                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
424
425                 if(s == "")
426                         s = s2;
427                 else
428                         if(random() < 0.8)
429                                 s = strcat(s, " ", s2);
430                         else
431                                 s = strcat(s2, " ", s);
432
433                 s2 = substring(s, strlen(s) - 2, 2);
434                 if(s2 == "AH" || s2 == "AY")
435                         s = strcat(s, "))");
436                 else
437                         s = strcat(s, " ))");
438
439                 if(random() < 0.1)
440                         s = substring(s, 0, strlen(s) - 1);
441
442                 if(random() < 0.1)
443                         s = strconv(1, 0, 0, s);
444
445                 localcmd(strcat(argv(1), " ", s));
446
447                 return TRUE;
448         }
449         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
450         {
451                 // test case for terencehill's color codes
452                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
453                 s2 = "";
454                 
455                 n = strlen(s);
456                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
457                 f = random() * 6;
458
459                 for(i = 0; i < n; ++i)
460                 {
461                         c = substring(s, i, 1);
462
463                         if(c == ";")
464                                 c = ":";
465                         else if(c == "^")
466                         {
467                                 c = "^^";
468                                 if(substring(s, i+1, 1) == "^")
469                                         ++i;
470                         }
471
472                         if(c != " ")
473                         {
474                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
475                                 c = strcat(rgb_to_hexcolor(rgb), c);
476                         }
477                         s2 = strcat(s2, c);
478                 }
479
480                 localcmd(strcat(argv(1), " ", s2));
481
482                 return TRUE;
483         }
484
485         return FALSE;
486 }