diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /game/server/NextBot/Behavior | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'game/server/NextBot/Behavior')
| -rw-r--r-- | game/server/NextBot/Behavior/BehaviorBackUp.h | 118 | ||||
| -rw-r--r-- | game/server/NextBot/Behavior/BehaviorMoveTo.h | 126 |
2 files changed, 244 insertions, 0 deletions
diff --git a/game/server/NextBot/Behavior/BehaviorBackUp.h b/game/server/NextBot/Behavior/BehaviorBackUp.h new file mode 100644 index 0000000..e3a65f0 --- /dev/null +++ b/game/server/NextBot/Behavior/BehaviorBackUp.h @@ -0,0 +1,118 @@ +// BehaviorBackUp.h +// Back up for a short duration +// Author: Michael Booth, March 2007 +//========= Copyright Valve Corporation, All rights reserved. ============// + +#ifndef _BEHAVIOR_BACK_UP_H_ +#define _BEHAVIOR_BACK_UP_H_ + + +//---------------------------------------------------------------------------------------------- +/** + * Move backwards for a short duration away from a given position. + * Useful to dislodge ourselves if we get stuck while following our path. + */ +template < typename Actor > +class BehaviorBackUp : public Action< Actor > +{ +public: + BehaviorBackUp( const Vector &avoidPos ); + + virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction ); + virtual ActionResult< Actor > Update( Actor *me, float interval ); + + virtual EventDesiredResult< Actor > OnStuck( Actor *me ); + + virtual const char *GetName( void ) const { return "BehaviorBackUp"; } + +private: + CountdownTimer m_giveUpTimer; + CountdownTimer m_backupTimer; + CountdownTimer m_jumpTimer; + Vector m_way; + Vector m_avoidPos; +}; + + +//---------------------------------------------------------------------------------------------- +template < typename Actor > +inline BehaviorBackUp< Actor >::BehaviorBackUp( const Vector &avoidPos ) +{ + m_avoidPos = avoidPos; +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor > +inline ActionResult< Actor > BehaviorBackUp< Actor >::OnStart( Actor *me, Action< Actor > *priorAction ) +{ + ILocomotion *mover = me->GetLocomotionInterface(); + + // don't back off if we're on a ladder + if ( mover && mover->IsUsingLadder() ) + { + return Done(); + } + + float backupTime = RandomFloat( 0.3f, 0.5f ); + + m_backupTimer.Start( backupTime ); + m_jumpTimer.Start( 1.5f * backupTime ); + m_giveUpTimer.Start( 2.5f * backupTime ); + + m_way = me->GetPosition() - m_avoidPos; + m_way.NormalizeInPlace(); + + return Continue(); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor > +inline ActionResult< Actor > BehaviorBackUp< Actor >::Update( Actor *me, float interval ) +{ + if ( m_giveUpTimer.IsElapsed() ) + { + return Done(); + } + +// if ( m_jumpTimer.HasStarted() && m_jumpTimer.IsElapsed() ) +// { +// me->GetLocomotionInterface()->Jump(); +// m_jumpTimer.Invalidate(); +// } + + ILocomotion *mover = me->GetLocomotionInterface(); + if ( mover ) + { + Vector goal; + + if ( m_backupTimer.IsElapsed() ) + { + // move towards bad spot + goal = m_avoidPos; // me->GetPosition() - 100.0f * m_way; + } + else + { + // move away from bad spot + goal = me->GetPosition() + 100.0f * m_way; + } + + mover->Approach( goal ); + } + + return Continue(); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor > +inline EventDesiredResult< Actor > BehaviorBackUp< Actor >::OnStuck( Actor *me ) +{ + return TryToSustain( RESULT_IMPORTANT, "Stuck while trying to back up" ); +} + + + +#endif // _BEHAVIOR_BACK_UP_H_ + diff --git a/game/server/NextBot/Behavior/BehaviorMoveTo.h b/game/server/NextBot/Behavior/BehaviorMoveTo.h new file mode 100644 index 0000000..f3b608f --- /dev/null +++ b/game/server/NextBot/Behavior/BehaviorMoveTo.h @@ -0,0 +1,126 @@ +// BehaviorMoveTo.h +// Move to a potentially far away position +// Author: Michael Booth, June 2007 +//========= Copyright Valve Corporation, All rights reserved. ============// + +#ifndef _BEHAVIOR_MOVE_TO_H_ +#define _BEHAVIOR_MOVE_TO_H_ + + +//---------------------------------------------------------------------------------------------- +/** + * Move to a potentially far away position, using path planning. + */ +template < typename Actor, typename PathCost > +class BehaviorMoveTo : public Action< Actor > +{ +public: + BehaviorMoveTo( const Vector &goal, Action< Actor > *successAction = NULL, Action< Actor > *failAction = NULL ); + + virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction ); + virtual ActionResult< Actor > Update( Actor *me, float interval ); + + virtual EventDesiredResult< Actor > OnMoveToSuccess( Actor *me, const Path *path ); + virtual EventDesiredResult< Actor > OnMoveToFailure( Actor *me, const Path *path, MoveToFailureType reason ); + + virtual bool ComputePath( Actor *me, const Vector &goal, PathFollower *path ); + + virtual const char *GetName( void ) const { return "BehaviorMoveTo"; } + +private: + Vector m_goal; + PathFollower m_path; + Action< Actor > *m_successAction; + Action< Actor > *m_failAction; +}; + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline BehaviorMoveTo< Actor, PathCost >::BehaviorMoveTo( const Vector &goal, Action< Actor > *successAction, Action< Actor > *failAction ) +{ + m_goal = goal; + m_path.Invalidate(); + m_successAction = successAction; + m_failAction = failAction; +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline bool BehaviorMoveTo< Actor, PathCost >::ComputePath( Actor *me, const Vector &goal, PathFollower *path ) +{ + PathCost cost( me ); + return path->Compute( me, goal, cost ); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnStart( Actor *me, Action< Actor > *priorAction ) +{ + if ( !this->ComputePath( me, m_goal, &m_path ) ) + { + if ( m_failAction ) + { + return this->ChangeTo( m_failAction, "No path to goal" ); + } + + return this->Done( "No path to goal" ); + } + + return this->Continue(); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline ActionResult< Actor > BehaviorMoveTo< Actor, PathCost >::Update( Actor *me, float interval ) +{ + // if path became invalid during last tick for any reason, we're done + if ( !m_path.IsValid() ) + { + if ( m_failAction ) + { + return this->ChangeTo( m_failAction, "Path is invalid" ); + } + + return this->Done( "Path is invalid" ); + } + + // move along path - success/fail event handlers will exit behavior when goal is reached + m_path.Update( me ); + + return this->Continue(); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline EventDesiredResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnMoveToSuccess( Actor *me, const Path *path ) +{ + if ( m_successAction ) + { + return this->TryChangeTo( m_successAction, RESULT_CRITICAL, "OnMoveToSuccess" ); + } + + return this->TryDone( RESULT_CRITICAL, "OnMoveToSuccess" ); +} + + +//---------------------------------------------------------------------------------------------- +template < typename Actor, typename PathCost > +inline EventDesiredResult< Actor > BehaviorMoveTo< Actor, PathCost >::OnMoveToFailure( Actor *me, const Path *path, MoveToFailureType reason ) +{ + if ( m_failAction ) + { + return this->TryChangeTo( m_failAction, RESULT_CRITICAL, "OnMoveToFailure" ); + } + + return this->TryDone( RESULT_CRITICAL, "OnMoveToFailure" ); +} + + + +#endif // _BEHAVIOR_MOVE_TO_H_ + |