summaryrefslogtreecommitdiff
path: root/game/server/tf/tf_hltvdirector.cpp
blob: 454344e246bdb8f0b32722df8a4e7a24c7f9ee86 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "cbase.h"
#include "hltvdirector.h"
#include "team_control_point.h"


CBaseEntity* GetCapturePointByIndex( int iCaptureIndex )
{
	CTeamControlPoint *pTeamControlPoint = (CTeamControlPoint *)gEntList.FindEntityByClassname( NULL, "team_control_point" );

	while ( pTeamControlPoint )
	{
		if ( pTeamControlPoint->GetPointIndex() == iCaptureIndex )
		{
			return pTeamControlPoint;
		}

		pTeamControlPoint = (CTeamControlPoint *)gEntList.FindEntityByClassname( pTeamControlPoint, "team_control_point" );
	}

	return NULL;
}

class CTFHLTVDirector : public CHLTVDirector
{
public:
	DECLARE_CLASS( CTFHLTVDirector, CHLTVDirector );

	const char** GetModEvents();
	void SetHLTVServer( IHLTVServer *hltv );
	void CreateShotFromEvent( CHLTVGameEvent *event );

	virtual char	*GetFixedCameraEntityName( void ) { return "info_observer_point"; }
};

void CTFHLTVDirector::SetHLTVServer( IHLTVServer *hltv )
{
	BaseClass::SetHLTVServer( hltv );

	if ( m_pHLTVServer )
	{
		// mod specific events the director uses to find interesting shots
		ListenForGameEvent( "teamplay_point_captured" );
		ListenForGameEvent( "teamplay_capture_blocked" );
		ListenForGameEvent( "teamplay_point_startcapture" );
		ListenForGameEvent( "teamplay_flag_event" );
		ListenForGameEvent( "ctf_flag_captured" );
	}
}

void CTFHLTVDirector::CreateShotFromEvent( CHLTVGameEvent *event )
{
	// show event at least for 2 more seconds after it occured
	const char *name = event->m_Event->GetName();

	int thera = RandomFloat()>0.5?20:-20;

	if ( !Q_strcmp( "teamplay_point_startcapture", name ) || 
		 !Q_strcmp( "teamplay_point_captured", name ) ||
		 !Q_strcmp( "teamplay_capture_blocked", name ) )
	{
		CBaseEntity *pCapturePoint = GetCapturePointByIndex( event->m_Event->GetInt( "cp" ) );

		int iCameraIndex = -1;
		float flClosest = 99999.9f;
		
		if ( pCapturePoint )
		{
			// Does it have an associated viewpoint?
			for ( int i = 0; i<m_nNumFixedCameras; i++ )
			{
				CBaseEntity *pCamera = m_pFixedCameras[ i ];
		
				if ( pCamera )
				{
					byte pvs[MAX_MAP_CLUSTERS/8];
					int clusterIndex = engine->GetClusterForOrigin( pCamera->GetAbsOrigin() );
					engine->GetPVSForCluster( clusterIndex, sizeof(pvs), pvs );
					bool bCameraInPVS = engine->CheckOriginInPVS( pCapturePoint->GetAbsOrigin(), pvs, sizeof( pvs ) );

					if ( bCameraInPVS == true )
					{
						float flDistance = (pCapturePoint->GetAbsOrigin() - pCamera->GetAbsOrigin()).Length();
						if ( flDistance <= flClosest )
						{
							iCameraIndex = i;
							flClosest = flDistance;
						}
					}
				}
			}
		}

		CBasePlayer *pPlayer = NULL;

		if ( !Q_strcmp( "teamplay_point_captured", name ) )
		{
			const char *pszCappers = event->m_Event->GetString("cappers");
			int nLength = Q_strlen(pszCappers);

			if ( nLength > 0 )
			{
				int iRandomCapper = pszCappers[ RandomInt(0,nLength-1) ];
				pPlayer = UTIL_PlayerByIndex( iRandomCapper );
			}
		}
		else if ( !Q_strcmp( "teamplay_capture_blocked", name ) )
		{
			int iBlocker = event->m_Event->GetInt("blocker");
			pPlayer = UTIL_PlayerByIndex( iBlocker );
		}

		if ( pPlayer )
		{
			if ( iCameraIndex >= 0 && RandomFloat() > 0.66f )
			{
				StartFixedCameraShot( iCameraIndex, pPlayer->entindex() );
			}
			else if ( pCapturePoint )
			{
				StartChaseCameraShot( pPlayer->entindex(), pCapturePoint->entindex(), 96, 20, thera, false );
			}
			else
			{
				StartChaseCameraShot( pPlayer->entindex(), 0, 96, 20, 0, false );
			}
		}
		else if ( iCameraIndex >= 0 && pCapturePoint )
		{
			// no player known for this event
			StartFixedCameraShot( iCameraIndex, pCapturePoint->entindex() );
		}

		// shot 2 seconds after event
		m_nNextShotTick = MIN( m_nNextShotTick, (event->m_Tick+TIME_TO_TICKS(1.0)) );
	}
	else if ( !Q_strcmp( "object_destroyed", name ) )
	{
		CBasePlayer *attacker = UTIL_PlayerByUserId( event->m_Event->GetInt("attacker") );
		if ( attacker )
		{
			int iObjectIndex = event->m_Event->GetInt("index");
			StartChaseCameraShot( attacker->entindex(), iObjectIndex, 96, 20, thera, false );
		}
	}
	else if ( !Q_strcmp( "ctf_flag_captured", name ) )
	{
		CBasePlayer *capper = UTIL_PlayerByUserId( event->m_Event->GetInt("capper") );
		if ( capper )
		{
			StartChaseCameraShot( capper->entindex(), 0, 96, 20, 0, false );
		}
	}
	else if ( !Q_strcmp( "teamplay_flag_event", name ) )
	{
		StartChaseCameraShot( event->m_Event->GetInt("player"), 0, 96, 20, 0, false );
	}
	else
	{

		// let baseclass create a shot
		BaseClass::CreateShotFromEvent( event );
	}
}

const char** CTFHLTVDirector::GetModEvents()
{
	// game events relayed to spectator clients
	static const char *s_modevents[] =
	{
		"game_newmap",
		"hltv_status",
		"hltv_chat",
		"player_connect",
		"player_disconnect",
		"player_changeclass",
		"player_team",
		"player_info",
		"player_death",
		"player_chat",
		"player_spawn",
		"round_start",
		"round_end",
		"server_cvar",
		"server_spawn",
				
		// additional TF events:
		"controlpoint_starttouch",
		"controlpoint_endtouch",
		"ctf_flag_captured",
		"teamplay_broadcast_audio",
		"teamplay_capture_blocked",
		"teamplay_flag_event",
		"teamplay_game_over",
		"teamplay_point_captured",
		"teamplay_round_stalemate",
		"teamplay_round_start",
		"teamplay_round_win",
		"teamplay_timer_time_added",
		"teamplay_update_timer",
		"teamplay_win_panel",
		"training_complete",
		"tf_game_over",
		"object_destroyed",
			
		NULL
	};

	return s_modevents;
}

static CTFHLTVDirector s_HLTVDirector;	// singleton

EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CHLTVDirector, IHLTVDirector, INTERFACEVERSION_HLTVDIRECTOR, s_HLTVDirector );

CHLTVDirector* HLTVDirector()
{
	return &s_HLTVDirector;
}

IGameSystem* HLTVDirectorSystem()
{
	return &s_HLTVDirector;
}