From d9fc7d0499833eaf92e7e82d5c159ea6cd106b9c Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Sat, 10 Aug 2019 20:24:40 +0200 Subject: [PATCH] Bots don't teleport around in the map anymore --- Pongbot/Bot/Bot.cpp | 42 +++++++++----- Pongbot/Bot/Bot.h | 4 +- Pongbot/Bot/Brain/BotBrain.cpp | 2 +- Pongbot/Bot/Brain/Tasks/BotTask.cpp | 2 +- Pongbot/TF2/Class/TFClassInfoProvider.cpp | 4 +- Pongbot/TF2/Class/TFClassInfoProvider.h | 68 ++++++++++++++++++----- Pongbot/Util.cpp | 2 +- Pongbot/stdafx.h | 8 ++- 8 files changed, 96 insertions(+), 36 deletions(-) diff --git a/Pongbot/Bot/Bot.cpp b/Pongbot/Bot/Bot.cpp index 39bf480..4accd02 100644 --- a/Pongbot/Bot/Bot.cpp +++ b/Pongbot/Bot/Bot.cpp @@ -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 #include @@ -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(); @@ -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); } @@ -106,7 +112,10 @@ QAngle Bot::GetViewAngle() const void Bot::SetViewAngle(QAngle angle) { - _TargetViewAngle = angle; + if (angle.IsValid()) + { + _TargetViewAngle = angle; + } } TFClass Bot::GetClass() const @@ -126,7 +135,10 @@ BotVisibles* Bot::GetBotVisibles() const void Bot::SetMovement(Vector2D movement) { - _Movement = movement; + if (movement.IsValid()) + { + _Movement = movement; + } } Vector2D Bot::GetMovement() const @@ -151,7 +163,7 @@ WeaponSlot Bot::GetSelectedWeaponSlot() const void Bot::SetSelectedWeapon(WeaponSlot weapon) { - if (!_ClassInfo->IsValid()) + if (!_ClassInfo.IsValid()) { return; } @@ -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; } @@ -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; diff --git a/Pongbot/Bot/Bot.h b/Pongbot/Bot/Bot.h index 43f733a..4a4b42d 100644 --- a/Pongbot/Bot/Bot.h +++ b/Pongbot/Bot/Bot.h @@ -3,11 +3,11 @@ #include "../TF2/WeaponSlot.h" #include "../TF2/TFTeam.h" #include "../TF2/Entity/Player.h" +#include "../TF2/Class/TFClassInfoProvider.h" #include class BotBrain; class BotVisibles; -struct TFClassInfo; class IBotController; class IPlayerInfo; struct edict_t; @@ -54,7 +54,7 @@ class Bot QAngle _TargetViewAngle; Vector2D _Movement; int _PressedButtons; - const TFClassInfo* _ClassInfo; + TFClassInfo _ClassInfo; WeaponSlot _SelectedWeaponSlot; void _SwitchToFittingTeam(); diff --git a/Pongbot/Bot/Brain/BotBrain.cpp b/Pongbot/Bot/Brain/BotBrain.cpp index b80de45..0b4d332 100644 --- a/Pongbot/Bot/Brain/BotBrain.cpp +++ b/Pongbot/Bot/Brain/BotBrain.cpp @@ -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 diff --git a/Pongbot/Bot/Brain/Tasks/BotTask.cpp b/Pongbot/Bot/Brain/Tasks/BotTask.cpp index 7ecce63..c77c42e 100644 --- a/Pongbot/Bot/Brain/Tasks/BotTask.cpp +++ b/Pongbot/Bot/Brain/Tasks/BotTask.cpp @@ -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()); } diff --git a/Pongbot/TF2/Class/TFClassInfoProvider.cpp b/Pongbot/TF2/Class/TFClassInfoProvider.cpp index 1e666f6..039af61 100644 --- a/Pongbot/TF2/Class/TFClassInfoProvider.cpp +++ b/Pongbot/TF2/Class/TFClassInfoProvider.cpp @@ -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); } \ No newline at end of file diff --git a/Pongbot/TF2/Class/TFClassInfoProvider.h b/Pongbot/TF2/Class/TFClassInfoProvider.h index 2b8ea28..fe75a1f 100644 --- a/Pongbot/TF2/Class/TFClassInfoProvider.h +++ b/Pongbot/TF2/Class/TFClassInfoProvider.h @@ -12,11 +12,24 @@ 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 { @@ -24,27 +37,56 @@ struct TFClassInfoWeapon } 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; }; @@ -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 _ClassInfos = diff --git a/Pongbot/Util.cpp b/Pongbot/Util.cpp index 185cae2..49a2d02 100644 --- a/Pongbot/Util.cpp +++ b/Pongbot/Util.cpp @@ -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; } diff --git a/Pongbot/stdafx.h b/Pongbot/stdafx.h index e40d7eb..4f76779 100644 --- a/Pongbot/stdafx.h +++ b/Pongbot/stdafx.h @@ -1,5 +1,7 @@ #pragma once +#include "TF2/Trace/TraceHeaders.h" #include +#include #include #include #include @@ -7,4 +9,8 @@ #include #include #include -#include \ No newline at end of file +#include +#include +#include +#include +#include \ No newline at end of file