begin hitbox cache

This commit is contained in:
nullifiedcat 2016-12-23 11:24:58 +03:00
parent 13ab308957
commit c4fe132d39
3 changed files with 97 additions and 37 deletions

View File

@ -19,10 +19,14 @@ CachedEntity::CachedEntity() {
m_Strings = new ESPStringCompound[MAX_STRINGS]();
m_nESPStrings = 0;
m_Bones = 0;
m_Bones = new matrix3x4_t[MAXSTUDIOBONES];
m_pHitboxCache = new EntityHitboxCache(this);
}
CachedEntity::~CachedEntity() {
delete m_Strings;
delete [] m_Strings;
delete [] m_Bones;
delete m_pHitboxCache;
}
IClientEntity* CachedEntity::InternalEntity() {
@ -45,11 +49,8 @@ void CachedEntity::Update(int idx) {
m_iClassID = m_pEntity->GetClientClass()->m_ClassID;
m_bGrenadeProjectile = false;
if (m_Bones) {
delete [] m_Bones;
m_Bones = 0;
}
m_bBonesSetup = false;
SAFE_CALL(m_pHitboxCache->Update());
switch (m_iClassID) {
case ClassID::CTFPlayer:
@ -177,9 +178,8 @@ ESPStringCompound CachedEntity::GetESPString(int idx) {
}
matrix3x4_t* CachedEntity::GetBones() {
if (!m_bBonesSetup || !m_Bones) {
m_Bones = new matrix3x4_t[128];
m_bBonesSetup = m_pEntity->SetupBones(m_Bones, 128, 0x100, 0); // interfaces::gvars->curtime
if (!m_bBonesSetup) {
m_bBonesSetup = m_pEntity->SetupBones(m_Bones, MAXSTUDIOBONES, 0x100, 0); // interfaces::gvars->curtime
}
return m_Bones;
}
@ -191,7 +191,7 @@ EntityCache::EntityCache() {
EntityCache::~EntityCache() {
logging::Info("Destroying EntityCache!");
delete m_pArray;
delete [] m_pArray;
}
void EntityCache::Update() {

View File

@ -20,6 +20,9 @@ class IClientEntity;
class Color;
struct ESPStringCompound;
struct player_info_s;
struct model_t;
struct mstudiohitboxset_t;
struct mstudiobbox_t;
#define MAX_STRINGS 16
@ -63,6 +66,33 @@ struct player_info_s;
#define HIGHEST_ENTITY gEntityCache.m_nMax
#define ENTITY(idx) gEntityCache.GetEntity(idx)
struct CachedHitbox {
Vector min;
Vector max;
Vector center;
mstudiobbox_t* bbox;
};
#define CACHE_MAX_HITBOXES 64
class EntityHitboxCache {
public:
EntityHitboxCache(CachedEntity* parent);
~EntityHitboxCache();
CachedHitbox* GetHitbox(int id);
void Update();
void InvalidateCache();
int m_nNumHitboxes;
const model_t* m_pLastModel;
mstudiohitboxset_t* m_pHitboxSet;
bool m_bSuccess;
CachedEntity* m_pParentEntity;
bool* m_CacheValidationFlags;
CachedHitbox* m_CacheInternal;
};
class CachedEntity {
public:
CachedEntity();
@ -99,39 +129,12 @@ public:
bool m_bBonesSetup;
matrix3x4_t* GetBones();
// CBaseEntity
/*int m_iTeamNum;
bool m_bEnemy;
// CBasePlayer
bool m_bIsAlivePlayer;
int m_fFlags;
int m_lifeState;
int m_iHealth;
Vector m_vecViewOffset;
// CBaseCombatCharacter
int m_hActiveWeapon;
IClientEntity* m_activeWeapon;
// CTFPlayer
bool m_bIsCritBoosted;
bool m_bIsInvulnerable;
int m_iCond;
int m_iCondEx;
int m_iCondEx2;
int m_iCondEx3;
float m_flChargeMeter;
float m_flEnergyDrinkMeter;
int m_nNumHealers;*/
// Players, Buildings, Stickies
// Entity fields end here.
EntityHitboxCache* m_pHitboxCache;
int m_IDX;
IClientEntity* InternalEntity();
IClientEntity* m_pEntity;

View File

@ -0,0 +1,57 @@
/*
* entityhitboxcache.cpp
*
* Created on: Dec 23, 2016
* Author: nullifiedcat
*/
#include "common.h"
EntityHitboxCache::EntityHitboxCache(CachedEntity* parent) {
m_CacheInternal = new CachedHitbox[CACHE_MAX_HITBOXES];
m_CacheValidationFlags = new bool[CACHE_MAX_HITBOXES];
InvalidateCache();
}
EntityHitboxCache::~EntityHitboxCache() {
delete [] m_CacheInternal;
delete [] m_CacheValidationFlags;
}
void EntityHitboxCache::InvalidateCache() {
for (int i = 0; i < CACHE_MAX_HITBOXES; i++)
m_CacheValidationFlags[i] = false;
}
void EntityHitboxCache::Update() {
InvalidateCache();
if (CE_BAD(m_pParentEntity)) return;
const model_t* model = RAW_ENT(m_pParentEntity)->GetModel();
if (!model) return;
if (model != m_pLastModel) {
studiohdr_t* shdr = interfaces::model->GetStudiomodel(model);
if (!shdr) return;
mstudiohitboxset_t* set = shdr->pHitboxSet(CE_INT(m_pParentEntity, netvar.iHitboxSet));
if (!set) return;
m_pLastModel = model;
m_pHitboxSet = set;
m_nNumHitboxes = set->numhitboxes;
if (m_nNumHitboxes > CACHE_MAX_HITBOXES) m_nNumHitboxes = CACHE_MAX_HITBOXES;
m_bSuccess = true;
}
}
CachedHitbox* EntityHitboxCache::GetHitbox(int id) {
if (id < 0 || id >= m_nNumHitboxes) return 0;
if (!m_CacheValidationFlags[id]) {
mstudiobbox_t* box = m_pHitboxSet->pHitbox(id);
if (!box) return 0;
if (box->bone < 0 || box->bone >= MAXSTUDIOBONES) return 0;
VectorTransform(box->bbmin, m_pParentEntity->GetBones()[box->bone], m_CacheInternal[id].min);
VectorTransform(box->bbmax, m_pParentEntity->GetBones()[box->bone], m_CacheInternal[id].max);
m_CacheInternal[id].bbox = box;
m_CacheInternal[id].center = (m_CacheInternal[id].min + m_CacheInternal[id].max) / 2;
m_CacheValidationFlags[id] = true;
}
return &m_CacheInternal[id];
}