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