Skip to content

Commit 268cfec

Browse files
committed
Added caching.
1 parent 62bad57 commit 268cfec

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/core/modules/entities/entities_collisions.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@
4141
//-----------------------------------------------------------------------------
4242
extern IEngineTrace *enginetrace;
4343
extern IPhysics *physics;
44+
extern CGlobalVars *gpGlobals;
4445

4546

4647
//-----------------------------------------------------------------------------
4748
// CCollisionManager class.
4849
//-----------------------------------------------------------------------------
4950
CCollisionManager::CCollisionManager():
5051
m_bInitialized(false),
51-
m_uiRefCount(0)
52+
m_uiRefCount(0),
53+
m_nTickCount(-1)
5254
{
5355

5456
}
@@ -206,6 +208,7 @@ void CCollisionManager::RegisterHook(T tFunc, unsigned int uiFilterIndex, unsign
206208
bool CCollisionManager::EnterScope(HookType_t eHookType, CHook *pHook)
207209
{
208210
static CCollisionManager *pManager = GetCollisionManager();
211+
pManager->RefreshCache();
209212

210213
CollisionScope_t scope;
211214
scope.m_bSkip = true;
@@ -288,6 +291,7 @@ bool CCollisionManager::EnterScope(HookType_t eHookType, CHook *pHook)
288291

289292
scope.m_pFilter = pWrapper;
290293
scope.m_uiIndex = uiIndex;
294+
scope.m_pCache = pManager->GetCache(uiIndex);
291295
scope.m_pExtraShouldHitCheckFunction = pWrapper->m_pExtraShouldHitCheckFunction;
292296
pWrapper->m_pExtraShouldHitCheckFunction = (ShouldHitFunc_t)CCollisionManager::ShouldHitEntity;
293297

@@ -349,14 +353,20 @@ bool CCollisionManager::ShouldHitEntity(IHandleEntity *pHandleEntity, int conten
349353
}
350354
}
351355

356+
if (scope.m_pCache->HasResult(uiIndex)) {
357+
return scope.m_pCache->GetResult(uiIndex);
358+
}
359+
352360
FOR_EACH_VEC(pManager->m_vecHashes, i) {
353361
if (pManager->m_vecHashes[i]->HasPair((void *)scope.m_pFilter->m_pPassEnt, (void *)pHandleEntity)) {
362+
scope.m_pCache->SetResult(uiIndex, false);
354363
return false;
355364
}
356365
}
357366

358367
static CEntityCollisionListenerManager *pListener = GetOnEntityCollisionListenerManager();
359368
if (!pListener->GetCount()) {
369+
scope.m_pCache->SetResult(uiIndex, true);
360370
return true;
361371
}
362372

@@ -369,14 +379,43 @@ bool CCollisionManager::ShouldHitEntity(IHandleEntity *pHandleEntity, int conten
369379
BEGIN_BOOST_PY()
370380
object oResult = pListener->m_vecCallables[i](oEntity, oOther);
371381
if (!oResult.is_none() && !extract<bool>(oResult)) {
382+
scope.m_pCache->SetResult(uiIndex, false);
372383
return false;
373384
}
374385
END_BOOST_PY_NORET()
375386
}
376387

388+
scope.m_pCache->SetResult(uiIndex, true);
377389
return true;
378390
}
379391

392+
void CCollisionManager::RefreshCache()
393+
{
394+
if (gpGlobals->tickcount == m_nTickCount) {
395+
return;
396+
}
397+
398+
for (CollisionCacheMap_t::const_iterator it = m_mapCache.begin(); it != m_mapCache.end(); it++) {
399+
delete it->second;
400+
}
401+
402+
m_mapCache.clear();
403+
m_nTickCount = gpGlobals->tickcount;
404+
}
405+
406+
CCollisionCache *CCollisionManager::GetCache(unsigned int uiIndex)
407+
{
408+
CollisionCacheMap_t::const_iterator it = m_mapCache.find(uiIndex);
409+
if (it != m_mapCache.end()) {
410+
return it->second;
411+
}
412+
413+
CCollisionCache *pCache = new CCollisionCache();
414+
m_mapCache[uiIndex] = pCache;
415+
416+
return pCache;
417+
}
418+
380419

381420
//-----------------------------------------------------------------------------
382421
// ICollisionHash class.
@@ -399,6 +438,26 @@ void ICollisionHash::UnloadInstance()
399438
}
400439

401440

441+
//-----------------------------------------------------------------------------
442+
// CCollisionCache class.
443+
//-----------------------------------------------------------------------------
444+
bool CCollisionCache::HasResult(unsigned int uiIndex)
445+
{
446+
return IsBitSet(uiIndex);
447+
}
448+
449+
bool CCollisionCache::GetResult(unsigned int uiIndex)
450+
{
451+
return m_vecCache.IsBitSet(uiIndex);
452+
}
453+
454+
void CCollisionCache::SetResult(unsigned int uiIndex, bool bResult)
455+
{
456+
Set(uiIndex);
457+
m_vecCache.Set((int)uiIndex, bResult);
458+
}
459+
460+
402461
//-----------------------------------------------------------------------------
403462
// CCollisionHash class.
404463
//-----------------------------------------------------------------------------

src/core/modules/entities/entities_collisions.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141

4242
// SDK
4343
#include "vphysics/object_hash.h"
44+
#include "bitvec.h"
4445

4546

4647
//-----------------------------------------------------------------------------
4748
// Forward declarations.
4849
//-----------------------------------------------------------------------------
4950
struct CollisionHookData_t;
51+
class CCollisionCache;
5052

5153

5254
//-----------------------------------------------------------------------------
@@ -58,6 +60,9 @@ struct CollisionHookData_t;
5860

5961
typedef boost::unordered_map<CHook *, CollisionHookData_t> CollisionHooksMap_t;
6062

63+
typedef CBitVec<MAX_EDICTS> CollisionCache_t;
64+
typedef boost::unordered_map<unsigned int, CCollisionCache *> CollisionCacheMap_t;
65+
6166

6267
//-----------------------------------------------------------------------------
6368
// CTraceFilterSimpleWrapper class.
@@ -80,6 +85,7 @@ struct CollisionScope_t
8085
unsigned int m_uiIndex;
8186
CTraceFilterSimpleWrapper *m_pFilter;
8287
ShouldHitFunc_t m_pExtraShouldHitCheckFunction;
88+
CCollisionCache *m_pCache;
8389
};
8490

8591

@@ -140,6 +146,21 @@ class CCollisionHash : public ICollisionHash
140146
};
141147

142148

149+
//-----------------------------------------------------------------------------
150+
// CCollisionCache class.
151+
//-----------------------------------------------------------------------------
152+
class CCollisionCache : private CollisionCache_t
153+
{
154+
public:
155+
bool HasResult(unsigned int uiIndex);
156+
bool GetResult(unsigned int uiIndex);
157+
void SetResult(unsigned int uiIndex, bool bResult);
158+
159+
private:
160+
CollisionCache_t m_vecCache;
161+
};
162+
163+
143164
//-----------------------------------------------------------------------------
144165
// CCollisionManager class.
145166
//-----------------------------------------------------------------------------
@@ -172,12 +193,18 @@ class CCollisionManager
172193

173194
static bool ShouldHitEntity(IHandleEntity *pHandleEntity, int contentsMask);
174195

196+
void RefreshCache();
197+
CCollisionCache *GetCache(unsigned int uiIndex);
198+
175199
private:
176200
bool m_bInitialized;
177201
unsigned int m_uiRefCount;
178202
CUtlVector<ICollisionHash *> m_vecHashes;
179203
CollisionHooksMap_t m_mapHooks;
180204
std::vector<CollisionScope_t> m_vecScopes;
205+
206+
int m_nTickCount;
207+
CollisionCacheMap_t m_mapCache;
181208
};
182209

183210
// Singleton accessor.

0 commit comments

Comments
 (0)