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