Quantcast

Fix for mainhand/offhand code, and added a debug frame

James Whitehead II [08-17-08 - 10:20]
Fix for mainhand/offhand code, and added a debug frame
Filename
DressToKill.lua
DressToKillOptions.lua
diff --git a/DressToKill.lua b/DressToKill.lua
index ce38e62..406b6f9 100644
--- a/DressToKill.lua
+++ b/DressToKill.lua
@@ -6,15 +6,16 @@ local L = DressToKillLocals

 local DEBUG = false
 local function debug(fmt, ...)
+	local msg = "|cffffff78DressToKill:|r "
+	if select("#", ...) > 0 then
+		msg = msg .. fmt:format(...)
+	else
+		msg = msg .. fmt
+	end
 	if DEBUG then
-		local msg = "|cffffff78DressToKill:|r "
-		if select("#", ...) > 0 then
-			msg = msg .. fmt:format(...)
-		else
-			msg = msg .. fmt
-		end
 		ChatFrame1:AddMessage(msg)
 	end
+	DressToKillDebugScrollFrame:AddMessage(msg)
 end

 function DressToKill:SetDebug(value)
@@ -96,82 +97,115 @@ function DressToKill:FindEmptySlot()
 end

 local function scanFunction(weightFunction)
-	debug(L["Beginning an inventory scan"])
+	debug(L["********** Beginning an inventory scan **********"])
 	UIErrorsFrame:AddMessage("Please do not change buffs or forms during scan...", 1, 0.2, 0.2)

 	local blacklist = {}
 	local slotAvail = {}
 	for slotName,slotId in pairs(slotIds) do
+		debug(L["Collecting available items for %s"], slotName)
 		local avail = GetInventoryItemsForSlot(slotId)
 		if next(avail) then
+			local c = 0
+			for k,v in pairs(avail) do c = c + 1 end
+			debug(L["Found %d available items"], c)
 			slotAvail[slotId] = avail
+		else
+			debug(L["Found no available items"])
 		end
 	end

 	-- Let's handle the weapons, since they're somewhat complicated
-	local mh_stash, oh_stash = {}, {}
-	local mainslot, offslot = slotIds["MainHandSlot"], slotIds["SecondaryHandSlot"]
+	local mainslot = GetInventorySlotInfo("MainHandSlot")
+	local offslot = GetInventorySlotInfo("SecondaryHandSlot")

 	-- Unequip both weapon slots
 	local mh_link = GetInventoryItemLink("player", mainslot)
 	local oh_link = GetInventoryItemLink("player", offslot)
+
+	debug(L["Mainhand link: %s"], tostring(mh_link))
+	debug(L["Offhand link: %s"], tostring(oh_link))
+
 	if mh_link then
+		debug(L["Found a mainhand weapon: %s"], mh_link)
 		local mh_bag, mh_slot = DressToKill:FindEmptySlot()
 		if not mh_bag then
 			print(L["Out of inventory space, cannot proceed"])
 			return
 		end
-		mh_stash.bag = mh_bag
-		mh_stash.slot = mh_slot
+		debug(L["Unequipping the mainhand weapon"])
+		PickupInventoryItem(mainslot)
+		PickupContainerItem(mh_bag, mh_slot)
+		coroutine.yield()
 	end

 	if oh_link then
+		debug(L["Found an offhand weapon: %s"], oh_link)
 		local oh_bag, oh_slot = DressToKill:FindEmptySlot()
 		if not oh_bag then
 			print(L["Out of inventory space, cannot proceed"])
 			return
 		end
-		oh_stash.bag = oh_bag
-		oh_stash.slot = oh_slot
+		debug(L["Unequipping the offhand weapon"])
+		PickupInventoryItem(offslot)
+		PickupContainerItem(oh_bag, oh_slot)
+		coroutine.yield()
 	end

