Skip to content

Commit

Permalink
Add switch to shortest way parameter to BotTaskGoto
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Aug 13, 2019
1 parent 265067e commit fb767a7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
10 changes: 8 additions & 2 deletions Pongbot/Bot/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void Bot::ExecClientCommand(const char* command, ...) const
vsnprintf(fullCommand, sizeof(fullCommand), command, args);
va_end(args);

IIServerPluginHelpers->ClientCommand(GetEdict(), fullCommand);
IIServerPluginHelpers->ClientCommand(_Edict, fullCommand);
}

WeaponSlot Bot::GetIdealWeaponForRange(float range) const
Expand Down Expand Up @@ -254,6 +254,11 @@ WeaponSlot Bot::GetIdealWeaponForRange(float range) const
}
}

IServerEntity* Bot::GetIServerEntity() const
{
return _Edict->GetIServerEntity();
}

void Bot::_SwitchToFittingTeam()
{
uint8_t red = 0;
Expand Down Expand Up @@ -308,7 +313,8 @@ void Bot::_UpdateBotBrain()
break;
}

Util::DebugLog("Created new bot brain (%s) for bot %s (Edict Index: %d)", _TFClassToJoinName(GetClass()), Name, GetEdict()->m_iIndex);
Util::DebugLog("Created new bot brain (%s) for bot %s (Edict Index: %d)", _TFClassToJoinName(GetClass()),
Name, _Edict->m_iIndex);
}

const char* Bot::_TFClassToJoinName(TFClass tfClass) const
Expand Down
1 change: 1 addition & 0 deletions Pongbot/Bot/Bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Bot
void ChangeClass(TFClass tfClass);
void ExecClientCommand(const char* command, ...) const;
WeaponSlot GetIdealWeaponForRange(float range) const;
IServerEntity* GetIServerEntity() const;

private:
const Player _Player;
Expand Down
2 changes: 1 addition & 1 deletion Pongbot/Bot/Brain/BotBrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void BotBrain::_DefaultThink()
WaypointNode* targetNode = _WaypointManager->GetClosestWaypointNode(botPos, -1, bot->GetTeam() == TEAM_RED ? NODE_ITEMFLAG_RED : NODE_ITEMFLAG_BLUE);
if (targetNode) // Map doesn't have a ITEMFLAG_RED/ITEMFLAG_BLUE node!
{
_SetBotTask(new BotTaskGoto(bot, targetNode->Pos, true, NODE_SPAWN_RED | NODE_SPAWN_BLUE)); // Don't walk through spawns
_SetBotTask(new BotTaskGoto(bot, targetNode->Pos, true, true, NODE_SPAWN_RED | NODE_SPAWN_BLUE)); // Don't walk through spawns
}
}
}
Expand Down
29 changes: 20 additions & 9 deletions Pongbot/Bot/Brain/Tasks/BotTaskGoto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../../../TF2/Entity/EntityProvider.h"
#include "../../../TF2/Entity/EntityDataProvider.h"
#include "../../../TF2/TFTeam.h"
#include "../../../TF2/Trace/TraceFilters.h"
#include "../../../Waypoint/WaypointNodeFlagTypes.h"
#include "../../../Util.h"
#include "../../../Waypoint/WaypointNodeFlagsProvider.h"
Expand All @@ -17,7 +18,7 @@ extern IVEngineServer* Engine;

static bool _DrawDebugBeam = false;

bool BotTaskGoto::_NewTargetNodeStack()
void BotTaskGoto::_NewTargetNodeStack()
{
Bot* bot = _GetBot();
Vector currentPos = bot->GetPos();
Expand Down Expand Up @@ -47,7 +48,8 @@ bool BotTaskGoto::_NewTargetNodeStack()
if (_WaypointNodeStack.empty())
{
// Can't even get there, abort
return false;
_AbortTask = true;
return;
}

_TargetPosQueue = std::queue<Vector>();
Expand All @@ -61,8 +63,6 @@ bool BotTaskGoto::_NewTargetNodeStack()
}
_TargetPosQueue.push(_TargetPos);
}

return true;
}

bool BotTaskGoto::_OnThink()
Expand Down Expand Up @@ -93,6 +93,21 @@ bool BotTaskGoto::_OnThink()
Util::DrawBeam(bot->GetEarPos(), targetPos, 255, 0, 0, debugBeamTick);
_DebugBeamDrawTime = Engine->Time() + debugBeamTick;
}

