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