Skip to content

Instantly share code, notes, and snippets.

@DelusionalLogic
Created July 3, 2012 00:52
Show Gist options
  • Save DelusionalLogic/3036693 to your computer and use it in GitHub Desktop.
Save DelusionalLogic/3036693 to your computer and use it in GitHub Desktop.
Target Selector UPGRADE v0.2
TARGET_LOW_HP = 1
TARGET_NEAR = 2
TARGET_FAR = 3
DAMAGE_MAGIC = 1
DAMAGE_PHYSICAL = 2
targetMode=" "
mouseRange = 400 -- distance from mouse the target must be to attack if targetMouse is true.
targetMouse = false --default
TargetSelector = {}
player = GetMyHero()
mouseArray = {}
Array = { --A blank array for enemies and mode flags.
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0}
}
CommandArray = { -- a Table of commands. Easier to add new word commands later.
{command=".ignore", targetflag=true, --targetflag true means it requires ".ignore TargetName" to work
method= function(i)
Array[i].mode = "ignore"
PrintChat(" >> Ignoring "..Array[i].enemy.charName)
end
},
{command=".target", targetflag=true,
method= function(i)
Array[i].mode="target"
PrintChat(" >> Targeting "..Array[i].enemy.charName)
end
},
{command=".focus", targetflag=true,
method= function(i)
local j
targetMode = "Priority"
PrintChat(" >> Priority mode activated")
Array[i].mode= "target"
PrintChat(" >> Focusing "..Array[i].enemy.charName)
for j=1, #Array, 1 do
Array[j].priorityNum = 0
end
Array[i].priorityNum = math.huge
end
},
{command=".mostap", targetflag = false,
method = function(i)
targetMode = "MostAP"
PrintChat(" >> Focusing non-ignored enemy with most AP!")
end
},
{command=".lowhp", targetflag = false,
method = function(i)
targetMode = "LowHP"
PrintChat(" >> Focusing non-ignored enemy with lowest effective HP")
end
},
{command=".near", targetflag = false,
method = function(i)
targetMode = "Near"
PrintChat(" >> Focusing the nearest non-ignored enemy")
end
},
{command=".far", targetflag = false,
method = function(i)
targetMode = "Far"
PrintChat(" >> Focusing the furthest non-ignored enemy")
end
},
{command=".printarray", targetflag = false,
method = function(i)
local i
for i=1,#Array, 1 do
if Array[i].enemy.charName ~= "###" then
PrintChat(" >> Enemy "..i.." : "..Array[i].enemy.charName.." Mode= "..Array[i].mode.." Priority = "..Array[i].priorityNum)
end
end
PrintChat(" >> Target Mode: "..targetMode)
if targetMouse == true then
PrintChat(" >> Closest to mouse")
end
end
},
{command=".primode", targetflag = false,
method = function(i)
targetMode = "Priority"
PrintChat(" >> Priority mode activated.")
end
},
{command=".setpriority", targetflag = true,
method = function(i)
end
},
{command=".mouse",targetflag = false,
method = function(i)
if targetMouse == false then
targetMouse = true
PrintChat(" >> Targeting Enemies "..mouseRange.." away from mouse.")
else
targetMouse = false
PrintChat(" >> Mouse targeting deactivated.")
end
end
}
}
function TargetSelector:new(mode, range, damageType)
local object = { mode = mode, range = range, damageType = damageType or DAMAGE_MAGIC, target = nil, paused=false }
setmetatable(object, { __index = TargetSelector })
if mode == TARGET_LOW_HP then --just a conversion for consistency
targetMode= "LowHP"
elseif mode == TARGET_NEAR then
targetMode= "Near"
elseif mode == TARGET_FAR then
targetMode= "Far"
end
return object
end
function TargetSelector:getChatCommand(text,playername)
local i,j,n
for j=1,#CommandArray,1 do
for i=1, #Array, 1 do
if string.find(text,Array[i].enemy.charName)~=nil or CommandArray[j].targetflag == false then --true if found a character name that matches an enemy OR it doesn't need a target name
if string.find(text,CommandArray[j].command)~=nil then --if found a given command
if string.find(text,"%d")~=nil then --found a number after command set priority
n = tonumber(string.sub(text,string.find(text,"%d")))
Array[i].priorityNum = n
targetMode = "Priority"
PrintChat(""..Array[i].enemy.charName.."'s priority set to "..n)
PrintChat(" >> Priority mode activated.")
else
CommandArray[j].method(i) --proceed with commands actions
if CommandArray[j].targetflag == false then --if command was a mode change not requiring a target, then for loop breaks
break
end
end
end
end
end
end
end
function TargetSelector:buildarray() --populates Array with enemy champion objects
local count = heroManager.iCount
local i
local n=1
for i = 1, count, 1 do
local object = heroManager:GetHero(i)
if object ~= nil and object.team ~= player.team then
Array[n].enemy = object
n=n+1
end
end
end
function TargetSelector:printarray() -- will print all enemies, their mode, and the targeting mode.
for i=1,#Array, 1 do
if Array[i].enemy.charName ~= "###" then
PrintChat(" >> Enemy "..i.." : "..Array[i].enemy.charName.." Mode= "..Array[i].mode.." Priority = "..Array[i].priorityNum)
end
end
PrintChat(" >> Target Mode: "..targetMode)
end
function CalculateMagicDmg(target, spellDamage) --TODO replace this with working target:CalculateMagicDamage() in next update
local magicArmor = target.magicArmor - player.magicPen
if magicArmor > 0 then
magicArmor = magicArmor * (1-(player.magicPenPercent/100))
else
magicArmor = 0
end
return spellDamage * (100/(100+magicArmor))
end
function TargetSelector:tick(time)
if targetMouse == true then
self:findHeroNearestMouse()
end
if self.paused then return end
if targetMode==" " then
targetMode = self.mode
end
if targetMode == "LowHP" then
self:low_hp()
elseif targetMode == "Far" then
self:far()
elseif targetMode == "Near" then
self:near()
elseif targetMode == "MostAP" then
self:mostAP()
elseif targetMode == "Priority" then
self:highestPriority()
end
end
function TargetSelector:pause()
self.paused = true
end
function TargetSelector:resume()
self.paused = false
end
function TargetSelector:highestPriority()
local highestP = 0
local highest = nil
if targetMouse == true then
for i=1, #mouseArray, 1 do
if mouseArray[i].mode ~= "ignore" then
if self:check_if_target(mouseArray[i].enemy) then
if player:GetDistance(object)<=self.range then
if mouseArray[i].priorityNum >= highestP then
highest = mouseArray[i].enemy
highestP=mouseArray[i].priorityNum
end
end
end
end
end
else
for i=1, #Array, 1 do
if Array[i].mode ~= "ignore" then
if self:check_if_target(Array[i].enemy) then
if player:GetDistanceTo(Array[i].enemy)<=self.range then
if Array[i].priorityNum >= highestP then
highest = Array[i].enemy
highestP=Array[i].priorityNum
end
end
end
end
end
end
self.target = highest
end
function TargetSelector:low_hp()
local tmp
local count,object
local ignoreflag=false
if targetMouse == true then
for i = 1, #mouseArray, 1 do
if mouseArray[i].mode ~= "ignore" then
if self:check_if_target(mouseArray[i].enemy) and player:GetDistance(object) <= self.range
and
(tmp == nil or
((self.damageType == DAMAGE_MAGIC and tmp.health * (100/CalculateMagicDmg(tmp,100)) > mouseArray[i].enemy.health * (100/CalculateMagicDmg(mouseArray[i].enemy,100)))
or
(self.damageType == DAMAGE_PHYSICAL and tmp.health * (player.totalDamage/player:CalculateDamage(tmp)) > mouseArray[i].enemy.health * (player.totalDamage/player:CalculateDamage(mouseArray[i].enemy))))
) then
tmp = mouseArray[i].enemy
end
end
end
else
if Array[1].enemy.charName == "###" then --backwards compatibility. If true, then not using updated script for text input.
count = heroManager.iCount
else
count = #Array
end
for i = 1, count, 1 do
if Array[1].enemy.charName == "###" then --backwards compatibility
object = heroManager:GetHero(i)
elseif Array[i].mode == "ignore" then
ignoreflag = true
elseif Array[i].mode ~= "ignore" then
ignoreflag = false
object = Array[i].enemy
end
if ignoreflag == false then
if self:check_if_target(object) and player:GetDistance(object) <= self.range
and
(tmp == nil or
((self.damageType == DAMAGE_MAGIC and tmp.health * (100/CalculateMagicDmg(tmp,100)) > object.health * (100/CalculateMagicDmg(object,100)))
or
(self.damageType == DAMAGE_PHYSICAL and tmp.health * (player.totalDamage/player:CalculateDamage(tmp)) > object.health * (player.totalDamage/player:CalculateDamage(object))))
) then
tmp = object
end
end
end
end
self.target = tmp
end
function TargetSelector:near()
local nc = nil
local count,object
local smallest_dist = self.range+1
local ignoreflag = false
if targetMouse == true then
for i = 1, #mouseArray, 1 do
if mouseArray[i].mode ~= "ignore" then
if self:check_if_target(mouseArray[i].enemy) then
local d = player:GetDistance(mouseArray[i].enemy)
if d<=self.range and d<smallest_dist then
nc = mouseArray[i].enemy
smallest_dist = d
end
end
end
end
else
if Array[1].enemy.charName == "###" then --backwards compatibility. If true, then not using updated script for text input.
count = heroManager.iCount
else
count = #Array
end
for i = 1, count, 1 do
if Array[1].enemy.charName == "###" then --backwards compatibility
object = heroManager:GetHero(i)
elseif Array[i].mode == "ignore" then
ignoreflag = true
elseif Array[i].mode ~= "ignore" then
ignoreflag = false
object = Array[i].enemy
end
if ignoreflag==false then
if self:check_if_target(object) then
local d = player:GetDistance(object)
if d<=self.range and d<smallest_dist then
nc = object
smallest_dist = d
end
end
end
end
end
self.target = nc
end
function TargetSelector:far()
local fc = nil
local biggest_dist = -1
local count,object
local ignoreflag = false
if targetMouse == true then
for i = 1, #mouseArray, 1 do
if mouseArray[i].mode ~= "ignore" then
if self:check_if_target(mouseArray[i].enemy) then
local d = player:GetDistance(mouseArray[i].enemy)
if d<=self.range and d>biggest_dist then
fc = mouseArray[i].enemy
biggest_dist = d
end
end
end
end
else
if Array[1].enemy.charName == "###" then --backwards compatibility. If true, then not using updated script for text input.
count = heroManager.iCount
else
count = #Array
end
for i = 1, count, 1 do
if Array[1].enemy.charName == "###" then --backwards compatibility
object = heroManager:GetHero(i)
elseif Array[i].mode == "ignore" then
ignoreflag = true
elseif Array[i].mode ~= "ignore" then
ignoreflag = false
object = Array[i].enemy
end
if ignoreflag == false then
if self:check_if_target(object) then
local d = player:GetDistance(object)
if d<=self.range and d>biggest_dist then
fc = object
biggest_dist = d
end
end
end
end
end
self.target = fc
end
function TargetSelector:mostAP()
local highestAP = 0
local highest = nil
local ignoreflag = false
if targetMouse == true then
for i=1, #mouseArray, 1 do
if mouseArray[i].mode ~= "ignore" then
if self:check_if_target(mouseArray[i].enemy) then
if player:GetDistance(object)<=self.range then
if mouseArray[i].enemy.ap >= highestAP then
highest = mouseArray[i].enemy
highestAP=mouseArray[i].enemy.ap
end
end
end
end
end
else
for i=1, #Array, 1 do
if Array[i].mode ~= "ignore" then
if self:check_if_target(Array[i].enemy) then
if player:GetDistanceTo(Array[i].enemy)<=self.range then
if Array[i].enemy.ap >= highestAP then
highest = Array[i].enemy
highestAP=Array[i].enemy.ap
end
end
end
end
end
end
self.target = highest
end
function TargetSelector:findHeroNearestMouse()
local i
local j=1
mouseArray = { --A blank array for enemies near the Mouse
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0},
{enemy = {charName="###"}, mode = "target", priorityNum = 0}
}
for i=1, #Array, 1 do
if self:check_if_target(Array[i].enemy) then
if (self:distanceFromMouse(Array[i].enemy) <= mouseRange) then
mouseArray[j].enemy = Array[i].enemy
mouseArray[j].mode = Array[i].mode
mouseArray[j].priorityNum = Array[i].priorityNum
j = j+1
end
end
end
end
function TargetSelector:distanceFromMouse(target)
if target ~= nil then
return math.floor(math.sqrt((target.x-MousePos.x)*(target.x-MousePos.x) + (target.z-MousePos.z)*(target.z-MousePos.z)))
else return math.huge
end
end
function TargetSelector:check_if_target(obj)
return obj ~= nil and obj.team ~= player.team and obj.visible and not obj.dead
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment