summaryrefslogtreecommitdiff
path: root/game/server/tf2/sensor_tf_team.cpp
blob: 083d2d41549bf3327392ceea43758bf006f776bc (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Definitions of all the entities that control logic flow within a map
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "EntityInput.h"
#include "EntityOutput.h"
#include "tf_team.h"
#include "tf_obj.h"


//-----------------------------------------------------------------------------
// Purpose: Detects a bunch of tf team state
//-----------------------------------------------------------------------------
class CSensorTFTeam : public CLogicalEntity
{
	DECLARE_CLASS( CSensorTFTeam, CLogicalEntity );

public:
	void Spawn( void );
	void Think( void );

private:
	DECLARE_DATADESC();

	// Computes the number of respawns stations on the sensed team
	int ComputeRespawnCount();

	// outputs
	COutputInt m_OnRespawnCountChanged;
	COutputInt m_OnResourceCountChanged;
	COutputInt m_OnMemberCountChanged;
	COutputInt m_OnRespawnCountChangedDelta;
	COutputInt m_OnResourceCountChangedDelta;
	COutputInt m_OnMemberCountChangedDelta;

	// What team am I sensing?
	int	m_nTeam;
	CTFTeam *m_pTeam;

	// So we can know when state changes...
	int m_nRespawnCount;
	int m_nResourceCount;
	int m_nMemberCount;
};


LINK_ENTITY_TO_CLASS( sensor_tf_team, CSensorTFTeam );


BEGIN_DATADESC( CSensorTFTeam )

	DEFINE_OUTPUT( m_OnRespawnCountChanged, "OnRespawnCountChanged" ),
	DEFINE_OUTPUT( m_OnResourceCountChanged, "OnResourceCountChanged" ),
	DEFINE_OUTPUT( m_OnMemberCountChanged, "OnMemberCountChanged" ),
	DEFINE_OUTPUT( m_OnRespawnCountChangedDelta, "OnRespawnCountChangedDelta" ),
	DEFINE_OUTPUT( m_OnResourceCountChangedDelta, "OnResourceCountChangedDelta" ),
	DEFINE_OUTPUT( m_OnMemberCountChangedDelta, "OnMemberCountChangedDelta" ),
	DEFINE_KEYFIELD( m_nTeam, FIELD_INTEGER, "team"),

END_DATADESC()




//-----------------------------------------------------------------------------
// Spawn!
//-----------------------------------------------------------------------------
void CSensorTFTeam::Spawn( void )
{
	// Hook us up to a team...
	m_pTeam = GetGlobalTFTeam( m_nTeam );

	// Gets us thinkin!
	SetNextThink( gpGlobals->curtime + 0.1f );

	// Force an output message on our first think
	m_nRespawnCount = -1;
	m_nResourceCount = -1;
}


//-----------------------------------------------------------------------------
// Compute the number of respawn stations on this team
//-----------------------------------------------------------------------------
int CSensorTFTeam::ComputeRespawnCount()
{
	int nCount = 0;
	for (int i = m_pTeam->GetNumObjects(); --i >= 0; )
	{
		CBaseObject *pObject = m_pTeam->GetObject(i);
		if ( pObject && (pObject->GetType() == OBJ_RESPAWN_STATION) )
		{
			++nCount;
		}
	}
	return nCount;
}


//-----------------------------------------------------------------------------
// Purpose: Forces a recompare
//-----------------------------------------------------------------------------
void CSensorTFTeam::Think( )
{
	if (!m_pTeam)
		return;

	// Check for a difference in the number of respawn stations
	int nRespawnCount = ComputeRespawnCount();
	if ( nRespawnCount != m_nRespawnCount )
	{
		m_OnRespawnCountChangedDelta.Set( nRespawnCount - m_nRespawnCount, this, this );
		m_nRespawnCount = nRespawnCount;
		m_OnRespawnCountChanged.Set( m_nRespawnCount, this, this );
	}

	// Check for a difference in the number of resources harvested
	if ( m_nResourceCount != m_pTeam->m_flTotalResourcesSoFar )
	{
		m_OnResourceCountChangedDelta.Set( m_pTeam->m_flTotalResourcesSoFar - m_nResourceCount, this, this );
		m_nResourceCount = m_pTeam->m_flTotalResourcesSoFar;
		m_OnResourceCountChanged.Set( m_nResourceCount, this, this );
	}

	// Check for a difference in the number of team members
	if ( m_nMemberCount != m_pTeam->GetNumPlayers() )
	{
		m_OnMemberCountChangedDelta.Set( m_pTeam->GetNumPlayers() - m_nMemberCount, this, this );
		m_nMemberCount = m_pTeam->GetNumPlayers();
		m_OnMemberCountChanged.Set( m_nMemberCount, this, this );
	}

	SetNextThink( gpGlobals->curtime + 0.1f );
}