-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsimple_bot.cpp
199 lines (156 loc) · 5.75 KB
/
simple_bot.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
// simple_bot.cpp
// A simple bot
// Michael Booth, February 2009
#include "cbase.h"
#include "simple_bot.h"
#include "nav_mesh.h"
//-----------------------------------------------------------------------------------------------------
// Command to add a Simple Bot where your crosshairs are aiming
//-----------------------------------------------------------------------------------------------------
CON_COMMAND_F( simple_bot_add, "Add a simple bot.", FCVAR_CHEAT )
{
CBasePlayer *player = UTIL_GetCommandClient();
if ( !player )
{
return;
}
Vector forward;
player->EyeVectors( &forward );
trace_t result;
UTIL_TraceLine( player->EyePosition(), player->EyePosition() + 999999.9f * forward, MASK_BLOCKLOS_AND_NPCS|CONTENTS_IGNORE_NODRAW_OPAQUE, player, COLLISION_GROUP_NONE, &result );
if ( !result.DidHit() )
{
return;
}
CSimpleBot *bot = static_cast< CSimpleBot * >( CreateEntityByName( "simple_bot" ) );
if ( bot )
{
Vector forward = player->GetAbsOrigin() - result.endpos;
forward.z = 0.0f;
forward.NormalizeInPlace();
QAngle angles;
VectorAngles( forward, angles );
bot->SetAbsAngles( angles );
bot->SetAbsOrigin( result.endpos + Vector( 0, 0, 10.0f ) );
DispatchSpawn( bot );
}
}
//-----------------------------------------------------------------------------------------------------
// The Simple Bot
//-----------------------------------------------------------------------------------------------------
LINK_ENTITY_TO_CLASS( simple_bot, CSimpleBot );
#ifndef TF_DLL
PRECACHE_REGISTER( simple_bot );
#endif
//-----------------------------------------------------------------------------------------------------
CSimpleBot::CSimpleBot()
{
ALLOCATE_INTENTION_INTERFACE( CSimpleBot );
m_locomotor = new NextBotGroundLocomotion( this );
}
//-----------------------------------------------------------------------------------------------------
CSimpleBot::~CSimpleBot()
{
DEALLOCATE_INTENTION_INTERFACE;
if ( m_locomotor )
delete m_locomotor;
}
//-----------------------------------------------------------------------------------------------------
void CSimpleBot::Precache()
{
BaseClass::Precache();
#ifndef DOTA_DLL
PrecacheModel( "models/humans/group01/female_01.mdl" );
#endif
}
//-----------------------------------------------------------------------------------------------------
void CSimpleBot::Spawn( void )
{
BaseClass::Spawn();
#ifndef DOTA_DLL
SetModel( "models/humans/group01/female_01.mdl" );
#endif
}
//---------------------------------------------------------------------------------------------
// The Simple Bot behaviors
//---------------------------------------------------------------------------------------------
/**
* For use with TheNavMesh->ForAllAreas()
* Find the Nth area in the sequence
*/
class SelectNthAreaFunctor
{
public:
SelectNthAreaFunctor( int count )
{
m_count = count;
m_area = NULL;
}
bool operator() ( CNavArea *area )
{
m_area = area;
return ( m_count-- > 0 );
}
int m_count;
CNavArea *m_area;
};
//---------------------------------------------------------------------------------------------
/**
* This action causes the bot to pick a random nav area in the mesh and move to it, then
* pick another, etc.
* Actions usually each have their own .cpp/.h file and are organized into folders since there
* are often many of them. For this example, we're keeping everything to a single .cpp/.h file.
*/
class CSimpleBotRoam : public Action< CSimpleBot >
{
public:
//----------------------------------------------------------------------------------
// OnStart is called once when the Action first becomes active
virtual ActionResult< CSimpleBot > OnStart( CSimpleBot *me, Action< CSimpleBot > *priorAction )
{
// smooth out the bot's path following by moving toward a point farther down the path
m_path.SetMinLookAheadDistance( 300.0f );
return Continue();
}
//----------------------------------------------------------------------------------
// Update is called repeatedly (usually once per server frame) while the Action is active
virtual ActionResult< CSimpleBot > Update( CSimpleBot *me, float interval )
{
if ( m_path.IsValid() && !m_timer.IsElapsed() )
{
// PathFollower::Update() moves the bot along the path using the bot's ILocomotion and IBody interfaces
m_path.Update( me );
}
else
{
SelectNthAreaFunctor pick( RandomInt( 0, TheNavMesh->GetNavAreaCount() - 1 ) );
TheNavMesh->ForAllAreas( pick );
if ( pick.m_area )
{
CSimpleBotPathCost cost( me );
m_path.Compute( me, pick.m_area->GetCenter(), cost );
}
// follow this path for a random duration (or until we reach the end)
m_timer.Start( RandomFloat( 5.0f, 10.0f ) );
}
return Continue();
}
//----------------------------------------------------------------------------------
// this is an event handler - many more are available (see declaration of Action< Actor > in NextBotBehavior.h)
virtual EventDesiredResult< CSimpleBot > OnStuck( CSimpleBot *me )
{
// we are stuck trying to follow the current path - invalidate it so a new one is chosen
m_path.Invalidate();
return TryContinue();
}
virtual const char *GetName( void ) const { return "Roam"; } // return name of this action
private:
PathFollower m_path;
CountdownTimer m_timer;
};
//---------------------------------------------------------------------------------------------
/**
* Instantiate the bot's Intention interface and start the initial Action (CSimpleBotRoam in this case)
*/
IMPLEMENT_INTENTION_INTERFACE( CSimpleBot, CSimpleBotRoam )