aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/client/c_team_train_watcher.cpp
blob: e58aafb12656e2ce64760c6d17c1fb54f9fd0e67 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Entity that propagates train data for escort game type
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "c_team_train_watcher.h"
#include "igameevents.h"
#include "c_team_objectiveresource.h"

#ifdef TF_CLIENT_DLL
#include "tf_shareddefs.h"
#include "teamplayroundbased_gamerules.h"
#endif

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

IMPLEMENT_CLIENTCLASS_DT(C_TeamTrainWatcher, DT_TeamTrainWatcher, CTeamTrainWatcher)

	RecvPropFloat( RECVINFO( m_flTotalProgress ) ),
	RecvPropInt( RECVINFO( m_iTrainSpeedLevel ) ),
	RecvPropFloat( RECVINFO( m_flRecedeTime ) ),
	RecvPropInt( RECVINFO( m_nNumCappers ) ),
#ifdef GLOWS_ENABLE
	RecvPropEHandle( RECVINFO( m_hGlowEnt ) ),
#endif // GLOWS_ENABLE

END_RECV_TABLE()

CUtlVector< CHandle<C_TeamTrainWatcher> > g_hTrainWatchers;

C_TeamTrainWatcher::C_TeamTrainWatcher()
{
	// force updates when we get our baseline
	m_iTrainSpeedLevel = -2;
	m_flTotalProgress = -1;
	m_flRecedeTime = -1;

#ifdef GLOWS_ENABLE
	m_pGlowEffect = NULL;
	m_hGlowEnt = NULL;
	m_hOldGlowEnt = NULL;
#endif // GLOWS_ENABLE
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_TeamTrainWatcher::~C_TeamTrainWatcher()
{
#ifdef GLOWS_ENABLE
	DestroyGlowEffect();
#endif // GLOWS_ENABLE
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::ClientThink()
{
#ifdef GLOWS_ENABLE
	if ( IsDormant() || ( m_hGlowEnt.Get() == NULL ) )
	{
		DestroyGlowEffect();
		m_hOldGlowEnt = NULL;
		m_hGlowEnt = NULL;
	}
#endif // GLOWS_ENABLE
}

#ifdef GLOWS_ENABLE
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::UpdateGlowEffect( void )
{
	// destroy the existing effect
	if ( m_pGlowEffect )
	{
		DestroyGlowEffect();
	}

	// create a new effect if we have a cart
	if ( m_hGlowEnt )
	{
		float r, g, b;
		TeamplayRoundBasedRules()->GetTeamGlowColor( GetTeamNumber(), r, g, b );
		m_pGlowEffect = new CGlowObject( m_hGlowEnt, Vector( r, g, b ), 1.0, true );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::DestroyGlowEffect( void )
{
	if ( m_pGlowEffect )
	{
		delete m_pGlowEffect;
		m_pGlowEffect = NULL;
	}
}
#endif // GLOWS_ENABLE

void C_TeamTrainWatcher::OnPreDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnPreDataChanged( updateType );

	m_iOldTrainSpeedLevel = m_iTrainSpeedLevel;
	m_flOldProgress = m_flTotalProgress;
	m_flOldRecedeTime = m_flRecedeTime;
	m_nOldNumCappers = m_nNumCappers;
#ifdef GLOWS_ENABLE
	m_hOldGlowEnt = m_hGlowEnt;
#endif // GLOWS_ENABLE
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	if ( updateType == DATA_UPDATE_CREATED )
	{
		SetNextClientThink( CLIENT_THINK_ALWAYS );
	}

	if ( m_iOldTrainSpeedLevel != m_iTrainSpeedLevel || m_nOldNumCappers != m_nNumCappers )
	{
		IGameEvent *event = gameeventmanager->CreateEvent( "escort_speed" );
		if ( event )
		{
			event->SetInt( "team", GetTeamNumber() );
			event->SetInt( "speed", m_iTrainSpeedLevel );
			event->SetInt( "players", m_nNumCappers );
			gameeventmanager->FireEventClientSide( event );
		}
	}

	if ( m_flOldProgress != m_flTotalProgress )
	{
		IGameEvent *event = gameeventmanager->CreateEvent( "escort_progress" );
		if ( event )
		{
			event->SetInt( "team", GetTeamNumber() );
			event->SetFloat( "progress", m_flTotalProgress );

			if ( m_flOldProgress <= -1 )
			{
				event->SetBool( "reset", true );
			}

			gameeventmanager->FireEventClientSide( event );
		}

		// check to see if the train is now on a hill
		if ( ObjectiveResource() )
		{
			int nNumHills = ObjectiveResource()->GetNumNodeHillData( GetTeamNumber() );
			if ( nNumHills > 0 )
			{
				float flStart = 0, flEnd = 0;
				for ( int i = 0 ; i < nNumHills ; i++ )
				{
					ObjectiveResource()->GetHillData( GetTeamNumber(), i, flStart, flEnd );

					bool state = ( m_flTotalProgress >= flStart && m_flTotalProgress <= flEnd );
					ObjectiveResource()->SetTrainOnHill( GetTeamNumber(), i, state );
				}
			}
		}
	}

	if ( m_flOldRecedeTime != m_flRecedeTime )
	{
		IGameEvent *event = gameeventmanager->CreateEvent( "escort_recede" );
		if ( event )
		{
			event->SetInt( "team", GetTeamNumber() );
			event->SetFloat( "recedetime", m_flRecedeTime );
			gameeventmanager->FireEventClientSide( event );
		}
	}
#ifdef GLOWS_ENABLE
	if ( m_hOldGlowEnt != m_hGlowEnt )
	{
		UpdateGlowEffect();
	}
#endif // GLOWS_ENABLE
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::Spawn( void )
{
	BaseClass::Spawn();

	if ( g_hTrainWatchers.Find( this ) == g_hTrainWatchers.InvalidIndex() )
	{
		g_hTrainWatchers.AddToTail( this );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void C_TeamTrainWatcher::UpdateOnRemove( void )
{
	BaseClass::UpdateOnRemove();

	g_hTrainWatchers.FindAndRemove( this );
}