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