Updated for latest hl2sdk-dota changes.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "PlayerManager.h"
33 #include "AdminCache.h"
34 #include "sm_stringutil.h"
35 #include "HalfLife2.h"
36 #include "sourcemod.h"
37 #include "ChatTriggers.h"
38 #include <inetchannel.h>
41 ConVar sm_show_activity("sm_show_activity", "13", FCVAR_SPONLY, "Activity display setting (see sourcemod.cfg)");
43 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
47 return g_Players.NumPlayers();
50 int maxplayers = g_Players.MaxClients();
52 for (int i=1; i<=maxplayers; ++i)
54 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
55 if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
61 return (g_Players.NumPlayers() + count);
64 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
66 return g_Players.MaxClients();
69 static cell_t sm_GetMaxHumanPlayers(IPluginContext *pCtx, const cell_t *params)
73 #if SOURCE_ENGINE >= SE_LEFT4DEAD
74 maxHumans = serverClients->GetMaxHumanPlayers();
79 return g_Players.MaxClients();
85 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
87 int index = params[1];
91 static ConVar *hostname = NULL;
94 hostname = icvar->FindVar("hostname");
97 return pCtx->ThrowNativeError("Could not find \"hostname\" cvar");
100 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), hostname->GetString(), NULL);
104 if ((index < 1) || (index > g_Players.GetMaxClients()))
106 return pCtx->ThrowNativeError("Client index %d is invalid", index);
109 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
110 if (!pPlayer->IsConnected())
112 return pCtx->ThrowNativeError("Client %d is not connected", index);
115 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
119 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
121 int index = params[1];
122 if ((index < 1) || (index > g_Players.GetMaxClients()))
124 return pCtx->ThrowNativeError("Client index %d is invalid", index);
127 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
128 if (!pPlayer->IsConnected())
130 return pCtx->ThrowNativeError("Client %d is not connected", index);
134 strcpy(buf, pPlayer->GetIPAddress());
136 if (params[4] && (ptr = strchr(buf, ':')))
141 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
145 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
147 int index = params[1];
148 if ((index < 1) || (index > g_Players.GetMaxClients()))
150 return pCtx->ThrowNativeError("Client index %d is invalid", index);
153 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
154 if (!pPlayer->IsConnected())
156 return pCtx->ThrowNativeError("Client %d is not connected", index);
159 bool validate = true;
162 validate = !!params[4];
165 const char *authstr = pPlayer->GetAuthString(validate);
167 if (!authstr || authstr[0] == '\0')
172 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), authstr);
177 static cell_t sm_GetSteamAccountID(IPluginContext *pCtx, const cell_t *params)
179 int index = params[1];
180 if ((index < 1) || (index > g_Players.GetMaxClients()))
182 return pCtx->ThrowNativeError("Client index %d is invalid", index);
185 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
186 if (!pPlayer->IsConnected())
188 return pCtx->ThrowNativeError("Client %d is not connected", index);
191 return pPlayer->GetSteamAccountID(!!params[2]);
194 static cell_t sm_IsClientConnected(IPluginContext *pCtx, const cell_t *params)
196 int index = params[1];
197 if ((index < 1) || (index > g_Players.GetMaxClients()))
199 return pCtx->ThrowNativeError("Client index %d is invalid", index);
202 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
205 static cell_t sm_IsClientInGame(IPluginContext *pCtx, const cell_t *params)
207 int index = params[1];
208 if ((index < 1) || (index > g_Players.GetMaxClients()))
210 return pCtx->ThrowNativeError("Client index %d is invalid", index);
213 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
216 static cell_t sm_IsClientAuthorized(IPluginContext *pCtx, const cell_t *params)
218 int index = params[1];
219 if ((index < 1) || (index > g_Players.GetMaxClients()))
221 return pCtx->ThrowNativeError("Client index %d is invalid", index);
224 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
227 static cell_t sm_IsClientFakeClient(IPluginContext *pCtx, const cell_t *params)
229 int index = params[1];
230 if ((index < 1) || (index > g_Players.GetMaxClients()))
232 return pCtx->ThrowNativeError("Client index %d is invalid", index);
235 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
236 if (!pPlayer->IsConnected())
238 return pCtx->ThrowNativeError("Client %d is not connected", index);
241 return (pPlayer->IsFakeClient()) ? 1 : 0;
244 static cell_t sm_IsClientSourceTV(IPluginContext *pCtx, const cell_t *params)
246 int index = params[1];
247 if ((index < 1) || (index > g_Players.GetMaxClients()))
249 return pCtx->ThrowNativeError("Client index %d is invalid", index);
252 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
253 if (!pPlayer->IsConnected())
255 return pCtx->ThrowNativeError("Client %d is not connected", index);
258 return (pPlayer->IsSourceTV()) ? 1 : 0;
261 static cell_t sm_IsClientReplay(IPluginContext *pCtx, const cell_t *params)
263 int index = params[1];
264 if ((index < 1) || (index > g_Players.GetMaxClients()))
266 return pCtx->ThrowNativeError("Client index %d is invalid", index);
269 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
270 if (!pPlayer->IsConnected())
272 return pCtx->ThrowNativeError("Client %d is not connected", index);
275 return (pPlayer->IsReplay()) ? 1 : 0;
278 static cell_t IsClientObserver(IPluginContext *pContext, const cell_t *params)
280 int client = params[1];
282 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
285 return pContext->ThrowNativeError("Client index %d is invalid", client);
286 } else if (!pPlayer->IsInGame()) {
287 return pContext->ThrowNativeError("Client %d is not in game", client);
290 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
293 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
296 return pInfo->IsObserver() ? 1 : 0;
299 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
301 int client = params[1];
302 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
305 return pContext->ThrowNativeError("Client index %d is invalid", client);
307 if (!pPlayer->IsConnected())
309 return pContext->ThrowNativeError("Client %d is not connected", client);
313 pContext->LocalToString(params[2], &key);
315 const char *val = engine->GetClientConVarValue(client, key);
321 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
325 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
327 int client = params[1];
328 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
331 return pContext->ThrowNativeError("Client index %d is invalid", client);
333 if (!pPlayer->IsConnected())
335 return pContext->ThrowNativeError("Client %d is not connected", client);
337 if (!g_Admins.IsValidAdmin(params[2]) && params[2] != INVALID_ADMIN_ID)
339 return pContext->ThrowNativeError("AdminId %x is invalid", params[2]);
342 pPlayer->SetAdminId(params[2], params[3] ? true : false);
347 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
349 int client = params[1];
350 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
353 return pContext->ThrowNativeError("Client index %d is invalid", client);
355 if (!pPlayer->IsConnected())
357 return pContext->ThrowNativeError("Client %d is not connected", client);
360 return pPlayer->GetAdminId();
363 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
365 int client = params[1];
366 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
369 return pContext->ThrowNativeError("Client index %d is invalid", client);
371 if (!pPlayer->IsConnected())
373 return pContext->ThrowNativeError("Client %d is not connected", client);
377 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
379 id = g_Admins.CreateAdmin(NULL);
380 pPlayer->SetAdminId(id, true);
384 for (int i=2; i<=params[0]; i++)
386 pContext->LocalToPhysAddr(params[i], &addr);
387 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
393 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
395 int client = params[1];
396 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
399 return pContext->ThrowNativeError("Client index %d is invalid", client);
401 if (!pPlayer->IsConnected())
403 return pContext->ThrowNativeError("Client %d is not connected", client);
407 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
413 for (int i=2; i<=params[0]; i++)
415 pContext->LocalToPhysAddr(params[i], &addr);
416 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
422 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
424 int client = params[1];
425 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
428 return pContext->ThrowNativeError("Client index %d is invalid", client);
430 if (!pPlayer->IsConnected())
432 return pContext->ThrowNativeError("Client %d is not connected", client);
436 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
438 id = g_Admins.CreateAdmin(NULL);
439 pPlayer->SetAdminId(id, true);
442 g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
447 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
449 int client = params[1];
450 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
453 return pContext->ThrowNativeError("Client index %d is invalid", client);
455 if (!pPlayer->IsConnected())
457 return pContext->ThrowNativeError("Client %d is not connected", client);
461 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
466 return g_Admins.GetAdminFlags(id, Access_Effective);
469 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
471 int client = params[1];
472 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
475 return pContext->ThrowNativeError("Client index %d is invalid", client);
477 if (!pPlayer->IsConnected())
479 return pContext->ThrowNativeError("Client %d is not connected", client);
482 return GetPlayerUserId(pPlayer->GetEdict());
485 static cell_t CanUserTarget(IPluginContext *pContext, const cell_t *params)
487 int client = params[1];
488 int target = params[2];
495 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
498 return pContext->ThrowNativeError("Client index %d is invalid", client);
499 } else if (!pPlayer->IsConnected()) {
500 return pContext->ThrowNativeError("Client %d is not connected", client);
503 CPlayer *pTarget = g_Players.GetPlayerByIndex(target);
506 return pContext->ThrowNativeError("Client index %d is invalid", target);
507 } else if (!pTarget->IsConnected()) {
508 return pContext->ThrowNativeError("Client %d is not connected", target);
511 return g_Admins.CanAdminTarget(pPlayer->GetAdminId(), pTarget->GetAdminId()) ? 1 : 0;
514 static cell_t GetClientTeam(IPluginContext *pContext, const cell_t *params)
516 int client = params[1];
518 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
521 return pContext->ThrowNativeError("Client index %d is invalid", client);
522 } else if (!pPlayer->IsInGame()) {
523 return pContext->ThrowNativeError("Client %d is not in game", client);
526 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
529 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
532 return pInfo->GetTeamIndex();
535 static cell_t GetFragCount(IPluginContext *pContext, const cell_t *params)
537 int client = params[1];
539 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
542 return pContext->ThrowNativeError("Client index %d is invalid", client);
543 } else if (!pPlayer->IsInGame()) {
544 return pContext->ThrowNativeError("Client %d is not in game", client);
547 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
550 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
553 return pInfo->GetFragCount();
556 static cell_t GetDeathCount(IPluginContext *pContext, const cell_t *params)
558 int client = params[1];
560 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
563 return pContext->ThrowNativeError("Client index %d is invalid", client);
564 } else if (!pPlayer->IsInGame()) {
565 return pContext->ThrowNativeError("Client %d is not in game", client);
568 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
571 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
574 return pInfo->GetDeathCount();
577 static cell_t GetArmorValue(IPluginContext *pContext, const cell_t *params)
579 int client = params[1];
581 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
584 return pContext->ThrowNativeError("Client index %d is invalid", client);
585 } else if (!pPlayer->IsInGame()) {
586 return pContext->ThrowNativeError("Client %d is not in game", client);
589 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
592 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
595 return pInfo->GetArmorValue();
598 static cell_t GetAbsOrigin(IPluginContext *pContext, const cell_t *params)
600 int client = params[1];
602 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
605 return pContext->ThrowNativeError("Client index %d is invalid", client);
606 } else if (!pPlayer->IsInGame()) {
607 return pContext->ThrowNativeError("Client %d is not in game", client);
610 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
613 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
617 pContext->LocalToPhysAddr(params[2], &pVec);
619 Vector vec = pInfo->GetAbsOrigin();
620 pVec[0] = sp_ftoc(vec.x);
621 pVec[1] = sp_ftoc(vec.y);
622 pVec[2] = sp_ftoc(vec.z);
627 static cell_t GetAbsAngles(IPluginContext *pContext, const cell_t *params)
629 int client = params[1];
631 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
634 return pContext->ThrowNativeError("Client index %d is invalid", client);
635 } else if (!pPlayer->IsInGame()) {
636 return pContext->ThrowNativeError("Client %d is not in game", client);
639 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
642 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
646 pContext->LocalToPhysAddr(params[2], &pAng);
648 QAngle ang = pInfo->GetAbsAngles();
649 pAng[0] = sp_ftoc(ang.x);
650 pAng[1] = sp_ftoc(ang.y);
651 pAng[2] = sp_ftoc(ang.z);
656 static cell_t GetPlayerMins(IPluginContext *pContext, const cell_t *params)
658 int client = params[1];
660 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
663 return pContext->ThrowNativeError("Client index %d is invalid", client);
664 } else if (!pPlayer->IsInGame()) {
665 return pContext->ThrowNativeError("Client %d is not in game", client);
668 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
671 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
675 pContext->LocalToPhysAddr(params[2], &pVec);
677 Vector vec = pInfo->GetPlayerMins();
678 pVec[0] = sp_ftoc(vec.x);
679 pVec[1] = sp_ftoc(vec.y);
680 pVec[2] = sp_ftoc(vec.z);
685 static cell_t GetPlayerMaxs(IPluginContext *pContext, const cell_t *params)
687 int client = params[1];
689 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
692 return pContext->ThrowNativeError("Client index %d is invalid", client);
693 } else if (!pPlayer->IsInGame()) {
694 return pContext->ThrowNativeError("Client %d is not in game", client);
697 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
700 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
704 pContext->LocalToPhysAddr(params[2], &pVec);
706 Vector vec = pInfo->GetPlayerMaxs();
707 pVec[0] = sp_ftoc(vec.x);
708 pVec[1] = sp_ftoc(vec.y);
709 pVec[2] = sp_ftoc(vec.z);
714 static cell_t GetWeaponName(IPluginContext *pContext, const cell_t *params)
716 int client = params[1];
718 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
721 return pContext->ThrowNativeError("Client index %d is invalid", client);
722 } else if (!pPlayer->IsInGame()) {
723 return pContext->ThrowNativeError("Client %d is not in game", client);
726 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
729 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
732 const char *weapon = pInfo->GetWeaponName();
733 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), weapon ? weapon : "", NULL);
738 static cell_t GetModelName(IPluginContext *pContext, const cell_t *params)
740 int client = params[1];
742 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
745 return pContext->ThrowNativeError("Client index %d is invalid", client);
746 } else if (!pPlayer->IsInGame()) {
747 return pContext->ThrowNativeError("Client %d is not in game", client);
750 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
753 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
756 const char *model = pInfo->GetModelName();
757 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), model ? model : "", NULL);
762 static cell_t GetHealth(IPluginContext *pContext, const cell_t *params)
764 int client = params[1];
766 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
769 return pContext->ThrowNativeError("Client index %d is invalid", client);
770 } else if (!pPlayer->IsInGame()) {
771 return pContext->ThrowNativeError("Client %d is not in game", client);
774 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
777 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
780 return pInfo->GetHealth();
783 static cell_t GetTimeConnected(IPluginContext *pContext, const cell_t *params)
785 int client = params[1];
787 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
790 return pContext->ThrowNativeError("Client index %d is invalid", client);
791 } else if (!pPlayer->IsInGame()) {
792 return pContext->ThrowNativeError("Client %d is not in game", client);
793 } else if (pPlayer->IsFakeClient()) {
794 return pContext->ThrowNativeError("Client %d is a bot", client);
797 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
803 return sp_ftoc(pInfo->GetTimeConnected());
806 static cell_t GetDataRate(IPluginContext *pContext, const cell_t *params)
808 int client = params[1];
810 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
813 return pContext->ThrowNativeError("Client index %d is invalid", client);
814 } else if (!pPlayer->IsInGame()) {
815 return pContext->ThrowNativeError("Client %d is not in game", client);
816 } else if (pPlayer->IsFakeClient()) {
817 return pContext->ThrowNativeError("Client %d is a bot", client);
820 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
826 return pInfo->GetDataRate();
829 static cell_t IsTimingOut(IPluginContext *pContext, const cell_t *params)
831 int client = params[1];
833 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
836 return pContext->ThrowNativeError("Client index %d is invalid", client);
837 } else if (!pPlayer->IsInGame()) {
838 return pContext->ThrowNativeError("Client %d is not in game", client);
839 } else if (pPlayer->IsFakeClient()) {
840 return pContext->ThrowNativeError("Client %d is a bot", client);
843 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
849 return pInfo->IsTimingOut() ? 1 : 0;
852 static cell_t GetLatency(IPluginContext *pContext, const cell_t *params)
854 int client = params[1];
857 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
860 return pContext->ThrowNativeError("Client index %d is invalid", client);
862 else if (!pPlayer->IsInGame())
864 return pContext->ThrowNativeError("Client %d is not in game", client);
866 else if (pPlayer->IsFakeClient())
868 return pContext->ThrowNativeError("Client %d is a bot", client);
871 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
877 if (params[2] == MAX_FLOWS)
879 value = pInfo->GetLatency(FLOW_INCOMING) + pInfo->GetLatency(FLOW_OUTGOING);
883 value = pInfo->GetLatency(params[2]);
886 return sp_ftoc(value);
889 static cell_t GetAvgLatency(IPluginContext *pContext, const cell_t *params)
891 int client = params[1];
894 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
897 return pContext->ThrowNativeError("Client index %d is invalid", client);
899 else if (!pPlayer->IsInGame())
901 return pContext->ThrowNativeError("Client %d is not in game", client);
903 else if (pPlayer->IsFakeClient())
905 return pContext->ThrowNativeError("Client %d is a bot", client);
908 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
914 if (params[2] == MAX_FLOWS)
916 value = pInfo->GetAvgLatency(FLOW_INCOMING) + pInfo->GetAvgLatency(FLOW_OUTGOING);
920 value = pInfo->GetAvgLatency(params[2]);
923 return sp_ftoc(value);
926 static cell_t GetAvgLoss(IPluginContext *pContext, const cell_t *params)
928 int client = params[1];
931 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
934 return pContext->ThrowNativeError("Client index %d is invalid", client);
936 else if (!pPlayer->IsInGame())
938 return pContext->ThrowNativeError("Client %d is not in game", client);
940 else if (pPlayer->IsFakeClient())
942 return pContext->ThrowNativeError("Client %d is a bot", client);
945 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
951 if (params[2] == MAX_FLOWS)
953 value = pInfo->GetAvgLoss(FLOW_INCOMING) + pInfo->GetAvgLoss(FLOW_OUTGOING);
957 value = pInfo->GetAvgLoss(params[2]);
960 return sp_ftoc(value);
963 static cell_t GetAvgChoke(IPluginContext *pContext, const cell_t *params)
965 int client = params[1];
968 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
971 return pContext->ThrowNativeError("Client index %d is invalid", client);
973 else if (!pPlayer->IsInGame())
975 return pContext->ThrowNativeError("Client %d is not in game", client);
977 else if (pPlayer->IsFakeClient())
979 return pContext->ThrowNativeError("Client %d is a bot", client);
982 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
988 if (params[2] == MAX_FLOWS)
990 value = pInfo->GetAvgChoke(FLOW_INCOMING) + pInfo->GetAvgChoke(FLOW_OUTGOING);
994 value = pInfo->GetAvgChoke(params[2]);
997 return sp_ftoc(value);
1000 static cell_t GetAvgData(IPluginContext *pContext, const cell_t *params)
1002 int client = params[1];
1005 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1008 return pContext->ThrowNativeError("Client index %d is invalid", client);
1010 else if (!pPlayer->IsInGame())
1012 return pContext->ThrowNativeError("Client %d is not in game", client);
1014 else if (pPlayer->IsFakeClient())
1016 return pContext->ThrowNativeError("Client %d is a bot", client);
1019 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1025 if (params[2] == MAX_FLOWS)
1027 value = pInfo->GetAvgData(FLOW_INCOMING) + pInfo->GetAvgData(FLOW_OUTGOING);
1031 value = pInfo->GetAvgData(params[2]);
1034 return sp_ftoc(value);
1037 static cell_t GetAvgPackets(IPluginContext *pContext, const cell_t *params)
1039 int client = params[1];
1042 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1045 return pContext->ThrowNativeError("Client index %d is invalid", client);
1047 else if (!pPlayer->IsInGame())
1049 return pContext->ThrowNativeError("Client %d is not in game", client);
1051 else if (pPlayer->IsFakeClient())
1053 return pContext->ThrowNativeError("Client %d is a bot", client);
1056 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1063 if (params[2] == MAX_FLOWS)
1065 value = pInfo->GetAvgPackets(FLOW_INCOMING) + pInfo->GetAvgPackets(FLOW_OUTGOING);
1069 value = pInfo->GetAvgPackets(params[2]);
1072 return sp_ftoc(value);
1075 static cell_t GetClientOfUserId(IPluginContext *pContext, const cell_t *params)
1077 return g_Players.GetClientOfUserId(params[1]);
1080 static cell_t _ShowActivity(IPluginContext *pContext,
1081 const cell_t *params,
1087 int value = sm_show_activity.GetInt();
1088 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1089 int client = params[1];
1091 const char *name = "Console";
1092 const char *sign = "ADMIN";
1093 bool display_in_chat = false;
1096 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1097 if (!pPlayer || !pPlayer->IsConnected())
1099 return pContext->ThrowNativeError("Client index %d is invalid", client);
1101 name = pPlayer->GetName();
1102 AdminId id = pPlayer->GetAdminId();
1103 if (id == INVALID_ADMIN_ID
1104 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1109 /* Display the message to the client? */
1110 if (replyto == SM_REPLY_CONSOLE)
1112 g_SourceMod.SetGlobalTarget(client);
1113 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1115 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1120 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1121 #if SOURCE_ENGINE == SE_DOTA
1122 engine->ClientPrintf(pPlayer->GetIndex(), message);
1124 engine->ClientPrintf(pPlayer->GetEdict(), message);
1126 display_in_chat = true;
1131 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1132 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1134 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1139 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1140 META_CONPRINT(message);
1148 int maxClients = g_Players.GetMaxClients();
1149 for (int i=1; i<=maxClients; i++)
1151 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1152 if (!pPlayer->IsInGame()
1153 || pPlayer->IsFakeClient()
1154 || (display_in_chat && i == client))
1158 AdminId id = pPlayer->GetAdminId();
1159 g_SourceMod.SetGlobalTarget(i);
1160 if (id == INVALID_ADMIN_ID
1161 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1163 /* Treat this as a normal user */
1164 if ((value & 1) || (value & 2))
1166 const char *newsign = sign;
1167 if ((value & 2) || (i == client))
1171 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1173 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1178 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1179 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1184 /* Treat this as an admin user */
1185 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1188 || ((value & 16) && is_root))
1190 const char *newsign = sign;
1191 if ((value & 8) || ((value & 16) && is_root) || (i == client))
1195 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1197 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1202 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1203 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1211 static cell_t _ShowActivity2(IPluginContext *pContext,
1212 const cell_t *params,
1218 int value = sm_show_activity.GetInt();
1219 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1220 int client = params[1];
1222 const char *name = "Console";
1223 const char *sign = "ADMIN";
1226 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1227 if (!pPlayer || !pPlayer->IsConnected())
1229 return pContext->ThrowNativeError("Client index %d is invalid", client);
1231 name = pPlayer->GetName();
1232 AdminId id = pPlayer->GetAdminId();
1233 if (id == INVALID_ADMIN_ID
1234 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1239 g_SourceMod.SetGlobalTarget(client);
1240 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1242 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1247 /* We don't display directly to the console because the chat text
1248 * simply gets added to the console, so we don't want it to print
1251 if (replyto == SM_REPLY_CONSOLE)
1254 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1255 engine->ClientPrintf(pPlayer->GetEdict(), message);
1257 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1258 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1262 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1263 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1268 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1269 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1271 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1276 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1277 META_CONPRINT(message);
1285 int maxClients = g_Players.GetMaxClients();
1286 for (int i=1; i<=maxClients; i++)
1288 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1289 if (!pPlayer->IsInGame()
1290 || pPlayer->IsFakeClient()
1295 AdminId id = pPlayer->GetAdminId();
1296 g_SourceMod.SetGlobalTarget(i);
1297 if (id == INVALID_ADMIN_ID
1298 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1300 /* Treat this as a normal user */
1301 if ((value & 1) || (value & 2))
1303 const char *newsign = sign;
1308 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1310 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1315 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1316 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1321 /* Treat this as an admin user */
1322 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1325 || ((value & 16) && is_root))
1327 const char *newsign = sign;
1328 if ((value & 8) || ((value & 16) && is_root))
1332 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1334 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1339 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1340 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1348 static cell_t ShowActivity(IPluginContext *pContext, const cell_t *params)
1350 return _ShowActivity(pContext, params, "[SM] ", 2);
1353 static cell_t ShowActivityEx(IPluginContext *pContext, const cell_t *params)
1356 pContext->LocalToString(params[2], &str);
1358 return _ShowActivity(pContext, params, str, 3);
1361 static cell_t ShowActivity2(IPluginContext *pContext, const cell_t *params)
1364 pContext->LocalToString(params[2], &str);
1366 return _ShowActivity2(pContext, params, str, 3);
1369 static cell_t KickClient(IPluginContext *pContext, const cell_t *params)
1371 int client = params[1];
1373 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1376 return pContext->ThrowNativeError("Client index %d is invalid", client);
1378 else if (!pPlayer->IsConnected())
1380 return pContext->ThrowNativeError("Client %d is not connected", client);
1383 /* Ignore duplicate kicks */
1384 if (pPlayer->IsInKickQueue())
1389 g_SourceMod.SetGlobalTarget(client);
1392 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1394 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1399 if (pPlayer->IsFakeClient())
1401 // Kick uses the kickid command for bots. It is already delayed
1402 // until the next frame unless someone flushes command buffer
1403 pPlayer->Kick(buffer);
1407 g_HL2.AddDelayedKick(client, pPlayer->GetUserId(), buffer);
1412 static cell_t KickClientEx(IPluginContext *pContext, const cell_t *params)
1414 int client = params[1];
1416 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1419 return pContext->ThrowNativeError("Client index %d is invalid", client);
1421 else if (!pPlayer->IsConnected())
1423 return pContext->ThrowNativeError("Client %d is not connected", client);
1426 g_SourceMod.SetGlobalTarget(client);
1429 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1431 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1436 pPlayer->Kick(buffer);
1441 static cell_t ChangeClientTeam(IPluginContext *pContext, const cell_t *params)
1443 int client = params[1];
1445 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1448 return pContext->ThrowNativeError("Client index %d is invalid", client);
1450 else if (!pPlayer->IsInGame())
1452 return pContext->ThrowNativeError("Client %d is not in game", client);
1455 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
1458 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
1461 pInfo->ChangeTeam(params[2]);
1466 static cell_t RunAdminCacheChecks(IPluginContext *pContext, const cell_t *params)
1468 int client = params[1];
1470 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1473 return pContext->ThrowNativeError("Client index %d is invalid", client);
1475 else if (!pPlayer->IsInGame())
1477 return pContext->ThrowNativeError("Client %d is not in game", client);
1479 else if (!pPlayer->IsAuthorized())
1481 return pContext->ThrowNativeError("Client %d is not authorized", client);
1484 AdminId id = pPlayer->GetAdminId();
1485 pPlayer->DoBasicAdminChecks();
1487 return (id != pPlayer->GetAdminId()) ? 1 : 0;
1490 static cell_t NotifyPostAdminCheck(IPluginContext *pContext, const cell_t *params)
1492 int client = params[1];
1494 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1497 return pContext->ThrowNativeError("Client index %d is invalid", client);
1499 else if (!pPlayer->IsInGame()) {
1500 return pContext->ThrowNativeError("Client %d is not in game", client);
1502 else if (!pPlayer->IsAuthorized())
1504 return pContext->ThrowNativeError("Client %d is not authorized", client);
1507 pPlayer->NotifyPostAdminChecks();
1512 static cell_t IsClientInKickQueue(IPluginContext *pContext, const cell_t *params)
1514 int client = params[1];
1516 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1520 return pContext->ThrowNativeError("Client index %d is invalid", client);
1522 else if (!pPlayer->IsConnected())
1524 return pContext->ThrowNativeError("Client %d is not in game", client);
1527 return pPlayer->IsInKickQueue() ? 1 : 0;
1530 static cell_t ProcessTargetString(IPluginContext *pContext, const cell_t *params)
1532 cmd_target_info_t info;
1534 pContext->LocalToString(params[1], (char **)&info.pattern);
1535 info.admin = params[2];
1536 pContext->LocalToPhysAddr(params[3], &info.targets);
1537 info.max_targets = params[4];
1538 info.flags = params[5];
1539 pContext->LocalToString(params[6], &info.target_name);
1540 info.target_name_maxlength = params[7];
1543 pContext->LocalToPhysAddr(params[8], &tn_is_ml);
1545 g_Players.ProcessCommandTarget(&info);
1547 if (info.target_name_style == COMMAND_TARGETNAME_ML)
1556 if (info.num_targets == 0)
1562 return info.num_targets;
1566 static cell_t FormatActivitySource(IPluginContext *pContext, const cell_t *params)
1573 const char *identity[2] = {"Console", "ADMIN"};
1578 if ((pTarget = g_Players.GetPlayerByIndex(target)) == NULL)
1580 return pContext->ThrowNativeError("Invalid client index %d", target);
1582 if (!pTarget->IsConnected())
1584 return pContext->ThrowNativeError("Client %d not connected", target);
1587 value = sm_show_activity.GetInt();
1593 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
1595 return pContext->ThrowNativeError("Invalid client index %d", client);
1597 if (!pPlayer->IsConnected())
1599 return pContext->ThrowNativeError("Client %d not connected", client);
1602 identity[0] = pPlayer->GetName();
1604 AdminId id = pPlayer->GetAdminId();
1605 if (id == INVALID_ADMIN_ID
1606 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1608 identity[1] = "PLAYER";
1613 bool bShowActivity = false;
1615 if ((aidTarget = pTarget->GetAdminId()) == INVALID_ADMIN_ID
1616 || !g_Admins.GetAdminFlag(aidTarget, Admin_Generic, Access_Effective))
1618 /* Treat this as a normal user */
1619 if ((value & 1) || (value & 2))
1621 if ((value & 2) || (target == client))
1625 bShowActivity = true;
1630 /* Treat this as an admin user */
1631 bool is_root = g_Admins.GetAdminFlag(aidTarget, Admin_Root, Access_Effective);
1634 || ((value & 16) && is_root))
1636 if ((value & 8) || ((value & 16) && is_root) || (target == client))
1640 bShowActivity = true;
1644 /* Otherwise, send it back to the script. */
1645 pContext->StringToLocalUTF8(params[3], params[4], identity[mode], NULL);
1647 return bShowActivity ? 1 : 0;
1650 static cell_t sm_GetClientSerial(IPluginContext *pContext, const cell_t *params)
1652 int client = params[1];
1654 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1657 return pContext->ThrowNativeError("Client index %d is invalid", client);
1660 return pPlayer->GetSerial();
1663 static cell_t sm_GetClientFromSerial(IPluginContext *pContext, const cell_t *params)
1665 return g_Players.GetClientFromSerial(params[1]);
1668 REGISTER_NATIVES(playernatives)
1670 {"AddUserFlags", AddUserFlags},
1671 {"CanUserTarget", CanUserTarget},
1672 {"ChangeClientTeam", ChangeClientTeam},
1673 {"GetClientAuthString", sm_GetClientAuthStr},
1674 {"GetSteamAccountID", sm_GetSteamAccountID},
1675 {"GetClientCount", sm_GetClientCount},
1676 {"GetClientInfo", sm_GetClientInfo},
1677 {"GetClientIP", sm_GetClientIP},
1678 {"GetClientName", sm_GetClientName},
1679 {"GetClientTeam", GetClientTeam},
1680 {"GetClientUserId", GetClientUserId},
1681 {"GetMaxClients", sm_GetMaxClients},
1682 {"GetMaxHumanPlayers", sm_GetMaxHumanPlayers},
1683 {"GetUserAdmin", GetUserAdmin},
1684 {"GetUserFlagBits", GetUserFlagBits},
1685 {"IsClientAuthorized", sm_IsClientAuthorized},
1686 {"IsClientConnected", sm_IsClientConnected},
1687 {"IsFakeClient", sm_IsClientFakeClient},
1688 {"IsClientSourceTV", sm_IsClientSourceTV},
1689 {"IsClientReplay", sm_IsClientReplay},
1690 {"IsClientInGame", sm_IsClientInGame},
1691 {"IsClientObserver", IsClientObserver},
1692 {"RemoveUserFlags", RemoveUserFlags},
1693 {"SetUserAdmin", SetUserAdmin},
1694 {"SetUserFlagBits", SetUserFlagBits},
1695 {"GetClientDeaths", GetDeathCount},
1696 {"GetClientFrags", GetFragCount},
1697 {"GetClientArmor", GetArmorValue},
1698 {"GetClientAbsOrigin", GetAbsOrigin},
1699 {"GetClientAbsAngles", GetAbsAngles},
1700 {"GetClientMins", GetPlayerMins},
1701 {"GetClientMaxs", GetPlayerMaxs},
1702 {"GetClientWeapon", GetWeaponName},
1703 {"GetClientModel", GetModelName},
1704 {"GetClientHealth", GetHealth},
1705 {"GetClientTime", GetTimeConnected},
1706 {"GetClientDataRate", GetDataRate},
1707 {"IsClientTimingOut", IsTimingOut},
1708 {"GetClientLatency", GetLatency},
1709 {"GetClientAvgLatency", GetAvgLatency},
1710 {"GetClientAvgLoss", GetAvgLoss},
1711 {"GetClientAvgChoke", GetAvgChoke},
1712 {"GetClientAvgData", GetAvgData},
1713 {"GetClientAvgPackets", GetAvgPackets},
1714 {"GetClientOfUserId", GetClientOfUserId},
1715 {"ShowActivity", ShowActivity},
1716 {"ShowActivityEx", ShowActivityEx},
1717 {"ShowActivity2", ShowActivity2},
1718 {"KickClient", KickClient},
1719 {"KickClientEx", KickClientEx},
1720 {"RunAdminCacheChecks", RunAdminCacheChecks},
1721 {"NotifyPostAdminCheck", NotifyPostAdminCheck},
1722 {"IsClientInKickQueue", IsClientInKickQueue},
1723 {"ProcessTargetString", ProcessTargetString},
1724 {"FormatActivitySource", FormatActivitySource},
1725 {"GetClientSerial", sm_GetClientSerial},
1726 {"GetClientFromSerial", sm_GetClientFromSerial},