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