Skip to content

Commit

Permalink
Dynamically allocate entity hitbox cache to reduce memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyNotElite authored and BenCat07 committed May 26, 2020
1 parent a60f52f commit 08dcfe7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions include/entitycache.hpp
Expand Up @@ -224,5 +224,6 @@ inline CachedEntity &Get(int idx)
}
void Update();
void Invalidate();
void Shutdown();
extern int max;
} // namespace entity_cache
7 changes: 4 additions & 3 deletions include/entityhitboxcache.hpp
Expand Up @@ -10,6 +10,7 @@
#include <cdll_int.h>
#include <studio.h>
#include <stdexcept>
#include <memory>

// Forward declaration from entitycache.hpp
class CachedEntity;
Expand Down Expand Up @@ -39,7 +40,7 @@ class EntityHitboxCache
void Init();
int GetNumHitboxes();
void Reset();
matrix3x4_t *GetBones();
matrix3x4_t *GetBones(int numbones = -1);

int m_nNumHitboxes;
bool m_bModelSet;
Expand All @@ -52,9 +53,9 @@ class EntityHitboxCache
bool m_VisCheckValidationFlags[CACHE_MAX_HITBOXES]{ false };
bool m_VisCheck[CACHE_MAX_HITBOXES]{ false };
bool m_CacheValidationFlags[CACHE_MAX_HITBOXES]{ false };
CachedHitbox m_CacheInternal[CACHE_MAX_HITBOXES]{};
std::vector<CachedHitbox> m_CacheInternal;

matrix3x4_t bones[128];
std::vector<matrix3x4_t> bones;
bool bones_setup{ false };
};

Expand Down
1 change: 0 additions & 1 deletion report-crash
Expand Up @@ -78,7 +78,6 @@ has_debug_info "$CATHOOK" || SYMBOLS=false
if [ $SYMBOLS == false ]; then
echo "No debug symbols detected!"
proccount=$(grep -c '^processor' /proc/cpuinfo)
\cp ./build/CMakeCache.txt ./scripts/CMakeCacheBackup.txt
pushd build
cmake -DInternal_Symbolized=true .. && cmake --build . --target cathook -- -j$proccount
popd
Expand Down
8 changes: 4 additions & 4 deletions src/crits.cpp
Expand Up @@ -326,7 +326,7 @@ bool shouldCrit()
static bool can_beggars_crit = false;
static bool attacked_last_tick = false;

bool canWeaponCrit(bool canShootCheck = true)
bool canWeaponCrit(bool draw = false)
{
// Is weapon elligible for crits?
IClientEntity *weapon = RAW_ENT(LOCAL_W);
Expand All @@ -344,9 +344,9 @@ bool canWeaponCrit(bool canShootCheck = true)
// Misc checks
if (!isAllowedToWithdrawFromBucket(weapon))
return false;
if (canShootCheck && !CanShoot() && !isRapidFire(weapon))
if (!draw && !CanShoot() && !isRapidFire(weapon))
return false;
if (CE_INT(LOCAL_W, netvar.iItemDefinitionIndex) == 730 && !can_beggars_crit)
if (!draw && CE_INT(LOCAL_W, netvar.iItemDefinitionIndex) == 730 && !can_beggars_crit)
return false;
// Check if we have done enough damage to crit
auto crit_mult_info = critMultInfo(weapon);
Expand Down Expand Up @@ -752,7 +752,7 @@ void Draw()
// fixObservedCritchance(wep);

// Used by multiple things
bool can_crit = canWeaponCrit(false);
bool can_crit = canWeaponCrit(true);

if (bucket != last_bucket || wep->entindex() != last_wep || update_shots.test_and_set(500))
{
Expand Down
9 changes: 9 additions & 0 deletions src/entitycache.cpp
Expand Up @@ -164,5 +164,14 @@ void Invalidate()
}
}

void Shutdown()
{
for (auto &ent : array)
{
ent.Reset();
ent.hitboxes.Reset();
}
}

int max = 0;
} // namespace entity_cache
37 changes: 26 additions & 11 deletions src/entityhitboxcache.cpp
Expand Up @@ -105,7 +105,7 @@ static settings::Boolean bonecache_enabled{ "source.use-bone-cache", "false" };

static std::mutex setupbones_mutex;

