]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/command/cl_cmd.qc
Merge branch 'master' into TimePath/global_self
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / command / cl_cmd.qc
1 // ==============================================
2 //  CSQC client commands code, written by Samual
3 //  Last updated: December 28th, 2011
4 // ==============================================
5
6 #include "../../common/command/command.qh"
7 #include "cl_cmd.qh"
8
9 #include "../autocvars.qh"
10 #include "../defs.qh"
11 #include "../hud.qh"
12 #include "../hud_config.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()
24 {SELFPARAM();
25         if(time - floor(time) > 0.5)
26         {
27                 PolyDrawModel(self);
28                 self.drawmask = 0;
29         }
30         else
31         {
32                 self.renderflags = 0;
33                 self.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                         LOG_INFO("Incorrect parameters for ^2boxparticles^7\n");
108                 case CMD_REQUEST_USAGE:
109                 {
110                         LOG_INFO("\nUsage:^3 lv_cmd boxparticles effectname own org_from org_to, dir_from, dir_to, countmultiplier, flags\n");
111                         LOG_INFO("  'effectname' is the name of a particle effect in effectinfo.txt\n");
112                         LOG_INFO("  'own' is the entity number of the owner (negative for csqc ent, positive for svqc ent)\n");
113                         LOG_INFO("  'org_from' is the starting origin of the box\n");
114                         LOG_INFO("  'org_to' is the ending origin of the box\n");
115                         LOG_INFO("  'dir_from' is the minimum velocity\n");
116                         LOG_INFO("  'dir_to' is the maximum velocity\n");
117                         LOG_INFO("  'countmultiplier' defines a multiplier for the particle count (affects count only, not countabsolute or trailspacing)\n");
118                         LOG_INFO("  'flags' can contain:\n");
119                         LOG_INFO("    1 to respect globals particles_alphamin, particles_alphamax (set right before via prvm_globalset client)\n");
120                         LOG_INFO("    2 to respect globals particles_colormin, particles_colormax (set right before via prvm_globalset client)\n");
121                         LOG_INFO("    4 to respect globals particles_fade (set right before via prvm_globalset client)\n");
122                         LOG_INFO("    128 to draw a trail, not a box\n");
123                         return;
124                 }
125         }
126 }
127
128 void LocalCommand_create_scrshot_ent(int request)
129 {
130         switch(request)
131         {
132                 case CMD_REQUEST_COMMAND:
133                 {
134                         string filename = strcat(MapInfo_Map_bspname, "_scrshot_ent.txt");
135                         int fh = fopen(filename, FILE_WRITE);
136
137                         if(fh >= 0)
138                         {
139                                 fputs(fh, "{\n");
140                                 fputs(fh, strcat("\"classname\" \"info_autoscreenshot\"\n"));
141                                 fputs(fh, strcat("\"origin\" \"", strcat(ftos(view_origin.x), " ", ftos(view_origin.y), " ", ftos(view_origin.z)), "\"\n"));
142                                 fputs(fh, strcat("\"angles\" \"", strcat(ftos(view_angles.x), " ", ftos(view_angles.y), " ", ftos(view_angles.z)), "\"\n"));
143                                 fputs(fh, "}\n");
144
145                                 LOG_INFO("Completed screenshot entity dump in ^2data/data/", MapInfo_Map_bspname, "_scrshot_ent.txt^7.\n");
146
147                                 fclose(fh);
148                         }
149                         else
150                         {
151                                 LOG_INFO("^1Error: ^7Could not dump to file!\n");
152                         }
153                         return;
154                 }
155
156                 default:
157                 case CMD_REQUEST_USAGE:
158                 {
159                         LOG_INFO("\nUsage:^3 cl_cmd create_scrshot_ent\n");
160                         LOG_INFO("  No arguments required.\n");
161                         return;
162                 }
163         }
164 }
165
166 void LocalCommand_debugmodel(int request, int argc)
167 {
168         switch(request)
169         {
170                 case CMD_REQUEST_COMMAND:
171                 {
172                         string modelname = argv(1);
173                         entity debugmodel_entity;
174
175                         debugmodel_entity = spawn();
176                         precache_model(modelname);
177                         setmodel(debugmodel_entity, modelname);
178                         setorigin(debugmodel_entity, view_origin);
179                         debugmodel_entity.angles = view_angles;
180                         debugmodel_entity.draw = DrawDebugModel;
181                         debugmodel_entity.classname = "debugmodel";
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                         LOG_INFO("Incorrect parameters for ^2handlevote^7\n");
234                 case CMD_REQUEST_USAGE:
235                 {
236                         LOG_INFO("\nUsage:^3 cl_cmd handlevote vote\n");
237                         LOG_INFO("  Where 'vote' is the selection for either the current poll or uid2name.\n");
238                         return;
239                 }
240         }
241 }
242
243 void LocalCommand_hud(int request, int argc)
244 {
245         switch(request)
246         {
247                 case CMD_REQUEST_COMMAND:
248                 {
249                         switch(argv(1))
250                         {
251                                 case "configure":
252                                 {
253                                         cvar_set("_hud_configure", ftos(!autocvar__hud_configure));
254                                         return;
255                                 }
256
257                                 case "quickmenu":
258                                 {
259                                         if(QuickMenu_IsOpened())
260                                                 QuickMenu_Close();
261                                         else
262                                                 QuickMenu_Open(argv(2), argv(3)); // mode, submenu
263                                         return;
264                                 }
265
266                                 case "minigame":
267                                 {
268                                         if(HUD_MinigameMenu_IsOpened())
269                                                 HUD_MinigameMenu_Close();
270                                         else
271                                                 HUD_MinigameMenu_Open();
272                                         return;
273                                 }
274
275                                 case "save":
276                                 {
277                                         if(argv(2))
278                                         {
279                                                 HUD_Panel_ExportCfg(argv(2));
280                                                 return;
281                                         }
282                                         else
283                                         {
284                                                 break; // go to usage, we're missing the paramater needed here.
285                                         }
286                                 }
287
288                                 case "scoreboard_columns_set":
289                                 {
290                                         Cmd_HUD_SetFields(argc);
291                                         return;
292                                 }
293
294                                 case "scoreboard_columns_help":
295                                 {
296                                         Cmd_HUD_Help();
297                                         return;
298                                 }
299
300                                 case "radar":
301                                 {
302                                         if(argv(2))
303                                                 HUD_Radar_Show_Maximized(InterpretBoolean(argv(2)),0);
304                                         else
305                                                 HUD_Radar_Show_Maximized(!hud_panel_radar_maximized,0);
306                                         return;
307                                 }
308
309                                 case "clickradar":
310                                 {
311                                         HUD_Radar_Show_Maximized(!hud_panel_radar_mouse,1);
312                                         return;
313                                 }
314                         }
315                 }
316
317                 default:
318                         LOG_INFO("Incorrect parameters for ^2hud^7\n");
319                 case CMD_REQUEST_USAGE:
320                 {
321                         LOG_INFO("\nUsage:^3 cl_cmd hud action [configname | radartoggle | layout]\n");
322                         LOG_INFO("  Where 'action' is the command to complete,\n");
323                         LOG_INFO("  'configname' is the name to save to for \"save\" action,\n");
324                         LOG_INFO("  'radartoggle' is to control hud_panel_radar_maximized for \"radar\" action,\n");
325                         LOG_INFO("  and 'layout' is how to organize the scoreboard columns for the set action.\n");
326                         LOG_INFO("  quickmenu [[default | file | \"\"] submenu]\n");
327                         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");
328                         LOG_INFO("    Submenu option allows to open quickmenu directly in a submenu, it requires to specify 'default', 'file' or '\"\"' option.\n");
329                         LOG_INFO("  Full list of commands here: \"configure, minigame, save, scoreboard_columns_help, scoreboard_columns_set, radar.\"\n");
330                         return;
331                 }
332         }
333 }
334
335 void LocalCommand_localprint(int request, int argc)
336 {
337         switch(request)
338         {
339                 case CMD_REQUEST_COMMAND:
340                 {
341                         if(argv(1))
342                         {
343                                 centerprint_hud(argv(1));
344                                 return;
345                         }
346                 }
347
348                 default:
349                         LOG_INFO("Incorrect parameters for ^2localprint^7\n");
350                 case CMD_REQUEST_USAGE:
351                 {
352                         LOG_INFO("\nUsage:^3 cl_cmd localprint \"message\"\n");
353                         LOG_INFO("  'message' is the centerprint message to send to yourself.\n");
354                         return;
355                 }
356         }
357 }
358
359 void LocalCommand_mv_download(int request, int argc)
360 {
361         switch(request)
362         {
363                 case CMD_REQUEST_COMMAND:
364                 {
365                         if(argv(1))
366                         {
367                                 Cmd_MapVote_MapDownload(argc);
368                                 return;
369                         }
370                 }
371
372                 default:
373                         LOG_INFO("Incorrect parameters for ^2mv_download^7\n");
374                 case CMD_REQUEST_USAGE:
375                 {
376                         LOG_INFO("\nUsage:^3 cl_cmd mv_download mapid\n");
377                         LOG_INFO("  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
378                         return;
379                 }
380         }
381 }
382
383 void LocalCommand_find(int request, int argc)
384 {
385         switch(request)
386         {
387                 case CMD_REQUEST_COMMAND:
388                 {
389                         entity client;
390
391                         for(client = world; (client = find(client, classname, argv(1))); )
392                                 LOG_INFO(etos(client), "\n");
393
394                         return;
395                 }
396
397                 default:
398                         LOG_INFO("Incorrect parameters for ^2find^7\n");
399                 case CMD_REQUEST_USAGE:
400                 {
401                         LOG_INFO("\nUsage:^3 cl_cmd find classname\n");
402                         LOG_INFO("  Where 'classname' is the classname to search for.\n");
403                         return;
404                 }
405         }
406 }
407
408 void LocalCommand_sendcvar(int request, int argc)
409 {
410         switch(request)
411         {
412                 case CMD_REQUEST_COMMAND:
413                 {
414                         if(argv(1))
415                         {
416                                 // W_FixWeaponOrder will trash argv, so save what we need.
417                                 string thiscvar = strzone(argv(1));
418                                 string s = cvar_string(thiscvar);
419
420                                 if(thiscvar == "cl_weaponpriority")
421                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
422                                 else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
423                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
424
425                                 localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
426                                 strunzone(thiscvar);
427                                 return;
428                         }
429                 }
430
431                 default:
432                         LOG_INFO("Incorrect parameters for ^2sendcvar^7\n");
433                 case CMD_REQUEST_USAGE:
434                 {
435                         LOG_INFO("\nUsage:^3 cl_cmd sendcvar <cvar>\n");
436                         LOG_INFO("  Where 'cvar' is the cvar plus arguments to send to the server.\n");
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 #define CLIENT_COMMANDS(request,arguments) \
472         CLIENT_COMMAND("blurtest", LocalCommand_blurtest(request), "Feature for testing blur postprocessing") \
473         CLIENT_COMMAND("boxparticles", LocalCommand_boxparticles(request, arguments), "Spawn particles manually") \
474         CLIENT_COMMAND("create_scrshot_ent", LocalCommand_create_scrshot_ent(request), "Create an entity at this location for automatic screenshots") \
475         CLIENT_COMMAND("debugmodel", LocalCommand_debugmodel(request, arguments), "Spawn a debug model manually") \
476         CLIENT_COMMAND("handlevote", LocalCommand_handlevote(request, arguments), "System to handle selecting a vote or option") \
477         CLIENT_COMMAND("hud", LocalCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
478         CLIENT_COMMAND("localprint", LocalCommand_localprint(request, arguments), "Create your own centerprint sent to yourself") \
479         CLIENT_COMMAND("find", LocalCommand_find(request, arguments), "Search through entities for matching classname") \
480         CLIENT_COMMAND("mv_download", LocalCommand_mv_download(request, arguments), "Retrieve mapshot picture from the server") \
481         CLIENT_COMMAND("sendcvar", LocalCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
482         /* nothing */
483
484 void LocalCommand_macro_help()
485 {
486         #define CLIENT_COMMAND(name,function,description) \
487                 { if(strtolower(description) != "") { LOG_INFO("  ^2", name, "^7: ", description, "\n"); } }
488
489         CLIENT_COMMANDS(0, 0);
490         #undef CLIENT_COMMAND
491
492         return;
493 }
494
495 bool LocalCommand_macro_command(int argc)
496 {
497         #define CLIENT_COMMAND(name,function,description) \
498                 { if(name == strtolower(argv(0))) { function; return true; } }
499
500         CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc);
501         #undef CLIENT_COMMAND
502
503         return false;
504 }
505
506 bool LocalCommand_macro_usage(int argc)
507 {
508         #define CLIENT_COMMAND(name,function,description) \
509                 { if(name == strtolower(argv(1))) { function; return true; } }
510
511         CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc);
512         #undef CLIENT_COMMAND
513
514         return false;
515 }
516
517 void LocalCommand_macro_write_aliases(int fh)
518 {
519         #define CLIENT_COMMAND(name,function,description) \
520                 { if(strtolower(description) != "") { CMD_Write_Alias("qc_cmd_cl", name, description); } }
521
522         CLIENT_COMMANDS(0, 0);
523         #undef CLIENT_COMMAND
524
525         return;
526 }
527
528
529 // =========================================
530 //  Main Function Called By Engine (cl_cmd)
531 // =========================================
532 // If this function exists, client code handles gamecommand instead of the engine code.
533
534 void GameCommand(string command)
535 {
536         int argc = tokenize_console(command);
537
538         // Guide for working with argc arguments by example:
539         // argc:   1    - 2      - 3     - 4
540         // argv:   0    - 1      - 2     - 3
541         // cmd     vote - master - login - password
542         string s = strtolower(argv(0));
543         if (s == "help")
544         {
545                 if(argc == 1)
546                 {
547                         LOG_INFO("\nClient console commands:\n");
548                         LocalCommand_macro_help();
549
550                         LOG_INFO("\nGeneric commands shared by all programs:\n");
551                         GenericCommand_macro_help();
552
553                         LOG_INFO("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are listed above.\n");
554                         LOG_INFO("For help about a specific command, type cl_cmd help COMMAND\n");
555
556                         return;
557                 }
558                 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
559                 {
560                         return;
561                 }
562                 else if(LocalCommand_macro_usage(argc)) // now try for normal commands too
563                 {
564                         return;
565                 }
566         }
567         // continue as usual and scan for normal commands
568         if (GenericCommand(command)// handled by common/command/generic.qc
569         || LocalCommand_macro_command(argc) // handled by one of the above LocalCommand_* functions
570         || MUTATOR_CALLHOOK(CSQC_ConsoleCommand, s, argc, command) // handled by a mutator
571         ) return;
572
573         // nothing above caught the command, must be invalid
574         LOG_INFO(((command != "") ? strcat("Unknown client command \"", command, "\"") : "No command provided"), ". For a list of supported commands, try cl_cmd help.\n");
575
576         return;
577 }
578
579
580 // ===================================
581 //  Macro system for console commands
582 // ===================================
583
584 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
585 // Please add client commands to the function above this, as this is only for special reasons.
586 #define CONSOLE_COMMANDS_NORMAL() \
587         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = true; }) \
588         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = false; }) \
589         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = true; }) \
590         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = false; }) \
591         /* nothing */
592
593 #define CONSOLE_COMMANDS_MOVEMENT() \
594         CONSOLE_COMMAND("+forward", { ++camera_direction.x; }) \
595         CONSOLE_COMMAND("-forward", { --camera_direction.x; }) \
596         CONSOLE_COMMAND("+back", { --camera_direction.x; }) \
597         CONSOLE_COMMAND("-back", { ++camera_direction.x; }) \
598         CONSOLE_COMMAND("+moveup", { ++camera_direction.z; }) \
599         CONSOLE_COMMAND("-moveup", { --camera_direction.z; }) \
600         CONSOLE_COMMAND("+movedown", { --camera_direction.z; }) \
601         CONSOLE_COMMAND("-movedown", { ++camera_direction.z; }) \
602         CONSOLE_COMMAND("+moveright", { --camera_direction.y; }) \
603         CONSOLE_COMMAND("-moveright", { ++camera_direction.y; }) \
604         CONSOLE_COMMAND("+moveleft", { ++camera_direction.y; }) \
605         CONSOLE_COMMAND("-moveleft", { --camera_direction.y; }) \
606         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
607         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
608         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
609         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
610         /* nothing */
611
612 void ConsoleCommand_macro_init()
613 {
614         // first init normal commands
615         #define CONSOLE_COMMAND(name,execution) \
616                 { registercommand(name); }
617
618         CONSOLE_COMMANDS_NORMAL();
619         #undef CONSOLE_COMMAND
620
621         // then init movement commands
622         #ifndef CAMERATEST
623         if(isdemo())
624         {
625         #endif
626                 #define CONSOLE_COMMAND(name,execution) \
627                         { registercommand(name); }
628
629                 CONSOLE_COMMANDS_MOVEMENT();
630                 #undef CONSOLE_COMMAND
631         #ifndef CAMERATEST
632         }
633         #endif
634 }
635
636 bool ConsoleCommand_macro_normal(string s, int argc)
637 {
638         #define CONSOLE_COMMAND(name,execution) \
639                 { if (name == s) { { execution } return true; } }
640
641         CONSOLE_COMMANDS_NORMAL();
642         #undef CONSOLE_COMMAND
643
644         return false;
645 }
646
647 bool ConsoleCommand_macro_movement(string s, int argc)
648 {
649         if(camera_active)
650         {
651                 #define CONSOLE_COMMAND(name,execution) \
652                         { if (name == s) { { execution } return true; } }
653
654                 CONSOLE_COMMANDS_MOVEMENT();
655                 #undef CONSOLE_COMMAND
656         }
657
658         return false;
659 }
660
661
662 // ======================================================
663 //  Main Function Called By Engine (registered commands)
664 // ======================================================
665 // Used to parse commands in the console that have been registered with the "registercommand" function
666
667 bool CSQC_ConsoleCommand(string command)
668 {
669         int argc = tokenize_console(command);
670         string s = strtolower(argv(0));
671         // Return value should be true if CSQC handled the command, otherwise return false to have the engine handle it.
672         return (ConsoleCommand_macro_normal(s, argc)
673         || ConsoleCommand_macro_movement(s, argc)
674         );
675 }