Skip to content

Commit

Permalink
Add full support for multicrafting
Browse files Browse the repository at this point in the history
Override multicraft parameters removed
  • Loading branch information
Dolgubon committed May 2, 2020
1 parent f4a8790 commit 14ab892
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 161 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.zip
188 changes: 112 additions & 76 deletions Enchanting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,55 +49,48 @@ local function copy(t)
end
return a
end

-----------------------------------------------------
-- ENCHANTING USER INTERACTION FUNCTIONS

-- Since bag indexes can change, this ignores those. Instead, it takes in the name, or the index (table of indexes is found in table above, and is specific to this library)
-- Bag indexes will be determined at time of crafting
local function LLC_CraftEnchantingGlyphItemID(self, potencyItemID, essenceItemID, aspectItemID, autocraft, reference, gearRequestTable, quantity, overrideNonMulticraft)
local function LLC_CraftEnchantingGlyphItemID(self, potencyItemID, essenceItemID, aspectItemID, autocraft, reference, gearRequestTable, quantity)
dbug('FUNCTION:LLCEnchantCraft')
if reference == nil then reference = "" end
if not self then d("Please call with colon notation") end
if autocraft==nil then autocraft = self.autocraft end
if not potencyItemID or not essenceItemID or not aspectItemID then d("Missing item Ids") return end
if not areIdsValid(potencyItemID, essenceItemID, aspectItemID) then d("invalid essence Ids") return end
local requestTable = gearRequestTable or {}
local iterations, calculatedQuantity = overrideNonMulticraft and 1 or quantity, overrideNonMulticraft and quantity or 1
if gearRequestTable then
requestTable['dualEnchantingSmithing'] = true
if iterations > 1 or calculatedQuantity > 1 then
d("Invalid attempt to craft multiple glyphs for one item.")
return
end
end

requestTable['glyphInfo'] = requestTable.glyphInfo or {}
requestTable['equipInfo'] = requestTable.equipInfo or {}
requestTable["potencyItemID"] = potencyItemID
requestTable["essenceItemID"] = essenceItemID
requestTable["aspectItemID"] = aspectItemID
requestTable["timestamp"] = requestTable["timestamp"] or GetTimeStamp()
requestTable["timestamp"] = requestTable["timestamp"] or LibLazyCrafting.GetNextQueueOrder()
requestTable["autocraft"] = autocraft
requestTable["Requester"] = self.addonName
requestTable["reference"] = requestTable["reference"] or reference
requestTable["overrideNonMulticraft"] = overrideNonMulticraft
requestTable["quantity"] = calculatedQuantity
requestTable["quantity"] = quantity or 1
requestTable["initialQuantity"] = quantity or 1
if requestTable["station"] then
requestTable["enchantingStation"] = requestTable["station"]
else
requestTable["station"] = CRAFTING_TYPE_ENCHANTING
end

for i = 1, iterations do
table.insert(craftingQueue[self.addonName][CRAFTING_TYPE_ENCHANTING],requestTable)
end
table.insert(craftingQueue[self.addonName][CRAFTING_TYPE_ENCHANTING],requestTable)

--sortCraftQueue()
if GetCraftingInteractionType()==CRAFTING_TYPE_ENCHANTING then
LibLazyCrafting.craftInteract(event, CRAFTING_TYPE_ENCHANTING)
end
return requestTable
end

