Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. --AI parameter constants
  2. AI_THINK_INTERVAL = 0.5
  3.  
  4. --AI state constants
  5. AI_STATE_IDLE = 0
  6. AI_STATE_AGGRESSIVE = 1
  7. AI_STATE_RETURNING = 2
  8.  
  9. function Spawn( entityKeyValues )
  10.  
  11. --Set the core fields for this AI
  12. thisEntity.state = AI_STATE_IDLE --The initial state
  13. thisEntity.stateThinks = { --Add thinking functions for each state
  14. [AI_STATE_IDLE] = 'IdleThink',
  15. [AI_STATE_AGGRESSIVE] = 'AggressiveThink',
  16. [AI_STATE_RETURNING] = 'ReturningThink'
  17. }
  18.  
  19. --Set parameters values as fields for later use
  20. spawnPos = thisEntity.spawnPos
  21. aggroRange = 300
  22. leashRange = 900
  23.  
  24. thisEntity:SetContextThink("GreevilThink", GreevilThink, 0.5)
  25. end
  26.  
  27. function GreevilThink()
  28. if not thisEntity:IsAlive() then
  29. return nil
  30. end
  31.  
  32. if thisEntity.state == 0 then
  33. IdleThink()
  34. elseif thisEntity.state == 1 then
  35. AggressiveThink()
  36. elseif thisEntity.state == 2 then
  37. ReturningThink()
  38. end
  39.  
  40. return AI_THINK_INTERVAL
  41. end
  42.  
  43. function IdleThink()
  44. --Find any enemy units around the AI unit inside the aggroRange
  45. local units = FindUnitsInRadius( thisEntity:GetTeam(), thisEntity:GetAbsOrigin(), nil,
  46. aggroRange, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_ALL, DOTA_UNIT_TARGET_FLAG_NONE,
  47. FIND_ANY_ORDER, false )
  48.  
  49. --If one or more units were found, start attacking the first one
  50. if #units > 0 then
  51. thisEntity:MoveToTargetToAttack( units[1] ) --Start attacking
  52. thisEntity.aggroTarget = units[1]
  53. thisEntity.state = AI_STATE_AGGRESSIVE --State transition
  54. return true
  55. end
  56. end
  57.  
  58. function AggressiveThink()
  59. --If target leaves leash range
  60. if ( thisEntity.spawnPos - thisEntity:GetAbsOrigin() ):Length() > thisEntity.leashRange then
  61. thisEntity:MoveToPosition( thisEntity.spawnPos ) --Move back to the spawnpoint
  62. thisEntity.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
  63. return true --Return to make sure no other code is executed in this state
  64. end
  65.  
  66. --If target dies
  67. if not thisEntity.aggroTarget:IsAlive() then
  68. thisEntity:MoveToPosition( thisEntity.spawnPos ) --Move back to the spawnpoint
  69. thisEntity.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
  70. return true --Return to make sure no other code is executed in this state
  71. end
  72.  
  73. -- aggro commands
  74. end
  75.  
  76. function ReturningThink()
  77. --Check if the AI unit has reached its spawn location yet
  78. if ( thisEntity.spawnPos - thisEntity:GetAbsOrigin() ):Length() < 10 then
  79. --Go into the idle state
  80. thisEntity.state = AI_STATE_IDLE
  81. return true
  82. end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement