AI CORE
Код:
AICore = {}
behaviorSystem = {} -- create the global so we can assign to it
function AICore:RandomEnemyHeroInRange( entity, range )
local enemies = FindUnitsInRadius( DOTA_TEAM_BADGUYS, entity:GetOrigin(), nil, range, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_HERO, 0, 0, false )
if #enemies > 0 then
local index = RandomInt( 1, #enemies )
return enemies[index]
else
return nil
end
end
function AICore:ClosestEnemyHeroInRange( entity, range )
local enemies = FindUnitsInRadius( DOTA_TEAM_BADGUYS, entity:GetOrigin(), nil, range, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_HERO, DOTA_UNIT_TARGET_FLAG_NO_INVIS + DOTA_UNIT_TARGET_FLAG_MAGIC_IMMUNE_ENEMIES, FIND_CLOSEST, false )
if #enemies > 0 then
local index = RandomInt( 1, #enemies )
return enemies[index]
else
return nil
end
end
function AICore:WeakestEnemyHeroInRange( entity, range )
local enemies = FindUnitsInRadius( DOTA_TEAM_BADGUYS, entity:GetOrigin(), nil, range, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_HERO, 0, 0, false )
local minHP = nil
local target = nil
for _,enemy in pairs(enemies) do
local distanceToEnemy = (entity:GetOrigin() - enemy:GetOrigin()):Length()
local HP = enemy:GetHealth()
if enemy:IsAlive() and (minHP == nil or HP < minHP) and distanceToEnemy < range then
minHP = HP
target = enemy
end
end
return target
end
function AICore:WeakestAllyHeroInRange( entity, range )
local allies = FindUnitsInRadius( DOTA_TEAM_BADGUYS, entity:GetOrigin(), nil, range, DOTA_UNIT_TARGET_TEAM_FRIENDLY, DOTA_UNIT_TARGET_HERO, 0, 0, false )
local minHP = nil
local target = nil
for _,ally in pairs(allies) do
local distanceToAlly = (entity:GetOrigin() - ally:GetOrigin()):Length()
local HP = ally:GetHealth()
if ally:IsAlive() and (minHP == nil or HP < minHP) and distanceToAlly < range then
minHP = HP
target = ally
end
end
return target
end
function AICore:CreateBehaviorSystem( behaviors )
local BehaviorSystem = {}
BehaviorSystem.possibleBehaviors = behaviors
BehaviorSystem.thinkDuration = 1.0
BehaviorSystem.repeatedlyIssueOrders = true -- if you're paranoid about dropped orders, leave this true
BehaviorSystem.currentBehavior =
{
endTime = 0,
order = { OrderType = DOTA_UNIT_ORDER_NONE }
}
function BehaviorSystem:Think()
if GameRules:GetGameTime() >= self.currentBehavior.endTime then
local newBehavior = self:ChooseNextBehavior()
if newBehavior == nil then
-- Do nothing here... this covers possible problems with ChooseNextBehavior
elseif newBehavior == self.currentBehavior then
self.currentBehavior:Continue()
else
if self.currentBehavior.End then self.currentBehavior:End() end
self.currentBehavior = newBehavior
self.currentBehavior:Begin()
end
end
if self.currentBehavior.order and self.currentBehavior.order.OrderType ~= DOTA_UNIT_ORDER_NONE then
if self.repeatedlyIssueOrders or
self.previousOrderType ~= self.currentBehavior.order.OrderType or
self.previousOrderTarget ~= self.currentBehavior.order.TargetIndex or
self.previousOrderPosition ~= self.currentBehavior.order.Position then
-- Keep sending the order repeatedly, in case we forgot >.<
ExecuteOrderFromTable( self.currentBehavior.order )
self.previousOrderType = self.currentBehavior.order.OrderType
self.previousOrderTarget = self.currentBehavior.order.TargetIndex
self.previousOrderPosition = self.currentBehavior.order.Position
end
end
if self.currentBehavior.Think then self.currentBehavior:Think(self.thinkDuration) end
return self.thinkDuration
end
function BehaviorSystem:ChooseNextBehavior()
local result = nil
local bestDesire = nil
for _,behavior in pairs( self.possibleBehaviors ) do
local thisDesire = behavior:Evaluate()
if bestDesire == nil or thisDesire > bestDesire then
result = behavior
bestDesire = thisDesire
end
end
return result
end
function BehaviorSystem:Deactivate()
print("End")
if self.currentBehavior.End then self.currentBehavior:End() end
end
return BehaviorSystem
end
всё работает и юнит юзает то, что нужно.
НО когда он заспавнен как нейтрал через Hammer , его AI больше не работает, подскажите пожалуйста, что делать?
AI моего юнита
Код:
require( "ai/ai_core" )
--------------------------------------------------------------------------------
function Spawn( entityKeyValues )
if not IsServer() then
return
end
if thisEntity == nil then
return
end
thisEntity.hSummonAbility = thisEntity:FindAbilityByName( "call_of_the_wild_boar_boss" )
thisEntity:SetContextThink( "PudgeThink", PudgeThink, 0.5 )
end
--------------------------------------------------------------------------------
function PudgeThink()
if not IsServer() then
return
end
if thisEntity == nil or thisEntity:IsNull() or ( not thisEntity:IsAlive() ) then
return -1
end
if GameRules:IsGamePaused() == true then
return 0.1
end
local hEnemies = FindUnitsInRadius( thisEntity:GetTeamNumber(), thisEntity:GetOrigin(), nil, 800, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_HERO, DOTA_UNIT_TARGET_FLAG_MAGIC_IMMUNE_ENEMIES, FIND_CLOSEST, false )
if #hEnemies == 0 then
return 1
end
if thisEntity.hSummonAbility and thisEntity.hSummonAbility:IsFullyCastable() then
return CastSummon()
end
return 0.5
end
--------------------------------------------------------------------------------
function CastSummon()
ExecuteOrderFromTable({
UnitIndex = thisEntity:entindex(),
OrderType = DOTA_UNIT_ORDER_CAST_NO_TARGET,
AbilityIndex = thisEntity.hSummonAbility:entindex(),
Queue = false,
})
return 0.5
end
--------------------------------------------------------------------------------
Последнее редактирование: