]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/command/cl_cmd.qc
Merge branch 'Penguinum/Antiwall' into 'master'
[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_sendcvar(int request, int argc)
355 {
356         switch(request)
357         {
358                 case CMD_REQUEST_COMMAND:
359                 {
360                         if(argv(1))
361                         {
362                                 // W_FixWeaponOrder will trash argv, so save what we need.
363                                 string thiscvar = strzone(argv(1));
364                                 string s = cvar_string(thiscvar);
365
366                                 if(thiscvar == "cl_weaponpriority")
367                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
368                                 else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
369                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
370
371                                 localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
372                                 strunzone(thiscvar);
373                                 return;
374                         }
375                 }
376
377                 default:
378                         print("Incorrect parameters for ^2sendcvar^7\n");
379                 case CMD_REQUEST_USAGE:
380                 {
381                         print("\nUsage:^3 cl_cmd sendcvar <cvar>\n");
382                         print("  Where 'cvar' is the cvar plus arguments to send to the server.\n");
383                         return;
384                 }
385         }
386 }
387
388 /* use this when creating a new command, making sure to place it in alphabetical order... also,
389 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
390 void LocalCommand_(int request)
391 {
392         switch(request)
393         {
394                 case CMD_REQUEST_COMMAND:
395                 {
396
397                         return;
398                 }
399
400                 default:
401                 case CMD_REQUEST_USAGE:
402                 {
403                         print("\nUsage:^3 cl_cmd \n");
404                         print("  No arguments required.\n");
405                         return;
406                 }
407         }
408 }
409 */
410
411
412 // ==================================
413 //  Macro system for client commands
414 // ==================================
415
416 // Normally do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
417 #define CLIENT_COMMANDS(request,arguments) \
418         CLIENT_COMMAND("blurtest", LocalCommand_blurtest(request), "Feature for testing blur postprocessing") \
419         CLIENT_COMMAND("boxparticles", LocalCommand_boxparticles(request, arguments), "Spawn particles manually") \
420         CLIENT_COMMAND("create_scrshot_ent", LocalCommand_create_scrshot_ent(request), "Create an entity at this location for automatic screenshots") \
421         CLIENT_COMMAND("debugmodel", LocalCommand_debugmodel(request, arguments), "Spawn a debug model manually") \
422         CLIENT_COMMAND("handlevote", LocalCommand_handlevote(request, arguments), "System to handle selecting a vote or option") \
423         CLIENT_COMMAND("hud", LocalCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
424         CLIENT_COMMAND("localprint", LocalCommand_localprint(request, arguments), "Create your own centerprint sent to yourself") \
425         CLIENT_COMMAND("mv_download", LocalCommand_mv_download(request, arguments), "Retrieve mapshot picture from the server") \
426         CLIENT_COMMAND("sendcvar", LocalCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
427         /* nothing */
428
429 void LocalCommand_macro_help()
430 {
431         #define CLIENT_COMMAND(name,function,description) \
432                 { if(strtolower(description) != "") { print("  ^2", name, "^7: ", description, "\n"); } }
433
434         CLIENT_COMMANDS(0, 0);
435         #undef CLIENT_COMMAND
436
437         return;
438 }
439
440 bool LocalCommand_macro_command(int argc)
441 {
442         #define CLIENT_COMMAND(name,function,description) \
443                 { if(name == strtolower(argv(0))) { function; return true; } }
444
445         CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc);
446         #undef CLIENT_COMMAND
447
448         return false;
449 }
450
451 bool LocalCommand_macro_usage(int argc)
452 {
453         #define CLIENT_COMMAND(name,function,description) \
454                 { if(name == strtolower(argv(1))) { function; return true; } }
455
456         CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc);
457         #undef CLIENT_COMMAND
458
459         return false;
460 }
461
462 void LocalCommand_macro_write_aliases(int fh)
463 {
464         #define CLIENT_COMMAND(name,function,description) \
465                 { if(strtolower(description) != "") { CMD_Write_Alias("qc_cmd_cl", name, description); } }
466
467         CLIENT_COMMANDS(0, 0);
468         #undef CLIENT_COMMAND
469
470         return;
471 }
472
473
474 // =========================================
475 //  Main Function Called By Engine (cl_cmd)
476 // =========================================
477 // If this function exists, client code handles gamecommand instead of the engine code.
478
479 void GameCommand(string command)
480 {
481         int argc = tokenize_console(command);
482
483         // Guide for working with argc arguments by example:
484         // argc:   1    - 2      - 3     - 4
485         // argv:   0    - 1      - 2     - 3
486         // cmd     vote - master - login - password
487
488         if(strtolower(argv(0)) == "help")
489         {
490                 if(argc == 1)
491                 {
492                         print("\nClient console commands:\n");
493                         LocalCommand_macro_help();
494
495                         print("\nGeneric commands shared by all programs:\n");
496                         GenericCommand_macro_help();
497
498                         print("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are listed above.\n");
499                         print("For help about a specific command, type cl_cmd help COMMAND\n");
500
501                         return;
502                 }
503                 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
504                 {
505                         return;
506                 }
507                 else if(LocalCommand_macro_usage(argc)) // now try for normal commands too
508                 {
509                         return;
510                 }
511         }
512         else if(GenericCommand(command))
513         {
514                 return; // handled by common/command/generic.qc
515         }
516         else if(LocalCommand_macro_command(argc)) // continue as usual and scan for normal commands
517         {
518                 return; // handled by one of the above LocalCommand_* functions
519         }
520
521         // nothing above caught the command, must be invalid
522         print(((command != "") ? strcat("Unknown client command \"", command, "\"") : "No command provided"), ". For a list of supported commands, try cl_cmd help.\n");
523
524         return;
525 }
526
527
528 // ===================================
529 //  Macro system for console commands
530 // ===================================
531
532 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
533 // Please add client commands to the function above this, as this is only for special reasons.
534 #define CONSOLE_COMMANDS_NORMAL() \
535         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = true; }) \
536         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = false; }) \
537         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = true; }) \
538         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = false; }) \
539         /* nothing */
540
541 #define CONSOLE_COMMANDS_MOVEMENT() \
542         CONSOLE_COMMAND("+forward", { ++camera_direction.x; }) \
543         CONSOLE_COMMAND("-forward", { --camera_direction.x; }) \
544         CONSOLE_COMMAND("+back", { --camera_direction.x; }) \
545         CONSOLE_COMMAND("-back", { ++camera_direction.x; }) \
546         CONSOLE_COMMAND("+moveup", { ++camera_direction.z; }) \
547         CONSOLE_COMMAND("-moveup", { --camera_direction.z; }) \
548         CONSOLE_COMMAND("+movedown", { --camera_direction.z; }) \
549         CONSOLE_COMMAND("-movedown", { ++camera_direction.z; }) \
550         CONSOLE_COMMAND("+moveright", { --camera_direction.y; }) \
551         CONSOLE_COMMAND("-moveright", { ++camera_direction.y; }) \
552         CONSOLE_COMMAND("+moveleft", { ++camera_direction.y; }) \
553         CONSOLE_COMMAND("-moveleft", { --camera_direction.y; }) \
554         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
555         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
556         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
557         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
558         /* nothing */
559
560 void ConsoleCommand_macro_init()
561 {
562         // first init normal commands
563         #define CONSOLE_COMMAND(name,execution) \
564                 { registercommand(name); }
565
566         CONSOLE_COMMANDS_NORMAL();
567         #undef CONSOLE_COMMAND
568
569         // then init movement commands
570         #ifndef CAMERATEST
571         if(isdemo())
572         {
573         #endif
574                 #define CONSOLE_COMMAND(name,execution) \
575                         { registercommand(name); }
576
577                 CONSOLE_COMMANDS_MOVEMENT();
578                 #undef CONSOLE_COMMAND
579         #ifndef CAMERATEST
580         }
581         #endif
582
583         return;
584 }
585
586 bool ConsoleCommand_macro_normal(int argc)
587 {
588         #define CONSOLE_COMMAND(name,execution) \
589                 { if(name == strtolower(argv(0))) { { execution } return true; } }
590
591         CONSOLE_COMMANDS_NORMAL();
592         #undef CONSOLE_COMMAND
593
594         return false;
595 }
596
597 bool ConsoleCommand_macro_movement(int argc)
598 {
599         if(camera_active)
600         {
601                 #define CONSOLE_COMMAND(name,execution) \
602                         { if(name == strtolower(argv(0))) { { execution } return true; } }
603
604                 CONSOLE_COMMANDS_MOVEMENT();
605                 #undef CONSOLE_COMMAND
606         }
607
608         return false;
609 }
610
611
612 // ======================================================
613 //  Main Function Called By Engine (registered commands)
614 // ======================================================
615 // Used to parse commands in the console that have been registered with the "registercommand" function
616
617 bool CSQC_ConsoleCommand(string command)
618 {
619         int argc = tokenize_console(command);
620
621         if(ConsoleCommand_macro_normal(argc))
622         {
623                 return true;
624         }
625         else if(ConsoleCommand_macro_movement(argc))
626         {
627                 return true;
628         }
629
630         // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
631
632         return false;
633 }