Skip to content

Commit

Permalink
NPCBots: Fix equipment not being returned and items not being retriev…
Browse files Browse the repository at this point in the history
…able from mail at bot's expiration
  • Loading branch information
trickerer committed May 7, 2024
1 parent eb13d77 commit f758b14
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/server/game/AI/NpcBots/bot_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void bot_ai::CheckOwnerExpiry()
if (!BotMgr::GetOwnershipExpireTime())
return; //disabled

if (IsTempBot())
if (IsTempBot() || !IAmFree())
return;

NpcBotData const* npcBotData = BotDataMgr::SelectNpcBotData(me->GetEntry());
Expand Down Expand Up @@ -512,7 +512,7 @@ void bot_ai::CheckOwnerExpiry()
// "FROM item_instance WHERE guid IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", CONNECTION_SYNCH

for (uint8 i = 0; i != BOT_INVENTORY_SIZE; ++i)
stmt->SetData(i, npcBotData->equips[i]);
stmt->SetData(i, npcBotData->equips[i] ? npcBotData->equips[i] : std::numeric_limits<uint32>::max());

PreparedQueryResult iiresult = CharacterDatabase.Query(stmt);
if (iiresult)
Expand All @@ -524,9 +524,21 @@ void bot_ai::CheckOwnerExpiry()
Field* fields2 = iiresult->Fetch();
uint32 itemGuidLow = fields2[11].Get<uint32>();
uint32 itemId = fields2[12].Get<uint32>();
Item* item = new Item;
ASSERT(item->LoadFromDB(itemGuidLow, ownerGuid, fields2, itemId));
items.push_back(item);
uint8 item_idx = std::numeric_limits<uint8>::max();

for (uint8 i = 0; i != BOT_INVENTORY_SIZE; ++i)
{
if (_equips[i] && _equips[i]->GetEntry() == itemId)
{
item_idx = i;
break;
}
}
if (item_idx >= BOT_INVENTORY_SIZE)
LOG_ERROR("npcbots", "bot_ai::CheckOwnerExpiry(): item id {} guid {} not found in bot's inventory!\n{}", itemId, itemGuidLow, me->GetGUID().ToString());

items.push_back(_equips[item_idx]);
_removeEquipment(item_idx);

} while (iiresult->NextRow());

Expand Down Expand Up @@ -12375,6 +12387,28 @@ bool bot_ai::_canEquip(ItemTemplate const* newProto, uint8 slot, bool ignoreItem
return false;
}

void bot_ai::_removeEquipment(uint8 slot)
{
Item* item = _equips[slot];
if (!item)
return; //already unequipped

_usableItemSlotsMask &= ~(1ul << slot);

RemoveItemBonuses(slot);
ApplyItemSetBonuses(item, false);

if (slot == BOT_SLOT_OFFHAND)
{
if (me->CanDualWield())
me->SetCanDualWield(false);
if (!(me->GetCreatureTemplate()->flags_extra & CREATURE_FLAG_EXTRA_NO_BLOCK))
const_cast<CreatureTemplate*>(me->GetCreatureTemplate())->flags_extra |= CREATURE_FLAG_EXTRA_NO_BLOCK;
}

_equips[slot] = nullptr;
}

bool bot_ai::_unequip(uint8 slot, ObjectGuid receiver)
{
EquipmentInfo const* einfo = BotDataMgr::GetBotEquipmentInfo(me->GetEntry());
Expand All @@ -12386,10 +12420,7 @@ bool bot_ai::_unequip(uint8 slot, ObjectGuid receiver)

uint32 itemId = item->GetEntry();

_usableItemSlotsMask &= ~(1ul << slot);

RemoveItemBonuses(slot);
ApplyItemSetBonuses(item, false);
_removeEquipment(slot);

//hand old weapon to master
if (receiver && (slot > BOT_SLOT_RANGED || einfo->ItemEntry[slot] != itemId))
Expand Down Expand Up @@ -12451,16 +12482,6 @@ bool bot_ai::_unequip(uint8 slot, ObjectGuid receiver)
}

_updateEquips(slot, nullptr);

//offhand check
if (slot == BOT_SLOT_OFFHAND)
{
if (me->CanDualWield())
me->SetCanDualWield(false);
if (!(me->GetCreatureTemplate()->flags_extra & CREATURE_FLAG_EXTRA_NO_BLOCK))
const_cast<CreatureTemplate*>(me->GetCreatureTemplate())->flags_extra |= CREATURE_FLAG_EXTRA_NO_BLOCK;
}

return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/server/game/AI/NpcBots/bot_ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class bot_ai : public CreatureAI
bool _canUseRanged() const;
bool _canUseRelic() const;
bool _canEquip(ItemTemplate const* newProto, uint8 slot, bool ignoreItemLevel, Item const* newItem = nullptr) const;
void _removeEquipment(uint8 slot);
bool _unequip(uint8 slot, ObjectGuid receiver);
bool _equip(uint8 slot, Item* newItem, ObjectGuid receiver);
bool _resetEquipment(uint8 slot, ObjectGuid receiver);
Expand Down

0 comments on commit f758b14

Please sign in to comment.