Code:
-- Choose the best item for this character, otherwise choose the most valuable to vendor:
local mostValuableQuestItemIndex, mostValuableQuestItemValue, bestQuestItemIndex, bestQuestItemArmorWeight = 1, 0, -1, -1
local armorWeights = { Plate = 4, Mail = 2, Leather = 1, Cloth = 0 }
-- Yanked this from LibItemUtils; sucks that we need this lookup table, but GetItemInfo only
-- returns an equipment location, which must first be converted to a slot value that GetInventoryItemLink understands:
local equipmentSlotLookup = {
INVTYPE_HEAD = {"HeadSlot", nil},
INVTYPE_NECK = {"NeckSlot", nil},
INVTYPE_SHOULDER = {"ShoulderSlot", nil},
INVTYPE_CLOAK = {"BackSlot", nil},
INVTYPE_CHEST = {"ChestSlot", nil},
INVTYPE_WRIST = {"WristSlot", nil},
INVTYPE_HAND = {"HandsSlot", nil},
INVTYPE_WAIST = {"WaistSlot", nil},
INVTYPE_LEGS = {"LegsSlot", nil},
INVTYPE_FEET = {"FeetSlot", nil},
INVTYPE_SHIELD = {"SecondaryHandSlot", nil},
INVTYPE_ROBE = {"ChestSlot", nil},
INVTYPE_2HWEAPON = {"MainHandSlot", "SecondaryHandSlot"},
INVTYPE_WEAPONMAINHAND = {"MainHandSlot", nil},
INVTYPE_WEAPONOFFHAND = {"SecondaryHandSlot", "MainHandSlot"},
INVTYPE_WEAPON = {"MainHandSlot","SecondaryHandSlot"},
INVTYPE_THROWN = {"RangedSlot", nil},
INVTYPE_RANGED = {"RangedSlot", nil},
INVTYPE_RANGEDRIGHT = {"RangedSlot", nil},
INVTYPE_FINGER = {"Finger0Slot", "Finger1Slot"},
INVTYPE_HOLDABLE = {"SecondaryHandSlot", "MainHandSlot"},
INVTYPE_TRINKET = {"Trinket0Slot", "Trinket1Slot"}
}
for questItemIndex = 1, numberOfQuestRewards do
local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount,
itemEquipLoc, itemTexture, itemSellPrice = GetItemInfo(GetQuestItemLink("choice", questItemIndex))
local itemId = itemLink:match("|Hitem:(%d+)")
local isItemEquippable = IsEquippableItem(itemId)
local _, _, _, _, isItemUsable = GetQuestItemInfo("choice", questItemIndex)
if itemSellPrice > mostValuableQuestItemValue then
-- Keep track of which item is most valuable:
mostValuableQuestItemIndex = questItemIndex
mostValuableQuestItemValue = itemSellPrice
end
if isItemEquippable == 1 and isItemUsable ~= nil then
-- NPC is offering us an item we can actually wear:
local currentEquippedItemLinksInSlots = {}
local currentWorstEquippedItemInSlot = nil
-- Figure out what we already have equipped:
for _, itemSlot in ipairs(equipmentSlotLookup[itemEquipLoc]) do
if itemSlot ~= nil then
local currentEquippedItemLinkInSlot = GetInventoryItemLink("player", GetInventorySlotInfo(itemSlot))
if currentEquippedItemLinkInSlot == nil then
-- Of the n item slots available, at least one of them has nothing equipped. Ergo, it is the worst:
currentWorstEquippedItemInSlot = nil
break
else
-- There's an item in this slot, get some details on it:
local _, _, _, currentEquippedItemLevelInSlot, _, _, currentEquippedItemSubTypeInSlot = GetItemInfo(currentEquippedItemLinkInSlot)
-- We haven't yet determined the worst item, or the item we see in this slot happens to be worse than the other item
-- we saw in this partner slot (ie. a ring in one slot is worse than a ring in another slot):
if currentWorstEquippedItemInSlot == nil or currentWorstEquippedItemInSlot.itemLevel > currentEquippedItemLevelInSlot then
currentWorstEquippedItemInSlot = {
itemLink = currentEquippedItemLinkInSlot,
itemLevel = currentEquippedItemLevelInSlot,
itemSubType = currentEquippedItemSubTypeInSlot
}
end
end
end
end
if currentWorstEquippedItemInSlot == nil then
-- We're not even wearing an item in this slot, and the vendor has something we can use, take it:
bestQuestItemIndex = questItemIndex
else
if itemLevel > currentWorstEquippedItemInSlot.itemLevel then
-- NPC is providing us with an better item than what we currently have in this slot:
if armorWeights[itemSubType] ~= nil then
-- Armor subtype is one which we care to select based on some priority order:
if armorWeights[itemSubType] > bestQuestItemArmorWeight then
-- If this piece of armor is a better subtype (ie. Plate is better than Cloth if we can wear it):
bestQuestItemIndex = questItemIndex
bestQuestItemArmorWeight = armorWeights[itemSubType]
end
elseif currentWorstEquippedItemInSlot.itemSubType == itemSubType then
-- This isn't a piece of armor (ie. might be a weapon) - only take it if it's the same
-- subtype as the item we are already wearing (if we're wearing a staff, and NPC offers
-- a staff and a dagger, we'll take the staff):
bestQuestItemIndex = questItemIndex
bestQuestItemArmorWeight = -1
end
end
end
end
end
if bestQuestItemIndex < 0 then
-- If we haven't determined an item upgrade by now, just choose the one that we can vendor for the most gold:
bestQuestItemIndex = mostValuableQuestItemIndex
end
GetQuestReward(bestQuestItemIndex)
All yours if you want to make use of it. Only part of that code that isn't mine is the lookup table for equipment locations -> item slots, which I pulled in from LibItemUtils.
Connect With Us