From 51a6841e785a183e7deaf7d91b104dc89b63a478 Mon Sep 17 00:00:00 2001 From: Jackz Date: Mon, 5 Oct 2020 15:50:39 -0500 Subject: [PATCH] jutils - add new stocks - GetNearestEntity - IsValidPlayer - IsValidTeamPlayer - GetPrimaryAmmo - CheatCommand --- scripting/include/jutils.inc | 58 ++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/scripting/include/jutils.inc b/scripting/include/jutils.inc index feb92f5..356e8dc 100644 --- a/scripting/include/jutils.inc +++ b/scripting/include/jutils.inc @@ -32,7 +32,14 @@ stock void GetHorizontalPositionFromClient(int client, float units, float finalP pos[1] += -150 * Sine(theta); finalPosition = pos; } - +//Credits to Timocop for the stock :D +/** +* Runs a single line of vscript code. +* NOTE: Dont use the "script" console command, it starts a new instance and leaks memory. Use this instead! +* +* @param sCode The code to run. +* @noreturn +*/ stock void L4D2_RunScript(const char[] sCode, any ...) { static int iScriptLogic = INVALID_ENT_REFERENCE; if(iScriptLogic == INVALID_ENT_REFERENCE || !IsValidEntity(iScriptLogic)) { @@ -216,4 +223,51 @@ stock bool GiveClientWeapon(int client, const char[] wpnName, bool lasers) { }else{ return false; } -} \ No newline at end of file +} +stock int GetNearestEntity(int client, char[] classname) +{ + int nearestEntity = -1; + float clientVecOrigin[3], entityVecOrigin[3]; + + //Get the distance between the first entity and client + float distance, nearestDistance = -1.0; + + //Find all the entity and compare the distances + int entity = -1; + while ((entity = FindEntityByClassname(entity, classname)) != -1) + { + GetEntPropVector(entity, Prop_Data, "m_vecOrigin", entityVecOrigin); + distance = GetVectorDistance(clientVecOrigin, entityVecOrigin); + + if (distance < nearestDistance || nearestDistance == -1.0) + { + nearestEntity = entity; + nearestDistance = distance; + } + } + return nearestEntity; +} + +stock bool IsValidPlayer(int i) { + return IsClientConnected(i) && IsClientInGame(i) && IsPlayerAlive(i); +} +stock bool IsValidTeamPlayer(int i, int team) { + return IsValidPlayer(i) && GetClientTeam(i) == team; +} +stock int GetPrimaryAmmo(int client) { + int weapon = GetPlayerWeaponSlot(client, 0); + if(weapon > -1) + return GetEntProp(weapon, Prop_Send, "m_iClip1"); + else + return -1; +} +stock void CheatCommand(int client, const char[] command, const char[] argument1, const char[] argument2) +{ + int userFlags = GetUserFlagBits(client); + SetUserFlagBits(client, ADMFLAG_ROOT); + int flags = GetCommandFlags(command); + SetCommandFlags(command, flags & ~FCVAR_CHEAT); + FakeClientCommand(client, "%s %s %s", command, argument1, argument2); + SetCommandFlags(command, flags); + SetUserFlagBits(client, userFlags); +} \ No newline at end of file