]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/command/cl_cmd.qc
Fix it here too
[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 "minigame":
256                                 {
257                                         if(HUD_MinigameMenu_IsOpened())
258                                                 HUD_MinigameMenu_Close();
259                                         else
260                                                 HUD_MinigameMenu_Open();
261                                         return;
262                                 }
263
264                                 case "save":
265                                 {
266                                         if(argv(2))
267                                         {
268                                                 HUD_Panel_ExportCfg(argv(2));
269                                                 return;
270                                         }
271                                         else
272                                         {
273                                                 break; // go to usage, we're missing the paramater needed here.
274                                         }
275                                 }
276
277                                 case "scoreboard_columns_set":
278                                 {
279                                         Cmd_HUD_SetFields(argc);
280                                         return;
281                                 }
282
283                                 case "scoreboard_columns_help":
284                                 {
285                                         Cmd_HUD_Help();
286                                         return;
287                                 }
288
289                                 case "radar":
290                                 {
291                                         if(argv(2))
292                                                 hud_panel_radar_maximized = InterpretBoolean(argv(2));
293                                         else
294                                                 hud_panel_radar_maximized = !hud_panel_radar_maximized;
295                                         return;
296                                 }
297                         }
298                 }
299
300                 default:
301                         print("Incorrect parameters for ^2hud^7\n");
302                 case CMD_REQUEST_USAGE:
303                 {
304                         print("\nUsage:^3 cl_cmd hud action [configname | radartoggle | layout]\n");
305                         print("  Where 'action' is the command to complete,\n");
306                         print("  'configname' is the name to save to for \"save\" action,\n");
307                         print("  'radartoggle' is to control hud_panel_radar_maximized for \"radar\" action,\n");
308                         print("  and 'layout' is how to organize the scoreboard columns for the set action.\n");
309                         print("  Full list of commands here: \"configure, minigame, save, scoreboard_columns_help, scoreboard_columns_set, radar.\"\n");
310                         return;
311                 }
312         }
313 }
314
315 void LocalCommand_localprint(int request, int argc)
316 {
317         switch(request)
318         {
319                 case CMD_REQUEST_COMMAND:
320                 {
321                         if(argv(1))
322                         {
323                                 centerprint_hud(argv(1));
324                                 return;
325                         }
326                 }
327
328                 default:
329                         print("Incorrect parameters for ^2localprint^7\n");
330                 case CMD_REQUEST_USAGE:
331                 {
332                         print("\nUsage:^3 cl_cmd localprint \"message\"\n");
333                         print("  'message' is the centerprint message to send to yourself.\n");
334                         return;
335                 }
336         }
337 }
338
339 void LocalCommand_mv_download(int request, int argc)
340 {
341         switch(request)
342         {
343                 case CMD_REQUEST_COMMAND:
344                 {
345                         if(argv(1))
346                         {
347                                 Cmd_MapVote_MapDownload(argc);
348                                 return;
349                         }
350                 }
351
352                 default:
353                         print("Incorrect parameters for ^2mv_download^7\n");
354                 case CMD_REQUEST_USAGE:
355                 {
356                         print("\nUsage:^3 cl_cmd mv_download mapid\n");
357                         print("  Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
358                         return;
359                 }
360         }
361 }
362
363 void LocalCommand_sendcvar(int request, int argc)
364 {
365         switch(request)
366         {
367                 case CMD_REQUEST_COMMAND:
368                 {
369                         if(argv(1))
370                         {
371                                 // W_FixWeaponOrder will trash argv, so save what we need.
372                                 string thiscvar = strzone(argv(1));
373                                 string s = cvar_string(thiscvar);
374
375                                 if(thiscvar == "cl_weaponpriority")
376                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
377                                 else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
378                                         s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
379
380                                 localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
381                                 strunzone(thiscvar);
382                                 return;
383                         }
384                 }
385
386                 default:
387                         print("Incorrect parameters for ^2sendcvar^7\n");
388                 case CMD_REQUEST_USAGE:
389                 {
390                         print("\nUsage:^3 cl_cmd sendcvar <cvar>\n");
391                         print("  Where 'cvar' is the cvar plus arguments to send to the server.\n");
392                         return;
393                 }
394         }
395 }
396
397 /* use this when creating a new command, making sure to place it in alphabetical order... also,
398 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
399 void LocalCommand_(int request)
400 {
401         switch(request)
402         {
403                 case CMD_REQUEST_COMMAND:
404                 {
405
406                         return;
407                 }
408
409                 default:
410                 case CMD_REQUEST_USAGE:
411                 {
412                         print("\nUsage:^3 cl_cmd \n");
413                         print("  No arguments required.\n");
414                         return;
415                 }
416         }
417 }
418 */
419
420
421 // ==================================
422 //  Macro system for client commands
423 // ==================================
424
425 // Normally do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
426 #define CLIENT_COMMANDS(request,arguments) \
427         CLIENT_COMMAND("blurtest", LocalCommand_blurtest(request), "Feature for testing blur postprocessing") \
428         CLIENT_COMMAND("boxparticles", LocalCommand_boxparticles(request, arguments), "Spawn particles manually") \
429         CLIENT_COMMAND("create_scrshot_ent", LocalCommand_create_scrshot_ent(request), "Create an entity at this location for automatic screenshots") \
430         CLIENT_COMMAND("debugmodel", LocalCommand_debugmodel(request, arguments), "Spawn a debug model manually") \
431         CLIENT_COMMAND("handlevote", LocalCommand_handlevote(request, arguments), "System to handle selecting a vote or option") \
432         CLIENT_COMMAND("hud", LocalCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
433         CLIENT_COMMAND("localprint", LocalCommand_localprint(request, arguments), "Create your own centerprint sent to yourself") \
434         CLIENT_COMMAND("mv_download", LocalCommand_mv_download(request, arguments), "Retrieve mapshot picture from the server") \
435         CLIENT_COMMAND("sendcvar", LocalCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
436         /* nothing */
437
438 void LocalCommand_macro_help()
439 {
440         #define CLIENT_COMMAND(name,function,description) \
441                 { if(strtolower(description) != "") { print("  ^2", name, "^7: ", description, "\n"); } }
442
443         CLIENT_COMMANDS(0, 0);
444         #undef CLIENT_COMMAND
445
446         return;
447 }
448
449 bool LocalCommand_macro_command(int argc)
450 {
451         #define CLIENT_COMMAND(name,function,description) \
452                 { if(name == strtolower(argv(0))) { function; return true; } }
453
454         CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc);
455         #undef CLIENT_COMMAND
456
457         return false;
458 }
459
460 bool LocalCommand_macro_usage(int argc)
461 {
462         #define CLIENT_COMMAND(name,function,description) \
463                 { if(name == strtolower(argv(1))) { function; return true; } }
464
465         CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc);
466         #undef CLIENT_COMMAND
467
468         return false;
469 }
470
471 void LocalCommand_macro_write_aliases(int fh)
472 {
473         #define CLIENT_COMMAND(name,function,description) \
474                 { if(strtolower(description) != "") { CMD_Write_Alias("qc_cmd_cl", name, description); } }
475
476         CLIENT_COMMANDS(0, 0);
477         #undef CLIENT_COMMAND
478
479         return;
480 }
481
482
483 // =========================================
484 //  Main Function Called By Engine (cl_cmd)
485 // =========================================
486 // If this function exists, client code handles gamecommand instead of the engine code.
487
488 void GameCommand(string command)
489 {
490         int argc = tokenize_console(command);
491
492         // Guide for working with argc arguments by example:
493         // argc:   1    - 2      - 3     - 4
494         // argv:   0    - 1      - 2     - 3
495         // cmd     vote - master - login - password
496
497         if(strtolower(argv(0)) == "help")
498         {
499                 if(argc == 1)
500                 {
501                         print("\nClient console commands:\n");
502                         LocalCommand_macro_help();
503
504                         print("\nGeneric commands shared by all programs:\n");
505                         GenericCommand_macro_help();
506
507                         print("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are listed above.\n");
508                         print("For help about a specific command, type cl_cmd help COMMAND\n");
509
510                         return;
511                 }
512                 else if(GenericCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
513                 {
514                         return;
515                 }
516                 else if(LocalCommand_macro_usage(argc)) // now try for normal commands too
517                 {
518                         return;
519                 }
520         }
521         else if(GenericCommand(command))
522         {
523                 return; // handled by common/command/generic.qc
524         }
525         else if(LocalCommand_macro_command(argc)) // continue as usual and scan for normal commands
526         {
527                 return; // handled by one of the above LocalCommand_* functions
528         }
529
530         // nothing above caught the command, must be invalid
531         print(((command != "") ? strcat("Unknown client command \"", command, "\"") : "No command provided"), ". For a list of supported commands, try cl_cmd help.\n");
532
533         return;
534 }
535
536
537 // ===================================
538 //  Macro system for console commands
539 // ===================================
540
541 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
542 // Please add client commands to the function above this, as this is only for special reasons.
543 #define CONSOLE_COMMANDS_NORMAL() \
544         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = true; }) \
545         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = false; }) \
546         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = true; }) \
547         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = false; }) \
548         /* nothing */
549
550 #define CONSOLE_COMMANDS_MOVEMENT() \
551         CONSOLE_COMMAND("+forward", { ++camera_direction.x; }) \
552         CONSOLE_COMMAND("-forward", { --camera_direction.x; }) \
553         CONSOLE_COMMAND("+back", { --camera_direction.x; }) \
554         CONSOLE_COMMAND("-back", { ++camera_direction.x; }) \
555         CONSOLE_COMMAND("+moveup", { ++camera_direction.z; }) \
556         CONSOLE_COMMAND("-moveup", { --camera_direction.z; }) \
557         CONSOLE_COMMAND("+movedown", { --camera_direction.z; }) \
558         CONSOLE_COMMAND("-movedown", { ++camera_direction.z; }) \
559         CONSOLE_COMMAND("+moveright", { --camera_direction.y; }) \
560         CONSOLE_COMMAND("-moveright", { ++camera_direction.y; }) \
561         CONSOLE_COMMAND("+moveleft", { ++camera_direction.y; }) \
562         CONSOLE_COMMAND("-moveleft", { --camera_direction.y; }) \
563         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
564         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
565         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
566         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
567         /* nothing */
568
569 void ConsoleCommand_macro_init()
570 {
571         // first init normal commands
572         #define CONSOLE_COMMAND(name,execution) \
573                 { registercommand(name); }
574
575         CONSOLE_COMMANDS_NORMAL();
576         #undef CONSOLE_COMMAND
577
578         // then init movement commands
579         #ifndef CAMERATEST
580         if(isdemo())
581         {
582         #endif
583                 #define CONSOLE_COMMAND(name,execution) \
584                         { registercommand(name); }
585
586                 CONSOLE_COMMANDS_MOVEMENT();
587                 #undef CONSOLE_COMMAND
588         #ifndef CAMERATEST
589         }
590         #endif
591
592         return;
593 }
594
595 bool ConsoleCommand_macro_normal(int argc)
596 {
597         #define CONSOLE_COMMAND(name,execution) \
598                 { if(name == strtolower(argv(0))) { { execution } return true; } }
599
600         CONSOLE_COMMANDS_NORMAL();
601         #undef CONSOLE_COMMAND
602
603         return false;
604 }
605
606 bool ConsoleCommand_macro_movement(int argc)
607 {
608         if(camera_active)
609         {
610                 #define CONSOLE_COMMAND(name,execution) \
611                         { if(name == strtolower(argv(0))) { { execution } return true; } }
612
613                 CONSOLE_COMMANDS_MOVEMENT();
614                 #undef CONSOLE_COMMAND
615         }
616
617         return false;
618 }
619
620
621 // ======================================================
622 //  Main Function Called By Engine (registered commands)
623 // ======================================================
624 // Used to parse commands in the console that have been registered with the "registercommand" function
625
626 bool CSQC_ConsoleCommand(string command)
627 {
628         int argc = tokenize_console(command);
629
630         if(ConsoleCommand_macro_normal(argc))
631         {
632                 return true;
633         }
634         else if(ConsoleCommand_macro_movement(argc))
635         {
636                 return true;
637         }
638
639         // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
640
641         return false;
642 }