]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Merge branch 'master' of git://de.git.xonotic.org/xonotic/xonotic-data.pk3dir
[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_nextframe(float request, float arguments, string command)
212 {
213         switch(request)
214         {
215                 case CMD_REQUEST_COMMAND:
216                 {
217                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
218                         return;
219                 }
220                         
221                 default:
222                 case CMD_REQUEST_USAGE:
223                 {
224                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command..."));
225                         print("  Where command will be executed next frame of this VM\n");
226                         return;
227                 }
228         }
229 }
230
231 void GenericCommand_removefromlist(float request, float argc)
232 {
233         switch(request)
234         {
235                 case CMD_REQUEST_COMMAND:
236                 {
237                         if(argc == 3)
238                         {
239                                 float i;
240                                 string original_cvar = argv(1);
241                                 string removal = argv(2);
242                                 string tmp_string;
243                                 
244                                 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
245                                 
246                                 for(i = 0; i < argc; ++i)
247                                         if(argv(i) != removal)
248                                                 tmp_string = strcat(tmp_string, " ", argv(i));
249                                                 
250                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
251                                 cvar_set(original_cvar, tmp_string);
252                                 
253                                 return;
254                         }
255                 }
256                         
257                 default:
258                         print("Incorrect parameters for ^2removefromlist^7\n");
259                 case CMD_REQUEST_USAGE:
260                 {
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");
264                         return;
265                 }
266         }
267 }
268
269 void GenericCommand_settemp(float request, float argc)
270 {
271         switch(request)
272         {
273                 case CMD_REQUEST_COMMAND:
274                 {
275                         if(argc >= 3)
276                         {
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"); 
279                                 else
280                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
281                         
282                                 return;
283                         }
284                 }
285                         
286                 default:
287                         print("Incorrect parameters for ^2settemp^7\n");
288                 case CMD_REQUEST_USAGE:
289                 {
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");
293                         return;
294                 }
295         }
296 }
297
298 void GenericCommand_settemp_restore(float request, float argc)
299 {
300         switch(request)
301         {
302                 case CMD_REQUEST_COMMAND:
303                 {
304                         float i = cvar_settemp_restore();
305                         
306                         if(i)
307                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
308                         else
309                                 dprint("Nothing to restore.\n");
310                         
311                         return;
312                 }
313                         
314                 default:
315                 case CMD_REQUEST_USAGE:
316                 {
317                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
318                         print("  No arguments required.\n");
319                         print("See also: ^2settemp^7\n");
320                         return;
321                 }
322         }
323 }
324
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)
328 {
329         switch(request)
330         {
331                 case CMD_REQUEST_COMMAND:
332                 {
333                         
334                         return;
335                 }
336                         
337                 default:
338                 case CMD_REQUEST_USAGE:
339                 {
340                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
341                         print("  No arguments required.\n");
342                         return;
343                 }
344         }
345 }
346 */
347
348 // ==================================
349 //  Macro system for server commands
350 // ==================================
351
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") \
362         /* nothing */
363
364 void GenericCommand_macro_help()
365 {
366         #define GENERIC_COMMAND(name,function,description) \
367                 { print("  ^2", name, "^7: ", description, "\n"); }
368                 
369         GENERIC_COMMANDS(0, 0, "")
370         #undef GENERIC_COMMAND
371         
372         return;
373 }
374
375 float GenericCommand_macro_command(float argc, string command)
376 {
377         #define GENERIC_COMMAND(name,function,description) \
378                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
379                 
380         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
381         #undef GENERIC_COMMAND
382         
383         return FALSE;
384 }
385
386 float GenericCommand_macro_usage(float argc)
387 {
388         #define GENERIC_COMMAND(name,function,description) \
389                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
390                 
391         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
392         #undef GENERIC_COMMAND
393         
394         return FALSE;
395 }
396
397 void GenericCommand_macro_write_aliases(float fh)
398 {
399         #define GENERIC_COMMAND(name,function,description) \
400                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
401         
402         GENERIC_COMMANDS(0, 0, "")
403         #undef GENERIC_COMMAND
404         
405         return;
406 }
407         
408
409 // ===========================================
410 //  Main Common Function For Generic Commands
411 // ===========================================
412 // Commands spread out among all programs (menu, client, and server) 
413
414 float GenericCommand(string command)
415 {
416         float argc = tokenize_console(command);
417         float n, j, f, i;
418         string s, s2, c;
419         vector rgb;
420
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
425         
426         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
427         {
428                 return TRUE; // handled by one of the above GenericCommand_* functions
429         }
430         else if(argc >= 3 && argv(0) == "red")
431         {
432                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
433                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
434                 return TRUE;
435         }
436         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
437         {
438                 // other test case
439                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
440
441                 n = floor(random() * 6 + 2);
442
443                 s2 = "";
444                 for(i = 0; i < n; ++i)
445                 {
446                         s2 = strcat(s2, "AH");
447                 }
448
449                 if(random() < 0.1)
450                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
451
452                 if(s == "")
453                         s = s2;
454                 else
455                         if(random() < 0.8)
456                                 s = strcat(s, " ", s2);
457                         else
458                                 s = strcat(s2, " ", s);
459
460                 s2 = substring(s, strlen(s) - 2, 2);
461                 if(s2 == "AH" || s2 == "AY")
462                         s = strcat(s, "))");
463                 else
464                         s = strcat(s, " ))");
465
466                 if(random() < 0.1)
467                         s = substring(s, 0, strlen(s) - 1);
468
469                 if(random() < 0.1)
470                         s = strconv(1, 0, 0, s);
471
472                 localcmd(strcat(argv(1), " ", s));
473
474                 return TRUE;
475         }
476         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
477         {
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)));
480                 s2 = "";
481                 
482                 n = strlen(s);
483                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
484                 f = random() * 6;
485
486                 for(i = 0; i < n; ++i)
487                 {
488                         c = substring(s, i, 1);
489
490                         if(c == ";")
491                                 c = ":";
492                         else if(c == "^")
493                         {
494                                 c = "^^";
495                                 if(substring(s, i+1, 1) == "^")
496                                         ++i;
497                         }
498
499                         if(c != " ")
500                         {
501                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
502                                 c = strcat(rgb_to_hexcolor(rgb), c);
503                         }
504                         s2 = strcat(s2, c);
505                 }
506
507                 localcmd(strcat(argv(1), " ", s2));
508
509                 return TRUE;
510         }
511
512         return FALSE;
513 }