<commit_before>--[[--------------------------------------------------------------------
    Ovale Spell Priority
    Copyright (C) 2012, 2013 Sidoine, <NAME>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License in the LICENSE
    file accompanying this program.
----------------------------------------------------------------------]]

local _, Ovale = ...
local OvaleBestAction = {}
Ovale.bestActionModule = OvaleBestAction

--<private-static-properties>
local OvaleActionBar = Ovale:GetModule("OvaleActionBar")
local OvaleCondition = Ovale.conditionModule
local OvaleData = Ovale:GetModule("OvaleData")
local OvalePaperDoll = Ovale:GetModule("OvalePaperDoll")
local OvaleStance = Ovale:GetModule("OvaleStance")
local OvaleState = Ovale.stateModule

local floor, ipairs, loadstring, pairs = math.floor, ipairs, loadstring, pairs
local strfind, tonumber, tostring = string.find, tonumber, tostring
local GetActionCooldown, GetActionTexture, GetInventorySlotInfo = GetActionCooldown, GetActionTexture, GetInventorySlotInfo
local GetItemIcon, GetItemCooldown, GetItemSpell, GetSpellInfo = GetItemIcon, GetItemCooldown, GetItemSpell, GetSpellInfo
local GetSpellTexture, IsActionInRange, IsCurrentAction = GetSpellTexture, IsActionInRange, IsCurrentAction
local IsItemInRange, IsSpellInRange, IsUsableAction, IsUsableSpell = IsItemInRange, IsSpellInRange, IsUsableAction, IsUsableSpell
--</private-static-properties>

--<private-static-methods>
local function printTime(temps)
	if (temps == nil) then
		Ovale:Print("> nil")
	else
		Ovale:Print("> "..temps)
	end
end

local function addTime(time1, duration)
	if not time1 then
		return nil
	else
		return time1 + duration
	end
end

local function isBeforeEqual(time1, time2)
	return time1 and (not time2 or time1<=time2)
end

local function isBefore(time1, time2)
	return time1 and (not time2 or time1<time2)
end

local function isAfterEqual(time1, time2)
	return not time1 or (time2 and time1>=time2)
end

local function isAfter(time1, time2)
	return not time1 or (time2 and time1>time2)
end

local function minTime(time1, time2)
	if isBefore(time1, time2) then
		return time1
	else
		return time2
	end
end

local function isBetween(checkTime, startTime, endTime)
	return isBeforeEqual(startTime, checkTime) and isAfterEqual(endTime, checkTime)
end

local function maxTime(time1, time2)
	if isAfter(time1, time2) then
		return time1
	else
		return time2
	end
end

--</private-static-methods>

--<public-static-methods>
function OvaleBestAction:StartNewAction()
	OvaleState:Reset()
	OvaleState:ApplySpells()
end

