|
发表于 2009-4-30 18:06:40
|
显示全部楼层
[codes=jass]
//===========================================================================
function IsTargetHerosEnemy takes unit target, unit hero returns boolean
local unit attacker
if IsUnitEnemy(hero, GetOwningPlayer(target))then
set attacker = GetStoredUnit(H2S(target), "Attacker")
if attacker == null or attacker == hero or IsUnitAliveBJ(attacker) == false then
set attacker = null
return true
endif
endif
set attacker = null
return false
endfunction
//===========================================================================
function GetCreepsFilterDisposal takes unit filterUnit, unit attacker returns boolean
if IsUnitEnemy(filterUnit, GetOwningPlayer(attacker))then
if GetOwningPlayer(filterUnit) == udg_CreepPlayers[1] or GetOwningPlayer(filterUnit) == udg_CreepPlayers[2] then
//if GetUnitUserData(filterUnit) == 0 then
return true
//endif
endif
endif
return false
endfunction
//===========================================================================
function GetCreepsFilter takes nothing returns boolean
return GetCreepsFilterDisposal(GetFilterUnit(), udg_attacker)
endfunction
//===========================================================================
function UnitTargetTurnToNormalEnum takes unit enumUnit returns nothing
call SetUnitUserData(enumUnit, 0)
call IssueUnitAttackWaypoint(enumUnit, GetUnitCurrentWaypointId(enumUnit))
endfunction
//===========================================================================
function UnitTargetTurnToNormal takes nothing returns nothing
call UnitTargetTurnToNormalEnum(GetEnumUnit())
endfunction
//===========================================================================
function DefendersTargetTurnToNormal takes group defenders returns nothing
call ForGroup(defenders, function UnitTargetTurnToNormal)
call GroupClear(defenders)
endfunction
//===========================================================================
function DefenderOutOfRangeAttackWaypointEnum takes nothing returns nothing
if GetUnitCurrentOrder(GetEnumUnit()) == 0 or IsUnitInRange(GetEnumUnit(), udg_attacker, GetUnitAcquireRange(GetEnumUnit())) == false then
call GroupRemoveUnit(udg_defenders, GetEnumUnit())
call UnitTargetTurnToNormalEnum(GetEnumUnit())
endif
endfunction
//===========================================================================
function DefendersAttackAttackerEnum takes nothing returns nothing
call IssueTargetOrder(GetEnumUnit(), "attack", udg_attacker)
call GroupAddUnit(udg_defenders, GetEnumUnit())
call SetUnitUserData(GetEnumUnit(), H2I2(udg_attacker))
endfunction
//===========================================================================
function DefendersBackToNormal takes nothing returns nothing
local string heroHandle = H2S(GetStoredUnit(H2S(GetExpiredTimer()), "Hero"))
call StoreHandle(heroHandle, "Attacker", null)
call DefendersTargetTurnToNormal(GetStoredGroup(heroHandle, "Defenders"))
endfunction
//===========================================================================
function DefineDefendGroup takes unit targetHero, unit attackingHero returns nothing
local string targetHeroHandle = H2S(targetHero)
local group defenders = GetStoredGroup(targetHeroHandle, "Defenders")
local unit attacker = GetStoredUnit(targetHeroHandle, "Attacker")
local boolexpr b
local group g
call PauseTimer(GetStoredTimer(targetHeroHandle, "DefendTimer"))
if attacker != attackingHero and attacker != null then
call DefendersTargetTurnToNormal(defenders)
endif
call StoreHandle(targetHeroHandle, "Attacker", attackingHero)
set udg_defenders = defenders
set udg_attacker = attackingHero
call ForGroup(defenders, function DefenderOutOfRangeAttackWaypointEnum)
set b = Filter(function GetCreepsFilter)
set g = CreateGroup()
call GroupEnumUnitsInRange(g, GetUnitX(attackingHero), GetUnitY(attackingHero), 500, b)
call ForGroup(g, function DefendersAttackAttackerEnum)
call DestroyGroup(g)
call DestroyBoolExpr(b)
call TimerStart(GetStoredTimer(targetHeroHandle, "DefendTimer"), 2, false, function DefendersBackToNormal)
set defenders = null
set attacker = null
set b = null
set g = null
endfunction
//===========================================================================
function OnAttackedConditions takes nothing returns boolean
return IsTargetHerosEnemy(GetTriggerUnit(), GetAttacker())
endfunction
//===========================================================================
function OnAttackedActions takes nothing returns nothing
call DefineDefendGroup(GetTriggerUnit(), GetAttacker())
endfunction
//===========================================================================
function OnAttackConditions takes nothing returns boolean
if IsUnitType(GetOrderTargetUnit(), UNIT_TYPE_HERO) == true and IsUnitIllusion(GetOrderTargetUnit()) == false then
if GetIssuedOrderId() == 851983 or GetIssuedOrderId() == 851971 then
if IsUnitVisible(GetTriggerUnit(), GetOwningPlayer(GetOrderTargetUnit()))then
return IsTargetHerosEnemy(GetOrderTargetUnit(), GetTriggerUnit())
endif
endif
endif
return false
endfunction
//===========================================================================
function OnAttackActions takes nothing returns nothing
call DefineDefendGroup(GetOrderTargetUnit(), GetTriggerUnit())
endfunction
//===========================================================================
function UnregisterArmyAiForHero takes unit hero returns nothing
local string heroHandle = H2S(hero)
local group g = GetStoredGroup(heroHandle, "Defenders")
local trigger t = GetStoredTrigger(heroHandle, "OnAttack")
local timer tm = GetStoredTimer(heroHandle, "DefendTimer")
call FlushStoredBooleanSimple(heroHandle, "ArmyAIRegistered")
call DestroyGroup(g)
call FlushStoredHandle(heroHandle, "Defenders")
call KillTrigger(t)
call FlushStoredHandle(heroHandle, "OnAttack")
set t = GetStoredTrigger(heroHandle, "OnAttacked")
call KillTrigger(t)
call FlushStoredHandle(heroHandle, "OnAttacked")
call FlushStoredMissionSimple(H2S(tm))
call KillTimer(tm)
call FlushStoredHandle(heroHandle, "DefendTimer")
set g = null
set t = null
set tm = null
endfunction
//===========================================================================
function RegisterArmyAiForHero takes unit hero returns nothing
local string heroHandle = H2S(hero)
local group defenders
local trigger attackOrderTrigger
local timer defendTimer
if GetStoredBooleanSimple(heroHandle, "ArmyAIRegistered") == true then
return
endif
call StoreBooleanSimple(heroHandle, "ArmyAIRegistered", true)
set defenders = CreateGroup()
call StoreHandle(heroHandle, "Defenders", defenders)
set attackOrderTrigger = CreateTrigger()
call StoreHandle(heroHandle, "OnAttack", attackOrderTrigger)
call TriggerRegisterUnitEvent(attackOrderTrigger, hero, EVENT_UNIT_ISSUED_TARGET_ORDER)
call TriggerAddCondition(attackOrderTrigger, Condition(function OnAttackConditions))
call TriggerAddAction(attackOrderTrigger, function OnAttackActions)
set attackOrderTrigger = CreateTrigger()
call StoreHandle(heroHandle, "OnAttacked", attackOrderTrigger)
call TriggerRegisterUnitEvent(attackOrderTrigger, hero, EVENT_UNIT_ATTACKED)
call TriggerAddCondition(attackOrderTrigger, Condition(function OnAttackedConditions))
call TriggerAddAction(attackOrderTrigger, function OnAttackedActions)
set defendTimer = CreateTimer()
call StoreHandle(heroHandle, "DefendTimer", defendTimer)
call StoreHandle(H2S(defendTimer), "Hero", hero)
set defenders = null
set attackOrderTrigger = null
set defendTimer = null
endfunction
[/codes] |
|