Skip to content

Commit

Permalink
Bots don't teleport around in the map anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Aug 10, 2019
1 parent 06c4fa4 commit d9fc7d0
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 36 deletions.
42 changes: 27 additions & 15 deletions Pongbot/Bot/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "Brain/BotBrainMed.h"
#include "Brain/BotBrainSniper.h"
#include "Brain/BotBrainSpy.h"
#include "../TF2/Class/TFClassInfoProvider.h"
#include "../ConVarHolder.h"
#include <metamod/ISmmAPI.h>
#include <hlsdk/public/game/server/iplayerinfo.h>
Expand All @@ -28,7 +27,8 @@ Bot::Bot(Player player, const char* name) : Name(name), _Player(player), _Edict(
_IBotController(IIBotManager->GetBotController(_Edict)),
_ClassInfo(_TFClassInfoProvider->GetClassInfo(TFClass::CLASS_UNK)),
_IPlayerInfo(IIPlayerInfoManager->GetPlayerInfo(_Edict)), _BotVisibles(new BotVisibles(this)),
_BotBrain(nullptr)
_BotBrain(nullptr), _Movement(Vector2D()), _TargetViewAngle(QAngle()), _PressedButtons(0),
_SelectedWeaponSlot(WEAPON_UNKNOWN)
{
_SwitchToFittingTeam();
_RandomClass();
Expand Down Expand Up @@ -68,9 +68,15 @@ void Bot::Think()

CBotCmd cmd;
cmd.buttons = _PressedButtons;
cmd.forwardmove = _Movement.x;
cmd.sidemove = _Movement.y;
cmd.viewangles = finalViewAngle;
if (_Movement.IsValid())
{
cmd.forwardmove = _Movement.x;
cmd.sidemove = _Movement.y;
}
if (finalViewAngle.IsValid())
{
cmd.viewangles = finalViewAngle;
}
_IBotController->RunPlayerMove(&cmd);
}

Expand Down Expand Up @@ -106,7 +112,10 @@ QAngle Bot::GetViewAngle() const

void Bot::SetViewAngle(QAngle angle)
{
_TargetViewAngle = angle;
if (angle.IsValid())
{
_TargetViewAngle = angle;
}
}

TFClass Bot::GetClass() const
Expand All @@ -126,7 +135,10 @@ BotVisibles* Bot::GetBotVisibles() const

void Bot::SetMovement(Vector2D movement)
{
_Movement = movement;
if (movement.IsValid())
{
_Movement = movement;
}
}

Vector2D Bot::GetMovement() const
Expand All @@ -151,7 +163,7 @@ WeaponSlot Bot::GetSelectedWeaponSlot() const

void Bot::SetSelectedWeapon(WeaponSlot weapon)
{
if (!_ClassInfo->IsValid())
if (!_ClassInfo.IsValid())
{
return;
}
Expand All @@ -160,13 +172,13 @@ void Bot::SetSelectedWeapon(WeaponSlot weapon)
switch (weapon)
{
case WEAPON_PRIMARY:
weaponName = _ClassInfo->Primary.WeaponName;
weaponName = _ClassInfo.GetPrimary().GetWeaponName();
break;
case WEAPON_SECONDARY:
weaponName = _ClassInfo->Secondary.WeaponName;
weaponName = _ClassInfo.GetSecondary().GetWeaponName();
break;
case WEAPON_MELEE:
weaponName = _ClassInfo->Melee.WeaponName;
weaponName = _ClassInfo.GetMelee().GetWeaponName();
break;
}

Expand Down Expand Up @@ -208,16 +220,16 @@ WeaponSlot Bot::GetIdealWeaponForRange(float range) const
// TODO: Take ammo into account
TFClassInfoWeapon weaponInfos[] =
{
_ClassInfo->Primary,
_ClassInfo->Secondary,
_ClassInfo->Melee
_ClassInfo.GetPrimary(),
_ClassInfo.GetSecondary(),
_ClassInfo.GetMelee()
};
WeaponSlot longRangeWeaponSlot = WEAPON_PRIMARY;
WeaponSlot middleRangeWeaponSlot = WEAPON_SECONDARY;
WeaponSlot shortRangeWeaponSlot = WEAPON_MELEE;
for (int i = 0; i < 3; i++)
{
unsigned int weaponFlags = weaponInfos[i].WeaponFlags;
unsigned int weaponFlags = weaponInfos[i].GetWeaponFlags();
if (weaponFlags & WEAPONFLAG_PRIORITIZE_LONGDIST)
{
longRangeWeaponSlot = (WeaponSlot)i;
Expand Down
4 changes: 2 additions & 2 deletions Pongbot/Bot/Bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "../TF2/WeaponSlot.h"
#include "../TF2/TFTeam.h"
#include "../TF2/Entity/Player.h"
#include "../TF2/Class/TFClassInfoProvider.h"
#include <hlsdk/public/mathlib/mathlib.h>

class BotBrain;
class BotVisibles;
struct TFClassInfo;
class IBotController;
class IPlayerInfo;
struct edict_t;
Expand Down Expand Up @@ -54,7 +54,7 @@ class Bot
QAngle _TargetViewAngle;
Vector2D _Movement;
int _PressedButtons;
const TFClassInfo* _ClassInfo;
TFClassInfo _ClassInfo;
WeaponSlot _SelectedWeaponSlot;

void _SwitchToFittingTeam();
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 @@ -100,7 +100,7 @@ void BotBrain::_DefaultThink()
else if (CTFFlag(closestObjective.Edict).GetOwner() == bot->GetEdict()->m_iIndex)
{
// I'm carrying the flag
WaypointNode* targetNode = _WaypointManager->GetClosestWaypointNode(botPos, -1, bot->GetTeam() == TFTeam::TEAM_RED ? WaypointNodeFlagType::NODE_ITEMFLAG_RED : WaypointNodeFlagType::NODE_ITEMFLAG_BLUE);
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
Expand Down
2 changes: 1 addition & 1 deletion Pongbot/Bot/Brain/Tasks/BotTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool BotTask::OnThink()
}

// Avoid jittering around when supposed to stand still
if (_BotTargetPos.IsZero() || Util::DistanceToNoZ(_Bot->GetPos(), _BotTargetPos) < _ConVarHolder->CVarBotMovementIgnoreRadius->GetFloat())
if (!_BotTargetPos.IsValid() || _BotTargetPos.IsZero() || Util::DistanceToNoZ(_Bot->GetPos(), _BotTargetPos) < _ConVarHolder->CVarBotMovementIgnoreRadius->GetFloat())
{
_Bot->SetMovement(Vector2D());
}
Expand Down
4 changes: 2 additions & 2 deletions Pongbot/TF2/Class/TFClassInfoProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void TFClassInfoProvider::Destroy()
}
}

const TFClassInfo* TFClassInfoProvider::GetClassInfo(TFClass tfClass) const
TFClassInfo TFClassInfoProvider::GetClassInfo(TFClass tfClass) const
{
return &_ClassInfos.at(tfClass);
return _ClassInfos.at(tfClass);
}
68 changes: 55 additions & 13 deletions Pongbot/TF2/Class/TFClassInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,81 @@ enum TFClassInfoWeaponFlags

struct TFClassInfoWeapon
{
TFClassInfoWeapon() : WeaponName("UNK"), WeaponFlags(0), _Valid(false) {}
TFClassInfoWeapon(const char* weaponName, unsigned int weaponFlags = 0) : WeaponName(weaponName), WeaponFlags(weaponFlags), _Valid(true) {}
TFClassInfoWeapon() : _WeaponName("UNK"), _WeaponFlags(0), _Valid(false) {}
TFClassInfoWeapon(const char* weaponName, unsigned int weaponFlags = 0) : _WeaponName(weaponName), _WeaponFlags(weaponFlags), _Valid(true) {}
int operator=(const TFClassInfoWeapon& target)
{
_WeaponName = target.GetWeaponName();
_WeaponFlags = target.GetWeaponFlags();
_Valid = target.IsValid();
}

const char* WeaponName;
const unsigned int WeaponFlags;
const char* GetWeaponName() const
{
return _WeaponName;
}

unsigned int GetWeaponFlags() const
{
return _WeaponFlags;
}

bool IsValid() const
{
return _Valid;
}

private:
const char* _WeaponName;
unsigned int _WeaponFlags;
bool _Valid;
};

struct TFClassInfo
{
TFClassInfo() : Speed(0.f), _Valid(false) {}
TFClassInfo() : _Speed(0.f), _Valid(false) {}
TFClassInfo(float speed, TFClassInfoWeapon primary, TFClassInfoWeapon secondary,
TFClassInfoWeapon melee) : Speed(speed), Primary(primary), Secondary(secondary), Melee(melee), _Valid(true) {}
TFClassInfoWeapon melee) : _Speed(speed), _Primary(primary), _Secondary(secondary), _Melee(melee), _Valid(true) {}
int operator=(const TFClassInfo& target)
{
_Speed = target.GetSpeed();
_Primary = target.GetPrimary();
_Secondary = target.GetSecondary();
_Melee = target.GetMelee();
_Valid = target.IsValid();
}

const float Speed;
// TODO: Support for additional weapon types (for example buff banner)
const TFClassInfoWeapon Primary;
const TFClassInfoWeapon Secondary;
const TFClassInfoWeapon Melee;
float GetSpeed() const
{
return _Speed;
}

TFClassInfoWeapon GetPrimary() const
{
return _Primary;
}

TFClassInfoWeapon GetSecondary() const
{
return _Secondary;
}

TFClassInfoWeapon GetMelee() const
{
return _Melee;
}

bool IsValid() const
{
return _Valid && Primary.IsValid() && Secondary.IsValid() && Melee.IsValid();
return _Valid && _Primary.IsValid() && _Secondary.IsValid() && _Melee.IsValid();
}

private:
float _Speed;
// TODO: Support for additional weapon types (for example buff banner)
TFClassInfoWeapon _Primary;
TFClassInfoWeapon _Secondary;
TFClassInfoWeapon _Melee;
bool _Valid;
};

Expand All @@ -57,7 +99,7 @@ class TFClassInfoProvider
static void Init();
static void Destroy();

const TFClassInfo* GetClassInfo(TFClass tfClass) const;
TFClassInfo GetClassInfo(TFClass tfClass) const;

private:
const std::map<TFClass, TFClassInfo> _ClassInfos =
Expand Down
2 changes: 1 addition & 1 deletion Pongbot/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace Util
{
Vector2D sins;
SinCos(DEG2RAD(bot->GetViewAngle().y - GetLookAtAngleForPos(bot, targetPos).y), &sins.y, &sins.x);
sins = sins / sins.Length() * _TFClassInfoProvider->GetClassInfo(bot->GetClass())->Speed * 3.f;
sins = sins / sins.Length() * _TFClassInfoProvider->GetClassInfo(bot->GetClass()).GetSpeed() * 3.f;

return sins;
}
Expand Down
8 changes: 7 additions & 1 deletion Pongbot/stdafx.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#pragma once
#include "TF2/Trace/TraceHeaders.h"
#include <metamod/ISmmPlugin.h>
#include <metamod/ISmmAPI.h>
#include <metamod/sourcehook/sourcehook.h>
#include <hlsdk/public/shake.h>
#include <hlsdk/game/shared/IEffects.h>
#include <hlsdk/public/eiface.h>
#include <hlsdk/public/game/server/iplayerinfo.h>
#include <hlsdk/public/mathlib/mathlib.h>
#include <hlsdk/public/edict.h>
#include <hlsdk/public/tier1/convar.h>
#include <hlsdk/public/tier1/convar.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>

0 comments on commit d9fc7d0

Please sign in to comment.