Skip to content

Commit

Permalink
Upgrade Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
caxanga334 committed Apr 27, 2024
1 parent 27376a8 commit 2012b32
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 32 deletions.
36 changes: 28 additions & 8 deletions configs/tf/mvm_upgrades.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ TFMvMUpgrades
* in case two or more upgrades have the same priority, they will be selected randomly
* priority must start at 1, bot thinks they finished their upgrades when no upgrades are found for a given priority
* "priority" "1"
* "slot" "0" -- weapon slot used by this upgrade, -1 for player upgrades, 9 for canteen power up bottle
* "slot" "0" -- weapon slot used by this upgrade,
* -1 for player upgrades, 9 for canteen power up bottle
* 0 for primary, 1 for secondary, 2 for melee
* "weapons" "5,7,333" -- comma-delimited list of item definition index this upgrade is restricted to
* "maxlevel" "0" -- limit that max upgrade level, 0 (default) to use the upgrade max cap
* }
Expand All @@ -32,44 +34,62 @@ TFMvMUpgrades
"upgrade"
{
"attribute" "dmg taken from blast reduced"
"attribute" "applies snare effect"
"priority" "1"
"slot" "1"
/* max level here should be optional, added for safety */
"maxlevel" "1"
/* Mad Milk, Mutated Milk */
"weapons" "222,1121"
}
"upgrade"
{
"attribute" "effect bar recharge rate increased"
"priority" "2"
"slot" "1"
/* Mad Milk, Mutated Milk */
"weapons" "222,1121"
}
"upgrade"
{
"attribute" "dmg taken from blast reduced"
"priority" "3"
"slot" "-1"
}
"upgrade"
{
"attribute" "dmg taken from bullets reduced"
"priority" "1"
"priority" "3"
"slot" "-1"
}
"upgrade"
{
"attribute" "dmg taken from crit reduced"
"priority" "1"
"priority" "3"
"slot" "-1"
}
"upgrade"
{
"attribute" "move speed bonus"
"priority" "1"
"priority" "3"
"slot" "-1"
}
"upgrade"
{
"attribute" "dmg taken from fire reduced"
"priority" "2"
"priority" "4"
"slot" "-1"
}
"upgrade"
{
"attribute" "increased jump height"
"priority" "2"
"priority" "4"
"slot" "-1"
}
"upgrade"
{
"attribute" "health regen"
"priority" "3"
"priority" "5"
"slot" "-1"
}
}
Expand Down
4 changes: 2 additions & 2 deletions extension/bot/tf2/tasks/scenario/mvm/tf2bot_mvm_upgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ TaskResult<CTF2Bot> CTF2BotMvMUpgradeTask::OnTaskUpdate(CTF2Bot* bot)
{
if (!m_buydelay.HasStarted()) // Bot reached the upgrade station, start the timer
{
m_buydelay.Start(randomgen->GetRandomReal<float>(2.0f, 5.0f));
m_buydelay.Start(randomgen->GetRandomReal<float>(1.0f, 2.0f));
}
else
{
if (m_buydelay.IsElapsed())
{
manager.Update(); // bot is inside an upgrade zone, buy upgrades
m_buydelay.Start(randomgen->GetRandomReal<float>(2.0f, 5.0f));
m_buydelay.Start(randomgen->GetRandomReal<float>(1.0f, 2.0f));
}
}
}
Expand Down
120 changes: 104 additions & 16 deletions extension/bot/tf2/tf2bot_upgrades.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "tf2bot.h"
#include "tf2bot_upgrades.h"

static ConVar sm_navbot_tf_debug_bot_upgrades("sm_navbot_tf_debug_bot_upgrades", "0", FCVAR_CHEAT, "Enables debugging bot upgrades.");

