Skip to content

Commit

Permalink
Merge pull request liyunfan1223#367 from liyunfan1223/self-command
Browse files Browse the repository at this point in the history
Fix .playerbot bot self
  • Loading branch information
liyunfan1223 authored Jul 24, 2024
2 parents 9b31802 + 75c12c4 commit 48ac024
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 43 deletions.
15 changes: 7 additions & 8 deletions conf/playerbots.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ AiPlayerbot.BotAutologin = 0
# Default: 0 (disabled)
AiPlayerbot.AllowPlayerBots = 0

# Bots will be summoned to player when accept group invitation
AiPlayerbot.SummonWhenGroup = 1

# Allow/deny bots from your guild
AiPlayerbot.AllowGuildBots = 1

# Bots will be summoned to player when accept group invitation
AiPlayerbot.SummonWhenGroup = 1

# Added following config
# Selfbot permission level (0 = disabled, 1 = gm only (default), 2 = all players, 3 = activate on login)
AiPlayerbot.SelfBotLevel = 1
Expand Down Expand Up @@ -541,7 +541,7 @@ AiPlayerbot.EquipmentPersistence = 0
# default: 80
AiPlayerbot.EquipmentPersistenceLevel = 80

# Bot automatically upgrade equipments on levelup
# Randombots automatically upgrade equipments on levelup
# Default: 1 (enabled)
AiPlayerbot.AutoUpgradeEquip = 1

Expand Down Expand Up @@ -578,15 +578,15 @@ AiPlayerbot.RandomBotQuestItems = "6948,5175,5176,5177,5178,16309,12382,13704,11
#
#

# Bots automatically learn classquest reward spells on levelup
# Randombots automatically learn classquest reward spells on levelup
# Default: 0 (disabled)
AiPlayerbot.AutoLearnQuestSpells = 0

# Bots automatically learn trainable spells on levelup
# Randombots automatically learn trainable spells on levelup
# Default: 1 (enabled)
AiPlayerbot.AutoLearnTrainerSpells = 1

# Bot automatically picks talent points on levelup
# Randombots automatically picks talent points on levelup
# Default: 1 (enabled)
AiPlayerbot.AutoPickTalents = 1

Expand Down Expand Up @@ -635,7 +635,6 @@ AiPlayerbot.RandomBotTeleLowerLevel = 3
AiPlayerbot.RandomBotTeleHigherLevel = 1

# Bots automatically teleport to another place for leveling on levelup
# Only for random bots
# Default: 1 (enabled)
AiPlayerbot.AutoTeleportForLevel = 1

Expand Down
2 changes: 1 addition & 1 deletion src/AiFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ void AiFactory::AddDefaultNonCombatStrategies(Player* player, PlayerbotAI* const
if (sPlayerbotAIConfig->autoSaveMana) {
nonCombatEngine->addStrategy("auto save mana");
}
if ((facade->IsRealPlayer() || sRandomPlayerbotMgr->IsRandomBot(player)) && !player->InBattleground())
if ((sRandomPlayerbotMgr->IsRandomBot(player)) && !player->InBattleground())
{
Player* master = facade->GetMaster();

Expand Down
2 changes: 1 addition & 1 deletion src/PlayerbotAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ PlayerbotAI::~PlayerbotAI()
delete aiObjectContext;

if (bot)
sPlayerbotsMgr->RemovePlayerBotData(bot->GetGUID());
sPlayerbotsMgr->RemovePlayerBotData(bot->GetGUID(), true);
}

void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
Expand Down
7 changes: 6 additions & 1 deletion src/PlayerbotFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ void PlayerbotFactory::InitPetTalents()
spells_row.erase(spells_row.begin() + index);
}
}
bot->SendTalentsInfoData(true);
}

void PlayerbotFactory::InitPet()
Expand Down Expand Up @@ -870,6 +871,7 @@ void PlayerbotFactory::InitTalentsTree(bool increment/*false*/, bool use_templat
if (bot->GetFreeTalentPoints())
InitTalents((specTab + 1) % 3);
}
bot->SendTalentsInfoData(false);
}

