Update gamedata for CS:S update.
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);
792 else if (!pPlayer->IsConnected())
794 return pContext->ThrowNativeError("Client %d is not connected", client);
796 else if (pPlayer->IsFakeClient())
798 return pContext->ThrowNativeError("Client %d is a bot", client);
801 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
807 return sp_ftoc(pInfo->GetTimeConnected());
810 static cell_t GetDataRate(IPluginContext *pContext, const cell_t *params)
812 int client = params[1];
814 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
817 return pContext->ThrowNativeError("Client index %d is invalid", client);
819 else if (!pPlayer->IsConnected())
821 return pContext->ThrowNativeError("Client %d is not connected", client);
823 else if (pPlayer->IsFakeClient())
825 return pContext->ThrowNativeError("Client %d is a bot", client);
828 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
834 return pInfo->GetDataRate();
837 static cell_t IsTimingOut(IPluginContext *pContext, const cell_t *params)
839 int client = params[1];
841 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
844 return pContext->ThrowNativeError("Client index %d is invalid", client);
846 else if (!pPlayer->IsConnected())
848 return pContext->ThrowNativeError("Client %d is not connected", client);
850 else if (pPlayer->IsFakeClient())
852 return pContext->ThrowNativeError("Client %d is a bot", client);
855 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
861 return pInfo->IsTimingOut() ? 1 : 0;
864 static cell_t GetLatency(IPluginContext *pContext, const cell_t *params)
866 int client = params[1];
869 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
872 return pContext->ThrowNativeError("Client index %d is invalid", client);
874 else if (!pPlayer->IsConnected())
876 return pContext->ThrowNativeError("Client %d is not connected", client);
878 else if (pPlayer->IsFakeClient())
880 return pContext->ThrowNativeError("Client %d is a bot", client);
883 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
889 if (params[2] == MAX_FLOWS)
891 value = pInfo->GetLatency(FLOW_INCOMING) + pInfo->GetLatency(FLOW_OUTGOING);
895 value = pInfo->GetLatency(params[2]);
898 return sp_ftoc(value);
901 static cell_t GetAvgLatency(IPluginContext *pContext, const cell_t *params)
903 int client = params[1];
906 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
909 return pContext->ThrowNativeError("Client index %d is invalid", client);
911 else if (!pPlayer->IsConnected())
913 return pContext->ThrowNativeError("Client %d is not connected", client);
915 else if (pPlayer->IsFakeClient())
917 return pContext->ThrowNativeError("Client %d is a bot", client);
920 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
926 if (params[2] == MAX_FLOWS)
928 value = pInfo->GetAvgLatency(FLOW_INCOMING) + pInfo->GetAvgLatency(FLOW_OUTGOING);
932 value = pInfo->GetAvgLatency(params[2]);
935 return sp_ftoc(value);
938 static cell_t GetAvgLoss(IPluginContext *pContext, const cell_t *params)
940 int client = params[1];
943 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
946 return pContext->ThrowNativeError("Client index %d is invalid", client);
948 else if (!pPlayer->IsConnected())
950 return pContext->ThrowNativeError("Client %d is not connected", client);
952 else if (pPlayer->IsFakeClient())
954 return pContext->ThrowNativeError("Client %d is a bot", client);
957 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
963 if (params[2] == MAX_FLOWS)
965 value = pInfo->GetAvgLoss(FLOW_INCOMING) + pInfo->GetAvgLoss(FLOW_OUTGOING);
969 value = pInfo->GetAvgLoss(params[2]);
972 return sp_ftoc(value);
975 static cell_t GetAvgChoke(IPluginContext *pContext, const cell_t *params)
977 int client = params[1];
980 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
983 return pContext->ThrowNativeError("Client index %d is invalid", client);
985 else if (!pPlayer->IsConnected())
987 return pContext->ThrowNativeError("Client %d is not connected", client);
989 else if (pPlayer->IsFakeClient())
991 return pContext->ThrowNativeError("Client %d is a bot", client);
994 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1000 if (params[2] == MAX_FLOWS)
1002 value = pInfo->GetAvgChoke(FLOW_INCOMING) + pInfo->GetAvgChoke(FLOW_OUTGOING);
1006 value = pInfo->GetAvgChoke(params[2]);
1009 return sp_ftoc(value);
1012 static cell_t GetAvgData(IPluginContext *pContext, const cell_t *params)
1014 int client = params[1];
1017 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1020 return pContext->ThrowNativeError("Client index %d is invalid", client);
1022 else if (!pPlayer->IsConnected())
1024 return pContext->ThrowNativeError("Client %d is not connected", client);
1026 else if (pPlayer->IsFakeClient())
1028 return pContext->ThrowNativeError("Client %d is a bot", client);
1031 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1037 if (params[2] == MAX_FLOWS)
1039 value = pInfo->GetAvgData(FLOW_INCOMING) + pInfo->GetAvgData(FLOW_OUTGOING);
1043 value = pInfo->GetAvgData(params[2]);
1046 return sp_ftoc(value);
1049 static cell_t GetAvgPackets(IPluginContext *pContext, const cell_t *params)
1051 int client = params[1];
1054 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1057 return pContext->ThrowNativeError("Client index %d is invalid", client);
1059 else if (!pPlayer->IsConnected())
1061 return pContext->ThrowNativeError("Client %d is not connected", client);
1063 else if (pPlayer->IsFakeClient())
1065 return pContext->ThrowNativeError("Client %d is a bot", client);
1068 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1074 if (params[2] == MAX_FLOWS)
1076 value = pInfo->GetAvgPackets(FLOW_INCOMING) + pInfo->GetAvgPackets(FLOW_OUTGOING);
1080 value = pInfo->GetAvgPackets(params[2]);
1083 return sp_ftoc(value);
1086 static cell_t GetClientOfUserId(IPluginContext *pContext, const cell_t *params)
1088 return g_Players.GetClientOfUserId(params[1]);
1091 static cell_t _ShowActivity(IPluginContext *pContext,
1092 const cell_t *params,
1098 int value = sm_show_activity.GetInt();
1099 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1100 int client = params[1];
1102 const char *name = "Console";
1103 const char *sign = "ADMIN";
1104 bool display_in_chat = false;
1107 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1108 if (!pPlayer || !pPlayer->IsConnected())
1110 return pContext->ThrowNativeError("Client index %d is invalid", client);
1112 name = pPlayer->GetName();
1113 AdminId id = pPlayer->GetAdminId();
1114 if (id == INVALID_ADMIN_ID
1115 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1120 /* Display the message to the client? */
1121 if (replyto == SM_REPLY_CONSOLE)
1123 g_SourceMod.SetGlobalTarget(client);
1124 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1126 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1131 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1132 pPlayer->PrintToConsole(message);
1133 display_in_chat = true;
1138 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1139 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1141 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1146 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1147 META_CONPRINT(message);
1155 int maxClients = g_Players.GetMaxClients();
1156 for (int i=1; i<=maxClients; i++)
1158 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1159 if (!pPlayer->IsInGame()
1160 || pPlayer->IsFakeClient()
1161 || (display_in_chat && i == client))
1165 AdminId id = pPlayer->GetAdminId();
1166 g_SourceMod.SetGlobalTarget(i);
1167 if (id == INVALID_ADMIN_ID
1168 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1170 /* Treat this as a normal user */
1171 if ((value & 1) || (value & 2))
1173 const char *newsign = sign;
1174 if ((value & 2) || (i == client))
1178 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1180 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1185 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1186 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1191 /* Treat this as an admin user */
1192 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1195 || ((value & 16) && is_root))
1197 const char *newsign = sign;
1198 if ((value & 8) || ((value & 16) && is_root) || (i == client))
1202 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1204 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1209 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1210 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1218 static cell_t _ShowActivity2(IPluginContext *pContext,
1219 const cell_t *params,
1225 int value = sm_show_activity.GetInt();
1226 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1227 int client = params[1];
1229 const char *name = "Console";
1230 const char *sign = "ADMIN";
1233 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1234 if (!pPlayer || !pPlayer->IsConnected())
1236 return pContext->ThrowNativeError("Client index %d is invalid", client);
1238 name = pPlayer->GetName();
1239 AdminId id = pPlayer->GetAdminId();
1240 if (id == INVALID_ADMIN_ID
1241 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1246 g_SourceMod.SetGlobalTarget(client);
1247 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1249 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1254 /* We don't display directly to the console because the chat text
1255 * simply gets added to the console, so we don't want it to print
1258 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1259 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1263 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1264 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1266 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1271 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1272 META_CONPRINT(message);
1280 int maxClients = g_Players.GetMaxClients();
1281 for (int i=1; i<=maxClients; i++)
1283 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1284 if (!pPlayer->IsInGame()
1285 || pPlayer->IsFakeClient()
1290 AdminId id = pPlayer->GetAdminId();
1291 g_SourceMod.SetGlobalTarget(i);
1292 if (id == INVALID_ADMIN_ID
1293 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1295 /* Treat this as a normal user */
1296 if ((value & 1) || (value & 2))
1298 const char *newsign = sign;
1303 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1305 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1310 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1311 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1316 /* Treat this as an admin user */
1317 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1320 || ((value & 16) && is_root))
1322 const char *newsign = sign;
1323 if ((value & 8) || ((value & 16) && is_root))
1327 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1329 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1334 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1335 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1343 static cell_t ShowActivity(IPluginContext *pContext, const cell_t *params)
1345 return _ShowActivity(pContext, params, "[SM] ", 2);
1348 static cell_t ShowActivityEx(IPluginContext *pContext, const cell_t *params)
1351 pContext->LocalToString(params[2], &str);
1353 return _ShowActivity(pContext, params, str, 3);
1356 static cell_t ShowActivity2(IPluginContext *pContext, const cell_t *params)
1359 pContext->LocalToString(params[2], &str);
1361 return _ShowActivity2(pContext, params, str, 3);
1364 static cell_t KickClient(IPluginContext *pContext, const cell_t *params)
1366 int client = params[1];
1368 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1371 return pContext->ThrowNativeError("Client index %d is invalid", client);
1373 else if (!pPlayer->IsConnected())
1375 return pContext->ThrowNativeError("Client %d is not connected", client);
1378 /* Ignore duplicate kicks */
1379 if (pPlayer->IsInKickQueue())
1384 g_SourceMod.SetGlobalTarget(client);
1387 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1389 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1394 if (pPlayer->IsFakeClient())
1396 // Kick uses the kickid command for bots. It is already delayed
1397 // until the next frame unless someone flushes command buffer
1398 pPlayer->Kick(buffer);
1402 g_HL2.AddDelayedKick(client, pPlayer->GetUserId(), buffer);
1407 static cell_t KickClientEx(IPluginContext *pContext, const cell_t *params)
1409 int client = params[1];
1411 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1414 return pContext->ThrowNativeError("Client index %d is invalid", client);
1416 else if (!pPlayer->IsConnected())
1418 return pContext->ThrowNativeError("Client %d is not connected", client);
1421 g_SourceMod.SetGlobalTarget(client);
1424 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1426 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1431 pPlayer->Kick(buffer);
1436 static cell_t ChangeClientTeam(IPluginContext *pContext, const cell_t *params)
1438 int client = params[1];
1440 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1443 return pContext->ThrowNativeError("Client index %d is invalid", client);
1445 else if (!pPlayer->IsInGame())
1447 return pContext->ThrowNativeError("Client %d is not in game", client);
1450 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
1453 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
1456 pInfo->ChangeTeam(params[2]);
1461 static cell_t RunAdminCacheChecks(IPluginContext *pContext, const cell_t *params)
1463 int client = params[1];
1465 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1468 return pContext->ThrowNativeError("Client index %d is invalid", client);
1470 else if (!pPlayer->IsInGame())
1472 return pContext->ThrowNativeError("Client %d is not in game", client);
1474 else if (!pPlayer->IsAuthorized())
1476 return pContext->ThrowNativeError("Client %d is not authorized", client);
1479 AdminId id = pPlayer->GetAdminId();
1480 pPlayer->DoBasicAdminChecks();
1482 return (id != pPlayer->GetAdminId()) ? 1 : 0;
1485 static cell_t NotifyPostAdminCheck(IPluginContext *pContext, const cell_t *params)
1487 int client = params[1];
1489 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1492 return pContext->ThrowNativeError("Client index %d is invalid", client);
1494 else if (!pPlayer->IsInGame()) {
1495 return pContext->ThrowNativeError("Client %d is not in game", client);
1497 else if (!pPlayer->IsAuthorized())
1499 return pContext->ThrowNativeError("Client %d is not authorized", client);
1502 pPlayer->NotifyPostAdminChecks();
1507 static cell_t IsClientInKickQueue(IPluginContext *pContext, const cell_t *params)
1509 int client = params[1];
1511 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1515 return pContext->ThrowNativeError("Client index %d is invalid", client);
1517 else if (!pPlayer->IsConnected())
1519 return pContext->ThrowNativeError("Client %d is not in game", client);
1522 return pPlayer->IsInKickQueue() ? 1 : 0;
1525 static cell_t ProcessTargetString(IPluginContext *pContext, const cell_t *params)
1527 cmd_target_info_t info;
1529 pContext->LocalToString(params[1], (char **)&info.pattern);
1530 info.admin = params[2];
1531 pContext->LocalToPhysAddr(params[3], &info.targets);
1532 info.max_targets = params[4];
1533 info.flags = params[5];
1534 pContext->LocalToString(params[6], &info.target_name);
1535 info.target_name_maxlength = params[7];
1538 pContext->LocalToPhysAddr(params[8], &tn_is_ml);
1540 g_Players.ProcessCommandTarget(&info);
1542 if (info.target_name_style == COMMAND_TARGETNAME_ML)
1551 if (info.num_targets == 0)
1557 return info.num_targets;
1561 static cell_t FormatActivitySource(IPluginContext *pContext, const cell_t *params)
1568 const char *identity[2] = {"Console", "ADMIN"};
1573 if ((pTarget = g_Players.GetPlayerByIndex(target)) == NULL)
1575 return pContext->ThrowNativeError("Invalid client index %d", target);
1577 if (!pTarget->IsConnected())
1579 return pContext->ThrowNativeError("Client %d not connected", target);
1582 value = sm_show_activity.GetInt();
1588 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
1590 return pContext->ThrowNativeError("Invalid client index %d", client);
1592 if (!pPlayer->IsConnected())
1594 return pContext->ThrowNativeError("Client %d not connected", client);
1597 identity[0] = pPlayer->GetName();
1599 AdminId id = pPlayer->GetAdminId();
1600 if (id == INVALID_ADMIN_ID
1601 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1603 identity[1] = "PLAYER";
1608 bool bShowActivity = false;
1610 if ((aidTarget = pTarget->GetAdminId()) == INVALID_ADMIN_ID
1611 || !g_Admins.GetAdminFlag(aidTarget, Admin_Generic, Access_Effective))
1613 /* Treat this as a normal user */
1614 if ((value & 1) || (value & 2))
1616 if ((value & 2) || (target == client))
1620 bShowActivity = true;
1625 /* Treat this as an admin user */
1626 bool is_root = g_Admins.GetAdminFlag(aidTarget, Admin_Root, Access_Effective);
1629 || ((value & 16) && is_root))
1631 if ((value & 8) || ((value & 16) && is_root) || (target == client))
1635 bShowActivity = true;
1639 /* Otherwise, send it back to the script. */
1640 pContext->StringToLocalUTF8(params[3], params[4], identity[mode], NULL);
1642 return bShowActivity ? 1 : 0;
1645 static cell_t sm_GetClientSerial(IPluginContext *pContext, const cell_t *params)
1647 int client = params[1];
1649 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1652 return pContext->ThrowNativeError("Client index %d is invalid", client);
1655 return pPlayer->GetSerial();
1658 static cell_t sm_GetClientFromSerial(IPluginContext *pContext, const cell_t *params)
1660 return g_Players.GetClientFromSerial(params[1]);
1663 REGISTER_NATIVES(playernatives)
1665 {"AddUserFlags", AddUserFlags},
1666 {"CanUserTarget", CanUserTarget},
1667 {"ChangeClientTeam", ChangeClientTeam},
1668 {"GetClientAuthString", sm_GetClientAuthStr},
1669 {"GetSteamAccountID", sm_GetSteamAccountID},
1670 {"GetClientCount", sm_GetClientCount},
1671 {"GetClientInfo", sm_GetClientInfo},
1672 {"GetClientIP", sm_GetClientIP},
1673 {"GetClientName", sm_GetClientName},
1674 {"GetClientTeam", GetClientTeam},
1675 {"GetClientUserId", GetClientUserId},
1676 {"GetMaxClients", sm_GetMaxClients},
1677 {"GetMaxHumanPlayers", sm_GetMaxHumanPlayers},
1678 {"GetUserAdmin", GetUserAdmin},
1679 {"GetUserFlagBits", GetUserFlagBits},
1680 {"IsClientAuthorized", sm_IsClientAuthorized},
1681 {"IsClientConnected", sm_IsClientConnected},
1682 {"IsFakeClient", sm_IsClientFakeClient},
1683 {"IsClientSourceTV", sm_IsClientSourceTV},
1684 {"IsClientReplay", sm_IsClientReplay},
1685 {"IsClientInGame", sm_IsClientInGame},
1686 {"IsClientObserver", IsClientObserver},
1687 {"RemoveUserFlags", RemoveUserFlags},
1688 {"SetUserAdmin", SetUserAdmin},
1689 {"SetUserFlagBits", SetUserFlagBits},
1690 {"GetClientDeaths", GetDeathCount},
1691 {"GetClientFrags", GetFragCount},
1692 {"GetClientArmor", GetArmorValue},
1693 {"GetClientAbsOrigin", GetAbsOrigin},
1694 {"GetClientAbsAngles", GetAbsAngles},
1695 {"GetClientMins", GetPlayerMins},
1696 {"GetClientMaxs", GetPlayerMaxs},
1697 {"GetClientWeapon", GetWeaponName},
1698 {"GetClientModel", GetModelName},
1699 {"GetClientHealth", GetHealth},
1700 {"GetClientTime", GetTimeConnected},
1701 {"GetClientDataRate", GetDataRate},
1702 {"IsClientTimingOut", IsTimingOut},
1703 {"GetClientLatency", GetLatency},
1704 {"GetClientAvgLatency", GetAvgLatency},
1705 {"GetClientAvgLoss", GetAvgLoss},
1706 {"GetClientAvgChoke", GetAvgChoke},
1707 {"GetClientAvgData", GetAvgData},
1708 {"GetClientAvgPackets", GetAvgPackets},
1709 {"GetClientOfUserId", GetClientOfUserId},
1710 {"ShowActivity", ShowActivity},
1711 {"ShowActivityEx", ShowActivityEx},
1712 {"ShowActivity2", ShowActivity2},
1713 {"KickClient", KickClient},
1714 {"KickClientEx", KickClientEx},
1715 {"RunAdminCacheChecks", RunAdminCacheChecks},
1716 {"NotifyPostAdminCheck", NotifyPostAdminCheck},
1717 {"IsClientInKickQueue", IsClientInKickQueue},
1718 {"ProcessTargetString", ProcessTargetString},
1719 {"FormatActivitySource", FormatActivitySource},
1720 {"GetClientSerial", sm_GetClientSerial},
1721 {"GetClientFromSerial", sm_GetClientFromSerial},