]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/gamecommand.qc
Merge remote branch 'origin/master' into samual/updatecommands
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / gamecommand.qc
1 // ==============================================
2 //  CSQC client commands code, written by Samual
3 //  Last updated: November 26th, 2011
4 // ==============================================
5
6         /*
7         if(cmd == "mv_download") {
8                 Cmd_MapVote_MapDownload(argc);
9         }
10         else if(cmd == "scoreboard_columns_set") {
11                 Cmd_HUD_SetFields(argc);
12         }
13         else if(cmd == "scoreboard_columns_help") {
14                 Cmd_HUD_Help(argc);
15         }
16         else if(cmd == "spawn") {
17                 s = argv(1);
18                 e = spawn();
19                 precache_model(s);
20                 setmodel(e, s);
21                 setorigin(e, view_origin);
22                 e.angles = view_angles;
23                 e.draw = DrawDebugModel;
24                 e.classname = "debugmodel";
25         }
26     else if(cmd == "vyes")
27     {
28         if(uid2name_dialog)
29         {
30             vote_active = 0; // force the panel to disappear right as we have selected the value (to prevent it from fading out in the normal vote panel pos)
31             vote_prev = 0;
32             localcmd("setreport cl_allow_uid2name 1\n");
33             vote_change = -9999;
34                         uid2name_dialog = 0;
35         }
36         else
37         {
38             localcmd("cmd vote yes\n");
39         }
40     }
41     else if(cmd == "vno")
42     {
43         if(uid2name_dialog)
44         {
45             vote_active = 0;
46             vote_prev = 0;
47             localcmd("setreport cl_allow_uid2name 0\n");
48             vote_change = -9999;
49                         uid2name_dialog = 0;
50         }
51         else
52         {
53             localcmd("cmd vote no\n");
54         }
55     }
56         */
57
58
59 #define GC_REQUEST_COMMAND 1
60 #define GC_REQUEST_USAGE 2
61
62 void Cmd_HUD_SetFields(float);
63 void Cmd_HUD_Help(float);
64
65 .vector view_ofs;
66 entity debug_shotorg;
67
68
69 // ============================
70 //  Misc. Supporting Functions
71 // ============================
72
73 float cvar_clientsettemp(string tmp_cvar, string value)
74 {
75         entity e;
76         
77         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
78                 if(e.netname == tmp_cvar)
79                         goto saved;
80                         
81         // creating a new entity to keep track of this cvar
82         e = spawn();
83         e.classname = "saved_cvar_value";
84         e.netname = strzone(tmp_cvar);
85         e.message = strzone(cvar_string(tmp_cvar));
86         return TRUE;
87         
88         // an entity for this cvar already exists, update the value
89         :saved
90         cvar_set(tmp_cvar, value);
91         return FALSE;
92 }
93
94 float cvar_clientsettemp_restore()
95 {
96         float i;
97         entity e;
98         
99         for(e = world; (e = find(e, classname, "saved_cvar_value")); )
100                 { cvar_set(e.netname, e.message); ++i; } 
101                 
102         return i;
103 }
104
105 void DrawDebugModel()
106 {
107         if(time - floor(time) > 0.5)
108         {
109                 PolyDrawModel(self);
110                 self.drawmask = 0;
111         }
112         else
113         {
114                 self.renderflags = 0;
115                 self.drawmask = MASK_NORMAL;
116         }
117 }
118
119
120 // =======================
121 //  Command Sub-Functions
122 // =======================
123
124 void GameCommand_blurtest(float request)
125 {
126         // Simple command to work with postprocessing temporarily... possibly completely pointless, the glsl shader is used for a real feature now...
127         // Anyway, to enable it, just compile the client with -DBLURTEST and then you can use the command.
128         
129         #ifdef BLURTEST
130         switch(request)
131         {
132                 case GC_REQUEST_COMMAND:
133                 {
134                         blurtest_time0 = time;
135                         blurtest_time1 = time + stof(argv(1));
136                         blurtest_radius = stof(argv(2));
137                         blurtest_power = stof(argv(3));
138                         print("Enabled blurtest\n");
139                         return; 
140                 }
141                         
142                 default:
143                 case GC_REQUEST_USAGE:
144                 {
145                         print("\nUsage:^3 cl_cmd blurtest\n");
146                         print("  No arguments required.\n");
147                         return;
148                 }
149         }
150         #else
151         if(request)
152         {
153                 print("Blurtest is not enabled on this client.\n");
154                 return;
155         }
156         #endif
157 }
158
159 void GameCommand_hud(float request, float argc) // TODO: Add aliases in commands.cfg
160 {
161         switch(request)
162         {
163                 case GC_REQUEST_COMMAND:
164                 {
165                         switch(argv(1))
166                         {
167                                 case "configure":
168                                 {
169                                         cvar_set("_hud_configure", ftos(!autocvar__hud_configure));
170                                         return;
171                                 }
172                                 
173                                 case "save":
174                                 {
175                                         if(argv(2))
176                                         {
177                                                 HUD_Panel_ExportCfg(argv(2));
178                                                 return;
179                                         }
180                                         else
181                                         {
182                                                 break; // go to usage, we're missing the paramater needed here.
183                                         }
184                                 }
185                                 
186                                 case "radar":
187                                 {
188                                         if(argv(2))
189                                                 hud_panel_radar_maximized = (stof(argv(2)) != 0);
190                                         else
191                                                 hud_panel_radar_maximized = !hud_panel_radar_maximized;
192                                         
193                                         return;
194                                 }
195                                 
196                                 case "scoreboard_columns_set":
197                                 {
198                                         Cmd_HUD_SetFields(argc); // todo update this function
199                                         
200                                         return;
201                                 }
202
203                                 case "scoreboard_columns_help":
204                                 {
205                                         Cmd_HUD_Help(argc); // todo update this function
206                                         
207                                         return;
208                                 }
209                         }
210                         return; 
211                 }
212                         
213                 default:
214                         print("Incorrect parameters for ^2hud^7\n");
215                 case GC_REQUEST_USAGE:
216                 {
217                         print("\nUsage:^3 cl_cmd hud action [configname | radartoggle]\n");
218                         print("  Where 'action' is the command to complete,\n");
219                         print("  'configname' is the name to save to for \"save\" action,\n");
220                         print("  and 'radartoggle' is to control hud_panel_radar_maximized for \"radar\" action.\n");
221                         print("  Full list of commands here: \"configure, save, radar.\"\n");
222                         return;
223                 }
224         }
225 }
226
227 void GameCommand_sendcvar(float request, float argc)
228 {
229         switch(request)
230         {
231                 case GC_REQUEST_COMMAND:
232                 {
233                         // W_FixWeaponOrder will trash argv, so save what we need.
234                         string thiscvar = strzone(argv(1));
235                         string s = cvar_string(thiscvar);
236                         
237                         if(thiscvar == "cl_weaponpriority")
238                                 s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 1);
239                         else if(substring(thiscvar, 0, 17) == "cl_weaponpriority" && strlen(thiscvar) == 18)
240                                 s = W_FixWeaponOrder(W_NumberWeaponOrder(s), 0);
241                                 
242                         localcmd("cmd sentcvar ", thiscvar, " \"", s, "\"\n");
243                         strunzone(thiscvar);
244                         
245                         return; 
246                 }
247                         
248                 default:
249                 case GC_REQUEST_USAGE:
250                 {
251                         print("\nUsage:^3 cl_cmd sendcvar <cvar>\n");
252                         print("  Where 'cvar' is the cvar plus arguments to send to the server.\n");
253                         return;
254                 }
255         }
256 }
257
258 void GameCommand_settemp(float request, float argc)
259 {
260         switch(request)
261         {
262                 case GC_REQUEST_COMMAND:
263                 {
264                         if((argv(1) == "restore") && (argc == 3))
265                         {
266                                 float i = cvar_clientsettemp_restore();
267                                 
268                                 if(i)
269                                         print("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
270                                 else
271                                         print("Nothing to restore.\n");
272                         }
273                         else
274                         {
275                                 if(cvar_clientsettemp(argv(1), argv(2)))
276                                         print("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
277                                 else
278                                         print("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
279                         }
280                                 
281                         return; 
282                 }
283                         
284                 default:
285                 case GC_REQUEST_USAGE:
286                 {
287                         print("\nUsage:^3 cl_cmd settemp <cvar> | [restore]\n");
288                         print("  Where 'cvar' is the cvar plus arguments to send to the server,\n");
289                         print("  or 'restore' allows you to restore all of the original temporary cvar values.\n");
290                         return;
291                 }
292         }
293 }
294
295 /* use this when creating a new command, making sure to place it in alphabetical order.
296 void GameCommand_(float request)
297 {
298         switch(request)
299         {
300                 case GC_REQUEST_COMMAND:
301                 {
302                         
303                         return; 
304                 }
305                         
306                 default:
307                 case GC_REQUEST_USAGE:
308                 {
309                         print("\nUsage:^3 cl_cmd \n");
310                         print("  No arguments required.\n");
311                         return;
312                 }
313         }
314 }
315 */
316
317
318 // ==================================
319 //  Macro system for client commands
320 // ==================================
321
322 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
323 #define CLIENT_COMMANDS(request,arguments) \
324         CLIENT_COMMAND("blurtest", GameCommand_blurtest(request), "Feature for testing blur postprocessing") \
325         CLIENT_COMMAND("hud", GameCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
326         CLIENT_COMMAND("sendcvar", GameCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
327         CLIENT_COMMAND("settemp", GameCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored by command or end of each match") \
328         /* nothing */
329         
330 void GameCommand_macro_help()
331 {
332         #define CLIENT_COMMAND(name,function,description) \
333                 { print("  ^2", name, "^7: ", description, "\n"); }
334                 
335         CLIENT_COMMANDS(0, 0)
336         #undef CLIENT_COMMAND
337         
338         return;
339 }
340
341 float GameCommand_macro_command(float argc)
342 {
343         #define CLIENT_COMMAND(name,function,description) \
344                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
345                 
346         CLIENT_COMMANDS(GC_REQUEST_COMMAND, argc)
347         #undef CLIENT_COMMAND
348         
349         return FALSE;
350 }
351
352 float GameCommand_macro_usage(float argc)
353 {
354         #define CLIENT_COMMAND(name,function,description) \
355                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
356                 
357         CLIENT_COMMANDS(GC_REQUEST_USAGE, argc)
358         #undef CLIENT_COMMAND
359         
360         return FALSE;
361 }
362
363
364 // =========================================
365 //  Main Function Called By Engine (cl_cmd)
366 // =========================================
367 // If this function exists, client code handles gamecommand instead of the engine code.
368
369 void GameCommand(string command)
370 {
371         float argc = tokenize_console(command);
372
373         if(strtolower(argv(0)) == "help") 
374         {
375                 if(argc == 1) 
376                 {
377                         print("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are:\n");
378                         GameCommand_macro_help();
379                         GameCommand_Generic("help");
380                         print("For help about specific commands, type cl_cmd help COMMAND\n");
381                         return;
382                 } 
383                 else if(GameCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
384                 {
385                         return;
386                 }
387         } 
388         else if(GameCommand_Generic(command)) 
389         {
390                 return; // handled by common/gamecommand.qc
391         }
392         else if(GameCommand_macro_command(argc)) // continue as usual and scan for normal commands
393         {
394                 return; // handled by one of the above GameCommand_* functions
395         }
396         
397         // nothing above caught the command, must be invalid
398         print("Unknown client command", ((command != "") ? strcat(" \"", command, "\"") : ""), ". For a list of supported commands, try cl_cmd help.\n");
399         
400         return;
401 }
402
403
404 // ===================================
405 //  Macro system for console commands
406 // ===================================
407
408 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
409 // Please add client commands to the function above this, as this is only for special reasons.
410 #define CONSOLE_COMMANDS_NORMAL \
411         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = TRUE; }) \
412         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = FALSE; }) \
413         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = TRUE; }) \
414         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = FALSE; }) \
415         /* nothing */
416         
417 #define CONSOLE_COMMANDS_MOVEMENT \
418         CONSOLE_COMMAND("+forward", { ++camera_direction_x; }) \
419         CONSOLE_COMMAND("-forward", { --camera_direction_x; }) \
420         CONSOLE_COMMAND("+back", { --camera_direction_x; }) \
421         CONSOLE_COMMAND("-back", { ++camera_direction_x; }) \
422         CONSOLE_COMMAND("+moveup", { ++camera_direction_z; }) \
423         CONSOLE_COMMAND("-moveup", { --camera_direction_z; }) \
424         CONSOLE_COMMAND("+movedown", { --camera_direction_z; }) \
425         CONSOLE_COMMAND("-movedown", { ++camera_direction_z; }) \
426         CONSOLE_COMMAND("+moveright", { --camera_direction_y; }) \
427         CONSOLE_COMMAND("-moveright", { ++camera_direction_y; }) \
428         CONSOLE_COMMAND("+moveleft", { ++camera_direction_y; }) \
429         CONSOLE_COMMAND("-moveleft", { --camera_direction_y; }) \
430         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
431         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
432         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
433         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
434         /* nothing */
435
436 void ConsoleCommand_macro_init()
437 {
438         // first init normal commands
439         #define CONSOLE_COMMAND(name,execution) \
440                 { registercommand(name); }
441
442         CONSOLE_COMMANDS_NORMAL
443         #undef CONSOLE_COMMAND
444         
445         // then init movement commands
446         #ifndef CAMERATEST
447         if(isdemo())
448         {
449         #endif
450                 #define CONSOLE_COMMAND(name,execution) \
451                         { registercommand(name); }
452
453                 CONSOLE_COMMANDS_MOVEMENT
454                 #undef CONSOLE_COMMAND
455         #ifndef CAMERATEST
456         }
457         #endif
458         
459         return;
460 }
461
462 float ConsoleCommand_macro_normal(float argc)
463 {
464         #define CONSOLE_COMMAND(name,execution) \
465                 { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
466                 
467         CONSOLE_COMMANDS_NORMAL
468         #undef CONSOLE_COMMAND
469         
470         return FALSE;
471 }
472
473 float ConsoleCommand_macro_movement(float argc)
474 {
475         if(camera_active)
476         {
477                 #define CONSOLE_COMMAND(name,execution) \
478                         { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
479
480                 CONSOLE_COMMANDS_MOVEMENT
481                 #undef CONSOLE_COMMAND
482         }
483         
484         return FALSE;
485 }
486
487
488 // ======================================================
489 //  Main Function Called By Engine (registered commands)
490 // ======================================================
491 // Used to parse commands in the console that have been registered with the "registercommand" function
492
493 float CSQC_ConsoleCommand(string command)
494 {
495         float argc = tokenize_console(command);
496
497         if(ConsoleCommand_macro_normal(argc))
498         {
499                 return TRUE;
500         }
501         else if(ConsoleCommand_macro_movement(argc))
502         {
503                 return TRUE;
504         }
505         
506         // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
507
508         return FALSE;
509 }