matrix3x4_t *EntityHitboxCache::GetBones()
matrix3x4_t *EntityHitboxCache::GetBones(int numbones)
{
static float bones_setup_time = 0.0f;
switch (*setupbones_time)
Expand All @@ -126,47 +126,60 @@ matrix3x4_t *EntityHitboxCache::GetBones()
}
if (!bones_setup)
{
// If numbones is not set, get it from some terrible and unnamed variable
if (numbones == -1)
{
if (parent_ref->m_Type() == ENTITY_PLAYER)
numbones = CE_INT(parent_ref, 0x844);
else
numbones = MAXSTUDIOBONES;
}

if (bones.size() < (size_t) numbones)
bones.resize(numbones);
if (g_Settings.is_create_move)
{
#if !ENABLE_TEXTMODE
if (!*bonecache_enabled || parent_ref->m_Type() != ENTITY_PLAYER || IsPlayerInvisible(parent_ref))
{
PROF_SECTION(bone_setup);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones, MAXSTUDIOBONES, 0x7FF00, bones_setup_time);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones.data(), numbones, 0x7FF00, bones_setup_time);
}
else
{
PROF_SECTION(bone_cache);
auto to_copy = CE_VAR(parent_ref, 0x838, matrix3x4_t *);
if (to_copy)
{
bones->Invalidate();
memcpy((matrix3x4_t *) bones, to_copy, 48 * (CE_INT(parent_ref, 0x844)));
// This is catastrophically bad, don't do this. Someone needs to fix this.
memcpy(bones.data(), to_copy, sizeof(matrix3x4_t) * numbones);
bones_setup = true;
}
else
{
PROF_SECTION(bone_setup);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones, MAXSTUDIOBONES, 0x7FF00, bones_setup_time);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones.data(), numbones, 0x7FF00, bones_setup_time);
}
}
#else
// Textmode bots miss/shoot at nothing when the tf2 bonecache is used
PROF_SECTION(bone_setup);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones, MAXSTUDIOBONES, 0x7FF00, bones_setup_time);
bones_setup = RAW_ENT(parent_ref)->SetupBones(bones, numbones, 0x7FF00, bones_setup_time);
#endif
}
}
return bones;
return bones.data();
}

void EntityHitboxCache::Reset()
{
memset(m_VisCheck, 0, sizeof(bool) * CACHE_MAX_HITBOXES);
memset(m_VisCheckValidationFlags, 0, sizeof(bool) * CACHE_MAX_HITBOXES);
memset(m_CacheValidationFlags, 0, sizeof(bool) * CACHE_MAX_HITBOXES);
memset(m_CacheInternal, 0, sizeof(CachedHitbox) * CACHE_MAX_HITBOXES);
memset(&bones, 0, sizeof(matrix3x4_t) * 128);
m_CacheInternal.clear();
m_CacheInternal.shrink_to_fit();
bones.clear();
bones.shrink_to_fit();
m_nNumHitboxes = 0;
m_bInit = false;
m_bModelSet = false;
Expand Down Expand Up @@ -198,13 +211,15 @@ CachedHitbox *EntityHitboxCache::GetHitbox(int id)
auto set = shdr->pHitboxSet(CE_INT(parent_ref, netvar.iHitboxSet));
if (!dynamic_cast<mstudiohitboxset_t *>(set))
return nullptr;
if (m_nNumHitboxes > m_CacheInternal.size())
m_CacheInternal.resize(m_nNumHitboxes);
box = set->pHitbox(id);
if (!box)
return nullptr;
if (box->bone < 0 || box->bone >= MAXSTUDIOBONES)
return nullptr;
VectorTransform(box->bbmin, GetBones()[box->bone], m_CacheInternal[id].min);
VectorTransform(box->bbmax, GetBones()[box->bone], m_CacheInternal[id].max);
VectorTransform(box->bbmin, GetBones(shdr->numbones)[box->bone], m_CacheInternal[id].min);
VectorTransform(box->bbmax, GetBones(shdr->numbones)[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;
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/HookTools.cpp
Expand Up @@ -6,9 +6,8 @@ namespace EC

struct EventCallbackData
{
explicit EventCallbackData(const EventFunction &function, std::string name, enum ec_priority priority) : function{ function }, priority{ int(priority) }, section{ name }, event_name{ name }
explicit EventCallbackData(const EventFunction &function, std::string name, enum ec_priority priority) : function{ function }, priority{ int(priority) }, section(name), event_name{ name }
{
section.m_name = name;
}
EventFunction function;
int priority;
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/LevelShutdown.cpp
Expand Up @@ -17,6 +17,8 @@ DEFINE_HOOKED_METHOD(LevelShutdown, void, void *this_)
g_Settings.bInvalid = true;
chat_stack::Reset();
EC::run(EC::LevelShutdown);
// Free memory for hitbox cache
entity_cache::Shutdown();
#if ENABLE_IPC
if (ipc::peer)
{
Expand Down

0 comments on commit 08dcfe7

Please sign in to comment.