-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsimple_bot.h
116 lines (96 loc) · 2.98 KB
/
simple_bot.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
//========= Copyright Valve Corporation, All rights reserved. ============//
// simple_bot.h
// A mininal example of a NextBotCombatCharacter (ie: non-player) bot
// Michael Booth, February 2009
#ifndef SIMPLE_BOT_H
#define SIMPLE_BOT_H
#include "NextBot.h"
#include "NextBotBehavior.h"
#include "NextBotGroundLocomotion.h"
#include "Path/NextBotPathFollow.h"
//----------------------------------------------------------------------------
/**
* A Simple Bot
*/
class CSimpleBot : public NextBotCombatCharacter
{
public:
DECLARE_CLASS( CSimpleBot, NextBotCombatCharacter );
CSimpleBot();
virtual ~CSimpleBot();
virtual void Precache();
virtual void Spawn( void );
// INextBot
DECLARE_INTENTION_INTERFACE( CSimpleBot )
virtual NextBotGroundLocomotion *GetLocomotionInterface( void ) const { return m_locomotor; }
private:
NextBotGroundLocomotion *m_locomotor;
};
//--------------------------------------------------------------------------------------------------------------
/**
* Functor used with the A* algorithm of NavAreaBuildPath() to determine the "cost" of moving from one area to another.
* "Cost" is generally the weighted distance between the centers of the areas. If you want the bot
* to avoid an area/ladder/elevator, increase the cost. If you want to disallow an area/ladder/elevator, return -1.
*/
class CSimpleBotPathCost : public IPathCost
{
public:
CSimpleBotPathCost( CSimpleBot *me )
{
m_me = me;
}
// return the cost (weighted distance between) of moving from "fromArea" to "area", or -1 if the move is not allowed
virtual float operator()( CNavArea *area, CNavArea *fromArea, const CNavLadder *ladder, const CFuncElevator *elevator, float length ) const
{
if ( fromArea == NULL )
{
// first area in path, no cost
return 0.0f;
}
else
{
if ( !m_me->GetLocomotionInterface()->IsAreaTraversable( area ) )
{
// our locomotor says we can't move here
return -1.0f;
}
// compute distance traveled along path so far
float dist;
if ( ladder )
{
dist = ladder->m_length;
}
else if ( length > 0.0 )
{
// optimization to avoid recomputing length
dist = length;
}
else
{
dist = ( area->GetCenter() - fromArea->GetCenter() ).Length();
}
float cost = dist + fromArea->GetCostSoFar();
// check height change
float deltaZ = fromArea->ComputeAdjacentConnectionHeightChange( area );
if ( deltaZ >= m_me->GetLocomotionInterface()->GetStepHeight() )
{
if ( deltaZ >= m_me->GetLocomotionInterface()->GetMaxJumpHeight() )
{
// too high to reach
return -1.0f;
}
// jumping is slower than flat ground
const float jumpPenalty = 5.0f;
cost += jumpPenalty * dist;
}
else if ( deltaZ < -m_me->GetLocomotionInterface()->GetDeathDropHeight() )
{
// too far to drop
return -1.0f;
}
return cost;
}
}
CSimpleBot *m_me;
};
#endif // SIMPLE_BOT_H