+	local mh_avail = GetInventoryItemsForSlot(mainslot)
+	local oh_avail = GetInventoryItemsForSlot(offslot)
+
 	-- Calculate weapon base score with nothing equipped
 	local linen_shirt = "\124cffffffff\124Hitem:2576:0:0:0:0:0:0:0\124h[White Linen Shirt]\124h\124r"
-	local weapon_base = weightFunction(linen_shirt, mainslot) + weightFunction(linen_shirt, offslot)
+	local mh_base = weightFunction(linen_shirt, mainslot) or 0
+	local oh_base = mh_base
 	local weapon_max, weapon_win = 0, {}

+	debug(L["Weapon base score: %s"], mh_base)
+
 	-- Try on each mainhand weapon
-	for mh_mask,item in pairs(slotAvail[mainslot]) do
-		local mh_equipped = DressToKill:EquipItem(mainslot, mh_mask, mh_stash)
+	for mh_mask,item in pairs(mh_avail) do
+		local name, link = GetItemInfo(item)
+		debug(L["Equipping mainhand weapon: %s"], link)
+
+		local mh_equipped = DressToKill:EquipItem(mainslot, mh_mask)
 		blacklist[mh_mask] = true

 		if mh_equipped then
+			debug(L["Successfully equipped %s"], link)
 			local mh_link = GetInventoryItemLink("player", mainslot)
-			local mh_score = weightFunction(mh_link, mainslot) - weapon_base
+			local mh_score = weightFunction(mh_link, mainslot) - mh_base
 			local score = mh_score
+			debug(L["Mainhand score: %d"], score)

 			if not IsEquippedItemType("Two-Hand") then
+				debug(L["This is a one hand weapon, try offhand weapons"])
 				-- Try to equip each off-hand weapon to compliment this one
-				for oh_mask,item in pairs(slotAvail[offslot]) do
-					local oh_equipped = DressToKill:EquipItem(offslot, oh_mask, oh_stash)
-
-					-- If we equipped offhand, then score it too
-					if oh_equipped then
-						local oh_link = GetInventoryItemLink("player", offslot) or linen_shirt
-						local oh_score = weightFunction(oh_link, offslot) - weapon_base
-						score = score + oh_score
-					end
-
-					debug("Got score of %s for %s and %s", score, mh_link, oh_link or "empty")
-					if score >= weapon_max then
-						weapon_max = score
-						weapon_win.mh = mh_mask
-						weapon_win.oh = oh_equipped and oh_mask or nil
-					end
-
-					-- Unequip the offhand item
-					if oh_equipped then
-						DressToKill:UnequipItem(offslot, oh_mask, oh_stash)
+				for oh_mask,item in pairs(oh_avail) do
+					if not blacklist[oh_mask] then
+						local name, link = GetItemInfo(item)
+
+						debug(L["Equipping offhand weapon: %s"], link)
+						local oh_equipped = DressToKill:EquipItem(offslot, oh_mask)
+
+						-- If we equipped offhand, then score it too
+						if oh_equipped then
+							local oh_link = GetInventoryItemLink("player", offslot) or linen_shirt
+							local oh_score = weightFunction(oh_link, offslot) - score
+							score = score + oh_score
+						end
+
+						debug("Got score of %s for %s and %s", score, mh_link, oh_link or "empty")
+						if score >= weapon_max then
+							weapon_max = score
+							weapon_win.mh = mh_mask
+							weapon_win.oh = oh_equipped and oh_mask or nil
+						end
+
+						-- Unequip the offhand item
+						if oh_equipped then
+							DressToKill:UnequipItem(offslot, oh_mask, oh_stash)
+						end
 					end
 				end
 			else
@@ -193,9 +227,11 @@ local function scanFunction(weightFunction)
 	DressToKill:EquipItem(mainslot, weapon_win.mh, mh_stash)
 	local link = GetInventoryItemLink("player", mainslot)
 	print("Choosing %s", link)
+	debug("Choosing %s", link)
 	blacklist[weapon_win.mh] = true
+
 	-- Equip the offhand winner, if there was one