// Switch to shortest way mode if target pos is visible and switchToShortestWayOnTargetPosVisible is on
if (_SwitchToShortestWayOnTargetPosVisible && !_ShortestWay)
{
trace_t traceResult;
Vector tracePos = targetPos;
tracePos.z += bot->GetEarPos().z;
Util::TraceLine(currentPos, tracePos, MASK_SOLID | MASK_OPAQUE,
&TraceFilterSimple(bot->GetIServerEntity(), nullptr), &traceResult);
if (!traceResult.DidHit())
{
_ShortestWay = true;
_NewTargetNodeStack();
}
}
}

return false;
Expand All @@ -110,11 +125,7 @@ void BotTaskGoto::_OnBotStuckPanic()

void BotTaskGoto::_OnBotDefinitelyStuck()
{
if (!_ShortestWay && !_NewTargetNodeStack())
{
// Can't get there
_AbortTask = true;
}
_NewTargetNodeStack();
}

CON_COMMAND(pongbot_bot_goto_debug, "Draw a beam to the bots' target pos")
Expand Down
14 changes: 8 additions & 6 deletions Pongbot/Bot/Brain/Tasks/BotTaskGoto.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ class Bot;
class BotTaskGoto : public BotTask
{
public:
BotTaskGoto(Bot* bot, Vector targetPos, bool shortestWay = true, int nodeFlagBlacklist = 0) : BotTask(bot),
_TargetPos(targetPos), _ShortestWay(shortestWay), _NodeFlagBlacklist(nodeFlagBlacklist),
_DebugBeamDrawTime(0.f)
BotTaskGoto(Bot* bot, Vector targetPos, bool shortestWay = true, bool switchToShortestWayOnTargetPosVisible = true,
int nodeFlagBlacklist = 0) : BotTask(bot), _TargetPos(targetPos), _ShortestWay(shortestWay),
_SwitchToShortestWayOnTargetPosVisible(switchToShortestWayOnTargetPosVisible),
_NodeFlagBlacklist(nodeFlagBlacklist), _DebugBeamDrawTime(0.f)
{
_AbortTask = !_NewTargetNodeStack();
_NewTargetNodeStack();
}

private:
std::queue<Vector> _TargetPosQueue;
const Vector _TargetPos;
const bool _ShortestWay;
bool _ShortestWay;
const bool _SwitchToShortestWayOnTargetPosVisible;
const int _NodeFlagBlacklist;
float _DebugBeamDrawTime;

bool _NewTargetNodeStack();
void _NewTargetNodeStack();

virtual bool _OnThink();
virtual void _OnBotStuckPanic();
Expand Down
4 changes: 2 additions & 2 deletions Pongbot/Bot/Visibles/BotVisibles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void BotVisibles::OnThink()
entityPos += Vector(0.f, 0.f, (Player(entity).GetHeadPos().z - entityPos.z) / 2.f);
}

bool clearLine = _HasClearLineToTarget(entity.GetEdict()->GetIServerEntity(), entityPos);
bool clearLine = _HasClearLineToTarget(entity.GetIServerEntity(), entityPos);
if (clearLine)
{
// Insert according to distance bot <-> edict
Expand Down Expand Up @@ -153,7 +153,7 @@ bool BotVisibles::_HasClearLineToTarget(IServerEntity* targetEntity, Vector targ
{
trace_t traceResult;
Util::TraceLine(_MBot->GetEarPos(), targetPos, MASK_SOLID | MASK_BLOCKLOS,
&TraceFilterSimple(_MBot->GetEdict()->GetIServerEntity(), targetEntity), &traceResult);
&TraceFilterSimple(_MBot->GetIServerEntity(), targetEntity), &traceResult);
return !traceResult.DidHit();
}

Expand Down
11 changes: 8 additions & 3 deletions Pongbot/TF2/Entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Vector Entity::GetPos() const
return Vector(0.f, 0.f, 0.f);
}

ICollideable* collideable = GetEdict()->GetCollideable();
ICollideable* collideable = _MEdict->GetCollideable();
return collideable ? collideable->GetCollisionOrigin() : Vector(0.f, 0.f, 0.f);
}

Expand All @@ -48,7 +48,7 @@ bool Entity::IsPlayer() const

bool Entity::Exists() const
{
return GetEdict();
return _MEdict;
}

const char* Entity::GetEdictClassName() const
Expand All @@ -58,5 +58,10 @@ const char* Entity::GetEdictClassName() const
return nullptr;
}

return GetEdict()->GetClassName();
return _MEdict->GetClassName();
}

IServerEntity* Entity::GetIServerEntity() const
{
return _MEdict->GetIServerEntity();
}
1 change: 1 addition & 0 deletions Pongbot/TF2/Entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Entity
bool IsPlayer() const;
bool Exists() const;
const char* GetEdictClassName() const;
IServerEntity* GetIServerEntity() const;

private:
edict_t* _MEdict;
Expand Down

0 comments on commit fb767a7

Please sign in to comment.