summaryrefslogtreecommitdiff
path: root/game/server/tf/nav_mesh/tf_path_follower.cpp
blob: f5663302c4cb3acf160c1bfdfb1c9f3aa8c3ac6e (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
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_path_follower.cpp
// Simplified path following for TF2
// Author: Michael Booth, November 2010

#include "cbase.h"

#include "NextBotManager.h"
#include "tf_path_follower.h"

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



//--------------------------------------------------------------------------------------------------------------
/**
 * Constructor
 */
CTFPathFollower::CTFPathFollower( void )
{
	m_goal = NULL;
	m_minLookAheadRange = 300.0f;
}


//--------------------------------------------------------------------------------------------------------------
CTFPathFollower::~CTFPathFollower()
{
	// allow bots to detach pointer to me
	CUtlVector< INextBot * > botVector;
	TheNextBots().CollectAllBots( &botVector );
	
	for( int i=0; i<botVector.Count(); ++i )
	{
		botVector[i]->NotifyPathDestruction( this );
	}
}


//--------------------------------------------------------------------------------------------------------------
/**
 * When the path is invalidated, the follower is also reset
 */
void CTFPathFollower::Invalidate( void )
{
	// extend
	Path::Invalidate();

	m_goal = NULL;
	MoveCursorToStart();
}


//--------------------------------------------------------------------------------------------------------------
/**
 * Invoked when the path is (re)computed (path is valid at the time of this call)
 */
void CTFPathFollower::OnPathChanged( INextBot *bot, Path::ResultType result )
{
	// start from the beginning
	m_goal = FirstSegment();
	MoveCursorToStart();
}


//--------------------------------------------------------------------------------------------------------------
/**
 * Move mover along path
 */
void CTFPathFollower::Update( INextBot *bot )
{
	VPROF_BUDGET( "CTFPathFollower::Update", "NextBot" );
	ILocomotion *mover = bot->GetLocomotionInterface();

	// track most recent path followed
	bot->SetCurrentPath( this );

	if ( !IsValid() || m_goal == NULL )
	{
		return;
	}

	// check if we've reached the end of the path
	const float nearRange = 25.0f;
	if ( mover->IsOnGround() && ( GetEndPosition() - mover->GetFeet() ).AsVector2D().IsLengthLessThan( nearRange ) )
	{
		// the end of the path has been reached
		mover->GetBot()->OnMoveToSuccess( this );

		if ( bot->IsDebugging( NEXTBOT_PATH ) )
		{
			DevMsg( "CTFPathFollower: OnMoveToSuccess\n" );
		}

		// don't invalidate if OnMoveToSuccess just recomputed a new path
		if ( GetAge() > 0.0f )
		{
			Invalidate();
		}

		return;
	}

	// move along the path
	MoveCursorToClosestPosition( mover->GetFeet(), SEEK_AHEAD );
	float myCursorPosition = GetCursorPosition();
	const Path::Data &data = GetCursorData();

	if ( !data.segmentPrior )
	{
		// this shouldn't happen
		mover->GetBot()->OnMoveToFailure( this, FAIL_STUCK );
		Invalidate();
		return;
	}

	// set goal to be just ahead of wherever we happen to be on the path
	m_goal = NextSegment( data.segmentPrior );

	if ( !m_goal )
	{
		m_goal = data.segmentPrior;
	}

	// find actual move-to position farther down the path
	Vector moveToPos = m_goal->pos;

	// follow point farther down the path to smooth out our movement
	for( float ahead = m_minLookAheadRange; ahead > 0.0f; ahead -= 50.0f )
	{
		MoveCursor( myCursorPosition, PATH_ABSOLUTE_DISTANCE );
		MoveCursor( ahead, PATH_RELATIVE_DISTANCE );

		// get path data at this lookahead point
		const Path::Data &data = GetCursorData();

		if ( mover->IsPotentiallyTraversable( mover->GetFeet(), data.pos ) )
		{
			moveToPos = data.pos;
			break;
		}
	}

	// move bot along path
	mover->FaceTowards( moveToPos );
	mover->Approach( moveToPos );

	// debug display
	if ( bot->IsDebugging( NEXTBOT_PATH ) )
	{
		Path::Draw();

		NDebugOverlay::Cross3D( moveToPos, 5.0f, 150, 150, 255, true, 0.1f );
		NDebugOverlay::Line( bot->GetEntity()->WorldSpaceCenter(), moveToPos, 255, 255, 0, true, 0.1f );
	}
}