找回密码
 点一下
查看: 1484|回复: 1

求几个好的jass的ai函数

[复制链接]
发表于 2012-10-5 12:09:56 | 显示全部楼层 |阅读模式
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
像这种的
发表于 2012-10-22 13:40:25 | 显示全部楼层
原来还有人在Jass区发帖啊...注意不到

记得老狼有个佣兵AI系统
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-12-22 16:00 , Processed in 0.098034 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表