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