includefilename = "asset\\composition\\AIScript\\LordAICommon.lua" 
includefilename = SYSTEM.RelativeToAbsolutePath(includefilename)
dofile(includefilename)

function Init ()
  SIGHT_DISTANCE = 300
end

function Update ()

function _IsArmyNearMe(myPositionX, myPositionY, AllyPositionX, AllyPositionY)
	ArmyDif = MAP.GetDistanceBetweenTwoPosition( myPositionX, myPositionY, AllyPositionX, AllyPositionY)
	if ArmyDif > 1000 then
		return false
	else
		return true
	end
end

  ARMY.SetBehavior(myArmyID, "Idle")

--myID = AI.GET_MY_ARNY_ID()
  myPositionX, myPositionY = ARMY.GetPosition(myArmyID)

-- Initialize

ClosestTowerID = ARMY.GetClosestID(myArmyID, "CAN_CAPTURE_TOWER")
ClosestTowerPositionX, ClosestTowerPositionY = TOWER.GetPosition(ClosestTowerID)

ClosestPowerUPID = ARMY.GetClosestID(myArmyID, "POWERUP")
ClosestPowerUPPositionX, ClosestPowerUPPositionY = POWERUP.GetPosition(ClosestPowerUPID)


TowerDif = MAP.GetDistanceBetweenTwoPosition( myPositionX, myPositionY,  ClosestTowerPositionX, ClosestTowerPositionY)
PowerUPDif = MAP.GetDistanceBetweenTwoPosition( myPositionX, myPositionY,  ClosestPowerUPPositionX, ClosestPowerUPPositionY)

-- Check Sight

SIGHT_DISTANCE = 1000

if  TowerDif > SIGHT_DISTANCE then
	ClosestTowerID = -1
end 

if  PowerUPDif > SIGHT_DISTANCE then
	ClosestPowerUPID = -1
end 

-- check get powerUP or Capture tower

if ClosestPowerUPID == -1 and ClosestTowerID == -1 then
  local CanClosestTowerID = ARMY.GetClosestID(myArmyID, "CAN_CAPTURE_TOWER")
  ARMY.SetBehavior(myArmyID, "Capturing")
	ARMY.SetGoal(myArmyID, "TOWER", CanClosestTowerID)
elseif ClosestPowerUPID == -1 then
  ARMY.SetBehavior(myArmyID, "Capturing")
	ARMY.SetGoal(myArmyID, "TOWER", ClosestTowerID)
elseif ClosestTowerID == -1 then
  ARMY.SetBehavior(myArmyID, "GetPowerUP")
	ARMY.SetGoal(myArmyID, "POWERUP", ClosestPowerUPID)
elseif TowerDif > PowerUPDif  then
  ARMY.SetBehavior(myArmyID, "GetPowerUP")
	ARMY.SetGoal(myArmyID, "POWERUP", ClosestPowerUPID)
else
  ARMY.SetBehavior(myArmyID, "Capturing")
	ARMY.SetGoal(myArmyID, "TOWER", ClosestTowerID)
end

-- Enemy initialize
EnemyID = ARMY.GetClosestID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY")
EnemyPositionX, EnemyPositionY = ARMY.GetPosition(EnemyID)
EnemyDif = MAP.GetDistanceBetweenTwoPosition( myPositionX, myPositionY, EnemyPositionX, EnemyPositionY)
EnemyShape = ARMY.GetShape(EnemyID)
EnemyHP = ARMY.GetHP(EnemyID)

if  EnemyDif > SIGHT_DISTANCE then
	EnemyID = -1
end 

--DEBUG.ShowMessage(EnemyDif)

if EnemyID ~= -1 then
	AllyID = ARMY.GetClosestID(myArmyID, "ALLY_ARMY")
	AllyShape = ARMY.GetShape(AllyID)
	AllyHP = ARMY.GetHP(AllyID)
	AllyPositionX, AllyPositionY = ARMY.GetPosition(AllyID)

	AllyState = "";
	if AllyID ~= -1 then
		if GAMERULE.IsShapeStronger(EnemyShape, AllyShape ) then
			AllyState = "Escape";
		elseif GAMERULE.IsShapeSame(EnemyShape, AllyShape ) then
			if ARMY.GetHPPercent(AllyID) > 0.6 then
				if _IsArmyNearMe(myPositionX, myPositionY, AllyPositionX, AllyPositionY) then
					AllyState = "Aggressive"
				else
					AllyState = "Neutral"
				end
			else
				if _IsArmyNearMe(myPositionX, myPositionY, AllyPositionX, AllyPositionY) then
					AllyState = "Neutral"
				else
					AllyState = "Escape"
				end
			end
		elseif GAMERULE.IsShapeWeaker(EnemyShape, AllyShape ) then
			if ARMY.GetHPPercent(AllyID) > 0.3 then
				AllyState = "Aggressive"
			elseif _IsArmyNearMe() then
				AllyState = "Aggressive"
			else
				AllyState = "Neutral"
			end
		end
	end

	if AllyState == "Aggressive" then
	  ARMY.SetBehavior(myArmyID, "AttackLord")
		ARMY.SetGoal(myArmyID, "ARMY", EnemyID)
	elseif AllyState == "Neutral" then
	  ARMY.SetBehavior(myArmyID, "AttackLord")
		ARMY.SetGoal(myArmyID, "ARMY", EnemyID)
	end

end
end 
