]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Compatibility with 0.5 for voting, plus fix player/world entity for common commands
[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                                 if(cvar_string(argv(1)) == "") // cvar was empty
31                                 {
32                                         cvar_set(argv(1), argv(2));
33                                 }
34                                 else // add it to the end of the list if the list doesn't already have it
35                                 {
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
40                                                         
41                                         cvar_set(argv(1), strcat(argv(2), " ", cvar_string(argv(1))));
42                                 }
43                                 return;
44                         }
45                 }
46                         
47                 default:
48                         // todo
49                 case CMD_REQUEST_USAGE:
50                 {
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.");
54                         return;
55                 }
56         }
57 }
58
59 void GenericCommand_dumpcommands(float request)
60 {
61         switch(request)
62         {
63                 case CMD_REQUEST_COMMAND:
64                 {
65                         float fh;
66                         string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
67                         fh = fopen(filename, FILE_WRITE);
68                         
69                         if(fh >= 0)
70                         {
71 #ifdef SVQC
72                                 
73                                 // write sv_cmd aliases
74                                 CMD_Write("dump of server console commands:\n");
75                                 GameCommand_macro_write_aliases(fh);
76                                 
77                                 // write client only aliases
78                                 CMD_Write("\ndump of networked client only commands:\n");
79                                 ClientCommand_macro_write_aliases(fh);
80                                 
81                                 // write common aliases
82                                 CMD_Write("\ndump of common commands:\n");
83                                 CommonCommand_macro_write_aliases(fh);
84
85                                 // write ban aliases
86                                 CMD_Write("\ndump of ban commands:\n");
87                                 
88 #endif
89                                 
90 #ifdef CSQC
91                                 
92                                 // write cl_cmd aliases
93                                 CMD_Write("dump of client commands:\n");
94                                 LocalCommand_macro_write_aliases(fh);
95                                 
96 #endif
97                                 
98                                 // write generic commands
99                                 CMD_Write("\ndump of generic commands:\n");
100                                 GenericCommand_macro_write_aliases(fh);
101                                 
102                                 print("Completed dump of aliases in ^2", 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 "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
155                                 {
156                                         if(argc == 3)
157                                         {
158                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
159                                                 
160                                                 for(i = 0; i < argc; ++i)
161                                                         if(argv(i) != argv(2))
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                                         break; // go to usage
170                                 }
171                                 
172                                 case "shuffle": // randomly shuffle the maplist
173                                 {
174                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
175                                         return;
176                                 }
177                                 
178                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
179                                 {
180                                         MapInfo_Enumerate();
181                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
182                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
183                                         
184                                         for(i = 0; i < argc; ++i)
185                                                 if(MapInfo_CheckMap(argv(i)))
186                                                         tmp_string = strcat(tmp_string, " ", argv(i));
187                                                         
188                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
189                                         cvar_set("g_maplist", tmp_string);
190                                         
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 command [map]")); // todo
203                         print("  No arguments required.\n");
204                         return;
205                 }
206         }
207 }
208
209 void GenericCommand_settemp(float request, float argc)
210 {
211         switch(request)
212         {
213                 case CMD_REQUEST_COMMAND:
214                 {
215                         if(argc >= 3)
216                         {
217                                 if(cvar_settemp(argv(1), argv(2)))
218                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
219                                 else
220                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
221                         
222                                 return;
223                         }
224                 }
225                         
226                 default:
227                         print("Incorrect parameters for ^2settemp^7\n");
228                 case CMD_REQUEST_USAGE:
229                 {
230                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
231                         print("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
232                         return;
233                 }
234         }
235 }
236
237 void GenericCommand_settemp_restore(float request, float argc)
238 {
239         switch(request)
240         {
241                 case CMD_REQUEST_COMMAND:
242                 {
243                         float i = cvar_settemp_restore();
244                         
245                         if(i)
246                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
247                         else
248                                 dprint("Nothing to restore.\n");
249                         
250                         return;
251                 }
252                         
253                 default:
254                 case CMD_REQUEST_USAGE:
255                 {
256                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
257                         print("  No arguments required.\n");
258                         return;
259                 }
260         }
261 }
262
263 /* use this when creating a new command, making sure to place it in alphabetical order... also,
264 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
265 void GenericCommand_(float request)
266 {
267         switch(request)
268         {
269                 case CMD_REQUEST_COMMAND:
270                 {
271                         
272                         return;
273                 }
274                         
275                 default:
276                 case CMD_REQUEST_USAGE:
277                 {
278                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
279                         print("  No arguments required.\n");
280                         return;
281                 }
282         }
283 }
284 */
285
286 // ==================================
287 //  Macro system for server commands
288 // ==================================
289
290 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
291 #define GENERIC_COMMANDS(request,arguments,command) \
292         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
293         GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
294         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
295         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
296         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
297         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
298         /* nothing */
299
300 void GenericCommand_macro_help()
301 {
302         #define GENERIC_COMMAND(name,function,description) \
303                 { if(strtolower(description) != string_null) { print("  ^2", name, "^7: ", description, "\n"); } }
304                 
305         GENERIC_COMMANDS(0, 0, "")
306         #undef GENERIC_COMMAND
307         
308         return;
309 }
310
311 float GenericCommand_macro_command(float argc, string command)
312 {
313         #define GENERIC_COMMAND(name,function,description) \
314                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
315                 
316         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
317         #undef GENERIC_COMMAND
318         
319         return FALSE;
320 }
321
322 float GenericCommand_macro_usage(float argc)
323 {
324         #define GENERIC_COMMAND(name,function,description) \
325                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
326                 
327         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
328         #undef GENERIC_COMMAND
329         
330         return FALSE;
331 }
332
333 void GenericCommand_macro_write_aliases(float fh)
334 {
335         #define GENERIC_COMMAND(name,function,description) \
336                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
337         
338         GENERIC_COMMANDS(0, 0, "")
339         #undef GENERIC_COMMAND
340         
341         return;
342 }
343         
344
345 // ===========================================
346 //  Main Common Function For Generic Commands
347 // ===========================================
348 // Commands spread out among all programs (menu, client, and server) 
349
350 float GenericCommand(string command)
351 {
352         float argc = tokenize_console(command);
353         float n, j, f, i;
354         string s, s2, c;
355         vector rgb;
356
357         // Guide for working with argc arguments by example:
358         // argc:   1    - 2      - 3     - 4
359         // argv:   0    - 1      - 2     - 3 
360         // cmd     vote - master - login - password
361         
362         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
363         {
364                 return TRUE; // handled by one of the above GenericCommand_* functions
365         }
366         else if(argc >= 3 && argv(0) == "red")
367         {
368                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
369                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
370                 return TRUE;
371         }
372         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
373         {
374                 // other test case
375                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
376
377                 n = floor(random() * 6 + 2);
378
379                 s2 = "";
380                 for(i = 0; i < n; ++i)
381                 {
382                         s2 = strcat(s2, "AH");
383                 }
384
385                 if(random() < 0.1)
386                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
387
388                 if(s == "")
389                         s = s2;
390                 else
391                         if(random() < 0.8)
392                                 s = strcat(s, " ", s2);
393                         else
394                                 s = strcat(s2, " ", s);
395
396                 s2 = substring(s, strlen(s) - 2, 2);
397                 if(s2 == "AH" || s2 == "AY")
398                         s = strcat(s, "))");
399                 else
400                         s = strcat(s, " ))");
401
402                 if(random() < 0.1)
403                         s = substring(s, 0, strlen(s) - 1);
404
405                 if(random() < 0.1)
406                         s = strconv(1, 0, 0, s);
407
408                 localcmd(strcat(argv(1), " ", s));
409
410                 return TRUE;
411         }
412         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
413         {
414                 // test case for terencehill's color codes
415                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
416                 s2 = "";
417                 
418                 n = strlen(s);
419                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
420                 f = random() * 6;
421
422                 for(i = 0; i < n; ++i)
423                 {
424                         c = substring(s, i, 1);
425
426                         if(c == ";")
427                                 c = ":";
428                         else if(c == "^")
429                         {
430                                 c = "^^";
431                                 if(substring(s, i+1, 1) == "^")
432                                         ++i;
433                         }
434
435                         if(c != " ")
436                         {
437                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
438                                 c = strcat(rgb_to_hexcolor(rgb), c);
439                         }
440                         s2 = strcat(s2, c);
441                 }
442
443                 localcmd(strcat(argv(1), " ", s2));
444
445                 return TRUE;
446         }
447
448         return FALSE;
449 }