Advertisement
mikeyy

GoalWeights fix

Jul 23rd, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. --Override TargetedAttackWeightsHero to return multiplied
  2. --KillHero values rather than setting to fixed amounts
  3. function TargetedAttackWeightsHero( action, aiBrain, agent, initialAgent )
  4.     if not agent.WorldStateData.CanUseAbilities then
  5.         return false
  6.     end
  7.  
  8.     local actionBp = HeroAIActionTemplates[action.ActionName]
  9.     if not action.Ability then
  10.         return false
  11.     end
  12.  
  13.     if not TestEnergy( agent, action.Ability ) then
  14.         return false
  15.     end
  16.  
  17.     local range = Ability[action.Ability].RangeMax * 1.5
  18.     if not initialAgent.GOAP.NearbyEnemyHeroes.ClosestDistance or initialAgent.GOAP.NearbyEnemyHeroes.ClosestDistance > range then
  19.         return false
  20.     end
  21.  
  22.     local returnValue = table.copy(actionBp.GoalWeights)
  23.     --Mithy: Defaulted to -1 to prevent errors
  24.     if not returnValue.KillHero then
  25.         returnValue.KillHero = -1
  26.     end
  27.     if initialAgent.GOAP.NearbyEnemyHeroes and not actionBp.ForceGoalWeights then
  28.         local damage = GetAbilityDamage(actionBp, action.Ability)
  29.         if damage then
  30.             local enemyHps = initialAgent.GOAP.NearbyEnemyHeroes.WeakestHealth
  31.             local damageRatio = enemyHps / damage
  32.  
  33.             --Mithy: Now multiplicative instead of hard-set
  34.             if damageRatio < 1.0 then
  35.                 returnValue.KillHero = returnValue.KillHero * 3
  36.             elseif damageRatio < 1.5 then
  37.                 returnValue.KillHero = returnValue.KillHero * 2
  38.             elseif actionBp.KillShot then
  39.                 return false
  40.             elseif damageRatio < 2.0 then
  41.                 returnValue.KillHero = returnValue.KillHero * 1.5
  42.             else
  43.                 returnValue.KillHero = returnValue.KillHero * 0.5
  44.             end
  45.         end
  46.     end
  47.  
  48.     if actionBp.WeakHeroNearbyAdd and agent.WorldStateData.WeakHeroNearby then
  49.         returnValue.KillHero = returnValue.KillHero + actionBp.WeakHeroNearbyAdd
  50.     end
  51.  
  52.     if actionBp.BonusGoalFunction then
  53.         local addValue = actionBp.BonusGoalFunction(action.Ability)
  54.         for k,v in returnValue do
  55.             returnValue[k] = v + addValue
  56.         end
  57.     end
  58.  
  59.     local ringRange = math.ceil( Ability[action.Ability].RangeMax / 16 )
  60.  
  61.     local heroThreat = aiBrain:GetThreatAtPosition( agent.Position, ringRange, 'Hero', 'Enemy' )
  62.  
  63.     if heroThreat > 0 then
  64.         return returnValue, actionBp.WeightTime or Ability[action.Ability].CastingTime or 0
  65.     end
  66.  
  67.     return false
  68. end
  69.  
  70.  
  71.  
  72. --Override TargetedAbilitySquadTargetWeights to return multiplied
  73. --KillSquadTarget values rather than setting to fixed amounts
  74. function TargetedAbilitySquadTargetWeights( action, aiBrain, agent, initialAgent )
  75.     if not agent.WorldStateData.CanUseAbilities then
  76.         return false
  77.     end
  78.  
  79.     local actionBp = HeroAIActionTemplates[action.ActionName]
  80.     if not action.Ability then
  81.         return false
  82.     end
  83.  
  84.     if not TestEnergy( agent, action.Ability ) then
  85.         return false
  86.     end
  87.  
  88.     if not Ability[action.Ability].RangeMax then
  89.         WARN('*AI ERROR: No RangeMax found for AI Action - ' .. action.ActionName)
  90.         return false
  91.     end
  92.  
  93.     local range = Ability[action.Ability].RangeMax * 1.5
  94.  
  95.     local target = initialAgent.GOAP.AttackTarget
  96.     if not target or VDist3XZSq( agent.Position, target.Position ) > range * range then
  97.         return false
  98.     end
  99.  
  100.     local returnValue = table.copy(actionBp.GoalWeights)
  101.     --Mithy: Defaulted to -1 to prevent errors
  102.     if not returnValue.KillSquadTarget then
  103.         returnValue.KillSquadTarget = -1
  104.     end
  105.     if not actionBp.ForceGoalWeights and initialAgent.GOAP.SquadTargetHealth then
  106.         local damage = GetAbilityDamage(actionBp, action.Ability)
  107.         if damage then
  108.             local enemyHps = initialAgent.GOAP.SquadTargetHealth
  109.             local damageRatio = enemyHps / damage
  110.  
  111.             --Mithy: Now multiplicative instead of hard-set
  112.             if damageRatio < 1.0 then
  113.                 returnValue.KillSquadTarget = returnValue.KillSquadTarget * 3
  114.             elseif damageRatio < 1.5 then
  115.                 returnValue.KillSquadTarget = returnValue.KillSquadTarget * 2
  116.             elseif actionBp.KillShot then
  117.                 return false
  118.             elseif damageRatio < 2.0 then
  119.                 returnValue.KillSquadTarget = returnValue.KillSquadTarget * 1.5
  120.             else
  121.                 returnValue.KillSquadTarget = returnValue.KillSquadTarget * 0.5
  122.             end
  123.         end
  124.     end
  125.  
  126.     if actionBp.SquadTargetNearbyAdd then
  127.         returnValue.KillSquadTarget = returnValue.KillSquadTarget + actionBp.SquadTargetNearbyAdd
  128.     end
  129.  
  130.     if actionBp.BonusGoalFunction then
  131.         local addValue = actionBp.BonusGoalFunction(action.Ability)
  132.         for k,v in returnValue do
  133.             returnValue[k] = v + addValue
  134.         end
  135.     end
  136.  
  137.     return returnValue, actionBp.WeightTime or Ability[action.Ability].CastingTime or 0
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement