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