|
function HandleToUnit takes handle h returns unit
return h
endfunction
//**********************************************************************************************
//* *
//* UnitGuardUnitAI function and subfunctions *
//* *
//**********************************************************************************************
//==============================================================================================
// Unit Guard AI Configuration: (Added this section so you can easily customize the ai without
// Messing with the functions, just replace the numbers)
//
constant function UnitGuardUnitAI_maxdelay takes nothing returns real
return 2.5 //* The max delay betwen orders issued to the summon
endfunction
constant function UnitGuardUnitAI_mindelay takes nothing returns real
return 1.00 //* The min delay betwen orders issued to the summon
endfunction
constant function UnitGuardUnitAI_maxdistance takes nothing returns real
return 1000.0 //* If the distance betwen the summon and the master is greater than this value,
// The summon will move to the position of the master instead of attack move
endfunction
constant function UnitGuardUnitAI_cometomaxdist takes nothing returns real
return 500.0 //* Max distance for issued orders to approach the master
endfunction
constant function UnitGuardUnitAI_cometomindist takes nothing returns real
return 100.0 //* Min distance for issued orders to approach the master
endfunction
//==============================================================================================
function GuardUnitOrder takes unit summon, unit master returns nothing
local integer o=GetUnitCurrentOrder(summon)
local real tx=GetUnitX(master)
local real ty=GetUnitY(master)
local real angle
local real dist
set dist=SquareRoot(Pow(tx-GetUnitX(summon),2) + Pow(ty-GetUnitY(summon),2))
if (dist > UnitGuardUnitAI_maxdistance() ) and (o == 0 or o == OrderId("attack") or o == OrderId("move") or o == OrderId("stop") or o==851971) then
call IssuePointOrder(summon, "move", tx,ty)
elseif dist >= UnitGuardUnitAI_cometomindist() and (o == 0 or o == 851971) then
set angle = GetRandomReal( GetUnitFacing(master)-80, GetUnitFacing(master)+80) * IntegerTertiaryOp( ModuloInteger( GetUnitPointValue(summon), 2) == 0,1,-1)
set dist = GetRandomReal(UnitGuardUnitAI_cometomindist(), UnitGuardUnitAI_cometomaxdist() )
if not IssuePointOrder(summon, "attack", tx+dist*CosBJ(angle), ty+dist*SinBJ(angle) ) then
call IssuePointOrder(summon, "move", tx+dist*CosBJ(angle), ty+dist*SinBJ(angle) )
endif
endif
set summon=null
set master=null
endfunction
function UnitGuardUnitAI_Smart_Order_Actions takes nothing returns nothing
local location target
local real angle
local real dist
local unit summon
local unit master
if GetIssuedOrderId() != 851971 then
return
endif
set summon=GetTriggerUnit()
set master=HandleToUnit( GetHandleHandle(summon, "master") )
loop
exitwhen GetUnitCurrentOrder(summon) != 851971
call GuardUnitOrder( summon, master)
call TriggerSleepAction(0)
endloop
set master=null
set summon=null
endfunction
function UnitGuardUnitAI_loop takes nothing returns nothing
// Executed in another thread
local unit summon=bj_ghoul[0]
local unit master=HandleToUnit( GetHandleHandle(summon, "master") )
local trigger smart=CreateTrigger()
local integer n=0
call TriggerAddAction( smart, function UnitGuardUnitAI_Smart_Order_Actions)
call TriggerRegisterUnitEvent( smart, summon, EVENT_UNIT_ISSUED_TARGET_ORDER )
call TriggerRegisterUnitEvent( smart, summon, EVENT_UNIT_ISSUED_POINT_ORDER )
loop
exitwhen IsUnitDeadBJ(summon)
call GuardUnitOrder( summon, master)
call PolledWait( GetRandomReal( UnitGuardUnitAI_mindelay(), UnitGuardUnitAI_maxdelay() ))
endloop
call FlushHandleLocals(summon)
call DestroyTrigger(smart)
set summon=null
set master=null
set smart=null
endfunction
function UnitGuardUnitAI takes unit summon, unit master returns nothing
local trigger ailoop=CreateTrigger()
local unit a=bj_ghoul[0]
call SetHandleHandle( summon, "master", master)
call TriggerAddAction(ailoop, function UnitGuardUnitAI_loop) //*Make UnitGuardUnitAI_loop to be executed in another thread
set bj_ghoul[0]=summon
call TriggerExecute(ailoop)
set bj_ghoul[0]=a
call DestroyTrigger(ailoop)
set a=null
set master=null
set summon=null
set ailoop=null
endfunction
像这种的 |
|