]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/common.qc
Declare more ints as ints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / common.qc
1 #include "common.qh"
2
3 #include "../../common/counting.qh"
4
5
6 // ====================================================
7 //  Shared code for server commands, written by Samual
8 //  Last updated: December 27th, 2011
9 // ====================================================
10
11 // select the proper prefix for usage and other messages
12 string GetCommandPrefix(entity caller)
13 {
14         if(caller)
15                 return "cmd";
16         else
17                 return "sv_cmd";
18 }
19
20 // if client return player nickname, or if server return admin nickname
21 string GetCallerName(entity caller)
22 {
23         if(caller)
24                 return caller.netname;
25         else
26                 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
27 }
28
29 // verify that the client provided is acceptable for use
30 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
31 {
32         if (!IS_CLIENT(client))
33                 return CLIENT_DOESNT_EXIST;
34         else if(must_be_real && !IS_REAL_CLIENT(client))
35                 return CLIENT_NOT_REAL;
36         else if(must_be_bots && !IS_BOT_CLIENT(client))
37                 return CLIENT_NOT_BOT;
38
39         return CLIENT_ACCEPTABLE;
40 }
41
42 // if the client is not acceptable, return a string to be used for error messages
43 string GetClientErrorString(float clienterror, string original_input)
44 {
45         switch(clienterror)
46         {
47                 case CLIENT_DOESNT_EXIST: { return strcat("Client '", original_input, "' doesn't exist"); }
48                 case CLIENT_NOT_REAL: { return strcat("Client '", original_input, "' is not real"); }
49                 case CLIENT_NOT_BOT: { return strcat("Client '", original_input, "' is not a bot"); }
50                 default: { return "Incorrect usage of GetClientErrorString"; }
51         }
52 }
53
54 // is this entity number even in the possible range of entities?
55 float VerifyClientNumber(float tmp_number)
56 {
57         if((tmp_number < 1) || (tmp_number > maxclients))
58                 return false;
59         else
60                 return true;
61 }
62
63 entity GetIndexedEntity(float argc, float start_index)
64 {
65         entity tmp_player, selection;
66         float tmp_number, index;
67         string tmp_string;
68
69         next_token = -1;
70         index = start_index;
71         selection = world;
72
73         if(argc > start_index)
74         {
75                 if(substring(argv(index), 0, 1) == "#")
76                 {
77                         tmp_string = substring(argv(index), 1, -1);
78                         ++index;
79
80                         if(tmp_string != "") // is it all one token? like #1
81                         {
82                                 tmp_number = stof(tmp_string);
83                         }
84                         else if(argc > index) // no, it's two tokens? # 1
85                         {
86                                 tmp_number = stof(argv(index));
87                                 ++index;
88                         }
89                         else
90                                 tmp_number = 0;
91                 }
92                 else // maybe it's ONLY a number?
93                 {
94                         tmp_number = stof(argv(index));
95                         ++index;
96                 }
97
98                 if(VerifyClientNumber(tmp_number))
99                 {
100                         selection = edict_num(tmp_number); // yes, it was a number
101                 }
102                 else // no, maybe it's a name?
103                 {
104                         FOR_EACH_CLIENT(tmp_player)
105                                 if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index)))
106                                         selection = tmp_player;
107
108                         index = (start_index + 1);
109                 }
110         }
111
112         next_token = index;
113         //print(strcat("start_index: ", ftos(start_index), ", next_token: ", ftos(next_token), ", edict: ", ftos(num_for_edict(selection)), ".\n"));
114         return selection;
115 }
116
117 // find a player which matches the input string, and return their entity
118 entity GetFilteredEntity(string input)
119 {
120         entity tmp_player, selection;
121         float tmp_number;
122
123         if(substring(input, 0, 1) == "#")
124                 tmp_number = stof(substring(input, 1, -1));
125         else
126                 tmp_number = stof(input);
127
128         if(VerifyClientNumber(tmp_number))
129         {
130                 selection = edict_num(tmp_number);
131         }
132         else
133         {
134                 selection = world;
135                 FOR_EACH_CLIENT(tmp_player)
136                         if (strdecolorize(tmp_player.netname) == strdecolorize(input))
137                                 selection = tmp_player;
138         }
139
140         return selection;
141 }
142
143 // same thing, but instead return their edict number
144 float GetFilteredNumber(string input)
145 {
146         entity selection = GetFilteredEntity(input);
147         float output;
148
149         output = num_for_edict(selection);
150
151         return output;
152 }
153
154 // switch between sprint and print depending on whether the receiver is the server or a player
155 void print_to(entity to, string input)
156 {
157     if(to)
158         sprint(to, strcat(input, "\n"));
159     else
160         print(input, "\n");
161 }
162
163 // ==========================================
164 //  Supporting functions for common commands
165 // ==========================================
166
167 // used by CommonCommand_timeout() and CommonCommand_timein() to handle game pausing and messaging and such.
168 void timeout_handler_reset()
169 {
170         timeout_caller = world;
171         timeout_time = 0;
172         timeout_leadtime = 0;
173
174         remove(self);
175 }
176
177 void timeout_handler_think()
178 {
179         entity tmp_player;
180
181         switch(timeout_status)
182         {
183                 case TIMEOUT_ACTIVE:
184                 {
185                         if(timeout_time > 0) // countdown is still going
186                         {
187                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_ENDING, timeout_time);
188
189                                 if(timeout_time == autocvar_sv_timeout_resumetime) // play a warning sound when only <sv_timeout_resumetime> seconds are left
190                                         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_PREPARE);
191
192                                 self.nextthink = time + TIMEOUT_SLOWMO_VALUE; // think again in one second
193                                 timeout_time -= 1; // decrease the time counter
194                         }
195                         else // time to end the timeout
196                         {
197                                 timeout_status = TIMEOUT_INACTIVE;
198
199                                 // reset the slowmo value back to normal
200                                 cvar_set("slowmo", ftos(orig_slowmo));
201
202                                 // unlock the view for players so they can move around again
203                                 FOR_EACH_REALPLAYER(tmp_player)
204                                         tmp_player.fixangle = false;
205
206                                 timeout_handler_reset();
207                         }
208
209                         return;
210                 }
211
212                 case TIMEOUT_LEADTIME:
213                 {
214                         if(timeout_leadtime > 0) // countdown is still going
215                         {
216                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_BEGINNING, timeout_leadtime);
217
218                                 self.nextthink = time + 1; // think again in one second
219                                 timeout_leadtime -= 1; // decrease the time counter
220                         }
221                         else // time to begin the timeout
222                         {
223                                 timeout_status = TIMEOUT_ACTIVE;
224
225                                 // set the slowmo value to the timeout default slowmo value
226                                 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
227
228                                 // reset all the flood variables
229                                 FOR_EACH_CLIENT(tmp_player)
230                                         tmp_player.nickspamcount = tmp_player.nickspamtime = tmp_player.floodcontrol_chat =
231                                         tmp_player.floodcontrol_chatteam = tmp_player.floodcontrol_chattell =
232                                         tmp_player.floodcontrol_voice = tmp_player.floodcontrol_voiceteam = 0;
233
234                                 // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
235                                 FOR_EACH_REALPLAYER(tmp_player)
236                                         tmp_player.lastV_angle = tmp_player.v_angle;
237
238                                 self.nextthink = time; // think again next frame to handle it under TIMEOUT_ACTIVE code
239                         }
240
241                         return;
242                 }
243
244
245                 case TIMEOUT_INACTIVE:
246                 default:
247                 {
248                         timeout_handler_reset();
249                         return;
250                 }
251         }
252 }
253
254
255
256 // ===================================================
257 //  Common commands used in both sv_cmd.qc and cmd.qc
258 // ===================================================
259
260 void CommonCommand_cvar_changes(float request, entity caller)
261 {
262         switch(request)
263         {
264                 case CMD_REQUEST_COMMAND:
265                 {
266                         print_to(caller, cvar_changes);
267                         return; // never fall through to usage
268                 }
269
270                 default:
271                 case CMD_REQUEST_USAGE:
272                 {
273                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
274                         print_to(caller, "  No arguments required.");
275                         print_to(caller, "See also: ^2cvar_purechanges^7");
276                         return;
277                 }
278         }
279 }
280
281 void CommonCommand_cvar_purechanges(float request, entity caller)
282 {
283         switch(request)
284         {
285                 case CMD_REQUEST_COMMAND:
286                 {
287                         print_to(caller, cvar_purechanges);
288                         return; // never fall through to usage
289                 }
290
291                 default:
292                 case CMD_REQUEST_USAGE:
293                 {
294                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
295                         print_to(caller, "  No arguments required.");
296                         print_to(caller, "See also: ^2cvar_changes^7");
297                         return;
298                 }
299         }
300 }
301
302 void CommonCommand_info(float request, entity caller, float argc)
303 {
304         switch(request)
305         {
306                 case CMD_REQUEST_COMMAND:
307                 {
308                         string command = builtin_cvar_string(strcat("sv_info_", argv(1)));
309
310                         if(command)
311                                 wordwrap_sprint(command, 1000);
312                         else
313                                 print_to(caller, "ERROR: unsupported info command");
314
315                         return; // never fall through to usage
316                 }
317
318                 default:
319                 case CMD_REQUEST_USAGE:
320                 {
321                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
322                         print_to(caller, "  Where 'request' is the suffixed string appended onto the request for cvar.");
323                         return;
324                 }
325         }
326 }
327
328 void CommonCommand_ladder(float request, entity caller)
329 {
330         switch(request)
331         {
332                 case CMD_REQUEST_COMMAND:
333                 {
334                         print_to(caller, ladder_reply);
335                         return; // never fall through to usage
336                 }
337
338                 default:
339                 case CMD_REQUEST_USAGE:
340                 {
341                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
342                         print_to(caller, "  No arguments required.");
343                         return;
344                 }
345         }
346 }
347
348 void CommonCommand_lsmaps(float request, entity caller)
349 {
350         switch(request)
351         {
352                 case CMD_REQUEST_COMMAND:
353                 {
354                         print_to(caller, lsmaps_reply);
355                         return; // never fall through to usage
356                 }
357
358                 default:
359                 case CMD_REQUEST_USAGE:
360                 {
361                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
362                         print_to(caller, "  No arguments required.");
363                         return;
364                 }
365         }
366 }
367
368 void CommonCommand_printmaplist(float request, entity caller)
369 {
370         switch(request)
371         {
372                 case CMD_REQUEST_COMMAND:
373                 {
374                         print_to(caller, maplist_reply);
375                         return; // never fall through to usage
376                 }
377
378                 default:
379                 case CMD_REQUEST_USAGE:
380                 {
381                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " printmaplist"));
382                         print_to(caller, "  No arguments required.");
383                         return;
384                 }
385         }
386 }
387
388 void CommonCommand_rankings(float request, entity caller)
389 {
390         switch(request)
391         {
392                 case CMD_REQUEST_COMMAND:
393                 {
394                         print_to(caller, rankings_reply);
395                         return; // never fall through to usage
396                 }
397
398                 default:
399                 case CMD_REQUEST_USAGE:
400                 {
401                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
402                         print_to(caller, "  No arguments required.");
403                         return;
404                 }
405         }
406 }
407
408 void CommonCommand_records(float request, entity caller)
409 {
410         switch(request)
411         {
412                 case CMD_REQUEST_COMMAND:
413                 {
414                         for(int i = 0; i < 10; ++i)
415                                 if(records_reply[i] != "")
416                                         print_to(caller, records_reply[i]);
417
418                         return; // never fall through to usage
419                 }
420
421                 default:
422                 case CMD_REQUEST_USAGE:
423                 {
424                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
425                         print_to(caller, "  No arguments required.");
426                         return;
427                 }
428         }
429 }
430
431 void CommonCommand_teamstatus(float request, entity caller)
432 {
433         switch(request)
434         {
435                 case CMD_REQUEST_COMMAND:
436                 {
437                         Score_NicePrint(caller);
438                         return; // never fall through to usage
439                 }
440
441                 default:
442                 case CMD_REQUEST_USAGE:
443                 {
444                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
445                         print_to(caller, "  No arguments required.");
446                         return;
447                 }
448         }
449 }
450
451 void CommonCommand_time(float request, entity caller)
452 {
453         switch(request)
454         {
455                 case CMD_REQUEST_COMMAND:
456                 {
457                         print_to(caller, strcat("time = ", ftos(time)));
458                         print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
459                         print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
460                         print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
461                         print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
462                         print_to(caller, strcat("localtime = ", strftime(true, "%a %b %e %H:%M:%S %Z %Y")));
463                         print_to(caller, strcat("gmtime = ", strftime(false, "%a %b %e %H:%M:%S %Z %Y")));
464                         return;
465                 }
466
467                 default:
468                 case CMD_REQUEST_USAGE:
469                 {
470                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
471                         print_to(caller, "  No arguments required.");
472                         return;
473                 }
474         }
475 }
476
477 void CommonCommand_timein(float request, entity caller)
478 {
479         switch(request)
480         {
481                 case CMD_REQUEST_COMMAND:
482                 {
483                         if(!caller || autocvar_sv_timeout)
484                         {
485                                 if (!timeout_status) { print_to(caller, "^7Error: There is no active timeout called."); }
486                                 else if(caller && (caller != timeout_caller)) { print_to(caller, "^7Error: You are not allowed to stop the active timeout."); }
487
488                                 else // everything should be okay, continue aborting timeout
489                                 {
490                                         switch(timeout_status)
491                                         {
492                                                 case TIMEOUT_LEADTIME:
493                                                 {
494                                                         timeout_status = TIMEOUT_INACTIVE;
495                                                         timeout_time = 0;
496                                                         timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
497                                                         bprint(strcat("^7The timeout was aborted by ", GetCallerName(caller), " !\n"));
498                                                         return;
499                                                 }
500
501                                                 case TIMEOUT_ACTIVE:
502                                                 {
503                                                         timeout_time = autocvar_sv_timeout_resumetime;
504                                                         timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
505                                                         bprint(strcat("^1Attention: ^7", GetCallerName(caller), " resumed the game! Prepare for battle!\n"));
506                                                         return;
507                                                 }
508
509                                                 default: dprint("timeout status was inactive, but this code was executed anyway?"); return;
510                                         }
511                                 }
512                         }
513                         else { print_to(caller, "^1Timeins are not allowed to be called, enable them with sv_timeout 1.\n"); }
514
515                         return; // never fall through to usage
516                 }
517
518                 default:
519                 case CMD_REQUEST_USAGE:
520                 {
521                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
522                         print_to(caller, "  No arguments required.");
523                         return;
524                 }
525         }
526 }
527
528 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
529 {
530         switch(request)
531         {
532                 case CMD_REQUEST_COMMAND:
533                 {
534                         if(!caller || autocvar_sv_timeout)
535                         {
536                                 float last_possible_timeout = ((autocvar_timelimit * 60) - autocvar_sv_timeout_leadtime - 1);
537
538                                 if(timeout_status) { print_to(caller, "^7Error: A timeout is already active."); }
539                                 else if(vote_called) { print_to(caller, "^7Error: You can not call a timeout while a vote is active."); }
540                                 else if(warmup_stage && !g_warmup_allow_timeout) { print_to(caller, "^7Error: You can not call a timeout in warmup-stage."); }
541                                 else if(time < game_starttime) { print_to(caller, "^7Error: You can not call a timeout while the map is being restarted."); }
542                                 else if(caller && (caller.allowed_timeouts < 1)) { print_to(caller, "^7Error: You already used all your timeout calls for this map."); }
543                                 else if(caller && !IS_PLAYER(caller)) { print_to(caller, "^7Error: You must be a player to call a timeout."); }
544                                 else if((autocvar_timelimit) && (last_possible_timeout < time - game_starttime)) { print_to(caller, "^7Error: It is too late to call a timeout now!"); }
545
546                                 else // everything should be okay, proceed with starting the timeout
547                                 {
548                                         if(caller) { caller.allowed_timeouts -= 1; }
549
550                                         bprint(GetCallerName(caller), " ^7called a timeout", (caller ? strcat(" (", ftos(caller.allowed_timeouts), " timeout(s) left)") : ""), "!\n"); // write a bprint who started the timeout (and how many they have left)
551
552                                         timeout_status = TIMEOUT_LEADTIME;
553                                         timeout_caller = caller;
554                                         timeout_time = autocvar_sv_timeout_length;
555                                         timeout_leadtime = autocvar_sv_timeout_leadtime;
556
557                                         timeout_handler = spawn();
558                                         timeout_handler.think = timeout_handler_think;
559                                         timeout_handler.nextthink = time; // always let the entity think asap
560
561                                         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_TIMEOUT);
562                                 }
563                         }
564                         else { print_to(caller, "^1Timeouts are not allowed to be called, enable them with sv_timeout 1.\n"); }
565
566                         return; // never fall through to usage
567                 }
568
569                 default:
570                 case CMD_REQUEST_USAGE:
571                 {
572                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
573                         print_to(caller, "  No arguments required.");
574                         return;
575                 }
576         }
577 }
578
579 void CommonCommand_who(float request, entity caller, float argc)
580 {
581         switch(request)
582         {
583                 case CMD_REQUEST_COMMAND:
584                 {
585                         float total_listed_players, is_bot;
586                         entity tmp_player;
587
588                         float privacy = (caller && autocvar_sv_status_privacy);
589                         string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
590                         string tmp_netaddress, tmp_crypto_idfp;
591
592                         print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : ""), ":"));
593                         print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
594                                 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
595
596                         total_listed_players = 0;
597                         FOR_EACH_CLIENT(tmp_player)
598                         {
599                                 is_bot = (IS_BOT_CLIENT(tmp_player));
600
601                                 if(is_bot)
602                                 {
603                                         tmp_netaddress = "null/botclient";
604                                         tmp_crypto_idfp = "null/botclient";
605                                 }
606                                 else if(privacy)
607                                 {
608                                         tmp_netaddress = "hidden";
609                                         tmp_crypto_idfp = "hidden";
610                                 }
611                                 else
612                                 {
613                                         tmp_netaddress = tmp_player.netaddress;
614                                         tmp_crypto_idfp = tmp_player.crypto_idfp;
615                                 }
616
617                                 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
618                                         num_for_edict(tmp_player),
619                                         tmp_player.netname,
620                                         tmp_player.ping,
621                                         tmp_player.ping_packetloss,
622                                         process_time(1, time - tmp_player.jointime),
623                                         tmp_netaddress,
624                                         tmp_crypto_idfp));
625
626                                 ++total_listed_players;
627                         }
628
629                         print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
630
631                         return; // never fall through to usage
632                 }
633
634                 default:
635                 case CMD_REQUEST_USAGE:
636                 {
637                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
638                         print_to(caller, "  Where 'separator' is the optional string to separate the values with, default is a space.");
639                         return;
640                 }
641         }
642 }
643
644 /* use this when creating a new command, making sure to place it in alphabetical order... also,
645 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
646 void CommonCommand_(float request, entity caller)
647 {
648         switch(request)
649         {
650                 case CMD_REQUEST_COMMAND:
651                 {
652
653                         return; // never fall through to usage
654                 }
655
656                 default:
657                 case CMD_REQUEST_USAGE:
658                 {
659                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
660                         print_to(caller, "  No arguments required.");
661                         return;
662                 }
663         }
664 }
665 */