function OvaleBestAction:GetActionInfo(element)
	if not element then
		return nil
	end
	
	local spellId = element.params[1]
	local action
	local actionTexture, actionInRange, actionCooldownStart, actionCooldownDuration,
		actionUsable, actionShortcut, actionIsCurrent, actionEnable
	
	local target = element.params.target
	if (not target) then
		target = OvaleCondition.defaultTarget
	end

	if (element.func == "spell" ) then
		action = OvaleActionBar:GetForSpell(spellId)
		if not OvaleData.spellList[spellId] and not action then 
			Ovale:Log("Spell "..spellId.." not learnt")
			return nil
		end
		
		actionCooldownStart, actionCooldownDuration, actionEnable = OvaleState:GetComputedSpellCD(spellId)
		if not actionCooldownStart or not actionCooldownDuration then
			Ovale:DebugPrint("unknown_spells", "No cooldown data for spell "..spellId)
		end
		
		local si = OvaleData:GetSpellInfo(spellId)
		if si then
			if si.stance and not OvaleStance:IsStance(si.stance) then
				return nil
			end
			
			if si.combo == 0 and OvaleState.state.combo == 0 then
				return nil
			end
			for k,v in pairs(OvaleData.secondaryPower) do
				if si[v] and si[v] > OvaleState.state[v] then
					return nil
				end
			end
			if actionCooldownStart and actionCooldownDuration then
				if si.blood or si.frost or si.unholy or si.death then
					local runecd = OvaleState:GetRunes(si.blood, si.frost, si.unholy, si.death, false)
					if runecd > actionCooldownStart + actionCooldownDuration then
						actionCooldownDuration = runecd - actionCooldownStart
					end
				end
			end
		end
		
		local spellName = OvaleData.spellList[spellId]
		if not spellName then
			spellName = GetSpellInfo(spellId)
		end
		actionTexture = GetSpellTexture(spellId)
		actionInRange = IsSpellInRange(spellName, target)
		actionUsable = IsUsableSpell(spellId)
		actionShortcut = nil
	elseif (element.func=="macro") then
		action = OvaleActionBar:GetForMacro(element.params[1])
		if action then
			actionTexture = GetActionTexture(action)
			actionInRange = IsActionInRange(action, target)
			actionCooldownStart, actionCooldownDuration, actionEnable = GetActionCooldown(action)
			actionUsable = IsUsableAction(action)
			actionShortcut = OvaleActionBar:GetBinding(action)
			actionIsCurrent = IsCurrentAction(action)
		else
			Ovale:Log("Unknown macro "..element.params[1])
		end
	elseif (element.func=="item") then
		local itemId
		if (type(element.params[1]) == "number") then
			itemId = element.params[1]
		else
			local _,_,id = strfind(GetInventoryItemLink("player",GetInventorySlotInfo(element.params[1])) or "","item:(%d+):%d+:%d+:%d+")
			if not id then
				return nil
			end
			itemId = tonumber(id)
		end
		
		if (Ovale.trace) then
			Ovale:Print("Item "..tostring(itemId))
		end

		local spellName = GetItemSpell(itemId)
		actionUsable = (spellName~=nil)
		
		action = OvaleActionBar:GetForItem(itemId)
		actionTexture = GetItemIcon(itemId)
		actionInRange = IsItemInRange(itemId, target)
		actionCooldownStart, actionCooldownDuration, actionEnable = GetItemCooldown(itemId)
		actionShortcut = nil
		actionIsCurrent = nil
	elseif element.func=="texture" then
		actionTexture = "Interface\\Icons\\"..element.params[1]
		actionCooldownStart = OvaleState.maintenant
		actionCooldownDuration = 0
		actionEnable = 1
		actionUsable = true
	end<commit_msg>Query OvaleEquipement for equipped items in slots for efficiency.<commit_after>--[[--------------------------------------------------------------------
    Ovale Spell Priority
    Copyright (C) 2012, 2013 Sidoine, <NAME>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License in the LICENSE
    file accompanying this program.
----------------------------------------------------------------------]]

local _, Ovale = ...
local OvaleBestAction = {}
Ovale.bestActionModule = OvaleBestAction

--<private-static-properties>
local OvaleActionBar = Ovale:GetModule("OvaleActionBar")
local OvaleCondition = Ovale.conditionModule
local OvaleData = Ovale:GetModule("OvaleData")
local OvaleEquipement = Ovale:GetModule("OvaleEquipement")
local OvalePaperDoll = Ovale:GetModule("OvalePaperDoll")
local OvaleStance = Ovale:GetModule("OvaleStance")
local OvaleState = Ovale.stateModule

local floor, ipairs, loadstring, pairs = math.floor, ipairs, loadstring, pairs
local strfind, tonumber, tostring = string.find, tonumber, tostring
local GetActionCooldown, GetActionTexture = GetActionCooldown, GetActionTexture
local GetItemIcon, GetItemCooldown, GetItemSpell, GetSpellInfo = GetItemIcon, GetItemCooldown, GetItemSpell, GetSpellInfo
local GetSpellTexture, IsActionInRange, IsCurrentAction = GetSpellTexture, IsActionInRange, IsCurrentAction
local IsItemInRange, IsSpellInRange, IsUsableAction, IsUsableSpell = IsItemInRange, IsSpellInRange, IsUsableAction, IsUsableSpell
--</private-static-properties>

--<private-static-methods>
local function printTime(temps)
	if (temps == nil) then
		Ovale:Print("> nil")
	else
		Ovale:Print("> "..temps)
	end
end

local function addTime(time1, duration)
	if not time1 then
		return nil
	else
		return time1 + duration
	end
end

local function isBeforeEqual(time1, time2)
	return time1 and (not time2 or time1<=time2)
end

local function isBefore(time1, time2)
	return time1 and (not time2 or time1<time2)
end

local function isAfterEqual(time1, time2)
	return not time1 or (time2 and time1>=time2)
end

local function isAfter(time1, time2)
	return not time1 or (time2 and time1>time2)
end

