mirror of
https://github.com/liyunfan1223/mod-playerbots.git
synced 2026-06-20 23:49:25 +02:00
<!-- Thank you for contributing to mod-playerbots, please make sure that you... 1. Submit your PR to the test-staging branch, not master. 2. Read the guidelines below before submitting. 3. Don't delete parts of this template. DESIGN PHILOSOPHY: We prioritize STABILITY, PERFORMANCE, AND PREDICTABILITY over behavioral realism. Every action and decision executes PER BOT AND PER TRIGGER. Small increases in logic complexity scale poorly across thousands of bots and negatively affect all. We prioritize a stable system over a smarter one. Bots don't need to behave perfectly; believable behavior is the goal, not human simulation. Default behavior must be cheap in processing; expensive behavior must be opt-in. Before submitting, make sure your changes aligns with these principles. --> ## Pull Request Description <!-- Describe what this change does and why it is needed --> Removed messages in failed attempts of buying tabard. Related with: #1885 ## How to Test the Changes <!-- - Step-by-step instructions to test the change. - Any required setup (e.g. multiple players, number of bots, specific configuration). - Expected behavior and how to verify it. --> Invite bot with guild strategy. Spam should not appear. ## Impact Assessment <!-- As a generic test, before and after measure of pmon (playerbot pmon tick) can help you here. --> - Does this change increase per-bot/per-tick processing or risk scaling poorly with thousands of bots? - - [x] No, not at all - - [ ] Minimal impact (**explain below**) - - [ ] Moderate impact (**explain below**) - Does this change modify default bot behavior? - - [x] No - - [ ] Yes (**explain why**) - Does this change add new decision branches or increase maintenance complexity? - - [x] No - - [ ] Yes (**explain below**) ## AI Assistance <!-- AI assistance is allowed, but all submitted code must be fully understood, reviewed, and owned by the contributor. We expect contributors to be honest about what they do and do not understand. --> Was AI assistance used while working on this change? - - [x] No - - [ ] Yes (**explain below**) <!-- If yes, please specify: - Purpose of usage (e.g. brainstorming, refactoring, documentation, code generation). - Which parts of the change were influenced or generated, and whether it was thoroughly reviewed. --> <!-- TRANSLATIONS: Anything new that the bots say in chat must be in a translatable format. This is done using GetBotTextOrDefault, which you can search for in the codebase to find examples. Your code needs to have English as the default fallback, while the full translations need to be in an SQL update file. The languages in the file are the nine language options supported by AzerothCore: English, Korean, French, German, Chinese, Taiwanese, Spanish, Spanish Mexico, and Russian. See data/sql/playerbots/updates/2025_12_27_ai_playerbot_fishing_text.sql as an example of a translation SQL update, whose content are called within the codebase at src/strategy/actions/FishingAction.cpp --> ## Final Checklist - - [x] Stability is not compromised. - - [x] Performance impact is understood, tested, and acceptable. - - [x] Added logic complexity is justified and explained. - - [x] Any new bot dialogue lines are translated. - - [x] Documentation updated if needed (Conf comments, WiKi commands). ## Notes for Reviewers <!-- Anything else that's helpful to review or test your pull request. -->
255 lines
9.1 KiB
C++
255 lines
9.1 KiB
C++
/*
|
|
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license, you may redistribute it
|
|
* and/or modify it under version 3 of the License, or (at your option), any later version.
|
|
*/
|
|
|
|
#include "BuyAction.h"
|
|
|
|
#include "BudgetValues.h"
|
|
#include "Event.h"
|
|
#include "ItemCountValue.h"
|
|
#include "ItemUsageValue.h"
|
|
#include "ItemVisitors.h"
|
|
#include "Playerbots.h"
|
|
#include "StatsWeightCalculator.h"
|
|
|
|
bool BuyAction::Execute(Event event)
|
|
{
|
|
bool buyUseful = false;
|
|
ItemIds itemIds;
|
|
std::string const link = event.getParam();
|
|
|
|
if (link == "vendor")
|
|
buyUseful = true;
|
|
else
|
|
{
|
|
itemIds = chat->parseItems(link);
|
|
}
|
|
|
|
GuidVector vendors = botAI->GetAiObjectContext()->GetValue<GuidVector>("nearest npcs")->Get();
|
|
|
|
bool vendored = false;
|
|
bool result = false;
|
|
for (GuidVector::iterator i = vendors.begin(); i != vendors.end(); ++i)
|
|
{
|
|
ObjectGuid vendorguid = *i;
|
|
Creature* pCreature = bot->GetNPCIfCanInteractWith(vendorguid, UNIT_NPC_FLAG_VENDOR);
|
|
if (!pCreature)
|
|
continue;
|
|
|
|
vendored = true;
|
|
|
|
if (buyUseful)
|
|
{
|
|
// Items are evaluated from high-level to low level.
|
|
// For each item the bot checks again if an item is usefull.
|
|
// Bot will buy until no usefull items are left.
|
|
|
|
VendorItemData const* tItems = pCreature->GetVendorItems();
|
|
if (!tItems)
|
|
continue;
|
|
|
|
VendorItemList m_items_sorted = tItems->m_items;
|
|
|
|
m_items_sorted.erase(std::remove_if(m_items_sorted.begin(), m_items_sorted.end(),
|
|
[](VendorItem* i)
|
|
{
|
|
ItemTemplate const* proto = sObjectMgr->GetItemTemplate(i->item);
|
|
return !proto;
|
|
}),
|
|
m_items_sorted.end());
|
|
|
|
if (m_items_sorted.empty())
|
|
continue;
|
|
|
|
StatsWeightCalculator calculator(bot);
|
|
calculator.SetItemSetBonus(false);
|
|
calculator.SetOverflowPenalty(false);
|
|
|
|
std::sort(m_items_sorted.begin(), m_items_sorted.end(),
|
|
[&calculator](VendorItem* i, VendorItem* j)
|
|
{
|
|
ItemTemplate const* item1 = sObjectMgr->GetItemTemplate(i->item);
|
|
ItemTemplate const* item2 = sObjectMgr->GetItemTemplate(j->item);
|
|
|
|
if (!item1 || !item2)
|
|
return false;
|
|
|
|
float score1 = calculator.CalculateItem(item1->ItemId);
|
|
float score2 = calculator.CalculateItem(item2->ItemId);
|
|
|
|
// Fallback to itemlevel if either score is 0
|
|
if (score1 == 0 || score2 == 0)
|
|
{
|
|
score1 = item1->ItemLevel;
|
|
score2 = item2->ItemLevel;
|
|
}
|
|
return score1 > score2; // Sort in descending order (highest score first)
|
|
});
|
|
|
|
std::unordered_map<uint32, float> bestPurchasedItemScore; // Track best item score per InventoryType
|
|
|
|
for (auto& tItem : m_items_sorted)
|
|
{
|
|
uint32 maxPurchases = 1; // Default to buying once
|
|
ItemTemplate const* proto = sObjectMgr->GetItemTemplate(tItem->item);
|
|
if (!proto)
|
|
continue;
|
|
|
|
if (proto->Class == ITEM_CLASS_CONSUMABLE || proto->Class == ITEM_CLASS_PROJECTILE)
|
|
{
|
|
maxPurchases = 10; // Allow up to 10 purchases if it's a consumable or projectile
|
|
}
|
|
|
|
for (uint32 i = 0; i < maxPurchases; i++)
|
|
{
|
|
ItemUsage usage = AI_VALUE2(ItemUsage, "item usage", tItem->item);
|
|
|
|
uint32 invType = proto->InventoryType;
|
|
|
|
// Calculate item score
|
|
float newScore = calculator.CalculateItem(proto->ItemId);
|
|
|
|
// Skip if we already bought a better item for this slot
|
|
if (bestPurchasedItemScore.find(invType) != bestPurchasedItemScore.end() &&
|
|
bestPurchasedItemScore[invType] > newScore)
|
|
{
|
|
break; // Skip lower-scoring items
|
|
}
|
|
|
|
// Check the bot's currently equipped item for this slot
|
|
uint8 dstSlot = botAI->FindEquipSlot(proto, NULL_SLOT, true);
|
|
Item* oldItem = bot->GetItemByPos(INVENTORY_SLOT_BAG_0, dstSlot);
|
|
|
|
float oldScore = 0.0f;
|
|
if (oldItem)
|
|
{
|
|
ItemTemplate const* oldItemProto = oldItem->GetTemplate();
|
|
if (oldItemProto)
|
|
oldScore = calculator.CalculateItem(oldItemProto->ItemId);
|
|
}
|
|
|
|
// Skip if the bot already has a better or equal item equipped
|
|
if (oldScore > newScore)
|
|
break;
|
|
|
|
uint32 price = proto->BuyPrice;
|
|
price = uint32(floor(price * bot->GetReputationPriceDiscount(pCreature)));
|
|
|
|
NeedMoneyFor needMoneyFor = NeedMoneyFor::none;
|
|
switch (usage)
|
|
{
|
|
case ITEM_USAGE_REPLACE:
|
|
case ITEM_USAGE_EQUIP:
|
|
case ITEM_USAGE_BAD_EQUIP:
|
|
case ITEM_USAGE_BROKEN_EQUIP:
|
|
needMoneyFor = NeedMoneyFor::gear;
|
|
break;
|
|
case ITEM_USAGE_AMMO:
|
|
needMoneyFor = NeedMoneyFor::ammo;
|
|
break;
|
|
case ITEM_USAGE_QUEST:
|
|
needMoneyFor = NeedMoneyFor::anything;
|
|
break;
|
|
case ITEM_USAGE_USE:
|
|
needMoneyFor = NeedMoneyFor::consumables;
|
|
break;
|
|
case ITEM_USAGE_SKILL:
|
|
needMoneyFor = NeedMoneyFor::tradeskill;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (needMoneyFor == NeedMoneyFor::none)
|
|
break;
|
|
|
|
if (AI_VALUE2(uint32, "free money for", uint32(needMoneyFor)) < price)
|
|
break;
|
|
|
|
if (!BuyItem(tItems, vendorguid, proto))
|
|
break;
|
|
|
|
// Store the best item score per InventoryType
|
|
bestPurchasedItemScore[invType] = newScore;
|
|
|
|
if (needMoneyFor == NeedMoneyFor::gear)
|
|
{
|
|
botAI->DoSpecificAction("equip upgrades packet action");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (itemIds.empty())
|
|
return false;
|
|
|
|
for (ItemIds::iterator i = itemIds.begin(); i != itemIds.end(); i++)
|
|
{
|
|
uint32 itemId = *i;
|
|
ItemTemplate const* proto = sObjectMgr->GetItemTemplate(itemId);
|
|
if (!proto)
|
|
continue;
|
|
|
|
result |= BuyItem(pCreature->GetVendorItems(), vendorguid, proto);
|
|
|
|
if (!result)
|
|
{
|
|
std::ostringstream out;
|
|
out << "Nobody sells " << ChatHelper::FormatItem(proto) << " nearby";
|
|
botAI->TellMaster(out.str());
|
|
continue;
|
|
}
|
|
|
|
ItemUsage usage = AI_VALUE2(ItemUsage, "item usage", itemId);
|
|
if (usage == ITEM_USAGE_REPLACE || usage == ITEM_USAGE_EQUIP ||
|
|
usage == ITEM_USAGE_BAD_EQUIP || usage == ITEM_USAGE_BROKEN_EQUIP)
|
|
{
|
|
botAI->DoSpecificAction("equip upgrades packet action");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return vendored;
|
|
}
|
|
|
|
bool BuyAction::BuyItem(VendorItemData const* tItems, ObjectGuid vendorguid, ItemTemplate const* proto)
|
|
{
|
|
if (!tItems || !proto)
|
|
return false;
|
|
|
|
uint32 itemId = proto->ItemId;
|
|
uint32 oldCount = bot->GetItemCount(itemId, false);
|
|
|
|
for (uint32 slot = 0; slot < tItems->GetItemCount(); ++slot)
|
|
{
|
|
if (tItems->GetItem(slot)->item != itemId)
|
|
continue;
|
|
|
|
uint32 botMoney = bot->GetMoney();
|
|
if (botAI->HasCheat(BotCheatMask::gold))
|
|
bot->SetMoney(10000000);
|
|
|
|
bot->BuyItemFromVendorSlot(vendorguid, slot, itemId, 1, NULL_BAG, NULL_SLOT);
|
|
|
|
if (botAI->HasCheat(BotCheatMask::gold))
|
|
bot->SetMoney(botMoney);
|
|
|
|
uint32 newCount = bot->GetItemCount(itemId, false);
|
|
if (newCount > oldCount)
|
|
{
|
|
std::ostringstream out;
|
|
out << "Buying " << ChatHelper::FormatItem(proto);
|
|
botAI->TellMaster(out.str());
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|