summaryrefslogtreecommitdiff
path: root/game/server/tf/bot_npc/bot_npc_decoy.cpp
blob: 757a467747f5c0a16c5ea7fb44532cb2a14466c8 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//========= Copyright Valve Corporation, All rights reserved. ============//
// bot_npc_decoy.cpp
// A NextBot non-player decoy that imitates a real player
// Michael Booth, January 2011

#include "cbase.h"
#include "tf_player.h"
#include "tf_gamerules.h"
#include "tf_team.h"
#include "nav_mesh/tf_nav_area.h"
#include "bot_npc_decoy.h"
#include "econ_wearable.h"

LINK_ENTITY_TO_CLASS( bot_npc_decoy, CBotNPCDecoy );
PRECACHE_REGISTER( bot_npc_decoy );

ConVar tf_decoy_lifetime( "tf_decoy_lifetime", "5", FCVAR_CHEAT, "The lifetime of a decoy, in seconds" );


//-----------------------------------------------------------------------------------------------------
CBotNPCDecoy::CBotNPCDecoy()
{
	ALLOCATE_INTENTION_INTERFACE( CBotNPCDecoy );

	m_locomotor = new CBotNPCDecoyLocomotion( this );
	m_body = new CBotNPCBody( this );

	m_eyeOffset = vec3_origin;
}


//-----------------------------------------------------------------------------------------------------
CBotNPCDecoy::~CBotNPCDecoy()
{
	DEALLOCATE_INTENTION_INTERFACE;

	if ( m_locomotor )
		delete m_locomotor;

	if ( m_body )
		delete m_body;
}

//-----------------------------------------------------------------------------------------------------
void CBotNPCDecoy::Precache()
{
	BaseClass::Precache();
}


//-----------------------------------------------------------------------------------------------------
void CBotNPCDecoy::Spawn( void )
{
	BaseClass::Spawn();

	SetCollisionGroup( COLLISION_GROUP_NONE );
	SetSolid( SOLID_NONE );
	AddSolidFlags( FSOLID_NOT_SOLID );

	CTFPlayer *owner = ToTFPlayer( GetOwnerEntity() );
	if ( !owner )
	{
		Warning( "Decoy spawned without an owner\n" );
		return;
	}

	int ownerClass = owner->m_Shared.InCond( TF_COND_DISGUISED ) ? owner->m_Shared.GetDisguiseClass() : owner->GetPlayerClass()->GetClassIndex();
	int ownerTeam = owner->m_Shared.InCond( TF_COND_DISGUISED ) ? owner->m_Shared.GetDisguiseTeam() : owner->GetTeamNumber();

	SetModel( GetPlayerClassData( ownerClass )->m_szModelName );
	ChangeTeam( ownerTeam );

	if ( ownerTeam == TF_TEAM_BLUE )
	{
		m_nSkin = 1;
	}
	else
	{
		m_nSkin = 0;
	}

	SetAbsOrigin( owner->GetAbsOrigin() );
	SetAbsAngles( owner->GetAbsAngles() );
	SetAbsVelocity( owner->GetAbsVelocity() );

	Vector headPos;
	QAngle headAngles;
	if ( GetAttachment( "head", headPos, headAngles ) )
	{
		m_eyeOffset = headPos - GetAbsOrigin();
	}

	CTFWeaponBase *theirWeapon = owner->m_Shared.GetDisguiseWeapon();
	if ( !theirWeapon )
	{
		theirWeapon = owner->GetActiveTFWeapon();
	}

	if ( theirWeapon )
	{
		CBaseAnimating *weapon = (CBaseAnimating *)CreateEntityByName( "prop_dynamic" );
		if ( weapon )
		{
			weapon->SetModel( theirWeapon->GetWorldModel() );

			// bonemerge the weapon into our model
			weapon->FollowEntity( this, true );

			// choose the appropriate run animation for this weapon
			switch( theirWeapon->GetTFWpnData().m_iWeaponType )
			{
			case TF_WPN_TYPE_PRIMARY:
				m_runActivity = ACT_MP_RUN_PRIMARY;
				break;

			case TF_WPN_TYPE_SECONDARY:
				m_runActivity = ACT_MP_RUN_SECONDARY;
				break;

			case TF_WPN_TYPE_MELEE:
			default:
				m_runActivity = ACT_MP_RUN_MELEE;
				break;
			}
		}
	}
}


