View Single Post
06-22-19, 07:59 AM   #1
DashingSplash
A Murloc Raider
Join Date: Jun 2019
Posts: 7
(Classic) Optimizing table search for auto dismount addon

Hi,

Been working on a new version of my Classic dismount addon; mostly, because it requires two button presses to activate an ability. In this version I register an OnKeyDown event and call for Dismount(). This works perfectly well in retail (BfA), but when my friend tries it out on the Classic Beta it requires two button presses for most abilities (excluding grenades, distract, and similar abilities that works fine). The only difference between the two of us is that I am working with a latency of 40 ms, and he around 120 ms. As such, I execute the OnKeyDown event in 1 ms and he around 3 ms.

Disclaimer: to try this addon out for yourself on retail you need to disable auto dismount, and use a flying mount (and be in the air).

Toc file:
Code:
## Interface: 80100
## Title: dashingDismount
## Notes: Dismount
## Author: DashingSplash
dashingDismount.lua
Lua file:
Code:
local dashingDismount = CreateFrame("Frame", nil, UIParent)
local dashingSplash = CreateFrame("Frame", nil, UIParent)

local table_1 = {t = {}, st = {}, ct = {}, at = {}}
local table_2 = {t2 = {}, st2 = {}, ct2 = {}, at2 = {}}
local key1, key2
local mod

dashingDismount:SetScript("OnKeyDown", function(self, key, ...)
	local beginTime = debugprofilestop()
	
	if IsMounted() then
		if GetActionBarPage() ~= 2 then
			if not IsModifierKeyDown() and tContains(table_1.t, key) then Dismount()
			
			elseif IsShiftKeyDown() and tContains(table_1.st, key) then Dismount()
			
			elseif IsControlKeyDown() and tContains(table_1.ct, key) then Dismount()
			
			elseif IsAltKeyDown() and tContains(table_1.at, key) then Dismount() end
		else
			if not IsModifierKeyDown() and tContains(table_2.t2, key) then Dismount()

			elseif IsShiftKeyDown() and tContains(table_2.st2, key) then Dismount()
			
			elseif IsControlKeyDown() and tContains(table_2.ct2, key) then Dismount()
			
			elseif IsAltKeyDown() and tContains(table_2.at2, key) then Dismount() end
		end
	end
	
	local timeUsed = debugprofilestop() - beginTime
	
	print(timeUsed)
end)

local function dashingKey(key)
	if GetActionBarPage() ~= 2 then
		if string.find(key, "SHIFT-") ~= nil then
			table_1.st[#table_1.st+1] = string.sub(key, 7)
		elseif string.find(key1, "CTRL-") ~= nil then
			table_1.ct[#table_1.ct+1] = string.sub(key, 6)
		elseif string.find(key1, "ALT-") ~= nil then
			table_1.at[#table_1.at+1] = string.sub(key, 5)
		else
			table_1.t[#table_1.t+1] = key
		end 
	else
		if string.find(key, "SHIFT-") ~= nil then
			table_2.st2[#table_2.st2+1] = string.sub(key, 7)
		elseif string.find(key1, "CTRL-") ~= nil then
			table_2.ct2[#table_2.ct2+1] = string.sub(key, 6)
		elseif string.find(key1, "ALT-") ~= nil then
			table_2.at2[#table_2.at2+1] = string.sub(key, 5)
		else
			table_2.t2[#table_2.t2+1] = key
		end 
	end
end

local function dashingBindings()
	local button, buttonName
	local slot
	local actionName
    local actionType, id
	
	local ActionBars		= {'Action', 'Action', 'Stance', 'MultiBarBottomLeft', 'MultiBarBottomRight', 'MultiBarRight', 'MultiBarLeft'}
	local ActionBarsBinding = {'ACTION', 'ACTION', 'SHAPESHIFT', 'MULTIACTIONBAR1', 'MULTIACTIONBAR2', 'MULTIACTIONBAR3', 'MULTIACTIONBAR4'}
	local protectedSkills	= {"Alchemy", "Cooking", "Enchanting", "Engineering", "Blacksmithing", "Tailoring", "First Aid"}

    for page, barName in ipairs(ActionBars) do	
	
		if page == 2 then 
			ChangeActionBarPage(2)
		else 
			ChangeActionBarPage(1)
		end
		
        for i = 1, 12 do
            button		= _G[barName .. 'Button' .. i]		
			buttonName	= ActionBarsBinding[page] .. "BUTTON" .. i
			
			if button ~= nil then
				slot = ActionButton_GetPagedID(button) or ActionButton_CalculateAction(button) or button:GetAttribute('action') or 0
				
				if HasAction(slot) then
					actionType, id	= GetActionInfo(slot)
					key1, key2		= GetBindingKey(buttonName)
					actionName		= GetSpellInfo(id)
					
					if not tContains(protectedSkills, actionName) then
						if key1 ~= nil then
							print(key1)
							dashingKey(key1)
						end
						if key2 ~= nil then
							print(key2)
							dashingKey(key2)
						end
					end	
				end
			end
        end
    end
end

local function dashingUpdate(self, event, ...)
	for _, tableWipe in pairs(table_1) do
		wipe(tableWipe)	
	end
	
	for _, tableWipe in pairs(table_2) do
		wipe(tableWipe)	
	end

	dashingBindings()
end

dashingSplash:RegisterEvent("MODIFIER_STATE_CHANGED")
dashingSplash:SetScript("OnEvent", function(self, event, key, state)
	if state == 1 then
		mod = string.sub(key, 2)
		print(mod)
	else
		mod = 'nil'
		print(mod)
	end
end)

dashingDismount:Show()
dashingDismount:EnableKeyboard(true)
dashingDismount:SetPropagateKeyboardInput(true)

dashingDismount:RegisterEvent("PLAYER_LOGIN")
dashingDismount:RegisterEvent("UPDATE_BINDINGS")
--dashingDismount:RegisterEvent("ACTIONBAR_SLOT_CHANGED")
dashingDismount:SetScript("OnEvent", dashingUpdate)
Note: not using dashingSplash:RegisterEvent("MODIFIER_STATE_CHANGED") at this moment for anything. Leaving it in since the question that follows might give inspiration for solutions.

My question is the following: how can I make the following code more efficient? If you have any input regarding the rest of the addon feel free to provide constructive criticism.
Code:
dashingDismount:SetScript("OnKeyDown", function(self, key, ...)
	local beginTime = debugprofilestop()
	
	if IsMounted() then
		if GetActionBarPage() ~= 2 then
			if not IsModifierKeyDown() and tContains(table_1.t, key) then Dismount()
			
			elseif IsShiftKeyDown() and tContains(table_1.st, key) then Dismount()
			
			elseif IsControlKeyDown() and tContains(table_1.ct, key) then Dismount()
			
			elseif IsAltKeyDown() and tContains(table_1.at, key) then Dismount() end
		else
			if not IsModifierKeyDown() and tContains(table_2.t2, key) then Dismount()

			elseif IsShiftKeyDown() and tContains(table_2.st2, key) then Dismount()
			
			elseif IsControlKeyDown() and tContains(table_2.ct2, key) then Dismount()
			
			elseif IsAltKeyDown() and tContains(table_2.at2, key) then Dismount() end
		end
	end
	
	local timeUsed = debugprofilestop() - beginTime
	
	print(timeUsed)
end)

Last edited by DashingSplash : 06-22-19 at 08:01 AM.
  Reply With Quote