CTF2BotUpgradeManager::CTF2BotUpgradeManager()
{
m_me = nullptr;
Expand All @@ -24,29 +26,68 @@ CTF2BotUpgradeManager::~CTF2BotUpgradeManager()

void CTF2BotUpgradeManager::Update()
{
if (!CanAffordAnyUpgrade() && m_state != STATE_DOREFUND)
{
OnDoneUpgrading();
return;
}

switch (m_state)
{
case CTF2BotUpgradeManager::STATE_GOBUY:
m_state = STATE_BUYINGSTUFF;
ExecuteBuy();
RemoveFinishedUpgrades(); // clean up

// If buy list is empty, advance into the next priority
if (m_tobuylist.empty())
{
if (!AnyUpgradeAvailable())
{
OnDoneUpgrading();
return;
}

if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
ConColorMsg(Color(0, 128, 0, 255), "%s: Buy list is empty!\n", m_me->GetDebugIdentifier());
ConColorMsg(Color(0, 128, 128, 255), "Advancing from priority %i to %i!\n", m_nextpriority, (m_nextpriority + 1));
}

m_state = STATE_ADVANCE;
return;
}

// I have stuff to buy but I can't afford anything
if (!CanAffordAnyUpgrade())
{
OnDoneUpgrading();
return;
}

m_state = STATE_BUYINGSTUFF; // enter buy state
return;
case CTF2BotUpgradeManager::STATE_BUYINGSTUFF:
ExecuteBuy();
RemoveFinishedUpgrades(); // clean up

if (!CanAffordAnyUpgrade())
{
OnDoneUpgrading();
return;
}
return;
case CTF2BotUpgradeManager::STATE_ADVANCE:
AdvancePriority();
FetchUpgrades();
m_state = STATE_GOBUY;
return;
case CTF2BotUpgradeManager::STATE_WAITFORNEXTWAVE:
return;
case CTF2BotUpgradeManager::STATE_DOREFUND:
ExecuteRefund();
OnUpgradesRefunded();
return;
case CTF2BotUpgradeManager::STATE_REFILTER:
if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
ConColorMsg(Color(0, 200, 200, 255), "%s: Running Upgrade Filter!\n", m_me->GetDebugIdentifier());
}

FilterUpgrades();
m_state = STATE_GOBUY;
return;
default:
return;
}
Expand All @@ -56,15 +97,25 @@ void CTF2BotUpgradeManager::ExecuteBuy()
{
if (!m_me->IsInUpgradeZone())
{
m_me->DebugPrintToConsole(BOTDEBUG_ERRORS, 255, 0, 0, "%s Error: CTF2BotUpgradeManager::Update called outside an upgrade zone!\n", m_me->GetDebugIdentifier());
if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
Vector origin = m_me->GetAbsOrigin();
smutils->LogError(myself, "BOT %s: CTF2BotUpgradeManager::Update called outsize an upgrade zone at <%3.2f, %3.2f, %3.2f>", m_me->GetDebugIdentifier(),
origin.x, origin.y, origin.z);
}

return;
}

if (m_tobuylist.empty())
{
m_me->DebugPrintToConsole(BOTDEBUG_TASKS, 0, 150, 0, "%s CTF2BotUpgradeManager::Update buy list is empty, advancing priority!\n", m_me->GetDebugIdentifier());
AdvancePriority();
FetchUpgrades();
if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
ConColorMsg(Color(0, 128, 0, 255), "%s: CTF2BotUpgradeManager::Update - Buy list is empty!", m_me->GetDebugIdentifier());
ConColorMsg(Color(0, 128, 128, 255), "Advancing from priority %i to %i!\n", m_nextpriority, (m_nextpriority + 1));
}

m_state = STATE_ADVANCE;
return;
}

Expand Down Expand Up @@ -92,10 +143,11 @@ void CTF2BotUpgradeManager::ExecuteBuy()
return;
}

if (m_me->IsDebugging(BOTDEBUG_TASKS))
if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
m_me->DebugPrintToConsole(BOTDEBUG_TASKS, 255, 87, 51, "%s bought upgrade ID %i <%s, %i>\n", m_me->GetDebugIdentifier(), upgradeinfo->GetUpgradeIndex(),
upgradeinfo->attribute.c_str(), upgradeinfo->quality);
ConColorMsg(Color(32, 255, 0, 255), "%s: Bought Upgrade -->\n", m_me->GetDebugIdentifier());
ConColorMsg(Color(0, 255, 255, 255), " ID: %i\n Attribute: %s\n Quality: %i\n Item Slot: %i\n Times Bought: %i\n",
upgradeinfo->GetUpgradeIndex(), upgradeinfo->attribute.c_str(), upgradeinfo->quality, upgradeinfo->itemslot, data->times_bought);
}

BeginBuyingUpgrades();
Expand Down Expand Up @@ -126,6 +178,23 @@ bool CTF2BotUpgradeManager::CanAffordAnyUpgrade() const
return false;
}

bool CTF2BotUpgradeManager::AnyUpgradeAvailable() const
{
const int maxprio = CTeamFortress2Mod::GetTF2Mod()->GetMvMUpgradeManager().GetMaxPriorityForClass(m_me->GetMyClassType());

if (m_nextpriority <= maxprio)
{
return true;
}

if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
ConColorMsg(Color(128, 255, 0, 255), "%s reached upgrade priority limit of %i (%i)!\n", m_me->GetDebugIdentifier(), maxprio, m_nextpriority);
}

return false;
}

void CTF2BotUpgradeManager::BeginBuyingUpgrades()
{
KeyValues* kvcmd = new KeyValues("MvM_UpgradesBegin");
Expand Down Expand Up @@ -174,6 +243,14 @@ void CTF2BotUpgradeManager::FetchUpgrades()

void CTF2BotUpgradeManager::FilterUpgrades()
{
if (m_me->GetMyWeaponsCount() == 0)
{
m_state = STATE_REFILTER;
m_me->UpdateMyWeapons(); // Force an update
return;
}


// Collect weapon item definition indexes
std::vector<int> myweaponindexes;
m_me->ForEveryWeapon([&myweaponindexes](const CBotWeapon& weapon) {
Expand All @@ -197,6 +274,17 @@ void CTF2BotUpgradeManager::FilterUpgrades()
return true; // upgrade has weapon restriction and the bot doesn't have the needed weapon, remove this upgrade
});

if (sm_navbot_tf_debug_bot_upgrades.GetBool())
{
for (auto it = start; it != m_tobuylist.end(); it++)
{
auto upgrade = *it;

ConColorMsg(Color(255, 0, 0, 255), "%s: Removing invalid upgrade %i <%s, %i, %i>. (Weapon Restriction)\n", m_me->GetDebugIdentifier(),
upgrade->GetUpgradeIndex(), upgrade->attribute.c_str(), upgrade->quality, upgrade->itemslot);
}
}

m_tobuylist.erase(start, m_tobuylist.end()); // remove upgrades
}

Expand Down
8 changes: 6 additions & 2 deletions extension/bot/tf2/tf2bot_upgrades.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class CTF2BotUpgradeManager

enum BuyUpgradeState : int
{
STATE_GOBUY = 0, // Go and buy something
STATE_GOBUY = 0, // Start buying stuff
STATE_BUYINGSTUFF, // Buying stuff
STATE_ADVANCE, // Advance into the next priority
STATE_WAITFORNEXTWAVE, // Already bought stuff, wait until the bot gets more money from the next wave
STATE_DOREFUND, // Bot should refund and rebuy stuff
STATE_REFILTER, // Rerun the weapon filter

MAX_STATES
};
Expand Down Expand Up @@ -76,7 +78,8 @@ class CTF2BotUpgradeManager
const int GetCurrentPriority() const { return m_nextpriority; }
const bool IsUpgradingDone() const { return m_tobuylist.empty(); }
const bool IsAtInitialPriority() const { return m_nextpriority == INITIAL_PRIORITY; }
const bool ShouldGoToAnUpgradeStation() const { return m_state == STATE_GOBUY || m_state == STATE_DOREFUND; }
const bool ShouldGoToAnUpgradeStation() const { return m_state == STATE_GOBUY || m_state == STATE_DOREFUND || m_state == STATE_REFILTER || m_state == STATE_ADVANCE ||
m_state == STATE_BUYINGSTUFF; }
const bool IsDoneForCurrentWave() const { return m_state == STATE_WAITFORNEXTWAVE; }
const bool IsBuyingUpgrades() const { return m_state == STATE_GOBUY || m_state == STATE_BUYINGSTUFF; }
BuyUpgradeState GetState() const { return m_state; }
Expand All @@ -101,6 +104,7 @@ class CTF2BotUpgradeManager
void ExecuteBuy();
void ExecuteRefund();
bool CanAffordAnyUpgrade() const;
bool AnyUpgradeAvailable() const;
};

#endif // !NAVBOT_TF2_MVM_UPGRADES_H_
2 changes: 2 additions & 0 deletions extension/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "manager.h"
#include <util/entprops.h>
#include <util/helpers.h>
#include <util/librandom.h>
#include <core/eventmanager.h>
#include <mods/basemod.h>
#include <bot/basebot.h>
Expand Down Expand Up @@ -158,6 +159,7 @@ bool NavBotExt::SDK_OnLoad(char* error, size_t maxlen, bool late)
extension = this;
m_hookruncmd = false;
m_gamedata = nullptr;
randomgen->ReSeed(); // set the initial seed based on the clock

// Create the directory
auto mod = smutils->GetGameFolderName();
Expand Down
9 changes: 6 additions & 3 deletions extension/mods/tf2/mvm_upgrade_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ SourceMod::SMCResult CMvMUpgradeManager::ReadSMC_NewSection(const SourceMod::SMC
if (strcmp(name, "upgrade") == 0)
{
m_parserinupgradesection = true;
m_parserinfo.attribute.clear();
m_parserinfo.priority = 1;
m_parserinfo.quality = MVM_DEFAULT_QUALITY;
ClearParserTemporaryInfo();
return SourceMod::SMCResult_Continue;
}

Expand Down Expand Up @@ -275,6 +273,11 @@ void CMvMUpgradeManager::PostLoadBotUpgradeInfo()
}

info.SetUpgrade(upgrade);

if (storage.second.m_highestpriority < info.priority)
{
storage.second.m_highestpriority = info.priority;
}
}
}

Expand Down
Loading

0 comments on commit 2012b32

Please sign in to comment.