diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.lua b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.lua index 4cf8b99490..cc4e0b2748 100644 --- a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.lua +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.lua @@ -258,6 +258,8 @@ local function ServerCommand(text) end Cmd.Say(text) end +-- a dot command from a click, for the other files (ForeverBots_Trade.lua): see Cmd.Say +Cmd.ns.Say = function(text) ServerCommand(text) end -- Stance button of the panel, three steps (Karrz 2026-11-03: "passive, defensive and aggressive"). ForeverBotsDB.stance is -- "nofight" - Passive: the bots attack nothing at all, not even to defend; heals and buffs go on (taming a beast, a sheep that diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc index e190078744..d43f0ab762 100644 --- a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots.toc @@ -2,7 +2,7 @@ ## Title: WoW Forever Bots ## Notes: One-click PlayerbotV2 companions: add a bot of any class and role, call, dismiss and command your bots. ## Author: Karrz -## Version: 0.6.21 +## Version: 0.6.22 ## IconTexture: Interface\AddOns\ForeverBots\Textures\mono_fb64.tga ## SavedVariables: ForeverBotsDB diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua new file mode 100644 index 0000000000..0eacb2deb8 --- /dev/null +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/ForeverBots_Trade.lua @@ -0,0 +1,78 @@ +--[[ +ForeverBots_Trade: one click on Trade accepts a trade with a bot. + +The stock Trade button sends the offer state the client last heard of. A bot answers an offer in the same moment (puts its side +in, changes it, accepts), so the click often carries an older state and the server answers "the trade has changed", click after +click. Right after the stock click this file sends what the window shows (both sides' money, the item and count in each of the +six traded slots) as `.bot trade `. When the window still holds exactly that, the server accepts for you +with the bot's current state; when it does not, nothing is accepted and you are told to look again. A trade with a player is +left to the stock click alone (the server answers "none"). + +The command goes the way every click command of the add-on goes (ns.Say): alone as a whisper to yourself, which the server runs +as a dot command before any whisper is made; in a group as an add-on message on the group channel. The answer is a hidden +system line (TRADE_RESP, AddonControl.cpp), which reaches a solo client. Nothing here runs from a timer or an event. +]] + +local _, ns = ... +local net = ns.Net +if not net then return end +net.ANSWERS.TRADE_RESP = true +local L = ns.L or setmetatable({}, { __index = function(_, k) return (k:gsub("^short:", "")) end }) + +local SLOTS = 6 -- traded slots; slot 7 ("will not be traded") does not change hands + +local function ItemId(link) + return type(link) == "string" and tonumber(link:match("item:(%d+)")) or 0 +end + +-- One side as the server writes it (TradeSideText): money, then entry:count per traded slot, 0:0 for an empty one. +local function Side(linkOf, infoOf, money) + local out = { tostring(math.floor(tonumber(money) or 0)) } + for i = 1, SLOTS do + local id = ItemId(linkOf and linkOf(i)) + local count = 0 + if id > 0 then + local _, _, n = infoOf(i) + count = tonumber(n) or 1 + end + out[#out + 1] = tostring(id) + out[#out + 1] = tostring(count) + end + return table.concat(out, ":") +end + +local function Seen() + if not (GetTradeTargetItemLink and GetTradePlayerItemLink and GetTargetTradeMoney and GetPlayerTradeMoney) then return nil end + return Side(GetTradeTargetItemLink, GetTradeTargetItemInfo, GetTargetTradeMoney()) .. ":" .. + Side(GetTradePlayerItemLink, GetTradePlayerItemInfo, GetPlayerTradeMoney()) +end + +local function Answered(fields) + if fields and fields[1] == "changed" and UIErrorsFrame then + UIErrorsFrame:AddMessage(L["The bot changed its offer: look at it again, then click Trade."], 1, 0.6, 0) + end +end + +-- after the stock click, from the click itself +local function AfterTradeClick() + if not (ns.Say and TradeFrame and TradeFrame:IsShown()) then return end + local seen = Seen() + if not seen then return end + net.seq = (net.seq % 0x7ffffffe) + 1 + local seq = net.seq + net.pending[seq] = { cb = Answered, mtype = "TRADE_RESP", at = GetTime() } + ns.Say(string.format(".bot trade %x %s", seq, seen)) +end + +local hooked = false +local function Hook() + local button = TradeFrameTradeButton or (TradeFrame and TradeFrame.TradeButton) + if hooked or not button or not button.HookScript then return end + button:HookScript("OnClick", AfterTradeClick) + hooked = true +end + +local f = CreateFrame("Frame") +f:RegisterEvent("TRADE_SHOW") +f:RegisterEvent("PLAYER_LOGIN") +f:SetScript("OnEvent", Hook) diff --git a/src/modules/PlayerbotV2/Addon/ForeverBots/Locale_ruRU.lua b/src/modules/PlayerbotV2/Addon/ForeverBots/Locale_ruRU.lua index 821c21e6a2..f7475b3320 100644 --- a/src/modules/PlayerbotV2/Addon/ForeverBots/Locale_ruRU.lua +++ b/src/modules/PlayerbotV2/Addon/ForeverBots/Locale_ruRU.lua @@ -579,4 +579,5 @@ ForeverBotsLocale = { ["Set with the mouse"] = "Поставить мышью", ["short:G%d"] = "Г%d", ["Tick every bot in this raid group. Click again when they are all ticked to untick them."] = "Отметить всех ботов этой группы рейда. Нажмите еще раз, когда отмечены все, чтобы снять отметку.", + ["The bot changed its offer: look at it again, then click Trade."] = "Бот изменил свое предложение: посмотрите еще раз и нажмите «Обмен».", -- check } diff --git a/src/modules/PlayerbotV2/Commands/cs_playerbot_v2.cpp b/src/modules/PlayerbotV2/Commands/cs_playerbot_v2.cpp index aebe2273de..963609bb53 100644 --- a/src/modules/PlayerbotV2/Commands/cs_playerbot_v2.cpp +++ b/src/modules/PlayerbotV2/Commands/cs_playerbot_v2.cpp @@ -810,6 +810,22 @@ public: return true; } + // `.bot trade `: right after the stock Trade click, the ForeverBots trade window sends what it + // shows, and a trade with a bot is accepted when that is still exactly the offer (AddonControl::BotTradeRequest). Alone the + // add-on sends it as a whisper to the player's own name from the click: a dot command runs before any whisper is made, so + // nothing is sent to anyone. The answer is a hidden TRADE_RESP frame, never plain text. + static bool HandleBotTrade(ChatHandler* handler, std::string seqHex, std::string seen) + { + WorldSession* sess = handler->GetSession(); + Player* me = sess ? sess->GetPlayer() : nullptr; + if (!me || seqHex.empty() || seqHex.size() > 8 || seqHex.find_first_not_of("0123456789abcdefABCDEF") != std::string::npos) + return true; // quiet: only the add-on sends this + if (seen.empty() || seen.size() > 230 || seen.find_first_not_of("0123456789:") != std::string::npos) + return true; + Playerbot::V2::AddonControl::BotTradeRequest(me, uint32(std::strtoul(seqHex.c_str(), nullptr, 16)), seen); + return true; + } + // ---- .bot: one-step companions ------------------------------------------------------------------------------ static Playerbot::Role ParseRoleWord(std::string const& token, bool& ok) diff --git a/src/modules/PlayerbotV2/Session/AddonControl.cpp b/src/modules/PlayerbotV2/Session/AddonControl.cpp index f50896cecf..69f2d75e19 100644 --- a/src/modules/PlayerbotV2/Session/AddonControl.cpp +++ b/src/modules/PlayerbotV2/Session/AddonControl.cpp @@ -37,6 +37,8 @@ #include "Player.h" #include "QuestDef.h" #include "RBAC.h" +#include "TradeData.h" +#include "TradePackets.h" #include "World.h" #include "WorldSession.h" @@ -1688,6 +1690,89 @@ void DispatchAssembled(WorldSession* sess, uint32 seq, // ============================================================================ // Public entry — called from the PlayerScript::OnChat hook. // ============================================================================ +// ============================================================================ +// Trading with a bot (ForeverBots_Trade.lua, `.bot trade `). +// The stock Trade button sends the offer state the client last heard of. A bot +// answers an offer in the same moment (puts its side in, changes it, accepts), +// so the click often carries an older state and the server answers "the trade +// has changed", click after click. Right after the stock click the add-on sends +// what the window shows: both sides' money and the item and count in each of the +// six traded slots. When that is exactly what the window holds now, the player's +// accept runs with the bot's current state; when it is not, nothing is accepted +// (the player has not seen the new offer) and the add-on says to look again. +// TRADE_RESP|done accepted for the player (the normal accept ran) +// TRADE_RESP|already the stock click was accepted already +// TRADE_RESP|changed the window is not what the player saw +// TRADE_RESP|none no window with a bot (a trade with a player keeps the stock click only) +// Answers always go as hidden system lines: alone, the client gets no add-on +// whisper through (Net.lua), and this request usually comes from a solo player. +// ============================================================================ +namespace { + +void SendTradeAnswer(WorldSession* sess, uint32 seq, std::string_view what) +{ + char head[24]; + std::snprintf(head, sizeof(head), "%s|%08x|1|1|", kProtoVersion, seq); + std::string body = std::string(kSysMarker) + head + "TRADE_RESP|" + std::string(what); + WorldPackets::Chat::Chat sys; + sys.Initialize(CHAT_MSG_SYSTEM, LANG_UNIVERSAL, nullptr, nullptr, body); + sess->SendPacket(sys.Write()); +} + +// One side as the add-on writes it: money, then entry and count of traded slots 1-6 (0:0 for an empty slot). +std::string TradeSideText(TradeData const* side) +{ + std::string out = std::to_string(side->GetMoney()); + for (uint8 i = 0; i < TRADE_SLOT_TRADED_COUNT; ++i) + { + Item const* item = side->GetItem(TradeSlots(i)); + out += ":" + std::to_string(item ? item->GetEntry() : 0u) + ":" + std::to_string(item ? item->GetCount() : 0u); + } + return out; +} + +} // anonymous + +void BotTradeRequest(Player* player, uint32 seq, std::string const& seen) +{ + WorldSession* sess = player ? player->GetSession() : nullptr; + if (!sess) + return; + + TradeData* mine = player->GetTradeData(); + Player* bot = mine ? mine->GetTrader() : nullptr; + TradeData* theirs = bot ? bot->GetTradeData() : nullptr; + // only a window between this player and a bot: a trade with a player keeps the client's own accept and its state check + if (!theirs || theirs->GetTrader() != player || !Services::Initialized() || !Services::SessionMgr().IsHeadless(bot->GetGUID())) + { + SendTradeAnswer(sess, seq, "none"); + return; + } + + if (mine->IsAccepted()) + { + SendTradeAnswer(sess, seq, "already"); + return; + } + + // what the player saw: the bot's side first, then the player's own + if (seen != TradeSideText(theirs) + ":" + TradeSideText(mine) || mine->IsInAcceptProcess() || theirs->IsInAcceptProcess()) + { + TC_LOG_DEBUG("playerbot.v2", "[bot_trade] {} with {}: window changed since the click", player->GetName(), bot->GetName()); + SendTradeAnswer(sess, seq, "changed"); + return; + } + + // The player's own accept with the bot's current state: every other check of a normal accept still runs (distance, money, + // bag room, items still there), and the bot finishes on its side as after a click. + WorldPacket packet(CMSG_ACCEPT_TRADE); + packet << uint32(theirs->GetServerStateIndex()); + WorldPackets::Trade::AcceptTrade accept(std::move(packet)); + accept.Read(); + sess->HandleAcceptTradeOpcode(accept); + SendTradeAnswer(sess, seq, "done"); +} + bool OnAddonWhisper(Player* sender, Player* /*receiver*/, std::string const& prefix, std::string const& body) { diff --git a/src/modules/PlayerbotV2/Session/AddonControl.h b/src/modules/PlayerbotV2/Session/AddonControl.h index 3e12f444c1..606d31d4bc 100644 --- a/src/modules/PlayerbotV2/Session/AddonControl.h +++ b/src/modules/PlayerbotV2/Session/AddonControl.h @@ -39,6 +39,10 @@ constexpr uint32_t kMaxReassemblyChunks = 64; // client-supplied chunk coun bool OnAddonWhisper(Player* sender, Player* receiver, std::string const& prefix, std::string const& body); +// `.bot trade ` from the ForeverBots trade window: accepts a trade with a bot when the window +// holds exactly what the player saw (BotTradeRequest in AddonControl.cpp). The answer is a TRADE_RESP frame. +void BotTradeRequest(Player* player, uint32 seq, std::string const& seen); + // Tear-down hook — reaps the per-account reassembly state and retry spool // when a session logs out. Called from PlayerbotV2 module on OnSessionLogout. void OnSessionLogout(uint32 sessionAccountId);