local function minTime(time1, time2)
	if isBefore(time1, time2) then
		return time1
	else
		return time2
	end
end

local function isBetween(checkTime, startTime, endTime)
	return isBeforeEqual(startTime, checkTime) and isAfterEqual(endTime, checkTime)
end

local function maxTime(time1, time2)
	if isAfter(time1, time2) then
		return time1
	else
		return time2
	end
end

--</private-static-methods>

--<public-static-methods>
function OvaleBestAction:StartNewAction()
	OvaleState:Reset()
	OvaleState:ApplySpells()
end

function OvaleBestAction:GetActionInfo(element)
	if not element then
		return nil
	end
	
	local spellId = element.params[1]
	local action
	local actionTexture, actionInRange, actionCooldownStart, actionCooldownDuration,
		actionUsable, actionShortcut, actionIsCurrent, actionEnable
	
	local target = element.params.target
	if (not target) then
		target = OvaleCondition.defaultTarget
	end

	if (element.func == "spell" ) then
		action = OvaleActionBar:GetForSpell(spellId)
		if not OvaleData.spellList[spellId] and not action then 
			Ovale:Log("Spell "..spellId.." not learnt")
			return nil
		end
		
		actionCooldownStart, actionCooldownDuration, actionEnable = OvaleState:GetComputedSpellCD(spellId)
		if not actionCooldownStart or not actionCooldownDuration then
			Ovale:DebugPrint("unknown_spells", "No cooldown data for spell "..spellId)
		end
		
		local si = OvaleData:GetSpellInfo(spellId)
		if si then
			if si.stance and not OvaleStance:IsStance(si.stance) then
				return nil
			end
			
			if si.combo == 0 and OvaleState.state.combo == 0 then
				return nil
			end
			for k,v in pairs(OvaleData.secondaryPower) do
				if si[v] and si[v] > OvaleState.state[v] then
					return nil
				end
			end
			if actionCooldownStart and actionCooldownDuration then
				if si.blood or si.frost or si.unholy or si.death then
					local runecd = OvaleState:GetRunes(si.blood, si.frost, si.unholy, si.death, false)
					if runecd > actionCooldownStart + actionCooldownDuration then
						actionCooldownDuration = runecd - actionCooldownStart
					end
				end
			end
		end
		
		local spellName = OvaleData.spellList[spellId]
		if not spellName then
			spellName = GetSpellInfo(spellId)
		end
		actionTexture = GetSpellTexture(spellId)
		actionInRange = IsSpellInRange(spellName, target)
		actionUsable = IsUsableSpell(spellId)
		actionShortcut = nil
	elseif (element.func=="macro") then
		action = OvaleActionBar:GetForMacro(element.params[1])
		if action then
			actionTexture = GetActionTexture(action)
			actionInRange = IsActionInRange(action, target)
			actionCooldownStart, actionCooldownDuration, actionEnable = GetActionCooldown(action)
			actionUsable = IsUsableAction(action)
			actionShortcut = OvaleActionBar:GetBinding(action)
			actionIsCurrent = IsCurrentAction(action)
		else
			Ovale:Log("Unknown macro "..element.params[1])
		end
	elseif (element.func=="item") then
		local itemId = element.params[1]
		if itemId and type(itemId) ~= "number" then
			itemId = OvaleEquipement:GetEquippedItem(itemId)
		end
		if not itemId then
			return nil
		end
		
		if (Ovale.trace) then
			Ovale:Print("Item "..tostring(itemId))
		end

		local spellName = GetItemSpell(itemId)
		actionUsable = (spellName~=nil)
		
		action = OvaleActionBar:GetForItem(itemId)
		actionTexture = GetItemIcon(itemId)
		actionInRange = IsItemInRange(itemId, target)
		actionCooldownStart, actionCooldownDuration, actionEnable = GetItemCooldown(itemId)
		actionShortcut = nil
		actionIsCurrent = nil
	elseif element.func=="texture" then
		actionTexture = "Interface\\Icons\\"..element.params[1]
		actionCooldownStart = OvaleState.maintenant
		actionCooldownDuration = 0
		actionEnable = 1
		actionUsable = true
	end
{'fraction_non_alphanumeric': 0.10472659870250231, 'fraction_numerical': 0.009604852978346954, 'mean_word_length': 4.354082092918358, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7616288', 'n_tokens_mistral': 4218, 'n_tokens_neox': 3919, 'n_words': 1001}