summaryrefslogtreecommitdiff
path: root/game/server/tf/bot/behavior/tf_bot_use_teleporter.cpp
blob: fc249fb212d5fcf5fa92f8379d9aba242efb1d92 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
// tf_bot_use_teleporter.cpp
// Ride a friendly teleporter
// Michael Booth, May 2010

#include "cbase.h"
#include "nav_mesh.h"
#include "tf_player.h"
#include "bot/tf_bot.h"
#include "bot/behavior/tf_bot_use_teleporter.h"

extern ConVar tf_bot_path_lookahead_range;

//---------------------------------------------------------------------------------------------
CTFBotUseTeleporter::CTFBotUseTeleporter( CObjectTeleporter *teleporter, UseHowType how )
{
	m_teleporter = teleporter;
	m_how = how;
	m_isInTransit = false;
}


//---------------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotUseTeleporter::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
	m_path.SetMinLookAheadDistance( me->GetDesiredPathLookAheadRange() );

	return Continue();
}


//---------------------------------------------------------------------------------------------
// We could compute the time it would take to walk from the tele entrance to exit
// and compare that to the time needed to wait for the tele to be ready to send us,
// but players tend to use the tele only if it is ready NOW (or very soon).
bool CTFBotUseTeleporter::IsTeleporterAvailable( void ) const
{
	if ( m_teleporter != NULL )
	{
		if ( !m_teleporter->IsReady() )
			return false;

		if ( m_teleporter->GetState() == TELEPORTER_STATE_READY )
			return true;

/* causes massive bot pileups
		if ( m_teleporter->GetState() == TELEPORTER_STATE_SENDING ||
			 m_teleporter->GetState() == TELEPORTER_STATE_RECHARGING )
		{
			if ( m_teleporter->GetUpgradeLevel() == 3 )
			{
				// we'll wait for level 3 teleporters - they're really fast
				return true;
			}
		}
*/
	}

	return false;
}


//---------------------------------------------------------------------------------------------
ActionResult< CTFBot >	CTFBotUseTeleporter::Update( CTFBot *me, float interval )
{
	if ( m_teleporter == NULL )
	{
		return Done( "Teleporter is gone" );
	}

	CObjectTeleporter *teleporterExit = m_teleporter->GetMatchingTeleporter();
	if ( !teleporterExit )
	{
		return Done( "Missing teleporter exit" );
	}

	if ( m_teleporter->IsSendingPlayer( me ) )
	{
		// note that we have been teleported, because it takes a few frames
		// to actually relocate us, even after our teleporter leaves the "sending" state
		m_isInTransit = true;
	}

	if ( m_isInTransit )
	{
		if ( me->IsRangeLessThan( teleporterExit, 25.0f ) )
		{
			return Done( "Successful teleport" );
		}
	}
	else if ( !IsTeleporterAvailable() && m_how == USE_IF_READY )
	{
		return Done( "Teleporter is not available" );
	}

	const CKnownEntity *threat = me->GetVisionInterface()->GetPrimaryKnownThreat();
	if ( threat && threat->IsVisibleRecently() )
	{
		// prepare to fight
		me->EquipBestWeaponForThreat( threat );
	}

	if ( m_repathTimer.IsElapsed() )
	{
		m_repathTimer.Start( RandomFloat( 1.0f, 2.0f ) );

		CTFBotPathCost cost( me, FASTEST_ROUTE );
		if ( m_path.Compute( me, m_teleporter->GetAbsOrigin(), cost ) == false )
		{
			// no path to teleporter
			return Done( "Can't reach teleporter!" );
		}
	}

	// move toward the teleporter until we're standing on it
	if ( me->GetLocomotionInterface()->GetGround() != m_teleporter )
	{
		m_path.Update( me );
	}

	return Continue();
}