From b91f6a8e15ee4c2f60aa49578b88ff5a59726fa1 Mon Sep 17 00:00:00 2001 From: avirar Date: Thu, 3 Oct 2024 12:28:44 +1000 Subject: [PATCH] Add files via upload --- src/strategy/actions/OpenItemAction.h | 20 ++++++ src/strategy/actions/OpenItemAction_v2.cpp | 74 ++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/strategy/actions/OpenItemAction.h create mode 100644 src/strategy/actions/OpenItemAction_v2.cpp diff --git a/src/strategy/actions/OpenItemAction.h b/src/strategy/actions/OpenItemAction.h new file mode 100644 index 00000000..2b3383de --- /dev/null +++ b/src/strategy/actions/OpenItemAction.h @@ -0,0 +1,20 @@ +#ifndef _PLAYERBOT_OPENITEMACTION_H +#define _PLAYERBOT_OPENITEMACTION_H + +#include "Action.h" +#include "Item.h" + +class PlayerbotAI; + +class OpenItemAction : public Action +{ +public: + OpenItemAction(PlayerbotAI* botAI) : Action(botAI, "open item") {} + + bool Execute(Event event) override; + +private: + bool CanOpenItem(Item* item); +}; + +#endif diff --git a/src/strategy/actions/OpenItemAction_v2.cpp b/src/strategy/actions/OpenItemAction_v2.cpp new file mode 100644 index 00000000..2ffa265a --- /dev/null +++ b/src/strategy/actions/OpenItemAction_v2.cpp @@ -0,0 +1,74 @@ +#include "OpenItemAction.h" +#include "PlayerbotAI.h" +#include "ItemTemplate.h" +#include "WorldPacket.h" +#include "Player.h" +#include "ObjectMgr.h" + +bool OpenItemAction::Execute(Event event) +{ + bool foundOpenable = false; + + // Check main inventory slots + for (uint8 slot = EQUIPMENT_SLOT_START; slot < INVENTORY_SLOT_ITEM_END; ++slot) + { + Item* item = bot->GetItemByPos(INVENTORY_SLOT_BAG_0, slot); + + if (item && CanOpenItem(item)) + { + OpenItem(item, INVENTORY_SLOT_BAG_0, slot); + foundOpenable = true; + } + } + + // Check items in the bags + for (uint8 bag = INVENTORY_SLOT_BAG_START; bag < INVENTORY_SLOT_BAG_END; ++bag) + { + Bag* bagItem = bot->GetBagByPos(bag); + if (!bagItem) + continue; + + for (uint32 slot = 0; slot < bagItem->GetBagSize(); ++slot) + { + Item* item = bot->GetItemByPos(bag, slot); + + if (item && CanOpenItem(item)) + { + OpenItem(item, bag, slot); + foundOpenable = true; + } + } + } + + // If no openable items found + if (!foundOpenable) + { + botAI->TellError("No openable items in inventory."); + } + + return foundOpenable; +} + +bool OpenItemAction::CanOpenItem(Item* item) +{ + if (!item) + return false; + + ItemTemplate const* itemTemplate = item->GetTemplate(); + if (!itemTemplate) + return false; + + // Check if the item has the openable flag + return itemTemplate->Flags & ITEM_FLAG_HAS_LOOT; +} + +void OpenItemAction::OpenItem(Item* item, uint8 bag, uint8 slot) +{ + WorldPacket packet(CMSG_OPEN_ITEM); + packet << bag << slot; + bot->GetSession()->HandleOpenItemOpcode(packet); + + std::ostringstream out; + out << "Opened item: " << item->GetTemplate()->Name1; + botAI->TellMaster(out.str()); +}