/// <summary> /// Gets the entity info of an entity using its index. /// </summary> /// <param name="index">The index of the entity</param> public CEntInfoV2 GetEntInfoByIndex(int index) { Debug.Assert(this.GameOffsets.EntInfoSize > 0); if (index < 0) { return(new CEntInfoV2()); } CEntInfoV2 ret; IntPtr addr = this.GameOffsets.GlobalEntityListPtr + ((int)this.GameOffsets.EntInfoSize * index); if (this.GameOffsets.EntInfoSize == CEntInfoSize.HL2) { CEntInfoV1 v1; this.GameProcess.ReadValue(addr, out v1); ret = CEntInfoV2.FromV1(v1); } else { this.GameProcess.ReadValue(addr, out ret); } return(ret); }
// warning: expensive - 7ms on i5 // do not call frequently! public IntPtr GetEntityByName(string name) { const int MAX_ENTS = 2048; // TODO: is portal2's max higher? for (int i = 0; i < MAX_ENTS; i++) { CEntInfoV2 info = this.GetEntInfoByIndex(i); if (info.EntityPtr == IntPtr.Zero) { continue; } IntPtr namePtr; this.GameProcess.ReadPtr32(info.EntityPtr + this.GameOffsets.BaseEntityTargetNameOffset, out namePtr); if (namePtr == IntPtr.Zero) { continue; } string n; this.GameProcess.ReadASCIIString(namePtr, out n, 32); // TODO: find real max len if (n == name) { return(info.EntityPtr); } } return(IntPtr.Zero); }
/// <summary> /// Gets the entity index of the entity with matching name /// </summary> /// <param name="name">The name of the entity</param> public int GetEntIndexByName(string name, params string[] ignore) { for (int i = 0; i < _maxEnts; i++) { CEntInfoV2 info = this.GetEntInfoByIndex(i); if (info.EntityPtr == IntPtr.Zero) { continue; } IntPtr namePtr; this.GameProcess.ReadPointer(info.EntityPtr + this.GameOffsets.BaseEntityTargetNameOffset, false, out namePtr); if (namePtr == IntPtr.Zero) { continue; } string n; this.GameProcess.ReadString(namePtr, ReadStringType.ASCII, 32, out n); // TODO: find real max len if (!(ignore?.Contains(n) ?? false) && n.CompareWildcard(name)) { return(i); } } return(-1); }
// warning: expensive - 7ms on i5 // do not call frequently! /// <summary> /// Gets the entity pointer of the entity with matching name /// </summary> /// <param name="name">The name of the entity</param> public IntPtr GetEntityByName(string name, params string[] ignore) { // se 2003 has a really convoluted ehandle system that basically equivalent to this // so let's just use the old system for that if (GameMemory.IsSource2003) { return(GetEntInfoByIndex(GetEntIndexByName(name, ignore)).EntityPtr); } else { CEntInfoV2 nextPtr = this.GetEntInfoByIndex(0); do { if (nextPtr.EntityPtr == IntPtr.Zero) { return(IntPtr.Zero); } IntPtr namePtr; this.GameProcess.ReadPointer(nextPtr.EntityPtr + this.GameOffsets.BaseEntityTargetNameOffset, false, out namePtr); if (namePtr != IntPtr.Zero) { this.GameProcess.ReadString(namePtr, ReadStringType.ASCII, 32, out string n); // TODO: find real max len if (!(ignore?.Contains(n) ?? false) && name.CompareWildcard(n)) { return(nextPtr.EntityPtr); } } nextPtr = GameProcess.ReadValue <CEntInfoV2>((IntPtr)nextPtr.m_pNext); }while (nextPtr.EntityPtr != IntPtr.Zero); } return(IntPtr.Zero); }
public static CEntInfoV2 FromV1(CEntInfoV1 v1) { var ret = new CEntInfoV2(); ret.m_pEntity = v1.m_pEntity; ret.m_SerialNumber = v1.m_SerialNumber; ret.m_pPrev = v1.m_pPrev; ret.m_pNext = v1.m_pNext; return ret; }
public static CEntInfoV2 FromV1(CEntInfoV1 v1) { var ret = new CEntInfoV2(); ret.m_pEntity = v1.m_pEntity; ret.m_SerialNumber = v1.m_SerialNumber; ret.m_pPrev = v1.m_pPrev; ret.m_pNext = v1.m_pNext; return(ret); }
/// <summary> /// Gets the entity index of the entity with matching position /// </summary> /// <param name="x">The x coordinate of the entity</param> /// <param name="y">The y coordinate of the entity</param> /// <param name="z">The z coordinate of the entity</param> /// <param name="d">The maximum allowed distance away from the specified position</param> /// <param name="xy">Whether to ignore the z component when evaluating positions</param> public int GetEntIndexByPos(float x, float y, float z, float d = 0f, bool xy = false) { Vector3f pos = new Vector3f(x, y, z); for (int i = 0; i < _maxEnts; i++) { CEntInfoV2 info = this.GetEntInfoByIndex(i); if (info.EntityPtr == IntPtr.Zero) { continue; } Vector3f newpos; if (!this.GameProcess.ReadValue(info.EntityPtr + this.GameOffsets.BaseEntityAbsOriginOffset, out newpos)) { continue; } if (d == 0f) { if (newpos.BitEquals(pos) && i != 1) //not equal 1 becase the player might be in the same exact position { return(i); } } else // check for distance if it's a non-static entity like an npc or a prop { if (xy) { if (newpos.DistanceXY(pos) <= d && i != 1) { return(i); } } else { if (newpos.Distance(pos) <= d && i != 1) { return(i); } } } } return(-1); }