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