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