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