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