void PlayerbotFactory::InitTalentsBySpecNo(Player* bot, int specNo, bool reset)
Expand Down Expand Up @@ -933,6 +935,7 @@ void PlayerbotFactory::InitTalentsBySpecNo(Player* bot, int specNo, bool reset)
break;
}
}
bot->SendTalentsInfoData(false);
}

void PlayerbotFactory::InitTalentsByParsedSpecLink(Player* bot, std::vector<std::vector<uint32>> parsedSpecLink, bool reset)
Expand Down Expand Up @@ -983,6 +986,7 @@ void PlayerbotFactory::InitTalentsByParsedSpecLink(Player* bot, std::vector<std:
break;
}
}
bot->SendTalentsInfoData(false);
}

class DestroyItemsVisitor : public IterateItemsVisitor
Expand Down Expand Up @@ -1734,7 +1738,7 @@ void PlayerbotFactory::InitBags(bool destroyOld)
bot->DestroyItem(INVENTORY_SLOT_BAG_0, slot, true);
}
if (old_bag) {
return;
continue;
}
Item* newItem = bot->EquipNewItem(dest, newItemId, true);
if (newItem)
Expand Down Expand Up @@ -2983,6 +2987,7 @@ void PlayerbotFactory::InitGlyphs(bool increment)
}
}
}
bot->SendTalentsInfoData(false);
}

void PlayerbotFactory::CancelAuras()
Expand Down
61 changes: 39 additions & 22 deletions src/PlayerbotMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
if (GET_PLAYERBOT_AI(master))
{
messages.push_back("Disable player botAI");
DisablePlayerBot(master->GetGUID());
delete GET_PLAYERBOT_AI(master);
}
else if (sPlayerbotAIConfig->selfBotLevel == 0)
messages.push_back("Self-bot is disabled");
Expand All @@ -851,7 +851,8 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
else
{
messages.push_back("Enable player botAI");
OnBotLogin(master);
sPlayerbotsMgr->AddPlayerbotData(master, true);
GET_PLAYERBOT_AI(master)->SetMaster(master);
}

return messages;
Expand Down Expand Up @@ -938,20 +939,22 @@ std::vector<std::string> PlayerbotHolder::HandlePlayerbotCommand(char const* arg
race_limit = "2, 5, 6, 8, 10";
break;
}
uint32 maxAccountId = sPlayerbotAIConfig->randomBotAccounts.back();
// find a bot fit conditions and not in any guild
QueryResult results = CharacterDatabase.Query("SELECT guid FROM characters "
"WHERE name IN (SELECT name FROM playerbots_names) AND class = '{}' AND online = 0 AND race IN ({}) AND guid NOT IN ( SELECT guid FROM guild_member ) "
"ORDER BY account DESC LIMIT 1", claz, race_limit);
"AND account <= {} "
"ORDER BY account DESC LIMIT 1", claz, race_limit, maxAccountId);
if (results)
{
Field* fields = results->Fetch();
ObjectGuid guid = ObjectGuid(HighGuid::Player, fields[0].Get<uint32>());
AddPlayerBot(guid, master->GetSession()->GetAccountId());

messages.push_back("addclass " + std::string(charname) + " ok");
messages.push_back("Add class " + std::string(charname));
return messages;
}
messages.push_back("addclass failed.");
messages.push_back("Add class failed.");
return messages;
}

Expand Down Expand Up @@ -1214,7 +1217,7 @@ PlayerbotMgr::PlayerbotMgr(Player* const master) : PlayerbotHolder(), master(ma
PlayerbotMgr::~PlayerbotMgr()
{
if (master)
sPlayerbotsMgr->RemovePlayerBotData(master->GetGUID());
sPlayerbotsMgr->RemovePlayerBotData(master->GetGUID(), false);
}

void PlayerbotMgr::UpdateAIInternal(uint32 elapsed, bool /*minimal*/)
Expand Down Expand Up @@ -1427,31 +1430,45 @@ void PlayerbotsMgr::AddPlayerbotData(Player* player, bool isBotAI)
return;
}
// If the guid already exists in the map, remove it
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsMap.find(player->GetGUID());
if (itr != _playerbotsMap.end())
{
_playerbotsMap.erase(itr);
}

if (!isBotAI)
{
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsMgrMap.find(player->GetGUID());
if (itr != _playerbotsMgrMap.end())
{
_playerbotsMgrMap.erase(itr);
}
PlayerbotMgr* playerbotMgr = new PlayerbotMgr(player);
ASSERT(_playerbotsMap.emplace(player->GetGUID(), playerbotMgr).second);
ASSERT(_playerbotsMgrMap.emplace(player->GetGUID(), playerbotMgr).second);

playerbotMgr->OnPlayerLogin(player);
}
else
{
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsAIMap.find(player->GetGUID());
if (itr != _playerbotsAIMap.end())
{
_playerbotsAIMap.erase(itr);
}
PlayerbotAI* botAI = new PlayerbotAI(player);
ASSERT(_playerbotsMap.emplace(player->GetGUID(), botAI).second);
ASSERT(_playerbotsAIMap.emplace(player->GetGUID(), botAI).second);
}
}

void PlayerbotsMgr::RemovePlayerBotData(ObjectGuid const& guid)
void PlayerbotsMgr::RemovePlayerBotData(ObjectGuid const& guid, bool is_AI)
{
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsMap.find(guid);
if (itr != _playerbotsMap.end())
{
_playerbotsMap.erase(itr);
if (is_AI) {
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsAIMap.find(guid);
if (itr != _playerbotsAIMap.end())
{
_playerbotsAIMap.erase(itr);
}
} else {
std::unordered_map<ObjectGuid, PlayerbotAIBase*>::iterator itr = _playerbotsMgrMap.find(guid);
if (itr != _playerbotsMgrMap.end())
{
_playerbotsMgrMap.erase(itr);
}
}
}

Expand All @@ -1464,8 +1481,8 @@ PlayerbotAI* PlayerbotsMgr::GetPlayerbotAI(Player* player)
// if (player->GetSession()->isLogingOut() || player->IsDuringRemoveFromWorld()) {
// return nullptr;
// }
auto itr = _playerbotsMap.find(player->GetGUID());
if (itr != _playerbotsMap.end())
auto itr = _playerbotsAIMap.find(player->GetGUID());
if (itr != _playerbotsAIMap.end())
{
if (itr->second->IsBotAI())
return reinterpret_cast<PlayerbotAI*>(itr->second);
Expand All @@ -1480,8 +1497,8 @@ PlayerbotMgr* PlayerbotsMgr::GetPlayerbotMgr(Player* player)
{
return nullptr;
}
auto itr = _playerbotsMap.find(player->GetGUID());
if (itr != _playerbotsMap.end())
auto itr = _playerbotsMgrMap.find(player->GetGUID());
if (itr != _playerbotsMgrMap.end())
{
if (!itr->second->IsBotAI())
return reinterpret_cast<PlayerbotMgr*>(itr->second);
Expand Down
5 changes: 3 additions & 2 deletions src/PlayerbotMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ class PlayerbotsMgr
}

void AddPlayerbotData(Player* player, bool isBotAI);
void RemovePlayerBotData(ObjectGuid const& guid);
void RemovePlayerBotData(ObjectGuid const& guid, bool is_AI);

PlayerbotAI* GetPlayerbotAI(Player* player);
PlayerbotMgr* GetPlayerbotMgr(Player* player);

private:
std::unordered_map<ObjectGuid, PlayerbotAIBase*> _playerbotsMap;
std::unordered_map<ObjectGuid, PlayerbotAIBase*> _playerbotsAIMap;
std::unordered_map<ObjectGuid, PlayerbotAIBase*> _playerbotsMgrMap;
};

#define sPlayerbotsMgr PlayerbotsMgr::instance()
Expand Down
2 changes: 1 addition & 1 deletion src/Playerbots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class PlayerbotsScript : public PlayerbotScript
{
botAI->HandleBotOutgoingPacket(*packet);
}
else if (PlayerbotMgr* playerbotMgr = GET_PLAYERBOT_MGR(player))
if (PlayerbotMgr* playerbotMgr = GET_PLAYERBOT_MGR(player))
{
playerbotMgr->HandleMasterOutgoingPacket(*packet);
}
Expand Down
2 changes: 1 addition & 1 deletion src/strategy/actions/InviteToGroupAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool InviteToGroupAction::Execute(Event event)

bool InviteToGroupAction::Invite(Player* player)
{
if (!player)
if (!player || !player->IsInWorld())
return false;

if (!GET_PLAYERBOT_AI(player) && !botAI->GetSecurity()->CheckLevelFor(PLAYERBOT_SECURITY_INVITE, true, player))
Expand Down
4 changes: 2 additions & 2 deletions src/strategy/actions/SellAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool SellAction::Execute(Event event)
return true;
}

if (text == "all")
if (text != "")
{
std::vector<Item *> items = parseItems(text, ITERATE_ITEMS_IN_BAGS);
for (Item *item : items)
Expand All @@ -84,7 +84,7 @@ bool SellAction::Execute(Event event)
return true;
}

botAI->TellError("Usage: s gray/*/vendor/all");
botAI->TellError("Usage: s gray/*/vendor/[item link]");
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/strategy/actions/TrainerAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ bool MaintenanceAction::Execute(Event event)
factory.ApplyEnchantAndGemsNew();
}
bot->DurabilityRepairAll(false, 1.0f, false);
bot->SendTalentsInfoData(false);
return true;
}

