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