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