-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNextBotUtil.h
278 lines (228 loc) · 8.25 KB
/
NextBotUtil.h
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// NextBotUtil.h
// Utilities for the NextBot system
// Author: Michael Booth, May 2006
//========= Copyright Valve Corporation, All rights reserved. ============//
#ifndef _NEXT_BOT_UTIL_H_
#define _NEXT_BOT_UTIL_H_
#include "NextBotLocomotionInterface.h"
#include "nav_area.h"
#include "nav_mesh.h"
#include "nav_pathfind.h"
//--------------------------------------------------------------------------------------------
/**
* A simple filter interface for various NextBot queries
*/
class INextBotEntityFilter
{
public:
// return true if the given entity passes this filter
virtual bool IsAllowed( CBaseEntity *entity ) const = 0;
};
// trace filter callback functions. needed for use with the querycache/optimization functionality
bool VisionTraceFilterFunction( IHandleEntity *pServerEntity, int contentsMask );
bool IgnoreActorsTraceFilterFunction( IHandleEntity *pServerEntity, int contentsMask );
//--------------------------------------------------------------------------------------------
/**
* Trace filter that skips all players and NextBots
*/
class NextBotTraceFilterIgnoreActors : public CTraceFilterSimple
{
public:
NextBotTraceFilterIgnoreActors( const IHandleEntity *passentity, int collisionGroup ) : CTraceFilterSimple( passentity, collisionGroup, IgnoreActorsTraceFilterFunction )
{
}
};
//--------------------------------------------------------------------------------------------
/**
* Trace filter that skips all players, NextBots, and non-LOS blockers
*/
class NextBotVisionTraceFilter : public CTraceFilterSimple
{
public:
NextBotVisionTraceFilter( const IHandleEntity *passentity, int collisionGroup ) : CTraceFilterSimple( passentity, collisionGroup, VisionTraceFilterFunction )
{
}
};
//--------------------------------------------------------------------------------------------
/**
* Trace filter that skips all NextBots, but includes Players
*/
class NextBotTraceFilterIgnoreNextBots : public CTraceFilterSimple
{
public:
NextBotTraceFilterIgnoreNextBots( const IHandleEntity *passentity, int collisionGroup )
: CTraceFilterSimple( passentity, collisionGroup )
{
}
virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
{
CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
#ifdef TERROR
CBasePlayer *player = ToBasePlayer( entity );
if ( player && player->IsGhost() )
return false;
#endif // TERROR
return ( entity->MyNextBotPointer() == NULL );
}
return false;
}
};
//--------------------------------------------------------------------------------------------
/**
* Trace filter that obeys INextBot::IsAbleToBlockMovementOf()
*/
class NextBotTraceFilter : public CTraceFilterSimple
{
public:
NextBotTraceFilter( const IHandleEntity *passentity, int collisionGroup )
: CTraceFilterSimple( passentity, collisionGroup )
{
CBaseEntity *entity = const_cast<CBaseEntity *>(EntityFromEntityHandle( passentity ));
m_passBot = entity->MyNextBotPointer();
}
virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
{
CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
#ifdef TERROR
CBasePlayer *player = ToBasePlayer( entity );
if ( player && player->IsGhost() )
return false;
#endif // TERROR
// Skip players on the same team - they're not solid to us, and we'll avoid them
if ( entity->IsPlayer() && m_passBot && m_passBot->GetEntity() &&
m_passBot->GetEntity()->GetTeamNumber() == entity->GetTeamNumber() )
return false;
INextBot *bot = entity->MyNextBotPointer();
return ( !bot || bot->IsAbleToBlockMovementOf( m_passBot ) );
}
return false;
}
const INextBot *m_passBot;
};
//--------------------------------------------------------------------------------------------
/**
* Trace filter that only hits players and NextBots
*/
class NextBotTraceFilterOnlyActors : public CTraceFilterSimple
{
public:
NextBotTraceFilterOnlyActors( const IHandleEntity *passentity, int collisionGroup )
: CTraceFilterSimple( passentity, collisionGroup )
{
}
virtual TraceType_t GetTraceType() const
{
return TRACE_ENTITIES_ONLY;
}
virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
{
CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
#ifdef TERROR
CBasePlayer *player = ToBasePlayer( entity );
if ( player && player->IsGhost() )
return false;
#endif // TERROR
return ( entity->MyNextBotPointer() || entity->IsPlayer() );
}
return false;
}
};
//--------------------------------------------------------------------------------------------
/**
* Trace filter that skips "traversable" entities. The "when" argument creates
* a temporal context for asking if an entity is IMMEDIATELY traversable (like thin
* glass that just breaks as we walk through it) or EVENTUALLY traversable (like a
* breakable object that will take some time to break through)
*/
class NextBotTraversableTraceFilter : public CTraceFilterSimple
{
public:
NextBotTraversableTraceFilter( INextBot *bot, ILocomotion::TraverseWhenType when = ILocomotion::EVENTUALLY ) : CTraceFilterSimple( bot->GetEntity(), COLLISION_GROUP_NONE )
{
m_bot = bot;
m_when = when;
}
virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
if ( m_bot->IsSelf( entity ) )
{
return false;
}
if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
{
return !m_bot->GetLocomotionInterface()->IsEntityTraversable( entity, m_when );
}
return false;
}
private:
INextBot *m_bot;
ILocomotion::TraverseWhenType m_when;
};
//---------------------------------------------------------------------------------------------
/**
* Given a vector of entities, a nav area, and a max travel distance, return
* the entity that has the shortest travel distance.
*/
inline CBaseEntity *SelectClosestEntityByTravelDistance( INextBot *me, const CUtlVector< CBaseEntity * > &candidateEntities, CNavArea *startArea, float travelRange )
{
// collect nearby walkable areas within travelRange
CUtlVector< CNavArea * > nearbyAreaVector;
CollectSurroundingAreas( &nearbyAreaVector, startArea, travelRange, me->GetLocomotionInterface()->GetStepHeight(), me->GetLocomotionInterface()->GetDeathDropHeight() );
// find closest entity in the collected area set
CBaseEntity *closeEntity = NULL;
float closeTravelRange = FLT_MAX;
for( int i=0; i<candidateEntities.Count(); ++i )
{
CBaseEntity *candidate = candidateEntities[i];
CNavArea *area = TheNavMesh->GetNearestNavArea( candidate, GETNAVAREA_CHECK_LOS, 500.0f );
if ( area && area->IsMarked() && area->GetCostSoFar() < closeTravelRange )
{
closeEntity = candidate;
closeTravelRange = area->GetCostSoFar();
}
}
return closeEntity;
}
#ifdef OBSOLETE
//--------------------------------------------------------------------------------------------
/**
* Trace filter that skips "traversable" entities, but hits other Actors.
* Used for obstacle avoidance.
*/
class NextBotMovementAvoidanceTraceFilter : public CTraceFilterSimple
{
public:
NextBotMovementAvoidanceTraceFilter( INextBot *bot ) : CTraceFilterSimple( bot->GetEntity(), COLLISION_GROUP_NONE )
{
m_bot = bot;
}
virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
{
CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
#ifdef TERROR
CBasePlayer *player = ToBasePlayer( entity );
if ( player && player->IsGhost() )
return false;
#endif // TERROR
if ( m_bot->IsSelf( entity ) )
{
return false;
}
if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
{
return !m_bot->GetLocomotionInterface()->IsEntityTraversable( entity, ILocomotion::IMMEDIATELY );
}
return false;
}
private:
INextBot *m_bot;
};
#endif
#endif // _NEXT_BOT_UTIL_H_