summaryrefslogtreecommitdiff
path: root/game/server/tf/bot/behavior/training/tf_bot_training.cpp
blob: 7db948b0e1b1ef253466ef34af2047a68992d0a4 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
////////////////////////////////////////////////////////////////////////////////////////////////////
// tf_bot_training.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#include "cbase.h"
#include "team.h"
#include "bot/tf_bot.h"
#include "bot/map_entities/tf_bot_generator.h"
#include "bot/behavior/training/tf_bot_training.h"
#include "tf_obj_sentrygun.h"

////////////////////////////////////////////////////////////////////////////////////////////////////

ActionResult< CTFBot > CTFDespawn::Update( CTFBot *me, float interval )
{
	// players need to be kicked, not deleted
	if ( me->GetEntity()->IsPlayer() )
	{
		CBasePlayer *player = dynamic_cast< CBasePlayer * >( me->GetEntity() );
		engine->ServerCommand( UTIL_VarArgs( "kickid %d\n", player->GetUserID() ) );
	}
	else
	{
		UTIL_Remove( me->GetEntity() );
	}
	return Continue();
}

////////////////////////////////////////////////////////////////////////////////////////////////////

ActionResult< CTFBot > CTFTrainingAttackSentryActionPoint::Update( CTFBot *me, float interval )
{
	CTFBotActionPoint* pActionPoint = me->GetActionPoint();
	if ( pActionPoint == NULL )
	{
		return Done();
	}

	if ( pActionPoint->IsWithinRange( me ) )
	{
		CObjectSentrygun *pSentrygun = me->GetEnemySentry();
		if ( pSentrygun )
		{
			me->GetBodyInterface()->AimHeadTowards( pSentrygun, IBody::MANDATORY, 1.0f, NULL, "Aiming at enemy sentry" );

			// because sentries are stationary, check if XY is on target to allow SelectTargetPoint() to adjust Z for grenades
			Vector toSentry = pSentrygun->WorldSpaceCenter() - me->EyePosition();
			toSentry.NormalizeInPlace();
			Vector forward;
			me->EyeVectors( &forward );
			
			if ( ( forward.x * toSentry.x + forward.y * toSentry.y ) > 0.95f )
			{			
				me->PressFireButton();
			}
		}
	}
	else
	{
		if ( m_repathTimer.IsElapsed() )
		{
			m_repathTimer.Start( RandomFloat( 1.0f, 2.0f ) );
			
			CTFBotPathCost cost( me, FASTEST_ROUTE );
			m_path.Compute( me, pActionPoint->GetAbsOrigin(), cost );
		}
		
		m_path.Update( me );
	}

	return Continue();
}

////////////////////////////////////////////////////////////////////////////////////////////////////

ActionResult< CTFBot > CTFGotoActionPoint::OnStart( CTFBot *me, Action< CTFBot > *priorAction )
{
	m_stayTimer.Invalidate();
	m_wasTeleported = false;

	return Continue();
}

ActionResult< CTFBot > CTFGotoActionPoint::Update( CTFBot *me, float interval )
{
	CTFBotActionPoint* pActionPoint = me->GetActionPoint();
	if ( pActionPoint == NULL )
	{
		return Done();
	}

	if ( pActionPoint->IsWithinRange( me ) )
	{
		// track if we ever get teleported during this process
		m_wasTeleported |= me->m_Shared.InCond( TF_COND_SELECTED_TO_TELEPORT );

		// we're at the action point
		if ( m_stayTimer.HasStarted() == false )
		{
			// this method may cause us to become suspended for other actions
			pActionPoint->ReachedActionPoint( me );

			m_stayTimer.Start( pActionPoint->m_stayTime );
		}
		else if ( m_stayTimer.IsElapsed() )
		{
			me->SetActionPoint( dynamic_cast< CTFBotActionPoint * >( pActionPoint->m_moveGoal.Get() ) );
			return ChangeTo( new CTFGotoActionPoint, "Reached point, going to next" );
		}		
	}
	else if ( m_wasTeleported )
	{
		// we reached our action point, but were teleported far away.
		// presumably we've resumed, so just go to the next action point.
		me->SetActionPoint( dynamic_cast< CTFBotActionPoint * >( pActionPoint->m_moveGoal.Get() ) );
		return ChangeTo( new CTFGotoActionPoint, "Reached point, going to next" );
	}
	else
	{
		if ( m_repathTimer.IsElapsed() )
		{
			m_repathTimer.Start( RandomFloat( 1.0f, 2.0f ) );
			
			CTFBotPathCost cost( me, FASTEST_ROUTE );
			m_path.Compute( me, pActionPoint->GetAbsOrigin(), cost );
		}
		
		m_path.Update( me );
	}
	return Continue();
}