mod-playerbots/src/Ai/Base/Actions/TradeStatusAction.cpp
kadeshar 55c5d29e2d
Replace hardcoded bot texts (#2408)
<!--
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 -->

Replaced hardcoded bot text with translationable.

Related with: #1295 


## 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.
-->

1. Invite bots
2. Check that text after "follow" and "stay" return texts "Following"
and "Staying"

## 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?
- - [ ] No
- - [x] 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.
-->

Summary hardcoded text and checking that they already exists to reuse.

<!--
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.
-->
2026-05-22 21:41:24 -07:00

380 lines
13 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 "TradeStatusAction.h"
#include "CraftValue.h"
#include "Event.h"
#include "GuildTaskMgr.h"
#include "ItemUsageValue.h"
#include "ItemVisitors.h"
#include "PlayerbotMgr.h"
#include "PlayerbotSecurity.h"
#include "PlayerbotTextMgr.h"
#include "Playerbots.h"
#include "RandomPlayerbotMgr.h"
#include "SetCraftAction.h"
bool TradeStatusAction::Execute(Event event)
{
Player* trader = bot->GetTrader();
Player* master = GetMaster();
if (!trader)
return false;
PlayerbotAI* traderBotAI = GET_PLAYERBOT_AI(trader);
// Allow the master and group members to trade
if (trader != master && !traderBotAI && (!bot->GetGroup() || !bot->GetGroup()->IsMember(trader->GetGUID())))
{
bot->Whisper(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_busy_now", "I'm kind of busy now", {}),
LANG_UNIVERSAL, trader);
return false;
}
if (sPlayerbotAIConfig.enableRandomBotTrading == 0 && (sRandomPlayerbotMgr.IsRandomBot(bot)|| sRandomPlayerbotMgr.IsAddclassBot(bot)))
{
bot->Whisper(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_disabled", "Trading is disabled", {}),
LANG_UNIVERSAL, trader);
return false;
}
// Allow trades from group members or bots
if ((!bot->GetGroup() || !bot->GetGroup()->IsMember(trader->GetGUID())) &&
(trader != master || !botAI->GetSecurity()->CheckLevelFor(PLAYERBOT_SECURITY_ALLOW_ALL, true, master)) &&
!traderBotAI)
{
WorldPacket p;
uint32 status = 0;
p << status;
bot->GetSession()->HandleCancelTradeOpcode(p);
return false;
}
WorldPacket p(event.getPacket());
p.rpos(0);
uint32 status;
p >> status;
if (status == TRADE_STATUS_TRADE_ACCEPT || (status == TRADE_STATUS_BACK_TO_TRADE && trader->GetTradeData() && trader->GetTradeData()->IsAccepted()))
{
WorldPacket p;
uint32 status = 0;
p << status;
uint32 discount = sRandomPlayerbotMgr.GetTradeDiscount(bot, trader);
if (CheckTrade())
{
std::map<uint32, uint32> givenItemIds, takenItemIds;
for (uint32 slot = 0; slot < TRADE_SLOT_TRADED_COUNT; ++slot)
{
Item* item = trader->GetTradeData()->GetItem((TradeSlots)slot);
if (item)
givenItemIds[item->GetTemplate()->ItemId] += item->GetCount();
item = bot->GetTradeData()->GetItem((TradeSlots)slot);
if (item)
takenItemIds[item->GetTemplate()->ItemId] += item->GetCount();
}
bot->GetSession()->HandleAcceptTradeOpcode(p);
if (bot->GetTradeData())
{
sRandomPlayerbotMgr.SetTradeDiscount(bot, trader, discount);
return false;
}
for (std::map<uint32, uint32>::iterator i = givenItemIds.begin(); i != givenItemIds.end(); ++i)
{
uint32 itemId = i->first;
uint32 count = i->second;
CraftData& craftData = AI_VALUE(CraftData&, "craft");
if (!craftData.IsEmpty() && craftData.IsRequired(itemId))
{
craftData.AddObtained(itemId, count);
}
GuildTaskMgr::instance().CheckItemTask(itemId, count, trader, bot);
}
for (std::map<uint32, uint32>::iterator i = takenItemIds.begin(); i != takenItemIds.end(); ++i)
{
uint32 itemId = i->first;
uint32 count = i->second;
CraftData& craftData = AI_VALUE(CraftData&, "craft");
if (!craftData.IsEmpty() && craftData.itemId == itemId)
{
craftData.Crafted(count);
}
}
return true;
}
}
else if (status == TRADE_STATUS_BEGIN_TRADE)
{
if (!bot->HasInArc(CAST_ANGLE_IN_FRONT, trader, sPlayerbotAIConfig.sightDistance))
bot->SetFacingToObject(trader);
BeginTrade();
return true;
}
return false;
}
void TradeStatusAction::BeginTrade()
{
Player* trader = bot->GetTrader();
if (!trader || GET_PLAYERBOT_AI(bot->GetTrader()))
return;
WorldPacket p;
bot->GetSession()->HandleBeginTradeOpcode(p);
ListItemsVisitor visitor;
IterateItems(&visitor);
botAI->TellMaster("=== Inventory ===");
TellItems(visitor.items, visitor.soulbound);
if (sRandomPlayerbotMgr.IsRandomBot(bot))
{
uint32 discount = sRandomPlayerbotMgr.GetTradeDiscount(bot, botAI->GetMaster());
if (discount)
{
std::ostringstream out;
out << "Discount up to: " << chat->formatMoney(discount);
botAI->TellMaster(out);
}
}
}
bool TradeStatusAction::CheckTrade()
{
Player* trader = bot->GetTrader();
if (!bot->GetTradeData() || !trader || !trader->GetTradeData())
return false;
if (!botAI->HasActivePlayerMaster() && GET_PLAYERBOT_AI(bot->GetTrader()))
{
for (uint32 slot = 0; slot < TRADE_SLOT_TRADED_COUNT; ++slot)
{
Item* item = bot->GetTradeData()->GetItem((TradeSlots)slot);
if (item)
break;
}
bool isGettingItem = false;
for (uint32 slot = 0; slot < TRADE_SLOT_TRADED_COUNT; ++slot)
{
Item* item = trader->GetTradeData()->GetItem((TradeSlots)slot);
if (item)
{
isGettingItem = true;
break;
}
}
if (isGettingItem)
{
if (bot->GetGroup() && bot->GetGroup()->IsMember(bot->GetTrader()->GetGUID()) &&
botAI->HasRealPlayerMaster())
botAI->TellMasterNoFacing(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_thank_you_player",
"Thank you %player",
{{"%player", chat->FormatWorldobject(bot->GetTrader())}}));
else
bot->Say(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_thank_you_player",
"Thank you %player",
{{"%player", chat->FormatWorldobject(bot->GetTrader())}}),
(bot->GetTeamId() == TEAM_ALLIANCE ? LANG_COMMON : LANG_ORCISH));
}
return isGettingItem;
}
if (!bot->GetSession())
{
return false;
}
uint32 accountId = bot->GetSession()->GetAccountId();
if (!sPlayerbotAIConfig.IsInRandomAccountList(accountId))
{
int32 botItemsMoney = CalculateCost(bot, true);
int32 botMoney = bot->GetTradeData()->GetMoney() + botItemsMoney;
int32 playerItemsMoney = CalculateCost(trader, false);
int32 playerMoney = trader->GetTradeData()->GetMoney() + playerItemsMoney;
if (playerMoney || botMoney)
botAI->PlaySound(playerMoney < botMoney ? TEXT_EMOTE_SIGH : TEXT_EMOTE_THANK);
return true;
}
int32 botItemsMoney = CalculateCost(bot, true);
int32 botMoney = bot->GetTradeData()->GetMoney() + botItemsMoney;
int32 playerItemsMoney = CalculateCost(trader, false);
int32 playerMoney = trader->GetTradeData()->GetMoney() + playerItemsMoney;
if (botItemsMoney > 0 && sPlayerbotAIConfig.enableRandomBotTrading == 2 && (sRandomPlayerbotMgr.IsRandomBot(bot)|| sRandomPlayerbotMgr.IsAddclassBot(bot)))
{
bot->Whisper(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_selling_disabled", "Selling is disabled.", {}),
LANG_UNIVERSAL, trader);
return false;
}
if (playerItemsMoney && sPlayerbotAIConfig.enableRandomBotTrading == 3 && (sRandomPlayerbotMgr.IsRandomBot(bot)|| sRandomPlayerbotMgr.IsAddclassBot(bot)))
{
bot->Whisper(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_buying_disabled", "Buying is disabled.", {}),
LANG_UNIVERSAL, trader);
return false;
}
for (uint32 slot = 0; slot < TRADE_SLOT_TRADED_COUNT; ++slot)
{
Item* item = bot->GetTradeData()->GetItem((TradeSlots)slot);
if (item && !item->GetTemplate()->SellPrice && !item->GetTemplate()->IsConjuredConsumable())
{
std::ostringstream out;
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_item_not_for_sale",
"%item - This is not for sale",
{{"%item", chat->FormatItem(item->GetTemplate())}}));
botAI->PlaySound(TEXT_EMOTE_NO);
return false;
}
item = trader->GetTradeData()->GetItem((TradeSlots)slot);
if (item)
{
std::ostringstream out;
out << item->GetTemplate()->ItemId;
ItemUsage usage = AI_VALUE2(ItemUsage, "item usage", out.str());
if ((botMoney && !item->GetTemplate()->BuyPrice) || usage == ITEM_USAGE_NONE)
{
std::ostringstream out;
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_item_not_needed",
"%item - I don't need this",
{{"%item", chat->FormatItem(item->GetTemplate())}}));
botAI->PlaySound(TEXT_EMOTE_NO);
return false;
}
}
}
if (!botMoney && !playerMoney)
return true;
if (!botItemsMoney && !playerItemsMoney)
{
botAI->TellError(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_no_items_error", "There are no items to trade", {}));
return false;
}
int32 discount = (int32)sRandomPlayerbotMgr.GetTradeDiscount(bot, trader);
int32 delta = playerMoney - botMoney;
int32 moneyDelta = (int32)trader->GetTradeData()->GetMoney() - (int32)bot->GetTradeData()->GetMoney();
bool success = false;
if (delta < 0)
{
if (delta + discount >= 0)
{
if (moneyDelta < 0)
{
botAI->TellError(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_discount_buy_only", "You can use discount to buy items only", {}));
botAI->PlaySound(TEXT_EMOTE_NO);
return false;
}
success = true;
}
}
else
success = true;
if (success)
{
sRandomPlayerbotMgr.AddTradeDiscount(bot, trader, delta);
switch (urand(0, 4))
{
case 0:
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_success_pleasure", "A pleasure doing business with you", {}));
break;
case 1:
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_success_fair_trade", "Fair trade", {}));
break;
case 2:
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_success_thanks", "Thanks", {}));
break;
case 3:
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_success_off_with_you", "Off with you", {}));
break;
}
botAI->PlaySound(TEXT_EMOTE_THANK);
return true;
}
std::ostringstream out;
botAI->TellMaster(PlayerbotTextMgr::instance().GetBotTextOrDefault(
"trade_want_money_for_this",
"I want %money for this",
{{"%money", chat->formatMoney(-(delta + discount))}}));
botAI->PlaySound(TEXT_EMOTE_NO);
return false;
}
int32 TradeStatusAction::CalculateCost(Player* player, bool sell)
{
Player* trader = bot->GetTrader();
TradeData* data = player->GetTradeData();
if (!data)
return 0;
uint32 sum = 0;
for (uint32 slot = 0; slot < TRADE_SLOT_TRADED_COUNT; ++slot)
{
Item* item = data->GetItem((TradeSlots)slot);
if (!item)
continue;
ItemTemplate const* proto = item->GetTemplate();
if (!proto)
continue;
if (proto->Quality < ITEM_QUALITY_NORMAL)
return 0;
CraftData& craftData = AI_VALUE(CraftData&, "craft");
if (!craftData.IsEmpty())
{
if (player == trader && !sell && craftData.IsRequired(proto->ItemId))
continue;
if (player == bot && sell && craftData.itemId == proto->ItemId && craftData.IsFulfilled())
{
sum += item->GetCount() * SetCraftAction::GetCraftFee(craftData);
continue;
}
}
if (sell)
sum += item->GetCount() * proto->SellPrice * sRandomPlayerbotMgr.GetSellMultiplier(bot);
else
sum += item->GetCount() * proto->BuyPrice * sRandomPlayerbotMgr.GetBuyMultiplier(bot);
}
return sum;
}