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