library AI initializer Init
globals
public group HEROES
private group swap = CreateGroup()
private group temp
//this is the place to set the distance away that the AI "clicks".
//should be larger than width of map frame (trees cliffs etc') so that point hits outside map.
//bc this function can only detect when clicked point is out of bounds, not blocked.
private real rundistance = 800.
private location leftpoint
private location rightpoint
//might need to use the two above to avoid a lot of leaking points.
endglobals
//==================== Here I turn away from walls ==========================
private function NotWall takes location targetp , location runnerp returns location
local real targetx = GetLocationX(targetp)
local real targety = GetLocationY(targetp)
local real rightx
local real righty
//local location rightpoint
local integer rightcounter
local real leftx
local real lefty
//local location leftpoint
local integer leftcounter
local real angle
if RectContainsLoc(GetPlayableMapRect(),targetp) then
return targetp
else
//here I choose the shorter angle:
set rightcounter = 0
set leftcounter = 0
set angle = AngleBetweenPoints(runnerp,targetp)
loop
exitwhen RectContainsLoc(GetPlayableMapRect(),rightpoint)
set rightcounter = rightcounter + 1
set angle = angle + 30.
call RemoveLocation(rightpoint)
set rightpoint = null
//set rightx = null
//set righty = null
set rightpoint = PolarProjectionBJ(runnerp,rundistance,angle)
set rightx = GetLocationX(rightpoint)
set righty = GetLocationY(rightpoint)
endloop
set angle = 0.
set angle = AngleBetweenPoints(runnerp,targetp)
//the left side:
loop
exitwhen RectContainsLoc(GetPlayableMapRect(),leftpoint)
set leftcounter = leftcounter + 1
//here is the distance which should be a constant so I should just change the number here:
set angle = angle - 30.
call RemoveLocation(leftpoint)
set leftpoint = null
//set leftx = null
//set lefty = null (I commented these bc I understand it's not necessary)
set leftpoint = PolarProjectionBJ(runnerp,rundistance,angle)
set leftx = GetLocationX(rightpoint)
set lefty = GetLocationY(rightpoint)
endloop
if rightcounter > leftcounter then
call RemoveLocation(rightpoint)
set rightpoint = null
return leftpoint
else
//leak removal bla bla
call RemoveLocation(leftpoint)
set leftpoint = null
return rightpoint
endif
//can I remove leaks after return statement or are those lines not read?
endif
endfunction
//============================================================================================
private function isthreat takes unit u returns boolean
if u == udg_Dragon then
return true
else
return IsUnitType(u, UNIT_TYPE_HERO)
endif
endfunction
//=============================================================================================
private function Flee takes unit fleer returns location
//this need to find who is in range, and accordingly decide how to run.
local location fleeloc = GetUnitLoc(fleer)
local real fleerx = GetLocationX(fleeloc)
local real fleery = GetLocationY(fleeloc)
local location targetpoint
local real angle
local group threatgroup = CreateGroup()
local group tempgroup = CreateGroup()
local unit FOG = null
local real threatrange = 1000.
local integer counter = 0
local unit threat1
local unit threat2
///now need to check for threats in range:
call GroupEnumUnitsInRange(threatgroup,fleerx,fleery,threatrange,null)
loop
set FOG = FirstOfGroup(threatgroup)
exitwhen FOG==null
if isthreat(FOG) then
set counter = counter + 1
call GroupAddUnit(tempgroup,FOG)
endif
call GroupRemoveUnit(threatgroup,FOG)
endloop
if CountUnitsInGroup(tempgroup) == 1 then
set FOG = FirstOfGroup(tempgroup)
set angle = AngleBetweenPoints(fleeloc,GetUnitLoc(FOG))
elseif CountUnitsInGroup(tempgroup) == 2 then
set threat1 = FirstOfGroup(tempgroup)
call GroupRemoveUnit(tempgroup,threat1)
set threat2 = FirstOfGroup(tempgroup)
call GroupRemoveUnit(tempgroup,threat2)
set angle = ((AngleBetweenPoints(fleeloc,GetUnitLoc(threat1))) + (AngleBetweenPoints(fleeloc,GetUnitLoc(threat2))))/2.
elseif CountUnitsInGroup(tempgroup) > 2 then
//eventually this should find the biggest threat and run from it. for now just random.
if IsUnitInGroup(udg_Dragon,tempgroup)then
set angle = AngleBetweenPoints(fleeloc,GetUnitLoc(udg_Dragon))
else
set FOG = FirstOfGroup(tempgroup)
set angle = AngleBetweenPoints(fleeloc,GetUnitLoc(FOG))
endif
endif
set targetpoint = PolarProjectionBJ(fleeloc,-1*(rundistance),angle)
//cleanup:
loop
exitwhen FirstOfGroup(tempgroup) == null
call GroupRemoveUnit(tempgroup,FirstOfGroup(tempgroup))
endloop
set threat1 = null
set threat2 = null
set FOG = null
call DestroyGroup(threatgroup)
call DestroyGroup(tempgroup)
return targetpoint
endfunction
//==========================================================================================
private function mainAI takes nothing returns nothing
local unit FOG
loop
//call BJDebugMsg("mainAI") - this one went off... the rest didn't
set FOG = FirstOfGroup(HEROES)
exitwhen FOG == null
call BJDebugMsg("exited loop")
///starting the actual function...
if UnitHasItem(FOG,udg_Crown)then
if GetUnitLifePercent(FOG)<50. then
if IsUnitInRange(FOG,udg_Dragon,800.)then
call UnitDropItemPointLoc(FOG,udg_Crown,GetUnitLoc(FOG))
endif
else
call IssuePointOrderLoc(FOG,"smart",Flee(FOG))
endif
else
call BJDebugMsg("dont havit")
if IsUnitInRange(FOG,udg_Dragon,1500.)then
call IssuePointOrderLoc(FOG,"smart",Flee(FOG))
else
if udg_Carrier != FOG then
call IssueTargetOrderBJ( FOG, "attack", udg_Carrier )
else
call IssueTargetItemOrder( FOG, "smart", udg_Crown )
call BJDebugMsg("oredered to get crown")
endif
endif
endif
///relevant function till here
call GroupAddUnit(swap,FOG)
call GroupRemoveUnit(HEROES,FOG)
endloop
set temp = HEROES
set HEROES = swap
set swap = temp
call BJDebugMsg("finished AI loop")
endfunction
//==============================================================
private function Init takes nothing returns nothing
local trigger AItrig = CreateTrigger()
call TriggerRegisterTimerEventPeriodic(AItrig,1.)
call TriggerAddAction(AItrig, function mainAI )
endfunction
endlibrary