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