]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Move markup to its own file
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
1 // =========================================================
2 //  Generic program common command code, reworked 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_maplist(float request, float argc)
60 {
61         switch(request)
62         {
63                 case CMD_REQUEST_COMMAND:
64                 {
65                         string tmp_string;
66                         float i;
67                         
68                         switch(argv(1))
69                         {
70                                 case "add": // appends new maps to the maplist
71                                 {
72                                         if(argc == 3)
73                                         {
74                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
75                                                 {
76                                                         print("maplist: ERROR: ", argv(2), " does not exist!\n");
77                                                         break;
78                                                 }
79                                                 
80                                                 if(cvar_string("g_maplist") == "")
81                                                         cvar_set("g_maplist", argv(2));
82                                                 else
83                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
84                                                         
85                                                 return;
86                                         }
87                                         break; // go to usage
88                                 }
89                                 
90                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
91                                 {
92                                         if(argc == 3)
93                                         {
94                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
95                                                 
96                                                 for(i = 0; i < argc; ++i)
97                                                         if(argv(i) != argv(2))
98                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
99                                                                 
100                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
101                                                 cvar_set("g_maplist", tmp_string);
102                                                 
103                                                 return;
104                                         }
105                                         break; // go to usage
106                                 }
107                                 
108                                 case "shuffle": // randomly shuffle the maplist
109                                 {
110                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
111                                         return;
112                                 }
113                                 
114                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
115                                 {
116                                         MapInfo_Enumerate();
117                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
118                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
119                                         
120                                         for(i = 0; i < argc; ++i)
121                                                 if(MapInfo_CheckMap(argv(i)))
122                                                         tmp_string = strcat(tmp_string, " ", argv(i));
123                                                         
124                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
125                                         cvar_set("g_maplist", tmp_string);
126                                         
127                                         return;
128                                 }
129                                         
130                                 default: break;
131                         }
132                 }
133                         
134                 default:
135                 case CMD_REQUEST_USAGE:
136                 {
137                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist command [map]")); // todo
138                         print("  No arguments required.\n");
139                         return;
140                 }
141         }
142 }
143
144 void GenericCommand_settemp(float request, float argc)
145 {
146         switch(request)
147         {
148                 case CMD_REQUEST_COMMAND:
149                 {
150                         if(argc >= 3)
151                         {
152                                 if(cvar_settemp(argv(1), argv(2)))
153                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
154                                 else
155                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
156                         
157                                 return;
158                         }
159                 }
160                         
161                 default:
162                         print("Incorrect parameters for ^2settemp^7\n");
163                 case CMD_REQUEST_USAGE:
164                 {
165                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
166                         print("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
167                         return;
168                 }
169         }
170 }
171
172 void GenericCommand_settemp_restore(float request, float argc)
173 {
174         switch(request)
175         {
176                 case CMD_REQUEST_COMMAND:
177                 {
178                         float i = cvar_settemp_restore();
179                         
180                         if(i)
181                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
182                         else
183                                 dprint("Nothing to restore.\n");
184                         
185                         return;
186                 }
187                         
188                 default:
189                 case CMD_REQUEST_USAGE:
190                 {
191                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
192                         print("  No arguments required.\n");
193                         return;
194                 }
195         }
196 }
197
198 /* use this when creating a new command, making sure to place it in alphabetical order... also,
199 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
200 void GenericCommand_(float request)
201 {
202         switch(request)
203         {
204                 case CMD_REQUEST_COMMAND:
205                 {
206                         
207                         return;
208                 }
209                         
210                 default:
211                 case CMD_REQUEST_USAGE:
212                 {
213                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " ")));
214                         print("  No arguments required.\n");
215                         return;
216                 }
217         }
218 }
219 */
220
221 // ==================================
222 //  Macro system for server commands
223 // ==================================
224
225 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
226 #define GENERIC_COMMANDS(request,arguments,command) \
227         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar at the end of a list") \
228         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
229         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
230         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
231         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
232         /* nothing */
233
234 void GenericCommand_macro_help()
235 {
236         #define GENERIC_COMMAND(name,function,description) \
237                 { print("  ^2", name, "^7: ", description, "\n"); }
238                 
239         GENERIC_COMMANDS(0, 0, "")
240         #undef GENERIC_COMMAND
241         
242         return;
243 }
244
245 float GenericCommand_macro_command(float argc, string command)
246 {
247         #define GENERIC_COMMAND(name,function,description) \
248                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
249                 
250         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
251         #undef GENERIC_COMMAND
252         
253         return FALSE;
254 }
255
256 float GenericCommand_macro_usage(float argc)
257 {
258         #define GENERIC_COMMAND(name,function,description) \
259                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
260                 
261         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
262         #undef GENERIC_COMMAND
263         
264         return FALSE;
265 }
266         
267
268 // ===========================================
269 //  Main Common Function For Generic Commands
270 // ===========================================
271 // Commands spread out among all programs (menu, client, and server) 
272
273 float GenericCommand(string command)
274 {
275         float argc = tokenize_console(command);
276         float n, j, f, i;
277         string s, s2, c;
278         vector rgb;
279
280         // Guide for working with argc arguments by example:
281         // argc:   1    - 2      - 3     - 4
282         // argv:   0    - 1      - 2     - 3 
283         // cmd     vote - master - login - password
284         
285         if(strtolower(argv(0)) == "help") 
286         {
287                 if(argc == 1) 
288                 {
289                         GenericCommand_macro_help();
290                         return TRUE;
291                 } 
292                 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
293                 {
294                         return TRUE;
295                 }
296         } 
297         else if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
298         {
299                 return TRUE; // handled by one of the above GenericCommand_* functions
300         }
301         else if(argc >= 3 && argv(0) == "red")
302         {
303                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
304                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
305                 return TRUE;
306         }
307         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
308         {
309                 // other test case
310                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
311
312                 n = floor(random() * 6 + 2);
313
314                 s2 = "";
315                 for(i = 0; i < n; ++i)
316                 {
317                         s2 = strcat(s2, "AH");
318                 }
319
320                 if(random() < 0.1)
321                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
322
323                 if(s == "")
324                         s = s2;
325                 else
326                         if(random() < 0.8)
327                                 s = strcat(s, " ", s2);
328                         else
329                                 s = strcat(s2, " ", s);
330
331                 s2 = substring(s, strlen(s) - 2, 2);
332                 if(s2 == "AH" || s2 == "AY")
333                         s = strcat(s, "))");
334                 else
335                         s = strcat(s, " ))");
336
337                 if(random() < 0.1)
338                         s = substring(s, 0, strlen(s) - 1);
339
340                 if(random() < 0.1)
341                         s = strconv(1, 0, 0, s);
342
343                 localcmd(strcat(argv(1), " ", s));
344
345                 return TRUE;
346         }
347         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
348         {
349                 // test case for terencehill's color codes
350                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
351                 s2 = "";
352                 
353                 n = strlen(s);
354                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
355                 f = random() * 6;
356
357                 for(i = 0; i < n; ++i)
358                 {
359                         c = substring(s, i, 1);
360
361                         if(c == ";")
362                                 c = ":";
363                         else if(c == "^")
364                         {
365                                 c = "^^";
366                                 if(substring(s, i+1, 1) == "^")
367                                         ++i;
368                         }
369
370                         if(c != " ")
371                         {
372                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
373                                 c = strcat(rgb_to_hexcolor(rgb), c);
374                         }
375                         s2 = strcat(s2, c);
376                 }
377
378                 localcmd(strcat(argv(1), " ", s2));
379
380                 return TRUE;
381         }
382
383         return FALSE;
384 }