aboutsummaryrefslogtreecommitdiff
path: root/mp/src/game/server/ai_looktarget.cpp
blob: b10bb7cffef92e8db2be53bda6411249d5cd340d (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "game.h"
#include "ai_looktarget.h"

// Mothballing this entity to get rid of it. info_hint does its job better (sjb)
//LINK_ENTITY_TO_CLASS( ai_looktarget, CAI_LookTarget );

BEGIN_DATADESC( CAI_LookTarget )

	// Keyfields
	DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
	DEFINE_KEYFIELD( m_iContext, FIELD_INTEGER, "context" ),
	DEFINE_KEYFIELD( m_iPriority, FIELD_INTEGER, "priority" ),
	DEFINE_KEYFIELD( m_flMaxDist, FIELD_FLOAT, "maxdist" ),

	// Fields
	DEFINE_FIELD( m_flTimeNextAvailable, FIELD_TIME ),

END_DATADESC()

//---------------------------------------------------------
//---------------------------------------------------------
int CAI_LookTarget::DrawDebugTextOverlays(void) 
{
	int text_offset = BaseClass::DrawDebugTextOverlays();

	if (m_debugOverlays & OVERLAY_BBOX_BIT)
	{
		int color = random->RandomInt( 50, 255 );
		NDebugOverlay::Cross3D( GetAbsOrigin(), 12, color, color, color, false, 0.1 );
	}

	if (m_debugOverlays & OVERLAY_TEXT_BIT) 
	{
		char tempstr[512];

		if( !IsEnabled() )
		{
			Q_snprintf(tempstr,sizeof(tempstr),"DISABLED" );
			EntityText(text_offset,tempstr,0);
			text_offset++;
		}

		if( IsEligible( NULL ) )
		{
			Q_snprintf(tempstr,sizeof(tempstr),"Eligible" );
			EntityText(text_offset,tempstr,0);
			text_offset++;
		}
		else
		{
			Q_snprintf(tempstr,sizeof(tempstr),"NOT Eligible for selection");
			EntityText(text_offset,tempstr,0);
			text_offset++;
		}
	}
	return text_offset;
}

//---------------------------------------------------------
//---------------------------------------------------------
bool CAI_LookTarget::IsEligible( CBaseEntity *pLooker )
{
	if( !IsEnabled() )
		return false;

	if( !IsAvailable() )
		return false;

	if( pLooker )
	{
		float maxdistsqr = m_flMaxDist * m_flMaxDist;
		
		Vector vecPos = GetAbsOrigin();

		if( vecPos.DistToSqr( pLooker->WorldSpaceCenter() ) > maxdistsqr )
		{
			return false;
		}
	}

	return true;
}

//---------------------------------------------------------
// Someone's reserving this entity because they're going 
// to attempt to look at it for flDuration seconds. We'll
// make it unavailable to anyone else for that time.
//---------------------------------------------------------
void CAI_LookTarget::Reserve( float flDuration )
{
	m_flTimeNextAvailable = gpGlobals->curtime + flDuration;

	if( HasSpawnFlags( SF_LOOKTARGET_ONLYONCE ) )
	{
		// No one will look at this again.
		Disable();
	}
}

//---------------------------------------------------------
//---------------------------------------------------------
CAI_LookTarget *CAI_LookTarget::GetFirstLookTarget()
{
	CBaseEntity		*pEnt;

	string_t iszLookTarget = FindPooledString( "ai_looktarget" );
	if( iszLookTarget == NULL_STRING )
	{
		return NULL;
	}

	pEnt = gEntList.FirstEnt();
	while( pEnt && pEnt->m_iClassname != iszLookTarget )
	{
		pEnt = gEntList.NextEnt( pEnt );
	}

	return (CAI_LookTarget*)pEnt;
}

//---------------------------------------------------------
//---------------------------------------------------------
CAI_LookTarget *CAI_LookTarget::GetNextLookTarget( CAI_LookTarget *pCurrentTarget )
{
	CBaseEntity		*pEnt;

	string_t iszLookTarget = FindPooledString( "ai_looktarget" );
	if( iszLookTarget == NULL_STRING )
	{
		return NULL;
	}

	pEnt = gEntList.NextEnt( pCurrentTarget );
	while( pEnt && pEnt->m_iClassname != iszLookTarget )
	{
		pEnt = gEntList.NextEnt( pEnt );
	}

	return (CAI_LookTarget*)pEnt;
}