//---------------------------------------------------------------------------------------------
unsigned int CBotNPCDecoy::PhysicsSolidMaskForEntity( void ) const
{ 
	// Only collide with the other team
	int teamContents = ( GetTeamNumber() == TF_TEAM_RED ) ? CONTENTS_BLUETEAM : CONTENTS_REDTEAM;

	return BaseClass::PhysicsSolidMaskForEntity() | teamContents;
}


//---------------------------------------------------------------------------------------------
bool CBotNPCDecoy::ShouldCollide( int collisionGroup, int contentsMask ) const
{
	if ( collisionGroup == COLLISION_GROUP_PLAYER_MOVEMENT )
	{
		switch( GetTeamNumber() )
		{
		case TF_TEAM_RED:
			if ( !( contentsMask & CONTENTS_REDTEAM ) )
				return false;
			break;

		case TF_TEAM_BLUE:
			if ( !( contentsMask & CONTENTS_BLUETEAM ) )
				return false;
			break;
		}
	}

	return BaseClass::ShouldCollide( collisionGroup, contentsMask );
}


//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
float CBotNPCDecoyLocomotion::GetRunSpeed( void ) const
{
	CTFPlayer *owner = ToTFPlayer( GetBot()->GetEntity()->GetOwnerEntity() );
	if ( !owner )
	{
		return 0.0f;
	}

	int ownerClass = owner->m_Shared.InCond( TF_COND_DISGUISED ) ? owner->m_Shared.GetDisguiseClass() : owner->GetPlayerClass()->GetClassIndex();
	return GetPlayerClassData( ownerClass )->m_flMaxSpeed;
}


//---------------------------------------------------------------------------------------------
// return maximum acceleration of locomotor
float CBotNPCDecoyLocomotion::GetMaxAcceleration( void ) const
{
	return 1500.0f;
}


//---------------------------------------------------------------------------------------------
// return maximum deceleration of locomotor
float CBotNPCDecoyLocomotion::GetMaxDeceleration( void ) const
{
	return 1500.0f;
}


//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
class CBotNPCDecoyBehavior : public Action< CBotNPCDecoy >
{
public:
	virtual ActionResult< CBotNPCDecoy > OnStart( CBotNPCDecoy *me, Action< CBotNPCDecoy > *priorAction )
	{
		m_timer.Start( tf_decoy_lifetime.GetFloat() );

		// play running animation
		if ( !me->GetBodyInterface()->IsActivity( me->GetRunActivity() ) )
		{
			me->GetBodyInterface()->StartActivity( me->GetRunActivity() );
		}

		return Continue(); 
	}

	virtual ActionResult< CBotNPCDecoy > Update( CBotNPCDecoy *me, float interval )
	{
		if ( m_timer.IsElapsed() )
		{
			// we're out of time
			UTIL_Remove( me );
			return Done( "Lifetime expired" );
		}

		CTFPlayer *owner = ToTFPlayer( me->GetOwnerEntity() );
		if ( !owner )
		{
			UTIL_Remove( me );
			return Done( "No owner!" );
		}

		Vector forward;
		me->GetVectors( &forward, NULL, NULL );

		me->GetLocomotionInterface()->SetDesiredSpeed( FLT_MAX );	// this is just a rate limiter
		me->GetLocomotionInterface()->Run();
		me->GetLocomotionInterface()->Approach( me->GetAbsOrigin() + 100.0f * forward );

		return Continue();
	}

	virtual const char *GetName( void ) const	{ return "Behavior"; }		// return name of this action

private:
	CountdownTimer m_timer;
};


IMPLEMENT_INTENTION_INTERFACE( CBotNPCDecoy, CBotNPCDecoyBehavior );