108 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
local addonname, shared = ...
 | 
						|
---@cast shared HeimdallShared
 | 
						|
---@cast addonname string
 | 
						|
local ModuleName = "Macroer"
 | 
						|
 | 
						|
---@diagnostic disable-next-line: missing-fields
 | 
						|
shared.Macroer = {}
 | 
						|
function shared.Macroer.Init()
 | 
						|
    ---@class stinky
 | 
						|
    ---@field name string
 | 
						|
    ---@field class string
 | 
						|
    ---@field seenAt number
 | 
						|
    ---@field hostile boolean
 | 
						|
 | 
						|
    local function FindOrCreateMacro(macroName)
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Finding or creating macro: %s", ModuleName, macroName))
 | 
						|
        end
 | 
						|
        local idx = GetMacroIndexByName(macroName)
 | 
						|
        if idx == 0 then
 | 
						|
            if Heimdall_Data.config.macroer.debug then
 | 
						|
                print(string.format("[%s] Creating new macro: %s", ModuleName, macroName))
 | 
						|
            end
 | 
						|
            CreateMacro(macroName, "INV_Misc_QuestionMark", "")
 | 
						|
        end
 | 
						|
        idx = GetMacroIndexByName(macroName)
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Macro index: %d", ModuleName, idx))
 | 
						|
        end
 | 
						|
        return idx
 | 
						|
    end
 | 
						|
 | 
						|
    ---@param stinkies table<string, stinky>
 | 
						|
    local function FixMacro(stinkies)
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Fixing macro with %d stinkies", ModuleName, #stinkies))
 | 
						|
        end
 | 
						|
        if not Heimdall_Data.config.macroer.enabled then
 | 
						|
            if Heimdall_Data.config.macroer.debug then
 | 
						|
                print(string.format("[%s] Module disabled, skipping macro update", ModuleName))
 | 
						|
            end
 | 
						|
            return
 | 
						|
        end
 | 
						|
        if InCombatLockdown() then
 | 
						|
            if Heimdall_Data.config.macroer.debug then
 | 
						|
                print(string.format("[%s] In combat, skipping macro update", ModuleName))
 | 
						|
            end
 | 
						|
            return
 | 
						|
        end
 | 
						|
 | 
						|
        local priorityMap = {}
 | 
						|
        for priority, className in ipairs(Heimdall_Data.config.macroer.priority) do
 | 
						|
            priorityMap[className] = priority
 | 
						|
        end
 | 
						|
        local minPriority = #Heimdall_Data.config.macroer.priority + 1
 | 
						|
 | 
						|
        local sortedStinkies = {}
 | 
						|
        for _, stinky in pairs(stinkies) do
 | 
						|
            if not Heimdall_Data.config.agents[stinky.name] then
 | 
						|
                sortedStinkies[#sortedStinkies + 1] = stinky
 | 
						|
            end
 | 
						|
        end
 | 
						|
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Processing %d non-agent stinkies", ModuleName, #sortedStinkies))
 | 
						|
        end
 | 
						|
 | 
						|
        table.sort(sortedStinkies, function(a, b)
 | 
						|
            local aPriority = priorityMap[a.class] or minPriority
 | 
						|
            local bPriority = priorityMap[b.class] or minPriority
 | 
						|
            return aPriority > bPriority
 | 
						|
        end)
 | 
						|
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Sorted stinkies: %d", ModuleName, #sortedStinkies))
 | 
						|
            shared.dumpTable(sortedStinkies)
 | 
						|
        end
 | 
						|
        local lines = { "/targetenemy" }
 | 
						|
        for _, stinky in pairs(sortedStinkies) do
 | 
						|
            if stinky.seenAt > GetTime() - 600 then
 | 
						|
                if Heimdall_Data.config.macroer.debug then
 | 
						|
                    print(string.format("[%s] Adding target macro for: %s", ModuleName, stinky.name))
 | 
						|
                end
 | 
						|
                lines[#lines + 1] = string.format("/tar %s", stinky.name)
 | 
						|
            end
 | 
						|
        end
 | 
						|
 | 
						|
        local idx = FindOrCreateMacro("HeimdallTarget")
 | 
						|
        local body = strjoin("\n", unpack(lines))
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Updating macro with %d lines", ModuleName, #lines))
 | 
						|
        end
 | 
						|
        EditMacro(idx, "HeimdallTarget", "INV_Misc_QuestionMark", body)
 | 
						|
    end
 | 
						|
 | 
						|
    shared.stinkyTracker.stinkies:onChange(function(value)
 | 
						|
        if Heimdall_Data.config.macroer.debug then
 | 
						|
            print(string.format("[%s] Stinkies changed, updating macro", ModuleName))
 | 
						|
        end
 | 
						|
        FixMacro(value)
 | 
						|
    end)
 | 
						|
 | 
						|
    if Heimdall_Data.config.macroer.debug then
 | 
						|
        print(string.format("[%s] Module initialized", ModuleName))
 | 
						|
    end
 | 
						|
    print("[Heimdall] Macroer loaded")
 | 
						|
end
 |