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