]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Add "removefromlist" command to strip something from a cvar
[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 void GenericCommand_addtolist(float request, float argc)
21 {
22         switch(request)
23         {
24                 case CMD_REQUEST_COMMAND:
25                 {
26                         float i;
27                         
28                         if(argc >= 2)
29                         {
30                                 string original_cvar = argv(1);
31                                 string tmp_string = argv(2);
32                                 
33                                 if(cvar_string(original_cvar) == "") // cvar was empty
34                                 {
35                                         cvar_set(original_cvar, tmp_string);
36                                 }
37                                 else // add it to the end of the list if the list doesn't already have it
38                                 {
39                                         argc = tokenizebyseparator(cvar_string(original_cvar), " ");
40                                         
41                                         for(i = 0; i < argc; ++i)
42                                                 if(argv(i) == tmp_string)
43                                                         return; // already in list
44                                                         
45                                         cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
46                                 }
47                                 return;
48                         }
49                 }
50                         
51                 default:
52                         // todo
53                 case CMD_REQUEST_USAGE:
54                 {
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.");
58                         return;
59                 }
60         }
61 }
62
63 void GenericCommand_dumpcommands(float request)
64 {
65         switch(request)
66         {
67                 case CMD_REQUEST_COMMAND:
68                 {
69                         float fh;
70                         string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
71                         fh = fopen(filename, FILE_WRITE);
72                         
73                         if(fh >= 0)
74                         {
75                                 #ifdef SVQC
76                                         
77                                         CMD_Write("dump of server console commands:\n");
78                                         GameCommand_macro_write_aliases(fh);
79                                         
80                                         CMD_Write("\ndump of networked client only commands:\n");
81                                         ClientCommand_macro_write_aliases(fh);
82                                         
83                                         CMD_Write("\ndump of common commands:\n");
84                                         CommonCommand_macro_write_aliases(fh);
85
86                                         CMD_Write("\ndump of ban commands:\n");
87                                         // todo
88                                         
89                                 #endif
90                                                                 
91                                 #ifdef CSQC
92                                         
93                                         CMD_Write("dump of client commands:\n");
94                                         LocalCommand_macro_write_aliases(fh);
95                                         
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                 case CMD_REQUEST_USAGE:
236                 {
237                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
238                         print("  No arguments required.\n");
239                         return;
240                 }
241         }
242 }
243
244 void GenericCommand_settemp(float request, float argc)
245 {
246         switch(request)
247         {
248                 case CMD_REQUEST_COMMAND:
249                 {
250                         if(argc >= 3)
251                         {
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"); 
254                                 else
255                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
256                         
257                                 return;
258                         }
259                 }
260                         
261                 default:
262                         print("Incorrect parameters for ^2settemp^7\n");
263                 case CMD_REQUEST_USAGE:
264                 {
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");
267                         return;
268                 }
269         }
270 }
271
272 void GenericCommand_settemp_restore(float request, float argc)
273 {
274         switch(request)
275         {
276                 case CMD_REQUEST_COMMAND:
277                 {
278                         float i = cvar_settemp_restore();
279                         
280                         if(i)
281                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
282                         else
283                                 dprint("Nothing to restore.\n");
284                         
285                         return;
286                 }
287                         
288                 default:
289                 case CMD_REQUEST_USAGE:
290                 {
291                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
292                         print("  No arguments required.\n");
293                         return;
294                 }
295         }
296 }
297
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)
301 {
302         switch(request)
303         {
304                 case CMD_REQUEST_COMMAND:
305                 {
306                         
307                         return;
308                 }
309                         
310                 default:
311                 case CMD_REQUEST_USAGE:
312                 {
313                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
314                         print("  No arguments required.\n");
315                         return;
316                 }
317         }
318 }
319 */
320
321 // ==================================
322 //  Macro system for server commands
323 // ==================================
324
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") \
334         /* nothing */
335
336 void GenericCommand_macro_help()
337 {
338         #define GENERIC_COMMAND(name,function,description) \
339                 { print("  ^2", name, "^7: ", description, "\n"); }
340                 
341         GENERIC_COMMANDS(0, 0, "")
342         #undef GENERIC_COMMAND
343         
344         return;
345 }
346
347 float GenericCommand_macro_command(float argc, string command)
348 {
349         #define GENERIC_COMMAND(name,function,description) \
350                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
351                 
352         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
353         #undef GENERIC_COMMAND
354         
355         return FALSE;
356 }
357
358 float GenericCommand_macro_usage(float argc)
359 {
360         #define GENERIC_COMMAND(name,function,description) \
361                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
362                 
363         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
364         #undef GENERIC_COMMAND
365         
366         return FALSE;
367 }
368
369 void GenericCommand_macro_write_aliases(float fh)
370 {
371         #define GENERIC_COMMAND(name,function,description) \
372                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
373         
374         GENERIC_COMMANDS(0, 0, "")
375         #undef GENERIC_COMMAND
376         
377         return;
378 }
379         
380
381 // ===========================================
382 //  Main Common Function For Generic Commands
383 // ===========================================
384 // Commands spread out among all programs (menu, client, and server) 
385
386 float GenericCommand(string command)
387 {
388         float argc = tokenize_console(command);
389         float n, j, f, i;
390         string s, s2, c;
391         vector rgb;
392
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
397         
398         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
399         {
400                 return TRUE; // handled by one of the above GenericCommand_* functions
401         }
402         else if(argc >= 3 && argv(0) == "red")
403         {
404                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
405                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
406                 return TRUE;
407         }
408         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
409         {
410                 // other test case
411                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
412
413                 n = floor(random() * 6 + 2);
414
415                 s2 = "";
416                 for(i = 0; i < n; ++i)
417                 {
418                         s2 = strcat(s2, "AH");
419                 }
420
421                 if(random() < 0.1)
422                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
423
424                 if(s == "")
425                         s = s2;
426                 else
427                         if(random() < 0.8)
428                                 s = strcat(s, " ", s2);
429                         else
430                                 s = strcat(s2, " ", s);
431
432                 s2 = substring(s, strlen(s) - 2, 2);
433                 if(s2 == "AH" || s2 == "AY")
434                         s = strcat(s, "))");
435                 else
436                         s = strcat(s, " ))");
437
438                 if(random() < 0.1)
439                         s = substring(s, 0, strlen(s) - 1);
440
441                 if(random() < 0.1)
442                         s = strconv(1, 0, 0, s);
443
444                 localcmd(strcat(argv(1), " ", s));
445
446                 return TRUE;
447         }
448         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
449         {
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)));
452                 s2 = "";
453                 
454                 n = strlen(s);
455                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
456                 f = random() * 6;
457
458                 for(i = 0; i < n; ++i)
459                 {
460                         c = substring(s, i, 1);
461
462                         if(c == ";")
463                                 c = ":";
464                         else if(c == "^")
465                         {
466                                 c = "^^";
467                                 if(substring(s, i+1, 1) == "^")
468                                         ++i;
469                         }
470
471                         if(c != " ")
472                         {
473                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
474                                 c = strcat(rgb_to_hexcolor(rgb), c);
475                         }
476                         s2 = strcat(s2, c);
477                 }
478
479                 localcmd(strcat(argv(1), " ", s2));
480
481                 return TRUE;
482         }
483
484         return FALSE;
485 }