3 #include <server/client.qh>
4 #include <common/weapons/_all.qh>
5 #include <common/stats.qh>
6 #include <server/world.qh>
7 #include <server/miscfunctions.qh>
9 #include <common/command/_mod.qh>
12 #include "../scores.qh"
14 #include <common/monsters/_mod.qh>
15 #include <common/notifications/all.qh>
16 #include <lib/warpzone/common.qh>
19 // ====================================================
20 // Shared code for server commands, written by Samual
21 // Last updated: December 27th, 2011
22 // ====================================================
24 // select the proper prefix for usage and other messages
25 string GetCommandPrefix(entity caller)
27 if (caller) return "cmd";
31 // if client return player nickname, or if server return admin nickname
32 string GetCallerName(entity caller)
34 if (caller) return playername(caller, false);
35 else return ((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : "SERVER ADMIN"); // autocvar_hostname
38 // verify that the client provided is acceptable for kicking
39 float VerifyKickableEntity(entity client)
41 if (!IS_REAL_CLIENT(client)) return CLIENT_NOT_REAL;
42 return CLIENT_ACCEPTABLE;
45 // verify that the client provided is acceptable for use
46 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
48 if (!IS_CLIENT(client)) return CLIENT_DOESNT_EXIST;
49 else if (must_be_real && !IS_REAL_CLIENT(client)) return CLIENT_NOT_REAL;
50 else if (must_be_bots && !IS_BOT_CLIENT(client)) return CLIENT_NOT_BOT;
52 return CLIENT_ACCEPTABLE;
55 // if the client is not acceptable, return a string to be used for error messages
56 string GetClientErrorString_color(float clienterror, string original_input, string col)
60 case CLIENT_DOESNT_EXIST:
61 { return strcat(col, "Client '", original_input, col, "' doesn't exist");
64 { return strcat(col, "Client '", original_input, col, "' is not real");
67 { return strcat(col, "Client '", original_input, col, "' is not a bot");
70 { return "Incorrect usage of GetClientErrorString";
75 // is this entity number even in the possible range of entities?
76 float VerifyClientNumber(float tmp_number)
78 if ((tmp_number < 1) || (tmp_number > maxclients)) return false;
82 entity GetIndexedEntity(int argc, float start_index)
85 float tmp_number, index;
92 if (argc > start_index)
94 if (substring(argv(index), 0, 1) == "#")
96 tmp_string = substring(argv(index), 1, -1);
99 if (tmp_string != "") // is it all one token? like #1
101 tmp_number = stof(tmp_string);
103 else if (argc > index) // no, it's two tokens? # 1
105 tmp_number = stof(argv(index));
113 else // maybe it's ONLY a number?
115 tmp_number = stof(argv(index));
119 if (VerifyClientNumber(tmp_number))
121 selection = edict_num(tmp_number); // yes, it was a number
123 else // no, maybe it's a name?
125 FOREACH_CLIENT(true, {
126 if(strdecolorize(it.netname) == strdecolorize(argv(start_index)))
129 break; // no reason to keep looking
133 index = (start_index + 1);
138 // print(strcat("start_index: ", ftos(start_index), ", next_token: ", ftos(next_token), ", edict: ", ftos(num_for_edict(selection)), ".\n"));
142 // find a player which matches the input string, and return their entity
143 entity GetFilteredEntity(string input)
148 if (substring(input, 0, 1) == "#") tmp_number = stof(substring(input, 1, -1));
149 else tmp_number = stof(input);
151 if (VerifyClientNumber(tmp_number))
153 selection = edict_num(tmp_number);
158 FOREACH_CLIENT(true, {
159 if(strdecolorize(it.netname) == strdecolorize(input))
162 break; // no reason to keep looking
170 // same thing, but instead return their edict number
171 float GetFilteredNumber(string input)
173 entity selection = GetFilteredEntity(input);
176 output = etof(selection);
181 // switch between sprint and print depending on whether the receiver is the server or a player
182 void print_to(entity to, string input)
184 if (to) sprint(to, strcat(input, "\n"));
185 else print(input, "\n");
188 // ==========================================
189 // Supporting functions for common commands
190 // ==========================================
192 // used by CommonCommand_timeout() and CommonCommand_timein() to handle game pausing and messaging and such.
193 void timeout_handler_reset(entity this)
195 timeout_caller = NULL;
197 timeout_leadtime = 0;
202 void timeout_handler_think(entity this)
204 switch (timeout_status)
208 if (timeout_time > 0) // countdown is still going
210 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_TIMEOUT_ENDING, timeout_time);
212 if (timeout_time == autocvar_sv_timeout_resumetime) // play a warning sound when only <sv_timeout_resumetime> seconds are left
213 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_PREPARE);
215 this.nextthink = time + TIMEOUT_SLOWMO_VALUE; // think again in one second
216 timeout_time -= 1; // decrease the time counter
218 else // time to end the timeout
220 Kill_Notification(NOTIF_ALL, NULL, MSG_CENTER, CPID_TIMEIN);
221 timeout_status = TIMEOUT_INACTIVE;
223 // reset the slowmo value back to normal
224 cvar_set("slowmo", ftos(orig_slowmo));
226 // unlock the view for players so they can move around again
227 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
231 timeout_handler_reset(this);
237 case TIMEOUT_LEADTIME:
239 if (timeout_leadtime > 0) // countdown is still going
241 Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_TIMEOUT_BEGINNING, timeout_leadtime);
243 this.nextthink = time + 1; // think again in one second
244 timeout_leadtime -= 1; // decrease the time counter
246 else // time to begin the timeout
248 timeout_status = TIMEOUT_ACTIVE;
250 // set the slowmo value to the timeout default slowmo value
251 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
253 // reset all the flood variables
254 FOREACH_CLIENT(true, {
255 it.nickspamcount = it.nickspamtime = it.floodcontrol_chat =
256 it.floodcontrol_chatteam = it.floodcontrol_chattell =
257 it.floodcontrol_voice = it.floodcontrol_voiceteam = 0;
260 // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
261 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
262 it.lastV_angle = it.v_angle;
265 this.nextthink = time; // think again next frame to handle it under TIMEOUT_ACTIVE code
272 case TIMEOUT_INACTIVE:
275 timeout_handler_reset(this);
282 // ===================================================
283 // Common commands used in both sv_cmd.qc and cmd.qc
284 // ===================================================
286 void CommonCommand_cvar_changes(int request, entity caller)
290 case CMD_REQUEST_COMMAND:
292 print_to(caller, cvar_changes);
293 return; // never fall through to usage
297 case CMD_REQUEST_USAGE:
299 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
300 print_to(caller, " No arguments required.");
301 print_to(caller, "See also: ^2cvar_purechanges^7");
307 void CommonCommand_cvar_purechanges(int request, entity caller)
311 case CMD_REQUEST_COMMAND:
313 print_to(caller, cvar_purechanges);
314 return; // never fall through to usage
318 case CMD_REQUEST_USAGE:
320 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
321 print_to(caller, " No arguments required.");
322 print_to(caller, "See also: ^2cvar_changes^7");
328 void CommonCommand_editmob(int request, entity caller, int argc)
332 case CMD_REQUEST_COMMAND:
334 if (autocvar_g_campaign) { print_to(caller, "Monster editing is disabled in singleplayer"); return; }
335 // no checks for g_monsters here, as it may be toggled mid match which existing monsters
339 makevectors(caller.v_angle);
340 WarpZone_TraceLine(caller.origin + caller.view_ofs, caller.origin + caller.view_ofs + v_forward * 100, MOVE_NORMAL, caller);
343 entity mon = trace_ent;
344 bool is_visible = IS_MONSTER(mon);
345 string argument = argv(2);
351 if (!caller) { print_to(caller, "Only players can edit monsters"); return; }
352 if (!argument) break; // escape to usage
353 if (!autocvar_g_monsters_edit) { print_to(caller, "Monster editing is disabled"); return; }
354 if (mon.realowner != caller && autocvar_g_monsters_edit < 2) { print_to(caller, "This monster does not belong to you"); return; }
355 if (!is_visible) { print_to(caller, "You must look at your monster to edit it"); return; }
357 string mon_oldname = mon.monster_name;
359 mon.monster_name = argument;
360 if (mon.sprite) WaypointSprite_UpdateSprites(mon.sprite, WP_Monster, WP_Null, WP_Null);
361 print_to(caller, sprintf("Your pet '%s' is now known as '%s'", mon_oldname, mon.monster_name));
366 if (!caller) { print_to(caller, "Only players can spawn monsters"); return; }
367 if (!argv(2)) break; // escape to usage
369 int moveflag, tmp_moncount = 0;
370 string arg_lower = strtolower(argument);
371 moveflag = (argv(3)) ? stof(argv(3)) : 1; // follow owner if not defined
373 if (arg_lower == "list") { print_to(caller, monsterlist_reply); return; }
375 IL_EACH(g_monsters, it.realowner == caller,
380 if (!autocvar_g_monsters) { print_to(caller, "Monsters are disabled"); return; }
381 if (autocvar_g_monsters_max <= 0 || autocvar_g_monsters_max_perplayer <= 0) { print_to(caller, "Monster spawning is disabled"); return; }
382 if (!IS_PLAYER(caller)) { print_to(caller, "You must be playing to spawn a monster"); return; }
383 if (MUTATOR_CALLHOOK(AllowMobSpawning, caller)) { print_to(caller, M_ARGV(1, string)); return; }
384 if (caller.vehicle) { print_to(caller, "You can't spawn monsters while driving a vehicle"); return; }
385 if (STAT(FROZEN, caller)) { print_to(caller, "You can't spawn monsters while frozen"); return; }
386 if (IS_DEAD(caller)) { print_to(caller, "You can't spawn monsters while dead"); return; }
387 if (tmp_moncount >= autocvar_g_monsters_max) { print_to(caller, "The maximum monster count has been reached"); return; }
388 if (tmp_moncount >= autocvar_g_monsters_max_perplayer) { print_to(caller, "You can't spawn any more monsters"); return; }
391 FOREACH(Monsters, it != MON_Null && it.netname == arg_lower,
397 if (!found && arg_lower != "random" && arg_lower != "anyrandom") { print_to(caller, "Invalid monster"); return; }
400 WarpZone_TraceBox(CENTER_OR_VIEWOFS(caller), caller.mins, caller.maxs, CENTER_OR_VIEWOFS(caller) + v_forward * 150, true, caller);
401 mon = spawnmonster(spawn(), arg_lower, MON_Null, caller, caller, trace_endpos, false, false, moveflag);
402 print_to(caller, strcat("Spawned ", mon.monster_name));
407 if (!caller) { print_to(caller, "Only players can kill monsters"); return; }
408 if (mon.realowner != caller && autocvar_g_monsters_edit < 2) { print_to(caller, "This monster does not belong to you"); return; }
409 if (!is_visible) { print_to(caller, "You must look at your monster to edit it"); return; }
411 Damage(mon, NULL, NULL, GetResource(mon, RES_HEALTH) + mon.max_health + 200, DEATH_KILL.m_id, DMG_NOWEP, mon.origin, '0 0 0');
412 print_to(caller, strcat("Your pet '", mon.monster_name, "' has been brutally mutilated"));
417 if (!caller) { print_to(caller, "Only players can edit monsters"); return; }
418 if (!argument) break; // escape to usage
419 if (!autocvar_g_monsters_edit) { print_to(caller, "Monster editing is disabled"); return; }
420 if (!is_visible) { print_to(caller, "You must look at your monster to edit it"); return; }
421 if (mon.realowner != caller && autocvar_g_monsters_edit < 2) { print_to(caller, "This monster does not belong to you"); return; }
422 if (mon.monsterdef == MON_MAGE) { print_to(caller, "Mage skins can't be changed"); return; } // TODO
424 mon.skin = stof(argument);
425 print_to(caller, strcat("Monster skin successfully changed to ", ftos(mon.skin)));
430 if (!caller) { print_to(caller, "Only players can edit monsters"); return; }
431 if (!argument) break; // escape to usage
432 if (!autocvar_g_monsters_edit) { print_to(caller, "Monster editing is disabled"); return; }
433 if (!is_visible) { print_to(caller, "You must look at your monster to edit it"); return; }
434 if (mon.realowner != caller && autocvar_g_monsters_edit < 2) { print_to(caller, "This monster does not belong to you"); return; }
436 mon.monster_moveflags = stof(argument);
437 print_to(caller, strcat("Monster move target successfully changed to ", ftos(mon.monster_moveflags)));
442 if (caller) { print_to(caller, "This command is not available to players"); return; }
443 if (MUTATOR_CALLHOOK(AllowMobButcher)) { LOG_INFO(M_ARGV(0, string)); return; }
445 int tmp_remcount = 0;
447 IL_EACH(g_monsters, true,
452 IL_CLEAR(g_monsters);
454 monsters_total = monsters_killed = totalspawned = 0;
456 print_to(caller, (tmp_remcount) ? sprintf("Killed %d monster%s", tmp_remcount, (tmp_remcount == 1) ? "" : "s") : "No monsters to kill");
463 case CMD_REQUEST_USAGE:
465 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " editmob command [arguments]"));
466 print_to(caller, " Where 'command' can be butcher spawn skin movetarget kill name");
467 print_to(caller, " spawn, skin, movetarget and name require 'arguments'");
468 print_to(caller, " spawn also takes arguments list and random");
469 print_to(caller, " Monster will follow owner if third argument of spawn command is not defined");
475 void CommonCommand_info(int request, entity caller, int argc)
479 case CMD_REQUEST_COMMAND:
481 string command = cvar_string(strcat("sv_info_", argv(1)));
483 if (command) wordwrap_sprint(caller, command, 1000);
484 else print_to(caller, "ERROR: unsupported info command");
486 return; // never fall through to usage
490 case CMD_REQUEST_USAGE:
492 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
493 print_to(caller, " Where 'request' is the suffixed string appended onto the request for cvar.");
499 void CommonCommand_ladder(int request, entity caller)
503 case CMD_REQUEST_COMMAND:
505 print_to(caller, ladder_reply);
506 return; // never fall through to usage
510 case CMD_REQUEST_USAGE:
512 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
513 print_to(caller, " No arguments required.");
519 void CommonCommand_lsmaps(int request, entity caller)
523 case CMD_REQUEST_COMMAND:
525 print_to(caller, lsmaps_reply);
526 return; // never fall through to usage
530 case CMD_REQUEST_USAGE:
532 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
533 print_to(caller, " No arguments required.");
539 void CommonCommand_printmaplist(int request, entity caller)
543 case CMD_REQUEST_COMMAND:
545 print_to(caller, maplist_reply);
546 return; // never fall through to usage
550 case CMD_REQUEST_USAGE:
552 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " printmaplist"));
553 print_to(caller, " No arguments required.");
559 void CommonCommand_rankings(int request, entity caller)
563 case CMD_REQUEST_COMMAND:
565 print_to(caller, rankings_reply);
566 return; // never fall through to usage
570 case CMD_REQUEST_USAGE:
572 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
573 print_to(caller, " No arguments required.");
579 void CommonCommand_records(int request, entity caller)
583 case CMD_REQUEST_COMMAND:
585 int num = stoi(argv(1));
586 if(num > 0 && num <= 10 && records_reply[num - 1] != "")
587 print_to(caller, records_reply[num - 1]);
590 for (int i = 0; i < 10; ++i)
591 if (records_reply[i] != "") print_to(caller, records_reply[i]);
594 return; // never fall through to usage
598 case CMD_REQUEST_USAGE:
600 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
601 print_to(caller, " No arguments required.");
607 void CommonCommand_teamstatus(int request, entity caller)
611 case CMD_REQUEST_COMMAND:
613 Score_NicePrint(caller);
614 return; // never fall through to usage
618 case CMD_REQUEST_USAGE:
620 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
621 print_to(caller, " No arguments required.");
627 void CommonCommand_time(int request, entity caller)
631 case CMD_REQUEST_COMMAND:
633 print_to(caller, strcat("time = ", ftos(time)));
634 print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
635 print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
636 print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
637 print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
638 print_to(caller, strcat("localtime = ", strftime(true, "%a %b %d %H:%M:%S %Z %Y")));
639 print_to(caller, strcat("gmtime = ", strftime(false, "%a %b %d %H:%M:%S %Z %Y")));
644 case CMD_REQUEST_USAGE:
646 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
647 print_to(caller, " No arguments required.");
653 void CommonCommand_timein(int request, entity caller)
657 case CMD_REQUEST_COMMAND:
659 if (!caller || autocvar_sv_timeout)
661 if (!timeout_status) { print_to(caller, "^7Error: There is no active timeout called."); }
662 else if (caller && (caller != timeout_caller))
664 print_to(caller, "^7Error: You are not allowed to stop the active timeout.");
667 else // everything should be okay, continue aborting timeout
669 switch (timeout_status)
671 case TIMEOUT_LEADTIME:
673 timeout_status = TIMEOUT_INACTIVE;
675 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
676 bprint(strcat("^7The timeout was aborted by ", GetCallerName(caller), " !\n"));
682 timeout_time = autocvar_sv_timeout_resumetime;
683 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
684 bprint(strcat("^1Attention: ^7", GetCallerName(caller), " resumed the game! Prepare for battle!\n"));
688 default: LOG_TRACE("timeout status was inactive, but this code was executed anyway?");
693 else { print_to(caller, "^1Timeins are not allowed to be called, enable them with sv_timeout 1.\n"); }
695 return; // never fall through to usage
699 case CMD_REQUEST_USAGE:
701 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
702 print_to(caller, " No arguments required.");
708 void CommonCommand_timeout(int request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
712 case CMD_REQUEST_COMMAND:
714 if (!caller || autocvar_sv_timeout)
716 float last_possible_timeout = ((autocvar_timelimit * 60) - autocvar_sv_timeout_leadtime - 1);
718 if (timeout_status) { print_to(caller, "^7Error: A timeout is already active."); }
719 else if (vote_called)
721 print_to(caller, "^7Error: You can not call a timeout while a vote is active.");
723 else if (warmup_stage && !g_warmup_allow_timeout)
725 print_to(caller, "^7Error: You can not call a timeout in warmup-stage.");
727 else if (time < game_starttime)
729 print_to(caller, "^7Error: You can not call a timeout while the map is being restarted.");
731 else if (caller && (CS(caller).allowed_timeouts < 1))
733 print_to(caller, "^7Error: You already used all your timeout calls for this map.");
735 else if (caller && !IS_PLAYER(caller))
737 print_to(caller, "^7Error: You must be a player to call a timeout.");
739 else if ((autocvar_timelimit) && (last_possible_timeout < time - game_starttime))
741 print_to(caller, "^7Error: It is too late to call a timeout now!");
744 else // everything should be okay, proceed with starting the timeout
746 if (caller) CS(caller).allowed_timeouts -= 1;
747 // write a bprint who started the timeout (and how many they have left)
748 bprint(GetCallerName(caller), " ^7called a timeout", (caller ? strcat(" (", ftos(CS(caller).allowed_timeouts), " timeout(s) left)") : ""), "!\n");
750 timeout_status = TIMEOUT_LEADTIME;
751 timeout_caller = caller;
752 timeout_time = autocvar_sv_timeout_length;
753 timeout_leadtime = autocvar_sv_timeout_leadtime;
755 timeout_handler = spawn();
756 setthink(timeout_handler, timeout_handler_think);
757 timeout_handler.nextthink = time; // always let the entity think asap
759 Send_Notification(NOTIF_ALL, NULL, MSG_ANNCE, ANNCE_TIMEOUT);
762 else { print_to(caller, "^1Timeouts are not allowed to be called, enable them with sv_timeout 1.\n"); }
764 return; // never fall through to usage
768 case CMD_REQUEST_USAGE:
770 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
771 print_to(caller, " No arguments required.");
777 void CommonCommand_who(int request, entity caller, int argc)
781 case CMD_REQUEST_COMMAND:
783 float total_listed_players, is_bot;
785 float privacy = (caller && autocvar_sv_status_privacy);
786 string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
787 string tmp_netaddress, tmp_crypto_idfp;
789 print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : ""), ":"));
790 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
791 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
793 total_listed_players = 0;
794 FOREACH_CLIENT(true, {
795 is_bot = (IS_BOT_CLIENT(it));
799 tmp_netaddress = "null/botclient";
800 tmp_crypto_idfp = "null/botclient";
804 tmp_netaddress = "hidden";
805 tmp_crypto_idfp = "hidden";
809 tmp_netaddress = it.netaddress;
810 tmp_crypto_idfp = it.crypto_idfp;
813 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
817 CS(it).ping_packetloss,
818 process_time(1, time - CS(it).jointime),
822 ++total_listed_players;
825 print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
827 return; // never fall through to usage
831 case CMD_REQUEST_USAGE:
833 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
834 print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
840 /* use this when creating a new command, making sure to place it in alphabetical order... also,
841 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
842 void CommonCommand_(int request, entity caller)
846 case CMD_REQUEST_COMMAND:
849 return; // never fall through to usage
853 case CMD_REQUEST_USAGE:
855 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
856 print_to(caller, " No arguments required.");