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