monogato

AoESkillshotPosition.lua

Feb 27th, 2013
90,860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. --[[
  2.     AoESkillshotPosition 2.0 by monogato
  3.    
  4.     GetAoESpellPosition(radius, main_target, [delay]) returns best position in order to catch as many enemies as possible with your AoE skillshot, making sure you get the main target.
  5.     Note: You can optionally add delay in ms for prediction (VIP if avaliable, normal else).
  6. ]]
  7.  
  8. function GetCenter(points)
  9.     local sum_x = 0
  10.     local sum_z = 0
  11.    
  12.     for i = 1, #points do
  13.         sum_x = sum_x + points[i].x
  14.         sum_z = sum_z + points[i].z
  15.     end
  16.    
  17.     local center = {x = sum_x / #points, y = 0, z = sum_z / #points}
  18.    
  19.     return center
  20. end
  21.  
  22. function ContainsThemAll(circle, points)
  23.     local radius_sqr = circle.radius*circle.radius
  24.     local contains_them_all = true
  25.     local i = 1
  26.    
  27.     while contains_them_all and i <= #points do
  28.         contains_them_all = GetDistanceSqr(points[i], circle.center) <= radius_sqr
  29.         i = i + 1
  30.     end
  31.    
  32.     return contains_them_all
  33. end
  34.  
  35. -- The first element (which is gonna be main_target) is untouchable.
  36. function FarthestFromPositionIndex(points, position)
  37.     local index = 2
  38.     local actual_dist_sqr
  39.     local max_dist_sqr = GetDistanceSqr(points[index], position)
  40.    
  41.     for i = 3, #points do
  42.         actual_dist_sqr = GetDistanceSqr(points[i], position)
  43.         if actual_dist_sqr > max_dist_sqr then
  44.             index = i
  45.             max_dist_sqr = actual_dist_sqr
  46.         end
  47.     end
  48.    
  49.     return index
  50. end
  51.  
  52. function RemoveWorst(targets, position)
  53.     local worst_target = FarthestFromPositionIndex(targets, position)
  54.    
  55.     table.remove(targets, worst_target)
  56.    
  57.     return targets
  58. end
  59.  
  60. function GetInitialTargets(radius, main_target)
  61.     local targets = {main_target}
  62.     local diameter_sqr = 4 * radius * radius
  63.    
  64.     for i=1, heroManager.iCount do
  65.         target = heroManager:GetHero(i)
  66.         if target.networkID ~= main_target.networkID and ValidTarget(target) and GetDistanceSqr(main_target, target) < diameter_sqr then table.insert(targets, target) end
  67.     end
  68.    
  69.     return targets
  70. end
  71.  
  72. function GetPredictedInitialTargets(radius, main_target, delay)
  73.     if VIP_USER and not vip_target_predictor then vip_target_predictor = TargetPredictionVIP(nil, nil, delay/1000) end
  74.     local predicted_main_target = VIP_USER and vip_target_predictor:GetPrediction(main_target) or GetPredictionPos(main_target, delay)
  75.     local predicted_targets = {predicted_main_target}
  76.     local diameter_sqr = 4 * radius * radius
  77.    
  78.     for i=1, heroManager.iCount do
  79.         target = heroManager:GetHero(i)
  80.         if ValidTarget(target) then
  81.             predicted_target = VIP_USER and vip_target_predictor:GetPrediction(target) or GetPredictionPos(target, delay)
  82.             if target.networkID ~= main_target.networkID and GetDistanceSqr(predicted_main_target, predicted_target) < diameter_sqr then table.insert(predicted_targets, predicted_target) end
  83.         end
  84.     end
  85.    
  86.     return predicted_targets
  87. end
  88.  
  89. -- I don´t need range since main_target is gonna be close enough. You can add it if you do.
  90. function GetAoESpellPosition(radius, main_target, delay)
  91.     local targets = delay and GetPredictedInitialTargets(radius, main_target, delay) or GetInitialTargets(radius, main_target)
  92.     local position = GetCenter(targets)
  93.     local best_pos_found = true
  94.     local circle = Circle(position, radius)
  95.     circle.center = position
  96.    
  97.     if #targets > 2 then best_pos_found = ContainsThemAll(circle, targets) end
  98.    
  99.     while not best_pos_found do
  100.         targets = RemoveWorst(targets, position)
  101.         position = GetCenter(targets)
  102.         circle.center = position
  103.         best_pos_found = ContainsThemAll(circle, targets)
  104.     end
  105.    
  106.     return position
  107. end
Advertisement
Add Comment
Please, Sign In to add comment