]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/command/cl_cmd.qc
4be39bf8b764ecbcd99d117ded9cd4b771f08372
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / command / cl_cmd.qc
1 // ==============================================
2 //  CSQC client commands code, written by Samual
3 //  Last updated: November 26th, 2011
4 // ==============================================
5
6         /*
7         if(cmd == "mv_download") {
8                 Cmd_MapVote_MapDownload(argc);
9         }
10         else if(cmd == "scoreboard_columns_set") {
11                 Cmd_HUD_SetFields(argc);
12         }
13         else if(cmd == "scoreboard_columns_help") {
14                 Cmd_HUD_Help(argc);
15         }
16         else if(cmd == "spawn") {
17                 s = argv(1);
18                 e = spawn();
19                 precache_model(s);
20                 setmodel(e, s);
21                 setorigin(e, view_origin);
22                 e.angles = view_angles;
23                 e.draw = DrawDebugModel;
24                 e.classname = "debugmodel";
25         }
26     else if(cmd == "vyes")
27     {
28         if(uid2name_dialog)
29         {
30             vote_active = 0; // force the panel to disappear right as we have selected the value (to prevent it from fading out in the normal vote panel pos)
31             vote_prev = 0;
32             localcmd("setreport cl_allow_uid2name 1\n");
33             vote_change = -9999;
34                         uid2name_dialog = 0;
35         }
36         else
37         {
38             localcmd("cmd vote yes\n");
39         }
40     }
41     else if(cmd == "vno")
42     {
43         if(uid2name_dialog)
44         {
45             vote_active = 0;
46             vote_prev = 0;
47             localcmd("setreport cl_allow_uid2name 0\n");
48             vote_change = -9999;
49                         uid2name_dialog = 0;
50         }
51         else
52         {
53             localcmd("cmd vote no\n");
54         }
55     }
56         */
57
58 // ============================
59 //  Misc. Supporting Functions
60 // ============================
61
62 float cvar_clientsettemp(string tmp_cvar, string value)
63 {
64         entity e;
65         
66         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
67                 if(e.netname == tmp_cvar)
68                         goto saved;
69                         
70         // creating a new entity to keep track of this cvar
71         e = spawn();
72         e.classname = "saved_cvar_value";
73         e.netname = strzone(tmp_cvar);
74         e.message = strzone(cvar_string(tmp_cvar));
75         return TRUE;
76         
77         // an entity for this cvar already exists, update the value
78         :saved
79         cvar_set(tmp_cvar, value);
80         return FALSE;
81 }
82
83 float cvar_clientsettemp_restore()
84 {
85         float i;
86         entity e;
87         
88         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
89                 { cvar_set(e.netname, e.message); ++i; } 
90                 
91         return i;
92 }
93
94 void DrawDebugModel()
95 {
96         if(time - floor(time) > 0.5)
97         {
98                 PolyDrawModel(self);
99                 self.drawmask = 0;
100         }
101         else
102         {
103                 self.renderflags = 0;
104                 self.drawmask = MASK_NORMAL;
105         }
106 }
107
108
109 // =======================
110 //  Command Sub-Functions
111 // =======================
112
113 void GameCommand_blurtest(float request)
114 {
115         // Simple command to work with postprocessing temporarily... possibly completely pointless, the glsl shader is used for a real feature now...
116         // Anyway, to enable it, just compile the client with -DBLURTEST and then you can use the command.
117         
118         #ifdef BLURTEST
119         switch(request)
120         {
121                 case CMD_REQUEST_COMMAND:
122                 {
123                         blurtest_time0 = time;
124                         blurtest_time1 = time + stof(argv(1));
125                         blurtest_radius = stof(argv(2));
126                         blurtest_power = stof(argv(3));
127                         print("Enabled blurtest\n");
128                         return; 
129                 }
130                         
131                 default:
132                 case CMD_REQUEST_USAGE:
133                 {
134                         print("\nUsage:^3 cl_cmd blurtest\n");
135                         print("  No arguments required.\n");
136                         return;
137                 }
138         }
139         #else
140         if(request)
141         {
142                 print("Blurtest is not enabled on this client.\n");
143                 return;
144         }
145         #endif
146 }
147
148 void GameCommand_hud(float request, float argc) // TODO: Add aliases in commands.cfg
149 {
150         switch(request)
151         {
152                 case CMD_REQUEST_COMMAND:
153                 {
154                         switch(argv(1))
155                         {
156                                 case "configure":
157                                 {
158                                         cvar_set("_hud_configure", ftos(!autocvar__hud_configure));
159                                         return;
160                                 }
161                                 
162                                 case "save":
163                                 {
164                                         if(argv(2))
165                                         {
166                                                 HUD_Panel_ExportCfg(argv(2));
167                                                 return;
168                                         }
169                                         else
170                                         {
171                                                 break; // go to usage, we're missing the paramater needed here.
172                                         }
173                                 }
174                                 
175                                 case "radar":
176                                 {
177                                         if(argv(2))
178                                                 hud_panel_radar_maximized = (stof(argv(2)) != 0);
179                                         else
180                                                 hud_panel_radar_maximized = !hud_panel_radar_maximized;
181                                         
182                                         return;
183                                 }
184                                 
185                                 case "scoreboard_columns_set":
186                                 {
187                                         Cmd_HUD_SetFields(argc); // todo update this function
188                                         
189                                         return;
190                                 }
191
192                                 case "scoreboard_columns_help":
193                                 {
194                                         Cmd_HUD_Help(argc); // todo update this function
195                                         
196                                         return;
197                                 }
198                         }
199                         return; 
200                 }
201                         
202                 default:
203                         print("Incorrect parameters for ^2hud^7\n");
204                 case CMD_REQUEST_USAGE:
205                 {
206                         print("\nUsage:^3 cl_cmd hud action [configname | radartoggle]\n");
207                         print("  Where 'action' is the command to complete,\n");
208                         print("  'configname' is the name to save to for \"save\" action,\n");
209                         print("  and 'radartoggle' is to control hud_panel_radar_maximized for \"radar\" action.\n");
210                         print("  Full list of commands here: \"configure, save, radar.\"\n");
211                         return;
212                 }
213         }
214 }
215
216 void GameCommand_sendcvar(float request, float argc)
217 {
218         switch(request)
219         {
220                 case CMD_REQUEST_COMMAND:
221                 {
222                         // W_FixWeaponOrder will trash argv, so save what we need.
223                         string thiscvar = strzone(argv(1));
224                         string s = cvar_string(thiscvar);
225                         
226                         if(thiscvar == "cl_weaponpriority")
227                                 s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
228                         else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
229                                 s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
230                                 
231                         localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
232                         strunzone(thiscvar);
233                         
234                         return; 
235                 }
236                         
237                 default:
238                 case CMD_REQUEST_USAGE:
239                 {
240                         print("\nUsage:^3 cl_cmd sendcvar <cvar>\n");
241                         print("  Where 'cvar' is the cvar plus arguments to send to the server.\n");
242                         return;
243                 }
244         }
245 }
246
247 void GameCommand_settemp(float request, float argc)
248 {
249         switch(request)
250         {
251                 case GC_REQUEST_COMMAND:
252                 {
253                         if((argv(1) == "restore") && (argc == 3))
254                         {
255                                 float i = cvar_clientsettemp_restore();
256                                 
257                                 if(i)
258                                         dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
259                                 else
260                                         dprint("Nothing to restore.\n");
261                         }
262                         else
263                         {
264                                 if(cvar_clientsettemp(argv(1), argv(2)))
265                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
266                                 else
267                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
268                         }
269                                 
270                         return; 
271                 }
272                         
273                 default:
274                 case GC_REQUEST_USAGE:
275                 {
276                         print("\nUsage:^3 cl_cmd settemp <cvar> | [restore]\n");
277                         print("  Where 'cvar' is the cvar plus arguments to send to the server,\n");
278                         print("  or 'restore' allows you to restore all of the original temporary cvar values.\n");
279                         return;
280                 }
281         }
282 }
283
284 /* use this when creating a new command, making sure to place it in alphabetical order.
285 void GameCommand_(float request)
286 {
287         switch(request)
288         {
289                 case CMD_REQUEST_COMMAND:
290                 {
291                         
292                         return; 
293                 }
294                         
295                 default:
296                 case CMD_REQUEST_USAGE:
297                 {
298                         print("\nUsage:^3 cl_cmd \n");
299                         print("  No arguments required.\n");
300                         return;
301                 }
302         }
303 }
304 */
305
306
307 // ==================================
308 //  Macro system for client commands
309 // ==================================
310
311 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
312 #define CLIENT_COMMANDS(request,arguments) \
313         CLIENT_COMMAND("blurtest", GameCommand_blurtest(request), "Feature for testing blur postprocessing") \
314         CLIENT_COMMAND("hud", GameCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
315         CLIENT_COMMAND("sendcvar", GameCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
316         CLIENT_COMMAND("settemp", GameCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored by command or end of each match") \
317         /* nothing */
318         
319 void GameCommand_macro_help()
320 {
321         #define CLIENT_COMMAND(name,function,description) \
322                 { print("  ^2", name, "^7: ", description, "\n"); }
323                 
324         CLIENT_COMMANDS(0, 0)
325         #undef CLIENT_COMMAND
326         
327         return;
328 }
329
330 float GameCommand_macro_command(float argc)
331 {
332         #define CLIENT_COMMAND(name,function,description) \
333                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
334                 
335         CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc)
336         #undef CLIENT_COMMAND
337         
338         return FALSE;
339 }
340
341 float GameCommand_macro_usage(float argc)
342 {
343         #define CLIENT_COMMAND(name,function,description) \
344                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
345                 
346         CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc)
347         #undef CLIENT_COMMAND
348         
349         return FALSE;
350 }
351
352
353 // =========================================
354 //  Main Function Called By Engine (cl_cmd)
355 // =========================================
356 // If this function exists, client code handles gamecommand instead of the engine code.
357
358 void GameCommand(string command)
359 {
360         float argc = tokenize_console(command);
361
362         if(strtolower(argv(0)) == "help") 
363         {
364                 if(argc == 1) 
365                 {
366                         print("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are:\n");
367                         GameCommand_macro_help();
368                         GameCommand_Generic("help");
369                         print("For help about specific commands, type cl_cmd help COMMAND\n");
370                         return;
371                 } 
372                 else if(GameCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
373                 {
374                         return;
375                 }
376         } 
377         else if(GameCommand_Generic(command)) 
378         {
379                 return; // handled by common/gamecommand.qc
380         }
381         else if(GameCommand_macro_command(argc)) // continue as usual and scan for normal commands
382         {
383                 return; // handled by one of the above GameCommand_* functions
384         }
385         
386         // nothing above caught the command, must be invalid
387         print("Unknown client command", ((command != "") ? strcat(" \"", command, "\"") : ""), ". For a list of supported commands, try cl_cmd help.\n");
388         
389         return;
390 }
391
392
393 // ===================================
394 //  Macro system for console commands
395 // ===================================
396
397 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
398 // Please add client commands to the function above this, as this is only for special reasons.
399 #define CONSOLE_COMMANDS_NORMAL \
400         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = TRUE; }) \
401         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = FALSE; }) \
402         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = TRUE; }) \
403         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = FALSE; }) \
404         /* nothing */
405         
406 #define CONSOLE_COMMANDS_MOVEMENT \
407         CONSOLE_COMMAND("+forward", { ++camera_direction_x; }) \
408         CONSOLE_COMMAND("-forward", { --camera_direction_x; }) \
409         CONSOLE_COMMAND("+back", { --camera_direction_x; }) \
410         CONSOLE_COMMAND("-back", { ++camera_direction_x; }) \
411         CONSOLE_COMMAND("+moveup", { ++camera_direction_z; }) \
412         CONSOLE_COMMAND("-moveup", { --camera_direction_z; }) \
413         CONSOLE_COMMAND("+movedown", { --camera_direction_z; }) \
414         CONSOLE_COMMAND("-movedown", { ++camera_direction_z; }) \
415         CONSOLE_COMMAND("+moveright", { --camera_direction_y; }) \
416         CONSOLE_COMMAND("-moveright", { ++camera_direction_y; }) \
417         CONSOLE_COMMAND("+moveleft", { ++camera_direction_y; }) \
418         CONSOLE_COMMAND("-moveleft", { --camera_direction_y; }) \
419         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
420         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
421         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
422         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
423         /* nothing */
424
425 void ConsoleCommand_macro_init()
426 {
427         // first init normal commands
428         #define CONSOLE_COMMAND(name,execution) \
429                 { registercommand(name); }
430
431         CONSOLE_COMMANDS_NORMAL
432         #undef CONSOLE_COMMAND
433         
434         // then init movement commands
435         #ifndef CAMERATEST
436         if(isdemo())
437         {
438         #endif
439                 #define CONSOLE_COMMAND(name,execution) \
440                         { registercommand(name); }
441
442                 CONSOLE_COMMANDS_MOVEMENT
443                 #undef CONSOLE_COMMAND
444         #ifndef CAMERATEST
445         }
446         #endif
447         
448         return;
449 }
450
451 float ConsoleCommand_macro_normal(float argc)
452 {
453         #define CONSOLE_COMMAND(name,execution) \
454                 { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
455                 
456         CONSOLE_COMMANDS_NORMAL
457         #undef CONSOLE_COMMAND
458         
459         return FALSE;
460 }
461
462 float ConsoleCommand_macro_movement(float argc)
463 {
464         if(camera_active)
465         {
466                 #define CONSOLE_COMMAND(name,execution) \
467                         { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
468
469                 CONSOLE_COMMANDS_MOVEMENT
470                 #undef CONSOLE_COMMAND
471         }
472         
473         return FALSE;
474 }
475
476
477 // ======================================================
478 //  Main Function Called By Engine (registered commands)
479 // ======================================================
480 // Used to parse commands in the console that have been registered with the "registercommand" function
481
482 float CSQC_ConsoleCommand(string command)
483 {
484         float argc = tokenize_console(command);
485
486         if(ConsoleCommand_macro_normal(argc))
487         {
488                 return TRUE;
489         }
490         else if(ConsoleCommand_macro_movement(argc))
491         {
492                 return TRUE;
493         }
494         
495         // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
496
497         return FALSE;
498 }