]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/commands/cl_cmd.qc
Automatically get the command name in the "Incorrect parameters ..." messages
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / commands / cl_cmd.qc
1 #include "cl_cmd.qh"
2 // ==============================================
3 //  CSQC client commands code, written by Samual
4 //  Last updated: December 28th, 2011
5 // ==============================================
6
7 #include <common/command/_mod.qh>
8 #include "cl_cmd.qh"
9
10 #include "../autocvars.qh"
11 #include "../defs.qh"
12 #include <client/hud/_mod.qh>
13 #include <client/hud/panel/quickmenu.qh>
14 #include <client/hud/panel/radar.qh>
15 #include "../main.qh"
16 #include "../mapvoting.qh"
17 #include "../miscfunctions.qh"
18
19 #include <client/mutators/_mod.qh>
20
21 #include <common/minigames/cl_minigames_hud.qh>
22
23 #include <common/mapinfo.qh>
24
25 void DrawDebugModel(entity this)
26 {
27         if (time - floor(time) > 0.5)
28         {
29                 PolyDrawModel(this);
30                 this.drawmask = 0;
31         }
32         else
33         {
34                 this.renderflags = 0;
35                 this.drawmask = MASK_NORMAL;
36         }
37 }
38
39
40 // =======================
41 //  Command Sub-Functions
42 // =======================
43
44 void LocalCommand_blurtest(int request)
45 {
46         TC(int, request);
47         // Simple command to work with postprocessing temporarily... possibly completely pointless, the glsl shader is used for a real feature now...
48         // Anyway, to enable it, just compile the client with -DBLURTEST and then you can use the command.
49
50         #ifdef BLURTEST
51         switch (request)
52         {
53                 case CMD_REQUEST_COMMAND:
54                 {
55                         blurtest_time0 = time;
56                         blurtest_time1 = time + stof(argv(1));
57                         blurtest_radius = stof(argv(2));
58                         blurtest_power = stof(argv(3));
59                         LOG_INFO("Enabled blurtest");
60                         return;
61                 }
62
63                 default:
64                 case CMD_REQUEST_USAGE:
65                 {
66                         LOG_HELP("Usage:^3 cl_cmd blurtest");
67                         LOG_HELP("  No arguments required.");
68                         return;
69                 }
70         }
71         #else
72         if (request)
73         {
74                 LOG_INFO("Blurtest is not enabled on this client.");
75                 return;
76         }
77         #endif
78 }
79
80 void LocalCommand_boxparticles(int request, int argc)
81 {
82         TC(int, request); TC(int, argc);
83         switch (request)
84         {
85                 case CMD_REQUEST_COMMAND:
86                 {
87                         if (argc == 9)
88                         {
89                                 int effect = _particleeffectnum(argv(1));
90                                 if (effect >= 0)
91                                 {
92                                         int index = stoi(argv(2));
93                                         entity own;
94                                         if (index <= 0)
95                                                 own = entitybyindex(-index);
96                                         else
97                                                 own = findfloat(NULL, entnum, index);
98                                         vector org_from = stov(argv(3));
99                                         vector org_to = stov(argv(4));
100                                         vector dir_from = stov(argv(5));
101                                         vector dir_to = stov(argv(6));
102                                         int countmultiplier = stoi(argv(7));
103                                         int flags = stoi(argv(8));
104                                         boxparticles(effect, own, org_from, org_to, dir_from, dir_to, countmultiplier, flags);
105                                         return;
106                                 }
107                         }
108                 }
109
110                 default:
111                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
112                 case CMD_REQUEST_USAGE:
113                 {
114                         LOG_HELP(
115                                 "Usage:^3 cl_cmd boxparticles effectname own org_from org_to, dir_from, dir_to, countmultiplier, flags\n"
116                                 "  'effectname' is the name of a particle effect in effectinfo.txt\n"
117                                 "  'own' is the entity number of the owner (negative for csqc ent, positive for svqc ent)\n"
118                                 "  'org_from' is the starting origin of the box\n"
119                                 "  'org_to' is the ending origin of the box\n"
120                                 "  'dir_from' is the minimum velocity\n"
121                                 "  'dir_to' is the maximum velocity\n"
122                                 "  'countmultiplier' defines a multiplier for the particle count (affects count only, not countabsolute or trailspacing)\n"
123                                 "  'flags' can contain:\n"
124                                 "    1 to respect globals particles_alphamin, particles_alphamax (set right before via prvm_globalset client)\n"
125                                 "    2 to respect globals particles_colormin, particles_colormax (set right before via prvm_globalset client)\n"
126                                 "    4 to respect globals particles_fade (set right before via prvm_globalset client)\n"
127                                 "    128 to draw a trail, not a box"
128                         );
129                         return;
130                 }
131         }
132 }
133
134 void LocalCommand_create_scrshot_ent(int request)
135 {
136         TC(int, request);
137         switch (request)
138         {
139                 case CMD_REQUEST_COMMAND:
140                 {
141                         string path = ((argv(1) == "") ? "" : strcat(argv(1), "/"));
142                         string filename = strcat(path, MapInfo_Map_bspname, "_scrshot_ent.txt");
143                         int fh = fopen(filename, FILE_APPEND);
144
145                         if (fh >= 0)
146                         {
147                                 fputs(fh, "{\n");
148                                 fputs(fh, strcat("\"classname\" \"info_autoscreenshot\"\n"));
149                                 fputs(fh, strcat("\"origin\" \"", strcat(ftos(view_origin.x), " ", ftos(view_origin.y), " ", ftos(view_origin.z)), "\"\n"));
150                                 fputs(fh, strcat("\"angles\" \"", strcat(ftos(view_angles.x), " ", ftos(view_angles.y), " ", ftos(view_angles.z)), "\"\n"));
151                                 fputs(fh, "}\n");
152
153                                 LOG_INFO("Completed screenshot entity dump in ^2data/data/", path, MapInfo_Map_bspname, "_scrshot_ent.txt^7.");
154
155                                 fclose(fh);
156                         }
157                         else
158                         {
159                                 LOG_INFO("^1Error: ^7Could not dump to file!");
160                         }
161                         return;
162                 }
163
164                 default:
165                 case CMD_REQUEST_USAGE:
166                 {
167                         LOG_HELP("Usage:^3 cl_cmd create_scrshot_ent [path]");
168                         LOG_HELP("  Where 'path' can be the subdirectory of data/data in which the file is saved.");
169                         return;
170                 }
171         }
172 }
173
174 void LocalCommand_debugmodel(int request, int argc)
175 {
176         TC(int, request); TC(int, argc);
177         switch (request)
178         {
179                 case CMD_REQUEST_COMMAND:
180                 {
181                         string modelname = argv(1);
182
183                         entity debugmodel_entity = new(debugmodel);
184                         precache_model(modelname);
185                         _setmodel(debugmodel_entity, modelname);
186                         setorigin(debugmodel_entity, view_origin);
187                         debugmodel_entity.angles = view_angles;
188                         debugmodel_entity.draw = DrawDebugModel;
189                         IL_PUSH(g_drawables, debugmodel_entity);
190
191                         return;
192                 }
193
194                 default:
195                 case CMD_REQUEST_USAGE:
196                 {
197                         LOG_HELP("Usage:^3 cl_cmd debugmodel model");
198                         LOG_HELP("  Where 'model' is a string of the model name to use for the debug model.");
199                         return;
200                 }
201         }
202 }
203
204 void LocalCommand_handlevote(int request, int argc)
205 {
206         TC(int, request); TC(int, argc);
207         switch (request)
208         {
209                 case CMD_REQUEST_COMMAND:
210                 {
211                         int vote_selection;
212                         string vote_string;
213
214                         if (InterpretBoolean(argv(1)))
215                         {
216                                 vote_selection = 2;
217                                 vote_string = "yes";
218                         }
219                         else
220                         {
221                                 vote_selection = 1;
222                                 vote_string = "no";
223                         }
224
225                         if (vote_selection)
226                         {
227                                 if (uid2name_dialog)  // handled by "uid2name" option
228                                 {
229                                         vote_active = 0;
230                                         vote_prev = 0;
231                                         vote_change = -9999;
232                                         localcmd(strcat("setreport cl_allow_uid2name ", ftos(vote_selection - 1), "\n"));
233                                         uid2name_dialog = 0;
234                                 }
235                                 else { localcmd(strcat("cmd vote ", vote_string, "\n")); }
236
237                                 return;
238                         }
239                 }
240
241                 default:
242                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
243                 case CMD_REQUEST_USAGE:
244                 {
245                         LOG_HELP("Usage:^3 cl_cmd handlevote vote");
246                         LOG_HELP("  Where 'vote' is the selection for either the current poll or uid2name.");
247                         return;
248                 }
249         }
250 }
251
252 void LocalCommand_hud(int request, int argc)
253 {
254         TC(int, request); TC(int, argc);
255         switch (request)
256         {
257                 case CMD_REQUEST_COMMAND:
258                 {
259                         if(MUTATOR_CALLHOOK(HUD_Command, argc))
260                                 return;
261
262                         switch (argv(1))
263                         {
264                                 case "configure":
265                                 {
266                                         cvar_set("_hud_configure", ftos(!autocvar__hud_configure));
267                                         return;
268                                 }
269
270                                 case "quickmenu":
271                                 {
272                                         if (argv(2) == "help")
273                                         {
274                                                 LOG_HELP(" quickmenu [[default | file | \"\"] submenu file]");
275                                                 LOG_HELP("Called without options (or with \"\") loads either the default quickmenu or a quickmenu file if hud_panel_quickmenu_file is set to a valid filename.");
276                                                 LOG_HELP("A submenu name can be given to open the quickmenu directly in a submenu; it requires to specify 'default', 'file' or '\"\"' option.");
277                                                 LOG_HELP("A file name can also be given to open a different quickmenu");
278                                                 return;
279                                         }
280                                         string file = ((argv(4) == "") ? autocvar_hud_panel_quickmenu_file : argv(4));
281                                         if (QuickMenu_IsOpened())
282                                                 QuickMenu_Close();
283                                         else
284                                                 QuickMenu_Open(argv(2), argv(3), file);  // mode, submenu
285                                         return;
286                                 }
287
288                                 case "save":
289                                 {
290                                         if (argv(2))
291                                         {
292                                                 HUD_Panel_ExportCfg(argv(2));
293                                                 return;
294                                         }
295                                         else
296                                         {
297                                                 break;  // go to usage, we're missing the paramater needed here.
298                                         }
299                                 }
300
301                                 case "scoreboard_columns_set":
302                                 {
303                                         Cmd_Scoreboard_SetFields(argc);
304                                         return;
305                                 }
306
307                                 case "scoreboard_columns_help":
308                                 {
309                                         Cmd_Scoreboard_Help();
310                                         return;
311                                 }
312
313                                 case "radar":
314                                 {
315                                         if (argv(2))
316                                                 HUD_Radar_Show_Maximized(InterpretBoolean(argv(2)), 0);
317                                         else
318                                                 HUD_Radar_Show_Maximized(!hud_panel_radar_maximized, 0);
319                                         return;
320                                 }
321
322                                 case "clickradar":
323                                 {
324                                         if(!isdemo())
325                                                 HUD_Radar_Show_Maximized(!hud_panel_radar_mouse, 1);
326                                         return;
327                                 }
328                         }
329                 }
330
331                 default:
332                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
333                 case CMD_REQUEST_USAGE:
334                 {
335                         LOG_HELP("Usage:^3 cl_cmd hud action [configname | radartoggle | layout]");
336                         LOG_HELP("  Where 'action' is the command to complete,");
337                         LOG_HELP("  'configname' is the name to save to for \"save\" action,");
338                         LOG_HELP("  'radartoggle' is to control hud_panel_radar_maximized for \"radar\" action,");
339                         LOG_HELP("  and 'layout' is how to organize the scoreboard columns for the set action.");
340                         LOG_HELP("  Full list of commands here: \"configure, quickmenu, minigame, save, scoreboard_columns_help, scoreboard_columns_set, radar.\"");
341                         return;
342                 }
343         }
344 }
345
346 void LocalCommand_localprint(int request, int argc)
347 {
348         TC(int, request); TC(int, argc);
349         switch (request)
350         {
351                 case CMD_REQUEST_COMMAND:
352                 {
353                         if (argv(1))
354                         {
355                                 centerprint_AddStandard(argv(1));
356                                 return;
357                         }
358                 }
359
360                 default:
361                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
362                 case CMD_REQUEST_USAGE:
363                 {
364                         LOG_HELP("Usage:^3 cl_cmd localprint \"message\"");
365                         LOG_HELP("  'message' is the centerprint message to send to yourself.");
366                         return;
367                 }
368         }
369 }
370
371 void LocalCommand_mv_download(int request, int argc)
372 {
373         TC(int, request); TC(int, argc);
374         switch (request)
375         {
376                 case CMD_REQUEST_COMMAND:
377                 {
378                         if (argv(1))
379                         {
380                                 Cmd_MapVote_MapDownload(argc);
381                                 return;
382                         }
383                 }
384
385                 default:
386                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
387                 case CMD_REQUEST_USAGE:
388                 {
389                         LOG_HELP("Usage:^3 cl_cmd mv_download mapid");
390                         LOG_HELP("  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.");
391                         return;
392                 }
393         }
394 }
395
396 void LocalCommand_sendcvar(int request, int argc)
397 {
398         TC(int, request); TC(int, argc);
399         switch (request)
400         {
401                 case CMD_REQUEST_COMMAND:
402                 {
403                         if (argv(1))
404                         {
405                                 // W_FixWeaponOrder will trash argv, so save what we need.
406                                 string thiscvar = string_null; strcpy(thiscvar, argv(1));
407                                 string s = cvar_string(thiscvar);
408
409                                 if (thiscvar == "cl_weaponpriority")
410                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
411                                 else if (substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
412                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
413
414                                 localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
415                                 strfree(thiscvar);
416                                 return;
417                         }
418                 }
419
420                 default:
421                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
422                 case CMD_REQUEST_USAGE:
423                 {
424                         LOG_HELP("Usage:^3 cl_cmd sendcvar <cvar>");
425                         LOG_HELP("  Where 'cvar' is the cvar to send to the server.");
426                         return;
427                 }
428         }
429 }
430
431 /* use this when creating a new command, making sure to place it in alphabetical order... also,
432 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
433 void LocalCommand_(int request)
434 {
435     switch(request)
436     {
437         case CMD_REQUEST_COMMAND:
438         {
439
440             return;
441         }
442
443         default:
444         case CMD_REQUEST_USAGE:
445         {
446             LOG_HELP("Usage:^3 cl_cmd ");
447             LOG_HELP("  No arguments required.");
448             return;
449         }
450     }
451 }
452 */
453
454
455 // ==================================
456 //  Macro system for client commands
457 // ==================================
458
459 // Normally do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
460 CLIENT_COMMAND(blurtest, "Feature for testing blur postprocessing") { LocalCommand_blurtest(request); }
461 CLIENT_COMMAND(boxparticles, "Spawn particles manually") { LocalCommand_boxparticles(request, arguments); }
462 CLIENT_COMMAND(create_scrshot_ent, "Create an entity at this location for automatic screenshots") { LocalCommand_create_scrshot_ent(request); }
463 CLIENT_COMMAND(debugmodel, "Spawn a debug model manually") { LocalCommand_debugmodel(request, arguments); }
464 CLIENT_COMMAND(handlevote, "System to handle selecting a vote or option") { LocalCommand_handlevote(request, arguments); }
465 CLIENT_COMMAND(hud, "Commands regarding/controlling the HUD system") { LocalCommand_hud(request, arguments); }
466 CLIENT_COMMAND(localprint, "Create your own centerprint sent to yourself") { LocalCommand_localprint(request, arguments); }
467 CLIENT_COMMAND(mv_download, "Retrieve mapshot picture from the server") { LocalCommand_mv_download(request, arguments); }
468 CLIENT_COMMAND(sendcvar, "Send a cvar to the server (like cl_weaponpriority)") { LocalCommand_sendcvar(request, arguments); }
469
470 void LocalCommand_macro_help()
471 {
472         FOREACH(CLIENT_COMMANDS, true, LOG_HELPF("  ^2%s^7: %s", it.m_name, it.m_description));
473 }
474
475 bool LocalCommand_macro_command(int argc, string command)
476 {
477         string c = strtolower(argv(0));
478         FOREACH(CLIENT_COMMANDS, it.m_name == c, {
479                 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
480                 return true;
481         });
482         return false;
483 }
484
485 bool LocalCommand_macro_usage(int argc)
486 {
487         string c = strtolower(argv(1));
488         FOREACH(CLIENT_COMMANDS, it.m_name == c, {
489                 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
490                 return true;
491         });
492         return false;
493 }
494
495 void LocalCommand_macro_write_aliases(int fh)
496 {
497         FOREACH(CLIENT_COMMANDS, true, CMD_Write_Alias("qc_cmd_cl", it.m_name, it.m_description));
498 }
499
500
501 // =========================================
502 //  Main Function Called By Engine (cl_cmd)
503 // =========================================
504 // If this function exists, client code handles gamecommand instead of the engine code.
505
506 void GameCommand(string command)
507 {
508         int argc = tokenize_console(command);
509
510         // Guide for working with argc arguments by example:
511         // argc:   1    - 2      - 3     - 4
512         // argv:   0    - 1      - 2     - 3
513         // cmd     vote - master - login - password
514         string s = strtolower(argv(0));
515         if (s == "help")
516         {
517                 if (argc == 1)
518                 {
519                         LOG_HELP("Client console commands:");
520                         LocalCommand_macro_help();
521
522                         LOG_HELP("\nGeneric commands shared by all programs:");
523                         GenericCommand_macro_help();
524
525                         LOG_HELP("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are listed above.");
526                         LOG_HELP("For help about a specific command, type cl_cmd help COMMAND");
527
528                         return;
529                 }
530                 else if (GenericCommand_macro_usage(argc))  // Instead of trying to call a command, we're going to see detailed information about it
531                 {
532                         return;
533                 }
534                 else if (LocalCommand_macro_usage(argc))  // now try for normal commands too
535                 {
536                         return;
537                 }
538         }
539         // continue as usual and scan for normal commands
540         if (GenericCommand(command)                                    // handled by common/command/generic.qc
541             || LocalCommand_macro_command(argc, command)               // handled by one of the above LocalCommand_* functions
542             || MUTATOR_CALLHOOK(CSQC_ConsoleCommand, s, argc, command) // handled by a mutator
543            ) return;
544
545         // nothing above caught the command, must be invalid
546         LOG_INFO(((command != "") ? strcat("Unknown client command \"", command, "\"") : "No command provided"), ". For a list of supported commands, try cl_cmd help.");
547 }
548
549
550 // ===================================
551 //  Macro system for console commands
552 // ===================================
553
554 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
555 // Please add client commands to the function above this, as this is only for special reasons.
556 // NOTE: showaccuracy is kept as legacy command
557 #define CONSOLE_COMMANDS_NORMAL() \
558         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = true; }) \
559         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = false; }) \
560         CONSOLE_COMMAND("+showaccuracy", { }) \
561         CONSOLE_COMMAND("-showaccuracy", { }) \
562         /* nothing */
563
564 #define CONSOLE_COMMANDS_MOVEMENT() \
565         CONSOLE_COMMAND("+forward", { ++camera_direction.x; }) \
566         CONSOLE_COMMAND("-forward", { --camera_direction.x; }) \
567         CONSOLE_COMMAND("+back", { --camera_direction.x; }) \
568         CONSOLE_COMMAND("-back", { ++camera_direction.x; }) \
569         CONSOLE_COMMAND("+moveup", { ++camera_direction.z; }) \
570         CONSOLE_COMMAND("-moveup", { --camera_direction.z; }) \
571         CONSOLE_COMMAND("+movedown", { --camera_direction.z; }) \
572         CONSOLE_COMMAND("-movedown", { ++camera_direction.z; }) \
573         CONSOLE_COMMAND("+moveright", { --camera_direction.y; }) \
574         CONSOLE_COMMAND("-moveright", { ++camera_direction.y; }) \
575         CONSOLE_COMMAND("+moveleft", { ++camera_direction.y; }) \
576         CONSOLE_COMMAND("-moveleft", { --camera_direction.y; }) \
577         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
578         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
579         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
580         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
581         /* nothing */
582
583 void ConsoleCommand_macro_init()
584 {
585         // first init normal commands
586         #define CONSOLE_COMMAND(name, execution) \
587                 { registercommand(name); }
588
589         CONSOLE_COMMANDS_NORMAL();
590         #undef CONSOLE_COMMAND
591
592         // then init movement commands
593         #ifndef CAMERATEST
594         if (isdemo())
595         {
596         #endif
597         #define CONSOLE_COMMAND(name, execution) \
598                 registercommand(name);
599
600         CONSOLE_COMMANDS_MOVEMENT();
601                 #undef CONSOLE_COMMAND
602         #ifndef CAMERATEST
603 }
604         #endif
605 }
606
607 bool ConsoleCommand_macro_normal(string s, int argc)
608 {
609         #define CONSOLE_COMMAND(name, execution) \
610                 { if (name == s) { { execution } return true; } }
611
612         CONSOLE_COMMANDS_NORMAL();
613         #undef CONSOLE_COMMAND
614
615         return false;
616 }
617
618 bool ConsoleCommand_macro_movement(string s, int argc)
619 {
620         if (camera_active)
621         {
622                 #define CONSOLE_COMMAND(name, execution) \
623                         { if (name == s) { { execution } return true; } }
624
625                 CONSOLE_COMMANDS_MOVEMENT();
626                 #undef CONSOLE_COMMAND
627         }
628
629         return false;
630 }
631
632
633 // ======================================================
634 //  Main Function Called By Engine (registered commands)
635 // ======================================================
636 // Used to parse commands in the console that have been registered with the "registercommand" function
637
638 bool CSQC_ConsoleCommand(string command)
639 {
640         int argc = tokenize_console(command);
641         string s = strtolower(argv(0));
642         // Return value should be true if CSQC handled the command, otherwise return false to have the engine handle it.
643         return ConsoleCommand_macro_normal(s, argc)
644                || ConsoleCommand_macro_movement(s, argc)
645         ;
646 }