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