-	if weapon_min.oh then
+	if weapon_win.oh then
 		DressToKill:EquipItem(offslot, weapon_win.oh, oh_stash)
 		local link = GetInventoryItemLink("player", offslot)
 		print("Choosing %s", link)
@@ -256,6 +292,7 @@ local function scanFunction(weightFunction)
 	end
 	UIErrorsFrame:AddMessage("Scan complete!", 0.2, 1, 0.2)
 	print("Evaluation complete!")
+	debug(L["Evaluation complete!"])
 end

 function DressToKill:EquipItem(slotId, mask, stash)
@@ -307,6 +344,10 @@ local function OnEvent(self, event, arg1)
 		self.equipped = false
 		debug(L["Clearing item we don't have the proficiency for"])
 		ClearCursor()
+	elseif event == "UI_ERROR_MESSAGE" and arg1 == ERR_2HSKILLNOTFOUND then
+		self.equipped = false
+		debug(L["Clearing item since we lack dual wielding skill"])
+		ClearCursor()
 	end

 	local thread = DressToKill.currentThread
@@ -336,6 +377,11 @@ SlashCmdList["DRESSTOKILL"] = function(msg, editbox)
 		return
 	end

+	if msg and msg:lower():match("%s*debug%s*") then
+		InterfaceOptionsFrame_OpenToFrame(DressToKillDebugFrame)
+		return
+	end
+
 	-- Check to see if a function was specified on the commandline
 	local weightName = msg:match("^%s*(.+)$")
 	if not weightName then
@@ -389,6 +435,9 @@ local bagMap = {
 	[0x800] = 4,
 }
 function DressToKill:ItemInBag(mask)
+	if not mask then
+		debug(debugstack())
+	end
 	if bit.band(mask, ITEM_INVENTORY_BAGS) > 0 then
 		local bag = bagMap[bit.band(mask, MASK_BAG)]
 		local slot = bit.band(mask, MASK_SLOT)
diff --git a/DressToKillOptions.lua b/DressToKillOptions.lua
index 9cb198c..697a35d 100644
--- a/DressToKillOptions.lua
+++ b/DressToKillOptions.lua
@@ -260,3 +260,45 @@ end )

 InterfaceOptions_AddCategory(frame)

+local debugframe = CreateFrame("Frame", "DressToKillDebugFrame", UIParent)
+debugframe.name = L["Debug Log"]
+debugframe.parent = L["Dress to Kill"]
+debugframe:Hide()
+debugframe:SetScript("OnShow", function(frame)
+	debugframe:SetScript("OnShow", nil)
+end)
+
+local scrollframe = CreateFrame("ScrollingMessageFrame", "DressToKillDebugScrollFrame", debugframe)
+scrollframe:SetPoint("TOPLEFT", 10, -10)
+scrollframe:SetPoint("BOTTOMRIGHT", -10, 10)
+scrollframe:SetFontObject("ChatFontNormal")
+scrollframe:SetJustifyH("LEFT")
+scrollframe:SetFading(false)
+scrollframe:EnableMouseWheel(true)
+scrollframe:SetInsertMode("BOTTOM")
+scrollframe:SetMaxLines(99999)
+local function scroll(self, delta)
+	if delta > 0 then
+		if IsShiftKeyDown() then
+			self:ScrollToTop()
+		elseif IsControlKeyDown() then
+			self:PageUp()
+		else
+			self:ScrollUp()
+		end
+	elseif delta < 0 then
+		if IsShiftKeyDown() then
+			self:ScrollToBottom()
+		elseif IsControlKeyDown() then
+			self:PageDown()
+		else
+			self:ScrollDown()
+		end
+	end
+end
+scrollframe:SetScript("OnHyperlinkClick", function(self, ...)
+	ChatFrame_OnHyperlinkShow(...)
+end)
+scrollframe:SetScript("OnMouseWheel", scroll)
+
+InterfaceOptions_AddCategory(debugframe)