--/script LLC_Global:CraftEnchantingItemId(45830, 45838, 45851, true, "Hum", nil, 2)
local function LLC_CraftEnchantingGlyph(self, potencyBagId, potencySlot, essenceBagId, essenceSlot, aspectBagId, aspectSlot, autocraft, reference, gearRequestTable)
return LLC_CraftEnchantingGlyphItemID(self, GetItemId(potencyBagId, potencySlot),GetItemId(essenceBagId, essenceSlot),GetItemId(aspectBagId,aspectSlot),autocraft, reference, gearRequestTable)
end
Expand Down Expand Up @@ -267,9 +260,9 @@ local function LLC_CraftEnchantingGlyphAttributes(self, isCP, level, enchantId,
end
end
local aspectId = qualityItemIdInfo[quality]
local a = LLC_CraftEnchantingGlyphItemID(self, potencyId, essenceId, aspectId, autocraft, reference, gearRequestTable)
local a = LLC_CraftEnchantingGlyphItemID(self, potencyId, essenceId, aspectId, autocraft, reference, gearRequestTable, gearRequestTable.smithingQuantity)
return a
-- LLC_CraftEnchantingGlyphItemID(self, GetItemId(potencyBagId, potencySlot),GetItemId(essenceBagId, essenceSlot),GetItemId(aspectBagId,aspectSlot),autocraft, reference)
-- LLC_Global:CraftEnchantingGlyphItemID(self, GetItemId(potencyBagId, potencySlot),GetItemId(essenceBagId, essenceSlot),GetItemId(aspectBagId,aspectSlot),autocraft, reference)
end

LibLazyCrafting.functionTable.CraftEnchantingGlyphByAttributes = LLC_CraftEnchantingGlyphAttributes
Expand Down Expand Up @@ -330,16 +323,19 @@ local function LLC_EnchantingCraftinteraction(station, earliest, addon, position
select(2,findItemLocationById(earliest["aspectItemID"])),
earliest["quantity"]
}
if locations[1] and locations[3] and locations[5] then
local maxCraftable = math.min(earliest["quantity"] or 1, GetMaxIterationsPossibleForEnchantingItem(unpack(locations)))
locations[7] = maxCraftable
if locations[1] and locations[3] and locations[5] and maxCraftable>0 then
dbug("CALL:ZOEnchantCraft")
LibLazyCrafting.isCurrentlyCrafting = {true, "enchanting", earliest["Requester"]}
LibLazyCrafting:setWatchingForNewItems(true)
CraftEnchantingItem(unpack(locations))
currentCraftAttempt= copy(earliest)
currentCraftAttempt.callback = LibLazyCrafting.craftResultFunctions[addon]
currentCraftAttempt.slot = FindFirstEmptySlotInBag(BAG_BACKPACK)
currentCraftAttempt.link = GetEnchantingResultingItemLink(unpack(locations))
currentCraftAttempt.position = position
currentCraftAttempt.timestamp = GetTimeStamp()
currentCraftAttempt.timestamp = earliest.timestamp
currentCraftAttempt.addon = addon
if currentCraftAttempt.link=="" then
-- User doesn't know all the runes. Boooo more work
Expand Down Expand Up @@ -384,82 +380,122 @@ local function applyGlyphToItem(requestTable)
-- if not glyphUniqueId == requestTable.glyphUniqueId and not equipUniqueId == requestTable.equipUniqueId then
-- d("Enchanting failed. Gear and glyph were moved")
-- end
local equipBag , equipSlot = searchUniqueId(requestTable.equipUniqueId)
local glyphBag, glyphSlot = searchUniqueId(requestTable.glyphUniqueId)
if not equipBag or not glyphBag then
if not equipBag then
d("LibLazyCrafting: Could not find crafted gear")
end
if not glyphBag then
d("LibLazyCrafting: Could not find crafted glyph")
local glyphInfo = requestTable.glyphInfo
local equipInfo = requestTable.equipInfo
local numLoops = math.min(#glyphInfo, #equipInfo)
for i = numLoops,1, -1 do

local equipBag , equipSlot = searchUniqueId(equipInfo[i].uniqueId)
local glyphBag, glyphSlot = searchUniqueId(glyphInfo[i].uniqueId)
if not equipBag or not glyphBag then
if not equipBag then
d("LibLazyCrafting: Could not find crafted gear")
end
if not glyphBag then
d("LibLazyCrafting: Could not find crafted glyph")
end
d("LibLazyCrafting: Aborting enchanting")
LibLazyCrafting.SendCraftEvent(LLC_ENCHANTMENT_FAILED, 0, requestTable.Requester, requestTable )
return
end
d("LibLazyCrafting: Aborting enchanting")
LibLazyCrafting.SendCraftEvent(LLC_ENCHANTMENT_FAILED, 0, requestTable.Requester, requestTable )
return
table.remove(glyphInfo)
local equipUniqueId = table.remove(equipInfo).uniqueId
-- Enchanting too many too fast will get you kicked!
zo_callLater(function()
EnchantItem(equipBag, equipSlot, glyphBag , glyphSlot)


zo_callLater( function()
local _,equipSlot = searchUniqueId(equipUniqueId)
LibLazyCrafting:SetItemStatusNew(equipSlot)
end, 550 )

LibLazyCrafting.SendCraftEvent(LLC_CRAFT_SUCCESS, 0, requestTable.Requester, requestTable )
end, (numLoops-i+1)*260

)
currentCraftAttempt = {}
end
lastSlotUsed = glyphSlot
EnchantItem(equipBag, equipSlot, glyphBag , glyphSlot)
-- Set the new gear as new
_,equipSlot = searchUniqueId(requestTable.equipUniqueId)
zo_callLater( function() LibLazyCrafting:SetItemStatusNew(equipSlot) end, 500 )
-- local resultTable =
-- {
-- ["bag"] = BAG_BACKPACK,
-- ["slot"] = currentCraftAttempt.slot,
-- ['link'] = currentCraftAttempt.link,
-- ['uniqueId'] = GetItemUniqueId(BAG_BACKPACK, currentCraftAttempt.slot),
-- ["quantity"] = 1,
-- ["reference"] = removedTable.reference,
-- }

LibLazyCrafting.SendCraftEvent(LLC_CRAFT_SUCCESS, 0, requestTable.Requester, requestTable )
currentCraftAttempt = {}
end


local function LLC_EnchantingCraftingComplete(event, station, lastCheck)
if currentCraftAttempt.allRunesKnown==false then -- User didn't know all the glyphs, so we get the item link *now* since they know all of them.
-- Hopefully they have more than one
currentCraftAttempt.link = GetEnchantingResultingItemLink(unpack(currentCraftAttempt.locations))
end
dbug("EVENT:CraftComplete")
if not currentCraftAttempt.addon then return end
if GetItemLinkName(GetItemLink(BAG_BACKPACK, currentCraftAttempt.slot,0)) == GetItemLinkName(currentCraftAttempt.link)
and GetItemLinkQuality(GetItemLink(BAG_BACKPACK, currentCraftAttempt.slot,0)) == GetItemLinkQuality(currentCraftAttempt.link)
then
-- We found it!
local function wasItemMade(bag, slot)
return GetItemLinkName(GetItemLink(BAG_BACKPACK, slot,0)) == GetItemLinkName(currentCraftAttempt.link)
and GetItemLinkQuality(GetItemLink(BAG_BACKPACK, slot,0)) == GetItemLinkQuality(currentCraftAttempt.link)
end
local function handleEnchantComplete(event, station, slot)
-- We found it!
dbug("ACTION:RemoveQueueItem")
local removedTable = table.remove(craftingQueue[currentCraftAttempt.addon][CRAFTING_TYPE_ENCHANTING] , currentCraftAttempt.position )
local removedTable = craftingQueue[currentCraftAttempt.Requester][CRAFTING_TYPE_ENCHANTING][currentCraftAttempt.position]
if (currentCraftAttempt.quantity or 1) <= 1 then
removedTable = table.remove(craftingQueue[currentCraftAttempt.Requester][CRAFTING_TYPE_ENCHANTING] , currentCraftAttempt.position )
removedTable.quantity = removedTable.quantity - 1
currentCraftAttempt.quantity = currentCraftAttempt.quantity - 1
else
removedTable = craftingQueue[currentCraftAttempt.Requester][CRAFTING_TYPE_ENCHANTING][currentCraftAttempt.position]
removedTable.quantity = removedTable.quantity - 1
currentCraftAttempt.quantity = currentCraftAttempt.quantity - 1
end
-- d(removedTable)
if removedTable.dualEnchantingSmithing then
removedTable.glyphInfo= removedTable.glyphInfo or {}
table.insert(removedTable.glyphInfo,
{
bag=BAG_BACKPACK,
slot=slot,
uniqueId=GetItemUniqueId(BAG_BACKPACK, slot),
uniqueIdString = Id64ToString(GetItemUniqueId(BAG_BACKPACK, slot)),
})
removedTable.glyphBag = BAG_BACKPACK
removedTable.glyphSlot = currentCraftAttempt.slot
removedTable.glyphSlot = slot
removedTable['glyphUniqueId'] = GetItemUniqueId(removedTable.glyphBag, removedTable.glyphSlot)
removedTable['glyphStringUniqueId'] = Id64ToString(removedTable['glyphUniqueId'])
removedTable.glyphCreated = true
currentCraftAttempt = {}
if removedTable.equipCreated then
applyGlyphToItem(removedTable)
return
else
LibLazyCrafting.SendCraftEvent( LLC_INITIAL_CRAFT_SUCCESS, station,removedTable.Requester, removedTable )
return
end
return removedTable
end
--sortCraftQueue()
local resultTable =
{
["bag"] = BAG_BACKPACK,
["slot"] = currentCraftAttempt.slot,
["slot"] = slot,
['link'] = currentCraftAttempt.link,
['uniqueId'] = GetItemUniqueId(BAG_BACKPACK, currentCraftAttempt.slot),
["quantity"] = removedTable.quantity,
["reference"] = removedTable.reference,
["overrideNonMulticraft"] = removedTable.overrideNonMulticraft
}
LibLazyCrafting.SendCraftEvent( LLC_CRAFT_SUCCESS , station, removedTable.Requester , resultTable )
return removedTable
end

local function LLC_EnchantingCraftingComplete(event, station, lastCheck)
if currentCraftAttempt.allRunesKnown==false then -- User didn't know all the glyphs, so we get the item link *now* since now they know them
-- Hopefully they have more than one
currentCraftAttempt.link = GetEnchantingResultingItemLink(unpack(currentCraftAttempt.locations))
end
dbug("EVENT:CraftComplete")
if not currentCraftAttempt.addon then return end
local bag, slot = LibLazyCrafting.findNextSlotIndex(wasItemMade)
local found = false
local removedTable
while slot ~= nil do
removedTable = handleEnchantComplete(event, station, slot)
bag, slot = LibLazyCrafting.findNextSlotIndex(wasItemMade, slot+1)
found = true
end
if found then
if removedTable.equipInfo and #removedTable.equipInfo>0 then
applyGlyphToItem(removedTable)
return
else
local copiedTable = LibLazyCrafting.tableShallowCopy(removedTable)
copiedTable.slot = slot
copiedTable.quantity = 1
LibLazyCrafting.SendCraftEvent(LLC_INITIAL_CRAFT_SUCCESS, station, removedTable.Requester, copiedTable)
return
end
currentCraftAttempt = {}
return
end

elseif lastCheck then
if lastCheck then
-- give up on finding it.
currentCraftAttempt = {}
elseif lastSlotUsed then
Expand Down Expand Up @@ -511,9 +547,9 @@ local function compileGlyphRequirements(self, requestTable, requirements)
requirements = {}
end
end
requirements[requestTable["potencyItemID"]] = 1
requirements[requestTable["essenceItemID"]] = 1
requirements[requestTable["aspectItemID"]] = 1
requirements[requestTable["potencyItemID"]] =1 *requestTable.quantity
requirements[requestTable["essenceItemID"]] =1 *requestTable.quantity
requirements[requestTable["aspectItemID"]] =1 *requestTable.quantity
return requirements
end

Expand Down
49 changes: 47 additions & 2 deletions LibLazyCrafting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function LibLazyCrafting:RegisterWidget(widgetType, widgetVersion)
end
end

local queuePosition = 0
function LibLazyCrafting.GetNextQueueOrder()
queuePosition = queuePosition + 1
return queuePosition
end

-- -- Re initialize crafts if this run of the library overwrote a previous one.
-- if oldVersion then
-- for k, reInitialize in pairs(LLC.widgets.initializers) do
Expand Down Expand Up @@ -308,6 +314,40 @@ function LibLazyCrafting.stackableCraftingComplete(event, station, lastCheck, cr
end
end

LibLazyCrafting.newItemsSeen =
{

}

function LibLazyCrafting:setWatchingForNewItems(state)
self.watchForNewItems = state
LibLazyCrafting.newItemsSeen = {}
end

function LibLazyCrafting.findNextSlotIndex(itemCheck, startSlot)
if startSlot == nil then
startSlot = -1
end
for k, item in pairs(LibLazyCrafting.newItemsSeen) do
if item.slotId>=startSlot and itemCheck(item.bagId, item.slotId) then
table.remove(LibLazyCrafting.newItemsSeen , k)
return item.bagId, item.slotId
end
end
return nil , nil
end

local function newItemWatcher(event, bagId, slotId, isNew, _, inventoryUpdateReason, countChange)
dbug("New item "..GetItemLink(bagId, slotId).." at bagId "..bagId.." and slotId "..slotId)
if LibLazyCrafting.watchForNewItems and isNew then
table.insert(LibLazyCrafting.newItemsSeen, {bagId=bagId, slotId=slotId,countChange=countChange})
end
end


EVENT_MANAGER:RegisterForEvent(LibLazyCrafting.name.."NewItemWatcher", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, newItemWatcher)
EVENT_MANAGER:AddFilterForEvent(LibLazyCrafting.name.."NewItemWatcher", EVENT_INVENTORY_SINGLE_SLOT_UPDATE, REGISTER_FILTER_IS_NEW_ITEM, true)

local function getItemLinkFromItemId(itemId) local name = GetItemLinkName(ZO_LinkHandler_CreateLink("Test Trash", nil, ITEM_LINK_TYPE,itemId, 1, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 10000, 0))
return ZO_LinkHandler_CreateLink(zo_strformat("<<t:1>>",name), nil, ITEM_LINK_TYPE,itemId, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
end
Expand Down Expand Up @@ -492,6 +532,9 @@ local function LLC_GetMatRequirements(self, requestTable)
if requestTable.station then
return LibLazyCrafting.craftInteractionTables[requestTable.station]:materialRequirements( requestTable)
end
if requestTable.dualEnchantingSmithing then
return LibLazyCrafting.craftInteractionTables[CRAFTING_TYPE_ENCHANTING]:materialRequirements( requestTable)
end
end

LibLazyCrafting.functionTable.getMatRequirements = LLC_GetMatRequirements
Expand All @@ -510,7 +553,6 @@ function LibLazyCrafting.SendCraftEvent( event, station, requester, returnTable
-- SHARED_INVENTORY:RefreshStatusSortOrder(v)
-- end
-- end

if event == LLC_NO_FURTHER_CRAFT_POSSIBLE then
for requester, callbackFunction in pairs(LibLazyCrafting.craftResultFunctions) do
if requester ~= "LLC_Global" then
Expand Down Expand Up @@ -611,7 +653,9 @@ end

function LibLazyCrafting:SetItemStatusNew(itemSlot)
-- d(itemSlot)
local v = PLAYER_INVENTORY.inventories[1].slots[1] [itemSlot]
-- PLAYER_INVENTORY:RefreshInventorySlot(1, itemSlot, BAG_BACKPACK)
local v = PLAYER_INVENTORY:GetBackpackItem(itemSlot)

if v then
v.brandNew = true
v.age = 1
Expand Down Expand Up @@ -675,6 +719,7 @@ local function endInteraction(event, station)
end
end
LLC_StopCraftAllItems()
LibLazyCrafting:setWatchingForNewItems(false)
end

-- Called when a crafting request is done.
Expand Down
Loading

0 comments on commit 14ab892

Please sign in to comment.