Expand All @@ -181,6 +182,7 @@ bool RemoveGlyphAction::Execute(Event event)
{
bot->SetGlyph(slotIndex, 0, true);
}
bot->SendTalentsInfoData(false);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/strategy/hunter/HunterTriggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ class FreezingTrapTrigger : public HasCcTargetTrigger
FreezingTrapTrigger(PlayerbotAI* botAI) : HasCcTargetTrigger(botAI, "freezing trap") { }
};

class RapidFireTrigger : public BuffTrigger
class RapidFireTrigger : public BoostTrigger
{
public:
RapidFireTrigger(PlayerbotAI* botAI) : BuffTrigger(botAI, "rapid fire") { }
RapidFireTrigger(PlayerbotAI* botAI) : BoostTrigger(botAI, "rapid fire") { }
};

class TrueshotAuraTrigger : public BuffTrigger
Expand Down
2 changes: 1 addition & 1 deletion src/strategy/mage/GenericMageStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void MageBoostStrategy::InitTriggers(std::vector<TriggerNode*>& triggers)
triggers.push_back(new TriggerNode("icy veins", NextAction::array(0, new NextAction("icy veins", 50.0f), nullptr)));
triggers.push_back(new TriggerNode("presence of mind", NextAction::array(0, new NextAction("presence of mind", 42.0f), nullptr)));
// triggers.push_back(new TriggerNode("arcane power", NextAction::array(0, new NextAction("arcane power", 41.0f), nullptr)));
// triggers.push_back(new TriggerNode("mirror image", NextAction::array(0, new NextAction("mirror image", 41.0f), nullptr)));
triggers.push_back(new TriggerNode("mirror image", NextAction::array(0, new NextAction("mirror image", 41.0f), nullptr)));
}

void MageCcStrategy::InitTriggers(std::vector<TriggerNode*>& triggers)
Expand Down
3 changes: 2 additions & 1 deletion src/strategy/values/SpellIdValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ChatHelper.h"
#include "Playerbots.h"
#include "Vehicle.h"
#include "World.h"

SpellIdValue::SpellIdValue(PlayerbotAI* botAI) : CalculatedValue<uint32>(botAI, "spell id", 20 * 1000)
{
Expand Down Expand Up @@ -34,7 +35,7 @@ uint32 SpellIdValue::Calculate()
char firstSymbol = tolower(namepart[0]);
int spellLength = wnamepart.length();

LocaleConstant loc = bot->GetSession()->GetSessionDbcLocale();
LocaleConstant loc = LOCALE_enUS;

std::set<uint32> spellIds;
for (PlayerSpellMap::iterator itr = bot->GetSpellMap().begin(); itr != bot->GetSpellMap().end(); ++itr)
Expand Down

0 comments on commit 48ac024

Please sign in to comment.