Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --AI parameter constants
- AI_THINK_INTERVAL = 0.5
- --AI state constants
- AI_STATE_IDLE = 0
- AI_STATE_AGGRESSIVE = 1
- AI_STATE_RETURNING = 2
- function Spawn( entityKeyValues )
- --Set the core fields for this AI
- thisEntity.state = AI_STATE_IDLE --The initial state
- thisEntity.stateThinks = { --Add thinking functions for each state
- [AI_STATE_IDLE] = 'IdleThink',
- [AI_STATE_AGGRESSIVE] = 'AggressiveThink',
- [AI_STATE_RETURNING] = 'ReturningThink'
- }
- --Set parameters values as fields for later use
- spawnPos = thisEntity.spawnPos
- aggroRange = 300
- leashRange = 900
- thisEntity:SetContextThink("GreevilThink", GreevilThink, 0.5)
- end
- function GreevilThink()
- if not thisEntity:IsAlive() then
- return nil
- end
- if thisEntity.state == 0 then
- IdleThink()
- elseif thisEntity.state == 1 then
- AggressiveThink()
- elseif thisEntity.state == 2 then
- ReturningThink()
- end
- return AI_THINK_INTERVAL
- end
- function IdleThink()
- --Find any enemy units around the AI unit inside the aggroRange
- local units = FindUnitsInRadius( thisEntity:GetTeam(), thisEntity:GetAbsOrigin(), nil,
- aggroRange, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_ALL, DOTA_UNIT_TARGET_FLAG_NONE,
- FIND_ANY_ORDER, false )
- --If one or more units were found, start attacking the first one
- if #units > 0 then
- thisEntity:MoveToTargetToAttack( units[1] ) --Start attacking
- thisEntity.aggroTarget = units[1]
- thisEntity.state = AI_STATE_AGGRESSIVE --State transition
- return true
- end
- end
- function AggressiveThink()
- --If target leaves leash range
- if ( thisEntity.spawnPos - thisEntity:GetAbsOrigin() ):Length() > thisEntity.leashRange then
- thisEntity:MoveToPosition( thisEntity.spawnPos ) --Move back to the spawnpoint
- thisEntity.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
- return true --Return to make sure no other code is executed in this state
- end
- --If target dies
- if not thisEntity.aggroTarget:IsAlive() then
- thisEntity:MoveToPosition( thisEntity.spawnPos ) --Move back to the spawnpoint
- thisEntity.state = AI_STATE_RETURNING --Transition the state to the 'Returning' state(!)
- return true --Return to make sure no other code is executed in this state
- end
- -- aggro commands
- end
- function ReturningThink()
- --Check if the AI unit has reached its spawn location yet
- if ( thisEntity.spawnPos - thisEntity:GetAbsOrigin() ):Length() < 10 then
- --Go into the idle state
- thisEntity.state = AI_STATE_IDLE
- return true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement