summaryrefslogtreecommitdiff
path: root/game/server/cstrike/funfactmgr_cs.cpp
blob: 4454e08d7812123f012dfecc83c4909f0912967e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//========= Copyright Valve Corporation, All rights reserved. ============//
#include "cbase.h"
#include "usermessages.h"
#include "funfactmgr_cs.h"

const float kCooldownRatePlayer		= 0.4f;
const float kCooldownRateFunFact	= 0.2f;

const float kWeightPlayerCooldown	= 0.8f;
const float kWeightFunFactCooldown	= 1.0f;
const float kWeightCoolness			= 2.0f;
const float kWeightRarity			= 1.0f;

#define DEBUG_FUNFACT_SCORING 0

//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CCSFunFactMgr::CCSFunFactMgr() : 
	CAutoGameSystemPerFrame( "CCSFunFactMgr" ),
	m_funFactDatabase(0, 100, DefLessFunc(int) )
{
	for ( int i = 0; i < MAX_PLAYERS; ++i )
	{
		m_playerCooldown[i] = 0.0f;
	}
}

CCSFunFactMgr::~CCSFunFactMgr()
{
	Shutdown();
}

//-----------------------------------------------------------------------------
// Purpose: Initializes the fun fact manager
//-----------------------------------------------------------------------------
bool CCSFunFactMgr::Init()
{
	ListenForGameEvent( "player_connect" );

	CFunFactHelper *pFunFactHelper = CFunFactHelper::s_pFirst;

	// create database of all fun fact evaluators (and initial usage metrics)
	while ( pFunFactHelper )
	{
		FunFactDatabaseEntry entry;
		entry.fCooldown = 0.0f;
		entry.iOccurrences = 0;
		entry.pEvaluator = pFunFactHelper->m_pfnCreate();
		m_funFactDatabase.Insert(entry.pEvaluator->GetId(), entry);

		pFunFactHelper = pFunFactHelper->m_pNext;
	}

	for (int i = 0; i < ARRAYSIZE(m_playerCooldown); ++i)
	{
		m_playerCooldown[i] = 0.0f;
	}

	m_numRounds = 0;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Shuts down the fun fact manager
//-----------------------------------------------------------------------------
void CCSFunFactMgr::Shutdown()
{
	FOR_EACH_MAP( m_funFactDatabase, iter )
	{
		delete m_funFactDatabase[iter].pEvaluator;
	}
	m_funFactDatabase.RemoveAll();
}

//-----------------------------------------------------------------------------
// Purpose: Per frame processing
//-----------------------------------------------------------------------------
void CCSFunFactMgr::Update( float frametime )
{

}

//-----------------------------------------------------------------------------
// Purpose: Listens for game events.  Clears out map based stats and player based stats when necessary
//-----------------------------------------------------------------------------
void CCSFunFactMgr::FireGameEvent( IGameEvent *event )
{
	const char *eventname = event->GetName();

	if ( Q_strcmp( "player_connect", eventname ) == 0 )
	{
		int index = event->GetInt("index");// player slot (entity index-1)
		ASSERT( index >= 0 && index < MAX_PLAYERS );
		if( index >= 0 && index < MAX_PLAYERS )
		{
			m_playerCooldown[index] = 0.0f;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Finds the best fun fact to display and returns all necessary information through the parameters
//-----------------------------------------------------------------------------
bool CCSFunFactMgr::GetRoundEndFunFact( int iWinningTeam, int iRoundResult, FunFact& funfact )
{
	FunFactVector validFunFacts;

	// Generate a vector of all valid fun facts for this round
	FOR_EACH_MAP( m_funFactDatabase, i )
	{
		FunFact funFact;
		if ( m_funFactDatabase[i].pEvaluator->Evaluate(validFunFacts) )
		{
			m_funFactDatabase[i].iOccurrences++;
		}
	}

	m_numRounds++;

	if (validFunFacts.Size() == 0)
		return false;

	// pick the fun fact with the highest score
	float fBestScore = -FLT_MAX;
	int iFunFactIndex = -1;

#if DEBUG_FUNFACT_SCORING
	Msg("Scoring fun facts:\n");
#endif

	FOR_EACH_VEC(validFunFacts, i)
	{
		float fScore = ScoreFunFact(validFunFacts[i]);

#if DEBUG_FUNFACT_SCORING
		char szPlayerName[64];
		const FunFact& funfact = validFunFacts[i];
		if (funfact.iPlayer > 0)
			V_strncpy(szPlayerName, ToCSPlayer(UTIL_PlayerByIndex(funfact.iPlayer))->GetPlayerName(), sizeof(szPlayerName));
		else
			V_strcpy(szPlayerName, "");

		Msg("(%5.4f) %s, %s, %i, %i, %i\n", fScore, funfact.szLocalizationToken, szPlayerName, funfact.iData1, funfact.iData2, funfact.iData3);
#endif

		if (fScore > fBestScore)
		{
			fBestScore = fScore;
			iFunFactIndex = i;
		}
	}

	if (iFunFactIndex < 0)
		return false;
	
	funfact = validFunFacts[iFunFactIndex];

	// decay player cooldowns
	for (int i = 0; i < MAX_PLAYERS; ++i )
	{
		m_playerCooldown[i] *= (1.0f - kCooldownRatePlayer);
	}

	// decay funfact cooldowns
	FOR_EACH_MAP(m_funFactDatabase, i)
	{
		m_funFactDatabase[i].fCooldown *= (1.0f - kCooldownRateFunFact);
	}

	// set player cooldown for player in funfact
	if ( funfact.iPlayer )
	{
		m_playerCooldown[funfact.iPlayer - 1] = 1.0f;
	}

	// set funfact cooldown for current funfact
	m_funFactDatabase[m_funFactDatabase.Find(funfact.id)].fCooldown = 1.0f;

	return true;
}

float CCSFunFactMgr::ScoreFunFact( const FunFact& funfact )
{
	float fScore = 0.0f;
	const FunFactDatabaseEntry& dbEntry = m_funFactDatabase[m_funFactDatabase.Find(funfact.id)];

	// add the coolness score for the funfact
	fScore += kWeightCoolness * dbEntry.pEvaluator->GetCoolness() * (1.0f + funfact.fMagnitude);

	// subtract the cooldown for the funfact
	fScore -= kWeightFunFactCooldown * dbEntry.fCooldown;
	
	// subtract the cooldown for the player
	if ( funfact.iPlayer ) {
		fScore -= kWeightPlayerCooldown * m_playerCooldown[funfact.iPlayer - 1];
	}

	// add the rarity bonus
	fScore += kWeightRarity * powf((1.0f - (float)dbEntry.iOccurrences / m_numRounds), 2.0f);

	return fScore;
}