Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1.  
  2. --[[
  3. Adjusted from SimpleAI made by Perry
  4. http://yrrep.me/dota/dota-simple-ai.html
  5. ]]
  6. --AI parameter constants
  7. AI_THINK_INTERVAL = 0.5 -- The interval in seconds between two think ticks
  8.  
  9. --AI state constants
  10. AI_STATE_IDLE = 0
  11. AI_STATE_AGGRESSIVE = 1
  12. AI_STATE_RETURNING = 2
  13.  
  14. --Define the SimpleAI class
  15. SimpleAI = {}
  16. SimpleAI.__index = SimpleAI
  17.  
  18. --[[ Create an instance of the SimpleAI class for some unit
  19. with some parameters. ]]
  20. function SimpleAI:MakeInstance( unit, params )
  21. --Construct an instance of the SimpleAI class
  22. local ai = {}
  23. setmetatable( ai, SimpleAI )
  24.  
  25. --Set the core fields for this AI
  26. ai.unit = unit --The unit this AI is controlling
  27. ai.state = AI_STATE_IDLE --The initial state
  28. ai.stateThinks = { --Add thinking functions for each state
  29. [AI_STATE_IDLE] = 'IdleThink',
  30. [AI_STATE_AGGRESSIVE] = 'AggressiveThink',
  31. [AI_STATE_RETURNING] = 'ReturningThink'
  32. }
  33.  
  34. --Set parameters values as fields for later use
  35. ai.spawnPos = params.spawnPos
  36. ai.aggroRange = params.aggroRange
  37. ai.leashRange = params.leashRange
  38.  
  39. require('libraries/timers')
  40. --Start thinking
  41. Timers:CreateTimer( ai.GlobalThink, ai )
  42.  
  43. --Return the constructed instance
  44. return ai
  45. end
  46.  
  47. --[[ The high-level thinker this AI will do every tick, selects the correct
  48. state-specific think function and executes it. ]]
  49. function SimpleAI:GlobalThink()
  50. --If the unit is dead, stop thinking
  51.  
  52. if not self:GetParent():IsAlive() then
  53. return nil
  54. end
  55.  
  56. --Execute the think function that belongs to the current state
  57. Dynamic_Wrap(SimpleAI, self.stateThinks[ self.state ])( self )
  58.  
  59. --Reschedule this thinker to be called again after a short duration
  60. return AI_THINK_INTERVAL
  61. end
  62.  
  63. --[[ Think function for the 'Idle' state. ]]
  64. function SimpleAI:IdleThink()
  65. --Find any enemy units around the AI unit inside the aggroRange
  66. local units = FindUnitsInRadius( self:GetParent():GetTeam(), self:GetParent():GetAbsOrigin(), nil,
  67. self.aggroRange, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_ALL, DOTA_UNIT_TARGET_FLAG_NONE,
  68. FIND_ANY_ORDER, false )
  69.  
  70. --If one or more units were found, start attacking the first one
  71. if #units > 0 then
  72. self:GetParent():MoveToTargetToAttack( units[1] ) --Start attacking
  73. self.aggroTarget = units[1]
  74. self.state = AI_STATE_AGGRESSIVE --State transition
  75. return true
  76. end
  77.  
  78. --State behavior
  79. --Whistle a tune
  80. end
  81.  
  82. --[[ Think function for the 'Aggressive' state. ]]
  83. function SimpleAI:AggressiveThink()
  84. --Check if the unit has walked outside its leash range
  85. if ( self.spawnPos - self.unit:GetAbsOrigin() ):Length() > self.leashRange then
  86. self.unit:MoveToPosition( self.spawnPos ) --Move back to the spawnpoint
  87. self.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
  88. return true --Return to make sure no other code is executed in this state
  89. end
  90.  
  91. --Check if the unit's target is still alive (self.aggroTarget will have to be set when transitioning into this state)
  92. if not self.aggroTarget:IsAlive() then
  93. self.unit:MoveToPosition( self.spawnPos ) --Move back to the spawnpoint
  94. self.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
  95. return true --Return to make sure no other code is executed in this state
  96. end
  97.  
  98. --State behavior
  99. --Here we can just do any behaviour you want to repeat in this state
  100. print('Attacking!')
  101. end
  102.  
  103. --[[ Think function for the 'Returning' state. ]]
  104. function SimpleAI:ReturningThink()
  105. --Check if the AI unit has reached its spawn location yet
  106. if ( self.spawnPos - self.unit:GetAbsOrigin() ):Length() < 10 then
  107. --Go into the idle state
  108. self.state = AI_STATE_IDLE
  109. return true
  110. end
  111. end
  112. function SimpleAI:OnCreated( kv )
  113. print("got to here1")
  114.  
  115.  
  116. parent = self:GetParent()
  117. SimpleAI:MakeInstance( parent, { spawnPos = parent:GetAbsOrigin(), aggroRange = 500, leashRange = 900 } )
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement