-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature distance #25
base: master
Are you sure you want to change the base?
Feature distance #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef COMPUTATIONAL_TOOLS | ||
#define COMPUTATIONAL_TOOLS | ||
|
||
#include <minmax.h> | ||
|
||
template <typename T, int N> | ||
class MovingAverage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A separate type for the total and the samples is a little weird. You could just combine them. |
||
{ | ||
enum { val = (N >= 1) & !(N & (N - 1)) }; | ||
static_assert(val, "The Sample size template parameter should be a power of 2"); | ||
|
||
public: | ||
MovingAverage() : num_samples_(0), total_(0) {} | ||
|
||
void Reset() | ||
{ | ||
num_samples_ = 0; | ||
total_ = 0; | ||
} | ||
|
||
void AddNumber( T sample ) | ||
{ | ||
if (num_samples_ < N) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do it without the branch here. Just initialize the array to zero. It's also a little odd to overload operator() for this. I'd say make a separate method for taking a sample. |
||
{ | ||
samples_[num_samples_++] = sample; | ||
total_ += sample; | ||
} else { | ||
T& oldest = samples_[num_samples_++ % N]; | ||
total_ += sample - oldest; | ||
oldest = sample; | ||
} | ||
} | ||
|
||
operator double() const { return total_ / min(num_samples_, N); } | ||
|
||
private: | ||
T samples_[N]; | ||
int num_samples_; | ||
T total_; | ||
}; | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
//========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============// | ||
// | ||
// Purpose: | ||
// | ||
//=============================================================================// | ||
|
||
#include "edict.h" | ||
#include "timedeventmgr.h" | ||
#include "tier0/vprof.h" | ||
|
||
// memdbgon must be the last include file in a .cpp file!!! | ||
#include "tier0/memdbgon.h" | ||
|
||
extern CGlobalVars *gpGlobals; | ||
|
||
// ------------------------------------------------------------------------------------------ // | ||
// CEventRegister. | ||
// ------------------------------------------------------------------------------------------ // | ||
|
||
CEventRegister::CEventRegister() | ||
{ | ||
m_bRegistered = false; | ||
m_pEventMgr = NULL; | ||
} | ||
|
||
|
||
CEventRegister::~CEventRegister() | ||
{ | ||
Term(); | ||
} | ||
|
||
|
||
void CEventRegister::Init( CTimedEventMgr *pMgr, IEventRegisterCallback *pCallback ) | ||
{ | ||
Term(); | ||
m_pEventMgr = pMgr; | ||
m_pCallback = pCallback; | ||
} | ||
|
||
|
||
void CEventRegister::Term() | ||
{ | ||
// Unregister. | ||
if ( m_pEventMgr && m_bRegistered ) | ||
{ | ||
m_pEventMgr->RemoveEvent( this ); | ||
} | ||
} | ||
|
||
|
||
void CEventRegister::SetUpdateInterval( float interval ) | ||
{ | ||
Assert( m_pEventMgr ); | ||
|
||
if ( m_pEventMgr ) | ||
{ | ||
// Register for this event. | ||
m_flUpdateInterval = interval; | ||
m_flNextEventTime = gpGlobals->curtime + m_flUpdateInterval; | ||
|
||
m_pEventMgr->RegisterForNextEvent( this ); | ||
} | ||
} | ||
|
||
|
||
void CEventRegister::StopUpdates() | ||
{ | ||
if ( m_pEventMgr ) | ||
{ | ||
// Unregister our next event. | ||
m_pEventMgr->RemoveEvent( this ); | ||
} | ||
} | ||
|
||
|
||
void CEventRegister::Reregister() | ||
{ | ||
if ( m_flUpdateInterval > 1e-6 && m_pEventMgr ) | ||
{ | ||
while ( m_flNextEventTime <= gpGlobals->curtime ) | ||
{ | ||
m_flNextEventTime += m_flUpdateInterval; | ||
} | ||
|
||
m_pEventMgr->RegisterForNextEvent( this ); | ||
} | ||
} | ||
|
||
|
||
// ------------------------------------------------------------------------------------------ // | ||
// CTimedEventMgr. | ||
// ------------------------------------------------------------------------------------------ // | ||
|
||
bool TimedEventMgr_LessFunc( CEventRegister* const &a, CEventRegister* const &b ) | ||
{ | ||
return a->m_flNextEventTime > b->m_flNextEventTime; | ||
} | ||
|
||
|
||
CTimedEventMgr::CTimedEventMgr() | ||
{ | ||
m_Events.SetLessFunc( TimedEventMgr_LessFunc ); | ||
} | ||
|
||
|
||
void CTimedEventMgr::FireEvents() | ||
{ | ||
VPROF( "CTimedEventMgr::FireEvents" ); | ||
while ( m_Events.Count() ) | ||
{ | ||
// Fire the top element, then break out. | ||
CEventRegister *pEvent = m_Events.ElementAtHead(); | ||
if ( gpGlobals->curtime >= pEvent->m_flNextEventTime ) | ||
{ | ||
// Reregister for the timed event, then fire the callback for the event. | ||
m_Events.RemoveAtHead(); | ||
pEvent->m_bRegistered = false; | ||
pEvent->Reregister(); | ||
|
||
pEvent->m_pCallback->FireEvent(); | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
void CTimedEventMgr::RegisterForNextEvent( CEventRegister *pEvent ) | ||
{ | ||
RemoveEvent( pEvent ); | ||
m_Events.Insert( pEvent ); | ||
pEvent->m_bRegistered = true; | ||
} | ||
|
||
|
||
void CTimedEventMgr::RemoveEvent( CEventRegister *pEvent ) | ||
{ | ||
if ( pEvent->m_bRegistered ) | ||
{ | ||
// Find the event in the list and remove it. | ||
int cnt = m_Events.Count(); | ||
for ( int i=0; i < cnt; i++ ) | ||
{ | ||
if ( m_Events.Element( i ) == pEvent ) | ||
{ | ||
m_Events.RemoveAt( i ); | ||
break; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
#include "SSTeamData.h" | ||
|
||
tfteam to_tfteam( int teamid ) | ||
{ | ||
|
||
switch (teamid) { | ||
TFTEAM_SPECTATOR: | ||
return TFTEAM_SPECTATOR; | ||
TFTEAM_RED: | ||
return TFTEAM_RED; | ||
TFTEAM_BLUE: | ||
return TFTEAM_BLUE; | ||
default: | ||
return TFTEAM_UNASSIGNED; | ||
} | ||
} | ||
|
||
void TeamScoreData::Reset() | ||
{ | ||
m_AverageMedicDistance.Reset(); | ||
} | ||
|
||
CTeamDataManager::CTeamDataManager() | ||
{ | ||
|
||
} | ||
|
||
CTeamDataManager::~CTeamDataManager() | ||
{ | ||
} | ||
|
||
void CTeamDataManager::Reset() | ||
{ | ||
TeamScoreData *tm_data = nullptr; | ||
|
||
if ((tm_data = GetTeamStats(TFTEAM_RED)) != nullptr) | ||
{ | ||
tm_data->Reset(); | ||
} | ||
|
||
if ((tm_data = GetTeamStats(TFTEAM_BLUE)) != nullptr) | ||
{ | ||
tm_data->Reset(); | ||
} | ||
|
||
} | ||
|
||
void CTeamDataManager::ResetTeamStatsData() | ||
{ | ||
} | ||
|
||
void CTeamDataManager::AddStatSample( tfteam team, TeamStat stat, float sample_point ) | ||
{ | ||
|
||
if (stat == TeamStat::MedicCohesion) | ||
{ | ||
TeamScoreData *team_data = GetTeamStats(team); | ||
if (!team_data) | ||
return; | ||
|
||
team_data->m_AverageMedicDistance.AddNumber(sample_point); | ||
} | ||
} | ||
|
||
double CTeamDataManager::GetStat( tfteam team, TeamStat stat ) | ||
{ | ||
if (stat == TeamStat::MedicCohesion) | ||
{ | ||
TeamScoreData *team_data = GetTeamStats(team); | ||
if (!team_data) | ||
return 0.0; | ||
return team_data->m_AverageMedicDistance; | ||
} | ||
|
||
return 0.0; | ||
} | ||
|
||
|
||
TeamScoreData *CTeamDataManager::GetTeamStats( tfteam team ) | ||
{ | ||
switch (team) | ||
{ | ||
case TFTEAM_RED: | ||
return &m_TeamScoreData[0]; | ||
case TFTEAM_BLUE: | ||
return &m_TeamScoreData[1]; | ||
default: | ||
return nullptr; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef SS_TEAM_DATA | ||
#define SS_TEAM_DATA | ||
|
||
#include "ComputationalTools.h" | ||
|
||
class CSizzPluginContext; | ||
/** | ||
* Maintains the round statistics for the team as a whole. | ||
*/ | ||
|
||
|
||
struct TeamScoreData | ||
{ | ||
MovingAverage<double, 64> m_AverageMedicDistance; | ||
void Reset(); | ||
}; | ||
|
||
enum tfteam | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TFTEAM_UNASSIGNED = 0, |
||
{ | ||
TFTEAM_UNASSIGNED = 0, | ||
TFTEAM_SPECTATOR = 1, | ||
TFTEAM_RED = 2, | ||
TFTEAM_BLUE = 3 | ||
}; | ||
|
||
enum class TeamStat | ||
{ | ||
MedicCohesion | ||
}; | ||
|
||
|
||
tfteam to_tfteam( int ); | ||
|
||
class CTeamDataManager | ||
{ | ||
public: | ||
CTeamDataManager(); | ||
~CTeamDataManager(); | ||
|
||
void AddStatSample( tfteam team, TeamStat stat, float sample_point ); | ||
double GetStat( tfteam team, TeamStat stat ); | ||
|
||
void ResetTeamStatsData(); | ||
void Reset(); | ||
|
||
private: | ||
TeamScoreData *GetTeamStats( tfteam team ); | ||
TeamScoreData m_TeamScoreData[2]; | ||
}; | ||
|
||
|
||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try including here. Hopefully no valve minmax clashes