]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/command/cl_cmd.qc
Also move all of the other game command files from other programs into their respecti...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / command / cl_cmd.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 /* use this when creating a new command, making sure to place it in alphabetical order.
259 void GameCommand_(float request)
260 {
261         switch(request)
262         {
263                 case GC_REQUEST_COMMAND:
264                 {
265                         
266                         return; 
267                 }
268                         
269                 default:
270                 case GC_REQUEST_USAGE:
271                 {
272                         print("\nUsage:^3 cl_cmd \n");
273                         print("  No arguments required.\n");
274                         return;
275                 }
276         }
277 }
278 */
279
280
281 // ==================================
282 //  Macro system for client commands
283 // ==================================
284
285 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
286 #define CLIENT_COMMANDS(request,arguments) \
287         CLIENT_COMMAND("blurtest", GameCommand_blurtest(request), "Feature for testing blur postprocessing") \
288         CLIENT_COMMAND("hud", GameCommand_hud(request, arguments), "Commands regarding/controlling the HUD system") \
289         CLIENT_COMMAND("sendcvar", GameCommand_sendcvar(request, arguments), "Send a cvar to the server (like weaponpriority)") \
290         /* nothing */
291         
292 void GameCommand_macro_help()
293 {
294         #define CLIENT_COMMAND(name,function,description) \
295                 { print("  ^2", name, "^7: ", description, "\n"); }
296                 
297         CLIENT_COMMANDS(0, 0)
298         #undef CLIENT_COMMAND
299         
300         return;
301 }
302
303 float GameCommand_macro_command(float argc)
304 {
305         #define CLIENT_COMMAND(name,function,description) \
306                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
307                 
308         CLIENT_COMMANDS(GC_REQUEST_COMMAND, argc)
309         #undef CLIENT_COMMAND
310         
311         return FALSE;
312 }
313
314 float GameCommand_macro_usage(float argc)
315 {
316         #define CLIENT_COMMAND(name,function,description) \
317                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
318                 
319         CLIENT_COMMANDS(GC_REQUEST_USAGE, argc)
320         #undef CLIENT_COMMAND
321         
322         return FALSE;
323 }
324
325
326 // =========================================
327 //  Main Function Called By Engine (cl_cmd)
328 // =========================================
329 // If this function exists, client code handles gamecommand instead of the engine code.
330
331 void GameCommand(string command)
332 {
333         float argc = tokenize_console(command);
334
335         if(strtolower(argv(0)) == "help") 
336         {
337                 if(argc == 1) 
338                 {
339                         print("\nUsage:^3 cl_cmd COMMAND...^7, where possible commands are:\n");
340                         GameCommand_macro_help();
341                         GameCommand_Generic("help");
342                         print("For help about specific commands, type cl_cmd help COMMAND\n");
343                         return;
344                 } 
345                 else if(GameCommand_macro_usage(argc)) // Instead of trying to call a command, we're going to see detailed information about it
346                 {
347                         return;
348                 }
349         } 
350         else if(GameCommand_Generic(command)) 
351         {
352                 return; // handled by common/gamecommand.qc
353         }
354         else if(GameCommand_macro_command(argc)) // continue as usual and scan for normal commands
355         {
356                 return; // handled by one of the above GameCommand_* functions
357         }
358         
359         // nothing above caught the command, must be invalid
360         print("Unknown client command", ((command != "") ? strcat(" \"", command, "\"") : ""), ". For a list of supported commands, try cl_cmd help.\n");
361         
362         return;
363 }
364
365
366 // ===================================
367 //  Macro system for console commands
368 // ===================================
369
370 // These functions are here specifically to add special + - commands to the game, and are not really normal commands.
371 // Please add client commands to the function above this, as this is only for special reasons.
372 #define CONSOLE_COMMANDS_NORMAL \
373         CONSOLE_COMMAND("+showscores", { scoreboard_showscores = TRUE; }) \
374         CONSOLE_COMMAND("-showscores", { scoreboard_showscores = FALSE; }) \
375         CONSOLE_COMMAND("+showaccuracy", { scoreboard_showaccuracy = TRUE; }) \
376         CONSOLE_COMMAND("-showaccuracy", { scoreboard_showaccuracy = FALSE; }) \
377         /* nothing */
378         
379 #define CONSOLE_COMMANDS_MOVEMENT \
380         CONSOLE_COMMAND("+forward", { ++camera_direction_x; }) \
381         CONSOLE_COMMAND("-forward", { --camera_direction_x; }) \
382         CONSOLE_COMMAND("+back", { --camera_direction_x; }) \
383         CONSOLE_COMMAND("-back", { ++camera_direction_x; }) \
384         CONSOLE_COMMAND("+moveup", { ++camera_direction_z; }) \
385         CONSOLE_COMMAND("-moveup", { --camera_direction_z; }) \
386         CONSOLE_COMMAND("+movedown", { --camera_direction_z; }) \
387         CONSOLE_COMMAND("-movedown", { ++camera_direction_z; }) \
388         CONSOLE_COMMAND("+moveright", { --camera_direction_y; }) \
389         CONSOLE_COMMAND("-moveright", { ++camera_direction_y; }) \
390         CONSOLE_COMMAND("+moveleft", { ++camera_direction_y; }) \
391         CONSOLE_COMMAND("-moveleft", { --camera_direction_y; }) \
392         CONSOLE_COMMAND("+roll_right", { ++camera_roll; }) \
393         CONSOLE_COMMAND("-roll_right", { --camera_roll; }) \
394         CONSOLE_COMMAND("+roll_left", { --camera_roll; }) \
395         CONSOLE_COMMAND("-roll_left", { ++camera_roll; }) \
396         /* nothing */
397
398 void ConsoleCommand_macro_init()
399 {
400         // first init normal commands
401         #define CONSOLE_COMMAND(name,execution) \
402                 { registercommand(name); }
403
404         CONSOLE_COMMANDS_NORMAL
405         #undef CONSOLE_COMMAND
406         
407         // then init movement commands
408         #ifndef CAMERATEST
409         if(isdemo())
410         {
411         #endif
412                 #define CONSOLE_COMMAND(name,execution) \
413                         { registercommand(name); }
414
415                 CONSOLE_COMMANDS_MOVEMENT
416                 #undef CONSOLE_COMMAND
417         #ifndef CAMERATEST
418         }
419         #endif
420         
421         return;
422 }
423
424 float ConsoleCommand_macro_normal(float argc)
425 {
426         #define CONSOLE_COMMAND(name,execution) \
427                 { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
428                 
429         CONSOLE_COMMANDS_NORMAL
430         #undef CONSOLE_COMMAND
431         
432         return FALSE;
433 }
434
435 float ConsoleCommand_macro_movement(float argc)
436 {
437         if(camera_active)
438         {
439                 #define CONSOLE_COMMAND(name,execution) \
440                         { if(name == strtolower(argv(0))) { { execution } return TRUE; } }
441
442                 CONSOLE_COMMANDS_MOVEMENT
443                 #undef CONSOLE_COMMAND
444         }
445         
446         return FALSE;
447 }
448
449
450 // ======================================================
451 //  Main Function Called By Engine (registered commands)
452 // ======================================================
453 // Used to parse commands in the console that have been registered with the "registercommand" function
454
455 float CSQC_ConsoleCommand(string command)
456 {
457         float argc = tokenize_console(command);
458
459         if(ConsoleCommand_macro_normal(argc))
460         {
461                 return TRUE;
462         }
463         else if(ConsoleCommand_macro_movement(argc))
464         {
465                 return TRUE;
466         }
467         
468         // Return value should be 1 if CSQC handled the command, otherwise return 0 to have the engine handle it.
469
470         return FALSE;
471 }